无法运行与libc链接的可执行文件 [英] Can't run executable linked with libc

查看:73
本文介绍了无法运行与libc链接的可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下命令组装世界:

I am assembling my hello world with the following command:

nasm -f elf64 test.asm

然后我用这个链接:

ld -s test.o -lc

我知道这可行,因为 file a.out 显示了我

I know this works because file a.out shows me

a.out:ELF 64位LSB可执行文件,x86-64,版本1(SYSV),动态链接(使用共享库),已剥离

但是,当我使用 ./a.out 运行它时,我会得到 bash:./a.out:没有这样的文件或目录

However when i run this with ./a.out i get bash: ./a.out: No such file or directory

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

When link without libc it runs fine! How can I get my libc linked exe to run?

推荐答案

直接的问题是 ld 默认使用/lib/ld64.so.1 作为解释器并且您可能会错过(它可能是指向/lib64/ld-linux-x86-64.so.2 或其他合适方法的符号链接):

The immediate problem is that ld uses /lib/ld64.so.1 as interpreter by default and you may be missing that (it can be a symlink to /lib64/ld-linux-x86-64.so.2 or whatever is appropriate):

$ 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

您可以通过将 -dynamic-linker/lib64/ld-linux-x86-64.so.2 选项传递给 ld 调用:

You can work around this by setting the interpreter explicitly by passing -dynamic-linker /lib64/ld-linux-x86-64.so.2 option to your ld invocation:

$ 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
$

简单的经验法则是,如果需要libc,则使用 gcc 进行链接,它将为您完成所有操作.还要确保使用 main 作为入口点,以便普通的libc启动代码有机会进行初始化.同样,只需从最后的 main 返回即可,不要直接使用 exit syscall(如果确实需要,可以使用libc的 exit 函数想).通常,不建议使用syscalls.

The simple rule of thumb however is to use gcc for linking if you need libc, it will do everything right for you. Also make sure you use main as entry point so that normal libc startup code has a chance of initializing. Similarly, just return from your main at the end and do not use exit syscall directly (you may use the exit function from libc if you really want). In general, using syscalls is not recommended.

这篇关于无法运行与libc链接的可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆