Ghidra is a software reverse engineering (SRE) framework
GP-1932 Decompiler support for address space attribute on pointers
caheckman7078885aeae9
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/architecture.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/architecture.ccindex c8bfcb4c25..a583368dce 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/architecture.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/architecture.cc@@ -831,9 +831,9 @@ void Architecture::parseGlobal(const Element *el)// We need to duplicate the range being marked as global into the overlay space(s)int4 num = numSpaces();for(int4 i=0;i<num;++i) {- OverlaySpace *ospc = (OverlaySpace *)getSpace(i);+ AddrSpace *ospc = getSpace(i);if (ospc == (AddrSpace *)0 || !ospc->isOverlay()) continue;- if (ospc->getBaseSpace() != range.getSpace()) continue;+ if (ospc->getContain() != range.getSpace()) continue;symboltab->addRange(scope,ospc,range.getFirst(),range.getLast());}}@@ -852,7 +852,7 @@ void Architecture::addOtherSpace(void)for(int4 i=0;i<num;++i){AddrSpace *ospc = getSpace(i);if (!ospc->isOverlay()) continue;- if (((OverlaySpace *)ospc)->getBaseSpace() != otherSpace) continue;+ if (ospc->getContain() != otherSpace) continue;symboltab->addRange(scope,ospc,0,otherSpace->getHighest());}}diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/cast.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/cast.ccindex daf4bdf883..86429db85b 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/cast.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/cast.cc@@ -226,8 +226,17 @@ Datatype *CastStrategyC::castStandard(Datatype *reqtype,Datatype *curtype,Datatype *curbase = curtype;bool isptr = false;while((reqbase->getMetatype()==TYPE_PTR)&&(curbase->getMetatype()==TYPE_PTR)) {- reqbase = ((const TypePointer *)reqbase)->getPtrTo();- curbase = ((const TypePointer *)curbase)->getPtrTo();+ const TypePointer *reqptr = (const TypePointer *)reqbase;+ const TypePointer *curptr = (const TypePointer *)curbase;+ if (reqptr->getWordSize() != curptr->getWordSize())+ return reqtype;+ if (reqptr->getSpace() != curptr->getSpace()) {+ if (reqptr->getSpace() != (AddrSpace *)0 && curptr->getSpace() != (AddrSpace *)0)+ return reqtype; // Pointers to different address spaces. We must cast+ // If one pointer doesn't have an address, assume a conversion to/from sub-type and don't need a cast+ }+ reqbase = reqptr->getPtrTo();+ curbase = curptr->getPtrTo();care_uint_int = true;isptr = true;}diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.ccindex 2c2ef1c936..b1a3c0304f 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc@@ -935,17 +935,25 @@ int4 ActionShadowVar::apply(Funcdata &data)return 0;}-/// \brief Make a limited search from a constant for a LOAD or STORE so we can see the AddrSpace being accessed+/// \brief Search for address space annotations in the path of a pointer constant.///-/// We traverse forward through the op reading the constant, through INT_ADD, INDIRECT, COPY, and MULTIEQUAL-/// until we hit a LOAD or STORE.+/// From a constant, search forward in its data-flow either for a LOAD or STORE operation where we can+/// see the address space being accessed, or search for a pointer data-type with an address space attribute.+/// We make a limited traversal through the op reading the constant, through INT_ADD, INDIRECT, COPY,+/// and MULTIEQUAL until we hit a LOAD or STORE./// \param vn is the constant we are searching from/// \param op is the PcodeOp reading the constant/// \return the discovered AddrSpace or null-AddrSpace *ActionConstantPtr::searchForLoadStore(Varnode *vn,PcodeOp *op)+AddrSpace *ActionConstantPtr::searchForSpaceAttribute(Varnode *vn,PcodeOp *op){for(int4 i=0;i<3;++i) {+ Datatype *dt = vn->getType();+ if (dt->getMetatype() == TYPE_PTR) {+ AddrSpace *spc = ((TypePointer*)dt)->getSpace();+ if (spc != (AddrSpace*)0 && spc->getAddrSize() == vn->getSize()) // If provided a pointer with space attribute+ return spc; // use that+ }switch(op->code()) {case CPUI_INT_ADD:case CPUI_COPY:@@ -988,6 +996,11 @@ AddrSpace *ActionConstantPtr::selectInferSpace(Varnode *vn,PcodeOp *op,const vec{AddrSpace *resSpace = (AddrSpace *)0;+ if (vn->getType()->getMetatype() == TYPE_PTR) {+ AddrSpace *spc = ((TypePointer *)vn->getType())->getSpace();+ if (spc != (AddrSpace *)0 && spc->getAddrSize() == vn->getSize())+ return spc;+ }for(int4 i=0;i<spaceList.size();++i) {AddrSpace *spc = spaceList[i];int4 minSize = spc->getMinimumPtrSize();@@ -998,7 +1011,7 @@ AddrSpace *ActionConstantPtr::selectInferSpace(Varnode *vn,PcodeOp *op,const vecelse if (vn->getSize() < minSize)continue;if (resSpace != (AddrSpace *)0) {- AddrSpace *searchSpc = searchForLoadStore(vn,op);+ AddrSpace *searchSpc = searchForSpaceAttribute(vn,op);if (searchSpc != (AddrSpace *)0)resSpace = searchSpc;break;@@ -2167,6 +2180,42 @@ int4 ActionDefaultParams::apply(Funcdata &data)return 0; // Indicate success}+/// \brief Check if the data-type of the given value being used as a pointer makes sense+///+/// If the data-type is a pointer make sure:+/// - The pointed-to size matches the size of the value being loaded are stored+/// - Any address space attached to the pointer matches the address space of the LOAD/STORE+///+/// If any of the conditions are violated, a warning is added to the output.+/// \param op is the LOAD/STORE acting on a pointer+/// \param vn is the given value being used as a pointer+/// \param data is the function containing the PcodeOp+void ActionSetCasts::checkPointerIssues(PcodeOp *op,Varnode *vn,Funcdata &data)++{+ Datatype *ptrtype = op->getIn(1)->getHighTypeReadFacing(op);+ int4 valsize = vn->getSize();+ if ((ptrtype->getMetatype()!=TYPE_PTR)|| (((TypePointer *)ptrtype)->getPtrTo()->getSize() != valsize)) {+ string name = op->getOpcode()->getName();+ name[0] = toupper( name[0] );+ data.warning(name + " size is inaccurate",op->getAddr());+ }+ if (ptrtype->getMetatype()==TYPE_PTR) {+ AddrSpace *spc = ((TypePointer *)ptrtype)->getSpace();+ if (spc != (AddrSpace *)0) {+ AddrSpace *opSpc = Address::getSpaceFromConst(op->getIn(0)->getAddr());+ if (opSpc != spc && spc->getContain() != opSpc) {+ string name = op->getOpcode()->getName();+ name[0] = toupper( name[0] );+ ostringstream s;+ s << name << " refers to '" << opSpc->getName() << "' but pointer attribute is '";+ s << spc->getName() << '\'';+ data.warning(s.str(),op->getAddr());+ }+ }+ }+}+/// \brief Test if the given cast conflict can be resolved by passing to the first structure field////// Test if the given Varnode data-type is a pointer to a structure and if interpreting@@ -2512,18 +2561,10 @@ int4 ActionSetCasts::apply(Funcdata &data)count += castInput(op,i,data,castStrategy);}if (opc == CPUI_LOAD) {- TypePointer *ptrtype = (TypePointer *)op->getIn(1)->getHighTypeReadFacing(op);- int4 valsize = op->getOut()->getSize();- if ((ptrtype->getMetatype()!=TYPE_PTR)||- (ptrtype->getPtrTo()->getSize() != valsize))- data.warning("Load size is inaccurate",op->getAddr());+ checkPointerIssues(op, op->getOut(), data);}else if (opc == CPUI_STORE) {- TypePointer *ptrtype = (TypePointer *)op->getIn(1)->getHighTypeReadFacing(op);- int4 valsize = op->getIn(2)->getSize();- if ((ptrtype->getMetatype()!=TYPE_PTR)||- (ptrtype->getPtrTo()->getSize() != valsize))- data.warning("Store size is inaccurate",op->getAddr());+ checkPointerIssues(op, op->getIn(2), data);}Varnode *vn = op->getOut();if (vn == (Varnode *)0) continue;diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hhindex 047992912f..25568f6c62 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hh@@ -180,7 +180,7 @@ public:/// \brief Check for constants, with pointer type, that correspond to global symbolsclass ActionConstantPtr : public Action {int4 localcount; ///< Number of passes made for this function- static AddrSpace *searchForLoadStore(Varnode *vn,PcodeOp *op);+ static AddrSpace *searchForSpaceAttribute(Varnode *vn,PcodeOp *op);static AddrSpace *selectInferSpace(Varnode *vn,PcodeOp *op,const vector<AddrSpace *> &spaceList);static SymbolEntry *isPointer(AddrSpace *spc,Varnode *vn,PcodeOp *op,int4 slot,Address &rampoint,uintb &fullEncoding,Funcdata &data);@@ -310,6 +310,7 @@ public:/// input. In this case, it casts to the necessary pointer type/// immediately.class ActionSetCasts : public Action {+ static void checkPointerIssues(PcodeOp *op,Varnode *vn,Funcdata &data);static bool testStructOffset0(Varnode *vn,PcodeOp *op,Datatype *ct,CastStrategy *castStrategy);static bool tryResolutionAdjustment(PcodeOp *op,int4 slot,Funcdata &data);static bool isOpIdentical(Datatype *ct1,Datatype *ct2);diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.ccindex 68553330a9..7a264d0b9e 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.cc@@ -2893,8 +2893,10 @@ void IfcReadonly::execute(istream &s)/// \class IfcPointerSetting/// \brief Create a pointer with additional settings: `pointer setting <name> <basetype> offset <val>`///-/// The new data-type is named and must be pointer. It must have a setting-/// - \b offset which creates a shifted pointer+/// Alternately: `pointer setting <name> <basetype> space <spacename>`+/// The new data-type is named and must be pointer.+/// An \e offset setting creates a relative pointer and attaches the provided offset value.+/// A \e space setting create a pointer with the provided address space as an attribute.void IfcPointerSetting::execute(istream &s){@@ -2927,6 +2929,19 @@ void IfcPointerSetting::execute(istream &s)AddrSpace *spc = dcp->conf->getDefaultDataSpace();dcp->conf->types->getTypePointerRel(spc->getAddrSize(), bt, ptrto, spc->getWordSize(), off,typeName);}+ else if (setting == "space") {+ string spaceName;+ s >> spaceName;+ if (spaceName.length() == 0)+ throw IfaceParseError("Missing name of address space");+ Datatype *ptrTo = dcp->conf->types->findByName(baseType);+ if (ptrTo == (Datatype *)0)+ throw IfaceParseError("Unknown base data-type: "+baseType);+ AddrSpace *spc = dcp->conf->getSpaceByName(spaceName);+ if (spc == (AddrSpace *)0)+ throw IfaceParseError("Unknown space: "+spaceName);+ dcp->conf->types->getTypePointerWithSpace(ptrTo,spc,typeName);+ }elsethrow IfaceParseError("Unknown pointer setting: "+setting);*status->optr << "Successfully created pointer: " << typeName << endl;diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/space.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/space.ccindex 7e70656a39..dc0ec5c4ac 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/space.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/space.cc@@ -659,13 +659,6 @@ OverlaySpace::OverlaySpace(AddrSpaceManager *m,const Translate *t)setFlags(overlay);}-/// \return the base address space-AddrSpace *OverlaySpace::getBaseSpace(void) const--{- return baseSpace;-}-void OverlaySpace::saveXml(ostream &s) const{diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/space.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/space.hhindex 86e1931139..2188f8c31d 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/space.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/space.hh@@ -244,7 +244,7 @@ class OverlaySpace : public AddrSpace {AddrSpace *baseSpace; ///< Space being overlayedpublic:OverlaySpace(AddrSpaceManager *m,const Translate *t); ///< Constructor- AddrSpace *getBaseSpace(void) const; ///< Get the address space being overlayed+ virtual AddrSpace *getContain(void) const { return baseSpace; }virtual void saveXml(ostream &s) const;virtual void restoreXml(const Element *el);};diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/translate.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/translate.ccindex d5ecdf9469..ceeda78f9b 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/translate.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/translate.cc@@ -345,8 +345,7 @@ void AddrSpaceManager::insertSpace(AddrSpace *spc)// fallthrucase IPTR_PROCESSOR:if (spc->isOverlay()) { // If this is a new overlay space- OverlaySpace *ospc = (OverlaySpace *)spc;- ospc->getBaseSpace()->setFlags(AddrSpace::overlaybase); // Mark the base as being overlayed+ spc->getContain()->setFlags(AddrSpace::overlaybase); // Mark the base as being overlayed}else if (spc->isOtherSpace()) {if (spc->index != OtherSpace::INDEX)diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/type.ccindex acb8290aad..a84da67820 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc@@ -619,6 +619,9 @@ void TypePointer::printRaw(ostream &s) const{ptrto->printRaw(s);s << " *";+ if (spaceid != (AddrSpace *)0) {+ s << '(' << spaceid->getName() << ')';+ }}int4 TypePointer::compare(const Datatype &op,int4 level) const@@ -629,6 +632,11 @@ int4 TypePointer::compare(const Datatype &op,int4 level) const// Both must be pointersTypePointer *tp = (TypePointer *) &op;if (wordsize != tp->wordsize) return (wordsize < tp->wordsize) ? -1 : 1;+ if (spaceid != tp->spaceid) {+ if (spaceid == (AddrSpace *)0) return 1; // Pointers with address space come earlier+ if (tp->spaceid == (AddrSpace *)0) return -1;+ return (spaceid->getIndex() < tp->spaceid->getIndex()) ? -1 : 1;+ }level -= 1;if (level < 0) {if (id == op.getId()) return 0;@@ -644,6 +652,11 @@ int4 TypePointer::compareDependency(const Datatype &op) constTypePointer *tp = (TypePointer *) &op; // Both must be pointersif (ptrto != tp->ptrto) return (ptrto < tp->ptrto) ? -1 : 1; // Compare absolute pointersif (wordsize != tp->wordsize) return (wordsize < tp->wordsize) ? -1 : 1;+ if (spaceid != tp->spaceid) {+ if (spaceid == (AddrSpace *)0) return 1; // Pointers with address space come earlier+ if (tp->spaceid == (AddrSpace *)0) return -1;+ return (spaceid->getIndex() < tp->spaceid->getIndex()) ? -1 : 1;+ }return (op.getSize()-size);}@@ -658,6 +671,8 @@ void TypePointer::saveXml(ostream &s) constsaveXmlBasic(metatype,s);if (wordsize != 1)a_v_i(s,"wordsize",wordsize);+ if (spaceid != (AddrSpace *)0)+ a_v(s,"space",spaceid->getName());s << '>';ptrto->saveXmlRef(s);s << "</type>";@@ -670,12 +685,17 @@ void TypePointer::restoreXml(const Element *el,TypeFactory &typegrp){restoreXmlBasic(el);- for(int4 i=0;i<el->getNumAttributes();++i)- if (el->getAttributeName(i) == "wordsize") {+ for(int4 i=0;i<el->getNumAttributes();++i) {+ const string &attrName(el->getAttributeName(i));+ if (attrName == "wordsize") {istringstream s(el->getAttributeValue(i));s.unsetf(ios::dec | ios::hex | ios::oct);s >> wordsize;}+ else if (attrName == "space") {+ spaceid = typegrp.getArch()->getSpaceByName(el->getAttributeValue(i));+ }+ }ptrto = typegrp.restoreXmlType( *el->getChildren().begin() );calcSubmeta();if (name.size() == 0) // Inherit only if no name@@ -1740,12 +1760,17 @@ void TypePointerRel::restoreXml(const Element *el,TypeFactory &typegrp)flags |= is_ptrrel;restoreXmlBasic(el);metatype = TYPE_PTR; // Don't use TYPE_PTRREL internally- for(int4 i=0;i<el->getNumAttributes();++i)- if (el->getAttributeName(i) == "wordsize") {+ for(int4 i=0;i<el->getNumAttributes();++i) {+ const string &attribName(el->getAttributeName(i));+ if (attribName == "wordsize") {istringstream s(el->getAttributeValue(i));s.unsetf(ios::dec | ios::hex | ios::oct);s >> wordsize;}+ else if (attribName == "space") {+ spaceid = typegrp.getArch()->getSpaceByName(el->getAttributeValue("space"));+ }+ }const List &list(el->getChildren());List::const_iterator iter;iter = list.begin();@@ -3104,6 +3129,13 @@ TypePointerRel *TypeFactory::getTypePointerRel(TypePointer *parentPtr,Datatype *////// The resulting data-type is named and not ephemeral and will display as a formal data-type/// in decompiler output.+/// \param sz is the size in bytes of the pointer+/// \param parent is data-type of the parent container being indirectly pointed to+/// \param ptrTo is the data-type being pointed directly to+/// \param ws is the addressable unit size of pointed to data+/// \param off is the offset of the pointed-to data-type relative to the \e container+/// \param nm is the name to associate with the pointer+/// \return the new/matching pointerTypePointerRel *TypeFactory::getTypePointerRel(int4 sz,Datatype *parent,Datatype *ptrTo,int4 ws,int4 off,const string &nm){@@ -3114,6 +3146,24 @@ TypePointerRel *TypeFactory::getTypePointerRel(int4 sz,Datatype *parent,Datatypereturn res;}+/// \brief Build a named pointer with an address space attribute+///+/// The new data-type acts like a typedef of a normal pointer but can affect the resolution of+/// constants by the type propagation system.+/// \param ptrTo is the data-type being pointed directly to+/// \param spc is the address space to associate with the pointer+/// \param nm is the name to associate with the pointer+/// \return the new/matching pointer+TypePointer *TypeFactory::getTypePointerWithSpace(Datatype *ptrTo,AddrSpace *spc,const string &nm)++{+ TypePointer tp(ptrTo,spc);+ tp.name = nm;+ tp.id = Datatype::hashName(nm);+ TypePointer *res = (TypePointer *)findAdd(tp);+ return res;+}+/// The indicated Datatype object is removed from this container./// Indirect references (via TypeArray TypeStruct etc.) are not affected/// \param ct is the data-type to destroydiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/type.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/type.hhindex 187bd3a3a3..998c38743b 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/type.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/type.hh@@ -281,25 +281,30 @@ class TypePointer : public Datatype {protected:friend class TypeFactory;Datatype *ptrto; ///< Type being pointed to+ AddrSpace *spaceid; ///< If non-null, the address space \b this is intented to point intouint4 wordsize; ///< What size unit does the pointer addressvoid restoreXml(const Element *el,TypeFactory &typegrp); ///< Restore \b this pointer data-type from an XML elementvoid calcSubmeta(void); ///< Calculate specific submeta for \b this pointer/// Internal constructor for use with restoreXml- TypePointer(void) : Datatype(0,TYPE_PTR) { ptrto = (Datatype *)0; wordsize=1; }+ TypePointer(void) : Datatype(0,TYPE_PTR) { ptrto = (Datatype *)0; wordsize=1; spaceid=(AddrSpace *)0; }public:/// Construct from another TypePointer- TypePointer(const TypePointer &op) : Datatype(op) { ptrto = op.ptrto; wordsize=op.wordsize; }+ TypePointer(const TypePointer &op) : Datatype(op) { ptrto = op.ptrto; wordsize=op.wordsize; spaceid=op.spaceid; }/// Construct from a size, pointed-to type, and wordsizeTypePointer(int4 s,Datatype *pt,uint4 ws) : Datatype(s,TYPE_PTR) {- ptrto = pt; flags = ptrto->getInheritable(); wordsize=ws; calcSubmeta(); }+ ptrto = pt; flags = ptrto->getInheritable(); wordsize=ws; spaceid=(AddrSpace *)0; calcSubmeta(); }+ /// Construct from a pointed-to type and an address space attribute+ TypePointer(Datatype *pt,AddrSpace *spc) : Datatype(spc->getAddrSize(), TYPE_PTR) {+ ptrto = pt; flags = ptrto->getInheritable(); spaceid=spc; wordsize=spc->getWordSize(); calcSubmeta(); }Datatype *getPtrTo(void) const { return ptrto; } ///< Get the pointed-to Datatype- uint4 getWordSize(void) const { return wordsize; } ///< Get the wordsize of the pointer+ uint4 getWordSize(void) const { return wordsize; } ///< Get the size of the addressable unit being pointed to+ AddrSpace *getSpace(void) const { return spaceid; } ///< Get any address space associated with \b this pointervirtual void printRaw(ostream &s) const;virtual int4 numDepend(void) const { return 1; }virtual Datatype *getDepend(int4 index) const { return ptrto; }virtual void printNameBase(ostream &s) const { s << 'p'; ptrto->printNameBase(s); }- virtual int4 compare(const Datatype &op,int4 level) const; // For tree structure- virtual int4 compareDependency(const Datatype &op) const; // For tree structure+ virtual int4 compare(const Datatype &op,int4 level) const;+ virtual int4 compareDependency(const Datatype &op) const;virtual Datatype *clone(void) const { return new TypePointer(*this); }virtual void saveXml(ostream &s) const;virtual TypePointer *downChain(uintb &off,TypePointer *&par,uintb &parOff,bool allowArrayWrap,TypeFactory &typegrp);@@ -597,6 +602,7 @@ public:Datatype *getTypedef(Datatype *ct,const string &name,uint8 id); ///< Create a new \e typedef data-typeTypePointerRel *getTypePointerRel(TypePointer *parentPtr,Datatype *ptrTo,int4 off); ///< Get pointer offset relative to a containerTypePointerRel *getTypePointerRel(int4 sz,Datatype *parent,Datatype *ptrTo,int4 ws,int4 off,const string &nm);+ TypePointer *getTypePointerWithSpace(Datatype *ptrTo,AddrSpace *spc,const string &nm);void destroyType(Datatype *ct); ///< Remove a data-type from \b thisDatatype *concretize(Datatype *ct); ///< Convert given data-type to concrete formvoid dependentOrder(vector<Datatype *> &deporder) const; ///< Place all data-types in dependency orderdiff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighParamID.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighParamID.javaindex 5c3df67093..463b9429ae 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighParamID.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighParamID.java@@ -21,8 +21,7 @@ import java.util.List;import org.xml.sax.*;import ghidra.program.model.address.Address;-import ghidra.program.model.data.DataType;-import ghidra.program.model.data.VoidDataType;+import ghidra.program.model.data.*;import ghidra.program.model.lang.*;import ghidra.program.model.listing.*;import ghidra.program.model.listing.Function.FunctionUpdateType;@@ -265,7 +264,7 @@ public class HighParamID extends PcodeSyntaxTree {dataType = pm.getDataType();}else {- dataType = dtManage.findUndefined(vn.getSize());+ dataType = Undefined.getUndefinedDataType(vn.getSize());}//Msg.debug(this, "func: " + func.getName() + " -- type: " + dataType.getName());if (!(dataType == null || dataType instanceof VoidDataType)) {@@ -298,7 +297,7 @@ public class HighParamID extends PcodeSyntaxTree {dataType = pm.getDataType();}else {- dataType = dtManage.findUndefined(vn.getSize());+ dataType = Undefined.getUndefinedDataType(vn.getSize());}Variable v = new ParameterImpl(null, dataType, buildStorage(vn), func.getProgram());//Msg.debug(this, "function(" + func.getName() + ")--param: " + v.toString() +diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PcodeDataTypeManager.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PcodeDataTypeManager.javaindex 861abe0ae7..231d41fc41 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PcodeDataTypeManager.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PcodeDataTypeManager.java@@ -20,6 +20,7 @@ import java.util.Arrays;import ghidra.app.plugin.processors.sleigh.SleighLanguage;import ghidra.program.database.data.PointerTypedefInspector;+import ghidra.program.model.address.AddressSpace;import ghidra.program.model.data.*;import ghidra.program.model.data.Enum;import ghidra.program.model.lang.CompilerSpec;@@ -31,12 +32,14 @@ import ghidra.xml.XmlPullParser;/***- * Class for making Ghidra DataTypes suitable for use with pcode- *+ * Class for marshaling DataType objects to and from the Decompiler.**/public class PcodeDataTypeManager {+ /**+ * A mapping between a DataType and its (name,id) on the decompiler side+ */private static class TypeMap {public DataType dt; // Full datatype objectpublic String name; // Name of the datatype on decompiler side@@ -114,12 +117,6 @@ public class PcodeDataTypeManager {return program;}- public DataType findUndefined(int size) {-// if (size==1)-// return dtMap.get("undefined");- return Undefined.getUndefinedDataType(size);- }-/*** Find a base/built-in data-type with the given name and/or id. If an id is provided and* a corresponding data-type exists, this data-type is returned. Otherwise the first@@ -278,6 +275,86 @@ public class PcodeDataTypeManager {return Undefined1DataType.dataType;}+ /**+ * Build XML for the void data-type+ * @param resBuf is the stream to write to+ */+ private void buildVoid(StringBuilder resBuf) {+ resBuf.append("<void/>");+ }++ /**+ * Build XML for a Pointer data-type+ * @param resBuf is the stream to write to+ * @param type is the Pointer data-type+ * @param spc if non-null, is the specific address space associated with the pointer+ * @param typeDef if non-null is the base TypeDef for this special form pointer+ * @param size if non-zero, is the size of the data-type in context+ */+ private void buildPointer(StringBuilder resBuf, Pointer type, AddressSpace spc, TypeDef typeDef,+ int size) {+ resBuf.append("<type");+ if (typeDef == null) {+ SpecXmlUtils.encodeStringAttribute(resBuf, "name", "");+ }+ else {+ appendNameIdAttributes(resBuf, typeDef); // Use the typedef name and id+ }+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "ptr");+ int ptrLen = type.getLength();+ if (ptrLen <= 0) {+ ptrLen = size;+ }+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", ptrLen);+ if (pointerWordSize != 1) {+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "wordsize", pointerWordSize);+ }+ if (spc != null) {+ SpecXmlUtils.encodeStringAttribute(resBuf, "space", spc.getName());+ }+ resBuf.append('>');+ DataType ptrto = type.getDataType();++ if (ptrto != null && ptrto.getDataTypeManager() != progDataTypes) {+ ptrto = ptrto.clone(progDataTypes);+ }++ if (ptrto == null) {+ buildTypeRef(resBuf, DefaultDataType.dataType, 1);+ }+ else if (ptrto instanceof AbstractStringDataType) {+ if ((ptrto instanceof StringDataType) || (type instanceof TerminatedStringDataType)) { // Convert pointer to string+ appendCharTypeRef(resBuf, dataOrganization.getCharSize()); // to pointer to char+ }+ else if (ptrto instanceof StringUTF8DataType) { // Convert pointer to string+ // TODO: Need to ensure that UTF8 decoding applies+ appendCharTypeRef(resBuf, 1); // to pointer to char+ }+ else if ((ptrto instanceof UnicodeDataType) ||+ (ptrto instanceof TerminatedUnicodeDataType)) {+ appendCharTypeRef(resBuf, 2);+ }+ else if ((ptrto instanceof Unicode32DataType) ||+ (ptrto instanceof TerminatedUnicode32DataType)) {+ appendCharTypeRef(resBuf, 4);+ }+ else {+ buildOpaqueString(resBuf, ptrto, 16384);+ }+ }+ else if (ptrto instanceof FunctionDefinition) {+ // FunctionDefinition may have size of -1, do not translate to undefined+ buildTypeRef(resBuf, ptrto, ptrto.getLength());+ }+ else if (ptrto.getLength() < 0 && !(ptrto instanceof FunctionDefinition)) {+ buildTypeRef(resBuf, Undefined1DataType.dataType, 1);+ }+ else {+ buildTypeRef(resBuf, ptrto, ptrto.getLength());+ }+ resBuf.append("</type>");+ }+/*** Build an XML representation of a pointer with an associated offset relative to a base data-type.* The pointer is encoded as a TypeDef (of a Pointer). The "pointed to" object is the base data-type,@@ -285,8 +362,10 @@ public class PcodeDataTypeManager {* @param resBuf is the output buffer accumulating the XML* @param type is the TypeDef encoding the relative pointer* @param offset is the relative offset (already extracted from the TypeDef)+ * @param space if non-null, is a specific address space associated with the pointer*/- private void buildPointerRelative(StringBuilder resBuf, TypeDef type, long offset) {+ private void buildPointerRelative(StringBuilder resBuf, TypeDef type, long offset,+ AddressSpace space) {Pointer pointer = (Pointer) type.getBaseDataType();resBuf.append("<type");SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "ptrrel");@@ -295,6 +374,9 @@ public class PcodeDataTypeManager {if (pointerWordSize != 1) {SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "wordsize", pointerWordSize);}+ if (space != null) {+ SpecXmlUtils.encodeStringAttribute(resBuf, "space", space.getName());+ }resBuf.append(">\n");DataType parent = pointer.getDataType();DataType ptrto = findPointerRelativeInner(parent, (int) offset);@@ -304,6 +386,393 @@ public class PcodeDataTypeManager {resBuf.append("</type>");}+ /**+ * Build XML for an Array data-type+ * @param resBuf is the stream to write to+ * @param type is the Array data-type+ * @param size if non-zero, is the size of the data-type in context+ */+ private void buildArray(StringBuilder resBuf, Array type, int size) {+ if (type.isZeroLength()) {+ // TODO: Zero-element arrays not yet supported+ buildOpaqueDataType(resBuf, type, size);+ return;+ }+ resBuf.append("<type");+ SpecXmlUtils.encodeStringAttribute(resBuf, "name", "");+ int sz = type.getLength();+ if (sz == 0) {+ sz = size;+ }+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "array");+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", sz);+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "arraysize", type.getNumElements());+ resBuf.append('>');+ buildTypeRef(resBuf, type.getDataType(), type.getElementLength());+ resBuf.append("</type>");+ }++ /**+ * Build XML for a Structure data-type+ * @param resBuf is the stream to write to+ * @param type is the Structure data-type+ * @param size if non-zero, is the size of the data-type in context+ */+ private void buildStructure(StringBuilder resBuf, Structure type, int size) {+ resBuf.append("<type");+ appendNameIdAttributes(resBuf, type);+ // if size is 0, insert an Undefined4 component+ //+ int sz = type.getLength();+ if (sz == 0) {+ type = new StructureDataType(type.getCategoryPath(), type.getName(), 1);+ sz = type.getLength();+ }+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "struct");+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", sz);+ resBuf.append(">\n");+ DataTypeComponent[] comps = type.getDefinedComponents();+ for (DataTypeComponent comp : comps) {+ if (comp.isBitFieldComponent() || comp.getLength() == 0) {+ // TODO: bitfields, zero-length components and zero-element arrays are not yet supported by decompiler+ continue;+ }+ resBuf.append("<field");+ String field_name = comp.getFieldName();+ if (field_name == null) {+ field_name = comp.getDefaultFieldName();+ }+ SpecXmlUtils.xmlEscapeAttribute(resBuf, "name", field_name);+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "offset", comp.getOffset());+ resBuf.append('>');+ DataType fieldtype = comp.getDataType();+ buildTypeRef(resBuf, fieldtype, comp.getLength());+ resBuf.append("</field>\n");+ }+ resBuf.append("</type>");+ }++ /**+ * Build XML for a Union data-type+ * @param resBuf is the stream to write to+ * @param unionType is the Union data-type+ */+ public void buildUnion(StringBuilder resBuf, Union unionType) {+ resBuf.append("<type");+ appendNameIdAttributes(resBuf, unionType);+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "union");+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", unionType.getLength());+ resBuf.append(">\n");+ DataTypeComponent[] comps = unionType.getDefinedComponents();+ for (DataTypeComponent comp : comps) {+ if (comp.getLength() == 0) {+ continue;+ }+ resBuf.append("<field");+ String field_name = comp.getFieldName();+ if (field_name == null) {+ field_name = comp.getDefaultFieldName();+ }+ SpecXmlUtils.xmlEscapeAttribute(resBuf, "name", field_name);+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "offset", comp.getOffset());+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "id", comp.getOrdinal());+ resBuf.append('>');+ DataType fieldtype = comp.getDataType();+ buildTypeRef(resBuf, fieldtype, comp.getLength());+ resBuf.append("</field>\n");+ }+ resBuf.append("</type>");+ }++ /**+ * Build XML for an Enum data-type+ * @param resBuf is the stream to write to+ * @param type is the Enum data-type+ * @param size if non-zero, is the size of the data-type in context+ */+ private void buildEnum(StringBuilder resBuf, Enum type, int size) {+ resBuf.append("<type");+ appendNameIdAttributes(resBuf, type);+ long[] keys = type.getValues();+ String metatype = "uint";+ for (long key : keys) {+ if (key < 0) {+ metatype = "int";+ break;+ }+ }+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", metatype);+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", type.getLength());+ SpecXmlUtils.encodeBooleanAttribute(resBuf, "enum", true);+ resBuf.append(">\n");+ for (long key : keys) {+ resBuf.append("<val");+ SpecXmlUtils.xmlEscapeAttribute(resBuf, "name", type.getName(key));+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "value", key);+ resBuf.append("/>");+ }+ resBuf.append("</type>");+ }++ /**+ * Build XML for a character data-type+ * @param resBuf is the stream to write to+ * @param type is the character data-type+ * @param size if non-zero, is the size of the data-type in context+ */+ private void buildCharDataType(StringBuilder resBuf, CharDataType type, int size) {+ resBuf.append("<type");+ appendNameIdAttributes(resBuf, type);+ boolean signed = type.isSigned();+ int sz = type.getLength();+ if (sz <= 0) {+ sz = size;+ }+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", signed ? "int" : "uint");+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", sz);+ if (sz == 1) {+ SpecXmlUtils.encodeBooleanAttribute(resBuf, "char", true);+ }+ else {+ SpecXmlUtils.encodeBooleanAttribute(resBuf, "utf", true);+ }+ resBuf.append('>');+ resBuf.append("</type>");+ }++ /**+ * Build XML for a wide character data-type+ * @param resBuf is the stream to write to+ * @param type is the Pointer data-type+ */+ private void buildWideCharDataType(StringBuilder resBuf, DataType type) {+ resBuf.append("<type");+ appendNameIdAttributes(resBuf, type);+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "int");+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", type.getLength());+ SpecXmlUtils.encodeBooleanAttribute(resBuf, "utf", true);+ resBuf.append('>');+ resBuf.append("</type>");+ }++ /**+ * Build XML for a string of char data-type+ * @param resBuf is the stream to write to+ * @param size is the length of the string+ */+ private void buildStringDataType(StringBuilder resBuf, int size) {+ resBuf.append("<type");+ SpecXmlUtils.encodeStringAttribute(resBuf, "name", "");+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "array");+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", size);+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "arraysize", size);+ resBuf.append('>');+ appendCharTypeRef(resBuf, dataOrganization.getCharSize());+ resBuf.append("</type>");+ }++ /**+ * Build XML for a UTF8 encoded string data-type+ * @param resBuf is the stream to write to+ * @param size is the length of the string (in bytes)+ */+ private void buildStringUTF8DataType(StringBuilder resBuf, int size) {+ resBuf.append("<type");+ SpecXmlUtils.encodeStringAttribute(resBuf, "name", "");+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "array");+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", size);+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "arraysize", size);+ resBuf.append('>');+ appendCharTypeRef(resBuf, 1); // TODO: Need to ensure that UTF8 decoding applies+ resBuf.append("</type>");+ }++ /**+ * Build XML for a UTF16 encoded string data-type+ * @param resBuf is the stream to write to+ * @param size is the length of the string (in bytes)+ */+ private void buildUnicodeDataType(StringBuilder resBuf, int size) {+ resBuf.append("<type");+ SpecXmlUtils.encodeStringAttribute(resBuf, "name", "");+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "array");+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", size);+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "arraysize", size / 2);+ resBuf.append('>');+ appendCharTypeRef(resBuf, 2);+ resBuf.append("</type>");+ }++ /**+ * Build XML for a UTF32 encoded string data-type+ * @param resBuf is the stream to write to+ * @param size is the length of the string (in bytes)+ */+ private void buildUnicode32DataType(StringBuilder resBuf, int size) {+ resBuf.append("<type");+ SpecXmlUtils.encodeStringAttribute(resBuf, "name", "");+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "array");+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", size);+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "arraysize", size / 4);+ resBuf.append('>');+ appendCharTypeRef(resBuf, 4);+ resBuf.append("</type>");+ }++ /**+ * Build XML for a FunctionDefinition data-type+ * @param resBuf is the stream to write to+ * @param type is the FunctionDefinition data-type+ */+ private void buildFunctionDefinition(StringBuilder resBuf, FunctionDefinition type) {+ resBuf.append("<type");+ appendNameIdAttributes(resBuf, type);+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "code");+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", 1); // Force size of 1+ resBuf.append('>');+ CompilerSpec cspec = program.getCompilerSpec();+ FunctionPrototype fproto = new FunctionPrototype(type, cspec, voidInputIsVarargs);+ fproto.buildPrototypeXML(resBuf, this);+ resBuf.append("</type>");+ }++ /**+ * Build XML for a boolean data-type+ * @param resBuf is the stream to write to+ * @param type is the boolean data-type+ */+ private void buildBooleanDataType(StringBuilder resBuf, DataType type) {+ resBuf.append("<type");+ appendNameIdAttributes(resBuf, type);+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "bool");+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", type.getLength());+ resBuf.append('>');+ resBuf.append("</type>");+ }++ /**+ * Build XML for an integer data-type+ * @param resBuf is the stream to write to+ * @param type is the integer data-type+ * @param size if non-zero, is the size of the data-type in context+ */+ private void buildAbstractIntegerDataType(StringBuilder resBuf, AbstractIntegerDataType type,+ int size) {+ resBuf.append("<type");+ boolean signed = type.isSigned();+ int sz = type.getLength();+ if (sz <= 0) {+ sz = size;+ }+ appendNameIdAttributes(resBuf, type);+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", signed ? "int" : "uint");+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", sz);+ resBuf.append('>');+ resBuf.append("</type>");+ }++ /**+ * Build XML for a floating-point data-type+ * @param resBuf is the stream to write to+ * @param type is the floating-point data-type+ */+ private void buildAbstractFloatDataType(StringBuilder resBuf, DataType type) {+ resBuf.append("<type");+ appendNameIdAttributes(resBuf, type);+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "float");+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", type.getLength());+ resBuf.append('>');+ resBuf.append("</type>");+ }++ /**+ * Build XML for a data-type whose internals are opaque (to the Decompiler)+ * @param resBuf is the stream to write to+ * @param type is the opaque data-type+ * @param size if non-zero, is the size of the data-type in context+ */+ private void buildOpaqueDataType(StringBuilder resBuf, DataType type, int size) {+ resBuf.append("<type");+ int sz = type.getLength();+ boolean isVarLength = false;+ if (sz <= 0) {+ sz = size;+ isVarLength = true;+ }+ appendNameIdAttributes(resBuf, type);+ if (sz < 16) {+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "unknown");+ }+ else {+ // Build an "opaque" structure with no fields+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "struct");+ }+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", sz);+ if (isVarLength) {+ SpecXmlUtils.encodeBooleanAttribute(resBuf, "varlength", true);+ }+ resBuf.append('>');+ resBuf.append("</type>");+ }++ /**+ * Build XML for a string data-type whose internals are opaque (to the Decompiler)+ * @param resBuf is the stream to write to+ * @param type is the opaque string+ * @param size is the length of the string (in bytes)+ */+ private void buildOpaqueString(StringBuilder resBuf, DataType type, int size) {+ resBuf.append("<type");+ appendNameIdAttributes(resBuf, type);+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "struct");+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", size);+ SpecXmlUtils.encodeBooleanAttribute(resBuf, "opaquestring", true);+ SpecXmlUtils.encodeBooleanAttribute(resBuf, "varlength", true);+ resBuf.append(">\n");+ resBuf.append("<field name=\"unknown_data1\"");+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "offset", 0);+ resBuf.append("> <typeref name=\"byte\"/></field>\n");+ size -= 1;+ resBuf.append("<field name=\"opaque_data\"");+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "offset", 1);+ resBuf.append("> <type");+ SpecXmlUtils.encodeStringAttribute(resBuf, "name", "");+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "array");+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", size);+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "arraysize", size);+ resBuf.append("><typeref name=\"byte\"/></type>");+ resBuf.append("</field>\n");+ resBuf.append("</type>");+ }++ /**+ * Build an XML document string representing the Structure that has+ * its size reported as zero.+ *+ * @param type data type to build XML for+ *+ * @return XML string document+ */+ public StringBuilder buildCompositeZeroSizePlaceholder(DataType type) {+ StringBuilder resBuf = new StringBuilder();+ String metaString;+ if (type instanceof Structure) {+ metaString = "struct";+ }+ else if (type instanceof Union) {+ metaString = "union";+ }+ else {+ return resBuf; //empty. Could throw AssertException.+ }+ resBuf.append("<type");+ SpecXmlUtils.xmlEscapeAttribute(resBuf, "name", type.getDisplayName());+ resBuf.append(" id=\"0x" + Long.toHexString(progDataTypes.getID(type)) + "\" metatype=\"");+ resBuf.append(metaString);+ resBuf.append("\" size=\"0\"></type>");+ return resBuf;+ }+/*** Build an XML representation of a TypeDef data-type. Generally this sends* a \<def> tag with a \<typeref> reference to the underlying data-type being typedefed,@@ -319,20 +788,29 @@ public class PcodeDataTypeManager {if (sz <= 0) {sz = size;}-if (type.isPointer()) {if (hasUnsupportedTypedefSettings(type)) {// switch refType to undefined type if pointer-typedef settings are unsupportedrefType = Undefined.getUndefinedDataType(sz);}else {+ AddressSpace space = PointerTypedefInspector.getPointerAddressSpace(type,+ program.getAddressFactory());long offset = PointerTypedefInspector.getPointerComponentOffset(type);if (offset != 0) {- buildPointerRelative(resBuf, type, offset);+ buildPointerRelative(resBuf, type, offset, space);+ return;+ }+ if (space != null) {+ // Cannot use space, unless we are build an actual Pointer+ // Its possible that refType is still a TypeDef+ refType = type.getBaseDataType();+ buildPointer(resBuf, (Pointer) refType, space, type, size);return;}}}+resBuf.append("<def");appendNameIdAttributes(resBuf, type);resBuf.append('>');@@ -342,12 +820,6 @@ public class PcodeDataTypeManager {return;}- private boolean hasUnsupportedTypedefSettings(TypeDef type) {- return (PointerTypedefInspector.getPointerType(type) != PointerType.DEFAULT ||- PointerTypedefInspector.hasPointerBitShift(type) ||- PointerTypedefInspector.hasPointerBitMask(type));- }-/*** Generate an XML tag describing the given data-type. Most data-types produce a {@code <type>} tag,* fully describing the data-type. Where possible a {@code <typeref>} tag is produced, which just gives@@ -411,6 +883,41 @@ public class PcodeDataTypeManager {resBuf.append("/>");}+ /**+ * Check for data-type settings that the Decompiler does not support+ * @param type is the data-type whose settings are checked+ * @return true if the data-type does have unsupported settings+ */+ private static boolean hasUnsupportedTypedefSettings(TypeDef type) {+ return (PointerTypedefInspector.getPointerType(type) != PointerType.DEFAULT ||+ PointerTypedefInspector.hasPointerBitShift(type) ||+ PointerTypedefInspector.hasPointerBitMask(type));+ }++ /**+ * Append the name and id associated with a given data-type to an XML stream+ * @param resBuf is the stream to append to+ * @param type is the given data-type+ */+ private void appendNameIdAttributes(StringBuilder resBuf, DataType type) {+ if (type instanceof BuiltIn) {+ SpecXmlUtils.xmlEscapeAttribute(resBuf, "name",+ ((BuiltIn) type).getDecompilerDisplayName(displayLanguage));+ }+ else {+ SpecXmlUtils.xmlEscapeAttribute(resBuf, "name", type.getName());+ long id = progDataTypes.getID(type);+ if (id > 0) {+ SpecXmlUtils.encodeUnsignedIntegerAttribute(resBuf, "id", id);+ }+ }+ }++ /**+ * Append an XML reference for a character data-type, based on the size in bytes+ * @param resBuf is the stream to append to+ * @param size is the requested size of the character data-type+ */private void appendCharTypeRef(StringBuilder resBuf, int size) {if (size == dataOrganization.getCharSize()) {resBuf.append("<typeref name=\"char\"/>"); // could have size 1 or 2@@ -435,27 +942,6 @@ public class PcodeDataTypeManager {throw new IllegalArgumentException("Unsupported character size");}- private void appendOpaqueString(StringBuilder resBuf, DataType type, int size) {- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "struct");- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", size);- SpecXmlUtils.encodeBooleanAttribute(resBuf, "opaquestring", true);- SpecXmlUtils.encodeBooleanAttribute(resBuf, "varlength", true);- resBuf.append(">\n");- resBuf.append("<field name=\"unknown_data1\"");- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "offset", 0);- resBuf.append("> <typeref name=\"byte\"/></field>\n");- size -= 1;- resBuf.append("<field name=\"opaque_data\"");- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "offset", 1);- resBuf.append("> <type");- SpecXmlUtils.encodeStringAttribute(resBuf, "name", "");- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "array");- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", size);- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "arraysize", size);- resBuf.append("><typeref name=\"byte\"/></type>");- resBuf.append("</field>\n");- }-/*** Build an XML document string representing the type information for a data type*@@ -468,332 +954,74 @@ public class PcodeDataTypeManager {type = type.clone(progDataTypes);}if ((type instanceof VoidDataType) || (type == null)) {- resBuf.append("<void/>");- return;+ buildVoid(resBuf);}else if (type instanceof TypeDef) {buildTypeDef(resBuf, (TypeDef) type, size);- return;}- resBuf.append("<type");- if (type instanceof Pointer) {- SpecXmlUtils.encodeStringAttribute(resBuf, "name", "");- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "ptr");- int ptrLen = type.getLength();- if (ptrLen <= 0) {- ptrLen = size;- }- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", ptrLen);- if (pointerWordSize != 1) {- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "wordsize", pointerWordSize);- }- resBuf.append('>');- DataType ptrto = ((Pointer) type).getDataType();-- if (ptrto != null && ptrto.getDataTypeManager() != progDataTypes) {- ptrto = ptrto.clone(progDataTypes);- }-- if (ptrto == null) {- buildTypeRef(resBuf, DefaultDataType.dataType, 1);- }- else if (ptrto instanceof AbstractStringDataType) {- if ((ptrto instanceof StringDataType) ||- (type instanceof TerminatedStringDataType)) { // Convert pointer to string- appendCharTypeRef(resBuf, dataOrganization.getCharSize()); // to pointer to char- }- else if (ptrto instanceof StringUTF8DataType) { // Convert pointer to string- // TODO: Need to ensure that UTF8 decoding applies- appendCharTypeRef(resBuf, 1); // to pointer to char- }- else if ((ptrto instanceof UnicodeDataType) ||- (ptrto instanceof TerminatedUnicodeDataType)) {- appendCharTypeRef(resBuf, 2);- }- else if ((ptrto instanceof Unicode32DataType) ||- (ptrto instanceof TerminatedUnicode32DataType)) {- appendCharTypeRef(resBuf, 4);- }- else {- resBuf.append("<type");- appendNameIdAttributes(resBuf, ptrto);- appendOpaqueString(resBuf, ptrto, 16384);- resBuf.append("</type>\n");- }- }- else if (ptrto instanceof FunctionDefinition) {- // FunctionDefinition may have size of -1, do not translate to undefined- buildTypeRef(resBuf, ptrto, ptrto.getLength());- }- else if (ptrto.getLength() < 0 && !(ptrto instanceof FunctionDefinition)) {- buildTypeRef(resBuf, Undefined1DataType.dataType, 1);- }- else {- buildTypeRef(resBuf, ptrto, ptrto.getLength());- }+ else if (type instanceof Pointer) {+ buildPointer(resBuf, (Pointer) type, null, null, size);}- else if ((type instanceof Array) && !type.isZeroLength()) {- // TODO: Zero-element arrays not yet supported- SpecXmlUtils.encodeStringAttribute(resBuf, "name", "");- int sz = type.getLength();- if (sz == 0) {- sz = size;- }- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "array");- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", sz);- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "arraysize",- ((Array) type).getNumElements());- resBuf.append('>');- buildTypeRef(resBuf, ((Array) type).getDataType(), ((Array) type).getElementLength());+ else if (type instanceof Array) {+ buildArray(resBuf, (Array) type, size);}else if (type instanceof Structure) {- appendNameIdAttributes(resBuf, type);- // if size is 0, insert an Undefined4 component- //- int sz = type.getLength();- if (sz == 0) {- type = new StructureDataType(type.getCategoryPath(), type.getName(), 1);- sz = type.getLength();- }- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "struct");- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", sz);- resBuf.append(">\n");- DataTypeComponent[] comps = ((Structure) type).getDefinedComponents();- for (DataTypeComponent comp : comps) {- if (comp.isBitFieldComponent() || comp.getLength() == 0) {- // TODO: bitfields, zero-length components and zero-element arrays are not yet supported by decompiler- continue;- }- resBuf.append("<field");- String field_name = comp.getFieldName();- if (field_name == null) {- field_name = comp.getDefaultFieldName();- }- SpecXmlUtils.xmlEscapeAttribute(resBuf, "name", field_name);- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "offset", comp.getOffset());- resBuf.append('>');- DataType fieldtype = comp.getDataType();- buildTypeRef(resBuf, fieldtype, comp.getLength());- resBuf.append("</field>\n");- }+ buildStructure(resBuf, (Structure) type, size);}else if (type instanceof Union) {buildUnion(resBuf, (Union) type);}else if (type instanceof Enum) {- appendNameIdAttributes(resBuf, type);- Enum enumDt = (Enum) type;- long[] keys = enumDt.getValues();- String metatype = "uint";- for (long key : keys) {- if (key < 0) {- metatype = "int";- break;- }- }- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", metatype);- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", enumDt.getLength());- SpecXmlUtils.encodeBooleanAttribute(resBuf, "enum", true);- resBuf.append(">\n");- for (long key : keys) {- resBuf.append("<val");- SpecXmlUtils.xmlEscapeAttribute(resBuf, "name", enumDt.getName(key));- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "value", key);- resBuf.append("/>");- }+ buildEnum(resBuf, (Enum) type, size);}else if (type instanceof CharDataType) {- appendNameIdAttributes(resBuf, type);- boolean signed = ((CharDataType) type).isSigned();- int sz = type.getLength();- if (sz <= 0) {- sz = size;- }- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", signed ? "int" : "uint");- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", sz);- if (sz == 1) {- SpecXmlUtils.encodeBooleanAttribute(resBuf, "char", true);- }- else {- SpecXmlUtils.encodeBooleanAttribute(resBuf, "utf", true);- }- resBuf.append('>');+ buildCharDataType(resBuf, (CharDataType) type, size);}else if (type instanceof WideCharDataType || type instanceof WideChar16DataType ||type instanceof WideChar32DataType) {- appendNameIdAttributes(resBuf, type);- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "int");- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", type.getLength());- SpecXmlUtils.encodeBooleanAttribute(resBuf, "utf", true);- resBuf.append('>');+ buildWideCharDataType(resBuf, type);}else if (type instanceof AbstractStringDataType) {if ((type instanceof StringDataType) || (type instanceof TerminatedStringDataType)) {- SpecXmlUtils.encodeStringAttribute(resBuf, "name", "");- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "array");- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", size);- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "arraysize", size);- resBuf.append('>');- appendCharTypeRef(resBuf, dataOrganization.getCharSize());+ buildStringDataType(resBuf, size);}else if (type instanceof StringUTF8DataType) {- SpecXmlUtils.encodeStringAttribute(resBuf, "name", "");- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "array");- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", size);- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "arraysize", size);- resBuf.append('>');- appendCharTypeRef(resBuf, 1); // TODO: Need to ensure that UTF8 decoding applies+ buildStringUTF8DataType(resBuf, size);}else if ((type instanceof UnicodeDataType) ||(type instanceof TerminatedUnicodeDataType)) {- SpecXmlUtils.encodeStringAttribute(resBuf, "name", "");- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "array");- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", size);- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "arraysize", size / 2);- resBuf.append('>');- appendCharTypeRef(resBuf, 2);+ buildUnicodeDataType(resBuf, size);}else if ((type instanceof Unicode32DataType) ||(type instanceof TerminatedUnicode32DataType)) {- SpecXmlUtils.encodeStringAttribute(resBuf, "name", "");- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "array");- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", size);- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "arraysize", size / 4);- resBuf.append('>');- appendCharTypeRef(resBuf, 4);+ buildUnicode32DataType(resBuf, size);}else {- appendNameIdAttributes(resBuf, type);- appendOpaqueString(resBuf, type, size);+ buildOpaqueString(resBuf, type, size);}}else if (type instanceof FunctionDefinition) {- if (size <= 0) {- size = 1;- }- appendNameIdAttributes(resBuf, type);- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "code");- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", 1); // Force size of 1- resBuf.append('>');- FunctionDefinition fdef = (FunctionDefinition) type;- CompilerSpec cspec = program.getCompilerSpec();- FunctionPrototype fproto = new FunctionPrototype(fdef, cspec, voidInputIsVarargs);- fproto.buildPrototypeXML(resBuf, this);+ buildFunctionDefinition(resBuf, (FunctionDefinition) type);}else if (type instanceof BooleanDataType) {- appendNameIdAttributes(resBuf, type);- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "bool");- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", type.getLength());- resBuf.append('>');+ buildBooleanDataType(resBuf, type);}else if (type instanceof AbstractIntegerDataType) { // must handle char and bool above- boolean signed = ((AbstractIntegerDataType) type).isSigned();- int sz = type.getLength();- if (sz <= 0) {- sz = size;- }- appendNameIdAttributes(resBuf, type);- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", signed ? "int" : "uint");- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", sz);- resBuf.append('>');+ buildAbstractIntegerDataType(resBuf, (AbstractIntegerDataType) type, size);}else if (type instanceof AbstractFloatDataType) {- appendNameIdAttributes(resBuf, type);- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "float");- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", type.getLength());- resBuf.append('>');- }- else {- int sz = type.getLength();- boolean isVarLength = false;- if (sz <= 0) {- sz = size;- isVarLength = true;- }- appendNameIdAttributes(resBuf, type);- if (sz < 16) {- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "unknown");- }- else {- // Build an "opaque" structure with no fields- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "struct");- }- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", sz);- if (isVarLength) {- SpecXmlUtils.encodeBooleanAttribute(resBuf, "varlength", true);- }- resBuf.append('>');- }- resBuf.append("</type>");- }-- private void appendNameIdAttributes(StringBuilder resBuf, DataType type) {- if (type instanceof BuiltIn) {- SpecXmlUtils.xmlEscapeAttribute(resBuf, "name",- ((BuiltIn) type).getDecompilerDisplayName(displayLanguage));+ buildAbstractFloatDataType(resBuf, type);}else {- SpecXmlUtils.xmlEscapeAttribute(resBuf, "name", type.getName());- long id = progDataTypes.getID(type);- if (id > 0) {- SpecXmlUtils.encodeUnsignedIntegerAttribute(resBuf, "id", id);- }+ buildOpaqueDataType(resBuf, type, size);}}/**- * Build an XML document string representing the Structure that has- * its size reported as zero.+ * Build the list of core data-types. Data-types that are always available to the Decompiler+ * and are associated with a (metatype,size) pair.*- * @param type data type to build XML for- *- * @return XML string document*/- public StringBuilder buildCompositeZeroSizePlaceholder(DataType type) {- StringBuilder resBuf = new StringBuilder();- String metaString;- if (type instanceof Structure) {- metaString = "struct";- }- else if (type instanceof Union) {- metaString = "union";- }- else {- return resBuf; //empty. Could throw AssertException.- }- resBuf.append("<type");- SpecXmlUtils.xmlEscapeAttribute(resBuf, "name", type.getDisplayName());- resBuf.append(" id=\"0x" + Long.toHexString(progDataTypes.getID(type)) + "\" metatype=\"");- resBuf.append(metaString);- resBuf.append("\" size=\"0\"></type>");- return resBuf;- }-- public void buildUnion(StringBuilder buffer, Union unionType) {- appendNameIdAttributes(buffer, unionType);- SpecXmlUtils.encodeStringAttribute(buffer, "metatype", "union");- SpecXmlUtils.encodeSignedIntegerAttribute(buffer, "size", unionType.getLength());- buffer.append(">\n");- DataTypeComponent[] comps = unionType.getDefinedComponents();- for (DataTypeComponent comp : comps) {- if (comp.getLength() == 0) {- continue;- }- buffer.append("<field");- String field_name = comp.getFieldName();- if (field_name == null) {- field_name = comp.getDefaultFieldName();- }- SpecXmlUtils.xmlEscapeAttribute(buffer, "name", field_name);- SpecXmlUtils.encodeSignedIntegerAttribute(buffer, "offset", comp.getOffset());- SpecXmlUtils.encodeSignedIntegerAttribute(buffer, "id", comp.getOrdinal());- buffer.append('>');- DataType fieldtype = comp.getDataType();- buildTypeRef(buffer, fieldtype, comp.getLength());- buffer.append("</field>\n");- }- }-private void generateCoreTypes() {voidDt = new VoidDataType(progDataTypes);ArrayList<TypeMap> typeList = new ArrayList<>();@@ -854,6 +1082,9 @@ public class PcodeDataTypeManager {typeList.toArray(coreBuiltin);}+ /**+ * Sort the list of core data-types based their id+ */private void sortCoreTypes() {Arrays.sort(coreBuiltin, (o1, o2) -> Long.compare(o1.id, o2.id));}@@ -883,8 +1114,8 @@ public class PcodeDataTypeManager {}/**- * Build the coretypes xml element- * @return coretypes xml element+ * Build the coretypes XML element+ * @return coretypes XML element*/public String buildCoreTypes() {