This is an archive of the discontinued LLVM Phabricator instance.

[lld] Implement safe icf for MachO
ClosedPublic

Authored by alx32 on Apr 13 2022, 6:50 PM.

Details

Reviewers
int3
Group Reviewers
Restricted Project
Commits
rGe29dc0c6fde2: [lld] Implement safe icf for MachO
Summary

This change implements --icf=safe for MachO based on addrsig section that is implemented in D123751.

Diff Detail

Event Timeline

alx32 created this revision.Apr 13 2022, 6:50 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 13 2022, 6:50 PM
Herald added a reviewer: Restricted Project. · View Herald Transcript
alx32 requested review of this revision.Apr 13 2022, 6:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 13 2022, 6:50 PM
thakis added a subscriber: thakis.Apr 14 2022, 5:31 AM

Very cool!

This needs a test case.

int3 added a comment.Apr 14 2022, 9:32 AM

Re testing: you can look at https://github.com/llvm/llvm-project/blob/main/lld/test/MachO/icf.s for inspiration. I would probably create a new test file that has some address-significant functions, and check that we don't dedup them with --icf=safe while continuing to do so with --icf=all.

alx32 updated this revision to Diff 423491.Apr 18 2022, 5:14 PM
int3 added inline comments.Apr 18 2022, 7:46 PM
lld/test/MachO/icf-safe.s
9–13

if you add --macho to the objdump command (i.e. llvm-objdump --macho -d) these will be rendered as bl _func0<N> instead, then it's easy to check that those values are the same / different

alx32 updated this revision to Diff 423660.Apr 19 2022, 10:12 AM
kyulee added a subscriber: kyulee.Apr 19 2022, 11:12 AM
int3 added inline comments.Apr 19 2022, 12:28 PM
lld/MachO/Driver.cpp
1510–1512

nit: can omit if braces

lld/MachO/ICF.cpp
361–365

can omit braces

370–371

hmm if I understand correctly, the failure case we are trying to avoid here is that a DSO may export two symbols a and b, and whatever loads that DSO may end up comparing the addresses of a and b for equality? If that's the case I think any exported symbol in a DSO should be marked as address-significant. But... that seems like we would end up marking a lot of symbols :/

referencedDynamically is only used to mark symbols that shouldn't be stripped (and currently only applies to __mh_execute_header, as per its block comment); but stripped symbols can still have their addresses taken if they are in the export trie.

What does LLD-ELF do here?

373

I think the _or_null isn't necessary, this should always be nonnull

380
389–390

codebase convention is to avoid auto for simple types

427
lld/MachO/ICF.h
24

symtab and config are globals in LLD; no need to pass them explicitly

lld/MachO/InputSection.h
74

would be nice to have a comment here

lld/test/MachO/icf-options.s
12

can just reuse DIAG-EMPTY here, no need for DIAG-SAFE

lld/test/MachO/icf-safe.s
17–19

nit: align

35–40

we usually try to create test cases that are as minimal as possible. can this just be a ret void?

105–107

ditto, I suppose we need noinline but can we omit the others?

int3 added inline comments.Apr 19 2022, 2:11 PM
lld/MachO/ICF.cpp
370–371

we should cover this with a test too

alx32 updated this revision to Diff 423771.Apr 19 2022, 4:49 PM
alx32 marked an inline comment as done.
alx32 marked 14 inline comments as done.
alx32 updated this revision to Diff 423960.Apr 20 2022, 10:46 AM
alx32 updated this revision to Diff 423964.Apr 20 2022, 10:50 AM
alx32 updated this revision to Diff 423966.Apr 20 2022, 10:54 AM
int3 accepted this revision.Apr 20 2022, 12:27 PM

lgtm!

lld/MachO/ICF.h
12

could forward-declare InputFile and move this include to the .cpp

23

oops, I belatedly realized that inputFiles is a global as well, doesn't need to be passed either :D

(we could do away with the SetVector.h include in that case)

lld/test/MachO/icf-safe.s
10–12

FileCheck operates via substring matching by default, so there's no need for the regex

nit: also align the lines with the _callAllFunctions line above

24

should remove the other attributes we deleted below (or just remove the entire comment altogether)

This revision is now accepted and ready to land.Apr 20 2022, 12:27 PM
smeenai added inline comments.
lld/MachO/InputSection.h
74

Nit: "symbol" is a bit confusing, since the flag is on a section. Perhaps something like this instead?

smeenai added inline comments.Apr 27 2022, 5:14 PM
lld/test/MachO/icf-safe.s
25

Nit: you can inline the attributes instead of declaring them out of line (same for everything below)

alx32 marked 5 inline comments as done.May 3 2022, 4:49 PM
alx32 updated this revision to Diff 426879.May 3 2022, 4:54 PM
alx32 updated this revision to Diff 426880.May 3 2022, 4:57 PM
This revision was landed with ongoing or failed builds.May 3 2022, 6:01 PM
Closed by commit rGe29dc0c6fde2: [lld] Implement safe icf for MachO (authored by alx32, committed by int3). · Explain Why
This revision was automatically updated to reflect the committed changes.