Fixed a bug in llvm-objdump when disassembling using -macho option for a binary
authorKevin Enderby <enderby@apple.com>
Fri, 18 May 2012 00:13:56 +0000 (00:13 +0000)
committerKevin Enderby <enderby@apple.com>
Fri, 18 May 2012 00:13:56 +0000 (00:13 +0000)
containing no symbols.  Fixed the crash and fixed it not disassembling anything.

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

test/MC/MachO/ARM/llvm-objdump-macho-stripped.s [new file with mode: 0644]
tools/llvm-objdump/MachODump.cpp

diff --git a/test/MC/MachO/ARM/llvm-objdump-macho-stripped.s b/test/MC/MachO/ARM/llvm-objdump-macho-stripped.s
new file mode 100644 (file)
index 0000000..7fcec52
--- /dev/null
@@ -0,0 +1,5 @@
+@ RUN: llvm-mc -triple=thumbv7-apple-ios -filetype=obj -o - < %s | llvm-objdump -d -macho -triple=thumbv7-apple-ios - | FileCheck %s
+       nop
+# CHECK:        0:     00 bf                                           nop
+# We are checking that disassembly happens when there are no symbols.
+# rdar://11460289
index 60c33f27892313e3396c08bae9212da81c90ec92..5e175a004d480266915a229b2a91bf5a3d39bf6e 100644 (file)
@@ -286,8 +286,10 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
 
   // Read and register the symbol table data.
   InMemoryStruct<macho::SymtabLoadCommand> SymtabLC;
-  MachOObj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC);
-  MachOObj->RegisterStringTable(*SymtabLC);
+  if (SymtabLCI) {
+    MachOObj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC);
+    MachOObj->RegisterStringTable(*SymtabLC);
+  }
 
   std::vector<SectionRef> Sections;
   std::vector<SymbolRef> Symbols;
@@ -498,6 +500,31 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
             InstrAnalysis.get(), Start, DebugOut, FunctionMap, Functions);
       }
     }
+    if (!CFG && !symbolTableWorked) {
+      // Reading the symbol table didn't work, disassemble the whole section. 
+      uint64_t SectAddress;
+      Sections[SectIdx].getAddress(SectAddress);
+      uint64_t SectSize;
+      Sections[SectIdx].getSize(SectSize);
+      uint64_t InstSize;
+      for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
+       MCInst Inst;
+
+       if (DisAsm->getInstruction(Inst, InstSize, memoryObject, Index,
+                                  DebugOut, nulls())) {
+         outs() << format("%8" PRIx64 ":\t", SectAddress + Index);
+
+         DumpBytes(StringRef(Bytes.data() + Index, InstSize));
+         IP->printInst(&Inst, outs(), "");
+
+         outs() << "\n";
+       } else {
+         errs() << "llvm-objdump: warning: invalid instruction encoding\n";
+         if (InstSize == 0)
+           InstSize = 1; // skip illegible bytes
+       }
+      }
+    }
 
     if (CFG) {
       if (!symbolTableWorked) {