[llvm-objdump] Print <unknown> in place of instruction text if it couldn't be disasse...
authorColin LeMahieu <colinl@codeaurora.org>
Fri, 18 Mar 2016 16:26:48 +0000 (16:26 +0000)
committerColin LeMahieu <colinl@codeaurora.org>
Fri, 18 Mar 2016 16:26:48 +0000 (16:26 +0000)
llvm-svn: 263793

llvm/test/MC/X86/invalid_opcode.s [new file with mode: 0644]
llvm/tools/llvm-objdump/llvm-objdump.cpp

diff --git a/llvm/test/MC/X86/invalid_opcode.s b/llvm/test/MC/X86/invalid_opcode.s
new file mode 100644 (file)
index 0000000..42e3aff
--- /dev/null
@@ -0,0 +1,4 @@
+# RUN: llvm-mc -triple=x86_64 -filetype=obj %s -o - | llvm-objdump -d - | FileCheck %s
+
+# CHECK: 0:       ff ff  <unknown>
+.word 0xffff
index eedfe30..f436183 100644 (file)
@@ -314,12 +314,15 @@ public:
                          ArrayRef<uint8_t> Bytes, uint64_t Address,
                          raw_ostream &OS, StringRef Annot,
                          MCSubtargetInfo const &STI) {
-    outs() << format("%8" PRIx64 ":", Address);
+    OS << format("%8" PRIx64 ":", Address);
     if (!NoShowRawInsn) {
-      outs() << "\t";
-      dumpBytes(Bytes, outs());
+      OS << "\t";
+      dumpBytes(Bytes, OS);
     }
-    IP.printInst(MI, outs(), "", STI);
+    if (MI)
+      IP.printInst(MI, OS, "", STI);
+    else
+      OS << " <unknown>";
   }
 };
 PrettyPrinter PrettyPrinterInst;
@@ -340,6 +343,11 @@ public:
                  ArrayRef<uint8_t> Bytes, uint64_t Address,
                  raw_ostream &OS, StringRef Annot,
                  MCSubtargetInfo const &STI) override {
+    if (!MI) {
+      printLead(Bytes, Address, OS);
+      OS << " <unknown>";
+      return;
+    }
     std::string Buffer;
     {
       raw_string_ostream TempStream(Buffer);
@@ -1088,72 +1096,69 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
         if (Index >= End)
           break;
 
-        if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
-                                   SectionAddr + Index, DebugOut,
-                                   CommentStream)) {
-          PIP.printInst(*IP, &Inst,
-                        Bytes.slice(Index, Size),
-                        SectionAddr + Index, outs(), "", *STI);
-          outs() << CommentStream.str();
-          Comments.clear();
-
-          // Try to resolve the target of a call, tail call, etc. to a specific
-          // symbol.
-          if (MIA && (MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) ||
-                      MIA->isConditionalBranch(Inst))) {
-            uint64_t Target;
-            if (MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) {
-              // In a relocatable object, the target's section must reside in
-              // the same section as the call instruction or it is accessed
-              // through a relocation.
-              //
-              // In a non-relocatable object, the target may be in any section.
-              //
-              // N.B. We don't walk the relocations in the relocatable case yet.
-              auto *TargetSectionSymbols = &Symbols;
-              if (!Obj->isRelocatableObject()) {
-                auto SectionAddress = std::upper_bound(
-                    SectionAddresses.begin(), SectionAddresses.end(), Target,
-                    [](uint64_t LHS,
-                       const std::pair<uint64_t, SectionRef> &RHS) {
-                      return LHS < RHS.first;
-                    });
-                if (SectionAddress != SectionAddresses.begin()) {
-                  --SectionAddress;
-                  TargetSectionSymbols = &AllSymbols[SectionAddress->second];
-                } else {
-                  TargetSectionSymbols = nullptr;
-                }
+        bool Disassembled = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
+                                                   SectionAddr + Index, DebugOut,
+                                                   CommentStream);
+        if (Size == 0)
+          Size = 1;
+        PIP.printInst(*IP, Disassembled ? &Inst : nullptr,
+                      Bytes.slice(Index, Size),
+                      SectionAddr + Index, outs(), "", *STI);
+        outs() << CommentStream.str();
+        Comments.clear();
+
+        // Try to resolve the target of a call, tail call, etc. to a specific
+        // symbol.
+        if (MIA && (MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) ||
+                    MIA->isConditionalBranch(Inst))) {
+          uint64_t Target;
+          if (MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) {
+            // In a relocatable object, the target's section must reside in
+            // the same section as the call instruction or it is accessed
+            // through a relocation.
+            //
+            // In a non-relocatable object, the target may be in any section.
+            //
+            // N.B. We don't walk the relocations in the relocatable case yet.
+            auto *TargetSectionSymbols = &Symbols;
+            if (!Obj->isRelocatableObject()) {
+              auto SectionAddress = std::upper_bound(
+                  SectionAddresses.begin(), SectionAddresses.end(), Target,
+                  [](uint64_t LHS,
+                      const std::pair<uint64_t, SectionRef> &RHS) {
+                    return LHS < RHS.first;
+                  });
+              if (SectionAddress != SectionAddresses.begin()) {
+                --SectionAddress;
+                TargetSectionSymbols = &AllSymbols[SectionAddress->second];
+              } else {
+                TargetSectionSymbols = nullptr;
               }
+            }
 
-              // Find the first symbol in the section whose offset is less than
-              // or equal to the target.
-              if (TargetSectionSymbols) {
-                auto TargetSym = std::upper_bound(
-                    TargetSectionSymbols->begin(), TargetSectionSymbols->end(),
-                    Target, [](uint64_t LHS,
-                               const std::pair<uint64_t, StringRef> &RHS) {
-                      return LHS < RHS.first;
-                    });
-                if (TargetSym != TargetSectionSymbols->begin()) {
-                  --TargetSym;
-                  uint64_t TargetAddress = std::get<0>(*TargetSym);
-                  StringRef TargetName = std::get<1>(*TargetSym);
-                  outs() << " <" << TargetName;
-                  uint64_t Disp = Target - TargetAddress;
-                  if (Disp)
-                    outs() << "+0x" << utohexstr(Disp);
-                  outs() << '>';
-                }
+            // Find the first symbol in the section whose offset is less than
+            // or equal to the target.
+            if (TargetSectionSymbols) {
+              auto TargetSym = std::upper_bound(
+                  TargetSectionSymbols->begin(), TargetSectionSymbols->end(),
+                  Target, [](uint64_t LHS,
+                              const std::pair<uint64_t, StringRef> &RHS) {
+                    return LHS < RHS.first;
+                  });
+              if (TargetSym != TargetSectionSymbols->begin()) {
+                --TargetSym;
+                uint64_t TargetAddress = std::get<0>(*TargetSym);
+                StringRef TargetName = std::get<1>(*TargetSym);
+                outs() << " <" << TargetName;
+                uint64_t Disp = Target - TargetAddress;
+                if (Disp)
+                  outs() << "+0x" << utohexstr(Disp);
+                outs() << '>';
               }
             }
           }
-          outs() << "\n";
-        } else {
-          errs() << ToolName << ": warning: invalid instruction encoding\n";
-          if (Size == 0)
-            Size = 1; // skip illegible bytes
         }
+        outs() << "\n";
 
         // Print relocation for instruction.
         while (rel_cur != rel_end) {