11

I want to supply the shared libraries along with my program rather than using the target system's due to version differences.

ldd says my program uses these shared libs:

linux-gate.so.1 =>  (0xf7ef0000)**(made by kernel)**  
libc.so.6 => /lib32/libc.so.6 (0xf7d88000)**(libc-2.7.so)**  
/lib/ld-linux.so.2 (0xf7ef1000)**(ld-2.7.so)**

I have successfully linked ld-xxx.so by compiling with:

gcc -std=c99 -D_POSIX_C_SOURCE=200112L -O2 -m32 -s -Wl,-dynamic-linker,ld-2.7.so myprogram.c

But I have not managed to successful link libc-xxx.so. How can I do that ?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Neeladri Vishweswaran
  • 1,695
  • 5
  • 22
  • 40
  • 1
    You may want to consider statically linking your program if you only have one executable. This way no one will accidentally lose your version of libc and if they remove your program they won't have your libc lingering. – nategoose Apr 28 '10 at 17:26
  • Same but statically: http://stackoverflow.com/questions/10763394/how-to-build-a-c-program-using-a-custom-version-of-glibc – Ciro Santilli OurBigBook.com Jun 08 '15 at 20:14

2 Answers2

15

I found out how to do it:

rpath specifies where the provided libraries are located. This folder should contain: libc.so.6, libdl.so.2, libgcc_s.so.1 and maybe more. Check with strace to find out which libraries your binary file uses.

ld.so is the provided linker

gcc -Xlinker -rpath=/default/path/to/libraries -Xlinker -I/default/path/to/libraries/ld.so program.c

vasek
  • 2,759
  • 1
  • 26
  • 30
Neeladri Vishweswaran
  • 1,695
  • 5
  • 22
  • 40
9

Passing -nodefaultlibs or -nostdlib to gcc will tell it to not pass the default libraries as arguments to ld. You will then be able to explicitly specify the libc you want to link against. See the gcc(1) man page for more details and caveats regarding both options.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358