코드에서 glibc 버전 알아내기

Posted at 2008/08/21 21:19 // in Programming // by Daniel

http://www.technovelty.org/linux/tips/glibc-version.html

실행중에 libc 버전을 봐야 할 경우, contstr을 쓰면 된다고 합니다

#include <stdio.h>

#include <unistd.h>

#include <alloca.h>

#include <string.h>


int main (void)

{

    size_t n = confstr (_CS_GNU_LIBC_VERSION, NULL, 0);

    if (n > 0)

    {

        char *buf = alloca (n);

        confstr (_CS_GNU_LIBC_VERSION, buf, n);

        printf("%s\n", buf);

    }

    return 0;

}

 

실행해 보면
 $ ./a.out
glibc 2.7

이렇게 나옵니다.
또는 돌리는 코드상에서가 아니고 콘솔에서 보고 싶다면 /lib/libc.so.6 파일을 실행해봐도 됩니다.
$ /lib/libc.so.6
GNU C Library stable release version 2.7, by Roland McGrath et al.
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.2.3 (Ubuntu 4.2.3-2ubuntu7).
Compiled on a Linux >>2.6.24-14-server<< system on 2008-04-04.
Available extensions:
    crypt add-on version 2.1 by Michael Glad and others
    GNU Libidn by Simon Josefsson
    Native POSIX Threads Library by Ulrich Drepper et al
    BIND-8.2.3-T5B
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.

그리고 LD_DEBUG를 이용하면 많은 정보를 볼 수 있습니다.

$ LD_DEBUG=help ls
Valid options for the LD_DEBUG environment variable are:

  libs        display library search paths
  reloc       display relocation processing
  files       display progress for input file
  symbols     display symbol table processing
  bindings    display information about symbol binding
  versions    display version dependencies
  all         all previous options combined
  statistics  display relocation statistics
  unused      determined unused DSOs
  help        display this help message and exit

To direct the debugging output into a file instead of standard output
a filename can be specified using the LD_DEBUG_OUTPUT environment variable.


예를 들어 아까의 a.out을 실행하면

$ LD_DEBUG=versions ./a.out
      1565:     checking for version `GLIBC_2.4' in file /lib/tls/i686/cmov/libc.so.6 [0] required by file ./a.out [0]
      1565:     checking for version `GLIBC_2.0' in file /lib/tls/i686/cmov/libc.so.6 [0] required by file ./a.out [0]
      1565:     checking for version `GLIBC_PRIVATE' in file /lib/ld-linux.so.2 [0] required by file /lib/tls/i686/cmov/libc.so.6 [0]
      1565:     checking for version `GLIBC_2.3' in file /lib/ld-linux.so.2 [0] required by file /lib/tls/i686/cmov/libc.so.6 [0]
      1565:     checking for version `GLIBC_2.1' in file /lib/ld-linux.so.2 [0] required by file /lib/tls/i686/cmov/libc.so.6 [0]
      1565:
      1565:     calling init: /lib/tls/i686/cmov/libc.so.6
      1565:
      1565:
      1565:     initialize program: ./a.out
      1565:
      1565:
      1565:     transferring control: ./a.out
      1565:
glibc 2.7
      1565:
      1565:     calling fini: ./a.out [0]
      1565:
      1565:
      1565:     calling fini: /lib/tls/i686/cmov/libc.so.6 [0]
      1565:

이런 식으로 나옵니다.
크리에이티브 커먼즈 라이센스
Creative Commons License

이 글에는 트랙백을 보낼 수 없습니다

  1. gamilla

    2008/08/22 10:36 [수정/삭제] [답글]

    ^^ 알고싶던 정보였는데... 감사합니다~

  2. Daniel

    2008/08/24 02:16 [수정/삭제] [답글]

    댓글 감사합니다.
    뭘요 제가 필요해서 적은건데,, 도움이 됐다니 좋네요