This is an archive of the discontinued LLVM Phabricator instance.

[lld-macho][nfc] Give non-text ConcatOutputSections order-independent finalization
ClosedPublic

Authored by int3 on Apr 6 2022, 9:48 PM.

Details

Summary

This diff is motivated by my work to add proper DWARF unwind support. As
detailed in PR50956 functions that need DWARF unwind need to have
compact unwind entries synthesized for them. These CU entries encode an
offset within __eh_frame that points to the corresponding DWARF FDE.

In order to encode this offset during
UnwindInfoSectionImpl::finalize(), we need to first assign values to
InputSection::outSecOff for each __eh_frame subsection. But
__eh_frame is ordered after __unwind_info (according to ld64 at
least), which puts us in a bit of a bind: outSecOff gets assigned
during finalization, but __eh_frame is being finalized after
__unwind_info.

But it occurred to me that there's no real need for most
ConcatOutputSections to be finalized sequentially. It's only necessary
for text-containing ConcatOutputSections that may contain branch relocs
which may need thunks. ConcatOutputSections containing other types of
data can be finalized in any order.

This diff moves the finalization logic for non-text sections into a
separate finalizeContents() method. This method is called before
section address assignment & unwind info finalization takes place. In
theory we could call these finalizeContents() methods in parallel, but
in practice it seems to be faster to do it all on the main thread.

Diff Detail

Event Timeline

int3 created this revision.Apr 6 2022, 9:48 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 6 2022, 9:48 PM
int3 requested review of this revision.Apr 6 2022, 9:48 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 6 2022, 9:48 PM
int3 retitled this revision from [lld-macho] Give non-text ConcatOutputSections order-independent finalization to [lld-macho][nfc] Give non-text ConcatOutputSections order-independent finalization.Apr 7 2022, 5:40 AM
int3 edited the summary of this revision. (Show Details)Apr 7 2022, 6:00 AM
oontvoo accepted this revision.Apr 7 2022, 7:14 AM
oontvoo added a subscriber: oontvoo.
oontvoo added inline comments.
lld/MachO/ConcatOutputSection.cpp
198–208

Why is the lambda needed here? (It's only got one caller ...)

Why not make it the body of the for loop?

410–418

nit: it seems a bit more intuitive to reverse the condition here. ie., "if segment == text", then make text-output-section, otherwise, concat-output-section.

lld/MachO/ConcatOutputSection.h
70–71

could we have some comments here on finalizae() vs finalizeContents() pls?

lld/MachO/Writer.cpp
976

less memory?
(citations needed 😄 )

This revision is now accepted and ready to land.Apr 7 2022, 7:14 AM

thx!

lld/MachO/ConcatOutputSection.h
55

perhaps make this a private, too?

int3 added inline comments.Apr 7 2022, 10:59 AM
lld/MachO/ConcatOutputSection.cpp
198–208

because I forgot to clean up my copypasta :p good catch

410–418

agreed

lld/MachO/Writer.cpp
976

oh, chromium_framework actually links a bit faster on my machine with the non-parallel setup. I'll make the comment more explicit :)

int3 marked 5 inline comments as done.Apr 7 2022, 3:10 PM
int3 added inline comments.
lld/MachO/ConcatOutputSection.cpp
198–208

factoring it out into its own method so that TextOutputSection can share it too

lld/MachO/ConcatOutputSection.h
55

This does get used externally (in offsetToInputSection as well as sortSegmentsAndSections).

70–71

OutputSections::finalize() already has a comment block that talks about finalizeContents(). I'll add a comment here to reference it

int3 updated this revision to Diff 421351.Apr 7 2022, 3:10 PM
int3 marked 2 inline comments as done.

address comments

This revision was landed with ongoing or failed builds.Apr 7 2022, 3:13 PM
This revision was automatically updated to reflect the committed changes.