Checking for libc vs. glibc using Python

M.-A. Lemburg mal at lemburg.com
Thu Oct 21 07:29:33 EDT 1999


Charles G Waldman wrote:
> 
> M.-A. Lemburg writes:
>  > Is it possible to examine a Python interpreter and check whether
>  > it was compiled against libc5 or glibc2 (libc6) on Linux/*BSD/etc. ?
>  >
>  > I'm currently using this hack, but would appreciate a more
>  > elegant and portable solution:
> 
>  > def system_nm(progfile):
>  >     try:
>  >      f = os.popen('nm %s' % progfile)
>  >     except os.error:
>  >      return ''
>  >     return f.read()
> 
> I would use "ldd" instead of "nm".  It's another hack, not much more
> elegant, but a little more portable.  In particular it doesn't require
> that python was compiled -g.
> 
> I'm not sure how much the ldd output varies on different unices
> (haven't tried this on BSD), but I think it should be pretty easy to
> find a portable way search for the libc.so.* line in the output and
> figure out the libc version.
> 
> This will fail if libc is linked statically, as far as I know this is
> rarely done.  It's still less of a restriction than requiring python
> to be built with debugging info.
> 
> It's still not exactly elegant but possibly a little more portable than
> what you were doing.

True, "ldd" would work even when compiled without debugging infos.
Perhaps I could use it as fallback solution when "nm" doesn't
find anything.

What does the output of ldd look like for glibc and libc on other
platforms (e.g. Redhat, *BSD, etc.) ? On SuSE 6.2 Linux I get:

        libX11.so.6 => /usr/i486-linux-libc5/lib/libX11.so.6 (0x40014000)
        libdl.so.1 => /usr/i486-linux-libc5/lib/libdl.so.1 (0x400b5000)
        libm.so.5 => /usr/i486-linux-libc5/lib/libm.so.5 (0x400b8000)
        libc.so.5 => /usr/i486-linux-libc5/lib/libc.so.5 (0x400c1000)

for a program linked against libc5 and

        libc.so.6 => /lib/libc.so.6 (0x40021000)
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

for one linked against libc6 (glibc). Note that I can't deduce the
glibc version from the above output.

Isn't there some other way to find out ?

Hmm, just checking via grep: I could probably simply scan the binary file
for "libc.so.<version>" and "__libc_init" or "GLIBC_<version>" instead
of using the OS tools. That should work on both types of binaries,
shared and statically linked.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Y2000:                                                    71 days left
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/





More information about the Python-list mailing list