3
votes

I need an help!! I am trying to build a standalone executable ie without ANY dynamic linking.

I wrote a small test program, generated a relocatable object file for it called test.o. When I try to build the standalone executable using GNU linker I get the below error:

$ld -static -o test test.o /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/libc.a /usr/lib/gcc/i486-linux-gnu/4.4/libgcc.a /usr/lib/gcc/i486-linux-gnu/4.4/libgcc_eh.a /usr/lib/gcc/i486-linux-gnu/4.4/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function _Unwind_Find_FDE': (.text+0x190b): undefined reference todl_iterate_phdr'

How to resolve the undefined symbol dl_iterate_phdr. In which archive this symbol is present?

Thanks!!!

EDIT1:

Just in case if I am not very clear, my motive is to generate a standalone executable ie an executable which is completely ready for execution while it gets loaded into memory i.e.) all symbol resolution and relocation is done by program linker itself instead of dynamic linker. Is it possible to generate such an executable?

FINAL UPDATE:

Now I got it to get complied with ld directly using the below command:

$ld -static -o test /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/gcc/i486-linux-gnu/4.4.3/crtbeginT.o /usr/lib/gcc/i486-linux-gnu/4.4.3/crtend.o test.o --start-group /usr/lib/gcc/i486-linux-gnu/4.4.3/libgcc.a /usr/lib/gcc/i486-linux-gnu/4.4.3/libgcc_eh.a /usr/lib/libc.a --end-group

man ld says --start-group archives --endgroup is used to resolve circular references!! Also i find symbol dl_iterate_phdr is defined in libc.a.

Thanks all for your help!!

3
Try and add -ldl to your linker flagsfge
Thanks!! -ldl resolves the undefined symbol error. But to generate a standalone executable I dont want to dynamically link any libraries. So I tried statically linking libdl.a but still I get the same undefined symbol error.Bala
What's your OS and gcc version? And did you try to link with gcc instead of ld directly?Chris
Chris, Im running on ubuntu and gcc's version is 4.4.3. Offcourse when I try to link with gcc it does work. But I am looking to use ld since I wanted to build a standalone executable. Any help please!!Bala

3 Answers

4
votes

When I try to build the standalone executable using GNU linker

Don't. Use of ld to link any user-space program is most often a bug. Your link line is certainly incorrect.

Use compiler driver to do the heavy lifting for you. This should work:

gcc -static -o test test.o

I am looking to use ld since I wanted to build a standalone executable

What makes you believe that GCC-built executable is less stand-alone than ld-built one? Whatever it is, you are mistaken: gcc simply invokes ld with correct arguments.

3
votes

If you're getting this error when targeting android, you need to link against libdl.so (-ldl)

0
votes

gcc -o main main.c -L . -static-libgcc -Wl,-static -lhello -lc