Bazel-built Shared C Libraries and the GHCi linker
Nobody has claimed this yet.
- Dominant language
- Starlark
- Stars
- 279
- Forks
- 93
- Avg merge
- 13d 11h
- Merged PRs (30d)
- 3
Description
Let’s say I have two cc_librarys a and b where a depends on b. The shared libraries built by Bazel underlink, i.e., the shared library doesn’t declare that it depends on other shared libraries instead this is tracked internally by Bazel.
If I make a Haskell library that depends on a, I will end up with extra-libraries: a b in my package config. The GHCi linker (triggered by TH) will go through those libraries and call dlopen(libname, RTLD_LAZY|RTLD_LOCAL). It respects the order in extra-libraries so it will first try to load a. If you are lucky, RTLD_LAZY works and despite a referencing b. However, this won’t work in general as RTLD_LAZY only works for function references and even then it can be overridden by things like -Wl,-z,now which, to make matters worse, is passed by default by Bazel. In that case dlopen will error out with an undefined symbol error and builds fail.
Sadly, I don’t know a nice solution to this:
- Changing the order in
extra-librarieswill break static linking afaict since in that caseashould come first. linkstatic = Trueworks sometimes but it is annoying to have to patch upstream rules and the GHCi linker is also fairly fragile (currently it’s failing for me on the official grpc library patched withlinkstatic = Truewithlibgpr_base.a: unhandled ELF relocation(RelA) type 19).- Get Bazel to properly declare dependencies of the shared library. Then the system linker should handle this properly. This seems fairly hard to do atm, see https://github.com/bazelbuild/bazel/issues/492.
I don’t have a self-contained test case that I can make public right now but we’re seeing this with the official grpc library, i.e., "@com_github_grpc_grpc//:grpc" which we get viahttp_archive`:
http_archive(
name = "com_github_grpc_grpc"
strip_prefix = "grpc-1.19.0",
urls = ["https://github.com/grpc/grpc/archive/v1.19.0.tar.gz"],
sha256 = "1d54cd95ed276c42c276e0a3df8ab33ee41968b73af14023c03a19db48f82e73",
)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the shared C libraries produced by Bazel for the official grpc target in the linked http_archive, then trace how the Haskell package's extra-libraries are loaded by GHCi through dlopen. A useful result would be a self-contained reproduction and a confirmed approach that avoids the undefined-symbol failure without breaking static linking.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, grpc, haskell
- Domain
- build-system, compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100