This is an archive of the discontinued LLVM Phabricator instance.

[lld-macho] Allow dead_strip to work with exported private extern symbols
ClosedPublic

Authored by thevinster on Apr 20 2022, 7:57 PM.

Details

Summary

It seems like we are overly asserting when running -dead_strip with
exported symbols. ld64 treats exported private extern symbols as a liveness
root. Loosen the assert to match ld64's behavior.

Diff Detail

Event Timeline

thevinster created this revision.Apr 20 2022, 7:57 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 20 2022, 7:57 PM
Herald added a reviewer: Restricted Project. · View Herald Transcript
thevinster retitled this revision from [lld-macho] Allow dead_strip on autohide symbols to [lld-macho] Allow dead_strip to work with autohide symbols that cannot be exported.Apr 20 2022, 8:07 PM
thevinster edited the summary of this revision. (Show Details)
thevinster edited the summary of this revision. (Show Details)
thevinster edited the summary of this revision. (Show Details)Apr 20 2022, 8:09 PM
thevinster published this revision for review.Apr 20 2022, 8:11 PM
thevinster edited the summary of this revision. (Show Details)
Herald added a project: Restricted Project. · View Herald TranscriptApr 20 2022, 8:12 PM
thakis added a subscriber: thakis.Apr 21 2022, 6:49 AM
thakis added inline comments.
lld/MachO/MarkLive.cpp
235

Should we call addSym for privateExterns? That seems wrong (?)

oontvoo added inline comments.
lld/MachO/MarkLive.cpp
235

Should we call addSym for privateExterns

normally we shouldn't but if a symbol is both private extern and autohide, then i think it should be ok,

relatedly, stray comment should be deleted or moved?

thevinster added inline comments.Apr 21 2022, 10:30 AM
lld/MachO/MarkLive.cpp
235

I thought the comment was referring to the addSym, but I don't think it will be changed so I'm happy to remove it.

if a symbol is both private extern and autohide, then i think it should be ok,

This is what I found from testing as well. Happy to re-add the assert back and only skip it if it is both privateExtern and autohide. I think that's the one I'm fixing in my local builds.

thevinster added inline comments.
lld/MachO/MarkLive.cpp
235

So with some further testing, this is looking fairly strange. I think ld64 is just preserving all the symbols that are exported even if they cannot be exported. It does feel strange to preserve the symbol if it's privateExtern... I can skip "unable to be exported syms" from being marked live which seems like the correct behavior, but this will deviate from the ld64's behavior. What do y'all think should be the correct approach here?

@int3 @oontvoo @thakis

oontvoo added inline comments.Apr 21 2022, 12:21 PM
lld/MachO/MarkLive.cpp
235

I thought the comment was referring to the addSym, but I don't think it will be changed so I'm happy to remove it.

The comment was referring to the fact that we shouldn't see "privateExtern" on a symbol from symtab and that such check could have been done in Driver (when we process the exported symbols).
Now that you're removing the assert, the comment can either move to Driver or just delete it.

(btw, the comment was lacking the case where a symbol is both privateExtern and autohide)

thevinster added inline comments.Apr 21 2022, 12:29 PM
lld/MachO/MarkLive.cpp
235

We have some pretty convoluted logic with privateExterns and autohides. For symbols that are both privateExterns and autohide, we end up nullifying the flag. We've effectively removed lost that information and can't distinguish if a particular symbol was originally privateExtern and autohide which makes checking for that case a harder. Although, from empirical testing w/ ld64, it looks like it just preserves them when symbols from the exported list cannot be exported.

int3 added inline comments.Apr 21 2022, 1:18 PM
lld/MachO/MarkLive.cpp
235

it looks like ld64 does treat a private extern symbol as a liveness root if it's exported, regardless of whether it is autohide. Let's just leave a comment to that effect and follow ld64's behavior. It doesn't cost us in terms of code complexity and it is one less thing for new adopters of LLD to worry about

lld/test/MachO/export-options.s
148–149

it's better to describe what the test should cover (exported-hidden) rather than what it shouldn't do (no_crash). (The comment itself can note that this used to crash.)

let's also mention explicitly that we expect _foo to be treated as a liveness root due to -exported_symbol

also nit: use hyphens instead of underscores for the file names

int3 added inline comments.Apr 21 2022, 1:33 PM
lld/test/MachO/export-options.s
148–149

or maybe AUTOHIDE-PRIVATE-DEAD-STRIP to make it clear that it's an extension to the AUTOHIDE-PRIVATE cases above

thevinster marked 6 inline comments as done.Apr 22 2022, 1:56 PM

Fixed up comments and renaming

thevinster retitled this revision from [lld-macho] Allow dead_strip to work with autohide symbols that cannot be exported to [lld-macho] Allow dead_strip to work with exported private extern symbols.Apr 22 2022, 2:01 PM
thevinster edited the summary of this revision. (Show Details)
int3 accepted this revision.Apr 22 2022, 2:20 PM

Thanks!

lld/MachO/MarkLive.cpp
231–234

maybe something like "NOTE: we are purposely not checking for privateExtern here because ..."

could also mention that exporting private externs is technically an ill-defined operation, so we are just following ld64's behavior

232
This revision is now accepted and ready to land.Apr 22 2022, 2:20 PM
thevinster marked 2 inline comments as done.Apr 22 2022, 6:43 PM