Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GHC Static linking does not work #3193

Closed
codygman opened this issue Jul 6, 2014 · 8 comments
Closed

GHC Static linking does not work #3193

codygman opened this issue Jul 6, 2014 · 8 comments

Comments

@codygman
Copy link
Contributor

codygman commented Jul 6, 2014

Marked this as a bug, because currently there is no way (easy or that I know of) to compile Haskell programs on nixos that work on other linuxes such as Ubuntu, Debian,or Red Hat.

I was of the opinion that:

ghc -static static-test.hs

Would give me a statically linked binary which works on *nix or most of them. However it seems that it uses nix specific linking even with -static specified:

[cody@cody-nixos:~/tmp]$ ghc -static static-test.hs
[1 of 1] Compiling Main             ( static-test.hs, static-test.o )
Linking static-test ...

[cody@cody-nixos:~/tmp]$ ldd static-test
    linux-vdso.so.1 (0x00007ffff33b3000)
    libgmp.so.10 => /nix/store/wxkad81khi8gzxs8kppq5yqkki5hy0np-gmp-5.1.3/lib/libgmp.so.10 (0x00007fb28261e000)
    libm.so.6 => /nix/store/pdskwizjw8ar31hql2wjnnx6g0s6xc50-glibc-2.19/lib/libm.so.6 (0x00007fb28231d000)
    librt.so.1 => /nix/store/pdskwizjw8ar31hql2wjnnx6g0s6xc50-glibc-2.19/lib/librt.so.1 (0x00007fb282115000)
    libdl.so.2 => /nix/store/pdskwizjw8ar31hql2wjnnx6g0s6xc50-glibc-2.19/lib/libdl.so.2 (0x00007fb281f11000)
    libgcc_s.so.1 => /nix/store/9fb027n5a2lf9qwpb98v77iycqq552pf-gcc-4.8.2/lib64/libgcc_s.so.1 (0x00007fb281cfb000)
    libc.so.6 => /nix/store/pdskwizjw8ar31hql2wjnnx6g0s6xc50-glibc-2.19/lib/libc.so.6 (0x00007fb28194e000)
    libpthread.so.0 => /nix/store/pdskwizjw8ar31hql2wjnnx6g0s6xc50-glibc-2.19/lib/libpthread.so.0 (0x00007fb281730000)
    /nix/store/pdskwizjw8ar31hql2wjnnx6g0s6xc50-glibc-2.19/lib/ld-linux-x86-64.so.2 (0x00007fb2828a1000)

I then learned about -optl-static and tried it:

[cody@cody-nixos:~/tmp]$ ghc -optl-static static-test.hs
Linking static-test ...
/nix/store/xjvdpqgn2kd4rg0k30z020kxylvlbpx0-binutils-2.23.1/bin/ld: cannot find -lgmp
/nix/store/1jp3vsjcl8ydiy92lzyjclwr943vh5lx-ghc-7.6.3/lib/ghc-7.6.3/libHSrts.a(Linker.o): In function `addDLL':
Linker.c:(.text+0x1a19): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
collect2: error: ld returned 1 exit status
@vcunat
Copy link
Member

vcunat commented Jul 6, 2014

Ah, I didn't notice. See #3194.

peti pushed a commit to peti/nixpkgs that referenced this issue Jul 10, 2014
… Haskell binaries

Pass '-optl-static -optl-pthread' to GHC to enable static linking.

Fixes <NixOS#3193>. Further details can be
found at <http://permalink.gmane.org/gmane.linux.distributions.nixos/13526>.
peti pushed a commit to peti/nixpkgs that referenced this issue Jul 12, 2014
… Haskell binaries

Pass '-optl-static -optl-pthread' to GHC to enable static linking.

Fixes <NixOS#3193>. Further details can be
found at <http://permalink.gmane.org/gmane.linux.distributions.nixos/13526>.
@codygman
Copy link
Contributor Author

I've been following the changes here, I'll try the updates out as soon as I can.

@peti
Copy link
Member

peti commented Jul 14, 2014

The current master branch supports static linking now.

@peti peti closed this as completed Jul 14, 2014
@codygman
Copy link
Contributor Author

Great, I got a simple example working, however when trying to statically compile a more complicated example:

/nix/store/bzvdg7g74rkw7p3zw3mqpp7ynnq4mxf1-binutils-2.23.1/bin/ld: cannot find -lpcre
/nix/store/bzvdg7g74rkw7p3zw3mqpp7ynnq4mxf1-binutils-2.23.1/bin/ld: cannot find -lmysqlclient
/nix/store/bzvdg7g74rkw7p3zw3mqpp7ynnq4mxf1-binutils-2.23.1/bin/ld: cannot find -lssl
/nix/store/bzvdg7g74rkw7p3zw3mqpp7ynnq4mxf1-binutils-2.23.1/bin/ld: cannot find -lcrypto
/nix/store/hb92nfw5iapmrgy7c12h04vhp6yqb85b-ghc-7.8.3/lib/ghc-7.8.3/rts-1.0/libHSrts.a(Linker.o): In function `internal_dlopen':
Linker.c:(.text+0x5c): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/nix/store/hb92nfw5iapmrgy7c12h04vhp6yqb85b-ghc-7.8.3/lib/ghc-7.8.3/unix-2.7.0.1/libHSunix-2.7.0.1.a(HsUnix.o): In function `__hsunix_getpwent':
HsUnix.c:(.text+0xb1): warning: Using 'getpwent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/nix/store/hb92nfw5iapmrgy7c12h04vhp6yqb85b-ghc-7.8.3/lib/ghc-7.8.3/unix-2.7.0.1/libHSunix-2.7.0.1.a(HsUnix.o): In function `__hsunix_getpwnam_r':
HsUnix.c:(.text+0xc1): warning: Using 'getpwnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/nix/store/hb92nfw5iapmrgy7c12h04vhp6yqb85b-ghc-7.8.3/lib/ghc-7.8.3/unix-2.7.0.1/libHSunix-2.7.0.1.a(HsUnix.o): In function `__hsunix_getpwuid_r':
HsUnix.c:(.text+0xd1): warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/nix/store/zg76yaqw5jyqfpj87ndkyl8swsdw3bmg-haskell-network-ghc7.8.3-2.5.0.0-shared/lib/ghc-7.8.3/network-2.5.0.0/libHSnetwork-2.5.0.0.a(HsNet.o): In function `hsnet_getaddrinfo':
(.text+0x21): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/nix/store/zg76yaqw5jyqfpj87ndkyl8swsdw3bmg-haskell-network-ghc7.8.3-2.5.0.0-shared/lib/ghc-7.8.3/network-2.5.0.0/libHSnetwork-2.5.0.0.a(BSD__266.o):(.text+0x7f): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/nix/store/zg76yaqw5jyqfpj87ndkyl8swsdw3bmg-haskell-network-ghc7.8.3-2.5.0.0-shared/lib/ghc-7.8.3/network-2.5.0.0/libHSnetwork-2.5.0.0.a(BSD__248.o): In function `c1q6p_info':
(.text+0xbd): warning: Using 'getprotobyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/nix/store/zg76yaqw5jyqfpj87ndkyl8swsdw3bmg-haskell-network-ghc7.8.3-2.5.0.0-shared/lib/ghc-7.8.3/network-2.5.0.0/libHSnetwork-2.5.0.0.a(BSD__231.o): In function `c1pYC_info':
(.text+0xb2): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
collect2: error: ld returned 1 exit status

I'm guessing I need to have the following libraries: pcre, mysqlclient, ssl, crypto

@codygman
Copy link
Contributor Author

According to a stackoverflow commenter, staticly linked programs are much less portable (is this true?)

http://stackoverflow.com/questions/8657908/deploying-yesod-to-heroku-cant-build-statically#answer-8658468

So would it be better for a program that does something like the following:

[cody@cody-nixos:~]$ ldd hello
    linux-vdso.so.1 (0x00007fffde5fb000)
    libgmp.so.10 => /nix/store/rgm3f9ih66b9b726kf1n1g4xx0gnv3rq-gmp-5.1.3/lib/libgmp.so.10 (0x00007f152fea5000)
    libm.so.6 => /nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19/lib/libm.so.6 (0x00007f152fba4000)
    librt.so.1 => /nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19/lib/librt.so.1 (0x00007f152f99c000)
    libdl.so.2 => /nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19/lib/libdl.so.2 (0x00007f152f798000)
    libc.so.6 => /nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19/lib/libc.so.6 (0x00007f152f3eb000)
    libpthread.so.0 => /nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19/lib/libpthread.so.0 (0x00007f152f1cd000)
    /nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19/lib/ld-linux-x86-64.so.2 (0x00007f1530128000)
[cody@cody-nixos:~]$ patchfor ubuntu hello
Patched hello to work on ubuntu
[cody@cody-nixos:~]$ ldd hello
    linux-vdso.so.1 =>  (0x00007fffbebfe000)
    libgmp.so.10 => /usr/lib/x86_64-linux-gnu/libgmp.so.10 (0x00007f27db51c000)
    libffi.so.6 => /usr/lib/x86_64-linux-gnu/libffi.so.6 (0x00007f27db314000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f27db00d000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f27dae05000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f27dac01000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f27da83a000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f27da61c000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f27db79a000)

Hm, notice the nix example doesn't have libffi.

@peti
Copy link
Member

peti commented Jul 16, 2014

Yes, if you want to link pcre and friends statically, then you need static versions of those libraries.

@vcunat
Copy link
Member

vcunat commented Jul 16, 2014

@codygman: the key thing was that statically linking glibc might be a problem, as far as I understood the post. For the other libraries it should be fine, I think, and increasing portability.

@nh2
Copy link
Contributor

nh2 commented Jun 22, 2018

To link fully statically (using musl libc instead of glibc), see https://github.com/nh2/static-haskell-nix/ for an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants