[llvm] r272837 - Fix llvm-objdump when disassembling a stripped Mach-O binary with the -macho option.

Kevin Enderby via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 15 14:14:01 PDT 2016


Author: enderby
Date: Wed Jun 15 16:14:01 2016
New Revision: 272837

URL: http://llvm.org/viewvc/llvm-project?rev=272837&view=rev
Log:
Fix llvm-objdump when disassembling a stripped Mach-O binary with the -macho option.
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

Added:
    llvm/trunk/test/tools/llvm-objdump/X86/Inputs/hello.exe.stripped.macho-x86_64   (with props)
    llvm/trunk/test/tools/llvm-objdump/X86/macho-disassembly-stripped.test
Modified:
    llvm/trunk/test/tools/llvm-objdump/X86/macho-dis-symname.test
    llvm/trunk/tools/llvm-objdump/MachODump.cpp

Added: llvm/trunk/test/tools/llvm-objdump/X86/Inputs/hello.exe.stripped.macho-x86_64
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/X86/Inputs/hello.exe.stripped.macho-x86_64?rev=272837&view=auto
==============================================================================
Binary file - no diff available.

Propchange: llvm/trunk/test/tools/llvm-objdump/X86/Inputs/hello.exe.stripped.macho-x86_64
------------------------------------------------------------------------------
    svn:executable = *

Propchange: llvm/trunk/test/tools/llvm-objdump/X86/Inputs/hello.exe.stripped.macho-x86_64
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Modified: llvm/trunk/test/tools/llvm-objdump/X86/macho-dis-symname.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/X86/macho-dis-symname.test?rev=272837&r1=272836&r2=272837&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/X86/macho-dis-symname.test (original)
+++ llvm/trunk/test/tools/llvm-objdump/X86/macho-dis-symname.test Wed Jun 15 16:14:01 2016
@@ -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

Added: llvm/trunk/test/tools/llvm-objdump/X86/macho-disassembly-stripped.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/X86/macho-disassembly-stripped.test?rev=272837&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/X86/macho-disassembly-stripped.test (added)
+++ llvm/trunk/test/tools/llvm-objdump/X86/macho-disassembly-stripped.test Wed Jun 15 16:14:01 2016
@@ -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

Modified: llvm/trunk/tools/llvm-objdump/MachODump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/MachODump.cpp?rev=272837&r1=272836&r2=272837&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/MachODump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/MachODump.cpp Wed Jun 15 16:14:01 2016
@@ -6677,7 +6677,27 @@ static void DisassembleMachO(StringRef F
 
       // 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.




More information about the llvm-commits mailing list