]> granicus.if.org Git - llvm/commitdiff
Fix llvm-objdump when disassembling a stripped Mach-O binary with the -macho option.
authorKevin Enderby <enderby@apple.com>
Wed, 15 Jun 2016 21:14:01 +0000 (21:14 +0000)
committerKevin Enderby <enderby@apple.com>
Wed, 15 Jun 2016 21:14:01 +0000 (21:14 +0000)
It was printing out nothing in this case.

llvm-objdump tries to disassemble sections a symbol at a time.  In the case of a
fully stripped Mach-O executable the only symbol remaining in the (__TEXT,__text)
section is the special linker defined symbol __mh_execute_header . This
symbol is special in that while it is N_SECT symbol in the (__TEXT,__text)
its address is before the start of the (__TEXT,__text).  It’s address is the
start of the __TEXT segment which is where the mach header is statically
linked. So the code in DisassembleMachO() needs to deal with this case specially.

rdar://26778273

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@272837 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/llvm-objdump/X86/Inputs/hello.exe.stripped.macho-x86_64 [new file with mode: 0755]
test/tools/llvm-objdump/X86/macho-dis-symname.test
test/tools/llvm-objdump/X86/macho-disassembly-stripped.test [new file with mode: 0644]
tools/llvm-objdump/MachODump.cpp

diff --git a/test/tools/llvm-objdump/X86/Inputs/hello.exe.stripped.macho-x86_64 b/test/tools/llvm-objdump/X86/Inputs/hello.exe.stripped.macho-x86_64
new file mode 100755 (executable)
index 0000000..9c52d16
Binary files /dev/null and b/test/tools/llvm-objdump/X86/Inputs/hello.exe.stripped.macho-x86_64 differ
index 39d16ecba6f80b445d9dbcc38152ffa8b3ecc8da..9e4f2e3c71f28170bb1853aeb19a2eb423716084 100644 (file)
@@ -17,3 +17,9 @@
 # CHECK-NOT: __start:
 # CHECK-NOT: 0000000100000d22
 # CHECK-NOT: _main:
+
+# not RUN: llvm-objdump -m -d %p/Inputs/exeThread.macho-x86_64 -dis-symname _environ 2>&1 | FileCheck -check-prefix BAD-SYMAME-1 %s
+BAD-SYMAME-1: -dis-symname: _environ not in the section
+
+# not RUN: llvm-objdump -m -d %p/Inputs/exeThread.macho-x86_64 -dis-symname __mh_execute_header 2>&1 | FileCheck -check-prefix BAD-SYMAME-2 %s
+BAD-SYMAME-2: -dis-symname: __mh_execute_header not in any section
diff --git a/test/tools/llvm-objdump/X86/macho-disassembly-stripped.test b/test/tools/llvm-objdump/X86/macho-disassembly-stripped.test
new file mode 100644 (file)
index 0000000..fab86f8
--- /dev/null
@@ -0,0 +1,6 @@
+// RUN: llvm-objdump -d -m -no-show-raw-insn -full-leading-addr -print-imm-hex %p/Inputs/hello.exe.stripped.macho-x86_64 | FileCheck %s
+
+CHECK: (__TEXT,__text) section
+CHECK: 0000000100000f30        pushq   %rbp
+CHECK: 0000000100000f31        movq    %rsp, %rbp
+CHECK: 0000000100000f34        subq    $0x20, %rsp
index fe7cf0dd335896679d5a3e347ddc341df9c81a16..3f4185e63307f6f208f51660ce3f10dda6872308 100644 (file)
@@ -6677,7 +6677,27 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
 
       // Make sure the symbol is defined in this section.
       bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
-      if (!containsSym)
+      if (!containsSym) {
+        if (!DisSymName.empty() && DisSymName == SymName) {
+          outs() << "-dis-symname: " << DisSymName << " not in the section\n";
+          return;
+       }
+        continue;
+      }
+      // The __mh_execute_header is special and we need to deal with that fact
+      // this symbol is before the start of the (__TEXT,__text) section and at the
+      // address of the start of the __TEXT segment.  This is because this symbol
+      // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
+      // start of the section in a standard MH_EXECUTE filetype.
+      if (!DisSymName.empty() && DisSymName == "__mh_execute_header") {
+        outs() << "-dis-symname: __mh_execute_header not in any section\n";
+        return;
+      }
+      // When this code is trying to disassemble a symbol at a time and in the case
+      // there is only the __mh_execute_header symbol left as in a stripped
+      // executable, we need to deal with this by ignoring this symbol so the whole
+      // section is disassembled and this symbol is then not displayed.
+      if (SymName == "__mh_execute_header")
         continue;
 
       // If we are only disassembling one symbol see if this is that symbol.