#define LLVM_MC_MCDISASSEMBLER_H
#include "llvm-c/Disassembler.h"
+#include "llvm/ADT/ArrayRef.h"
#include "llvm/MC/MCRelocationInfo.h"
#include "llvm/MC/MCSymbolizer.h"
#include "llvm/Support/DataTypes.h"
class MCInst;
class MCSubtargetInfo;
-class MemoryObject;
class raw_ostream;
class MCContext;
/// disassemblable but invalid,
/// MCDisassembler::Fail if the instruction was invalid.
virtual DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
- const MemoryObject &Region,
- uint64_t Address, raw_ostream &VStream,
+ ArrayRef<uint8_t> Bytes, uint64_t Address,
+ raw_ostream &VStream,
raw_ostream &CStream) const = 0;
private:
bool decodeInst(StringRef Symbol, MCInst &Inst, uint64_t &Size) const {
MCDisassembler *Dis = Checker.Disassembler;
StringRef SectionMem = Checker.getSubsectionStartingAt(Symbol);
- StringRefMemoryObject SectionBytes(SectionMem, 0);
+ ArrayRef<uint8_t> SectionBytes((uint8_t *)SectionMem.begin(),
+ SectionMem.size());
MCDisassembler::DecodeStatus S =
Dis->getInstruction(Inst, Size, SectionBytes, 0, nulls(), nulls());
size_t OutStringSize){
LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
// Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
- StringRef Data((const char*) Bytes, BytesSize);
- StringRefMemoryObject MemoryObject(Data, PC);
+ ArrayRef<uint8_t> Data(Bytes, BytesSize);
uint64_t Size;
MCInst Inst;
MCDisassembler::DecodeStatus S;
SmallVector<char, 64> InsnStr;
raw_svector_ostream Annotations(InsnStr);
- S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
+ S = DisAsm->getInstruction(Inst, Size, Data, 0,
/*REMOVE*/ nulls(), Annotations);
switch (S) {
case MCDisassembler::Fail:
#include "llvm/MC/MCInst.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
-#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/TargetRegistry.h"
using namespace llvm;
}
DecodeStatus AArch64Disassembler::getInstruction(MCInst &MI, uint64_t &Size,
- const MemoryObject &Region,
+ ArrayRef<uint8_t> Bytes,
uint64_t Address,
raw_ostream &OS,
raw_ostream &CS) const {
CommentStream = &CS;
- uint8_t Bytes[4];
-
Size = 0;
// We want to read exactly 4 bytes of data.
- if (Region.readBytes(Address, 4, Bytes) == -1)
+ if (Bytes.size() < 4)
return Fail;
Size = 4;
~AArch64Disassembler() {}
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,
raw_ostream &CStream) const override;
};
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/LEB128.h"
-#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/raw_ostream.h"
#include <vector>
~ARMDisassembler() {}
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 &CStream) const override;
};
~ThumbDisassembler() {}
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 &CStream) const override;
}
DecodeStatus ARMDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
- const MemoryObject &Region,
+ ArrayRef<uint8_t> Bytes,
uint64_t Address, raw_ostream &OS,
raw_ostream &CS) const {
CommentStream = &CS;
- uint8_t Bytes[4];
-
assert(!(STI.getFeatureBits() & ARM::ModeThumb) &&
"Asked to disassemble an ARM instruction but Subtarget is in Thumb "
"mode!");
// We want to read exactly 4 bytes of data.
- if (Region.readBytes(Address, 4, Bytes) == -1) {
+ if (Bytes.size() < 4) {
Size = 0;
return MCDisassembler::Fail;
}
}
DecodeStatus ThumbDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
- const MemoryObject &Region,
+ ArrayRef<uint8_t> Bytes,
uint64_t Address,
raw_ostream &OS,
raw_ostream &CS) const {
CommentStream = &CS;
- uint8_t Bytes[4];
-
assert((STI.getFeatureBits() & ARM::ModeThumb) &&
"Asked to disassemble in Thumb mode but Subtarget is in ARM mode!");
// We want to read exactly 2 bytes of data.
- if (Region.readBytes(Address, 2, Bytes) == -1) {
+ if (Bytes.size() < 2) {
Size = 0;
return MCDisassembler::Fail;
}
}
// We want to read exactly 4 bytes of data.
- if (Region.readBytes(Address, 4, Bytes) == -1) {
+ if (Bytes.size() < 4) {
Size = 0;
return MCDisassembler::Fail;
}
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/LEB128.h"
-#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/Endian.h"
: MCDisassembler(STI, Ctx) {}
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 &CStream) const override;
};
}
DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
- MemoryObject const &Region,
+ ArrayRef<uint8_t> Bytes,
uint64_t Address,
raw_ostream &os,
raw_ostream &cs) const {
- std::array<uint8_t, 4> Bytes;
Size = 4;
- if (Region.readBytes(Address, Bytes.size(), Bytes.data()) == -1)
+ if (Bytes.size() < 4)
return MCDisassembler::Fail;
uint32_t insn =
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Support/MathExtras.h"
-#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/TargetRegistry.h"
using namespace llvm;
}
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 &CStream) const override;
};
MipsDisassemblerBase(STI, Ctx, bigEndian) {}
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 &CStream) const override;
};
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
-static DecodeStatus readInstruction32(const MemoryObject &Region,
- uint64_t Address, uint64_t &Size,
- uint32_t &Insn, bool IsBigEndian,
- bool IsMicroMips) {
- uint8_t Bytes[4];
-
+static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
+ uint64_t &Size, uint32_t &Insn,
+ bool IsBigEndian, bool IsMicroMips) {
// We want to read exactly 4 Bytes of data.
- if (Region.readBytes(Address, 4, Bytes) == -1) {
+ if (Bytes.size() < 4) {
Size = 0;
return MCDisassembler::Fail;
}
}
DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
- const MemoryObject &Region,
+ ArrayRef<uint8_t> Bytes,
uint64_t Address,
raw_ostream &VStream,
raw_ostream &CStream) const {
uint32_t Insn;
DecodeStatus Result =
- readInstruction32(Region, Address, Size, Insn, IsBigEndian, IsMicroMips);
+ readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, IsMicroMips);
if (Result == MCDisassembler::Fail)
return MCDisassembler::Fail;
}
DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
- const MemoryObject &Region,
+ ArrayRef<uint8_t> Bytes,
uint64_t Address,
raw_ostream &VStream,
raw_ostream &CStream) const {
uint32_t Insn;
DecodeStatus Result =
- readInstruction32(Region, Address, Size, Insn, IsBigEndian, false);
+ readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
if (Result == MCDisassembler::Fail)
return MCDisassembler::Fail;
#include "llvm/MC/MCFixedLenDisassembler.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCSubtargetInfo.h"
-#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/TargetRegistry.h"
using namespace llvm;
virtual ~PPCDisassembler() {}
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 &CStream) const override;
};
#include "PPCGenDisassemblerTables.inc"
DecodeStatus PPCDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
- const MemoryObject &Region,
+ ArrayRef<uint8_t> Bytes,
uint64_t Address, raw_ostream &OS,
raw_ostream &CS) const {
// Get the four bytes of the instruction.
- uint8_t Bytes[4];
Size = 4;
- if (Region.readBytes(Address, Size, Bytes) == -1) {
+ if (Bytes.size() < 4) {
Size = 0;
return MCDisassembler::Fail;
}
#include "SparcSubtarget.h"
#include "llvm/MC/MCDisassembler.h"
#include "llvm/MC/MCFixedLenDisassembler.h"
-#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/TargetRegistry.h"
using namespace llvm;
virtual ~SparcDisassembler() {}
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 &CStream) const override;
};
#include "SparcGenDisassemblerTables.inc"
-/// Read four bytes from the MemoryObject and return 32 bit word.
-static DecodeStatus readInstruction32(const MemoryObject &Region,
- uint64_t Address, uint64_t &Size,
- uint32_t &Insn) {
- uint8_t Bytes[4];
-
+/// Read four bytes from the ArrayRef and return 32 bit word.
+static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
+ uint64_t &Size, uint32_t &Insn) {
// We want to read exactly 4 Bytes of data.
- if (Region.readBytes(Address, 4, Bytes) == -1) {
+ if (Bytes.size() < 4) {
Size = 0;
return MCDisassembler::Fail;
}
}
DecodeStatus SparcDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
- const MemoryObject &Region,
+ ArrayRef<uint8_t> Bytes,
uint64_t Address,
raw_ostream &VStream,
raw_ostream &CStream) const {
uint32_t Insn;
- DecodeStatus Result = readInstruction32(Region, Address, Size, Insn);
+ DecodeStatus Result = readInstruction32(Bytes, Address, Size, Insn);
if (Result == MCDisassembler::Fail)
return MCDisassembler::Fail;
#include "llvm/MC/MCFixedLenDisassembler.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCSubtargetInfo.h"
-#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/TargetRegistry.h"
using namespace llvm;
: MCDisassembler(STI, Ctx) {}
virtual ~SystemZDisassembler() {}
- DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
- const MemoryObject &Region, uint64_t Address,
+ DecodeStatus getInstruction(MCInst &instr, uint64_t &Size,
+ ArrayRef<uint8_t> Bytes, uint64_t Address,
raw_ostream &VStream,
raw_ostream &CStream) const override;
};
#include "SystemZGenDisassemblerTables.inc"
DecodeStatus SystemZDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
- const MemoryObject &Region,
+ ArrayRef<uint8_t> Bytes,
uint64_t Address,
raw_ostream &OS,
raw_ostream &CS) const {
// Get the first two bytes of the instruction.
- uint8_t Bytes[6];
Size = 0;
- if (Region.readBytes(Address, 2, Bytes) == -1)
+ if (Bytes.size() < 2)
return MCDisassembler::Fail;
// The top 2 bits of the first byte specify the size.
}
// Read any remaining bytes.
- if (Size > 2 && Region.readBytes(Address + 2, Size - 2, Bytes + 2) == -1)
+ if (Bytes.size() < Size)
return MCDisassembler::Fail;
// Construct the instruction.
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Support/Debug.h"
-#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/raw_ostream.h"
}
}
-/// 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
-/// be a pointer to a MemoryObject.
+/// be a pointer to a Region.
/// @param Byte - A pointer to the byte to be read.
/// @param Address - The address to be read.
static int regionReader(const void *Arg, uint8_t *Byte, uint64_t Address) {
- const MemoryObject *Region = static_cast<const MemoryObject *>(Arg);
- return Region->readByte(Address, Byte);
+ auto *R = static_cast<const Region *>(Arg);
+ 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
//
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 {
CommentStream = &CStream;
if (&VStream == &nulls())
LoggerFn = nullptr; // Disable logging completely if it's going to nulls().
- int Ret = decodeInstruction(&InternalInstr, regionReader,
- (const void *)&Region, LoggerFn, (void *)&VStream,
+ Region R(Bytes, Address);
+
+ int Ret = decodeInstruction(&InternalInstr, regionReader, (const void *)&R,
+ LoggerFn, (void *)&VStream,
(const void *)MII.get(), Address, fMode);
if (Ret) {
std::unique_ptr<const MCInstrInfo> MII);
public:
DecodeStatus getInstruction(MCInst &instr, uint64_t &size,
- const MemoryObject ®ion, uint64_t address,
+ ArrayRef<uint8_t> Bytes, uint64_t Address,
raw_ostream &vStream,
raw_ostream &cStream) const override;
#include "llvm/MC/MCFixedLenDisassembler.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCSubtargetInfo.h"
-#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/TargetRegistry.h"
using namespace llvm;
MCDisassembler(STI, Ctx) {}
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 &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) {
- uint8_t Bytes[4];
-
// We want to read exactly 2 Bytes of data.
- if (Region.readBytes(Address, 2, Bytes) == -1) {
+ if (Bytes.size() < 2) {
Size = 0;
return false;
}
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) {
- uint8_t Bytes[4];
-
// We want to read exactly 4 Bytes of data.
- if (Region.readBytes(Address, 4, Bytes) == -1) {
+ if (Bytes.size() < 4) {
Size = 0;
return false;
}
}
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 {
uint16_t insn16;
- if (!readInstruction16(Region, Address, Size, insn16)) {
+ if (!readInstruction16(Bytes, Address, Size, insn16)) {
return Fail;
}
uint32_t insn32;
- if (!readInstruction32(Region, Address, Size, insn32)) {
+ if (!readInstruction32(Bytes, Address, Size, insn32)) {
return Fail;
}
MCStreamer &Streamer, bool InAtomicBlock,
const MCSubtargetInfo &STI) {
// Wrap the vector in a MemoryObject.
- StringRef Data((const char*)Bytes.first.data(), Bytes.first.size());
- StringRefMemoryObject memoryObject(Data);
+ ArrayRef<uint8_t> Data(Bytes.first.data(), Bytes.first.size());
// Disassemble it to strings.
uint64_t Size;
MCInst Inst;
MCDisassembler::DecodeStatus S;
- S = DisAsm.getInstruction(Inst, Size, memoryObject, Index,
+ S = DisAsm.getInstruction(Inst, Size, Data.slice(Index), Index,
/*REMOVE*/ nulls(), nulls());
switch (S) {
case MCDisassembler::Fail:
if (SegmentName != "__TEXT")
continue;
- StringRef Bytes;
- Sections[SectIdx].getContents(Bytes);
+ StringRef BytesStr;
+ Sections[SectIdx].getContents(BytesStr);
+ ArrayRef<uint8_t> Bytes((uint8_t *)BytesStr.data(), BytesStr.size());
uint64_t SectAddress = Sections[SectIdx].getAddress();
- StringRefMemoryObject MemoryObject(Bytes, SectAddress);
bool symbolTableWorked = false;
// Parse relocations.
symbolTableWorked = true;
- StringRef Data(Bytes.data() + Start, End - Start);
- StringRefMemoryObject SectionMemoryObject(Data, SectAddress + Start);
-
DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
bool isThumb =
(MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
DTI->second.getLength(Length);
uint16_t 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) &&
(PC == (DTI->first + Length - 1)) && (Length & 1))
Size++;
bool gotInst;
if (isThumb)
- gotInst = ThumbDisAsm->getInstruction(Inst, Size, SectionMemoryObject,
+ gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
PC, DebugOut, Annotations);
else
- gotInst = DisAsm->getInstruction(Inst, Size, SectionMemoryObject, PC,
+ gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
DebugOut, Annotations);
if (gotInst) {
if (!NoShowRawInsn) {
- DumpBytes(StringRef(Bytes.data() + Index, Size));
+ DumpBytes(StringRef((char *)Bytes.data() + Index, Size));
}
formatted_raw_ostream FormattedOS(outs());
Annotations.flush();
MCInst Inst;
uint64_t PC = SectAddress + Index;
- if (DisAsm->getInstruction(Inst, InstSize, MemoryObject, PC, DebugOut,
- nulls())) {
+ if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
+ DebugOut, nulls())) {
if (FullLeadingAddr) {
if (MachOOF->is64Bit())
outs() << format("%016" PRIx64, PC);
}
if (!NoShowRawInsn) {
outs() << "\t";
- DumpBytes(StringRef(Bytes.data() + Index, InstSize));
+ DumpBytes(StringRef((char *)Bytes.data() + Index, InstSize));
}
IP->printInst(&Inst, outs(), "");
outs() << "\n";
SmallString<40> Comments;
raw_svector_ostream CommentStream(Comments);
- StringRef Bytes;
- if (error(Section.getContents(Bytes)))
+ StringRef BytesStr;
+ if (error(Section.getContents(BytesStr)))
break;
- StringRefMemoryObject memoryObject(Bytes, SectionAddr);
+ ArrayRef<uint8_t> Bytes((uint8_t *)BytesStr.data(), BytesStr.size());
+
uint64_t Size;
uint64_t Index;
for (Index = Start; Index < End; Index += Size) {
MCInst Inst;
- if (DisAsm->getInstruction(Inst, Size, memoryObject,
- SectionAddr + Index,
- DebugOut, CommentStream)) {
+ if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
+ SectionAddr + Index, DebugOut,
+ CommentStream)) {
outs() << format("%8" PRIx64 ":", SectionAddr + Index);
if (!NoShowRawInsn) {
outs() << "\t";
- DumpBytes(StringRef(Bytes.data() + Index, Size));
+ DumpBytes(StringRef((char *)Bytes.data() + Index, Size));
}
IP->printInst(&Inst, outs(), "");
outs() << CommentStream.str();