This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objdump] Add -dyld_info to llvm-otool
ClosedPublic

Authored by BertalanD on Aug 17 2022, 8:53 AM.

Details

Summary

This option outputs the location, encoded value and target of chained
fixups, using the same format as otool -dyld_info.

This initial implementation only supports the DYLD_CHAINED_PTR_64 and
DYLD_CHAINED_PTR_64_OFFSET pointer encodings, which are used in x86_64
and arm64 userspace binaries.

When Apple's effort to upstream their chained fixups code continues,
we'll replace this code with the then-upstreamed code. But we need
something in the meantime for testing ld64.lld's chained fixups code.

Diff Detail

Event Timeline

BertalanD created this revision.Aug 17 2022, 8:53 AM
Herald added a project: Restricted Project. · View Herald Transcript
BertalanD requested review of this revision.Aug 17 2022, 8:53 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 17 2022, 8:53 AM
thakis added inline comments.Aug 17 2022, 12:38 PM
llvm/tools/llvm-objdump/OtoolOpts.td
44 ↗(On Diff #453299)

(this needs rebasing across 164266739298b39d3eac8d79ad12d1d654e2825e)

Please remember to add new options to the CommandGuide(s) at llvm/docs/CommandGuide.

Rebased onto 164266739298b39d3eac8d79ad12d1d654e2825e; which added the docs for this flag.

BertalanD added inline comments.Aug 18 2022, 1:08 AM
llvm/lib/Object/MachOObjectFile.cpp
3402–3404

reminder to self: simplify this with maskTrailingOnes.

llvm/test/tools/llvm-objdump/MachO/dyld-info.test
10–16

All the {{0*}} are a bit awkward, but I want this test to pass with cctools otool as well. It has a bunch of extra logic in the printing code to use the width of the largest number, while this patch always uses a fixed width; so the test needs to accept an arbitrary number of leading zeros.

ping!

I cleaned the patch up a bit and rebased it. D132560 depends on this for tests.

BertalanD added inline comments.Aug 25 2022, 10:22 AM
llvm/lib/Object/MachOObjectFile.cpp
3402–3404

nevermind. That wouldn't make the code any clearer.

This weird lambda is needed because bitfields are a pain to work with when endianness is involved.

thakis accepted this revision.Aug 26 2022, 10:19 AM

Sorry for the delay.

llvm/lib/Object/MachOObjectFile.cpp
2116

The name-based version of this has to loop, but couldn't this version just do:

auto &LoadCmd = LoadCommands[SegmentIndex];

instead of the loop? (Range check before that, of course.)

3402–3404

My opinion here is the same as on D131982 (but I agree it makes sense to do the same thing here as over there, and we currently do the manual bit fiddling there).

3409

Put this comment up with the struct definitions too.

3450

We have this loop three times. Maybe move it into a FindNextPageWithFixups() helper?

llvm/test/tools/llvm-objdump/MachO/dyld-info.test
4

Should we have a test for llvm-otool -dyld_info running on a file with opcode fixups (instead of chained fixups) too? (ok to punt this to a follow-up)

10–16

Maybe just do the FIXME: Align the columns properly? That's what, like 6 lines?

llvm/tools/llvm-objdump/MachODump.cpp
1190–1191

We should probably rename this function to printMachODyldInfo now.

This revision is now accepted and ready to land.Aug 26 2022, 10:19 AM
thakis added inline comments.Aug 26 2022, 10:23 AM
llvm/lib/Object/MachOObjectFile.cpp
2116

Please ignore, I just realized SegmentIndex, not LoadCommandIndex.

BertalanD updated this revision to Diff 456014.Aug 26 2022, 1:54 PM
  • Moved code for finding the next fixup into a helper function (findNextPageWithFixups).
  • Folded printMachOChainedFixups into PrintDyldInfo.
  • Added test to ensure that we can handle objects that don't use chained fixups.
  • Minor comment tweaks.
BertalanD marked 5 inline comments as done.Aug 26 2022, 1:57 PM
BertalanD updated this revision to Diff 456017.Aug 26 2022, 2:00 PM
BertalanD marked an inline comment as done.
BertalanD added inline comments.
llvm/lib/Object/MachOObjectFile.cpp
3409

oops, missed this in Diff 4.
fixed

BertalanD added a subscriber: Restricted Project.Aug 26 2022, 2:29 PM
thakis added inline comments.Aug 26 2022, 5:03 PM
llvm/test/tools/llvm-objdump/MachO/dyld-info.test
10–16

Thoughts on doing this fixme before landing?

BertalanD updated this revision to Diff 456091.Aug 27 2022, 1:08 AM
BertalanD marked an inline comment as done.

pretty-print the table with proper column alignment

BertalanD marked 3 inline comments as done.Aug 27 2022, 1:11 AM
thakis accepted this revision.Aug 27 2022, 4:41 AM

Thanks! Looks ready go to :)

(once CI is fixed; https://reviews.llvm.org/harbormaster/unit/view/4889067/ looks like it might be failing due to this patch)

BertalanD updated this revision to Diff 456112.Aug 27 2022, 6:10 AM

Fix CI by properly ignoring error during the first iteration in PrintDyldInfo().

thakis added inline comments.Aug 27 2022, 10:35 AM
llvm/tools/llvm-objdump/MachODump.cpp
1362

nit: Not sure if this line is necessary; Error's move ctor comment sounds a bit like it already does this to the moved-from value: http://llvm-cs.pcc.me.uk/include/llvm/Support/Error.h#190

BertalanD added inline comments.Aug 27 2022, 2:47 PM
llvm/tools/llvm-objdump/MachODump.cpp
1362

But then, we'd do a use-after-move later in the function, which is a bit dodgy.

thakis added inline comments.Aug 27 2022, 3:54 PM
llvm/tools/llvm-objdump/MachODump.cpp
1362

Up to you. But it's up to classes if they want to have defined after use semantics, and this one does have defined semantics. *Shrug*

This revision was automatically updated to reflect the committed changes.