Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would it be impossible to fully statically link an application?

I'm trying to compile a statically linked binary with GCC and I'm getting warning messages like:

warning: Using 'getpwnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

I don't even know what getwnam_r does, but I assume it's getting called from inside some higher level API. I receive a similar message for gethostbyname.

Why would it not be possible to just statically link these functions in like every other function?

like image 443
dsimcha Avatar asked Nov 15 '11 17:11

dsimcha


People also ask

What are the disadvantages of static linking?

The major disadvantages of static linking are increases in the memory required to run an executable, network bandwidth to transfer it, and disk space to store it.

What does it mean to statically link?

Static linking means that the code for all routines called by your program becomes part of the executable file. Statically linked programs can be moved to run on systems without the XL Fortran runtime libraries.

When would you use a static link?

Static linking increases the file size of your program, and it may increase the code size in memory if other applications, or other copies of your application, are running on the system. This option forces the linker to place the library procedures your program references into the program's object file.

Why is the statically linked executable so much larger than the dynamically linked executable?

Static linking produces a larger executable file than dynamic linking because it has to compile all of the library code directly into the executable. The benefit is a reduction in overhead from no longer having to call functions from a library, and anywhere from somewhat to noticeably faster load times.


1 Answers

Function calls that need access to NSS or iconv need access will open other libs dynamically, since NSS needs plugins to work (the helper modules like pam_unix.so). When the NSS system dlopens these modules, there will be two conflicting versions of glibc - the one your program brought with it (statically compiled in), and the one dlopen()ed by NSS dependencies. Shit will happen.

This is why you can't build static programs using getpwnam_r and a few other functions.

like image 108
thiton Avatar answered Sep 21 '22 20:09

thiton