LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-28-2010, 08:46 AM   #1
kinkle
LQ Newbie
 
Registered: Mar 2009
Location: MN
Distribution: Debian squeeze
Posts: 13

Rep: Reputation: 3
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 ?
 
Old 04-28-2010, 02:18 PM   #2
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Blog Entries: 24

Rep: Reputation: 43
Quote:
Originally Posted by kinkle View Post
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?
 
Old 04-28-2010, 03:10 PM   #3
kinkle
LQ Newbie
 
Registered: Mar 2009
Location: MN
Distribution: Debian squeeze
Posts: 13

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by CoderMan View Post
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 View Post
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 ?

Last edited by kinkle; 04-28-2010 at 03:13 PM.
 
Old 04-29-2010, 12:34 AM   #4
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Blog Entries: 24

Rep: Reputation: 43
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?
 
Old 04-29-2010, 02:46 AM   #5
kinkle
LQ Newbie
 
Registered: Mar 2009
Location: MN
Distribution: Debian squeeze
Posts: 13

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by CoderMan View Post
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
 
Old 04-29-2010, 08:45 AM   #6
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 676

Rep: Reputation: 137Reputation: 137
Quote:
Originally Posted by kinkle View Post
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ERROR :version GLIBC_2.0 not defined in file libc.so.6 with link time reference gemini42 Slackware 10 12-13-2010 10:24 AM
ymbol errno, version GLIBC_2.0 not defined in file libc.so.6 with link time reference papodu Linux - Software 0 07-05-2008 05:35 PM
Link php to 32bit libc FirmbIT Programming 1 01-15-2007 11:38 PM
gcc i686 libc xiaoyuer18 Linux - Distributions 0 10-12-2005 05:19 PM
GCC Make error on Solarias 2.8: libc.a unknown file type vinaybms Solaris / OpenSolaris 1 06-20-2005 01:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration