1

I provide the linker with my files: ld-2.7.so, libc-2.7.so and crt1.o but it fails to compile successfully. Is it possible to link to a different glibc from the default one ?

(Static compilation or installing a separate glibc is not an option.)

gcc -Wl,-dynamic-linker,ld-2.7.so,libc-2.7.so,crt1.o -nostdlib program.c

crt1.o: In function `_start':  
(.text+0x12): undefined reference to `__libc_csu_fini'  
crt1.o: In function `_start':  
(.text+0x19): undefined reference to `__libc_csu_init'  
/tmp/user/1000/ccauFlwt.o: In function `findsize':  
program.c:(.text+0x21): undefined reference to `stat'  
/tmp/user/1000/ccauFlwt.o: In function `findtime':  
program.c:(.text+0x4c): undefined reference to `stat'  
collect2: ld returned 1 exit status  
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Neeladri Vishweswaran
  • 1,695
  • 5
  • 22
  • 40
  • Stab in the dark: you might try listing the libraries twice in the command line: `-Wl,-dynamic-linker,ld-2.7.so,libc-2.7.so,crt1.o,ld-2.7.so,libc-2.7.so,crt1.o`. – bstpierre Nov 04 '10 at 19:33
  • `__libc_csu_init` and `fini` are probably some C++ constructor/destructor junk in other `crt*.o` files that the glibc `crt1.o` uselessly wants to pull in even for C programs. You could try to find the stuff to make it work, or just disassemble `crt1.o` (it's tiny) and replace it with your own version that doesn't do any of that junk. – R.. GitHub STOP HELPING ICE Nov 04 '10 at 21:43
  • Same when using `ld` directly: http://stackoverflow.com/questions/6656317/linking-a-c-program-directly-with-ld-fails-with-undefined-reference-to-libc-c – Ciro Santilli OurBigBook.com Jun 08 '15 at 12:55

3 Answers3

2

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

Neeladri Vishweswaran
  • 1,695
  • 5
  • 22
  • 40
1

As @onemasse said, you also need header files compatible with that version of libc. Between glibc versions things still work anyway though, but this can not relied upon.

Otherwise, I would suggest you use export LD_PRELOAD_PATH to wherever your other libc is before starting the new library.

But what you really is after, is to cross-compile in linux, for linux, but with another libc. Have a look at crosstool.

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
0

If you want to link to another libc than the one provided by the system you also need to compile your program with header files compatible with that version of libc.

Also, I'm not familiar with the option "-dynamic-linker". It's not in the man page for gcc. I really can't work out what you're trying to do.

Maybe you should try this instead and work from there:

gcc -ldl program.c -o program

You normally don't have to explicitly link with "libc" or "crt1.o".

onemasse
  • 6,514
  • 8
  • 32
  • 37