nh2 / static-haskell-nix

easily build most Haskell programs into fully static Linux executables
388 stars 36 forks source link

Document example failure of glibc static linking with NSS somewhere #17

Open nh2 opened 5 years ago

nh2 commented 5 years ago

I got this example segfault of somebody trying to use an executable statically linked with glibc and using the network.

The stacktrace is of the program crashing in gdb; stack build ... --no-strip was used to obtain a sensible stack trace, otherwise it would show just in ?? () in the entire trace.

Thread 11 "myprogram:w" received signal SIGSEGV, Segmentation fault.
[Switching to LWP 494]
0x00007ffff43857a0 in internal_getent (stream=stream@entry=0x7ffff4059680, result=result@entry=0x368b1a0 <resbuf.11135>, buffer=buffer@entry=0x36e7a20 "# Internet (IP) protocols\n", buflen=buflen@entry=1024, errnop=errnop@entry=0x7fffe67fc6a0)
    at nss_files/files-XXX.c:216
216    nss_files/files-XXX.c: No such file or directory.
(gdb) bt
#0  0x00007ffff43857a0 in internal_getent (stream=stream@entry=0x7ffff4059680, result=result@entry=0x368b1a0 <resbuf.11135>, buffer=buffer@entry=0x36e7a20 "# Internet (IP) protocols\n", buflen=buflen@entry=1024, errnop=errnop@entry=0x7fffe67fc6a0)
    at nss_files/files-XXX.c:216
#1  0x00007ffff4385a32 in _nss_files_getprotobyname_r (name=0x420021c800 "tcp", result=0x368b1a0 <resbuf.11135>, buffer=0x36e7a20 "# Internet (IP) protocols\n", buflen=1024, errnop=0x7fffe67fc6a0) at nss_files/files-proto.c:38
#2  0x0000000003026f47 in getprotobyname_r ()
#3  0x0000000003026d18 in getprotobyname ()
#4  0x0000000002208aee in ?? ()
#5  0x0000000000000000 in ?? ()

Should probably document that somewhere as an example for why you may want to use musl here.

nh2 commented 4 years ago

The glibc warning:

Hello.hs:

main = putStrLn "hello world"
% ghc --make Hello.hs -optl-static
[1 of 1] Compiling Main             ( Hello.hs, Hello.o )
Linking Hello ...

rts/Linker.c:601:0: error:
     warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
    |
601 |    hdl = dlopen(dll_name, RTLD_LAZY|RTLD_LOCAL); /* see Note [RTLD_LOCAL] */
    | ^
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libnuma.a(affinity.o):function affinity_ip: warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
cstrahan commented 4 years ago

See https://stackoverflow.com/questions/57476533/why-is-statically-linking-glibc-discouraged/57478728#57478728

Two options:

nh2 commented 2 years ago

Another example of static glibc breakage: https://old.reddit.com/r/haskell/comments/vqqq7x/trying_to_build_a_statically_linked_binary/