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

Fix linker errors on Ubuntu >= 18.04 with GHC bindist #1388

Merged
merged 4 commits into from
Jul 10, 2020
Merged

Conversation

aherrmann
Copy link
Member

Closes #1349

On some more recent Linux distributions (e.g. Ubuntu 18.04) gcc defaults to linking with -pie. At the same time GHC passes -r to the linker when joining object files. In order to avoid errors of the form

 /usr/bin/ld: -r and -pie may not be used together

we need to pass -no-pie to the linker. Additionally, GHC compiles the main function during linking with non-position independent code. Attempting to link this code with -pie will cause the following error:

/usr/bin/ld.gold: error: /tmp/ghc3_0/ghc_2.o: requires dynamic R_X86_64_32 reloc against 'ZCMain_main_closure' which may overflow at runtime; recompile with -fPIC
/usr/bin/ld.gold: error: ../../../../../external/rules_haskell_ghc_linux_amd64/lib/base-4.13.0.0/libHSbase-4.13.0.0.a(Base.o): requires unsupported dynamic reloc 11; recompile with -fPIC

This was not caught on rules_haskell's bindist CI since it was still running on Ubuntu 16.04 which hadn't enabled -pie by default, yet. I've upgraded the rules_haskell bindist CI image to Ubuntu 18.04. I've confirmed that this does not break the build on Ubuntu 16.04.

aherrmann added 3 commits July 7, 2020 12:17
Covers R_X86_64_32 relocation errors and incompatibility of `-r` and
`-pie` on Ubuntu>=18.04.
@@ -112,9 +113,6 @@ def cc_interop_info(ctx):
cc_files = ctx.files._cc_toolchain + cc_wrapper.inputs.to_list()
cc_manifests = cc_wrapper.manifests

# XXX Workaround https://github.com/bazelbuild/bazel/issues/6876.
linker_flags = [flag for flag in linker_flags if flag not in ["-shared"]]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While debugging #1349 I noticed that this could be simplified by following the suggestion from bazelbuild/bazel#6876 (comment).

Copy link
Contributor

@hanshoglund hanshoglund left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good as a fix for the Ubuntu issue, but setting no-pie everywhere seems like a bit of a sledgehammer. It would be good to get more clarity on the upstream issue.

haskell/private/actions/link.bzl Outdated Show resolved Hide resolved
haskell/cabal.bzl Outdated Show resolved Hide resolved
@dcreager
Copy link

dcreager commented Jul 9, 2020

We were seeing this on github/semantic#595, and I can confirm that this patch fixes our Linux builds.

@aherrmann
Copy link
Member Author

setting no-pie everywhere seems like a bit of a sledgehammer. It would be good to get more clarity on the upstream issue.

As pointed out in this GHC note that corresponds to GHC's behavior provided that the C compiler supports the -no-pie flag. As pointed out in 26cc476 it's not so straight-forward to implement such autodetection in the case of rules_haskell. If necessary we could make this user-configurable or if really necessary perform auto-detection in a GHC wrapper.

@aherrmann aherrmann added the merge-queue merge on green CI label Jul 10, 2020
@mergify mergify bot merged commit abaec65 into master Jul 10, 2020
@mergify mergify bot deleted the fix-bindist branch July 10, 2020 10:27
@mergify mergify bot removed the merge-queue merge on green CI label Jul 10, 2020
mboes added a commit that referenced this pull request Sep 16, 2020
When using GHC 8.10, passing `-no-pie` to the linker when building
libraries leads to obscure linker errors. Since PIE pertains to
executables, it doesn't make much sense passing that flag when
building libraries anyways. Removing it, both for `haskell_library`
and `haskell_cabal_library` fixes GHC 8.10 support. We make sure to
keep the flag for executables. This would otherwise negate the effect
of #1388.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rules_haskell buildkite broken
3 participants