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

Ignore missing symbols when compiling a shared library. #3085

Closed
heibor opened this issue Aug 17, 2019 · 8 comments
Closed

Ignore missing symbols when compiling a shared library. #3085

heibor opened this issue Aug 17, 2019 · 8 comments
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-macos use case Describes a real use case that is difficult or impossible, but does not propose a solution.
Milestone

Comments

@heibor
Copy link

heibor commented Aug 17, 2019

Clang receives the option -undefined,dynamic_lookup to ignore missing symbols when compiling a library.
Is there a a similar option in Zig? Then I can have better scalability to compile a shared library.

@heibor
Copy link
Author

heibor commented Aug 17, 2019

Or it can be better to ignore some specific symbols.

@andrewrk andrewrk added this to the 0.6.0 milestone Aug 17, 2019
@andrewrk
Copy link
Member

You mean when linking against a shared library, not producing one, right?

@heibor
Copy link
Author

heibor commented Aug 18, 2019

-undefined dynamic_lookup which will mark all undefined symbols as having to be looked up at runtime.

Much more safe way to resolve it for specific symbols:

-Wl,-U,symbol_name, which only does so for the given symbol (note: you have to prepend an underscore to the symbol name)

Please refer to this.

@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Feb 11, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 17, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@dsimunic
Copy link

dsimunic commented Jan 3, 2021

I'd second this report. For example, it's impossible to build postgres extensions on macOS without passing either -undefined dynamic_lookup or -bundle -flat_namespace -undefined suppress to the linker.

The build fails and then one has to manually link the .o file into a .dylib.

zig build
warning: unexpected LLD stderr:
Undefined symbol: zig-cache/o/1eef5bd778e281276db3520369bc4da3/pgext.o: _pg_detoast_datum
Undefined symbol: zig-cache/o/1eef5bd778e281276db3520369bc4da3/pgext.o: _errmsg
Undefined symbol: zig-cache/o/1eef5bd778e281276db3520369bc4da3/pgext.o: _errfinish
Undefined symbol: zig-cache/o/1eef5bd778e281276db3520369bc4da3/pgext.o: _errstart
symbol(s) not found

A more complex extension could have dozens of symbols like that. One has to manually complete the linking step:

cc -shared -undefined dynamic_lookup -o zig-cache/lib/pgext.dylib zig-cache/o/1eef5bd778e281276db3520369bc4da3/pgext.o

Sadly, that fails in postgres for debug builds:

could not load library "zig-cache/lib/pgext.dylib": dlopen(zig-cache/lib/pgext.dylib, 10): Symbol not found: ___zig_probe_stack
  Referenced from: zig-cache/lib/pgext.dylib
  Expected in: flat namespace

Wondering if there's any workaround as of 0.7.1? (Note there there's no library to link against--the missing symbols are in the postgres executable itself).

@daurnimator
Copy link
Contributor

Sadly, that fails in postgres for debug builds:

could not load library "zig-cache/lib/pgext.dylib": dlopen(zig-cache/lib/pgext.dylib, 10): Symbol not found: ___zig_probe_stack
  Referenced from: zig-cache/lib/pgext.dylib
  Expected in: flat namespace

Wondering if there's any workaround as of 0.7.1? (Note there there's no library to link against--the missing symbols are in the postgres executable itself).

You need to include the zig compiler rt. Pass -fcompiler-rt to your build-obj invocation.

@dsimunic
Copy link

dsimunic commented Jan 4, 2021

Thanks. The question for workarounds was more general about the whole business of dynamic linking on mac. That last remark is a data point about the complexity of the thing--one would expect that just linking with dynamic_lookup flag would solve the problem, but obviously it doesn't completely.

@haberman
Copy link

haberman commented Mar 7, 2021

You need to include the zig compiler rt. Pass -fcompiler-rt to your build-obj invocation.

This doesn't currently work on macho, see my report here. It fails with:

zig build-obj -fcompiler-rt -fPIC -DPYHEXVER=50857456 -I /Users/haberman/opt/anaconda3/include/python3.8 -I /usr/include pdfxt.zig
error: Embedding compiler-rt into macho objects is not yet implemented.

I created a proposal to fix this problem, see: Proposal: fix shared library linking on macOS.

@SpexGuy SpexGuy added the use case Describes a real use case that is difficult or impossible, but does not propose a solution. label Mar 21, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0, 0.8.0 May 19, 2021
@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-macos labels May 19, 2021
@ifreund
Copy link
Member

ifreund commented May 19, 2021

This has already been implemented on master branch, you may now pass -fallow-shlib-undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-macos use case Describes a real use case that is difficult or impossible, but does not propose a solution.
Projects
None yet
Development

No branches or pull requests

7 participants