Linking a dynamically linked executable with ld

和自甴很熟 提交于 2019-12-20 01:52:14

问题


i'm trying to create a dynamically linked executable (elf_i386) without gcc. The program is very simple (only a printf)...here the commands:

$ gcc -c simple.c
$ ld -o simple -dynamic-linker /lib/ld-linux.so.2 --entry main /usr/lib/crt1.o /usr/lib/crti.o simple.o -lc /usr/lib/crtn.o

The executable is created and also file command and ldd command show the right output... However when i launch the program after the call to printf i get a segmentation fault...i've examined with objdump the executable and i think the problem is about the dtors...seems that compiling with:

$gcc -o simple simple.c

a section .dtors is present while it is not present inside the executable created directly with ld :(

Any ideas?


回答1:


Lose the --entry main. main isn't your entry point, _start is. Try this:

$ gcc -c hello.c
$ ld -o hello -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o hello.o -lc /usr/lib/crtn.o
$ ./hello
hello, world
$ 



回答2:


It is not necessary to include the C run time environment i guess unless you are using return from your main().

We can strip the CRT and just link using :

ld -o hello -lc -dynamic-linker /lib/ld-linux.so.2 hello.o -e main

Will work.



来源:https://stackoverflow.com/questions/5821237/linking-a-dynamically-linked-executable-with-ld

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!