Start stubbing out MCModule and MCAtom, which provide an API for accessing the rich disassembly of a complete object or executable.

These are very much a work in progress, and not really useful yet.

llvm-svn: 140345
This commit is contained in:
Owen Anderson 2011-09-22 22:32:22 +00:00
parent dedb558e4d
commit d3151e11e8
4 changed files with 257 additions and 0 deletions

75
include/llvm/MC/MCAtom.h Normal file
View File

@ -0,0 +1,75 @@
//===-- llvm/MC/MCAtom.h - MCAtom class ---------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains the declaration of the MCAtom class, which is used to
// represent a contiguous region in a decoded object that is uniformly data or
// instructions;
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_MC_MCATOM_H
#define LLVM_MC_MCATOM_H
#include "llvm/MC/MCInst.h"
#include "llvm/Support/DataTypes.h"
#include <vector>
namespace llvm {
class MCModule;
/// MCData - An entry in a data MCAtom.
// NOTE: This may change to a more complex type in the future.
typedef uint8_t MCData;
/// MCAtom - Represents a contiguous range of either instructions (a TextAtom)
/// or data (a DataAtom). Address ranges are expressed as _closed_ intervals.
class MCAtom {
friend class MCModule;
typedef enum { TextAtom, DataAtom } AtomType;
AtomType Type;
MCModule *Parent;
uint64_t Begin, End;
std::vector<std::pair<uint64_t, MCInst> > Text;
std::vector<MCData> Data;
// Private constructor - only callable by MCModule
MCAtom(AtomType T, MCModule *P, uint64_t B, uint64_t E)
: Type(T), Parent(P), Begin(B), End(E) { }
public:
bool isTextAtom() { return Type == TextAtom; }
bool isDataAtom() { return Type == DataAtom; }
void addInst(const MCInst &I, uint64_t Address) {
assert(Type == TextAtom && "Trying to add MCInst to a non-text atom!");
Text.push_back(std::make_pair(Address, I));
}
void addData(const MCData &D) {
assert(Type == DataAtom && "Trying to add MCData to a non-data atom!");
Data.push_back(D);
}
/// split - Splits the atom in two at a given address, which must align with
/// and instruction boundary if this is a TextAtom. Returns the newly created
/// atom representing the high part of the split.
MCAtom *split(uint64_t SplitPt);
/// truncate - Truncates an atom so that TruncPt is the last byte address
/// contained in the atom.
void truncate(uint64_t TruncPt);
};
}
#endif

View File

@ -0,0 +1,58 @@
//===-- llvm/MC/MCModule.h - MCModule class ---------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains the declaration of the MCModule class, which is used to
// represent a complete, disassembled object file or executable.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_MC_MCMODULE_H
#define LLVM_MC_MCMODULE_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/IntervalMap.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/DataTypes.h"
namespace llvm {
class MCAtom;
/// MCModule - This class represent a completely disassembled object file or
/// executable. It comprises a list of MCAtom's, and a branch target table.
/// Each atom represents a contiguous range of either instructions or data.
class MCModule {
/// AtomAllocationTracker - An MCModule owns its component MCAtom's, so it
/// must track them in order to ensure they are properly freed as atoms are
/// merged or otherwise manipulated.
SmallPtrSet<MCAtom*, 8> AtomAllocationTracker;
/// OffsetMap - Efficiently maps offset ranges to MCAtom's.
IntervalMap<uint64_t, MCAtom*> OffsetMap;
/// BranchTargetMap - Maps offsets that are determined to be branches and
/// can be statically resolved to their target offsets.
DenseMap<uint64_t, MCAtom*> BranchTargetMap;
friend class MCAtom;
/// remap - Update the interval mapping for an MCAtom.
void remap(MCAtom *Atom, uint64_t NewBegin, uint64_t NewEnd);
public:
MCModule(IntervalMap<uint64_t, MCAtom*>::Allocator &A) : OffsetMap(A) { }
/// createAtom - Creates a new MCAtom covering the specified offset range.
MCAtom *createAtom(MCAtom::AtomType Type, uint64_t Begin, uint64_t End);
};
}
#endif

79
lib/MC/MCAtom.cpp Normal file
View File

@ -0,0 +1,79 @@
//===- lib/MC/MCAtom.cpp - MCAtom implementation --------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/MC/MCAtom.h"
#include "llvm/MC/MCModule.h"
#include "llvm/Support/ErrorHandling.h"
using namespace llvm;
MCAtom *MCAtom::split(uint64_t SplitPt) {
assert((SplitPt > Begin && SplitPt <= End) &&
"Splitting at point not contained in atom!");
// Compute the new begin/end points.
uint64_t LeftBegin = Begin;
uint64_t LeftEnd = SplitPt - 1;
uint64_t RightBegin = SplitPt;
uint64_t RightEnd = End;
// Remap this atom to become the lower of the two new ones.
Parent->remap(this, LeftBegin, LeftEnd);
// Create a new atom for the higher atom.
MCAtom *RightAtom = Parent->createAtom(Type, RightBegin, RightEnd);
// Split the contents of the original atom between it and the new one. The
// precise method depends on whether this is a data or a text atom.
if (isDataAtom()) {
std::vector<MCData>::iterator I = Data.begin() + (RightBegin - LeftBegin);
assert(I != Data.end() && "Split point not found in range!");
std::copy(I, Data.end(), RightAtom->Data.end());
Data.erase(I, Data.end());
} else if (isTextAtom()) {
std::vector<std::pair<uint64_t, MCInst> >::iterator I = Text.begin();
while (I != Text.end() && I->first < SplitPt) ++I;
assert(I != Text.end() && "Split point not found in disassembly!");
assert(I->first == SplitPt &&
"Split point does not fall on instruction boundary!");
std::copy(I, Text.end(), RightAtom->Text.end());
Text.erase(I, Text.end());
} else
llvm_unreachable("Unknown atom type!");
return RightAtom;
}
void MCAtom::truncate(uint64_t TruncPt) {
assert((TruncPt >= Begin && TruncPt < End) &&
"Truncation point not contained in atom!");
Parent->remap(this, Begin, TruncPt);
if (isDataAtom()) {
Data.resize(TruncPt - Begin + 1);
} else if (isTextAtom()) {
std::vector<std::pair<uint64_t, MCInst> >::iterator I = Text.begin();
while (I != Text.end() && I->first <= TruncPt) ++I;
assert(I != Text.end() && "Truncation point not found in disassembly!");
assert(I->first == TruncPt+1 &&
"Truncation point does not fall on instruction boundary");
Text.erase(I, Text.end());
} else
llvm_unreachable("Unknown atom type!");
}

45
lib/MC/MCModule.cpp Normal file
View File

@ -0,0 +1,45 @@
//===- lib/MC/MCModule.cpp - MCModule implementation --------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/MC/MCAtom.h"
#include "llvm/MC/MCModule.h"
using namespace llvm;
MCAtom *MCModule::createAtom(MCAtom::AtomType Type,
uint64_t Begin, uint64_t End) {
assert(Begin < End && "Creating MCAtom with endpoints reversed?");
// Check for atoms already covering this range.
IntervalMap<uint64_t, MCAtom*>::iterator I = OffsetMap.find(Begin);
assert((!I.valid() || I.start() < End) && "Offset range already occupied!");
// Create the new atom and add it to our maps.
MCAtom *NewAtom = new MCAtom(Type, this, Begin, End);
AtomAllocationTracker.insert(NewAtom);
OffsetMap.insert(Begin, End, NewAtom);
return NewAtom;
}
// remap - Update the interval mapping for an atom.
void MCModule::remap(MCAtom *Atom, uint64_t NewBegin, uint64_t NewEnd) {
// Find and erase the old mapping.
IntervalMap<uint64_t, MCAtom*>::iterator I = OffsetMap.find(Atom->Begin);
assert(I.valid() && "Atom offset not found in module!");
assert(*I == Atom && "Previous atom mapping was invalid!");
I.erase();
// Insert the new mapping.
OffsetMap.insert(NewBegin, NewEnd, Atom);
// Update the atom internal bounds.
Atom->Begin = NewBegin;
Atom->End = NewEnd;
}