This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objdump] Add an llvm-otool tool
ClosedPublic

Authored by thakis on Apr 15 2021, 11:08 AM.

Details

Summary

This implements an LLVM tool that's flag- and output-compatible
with macOS's otool -- except for bugs, but from testing with both
otool and xcrun otool-classic, llvm-otool matches vanilla
otool's behavior very well already. It's not 100% perfect, but
it's a very solid start.

This uses the same approach as llvm-objcopy: llvm-objdump uses
a different OptTable when it's invoked as llvm-otool. This
is possible thanks to D100433.

Diff Detail

Event Timeline

thakis created this revision.Apr 15 2021, 11:08 AM
thakis requested review of this revision.Apr 15 2021, 11:08 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 15 2021, 11:08 AM

Introducing a new LLVM utility needs an announcement on llvm-dev. Can you start one?
This can potentially attract other parties who are interested in Mach-O tools.

mstorsjo added inline comments.
llvm/tools/llvm-objcopy/CopyConfig.cpp
248 ↗(On Diff #337838)

Stray unrelated change?

thakis added inline comments.Apr 16 2021, 4:22 AM
llvm/tools/llvm-objcopy/CopyConfig.cpp
248 ↗(On Diff #337838)

Oh, yes, will remove.

thakis updated this revision to Diff 338063.Apr 16 2021, 4:23 AM

remove accidental unrelated change

keith added a subscriber: keith.Apr 16 2021, 8:58 AM

Mostly looks good, with only few nits, but I hope some Mach-O users can review the functional part.

I'll also warn that MachODump.cpp may lack maintenance... so parties investing in it may need to put in some maintenance energy.

llvm/test/tools/llvm-objdump/MachO/relocations.test
14

Add -NEXT: whenever applicable.

llvm/tools/llvm-objdump/llvm-objdump.cpp
104

I know the messages are not super consistent in the tool, but https://llvm.org/docs/CodingStandards.html#error-and-warning-messages Usually we don't have a period and don't capitalize.

2874

Delete braces around one-line simple statement.

2994

Use Optional<CommonOptTable> and emplace.

3044–3048

Oh, otool doesn't like the TargetRegistry::printRegisteredTargetsForVersion message?

Hi! Thanks for working on this, I actually had some similar plans in the past.
One question (I believe that you have already explored it, so I'm asking mostly for the future reference) - did you choose llvm-objdump because it's the closest (from the available options) counterpart to otool featurewise ?
(e.g. closer than llvm-readobj) ?

thakis updated this revision to Diff 338190.Apr 16 2021, 11:42 AM
thakis marked 3 inline comments as done.

comments

Thanks!

In D100583#2695291, @alexshap wrote:

Hi! Thanks for working on this, I actually had some similar plans in the past.
One question (I believe that you have already explored it, so I'm asking mostly for the future reference) - did you choose llvm-objdump because it's the closest (from the available options) counterpart to otool featurewise ?
(e.g. closer than llvm-readobj) ?

Several reasons:

  1. llvm-objdump is "philosophically" LLVM's user-facing tool, while readobj is more internal tool for use by tests. otool is user-facing.
  2. From a more practical perspective, llvm-objdump --macho's output (with the right flags, which I'm using here) is already extremely close to otool's output
  3. man otool on macOS refers to a mythological llvm-otool that's based on llvm-objdump, so this patch makes that mythological tool a reality :)
llvm/tools/llvm-objdump/llvm-objdump.cpp
104

We're somewhat consistent about this particular string: % grep -Ri 'Pass @FILE as argument to read options from FILE' llvm/tools :)

(Also, it's just moving around in this change.)

2994

From what I can tell, that doesn't work with subclassing: Optional<CommonOptTable> reserves space for an CommonOptTable, and OtoolOptTable / ObjdumpOptTable might be larger. (Currently they aren't, but OptionalStorage::emplace() also tries to construct a T, in this case a CommonOptTable, and doesn't have anything to construct a subclass)

3044–3048

Probably not? We can decide, I guess :) Mach-O files are intel or arm in practice, so listing all targets isn't super interesting, and vanilla otool's help output is fairly laconic…

@aprantl @JDevlieghere (as folks who've implemented other apple-compatible/replacement tools in LLVM) - you folks have any interest in this work one way or another?

This looks reasonable (in approach) to me, but I haven't done a detailed code review.

  1. llvm-objdump is "philosophically" LLVM's user-facing tool, while readobj is more internal tool for use by tests. otool is user-facing.

FWIW, I don't think this difference is a true difference anymore, at least for the ELF aspects (I can't say one way or the other for other formats) - llvm-readelf is now used in production systems outside of LLVM, and should be a drop-in replacement for GNU readelf, so I think it is just as user-facing as llvm-objdump these days. In fact, when someone asks which tool to use, I usually point them at llvm-readelf over llvm-objdump for various reasons. That being said, I think your other points are perfectly valid.

llvm/tools/llvm-objdump/llvm-objdump.cpp
104

FWIW, I don't think the error/warning message styling applies here, as this is in the help text.

2999–3003

This needs testing. It's been a thing we've broken on more than one occasion in llvm-objcopy too.

You should probably also add a file to llvm/docs/CommandGuide for the new tool, along with an appropriate link in the parent page.

thakis updated this revision to Diff 338568.Apr 19 2021, 11:13 AM
thakis marked 2 inline comments as done.

add tool-name.test, llvm-otool.rst

You should probably also add a file to llvm/docs/CommandGuide for the new tool, along with an appropriate link in the parent page.

Done, thanks!

thakis updated this revision to Diff 338569.Apr 19 2021, 11:16 AM

add to docs index too

MaskRay accepted this revision.Apr 19 2021, 1:45 PM

LG

llvm/docs/CommandGuide/llvm-otool.rst
25

The style for other utilities are capitalized descriptions and trailing periods.

llvm/test/tools/llvm-objdump/MachO/relocations.test
18

It'd be good to change CHECK to VERBOSE and NONVERBOSE to a normal prefix.

llvm/tools/llvm-objdump/llvm-objdump.cpp
2870

Use "negative booleans", which may easily lead to double negations, especially that what have the positive option names now...

This revision is now accepted and ready to land.Apr 19 2021, 1:45 PM

I think the new frontend also needs to be added to LLVM_TEST_DEPENDS in llvm/test/CMakeLists.txt, otherwise the symlink isn't created if you e.g. do check-llvm straight from a fresh build dir.

jhenderson added inline comments.Apr 20 2021, 1:47 AM
llvm/docs/CommandGuide/llvm-otool.rst
18

Probably this should be as inline.

thakis updated this revision to Diff 338805.Apr 20 2021, 3:54 AM
thakis marked 2 inline comments as done.

tweak rst file more, add to LLVM_TEST_DEPENDS

I think the new frontend also needs to be added to LLVM_TEST_DEPENDS in llvm/test/CMakeLists.txt, otherwise the symlink isn't created if you e.g. do check-llvm straight from a fresh build dir.

Done.

(All the other comments too.)

llvm/test/tools/llvm-objdump/MachO/relocations.test
18

changed CHECK to VERBOSE. What's a "normal prefix"? But added llvm-objdump --macho -r --non-verbose to the test too to make it clearer where the NONVERBOSE prefix comes from.

llvm/tools/llvm-objdump/llvm-objdump.cpp
2870

Yes, will rename in a follow-up. I think with the cl::opt parsing, it had to be named like this, but now it doesn't have to.

jhenderson accepted this revision.Apr 20 2021, 3:59 AM

LGTM from my point of view, with one suggestion, but please wait for @MaskRay to be happy too.

llvm/docs/CommandGuide/llvm-otool.rst
109

This should create a hyperlink to the option. Make sure to verify it's working though with a local docs build.

thakis marked an inline comment as done.Apr 20 2021, 5:17 AM

LGTM from my point of view, with one suggestion, but please wait for @MaskRay to be happy too.

Thanks! maskray lg'd further up already, so I think I should be good to go :) Committing…

llvm/docs/CommandGuide/llvm-otool.rst
109

Done. Don't have a local docs build set up, will watch the sphinx bot instead.

This revision was landed with ongoing or failed builds.Apr 20 2021, 5:25 AM
This revision was automatically updated to reflect the committed changes.
thakis marked an inline comment as done.
Harbormaster completed remote builds in B99677: Diff 338806.
aprantl added a subscriber: davide.Apr 21 2021, 1:59 PM