Repository navigation
Description
Problem Statement
It is currently impossible to use Zig to build shared (dynamic) libraries on macOS where some symbols are left undefined. For example, consider the program:
extern fn bar() void;
export fn foo() void {
bar();
}On Linux we can build a shared library from this without error:
$ zig build-lib -dynamic test.zig
$ ls
libtest.so test.zig zig-cache
However the same program compiled with the same command line fails on macOS:
$ zig build-lib -dynamic test.zig
warning(link): unexpected LLD stderr:
Undefined symbol: zig-cache/o/c4e95092e3ee25bb54ad9a24fb2ee9a5/test.o: _bar
symbol(s) not found
error: FileNotFound
This has previously been reported in #3085 and #8016.
Motivation
This is a blocker for creating loadable extension modules for Python, Ruby, Lua, Postgres, etc. -- basically any program that loads modules via dlopen() and expects that the symbols will be resolved at load time. If you only need your module to work on Linux, then Zig will work. But if you need macOS support, there appears to be no way to make this work, and no workaround that I could find.
Background
Both macOS and Linux have command line options that control whether symbols may be undefined at link time. However the options are different and the two platforms have opposite defaults. By default, Linux is lax and macOS is strict.
Linux
Linux has two different options:
--no-undefined: causes the link to fail if unresolved symbols are present in the library currently being built. This is unset by default, so Linux allows undefined references by default, as we observed in the problem statement.--no-allow-shlib-undefined: causes the link to fail if unresolved symbols are present in any shared libraries that we are linking against (eg. pre-existing shared libraries). This defaults to unset (ie. allowing undefined references) when you are building a shared library, but the default is the opposite (failing for undefined references) if you are building an executable. This is a somewhat odd option that I don't know a real use case for, and macOS doesn't have an analogue to this that I am aware of.
(There are also a few other flags such as --unresolved-symbols=method and -z defs, which are just different porcelain around the same underlying options, AFAICT).
More info in this StackOverflow thread
macOS
macOS has just one option for this:
-undefined treatment, wheretreatmentis one of:error,warning,suppress, ordynamic_lookup. The default iserror.
Existing practice for loadable modules on macOS is to prefer -undefined dynamic_lookup. For example, see: Building Python extension modules on OS X with cmake or autotools
Prior Art
zig cc has an existing option --allow-shlib-undefined. It appears to have the following semantics:
- This flag is only available for
zig cc, notzig build-lib. - On macOS, it adds
-undefined dynamic_lookupto the link, which is just what we want. 👍👍👍 - On ELF targets, it adds
--allow-shlib-undefinedto the link. This seems wrong and possibly confused, since it is not the analogue of the-undefined dynamic_lookupflag on macOS (see the "Linux" section above).
Proposed Solution
The only hard requirement is that we can somehow pass -undefined dynamic_lookup to the linker on macOS. Anything that makes this possible, even an escape hatch like -Wl,arbitrary_linker_flag would solve the problem.
That said, here is my proposed solution:
- Make Zig pass
-undefined dynamic_lookupto the macOS linker by default forzig build-lib -dynamic. That way, anybody porting their code from Linux to macOS will find that it "just works" and no platform-specific tweaks are needed. - Do the same for
zig cc, and deprecate thezig cc --allow-shlib-undefinedoption (since this behavior is the default now). - (If desired/requested): Add a
zig build-lib --no-undefinedoption for cases where users do truly want to force the shared library to define all symbols. This option would remove-undefined dynamic_lookupon macOS and add--no-undefinedon Linux.
I think the use case for (3) is somewhat rare: I personally have never used ld --no-undefined or heard of anybody using it. I'm sure there are some users for it out there, but in terms of bang for the buck this seems rather niche.
Activity
mikdusan commentedon Mar 13, 2021
If a workaround is immediately needed you can minimally specify a target (at least 1 element has to be explicit). Here the
macoscomponent is explicit. This puts zig into a "cross-compile" mentality and it adds-undefined dynamic_lookup. Note we are in a state of flux with macos linking, so this should work now in master but no guarantees how long it will work.haberman commentedon Mar 13, 2021
Thanks @mikdusan. That appears to work!
Do you have any feedback on the proposal?
mikdusan commentedon Mar 13, 2021
@haberman,
my initial thoughts are, with respect to creating dynamic-libs:
zig {cc,c++,build-lib}undef treatment consistent with each target format native linkerzig {cc,c++}as it already exposes cli options to change undef treatmentbuild-lib(dynamic-lib) to set undef treatmenthaberman commentedon Mar 13, 2021
Thanks for the feedback @mikdusan!
This seems unfortunate. It forces each user has to go through the trouble of adding this flag when they port to Mac. What is the benefit?
I had thought the
zigcommand-line driver was trying to provide relatively consistent semantics across platforms.In my proposal I concluded that the existing flag (
--allow-shlib-undefined) has inconsistent semantics across ELF and Mach-O. Do you disagree with that assessment?mikdusan commentedon Mar 13, 2021
@haberman,
I'm sure there are more but these benefits come to mind when resolving symbols:
libFoo.sois encoded withlibBar.sopathname and if done right will only require end-user to have-lFooinstead of both. Consider more frameworks with half dozen or more dependent libslibFoo.dylib, it supports two-level namespaces and associates symbols to libraries which has search/loadtime benefits, and a big help in diagnosing missing symbols: you know which lib it should be inzig {cc,c++}drivers are very different fromzig {build-obj,build-lib,build-exe}. One of the primary things is to makezig {cc,c++}work "as expected". This includescmakeandautotoolsstyle probes, which are complex and full of assumptions. So here's an example which may or may not hold true for this particular case about undef symbols:zig cc→cang version 11.0.1macosmachoat this point is a detection test going to run to detect if undefined is allowed? The test isn't so simple. You can't just key off "try a simple clang -shared ..." and see if it fails. A real test has to make sure the failure is for the reason it's supposed to fail. So it's at this point that assumptions come in. If those 4 hypothetical steps were taken, the project config logic could simply be "symbols required" and conversely for gcc/linux/elf "undef is ok" and if we geared
zig ccdo do something different then things start to break everywhere and this stuff is difficult to diagnose.currently whether or not zig is in "cross-compilation" mode has an impact on
--allow-shlib-undefined. It's something like "if cross-compiling then allow undefined" and I have a feeling it's too simplistic.haberman commentedon Mar 14, 2021
@mikdusan,
I take your point that macOS has a two level namespace, which confers extra benefits if a dylib is available for resolution at link time. But I disagree when you say "if build-time linking fails something is really wrong" -- this is a common case when libraries are intended for
dlopen()(eg. native extensions for language interpreters).If build errors for undefined symbols were solving lots of problems, I would expect to see more users opting into these checks on Linux by using
-Wl,--no-undefined. But I don't think this is a common practice.I totally agree:
zig ccis trying to mimic the interface of existingcccommand-line drivers. I agree that it should do whateverccdoes now.But for
zig build-lib, which doesn't have this backward-compatibility constraint, would it make sense to make it more consistent across platforms?haberman commentedon Mar 14, 2021
This is a somewhat more subtle and complicated point, so I'm putting it in a separate comment:
I'm making a slightly different point, which is that
zig cc --allow-shlib-undefinedseems to be treating macOS-undefined dynamic_lookupand Linux--allow-shlib-undefinedas equivalent, but they aren't quite the same.Take a command like
gcc -shared -o libFoo.so foo.c libBar.so. There are two separate checks you can do:foo.chas unresolved symbols (ie. direct refs)?libBar.sohas unresolved symbols (ie. shlib refs)?On macOS,
-undefined dynamic_lookupaffects both direct refs and shlib refs. It only offers the user one actual bit of state that controls both checks. Whereas the Linux linker has two bits of state that the user can control independently. I tried to gather some more information on exactly how these flags work, and this is what I came up with:-shared-shared-flat_namespace-shared-undefined dynamic_lookup-flat_namespace-undefined dynamic_lookup-shared-shared--no-undefinedor-z defs-shared--no-allow-shlib-undefined--allow-shlib-undefinedz undefs*) It will check these if the dylib was also built with
-flat_namespaceIf
zig ccis trying to behave more like regularcc, it seems like it shouldn't try to translate between--allow-shlib-undefinedand-undefined dynamic_lookupsince these two have different semantics.mikdusan commentedon Mar 14, 2021
wojtekmach commentedon Jun 30, 2021
Hey everyone, I tried ignoring missing symbols on latest master but couldn't get it to work.
I assume it's because zld doesn't support this flag yet. Any help, workarounds, etc, would be really appreciated.
3 remaining items
Add macOS target support for `--zig`
mikdusan commentedon Sep 17, 2023
this works today:
wojtekmach commentedon Sep 17, 2023
Great news, thank you!
Fix macOS build
Fix macOS build
fawzi commentedon Mar 22, 2024
If someone like me comes here trying to find how to fix a build.zig for a plugin,
lib.linker_allow_shlib_undefined = trueis the right incantation, not all options are settable at construction time and listed in Build.SharedLibraryOptions...