求助:如何使用ld链接这段汇编代码(solved)
本帖最后由 beyond_touch 于 2010-09-02 09:49 编辑这边这段汇编代码,我用gcc可以编译通过。但是用as/ld分开编译/链接就会出错.
$ make
as -g -o cpuid.o cpuid.s
ld -lc -o cpuid cpuid.o /usr/lib/crt1.o /usr/lib/crti.o
/usr/lib/crt1.o: In function `_start':
(.text+0x12): undefined reference to `__libc_csu_fini'
/usr/lib/crt1.o: In function `_start':
(.text+0x19): undefined reference to `__libc_csu_init'
make: *** Error 1
.data
buffer:
.asciz "The Processor Vendor ID is '%s'\n\0"
.text
.globl main
main:
pushq %rbp
movq %rsp, %rbp
subq $32, %rsp
movl %edi, -4(%rbp)
movq %rdi, -16(%rbp)
xorl %eax, %eax
cpuid
movl %ebx, -32(%rbp)
movl %edx, -28(%rbp)
movl %ecx, -24(%rbp)
movl $0, -20(%rbp)
leaq buffer(%rip), %rdi
leaq -32(%rbp), %rsi
movl $0, %eax
call printf
xorl %eax, %eax
leave
ret
AS=as
LD=ld
ASFLAGS=-g
LDFLAGS=-lc
WRAPPERS=crt1.o crti.o
OBJS=cpuid.o
vpath %.o /usr/lib
cpuid: $(OBJS) $(WRAPPERS)
$(LD) $(LDFLAGS) -o $@ $^
$(OBJS): %.o: %.s
$(AS) $(ASFLAGS) -o $@ $^
.PHONY: clean
clean:
rm -f cpuid *.o
先用如下命令,查看一下ld都额外链接了哪些库和文件。
$gcc -v -lc -o cpuid cpuid.o 你用gcc -dumpspecs看一下,在startfile那一节,有哪些.o参与了startfile的链接的 本帖最后由 beyond_touch 于 2010-09-02 09:49 编辑
谢谢楼上两位
:em17:真是绝了〜
ld --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2-o cpuid -lc /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/crtbegin.o cpuid.o /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/crtend.o /usr/lib/crtn.o
注意那个"-lc", 它只要出现在现在这个位置,或者更加靠前一点的位置,链接就会出错。
再靠后边一个位置就可以通过链接。
$ ld -v
GNU ld (GNU Binutils) 2.20.1.20100303
真是TM绝了:em27:
修改之后的MakefileAS=as
LD=ld
ASFLAGS=-g
LDFLAGS=-m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc
WRAPPERS=crt1.o crti.o crtbegin.o crtend.o crtn.o
OBJS=cpuid.o
vpath %.o /usr/lib /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3
cpuid: $(OBJS) $(WRAPPERS)
$(LD) -o $@ $^ $(LDFLAGS)
$(OBJS): %.o: %.s
$(AS) -o $@ $^ $(ASFLAGS)
.PHONY: clean
clean:
rm -f cpuid *.o
个人觉得,凡是用ld的地方,不妨换成gcc,这样就可以把额外要链接的文件交给gcc自己来处理。
而且,as可以换成gcc -S。
页:
[1]