c - 使用 ld 链接已编译的程序集和 C 文件
问题描述
我已经编译了这些程序:
BITS 16
extern _main
start:
mov ax, 07C0h
add ax, 288
mov ss, ax
mov sp, 4096
mov ax, 07C0h
mov ds, ax
mov si, text_string
call print_string
jmp $
text_string db 'Calling Main Script'
call _main
print_string:
mov ah, 0Eh
.repeat:
lodsb
cmp al, 0
je .done
int 10h
jmp .repeat
.done:
ret
times 510-($-$$) db 0
dw 0xAA55
这是一个测试,只是为了尝试链接它们
int main()
{
return 0;
}
两者都可以自己编译完全正常使用:
gcc -Wall -m32 main.c
nasm -f elf bootloader.asm
但是我无法使用链接它们:
ld bootloader.o main.o -lc -I /lib/Id-linux.so.2
并且我收到此错误:
ld: i386 architecture of input file `bootloader.o' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `main.o' is incompatible with i386:x86-64 output
ld: warning: cannot find entry symbol _start; defaulting to 0000000000401000
ld: bootloader.o: file class ELFCLASS32 incompatible with ELFCLASS64
ld: final link failed: file in wrong format
任何帮助都会非常感谢
解决方案
GCC 默认已经动态链接libc
,所以如果你想手动链接ld
,请确保你的 ELF 可执行static
,你可以通过-static
标志传递。
gcc -o <filename> <filename>.c -static -Wall -m32
然后链接ld -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o <filename> -lc <filename>.o
我想,由于像 NASM 这样的汇编程序具有静态(不带 的独立libc
),您可以使用 直接使 ELF 动态可执行,您可以使用标志libc
传递。-dynamic-linker
例如 :
x86
nasm -f elf32 -o <filename>.o <filename>.asm
ld -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o <filename> -lc <filename>.o
x86_64
nasm -f elf64 -o <filename>.o <filename>.asm
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o <filename> -lc <filename>.o
推荐阅读
- ios - 如何在 XCode 上为 Float 放置 TextField 的内容类型?
- ios - 我在哪里可以找到应用内订阅的取消日期?
- canvas - 如何在 Apache NiFi 中管理、排序和过滤大量流?
- python - 从 STEP 文件中提取参数方程
- ios - 安装 GraphQL iOS 框架
- javascript - Colspan 以匹配上方根据屏幕尺寸隐藏 td 的行
- javascript - 使用回调时从函数中获取空数组
- c - 为什么在 x64dbg 中加载 .exe 文件时找不到“_main”符号?
- android - 无法查看在 android studio 的设计视图中删除的文本视图
- java - 使用比较器,如何确定对象之间的匹配质量,或者有更好的方法