diff options
author | Michael Trent <mtrent@apple.com> | 2019-01-15 20:41:30 +0000 |
---|---|---|
committer | Michael Trent <mtrent@apple.com> | 2019-01-15 20:41:30 +0000 |
commit | 710eb304b030f62ad7431510bb09b563d6dee610 (patch) | |
tree | fc1b74e1bc2ee287fe83e2222699c32b41ddf30b | |
parent | 39e7fe2fd6b2fe990269c8446f470573bcd75271 (diff) |
llvm-objdump -m -D should disassemble all text segments
Summary:
When running llvm-objdump with the -macho option objdump will by default
disassemble only the __TEXT,__text section (or __TEXT_EXEC,__text when
disassembling MH_KEXT_BUNDLE files). The -disassemble-all option is
treated no diferently than -disassemble.
This change upates llvm-objdump's MachO parsing code to disassemble all
__text sections found in a file when -disassemble-all is specified. This
is useful for disassembling files with more than one __text section, or
when disassembling files whose __text section is not present in __TEXT.
I added a lit test case that verifies "llvm-objdump -m -d" and
"llvm-objdump -m -D" produce the expected results on a reference binary.
I also updated the CommandGuide documentation for llvm-objdump.rst and
verified it renders correctly as man and html.
rdar://42899338
Reviewers: ab, pete, lhames
Reviewed By: lhames
Subscribers: rupprecht, llvm-commits
Differential Revision: https://reviews.llvm.org/D56649
-rw-r--r-- | llvm/docs/CommandGuide/llvm-objdump.rst | 42 | ||||
-rwxr-xr-x | llvm/test/tools/llvm-objdump/X86/Inputs/macho-multiple-text | bin | 0 -> 12580 bytes | |||
-rw-r--r-- | llvm/test/tools/llvm-objdump/X86/macho-disassemble-all.test | 39 | ||||
-rw-r--r-- | llvm/tools/llvm-objdump/MachODump.cpp | 15 |
4 files changed, 77 insertions, 19 deletions
diff --git a/llvm/docs/CommandGuide/llvm-objdump.rst b/llvm/docs/CommandGuide/llvm-objdump.rst index 0d029152047..c3e7c166005 100644 --- a/llvm/docs/CommandGuide/llvm-objdump.rst +++ b/llvm/docs/CommandGuide/llvm-objdump.rst @@ -16,12 +16,19 @@ stream. COMMANDS -------- -At least one of the following commands are required, and some commands can be combined with other commands: +At least one of the following commands are required, and some commands can be +combined with other commands: -.. option:: -disassemble +.. option:: -d, -disassemble + + Display assembler mnemonics for the machine instructions. Disassembles all + text sections found in the input file(s). + +.. option:: -D, -disassemble-all + + Display assembler mnemonics for the machine instructions. Disassembles all + sections found in the input file(s). - Display assembler mnemonics for the machine instructions - .. option:: -help Display usage information and exit. Does not stack with other commands. @@ -45,14 +52,14 @@ At least one of the following commands are required, and some commands can be co .. option:: -version Display the version of this program. Does not stack with other commands. - + OPTIONS ------- :program:`llvm-objdump` supports the following options: .. option:: -arch=<architecture> - Specify the architecture to disassemble. see -version for available + Specify the architecture to disassemble. see ``-version`` for available architectures. .. option:: -cfg @@ -68,14 +75,15 @@ OPTIONS Print line information from debug info if available. -.. option:: -macho +.. option:: -m, -macho - Use Mach-O specific object file parser. + Use Mach-O specific object file parser. Commands and other options may behave + differently when used with ``-macho``. .. option:: -mattr=<a1,+a2,-a3,...> Target specific attributes. - + .. option:: -mc-x86-disable-arith-relaxation Disable relaxation of arithmetic instruction for X86. @@ -83,26 +91,26 @@ OPTIONS .. option:: -stats Enable statistics output from program. - + .. option:: -triple=<string> - Target triple to disassemble for, see -version for available targets. - + Target triple to disassemble for, see ``-version`` for available targets. + .. option:: -x86-asm-syntax=<style> When used with the ``-disassemble`` option, choose style of code to emit from X86 backend. Supported values are: .. option:: att - + AT&T-style assembly - + .. option:: intel - + Intel-style assembly - - The default disassembly style is **att**. + + The default disassembly style is **att**. BUGS ---- diff --git a/llvm/test/tools/llvm-objdump/X86/Inputs/macho-multiple-text b/llvm/test/tools/llvm-objdump/X86/Inputs/macho-multiple-text Binary files differnew file mode 100755 index 00000000000..7b04830ff27 --- /dev/null +++ b/llvm/test/tools/llvm-objdump/X86/Inputs/macho-multiple-text diff --git a/llvm/test/tools/llvm-objdump/X86/macho-disassemble-all.test b/llvm/test/tools/llvm-objdump/X86/macho-disassemble-all.test new file mode 100644 index 00000000000..ce22d740087 --- /dev/null +++ b/llvm/test/tools/llvm-objdump/X86/macho-disassemble-all.test @@ -0,0 +1,39 @@ +// RUN: llvm-objdump -m -d -full-leading-addr -print-imm-hex -no-show-raw-insn %p/Inputs/macho-multiple-text | FileCheck %s -check-prefix=TEXT + +TEXT: (__TEXT,__text) section +TEXT_NEXT: _main: +TEXT_NEXT: 0000000100000f60 pushq %rbp +TEXT_NEXT: 0000000100000f61 movq %rsp, %rbp +TEXT_NEXT: 0000000100000f64 subq $0x10, %rsp +TEXT_NEXT: 0000000100000f68 movl $0x0, -0x4(%rbp) +TEXT_NEXT: 0000000100000f6f callq _hello +TEXT_NEXT: 0000000100000f74 xorl %eax, %eax +TEXT_NEXT: 0000000100000f76 addq $0x10, %rsp +TEXT_NEXT: 0000000100000f7a popq %rbp +TEXT_NEXT: 0000000100000f7b retq + +// RUN: llvm-objdump -m -D -full-leading-addr -print-imm-hex -no-show-raw-insn %p/Inputs/macho-multiple-text | FileCheck %s -check-prefix=ALL + +ALL: (__TEXT,__text) section +ALL_NEXT: _main: +ALL_NEXT: 0000000100000f60 pushq %rbp +ALL_NEXT: 0000000100000f61 movq %rsp, %rbp +ALL_NEXT: 0000000100000f64 subq $0x10, %rsp +ALL_NEXT: 0000000100000f68 movl $0x0, -0x4(%rbp) +ALL_NEXT: 0000000100000f6f callq _hello +ALL_NEXT: 0000000100000f74 xorl %eax, %eax +ALL_NEXT: 0000000100000f76 addq $0x10, %rsp +ALL_NEXT: 0000000100000f7a popq %rbp +ALL_NEXT: 0000000100000f7b retq +ALL: (__TEXT_EXEC,__text) section +ALL_NEXT: _hello: +ALL_NEXT: 0000000100001000 pushq %rbp +ALL_NEXT: 0000000100001001 movq %rsp, %rbp +ALL_NEXT: 0000000100001004 subq $0x10, %rsp +ALL_NEXT: 0000000100001008 leaq -0x71(%rip), %rdi ## literal pool for: "hello, world!\n" +ALL_NEXT: 000000010000100f movb $0x0, %al +ALL_NEXT: 0000000100001011 callq 0x100000f7c ## symbol stub for: _printf +ALL_NEXT: 0000000100001016 movl %eax, -0x4(%rbp) +ALL_NEXT: 0000000100001019 addq $0x10, %rsp +ALL_NEXT: 000000010000101d popq %rbp +ALL_NEXT: 000000010000101e retq diff --git a/llvm/tools/llvm-objdump/MachODump.cpp b/llvm/tools/llvm-objdump/MachODump.cpp index c8707ac1183..5ef7058ec9d 100644 --- a/llvm/tools/llvm-objdump/MachODump.cpp +++ b/llvm/tools/llvm-objdump/MachODump.cpp @@ -1610,8 +1610,19 @@ static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF, if (Disassemble || IndirectSymbols || !FilterSections.empty() || UnwindInfo) if (Error Err = MachOOF->checkSymbolTable()) report_error(ArchiveName, FileName, std::move(Err), ArchitectureName); - - if (Disassemble) { + + if (DisassembleAll) { + for (const SectionRef &Section : MachOOF->sections()) { + StringRef SectName; + Section.getName(SectName); + if (SectName.equals("__text")) { + DataRefImpl Ref = Section.getRawDataRefImpl(); + StringRef SegName = MachOOF->getSectionFinalSegmentName(Ref); + DisassembleMachO(FileName, MachOOF, SegName, SectName); + } + } + } + else if (Disassemble) { if (MachOOF->getHeader().filetype == MachO::MH_KEXT_BUNDLE && MachOOF->getHeader().cputype == MachO::CPU_TYPE_ARM64) DisassembleMachO(FileName, MachOOF, "__TEXT_EXEC", "__text"); |