This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Statically linked binary & NSS


I would like to create a statically linked binary but it seems that
the -static flag is not enough. The problem seems to come from glibc
dynamically loading some libnss_* libraries, depending on the contents
of /etc/nsswitch.conf.

Here is my test program:
    #include <stdio.h>
    #include <pwd.h>
    main()
    {
      struct passwd *p;
      p = getpwnam("root");
      if (p == NULL) {
    	printf("no root!\n");
      } else {
    	printf("root is %d\n", p->pw_uid);
      }
    }

# gcc -v
Reading specs from /usr/local/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.91.57/specs
gcc version egcs-2.91.57 19980901 (egcs-1.1 release)
# gcc -static -o test test.c
# ./test
root is 0
# ldd ./test
        not a dynamic executable

So far so good but this still loads dynamic libraries:

# strace ./test
...
open("/etc/nsswitch.conf", O_RDONLY)    = 3
...
open("/lib/libnss_files.so.1", O_RDONLY) = 3

And this fails in a chroot jail:

# mkdir -p /tmp/jail/etc
# cp /etc/passwd /tmp/jail/etc
# cp /etc/nsswitch.conf /tmp/jail/etc
# cp test /tmp/jail
# chroot /tmp/jail /test
no root!
# strace chroot /tmp/jail /test
...
open("/etc/nsswitch.conf", O_RDONLY)    = 3
...
open("/lib/libnss_files.so.1", O_RDONLY) = -1 ENOENT (No such file or directory)
...

So, how can I create a really static binary? Are there some other
libraries dynamically loading files like this?

If this matters, I'm using Red Hat 5.1, glibc 2.0.7 and egcs 1.1.

Thanks in advance for any help...

________________________________________________________
Lionel Cons        http://wwwinfo.cern.ch/~cons
CERN               http://www.cern.ch
 
Boren's Laws:
	When in charge, ponder.
	When in trouble, delegate.
	When in doubt, mumble.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]