This is an archive of the discontinued LLVM Phabricator instance.

[lld-macho] Unreferenced weak dylib symbols shouldn't fetch archive symbols
ClosedPublic

Authored by int3 on Dec 3 2021, 6:28 PM.

Details

Summary

We were fetching archive symbols too eagerly, bloating binary size as well as
just screwing up binaries that expected to look up certain symbols only at
runtime.

Diff Detail

Event Timeline

int3 created this revision.Dec 3 2021, 6:28 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 3 2021, 6:28 PM
int3 requested review of this revision.Dec 3 2021, 6:28 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 3 2021, 6:28 PM
oontvoo added a subscriber: oontvoo.Dec 4 2021, 6:10 PM
oontvoo added inline comments.
lld/MachO/SymbolTable.cpp
193

I wonder if this should be == RefState::Strong.

can't find docs anywhere for MachO, but in ELF, it seems weak-ref doesn't trigger a fetch from an archive ...

int3 marked an inline comment as done.Dec 4 2021, 9:27 PM
int3 added inline comments.
lld/MachO/SymbolTable.cpp
193

good question. Turns out that for Mach-O, weak refs do trigger fetches. But I should have tested it :) adding one now

int3 updated this revision to Diff 391887.Dec 4 2021, 9:27 PM
int3 marked an inline comment as done.

extend test

oontvoo accepted this revision.Dec 5 2021, 6:52 AM

LG Thanks!

This revision is now accepted and ready to land.Dec 5 2021, 6:52 AM
MaskRay added inline comments.
lld/MachO/SymbolTable.cpp
194

file->fetch(sym); is not covered by tests. D116916 will add the coverage.

Question: do all these tests match ld64 behavior? I think the weak-dylib condition is quite nice now because its rules are commutative.

## (Weak) archive symbols have the same precedence as dylib symbols.

Non-weak-dylib looks a bit odd as it is not commutative.

int3 added a comment.Mar 3 2022, 2:06 PM

Non-weak-dylib looks a bit odd as it is not commutative.

Non-weak-dylib has the same precedence as archive symbols, so yeah, it's not commutative. This is what ld64 does too.

It took me a while to find the test that covered this case, so I put up D120938: [lld-macho][nfc] Rename some tests for consistency

Herald added a project: Restricted Project. · View Herald TranscriptMar 3 2022, 2:06 PM