- 论坛徽章:
- 2
|
我在32位的ubuntu1604上面做了个很小的实验:
- $ cat g.s
- .section .text
- .globl _start
- _start:
- movl $1,%eax
- int $0x80
复制代码 只是一个exit调用而已,别的什么也没有。
- $ as g.s -o g.o && ld g.o -o g
- $ g
复制代码 没有问题。但是如果我试图链接-lc就会有问题。
- $ as g.s -o g.o && ld g.o -o g -lc
- $ g
复制代码 bash: ./g: 没有那个文件或目录
我本来想尝试一下在汇编代码里面去调用call printf/call exit这样的c函数的。
但是发现即使什么也不掉用,光是链接的话,都会导致生成的2进制程序无法运行。
这看起来非常奇怪,我ldd看了一下,都能找到的:
- $ ldd g
- linux-gate.so.1 => (0xb7756000)
- libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb7582000)
- /usr/lib/libc.so.1 => /lib/ld-linux.so.2 (0x8003e000)
复制代码 为什么运行就不行了? ./g运行也是不行,同样的错。
- 我最后尝试了一下带着调试信息来编译,并且用gdb来运行,如下所示:
- $ as -g g.s -o g.o && ld g.o -o g -lc
- $ gdb g
- ...
- (gdb) list
- 1 .section .text
- 2 .globl _start
- 3 _start:
- 4 movl $1,%eax
- 5 int $0x80
- (gdb) b 3
- Breakpoint 1 at 0x80480ff: file g.s, line 3.
- (gdb) r
- Starting program: /home/x/cpp/g
- /bin/bash: /home/x/cpp/g: 没有那个文件或目录
- During startup program exited with code 127.
复制代码 看起来,gdb能够抓到这个错误,也就是During startup program existed。刚启动就退出了,shell还没有来得及执行它,所以shell说找不到这个程序,对吧。
但是原因是什么呢? 大侠给点解释吧,我觉得从gas汇编的程序去链接c运行时库应该是个很普通的场景啊,这个错误如何更正?
|
|