Merge remote-tracking branch 'origin/GP-2157_MarshalAPI'

This commit is contained in:
Ryan Kurtz
2022-06-21 14:05:10 -04:00
97 changed files with 5313 additions and 3733 deletions
@@ -51,6 +51,7 @@ model {
source {
srcDir "src/decompile/cpp"
include "marshal.cc"
include "space.cc"
include "float.cc"
include "address.cc"
@@ -152,6 +153,7 @@ model {
source {
srcDir "src/decompile/cpp"
include "marshal.cc"
include "space.cc"
include "float.cc"
include "address.cc"
@@ -77,7 +77,7 @@ EXTERNAL_CONSOLEEXT_NAMES=$(subst .cc,,$(notdir $(EXTERNAL_CONSOLEEXT_SOURCE)))
# The following macros partition all the source files, there should be no overlaps
# Some core source files used in all projects
CORE= xml space float address pcoderaw translate opcodes globalcontext
CORE= xml marshal space float address pcoderaw translate opcodes globalcontext
# Additional core files for any projects that decompile
DECCORE=capability architecture options graph cover block cast typeop database cpool \
comment stringmanage fspec action loadimage grammar varnode op \
@@ -128,8 +128,6 @@ public:
virtual int4 apply(Funcdata &data)=0;
virtual int4 print(ostream &s,int4 num,int4 depth) const; ///< Print a description of this Action to stream
virtual void printState(ostream &s) const; ///< Print status to stream
virtual void saveXml(ostream &s) const {} ///< Save specifics of this action to stream
virtual void restoreXml(const Element *el,Funcdata *fd) {} ///< Load specifics of action from XML
virtual Action *getSubAction(const string &specify); ///< Retrieve a specific sub-action by name
virtual Rule *getSubRule(const string &specify); ///< Retrieve a specific sub-rule by name
};
@@ -16,6 +16,17 @@
#include "address.hh"
#include "translate.hh"
AttributeId ATTRIB_FIRST = AttributeId("first",27);
AttributeId ATTRIB_LAST = AttributeId("last",28);
AttributeId ATTRIB_UNIQ = AttributeId("uniq",29);
ElementId ELEM_ADDR = ElementId("addr",11);
ElementId ELEM_RANGE = ElementId("range",12);
ElementId ELEM_RANGELIST = ElementId("rangelist",13);
ElementId ELEM_REGISTER = ElementId("register",14);
ElementId ELEM_SEQNUM = ElementId("seqnum",15);
ElementId ELEM_VARNODE = ElementId("varnode",16);
ostream &operator<<(ostream &s,const SeqNum &sq)
{
@@ -44,27 +55,30 @@ SeqNum::SeqNum(Address::mach_extreme ex) : pc(ex)
uniq = (ex == Address::m_minimal) ? 0 : ~((uintm)0);
}
void SeqNum::saveXml(ostream &s) const
void SeqNum::encode(Encoder &encoder) const
{
s << "<seqnum";
pc.getSpace()->saveXmlAttributes(s,pc.getOffset());
a_v_u(s,"uniq",uniq);
s << "/>";
encoder.openElement(ELEM_SEQNUM);
pc.getSpace()->encodeAttributes(encoder,pc.getOffset());
encoder.writeUnsignedInteger(ATTRIB_UNIQ, uniq);
encoder.closeElement(ELEM_SEQNUM);
}
SeqNum SeqNum::restoreXml(const Element *el,const AddrSpaceManager *manage)
SeqNum SeqNum::decode(Decoder &decoder,const AddrSpaceManager *manage)
{
uintm uniq = ~((uintm)0);
Address pc = Address::restoreXml(el,manage); // Recover address
for(int4 i=0;i<el->getNumAttributes();++i)
if (el->getAttributeName(i) == "uniq") {
istringstream s2(el->getAttributeValue(i)); // Recover unique (if present)
s2.unsetf(ios::dec | ios::hex | ios::oct);
s2 >> uniq;
uint4 elemId = decoder.openElement(ELEM_SEQNUM);
Address pc = Address::decode(decoder,manage); // Recover address
for(;;) {
uint4 attribId = decoder.getNextAttributeId();
if (attribId == 0) break;
if (attribId == ATTRIB_UNIQ) {
uniq = decoder.readUnsignedInteger();
break;
}
}
decoder.closeElement(elemId);
return SeqNum(pc,uniq);
}
@@ -85,16 +99,6 @@ Address::Address(mach_extreme ex)
}
}
/// \deprecated Convert this to the most basic physical address.
/// This routine is only present for backward compatibility
/// with SLED
void Address::toPhysical(void)
{ AddrSpace *phys = base->getContain();
if ((phys != (AddrSpace *)0)&&(base->getType()==IPTR_SPACEBASE))
base = phys;
}
/// Return \b true if the range starting at \b this extending the given number of bytes
/// is contained by the second given range.
/// \param sz is the given number of bytes in \b this range
@@ -187,29 +191,27 @@ void Address::renormalize(int4 size) {
base->getManager()->renormalizeJoinAddress(*this,size);
}
/// This is usually used to build an address from an \b \<addr\>
/// tag, but it can be used to create an address from any tag
/// with the appropriate attributes
/// This is usually used to decode an address from an \b \<addr\>
/// element, but any element can be used if it has the appropriate attributes
/// - \e space indicates the address space of the tag
/// - \e offset indicates the offset within the space
///
/// or a \e name attribute can be used to recover an address
/// based on a register name.
/// \param el is the parsed tag
/// \param decoder is the stream decoder
/// \param manage is the address space manager for the program
/// \return the resulting Address
Address Address::restoreXml(const Element *el,const AddrSpaceManager *manage)
Address Address::decode(Decoder &decoder,const AddrSpaceManager *manage)
{
VarnodeData var;
var.restoreXml(el,manage);
var.decode(decoder,manage);
return Address(var.space,var.offset);
}
/// This is usually used to build an address from an \b \<addr\>
/// tag, but it can be used to create an address from any tag
/// with the appropriate attributes
/// This is usually used to decode an address from an \b \<addr\>
/// element, but any element can be used if it has the appropriate attributes
/// - \e space indicates the address space of the tag
/// - \e offset indicates the offset within the space
/// - \e size indicates the size of an address range
@@ -217,20 +219,46 @@ Address Address::restoreXml(const Element *el,const AddrSpaceManager *manage)
/// or a \e name attribute can be used to recover an address
/// and size based on a register name. If a size is recovered
/// it is stored in \e size reference.
/// \param el is the parsed tag
/// \param decoder is the stream decoder
/// \param manage is the address space manager for the program
/// \param size is the reference to any recovered size
/// \return the resulting Address
Address Address::restoreXml(const Element *el,const AddrSpaceManager *manage,int4 &size)
Address Address::decode(Decoder &decoder,const AddrSpaceManager *manage,int4 &size)
{
VarnodeData var;
var.restoreXml(el,manage);
var.decode(decoder,manage);
size = var.size;
return Address(var.space,var.offset);
}
Range::Range(const RangeProperties &properties,const AddrSpaceManager *manage)
{
if (properties.isRegister) {
const Translate *trans = manage->getDefaultCodeSpace()->getTrans();
const VarnodeData &point(trans->getRegister(properties.spaceName));
spc = point.space;
first = point.offset;
last = (first-1) + point.size;
return;
}
spc = manage->getSpaceByName(properties.spaceName);
if (spc == (AddrSpace *)0)
throw LowlevelError("Undefined space: "+properties.spaceName);
if (spc == (AddrSpace *)0)
throw LowlevelError("No address space indicated in range tag");
first = properties.first;
last = properties.last;
if (!properties.seenLast) {
last = spc->getHighest();
}
if (first > spc->getHighest() || last > spc->getHighest() || last < first)
throw LowlevelError("Illegal range tag");
}
/// Get the last address +1, updating the space, or returning
/// the extremal address if necessary
/// \param manage is used to fetch the next address space
@@ -259,48 +287,60 @@ void Range::printBounds(ostream &s) const
s << hex << first << '-' << last;
}
/// Write this object to a stream as a \<range> tag.
/// \param s is the output stream
void Range::saveXml(ostream &s) const
/// Encode \b this to a stream as a \<range> element.
/// \param encoder is the stream encoder
void Range::encode(Encoder &encoder) const
{
s << "<range";
a_v(s,"space",spc->getName());
a_v_u(s,"first",first);
a_v_u(s,"last",last);
s << "/>\n";
encoder.openElement(ELEM_RANGE);
encoder.writeString(ATTRIB_SPACE, spc->getName());
encoder.writeUnsignedInteger(ATTRIB_FIRST, first);
encoder.writeUnsignedInteger(ATTRIB_LAST, last);
encoder.closeElement(ELEM_RANGE);
}
/// Reconstruct this object from an XML \<range> element
/// \param el is the XML element
/// \param manage is the space manage for recovering AddrSpace objects
void Range::restoreXml(const Element *el,const AddrSpaceManager *manage)
/// Reconstruct this object from a \<range> or \<register> element
/// \param decoder is the stream decoder
/// \param manage is the space manager for recovering AddrSpace objects
void Range::decode(Decoder &decoder,const AddrSpaceManager *manage)
{
uint4 elemId = decoder.openElement();
if (elemId != ELEM_RANGE && elemId != ELEM_REGISTER)
throw XmlError("Expecting <range> or <register> element");
decodeFromAttributes(decoder,manage);
decoder.closeElement(elemId);
}
/// Reconstruct from attributes that may not be part of a \<range> element.
/// \param decoder is the stream decoder
/// \param manage is the space manager for recovering AddrSpace objects
void Range::decodeFromAttributes(Decoder &decoder,const AddrSpaceManager *manage)
{
spc = (AddrSpace *)0;
bool seenLast = false;
first = 0;
last = 0;
for(int4 i=0;i<el->getNumAttributes();++i) {
if (el->getAttributeName(i) == "space") {
spc = manage->getSpaceByName(el->getAttributeValue(i));
for(;;) {
uint4 attribId = decoder.getNextAttributeId();
if (attribId == 0) break;
if (attribId == ATTRIB_SPACE) {
string spcname = decoder.readString();
spc = manage->getSpaceByName(spcname);
if (spc == (AddrSpace *)0)
throw LowlevelError("Undefined space: "+el->getAttributeValue(i));
throw LowlevelError("Undefined space: "+spcname);
}
else if (el->getAttributeName(i) == "first") {
istringstream s(el->getAttributeValue(i));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> first;
else if (attribId == ATTRIB_FIRST) {
first = decoder.readUnsignedInteger();
}
else if (el->getAttributeName(i) == "last") {
istringstream s(el->getAttributeValue(i));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> last;
else if (attribId == ATTRIB_LAST) {
last = decoder.readUnsignedInteger();
seenLast = true;
}
else if (el->getAttributeName(i) == "name") {
else if (attribId == ATTRIB_NAME) {
const Translate *trans = manage->getDefaultCodeSpace()->getTrans();
const VarnodeData &point(trans->getRegister(el->getAttributeValue(i)));
const VarnodeData &point(trans->getRegister(decoder.readString()));
spc = point.space;
first = point.offset;
last = (first-1) + point.size;
@@ -316,6 +356,31 @@ void Range::restoreXml(const Element *el,const AddrSpaceManager *manage)
throw LowlevelError("Illegal range tag");
}
void RangeProperties::decode(Decoder &decoder)
{
uint4 elemId = decoder.openElement();
if (elemId != ELEM_RANGE && elemId != ELEM_REGISTER)
throw XmlError("Expecting <range> or <register> element");
for(;;) {
uint4 attribId = decoder.getNextAttributeId();
if (attribId == 0) break;
if (attribId == ATTRIB_SPACE)
spaceName = decoder.readString();
else if (attribId == ATTRIB_FIRST)
first = decoder.readUnsignedInteger();
else if (attribId == ATTRIB_LAST) {
last = decoder.readUnsignedInteger();
seenLast = true;
}
else if (attribId == ATTRIB_NAME) {
spaceName = decoder.readString();
isRegister = true;
}
}
decoder.closeElement(elemId);
}
/// Insert a new Range merging as appropriate to maintain the disjoint cover
/// \param spc is the address space containing the new range
/// \param first is the offset of the first byte in the new range
@@ -539,36 +604,33 @@ void RangeList::printBounds(ostream &s) const
}
}
/// Serialize this object to an XML \<rangelist> tag
/// \param s is the output stream
void RangeList::saveXml(ostream &s) const
/// Encode \b this as a \<rangelist> element
/// \param encoder is the stream encoder
void RangeList::encode(Encoder &encoder) const
{
set<Range>::const_iterator iter;
s << "<rangelist>\n";
encoder.openElement(ELEM_RANGELIST);
for(iter=tree.begin();iter!=tree.end();++iter) {
(*iter).saveXml(s);
(*iter).encode(encoder);
}
s << "</rangelist>\n";
encoder.closeElement(ELEM_RANGELIST);
}
/// Recover each individual disjoint Range for \b this RangeList as encoded
/// in a \<rangelist> tag.
/// \param el is the XML element
/// Recover each individual disjoint Range for \b this RangeList.
/// \param decoder is the stream decoder
/// \param manage is manager for retrieving address spaces
void RangeList::restoreXml(const Element *el,const AddrSpaceManager *manage)
void RangeList::decode(Decoder &decoder,const AddrSpaceManager *manage)
{
const List &list(el->getChildren());
List::const_iterator iter;
for(iter=list.begin();iter!=list.end();++iter) {
const Element *subel = *iter;
uint4 elemId = decoder.openElement(ELEM_RANGELIST);
while(decoder.peekElement() != 0) {
Range range;
range.restoreXml(subel,manage);
range.decode(decoder,manage);
tree.insert(range);
}
decoder.closeElement(elemId);
}
#ifdef UINTB4
@@ -30,6 +30,17 @@
class AddrSpaceManager;
extern AttributeId ATTRIB_FIRST; ///< Marshaling attribute "first"
extern AttributeId ATTRIB_LAST; ///< Marshaling attribute "last"
extern AttributeId ATTRIB_UNIQ; ///< Marshaling attribute "uniq"
extern ElementId ELEM_ADDR; ///< Marshaling element \<addr>
extern ElementId ELEM_RANGE; ///< Marshaling element \<range>
extern ElementId ELEM_RANGELIST; ///< Marshaling element \<rangelist>
extern ElementId ELEM_REGISTER; ///< Marshaling element \<register>
extern ElementId ELEM_SEQNUM; ///< Marshaling element \<seqnum>
extern ElementId ELEM_VARNODE; ///< Marshaling element \<varnode>
/// \brief A low-level machine address for labelling bytes and data.
///
/// All data that can be manipulated within the processor reverse
@@ -65,7 +76,6 @@ public:
int4 read(const string &s); ///< Read in the address from a string
AddrSpace *getSpace(void) const; ///< Get the address space
uintb getOffset(void) const; ///< Get the address offset
void toPhysical(void); ///< Convert this to a physical address
char getShortcut(void) const; ///< Get the shortcut character for the address space
Address &operator=(const Address &op2); ///< Copy an address
bool operator==(const Address &op2) const; ///< Compare two addresses for equality
@@ -82,14 +92,14 @@ public:
bool isConstant(void) const; ///< Is this a \e constant \e value
void renormalize(int4 size); ///< Make sure there is a backing JoinRecord if \b this is in the \e join space
bool isJoin(void) const; ///< Is this a \e join \e value
void saveXml(ostream &s) const; ///< Save this to a stream as an XML tag
void saveXml(ostream &s,int4 size) const; ///< Save this and a size to a stream as an XML tag
void encode(Encoder &encoder) const; ///< Encode \b this to a stream
void encode(Encoder &encoder,int4 size) const; ///< Encode \b this and a size to a stream
/// Restore an address from parsed XML
static Address restoreXml(const Element *el,const AddrSpaceManager *manage);
static Address decode(Decoder &decoder,const AddrSpaceManager *manage);
/// Restore an address and size from parsed XML
static Address restoreXml(const Element *el,const AddrSpaceManager *manage,int4 &size);
static Address decode(Decoder &decoder,const AddrSpaceManager *manage,int4 &size);
};
/// \brief A class for uniquely labelling and comparing PcodeOps
@@ -144,16 +154,18 @@ public:
return (pc < op2.pc);
}
/// Save a SeqNum to a stream as an XML tag
void saveXml(ostream &s) const;
/// Encode a SeqNum to a stream
void encode(Encoder &encoder) const;
/// Restore a SeqNum from parsed XML
static SeqNum restoreXml(const Element *el,const AddrSpaceManager *manage);
/// Decode a SeqNum from a stream
static SeqNum decode(Decoder &decoder,const AddrSpaceManager *manage);
/// Write out a SeqNum to a stream
/// Write out a SeqNum in human readable form to a stream
friend ostream &operator<<(ostream &s,const SeqNum &sq);
};
class RangeProperties;
/// \brief A contiguous range of bytes in some address space
class Range {
friend class RangeList;
@@ -169,7 +181,8 @@ public:
/// \param l is the offset of the last byte in the range
Range(AddrSpace *s,uintb f,uintb l) {
spc = s; first = f; last = l; }
Range(void) {} ///< Constructor for use with restoreXml
Range(void) {} ///< Constructor for use with decode
Range(const RangeProperties &properties,const AddrSpaceManager *manage); ///< Construct range out of basic properties
AddrSpace *getSpace(void) const { return spc; } ///< Get the address space containing \b this Range
uintb getFirst(void) const { return first; } ///< Get the offset of the first byte in \b this Range
uintb getLast(void) const { return last; } ///< Get the offset of the last byte in \b this Range
@@ -188,8 +201,24 @@ public:
return (spc->getIndex() < op2.spc->getIndex());
return (first < op2.first); }
void printBounds(ostream &s) const; ///< Print \b this Range to a stream
void saveXml(ostream &s) const; ///< Save \b this Range to an XML stream
void restoreXml(const Element *el,const AddrSpaceManager *manage); ///< Restore \b this from XML stream
void encode(Encoder &encoder) const; ///< Encode \b this Range to a stream
void decode(Decoder &decoder,const AddrSpaceManager *manage); ///< Restore \b this from a stream
void decodeFromAttributes(Decoder &decoder,const AddrSpaceManager *manage); ///< Read \b from attributes on another tag
};
/// \brief A partially parsed description of a Range
///
/// Class that allows \<range> tags to be parsed, when the address space doesn't yet exist
class RangeProperties {
friend class Range;
string spaceName; ///< Name of the address space containing the range
uintb first; ///< Offset of first byte in the Range
uintb last; ///< Offset of last byte in the Range
bool isRegister; ///< Range is specified a register name
bool seenLast; ///< End of the range is actively specified
public:
RangeProperties(void) { first = 0; last = 0; isRegister = false; seenLast = false; }
void decode(Decoder &decoder); ///< Restore \b this from an XML stream
};
/// \brief A disjoint set of Ranges, possibly across multiple address spaces
@@ -217,8 +246,8 @@ public:
bool inRange(const Address &addr,int4 size) const; ///< Check containment an address range
uintb longestFit(const Address &addr,uintb maxsize) const; ///< Find size of biggest Range containing given address
void printBounds(ostream &s) const; ///< Print a description of \b this RangeList to stream
void saveXml(ostream &s) const; ///< Write \b this RangeList to an XML stream
void restoreXml(const Element *el,const AddrSpaceManager *manage); ///< Restore \b this RangeList from an XML stream
void encode(Encoder &encoder) const; ///< Encode \b this RangeList to a stream
void decode(Decoder &decoder,const AddrSpaceManager *manage); ///< Decode \b this RangeList from a \<rangelist> element
};
/// Precalculated masks indexed by size
@@ -416,27 +445,27 @@ inline bool Address::isJoin(void) const {
return (base->getType() == IPTR_JOIN);
}
/// Save an \b \<addr\> tag corresponding to this address to a
/// Save an \<addr\> element corresponding to this address to a
/// stream. The exact format is determined by the address space,
/// but this generally has a \e space and an \e offset attribute.
/// \param s is the stream being written to
inline void Address::saveXml(ostream &s) const {
s << "<addr";
/// \param encoder is the stream encoder
inline void Address::encode(Encoder &encoder) const {
encoder.openElement(ELEM_ADDR);
if (base!=(AddrSpace *)0)
base->saveXmlAttributes(s,offset);
s << "/>";
base->encodeAttributes(encoder,offset);
encoder.closeElement(ELEM_ADDR);
}
/// Save an \b \<addr\> tag corresponding to this address to a
/// Encode an \<addr> element corresponding to this address to a
/// stream. The tag will also include an extra \e size attribute
/// so that it can describe an entire memory range.
/// \param s is the stream being written to
/// \param encoder is the stream encoder
/// \param size is the number of bytes in the range
inline void Address::saveXml(ostream &s,int4 size) const {
s << "<addr";
inline void Address::encode(Encoder &encoder,int4 size) const {
encoder.openElement(ELEM_ADDR);
if (base!=(AddrSpace *)0)
base->saveXmlAttributes(s,offset,size);
s << "/>";
base->encodeAttributes(encoder,offset,size);
encoder.closeElement(ELEM_ADDR);
}
/// \param addr is the Address to test for containment
File diff suppressed because it is too large Load Diff
@@ -61,6 +61,47 @@ public:
class Architecture;
extern AttributeId ATTRIB_ADJUSTVMA; ///< Marshaling attribute "adjustvma"
extern AttributeId ATTRIB_ENABLE; ///< Marshaling attribute "enable"
extern AttributeId ATTRIB_GROUP; ///< Marshaling attribute "group"
extern AttributeId ATTRIB_GROWTH; ///< Marshaling attribute "growth"
extern AttributeId ATTRIB_LOADERSYMBOLS; ///< Marshaling attribute "loadersymbols"
extern AttributeId ATTRIB_PARENT; ///< Marshaling attribute "parent"
extern AttributeId ATTRIB_REGISTER; ///< Marshaling attribute "register"
extern AttributeId ATTRIB_REVERSEJUSTIFY; ///< Marshaling attribute "reversejustify"
extern AttributeId ATTRIB_SIGNEXT; ///< Marshaling attribute "signext"
extern AttributeId ATTRIB_STYLE; ///< Marshaling attribute "style"
extern ElementId ELEM_ADDRESS_SHIFT_AMOUNT; ///< Marshaling element \<address_shift_amount>
extern ElementId ELEM_AGGRESSIVETRIM; ///< Marshaling element \<aggressivetrim>
extern ElementId ELEM_COMPILER_SPEC; ///< Marshaling element \<compiler_spec>
extern ElementId ELEM_DATA_SPACE; ///< Marshaling element \<data_space>
extern ElementId ELEM_DEFAULT_MEMORY_BLOCKS; ///< Marshaling element \<default_memory_blocks>
extern ElementId ELEM_DEFAULT_PROTO; ///< Marshaling element \<default_proto>
extern ElementId ELEM_DEFAULT_SYMBOLS; ///< Marshaling element \<default_symbols>
extern ElementId ELEM_EVAL_CALLED_PROTOTYPE; ///< Marshaling element \<eval_called_prototype>
extern ElementId ELEM_EVAL_CURRENT_PROTOTYPE; ///< Marshaling element \<eval_current_prototype>
extern ElementId ELEM_EXPERIMENTAL_RULES; ///< Marshaling element \<experimental_rules>
extern ElementId ELEM_FLOWOVERRIDELIST; ///< Marshaling element \<flowoverridelist>
extern ElementId ELEM_FUNCPTR; ///< Marshaling element \<funcptr>
extern ElementId ELEM_GLOBAL; ///< Marshaling element \<global>
extern ElementId ELEM_INCIDENTALCOPY; ///< Marshaling element \<incidentalcopy>
extern ElementId ELEM_INFERPTRBOUNDS; ///< Marshaling element \<inferptrbounds>
extern ElementId ELEM_MODELALIAS; ///< Marshaling element \<modelalias>
extern ElementId ELEM_NOHIGHPTR; ///< Marshaling element \<nohighptr>
extern ElementId ELEM_PROCESSOR_SPEC; ///< Marshaling element \<processor_spec>
extern ElementId ELEM_PROGRAMCOUNTER; ///< Marshaling element \<programcounter>
extern ElementId ELEM_PROPERTIES; ///< Marshaling element \<properties>
extern ElementId ELEM_READONLY; ///< Marshaling element \<readonly>
extern ElementId ELEM_REGISTER_DATA; ///< Marshaling element \<register_data>
extern ElementId ELEM_RULE; ///< Marshaling element \<rule>
extern ElementId ELEM_SAVE_STATE; ///< Marshaling element \<save_state>
extern ElementId ELEM_SEGMENTED_ADDRESS; ///< Marshaling element \<segmented_address>
extern ElementId ELEM_SPACEBASE; ///< Marshaling element \<spacebase>
extern ElementId ELEM_SPECEXTENSIONS; ///< Marshaling element \<specextensions>
extern ElementId ELEM_STACKPOINTER; ///< Marshaling element \<stackpointer>
extern ElementId ELEM_VOLATILE; ///< Marshaling element \<volatile>
/// \brief Abstract extension point for building Architecture objects
///
/// Decompilation hinges on initially recognizing the format of code then
@@ -185,7 +226,7 @@ public:
void setPrototype(const PrototypePieces &pieces); ///< Set the prototype for a particular function
void setPrintLanguage(const string &nm); ///< Establish a particular output language
void globalify(void); ///< Mark \e all spaces as global
void restoreFlowOverride(const Element *el); ///< Set flow overrides from XML
void decodeFlowOverride(Decoder &decoder); ///< Set flow overrides from XML
virtual ~Architecture(void); ///< Destructor
virtual string getDescription(void) const { return archid; } ///< Get a string describing \b this architecture
@@ -195,8 +236,8 @@ public:
/// Write the given message to whatever the registered error stream is
/// \param message is the error message
virtual void printMessage(const string &message) const=0;
virtual void saveXml(ostream &s) const; ///< Serialize this architecture to XML
virtual void restoreXml(DocumentStorage &store); ///< Restore the Architecture state from an XML stream
virtual void encode(Encoder &encoder) const; ///< Encode \b this architecture to a stream
virtual void restoreXml(DocumentStorage &store); ///< Restore the Architecture state from XML documents
virtual void nameFunction(const Address &addr,string &name) const; ///< Pick a default name for a function
#ifdef OPACTION_DEBUG
void setDebugStream(ostream *s) { debugstream = s; } ///< Establish the debug console stream
@@ -264,25 +305,26 @@ protected:
void parseCompilerConfig(DocumentStorage &store); ///< Apply compiler specific configuration
void parseExtraRules(DocumentStorage &store); ///< Apply any Rule tags
void parseDynamicRule(const Element *el); ///< Apply details of a dynamic Rule object
ProtoModel *parseProto(const Element *el); ///< Build a proto-type model from an XML tag
void parseProtoEval(const Element *el); ///< Apply prototype evaluation configuration
void parseDefaultProto(const Element *el); ///< Apply default prototype model configuration
void parseGlobal(const Element *el); ///< Apply global space configuration
void decodeDynamicRule(Decoder &decoder); ///< Apply details of a dynamic Rule object
ProtoModel *decodeProto(Decoder &decoder); ///< Parse a proto-type model from a stream
void decodeProtoEval(Decoder &decoder); ///< Apply prototype evaluation configuration
void decodeDefaultProto(Decoder &decoder); ///< Apply default prototype model configuration
void decodeGlobal(Decoder &decoder,vector<RangeProperties> &rangeProps); ///< Parse information about global ranges
void addToGlobalScope(const RangeProperties &props); ///< Add a memory range to the set of addresses considered \e global
void addOtherSpace(void); ///< Add OTHER space and all of its overlays to the symboltab
void parseReadOnly(const Element *el); ///< Apply read-only region configuration
void parseVolatile(const Element *el); ///< Apply volatile region configuration
void parseReturnAddress(const Element *el); ///< Apply return address configuration
void parseIncidentalCopy(const Element *el); ///< Apply incidental copy configuration
void parseLaneSizes(const Element *el); ///< Apply lane size configuration
void parseStackPointer(const Element *el); ///< Apply stack pointer configuration
void parseDeadcodeDelay(const Element *el); ///< Apply dead-code delay configuration
void parseInferPtrBounds(const Element *el); ///< Apply pointer inference bounds
void parseFuncPtrAlign(const Element *el); ///< Apply function pointer alignment configuration
void parseSpacebase(const Element *el); ///< Create an additional indexed space
void parseNoHighPtr(const Element *el); ///< Apply memory alias configuration
void parsePreferSplit(const Element *el); ///< Designate registers to be split
void parseAggressiveTrim(const Element *el); ///< Designate how to trim extension p-code ops
void decodeReadOnly(Decoder &decoder); ///< Apply read-only region configuration
void decodeVolatile(Decoder &decoder); ///< Apply volatile region configuration
void decodeReturnAddress(Decoder &decoder); ///< Apply return address configuration
void decodeIncidentalCopy(Decoder &decoder); ///< Apply incidental copy configuration
void decodeLaneSizes(Decoder &decoder); ///< Apply lane size configuration
void decodeStackPointer(Decoder &decoder); ///< Apply stack pointer configuration
void decodeDeadcodeDelay(Decoder &decoder); ///< Apply dead-code delay configuration
void decodeInferPtrBounds(Decoder &decoder); ///< Apply pointer inference bounds
void decodeFuncPtrAlign(Decoder &decoder); ///< Apply function pointer alignment configuration
void decodeSpacebase(Decoder &decoder); ///< Create an additional indexed space
void decodeNoHighPtr(Decoder &decoder); ///< Apply memory alias configuration
void decodePreferSplit(Decoder &decoder); ///< Designate registers to be split
void decodeAggressiveTrim(Decoder &decoder); ///< Designate how to trim extension p-code ops
};
/// \brief A resolver for segmented architectures
@@ -19,6 +19,8 @@
// Constructing this object registers capability
BfdArchitectureCapability BfdArchitectureCapability::bfdArchitectureCapability;
ElementId ELEM_BFD_SAVEFILE = ElementId("bfd_savefile",46);
BfdArchitectureCapability::BfdArchitectureCapability(void)
{
@@ -125,16 +127,15 @@ BfdArchitecture::BfdArchitecture(const string &fname,const string &targ,ostream
adjustvma = 0;
}
void BfdArchitecture::saveXml(ostream &s) const
void BfdArchitecture::encode(Encoder &encoder) const
{ // prepend extra stuff to specify binary file and spec
s << "<bfd_savefile";
saveXmlHeader(s);
a_v_u(s,"adjustvma",adjustvma);
s << ">\n";
types->saveXmlCoreTypes(s);
SleighArchitecture::saveXml(s); // Save the rest of the state
s << "</bfd_savefile>\n";
encoder.openElement(ELEM_BFD_SAVEFILE);
encodeHeader(encoder);
encoder.writeUnsignedInteger(ATTRIB_ADJUSTVMA, adjustvma);
types->encodeCoreTypes(encoder);
SleighArchitecture::encode(encoder); // Save the rest of the state
encoder.closeElement(ELEM_BFD_SAVEFILE);
}
void BfdArchitecture::restoreXml(DocumentStorage &store)
@@ -20,6 +20,8 @@
#include "sleigh_arch.hh"
#include "loadimage_bfd.hh"
extern ElementId ELEM_BFD_SAVEFILE; ///< Marshaling element \<bfd_savefile>
/// \brief Extension point for building a GNU BFD capable Architecture
class BfdArchitectureCapability : public ArchitectureCapability {
static BfdArchitectureCapability bfdArchitectureCapability; ///< The singleton instance
@@ -40,7 +42,7 @@ class BfdArchitecture : public SleighArchitecture {
virtual void resolveArchitecture(void);
virtual void postSpecFile(void);
public:
virtual void saveXml(ostream &s) const;
virtual void encode(Encoder &encoder) const;
virtual void restoreXml(DocumentStorage &store);
BfdArchitecture(const string &fname,const string &targ,ostream *estream); ///< Constructor
virtual ~BfdArchitecture(void) {}
@@ -17,34 +17,43 @@
#include "block.hh"
#include "funcdata.hh"
/// The edge is saved assuming we already know what block we are in
/// \param s is the output stream
void BlockEdge::saveXml(ostream &s) const
AttributeId ATTRIB_ALTINDEX = AttributeId("altindex",40);
AttributeId ATTRIB_DEPTH = AttributeId("depth",41);
AttributeId ATTRIB_END = AttributeId("end",42);
AttributeId ATTRIB_OPCODE = AttributeId("opcode",43);
AttributeId ATTRIB_REV = AttributeId("rev",44);
ElementId ELEM_BHEAD = ElementId("bhead",47);
ElementId ELEM_BLOCK = ElementId("block",48);
ElementId ELEM_BLOCKEDGE = ElementId("blockedge",49);
ElementId ELEM_EDGE = ElementId("edge",50);
/// The edge is saved assuming we already know what block we are in.
/// \param encoder is the stream encoder
void BlockEdge::encode(Encoder &encoder) const
{
s << "<edge";
encoder.openElement(ELEM_EDGE);
// We are not saving label currently
a_v_i(s,"end",point->getIndex()); // Reference to other end of edge
a_v_i(s,"rev",reverse_index); // Position within other blocks edgelist
s << "/>\n";
encoder.writeSignedInteger(ATTRIB_END, point->getIndex()); // Reference to other end of edge
encoder.writeSignedInteger(ATTRIB_REV, reverse_index); // Position within other blocks edgelist
encoder.closeElement(ELEM_EDGE);
}
/// \param el is the \<edge> tag
/// Parse an \<edge> element
/// \param decoder is the stream decoder
/// \param resolver is used to cross-reference the edge's FlowBlock endpoints
void BlockEdge::restoreXml(const Element *el,BlockMap &resolver)
void BlockEdge::decode(Decoder &decoder,BlockMap &resolver)
{
uint4 elemId = decoder.openElement(ELEM_EDGE);
label = 0; // Tag does not currently contain info about label
int4 endIndex;
istringstream s(el->getAttributeValue("end"));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> endIndex;
int4 endIndex = decoder.readSignedInteger(ATTRIB_END);
point = resolver.findLevelBlock(endIndex);
if (point == (FlowBlock *)0)
throw LowlevelError("Bad serialized edge in block graph");
istringstream s2(el->getAttributeValue("rev"));
s2.unsetf(ios::dec | ios::hex | ios::oct);
s2 >> reverse_index;
reverse_index = decoder.readSignedInteger(ATTRIB_REV);
decoder.closeElement(elemId);
}
FlowBlock::FlowBlock(void)
@@ -68,14 +77,15 @@ void FlowBlock::addInEdge(FlowBlock *b,uint4 lab)
b->outofthis.push_back(BlockEdge(this,lab,brev));
}
/// \param el is the \<edge> element
/// Parse the next \<edge> element in the stream
/// \param decoder is the stream decoder
/// \param resolver is used to resolve block references
void FlowBlock::restoreNextInEdge(const Element *el,BlockMap &resolver)
void FlowBlock::decodeNextInEdge(Decoder &decoder,BlockMap &resolver)
{
intothis.emplace_back();
BlockEdge &inedge(intothis.back());
inedge.restoreXml(el,resolver);
inedge.decode(decoder,resolver);
while(inedge.point->outofthis.size() <= inedge.reverse_index)
inedge.point->outofthis.emplace_back();
BlockEdge &outedge(inedge.point->outofthis[inedge.reverse_index]);
@@ -621,7 +631,7 @@ JumpTable *FlowBlock::getJumptable(void) const
}
/// Given a string describing a FlowBlock type, return the block_type.
/// This is currently only used by the restoreXml() process.
/// This is currently only used by the decode() process.
/// TODO: Fill in the remaining names and types
/// \param nm is the name string
/// \return the corresponding block_type
@@ -1286,14 +1296,14 @@ void BlockGraph::finalizePrinting(Funcdata &data) const
(*iter)->finalizePrinting(data);
}
void BlockGraph::saveXmlBody(ostream &s) const
void BlockGraph::encodeBody(Encoder &encoder) const
{
FlowBlock::saveXmlBody(s);
FlowBlock::encodeBody(encoder);
for(int4 i=0;i<list.size();++i) {
FlowBlock *bl = list[i];
s << "<bhead";
a_v_i(s,"index",bl->getIndex());
encoder.openElement(ELEM_BHEAD);
encoder.writeSignedInteger(ATTRIB_INDEX, bl->getIndex());
FlowBlock::block_type bt = bl->getType();
string nm;
if (bt == FlowBlock::t_if) {
@@ -1307,54 +1317,47 @@ void BlockGraph::saveXmlBody(ostream &s) const
}
else
nm = FlowBlock::typeToName(bt);
a_v(s,"type",nm);
s << "/>\n";
encoder.writeString(ATTRIB_TYPE, nm);
encoder.closeElement(ELEM_BHEAD);
}
for(int4 i=0;i<list.size();++i)
list[i]->saveXml(s);
list[i]->encode(encoder);
}
void BlockGraph::restoreXmlBody(List::const_iterator &iter,List::const_iterator enditer,BlockMap &resolver)
void BlockGraph::decodeBody(Decoder &decoder,BlockMap &resolver)
{
BlockMap newresolver(resolver);
FlowBlock::restoreXmlBody(iter,enditer,newresolver);
vector<FlowBlock *> tmplist;
while(iter != enditer) {
const Element *el = *iter;
if (el->getName() != "bhead") break;
++iter;
int4 newindex;
istringstream s(el->getAttributeValue("index"));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> newindex;
const string &nm( el->getAttributeValue("type") );
FlowBlock *bl = newresolver.createBlock(nm);
for(;;) {
uint4 subId = decoder.peekElement();
if (subId != ELEM_BHEAD) break;
decoder.openElement();
int4 newindex = decoder.readSignedInteger(ATTRIB_INDEX);
FlowBlock *bl = newresolver.createBlock(decoder.readString(ATTRIB_TYPE));
bl->index = newindex; // Need to set index here for sort
tmplist.push_back(bl);
decoder.closeElement(subId);
}
newresolver.sortList();
for(int4 i=0;i<tmplist.size();++i) {
if (iter == enditer)
throw LowlevelError("Bad BlockGraph xml");
FlowBlock *bl = tmplist[i];
bl->restoreXml(*iter,newresolver);
bl->decode(decoder,newresolver);
addBlock(bl);
++iter;
}
}
/// This is currently just a wrapper around the FlowBlock::restoreXml()
/// that sets of the BlockMap resolver
/// \param el is the root \<block> tag
/// Parse a \<block> element. This is currently just a wrapper around the
/// FlowBlock::decode() that sets of the BlockMap resolver
/// \param decoder is the stream decoder
/// \param m is the address space manager
void BlockGraph::restoreXml(const Element *el,const AddrSpaceManager *m)
void BlockGraph::decode(Decoder &decoder,const AddrSpaceManager *m)
{
BlockMap resolver(m);
FlowBlock::restoreXml(el,resolver);
FlowBlock::decode(decoder,resolver);
// Restore goto references here
}
@@ -2353,77 +2356,70 @@ bool BlockBasic::isComplex(void) const
return false;
}
/// \param s is the output stream
void FlowBlock::saveXmlHeader(ostream &s) const
/// \param encoder is the stream encoder
void FlowBlock::encodeHeader(Encoder &encoder) const
{
a_v_i(s,"index",index);
encoder.writeSignedInteger(ATTRIB_INDEX, index);
}
/// \param el is the XML element to pull attributes from
void FlowBlock::restoreXmlHeader(const Element *el)
/// \param decoder is the stream decoder to pull attributes from
void FlowBlock::decodeHeader(Decoder &decoder)
{
istringstream s(el->getAttributeValue("index"));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> index;
index = decoder.readSignedInteger(ATTRIB_INDEX);
}
/// Write \<edge> tags to stream
/// \param s is the output stream
void FlowBlock::saveXmlEdges(ostream &s) const
/// Write \<edge> element to a stream
/// \param encoder is the stream encoder
void FlowBlock::encodeEdges(Encoder &encoder) const
{
for(int4 i=0;i<intothis.size();++i) {
intothis[i].saveXml(s);
intothis[i].encode(encoder);
}
}
/// \brief Restore edges from an XML stream
/// \brief Restore edges from an encoded stream
///
/// \param iter is an iterator to the \<edge> tags
/// \param enditer marks the end of the list of tags
/// \param decoder is the stream decoder
/// \param resolver is used to recover FlowBlock cross-references
void FlowBlock::restoreXmlEdges(List::const_iterator &iter,List::const_iterator enditer,BlockMap &resolver)
void FlowBlock::decodeEdges(Decoder &decoder,BlockMap &resolver)
{
while(iter != enditer) {
const Element *el = *iter;
if (el->getName() != "edge")
return;
++iter;
restoreNextInEdge(el,resolver);
for(;;) {
uint4 subId = decoder.peekElement();
if (subId != ELEM_EDGE)
break;
decodeNextInEdge(decoder,resolver);
}
}
/// Serialize \b this and all its sub-components as an XML \<block> tag.
/// \param s is the output stream
void FlowBlock::saveXml(ostream &s) const
/// Encode \b this and all its sub-components as a \<block> element.
/// \param encoder is the stream encoder
void FlowBlock::encode(Encoder &encoder) const
{
s << "<block";
saveXmlHeader(s);
s << ">\n";
saveXmlBody(s);
saveXmlEdges(s);
s << "</block>\n";
encoder.openElement(ELEM_BLOCK);
encodeHeader(encoder);
encodeBody(encoder);
encodeEdges(encoder);
encoder.closeElement(ELEM_BLOCK);
}
/// Recover \b this and all it sub-components from an XML \<block> tag.
/// Recover \b this and all it sub-components from a \<block> element.
///
/// This will construct all the sub-components using \b resolver as a factory.
/// \param el is the root XML element
/// \param decoder is the stream decoder
/// \param resolver acts as a factory and resolves cross-references
void FlowBlock::restoreXml(const Element *el,BlockMap &resolver)
void FlowBlock::decode(Decoder &decoder,BlockMap &resolver)
{
restoreXmlHeader(el);
const List &list(el->getChildren());
List::const_iterator iter;
iter = list.begin();
restoreXmlBody(iter,list.end(),resolver);
restoreXmlEdges(iter,list.end(),resolver);
uint4 elemId = decoder.openElement(ELEM_BLOCK);
decodeHeader(decoder);
decodeBody(decoder,resolver);
decodeEdges(decoder,resolver);
decoder.closeElement(elemId);
}
/// If there are two branches, pick the fall-thru branch
@@ -2560,17 +2556,16 @@ void BlockBasic::setOrder(void)
}
}
void BlockBasic::saveXmlBody(ostream &s) const
void BlockBasic::encodeBody(Encoder &encoder) const
{
cover.saveXml(s);
cover.encode(encoder);
}
void BlockBasic::restoreXmlBody(List::const_iterator &iter,List::const_iterator enditer,BlockMap &resolver)
void BlockBasic::decodeBody(Decoder &decoder,BlockMap &resolver)
{
cover.restoreXml(*iter, resolver.getAddressManager());
++iter;
cover.decode(decoder, resolver.getAddressManager());
}
void BlockBasic::printHeader(ostream &s) const
@@ -2635,12 +2630,12 @@ void BlockCopy::printTree(ostream &s,int4 level) const
copy->printTree(s,level);
}
void BlockCopy::saveXmlHeader(ostream &s) const
void BlockCopy::encodeHeader(Encoder &encoder) const
{
FlowBlock::saveXmlHeader(s);
FlowBlock::encodeHeader(encoder);
int4 altindex = copy->getIndex();
a_v_i(s,"altindex",altindex);
encoder.writeSignedInteger(ATTRIB_ALTINDEX, altindex);
}
void BlockGoto::markUnstructured(void)
@@ -2692,17 +2687,17 @@ FlowBlock *BlockGoto::nextFlowAfter(const FlowBlock *bl) const
return getGotoTarget()->getFrontLeaf();
}
void BlockGoto::saveXmlBody(ostream &s) const
void BlockGoto::encodeBody(Encoder &encoder) const
{
BlockGraph::saveXmlBody(s);
s << "<target";
BlockGraph::encodeBody(encoder);
encoder.openElement(ELEM_TARGET);
const FlowBlock *leaf = gototarget->getFrontLeaf();
int4 depth = gototarget->calcDepth(leaf);
a_v_i(s,"index",leaf->getIndex());
a_v_i(s,"depth",depth);
a_v_u(s,"type",gototype);
s << "/>\n";
encoder.writeSignedInteger(ATTRIB_INDEX, leaf->getIndex());
encoder.writeSignedInteger(ATTRIB_DEPTH, depth);
encoder.writeUnsignedInteger(ATTRIB_TYPE, gototype);
encoder.closeElement(ELEM_TARGET);
}
void BlockMultiGoto::scopeBreak(int4 curexit,int4 curloopexit)
@@ -2725,18 +2720,18 @@ FlowBlock *BlockMultiGoto::nextFlowAfter(const FlowBlock *bl) const
return (FlowBlock *)0;
}
void BlockMultiGoto::saveXmlBody(ostream &s) const
void BlockMultiGoto::encodeBody(Encoder &encoder) const
{
BlockGraph::saveXmlBody(s);
BlockGraph::encodeBody(encoder);
for(int4 i=0;i<gotoedges.size();++i) {
FlowBlock *gototarget = gotoedges[i];
const FlowBlock *leaf = gototarget->getFrontLeaf();
int4 depth = gototarget->calcDepth(leaf);
s << "<target";
a_v_i(s,"index",leaf->getIndex());
a_v_i(s,"depth",depth);
s << "/>\n";
encoder.openElement(ELEM_TARGET);
encoder.writeSignedInteger(ATTRIB_INDEX, leaf->getIndex());
encoder.writeSignedInteger(ATTRIB_DEPTH, depth);
encoder.closeElement(ELEM_TARGET);
}
}
@@ -2846,12 +2841,12 @@ FlowBlock *BlockCondition::nextFlowAfter(const FlowBlock *bl) const
return (FlowBlock *)0; // Do not know where flow goes
}
void BlockCondition::saveXmlHeader(ostream &s) const
void BlockCondition::encodeHeader(Encoder &encoder) const
{
BlockGraph::saveXmlHeader(s);
BlockGraph::encodeHeader(encoder);
string nm(get_opname(opc));
a_v(s,"opcode",nm);
encoder.writeString(ATTRIB_OPCODE, nm);
}
void BlockIf::markUnstructured(void)
@@ -2924,18 +2919,18 @@ FlowBlock *BlockIf::nextFlowAfter(const FlowBlock *bl) const
return getParent()->nextFlowAfter(this);
}
void BlockIf::saveXmlBody(ostream &s) const
void BlockIf::encodeBody(Encoder &encoder) const
{
BlockGraph::saveXmlBody(s);
BlockGraph::encodeBody(encoder);
if (getSize() == 1) { // If this is a if GOTO block
const FlowBlock *leaf = gototarget->getFrontLeaf();
int4 depth = gototarget->calcDepth(leaf);
s << "<target";
a_v_i(s,"index",leaf->getIndex());
a_v_i(s,"depth",depth);
a_v_u(s,"type",gototype);
s << "/>\n";
encoder.openElement(ELEM_TARGET);
encoder.writeSignedInteger(ATTRIB_INDEX, leaf->getIndex());
encoder.writeSignedInteger(ATTRIB_DEPTH, depth);
encoder.writeUnsignedInteger(ATTRIB_TYPE, gototype);
encoder.closeElement(ELEM_TARGET);
}
}
@@ -35,6 +35,17 @@ class BlockSwitch;
class PrintLanguage;
class BlockMap;
extern AttributeId ATTRIB_ALTINDEX; ///< Marshaling attribute "altindex"
extern AttributeId ATTRIB_DEPTH; ///< Marshaling attribute "depth"
extern AttributeId ATTRIB_END; ///< Marshaling attribute "end"
extern AttributeId ATTRIB_OPCODE; ///< Marshaling attribute "opcode"
extern AttributeId ATTRIB_REV; ///< Marshaling attribute "rev"
extern ElementId ELEM_BHEAD; ///< Marshaling element \<bhead>
extern ElementId ELEM_BLOCK; ///< Marshaling element \<block>
extern ElementId ELEM_BLOCKEDGE; ///< Marshaling element \<blockedge>
extern ElementId ELEM_EDGE; ///< Marshaling element \<edge>
/// \brief A control-flow edge between blocks (FlowBlock)
///
/// The edge is owned by the source block and can have FlowBlock::edge_flags
@@ -45,10 +56,10 @@ struct BlockEdge {
uint4 label; ///< Label of the edge
FlowBlock *point; ///< Other end of the edge
int4 reverse_index; ///< Index for edge coming other way
BlockEdge(void) {} ///< Constructor for use with restoreXml
BlockEdge(void) {} ///< Constructor for use with decode
BlockEdge(FlowBlock *pt,uint4 lab,int4 rev) { label=lab; point=pt; reverse_index = rev; } ///< Constructor
void saveXml(ostream &s) const; ///< Save the edge to an XML stream
void restoreXml(const Element *el,BlockMap &resolver); ///< Restore \b this edge from an XML stream
void encode(Encoder &encoder) const; ///< Encode \b this edge to a stream
void decode(Decoder &decoder,BlockMap &resolver); ///< Restore \b this edge from a stream
};
/// \brief Description of a control-flow block containing PcodeOps
@@ -119,7 +130,7 @@ private:
// the result of the condition being false
static void replaceEdgeMap(vector<BlockEdge> &vec); ///< Update block references in edges with copy map
void addInEdge(FlowBlock *b,uint4 lab); ///< Add an edge coming into \b this
void restoreNextInEdge(const Element *el,BlockMap &resolver); ///< Restore the next input edge from XML
void decodeNextInEdge(Decoder &decoder,BlockMap &resolver); ///< Restore the next input edge from XML
void halfDeleteInEdge(int4 slot); ///< Delete the \e in half of an edge, correcting indices
void halfDeleteOutEdge(int4 slot); ///< Delete the \e out half of an edge, correcting indices
void removeInEdge(int4 slot); ///< Remove an incoming edge
@@ -172,20 +183,19 @@ public:
virtual FlowBlock *nextFlowAfter(const FlowBlock *bl) const;
virtual void finalTransform(Funcdata &data) {} ///< Do any structure driven final transforms
virtual void finalizePrinting(Funcdata &data) const {} ///< Make any final configurations necessary to print the block
virtual void saveXmlHeader(ostream &s) const; ///< Save basic information as XML attributes
virtual void restoreXmlHeader(const Element *el); ///< Restore basic information for XML attributes
virtual void saveXmlBody(ostream &s) const {} ///< Save detail about components to an XML stream
virtual void encodeHeader(Encoder &encoder) const; ///< Encode basic information as attributes
virtual void decodeHeader(Decoder &decoder); ///< Decode basic information from element attributes
virtual void encodeBody(Encoder &encoder) const {} ///< Encode detail about components to a stream
/// \brief Restore details about \b this FlowBlock from an XML stream
/// \brief Restore details about \b this FlowBlock from an element stream
///
/// \param iter is an iterator to XML elements containing component tags etc.
/// \param enditer marks the end of the XML tags
/// \param resolver is used to recover FlowBlock objects based on XML references
virtual void restoreXmlBody(List::const_iterator &iter,List::const_iterator enditer,BlockMap &resolver) {}
void saveXmlEdges(ostream &s) const; ///< Save edge information to an XML stream
void restoreXmlEdges(List::const_iterator &iter,List::const_iterator enditer,BlockMap &resolver);
void saveXml(ostream &s) const; ///< Write out \b this to an XML stream
void restoreXml(const Element *el,BlockMap &resolver); ///< Restore \b this from an XML stream
/// \param decoder is the stream decoder
/// \param resolver is used to recover FlowBlock objects based on elment references
virtual void decodeBody(Decoder &decoder,BlockMap &resolver) {}
void encodeEdges(Encoder &encoder) const; ///< Encode edge information to a stream
void decodeEdges(Decoder &decoder,BlockMap &resolver);
void encode(Encoder &encoder) const; ///< Encode \b this to a stream
void decode(Decoder &decoder,BlockMap &resolver); ///< Decode \b this from a stream
const FlowBlock *nextInFlow(void) const; ///< Return next block to be executed in flow
void setVisitCount(int4 i) { visitcount = i; } ///< Set the number of times this block has been visited
int4 getVisitCount(void) const { return visitcount; } ///< Get the count of visits
@@ -299,9 +309,9 @@ public:
virtual FlowBlock *nextFlowAfter(const FlowBlock *bl) const;
virtual void finalTransform(Funcdata &data);
virtual void finalizePrinting(Funcdata &data) const;
virtual void saveXmlBody(ostream &s) const;
virtual void restoreXmlBody(List::const_iterator &iter,List::const_iterator enditer,BlockMap &resolver);
void restoreXml(const Element *el,const AddrSpaceManager *m); ///< Restore \b this BlockGraph from an XML stream
virtual void encodeBody(Encoder &encoder) const;
virtual void decodeBody(Decoder &decoder,BlockMap &resolver);
void decode(Decoder &decoder,const AddrSpaceManager *m); ///< Restore \b this BlockGraph from an XML stream
void addEdge(FlowBlock *begin,FlowBlock *end); ///< Add a directed edge between component FlowBlocks
void addLoopEdge(FlowBlock *begin,int4 outindex); ///< Mark a given edge as a \e loop edge
void removeEdge(FlowBlock *begin,FlowBlock *end); ///< Remove an edge between component FlowBlocks
@@ -383,8 +393,8 @@ public:
virtual Address getStop(void) const;
virtual block_type getType(void) const { return t_basic; }
virtual FlowBlock *subBlock(int4 i) const { return (FlowBlock *)0; }
virtual void saveXmlBody(ostream &s) const;
virtual void restoreXmlBody(List::const_iterator &iter,List::const_iterator enditer,BlockMap &resolver);
virtual void encodeBody(Encoder &encoder) const;
virtual void decodeBody(Decoder &decoder,BlockMap &resolver);
virtual void printHeader(ostream &s) const;
virtual void printRaw(ostream &s) const;
virtual void emit(PrintLanguage *lng) const { lng->emitBlockBasic(this); }
@@ -431,7 +441,7 @@ public:
virtual bool negateCondition(bool toporbottom) { bool res = copy->negateCondition(true); FlowBlock::negateCondition(toporbottom); return res; }
virtual FlowBlock *getSplitPoint(void) { return copy->getSplitPoint(); }
virtual bool isComplex(void) const { return copy->isComplex(); }
virtual void saveXmlHeader(ostream &s) const;
virtual void encodeHeader(Encoder &encoder) const;
};
/// \brief A block that terminates with an unstructured (goto) branch to another block
@@ -458,7 +468,7 @@ public:
virtual const FlowBlock *getExitLeaf(void) const { return getBlock(0)->getExitLeaf(); }
virtual PcodeOp *lastOp(void) const { return getBlock(0)->lastOp(); }
virtual FlowBlock *nextFlowAfter(const FlowBlock *bl) const;
virtual void saveXmlBody(ostream &s) const;
virtual void encodeBody(Encoder &encoder) const;
};
/// \brief A block with multiple edges out, at least one of which is an unstructured (goto) branch.
@@ -486,7 +496,7 @@ public:
virtual const FlowBlock *getExitLeaf(void) const { return getBlock(0)->getExitLeaf(); }
virtual PcodeOp *lastOp(void) const { return getBlock(0)->lastOp(); }
virtual FlowBlock *nextFlowAfter(const FlowBlock *bl) const;
virtual void saveXmlBody(ostream &s) const;
virtual void encodeBody(Encoder &encoder) const;
};
/// \brief A series of blocks that execute in sequence.
@@ -531,7 +541,7 @@ public:
virtual PcodeOp *lastOp(void) const;
virtual bool isComplex(void) const { return getBlock(0)->isComplex(); }
virtual FlowBlock *nextFlowAfter(const FlowBlock *bl) const;
virtual void saveXmlHeader(ostream &s) const;
virtual void encodeHeader(Encoder &encoder) const;
};
/// \brief A basic "if" block
@@ -569,7 +579,7 @@ public:
virtual const FlowBlock *getExitLeaf(void) const;
virtual PcodeOp *lastOp(void) const;
virtual FlowBlock *nextFlowAfter(const FlowBlock *bl) const;
virtual void saveXmlBody(ostream &s) const;
virtual void encodeBody(Encoder &encoder) const;
};
/// \brief A loop structure where the condition is checked at the top.
@@ -703,7 +713,7 @@ public:
/// list of FlowBlock objects sorted by index and then looks up the FlowBlock matching a given
/// index as edges specify them.
class BlockMap {
const AddrSpaceManager *manage; ///< Address space manager used to restore FlowBlock address ranges
const AddrSpaceManager *manage; ///< Address space manager used to decode FlowBlock address ranges
vector<FlowBlock *> sortlist; ///< The list of deserialized FlowBlock objects
FlowBlock *resolveBlock(FlowBlock::block_type bt); ///< Construct a FlowBlock of the given type
static FlowBlock *findBlock(const vector<FlowBlock *> &list,int4 ind); ///< Locate a FlowBlock with a given index
@@ -16,34 +16,30 @@
#include "callgraph.hh"
#include "funcdata.hh"
void CallGraphEdge::saveXml(ostream &s) const
ElementId ELEM_CALLGRAPH = ElementId("callgraph",51);
ElementId ELEM_NODE = ElementId("node",52);
void CallGraphEdge::encode(Encoder &encoder) const
{
s << " <edge>\n";
s << " ";
from->getAddr().saveXml(s);
s << "\n ";
to->getAddr().saveXml(s);
s << "\n ";
callsiteaddr.saveXml(s);
s << "\n </edge>\n";
encoder.openElement(ELEM_EDGE);
from->getAddr().encode(encoder);
to->getAddr().encode(encoder);
callsiteaddr.encode(encoder);
encoder.closeElement(ELEM_EDGE);
}
void CallGraphEdge::restoreXml(const Element *el,CallGraph *graph)
void CallGraphEdge::decode(Decoder &decoder,CallGraph *graph)
{
uint4 elemId = decoder.openElement(ELEM_EDGE);
const AddrSpaceManager *manage = graph->getArch();
Address fromaddr,toaddr,siteaddr;
const List &list(el->getChildren());
List::const_iterator iter;
iter = list.begin();
fromaddr = Address::restoreXml(*iter,manage);
++iter;
toaddr = Address::restoreXml(*iter,manage);
++iter;
siteaddr = Address::restoreXml(*iter,manage);
fromaddr = Address::decode(decoder,manage);
toaddr = Address::decode(decoder,manage);
siteaddr = Address::decode(decoder,manage);
decoder.closeElement(elemId);
CallGraphNode *fromnode = graph->findNode(fromaddr);
if (fromnode == (CallGraphNode *)0)
@@ -66,28 +62,29 @@ void CallGraphNode::setFuncdata(Funcdata *f)
fd = f;
}
void CallGraphNode::saveXml(ostream &s) const
void CallGraphNode::encode(Encoder &encoder) const
{
s << " <node";
encoder.openElement(ELEM_NODE);
if (name.size() != 0)
a_v(s,"name",name);
s << ">\n ";
entryaddr.saveXml(s);
s << "\n </node>\n";
encoder.writeString(ATTRIB_NAME, name);
entryaddr.encode(encoder);
encoder.closeElement(ELEM_NODE);
}
void CallGraphNode::restoreXml(const Element *el,CallGraph *graph)
void CallGraphNode::decode(Decoder &decoder,CallGraph *graph)
{
int4 num = el->getNumAttributes();
uint4 elemId = decoder.openElement(ELEM_NODE);
string name;
for(int4 i=0;i<num;++i) {
if (el->getAttributeName(i) == "name")
name = el->getAttributeValue(i);
for(;;) {
uint4 attribId = decoder.getNextAttributeId();
if (attribId == 0) break;
if (attribId == ATTRIB_NAME)
name = decoder.readString();
}
Address addr = Address::restoreXml(*el->getChildren().begin(),graph->getArch());
Address addr = Address::decode(decoder,graph->getArch());
decoder.closeElement(elemId);
graph->addNode(addr,name);
}
@@ -431,41 +428,39 @@ void CallGraph::buildEdges(Funcdata *fd)
}
}
void CallGraph::saveXml(ostream &s) const
void CallGraph::encode(Encoder &encoder) const
{
map<Address,CallGraphNode>::const_iterator iter;
s << "<callgraph>\n";
encoder.openElement(ELEM_CALLGRAPH);
for(iter=graph.begin();iter!=graph.end();++iter)
(*iter).second.saveXml(s);
(*iter).second.encode(encoder);
// Dump all the "in" edges
for(iter=graph.begin();iter!=graph.end();++iter) {
const CallGraphNode &node( (*iter).second );
for(uint4 i=0;i<node.inedge.size();++i)
node.inedge[i].saveXml(s);
node.inedge[i].encode(encoder);
}
s << "</callgraph>\n";
encoder.closeElement(ELEM_CALLGRAPH);
}
void CallGraph::restoreXml(const Element *el)
void CallGraph::decoder(Decoder &decoder)
{
const List &list(el->getChildren());
List::const_iterator iter;
iter = list.begin();
while(iter != list.end()) {
const Element *subel = *iter;
++iter;
if (subel->getName() == "edge")
CallGraphEdge::restoreXml(subel,this);
uint4 elemId = decoder.openElement(ELEM_CALLGRAPH);
for(;;) {
uint4 subId = decoder.peekElement();
if (subId == 0) break;
if (subId == ELEM_EDGE)
CallGraphEdge::decode(decoder,this);
else
CallGraphNode::restoreXml(subel,this);
CallGraphNode::decode(decoder,this);
}
decoder.closeElement(elemId);
}
@@ -24,6 +24,9 @@ class Funcdata;
class CallGraphNode;
class CallGraph;
extern ElementId ELEM_CALLGRAPH; ///< Marshaling element \<callgraph>
extern ElementId ELEM_NODE; ///< Marshaling element \<node>
class CallGraphEdge {
public:
enum {
@@ -41,9 +44,9 @@ private:
public:
CallGraphEdge(void) { flags = 0; }
bool isCycle(void) const { return ((flags&1)!=0); }
void saveXml(ostream &s) const;
void encode(Encoder &encoder) const;
const Address &getCallSiteAddr(void) const { return callsiteaddr; }
static void restoreXml(const Element *el,CallGraph *graph);
static void decode(Decoder &decoder,CallGraph *graph);
};
class CallGraphNode {
@@ -77,8 +80,8 @@ public:
const CallGraphEdge &getOutEdge(int4 i) const { return outedge[i]; }
CallGraphNode *getOutNode(int4 i) const { return outedge[i].to; }
void setFuncdata(Funcdata *f);
void saveXml(ostream &s) const;
static void restoreXml(const Element *el,CallGraph *graph);
void encode(Encoder &encoder) const;
static void decode(Decoder &decoder,CallGraph *graph);
};
struct LeafIterator {
@@ -116,8 +119,8 @@ public:
map<Address,CallGraphNode>::iterator end(void) { return graph.end(); }
void buildAllNodes(void);
void buildEdges(Funcdata *fd);
void saveXml(ostream &s) const;
void restoreXml(const Element *el);
void encode(Encoder &encoder) const;
void decoder(Decoder &decoder);
};
#endif
@@ -16,6 +16,10 @@
#include "comment.hh"
#include "funcdata.hh"
ElementId ELEM_COMMENT = ElementId("comment",53);
ElementId ELEM_COMMENTDB = ElementId("commentdb",54);
ElementId ELEM_TEXT = ElementId("text",55);
/// \param tp is the set of properties to associate with the comment (or 0 for no properties)
/// \param fad is the Address of the function containing the comment
/// \param ad is the Address of the instruction associated with the comment
@@ -26,46 +30,45 @@ Comment::Comment(uint4 tp,const Address &fad,const Address &ad,int4 uq,const str
{
}
/// The single comment is saved as a \<comment> tag.
/// \param s is the output stream
void Comment::saveXml(ostream &s) const
/// The single comment is encoded as a \<comment> element.
/// \param encoder is the stream encoder
void Comment::encode(Encoder &encoder) const
{
string tpname = Comment::decodeCommentType(type);
s << "<comment";
a_v(s,"type",tpname);
s << ">\n";
s << " <addr";
funcaddr.getSpace()->saveXmlAttributes(s,funcaddr.getOffset());
s << "/>\n";
s << " <addr";
addr.getSpace()->saveXmlAttributes(s,addr.getOffset());
s << "/>\n";
s << " <text>";
xml_escape(s,text.c_str());
s << " </text>\n";
s << "</comment>\n";
encoder.openElement(ELEM_COMMENT);
encoder.writeString(ATTRIB_TYPE, tpname);
encoder.openElement(ELEM_ADDR);
funcaddr.getSpace()->encodeAttributes(encoder,funcaddr.getOffset());
encoder.closeElement(ELEM_ADDR);
encoder.openElement(ELEM_ADDR);
addr.getSpace()->encodeAttributes(encoder,addr.getOffset());
encoder.closeElement(ELEM_ADDR);
encoder.openElement(ELEM_TEXT);
encoder.writeString(ATTRIB_CONTENT, text);
encoder.closeElement(ELEM_TEXT);
encoder.closeElement(ELEM_COMMENT);
}
/// The comment is parsed from a \<comment> tag.
/// \param el is the \<comment> element
/// Parse a \<comment> element from the given stream decoder
/// \param decoder is the given stream decoder
/// \param manage is a manager for resolving address space references
void Comment::restoreXml(const Element *el,const AddrSpaceManager *manage)
void Comment::decode(Decoder &decoder,const AddrSpaceManager *manage)
{
emitted = false;
type = 0;
type = Comment::encodeCommentType(el->getAttributeValue("type"));
const List &list(el->getChildren());
List::const_iterator iter;
iter = list.begin();
funcaddr = Address::restoreXml(*iter,manage);
++iter;
addr = Address::restoreXml(*iter,manage);
++iter;
if (iter != list.end())
text = (*iter)->getContent();
uint4 elemId = decoder.openElement(ELEM_COMMENT);
type = Comment::encodeCommentType(decoder.readString(ATTRIB_TYPE));
funcaddr = Address::decode(decoder,manage);
addr = Address::decode(decoder,manage);
uint4 subId = decoder.peekElement();
if (subId != 0) {
decoder.openElement();
text = decoder.readString(ATTRIB_CONTENT);
decoder.closeElement(subId);
}
decoder.closeElement(elemId);
}
/// \param name is a string representation of a single comment property
@@ -235,28 +238,27 @@ CommentSet::const_iterator CommentDatabaseInternal::endComment(const Address &fa
return commentset.lower_bound(&testcomm);
}
void CommentDatabaseInternal::saveXml(ostream &s) const
void CommentDatabaseInternal::encode(Encoder &encoder) const
{
CommentSet::const_iterator iter;
s << "<commentdb>\n";
encoder.openElement(ELEM_COMMENTDB);
for(iter=commentset.begin();iter!=commentset.end();++iter)
(*iter)->saveXml(s);
s << "</commentdb>\n";
(*iter)->encode(encoder);
encoder.closeElement(ELEM_COMMENTDB);
}
void CommentDatabaseInternal::restoreXml(const Element *el,const AddrSpaceManager *manage)
void CommentDatabaseInternal::decode(Decoder &decoder,const AddrSpaceManager *manage)
{
const List &list(el->getChildren());
List::const_iterator iter;
for(iter=list.begin();iter!=list.end();++iter) {
uint4 elemId = decoder.openElement(ELEM_COMMENTDB);
while(decoder.peekElement() != 0) {
Comment com;
com.restoreXml(*iter,manage);
com.decode(decoder,manage);
addComment(com.getType(),com.getFuncAddr(),com.getAddr(),com.getText());
}
decoder.closeElement(elemId);
}
/// Figure out position of given Comment and initialize its key.
@@ -25,6 +25,10 @@ class FlowBlock;
class PcodeOp;
class Funcdata;
extern ElementId ELEM_COMMENT; ///< Marshaling element \<comment>
extern ElementId ELEM_COMMENTDB; ///< Marshaling element \<commentdb>
extern ElementId ELEM_TEXT; ///< Marshaling element \<text>
/// \brief A comment attached to a specific function and code address
///
/// Things contains the actual character data of the comment. It is
@@ -53,7 +57,7 @@ public:
warningheader = 32 ///< The comment is auto-generated and should be in the header
};
Comment(uint4 tp,const Address &fad,const Address &ad,int4 uq,const string &txt); ///< Constructor
Comment(void) {} ///< Constructor for use with restoreXml
Comment(void) {} ///< Constructor for use with decode
void setEmitted(bool val) const { emitted = val; } ///< Mark that \b this comment has been emitted
bool isEmitted(void) const { return emitted; } ///< Return \b true if \b this comment is already emitted
uint4 getType(void) const { return type; } ///< Get the properties associated with the comment
@@ -61,8 +65,8 @@ public:
const Address &getAddr(void) const { return addr; } ///< Get the address to which the instruction is attached
int4 getUniq(void) const { return uniq; } ///< Get the sub-sorting index
const string &getText(void) const { return text; } ///< Get the body of the comment
void saveXml(ostream &s) const; ///< Save the comment to an XML stream
void restoreXml(const Element *el,const AddrSpaceManager *manage); ///< Restore the comment from XML
void encode(Encoder &encoder) const; ///< Encode the comment to a stream
void decode(Decoder &decoder,const AddrSpaceManager *manage); ///< Restore the comment from XML
static uint4 encodeCommentType(const string &name); ///< Convert name string to comment property
static string decodeCommentType(uint4 val); ///< Convert comment property to string
};
@@ -134,17 +138,17 @@ public:
/// \return the ending iterator
virtual CommentSet::const_iterator endComment(const Address &fad) const=0;
/// \brief Save all comments in the container to an XML stream
/// \brief Encode all comments in the container to a stream
///
/// Writes a \<commentdb> tag, with \<comment> sub-tags for each Comment object.
/// \param s is the output stream
virtual void saveXml(ostream &s) const=0;
/// Writes a \<commentdb> element, with \<comment> children for each Comment object.
/// \param encoder is the stream encoder
virtual void encode(Encoder &encoder) const=0;
/// \brief Restore all comments from XML
/// \brief Restore all comments from a \<commentdb> element
///
/// \param el is the root \<commentdb> element
/// \param decoder is the stream decoder
/// \param manage is a manager for resolving address space references
virtual void restoreXml(const Element *el,const AddrSpaceManager *manage)=0;
virtual void decode(Decoder &decoder,const AddrSpaceManager *manage)=0;
};
@@ -166,8 +170,8 @@ public:
virtual void deleteComment(Comment *com);
virtual CommentSet::const_iterator beginComment(const Address &fad) const;
virtual CommentSet::const_iterator endComment(const Address &fad) const;
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el,const AddrSpaceManager *manage);
virtual void encode(Encoder &encoder) const;
virtual void decode(Decoder &decoder,const AddrSpaceManager *manage);
};
/// \brief A class for sorting comments into and within basic blocks
@@ -28,7 +28,6 @@ CommentDatabaseGhidra::CommentDatabaseGhidra(ArchitectureGhidra *g)
void CommentDatabaseGhidra::fillCache(const Address &fad) const
{
Document *doc;
uint4 commentfilter;
if (cachefilled) return; // Already queried ghidra
@@ -41,10 +40,9 @@ void CommentDatabaseGhidra::fillCache(const Address &fad) const
iter = cache.beginComment(fad);
iterend = cache.endComment(fad);
doc = ghidra->getComments(fad,commentfilter);
if (doc != (Document *)0) {
cache.restoreXml(doc->getRoot(),ghidra);
delete doc;
XmlDecode decoder;
if (ghidra->getComments(fad,commentfilter,decoder)) {
cache.decode(decoder,ghidra);
}
}
@@ -45,10 +45,10 @@ public:
throw LowlevelError("deleteComment unimplemented"); }
virtual CommentSet::const_iterator beginComment(const Address &fad) const;
virtual CommentSet::const_iterator endComment(const Address &fad) const;
virtual void saveXml(ostream &s) const {
throw LowlevelError("commentdb::saveXml unimplemented"); }
virtual void restoreXml(const Element *el,const AddrSpaceManager *trans) {
throw LowlevelError("commentdb::restoreXml unimplemented"); }
virtual void encode(Encoder &encoder) const {
throw LowlevelError("commentdb::encode unimplemented"); }
virtual void decode(Decoder &decoder,const AddrSpaceManager *trans) {
throw LowlevelError("CommentDatabaseGhidra::decode unimplemented"); }
};
#endif
@@ -132,7 +132,8 @@ void IfcSave::execute(istream &s)
if (!fs)
throw IfaceExecutionError("Unable to open file: "+savefile);
dcp->conf->saveXml(fs);
XmlEncode encoder(fs);
dcp->conf->encode(encoder);
fs.close();
}
@@ -15,41 +15,52 @@
*/
#include "cpool.hh"
/// Save the constant pool object description as a \<cpoolrec> tag.
/// \param s is the output stream
void CPoolRecord::saveXml(ostream &s) const
AttributeId ATTRIB_A = AttributeId("a",45);
AttributeId ATTRIB_B = AttributeId("b",46);
AttributeId ATTRIB_LENGTH = AttributeId("length",47);
AttributeId ATTRIB_TAG = AttributeId("tag",48);
ElementId ELEM_CONSTANTPOOL = ElementId("constantpool",56);
ElementId ELEM_CPOOLREC = ElementId("cpoolrec",57);
ElementId ELEM_REF = ElementId("ref",58);
ElementId ELEM_TOKEN = ElementId("token",59);
/// Encode the constant pool object description as a \<cpoolrec> element.
/// \param encoder is the stream encoder
void CPoolRecord::encode(Encoder &encoder) const
{
s << "<cpoolrec";
encoder.openElement(ELEM_CPOOLREC);
if (tag == pointer_method)
a_v(s,"tag","method");
encoder.writeString(ATTRIB_TAG, "method");
else if (tag == pointer_field)
a_v(s,"tag","field");
encoder.writeString(ATTRIB_TAG, "field");
else if (tag == instance_of)
a_v(s,"tag","instanceof");
encoder.writeString(ATTRIB_TAG, "instanceof");
else if (tag == array_length)
a_v(s,"tag","arraylength");
encoder.writeString(ATTRIB_TAG, "arraylength");
else if (tag == check_cast)
a_v(s,"tag","checkcast");
encoder.writeString(ATTRIB_TAG, "checkcast");
else if (tag == string_literal)
a_v(s,"tag","string");
encoder.writeString(ATTRIB_TAG, "string");
else if (tag == class_reference)
a_v(s,"tag","classref");
encoder.writeString(ATTRIB_TAG, "classref");
else
a_v(s,"tag","primitive");
encoder.writeString(ATTRIB_TAG, "primitive");
if (isConstructor())
a_v_b(s,"constructor",true);
encoder.writeBool(ATTRIB_CONSTRUCTOR, true);
if (isDestructor())
a_v_b(s,"destructor",true);
s << ">\n";
encoder.writeBool(ATTRIB_DESTRUCTOR, true);
if (tag == primitive) {
s << " <value>0x";
s << hex << value;
s << "</value>\n";
encoder.openElement(ELEM_VALUE);
encoder.writeUnsignedInteger(ATTRIB_CONTENT, value);
encoder.closeElement(ELEM_VALUE);
}
if (byteData != (uint1 *)0) {
s << " <data length=\"" << dec << byteDataLen << "\">\n";
encoder.openElement(ELEM_DATA);
encoder.writeSignedInteger(ATTRIB_LENGTH, byteDataLen);
int4 wrap = 0;
ostringstream s;
for(int4 i=0;i<byteDataLen;++i) {
s << setfill('0') << setw(2) << hex << byteData[i] << ' ';
wrap += 1;
@@ -58,31 +69,33 @@ void CPoolRecord::saveXml(ostream &s) const
wrap = 0;
}
}
s << " </data>\n";
encoder.writeString(ATTRIB_CONTENT, s.str());
encoder.closeElement(ELEM_DATA);
}
else {
s << " <token>";
xml_escape(s,token.c_str());
s << " </token>\n";
encoder.openElement(ELEM_TOKEN);
encoder.writeString(ATTRIB_CONTENT, token);
encoder.closeElement(ELEM_TOKEN);
}
type->saveXml(s);
s << "</cpoolrec>\n";
type->encode(encoder);
encoder.closeElement(ELEM_CPOOLREC);
}
/// Initialize \b this CPoolRecord instance from a \<cpoolrec> tag.
/// \param el is the \<cpoolrec> element
/// Initialize \b this CPoolRecord instance from a \<cpoolrec> element.
/// \param decoder is the stream decoder
/// \param typegrp is the TypeFactory used to resolve data-types
void CPoolRecord::restoreXml(const Element *el,TypeFactory &typegrp)
void CPoolRecord::decode(Decoder &decoder,TypeFactory &typegrp)
{
tag = primitive; // Default tag
value = 0;
flags = 0;
int4 num = el->getNumAttributes();
for(int4 i=0;i<num;++i) {
const string &attr(el->getAttributeName(i));
if (attr == "tag") {
const string &tagstring(el->getAttributeValue(i));
uint4 elemId = decoder.openElement(ELEM_CPOOLREC);
for(;;) {
uint4 attribId = decoder.getNextAttributeId();
if (attribId == 0) break;
if (attribId == ATTRIB_TAG) {
string tagstring = decoder.readString();
if (tagstring == "method")
tag = pointer_method;
else if (tagstring == "field")
@@ -98,36 +111,27 @@ void CPoolRecord::restoreXml(const Element *el,TypeFactory &typegrp)
else if (tagstring == "classref")
tag = class_reference;
}
else if (attr == "constructor") {
if (xml_readbool(el->getAttributeValue(i)))
else if (attribId == ATTRIB_CONSTRUCTOR) {
if (decoder.readBool())
flags |= CPoolRecord::is_constructor;
}
else if (attr == "destructor") {
if (xml_readbool(el->getAttributeValue(i)))
else if (attribId == ATTRIB_DESTRUCTOR) {
if (decoder.readBool())
flags |= CPoolRecord::is_destructor;
}
}
const List &list(el->getChildren());
List::const_iterator iter;
iter = list.begin();
const Element *subel;
uint4 subId;
if (tag == primitive) { // First tag must be value
subel = *iter;
istringstream s1(subel->getContent());
s1.unsetf(ios::dec | ios::hex | ios::oct);
s1 >> value;
++iter;
subId = decoder.openElement(ELEM_VALUE);
value = decoder.readUnsignedInteger(ATTRIB_CONTENT);
decoder.closeElement(subId);
}
subel = *iter;
++iter;
if (subel->getName() == "token")
token = subel->getContent();
subId = decoder.openElement();
if (subId == ELEM_TOKEN)
token = decoder.readString(ATTRIB_CONTENT);
else {
istringstream s2(subel->getAttributeValue("length"));
s2.unsetf(ios::dec | ios::hex | ios::oct);
s2 >> byteDataLen;
istringstream s3(subel->getContent());
byteDataLen = decoder.readSignedInteger(ATTRIB_LENGTH);
istringstream s3(decoder.readString(ATTRIB_CONTENT));
byteData = new uint1[byteDataLen];
for(int4 i=0;i<byteDataLen;++i) {
uint4 val;
@@ -135,16 +139,17 @@ void CPoolRecord::restoreXml(const Element *el,TypeFactory &typegrp)
byteData[i] = (uint1)val;
}
}
decoder.closeElement(subId);
if (tag == string_literal && (byteData == (uint1 *)0))
throw LowlevelError("Bad constant pool record: missing <data>");
subel = *iter;
if (flags != 0) {
bool isConstructor = ((flags & is_constructor)!=0);
bool isDestructor = ((flags & is_destructor)!=0);
type = typegrp.restoreXmlTypeWithCodeFlags(subel,isConstructor,isDestructor);
type = typegrp.decodeTypeWithCodeFlags(decoder,isConstructor,isDestructor);
}
else
type = typegrp.restoreXmlType(subel);
type = typegrp.decodeType(decoder);
decoder.closeElement(elemId);
}
void ConstantPool::putRecord(const vector<uintb> &refs,uint4 tag,const string &tok,Datatype *ct)
@@ -156,36 +161,34 @@ void ConstantPool::putRecord(const vector<uintb> &refs,uint4 tag,const string &t
newrec->type = ct;
}
const CPoolRecord *ConstantPool::restoreXmlRecord(const vector<uintb> &refs,const Element *el,TypeFactory &typegrp)
const CPoolRecord *ConstantPool::decodeRecord(const vector<uintb> &refs,Decoder &decoder,TypeFactory &typegrp)
{
CPoolRecord *newrec = createRecord(refs);
newrec->restoreXml(el,typegrp);
newrec->decode(decoder,typegrp);
return newrec;
}
/// The reference is output as a \<ref> tag.
/// \param s is the output stream
void ConstantPoolInternal::CheapSorter::saveXml(ostream &s) const
/// The reference is encoded as a \<ref> element.
/// \param encoder is the stream encoder
void ConstantPoolInternal::CheapSorter::encode(Encoder &encoder) const
{
s << "<ref";
a_v_u(s,"a",a);
a_v_u(s,"b",b);
s << "/>\n";
encoder.openElement(ELEM_REF);
encoder.writeUnsignedInteger(ATTRIB_A, a);
encoder.writeUnsignedInteger(ATTRIB_B, b);
encoder.closeElement(ELEM_REF);
}
/// Restore \b this \e reference from a \<ref> XML tag
/// \param el is the XML element
void ConstantPoolInternal::CheapSorter::restoreXml(const Element *el)
/// Restore \b this \e reference from a \<ref> element
/// \param decoder is the stream decoder
void ConstantPoolInternal::CheapSorter::decode(Decoder &decoder)
{
istringstream s1(el->getAttributeValue("a"));
s1.unsetf(ios::dec | ios::hex | ios::oct);
s1 >> a;
istringstream s2(el->getAttributeValue("b"));
s2.unsetf(ios::dec | ios::hex | ios::oct);
s2 >> b;
uint4 elemId = decoder.openElement(ELEM_REF);
a = decoder.readUnsignedInteger(ATTRIB_A);
b = decoder.readUnsignedInteger(ATTRIB_B);
decoder.closeElement(elemId);
}
CPoolRecord *ConstantPoolInternal::createRecord(const vector<uintb> &refs)
@@ -210,32 +213,29 @@ const CPoolRecord *ConstantPoolInternal::getRecord(const vector<uintb> &refs) co
return &(*iter).second;
}
void ConstantPoolInternal::saveXml(ostream &s) const
void ConstantPoolInternal::encode(Encoder &encoder) const
{
map<CheapSorter,CPoolRecord>::const_iterator iter;
s << "<constantpool>\n";
encoder.openElement(ELEM_CONSTANTPOOL);
for(iter=cpoolMap.begin();iter!=cpoolMap.end();++iter) {
(*iter).first.saveXml(s);
(*iter).second.saveXml(s);
(*iter).first.encode(encoder);
(*iter).second.encode(encoder);
}
s << "</constantpool>\n";
encoder.closeElement(ELEM_CONSTANTPOOL);
}
void ConstantPoolInternal::restoreXml(const Element *el,TypeFactory &typegrp)
void ConstantPoolInternal::decode(Decoder &decoder,TypeFactory &typegrp)
{
const List &list(el->getChildren());
List::const_iterator iter;
for(iter=list.begin();iter!=list.end();++iter) {
const Element *subel = *iter;
uint4 elemId = decoder.openElement(ELEM_CONSTANTPOOL);
while(decoder.peekElement() != 0) {
CheapSorter sorter;
sorter.restoreXml(subel);
sorter.decode(decoder);
vector<uintb> refs;
sorter.apply(refs);
++iter;
subel = *iter;
CPoolRecord *newrec = createRecord(refs);
newrec->restoreXml(subel,typegrp);
newrec->decode(decoder,typegrp);
}
decoder.closeElement(elemId);
}
@@ -21,6 +21,16 @@
#include "type.hh"
extern AttributeId ATTRIB_A; ///< Marshaling attribute "a"
extern AttributeId ATTRIB_B; ///< Marshaling attribute "b"
extern AttributeId ATTRIB_LENGTH; ///< Marshaling attribute "length"
extern AttributeId ATTRIB_TAG; ///< Marshaling attribute "tag"
extern ElementId ELEM_CONSTANTPOOL; ///< Marshaling element \<constantpool>
extern ElementId ELEM_CPOOLREC; ///< Marshaling element \<cpoolrec>
extern ElementId ELEM_REF; ///< Marshaling element \<ref>
extern ElementId ELEM_TOKEN; ///< Marshaling element \<token>
/// \brief A description of a byte-code object referenced by a constant
///
/// Byte-code languages can make use of objects that the \e system knows about
@@ -78,8 +88,8 @@ public:
uintb getValue(void) const { return value; } ///< Get the constant value associated with \b this
bool isConstructor(void) const { return ((flags & is_constructor)!=0); } ///< Is object a constructor method
bool isDestructor(void) const { return ((flags & is_destructor)!=0); } ///< Is object a destructor method
void saveXml(ostream &s) const; ///< Save object to an XML stream
void restoreXml(const Element *el,TypeFactory &typegrp); ///< Restore object from XML stream
void encode(Encoder &encoder) const; ///< Encode \b this to a stream
void decode(Decoder &decoder,TypeFactory &typegrp); ///< Decode \b this from a stream
};
/// \brief An interface to the pool of \b constant objects for byte-code languages
@@ -116,33 +126,33 @@ public:
/// \param ct is the data-type associated with the object
void putRecord(const vector<uintb> &refs,uint4 tag,const string &tok,Datatype *ct);
/// \brief Restore a CPoolRecord given a \e reference and an XML stream
/// \brief Restore a CPoolRecord given a \e reference and a stream decoder
///
/// A \<cpoolrec> element initializes the new record which is immediately associated
/// with the \e reference.
/// \param refs is the \e reference (made up of 1 or more identifying integers)
/// \param el is the XML element
/// \param decoder is the given stream decoder
/// \param typegrp is the TypeFactory used to resolve data-type references in XML
/// \return the newly allocated and initialized CPoolRecord
const CPoolRecord *restoreXmlRecord(const vector<uintb> &refs,const Element *el,TypeFactory &typegrp);
const CPoolRecord *decodeRecord(const vector<uintb> &refs,Decoder &decoder,TypeFactory &typegrp);
virtual bool empty(void) const=0; ///< Is the container empty of records
virtual void clear(void)=0; ///< Release any (local) resources
/// \brief Save all records in this container to an XML stream
/// \brief Encode all records in this container to a stream
///
/// (If supported) An \<constantpool> element is written containing \<cpoolrec>
/// (If supported) A \<constantpool> element is written containing \<cpoolrec>
/// child elements for each CPoolRecord in the container.
/// \param s is the output stream
virtual void saveXml(ostream &s) const=0;
/// \param encoder is the stream encoder
virtual void encode(Encoder &encoder) const=0;
/// \brief Restore constant pool records from an XML stream
/// \brief Restore constant pool records from the given stream decoder
///
/// (If supported) The container is populated with CPooLRecords initialized
/// from a \<constantpool> XML tag.
/// \param el is the XML element
/// (If supported) The container is populated with CPoolRecords initialized
/// from a \<constantpool> element.
/// \param decoder is the given stream decoder
/// \param typegrp is the TypeFactory used to resolve data-type references in the XML
virtual void restoreXml(const Element *el,TypeFactory &typegrp)=0;
virtual void decode(Decoder &decoder,TypeFactory &typegrp)=0;
};
/// \brief An implementation of the ConstantPool interface storing records internally in RAM
@@ -182,8 +192,8 @@ class ConstantPoolInternal : public ConstantPool {
/// \param refs is the provided container of integers
void apply(vector<uintb> &refs) const { refs.push_back(a); refs.push_back(b); }
void saveXml(ostream &s) const; ///< Serialize the \e reference to an XML element
void restoreXml(const Element *el); ///< Deserialize the \e reference from an XML element
void encode(Encoder &encoder) const; ///< Encode the \e reference to a stream
void decode(Decoder &decoder); ///< Decode the \e reference from a stream
};
map<CheapSorter,CPoolRecord> cpoolMap; ///< A map from \e reference to constant pool record
virtual CPoolRecord *createRecord(const vector<uintb> &refs);
@@ -191,8 +201,8 @@ public:
virtual const CPoolRecord *getRecord(const vector<uintb> &refs) const;
virtual bool empty(void) const { return cpoolMap.empty(); }
virtual void clear(void) { cpoolMap.clear(); }
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el,TypeFactory &typegrp);
virtual void encode(Encoder &encoder) const;
virtual void decode(Decoder &decoder,TypeFactory &typegrp);
};
#endif
@@ -32,9 +32,10 @@ const CPoolRecord *ConstantPoolGhidra::getRecord(const vector<uintb> &refs) cons
{
const CPoolRecord *rec = cache.getRecord(refs);
if (rec == (const CPoolRecord *)0) {
Document *doc;
XmlDecode decoder;
bool success;
try {
doc = ghidra->getCPoolRef(refs);
success = ghidra->getCPoolRef(refs,decoder);
}
catch(JavaError &err) {
throw LowlevelError("Error fetching constant pool record: " + err.explain);
@@ -42,24 +43,23 @@ const CPoolRecord *ConstantPoolGhidra::getRecord(const vector<uintb> &refs) cons
catch(XmlError &err) {
throw LowlevelError("Error in constant pool record xml: "+err.explain);
}
if (doc == (Document *)0) {
if (!success) {
ostringstream s;
s << "Could not retrieve constant pool record for reference: 0x" << refs[0];
throw LowlevelError(s.str());
}
rec = cache.restoreXmlRecord(refs,doc->getRoot(),*ghidra->types);
delete doc;
rec = cache.decodeRecord(refs,decoder,*ghidra->types);
}
return rec;
}
void ConstantPoolGhidra::saveXml(ostream &s) const
void ConstantPoolGhidra::encode(Encoder &encoder) const
{
throw LowlevelError("Cannot access constant pool with this method");
}
void ConstantPoolGhidra::restoreXml(const Element *el,TypeFactory &typegrp)
void ConstantPoolGhidra::decode(Decoder &decoder,TypeFactory &typegrp)
{
throw LowlevelError("Cannot access constant pool with this method");
@@ -25,7 +25,7 @@
///
/// The actual CPoolRecord objects are cached locally, but new queries are placed
/// with the Ghidra client hosting the program currently being decompiled. The
/// queries and response records are sent via XML. The saveXml() and restoreXml()
/// queries and response records are sent via XML. The encode() and decode()
/// methods are disabled. The clear() method only releases the local cache,
/// no records on the Ghidra client are affected.
class ConstantPoolGhidra : public ConstantPool {
@@ -37,8 +37,8 @@ public:
virtual const CPoolRecord *getRecord(const vector<uintb> &refs) const;
virtual bool empty(void) const { return false; }
virtual void clear(void) { cache.clear(); }
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el,TypeFactory &typegrp);
virtual void encode(Encoder &encoder) const;
virtual void decode(Decoder &decoder,TypeFactory &typegrp);
};
#endif
File diff suppressed because it is too large Load Diff
@@ -34,6 +34,28 @@ class Database;
class Symbol;
class PrintLanguage;
extern AttributeId ATTRIB_CAT; ///< Marshaling attribute "cat"
extern AttributeId ATTRIB_FIELD; ///< Marshaling attribute "field"
extern AttributeId ATTRIB_MERGE; ///< Marshaling attribute "merge"
extern AttributeId ATTRIB_SCOPEIDBYNAME; ///< Marshaling attribute "scopeidbyname"
extern AttributeId ATTRIB_VOLATILE; ///< Marshaling attribute "volatile"
extern ElementId ELEM_COLLISION; ///< Marshaling element \<collision>
extern ElementId ELEM_DB; ///< Marshaling element \<db>
extern ElementId ELEM_EQUATESYMBOL; ///< Marshaling element \<equatesymbol>
extern ElementId ELEM_EXTERNREFSYMBOL; ///< Marshaling element \<externrefsymbol>
extern ElementId ELEM_FACETSYMBOL; ///< Marshaling element \<facetsymbol>
extern ElementId ELEM_FUNCTIONSHELL; ///< Marshaling element \<functionshell>
extern ElementId ELEM_HASH; ///< Marshaling element \<hash>
extern ElementId ELEM_HOLE; ///< Marshaling element \<hole>
extern ElementId ELEM_LABELSYM; ///< Marshaling element \<labelsym>
extern ElementId ELEM_MAPSYM; ///< Marshaling element \<mapsym>
extern ElementId ELEM_PARENT; ///< Marshaling element \<parent>
extern ElementId ELEM_PROPERTY_CHANGEPOINT; ///< Marshaling element \<property_changepoint>
extern ElementId ELEM_RANGEEQUALSSYMBOLS; ///< Marshaling element \<rangeequalssymbols>
extern ElementId ELEM_SCOPE; ///< Marshaling element \<scope>
extern ElementId ELEM_SYMBOLLIST; ///< Marshaling element \<symbollist>
/// \brief A storage location for a particular Symbol
///
/// Where a Symbol is stored, as a byte address and a size, is of particular importance
@@ -134,8 +156,8 @@ public:
bool updateType(Varnode *vn) const; ///< Update a Varnode data-type from \b this
Datatype *getSizedType(const Address &addr,int4 sz) const; ///< Get the data-type associated with (a piece of) \b this
void printEntry(ostream &s) const; ///< Dump a description of \b this to a stream
void saveXml(ostream &s) const; ///< Save \b this to an XML stream
List::const_iterator restoreXml(List::const_iterator iter,const AddrSpaceManager *manage); ///< Restore \b this from an XML stream
void encode(Encoder &encoder) const; ///< Encode \b this to a stream
void decode(Decoder &decoder,const AddrSpaceManager *manage); ///< Decode \b this from a stream
};
typedef rangemap<SymbolEntry> EntryMap; ///< A rangemap of SymbolEntry
@@ -192,7 +214,7 @@ public:
};
Symbol(Scope *sc,const string &nm,Datatype *ct); ///< Construct given a name and data-type
Symbol(Scope *sc); ///< Construct for use with restoreXml()
Symbol(Scope *sc); ///< Construct for use with decode()
const string &getName(void) const { return name; } ///< Get the local name of the symbol
Datatype *getType(void) const { return type; } ///< Get the data-type
uint8 getId(void) const { return symbolId; } ///< Get a unique id for the symbol
@@ -219,12 +241,12 @@ public:
SymbolEntry *getMapEntry(int4 i) const { return &(*mapentry[i]); } ///< Return the i-th SymbolEntry for \b this Symbol
int4 getMapEntryPosition(const SymbolEntry *entry) const; ///< Position of given SymbolEntry within \b this multi-entry Symbol
int4 getResolutionDepth(const Scope *useScope) const; ///< Get number of scope names needed to resolve \b this symbol
void saveXmlHeader(ostream &s) const; ///< Save basic Symbol properties as XML attributes
void restoreXmlHeader(const Element *el); ///< Restore basic Symbol properties from XML
void saveXmlBody(ostream &s) const; ///< Save details of the Symbol to XML
void restoreXmlBody(List::const_iterator iter); ///< Restore details of the Symbol from XML
virtual void saveXml(ostream &s) const; ///< Save \b this Symbol to an XML stream
virtual void restoreXml(const Element *el); ///< Restore \b this Symbol from an XML stream
void encodeHeader(Encoder &encoder) const; ///< Encode basic Symbol properties as attributes
void decodeHeader(Decoder &decoder); ///< Decode basic Symbol properties from a \<symbol> element
void encodeBody(Encoder &encoder) const; ///< Encode details of the Symbol to a stream
void decodeBody(Decoder &decoder); ///< Decode details of the Symbol from a \<symbol> element
virtual void encode(Encoder &encoder) const; ///< Encode \b this Symbol to a stream
virtual void decode(Decoder &decoder); ///< Decode \b this Symbol from a stream
virtual int4 getBytesConsumed(void) const; ///< Get number of bytes consumed within the address->symbol map
static uint8 ID_BASE; ///< Base of internal ID's
};
@@ -259,10 +281,10 @@ class FunctionSymbol : public Symbol {
void buildType(void); ///< Build the data-type associated with \b this Symbol
public:
FunctionSymbol(Scope *sc,const string &nm,int4 size); ///< Construct given the name
FunctionSymbol(Scope *sc,int4 size); ///< Constructor for use with restoreXml
FunctionSymbol(Scope *sc,int4 size); ///< Constructor for use with decode
Funcdata *getFunction(void); ///< Get the underlying Funcdata object
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el);
virtual void encode(Encoder &encoder) const;
virtual void decode(Decoder &decoder);
virtual int4 getBytesConsumed(void) const { return consumeSize; }
};
@@ -275,21 +297,21 @@ class EquateSymbol : public Symbol {
uintb value; ///< Value of the constant being equated
public:
EquateSymbol(Scope *sc,const string &nm,uint4 format,uintb val); ///< Constructor
EquateSymbol(Scope *sc) : Symbol(sc) { value = 0; category = equate; } ///< Constructor for use with restoreXml
EquateSymbol(Scope *sc) : Symbol(sc) { value = 0; category = equate; } ///< Constructor for use with decode
uintb getValue(void) const { return value; } ///< Get the constant value
bool isValueClose(uintb op2Value,int4 size) const; ///< Is the given value similar to \b this equate
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el);
virtual void encode(Encoder &encoder) const;
virtual void decode(Decoder &decoder);
};
class UnionFacetSymbol : public Symbol {
int4 fieldNum; ///< Particular field to associate with Symbol access
public:
UnionFacetSymbol(Scope *sc,const string &nm,Datatype *unionDt,int4 fldNum); ///< Constructor from components
UnionFacetSymbol(Scope *sc) : Symbol(sc) { fieldNum = -1; category = union_facet; } ///< Constructor for restoreXml
UnionFacetSymbol(Scope *sc) : Symbol(sc) { fieldNum = -1; category = union_facet; } ///< Constructor for decode
int4 getFieldNumber(void) const { return fieldNum; } ///< Get the particular field associate with \b this
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el);
virtual void encode(Encoder &encoder) const;
virtual void decode(Decoder &decoder);
};
/// \brief A Symbol that labels code internal to a function
@@ -297,9 +319,9 @@ class LabSymbol : public Symbol {
void buildType(void); ///< Build placeholder data-type
public:
LabSymbol(Scope *sc,const string &nm); ///< Construct given name
LabSymbol(Scope *sc); ///< Constructor for use with restoreXml
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el);
LabSymbol(Scope *sc); ///< Constructor for use with decode
virtual void encode(Encoder &encoder) const;
virtual void decode(Decoder &decoder);
};
/// \brief A function Symbol referring to an external location
@@ -314,10 +336,10 @@ class ExternRefSymbol : public Symbol {
virtual ~ExternRefSymbol(void) {}
public:
ExternRefSymbol(Scope *sc,const Address &ref,const string &nm); ///< Construct given a \e placeholder address
ExternRefSymbol(Scope *sc) : Symbol(sc) {} ///< For use with restoreXml
ExternRefSymbol(Scope *sc) : Symbol(sc) {} ///< For use with decode
const Address &getRefAddr(void) const { return refaddr; } ///< Return the \e placeholder address
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el);
virtual void encode(Encoder &encoder) const;
virtual void decode(Decoder &decoder);
};
/// \brief Comparator for sorting Symbol objects by name
@@ -391,6 +413,17 @@ public:
}
};
/// \brief Exception thrown when a function is added more than once to the database
///
/// Stores off the address of the function, so a handler can recover from the exception
/// and pick up the original symbol.
struct DuplicateFunctionError : public RecovError {
Address address; ///< Address of function causing the error
string functionName; ///< Name of the function
DuplicateFunctionError(const Address &addr,const string &nm) : RecovError("Duplicate Function") {
address = addr; functionName = nm; } ///< Constructor
};
typedef map<uint8,Scope *> ScopeMap; ///< A map from id to Scope
/// \brief A collection of Symbol objects within a single (namespace or functional) scope
@@ -661,8 +694,9 @@ public:
/// \return return a unique version of the name
virtual string makeNameUnique(const string &nm) const=0;
virtual void saveXml(ostream &s) const=0; ///< Write out \b this as a \<scope> XML tag
virtual void restoreXml(const Element *el)=0; ///< Restore \b this Scope from a \<scope> XML tag
virtual void encode(Encoder &encoder) const=0; ///< Encode \b this as a \<scope> element
virtual void decode(Decoder &decoder)=0; ///< Decode \b this Scope from a \<scope> element
virtual void decodeWrappingAttributes(Decoder &decoder) {} ///< Restore attributes for \b this Scope from wrapping element
virtual void printEntries(ostream &s) const=0; ///< Dump a description of all SymbolEntry objects to a stream
/// \brief Get the number of Symbols in the given category
@@ -709,8 +743,8 @@ public:
Scope *discoverScope(const Address &addr,int4 sz,const Address &usepoint); ///< Find the owning Scope of a given memory range
ScopeMap::const_iterator childrenBegin() const { return children.begin(); } ///< Beginning iterator of child scopes
ScopeMap::const_iterator childrenEnd() const { return children.end(); } ///< Ending iterator of child scopes
void saveXmlRecursive(ostream &s,bool onlyGlobal) const; ///< Save all contained scopes as an XML stream
void overrideSizeLockType(Symbol *sym,Datatype *ct); ///< Change the data-type of a Symbol that is \e sizelocked
void encodeRecursive(Encoder &encoder,bool onlyGlobal) const; ///< Encode all contained scopes to a stream
void overrideSizeLockType(Symbol *sym,Datatype *ct); ///< Change the data-type of a Symbol that is \e sizelocked
void resetSizeLockType(Symbol *sym); ///< Clear a Symbol's \e size-locked data-type
void setThisPointer(Symbol *sym,bool val) { sym->setThisPointer(val); } ///< Toggle the given Symbol as the "this" pointer
bool isSubScope(const Scope *scp) const; ///< Is this a sub-scope of the given Scope
@@ -722,7 +756,7 @@ public:
Symbol *addSymbol(const string &nm,Datatype *ct); ///< Add a new Symbol \e without mapping it to an address
SymbolEntry *addMapPoint(Symbol *sym,const Address &addr,
const Address &usepoint); ///< Map a Symbol to a specific address
Symbol *addMapSym(const Element *el); ///< Add a mapped Symbol from a \<mapsym> XML tag
Symbol *addMapSym(Decoder &decoder); ///< Parse a mapped Symbol from a \<mapsym> element
FunctionSymbol *addFunction(const Address &addr,const string &nm);
ExternRefSymbol *addExternalRef(const Address &addr,const Address &refaddr,const string &nm);
LabSymbol *addCodeLabel(const Address &addr,const string &nm);
@@ -740,8 +774,8 @@ public:
/// a set of Symbol objects (the set owns the Symbol objects). It also implements
/// a \b maptable, which is a list of rangemaps that own the SymbolEntry objects.
class ScopeInternal : public Scope {
void processHole(const Element *el);
void processCollision(const Element *el);
void decodeHole(Decoder &decoder);
void decodeCollision(Decoder &decoder);
void insertNameTree(Symbol *sym);
SymbolNameTree::const_iterator findFirstByName(const string &nm) const;
protected:
@@ -799,8 +833,8 @@ public:
Datatype *ct,int4 &index,uint4 flags) const;
virtual string buildUndefinedName(void) const;
virtual string makeNameUnique(const string &nm) const;
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el);
virtual void encode(Encoder &encoder) const;
virtual void decode(Decoder &decoder);
virtual void printEntries(ostream &s) const;
virtual int4 getCategorySize(int4 cat) const;
virtual Symbol *getCategorySymbol(int4 cat,int4 ind) const;
@@ -867,7 +901,7 @@ class Database {
void clearResolve(Scope *scope); ///< Clear the \e ownership ranges associated with the given Scope
void clearReferences(Scope *scope); ///< Clear any map references to the given Scope and its children
void fillResolve(Scope *scope); ///< Add the \e ownership ranges of the given Scope to the map
Scope *parseParentTag(const Element *el); ///< Figure out parent scope given \<parent> tag.
Scope *parseParentTag(Decoder &decoder); ///< Figure out parent scope given \<parent> tag.
public:
Database(Architecture *g,bool idByName); ///< Constructor
~Database(void); ///< Destructor
@@ -891,9 +925,9 @@ public:
void setPropertyRange(uint4 flags,const Range &range); ///< Set boolean properties over a given memory range
void setProperties(const partmap<Address,uint4> &newflags) { flagbase = newflags; } ///< Replace the property map
const partmap<Address,uint4> &getProperties(void) const { return flagbase; } ///< Get the entire property map
void saveXml(ostream &s) const; ///< Save the whole Database to an XML stream
void restoreXml(const Element *el); ///< Recover the whole database from XML
void restoreXmlScope(const Element *el,Scope *newScope); ///< Register and fill out a single Scope from an XML \<scope> tag
void encode(Encoder &encoder) const; ///< Encode the whole Database to a stream
void decode(Decoder &decoder); ///< Decode the whole database from a stream
void decodeScope(Decoder &decoder,Scope *newScope); ///< Register and fill out a single Scope from an XML \<scope> tag
};
/// \param sc is the scope containing the new symbol
@@ -52,120 +52,103 @@ Scope *ScopeGhidra::reresolveScope(uint8 id) const
if (cacheScope != (Scope *)0)
return cacheScope; // Scope was previously cached
Document *doc = ghidra->getNamespacePath(id);
if (doc == (Document *)0)
XmlDecode decoder;
if (!ghidra->getNamespacePath(id,decoder))
throw LowlevelError("Could not get namespace info");
Scope *curscope = symboltab->getGlobalScope(); // Get pointer to ourselves (which is not const)
try {
const List &list(doc->getRoot()->getChildren());
List::const_iterator iter = list.begin();
++iter; // Skip element describing the root scope
while(iter != list.end()) {
const Element *el = *iter;
++iter;
uint8 scopeId;
istringstream s(el->getAttributeValue("id"));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> scopeId;
curscope = symboltab->findCreateScope(scopeId, el->getContent(), curscope);
}
delete doc;
}
catch(LowlevelError &err) {
delete doc;
throw err;
uint4 elemId = decoder.openElement();
uint4 subId = decoder.openElement();
decoder.closeElementSkipping(subId); // Skip element describing the root scope
for(;;) {
subId = decoder.openElement();
if (subId == 0) break;
uint8 scopeId = decoder.readUnsignedInteger(ATTRIB_ID);
curscope = symboltab->findCreateScope(scopeId, decoder.readString(ATTRIB_CONTENT), curscope);
decoder.closeElement(subId);
}
decoder.closeElement(elemId);
return curscope;
}
/// The Ghidra client can respond to a query negatively by sending a
/// \<hole> tag, which describes the (largest) range of addresses containing
/// \<hole> element, which describes the (largest) range of addresses containing
/// the query address that do not have any Symbol mapped to them. This object
/// stores this information in the \b holes map, which it consults to avoid
/// sending queries for the same unmapped address repeatedly. The tag may
/// also contain boolean property information about the memory range, which
/// also gets stored.
/// \param el is the \<hole> element
void ScopeGhidra::processHole(const Element *el) const
/// \param decoder is the stream decoder
void ScopeGhidra::decodeHole(Decoder &decoder) const
{
Range range;
range.restoreXml(el,ghidra);
holes.insertRange(range.getSpace(),range.getFirst(),range.getLast());
uint4 elemId = decoder.openElement(ELEM_HOLE);
uint4 flags = 0;
for(int4 i=0;i<el->getNumAttributes();++i) {
if ((el->getAttributeName(i)=="readonly")&&
xml_readbool(el->getAttributeValue(i)))
Range range;
range.decodeFromAttributes(decoder,ghidra);
decoder.rewindAttributes();
for(;;) {
uint4 attribId = decoder.getNextAttributeId();
if (attribId == 0) break;
if (attribId==ATTRIB_READONLY && decoder.readBool())
flags |= Varnode::readonly;
else if ((el->getAttributeName(i)=="volatile")&&
xml_readbool(el->getAttributeValue(i)))
else if (attribId==ATTRIB_VOLATILE && decoder.readBool())
flags |= Varnode::volatil;
}
holes.insertRange(range.getSpace(),range.getFirst(),range.getLast());
decoder.closeElement(elemId);
if (flags != 0) {
ghidra->symboltab->setPropertyRange(flags,range);
cacheDirty = true;
}
}
/// Build the global object described by the XML document
/// and put it in the cache. The XML can either be a
/// \<hole> tag, describing the absence of symbols at the queried
/// address, or one of the symbol tags
/// \param doc is the XML document
/// Build the global object described by the stream element
/// and put it in the cache. The element can either be a \<hole>, describing the absence
/// of symbols at the queried address, or one of the symbol elements.
/// \param decoder is the stream decoder
/// \return the newly constructed Symbol or NULL if there was a hole
Symbol *ScopeGhidra::dump2Cache(Document *doc) const
Symbol *ScopeGhidra::dump2Cache(Decoder &decoder) const
{
const Element *el = doc->getRoot();
Symbol *sym = (Symbol *)0;
if (el->getName() == "hole") {
processHole(el);
uint4 elemId = decoder.peekElement();
if (elemId == ELEM_HOLE) {
decodeHole(decoder);
return sym;
}
List::const_iterator iter = el->getChildren().begin();
uint8 scopeId;
{
istringstream s(el->getAttributeValue("id"));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> scopeId;
}
decoder.openElement();
uint8 scopeId = decoder.readUnsignedInteger(ATTRIB_ID);
Scope *scope = reresolveScope(scopeId);
el = *iter;
try {
sym = scope->addMapSym(el);
sym = scope->addMapSym(decoder);
decoder.closeElement(elemId);
}
catch(RecovError &err) {
catch(DuplicateFunctionError &err) {
// Duplicate name error (when trying to create the new function's scope)
// Check for function object where the full size is not stored in the cache
// Entries for functions always start at the entry address
// of the function in order to deal with non-contiguous functions
// But ghidra will still return the function for queries at addresses in the
// interior of the function
const Element *symel = *el->getChildren().begin();
if (symel->getName() == "function") { // Make sure new record is for a function
const Element *baseAddrEl = *symel->getChildren().begin();
Address baseaddr = Address::restoreXml( baseAddrEl, glb ); // Decode address from record
if (!err.address.isInvalid()) { // Make sure the address was parsed
vector<Symbol *> symList;
scope->queryByName(symel->getAttributeValue("name"),symList); // Lookup symbols with duplicate name
scope->queryByName(err.functionName,symList); // Lookup symbols with duplicate name
for(int4 i=0;i<symList.size();++i) {
FunctionSymbol *funcSym = dynamic_cast<FunctionSymbol *>(symList[i]);
if (funcSym != (FunctionSymbol *)0) { // If duplicate symbol is for function
if (funcSym->getFunction()->getAddress() == baseaddr) { // and the address matches
if (funcSym->getFunction()->getAddress() == err.address) { // and the address matches
sym = funcSym; // use the old symbol
break;
}
}
}
}
if (sym == (Symbol *)0) {
ostringstream s;
s << err.explain << ": entry didn't cache";
throw LowlevelError(s.str());
}
if (sym == (Symbol *)0)
throw LowlevelError("DuplicateFunctionError, but could not recover original symbol");
}
if (sym != (Symbol *)0) {
SymbolEntry *entry = sym->getFirstWholeMap();
@@ -208,7 +191,6 @@ Symbol *ScopeGhidra::dump2Cache(Document *doc) const
Symbol *ScopeGhidra::removeQuery(const Address &addr) const
{
Document *doc;
Symbol *sym = (Symbol *)0;
// Don't send up queries on constants or uniques
@@ -230,10 +212,9 @@ Symbol *ScopeGhidra::removeQuery(const Address &addr) const
// Have we queried this address before
if (holes.inRange(addr,1)) return (Symbol *)0;
doc = ghidra->getMappedSymbolsXML(addr); // Query GHIDRA about this address
if (doc != (Document *)0) {
sym = dump2Cache(doc); // Add it to the cache
delete doc;
XmlDecode decoder;
if (ghidra->getMappedSymbolsXML(addr,decoder)) { // Query GHIDRA about this address
sym = dump2Cache(decoder); // Add it to the cache
}
return sym;
}
@@ -368,14 +349,12 @@ Funcdata *ScopeGhidra::resolveExternalRefFunction(ExternRefSymbol *sym) const
if (resFd == (Funcdata *)0) {
// If the function isn't in cache, we use the special
// getExternalRefXML interface to recover the external function
Document *doc;
SymbolEntry *entry = sym->getFirstWholeMap();
doc = ghidra->getExternalRefXML(entry->getAddr());
if (doc != (Document *)0) {
XmlDecode decoder;
if (ghidra->getExternalRefXML(entry->getAddr(),decoder)) {
FunctionSymbol *funcSym;
// Make sure referenced function is cached
funcSym = dynamic_cast<FunctionSymbol *>(dump2Cache(doc));
delete doc;
funcSym = dynamic_cast<FunctionSymbol *>(dump2Cache(decoder));
if (funcSym != (FunctionSymbol *)0)
resFd = funcSym->getFunction();
}
@@ -39,9 +39,9 @@ class ScopeGhidra : public Scope {
vector<int4> spacerange; ///< List of address spaces that are in the global range
partmap<Address,uint4> flagbaseDefault; ///< Default boolean properties on memory
mutable bool cacheDirty; ///< Is flagbaseDefault different from cache
Symbol *dump2Cache(Document *doc) const; ///< Parse a response into the cache
Symbol *dump2Cache(Decoder &decoder) const; ///< Parse a response into the cache
Symbol *removeQuery(const Address &addr) const; ///< Process a query that missed the cache
void processHole(const Element *el) const; ///< Process a response describing a hole
void decodeHole(Decoder &decoder) const; ///< Process a \<hole> response element
Scope *reresolveScope(uint8 id) const; ///< Find the Scope that will contain a result Symbol
virtual void addRange(AddrSpace *spc,uintb first,uintb last);
virtual void removeRange(AddrSpace *spc,uintb first,uintb last) {
@@ -107,8 +107,8 @@ public:
virtual void renameSymbol(Symbol *sym,const string &newname) { throw LowlevelError("renameSymbol unimplemented"); }
virtual void retypeSymbol(Symbol *sym,Datatype *ct) { throw LowlevelError("retypeSymbol unimplemented"); }
virtual string makeNameUnique(const string &nm) const { throw LowlevelError("makeNameUnique unimplemented"); }
virtual void saveXml(ostream &s) const { throw LowlevelError("saveXml unimplemented"); }
virtual void restoreXml(const Element *el) { throw LowlevelError("restoreXml unimplemented"); }
virtual void encode(Encoder &encoder) const { throw LowlevelError("encode unimplemented"); }
virtual void decode(Decoder &decoder) { throw LowlevelError("decode unimplemented"); }
virtual void printEntries(ostream &s) const { throw LowlevelError("printEntries unimplemented"); }
virtual int4 getCategorySize(int4 cat) const { throw LowlevelError("getCategorySize unimplemented"); }
virtual Symbol *getCategorySymbol(int4 cat,int4 ind) const { throw LowlevelError("getCategorySymbol unimplemented"); }
File diff suppressed because it is too large Load Diff
@@ -24,6 +24,38 @@
class JoinRecord;
extern AttributeId ATTRIB_CUSTOM; ///< Marshaling attribute "custom"
extern AttributeId ATTRIB_DOTDOTDOT; ///< Marshaling attribute "dotdotdot"
extern AttributeId ATTRIB_EXTENSION; ///< Marshaling attribute "extension"
extern AttributeId ATTRIB_HASTHIS; ///< Marshaling attribute "hasthis"
extern AttributeId ATTRIB_INLINE; ///< Marshaling attribute "inline"
extern AttributeId ATTRIB_KILLEDBYCALL; ///< Marshaling attribute "killedbycall"
extern AttributeId ATTRIB_MAXSIZE; ///< Marshaling attribute "maxsize"
extern AttributeId ATTRIB_MINSIZE; ///< Marshaling attribute "minsize"
extern AttributeId ATTRIB_MODELLOCK; ///< Marshaling attribute "modellock"
extern AttributeId ATTRIB_NORETURN; ///< Marshaling attribute "noreturn"
extern AttributeId ATTRIB_POINTERMAX; ///< Marshaling attribute "pointermax"
extern AttributeId ATTRIB_SEPARATEFLOAT; ///< Marshaling attribute "separatefloat"
extern AttributeId ATTRIB_STACKSHIFT; ///< Marshaling attribute "stackshift"
extern AttributeId ATTRIB_STRATEGY; ///< Marshaling attribute "strategy"
extern AttributeId ATTRIB_THISBEFORERETPOINTER; ///< Marshaling attribute "thisbeforeretpointer"
extern AttributeId ATTRIB_VOIDLOCK; ///< Marshaling attribute "voidlock"
extern ElementId ELEM_GROUP; ///< Marshaling element \<group>
extern ElementId ELEM_INTERNALLIST; ///< Marshaling element \<internallist>
extern ElementId ELEM_KILLEDBYCALL; ///< Marshaling element \<killedbycall>
extern ElementId ELEM_LIKELYTRASH; ///< Marshaling element \<likelytrash>
extern ElementId ELEM_LOCALRANGE; ///< Marshaling element \<localrange>
extern ElementId ELEM_MODEL; ///< Marshaling element \<model>
extern ElementId ELEM_PARAM; ///< Marshaling element \<param>
extern ElementId ELEM_PARAMRANGE; ///< Marshaling element \<paramrange>
extern ElementId ELEM_PENTRY; ///< Marshaling element \<pentry>
extern ElementId ELEM_PROTOTYPE; ///< Marshaling element \<prototype>
extern ElementId ELEM_RESOLVEPROTOTYPE; ///< Marshaling element \<resolveprototype>
extern ElementId ELEM_RETPARAM; ///< Marshaling element \<retparam>
extern ElementId ELEM_RETURNSYM; ///< Marshaling element \<returnsym>
extern ElementId ELEM_UNAFFECTED; ///< Marshaling element \<unaffected>
/// \brief Exception thrown when a prototype can't be modeled properly
struct ParamUnassignedError : public LowlevelError {
ParamUnassignedError(const string &s) : LowlevelError(s) {} ///< Constructor
@@ -85,7 +117,7 @@ private:
/// \brief Is the logical value left-justified within its container
bool isLeftJustified(void) const { return (((flags&force_left_justify)!=0)||(!spaceid->isBigEndian())); }
public:
ParamEntry(int4 grp) { group=grp; } ///< Constructor for use with restoreXml
ParamEntry(int4 grp) { group=grp; } ///< Constructor for use with decode
int4 getGroup(void) const { return group; } ///< Get the group id \b this belongs to
int4 getGroupSize(void) const { return groupsize; } ///< Get the number of groups occupied by \b this
int4 getSize(void) const { return size; } ///< Get the size of the memory range in bytes.
@@ -102,13 +134,13 @@ public:
bool intersects(const Address &addr,int4 sz) const; ///< Does \b this intersect the given range in some way
int4 justifiedContain(const Address &addr,int4 sz) const; ///< Calculate endian aware containment
bool getContainer(const Address &addr,int4 sz,VarnodeData &res) const;
bool contains(const ParamEntry &op2) const; ///< Does \this contain the given entry (as a subpiece)
bool contains(const ParamEntry &op2) const; ///< Does \b this contain the given entry (as a subpiece)
OpCode assumedExtension(const Address &addr,int4 sz,VarnodeData &res) const;
int4 getSlot(const Address &addr,int4 skip) const;
AddrSpace *getSpace(void) const { return spaceid; } ///< Get the address space containing \b this entry
uintb getBase(void) const { return addressbase; } ///< Get the starting offset of \b this entry
Address getAddrBySlot(int4 &slot,int4 sz) const;
void restoreXml(const Element *el,const AddrSpaceManager *manage,bool normalstack,bool grouped,list<ParamEntry> &curList);
void decode(Decoder &decoder,const AddrSpaceManager *manage,bool normalstack,bool grouped,list<ParamEntry> &curList);
bool isParamCheckHigh(void) const { return ((flags & extracheck_high)!=0); } ///< Return \b true if there is a high overlap
bool isParamCheckLow(void) const { return ((flags & extracheck_low)!=0); } ///< Return \b true if there is a low overlap
static void orderWithinGroup(const ParamEntry &entry1,const ParamEntry &entry2); ///< Enforce ParamEntry group ordering rules
@@ -302,11 +334,11 @@ public:
class FspecSpace : public AddrSpace {
public:
FspecSpace(AddrSpaceManager *m,const Translate *t,int4 ind); ///< Constructor
virtual void saveXmlAttributes(ostream &s,uintb offset) const;
virtual void saveXmlAttributes(ostream &s,uintb offset,int4 size) const;
virtual void encodeAttributes(Encoder &encoder,uintb offset) const;
virtual void encodeAttributes(Encoder &encoder,uintb offset,int4 size) const;
virtual void printRaw(ostream &s,uintb offset) const;
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el);
virtual void decode(Decoder &decoder);
static const string NAME; ///< Reserved name for the fspec space
};
@@ -342,7 +374,7 @@ private:
VarnodeData range; ///< The memory range affected
uint4 type; ///< The type of effect
public:
EffectRecord(void) {} ///< Constructor for use with restoreXml()
EffectRecord(void) {} ///< Constructor for use with decode()
EffectRecord(const EffectRecord &op2) { range = op2.range; type = op2.type; } ///< Copy constructor
EffectRecord(const Address &addr,int4 size); ///< Construct a memory range with an unknown effect
EffectRecord(const ParamEntry &entry,uint4 t); ///< Construct an effect on a parameter storage location
@@ -352,8 +384,8 @@ public:
int4 getSize(void) const { return range.size; } ///< Get the size of the affected range
bool operator==(const EffectRecord &op2) const; ///< Equality operator
bool operator!=(const EffectRecord &op2) const; ///< Inequality operator
void saveXml(ostream &s) const; ///< Save the record to an XML stream
void restoreXml(uint4 grouptype,const Element *el,const AddrSpaceManager *manage); ///< Restore the record from an XML stream
void encode(Encoder &encoder) const; ///< Encode the record to a stream
void decode(uint4 grouptype,Decoder &decoder,const AddrSpaceManager *manage); ///< Decode the record from a stream
static bool compareByAddress(const EffectRecord &op1,const EffectRecord &op2);
};
@@ -496,13 +528,13 @@ public:
/// \return the maximum number of passes across all parameters in \b this model
virtual int4 getMaxDelay(void) const=0;
/// \brief Restore the model from an XML stream
/// \brief Restore the model from an \<input> or \<output> element in the stream
///
/// \param el is the root \<input> or \<output> element
/// \param decoder is the stream decoder
/// \param manage is used to resolve references to address spaces
/// \param effectlist is a container collecting EffectRecords across all parameters
/// \param normalstack is \b true if parameters are pushed on the stack in the normal order
virtual void restoreXml(const Element *el,const AddrSpaceManager *manage,vector<EffectRecord> &effectlist,bool normalstack)=0;
virtual void decode(Decoder &decoder,const AddrSpaceManager *manage,vector<EffectRecord> &effectlist,bool normalstack)=0;
virtual ParamList *clone(void) const=0; ///< Clone this parameter list model
};
@@ -539,12 +571,12 @@ protected:
void calcDelay(void); ///< Calculate the maximum heritage delay for any potential parameter in this list
void addResolverRange(AddrSpace *spc,uintb first,uintb last,ParamEntry *paramEntry,int4 position);
void populateResolver(void); ///< Build the ParamEntry resolver maps
void parsePentry(const Element *el,const AddrSpaceManager *manage,vector<EffectRecord> &effectlist,
void parsePentry(Decoder &decoder,const AddrSpaceManager *manage,vector<EffectRecord> &effectlist,
int4 groupid,bool normalstack,bool autokill,bool splitFloat,bool grouped);
void parseGroup(const Element *el,const AddrSpaceManager *manage,vector<EffectRecord> &effectlist,
void parseGroup(Decoder &decoder,const AddrSpaceManager *manage,vector<EffectRecord> &effectlist,
int4 groupid,bool normalstack,bool autokill,bool splitFloat);
public:
ParamListStandard(void) {} ///< Construct for use with restoreXml()
ParamListStandard(void) {} ///< Construct for use with decode()
ParamListStandard(const ParamListStandard &op2); ///< Copy constructor
virtual ~ParamListStandard(void);
const list<ParamEntry> &getEntry(void) const { return entry; } ///< Get the list of parameter entries
@@ -562,7 +594,7 @@ public:
virtual AddrSpace *getSpacebase(void) const { return spacebase; }
virtual void getRangeList(AddrSpace *spc,RangeList &res) const;
virtual int4 getMaxDelay(void) const { return maxdelay; }
virtual void restoreXml(const Element *el,const AddrSpaceManager *manage,vector<EffectRecord> &effectlist,bool normalstack);
virtual void decode(Decoder &decoder,const AddrSpaceManager *manage,vector<EffectRecord> &effectlist,bool normalstack);
virtual ParamList *clone(void) const;
};
@@ -593,7 +625,7 @@ public:
/// conventions. The assignMap() method may make less sense in this scenario.
class ParamListRegister : public ParamListStandard {
public:
ParamListRegister(void) : ParamListStandard() {} ///< Constructor for use with restoreXml()
ParamListRegister(void) : ParamListStandard() {} ///< Constructor for use with decode()
ParamListRegister(const ParamListRegister &op2) : ParamListStandard(op2) {} ///< Copy constructor
virtual uint4 getType(void) const { return p_register; }
virtual void fillinMap(ParamActive *active) const;
@@ -610,11 +642,11 @@ public:
/// to inform the input model.
class ParamListStandardOut : public ParamListRegisterOut {
public:
ParamListStandardOut(void) : ParamListRegisterOut() {} ///< Constructor for use with restoreXml()
ParamListStandardOut(void) : ParamListRegisterOut() {} ///< Constructor for use with decode()
ParamListStandardOut(const ParamListStandardOut &op2) : ParamListRegisterOut(op2) {} ///< Copy constructor
virtual uint4 getType(void) const { return p_standard_out; }
virtual void assignMap(const vector<Datatype *> &proto,TypeFactory &typefactory,vector<ParameterPieces> &res) const;
virtual void restoreXml(const Element *el,const AddrSpaceManager *manage,vector<EffectRecord> &effectlist,bool normalstack);
virtual void decode(Decoder &decoder,const AddrSpaceManager *manage,vector<EffectRecord> &effectlist,bool normalstack);
virtual ParamList *clone(void) const;
};
@@ -628,7 +660,7 @@ public:
/// need to be invoked.
class ParamListMerged : public ParamListStandard {
public:
ParamListMerged(void) : ParamListStandard() {} ///< Constructor for use with restoreXml
ParamListMerged(void) : ParamListStandard() {} ///< Constructor for use with decode
ParamListMerged(const ParamListMerged &op2) : ParamListStandard(op2) {} ///< Copy constructor
void foldIn(const ParamListStandard &op2); ///< Add another model to the union
void finalize(void) { populateResolver(); } ///< Fold-ins are finished, finalize \b this
@@ -686,7 +718,7 @@ public:
enum {
extrapop_unknown = 0x8000 ///< Reserved extrapop value meaning the function's \e extrapop is unknown
};
ProtoModel(Architecture *g); ///< Constructor for use with restoreXml()
ProtoModel(Architecture *g); ///< Constructor for use with decode()
ProtoModel(const string &nm,const ProtoModel &op2); ///< Copy constructor changing the name
virtual ~ProtoModel(void); ///< Destructor
const string &getName(void) const { return name; } ///< Get the name of the prototype model
@@ -909,7 +941,7 @@ public:
int4 getMaxOutputDelay(void) const { return output->getMaxDelay(); }
virtual bool isMerged(void) const { return false; } ///< Is \b this a merged prototype model
virtual void restoreXml(const Element *el); ///< Restore \b this model from an XML stream
virtual void decode(Decoder &decoder); ///< Restore \b this model from a stream
static uint4 lookupEffect(const vector<EffectRecord> &efflist,const Address &addr,int4 size);
static int4 lookupRecord(const vector<EffectRecord> &efflist,int4 listSize,const Address &addr,int4 size);
};
@@ -969,7 +1001,7 @@ public:
void foldIn(ProtoModel *model); ///< Fold-in an additional prototype model
ProtoModel *selectModel(ParamActive *active) const; ///< Select the best model given a set of trials
virtual bool isMerged(void) const { return true; }
virtual void restoreXml(const Element *el);
virtual void decode(Decoder &decoder);
};
class Symbol;
@@ -1116,18 +1148,19 @@ public:
virtual ProtoParameter *getOutput(void)=0; ///< Get the return-value description
virtual ProtoStore *clone(void) const=0; ///< Clone the entire collection of parameter descriptions
/// \brief Save any parameters that are not backed by symbols to an XML stream
/// \brief Encode any parameters that are not backed by symbols to a stream
///
/// Symbols are stored elsewhere, so symbol backed parameters are not serialized.
/// If there are any internal parameters an \<internallist> tag is emitted.
/// \param s is the output stream
virtual void saveXml(ostream &s) const=0;
/// If there are any internal parameters an \<internallist> element is emitted.
/// \param encoder is the stream encoder
virtual void encode(Encoder &encoder) const=0;
/// \brief Restore any internal parameter descriptions from an XML stream
/// \brief Restore any internal parameter descriptions from a stream
///
/// \param el is a root \<internallist> element containing \<param> and \<retparam> sub-tags.
/// Parse an \<internallist> element containing \<param> and \<retparam> child elements.
/// \param decoder is the stream decoder
/// \param model is prototype model for determining storage for unassigned parameters
virtual void restoreXml(const Element *el,ProtoModel *model)=0;
virtual void decode(Decoder &decoder,ProtoModel *model)=0;
};
/// \brief A parameter with a formal backing Symbol
@@ -1183,8 +1216,8 @@ public:
virtual void clearOutput(void);
virtual ProtoParameter *getOutput(void);
virtual ProtoStore *clone(void) const;
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el,ProtoModel *model);
virtual void encode(Encoder &encoder) const;
virtual void decode(Decoder &decoder,ProtoModel *model);
};
/// \brief A collection of parameter descriptions without backing symbols
@@ -1207,8 +1240,8 @@ public:
virtual void clearOutput(void);
virtual ProtoParameter *getOutput(void);
virtual ProtoStore *clone(void) const;
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el,ProtoModel *model);
virtual void encode(Encoder &encoder) const;
virtual void decode(Decoder &decoder,ProtoModel *model);
};
/// \brief Raw components of a function prototype (obtained from parsing source code)
@@ -1258,10 +1291,10 @@ class FuncProto {
int4 injectid; ///< (If non-negative) id of p-code snippet that should replace this function
int4 returnBytesConsumed; ///< Number of bytes of return value that are consumed by callers (0 = all bytes)
void updateThisPointer(void); ///< Make sure any "this" parameter is properly marked
void saveEffectXml(ostream &s) const; ///< Save any overriding EffectRecords to XML stream
void saveLikelyTrashXml(ostream &s) const; ///< Save any overriding likelytrash registers to XML stream
void restoreEffectXml(void); ///< Merge in any EffectRecord overrides
void restoreLikelyTrashXml(void); ///< Merge in any \e likelytrash overrides
void encodeEffect(Encoder &encoder) const; ///< Encode any overriding EffectRecords to stream
void encodeLikelyTrash(Encoder &encoder) const; ///< Encode any overriding likelytrash registers to stream
void decodeEffect(void); ///< Merge in any EffectRecord overrides
void decodeLikelyTrash(void); ///< Merge in any \e likelytrash overrides
protected:
void paramShift(int4 paramshift); ///< Add parameters to the front of the input parameter list
bool isParamshiftApplied(void) const { return ((flags&paramshift_applied)!=0); } ///< Has a parameter shift been applied
@@ -1502,8 +1535,8 @@ public:
/// \return the active set of flags for \b this prototype
uint4 getComparableFlags(void) const { return (flags & (dotdotdot | is_constructor | is_destructor | has_thisptr )); }
void saveXml(ostream &s) const;
void restoreXml(const Element *el,Architecture *glb);
void encode(Encoder &encoder) const;
void decode(Decoder &decoder,Architecture *glb);
};
class Funcdata;
@@ -14,7 +14,14 @@
* limitations under the License.
*/
#include "funcdata.hh"
//#include <fstream>
AttributeId ATTRIB_NOCODE = AttributeId("nocode",70);
ElementId ELEM_AST = ElementId("ast",89);
ElementId ELEM_FUNCTION = ElementId("function",90);
ElementId ELEM_HIGHLIST = ElementId("highlist",91);
ElementId ELEM_JUMPTABLELIST = ElementId("jumptablelist",92);
ElementId ELEM_VARNODES = ElementId("varnodes",93);
/// \param nm is the (base) name of the function
/// \param scope is Symbol scope associated with the function
@@ -42,7 +49,7 @@ Funcdata::Funcdata(const string &nm,Scope *scope,const Address &addr,FunctionSym
size = sz;
AddrSpace *stackid = glb->getStackSpace();
if (nm.size()==0)
localmap = (ScopeLocal *)0; // Filled in by restoreXml
localmap = (ScopeLocal *)0; // Filled in by decode
else {
uint8 id;
if (sym != (FunctionSymbol *)0)
@@ -542,66 +549,65 @@ void Funcdata::printLocalRange(ostream &s) const
}
}
/// This parses a \<jumptablelist> tag and builds a JumpTable object for
/// each \<jumptable> sub-tag.
/// \param el is the root \<jumptablelist> tag
void Funcdata::restoreXmlJumpTable(const Element *el)
/// Parse a \<jumptablelist> element and build a JumpTable object for
/// each \<jumptable> child element.
/// \param decoder is the stream decoder
void Funcdata::decodeJumpTable(Decoder &decoder)
{
const List &list( el->getChildren() );
List::const_iterator iter;
for(iter=list.begin();iter!=list.end();++iter) {
uint4 elemId = decoder.openElement(ELEM_JUMPTABLELIST);
while(decoder.peekElement() != 0) {
JumpTable *jt = new JumpTable(glb);
jt->restoreXml(*iter);
jt->decode(decoder);
jumpvec.push_back(jt);
}
decoder.closeElement(elemId);
}
/// A \<jumptablelist> tag is written with \<jumptable> sub-tags describing
/// A \<jumptablelist> element is written with \<jumptable> children describing
/// each jump-table associated with the control-flow of \b this function.
/// \param s is the output stream
void Funcdata::saveXmlJumpTable(ostream &s) const
/// \param encoder is the stream encoder
void Funcdata::encodeJumpTable(Encoder &encoder) const
{
if (jumpvec.empty()) return;
vector<JumpTable *>::const_iterator iter;
s << "<jumptablelist>\n";
encoder.openElement(ELEM_JUMPTABLELIST);
for(iter=jumpvec.begin();iter!=jumpvec.end();++iter)
(*iter)->saveXml(s);
s << "</jumptablelist>\n";
(*iter)->encode(encoder);
encoder.closeElement(ELEM_JUMPTABLELIST);
}
/// \brief Save XML descriptions for a set of Varnodes to stream
/// \brief Encode descriptions for a set of Varnodes to a stream
///
/// This is an internal function for the function's save to XML system.
/// Individual XML tags are written in sequence for Varnodes in a given set.
/// This is an internal function for the function's marshaling system.
/// Individual elements are written in sequence for Varnodes in a given set.
/// The set is bounded by iterators using the 'loc' ordering.
/// \param s is the output stream
/// \param encoder is the stream encoder
/// \param iter is the beginning of the set
/// \param enditer is the end of the set
void Funcdata::saveVarnodeXml(ostream &s,VarnodeLocSet::const_iterator iter,VarnodeLocSet::const_iterator enditer)
void Funcdata::encodeVarnode(Encoder &encoder,VarnodeLocSet::const_iterator iter,VarnodeLocSet::const_iterator enditer)
{
Varnode *vn;
while(iter!=enditer) {
vn = *iter++;
vn->saveXml(s);
s << '\n';
vn->encode(encoder);
}
}
/// This produces a single \<highlist> tag, with a \<high> sub-tag for each
/// This produces a single \<highlist> element, with a \<high> child for each
/// high-level variable (HighVariable) currently associated with \b this function.
/// \param s is the output stream
void Funcdata::saveXmlHigh(ostream &s) const
/// \param encoder is the stream encoder
void Funcdata::encodeHigh(Encoder &encoder) const
{
Varnode *vn;
HighVariable *high;
if (!isHighOn()) return;
s << "<highlist>";
encoder.openElement(ELEM_HIGHLIST);
VarnodeLocSet::const_iterator iter;
for(iter=beginLoc();iter!=endLoc();++iter) {
vn = *iter;
@@ -609,104 +615,98 @@ void Funcdata::saveXmlHigh(ostream &s) const
high = vn->getHigh();
if (high->isMark()) continue;
high->setMark();
high->saveXml(s);
high->encode(encoder);
}
for(iter=beginLoc();iter!=endLoc();++iter) {
vn = *iter;
if (!vn->isAnnotation())
vn->getHigh()->clearMark();
}
s << "</highlist>\n";
encoder.closeElement(ELEM_HIGHLIST);
}
/// A single \<ast> tag is produced with children describing Varnodes, PcodeOps, and
/// A single \<ast> element is produced with children describing Varnodes, PcodeOps, and
/// basic blocks making up \b this function's current syntax tree.
/// \param s is the output stream
void Funcdata::saveXmlTree(ostream &s) const
/// \param encoder is the stream encoder
void Funcdata::encodeTree(Encoder &encoder) const
{
s << "<ast>\n";
s << "<varnodes>\n";
encoder.openElement(ELEM_AST);
encoder.openElement(ELEM_VARNODES);
for(int4 i=0;i<glb->numSpaces();++i) {
AddrSpace *base = glb->getSpace(i);
if (base == (AddrSpace *)0 || base->getType()==IPTR_IOP) continue;
VarnodeLocSet::const_iterator iter = vbank.beginLoc(base);
VarnodeLocSet::const_iterator enditer = vbank.endLoc(base);
saveVarnodeXml(s,iter,enditer);
encodeVarnode(encoder,iter,enditer);
}
s << "</varnodes>\n";
encoder.closeElement(ELEM_VARNODES);
list<PcodeOp *>::iterator oiter,endoiter;
PcodeOp *op;
BlockBasic *bs;
for(int4 i=0;i<bblocks.getSize();++i) {
bs = (BlockBasic *)bblocks.getBlock(i);
s << "<block";
a_v_i(s,"index",bs->getIndex());
s << ">\n";
bs->saveXmlBody(s);
encoder.openElement(ELEM_BLOCK);
encoder.writeSignedInteger(ATTRIB_INDEX, bs->getIndex());
bs->encodeBody(encoder);
oiter = bs->beginOp();
endoiter = bs->endOp();
while(oiter != endoiter) {
op = *oiter++;
op->saveXml(s);
s << '\n';
op->encode(encoder);
}
s << "</block>\n";
encoder.closeElement(ELEM_BLOCK);
}
for(int4 i=0;i<bblocks.getSize();++i) {
bs = (BlockBasic *)bblocks.getBlock(i);
if (bs->sizeIn() == 0) continue;
s << "<blockedge";
a_v_i(s,"index",bs->getIndex());
s << ">\n";
bs->saveXmlEdges(s);
s << "</blockedge>\n";
encoder.openElement(ELEM_BLOCKEDGE);
encoder.writeSignedInteger(ATTRIB_INDEX, bs->getIndex());
bs->encodeEdges(encoder);
encoder.closeElement(ELEM_BLOCKEDGE);
}
s << "</ast>\n";
encoder.closeElement(ELEM_AST);
}
/// An XML description of \b this function is written to the stream,
/// A description of \b this function is written to the stream,
/// including name, address, prototype, symbol, jump-table, and override information.
/// If indicated by the caller, a description of the entire PcodeOp and Varnode
/// tree is also emitted.
/// \param s is the output stream
/// \param encoder is the stream encoder
/// \param id is the unique id associated with the function symbol
/// \param savetree is \b true if the p-code tree should be emitted
void Funcdata::saveXml(ostream &s,uint8 id,bool savetree) const
void Funcdata::encode(Encoder &encoder,uint8 id,bool savetree) const
{
s << "<function";
encoder.openElement(ELEM_FUNCTION);
if (id != 0)
a_v_u(s, "id", id);
a_v(s,"name",name);
a_v_i(s,"size",size);
encoder.writeUnsignedInteger(ATTRIB_ID, id);
encoder.writeString(ATTRIB_NAME, name);
encoder.writeSignedInteger(ATTRIB_SIZE, size);
if (hasNoCode())
a_v_b(s,"nocode",true);
s << ">\n";
baseaddr.saveXml(s);
s << '\n';
encoder.writeBool(ATTRIB_NOCODE, true);
baseaddr.encode(encoder);
if (!hasNoCode()) {
localmap->saveXmlRecursive(s,false); // Save scope and all subscopes
localmap->encodeRecursive(encoder,false); // Save scope and all subscopes
}
if (savetree) {
saveXmlTree(s);
saveXmlHigh(s);
encodeTree(encoder);
encodeHigh(encoder);
}
saveXmlJumpTable(s);
funcp.saveXml(s); // Must be saved after database
localoverride.saveXml(s,glb);
s << "</function>\n";
encodeJumpTable(encoder);
funcp.encode(encoder); // Must be saved after database
localoverride.encode(encoder,glb);
encoder.closeElement(ELEM_FUNCTION);
}
/// From an XML \<function> tag, recover the name, address, prototype, symbol,
/// Parse a \<function> element, recovering the name, address, prototype, symbol,
/// jump-table, and override information for \b this function.
/// \param el is the root \<function> tag
/// \param decoder is the stream decoder
/// \return the symbol id associated with the function
uint8 Funcdata::restoreXml(const Element *el)
uint8 Funcdata::decode(Decoder &decoder)
{
// clear(); // Shouldn't be needed
@@ -714,22 +714,20 @@ uint8 Funcdata::restoreXml(const Element *el)
size = -1;
uint8 id = 0;
AddrSpace *stackid = glb->getStackSpace();
for(int4 i=0;i<el->getNumAttributes();++i) {
const string &attrName(el->getAttributeName(i));
if (attrName == "name")
name = el->getAttributeValue(i);
else if (attrName == "size") {
istringstream s( el->getAttributeValue(i));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> size;
uint4 elemId = decoder.openElement(ELEM_FUNCTION);
for(;;) {
uint4 attribId = decoder.getNextAttributeId();
if (attribId == 0) break;
if (attribId == ATTRIB_NAME)
name = decoder.readString();
else if (attribId == ATTRIB_SIZE) {
size = decoder.readSignedInteger();
}
else if (attrName == "id") {
istringstream s( el->getAttributeValue(i));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> id;
else if (attribId == ATTRIB_ID) {
id = decoder.readUnsignedInteger();
}
else if (attrName == "nocode") {
if (xml_readbool(el->getAttributeValue(i)))
else if (attribId == ATTRIB_NOCODE) {
if (decoder.readBool())
flags |= no_code;
}
}
@@ -737,27 +735,20 @@ uint8 Funcdata::restoreXml(const Element *el)
throw LowlevelError("Missing function name");
if (size == -1)
throw LowlevelError("Missing function size");
const List &list( el->getChildren() );
List::const_iterator iter = list.begin();
baseaddr = Address::restoreXml( *iter, glb );
++iter;
for(;iter!=list.end();++iter) {
if ((*iter)->getName() == "localdb") {
baseaddr = Address::decode( decoder, glb );
for(;;) {
uint4 subId = decoder.peekElement();
if (subId == 0) break;
if (subId == ELEM_LOCALDB) {
if (localmap != (ScopeLocal *)0)
throw LowlevelError("Pre-existing local scope when restoring: "+name);
ScopeLocal *newMap = new ScopeLocal(id,stackid,this,glb);
glb->symboltab->restoreXmlScope(*iter,newMap); // May delete newMap and throw
glb->symboltab->decodeScope(decoder,newMap); // May delete newMap and throw
localmap = newMap;
}
// else if ((*iter)->getName() == "scope") {
// const Element *scopeel = *iter;
// ScopeInternal *subscope = new ScopeInternal("",glb);
// subscope->restrictScope(this);
// glb->symboltab->restore_nonglobal_xml(scopeel,subscope);
// }
else if ((*iter)->getName() == "override")
localoverride.restoreXml(*iter,glb);
else if ((*iter)->getName() == "prototype") {
else if (subId == ELEM_OVERRIDE)
localoverride.decode(decoder,glb);
else if (subId == ELEM_PROTOTYPE) {
if (localmap == (ScopeLocal *)0) {
// If we haven't seen a <localdb> tag yet, assume we have a default local scope
ScopeLocal *newMap = new ScopeLocal(id,stackid,this,glb);
@@ -766,11 +757,12 @@ uint8 Funcdata::restoreXml(const Element *el)
localmap = newMap;
}
funcp.setScope(localmap,baseaddr+ -1); // localmap built earlier
funcp.restoreXml(*iter,glb);
funcp.decode(decoder,glb);
}
else if ((*iter)->getName() == "jumptablelist")
restoreXmlJumpTable(*iter);
else if (subId == ELEM_JUMPTABLELIST)
decodeJumpTable(decoder);
}
decoder.closeElement(elemId);
if (localmap == (ScopeLocal *)0) { // Seen neither <localdb> or <prototype>
// This is a function shell, so we provide default locals
ScopeLocal *newMap = new ScopeLocal(id,stackid,this,glb);
@@ -28,6 +28,14 @@
class FlowInfo;
extern AttributeId ATTRIB_NOCODE; ///< Marshaling attribute "nocode"
extern ElementId ELEM_AST; ///< Marshaling element \<ast>
extern ElementId ELEM_FUNCTION; ///< Marshaling element \<function>
extern ElementId ELEM_HIGHLIST; ///< Marshaling element \<highlist>
extern ElementId ELEM_JUMPTABLELIST; ///< Marshaling element \<jumptablelist>
extern ElementId ELEM_VARNODES; ///< Marshaling element \<varnodes>
/// \brief Container for data structures associated with a single function
///
/// This class holds the primary data structures for decompiling a function. In particular it holds
@@ -120,7 +128,7 @@ class Funcdata {
void nodeSplitRawDuplicate(BlockBasic *b,BlockBasic *bprime);
void nodeSplitInputPatch(BlockBasic *b,BlockBasic *bprime,int4 inedge);
static bool descendantsOutside(Varnode *vn);
static void saveVarnodeXml(ostream &s,VarnodeLocSet::const_iterator iter,VarnodeLocSet::const_iterator enditer);
static void encodeVarnode(Encoder &encoder,VarnodeLocSet::const_iterator iter,VarnodeLocSet::const_iterator enditer);
static bool checkIndirectUse(Varnode *vn);
static PcodeOp *findPrimaryBranch(PcodeOpTree::const_iterator iter,PcodeOpTree::const_iterator enditer,
bool findbranch,bool findcall,bool findreturn);
@@ -177,12 +185,12 @@ public:
void printVarnodeTree(ostream &s) const; ///< Print a description of all Varnodes to a stream
void printBlockTree(ostream &s) const; ///< Print a description of control-flow structuring to a stream
void printLocalRange(ostream &s) const; ///< Print description of memory ranges associated with local scopes
void saveXml(ostream &s,uint8 id,bool savetree) const; ///< Emit an XML description of \b this function to stream
uint8 restoreXml(const Element *el); ///< Restore the state of \b this function from an XML description
void saveXmlJumpTable(ostream &s) const; ///< Emit an XML description of jump-tables to stream
void restoreXmlJumpTable(const Element *el); ///< Restore jump-tables from an XML description
void saveXmlTree(ostream &s) const; ///< Save an XML description of the p-code tree to stream
void saveXmlHigh(ostream &s) const; ///< Save an XML description of all HighVariables to stream
void encode(Encoder &encoder,uint8 id,bool savetree) const; ///< Encode a description of \b this function to stream
uint8 decode(Decoder &decoder); ///< Restore the state of \b this function from an XML description
void encodeJumpTable(Encoder &encoder) const; ///< Encode a description of jump-tables to stream
void decodeJumpTable(Decoder &decoder); ///< Decode jump-tables from a stream
void encodeTree(Encoder &encoder) const; ///< Encode a description of the p-code tree to stream
void encodeHigh(Encoder &encoder) const; ///< Encode a description of all HighVariables to stream
Override &getOverride(void) { return localoverride; } ///< Get the Override object for \b this function
@@ -139,22 +139,23 @@ void ArchitectureGhidra::readStringStream(istream &s,string &res)
}
/// The method expects to see protocol markers indicating a string from the client,
/// otherwise it throws and exception. The string is read in and then parsed as XML.
/// otherwise it throws and exception. The string is read into the given stream decoder.
/// \param s is the input stream from the client.
/// \return the XML document
Document *ArchitectureGhidra::readXMLStream(istream &s)
/// \param decoder is the given stream decoder that will hold the result
/// \return \b true if a response was received
bool ArchitectureGhidra::readStream(istream &s,Decoder &decoder)
{
int4 type = readToAnyBurst(s);
if (type==14) {
Document *doc = xml_tree(s);
decoder.ingestStream(s);
type = readToAnyBurst(s);
if (type!=15)
throw JavaError("alignment","Expecting XML string end");
return doc;
return true;
}
if ((type&1)==1)
return (Document *)0;
return false;
throw JavaError("alignment","Expecting string or end of query response");
}
@@ -231,17 +232,19 @@ void ArchitectureGhidra::readResponseEnd(istream &s)
}
/// Read up to the beginning of a query response, check for an
/// exception record, otherwise read in a string as an XML document.
/// exception record, otherwise pass back an element for decoding.
/// \param s is the input stream from the client
/// \return the XML document
Document *ArchitectureGhidra::readXMLAll(istream &s)
/// \param decoder is the stream decoder that will hold the result
/// \return \b true if we received a valid response
bool ArchitectureGhidra::readAll(istream &s,Decoder &decoder)
{
readToResponse(s);
Document *doc = readXMLStream(s);
if (doc != (Document *)0)
if (readStream(s,decoder)) {
readResponseEnd(s);
return doc;
return true;
}
return false;
}
/// Read up to the beginning of a query response, check for an
@@ -339,8 +342,10 @@ void ArchitectureGhidra::buildTypegrp(DocumentStorage &store)
{
const Element *el = store.getTag("coretypes");
types = new TypeFactoryGhidra(this);
if (el != (const Element *)0)
types->restoreXmlCoreTypes(el);
if (el != (const Element *)0) {
XmlDecode decoder(el);
types->decodeCoreTypes(decoder);
}
else {
// Put in the core types
types->setCoreType("void",1,TYPE_VOID,false);
@@ -398,11 +403,12 @@ void ArchitectureGhidra::resolveArchitecture(void)
}
/// Ask the Ghidra client if it knows about a specific processor register.
/// The client responds with a \<addr> XML element describing the storage
/// The client responds with a \<addr> element describing the storage
/// location of the register.
/// \param regname is the name to query for
/// \return the storage address as XML or NULL if the register is unknown
Document *ArchitectureGhidra::getRegister(const string &regname)
/// \param decoder is the stream decoder that will hold the result
/// \return \b true if the query completed successfully
bool ArchitectureGhidra::getRegister(const string &regname,Decoder &decoder)
{
sout.write("\000\000\001\004",4);
@@ -411,7 +417,7 @@ Document *ArchitectureGhidra::getRegister(const string &regname)
sout.write("\000\000\001\005",4);
sout.flush();
return readXMLAll(sin);
return readAll(sin,decoder);
}
/// Given a storage location and size, ask the Ghidra client if it knows of
@@ -426,7 +432,8 @@ string ArchitectureGhidra::getRegisterName(const VarnodeData &vndata)
writeStringStream(sout,"getRegisterName");
sout.write("\000\000\001\016",4); // Beginning of string header
Address addr(vndata.space,vndata.offset);
addr.saveXml(sout,vndata.size);
encoder.clear();
addr.encode(encoder,vndata.size);
sout.write("\000\000\001\017",4);
sout.write("\000\000\001\005",4);
sout.flush();
@@ -438,24 +445,26 @@ string ArchitectureGhidra::getRegisterName(const VarnodeData &vndata)
return res;
}
/// The Ghidra client will return a description of registers that have
/// The Ghidra client will pass back a description of registers that have
/// known values at the given address. The response is generally a
/// \<tracked_pointset> which contains \<set> children which contains
/// a storage location and value.
/// \param addr is the given address
/// \return the response Document
Document *ArchitectureGhidra::getTrackedRegisters(const Address &addr)
/// \param decoder is the stream decoder which will hold the result
/// \return \b true if the query completed successfully
bool ArchitectureGhidra::getTrackedRegisters(const Address &addr,Decoder &decoder)
{
sout.write("\000\000\001\004",4);
writeStringStream(sout,"getTrackedRegisters");
sout.write("\000\000\001\016",4); // Beginning of string header
addr.saveXml(sout);
encoder.clear();
addr.encode(encoder);
sout.write("\000\000\001\017",4);
sout.write("\000\000\001\005",4);
sout.flush();
return readXMLAll(sin);
return readAll(sin,decoder);
}
/// The first operand to a CALLOTHER op indicates the specific user-defined op.
@@ -491,7 +500,8 @@ uint1 *ArchitectureGhidra::getPcodePacked(const Address &addr)
sout.write("\000\000\001\004",4);
writeStringStream(sout,"getPacked");
sout.write("\000\000\001\016",4); // Beginning of string header
addr.saveXml(sout);
encoder.clear();
addr.encode(encoder);
sout.write("\000\000\001\017",4);
sout.write("\000\000\001\005",4);
sout.flush();
@@ -499,54 +509,59 @@ uint1 *ArchitectureGhidra::getPcodePacked(const Address &addr)
return readPackedAll(sin);
}
/// The Ghidra client will return a \<symbol> tag, \<function> tag, or some
/// other related Symbol information. If there no symbol at the address
/// the client should return a \<hole> tag describing the size of the
/// The Ghidra client will pass back a \<symbol> element, \<function> element, or some
/// other related Symbol information, in the given stream decoder. If there is no symbol at the address
/// the client will return a \<hole> element describing the size of the
/// memory region that is free of symbols.
/// \param addr is the given address
/// \return the symbol document
Document *ArchitectureGhidra::getMappedSymbolsXML(const Address &addr)
/// \param decoder is the given stream decoder that will hold the result
/// \return \b true if the query completes successfully
bool ArchitectureGhidra::getMappedSymbolsXML(const Address &addr,Decoder &decoder)
{
sout.write("\000\000\001\004",4);
writeStringStream(sout,"getMappedSymbolsXML");
sout.write("\000\000\001\016",4); // Beginning of string header
addr.saveXml(sout);
sout.write("\000\000\001\016",4); // Beginning of string
encoder.clear();
addr.encode(encoder);
sout.write("\000\000\001\017",4);
sout.write("\000\000\001\005",4);
sout.flush();
return readXMLAll(sin);
return readAll(sin,decoder);
}
/// This asks the Ghidra client to resolve an \e external \e reference.
/// This is an address for which the client holds a reference to a function
/// that is elsewhere in memory or not in memory at all. The client
/// should unravel the reference from the given address and return either
/// a \<function> tag describing the referred to function symbol or
/// a \<hole> tag if the reference can't be resolved
/// should unravel the reference from the given address and pass back either
/// a \<function> element describing the referred to function symbol or
/// a \<hole> element if the reference can't be resolved.
/// \param addr is the given address
/// \return a description of the referred to function
Document *ArchitectureGhidra::getExternalRefXML(const Address &addr)
/// \param decoder is the stream decoder that will hold the result
/// \return \b true if the query completes successfully
bool ArchitectureGhidra::getExternalRefXML(const Address &addr,Decoder &decoder)
{
sout.write("\000\000\001\004",4);
writeStringStream(sout,"getExternalRefXML");
sout.write("\000\000\001\016",4); // Beginning of string header
addr.saveXml(sout);
encoder.clear();
addr.encode(encoder);
sout.write("\000\000\001\017",4);
sout.write("\000\000\001\005",4);
sout.flush();
return readXMLAll(sin);
return readAll(sin,decoder);
}
/// Ask the Ghidra client to list all namespace elements between the global root
/// and the namespace of the given id. The client should return a \<parent> tag with
/// a \<val> child for each namespace in the path.
/// and the namespace of the given id. The client will pass back a \<parent> element with
/// a \<val> child for each namespace in the path, in the given stream decoder
/// \param id is the given id of the namespace to resolve
/// \return the XML document
Document *ArchitectureGhidra::getNamespacePath(uint8 id)
/// \param decoder is the given stream decoder that will hold the result
/// \return \b true if the query completed successfully
bool ArchitectureGhidra::getNamespacePath(uint8 id,Decoder &decoder)
{
sout.write("\000\000\001\004",4);
@@ -557,7 +572,7 @@ Document *ArchitectureGhidra::getNamespacePath(uint8 id)
sout.write("\000\000\001\005",4);
sout.flush();
return readXMLAll(sin);
return readAll(sin,decoder);
}
bool ArchitectureGhidra::isNameUsed(const string &nm,uint8 startId,uint8 stopId)
@@ -593,7 +608,8 @@ string ArchitectureGhidra::getCodeLabel(const Address &addr)
sout.write("\000\000\001\004",4);
writeStringStream(sout,"getSymbol");
sout.write("\000\000\001\016",4); // Beginning of string header
addr.saveXml(sout);
encoder.clear();
addr.encode(encoder);
sout.write("\000\000\001\017",4);
sout.write("\000\000\001\005",4);
sout.flush();
@@ -605,12 +621,13 @@ string ArchitectureGhidra::getCodeLabel(const Address &addr)
return res;
}
/// The Ghidra client should respond with a \<type> tag giving details
/// The Ghidra client will pass back a \<type> element giving details
/// about the data-type.
/// \param name is the name of the data-type
/// \param id is a unique id associated with the data-type, pass 0 if unknown
/// \return the data-type XML element or NULL
Document *ArchitectureGhidra::getType(const string &name,uint8 id)
/// \param decoder is the stream decoder that will hold the result
/// \return \b true if the query completed successfully
bool ArchitectureGhidra::getType(const string &name,uint8 id,Decoder &decoder)
{
sout.write("\000\000\001\004",4);
@@ -622,23 +639,25 @@ Document *ArchitectureGhidra::getType(const string &name,uint8 id)
sout.write("\000\000\001\005",4);
sout.flush();
return readXMLAll(sin);
return readAll(sin,decoder);
}
/// Ask Ghidra client for all comments associated with one function.
/// The caller must provide the sub-set of properties (Comment::comment_type) for
/// the query to match. The client will return a \<commentdb> tag with
/// a \<comment> tag child for each comment found.
/// the query to match. A \<commentdb> element with \<comment> element child for each comment found
/// will be passed back in the given stream decoder.
/// \param fad is the address of the function to query
/// \param flags specifies the properties the query will match (must be non-zero)
/// \return an XML document describing each comment
Document *ArchitectureGhidra::getComments(const Address &fad,uint4 flags)
/// \param decoder is the given stream decoder that will hold the result
/// \return \b true if the query completed successfully
bool ArchitectureGhidra::getComments(const Address &fad,uint4 flags,Decoder &decoder)
{
sout.write("\000\000\001\004",4);
writeStringStream(sout,"getComments");
sout.write("\000\000\001\016",4); // Beginning of string header
fad.saveXml(sout);
encoder.clear();
fad.encode(encoder);
sout.write("\000\000\001\017",4);
sout.write("\000\000\001\016",4); // Beginning of string header
sout << dec << flags;
@@ -646,7 +665,7 @@ Document *ArchitectureGhidra::getComments(const Address &fad,uint4 flags)
sout.write("\000\000\001\005",4);
sout.flush();
return readXMLAll(sin);
return readAll(sin,decoder);
}
/// The Ghidra client is queried for a range of bytes, which are returned
@@ -661,7 +680,8 @@ void ArchitectureGhidra::getBytes(uint1 *buf,int4 size,const Address &inaddr)
sout.write("\000\000\001\004",4);
writeStringStream(sout,"getBytes");
sout.write("\000\000\001\016",4); // Beginning of string header
inaddr.saveXml(sout,size);
encoder.clear();
inaddr.encode(encoder,size);
sout.write("\000\000\001\017",4);
sout.write("\000\000\001\005",4);
sout.flush();
@@ -708,7 +728,8 @@ void ArchitectureGhidra::getStringData(vector<uint1> &buffer,const Address &addr
sout.write("\000\000\001\004",4);
writeStringStream(sout,"getString");
sout.write("\000\000\001\016",4); // Beginning of string header
addr.saveXml(sout,maxBytes);
encoder.clear();
addr.encode(encoder,maxBytes);
sout.write("\000\000\001\017",4);
writeStringStream(sout,ct->getName());
sout.write("\000\000\001\016",4); // Beginning of string header
@@ -753,14 +774,14 @@ void ArchitectureGhidra::getStringData(vector<uint1> &buffer,const Address &addr
/// - CALLMECHANISM_TYPE
/// - EXECUTABLEPCODE_TYPE
///
/// This and additional context is provided to the Ghidra client which returns
/// an XML document describing the p-code. The document will be an \<inst> tag
/// containing individual \<op> tags corresponding to individual p-code ops.
/// This and additional context is provided to the Ghidra client which passes back
/// an \<inst> element containing individual \<op> tags corresponding to individual p-code ops.
/// \param name is the name of the injection
/// \param type is the type of injection
/// \param con is the context object
/// \return an XML document describing the p-code ops to inject
Document *ArchitectureGhidra::getPcodeInject(const string &name,int4 type,const InjectContext &con)
/// \param decoder is the stream decoder which will hold the result
/// \return \b true if the query completed successfully
bool ArchitectureGhidra::getPcodeInject(const string &name,int4 type,const InjectContext &con,Decoder &decoder)
{
sout.write("\000\000\001\004",4);
@@ -774,21 +795,22 @@ Document *ArchitectureGhidra::getPcodeInject(const string &name,int4 type,const
writeStringStream(sout,"getXPcode");
writeStringStream(sout,name);
sout.write("\000\000\001\016",4);
con.saveXml(sout);
encoder.clear();
con.encode(encoder);
sout.write("\000\000\001\017",4);
sout.write("\000\000\001\005",4);
sout.flush();
return readXMLAll(sin);
return readAll(sin,decoder);
}
/// The Ghidra client is provided a sequence of 1 or more integer values
/// extracted from a CPOOLREF op. It returns an XML document describing
/// the constant pool record referenced by the integer(s) or will throw
/// an exception if record isn't properly referenced.
/// extracted from a CPOOLREF op. A description of the constant pool record referenced by the integer(s)
/// is passed back in the given stream decoder, or an exception is thrown if the record isn't properly referenced.
/// \param refs is an array of 1 or more integer values referencing a constant pool record
/// \return a description of the record as a \<cpoolrec> XML document.
Document *ArchitectureGhidra::getCPoolRef(const vector<uintb> &refs)
/// \param decoder is the given stream decoder that will hold the result
/// \return \b true if the query completed successfully
bool ArchitectureGhidra::getCPoolRef(const vector<uintb> &refs,Decoder &decoder)
{
sout.write("\000\000\001\004",4);
@@ -802,7 +824,7 @@ Document *ArchitectureGhidra::getCPoolRef(const vector<uintb> &refs)
sout.write("\000\000\001\005",4);
sout.flush();
return readXMLAll(sin);
return readAll(sin,decoder);
}
// Document *ArchitectureGhidra::getScopeProperties(Scope *newscope)
@@ -839,7 +861,7 @@ void ArchitectureGhidra::printMessage(const string &message) const
/// \param o is the output stream to the Ghidra client
ArchitectureGhidra::ArchitectureGhidra(const string &pspec,const string &cspec,const string &tspec,
const string &corespec,istream &i,ostream &o)
: Architecture(), sin(i), sout(o)
: Architecture(), sin(i), sout(o), encoder(sout)
{
print->setXML(true);
@@ -60,6 +60,7 @@ struct JavaError : public LowlevelError {
class ArchitectureGhidra : public Architecture {
istream &sin; ///< Input stream for interfacing with Ghidra
ostream &sout; ///< Output stream for interfacing with Ghidra
XmlEncode encoder; ///< Encoder used to write to Ghidra
mutable string warnings; ///< Warnings accumulated by the decompiler
string pspecxml; ///< XML pspec passed from Ghidra
string cspecxml; ///< XML cspec passed from Ghidra
@@ -86,21 +87,21 @@ public:
istream &i,ostream &o);
const string &getWarnings(void) const { return warnings; } ///< Get warnings produced by the last decompilation
void clearWarnings(void) { warnings.clear(); } ///< Clear warnings
Document *getRegister(const string &regname); ///< Retrieve a register description given a name
bool getRegister(const string &regname,Decoder &decoder); ///< Retrieve a register description given a name
string getRegisterName(const VarnodeData &vndata); ///< Retrieve a register name given its storage location
Document *getTrackedRegisters(const Address &addr); ///< Retrieve \e tracked register values at the given address
bool getTrackedRegisters(const Address &addr,Decoder &decoder); ///< Retrieve \e tracked register values at the given address
string getUserOpName(int4 index); ///< Get the name of a user-defined p-code op
uint1 *getPcodePacked(const Address &addr); ///< Get p-code for a single instruction
Document *getMappedSymbolsXML(const Address &addr); ///< Get symbols associated with the given address
Document *getExternalRefXML(const Address &addr); ///< Retrieve a description of an external function
Document *getNamespacePath(uint8 id); ///< Get a description of a namespace path
bool getMappedSymbolsXML(const Address &addr,Decoder &decoder); ///< Get symbols associated with the given address
bool getExternalRefXML(const Address &addr,Decoder &decoder); ///< Retrieve a description of an external function
bool getNamespacePath(uint8 id,Decoder &decoder); ///< Get a description of a namespace path
bool isNameUsed(const string &nm,uint8 startId,uint8 stopId); ///< Is given name used along namespace path
string getCodeLabel(const Address &addr); ///< Retrieve a label at the given address
Document *getType(const string &name,uint8 id); ///< Retrieve a data-type description for the given name and id
Document *getComments(const Address &fad,uint4 flags); ///< Retrieve comments for a particular function
bool getType(const string &name,uint8 id,Decoder &decoder); ///< Retrieve a data-type description for the given name and id
bool getComments(const Address &fad,uint4 flags,Decoder &decoder); ///< Retrieve comments for a particular function
void getBytes(uint1 *buf,int4 size,const Address &inaddr); ///< Retrieve bytes in the LoadImage at the given address
Document *getPcodeInject(const string &name,int4 type,const InjectContext &con);
Document *getCPoolRef(const vector<uintb> &refs); ///< Resolve a constant pool reference
bool getPcodeInject(const string &name,int4 type,const InjectContext &con,Decoder &decoder);
bool getCPoolRef(const vector<uintb> &refs,Decoder &decoder); ///< Resolve a constant pool reference
// Document *getScopeProperties(Scope *newscope);
/// \brief Toggle whether the data-flow and control-flow is emitted as part of the main decompile action
@@ -138,8 +139,8 @@ public:
static void writeStringStream(ostream &s,const string &msg); ///< Send a string to the client
static void readToResponse(istream &s); ///< Read the query response protocol marker
static void readResponseEnd(istream &s); ///< Read the ending query response protocol marker
static Document *readXMLAll(istream &s); ///< Read a whole response as an XML document
static Document *readXMLStream(istream &s); ///< Receive an XML document from the client
static bool readAll(istream &s,Decoder &decoder); ///< Read a whole response as an XML document
static bool readStream(istream &s,Decoder &decoder); ///< Receive an XML document from the client
static uint1 *readPackedStream(istream &s); ///< Read packed p-code op information
static uint1 *readPackedAll(istream &s); ///< Read a whole response as packed p-code op information
static void passJavaException(ostream &s,const string &tp,const string &msg);
@@ -19,11 +19,24 @@ const TrackedSet &ContextGhidra::getTrackedSet(const Address &addr) const
{
cache.clear();
XmlDecode decoder;
((ArchitectureGhidra *)glb)->getTrackedRegisters(addr,decoder);
Document *doc = ((ArchitectureGhidra *)glb)->getTrackedRegisters(addr);
Element *root = doc->getRoot();
restoreTracked(root,glb,cache);
delete doc;
uint4 elemId = decoder.openElement(ELEM_TRACKED_POINTSET);
decodeTracked(decoder,glb,cache);
decoder.closeElement(elemId);
return cache;
}
void ContextGhidra::decode(Decoder &decoder,const AddrSpaceManager *manage)
{
decoder.skipElement(); // Ignore details handled by ghidra
}
void ContextGhidra::decodeFromSpec(Decoder &decoder,const AddrSpaceManager *manage)
{
decoder.skipElement(); // Ignore details handled by ghidra
}
@@ -51,8 +51,8 @@ public:
virtual const TrackedSet &getTrackedSet(const Address &addr) const;
// Ignored routines (taken care of by GHIDRA)
virtual void restoreXml(const Element *el,const AddrSpaceManager *manage) {}
virtual void restoreFromSpec(const Element *el,const AddrSpaceManager *manage) {}
virtual void decode(Decoder &decoder,const AddrSpaceManager *manage);
virtual void decodeFromSpec(Decoder &decoder,const AddrSpaceManager *manage);
// Unimplemented routines (should never be called)
virtual int getContextSize(void) const {
@@ -63,8 +63,8 @@ public:
throw LowlevelError("getContext should not be called for GHIDRA"); }
virtual void registerVariable(const string &nm,int4 sbit,int4 ebit) {
throw LowlevelError("registerVariable should not be called for GHIDRA"); }
virtual void saveXml(ostream &s) const {
throw LowlevelError("context::saveXml should not be called for GHIDRA"); }
virtual void encode(Encoder &encoder) const {
throw LowlevelError("context::encode should not be called for GHIDRA"); }
virtual TrackedSet &createSet(const Address &addr1,const Address &addr2) {
throw LowlevelError("createSet should not be called for GHIDRA"); }
@@ -274,12 +274,9 @@ void DecompileAt::loadParameters(void)
{
GhidraCommand::loadParameters();
Document *doc;
doc = ArchitectureGhidra::readXMLStream(sin); // Read XML of address directly from in stream
addr = Address::restoreXml(doc->getRoot(),ghidra); // Parse XML for functions address
addr.toPhysical(); // Only for backward compatibility
// with SLED
delete doc;
XmlDecode decoder;
ArchitectureGhidra::readStream(sin,decoder); // Read encoded address directly from in stream
addr = Address::decode(decoder,ghidra); // Decode for functions address
}
void DecompileAt::rawAction(void)
@@ -305,25 +302,18 @@ void DecompileAt::rawAction(void)
sout.write("\000\000\001\016",4);
// Write output XML directly to outstream
if (fd->isProcComplete()) {
//bool v1 = ghidra->getSendParamMeasures();
//sout << "value: " << ghidra->getSendParamMeasures() << "\n";
//bool v2 = (ghidra->allacts.getCurrentName() == "paramid");
//sout << "value: " << (ghidra->allacts.getCurrentName() == "paramid") << "\n";
//bool v3 = v1 && v2;
sout << "<doc>\n";
//sout << (v1?"1":"0") << "(" << (int4)v1 << ")\n" << (v2?"1":"0") << "\n" << (v3?"1":"0") << "\n";
XmlEncode encoder(sout);
if (ghidra->getSendParamMeasures() && (ghidra->allacts.getCurrentName() == "paramid")) {
ParamIDAnalysis pidanalysis( fd, true ); // Only send back final prototype
pidanalysis.saveXml( sout, true );
pidanalysis.encode( encoder, true );
}
else {
if (ghidra->getSendParamMeasures()) {
ParamIDAnalysis pidanalysis( fd, false );
pidanalysis.saveXml( sout, true );
pidanalysis.encode( encoder, true );
}
fd->saveXml(sout,0,ghidra->getSendSyntaxTree());
fd->encode(encoder,0,ghidra->getSendSyntaxTree());
if (ghidra->getSendCCode()&&
(ghidra->allacts.getCurrentName() == "decompile"))
ghidra->print->docFunction(fd);
@@ -337,10 +327,10 @@ void StructureGraph::loadParameters(void)
{
GhidraCommand::loadParameters();
Document *doc;
doc = ArchitectureGhidra::readXMLStream(sin);
ingraph.restoreXml(doc->getRoot(),ghidra);
delete doc;
XmlDecode decoder;
ArchitectureGhidra::readStream(sin,decoder);
ingraph.decode(decoder,ghidra);
}
void StructureGraph::rawAction(void)
@@ -358,7 +348,8 @@ void StructureGraph::rawAction(void)
resultgraph.orderBlocks();
sout.write("\000\000\001\016",4);
resultgraph.saveXml(sout);
XmlEncode encoder(sout);
resultgraph.encode(encoder);
sout.write("\000\000\001\017",4);
ingraph.clear();
}
@@ -413,28 +404,12 @@ void SetAction::sendResult(void)
GhidraCommand::sendResult();
}
SetOptions::SetOptions(void) : GhidraCommand()
{
doc = (Document *)0;
}
SetOptions::~SetOptions(void)
{
if (doc != (Document *)0)
delete doc;
}
void SetOptions::loadParameters(void)
{
GhidraCommand::loadParameters();
if (doc != (Document *)0) {
delete doc;
doc = (Document *)0;
}
doc = ArchitectureGhidra::readXMLStream(sin);
decoder.clear();
ArchitectureGhidra::readStream(sin,decoder);
}
void SetOptions::rawAction(void)
@@ -443,9 +418,8 @@ void SetOptions::rawAction(void)
res = false;
ghidra->resetDefaults();
ghidra->options->restoreXml(doc->getRoot());
delete doc;
doc = (Document *)0;
ghidra->options->decode(decoder);
decoder.clear();
res = true;
}
@@ -514,6 +488,8 @@ int main(int argc,char **argv)
{
signal(SIGSEGV, &ArchitectureGhidra::segvHandler); // Exit on SEGV errors
AttributeId::initialize();
ElementId::initialize();
CapabilityPoint::initializeAll();
int4 status = 0;
while(status == 0) {
@@ -220,12 +220,10 @@ public:
/// The command returns a single character message, 't' or 'f', indicating whether the
/// configuration succeeded.
class SetOptions : public GhidraCommand {
Document *doc; ///< The XML option document
XmlDecode decoder; ///< The XML option document
virtual void loadParameters(void);
virtual void sendResult(void);
public:
SetOptions(void);
virtual ~SetOptions(void);
bool res; ///< Set to \b true if the option change succeeded
virtual void rawAction(void);
};
@@ -36,7 +36,8 @@ void GhidraTranslate::initialize(DocumentStorage &store)
const Element *el = store.getTag("sleigh");
if (el == (const Element *)0)
throw LowlevelError("Could not find ghidra sleigh tag");
restoreXml(el);
XmlDecode decoder(el);
decode(decoder);
}
const VarnodeData &GhidraTranslate::getRegister(const string &nm) const
@@ -45,9 +46,10 @@ const VarnodeData &GhidraTranslate::getRegister(const string &nm) const
map<string,VarnodeData>::const_iterator iter = nm2addr.find(nm);
if (iter != nm2addr.end())
return (*iter).second;
Document *doc;
XmlDecode decoder;
try {
doc = glb->getRegister(nm); // Ask Ghidra client about the register
if (!glb->getRegister(nm,decoder)) // Ask Ghidra client about the register
throw LowlevelError("No register named "+nm);
}
catch(XmlError &err) {
ostringstream errmsg;
@@ -55,16 +57,13 @@ const VarnodeData &GhidraTranslate::getRegister(const string &nm) const
errmsg << " -- " << err.explain;
throw LowlevelError(errmsg.str());
}
if (doc == (Document *)0)
throw LowlevelError("No register named "+nm);
Address regaddr;
int4 regsize;
regaddr = Address::restoreXml( doc->getRoot(), this, regsize);
regaddr = Address::decode( decoder, this, regsize);
VarnodeData vndata;
vndata.space = regaddr.getSpace();
vndata.offset = regaddr.getOffset();
vndata.size = regsize;
delete doc;
return cacheRegister(nm,vndata);
}
@@ -142,33 +141,23 @@ int4 GhidraTranslate::oneInstruction(PcodeEmit &emit,const Address &baseaddr) co
return offset;
}
/// The Ghidra client passes descriptions of address spaces and other
/// information that needs to be cached by the decompiler
/// \param el is the element of the initialization tag
void GhidraTranslate::restoreXml(const Element *el)
/// Parse the \<sleigh> element passed back by the Ghidra client, describing address spaces
/// and other information that needs to be cached by the decompiler.
/// \param decoder is the stream decoder
void GhidraTranslate::decode(Decoder &decoder)
{
setBigEndian(xml_readbool(el->getAttributeValue("bigendian")));
{
istringstream s(el->getAttributeValue("uniqbase"));
s.unsetf(ios::dec | ios::hex | ios::oct);
uintm ubase;
s >> ubase;
setUniqueBase(ubase);
}
const List &list(el->getChildren());
List::const_iterator iter;
iter = list.begin();
restoreXmlSpaces(*iter,this);
++iter;
while(iter != list.end()) {
const Element *subel = *iter;
if (subel->getName() == "truncate_space") {
TruncationTag tag;
tag.restoreXml(subel);
truncateSpace(tag);
}
++iter;
uint4 elemId = decoder.openElement(ELEM_SLEIGH);
setBigEndian(decoder.readBool(ATTRIB_BIGENDIAN));
setUniqueBase(decoder.readUnsignedInteger(ATTRIB_UNIQBASE));
decodeSpaces(decoder,this);
for(;;) {
uint4 subId = decoder.peekElement();
if (subId != ELEM_TRUNCATE_SPACE) break;
TruncationTag tag;
tag.decode(decoder);
truncateSpace(tag);
}
decoder.closeElement(elemId);
}
@@ -36,7 +36,7 @@ class GhidraTranslate : public Translate {
mutable map<string,VarnodeData> nm2addr; ///< Mapping from register name to Varnode
mutable map<VarnodeData,string> addr2nm; ///< Mapping rom Varnode to register name
const VarnodeData &cacheRegister(const string &nm,const VarnodeData &data) const;
void restoreXml(const Element *el); ///< Initialize \b this Translate from XML
void decode(Decoder &decoder); ///< Initialize \b this Translate from a stream
public:
GhidraTranslate(ArchitectureGhidra *g) { glb = g; } ///< Constructor
@@ -15,6 +15,14 @@
*/
#include "globalcontext.hh"
ElementId ELEM_CONTEXT_DATA = ElementId("context_data",94);
ElementId ELEM_CONTEXT_POINTS = ElementId("context_points",95);
ElementId ELEM_CONTEXT_POINTSET = ElementId("context_pointset",96);
ElementId ELEM_CONTEXT_SET = ElementId("context_set",97);
ElementId ELEM_SET = ElementId("set",98);
ElementId ELEM_TRACKED_POINTSET = ElementId("tracked_pointset",99);
ElementId ELEM_TRACKED_SET = ElementId("tracked_set",100);
/// Bits within the whole context blob are labeled starting with 0 as the most significant bit
/// in the first word in the sequence. The new context value must be contained within a single
/// word.
@@ -30,76 +38,64 @@ ContextBitRange::ContextBitRange(int4 sbit,int4 ebit)
mask = (~((uintm)0))>>(startbit+shift);
}
/// The register storage and value are serialized as a \<set> tag.
/// \param s is the output stream
void TrackedContext::saveXml(ostream &s) const
/// The register storage and value are encoded as a \<set> element.
/// \param encoder is the stream encoder
void TrackedContext::encode(Encoder &encoder) const
{
s << "<set";
loc.space->saveXmlAttributes(s,loc.offset,loc.size);
a_v_u(s,"val",val);
s << "/>\n";
encoder.openElement(ELEM_SET);
loc.space->encodeAttributes(encoder,loc.offset,loc.size);
encoder.writeUnsignedInteger(ATTRIB_VAL, val);
encoder.closeElement(ELEM_SET);
}
/// Read a \<set> tag to fill in the storage and value details
/// \param el is the root \<set> tag
/// Parse a \<set> element to fill in the storage and value details.
/// \param decoder is the stream decoder
/// \param manage is the manager used to decode address references
void TrackedContext::restoreXml(const Element *el,const AddrSpaceManager *manage)
void TrackedContext::decode(Decoder &decoder,const AddrSpaceManager *manage)
{
int4 size;
Address addr = Address::restoreXml(el,manage,size);
istringstream s(el->getAttributeValue("val"));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> val;
uint4 elemId = decoder.openElement(ELEM_SET);
loc.decodeFromAttributes(decoder, manage);
loc.space = addr.getSpace();
loc.offset = addr.getOffset();
loc.size = size;
val = decoder.readUnsignedInteger(ATTRIB_VAL);
decoder.closeElement(elemId);
}
/// \brief Save all tracked register values for a specific address to an XML stream
/// \brief Encode all tracked register values for a specific address to a stream
///
/// Encode all the tracked register values associated with a specific target address
/// as a \<tracked_pointset> tag.
/// \param s is the output stream
/// \param encoder is the stream encoder
/// \param addr is the specific address we have tracked values for
/// \param vec is the list of tracked values
void ContextDatabase::saveTracked(ostream &s,const Address &addr,
const TrackedSet &vec)
void ContextDatabase::encodeTracked(Encoder &encoder,const Address &addr,const TrackedSet &vec)
{
if (vec.empty()) return;
s << "<tracked_pointset";
addr.getSpace()->saveXmlAttributes(s,addr.getOffset() );
s << ">\n";
encoder.openElement(ELEM_TRACKED_POINTSET);
addr.getSpace()->encodeAttributes(encoder,addr.getOffset() );
for(int4 i=0;i<vec.size();++i) {
s << " ";
vec[i].saveXml(s);
vec[i].encode(encoder);
}
s << "</tracked_pointset>\n";
encoder.closeElement(ELEM_TRACKED_POINTSET);
}
/// \brief Restore a sequence of tracked register values from an XML stream
/// \brief Restore a sequence of tracked register values from the given stream decoder
///
/// Given a root \<tracked_pointset> tag, decode each child in turn populating a list of
/// Parse a \<tracked_pointset> element, decoding each child in turn to populate a list of
/// TrackedContext objects.
/// \param el is the root tag
/// \param decoder is the given stream decoder
/// \param manage is used to resolve address space references
/// \param vec is the container that will hold the new TrackedContext objects
void ContextDatabase::restoreTracked(const Element *el,const AddrSpaceManager *manage,
void ContextDatabase::decodeTracked(Decoder &decoder,const AddrSpaceManager *manage,
TrackedSet &vec)
{
vec.clear(); // Clear out any old stuff
const List &list(el->getChildren());
List::const_iterator iter = list.begin();
while(iter != list.end()) {
const Element *subel = *iter;
while(decoder.peekElement() != 0) {
vec.emplace_back();
vec.back().restoreXml(subel,manage);
++iter;
vec.back().decode(decoder,manage);
}
}
@@ -314,54 +310,47 @@ ContextInternal::FreeArray &ContextInternal::FreeArray::operator=(const FreeArra
return *this;
}
/// \brief Write out a single context block as an XML tag
/// \brief Encode a single context block to a stream
///
/// The blob is broken up into individual values and written out as a series
/// of \<set> tags within a parent \<context_pointset> tag.
/// \param s is the output stream
/// of \<set> elements within a parent \<context_pointset> element.
/// \param encoder is the stream encoder
/// \param addr is the address of the split point where the blob is valid
/// \param vec is the array of words holding the blob values
void ContextInternal::saveContext(ostream &s,const Address &addr,
const uintm *vec) const
void ContextInternal::encodeContext(Encoder &encoder,const Address &addr,const uintm *vec) const
{
s << "<context_pointset";
addr.getSpace()->saveXmlAttributes(s,addr.getOffset() );
s << ">\n";
encoder.openElement(ELEM_CONTEXT_POINTSET);
addr.getSpace()->encodeAttributes(encoder,addr.getOffset() );
map<string,ContextBitRange>::const_iterator iter;
for(iter=variables.begin();iter!=variables.end();++iter) {
uintm val = (*iter).second.getValue(vec);
s << " <set";
a_v(s,"name",(*iter).first);
a_v_u(s,"val",val);
s << "/>\n";
encoder.openElement(ELEM_SET);
encoder.writeString(ATTRIB_NAME, (*iter).first);
encoder.writeUnsignedInteger(ATTRIB_VAL, val);
encoder.closeElement(ELEM_SET);
}
s << "</context_pointset>\n";
encoder.closeElement(ELEM_CONTEXT_POINTSET);
}
/// \brief Restore a context blob for given address range from an XML tag
/// \brief Restore a context blob for given address range from a stream decoder
///
/// The tag can be either \<context_pointset> or \<context_set>. In either case,
/// Parse either a \<context_pointset> or \<context_set> element. In either case,
/// children are parsed to get context variable values. Then a context blob is
/// reconstructed from the values. The new blob is added to the interval map based
/// on the address range. If the start address is invalid, the default value of
/// the context variables are painted. The second address can be invalid, if
/// only a split point is known.
/// \param el is the root XML tag
/// \param decoder is the stream decoder
/// \param addr1 is the starting address of the given range
/// \param addr2 is the ending address of the given range
void ContextInternal::restoreContext(const Element *el,const Address &addr1,const Address &addr2)
void ContextInternal::decodeContext(Decoder &decoder,const Address &addr1,const Address &addr2)
{
const List &list(el->getChildren());
List::const_iterator iter = list.begin();
while(iter != list.end()) {
const Element *subel = *iter;
istringstream s(subel->getAttributeValue("val"));
s.unsetf(ios::dec | ios::hex | ios::oct);
uintm val;
s >> val;
ContextBitRange &var(getVariable(subel->getAttributeValue("name")));
for(;;) {
uint4 subId = decoder.openElement();
if (subId != ELEM_SET) break;
uintm val = decoder.readUnsignedInteger(ATTRIB_VAL);
ContextBitRange &var(getVariable(decoder.readString(ATTRIB_NAME)));
vector<uintm *> vec;
if (addr1.isInvalid()) { // Invalid addr1, indicates we should set default value
uintm *defaultBuffer = getDefaultValue();
@@ -373,7 +362,7 @@ void ContextInternal::restoreContext(const Element *el,const Address &addr1,cons
getRegionForSet(vec,addr1,addr2,var.getWord(),var.getMask()<<var.getShift());
for(int4 i=0;i<vec.size();++i)
var.setValue(vec[i],val);
++iter;
decoder.closeElement(subId);
}
}
@@ -487,83 +476,81 @@ TrackedSet &ContextInternal::createSet(const Address &addr1,const Address &addr2
return res;
}
void ContextInternal::saveXml(ostream &s) const
void ContextInternal::encode(Encoder &encoder) const
{
if (database.empty() && trackbase.empty()) return;
s << "<context_points>\n";
encoder.openElement(ELEM_CONTEXT_POINTS);
partmap<Address,FreeArray>::const_iterator fiter,fenditer;
fiter = database.begin();
fenditer = database.end();
for(;fiter!=fenditer;++fiter) // Save context at each changepoint
saveContext(s,(*fiter).first,(*fiter).second.array);
encodeContext(encoder,(*fiter).first,(*fiter).second.array);
partmap<Address,TrackedSet>::const_iterator titer,tenditer;
titer = trackbase.begin();
tenditer = trackbase.end();
for(;titer!=tenditer;++titer)
saveTracked(s,(*titer).first,(*titer).second);
encodeTracked(encoder,(*titer).first,(*titer).second);
s << "</context_points>\n";
encoder.closeElement(ELEM_CONTEXT_POINTS);
}
void ContextInternal::restoreXml(const Element *el,const AddrSpaceManager *manage)
void ContextInternal::decode(Decoder &decoder,const AddrSpaceManager *manage)
{
const List &list(el->getChildren());
List::const_iterator iter = list.begin();
while(iter != list.end()) {
const Element *subel = *iter;
if (subel->getName() == "context_pointset") {
if (subel->getNumAttributes()==0) {
restoreContext(subel,Address(),Address()); // Restore the default value
uint4 elemId = decoder.openElement(ELEM_CONTEXT_POINTS);
for(;;) {
uint4 subId = decoder.openElement();
if (subId == 0) break;
if (subId == ELEM_CONTEXT_POINTSET) {
uint4 attribId = decoder.getNextAttributeId();
decoder.rewindAttributes();
if (attribId==0) {
decodeContext(decoder,Address(),Address()); // Restore the default value
}
else {
Address addr = Address::restoreXml(subel,manage);
restoreContext(subel,addr,Address());
VarnodeData vData;
vData.decodeFromAttributes(decoder, manage);
decodeContext(decoder,vData.getAddr(),Address());
}
}
else if (subel->getName() == "tracked_pointset") {
Address addr = Address::restoreXml(subel,manage);
restoreTracked(subel,manage,trackbase.split(addr) );
else if (subId == ELEM_TRACKED_POINTSET) {
VarnodeData vData;
vData.decodeFromAttributes(decoder, manage);
decodeTracked(decoder,manage,trackbase.split(vData.getAddr()) );
}
else
throw LowlevelError("Bad <context_points> tag: "+subel->getName());
++iter;
throw LowlevelError("Bad <context_points> tag");
decoder.closeElement(subId);
}
decoder.closeElement(elemId);
}
void ContextInternal::restoreFromSpec(const Element *el,const AddrSpaceManager *manage)
void ContextInternal::decodeFromSpec(Decoder &decoder,const AddrSpaceManager *manage)
{
const List &list(el->getChildren());
List::const_iterator iter = list.begin();
while(iter != list.end()) {
const Element *subel = *iter;
if (subel->getName() == "context_set") {
Range range;
range.restoreXml(subel,manage); // There MUST be a range
Address addr1,addr2;
addr1 = range.getFirstAddr();
addr2 = range.getLastAddrOpen(manage);
restoreContext(subel,addr1,addr2);
uint4 elemId = decoder.openElement(ELEM_CONTEXT_DATA);
for(;;) {
uint4 subId = decoder.openElement();
if (subId == 0) break;
Range range;
range.decodeFromAttributes(decoder, manage); // There MUST be a range
Address addr1 = range.getFirstAddr();
Address addr2 = range.getLastAddrOpen(manage);
if (subId == ELEM_CONTEXT_SET) {
decodeContext(decoder,addr1,addr2);
}
else if (subel->getName() == "tracked_set") {
Range range;
range.restoreXml(subel,manage); // There MUST be a range
Address addr1,addr2;
addr1 = range.getFirstAddr();
addr2 = range.getLastAddrOpen(manage);
restoreTracked(subel,manage,createSet(addr1,addr2));
else if (subId == ELEM_TRACKED_SET) {
decodeTracked(decoder,manage,createSet(addr1,addr2));
}
else
throw LowlevelError("Bad <context_data> tag: "+subel->getName());
++iter;
throw LowlevelError("Bad <context_data> tag");
decoder.closeElement(subId);
}
decoder.closeElement(elemId);
}
/// \param db is the context database that will be encapsulated
@@ -22,6 +22,14 @@
#include "pcoderaw.hh"
#include "partmap.hh"
extern ElementId ELEM_CONTEXT_DATA; ///< Marshaling element \<context_data>
extern ElementId ELEM_CONTEXT_POINTS; ///< Marshaling element \<context_points>
extern ElementId ELEM_CONTEXT_POINTSET; ///< Marshaling element \<context_pointset>
extern ElementId ELEM_CONTEXT_SET; ///< Marshaling element \<context_set>
extern ElementId ELEM_SET; ///< Marshaling element \<set>
extern ElementId ELEM_TRACKED_POINTSET; ///< Marshaling element \<tracked_pointset>
extern ElementId ELEM_TRACKED_SET; ///< Marshaling element \<tracked_set>
/// \brief Description of a context variable within the disassembly context \e blob
///
/// Disassembly context is stored as individual (integer) values packed into a sequence of words. This class
@@ -34,7 +42,7 @@ class ContextBitRange {
int4 shift; ///< Right-shift amount to apply when unpacking this value from its word
uintm mask; ///< Mask to apply (after shifting) when unpacking this value from its word
public:
ContextBitRange(void) { } ///< Constructor for use with restoreXml()
ContextBitRange(void) { } ///< Construct an undefined bit range
ContextBitRange(int4 sbit,int4 ebit); ///< Construct a context value given an absolute bit range
int4 getShift(void) const { return shift; } ///< Return the shift-amount for \b this value
uintm getMask(void) const { return mask; } ///< Return the mask for \b this value
@@ -68,8 +76,8 @@ public:
struct TrackedContext {
VarnodeData loc; ///< Storage details of the register being tracked
uintb val; ///< The value of the register
void restoreXml(const Element *el,const AddrSpaceManager *manage); ///< Restore \b this from an XML stream
void saveXml(ostream &s) const; ///< Save \b this to an XML stream
void decode(Decoder &decoder,const AddrSpaceManager *manage); ///< Decode \b this from a stream
void encode(Encoder &encoder) const; ///< Encode \b this to a stream
};
typedef vector<TrackedContext> TrackedSet; ///< A set of tracked registers and their values (at one code point)
@@ -107,8 +115,8 @@ typedef vector<TrackedContext> TrackedSet; ///< A set of tracked registers and
/// a list of TrackedContext objects.
class ContextDatabase {
protected:
static void saveTracked(ostream &s,const Address &addr,const TrackedSet &vec);
static void restoreTracked(const Element *el,const AddrSpaceManager *manage,TrackedSet &vec);
static void encodeTracked(Encoder &encoder,const Address &addr,const TrackedSet &vec);
static void decodeTracked(Decoder &decoder,const AddrSpaceManager *manage,TrackedSet &vec);
/// \brief Retrieve the context variable description object by name
///
@@ -218,24 +226,24 @@ public:
/// \return the empty set of tracked register values
virtual TrackedSet &createSet(const Address &addr1,const Address &addr2)=0;
/// \brief Serialize the entire database to an XML stream
/// \brief Encode the entire database to a stream
///
/// \param s is the output stream
virtual void saveXml(ostream &s) const=0;
/// \param encoder is the stream encoder
virtual void encode(Encoder &encoder) const=0;
/// \brief Restore the state of \b this database object from a serialized XML stream
/// \brief Restore the state of \b this database object from the given stream decoder
///
/// \param el is the root element of the XML describing the database state
/// \param decoder is the given stream decoder
/// \param manage is used to resolve address space references
virtual void restoreXml(const Element *el,const AddrSpaceManager *manage)=0;
virtual void decode(Decoder &decoder,const AddrSpaceManager *manage)=0;
/// \brief Add initial context state from XML tags in compiler/processor specifications
/// \brief Add initial context state from elements in the compiler/processor specifications
///
/// The database can be configured with a consistent initial state by providing
/// \<context_data> tags in either the compiler or processor specification file for the architecture
/// \param el is a \<context_data> tag
/// Parse a \<context_data> element from the given stream decoder from either the compiler
/// or processor specification file for the architecture, initializing this database.
/// \param decoder is the given stream decoder
/// \param manage is used to resolve address space references
virtual void restoreFromSpec(const Element *el,const AddrSpaceManager *manage)=0;
virtual void decodeFromSpec(Decoder &decoder,const AddrSpaceManager *manage)=0;
void setVariableDefault(const string &nm,uintm val); ///< Provide a default value for a context variable
uintm getDefaultValue(const string &nm) const; ///< Retrieve the default value for a context variable
@@ -274,8 +282,8 @@ class ContextInternal : public ContextDatabase {
map<string,ContextBitRange> variables; ///< Map from context variable name to description object
partmap<Address,FreeArray> database; ///< Partition map of context blobs (FreeArray)
partmap<Address,TrackedSet> trackbase; ///< Partition map of tracked register sets
void saveContext(ostream &s,const Address &addr,const uintm *vec) const;
void restoreContext(const Element *el,const Address &addr1,const Address &addr2);
void encodeContext(Encoder &encoder,const Address &addr,const uintm *vec) const;
void decodeContext(Decoder &decoder,const Address &addr1,const Address &addr2);
virtual ContextBitRange &getVariable(const string &nm);
virtual const ContextBitRange &getVariable(const string &nm) const;
virtual void getRegionForSet(vector<uintm *> &res,const Address &addr1,
@@ -296,9 +304,9 @@ public:
virtual const TrackedSet &getTrackedSet(const Address &addr) const { return trackbase.getValue(addr); }
virtual TrackedSet &createSet(const Address &addr1,const Address &addr2);
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el,const AddrSpaceManager *manage);
virtual void restoreFromSpec(const Element *el,const AddrSpaceManager *manage);
virtual void encode(Encoder &encoder) const;
virtual void decode(Decoder &decoder,const AddrSpaceManager *manage);
virtual void decodeFromSpec(Decoder &decoder,const AddrSpaceManager *manage);
};
/// \brief A helper class for caching the active context blob to minimize database lookups
@@ -314,7 +314,7 @@ void IfcOption::execute(istream &s)
}
try {
string res = dcp->conf->options->set(optname,p1,p2,p3);
string res = dcp->conf->options->set(ElementId::find(optname),p1,p2,p3);
*status->optr << res << endl;
}
catch(ParseError &err) {
@@ -2690,7 +2690,8 @@ void IfcCallGraphDump::execute(istream &s)
if (!os)
throw IfaceExecutionError("Unable to open file "+name);
dcp->cgraph->saveXml(os);
XmlEncode encoder(os);
dcp->cgraph->encode(encoder);
os.close();
*status->optr << "Successfully saved callgraph to " << name << endl;
}
@@ -2723,7 +2724,8 @@ void IfcCallGraphLoad::execute(istream &s)
Document *doc = store.parseDocument(is);
dcp->allocateCallGraph();
dcp->cgraph->restoreXml(doc->getRoot());
XmlDecode decoder(doc->getRoot());
dcp->cgraph->decoder(decoder);
*status->optr << "Successfully read in callgraph" << endl;
Scope *gscope = dcp->conf->symboltab->getGlobalScope();
@@ -3014,7 +3016,8 @@ void IfcStructureBlocks::execute(istream &s)
try {
BlockGraph ingraph;
ingraph.restoreXml(doc->getRoot(),dcp->conf);
XmlDecode decoder(doc->getRoot());
ingraph.decode(decoder,dcp->conf);
BlockGraph resultgraph;
vector<FlowBlock *> rootlist;
@@ -3030,7 +3033,8 @@ void IfcStructureBlocks::execute(istream &s)
sout.open(outfile.c_str());
if (!sout)
throw IfaceExecutionError("Unable to open output file: "+outfile);
resultgraph.saveXml(sout);
XmlEncode encoder(sout);
resultgraph.encode(encoder);
sout.close();
}
catch(LowlevelError &err) {
@@ -15,42 +15,43 @@
*/
#include "inject_ghidra.hh"
void InjectContextGhidra::saveXml(ostream &s) const
void InjectContextGhidra::encode(Encoder &encoder) const
{
s << "<context>\n";
baseaddr.saveXml(s);
calladdr.saveXml(s);
encoder.openElement(ELEM_CONTEXT);
baseaddr.encode(encoder);
calladdr.encode(encoder);
if (!inputlist.empty()) {
s << "<input>\n";
encoder.openElement(ELEM_INPUT);
for(int4 i=0;i<inputlist.size();++i) {
const VarnodeData &vn( inputlist[i] );
s << "<addr";
vn.space->saveXmlAttributes(s,vn.offset,vn.size);
s << "/>\n";
encoder.openElement(ELEM_ADDR);
vn.space->encodeAttributes(encoder,vn.offset,vn.size);
encoder.closeElement(ELEM_ADDR);
}
s << "</input>\n";
encoder.closeElement(ELEM_INPUT);
}
if (!output.empty()) {
s << "<output>\n";
encoder.openElement(ELEM_OUTPUT);
for(int4 i=0;i<output.size();++i) {
const VarnodeData &vn( output[i] );
s << "<addr";
vn.space->saveXmlAttributes(s,vn.offset,vn.size);
s << "/>\n";
encoder.openElement(ELEM_ADDR);
vn.space->encodeAttributes(encoder,vn.offset,vn.size);
encoder.closeElement(ELEM_ADDR);
}
s << "</output>\n";
encoder.closeElement(ELEM_OUTPUT);
}
s << "</context>\n";
encoder.closeElement(ELEM_CONTEXT);
}
void InjectPayloadGhidra::inject(InjectContext &con,PcodeEmit &emit) const
{
Document *doc;
ArchitectureGhidra *ghidra = (ArchitectureGhidra *)con.glb;
XmlDecode decoder;
try {
doc = ghidra->getPcodeInject(name,type,con);
if (!ghidra->getPcodeInject(name,type,con,decoder))
throw LowlevelError("Could not retrieve pcode snippet: "+name);
}
catch(JavaError &err) {
throw LowlevelError("Error getting pcode snippet: " + err.explain);
@@ -58,15 +59,19 @@ void InjectPayloadGhidra::inject(InjectContext &con,PcodeEmit &emit) const
catch(XmlError &err) {
throw LowlevelError("Error in pcode snippet xml: "+err.explain);
}
if (doc == (Document *)0) {
throw LowlevelError("Could not retrieve pcode snippet: "+name);
}
const Element *el = doc->getRoot();
const List &list(el->getChildren());
List::const_iterator iter;
for(iter=list.begin();iter!=list.end();++iter)
emit.restoreXmlOp(*iter,ghidra->translate);
delete doc;
uint4 elemId = decoder.openElement();
while(decoder.peekElement() != 0)
emit.decodeOp(decoder,ghidra->translate);
decoder.closeElement(elemId);
}
void InjectPayloadGhidra::decode(Decoder &decoder)
{
// Restore a raw <pcode> tag. Used for uponentry, uponreturn
uint4 elemId = decoder.openElement(ELEM_PCODE);
decodePayloadAttributes(decoder);
decoder.closeElementSkipping(elemId);
}
void InjectPayloadGhidra::printTemplate(ostream &s) const
@@ -80,10 +85,12 @@ InjectCallfixupGhidra::InjectCallfixupGhidra(const string &src,const string &nm)
{
}
void InjectCallfixupGhidra::restoreXml(const Element *el)
void InjectCallfixupGhidra::decode(Decoder &decoder)
{
name = el->getAttributeValue("name");
uint4 elemId = decoder.openElement(ELEM_CALLFIXUP);
name = decoder.readString(ATTRIB_NAME);
decoder.closeElementSkipping(elemId); // Skip processing the body, let ghidra handle this
}
InjectCallotherGhidra::InjectCallotherGhidra(const string &src,const string &nm)
@@ -91,16 +98,18 @@ InjectCallotherGhidra::InjectCallotherGhidra(const string &src,const string &nm)
{
}
void InjectCallotherGhidra::restoreXml(const Element *el)
void InjectCallotherGhidra::decode(Decoder &decoder)
{
const List &list(el->getChildren());
List::const_iterator iter;
name = el->getAttributeValue("targetop");
iter = list.begin();
if ((iter == list.end()) || ((*iter)->getName() != "pcode"))
uint4 elemId = decoder.openElement(ELEM_CALLOTHERFIXUP);
name = decoder.readString(ATTRIB_TARGETOP);
uint4 subId = decoder.openElement();
if (subId != ELEM_PCODE)
throw LowlevelError("<callotherfixup> does not contain a <pcode> tag");
InjectPayload::restoreXml(*iter);
decodePayloadAttributes(decoder);
decodePayloadParams(decoder);
decoder.closeElementSkipping(subId); // Skip processing the body, let ghidra handle this
decoder.closeElement(elemId);
}
ExecutablePcodeGhidra::ExecutablePcodeGhidra(Architecture *g,const string &src,const string &nm)
@@ -111,10 +120,11 @@ ExecutablePcodeGhidra::ExecutablePcodeGhidra(Architecture *g,const string &src,c
void ExecutablePcodeGhidra::inject(InjectContext &con,PcodeEmit &emit) const
{
Document *doc;
ArchitectureGhidra *ghidra = (ArchitectureGhidra *)con.glb;
XmlDecode decoder;
try {
doc = ghidra->getPcodeInject(name,type,con);
if (!ghidra->getPcodeInject(name,type,con,decoder))
throw LowlevelError("Could not retrieve pcode snippet: "+name);
}
catch(JavaError &err) {
throw LowlevelError("Error getting pcode snippet: " + err.explain);
@@ -122,22 +132,22 @@ void ExecutablePcodeGhidra::inject(InjectContext &con,PcodeEmit &emit) const
catch(XmlError &err) {
throw LowlevelError("Error in pcode snippet xml: "+err.explain);
}
if (doc == (Document *)0) {
throw LowlevelError("Could not retrieve pcode snippet: "+name);
}
const Element *el = doc->getRoot();
const List &list(el->getChildren());
List::const_iterator iter;
for(iter=list.begin();iter!=list.end();++iter)
emit.restoreXmlOp(*iter,ghidra->translate);
delete doc;
uint4 elemId = decoder.openElement();
while(decoder.peekElement() != 0)
emit.decodeOp(decoder,ghidra->translate);
decoder.closeElement(elemId);
}
void ExecutablePcodeGhidra::restoreXml(const Element *el)
void ExecutablePcodeGhidra::decode(Decoder &decoder)
{
InjectPayload::restoreXml(el); // Read parameters
// But ignore rest of body
uint4 elemId = decoder.openElement();
if (elemId != ELEM_CASE_PCODE && elemId != ELEM_ADDR_PCODE &&
elemId != ELEM_DEFAULT_PCODE && elemId != ELEM_SIZE_PCODE)
throw XmlError("Expecting <case_pcode>, <addr_pcode>, <default_pcode>, or <size_pcode>");
decodePayloadAttributes(decoder);
decodePayloadParams(decoder); // Parse the parameters
decoder.closeElementSkipping(elemId); // But skip rest of body
}
void ExecutablePcodeGhidra::printTemplate(ostream &s) const
@@ -28,7 +28,7 @@
/// that can then be forwarded to the Ghidra client.
class InjectContextGhidra : public InjectContext {
public:
virtual void saveXml(ostream &s) const;
virtual void encode(Encoder &encoder) const;
};
/// \brief An injection payload that uses a Ghidra client to generate the p-code ops
@@ -41,7 +41,7 @@ class InjectPayloadGhidra : public InjectPayload {
public:
InjectPayloadGhidra(const string &src,const string &nm,int4 tp) : InjectPayload(nm,tp) { source = src; } ///< Constructor
virtual void inject(InjectContext &context,PcodeEmit &emit) const;
virtual void restoreXml(const Element *el) {}
virtual void decode(Decoder &decoder);
virtual void printTemplate(ostream &s) const;
virtual string getSource(void) const { return source; }
};
@@ -50,14 +50,14 @@ public:
class InjectCallfixupGhidra : public InjectPayloadGhidra {
public:
InjectCallfixupGhidra(const string &src,const string &nm); ///< Constructor
virtual void restoreXml(const Element *el);
virtual void decode(Decoder &decoder);
};
/// \brief A callother-fixup injection that uses a Ghidra client to generate the p-code ops
class InjectCallotherGhidra : public InjectPayloadGhidra {
public:
InjectCallotherGhidra(const string &src,const string &nm); ///< Constructor
virtual void restoreXml(const Element *el);
virtual void decode(Decoder &decoder);
};
/// \brief A \e p-code \e script that uses a Ghidra client to generate the p-code ops
@@ -69,7 +69,7 @@ class ExecutablePcodeGhidra : public ExecutablePcode {
public:
ExecutablePcodeGhidra(Architecture *g,const string &src,const string &nm); ///< Constructor
virtual void inject(InjectContext &context,PcodeEmit &emit) const;
virtual void restoreXml(const Element *el);
virtual void decode(Decoder &decoder);
virtual void printTemplate(ostream &s) const;
};
@@ -61,22 +61,31 @@ void InjectPayloadSleigh::inject(InjectContext &context,PcodeEmit &emit) const
con.cacher.emit(con.baseaddr,&emit);
}
void InjectPayloadSleigh::restoreXml(const Element *el)
/// The content is read as raw p-code source
/// \param decoder is the XML stream decoder
void InjectPayloadSleigh::decodeBody(Decoder &decoder)
{
InjectPayload::restoreXml(el);
const List &list(el->getChildren());
List::const_iterator iter;
for(iter=list.begin();iter!=list.end();++iter) {
const Element *subel = *iter;
if (subel->getName() == "body") {
parsestring = subel->getContent();
}
uint4 elemId = decoder.openElement(); // Tag may not be present
if (elemId == ELEM_BODY) {
parsestring = decoder.readString(ATTRIB_CONTENT);
decoder.closeElement(elemId);
}
if (parsestring.size() == 0 && (!dynamic))
throw LowlevelError("Missing <body> subtag in <pcode>: "+getSource());
}
void InjectPayloadSleigh::decode(Decoder &decoder)
{
// Restore a raw <pcode> tag. Used for uponentry, uponreturn
uint4 elemId = decoder.openElement(ELEM_PCODE);
decodePayloadAttributes(decoder);
decodePayloadParams(decoder);
decodeBody(decoder);
decoder.closeElement(elemId);
}
void InjectPayloadSleigh::printTemplate(ostream &s) const
{
@@ -138,23 +147,27 @@ InjectPayloadCallfixup::InjectPayloadCallfixup(const string &sourceName)
{
}
void InjectPayloadCallfixup::restoreXml(const Element *el)
void InjectPayloadCallfixup::decode(Decoder &decoder)
{
const List &list(el->getChildren());
List::const_iterator iter;
name = el->getAttributeValue("name");
uint4 elemId = decoder.openElement(ELEM_CALLFIXUP);
name = decoder.readString(ATTRIB_NAME);
bool pcodeSubtag = false;
for(iter=list.begin();iter!=list.end();++iter) {
const Element *subel = *iter;
if (subel->getName() == "pcode") {
InjectPayloadSleigh::restoreXml(subel);
for(;;) {
uint4 subId = decoder.openElement();
if (subId == 0) break;
if (subId == ELEM_PCODE) {
decodePayloadAttributes(decoder);
decodePayloadParams(decoder);
decodeBody(decoder);
pcodeSubtag = true;
}
else if (subel->getName() == "target")
targetSymbolNames.push_back(subel->getAttributeValue("name"));
else if (subId == ELEM_TARGET)
targetSymbolNames.push_back(decoder.readString(ATTRIB_NAME));
decoder.closeElement(subId);
}
decoder.closeElement(elemId);
if (!pcodeSubtag)
throw LowlevelError("<callfixup> is missing <pcode> subtag: "+name);
}
@@ -164,16 +177,19 @@ InjectPayloadCallother::InjectPayloadCallother(const string &sourceName)
{
}
void InjectPayloadCallother::restoreXml(const Element *el)
void InjectPayloadCallother::decode(Decoder &decoder)
{
const List &list(el->getChildren());
List::const_iterator iter;
name = el->getAttributeValue("targetop");
iter = list.begin();
if ((iter == list.end()) || ((*iter)->getName() != "pcode"))
uint4 elemId = decoder.openElement(ELEM_CALLOTHERFIXUP);
name = decoder.readString(ATTRIB_TARGETOP);
uint4 subId = decoder.openElement();
if (subId != ELEM_PCODE)
throw LowlevelError("<callotherfixup> does not contain a <pcode> tag");
InjectPayloadSleigh::restoreXml(*iter);
decodePayloadAttributes(decoder);
decodePayloadParams(decoder);
decodeBody(decoder);
decoder.closeElement(subId);
decoder.closeElement(elemId);
}
ExecutablePcodeSleigh::ExecutablePcodeSleigh(Architecture *g,const string &src,const string &nm)
@@ -211,22 +227,18 @@ void ExecutablePcodeSleigh::inject(InjectContext &context,PcodeEmit &emit) const
con.cacher.emit(con.baseaddr,&emit);
}
void ExecutablePcodeSleigh::restoreXml(const Element *el)
void ExecutablePcodeSleigh::decode(Decoder &decoder)
{
InjectPayload::restoreXml(el);
const List &list(el->getChildren());
List::const_iterator iter;
bool hasbody = false;
for (iter = list.begin(); iter != list.end(); ++iter) {
const Element *subel = *iter;
if (subel->getName() == "body") {
hasbody = true;
parsestring = subel->getContent();
}
}
if (!hasbody)
throw LowlevelError("Missing <body> subtag in <pcode>: " + getSource());
uint4 elemId = decoder.openElement();
if (elemId != ELEM_CASE_PCODE && elemId != ELEM_ADDR_PCODE && elemId != ELEM_DEFAULT_PCODE && elemId != ELEM_SIZE_PCODE)
throw XmlError("Expecting <case_pcode>, <addr_pcode>, <default_pcode>, or <size_pcode>");
decodePayloadAttributes(decoder);
decodePayloadParams(decoder);
uint4 subId = decoder.openElement(ELEM_BODY);
parsestring = decoder.readString(ATTRIB_CONTENT);
decoder.closeElement(subId);
decoder.closeElement(elemId);
}
void ExecutablePcodeSleigh::printTemplate(ostream &s) const
@@ -243,16 +255,12 @@ InjectPayloadDynamic::~InjectPayloadDynamic(void)
delete (*iter).second;
}
void InjectPayloadDynamic::restoreEntry(const Element *el)
void InjectPayloadDynamic::decodeEntry(Decoder &decoder)
{
const List &list(el->getChildren());
List::const_iterator iter;
iter = list.begin();
Address addr = Address::restoreXml(*iter,glb);
++iter;
istringstream s((*iter)->getContent());
Address addr = Address::decode(decoder,glb);
uint4 subId = decoder.openElement(ELEM_PAYLOAD);
istringstream s(decoder.readString(ATTRIB_CONTENT));
try {
Document *doc = xml_tree(s);
map<Address,Document *>::iterator iter = addrMap.find(addr);
@@ -263,6 +271,7 @@ void InjectPayloadDynamic::restoreEntry(const Element *el)
catch(XmlError &err) {
throw LowlevelError("Error in dynamic payload XML");
}
decoder.closeElement(subId);
}
void InjectPayloadDynamic::inject(InjectContext &context,PcodeEmit &emit) const
@@ -272,10 +281,11 @@ void InjectPayloadDynamic::inject(InjectContext &context,PcodeEmit &emit) const
if (eiter == addrMap.end())
throw LowlevelError("Missing dynamic inject");
const Element *el = (*eiter).second->getRoot();
const List &list(el->getChildren());
List::const_iterator iter;
for(iter=list.begin();iter!=list.end();++iter)
emit.restoreXmlOp(*iter,glb->translate);
XmlDecode decoder(el);
uint4 rootId = decoder.openElement(ELEM_INST);
while(decoder.peekElement() != 0)
emit.decodeOp(decoder,glb->translate);
decoder.closeElement(rootId);
}
PcodeInjectLibrarySleigh::PcodeInjectLibrarySleigh(Architecture *g)
@@ -402,26 +412,24 @@ void PcodeInjectLibrarySleigh::registerInject(int4 injectid)
}
}
void PcodeInjectLibrarySleigh::restoreDebug(const Element *el)
void PcodeInjectLibrarySleigh::decodeDebug(Decoder &decoder)
{
const List &list(el->getChildren());
List::const_iterator iter;
for(iter=list.begin();iter!=list.end();++iter) {
const Element *subel = *iter;
const string &name( subel->getAttributeValue("name") );
istringstream s( subel->getAttributeValue("type") );
int4 type = -1;
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> type;
uint4 elemId = decoder.openElement(ELEM_INJECTDEBUG);
for(;;) {
uint4 subId = decoder.openElement();
if (subId != ELEM_INJECT) break;
string name = decoder.readString(ATTRIB_NAME);
int4 type = decoder.readSignedInteger(ATTRIB_TYPE);
int4 id = getPayloadId(type,name);
InjectPayloadDynamic *payload = dynamic_cast<InjectPayloadDynamic *>(getPayload(id));
if (payload == (InjectPayloadDynamic *)0) {
payload = forceDebugDynamic(id);
}
payload->restoreEntry(subel);
payload->decodeEntry(decoder);
decoder.closeElement(subId);
}
decoder.closeElement(elemId);
}
const vector<OpBehavior *> &PcodeInjectLibrarySleigh::getBehaviors(void)
@@ -25,7 +25,7 @@ public:
ParserContext *pos;
InjectContextSleigh(void) { pos = (ParserContext *)0; }
virtual ~InjectContextSleigh(void);
virtual void saveXml(ostream &s) const {} // We don't need this functionality for sleigh
virtual void encode(Encoder &encoder) const {} // We don't need this functionality for sleigh
};
class InjectPayloadSleigh : public InjectPayload {
@@ -33,11 +33,13 @@ class InjectPayloadSleigh : public InjectPayload {
ConstructTpl *tpl;
string parsestring;
string source;
protected:
void decodeBody(Decoder &decoder); ///< Parse the <body> tag
public:
InjectPayloadSleigh(const string &src,const string &nm,int4 tp);
virtual ~InjectPayloadSleigh(void);
virtual void inject(InjectContext &context,PcodeEmit &emit) const;
virtual void restoreXml(const Element *el);
virtual void decode(Decoder &decoder);
virtual void printTemplate(ostream &s) const;
virtual string getSource(void) const { return source; }
@@ -52,13 +54,13 @@ class InjectPayloadCallfixup : public InjectPayloadSleigh {
vector<string> targetSymbolNames;
public:
InjectPayloadCallfixup(const string &sourceName);
virtual void restoreXml(const Element *el);
virtual void decode(Decoder &decoder);
};
class InjectPayloadCallother : public InjectPayloadSleigh {
public:
InjectPayloadCallother(const string &sourceName);
virtual void restoreXml(const Element *el);
virtual void decode(Decoder &decoder);
};
class ExecutablePcodeSleigh : public ExecutablePcode {
@@ -70,7 +72,7 @@ protected:
ExecutablePcodeSleigh(Architecture *g,const string &src,const string &nm);
virtual ~ExecutablePcodeSleigh(void);
virtual void inject(InjectContext &context,PcodeEmit &emit) const;
virtual void restoreXml(const Element *el);
virtual void decode(Decoder &decoder);
virtual void printTemplate(ostream &s) const;
};
@@ -80,8 +82,9 @@ class InjectPayloadDynamic : public InjectPayload {
public:
InjectPayloadDynamic(Architecture *g,const string &nm,int4 tp) : InjectPayload(nm,tp) { glb = g; dynamic = true; }
virtual ~InjectPayloadDynamic(void);
void restoreEntry(const Element *el);
void decodeEntry(Decoder &decoder);
virtual void inject(InjectContext &context,PcodeEmit &emit) const;
virtual void decode(Decoder &decoder) { throw LowlevelError("decode not supported for InjectPayloadDynamic"); }
virtual void printTemplate(ostream &s) const { s << "dynamic"; }
virtual string getSource(void) const { return "dynamic"; }
};
@@ -98,7 +101,7 @@ protected:
virtual void registerInject(int4 injectid);
public:
PcodeInjectLibrarySleigh(Architecture *g);
virtual void restoreDebug(const Element *el);
virtual void decodeDebug(Decoder &decoder);
virtual int4 manualCallFixup(const string &name,const string &snippetstring);
virtual int4 manualCallOtherFixup(const string &name,const string &outname,const vector<string> &inname,
const string &snippet);
@@ -17,32 +17,38 @@
#include "emulate.hh"
#include "flow.hh"
/// \param s is the XML stream to write to
void LoadTable::saveXml(ostream &s) const
AttributeId ATTRIB_LABEL = AttributeId("label",71);
AttributeId ATTRIB_NUM = AttributeId("num",72);
ElementId ELEM_BASICOVERRIDE = ElementId("basicoverride",101);
ElementId ELEM_DEST = ElementId("dest",102);
ElementId ELEM_JUMPTABLE = ElementId("jumptable",103);
ElementId ELEM_LOADTABLE = ElementId("loadtable",104);
ElementId ELEM_NORMADDR = ElementId("normaddr",105);
ElementId ELEM_NORMHASH = ElementId("normhash",106);
ElementId ELEM_STARTVAL = ElementId("startval",107);
/// \param encoder is the stream encoder
void LoadTable::encode(Encoder &encoder) const
{
s << "<loadtable";
a_v_i(s,"size",size);
a_v_i(s,"num",num);
s << ">\n ";
addr.saveXml(s);
s << "</loadtable>\n";
encoder.openElement(ELEM_LOADTABLE);
encoder.writeSignedInteger(ATTRIB_SIZE, size);
encoder.writeSignedInteger(ATTRIB_NUM, num);
addr.encode(encoder);
encoder.closeElement(ELEM_LOADTABLE);
}
/// \param el is the root \<loadtable> tag
/// \param decoder is the stream decoder
/// \param glb is the architecture for resolving address space tags
void LoadTable::restoreXml(const Element *el,Architecture *glb)
void LoadTable::decode(Decoder &decoder,Architecture *glb)
{
istringstream s1(el->getAttributeValue("size"));
s1.unsetf(ios::dec | ios::hex | ios::oct);
s1 >> size;
istringstream s2(el->getAttributeValue("num"));
s2.unsetf(ios::dec | ios::hex | ios::oct);
s2 >> num;
const List &list( el->getChildren() );
List::const_iterator iter = list.begin();
addr = Address::restoreXml( *iter, glb);
uint4 elemId = decoder.openElement(ELEM_LOADTABLE);
size = decoder.readSignedInteger(ATTRIB_SIZE);
num = decoder.readSignedInteger(ATTRIB_NUM);
addr = Address::decode( decoder, glb);
decoder.closeElement(elemId);
}
/// We assume the list of LoadTable entries is sorted and perform an in-place
@@ -1899,55 +1905,61 @@ void JumpBasicOverride::clear(void)
istrivial = false;
}
void JumpBasicOverride::saveXml(ostream &s) const
void JumpBasicOverride::encode(Encoder &encoder) const
{
set<Address>::const_iterator iter;
s << "<basicoverride>\n";
encoder.openElement(ELEM_BASICOVERRIDE);
for(iter=adset.begin();iter!=adset.end();++iter) {
s << " <dest";
encoder.openElement(ELEM_DEST);
AddrSpace *spc = (*iter).getSpace();
uintb off = (*iter).getOffset();
spc->saveXmlAttributes(s,off);
s << "/>\n";
spc->encodeAttributes(encoder,off);
encoder.closeElement(ELEM_DEST);
}
if (hash != 0) {
s << " <normaddr";
normaddress.getSpace()->saveXmlAttributes(s,normaddress.getOffset());
s << "/>\n";
s << " <normhash>0x" << hex << hash << "</normhash>\n";
encoder.openElement(ELEM_NORMADDR);
normaddress.getSpace()->encodeAttributes(encoder,normaddress.getOffset());
encoder.closeElement(ELEM_NORMADDR);
encoder.openElement(ELEM_NORMHASH);
encoder.writeUnsignedInteger(ATTRIB_CONTENT, hash);
encoder.closeElement(ELEM_NORMHASH);
}
if (startingvalue != 0) {
s << " <startval>0x" << hex << startingvalue << "</startval>\n";
encoder.openElement(ELEM_STARTVAL);
encoder.writeUnsignedInteger(ATTRIB_CONTENT, startingvalue);
encoder.closeElement(ELEM_STARTVAL);
}
s << "</basicoverride>\n";
encoder.closeElement(ELEM_BASICOVERRIDE);
}
void JumpBasicOverride::restoreXml(const Element *el,Architecture *glb)
void JumpBasicOverride::decode(Decoder &decoder,Architecture *glb)
{
const List &list( el->getChildren() );
List::const_iterator iter = list.begin();
while(iter != list.end()) {
const Element *subel = *iter;
++iter;
if (subel->getName() == "dest") {
adset.insert( Address::restoreXml(subel,glb) );
uint4 elemId = decoder.openElement(ELEM_BASICOVERRIDE);
for(;;) {
uint4 subId = decoder.openElement();
if (subId == 0) break;
if (subId == ELEM_DEST) {
VarnodeData vData;
vData.decodeFromAttributes(decoder, glb);
adset.insert( vData.getAddr() );
}
else if (subel->getName() == "normaddr")
normaddress = Address::restoreXml(subel,glb);
else if (subel->getName() == "normhash") {
istringstream s1(subel->getContent());
s1.unsetf(ios::dec | ios::hex | ios::oct);
s1 >> hash;
else if (subId == ELEM_NORMADDR) {
VarnodeData vData;
vData.decodeFromAttributes(decoder, glb);
normaddress = vData.getAddr();
}
else if (subel->getName() == "startval") {
istringstream s2(subel->getContent());
s2.unsetf(ios::dec | ios::hex | ios::oct);
s2 >> startingvalue;
else if (subId == ELEM_NORMHASH) {
hash = decoder.readUnsignedInteger(ATTRIB_CONTENT);
}
else if (subId == ELEM_STARTVAL) {
startingvalue = decoder.readUnsignedInteger(ATTRIB_CONTENT);
}
decoder.closeElement(subId);
}
decoder.closeElement(elemId);
if (adset.empty())
throw LowlevelError("Empty jumptable override");
}
@@ -2594,83 +2606,81 @@ void JumpTable::clear(void)
// -opaddress- -maxtablesize- -maxaddsub- -maxleftright- -maxext- -collectloads- are permanent
}
/// The recovered addresses and case labels are saved to the XML stream.
/// If override information is present, this is also incorporated into the tag.
/// \param s is the stream to write to
void JumpTable::saveXml(ostream &s) const
/// The recovered addresses and case labels are encode to the stream.
/// If override information is present, this is also incorporated into the element.
/// \param encoder is the stream encoder
void JumpTable::encode(Encoder &encoder) const
{
if (!isRecovered())
throw LowlevelError("Trying to save unrecovered jumptable");
s << "<jumptable>\n";
opaddress.saveXml(s);
s << '\n';
encoder.openElement(ELEM_JUMPTABLE);
opaddress.encode(encoder);
for(int4 i=0;i<addresstable.size();++i) {
s << "<dest";
encoder.openElement(ELEM_DEST);
AddrSpace *spc = addresstable[i].getSpace();
uintb off = addresstable[i].getOffset();
if (spc != (AddrSpace *)0)
spc->saveXmlAttributes(s,off);
spc->encodeAttributes(encoder,off);
if (i<label.size()) {
if (label[i] != 0xBAD1ABE1)
a_v_u(s,"label",label[i]);
encoder.writeUnsignedInteger(ATTRIB_LABEL, label[i]);
}
s << "/>\n";
encoder.closeElement(ELEM_DEST);
}
if (!loadpoints.empty()) {
for(int4 i=0;i<loadpoints.size();++i)
loadpoints[i].saveXml(s);
loadpoints[i].encode(encoder);
}
if ((jmodel != (JumpModel *)0)&&(jmodel->isOverride()))
jmodel->saveXml(s);
s << "</jumptable>\n";
jmodel->encode(encoder);
encoder.closeElement(ELEM_JUMPTABLE);
}
/// Restore the addresses, \e case labels, and any override information from the tag.
/// Parse addresses, \e case labels, and any override information from a \<jumptable> element.
/// Other parts of the model and jump-table will still need to be recovered.
/// \param el is the root \<jumptable> tag to restore from
void JumpTable::restoreXml(const Element *el)
/// \param decoder is the stream decoder
void JumpTable::decode(Decoder &decoder)
{
const List &list( el->getChildren() );
List::const_iterator iter = list.begin();
opaddress = Address::restoreXml( *iter, glb);
uint4 elemId = decoder.openElement(ELEM_JUMPTABLE);
opaddress = Address::decode( decoder, glb);
bool missedlabel = false;
++iter;
while(iter != list.end()) {
const Element *subel = *iter;
if (subel->getName() == "dest") {
addresstable.push_back( Address::restoreXml( subel, glb) );
int4 maxnum = subel->getNumAttributes();
int4 i;
for(i=0;i<maxnum;++i) {
if (subel->getAttributeName(i) == "label") break;
for(;;) {
uint4 subId = decoder.peekElement();
if (subId == 0) break;
if (subId == ELEM_DEST) {
decoder.openElement();
bool foundlabel = false;
for(;;) {
uint4 attribId = decoder.getNextAttributeId();
if (attribId == 0) break;
if (attribId == ATTRIB_LABEL) {
if (missedlabel)
throw LowlevelError("Jumptable entries are missing labels");
uintb lab = decoder.readUnsignedInteger();
label.push_back(lab);
foundlabel = true;
break;
}
}
if (i<maxnum) { // Found a label attribute
if (missedlabel)
throw LowlevelError("Jumptable entries are missing labels");
istringstream s1(subel->getAttributeValue(i));
s1.unsetf(ios::dec | ios::hex | ios::oct);
uintb lab;
s1 >> lab;
label.push_back(lab);
}
else // No label attribute
if (!foundlabel) // No label attribute
missedlabel = true; // No following entries are allowed to have a label attribute
addresstable.push_back( Address::decode( decoder, glb) );
}
else if (subel->getName() == "loadtable") {
else if (subId == ELEM_LOADTABLE) {
loadpoints.emplace_back();
loadpoints.back().restoreXml(subel,glb);
loadpoints.back().decode(decoder,glb);
}
else if (subel->getName() == "basicoverride") {
else if (subId == ELEM_BASICOVERRIDE) {
if (jmodel != (JumpModel *)0)
throw LowlevelError("Duplicate jumptable override specs");
jmodel = new JumpBasicOverride(this);
jmodel->restoreXml(subel,glb);
jmodel->decode(decoder,glb);
}
++iter;
}
decoder.closeElement(elemId);
if (label.size()!=0) {
while(label.size() < addresstable.size())
@@ -24,6 +24,17 @@
class EmulateFunction;
extern AttributeId ATTRIB_LABEL; ///< Marshaling attribute "label"
extern AttributeId ATTRIB_NUM; ///< Marshaling attribute "num"
extern ElementId ELEM_BASICOVERRIDE; ///< Marshaling element \<basicoverride>
extern ElementId ELEM_DEST; ///< Marshaling element \<dest>
extern ElementId ELEM_JUMPTABLE; ///< Marshaling element \<jumptable>
extern ElementId ELEM_LOADTABLE; ///< Marshaling element \<loadtable>
extern ElementId ELEM_NORMADDR; ///< Marshaling element \<normaddr>
extern ElementId ELEM_NORMHASH; ///< Marshaling element \<normhash>
extern ElementId ELEM_STARTVAL; ///< Marshaling element \<startval>
/// \brief Exception thrown for a thunk mechanism that looks like a jump-table
struct JumptableThunkError : public LowlevelError {
JumptableThunkError(const string &s) : LowlevelError(s) {} ///< Construct with an explanatory string
@@ -44,12 +55,12 @@ class LoadTable {
int4 size; ///< Size of table entry
int4 num; ///< Number of entries in table;
public:
LoadTable(void) {} // Constructor for use with restoreXml
LoadTable(void) {} // Constructor for use with decode
LoadTable(const Address &ad,int4 sz) { addr = ad, size = sz; num = 1; } ///< Constructor for a single entry table
LoadTable(const Address &ad,int4 sz,int4 nm) { addr = ad; size = sz; num = nm; } ///< Construct a full table
bool operator<(const LoadTable &op2) const { return (addr < op2.addr); } ///< Compare \b this with another table by address
void saveXml(ostream &s) const; ///< Save a description of \b this as an \<loadtable> XML tag
void restoreXml(const Element *el,Architecture *glb); ///< Read in \b this table from a \<loadtable> XML description
void encode(Encoder &encoder) const; ///< Encode a description of \b this as an \<loadtable> element
void decode(Decoder &decoder,Architecture *glb); ///< Decode \b this table from a \<loadtable> element
static void collapseTable(vector<LoadTable> &table); ///< Collapse a sequence of table descriptions
};
@@ -308,9 +319,9 @@ public:
virtual bool sanityCheck(Funcdata *fd,PcodeOp *indop,vector<Address> &addresstable)=0;
virtual JumpModel *clone(JumpTable *jt) const=0; ///< Clone \b this model
virtual void clear(void) {} ///< Clear any non-permanent aspects of the model
virtual void saveXml(ostream &s) const {} ///< Save this model as an XML tag
virtual void restoreXml(const Element *el,Architecture *glb) {} ///< Restore \b this model from an XML tag
virtual void clear(void) {} ///< Clear any non-permanent aspects of the model
virtual void encode(Encoder &encoder) const {} ///< Encode this model to a stream
virtual void decode(Decoder &decoder,Architecture *glb) {} ///< Decode \b this model from a stream
};
/// \brief A trivial jump-table model, where the BRANCHIND input Varnode is the switch variable
@@ -451,8 +462,8 @@ public:
virtual bool sanityCheck(Funcdata *fd,PcodeOp *indop,vector<Address> &addresstable) { return true; }
virtual JumpModel *clone(JumpTable *jt) const;
virtual void clear(void);
virtual void saveXml(ostream &s) const;
virtual void restoreXml(const Element *el,Architecture *glb);
virtual void encode(Encoder &encoder) const;
virtual void decode(Decoder &decoder,Architecture *glb);
};
class JumpAssistOp;
@@ -563,9 +574,9 @@ public:
bool recoverLabels(Funcdata *fd); ///< Recover the case labels for \b this jump-table
bool checkForMultistage(Funcdata *fd); ///< Check if this jump-table requires an additional recovery stage
void clear(void); ///< Clear instance specific data for \b this jump-table
void saveXml(ostream &s) const; ///< Save \b this jump-table as a \<jumptable> XML tag
void restoreXml(const Element *el); ///< Recover \b this jump-table from a \<jumptable> XML tag
};
void encode(Encoder &encoder) const; ///< Encode \b this jump-table as a \<jumptable> element
void decode(Decoder &decoder); ///< Decode \b this jump-table from a \<jumptable> element
};
/// \param op2 is the other IndexPair to compare with \b this
/// \return \b true if \b this is ordered before the other IndexPair
@@ -18,6 +18,8 @@
void startDecompilerLibrary(const char *sleighhome)
{
AttributeId::initialize();
ElementId::initialize();
CapabilityPoint::initializeAll();
ArchitectureCapability::sortCapabilities();
@@ -28,6 +30,8 @@ void startDecompilerLibrary(const char *sleighhome)
void startDecompilerLibrary(const vector<string> &extrapaths)
{
AttributeId::initialize();
ElementId::initialize();
CapabilityPoint::initializeAll();
ArchitectureCapability::sortCapabilities();
@@ -38,6 +42,8 @@ void startDecompilerLibrary(const vector<string> &extrapaths)
void startDecompilerLibrary(const char *sleighhome,const vector<string> &extrapaths)
{
AttributeId::initialize();
ElementId::initialize();
CapabilityPoint::initializeAll();
ArchitectureCapability::sortCapabilities();
@@ -16,6 +16,11 @@
#include "loadimage_xml.hh"
#include "translate.hh"
AttributeId ATTRIB_ARCH = AttributeId("arch",73);
ElementId ELEM_BINARYIMAGE = ElementId("binaryimage",108);
ElementId ELEM_BYTECHUNK = ElementId("bytechunk",109);
/// \param f is the (path to the) underlying XML file
/// \param el is the parsed form of the file
LoadImageXml::LoadImageXml(const string &f,const Element *el) : LoadImage(f)
@@ -30,37 +35,42 @@ LoadImageXml::LoadImageXml(const string &f,const Element *el) : LoadImage(f)
archtype = el->getAttributeValue("arch");
}
/// Write out the byte chunks and symbols as XML tags
/// \param s is the output stream
void LoadImageXml::saveXml(ostream &s) const
/// Encode the byte chunks and symbols as elements
/// \param encoder is the stream encoder
void LoadImageXml::encode(Encoder &encoder) const
{
s << "<binaryimage arch=\"" << archtype << "\">\n";
encoder.openElement(ELEM_BINARYIMAGE);
encoder.writeString(ATTRIB_ARCH, archtype);
map<Address,vector<uint1> >::const_iterator iter1;
for(iter1=chunk.begin();iter1!=chunk.end();++iter1) {
const vector<uint1> &vec((*iter1).second);
if (vec.size() == 0) continue;
s << " <bytechunk";
(*iter1).first.getSpace()->saveXmlAttributes(s,(*iter1).first.getOffset());
encoder.openElement(ELEM_BYTECHUNK);
(*iter1).first.getSpace()->encodeAttributes(encoder,(*iter1).first.getOffset());
if (readonlyset.find((*iter1).first) != readonlyset.end())
s << " readonly=\"true\"";
s << ">\n " << setfill('0');
encoder.writeBool(ATTRIB_READONLY, "true");
ostringstream s;
s << '\n' << setfill('0');
for(int4 i=0;i<vec.size();++i) {
s << hex << setw(2) << (int4)vec[i];
if (i%20 == 19)
s << "\n ";
s << '\n';
}
s << "\n </bytechunk>\n";
s << '\n';
encoder.writeString(ATTRIB_CONTENT, s.str());
encoder.closeElement(ELEM_BYTECHUNK);
}
map<Address,string>::const_iterator iter2;
for(iter2=addrtosymbol.begin();iter2!=addrtosymbol.end();++iter2) {
s << " <symbol";
(*iter2).first.getSpace()->saveXmlAttributes(s,(*iter2).first.getOffset());
s << " name=\"" << (*iter2).second << "\"/>\n";
encoder.openElement(ELEM_SYMBOL);
(*iter2).first.getSpace()->encodeAttributes(encoder,(*iter2).first.getOffset());
encoder.writeString(ATTRIB_NAME, (*iter2).second);
encoder.closeElement(ELEM_SYMBOL);
}
s << "</binaryimage>\n";
encoder.closeElement(ELEM_BINARYIMAGE);
}
/// \param m is for looking up address space
@@ -71,35 +81,40 @@ void LoadImageXml::open(const AddrSpaceManager *m)
uint4 sz; // unused size
// Read parsed xml file
const List &list(rootel->getChildren());
List::const_iterator iter;
iter = list.begin();
while(iter != list.end()) {
Element *subel = *iter++;
if (subel->getName()=="symbol") {
XmlDecode decoder(rootel);
uint4 elemId = decoder.openElement(ELEM_BINARYIMAGE);
for(;;) {
uint4 subId = decoder.openElement();
if (subId == 0) break;
if (subId==ELEM_SYMBOL) {
AddrSpace *base = (AddrSpace *)0;
base = manage->getSpaceByName(subel->getAttributeValue("space"));
string spaceName = decoder.readString(ATTRIB_SPACE);
base = manage->getSpaceByName(spaceName);
if (base == (AddrSpace *)0)
throw LowlevelError("Unknown space name: "+subel->getAttributeValue("space"));
Address addr(base,base->restoreXmlAttributes(subel,sz));
const string &nm(subel->getAttributeValue("name"));
throw LowlevelError("Unknown space name: "+spaceName);
Address addr(base,base->decodeAttributes(decoder,sz));
string nm = decoder.readString(ATTRIB_NAME);
addrtosymbol[addr] = nm;
}
else if (subel->getName() == "bytechunk") {
else if (subId == ELEM_BYTECHUNK) {
AddrSpace *base = (AddrSpace *)0;
base = manage->getSpaceByName(subel->getAttributeValue("space"));
string spaceName = decoder.readString(ATTRIB_SPACE);
base = manage->getSpaceByName(spaceName);
if (base == (AddrSpace *)0)
throw LowlevelError("Unknown space name: "+subel->getAttributeValue("space"));
Address addr(base,base->restoreXmlAttributes(subel,sz));
throw LowlevelError("Unknown space name: "+spaceName);
Address addr(base,base->decodeAttributes(decoder,sz));
map<Address,vector<uint1> >::iterator chnkiter;
vector<uint1> &vec( chunk[addr] );
vec.clear();
for(int4 i=0;i<subel->getNumAttributes();++i) {
if (subel->getAttributeName(i) == "readonly")
if (xml_readbool(subel->getAttributeValue(i)))
decoder.rewindAttributes();
for(;;) {
uint4 attribId = decoder.getNextAttributeId();
if (attribId == 0) break;
if (attribId == ATTRIB_READONLY)
if (decoder.readBool())
readonlyset.insert(addr);
}
istringstream is(subel->getContent());
istringstream is(decoder.readString(ATTRIB_CONTENT));
int4 val;
char c1,c2;
is >> ws;
@@ -126,8 +141,10 @@ void LoadImageXml::open(const AddrSpaceManager *m)
}
}
else
throw LowlevelError("Unknown LoadImageXml tag: "+subel->getName());
throw LowlevelError("Unknown LoadImageXml tag");
decoder.closeElement(subId);
}
decoder.closeElement(elemId);
pad();
}
@@ -21,6 +21,11 @@
#include "loadimage.hh"
extern AttributeId ATTRIB_ARCH; ///< Marshaling attribute "arch"
extern ElementId ELEM_BINARYIMAGE; ///< Marshaling element \<binaryimage>
extern ElementId ELEM_BYTECHUNK; ///< Marshaling element \<bytechunk>
/// \brief Implementation of the LoadImage interface using underlying data stored in an XML format
///
/// The image data is stored in an XML file in a \<binaryimage> file.
@@ -38,7 +43,7 @@ public:
LoadImageXml(const string &f,const Element *el); ///< Constructor
void open(const AddrSpaceManager *m); ///< Read XML tags into the containers
void clear(void); ///< Clear out all the caches
void saveXml(ostream &s) const; ///< Save the image back out to an XML stream
void encode(Encoder &encoder) const; ///< Encode the image to a stream
virtual ~LoadImageXml(void) { clear(); }
virtual void loadFill(uint1 *ptr,int4 size,const Address &addr);
virtual void openSymbols(void) const;

Some files were not shown because too many files have changed in this diff Show More