[llvm] r263793 - [llvm-objdump] Print <unknown> in place of instruction text if it couldn't be disassembled.
Colin LeMahieu via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 18 09:26:48 PDT 2016
Author: colinl
Date: Fri Mar 18 11:26:48 2016
New Revision: 263793
URL: http://llvm.org/viewvc/llvm-project?rev=263793&view=rev
Log:
[llvm-objdump] Print <unknown> in place of instruction text if it couldn't be disassembled.
Added:
llvm/trunk/test/MC/X86/invalid_opcode.s
Modified:
llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
Added: llvm/trunk/test/MC/X86/invalid_opcode.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/X86/invalid_opcode.s?rev=263793&view=auto
==============================================================================
--- llvm/trunk/test/MC/X86/invalid_opcode.s (added)
+++ llvm/trunk/test/MC/X86/invalid_opcode.s Fri Mar 18 11:26:48 2016
@@ -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
Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp?rev=263793&r1=263792&r2=263793&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp Fri Mar 18 11:26:48 2016
@@ -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 Obje
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) {
More information about the llvm-commits
mailing list