This is an archive of the discontinued LLVM Phabricator instance.

[lld-macho] Implement builtin section renaming
ClosedPublic

Authored by gkm on Apr 27 2021, 12:50 PM.

Details

Reviewers
int3
Group Reviewers
Restricted Project
Commits
rG27b426b0c8ab: [lld-macho] Implement builtin section renaming
Summary

ld64 automatically renames many sections depending on output type and assorted flags. Here, we implement the most common configs. We can add more obscure flags and behaviors as needed.

Depends on D101393

Diff Detail

Event Timeline

gkm created this revision.Apr 27 2021, 12:50 PM
Herald added a project: Restricted Project. · View Herald Transcript
Herald added a subscriber: dang. · View Herald Transcript
gkm requested review of this revision.Apr 27 2021, 12:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 27 2021, 12:50 PM
int3 added a comment.Apr 27 2021, 2:18 PM

I think we should implement features on an as-needed basis. E.g. we aren't likely to support MH_KEXT_BUNDLE or MH_DYLINKER any time in the near future, so I don't think adding code for them is worthwhile.

Do we have use cases for -data_const and -text_exec? Also, what exactly are shared regions?

gkm added a comment.Apr 27 2021, 4:40 PM
This comment was removed by gkm.
gkm added a comment.Apr 27 2021, 5:07 PM

I think we should implement features on an as-needed basis. E.g. we aren't likely to support MH_KEXT_BUNDLE or MH_DYLINKER any time in the near future, so I don't think adding code for them is worthwhile.

Done.

Do we have use cases for -data_const and -text_exec? Also, what exactly are shared regions?

  • I implemented __DATA -> __DATA_CONST renames since it's default behavior for all PIE executables, so seemed important. Regarding the command-line options, if nothing else, -data_const and -no_data_const are are useful for explicit control of the feature for the test, though without them I can also disable it for executables with -no_pie.
  • I don't know the uses cases for -text_exec. I removed it from this diff.
  • Shared regions are related to dyld shared cache, and again, I don't know that it's useful now, so I removed it from this diff.
gkm updated this revision to Diff 341045.Apr 27 2021, 5:58 PM
  • revise according to review feedback
gkm added inline comments.Apr 27 2021, 6:03 PM
lld/MachO/MergedOutputSection.cpp
57–64

This has no test. It feels like overkill to make a testcase for it. This is a bug fix discovered during testing section renames. The old message appeared as ...

Cannot add merge section; inconsistent type flags ^@

Where the section type was a raw NUL (0) byte, and clearly wrong.

gkm updated this revision to Diff 341059.Apr 27 2021, 7:22 PM
  • Add a test for error diagnistic when merging sections with mismatched types
gkm added inline comments.Apr 27 2021, 7:23 PM
lld/MachO/MergedOutputSection.cpp
57–64

After adding section names to the diagnostic message, I went ahead and added a test case. ;)

int3 added a comment.Apr 28 2021, 2:17 AM

Yeah keeping -data_const makes sense. Thanks for removing the rest :)

lld/MachO/Driver.cpp
561–562

Our implementation does so too, right? Would be good to make that explicit... also I think this comment would make more sense at the point where we invoke initializeSectionRenameMap to highlight that we are calling it before handling OPT_rename_section

567–573

nit: I think this would be cleaner as a separately-declared array

578–580

Do we really need to discuss ld64's implementation details here?

799

I think we're unlikely to support -preload any time soon either

802

maybe "unsupported output type when trying to determine data-const"?

lld/MachO/MergedOutputSection.cpp
26–28

intentional change?

57–64

Thanks! I think it's good to test error messages, particularly since they're infrequently executed on typical inputs. (The fact that you discovered a latent bug further demonstrates this :) )

nit: no need for llvm::

lld/test/MachO/builtin-rename.s
6

why the 2>/dev/null?

26–28
gkm updated this revision to Diff 342542.May 3 2021, 2:18 PM
gkm marked 8 inline comments as done.
  • Revise according to review feedback
int3 accepted this revision.May 3 2021, 3:35 PM

lgtm

lld/MachO/MergedOutputSection.cpp
57–64

reminder to remove llvm:: :)

69–70

ditto (remove llvm::)

This revision is now accepted and ready to land.May 3 2021, 3:35 PM
gkm marked 3 inline comments as done.May 3 2021, 5:43 PM
gkm added inline comments.
lld/MachO/MergedOutputSection.cpp
26–28

Yes, though semi-gratuitous. I like seeing all the simple assignments from both sides if the if (inputs.empty()) stacked together.

lld/test/MachO/builtin-rename.s
6

Copy pasta.

26–28

Ugh. Thanx. Case-insensitive filesystem names suck.

This revision was automatically updated to reflect the committed changes.