Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
970 views
in Technique[技术] by (71.8m points)

linux - Can't run executable linked with libc

I am assembling my hello world with the following command:

nasm -f elf64 test.asm

I then link with this:

ld -s test.o -lc

I know this works because file a.out shows me

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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
$

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...