Skip to content

Commit a60c134

Browse files
committed
Add -dynamic when linking shared haskell_binary
This matches the behavior of Cabal when linking binaries. Without this flag, will GHC statically link every implicit dependency provided by the global package database. I've noticed a few strange behaviors caused by this, all involving `haskell_prebuilt_library`: - `examples/with_prelude/haskell` segfaults on macOS. The binary contains both statically and dynamically linked copies of `rts`, but only the static copy is initialized. The dynamic copy comes via `//haskell:library` (`haskell_library` passes `-dynamic`). Linux symbol namespacing would merge the two, but macOS doesn't. - if the global package database is limited to only `rts` (GHC is hard-coded to require this), linking fails on Linux using lld. Linking the static `rts` against a dynamic `base` would require creating copy relocations for read-only symbols in `base`. - if the global package database is limited as above, any resulting binary would throw a runtime linker error on macOS. On macOS, GHC sets `-dead_strip_dylibs` to remove unused dylibs. During static linking, symbols required by `base` are removed and the `rts` dylib is stripped as dead code. I can't find a reason for why the flag was originally excluded. Based on the issues it causes, including it seems like the correct behavior.
1 parent 9e72fd5 commit a60c134

2 files changed

Lines changed: 2 additions & 4 deletions

File tree

examples/with_prelude/test_utils.bzl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ def assert_output(name, command, output):
1717
def haskell_library(deps = [], **kwargs):
1818
native.haskell_library(deps = deps + ["//third-party/haskell:base"], **kwargs)
1919

20-
def haskell_binary(linker_flags = [], deps = [], **kwargs):
20+
def haskell_binary(deps = [], **kwargs):
2121
native.haskell_binary(
22-
# Workaround for as yet not triaged runtime segfault.
23-
linker_flags = linker_flags + ["-dynamic"] if host_info().os.is_macos else linker_flags,
2422
deps = deps + ["//third-party/haskell:base"],
2523
**kwargs,
2624
)

prelude/haskell/haskell.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ def haskell_binary_impl(ctx: AnalysisContext) -> list[Provider]:
11331133
sos_dir = "__{}__shared_libs_symlink_tree".format(ctx.attrs.name)
11341134
rpath_ref = get_rpath_origin(get_cxx_toolchain_info(ctx).linker_info.type)
11351135
rpath_ldflag = "-Wl,{}/{}".format(rpath_ref, sos_dir)
1136-
link.add("-optl", "-Wl,-rpath", "-optl", rpath_ldflag)
1136+
link.add("-dynamic", "-optl", "-Wl,-rpath", "-optl", rpath_ldflag)
11371137
symlink_dir = create_shlib_symlink_tree(
11381138
actions = ctx.actions,
11391139
out = sos_dir,

0 commit comments

Comments
 (0)