haskell / cabal

Official upstream development repository for Cabal and cabal-install
https://haskell.org/cabal
Other
1.61k stars 691 forks source link

`ld-options` are passed to GCC when linking hsc2hs object files #8824

Open NicolasT opened 1 year ago

NicolasT commented 1 year ago

It appears that, when a library specifies some ld-options, these ld-options are passed to gcc when it's invoked to link some object files generated as part of some the pipeline to turn a hsc file into a hs file, i.e., when the C code generated by hsc2hs (which should output the final hs file) gets linked.

This is counter-intuitive, and (IMHO) incorrect: any ld-options set by some library should be used to link said library into some Haskell executable, not when hsc2hs is used by some project to link the hsc2hs-generated program.

In my case, my library specifies -Wl,-wrap,sigfillset as ld-options, and the library itself does provide the (then required) __wrap_sigfillset symbol. However, when -Wl,-wrap,sigfillset is passed to the gcc invocation of the hsc2hs pipeline, on some platforms, build fails since my library is not linked into the generator executable (and indeed, it should not be). This leaves __wrap_sigfillset undefined, which on some platforms is OK (if there's no reference), but on some platforms it's not (there, libgcc seems to use sigfillset internally).

Looking at the failing command (see below), I wonder whether most of these options should be used when linking the hsc2hs generator program at all: this program should not use any Haskell libraries at all, so why should all paths to Haskell libraries be passed in (-L...), and rpaths set (-Wl,-R,...), or link against libdl or libgmp simply because the RTS or, e.g., ghc-bignum would rely on this if they were linked in?

To reproduce, clone https://github.com/NicolasT/landlock-hs.git, checkout tag psx-0.1.1.1 and run cabal test psx-test on some system exposing the issue, e.g., in the docker.io/haskell:9.2-slim container image.

The issue can not be reproduced on Fedora 36, or in the buildpack-deps:jammy container image (which is used in the package CI).

Cabal version: 3.8.1.0 (in the docker.io/haskell:9.2-slim container image) GHC version: 9.2.6 (in the docker.io/haskell:9.2-slim container image) GCC version: 8.3.0 (in the docker.io/haskell:9.2-slim container image)

Error output when running in `docker.io/haskell:9.2-slim`, formatted for readability ``` linking /root/landlock-hs/dist-newstyle/build/x86_64-linux/ghc-9.2.6/psx-0.1.1.1/t/psx-test/build/psx-test/psx-test-tmp/TestCases_hsc_make.o failed (exit code 1) rsp file was: "/root/landlock-hs/dist-newstyle/build/x86_64-linux/ghc-9.2.6/psx-0.1.1.1/t/psx-test/build/psx-test/psx-test-tmp/hsc2hscall2211-2.rsp" command was: /usr/bin/gcc /root/landlock-hs/dist-newstyle/build/x86_64-linux/ghc-9.2.6/psx-0.1.1.1/t/psx-test/build/psx-test/psx-test-tmp/TestCases_hsc_make.o /root/landlock-hs/dist-newstyle/build/x86_64-linux/ghc-9.2.6/psx-0.1.1.1/t/psx-test/build/psx-test/psx-test-tmp/TestCases_hsc_utils.o -o /root/landlock-hs/dist-newstyle/build/x86_64-linux/ghc-9.2.6/psx-0.1.1.1/t/psx-test/build/psx-test/psx-test-tmp/TestCases_hsc_make -fuse-ld=gold -fuse-ld=gold -L/root/.cabal/store/ghc-9.2.6/tasty-hunit-0.10.0.3-e084648d17d32bb1abbd3ca537bd5bfff80727c23a787ec9695937b69e4adbb4/lib -Wl,-R,/root/.cabal/store/ghc-9.2.6/tasty-hunit-0.10.0.3-e084648d17d32bb1abbd3ca537bd5bfff80727c23a787ec9695937b69e4adbb4/lib -L/root/.cabal/store/ghc-9.2.6/tasty-1.4.3-36c4a01033ce624e9acfedecd08f66aad2bad45a58c4f4ec849003f8d3baaace/lib -Wl,-R,/root/.cabal/store/ghc-9.2.6/tasty-1.4.3-36c4a01033ce624e9acfedecd08f66aad2bad45a58c4f4ec849003f8d3baaace/lib -L/root/.cabal/store/ghc-9.2.6/tagged-0.8.7-7c2435cb682e1e73c62bea70d092c856605bd94d7df319394ac29194ef0885c6/lib -Wl,-R,/root/.cabal/store/ghc-9.2.6/tagged-0.8.7-7c2435cb682e1e73c62bea70d092c856605bd94d7df319394ac29194ef0885c6/lib -L/root/landlock-hs/dist-newstyle/build/x86_64-linux/ghc-9.2.6/psx-0.1.1.1/build -Wl,-R,/root/landlock-hs/dist-newstyle/build/x86_64-linux/ghc-9.2.6/psx-0.1.1.1/build -Wl,-wrap,sigfillset -pthread -Wl,-wrap,pthread_create -L/root/.cabal/store/ghc-9.2.6/optparse-applicative-0.17.0.0-e4401672759d72ca14eedbb73e3812732d66d3a9669f6434ef3f862715755af2/lib -Wl,-R,/root/.cabal/store/ghc-9.2.6/optparse-applicative-0.17.0.0-e4401672759d72ca14eedbb73e3812732d66d3a9669f6434ef3f862715755af2/lib -L/root/.cabal/store/ghc-9.2.6/transformers-compat-0.7.2-44f55ef23e9de21af01f9df06371964a4eef112547c085a9c360895e34da8c78/lib -Wl,-R,/root/.cabal/store/ghc-9.2.6/transformers-compat-0.7.2-44f55ef23e9de21af01f9df06371964a4eef112547c085a9c360895e34da8c78/lib -L/opt/ghc/9.2.6/lib/ghc-9.2.6/transformers-0.5.6.2 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/transformers-0.5.6.2 -L/opt/ghc/9.2.6/lib/ghc-9.2.6/process-1.6.16.0 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/process-1.6.16.0 -L/opt/ghc/9.2.6/lib/ghc-9.2.6/directory-1.3.6.2 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/directory-1.3.6.2 -L/opt/ghc/9.2.6/lib/ghc-9.2.6/unix-2.7.2.2 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/unix-2.7.2.2 -lrt -lutil -ldl -lpthread -L/opt/ghc/9.2.6/lib/ghc-9.2.6/time-1.11.1.1 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/time-1.11.1.1 -L/root/.cabal/store/ghc-9.2.6/call-stack-0.4.0-8d6df4e568b29c932fefe0a48f52e214ed5075419c264ff8613ecd8ab4d3e9e3/lib -Wl,-R,/root/.cabal/store/ghc-9.2.6/call-stack-0.4.0-8d6df4e568b29c932fefe0a48f52e214ed5075419c264ff8613ecd8ab4d3e9e3/lib -L/root/.cabal/store/ghc-9.2.6/async-2.2.4-afac1822daeb88db40777c7d5eb36cd8644e63b88e20f3003c1c555eb2d1be16/lib -Wl,-R,/root/.cabal/store/ghc-9.2.6/async-2.2.4-afac1822daeb88db40777c7d5eb36cd8644e63b88e20f3003c1c555eb2d1be16/lib -L/opt/ghc/9.2.6/lib/ghc-9.2.6/stm-2.5.0.2 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/stm-2.5.0.2 -L/root/.cabal/store/ghc-9.2.6/hashable-1.4.2.0-219a6123a837b28f223d0cb19a87b11bc91fe8ce450c51a52f616cdd71344a6b/lib -Wl,-R,/root/.cabal/store/ghc-9.2.6/hashable-1.4.2.0-219a6123a837b28f223d0cb19a87b11bc91fe8ce450c51a52f616cdd71344a6b/lib -L/opt/ghc/9.2.6/lib/ghc-9.2.6/text-1.2.5.0 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/text-1.2.5.0 -L/opt/ghc/9.2.6/lib/ghc-9.2.6/binary-0.8.9.0 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/binary-0.8.9.0 -L/opt/ghc/9.2.6/lib/ghc-9.2.6/filepath-1.4.2.2 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/filepath-1.4.2.2 -L/root/.cabal/store/ghc-9.2.6/data-array-byte-0.1.0.1-19afe996f4ddff3223a47504ed51afc89f5e0694562791e69a27a9cc24b1588a/lib -Wl,-R,/root/.cabal/store/ghc-9.2.6/data-array-byte-0.1.0.1-19afe996f4ddff3223a47504ed51afc89f5e0694562791e69a27a9cc24b1588a/lib -L/opt/ghc/9.2.6/lib/ghc-9.2.6/containers-0.6.5.1 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/containers-0.6.5.1 -L/opt/ghc/9.2.6/lib/ghc-9.2.6/bytestring-0.11.4.0 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/bytestring-0.11.4.0 -L/opt/ghc/9.2.6/lib/ghc-9.2.6/template-haskell-2.18.0.0 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/template-haskell-2.18.0.0 -L/opt/ghc/9.2.6/lib/ghc-9.2.6/pretty-1.1.3.6 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/pretty-1.1.3.6 -L/opt/ghc/9.2.6/lib/ghc-9.2.6/ghc-boot-th-9.2.6 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/ghc-boot-th-9.2.6 -L/opt/ghc/9.2.6/lib/ghc-9.2.6/deepseq-1.4.6.1 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/deepseq-1.4.6.1 -L/opt/ghc/9.2.6/lib/ghc-9.2.6/array-0.5.4.0 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/array-0.5.4.0 -L/root/.cabal/store/ghc-9.2.6/ansi-wl-pprint-0.6.9-94a0c40313bb735e3fa9c249ed82233819be30e73ca38bf207f432b531fd1602/lib -Wl,-R,/root/.cabal/store/ghc-9.2.6/ansi-wl-pprint-0.6.9-94a0c40313bb735e3fa9c249ed82233819be30e73ca38bf207f432b531fd1602/lib -L/root/.cabal/store/ghc-9.2.6/ansi-terminal-0.11.4-6c0b523a25787830691f9690ae618a71ca0225539bda16966e2bad411cb759c7/lib -Wl,-R,/root/.cabal/store/ghc-9.2.6/ansi-terminal-0.11.4-6c0b523a25787830691f9690ae618a71ca0225539bda16966e2bad411cb759c7/lib -L/root/.cabal/store/ghc-9.2.6/colour-2.3.6-71b19ed6a70a6899e8a7167d690e614939a12c4976261b7d380715a4d3500894/lib -Wl,-R,/root/.cabal/store/ghc-9.2.6/colour-2.3.6-71b19ed6a70a6899e8a7167d690e614939a12c4976261b7d380715a4d3500894/lib -L/opt/ghc/9.2.6/lib/ghc-9.2.6/base-4.16.4.0 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/base-4.16.4.0 -L/opt/ghc/9.2.6/lib/ghc-9.2.6/ghc-bignum-1.2 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/ghc-bignum-1.2 -lgmp -L/opt/ghc/9.2.6/lib/ghc-9.2.6/ghc-prim-0.8.0 -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/ghc-prim-0.8.0 -lc -lm -L/opt/ghc/9.2.6/lib/ghc-9.2.6/rts -Wl,-R,/opt/ghc/9.2.6/lib/ghc-9.2.6/rts -lm -lrt -ldl -lpthread error: /usr/lib/gcc/x86_64-linux-gnu/8/libgcc.a(generic-morestack.o):function __generic_morestack_set_initial_sp: error: undefined reference to '__wrap_sigfillset' ``` Note how ``` -Wl,-wrap,sigfillset -pthread -Wl,-wrap,pthread_create ``` which are the `ld-options` set by the `psx` library are passed through, which should not be the case.
gbaz commented 1 year ago

Need some people who understand linker and ffi issues well to comment here. @bgamari @mpickering

NicolasT commented 1 year ago

Friendly bump

NicolasT commented 6 months ago

Running into this again :disappointed: So another bump?