无法运行与libc链接的可执行文件

5 linux assembly x86-64 nasm ld

我用以下命令组装我的hello世界:

nasm -f elf64 test.asm

然后我链接到这个:

ld -s test.o -lc

我知道这是有效的,因为我看到file a.out

a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), stripped

然而当我运行这个./a.out我得到bash: ./a.out: No such file or directory

没有libc的链接运行正常!如何让我的libc链接exe运行?

Jes*_*ter 12

眼前的问题是,ld使用/lib/ld64.so.1默认的解释,你可能丢失的是(它可以是一个符号链接/lib64/ld-linux-x86-64.so.2或任何为宜):

$ readelf -l a.out | grep interpreter
      [Requesting program interpreter: /lib/ld64.so.1]
$ ls -l /lib/ld64.so.1
ls: cannot access /lib/ld64.so.1: No such file or directory
Run Code Online (Sandbox Code Playgroud)

您可以通过将-dynamic-linker /lib64/ld-linux-x86-64.so.2选项传递给ld调用来显式设置解释器来解决此问题:

$ ld -s -dynamic-linker /lib64/ld-linux-x86-64.so.2 test.o -lc
$ readelf -l a.out | grep interpreter
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
$ ./a.out
$
Run Code Online (Sandbox Code Playgroud)

然而,简单的经验法则是gcc用于链接,如果你需要libc,它将为你做一切.还要确保将其main用作入口点,以便正常的libc启动代码有可能进行初始化.同样,只需从main最后返回,不要exit直接使用系统调用(exit如果你真的需要,可以使用libc中的函数).通常,不建议使用系统调用.