[llvm] r294212 - Fix a bug in llvm-obdump(1) with the -macho and -disassemble options

Kevin Enderby via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 6 10:43:19 PST 2017


Author: enderby
Date: Mon Feb  6 12:43:18 2017
New Revision: 294212

URL: http://llvm.org/viewvc/llvm-project?rev=294212&view=rev
Log:
Fix a bug in llvm-obdump(1) with the -macho and -disassemble options
which caused it to not disassemble the bytes a the start of the section if
the section had symbols and the first symbol was not at the start of the
section.

rdar://30143243

Added:
    llvm/trunk/test/tools/llvm-objdump/X86/Inputs/nofirst-symbol.macho-x86_64   (with props)
    llvm/trunk/test/tools/llvm-objdump/X86/macho-nofirst-symbol-disassembly.test
Modified:
    llvm/trunk/tools/llvm-objdump/MachODump.cpp

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

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

Added: llvm/trunk/test/tools/llvm-objdump/X86/macho-nofirst-symbol-disassembly.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/X86/macho-nofirst-symbol-disassembly.test?rev=294212&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/X86/macho-nofirst-symbol-disassembly.test (added)
+++ llvm/trunk/test/tools/llvm-objdump/X86/macho-nofirst-symbol-disassembly.test Mon Feb  6 12:43:18 2017
@@ -0,0 +1,8 @@
+// RUN: llvm-objdump -d -m %p/Inputs/nofirst-symbol.macho-x86_64 | FileCheck %s
+
+CHECK:        0:	90 	nop
+CHECK: _foo:
+CHECK:        1:	c3 	retq
+CHECK: _bar:
+CHECK:        2:	90 	nop
+CHECK:        3:	c3 	retq

Modified: llvm/trunk/tools/llvm-objdump/MachODump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/MachODump.cpp?rev=294212&r1=294211&r2=294212&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/MachODump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/MachODump.cpp Mon Feb  6 12:43:18 2017
@@ -6602,6 +6602,12 @@ static void DisassembleMachO(StringRef F
     if (Bytes.size() == 0)
       return;
 
+    // If the section has symbols but no symbol at the start of the section
+    // these are used to make sure the bytes before the first symbol are
+    // disassembled.
+    bool FirstSymbol = true;
+    bool FirstSymbolAtSectionStart = true;
+
     // Disassemble symbol by symbol.
     for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
       Expected<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
@@ -6691,11 +6697,29 @@ static void DisassembleMachO(StringRef F
       // (i.e. we're not targeting M-class) and the function is Thumb.
       bool UseThumbTarget = IsThumb && ThumbTarget;
 
-      outs() << SymName << ":\n";
+      // If we are not specifying a symbol to start disassembly with and this
+      // is the first symbol in the section but not at the start of the section
+      // then move the disassembly index to the start of the section and
+      // don't print the symbol name just yet.  This is so the bytes before the
+      // first symbol are disassembled.
+      uint64_t SymbolStart = Start;
+      if (DisSymName.empty() && FirstSymbol && Start != 0) {
+        FirstSymbolAtSectionStart = false;
+        Start = 0;
+      }
+      else
+        outs() << SymName << ":\n";
+
       DILineInfo lastLine;
       for (uint64_t Index = Start; Index < End; Index += Size) {
         MCInst Inst;
 
+        // If this is the first symbol in the section and it was not at the
+        // start of the section, see if we are at its Index now and if so print
+        // the symbol name.
+        if (FirstSymbol && !FirstSymbolAtSectionStart && Index == SymbolStart)
+          outs() << SymName << ":\n";
+
         uint64_t PC = SectAddress + Index;
         if (!NoLeadingAddr) {
           if (FullLeadingAddr) {
@@ -6788,6 +6812,9 @@ static void DisassembleMachO(StringRef F
           }
         }
       }
+      // Now that we are done disassembled the first symbol set the bool that
+      // were doing this to false.
+      FirstSymbol = false;
     }
     if (!symbolTableWorked) {
       // Reading the symbol table didn't work, disassemble the whole section.




More information about the llvm-commits mailing list