LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   gcc: link to a different libc file (https://www.linuxquestions.org/questions/linux-software-2/gcc-link-to-a-different-libc-file-804759/)

kinkle 04-28-2010 08:46 AM

gcc: link to a different libc file
 
I want to supply the shared libs 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 like this:

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 successfuly link libc-xxx.so. How can I do that ?

CoderMan 04-28-2010 02:18 PM

Quote:

Originally Posted by kinkle (Post 3950607)
I want to supply the shared libs 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 like this:

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 successfuly link libc-xxx.so. How can I do that ?

I've never tried that before. But can you use the -nostdlib option and then specify your standard libraries explicitly?

From GCC(1):

Code:

      -nostdlib
          Do not use the standard system startup files or libraries when linking.  No
          startup files and only the libraries you specify will be passed to the linker.
          The compiler may generate calls to "memcmp", "memset", "memcpy" and "memmove".
          These entries are usually resolved by entries in libc.  These entry points
          should be supplied through some other mechanism when this option is specified.

          One of the standard libraries bypassed by -nostdlib and -nodefaultlibs is
          libgcc.a, a library of internal subroutines that GCC uses to overcome shortcom‐
          ings of particular machines, or special needs for some languages.

          In most cases, you need libgcc.a even when you want to avoid other standard
          libraries.  In other words, when you specify -nostdlib or -nodefaultlibs you
          should usually specify -lgcc as well.  This ensures that you have no unresolved
          references to internal GCC library subroutines.  (For example, __main, used to
          ensure C++ constructors will be called.)

Just out of curiousity, what version differences are you concerned about?

kinkle 04-28-2010 03:10 PM

Quote:

Originally Posted by CoderMan (Post 3950969)
I've never tried that before. But can you use the -nostdlib option and then specify your standard libraries explicitly?

I tried:
gcc -std=c99 -D_POSIX_C_SOURCE=200112L -O2 -m32 -s -Wl,-dynamic-linker,ld-2.7.so -L. -lc-2.7 -nostdlib -lgcc pig.c

but it returns:

/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000008048ad0
Quote:

Originally Posted by CoderMan (Post 3950969)
Just out of curiousity, what version differences are you concerned about?

I want to avoid linux-distribution differences.

All I want is to run my program in a very old CentOS but my program in compiled in modern Debian. A much easier solution would be to simply compile my program in CentOS but I do not have permission to do this. So, I was thinking of making my program use the libc and ld files of Debian instead of the ones provided by CentOS.

Is this not possible ?

CoderMan 04-29-2010 12:34 AM

Maybe I missing something here... but if you are so concerned about portability that you are willing to install your own standard libs, why don't you just do static linking only, and compile everything directly into the binary?

kinkle 04-29-2010 02:46 AM

Quote:

Originally Posted by CoderMan (Post 3951402)
Maybe I missing something here... but if you are so concerned about portability that you are willing to install your own standard libs, why don't you just do static linking only, and compile everything directly into the binary?

Because getaddrinfo cannot be statically compiled. Plus, the resulting binary is too large compared to the shared method.

I have included the header netdb.h, where getaddrinfo is included, but gcc issues this warning:

warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

gcc -m32 -static -s -O2 -std=c99 -D_POSIX_C_SOURCE=200112L myprogram.c

Valery Reznic 04-29-2010 08:45 AM

Quote:

Originally Posted by kinkle (Post 3951020)
I want to avoid linux-distribution differences.

All I want is to run my program in a very old CentOS but my program in compiled in modern Debian. A much easier solution would be to simply compile my program in CentOS but I do not have permission to do this. So, I was thinking of making my program use the libc and ld files of Debian instead of the ones provided by CentOS.

Is this not possible ?

It's depend. Run on your debian command
Code:

/sbin/ldconfig -p | grep libc.so.6
You should get something like this:
Code:

        libc.so.6 (libc6,x86-64, OS ABI: Linux 2.6.18) => /lib64/libc.so.6
        libc.so.6 (libc6, hwcap: 0x0018000000000000, OS ABI: Linux 2.6.18) => /lib/i686/nosegneg/libc.so.6
        libc.so.6 (libc6, OS ABI: Linux 2.6.18) => /lib/libc.so.6

If kernel version on your CentOS is < than MINIMAL OS ABI version in your debian - you are out of luck. There is no way, program build on that debian can be run on this centos.

In this case your only option - install same ancient centos somewhere (let say in VM), build your program there and copy it to the "production" centos

If kernel version on your CentOS is >= than MINIMAL OS ABI version in your debian you have following options:
1. Same as above - create VM with a same centos and build there.
2. Build your program on debian as usual, without any tricks and then use
Ermine - http://magicErmine.com or
statifier - http://statifier.sf.net

to create from your dynamically linked executable self-contained one.


Statifier is licensed under GPL, Ermine is commercial.
On the other hand Ermine works better than statifier on systems with
memory randomization.


All times are GMT -5. The time now is 04:10 PM.