Paul Molodowitch
Paul Molodowitch

Reputation: 1446

Tell which version of symbols are available for linking against (in libc)?

Ok, so I want to link against a lower version of libc / glibc, for compatibility. I noticed this answer about how to do this, on a case-by-case basis:

How can I link to a specific glibc version? https://stackoverflow.com/a/2858996/920545

However, when I tried to apply this myself, I ran into problems, because I can't figure out what lower-version-number I should use to link against. Using the example in the answer, if I use "nm" to inspect the symbols provided by my /lib/libc.so.6 (which, in my case, is a link to libc-2.17.so), I see that it seems to provide versions 2.0 and 2.3 of realpath:

> nm /lib/libc.so.6 | grep realpath@
4878d610 T realpath@@GLIBC_2.3
48885c20 T realpath@GLIBC_2.0

However, if I try to link against realpath@GLIBC_2.0:

__asm__(".symver realpath,realpath@GLIBC_2.0");

...i get an error:

> gcc -o test_glibc test_glibc.c
/tmp/ccMfnLmS.o: In function `main':
test_glibc.c:(.text+0x25): undefined reference to `realpath@GLIBC_2.0'
collect2: error: ld returned 1 exit status

However, using realpath@GLIBC_2.3 works... and the code from the example, realpath@GLIBC_2.2.5 works - even though, according to nm, no such symbol exists. (FYI, if I compile without any __asm__ instruction, then inspect with nm, I see that it linked against realpath@GLIBC_2.3, which makes sense; and I confirmed that linking to realpath@GLIBC_2.2.5 works.)

So, my question is, how the heck to I know which version of the various functions I can link against? Or even which are available? Are there some other kwargs I should be feeding to nm? Am I inspecting the wrong library?

Thanks!

Upvotes: 0

Views: 2582

Answers (1)

thkala
thkala

Reputation: 86333

It seems to me that you have mixed up your libraries and binaries a bit...

/lib/libc.so.6 on most Linux distributions is a 32-bit shared object and should contain the *@GLIBC_2.0 symbols. If you are on an x86_64 platform, though, I would expect GCC to produce an 64-bit binary by default. 64-bit binaries are generally linked against /lib64/libc.so.6, which would not contain compatibility symbols for an old glibc version like 2.0 - the x86_64 architecture did not even exist back then...

Try compiling your *@GLIBC_2.0 program with the -m32 GCC flag to force linking against the 32-bit C library.

Upvotes: 1

Related Questions