This is an archive of the discontinued LLVM Phabricator instance.

[lld-macho] Follow-up to D77893
ClosedPublic

Authored by int3 on May 5 2020, 5:36 PM.

Details

Summary
  1. Don't have isHidden() depend on isNeeded(). Whether a section is hidden is orthogonal from whether it is needed: hidden sections will never have a header regardless of whether they have a body. (I know we override this method with return false for synthetic sections, but regardless I think it's confusing to write it this way for non-synthetic sections.)
  1. Don't call writeTo() on unneeded sections. D78270 assumes that this is true when implementing the stub helper section.
  1. Filter out the unneeded sections early on to avoid having to deal with them in multiple places.
  1. Remove assumption in test that the referenced file has no other symbols. (We should create separate input files for future tests to avoid such issues.)

Diff Detail

Event Timeline

int3 created this revision.May 5 2020, 5:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 5 2020, 5:36 PM

LGTM, but I'd like @Ktwu to take a look as well.

Ktwu accepted this revision.May 6 2020, 12:14 PM
Ktwu added inline comments.
lld/MachO/OutputSegment.cpp
43

I personally find this more complicated to read :(

Knowing that hidden state isn't as simple as checking isHidden() -- isNeeded() is also needed -- seemed confusing to me :/

lld/MachO/Writer.cpp
117

My bad, was this an actual bug? Why not hide the empty() check within a function?

128

Thanks!

This revision is now accepted and ready to land.May 6 2020, 12:14 PM
int3 marked 2 inline comments as done.May 6 2020, 12:50 PM
int3 added inline comments.
lld/MachO/OutputSegment.cpp
43

I agree that sprinkling isNeeded() checks around isn't pretty, but the flip side is that there's no longer an implicit requirement that every implementation of OutputSection must maintain the invariant that isNeeded() == false => isHidden() == false.

But see also https://reviews.llvm.org/D77893#2022314 -- I think we could further clean things up by filtering out all the unneeded sections earlier.

lld/MachO/Writer.cpp
117

It wasn't an actual bug, but the check here was to avoid calling firstSection() below on an empty sections vector. I think using isNeeded() which also checks if any contained sections are needed obscures this (plus it confused me for a bit as to why we had to check isNeeded() here when we were already checking it before calling writeTo()).

I'll add a comment here to make its purpose clear

int3 marked an inline comment as done.May 6 2020, 12:58 PM
int3 added inline comments.
lld/MachO/OutputSegment.cpp
43

Actually, I can do the filtering in this diff, inside sortOutputSegmentsAndSections, instead of depending on the larger proposed refactor. How does that sound?

Can unneeded sections be removed?

Ktwu added inline comments.May 7 2020, 11:47 AM
lld/MachO/OutputSegment.cpp
43

Yup, do it!

int3 updated this revision to Diff 262993.May 8 2020, 10:27 PM
int3 marked 2 inline comments as done.
int3 edited the summary of this revision. (Show Details)

remove unneeded sections early

int3 added inline comments.May 8 2020, 10:51 PM
lld/MachO/Writer.cpp
117

Actually, I think it was an actual bug: the whole purpose of this check is for __LINKEDIT, which is the only segment that may have writeTo() called on it even if it contained no sections. But isNeeded() will return true for __LINKEDIT.

This revision was automatically updated to reflect the committed changes.