This is an archive of the discontinued LLVM Phabricator instance.

[lld-macho] Implement -headerpad
ClosedPublic

Authored by int3 on Jul 27 2020, 4:44 PM.

Details

Reviewers
MaskRay
compnerd
Group Reviewers
Restricted Project
Commits
rG22e6648a1834: [lld-macho] Implement -headerpad
Summary

Tools like install_name_tool and codesign may modify the Mach-O
header and increase its size. The linker has to provide padding to make this
possible. This diff does that, plus sets its default value to 32 bytes (which
is what ld64 does).

Unlike ld64, however, we lay out our sections *exactly* -headerpad bytes from
the header, whereas ld64 just treats the padding requirement as a lower bound.
ld64 actually starts laying out the non-header sections in the __TEXT segment
from the end of the (page-aligned) segment rather than the front, so its
binaries typically have more than -headerpad bytes of actual padding. I
am not sure whether it's important to emulate that behavior, so I'm sticking
with our simpler lay-out-from-front implementation for now.

Diff Detail

Event Timeline

int3 created this revision.Jul 27 2020, 4:44 PM
Herald added a project: Restricted Project. · View Herald Transcript
int3 edited the summary of this revision. (Show Details)Jul 27 2020, 7:51 PM

I think that the page offset trick that ld64 does is important. That sounds very much like page handling for the loader to be more efficient, and I think that we should emulate that behaviour (with a comment in the code).

lld/MachO/Driver.cpp
399

Would be nice to comment the default value (32).

int3 added a comment.Jul 28 2020, 9:55 AM

Happy to emulate that behavior, but I'd like to check my understanding of the performance implications. Is it just a matter of saving one page? I guess it'd be significant if the first or last page in __TEXT was hot code and benefits from being in the L1 TLB, but I'm not sure how often that is the case.

int3 added a comment.Jul 28 2020, 10:13 AM

Also, would you be fine with the emulation of ld64's alignment behavior being punted to a future diff? I think it's pretty orthogonal in terms of the code changes required.

compnerd accepted this revision.Jul 29 2020, 1:59 PM

Yes, that is roughly the tradeoff. It matters more for smaller binaries, but it also helps ensure that the code page is aligned and ensures that any offset differences don't matter and you can do same page merging. I don't have any objections against it being a follow up patch.

This revision is now accepted and ready to land.Jul 29 2020, 1:59 PM
This revision was landed with ongoing or failed builds.Jul 30 2020, 2:30 PM
This revision was automatically updated to reflect the committed changes.
int3 added inline comments.Jul 30 2020, 2:40 PM
lld/MachO/Driver.cpp
399

oops, forgot about this, addressed in c89e46e767e2