This is an archive of the discontinued LLVM Phabricator instance.

[lld/mac] Add some support for dynamic lookup symbols, and implement -U
ClosedPublic

Authored by thakis on Feb 25 2021, 6:54 PM.

Details

Summary

Dynamic lookup symbols are symbols that work like dynamic symbols
in ELF: They're not bound to a dylib like normal Mach-O twolevel lookup
symbols, but they live in a global pool and dyld resolves them against
exported symbols from all loaded dylibs.

This adds support for dynamical lookup symbols to lld/mac. They are
represented as DylibSymbols with file set to nullptr.

This also uses this support to implement the -U flag, which makes
a specific symbol that's undefined at the end of the link a
dynamic lookup symbol.

For -U, it'd be sufficient to just to a pass over remaining undefined symbols
at the end of the link and to replace them with dynamic lookup symbols then.
But I'd like to use this code to implement flat_namespace too, and that will
require real support for resolving dynamic lookup symbols in SymbolTable. So
this patch adds this now already.

While writing tests for this, I noticed that we didn't set N_WEAK_DEF in the
symbol table for DylibSymbols, so this fixes that too.


In addition to -flat_namespace, this will also be useful for implementing
-undefined dynamic_lookup, and, with -flat_namespace, -undefined warning
and -undefined suppress – but for these, the pass at the end would've sufficed too.

This isn't 100% done, there are a few XXXs left, but I'm out of evening, and what's
left is pretty basic. So I'm sending it out now for better timezone overlap :)

Diff Detail

Event Timeline

thakis created this revision.Feb 25 2021, 6:54 PM
Herald added a project: Restricted Project. · View Herald Transcript
Herald added a subscriber: dang. · View Herald Transcript
thakis requested review of this revision.Feb 25 2021, 6:54 PM
thakis added inline comments.Feb 25 2021, 7:00 PM
lld/MachO/SyntheticSections.cpp
556–557

I'll extract the ordinal encoding code here and further up into a shared helper function before landing this change if that sounds ok. It's the same code down here and up there, and it needs the same change now.

lld/MachO/Writer.cpp
473

This is a remainder from an old version of the patch, where I tried to make dynamic lookup symbols weak. But that didn't work due to this getting set, so I changed SymbolTable to make non-dynamic-lookup symbols always win over them. So this XXX is already done (and covered by tests).

475

I was debating if I should make a new Symbol subclass for DylibSymbols that don't come from a file, but in most places both dynamic lookup symbols and dylib-bound symbols will be handled the same way, so I decided against the new class.

int3 added inline comments.Feb 26 2021, 10:35 AM
lld/MachO/SyntheticSections.cpp
297–298

you should pull and rebase past D95913: [lld-macho] Implement -bundle_loader

298

that would be BIND_SPECIAL_DYLIB_FLAT_LOOKUP

lld/test/MachO/U_dynamic_lookup.s
1 ↗(On Diff #326579)

nit: use hyphens instead of underscores in test file names

thakis updated this revision to Diff 326791.Feb 26 2021, 1:36 PM

address comments and rebase

thakis marked 3 inline comments as done.Feb 26 2021, 1:37 PM

Ok, this is ready now :)

lld/MachO/SyntheticSections.cpp
297–298

Hey, nice :)

int3 accepted this revision.Feb 26 2021, 1:46 PM

lgtm!

This revision is now accepted and ready to land.Feb 26 2021, 1:46 PM
This revision was landed with ongoing or failed builds.Feb 26 2021, 1:51 PM
This revision was automatically updated to reflect the committed changes.
thakis marked an inline comment as done.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 26 2021, 1:51 PM