Pass an ArrayRef to MCDisassembler::getInstruction.

With this patch MCDisassembler::getInstruction takes an ArrayRef<uint8_t>
instead of a MemoryObject.

Even on X86 there is a maximum size an instruction can have. Given
that, it seems way simpler and more efficient to just pass an ArrayRef
to the disassembler instead of a MemoryObject and have it do a virtual
call every time it wants some extra bytes.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@221751 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2014-11-12 02:04:27 +00:00
parent 9abbcb7453
commit 6a222ec893
17 changed files with 95 additions and 113 deletions

View File

@ -10,6 +10,7 @@
#define LLVM_MC_MCDISASSEMBLER_H #define LLVM_MC_MCDISASSEMBLER_H
#include "llvm-c/Disassembler.h" #include "llvm-c/Disassembler.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/MC/MCRelocationInfo.h" #include "llvm/MC/MCRelocationInfo.h"
#include "llvm/MC/MCSymbolizer.h" #include "llvm/MC/MCSymbolizer.h"
#include "llvm/Support/DataTypes.h" #include "llvm/Support/DataTypes.h"
@ -18,7 +19,6 @@ namespace llvm {
class MCInst; class MCInst;
class MCSubtargetInfo; class MCSubtargetInfo;
class MemoryObject;
class raw_ostream; class raw_ostream;
class MCContext; class MCContext;
@ -76,8 +76,8 @@ public:
/// disassemblable but invalid, /// disassemblable but invalid,
/// MCDisassembler::Fail if the instruction was invalid. /// MCDisassembler::Fail if the instruction was invalid.
virtual DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, virtual DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
const MemoryObject &Region, ArrayRef<uint8_t> Bytes, uint64_t Address,
uint64_t Address, raw_ostream &VStream, raw_ostream &VStream,
raw_ostream &CStream) const = 0; raw_ostream &CStream) const = 0;
private: private:

View File

@ -668,7 +668,8 @@ private:
bool decodeInst(StringRef Symbol, MCInst &Inst, uint64_t &Size) const { bool decodeInst(StringRef Symbol, MCInst &Inst, uint64_t &Size) const {
MCDisassembler *Dis = Checker.Disassembler; MCDisassembler *Dis = Checker.Disassembler;
StringRef SectionMem = Checker.getSubsectionStartingAt(Symbol); StringRef SectionMem = Checker.getSubsectionStartingAt(Symbol);
StringRefMemoryObject SectionBytes(SectionMem, 0); ArrayRef<uint8_t> SectionBytes((uint8_t *)SectionMem.begin(),
SectionMem.size());
MCDisassembler::DecodeStatus S = MCDisassembler::DecodeStatus S =
Dis->getInstruction(Inst, Size, SectionBytes, 0, nulls(), nulls()); Dis->getInstruction(Inst, Size, SectionBytes, 0, nulls(), nulls());

View File

@ -245,8 +245,7 @@ size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
size_t OutStringSize){ size_t OutStringSize){
LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
// Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject. // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
StringRef Data((const char*) Bytes, BytesSize); ArrayRef<uint8_t> Data(Bytes, BytesSize);
StringRefMemoryObject MemoryObject(Data, PC);
uint64_t Size; uint64_t Size;
MCInst Inst; MCInst Inst;
@ -255,7 +254,7 @@ size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
MCDisassembler::DecodeStatus S; MCDisassembler::DecodeStatus S;
SmallVector<char, 64> InsnStr; SmallVector<char, 64> InsnStr;
raw_svector_ostream Annotations(InsnStr); raw_svector_ostream Annotations(InsnStr);
S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC, S = DisAsm->getInstruction(Inst, Size, Data, 0,
/*REMOVE*/ nulls(), Annotations); /*REMOVE*/ nulls(), Annotations);
switch (S) { switch (S) {
case MCDisassembler::Fail: case MCDisassembler::Fail:

View File

@ -19,7 +19,6 @@
#include "llvm/MC/MCInst.h" #include "llvm/MC/MCInst.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetRegistry.h"
using namespace llvm; using namespace llvm;
@ -200,17 +199,15 @@ static MCDisassembler *createAArch64Disassembler(const Target &T,
} }
DecodeStatus AArch64Disassembler::getInstruction(MCInst &MI, uint64_t &Size, DecodeStatus AArch64Disassembler::getInstruction(MCInst &MI, uint64_t &Size,
const MemoryObject &Region, ArrayRef<uint8_t> Bytes,
uint64_t Address, uint64_t Address,
raw_ostream &OS, raw_ostream &OS,
raw_ostream &CS) const { raw_ostream &CS) const {
CommentStream = &CS; CommentStream = &CS;
uint8_t Bytes[4];
Size = 0; Size = 0;
// We want to read exactly 4 bytes of data. // We want to read exactly 4 bytes of data.
if (Region.readBytes(Address, 4, Bytes) == -1) if (Bytes.size() < 4)
return Fail; return Fail;
Size = 4; Size = 4;

View File

@ -29,7 +29,7 @@ public:
~AArch64Disassembler() {} ~AArch64Disassembler() {}
MCDisassembler::DecodeStatus MCDisassembler::DecodeStatus
getInstruction(MCInst &Instr, uint64_t &Size, const MemoryObject &Segion, getInstruction(MCInst &Instr, uint64_t &Size, ArrayRef<uint8_t> Bytes,
uint64_t Address, raw_ostream &VStream, uint64_t Address, raw_ostream &VStream,
raw_ostream &CStream) const override; raw_ostream &CStream) const override;
}; };

View File

@ -20,7 +20,6 @@
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/LEB128.h" #include "llvm/Support/LEB128.h"
#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include <vector> #include <vector>
@ -95,7 +94,7 @@ public:
~ARMDisassembler() {} ~ARMDisassembler() {}
DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
const MemoryObject &Region, uint64_t Address, ArrayRef<uint8_t> Bytes, uint64_t Address,
raw_ostream &VStream, raw_ostream &VStream,
raw_ostream &CStream) const override; raw_ostream &CStream) const override;
}; };
@ -110,7 +109,7 @@ public:
~ThumbDisassembler() {} ~ThumbDisassembler() {}
DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
const MemoryObject &Region, uint64_t Address, ArrayRef<uint8_t> Bytes, uint64_t Address,
raw_ostream &VStream, raw_ostream &VStream,
raw_ostream &CStream) const override; raw_ostream &CStream) const override;
@ -407,19 +406,17 @@ static MCDisassembler *createThumbDisassembler(const Target &T,
} }
DecodeStatus ARMDisassembler::getInstruction(MCInst &MI, uint64_t &Size, DecodeStatus ARMDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
const MemoryObject &Region, ArrayRef<uint8_t> Bytes,
uint64_t Address, raw_ostream &OS, uint64_t Address, raw_ostream &OS,
raw_ostream &CS) const { raw_ostream &CS) const {
CommentStream = &CS; CommentStream = &CS;
uint8_t Bytes[4];
assert(!(STI.getFeatureBits() & ARM::ModeThumb) && assert(!(STI.getFeatureBits() & ARM::ModeThumb) &&
"Asked to disassemble an ARM instruction but Subtarget is in Thumb " "Asked to disassemble an ARM instruction but Subtarget is in Thumb "
"mode!"); "mode!");
// We want to read exactly 4 bytes of data. // We want to read exactly 4 bytes of data.
if (Region.readBytes(Address, 4, Bytes) == -1) { if (Bytes.size() < 4) {
Size = 0; Size = 0;
return MCDisassembler::Fail; return MCDisassembler::Fail;
} }
@ -673,19 +670,17 @@ void ThumbDisassembler::UpdateThumbVFPPredicate(MCInst &MI) const {
} }
DecodeStatus ThumbDisassembler::getInstruction(MCInst &MI, uint64_t &Size, DecodeStatus ThumbDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
const MemoryObject &Region, ArrayRef<uint8_t> Bytes,
uint64_t Address, uint64_t Address,
raw_ostream &OS, raw_ostream &OS,
raw_ostream &CS) const { raw_ostream &CS) const {
CommentStream = &CS; CommentStream = &CS;
uint8_t Bytes[4];
assert((STI.getFeatureBits() & ARM::ModeThumb) && assert((STI.getFeatureBits() & ARM::ModeThumb) &&
"Asked to disassemble in Thumb mode but Subtarget is in ARM mode!"); "Asked to disassemble in Thumb mode but Subtarget is in ARM mode!");
// We want to read exactly 2 bytes of data. // We want to read exactly 2 bytes of data.
if (Region.readBytes(Address, 2, Bytes) == -1) { if (Bytes.size() < 2) {
Size = 0; Size = 0;
return MCDisassembler::Fail; return MCDisassembler::Fail;
} }
@ -737,7 +732,7 @@ DecodeStatus ThumbDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
} }
// We want to read exactly 4 bytes of data. // We want to read exactly 4 bytes of data.
if (Region.readBytes(Address, 4, Bytes) == -1) { if (Bytes.size() < 4) {
Size = 0; Size = 0;
return MCDisassembler::Fail; return MCDisassembler::Fail;
} }

View File

@ -20,7 +20,6 @@
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/LEB128.h" #include "llvm/Support/LEB128.h"
#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/Endian.h" #include "llvm/Support/Endian.h"
@ -43,7 +42,7 @@ public:
: MCDisassembler(STI, Ctx) {} : MCDisassembler(STI, Ctx) {}
DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
MemoryObject const &Region, uint64_t Address, ArrayRef<uint8_t> Bytes, uint64_t Address,
raw_ostream &VStream, raw_ostream &VStream,
raw_ostream &CStream) const override; raw_ostream &CStream) const override;
}; };
@ -63,13 +62,12 @@ extern "C" void LLVMInitializeHexagonDisassembler() {
} }
DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size, DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
MemoryObject const &Region, ArrayRef<uint8_t> Bytes,
uint64_t Address, uint64_t Address,
raw_ostream &os, raw_ostream &os,
raw_ostream &cs) const { raw_ostream &cs) const {
std::array<uint8_t, 4> Bytes;
Size = 4; Size = 4;
if (Region.readBytes(Address, Bytes.size(), Bytes.data()) == -1) if (Bytes.size() < 4)
return MCDisassembler::Fail; return MCDisassembler::Fail;
uint32_t insn = uint32_t insn =

View File

@ -20,7 +20,6 @@
#include "llvm/MC/MCInst.h" #include "llvm/MC/MCInst.h"
#include "llvm/MC/MCSubtargetInfo.h" #include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Support/MathExtras.h" #include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetRegistry.h"
using namespace llvm; using namespace llvm;
@ -73,7 +72,7 @@ public:
} }
DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
const MemoryObject &Region, uint64_t Address, ArrayRef<uint8_t> Bytes, uint64_t Address,
raw_ostream &VStream, raw_ostream &VStream,
raw_ostream &CStream) const override; raw_ostream &CStream) const override;
}; };
@ -86,7 +85,7 @@ public:
MipsDisassemblerBase(STI, Ctx, bigEndian) {} MipsDisassemblerBase(STI, Ctx, bigEndian) {}
DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
const MemoryObject &Region, uint64_t Address, ArrayRef<uint8_t> Bytes, uint64_t Address,
raw_ostream &VStream, raw_ostream &VStream,
raw_ostream &CStream) const override; raw_ostream &CStream) const override;
}; };
@ -697,16 +696,13 @@ static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
return MCDisassembler::Success; return MCDisassembler::Success;
} }
/// Read four bytes from the MemoryObject and return 32 bit word sorted /// Read four bytes from the ArrayRef and return 32 bit word sorted
/// according to the given endianess /// according to the given endianess
static DecodeStatus readInstruction32(const MemoryObject &Region, static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
uint64_t Address, uint64_t &Size, uint64_t &Size, uint32_t &Insn,
uint32_t &Insn, bool IsBigEndian, bool IsBigEndian, bool IsMicroMips) {
bool IsMicroMips) {
uint8_t Bytes[4];
// We want to read exactly 4 Bytes of data. // We want to read exactly 4 Bytes of data.
if (Region.readBytes(Address, 4, Bytes) == -1) { if (Bytes.size() < 4) {
Size = 0; Size = 0;
return MCDisassembler::Fail; return MCDisassembler::Fail;
} }
@ -733,14 +729,14 @@ static DecodeStatus readInstruction32(const MemoryObject &Region,
} }
DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size, DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
const MemoryObject &Region, ArrayRef<uint8_t> Bytes,
uint64_t Address, uint64_t Address,
raw_ostream &VStream, raw_ostream &VStream,
raw_ostream &CStream) const { raw_ostream &CStream) const {
uint32_t Insn; uint32_t Insn;
DecodeStatus Result = DecodeStatus Result =
readInstruction32(Region, Address, Size, Insn, IsBigEndian, IsMicroMips); readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, IsMicroMips);
if (Result == MCDisassembler::Fail) if (Result == MCDisassembler::Fail)
return MCDisassembler::Fail; return MCDisassembler::Fail;
@ -799,14 +795,14 @@ DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
} }
DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size, DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
const MemoryObject &Region, ArrayRef<uint8_t> Bytes,
uint64_t Address, uint64_t Address,
raw_ostream &VStream, raw_ostream &VStream,
raw_ostream &CStream) const { raw_ostream &CStream) const {
uint32_t Insn; uint32_t Insn;
DecodeStatus Result = DecodeStatus Result =
readInstruction32(Region, Address, Size, Insn, IsBigEndian, false); readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
if (Result == MCDisassembler::Fail) if (Result == MCDisassembler::Fail)
return MCDisassembler::Fail; return MCDisassembler::Fail;

View File

@ -12,7 +12,6 @@
#include "llvm/MC/MCFixedLenDisassembler.h" #include "llvm/MC/MCFixedLenDisassembler.h"
#include "llvm/MC/MCInst.h" #include "llvm/MC/MCInst.h"
#include "llvm/MC/MCSubtargetInfo.h" #include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetRegistry.h"
using namespace llvm; using namespace llvm;
@ -29,7 +28,7 @@ public:
virtual ~PPCDisassembler() {} virtual ~PPCDisassembler() {}
DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
const MemoryObject &Region, uint64_t Address, ArrayRef<uint8_t> Bytes, uint64_t Address,
raw_ostream &VStream, raw_ostream &VStream,
raw_ostream &CStream) const override; raw_ostream &CStream) const override;
}; };
@ -322,13 +321,12 @@ static DecodeStatus decodeCRBitMOperand(MCInst &Inst, uint64_t Imm,
#include "PPCGenDisassemblerTables.inc" #include "PPCGenDisassemblerTables.inc"
DecodeStatus PPCDisassembler::getInstruction(MCInst &MI, uint64_t &Size, DecodeStatus PPCDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
const MemoryObject &Region, ArrayRef<uint8_t> Bytes,
uint64_t Address, raw_ostream &OS, uint64_t Address, raw_ostream &OS,
raw_ostream &CS) const { raw_ostream &CS) const {
// Get the four bytes of the instruction. // Get the four bytes of the instruction.
uint8_t Bytes[4];
Size = 4; Size = 4;
if (Region.readBytes(Address, Size, Bytes) == -1) { if (Bytes.size() < 4) {
Size = 0; Size = 0;
return MCDisassembler::Fail; return MCDisassembler::Fail;
} }

View File

@ -16,7 +16,6 @@
#include "SparcSubtarget.h" #include "SparcSubtarget.h"
#include "llvm/MC/MCDisassembler.h" #include "llvm/MC/MCDisassembler.h"
#include "llvm/MC/MCFixedLenDisassembler.h" #include "llvm/MC/MCFixedLenDisassembler.h"
#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetRegistry.h"
using namespace llvm; using namespace llvm;
@ -35,7 +34,7 @@ public:
virtual ~SparcDisassembler() {} virtual ~SparcDisassembler() {}
DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
const MemoryObject &Region, uint64_t Address, ArrayRef<uint8_t> Bytes, uint64_t Address,
raw_ostream &VStream, raw_ostream &VStream,
raw_ostream &CStream) const override; raw_ostream &CStream) const override;
}; };
@ -207,14 +206,11 @@ static DecodeStatus DecodeSWAP(MCInst &Inst, unsigned insn, uint64_t Address,
#include "SparcGenDisassemblerTables.inc" #include "SparcGenDisassemblerTables.inc"
/// Read four bytes from the MemoryObject and return 32 bit word. /// Read four bytes from the ArrayRef and return 32 bit word.
static DecodeStatus readInstruction32(const MemoryObject &Region, static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
uint64_t Address, uint64_t &Size, uint64_t &Size, uint32_t &Insn) {
uint32_t &Insn) {
uint8_t Bytes[4];
// We want to read exactly 4 Bytes of data. // We want to read exactly 4 Bytes of data.
if (Region.readBytes(Address, 4, Bytes) == -1) { if (Bytes.size() < 4) {
Size = 0; Size = 0;
return MCDisassembler::Fail; return MCDisassembler::Fail;
} }
@ -227,13 +223,13 @@ static DecodeStatus readInstruction32(const MemoryObject &Region,
} }
DecodeStatus SparcDisassembler::getInstruction(MCInst &Instr, uint64_t &Size, DecodeStatus SparcDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
const MemoryObject &Region, ArrayRef<uint8_t> Bytes,
uint64_t Address, uint64_t Address,
raw_ostream &VStream, raw_ostream &VStream,
raw_ostream &CStream) const { raw_ostream &CStream) const {
uint32_t Insn; uint32_t Insn;
DecodeStatus Result = readInstruction32(Region, Address, Size, Insn); DecodeStatus Result = readInstruction32(Bytes, Address, Size, Insn);
if (Result == MCDisassembler::Fail) if (Result == MCDisassembler::Fail)
return MCDisassembler::Fail; return MCDisassembler::Fail;

View File

@ -12,7 +12,6 @@
#include "llvm/MC/MCFixedLenDisassembler.h" #include "llvm/MC/MCFixedLenDisassembler.h"
#include "llvm/MC/MCInst.h" #include "llvm/MC/MCInst.h"
#include "llvm/MC/MCSubtargetInfo.h" #include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetRegistry.h"
using namespace llvm; using namespace llvm;
@ -28,8 +27,8 @@ public:
: MCDisassembler(STI, Ctx) {} : MCDisassembler(STI, Ctx) {}
virtual ~SystemZDisassembler() {} virtual ~SystemZDisassembler() {}
DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, DecodeStatus getInstruction(MCInst &instr, uint64_t &Size,
const MemoryObject &Region, uint64_t Address, ArrayRef<uint8_t> Bytes, uint64_t Address,
raw_ostream &VStream, raw_ostream &VStream,
raw_ostream &CStream) const override; raw_ostream &CStream) const override;
}; };
@ -287,14 +286,13 @@ static DecodeStatus decodeBDLAddr64Disp12Len8Operand(MCInst &Inst,
#include "SystemZGenDisassemblerTables.inc" #include "SystemZGenDisassemblerTables.inc"
DecodeStatus SystemZDisassembler::getInstruction(MCInst &MI, uint64_t &Size, DecodeStatus SystemZDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
const MemoryObject &Region, ArrayRef<uint8_t> Bytes,
uint64_t Address, uint64_t Address,
raw_ostream &OS, raw_ostream &OS,
raw_ostream &CS) const { raw_ostream &CS) const {
// Get the first two bytes of the instruction. // Get the first two bytes of the instruction.
uint8_t Bytes[6];
Size = 0; Size = 0;
if (Region.readBytes(Address, 2, Bytes) == -1) if (Bytes.size() < 2)
return MCDisassembler::Fail; return MCDisassembler::Fail;
// The top 2 bits of the first byte specify the size. // The top 2 bits of the first byte specify the size.
@ -311,7 +309,7 @@ DecodeStatus SystemZDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
} }
// Read any remaining bytes. // Read any remaining bytes.
if (Size > 2 && Region.readBytes(Address + 2, Size - 2, Bytes + 2) == -1) if (Bytes.size() < Size)
return MCDisassembler::Fail; return MCDisassembler::Fail;
// Construct the instruction. // Construct the instruction.

View File

@ -23,7 +23,6 @@
#include "llvm/MC/MCInstrInfo.h" #include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCSubtargetInfo.h" #include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
@ -97,15 +96,26 @@ X86GenericDisassembler::X86GenericDisassembler(
} }
} }
/// A callback function that wraps the readByte method from MemoryObject. struct Region {
ArrayRef<uint8_t> Bytes;
uint64_t Base;
Region(ArrayRef<uint8_t> Bytes, uint64_t Base) : Bytes(Bytes), Base(Base) {}
};
/// A callback function that wraps the readByte method from Region.
/// ///
/// @param Arg - The generic callback parameter. In this case, this should /// @param Arg - The generic callback parameter. In this case, this should
/// be a pointer to a MemoryObject. /// be a pointer to a Region.
/// @param Byte - A pointer to the byte to be read. /// @param Byte - A pointer to the byte to be read.
/// @param Address - The address to be read. /// @param Address - The address to be read.
static int regionReader(const void *Arg, uint8_t *Byte, uint64_t Address) { static int regionReader(const void *Arg, uint8_t *Byte, uint64_t Address) {
const MemoryObject *Region = static_cast<const MemoryObject *>(Arg); auto *R = static_cast<const Region *>(Arg);
return Region->readByte(Address, Byte); ArrayRef<uint8_t> Bytes = R->Bytes;
unsigned Index = Address - R->Base;
if (Bytes.size() <= Index)
return -1;
*Byte = Bytes[Index];
return 0;
} }
/// logger - a callback function that wraps the operator<< method from /// logger - a callback function that wraps the operator<< method from
@ -127,7 +137,7 @@ static void logger(void* arg, const char* log) {
// //
MCDisassembler::DecodeStatus X86GenericDisassembler::getInstruction( MCDisassembler::DecodeStatus X86GenericDisassembler::getInstruction(
MCInst &Instr, uint64_t &Size, const MemoryObject &Region, uint64_t Address, MCInst &Instr, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t Address,
raw_ostream &VStream, raw_ostream &CStream) const { raw_ostream &VStream, raw_ostream &CStream) const {
CommentStream = &CStream; CommentStream = &CStream;
@ -137,8 +147,10 @@ MCDisassembler::DecodeStatus X86GenericDisassembler::getInstruction(
if (&VStream == &nulls()) if (&VStream == &nulls())
LoggerFn = nullptr; // Disable logging completely if it's going to nulls(). LoggerFn = nullptr; // Disable logging completely if it's going to nulls().
int Ret = decodeInstruction(&InternalInstr, regionReader, Region R(Bytes, Address);
(const void *)&Region, LoggerFn, (void *)&VStream,
int Ret = decodeInstruction(&InternalInstr, regionReader, (const void *)&R,
LoggerFn, (void *)&VStream,
(const void *)MII.get(), Address, fMode); (const void *)MII.get(), Address, fMode);
if (Ret) { if (Ret) {

View File

@ -97,7 +97,7 @@ public:
std::unique_ptr<const MCInstrInfo> MII); std::unique_ptr<const MCInstrInfo> MII);
public: public:
DecodeStatus getInstruction(MCInst &instr, uint64_t &size, DecodeStatus getInstruction(MCInst &instr, uint64_t &size,
const MemoryObject &region, uint64_t address, ArrayRef<uint8_t> Bytes, uint64_t Address,
raw_ostream &vStream, raw_ostream &vStream,
raw_ostream &cStream) const override; raw_ostream &cStream) const override;

View File

@ -19,7 +19,6 @@
#include "llvm/MC/MCFixedLenDisassembler.h" #include "llvm/MC/MCFixedLenDisassembler.h"
#include "llvm/MC/MCInst.h" #include "llvm/MC/MCInst.h"
#include "llvm/MC/MCSubtargetInfo.h" #include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetRegistry.h"
using namespace llvm; using namespace llvm;
@ -37,18 +36,16 @@ public:
MCDisassembler(STI, Ctx) {} MCDisassembler(STI, Ctx) {}
DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
const MemoryObject &Region, uint64_t Address, ArrayRef<uint8_t> Bytes, uint64_t Address,
raw_ostream &VStream, raw_ostream &VStream,
raw_ostream &CStream) const override; raw_ostream &CStream) const override;
}; };
} }
static bool readInstruction16(const MemoryObject &Region, uint64_t Address, static bool readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
uint64_t &Size, uint16_t &Insn) { uint64_t &Size, uint16_t &Insn) {
uint8_t Bytes[4];
// We want to read exactly 2 Bytes of data. // We want to read exactly 2 Bytes of data.
if (Region.readBytes(Address, 2, Bytes) == -1) { if (Bytes.size() < 2) {
Size = 0; Size = 0;
return false; return false;
} }
@ -57,12 +54,10 @@ static bool readInstruction16(const MemoryObject &Region, uint64_t Address,
return true; return true;
} }
static bool readInstruction32(const MemoryObject &Region, uint64_t Address, static bool readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
uint64_t &Size, uint32_t &Insn) { uint64_t &Size, uint32_t &Insn) {
uint8_t Bytes[4];
// We want to read exactly 4 Bytes of data. // We want to read exactly 4 Bytes of data.
if (Region.readBytes(Address, 4, Bytes) == -1) { if (Bytes.size() < 4) {
Size = 0; Size = 0;
return false; return false;
} }
@ -741,11 +736,11 @@ DecodeL4RSrcDstSrcDstInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
} }
MCDisassembler::DecodeStatus XCoreDisassembler::getInstruction( MCDisassembler::DecodeStatus XCoreDisassembler::getInstruction(
MCInst &instr, uint64_t &Size, const MemoryObject &Region, uint64_t Address, MCInst &instr, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t Address,
raw_ostream &vStream, raw_ostream &cStream) const { raw_ostream &vStream, raw_ostream &cStream) const {
uint16_t insn16; uint16_t insn16;
if (!readInstruction16(Region, Address, Size, insn16)) { if (!readInstruction16(Bytes, Address, Size, insn16)) {
return Fail; return Fail;
} }
@ -759,7 +754,7 @@ MCDisassembler::DecodeStatus XCoreDisassembler::getInstruction(
uint32_t insn32; uint32_t insn32;
if (!readInstruction32(Region, Address, Size, insn32)) { if (!readInstruction32(Bytes, Address, Size, insn32)) {
return Fail; return Fail;
} }

View File

@ -38,8 +38,7 @@ static bool PrintInsts(const MCDisassembler &DisAsm,
MCStreamer &Streamer, bool InAtomicBlock, MCStreamer &Streamer, bool InAtomicBlock,
const MCSubtargetInfo &STI) { const MCSubtargetInfo &STI) {
// Wrap the vector in a MemoryObject. // Wrap the vector in a MemoryObject.
StringRef Data((const char*)Bytes.first.data(), Bytes.first.size()); ArrayRef<uint8_t> Data(Bytes.first.data(), Bytes.first.size());
StringRefMemoryObject memoryObject(Data);
// Disassemble it to strings. // Disassemble it to strings.
uint64_t Size; uint64_t Size;
@ -49,7 +48,7 @@ static bool PrintInsts(const MCDisassembler &DisAsm,
MCInst Inst; MCInst Inst;
MCDisassembler::DecodeStatus S; MCDisassembler::DecodeStatus S;
S = DisAsm.getInstruction(Inst, Size, memoryObject, Index, S = DisAsm.getInstruction(Inst, Size, Data.slice(Index), Index,
/*REMOVE*/ nulls(), nulls()); /*REMOVE*/ nulls(), nulls());
switch (S) { switch (S) {
case MCDisassembler::Fail: case MCDisassembler::Fail:

View File

@ -1611,11 +1611,11 @@ static void DisassembleInputMachO2(StringRef Filename,
if (SegmentName != "__TEXT") if (SegmentName != "__TEXT")
continue; continue;
StringRef Bytes; StringRef BytesStr;
Sections[SectIdx].getContents(Bytes); Sections[SectIdx].getContents(BytesStr);
ArrayRef<uint8_t> Bytes((uint8_t *)BytesStr.data(), BytesStr.size());
uint64_t SectAddress = Sections[SectIdx].getAddress(); uint64_t SectAddress = Sections[SectIdx].getAddress();
StringRefMemoryObject MemoryObject(Bytes, SectAddress);
bool symbolTableWorked = false; bool symbolTableWorked = false;
// Parse relocations. // Parse relocations.
@ -1715,9 +1715,6 @@ static void DisassembleInputMachO2(StringRef Filename,
symbolTableWorked = true; symbolTableWorked = true;
StringRef Data(Bytes.data() + Start, End - Start);
StringRefMemoryObject SectionMemoryObject(Data, SectAddress + Start);
DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl(); DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
bool isThumb = bool isThumb =
(MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget; (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
@ -1751,7 +1748,7 @@ static void DisassembleInputMachO2(StringRef Filename,
DTI->second.getLength(Length); DTI->second.getLength(Length);
uint16_t Kind; uint16_t Kind;
DTI->second.getKind(Kind); DTI->second.getKind(Kind);
Size = DumpDataInCode(Bytes.data() + Index, Length, Kind); Size = DumpDataInCode((char *)Bytes.data() + Index, Length, Kind);
if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) && if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
(PC == (DTI->first + Length - 1)) && (Length & 1)) (PC == (DTI->first + Length - 1)) && (Length & 1))
Size++; Size++;
@ -1763,14 +1760,14 @@ static void DisassembleInputMachO2(StringRef Filename,
bool gotInst; bool gotInst;
if (isThumb) if (isThumb)
gotInst = ThumbDisAsm->getInstruction(Inst, Size, SectionMemoryObject, gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
PC, DebugOut, Annotations); PC, DebugOut, Annotations);
else else
gotInst = DisAsm->getInstruction(Inst, Size, SectionMemoryObject, PC, gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
DebugOut, Annotations); DebugOut, Annotations);
if (gotInst) { if (gotInst) {
if (!NoShowRawInsn) { if (!NoShowRawInsn) {
DumpBytes(StringRef(Bytes.data() + Index, Size)); DumpBytes(StringRef((char *)Bytes.data() + Index, Size));
} }
formatted_raw_ostream FormattedOS(outs()); formatted_raw_ostream FormattedOS(outs());
Annotations.flush(); Annotations.flush();
@ -1814,8 +1811,8 @@ static void DisassembleInputMachO2(StringRef Filename,
MCInst Inst; MCInst Inst;
uint64_t PC = SectAddress + Index; uint64_t PC = SectAddress + Index;
if (DisAsm->getInstruction(Inst, InstSize, MemoryObject, PC, DebugOut, if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
nulls())) { DebugOut, nulls())) {
if (FullLeadingAddr) { if (FullLeadingAddr) {
if (MachOOF->is64Bit()) if (MachOOF->is64Bit())
outs() << format("%016" PRIx64, PC); outs() << format("%016" PRIx64, PC);
@ -1826,7 +1823,7 @@ static void DisassembleInputMachO2(StringRef Filename,
} }
if (!NoShowRawInsn) { if (!NoShowRawInsn) {
outs() << "\t"; outs() << "\t";
DumpBytes(StringRef(Bytes.data() + Index, InstSize)); DumpBytes(StringRef((char *)Bytes.data() + Index, InstSize));
} }
IP->printInst(&Inst, outs(), ""); IP->printInst(&Inst, outs(), "");
outs() << "\n"; outs() << "\n";

View File

@ -374,10 +374,11 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
SmallString<40> Comments; SmallString<40> Comments;
raw_svector_ostream CommentStream(Comments); raw_svector_ostream CommentStream(Comments);
StringRef Bytes; StringRef BytesStr;
if (error(Section.getContents(Bytes))) if (error(Section.getContents(BytesStr)))
break; break;
StringRefMemoryObject memoryObject(Bytes, SectionAddr); ArrayRef<uint8_t> Bytes((uint8_t *)BytesStr.data(), BytesStr.size());
uint64_t Size; uint64_t Size;
uint64_t Index; uint64_t Index;
@ -404,13 +405,13 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
for (Index = Start; Index < End; Index += Size) { for (Index = Start; Index < End; Index += Size) {
MCInst Inst; MCInst Inst;
if (DisAsm->getInstruction(Inst, Size, memoryObject, if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
SectionAddr + Index, SectionAddr + Index, DebugOut,
DebugOut, CommentStream)) { CommentStream)) {
outs() << format("%8" PRIx64 ":", SectionAddr + Index); outs() << format("%8" PRIx64 ":", SectionAddr + Index);
if (!NoShowRawInsn) { if (!NoShowRawInsn) {
outs() << "\t"; outs() << "\t";
DumpBytes(StringRef(Bytes.data() + Index, Size)); DumpBytes(StringRef((char *)Bytes.data() + Index, Size));
} }
IP->printInst(&Inst, outs(), ""); IP->printInst(&Inst, outs(), "");
outs() << CommentStream.str(); outs() << CommentStream.str();