Skip to content

feat: Support linking against shared libraries #165

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

Merged
merged 13 commits into from
Dec 30, 2023
Merged

feat: Support linking against shared libraries #165

merged 13 commits into from
Dec 30, 2023

Conversation

aherrmann
Copy link
Owner

Closes #164
Depends on #162

  • Link Zig target against shared library.
  • todo note: relative NEEDED path
  • Fix unused direct_data
  • Add shared libraries to runtime deps

@aherrmann aherrmann changed the base branch from main to cdeps December 26, 2023 11:59
Base automatically changed from cdeps to main December 26, 2023 13:22
@aherrmann
Copy link
Owner Author

A couple things need fixing for shared library dependencies to work:

  • On Linux a dynamic binary requires a loader. Zig seems to default to assuming a musl loader. The lack thereof, e.g. on Ubuntu, causes a "File not found" error. To fix this the platform needs to be configured to explicitly set the system abi. E.g. on Ubuntu x86_64-linux-gnu would work.
  • At least on Linux a dynamic binary does not work if we do not link against libc. Otherwise, executing the binary fails with a segfault.
  • The soname of the cc_library produced dynamic binary seems to capture the full output path, at least the Zig linker generates a NEEDED entry with the full path bazel-out/k8-fastbuild/bin/_solib_k8/_U_S_Slink-dependencies_Sshared-library_Cadd___Ulink-dependencies_Sshared-library/libadd_shared.so. However, this path is not always valid, e.g. the result binary may work when executed directly in the workspace root, but it does not work when executed via bazel run or from a different directory. Experiments by invoking zig build-exe directly caused the Zig linker to generate such full path NEEDED entries even when using -ladd_shared flags. It is unclear if there is a set of Zig command-line options that would produce the wanted NEEDED libadd_shared.so and RUNPATH $ORIGIN/... entries. It may be necessary to post-process the produced binary.

@aherrmann aherrmann marked this pull request as ready for review December 30, 2023 13:39
@aherrmann aherrmann enabled auto-merge December 30, 2023 13:41
@aherrmann aherrmann disabled auto-merge December 30, 2023 13:57
@aherrmann aherrmann enabled auto-merge December 30, 2023 14:15
@aherrmann aherrmann merged commit 06734ec into main Dec 30, 2023
@aherrmann aherrmann deleted the dyndeps branch December 30, 2023 14:19
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.

Link against shared cc_library.
1 participant