Ghidra is a software reverse engineering (SRE) framework
GP-7136 join_per_primitive padding and shared stack
caheckmanbb33bad1960a
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/address.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/address.ccindex add6bb31c1..7fbf13f78b 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/address.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/address.cc@@ -101,6 +101,35 @@ Address::Address(mach_extreme ex)}}+/// If there is a register container of the given region print a description of the region+/// using the register name. Otherwise just print region as an address.+/// Return the size of the register or the \e expected size for the region.+/// \param s is the stream to print to+/// \param size is the number of bytes in the region+/// \return the size of the expected size+int4 Address::printRaw(ostream &s,int4 size) const++{+ const Translate *trans = base->getTrans();+ int4 expect;++ string name = trans->getRegisterName(base, offset, size);+ if (name.size() != 0) {+ const VarnodeData &point(trans->getRegister(name));+ uintb off = offset - point.offset;+ s << name;+ expect = point.size;+ if (off != 0)+ s << '+' << dec << off;+ }+ else {+ s << base->getShortcut();+ expect = trans->getDefaultSize();+ base->printRaw(s,offset);+ }+ return expect;+}+/// 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 rangediff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/address.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/address.hhindex f2ef7a9ae9..f854f1e0b1 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/address.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/address.hh@@ -74,7 +74,8 @@ public:bool isInvalid(void) const; ///< Is the address invalid?int4 getAddrSize(void) const; ///< Get the number of bytes in the addressbool isBigEndian(void) const; ///< Is data at this address big endian encoded- void printRaw(ostream &s) const; ///< Write a raw version of the address to a stream+ void printRaw(ostream &s) const; ///< Write a raw version of the address to a stream for debugging/logging+ int4 printRaw(ostream &s,int4 size) const; ///< Write a (small) region to a stream for debugging/loggingint4 read(const string &s); ///< Read in the address from a stringAddrSpace *getSpace(void) const; ///< Get the address spaceuintb getOffset(void) const; ///< Get the address offsetdiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.ccindex d63fdf395e..22a88bc682 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc@@ -1556,6 +1556,7 @@ void ActionFuncLink::funcLinkInput(FuncCallSpecs *fc,Funcdata &data)bool setplaceholder = varargs;for(int4 i=0;i<numparam;++i) {ProtoParameter *param = fc->getParam(i);+ if (!param->hasStorage()) continue;active->registerTrial(param->getAddress(),param->getSize());active->getTrial(i).markActive(); // Parameter is not optionalif (varargs){@@ -1635,7 +1636,7 @@ void ActionFuncLink::funcLinkOutput(FuncCallSpecs *fc,Funcdata &data)if (fc->isOutputLocked()) {ProtoParameter *outparam = fc->getOutput();Datatype *outtype = outparam->getType();- if (outtype->getMetatype() != TYPE_VOID) {+ if (outparam->hasStorage()) {int4 sz = outparam->getSize();if (outtype->getMetatype() == TYPE_BOOL && data.isTypeRecoveryOn())data.opMarkCalculatedBool(callop);@@ -1773,6 +1774,8 @@ int4 ActionParamDouble::apply(Funcdata &data)Datatype *tp = param->getType();if (!tp->isPrimitiveWhole())continue; // Not double precision objects+ if (!param->hasStorage())+ continue;Varnode *vn = data.findVarnodeInput(tp->getSize(),param->getAddress());if (vn == (Varnode *)0) continue;if (vn->getSize() < minDoubleSize) continue;@@ -1944,60 +1947,37 @@ void ActionReturnRecovery::buildReturnOutput(ParamActive *active,PcodeOp *retop,}if (newparam.size()<=2) // Easy zero or one return varnode casedata.opSetAllInput(retop,newparam);- else if (newparam.size()==3) { // Two piece concatenation case- Varnode *lovn = newparam[1];- Varnode *hivn = newparam[2];- ParamTrial &triallo( active->getTrial(0) );- ParamTrial &trialhi( active->getTrial(1) );- Address joinaddr = data.getArch()->constructJoinAddress(data.getArch()->translate,- trialhi.getAddress(),trialhi.getSize(),- triallo.getAddress(),triallo.getSize());- PcodeOp *newop = data.newOp(2,retop->getAddr());- data.opSetOpcode(newop,CPUI_PIECE);- Varnode *newwhole = data.newVarnodeOut(trialhi.getSize()+triallo.getSize(),joinaddr,newop);- newwhole->setWriteMask(); // Don't let new Varnode cause additional heritage- data.opInsertBefore(newop,retop);- newparam.pop_back();- newparam.back() = newwhole;- data.opSetAllInput(retop,newparam);- data.opSetInput(newop,hivn,0);- data.opSetInput(newop,lovn,1);- }- else { // We may have several varnodes from a single container- // Concatenate them into a single result- newparam.clear();- newparam.push_back(retop->getIn(0));- int4 offmatch = 0;- Varnode *preexist = (Varnode *)0;- for(int4 i=0;i<active->getNumTrials();++i) {- ParamTrial &curtrial(active->getTrial(i));- if (!curtrial.isUsed()) break;- if (curtrial.getSlot() >= retop->numInput()) break;- if (preexist == (Varnode *)0) {- preexist = retop->getIn(curtrial.getSlot());- offmatch = curtrial.getOffset() + curtrial.getSize();- }- else if (offmatch == curtrial.getOffset()) {- offmatch += curtrial.getSize();- Varnode *vn = retop->getIn(curtrial.getSlot());- // Concatenate the preexisting pieces with this new piece- PcodeOp *newop = data.newOp(2,retop->getAddr());- data.opSetOpcode(newop,CPUI_PIECE);- Address addr = preexist->getAddr();- if (vn->getAddr() < addr)- addr = vn->getAddr();- Varnode *newout = data.newVarnodeOut(preexist->getSize()+vn->getSize(),addr,newop);- newout->setWriteMask(); // Don't let new Varnode cause additional heritage- data.opSetInput(newop,vn,0); // Most sig part- data.opSetInput(newop,preexist,1);- data.opInsertBefore(newop,retop);- preexist = newout;+ else {+ vector<VarnodeData> pieces;+ int4 wholeSize = 0;+ for(int4 i=newparam.size()-1;i>=1;--i) {+ pieces.emplace_back(newparam[i]->getAddr(),newparam[i]->getSize());+ wholeSize += newparam[i]->getSize();+ }+ Address wholeAddr = data.getArch()->joinPieces(pieces, data.getArch()->translate);+ Varnode *lastVn = newparam[1]; // Start with least significant piece+ int4 curSize = lastVn->getSize();+ for(int4 i=2;i<newparam.size();++i) { // Concatenate sequentially from least to most significant+ Varnode *vn = newparam[i];+ curSize += vn->getSize(); // Size of current concatenation+ Address addr = wholeAddr; // Calculate address of current concatenation+ if (curSize != wholeSize) {+ if (addr.isBigEndian())+ addr = addr + (wholeSize - curSize);+ addr.renormalize(curSize);}- else- break;+ PcodeOp *newop = data.newOp(2,retop->getAddr());+ data.opSetOpcode(newop,CPUI_PIECE);+ Varnode *newout = data.newVarnodeOut(lastVn->getSize()+vn->getSize(),addr,newop);+ newout->setWriteMask(); // Don't let new Varnode cause additional heritage+ data.opSetInput(newop,vn,0); // Most sig part+ data.opSetInput(newop,lastVn,1);+ data.opInsertBefore(newop,retop);+ lastVn= newout;}- if (preexist != (Varnode *)0)- newparam.push_back(preexist);+ newparam.clear();+ newparam.push_back(retop->getIn(0));+ newparam.push_back(lastVn);data.opSetAllInput(retop,newparam);}}@@ -2070,6 +2050,7 @@ int4 ActionRestrictLocal::apply(Funcdata &data)int4 numparam = fc->numParams();for(int4 j=0;j<numparam;++j) {ProtoParameter *param = fc->getParam(j);+ if (!param->hasStorage()) continue;Address addr = param->getAddress();spacetype tp = addr.getSpace()->getType();if (tp == IPTR_SPACEBASE) {@@ -4850,7 +4831,7 @@ int4 ActionPrototypeTypes::apply(Funcdata &data)if (data.getFuncProto().isOutputLocked()) {ProtoParameter *outparam = data.getFuncProto().getOutput();- if (outparam->getType()->getMetatype() != TYPE_VOID) {+ if (outparam->hasStorage()) {for(iter=data.beginOp(CPUI_RETURN);iter!=iterend;++iter) {PcodeOp *op = *iter;if (op->isDead()) continue;@@ -4903,6 +4884,7 @@ int4 ActionPrototypeTypes::apply(Funcdata &data)int4 numparams = data.getFuncProto().numParams();for(int4 i=0;i<numparams;++i) {ProtoParameter *param = data.getFuncProto().getParam(i);+ if (!param->hasStorage()) continue;Varnode *vn = data.newVarnode( param->getSize(), param->getAddress());vn = data.setInputVarnode(vn);vn->setLockedInput();@@ -5391,7 +5373,7 @@ void ActionInferTypes::buildLocaltypes(Funcdata &data)SymbolEntry *entry = vn->getSymbolEntry();if (entry != (SymbolEntry *)0 && !vn->isTypeLock() && entry->getSymbol()->isTypeLocked()) {int4 curOff = entry->getOffset();- if (!entry->isDynamic())+ if (entry->isMapEntry())curOff += (vn->getAddr().getOffset() - ((MapEntry *)entry)->getAddr().getOffset());ct = typegrp->getExactPiece(entry->getSymbol()->getType(), curOff, vn->getSize());if (ct == (Datatype *)0 || ct->getMetatype() == TYPE_UNKNOWN) // If we can't resolve, or resolve to UNKNOWNdiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hhindex 0faf1a7f6d..ad8cbfcd93 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hh@@ -1043,7 +1043,8 @@ public:if (!grouplist.contains(getGroup())) return (Action *)0;return new ActionCopyMarker(getGroup());}- virtual int4 apply(Funcdata &data) { data.getMerge().markInternalCopies(); return 0; }+ virtual int4 apply(Funcdata &data) {+ data.getMerge().markInternalCopies(); data.getMerge().markPurePaddingCopies(); return 0; }};/// \brief Attach \e dynamically mapped symbols to Varnodes in time for data-type propagationdiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/database.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/database.ccindex 1d3dc214ea..a03f930615 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/database.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/database.cc@@ -65,11 +65,10 @@ SymbolRange::subsorttype SymbolRange::getSubsort(void) constSymbolEntry::SymbolEntry(Symbol *sym): symbol(sym){+ properties = 0;extraflags = 0;offset = 0;size = -1;- entrytype = map_entry;- is_piece = false;}/// \param sym is the Symbol \b this will be a map for@@ -82,11 +81,10 @@ SymbolEntry::SymbolEntry(Symbol *sym,uint4 exflags,int4 sz,int4 off,const RangeL{size = sz;symbol = sym;+ properties = 0;extraflags = exflags;offset = off;uselimit = use;- entrytype = map_entry;- is_piece = false;}/// This storage location may only hold the Symbol value for a limited portion of the code.@@ -133,6 +131,7 @@ bool SymbolEntry::updateType(Varnode *vn) constMapEntry::MapEntry(Symbol *sym,uint4 exflags,const Address &ad,int4 sz,int4 off,const RangeList &use): SymbolEntry(sym,exflags,sz,off,use), addr(ad){+ properties |= map_entry;}Datatype *MapEntry::getSizedType(const Address &inaddr,int4 sz) const@@ -168,8 +167,6 @@ void MapEntry::decode(Decoder &decoder){addr = Address::decode(decoder);- if (addr.isInvalid())- throw LowlevelError("Invalid address decoding MapEntry");uselimit.decode(decoder);}@@ -184,7 +181,7 @@ MapEntryConflict::MapEntryConflict(Symbol *sym,Varnode *vn)uniq = seqNum.getTime();uselimit.insertRange(seqNum.getAddr().getSpace(),seqNum.getAddr().getOffset(),seqNum.getAddr().getOffset());- entrytype = conflict_entry;+ properties |= conflict_entry;}void MapEntryConflict::printEntry(ostream &s) const@@ -223,7 +220,7 @@ DynamicEntry::DynamicEntry(Symbol *sym,uint4 exfl,uint8 h,int4 off,int4 sz,const: SymbolEntry(sym,exfl,sz,off,rnglist){hash = h;- entrytype = dynamic_entry;+ properties |= dynamic_entry;}Datatype *DynamicEntry::getSizedType(const Address &inaddr,int4 sz) const@@ -263,6 +260,44 @@ void DynamicEntry::decode(Decoder &decoder)uselimit.decode(decoder);}+UnassignedEntry::UnassignedEntry(Symbol *sym)+ : SymbolEntry(sym)+{+ // uselimit not relevent+ properties |= unassigned_entry;+ size = sym->getBytesConsumed();+}++Datatype *UnassignedEntry::getSizedType(const Address &addr,int4 sz) const++{+ Datatype *cur = symbol->getType();+ return symbol->getScope()->getArch()->types->getExactPiece(cur, offset, sz);+}++void UnassignedEntry::printEntry(ostream &s) const++{+ s << symbol->getName() << " : <unassigned>:";+ s << dec << (uint4) symbol->getType()->getSize();+ s << ' ';+ symbol->getType()->printRaw(s);+}++void UnassignedEntry::encode(Encoder &encoder) const++{+ encoder.openElement(ELEM_ADDR);+ encoder.closeElement(ELEM_ADDR);+ uselimit.encode(encoder);+}++void UnassignedEntry::decode(Decoder &decoder)++{+ throw LowlevelError("Cannot decode UnassignedEntry directly");+}+Symbol::~Symbol(void){@@ -332,7 +367,7 @@ MapEntry *Symbol::getMapEntry(const Address &addr) const{for(int4 i=0;i<mapentry.size();++i) {SymbolEntry *tmp = mapentry[i];- if (tmp->isDynamic()) continue;+ if (!tmp->isMapEntry()) continue;MapEntry *res = (MapEntry *)tmp;const Address &entryaddr( res->getAddr() );if (addr.getSpace() != entryaddr.getSpace()) continue;@@ -1179,6 +1214,10 @@ void Scope::removeRange(AddrSpace *spc,uintb first,uintb last)void Scope::addMap(MapEntry *entry){+ if (entry->addr.isInvalid()) {+ addUnassigned(entry);+ return;+ }// First set properties of this symbol based on scope// entry.symbol->flags |= Varnode::mapped;if (isGlobal())@@ -1212,19 +1251,21 @@ void Scope::addMap(MapEntry *entry)for(int4 j=0;j<num;++j) {int4 i = bigendian ? j : (num-1-j); // Take pieces in endian orderconst VarnodeData &vdat(rec->getPiece(i));- exfl = 0;- if (entry->symbol->getType()->isPrimitiveWhole()) {- if (i==0) // i==0 is most signif- exfl = Varnode::precishi;- else if (i==num-1)- exfl = Varnode::precislo;- else- exfl = Varnode::precislo | Varnode::precishi; // Middle pieces have both flags set+ if (vdat.space->getType() != IPTR_CONSTANT) {+ exfl = 0;+ if (entry->symbol->getType()->isPrimitiveWhole()) {+ if (i==0) // i==0 is most signif+ exfl = Varnode::precishi;+ else if (i==num-1)+ exfl = Varnode::precislo;+ else+ exfl = Varnode::precislo | Varnode::precishi; // Middle pieces have both flags set+ }+ // NOTE: we do not turn on the mapped flag for the pieces+ MapEntry *piece = new MapEntry(entry->symbol,exfl,vdat.getAddr(),vdat.size,off,entry->uselimit);+ piece->properties |= SymbolEntry::piece;+ addMapInternal(entry->symbol,piece);}- // NOTE: we do not turn on the mapped flag for the pieces- MapEntry *piece = new MapEntry(entry->symbol,exfl,vdat.getAddr(),vdat.size,off,entry->uselimit);- piece->is_piece = true;- addMapInternal(entry->symbol,piece);off += vdat.size;}}@@ -1246,6 +1287,18 @@ void Scope::addDynamic(DynamicEntry *entry)addDynamicMapInternal(entry->symbol,entry);}+void Scope::addUnassigned(MapEntry *entry)++{+ Symbol *sym = entry->symbol;+ delete entry;+ UnassignedEntry *unassignedEntry = new UnassignedEntry(sym);+ if (isGlobal())+ entry->symbol->flags |= Varnode::persist;++ unassignedEntry->symbol->mapentry.push_back(unassignedEntry);+}+Scope::~Scope(void){@@ -1872,7 +1925,7 @@ string Scope::buildDefaultName(Symbol *sym,int4 &base,Varnode *vn) constif (sym->numEntries() != 0) {Address addr;SymbolEntry *entry = sym->getMapEntry(0);- if (!entry->isDynamic())+ if (entry->isMapEntry())addr = ((MapEntry *)entry)->getAddr();Address usepoint = entry->getFirstUseAddress();uint4 flags = usepoint.isInvalid() ? Varnode::addrtied : 0;@@ -2220,12 +2273,12 @@ void ScopeInternal::removeSymbolMappings(Symbol *symbol)// Remove each mapping of the symbolfor(iter=symbol->mapentry.begin();iter!=symbol->mapentry.end();++iter) {SymbolEntry *entry = *iter;- if (!entry->isDynamic()) {+ if (entry->isMapEntry()) {AddrSpace *spc = ((MapEntry *)entry)->getAddr().getSpace();EntryMap *rangemap = maptable[spc->getIndex()];rangemap->erase( ((MapEntry *)entry)->mapIterator );}- else+ else if (entry->isDynamic())dynamicentry.erase( ((DynamicEntry *)entry)->dynIterator );delete entry;}@@ -2274,7 +2327,7 @@ void ScopeInternal::retypeSymbol(Symbol *sym,Datatype *ct)}if (sym->mapentry.size()==1) {SymbolEntry *entry = sym->mapentry.back();- if (!entry->isDynamic() && entry->isAddrTied()) {+ if (entry->isMapEntry() && entry->isAddrTied()) {Address addr(((MapEntry *)entry)->getAddr()); // Save the starting address of mapEntryMap *rangemap = maptable[ addr.getSpace()->getIndex() ]; // Find the correct rangemap@@ -2567,7 +2620,10 @@ string ScopeInternal::buildVariableName(const Address &addr,ostringstream s;int4 sz = (ct == (Datatype *)0) ? 1 : ct->getSize();- if ((flags & Varnode::unaffected)!=0) {+ if (addr.isInvalid()) {+ s << "unassigned_";+ }+ else if ((flags & Varnode::unaffected)!=0) {if ((flags & Varnode::return_address)!=0)s << "unaff_retaddr";else {@@ -2889,7 +2945,7 @@ void ScopeInternal::decode(Decoder &decoder)Symbol *sym = addMapSym(decoder);if (rangeequalssymbols) {SymbolEntry *e = sym->getFirstWholeMap();- if (!e->isDynamic()) {+ if (e->isMapEntry()) {MapEntry *map = (MapEntry *)e;glb->symboltab->addRange(this,map->getAddr().getSpace(),map->getFirst(),map->getLast());}@@ -2897,7 +2953,7 @@ void ScopeInternal::decode(Decoder &decoder)uint4 props = sym->getFlags() & (Varnode::readonly | Varnode::volatil);if (props != 0) {SymbolEntry *e = sym->getFirstWholeMap();- if (!e->isDynamic()) {+ if (e->isMapEntry()) {MapEntry *map = (MapEntry *)e;Range rng(map->getAddr().getSpace(),map->getFirst(),map->getLast());glb->symboltab->setPropertyRange(props,rng);diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/database.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/database.hhindex 95bf119464..59cc8ed490 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/database.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/database.hh@@ -121,24 +121,26 @@ class SymbolEntry {/// \brief The type (derived class) of SymbolEntryprotected:enum {- map_entry = 0, ///< Is a MapEntry- conflict_entry = 1, ///< Is a MapEntryConflict- dynamic_entry = 2 ///< Is a DynamicEntry+ map_entry = 1, ///< Is a MapEntry+ conflict_entry = 2, ///< Is a MapEntryConflict+ dynamic_entry = 4, ///< Is a DynamicEntry+ unassigned_entry = 8, ///< Is an UnassignedEntry+ piece = 16 ///< Is a proper piece of whole Symbol};Symbol *symbol; ///< Symbol object being mappedRangeList uselimit; ///< Code address ranges where this storage is valid+ uint4 properties; ///< Boolean properties of \b this entryuint4 extraflags; ///< Varnode flags specific to this storage locationint4 offset; ///< Offset into the Symbol that \b this coversint4 size; ///< Number of bytes consumed by \b this (piece of the) storage- uint2 entrytype; ///< Type of SymbolEntry- bool is_piece; ///< Is \b this a piece of the whole symbolpublic:SymbolEntry(Symbol *sym); ///< Construct an uninitialized SymbolEntrySymbolEntry(Symbol *sym,uint4 exflags,int4 sz,int4 off,const RangeList &use); ///< Constructorvirtual ~SymbolEntry(void) {}- bool isPiece(void) const { return is_piece; } ///< Is \b this a proper piece of the whole Symbol- bool isDynamic(void) const { return (entrytype == dynamic_entry); } ///< Is \b storage \e dynamic- bool isConflict(void) const { return (entrytype == conflict_entry); } ///< Does storage have potential Symbol conflicts+ bool isPiece(void) const { return ((properties & piece)!=0); } ///< Is \b this a proper piece of the whole Symbol+ bool isMapEntry(void) const { return ((properties & map_entry)!=0); } ///< Is \b this a MapEntry+ bool isDynamic(void) const { return ((properties & dynamic_entry)!=0); } ///< Is \b this a DynamicEntry+ bool isConflict(void) const { return ((properties & conflict_entry)!=0); } ///< Does storage have potential Symbol conflicts/// \brief Get the data-type associated with (a piece of) \b this///@@ -177,7 +179,7 @@ protected:list<SymbolRange>::iterator mapIterator; ///< Position within containerpublic:MapEntry(Symbol *sym,uint4 exflags,const Address &ad,int4 sz,int4 off,const RangeList &use); ///< Constructor- MapEntry(Symbol *sym) : SymbolEntry(sym) {} ///< Construct and uninitialized MapEntry for use with decode()+ MapEntry(Symbol *sym) : SymbolEntry(sym) { properties |= map_entry; } ///< Construct and uninitialized MapEntry for use with decode()virtual Datatype *getSizedType(const Address &addr,int4 sz) const;virtual void printEntry(ostream &s) const;virtual void encode(Encoder &encoder) const;@@ -214,7 +216,7 @@ class DynamicEntry : public SymbolEntry {list<DynamicEntry *>::iterator dynIterator; ///< Position within containerpublic:DynamicEntry(Symbol *sym,uint4 exfl,uint8 h,int4 off,int4 sz,const RangeList &rnglist); ///< Constructor- DynamicEntry(Symbol *sym) : SymbolEntry(sym) { hash = 0; entrytype = dynamic_entry; } ///< Constructor for use with decode()+ DynamicEntry(Symbol *sym) : SymbolEntry(sym) { hash = 0; properties |= dynamic_entry; } ///< Constructor for use with decode()virtual Datatype *getSizedType(const Address &addr,int4 sz) const;virtual void printEntry(ostream &s) const;virtual void encode(Encoder &encoder) const;@@ -222,6 +224,17 @@ public:uint8 getHash(void) const { return hash; } ///< Get the hash used to identify \b this storage};+/// \brief A placeholder mapping for a Symbol that is not mapped or \e unassigned+///+class UnassignedEntry : public SymbolEntry {+public:+ UnassignedEntry(Symbol *sym); ///< Constructor+ virtual Datatype *getSizedType(const Address &addr,int4 sz) const;+ virtual void printEntry(ostream &s) const;+ virtual void encode(Encoder &encoder) const;+ virtual void decode(Decoder &decoder);+};+/// \brief The base class for a symbol in a symbol table or scope////// At its most basic, a Symbol is a \b name and a \b data-type.@@ -598,8 +611,10 @@ protected:/// \param sym is the given Symbol being mapped/// \param entry is the DynamicEntry to addvirtual void addDynamicMapInternal(Symbol *sym,DynamicEntry *entry)=0;+void addMap(MapEntry *entry); ///< Integrate a MapEntry into the range mapsvoid addDynamic(DynamicEntry *entry); ///< Integrate a DynamicEntry into the Scope+ void addUnassigned(MapEntry *entry); ///< Mark a symbol as unassignedvoid setSymbolId(Symbol *sym,uint8 id) const { sym->symbolId = id; } ///< Adjust the id associated with a symbolvoid setDisplayName(const string &nm) { displayName = nm; } ///< Change name displayed in outputpublic:diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/database_ghidra.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/database_ghidra.ccindex b067d7ca5c..87b294bc33 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/database_ghidra.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/database_ghidra.cc@@ -142,7 +142,7 @@ Symbol *ScopeGhidra::dump2Cache(Decoder &decoder) const}if (sym != (Symbol *)0) {SymbolEntry *ent = sym->getFirstWholeMap();- if (ent != (SymbolEntry *)0 && !ent->isDynamic()) {+ if (ent != (SymbolEntry *)0 && ent->isMapEntry()) {MapEntry *entry = (MapEntry *)ent;if (scope != cache) { // We have a namespace cache// With a global namespace, mark the address range as a "hole", so the same query won'tdiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.ccindex d2d623bd67..4668b2ca89 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.cc@@ -50,6 +50,7 @@ ElementId ELEM_RETPARAM = ElementId("retparam",171);ElementId ELEM_RETURNSYM = ElementId("returnsym",172);ElementId ELEM_UNAFFECTED = ElementId("unaffected",173);ElementId ELEM_INTERNAL_STORAGE = ElementId("internal_storage",286);+ElementId ELEM_SHARESTACK = ElementId("sharestack",291);/// \brief Find a ParamEntry matching the given storage Varnode///@@ -99,7 +100,9 @@ void ParamEntry::resolveJoin(list<ParamEntry> &curList)return;groupSet.clear();for(int4 i=0;i<joinrec->numPieces();++i) {- const ParamEntry *entry = findEntryByStorage(curList, joinrec->getPiece(i));+ const VarnodeData &vdat(joinrec->getPiece(i));+ if (vdat.space->getType() == IPTR_CONSTANT) continue;+ const ParamEntry *entry = findEntryByStorage(curList, vdat);if (entry != (const ParamEntry *)0) {groupSet.insert(groupSet.end(),entry->groupSet.begin(),entry->groupSet.end());// For output <pentry>, if the most signifigant part overlaps with an earlier <pentry>@@ -536,7 +539,7 @@ void ParamEntry::decode(Decoder &decoder,bool normalstack,bool grouped,list<Paraelse if (ext == "float")flags |= smallsize_floatext;else if (ext != "none")- throw LowlevelError("Bad extension attribute");+ throw LowlevelError("Bad extension attribute: "+ext);}elsethrow LowlevelError("Unknown <pentry> attribute");@@ -551,9 +554,9 @@ void ParamEntry::decode(Decoder &decoder,bool normalstack,bool grouped,list<Paraspaceid = addr.getSpace();addressbase = addr.getOffset();if (alignment != 0) {-// if ((addressbase % alignment) != 0)-// throw LowlevelError("Stack <pentry> address must match alignment");numslots = size / alignment;+ if (spaceid->getType() == IPTR_SPACEBASE)+ flags |= is_stackspill;}if (spaceid->isReverseJustified()) {if (spaceid->isBigEndian())@@ -598,7 +601,6 @@ ParamListStandard::ParamListStandard(const ParamListStandard &op2){numgroup = op2.numgroup;entry = op2.entry;- spacebase = op2.spacebase;maxdelay = op2.maxdelay;thisbeforeret = op2.thisbeforeret;autoKilledByCall = op2.autoKilledByCall;@@ -641,15 +643,10 @@ void ParamListStandard::extractTiles(vector<const ParamEntry *> &tiles,type_clasconst ParamEntry *ParamListStandard::getStackEntry(void) const{- list<ParamEntry>::const_iterator iter = entry.end();- if (iter != entry.begin()) {- --iter; // Stack entry necessarily must be the last entry- const ParamEntry &curEntry( *iter );- if (!curEntry.isExclusion() && curEntry.getSpace()->getType() == IPTR_SPACEBASE) {- return &(*iter);- }- }- return (const ParamEntry *)0;+ if (entry.empty())+ return (const ParamEntry *)0;+ const ParamEntry *res = &entry.back();+ return (res->isStackSpill()) ? res : (const ParamEntry *)0;}/// Find the (first) entry containing the given memory range@@ -772,6 +769,12 @@ uint4 ParamListStandard::assignAddress(Datatype *dt,const PrototypePieces &protovector<int4> &status,ParameterPieces &res) const{+// if (dt->isFormalZeroLength()) {+// res.addr = Address();+// res.type = dt;+// res.flags = 0;+// return AssignAction::success;+// }for(list<ModelRule>::const_iterator iter=modelRules.begin();iter!=modelRules.end();++iter) {uint4 responseCode = (*iter).assignAddress(dt, proto, pos, tlist, status, res);if (responseCode != AssignAction::fail)@@ -781,28 +784,15 @@ uint4 ParamListStandard::assignAddress(Datatype *dt,const PrototypePieces &protoreturn assignAddressFallback(store,dt,false,status,res);}-void ParamListStandard::assignMap(const PrototypePieces &proto,TypeFactory &typefactory,vector<ParameterPieces> &res) const+void ParamListStandard::allocateStatus(vector<int4> &status) const{- vector<int4> status(numgroup,0);+ status.resize(numgroup,0);+}- if (res.size() == 2) { // Check for hidden parameters defined by the output list- Datatype *dt = res.back().type;- if ((res.back().flags & ParameterPieces::hiddenretparm) != 0) {- // Need to pull from registers marked as hiddenret- if (assignAddressFallback(TYPECLASS_HIDDENRET,dt,false,status,res.back()) == AssignAction::fail) {- throw ParamUnassignedError("Cannot assign parameter address for " + res.back().type->getName());- }- }- else {- // Assign as a regular first input pointer parameter- if (assignAddress(dt,proto,0,typefactory,status,res.back()) == AssignAction::fail) {- throw ParamUnassignedError("Cannot assign parameter address for " + res.back().type->getName());- }- }-- res.back().flags |= ParameterPieces::hiddenretparm;- }+void ParamListStandard::assignMap(const PrototypePieces &proto,TypeFactory &typefactory,vector<int4> &status,+ vector<ParameterPieces> &res) const+{for(int4 i=0;i<proto.intypes.size();++i) {res.emplace_back();Datatype *dt = proto.intypes[i];@@ -1200,6 +1190,7 @@ void ParamListStandard::populateResolver(void)for(int4 i=0;i<joinRec->numPieces();++i) {// Individual pieces making up the join are mapped to the ParamEntryconst VarnodeData &vData(joinRec->getPiece(i));+ if (vData.space->getType() == IPTR_CONSTANT) continue;uintb last = vData.offset + (vData.size - 1);addResolverRange(vData.space,vData.offset,last,paramEntry,position);position += 1;@@ -1239,10 +1230,7 @@ void ParamListStandard::parsePentry(Decoder &decoder,vector<EffectRecord> &effecresourceStart.push_back(groupid);}}- AddrSpace *spc = entry.back().getSpace();- if (spc->getType() == IPTR_SPACEBASE)- spacebase = spc;- else if (autoKilledByCall) // If a register parameter AND we automatically generate killedbycall+ if (!entry.back().isStackSpill() && autoKilledByCall) // If a register parameter AND we automatically generate killedbycalleffectlist.push_back(EffectRecord(entry.back(),EffectRecord::killedbycall));int4 maxgroup = entry.back().getAllGroups().back() + 1;@@ -1435,6 +1423,15 @@ OpCode ParamListStandard::assumedExtension(const Address &addr,int4 size,Varnodereturn CPUI_COPY;}+AddrSpace *ParamListStandard::getSpacebase(void) const++{+ const ParamEntry *entry = getStackEntry();+ if (entry != (const ParamEntry *)0)+ return entry->getSpace();+ return (AddrSpace *)0;+}+void ParamListStandard::getRangeList(AddrSpace *spc,RangeList &res) const{@@ -1452,7 +1449,6 @@ void ParamListStandard::decode(Decoder &decoder,vector<EffectRecord> &effectlist{glb = decoder.getAddrSpaceManager();numgroup = 0;- spacebase = (AddrSpace *)0;int4 pointermax = 0;thisbeforeret = false;autoKilledByCall = false;@@ -1516,10 +1512,10 @@ ParamList *ParamListStandard::clone(void) constreturn res;}-void ParamListRegisterOut::assignMap(const PrototypePieces &proto,TypeFactory &typefactory,vector<ParameterPieces> &res) const+void ParamListRegisterOut::assignMap(const PrototypePieces &proto,TypeFactory &typefactory,vector<int4> &status,+ vector<ParameterPieces> &res) const{- vector<int4> status(numgroup,0);res.emplace_back();if (proto.outtype->getMetatype() != TYPE_VOID) {assignAddress(proto.outtype,proto,-1,typefactory,status,res.back());@@ -1566,11 +1562,10 @@ ParamList *ParamListRegister::clone(void) constreturn res;}-void ParamListStandardOut::assignMap(const PrototypePieces &proto,TypeFactory &typefactory,vector<ParameterPieces> &res) const+void ParamListStandardOut::assignMap(const PrototypePieces &proto,TypeFactory &typefactory,vector<int4> &status,+ vector<ParameterPieces> &res) const{- vector<int4> status(numgroup,0);-res.emplace_back();if (proto.outtype->getMetatype() == TYPE_VOID) {res.back().type = proto.outtype;@@ -1584,7 +1579,7 @@ void ParamListStandardOut::assignMap(const PrototypePieces &proto,TypeFactory &tif (responseCode == AssignAction::hiddenret_ptrparam || responseCode == AssignAction::hiddenret_specialreg ||responseCode == AssignAction::hiddenret_specialreg_void) { // Could not assign an address (too big)- AddrSpace *spc = spacebase;+ AddrSpace *spc = getSpacebase();if (spc == (AddrSpace *)0)spc = typefactory.getArch()->getDefaultDataSpace();int4 pointersize = spc->getAddrSize();@@ -1795,12 +1790,9 @@ void ParamListMerged::foldIn(const ParamListStandard &op2){if (entry.empty()) {- spacebase = op2.getSpacebase();entry = op2.getEntry();return;}- if ((spacebase != op2.getSpacebase())&&(op2.getSpacebase() != (AddrSpace *)0))- throw LowlevelError("Cannot merge prototype models with different stacks");list<ParamEntry>::const_iterator iter2;for(iter2=op2.getEntry().begin();iter2!=op2.getEntry().end();++iter2) {@@ -1943,7 +1935,7 @@ ParamActive::ParamActive(bool recoversub)isfullychecked = false;needsfinalcheck = false;recoversubcall = recoversub;- joinReverse = false;+ joinMostSigFirst = false;}void ParamActive::clear(void)@@ -1954,7 +1946,7 @@ void ParamActive::clear(void)stackplaceholder = -1;numpasses = 0;isfullychecked = false;- joinReverse = false;+ joinMostSigFirst = false;}/// A ParamTrial object is created and a slot is assigned.@@ -2206,13 +2198,7 @@ void ParameterPieces::assignAddressFromPieces(vector<VarnodeData> &pieces,bool mreverse.push_back(pieces[i]);pieces.swap(reverse);}- JoinRecord::mergeSequence(pieces,glb->translate);- if (pieces.size() == 1) {- addr = pieces[0].getAddr();- return;- }- JoinRecord *joinRecord = glb->findAddJoin(pieces, 0);- addr = joinRecord->getUnified().getAddr();+ addr = glb->joinPieces(pieces, glb->translate);}/// The type is set to \e unknown_effect@@ -2371,6 +2357,8 @@ ProtoModel::ProtoModel(const string &nm,const ProtoModel &op2)output = op2.output->clone();elseoutput = (ParamList *)0;+ for (int4 i=0;i <sharedActions.size(); ++i)+ sharedActions[i] = op2.sharedActions[i]->clone(this);effectlist = op2.effectlist;likelytrash = op2.likelytrash;@@ -2428,9 +2416,13 @@ bool ProtoModel::isCompatible(const ProtoModel *op2) constvoid ProtoModel::assignParameterStorage(const PrototypePieces &proto,vector<ParameterPieces> &res,bool ignoreOutputError){+ vector<int4> inputStatus;+ vector<int4> outputStatus;+ input->allocateStatus(inputStatus);+ output->allocateStatus(outputStatus);if (ignoreOutputError) {try {- output->assignMap(proto,*glb->types,res);+ output->assignMap(proto,*glb->types,outputStatus,res);}catch(ParamUnassignedError &err) {res.clear();@@ -2441,23 +2433,13 @@ void ProtoModel::assignParameterStorage(const PrototypePieces &proto,vector<Para}}else {- output->assignMap(proto,*glb->types,res);- }- input->assignMap(proto,*glb->types,res);-- if (hasThis && res.size() > 1) {- int4 thisIndex = 1;- if ((res[1].flags & ParameterPieces::hiddenretparm) != 0 && res.size() > 2) {- if (input->isThisBeforeRetPointer()) {- // pointer has been bumped by auto-return-storage- res[1].swapMarkup(res[2]); // must swap markup for slots 1 and 2- }- else {- thisIndex = 2;- }- }- res[thisIndex].flags |= ParameterPieces::isthis;+ output->assignMap(proto,*glb->types,outputStatus,res);}+ for(int4 i=0;i<sharedActions.size();++i)+ sharedActions[i]->applyBefore(proto, *glb->types, res, inputStatus, outputStatus);+ input->assignMap(proto,*glb->types,inputStatus,res);+ for (int4 i=0;i<sharedActions.size();++i)+ sharedActions[i]->applyAfter(proto, *glb->types, res, inputStatus, outputStatus);}/// \brief Look up an effect from the given EffectRecord list@@ -2597,6 +2579,7 @@ void ProtoModel::decode(Decoder &decoder)throw LowlevelError("Missing prototype attributes");buildParamList(strategystring); // Allocate input and output ParamLists+ sharedActions.push_back(new HiddenReturnAction(this));for(;;) {uint4 subId = decoder.peekElement();if (subId == 0) break;@@ -2656,6 +2639,11 @@ void ProtoModel::decode(Decoder &decoder)}decoder.closeElement(subId);}+ else if (subId == ELEM_SHARESTACK) {+ unique_ptr<ShareStackAction> action(new ShareStackAction(this));+ action->decode(decoder);+ sharedActions.push_back(action.release());+ }else if (subId == ELEM_LIKELYTRASH) {decoder.openElement();while(decoder.peekElement() != 0) {@@ -2993,7 +2981,7 @@ Address ParameterSymbol::getAddress(void) const{SymbolEntry *entry = sym->getFirstWholeMap();- if (entry->isDynamic())+ if (!entry->isMapEntry())return Address();return ((MapEntry *)entry)->getAddr();}@@ -3004,6 +2992,12 @@ int4 ParameterSymbol::getSize(void) constreturn sym->getFirstWholeMap()->getSize();}+bool ParameterSymbol::hasStorage(void) const++{+ return sym->getFirstWholeMap()->isMapEntry();+}+bool ParameterSymbol::isTypeLocked(void) const{@@ -3982,6 +3976,7 @@ void FuncProto::resolveExtraPop(void)int4 expop = 4; // Extrapop is at least 4 for the return addressfor(int4 i=0;i<numparams;++i) {ProtoParameter *param = getParam(i);+ if (!param->hasStorage()) continue;const Address &addr( param->getAddress() );if (addr.getSpace()->getType() != IPTR_SPACEBASE) continue;int4 cur = (int4)addr.getOffset() + param->getSize();@@ -4240,6 +4235,7 @@ int4 FuncProto::characterizeAsInputParam(const Address &addr,int4 size) constfor(int4 i=0;i<num;++i) {ProtoParameter *param = getParam(i);if (!param->isTypeLocked()) continue;+ if (!param->hasStorage()) continue;locktest = true;Address iaddr = param->getAddress();// If the parameter already exists, the varnode must be justified in the parameter relative@@ -4279,7 +4275,7 @@ int4 FuncProto::characterizeAsOutput(const Address &addr,int4 size) const{if (isOutputLocked()) {ProtoParameter *outparam = getOutput();- if (outparam->getType()->getMetatype() == TYPE_VOID)+ if (!outparam->hasStorage())return ParamEntry::no_containment;Address iaddr = outparam->getAddress();// If the output is locked, the varnode must be justified in the location relative@@ -4315,6 +4311,7 @@ bool FuncProto::possibleInputParam(const Address &addr,int4 size) constfor(int4 i=0;i<num;++i) {ProtoParameter *param = getParam(i);if (!param->isTypeLocked()) continue;+ if (!param->hasStorage()) continue;locktest = true;Address iaddr = param->getAddress();// If the parameter already exists, the varnode must be justified in the parameter relative@@ -4341,7 +4338,7 @@ bool FuncProto::possibleOutputParam(const Address &addr,int4 size) const{if (isOutputLocked()) {ProtoParameter *outparam = getOutput();- if (outparam->getType()->getMetatype() == TYPE_VOID)+ if (!outparam->hasStorage())return false;Address iaddr = outparam->getAddress();// If the output is locked, the varnode must be justified in the location relative@@ -4375,6 +4372,7 @@ bool FuncProto::unjustifiedInputParam(const Address &addr,int4 size,VarnodeDatafor(int4 i=0;i<num;++i) {ProtoParameter *param = getParam(i);if (!param->isTypeLocked()) continue;+ if (!param->hasStorage()) continue;locktest = true;Address iaddr = param->getAddress();// If the parameter already exists, test if -addr- -size- is improperly contained in param@@ -4409,6 +4407,7 @@ bool FuncProto::getBiggestContainedInputParam(const Address &loc,int4 size,Varnofor(int4 i=0;i<num;++i) {ProtoParameter *param = getParam(i);if (!param->isTypeLocked()) continue;+ if (!param->hasStorage()) continue;locktest = true;Address iaddr = param->getAddress();if (iaddr.containedBy(param->getSize(), loc, size)) {@@ -4435,7 +4434,7 @@ bool FuncProto::getBiggestContainedOutput(const Address &loc,int4 size,VarnodeDa{if (isOutputLocked()) {ProtoParameter *outparam = getOutput();- if (outparam->getType()->getMetatype() == TYPE_VOID)+ if (!outparam->hasStorage())return false;Address iaddr = outparam->getAddress();if (iaddr.containedBy(outparam->getSize(), loc, size)) {@@ -5045,7 +5044,9 @@ bool FuncCallSpecs::transferLockedInput(vector<Varnode *> &newinput,const FuncPrint4 numparams = source.numParams();Varnode *stackref = (Varnode *)0;for(int4 i=0;i<numparams;++i) {- int4 reuse = transferLockedInputParam(source.getParam(i));+ ProtoParameter *param = source.getParam(i);+ if (!param->hasStorage()) continue;+ int4 reuse = transferLockedInputParam(param);if (reuse == 0) return false;if (reuse > 0)newinput.push_back(op->getIn(reuse));@@ -5072,7 +5073,7 @@ bool FuncCallSpecs::transferLockedOutput(vector<Varnode *> &newoutput,const Func{ProtoParameter *param = source.getOutput();- if (param->getType()->getMetatype() == TYPE_VOID) {+ if (!param->hasStorage()) {return true;}transferLockedOutputParam(param,newoutput);@@ -5125,6 +5126,7 @@ void FuncCallSpecs::commitNewInputs(Funcdata &data,vector<Varnode *> &newinput)int4 numparams = numParams();for(int4 i=0;i<numparams;++i) {ProtoParameter *param = getParam(i);+ if (!param->hasStorage()) continue;Varnode *vn = buildParam(data,newinput[1+i],param,stackref);newinput[1+i] = vn;activeinput.registerTrial(param->getAddress(),param->getSize());@@ -5735,82 +5737,59 @@ Varnode *FuncCallSpecs::findPreexistingWhole(Varnode *vn1,Varnode *vn2)void FuncCallSpecs::buildOutputFromTrials(Funcdata &data,vector<Varnode *> &trialvn){- Varnode *finaloutvn;vector<Varnode *> finalvn;- for(int4 i=0;i<activeoutput.getNumTrials();++i) { // Reorder the varnodes+ for(int4 i=0;i<activeoutput.getNumTrials();++i) { // Extract the used VarnodesParamTrial &curtrial(activeoutput.getTrial(i));if (!curtrial.isUsed()) break;Varnode *vn = trialvn[ curtrial.getSlot() - 1];finalvn.push_back(vn);}- activeoutput.deleteUnusedTrials(); // This deletes unused, and renumbers used (matches finalvn)+ activeoutput.deleteUnusedTrials(); // This deletes unused, and renumbers usedif (activeoutput.getNumTrials()==0) return; // Nothing is a formal output+ if (!activeoutput.isJoinMostSigFirst() && finalvn.size() > 1) { // Make sure Varnodes listed from most to least significant+ vector<Varnode *> reverse;+ for(int4 i=finalvn.size()-1;i>=0;--i)+ reverse.push_back(finalvn[i]);+ finalvn.swap(reverse);+ }+vector<PcodeOp *> deletedops;if (activeoutput.getNumTrials()==1) { // We have a single, properly justified output- finaloutvn = finalvn[0];+ Varnode *finaloutvn = finalvn[0];PcodeOp *indop = finaloutvn->getDef();-// ParamTrial &curtrial(activeoutput.getTrial(0));-// if (finaloutvn->getSize() != curtrial.getSize()) { // If the varnode does not exactly match the original trial-// int4 res = curtrial.getEntry()->justifiedContain(finaloutvn->getAddress(),finaloutvn->getSize());-// if (res > 0) {-// data.opUninsert(indop);-// data.opSetOpcode(indop,CPUI_SUBPIECE); // Insert a subpiece-// Varnode *wholevn = data.newVarnodeOut(curtrial.getSize(),curtrial.getAddress(),op);-// data.opSetInput(indop,wholevn,0);-// data.opSetInput(indop,data.newConstant(4,res),1);-// data.opInsertAfter(indop,op);-// return;-// }-// }deletedops.push_back(indop);data.opSetOutput(op,finaloutvn); // Move varnode to its new position as output of call}- else if (activeoutput.getNumTrials()==2) {- Varnode *hivn,*lovn;- if (activeoutput.isJoinReverse()) {- hivn = finalvn[0];- lovn = finalvn[1];- }- else {- hivn = finalvn[1];- lovn = finalvn[0];- }- if (data.isDoublePrecisOn()) {- lovn->setPrecisLo(); // Mark that these varnodes are part of a larger precision whole- hivn->setPrecisHi();- }- deletedops.push_back(hivn->getDef());- deletedops.push_back(lovn->getDef());- finaloutvn = findPreexistingWhole(hivn,lovn);- if (finaloutvn == (Varnode *)0) {- Address joinaddr = data.getArch()->constructJoinAddress(data.getArch()->translate,- hivn->getAddr(),hivn->getSize(),- lovn->getAddr(),lovn->getSize());- finaloutvn = data.newVarnode(hivn->getSize()+lovn->getSize(),joinaddr);- data.opSetOutput(op,finaloutvn);- PcodeOp *sublo = data.newOp(2,op->getAddr());- data.opSetOpcode(sublo,CPUI_SUBPIECE);- data.opSetInput(sublo,finaloutvn,0);- data.opSetInput(sublo,data.newConstant(4,0),1);- data.opSetOutput(sublo,lovn);- data.opInsertAfter(sublo,op);- PcodeOp *subhi = data.newOp(2,op->getAddr());- data.opSetOpcode(subhi,CPUI_SUBPIECE);- data.opSetInput(subhi,finaloutvn,0);- data.opSetInput(subhi,data.newConstant(4,lovn->getSize()),1);- data.opSetOutput(subhi,hivn);- data.opInsertAfter(subhi,op);- }- else { // Preexisting whole- deletedops.push_back(finaloutvn->getDef()); // Its inputs are used only in this op- data.opSetOutput(op,finaloutvn);+ else {+ vector<VarnodeData> pieces;+ int4 finalSize = 0;+ for(int4 i=0;i<finalvn.size();++i) {+ Varnode *vn = finalvn[i];+ pieces.emplace_back(vn->getAddr(),vn->getSize());+ finalSize += vn->getSize();+ deletedops.push_back(vn->getDef());+ }+ Address addr = data.getArch()->joinPieces(pieces,data.getArch()->translate);+ Varnode *finaloutvn = data.newVarnode(finalSize,addr);+ data.opSetOutput(op,finaloutvn);+ int4 truncAmount = 0;+ for(int4 i=finalvn.size()-1;i>=0;--i) {+ PcodeOp *subop = data.newOp(2,op->getAddr());+ data.opSetOpcode(subop,CPUI_SUBPIECE);+ data.opSetInput(subop,finaloutvn,0);+ data.opSetInput(subop,data.newConstant(4,truncAmount),1);+ data.opSetOutput(subop,finalvn[i]); // Move output from INDIRECT to SUBPIECE+ data.opInsertAfter(subop,op);+ truncAmount += finalvn[i]->getSize();+ }+ if (finalvn.size() == 2 && data.isDoublePrecisOn()) {+ finalvn[1]->setPrecisLo(); // Mark that these varnodes are part of a larger precision whole+ finalvn[0]->setPrecisHi();}}- else- return;for(int4 i=0;i<deletedops.size();++i) { // Destroy the original INDIRECT opsPcodeOp *dop = deletedops[i];diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.hhindex c9e08cdacd..efd0d5a3e6 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.hh@@ -59,6 +59,7 @@ extern ElementId ELEM_RETPARAM; ///< Marshaling element \<retparam>extern ElementId ELEM_RETURNSYM; ///< Marshaling element \<returnsym>extern ElementId ELEM_UNAFFECTED; ///< Marshaling element \<unaffected>extern ElementId ELEM_INTERNAL_STORAGE; ///< Marshaling element \<internal_storage>+extern ElementId ELEM_SHARESTACK; ///< Marshaling element \<sharestack>/// \brief Exception thrown when a prototype can't be modeled properlystruct ParamUnassignedError : public LowlevelError {@@ -95,7 +96,8 @@ public:extracheck_low = 0x100, ///< Perform extra checks during parameter recovery on least sig portion of the doubleis_grouped = 0x200, ///< This entry is grouped with other entriesoverlapping = 0x400, ///< Overlaps an earlier entry (and doesn't consume additional resource slots)- first_storage = 0x800 ///< Entry is first in its storage class+ first_storage = 0x800, ///< Entry is first in its storage class+ is_stackspill = 0x1000 ///< Is the \e stack entry that parameter assignments spill over into};enum {no_containment, ///< Range neither contains nor is contained by a ParamEntry@@ -131,6 +133,7 @@ public:int4 getAlign(void) const { return alignment; } ///< Get the alignment of \b this entryJoinRecord *getJoinRecord(void) const { return joinrec; } ///< Get record describing joined pieces (or null if only 1 piece)type_class getType(void) const { return type; } ///< Get the data-type class associated with \b this+ bool isStackSpill(void) const { return ((flags & is_stackspill)!=0); } ///< Return \b true if \b this is the \e stack entry holding spillbool isExclusion(void) const { return (alignment==0); } ///< Return \b true if this holds a single parameter exclusivelybool isReverseStack(void) const { return ((flags & reverse_stack)!=0); } ///< Return \b true if parameters are allocated in reverse orderbool isGrouped(void) const { return ((flags & is_grouped)!=0); } ///< Return \b true if \b this is grouped with other entries@@ -293,7 +296,7 @@ class ParamActive {bool isfullychecked; ///< True if all trials are fully examined (and no new trials are expected)bool needsfinalcheck; ///< Should a final pass be made on trials (to take into account control-flow changes)bool recoversubcall; ///< True if \b this is being used to recover prototypes of a sub-function call- bool joinReverse; ///< True if varnodes should be joined in reverse order+ bool joinMostSigFirst; ///< True if trials are joined starting with the most significant piece firstpublic:ParamActive(bool recoversub); ///< Construct an empty containervoid clear(void); ///< Reset to an empty container@@ -305,8 +308,8 @@ public:int4 whichTrial(const Address &addr,int4 sz) const; ///< Get the trial overlapping with the given memory rangebool needsFinalCheck(void) const { return needsfinalcheck; } ///< Is a final check requiredvoid markNeedsFinalCheck(void) { needsfinalcheck = true; } ///< Mark that a final check is required- bool isJoinReverse(void) const { return joinReverse; } ///< Do Varnodes need to be joined in reverse order- void setJoinReverse(void) { joinReverse = true; } ///< Mark that varnodes need to be joined in reverse order+ bool isJoinMostSigFirst(void) const { return joinMostSigFirst; } ///< Return \b true if pieces are joined starting with most significant+ void setJoinMostSigFirst(void) { joinMostSigFirst = true; } ///< Mark that pieces are joined starting with the most significantbool isRecoverSubcall(void) const { return recoversubcall; } ///< Are these trials for a call to a sub-functionbool isFullyChecked(void) const { return isfullychecked; } ///< Are all trials checked with no new trials expectedvoid markFullyChecked(void) { isfullychecked = true; } ///< Mark that all trials are checked@@ -437,13 +440,20 @@ public:virtual ~ParamList(void) {} ///< Destructorvirtual uint4 getType(void) const=0; ///< Get the type of parameter list+ /// \brief Initialized the resource state for \b this to indicate all resources are available+ ///+ /// \param status is the state object+ virtual void allocateStatus(vector<int4> &status) const=0;+/// \brief Given list of data-types, map the list positions to storage locations////// If we know the function prototype, recover how parameters are actually stored using the model./// \param proto is the ordered list of data-types/// \param typefactory is the TypeFactory (for constructing pointers)+ /// \param status is the state of consumption for \b this resource/// \param res will contain the storage locations corresponding to the datatypes- virtual void assignMap(const PrototypePieces &proto,TypeFactory &typefactory,vector<ParameterPieces> &res) const=0;+ virtual void assignMap(const PrototypePieces &proto,TypeFactory &typefactory,vector<int4> &status,+ vector<ParameterPieces> &res) const=0;/// \brief Given an unordered list of storage locations, calculate a function prototype///@@ -599,7 +609,6 @@ protected:list<ParamEntry> entry; ///< The ordered list of parameter entriesvector<ParamEntryResolver *> resolverMap; ///< Map from space id to resolverlist<ModelRule> modelRules; ///< Rules to apply when assigning addresses- AddrSpace *spacebase; ///< Address space containing relative offset parametersconst AddrSpaceManager *glb; ///< Owning managerconst ParamEntry *findEntry(const Address &loc,int4 size,bool just) const; ///< Given storage location find matching ParamEntryconst ParamEntry *selectUnreferenceEntry(int4 grp,type_class prefType) const; ///< Select entry to fill an unreferenced param@@ -631,7 +640,9 @@ public:uint4 assignAddress(Datatype *dt,const PrototypePieces &proto,int4 pos,TypeFactory &tlst,vector<int4> &status,ParameterPieces &res) const;virtual uint4 getType(void) const { return p_standard; }- virtual void assignMap(const PrototypePieces &proto,TypeFactory &typefactory,vector<ParameterPieces> &res) const;+ virtual void allocateStatus(vector<int4> &status) const;+ virtual void assignMap(const PrototypePieces &proto,TypeFactory &typefactory,vector<int4> &status,+ vector<ParameterPieces> &res) const;virtual void fillinMap(ParamActive *active) const;virtual bool checkJoin(const Address &hiaddr,int4 hisize,const Address &loaddr,int4 losize) const;virtual bool checkSplit(const Address &loc,int4 size,int4 splitpoint) const;@@ -641,7 +652,7 @@ public:virtual bool getBiggestContainedParam(const Address &loc,int4 size,VarnodeData &res) const;virtual bool unjustifiedContainer(const Address &loc,int4 size,VarnodeData &res) const;virtual OpCode assumedExtension(const Address &addr,int4 size,VarnodeData &res) const;- virtual AddrSpace *getSpacebase(void) const { return spacebase; }+ virtual AddrSpace *getSpacebase(void) const;virtual bool isThisBeforeRetPointer(void) const { return thisbeforeret; }virtual void getRangeList(AddrSpace *spc,RangeList &res) const;virtual int4 getMaxDelay(void) const { return maxdelay; }@@ -667,7 +678,8 @@ public:useFillinFallback = op2.useFillinFallback; } ///< Copy constructorvoid fillinMapFallback(ParamActive *active,bool firstOnly) const;virtual uint4 getType(void) const { return p_standard_out; }- virtual void assignMap(const PrototypePieces &proto,TypeFactory &typefactory,vector<ParameterPieces> &res) const;+ virtual void assignMap(const PrototypePieces &proto,TypeFactory &typefactory,vector<int4> &status,+ vector<ParameterPieces> &res) const;virtual void fillinMap(ParamActive *active) const;virtual bool possibleParam(const Address &loc,int4 size) const;virtual void decode(Decoder &decoder,vector<EffectRecord> &effectlist,bool normalstack);@@ -686,7 +698,8 @@ public:ParamListRegisterOut(void) : ParamListStandardOut() {} ///< ConstructorParamListRegisterOut(const ParamListRegisterOut &op2) : ParamListStandardOut(op2) {} ///< Copy constructorvirtual uint4 getType(void) const { return p_register_out; }- virtual void assignMap(const PrototypePieces &proto,TypeFactory &typefactory,vector<ParameterPieces> &res) const;+ virtual void assignMap(const PrototypePieces &proto,TypeFactory &typefactory,vector<int4> &status,+ vector<ParameterPieces> &res) const;virtual ParamList *clone(void) const;};@@ -721,7 +734,8 @@ public:void foldIn(const ParamListStandard &op2); ///< Add another model to the unionvoid finalize(void) { populateResolver(); } ///< Fold-ins are finished, finalize \b thisvirtual uint4 getType(void) const { return p_merged; }- virtual void assignMap(const PrototypePieces &proto,TypeFactory &typefactory,vector<ParameterPieces> &res) const {+ virtual void assignMap(const PrototypePieces &proto,TypeFactory &typefactory,vector<int4> &status,+ vector<ParameterPieces> &res) const {throw LowlevelError("Cannot assign prototype before model has been resolved"); }virtual void fillinMap(ParamActive *active) const {throw LowlevelError("Cannot determine prototype before model has been resolved"); }@@ -757,6 +771,7 @@ class ProtoModel {int4 extrapop; ///< Extra bytes popped from stackParamList *input; ///< Resource model for input parametersParamList *output; ///< Resource model for output parameters+ vector<const SharedAction *> sharedActions; ///< Actions invoked during parameter assignmentconst ProtoModel *compatModel; ///< The model \b this is a copy ofvector<EffectRecord> effectlist; ///< List of side-effectsvector<VarnodeData> likelytrash; ///< Storage locations potentially carrying \e trash values@@ -781,6 +796,8 @@ public:virtual ~ProtoModel(void); ///< Destructorconst string &getName(void) const { return name; } ///< Get the name of the prototype modelArchitecture *getArch(void) const { return glb; } ///< Get the owning Architecture+ const ParamList *getInputResource(void) const { return input; } ///< Get input parameter resources+ const ParamList *getOutputResource(void) const { return output; } ///< Get output parameter resourcesconst ProtoModel *getAliasParent(void) const { return compatModel; } ///< Return \e model \b this is an alias of (or null)uint4 hasEffect(const Address &addr,int4 size) const; ///< Determine side-effect of \b this on the given memory rangeint4 getExtraPop(void) const { return extrapop; } ///< Get the stack-pointer \e extrapop for \b this model@@ -1110,6 +1127,7 @@ public:virtual Datatype *getType(void) const=0; ///< Get the data-type associate with \b thisvirtual Address getAddress(void) const=0; ///< Get the storage address for \b this parametervirtual int4 getSize(void) const=0; ///< Get the number of bytes occupied by \b this parameter+ virtual bool hasStorage(void) const=0; ///< Does \b this have assigned storagevirtual bool isTypeLocked(void) const=0; ///< Is the parameter data-type lockedvirtual bool isNameLocked(void) const=0; ///< Is the parameter name lockedvirtual bool isSizeTypeLocked(void) const=0; ///< Is the size of the parameter locked@@ -1179,6 +1197,7 @@ public:virtual Datatype *getType(void) const { return type; }virtual Address getAddress(void) const { return addr; }virtual int4 getSize(void) const { return type->getSize(); }+ virtual bool hasStorage(void) const { return !addr.isInvalid(); }virtual bool isTypeLocked(void) const { return ((flags&ParameterPieces::typelock)!=0); }virtual bool isNameLocked(void) const { return ((flags&ParameterPieces::namelock)!=0); }virtual bool isSizeTypeLocked(void) const { return ((flags&ParameterPieces::sizelock)!=0); }@@ -1267,6 +1286,7 @@ public:virtual Datatype *getType(void) const;virtual Address getAddress(void) const;virtual int4 getSize(void) const;+ virtual bool hasStorage(void) const;virtual bool isTypeLocked(void) const;virtual bool isNameLocked(void) const;virtual bool isSizeTypeLocked(void) const;diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_varnode.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_varnode.ccindex afa5a24914..41d9834423 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_varnode.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_varnode.cc@@ -1294,6 +1294,8 @@ Varnode *Funcdata::findLinkedVarnode(SymbolEntry *entry) constreturn (Varnode *)0;return vn;}+ if (!entry->isMapEntry())+ return (Varnode *)0;MapEntry *mapentry = (MapEntry *)entry;VarnodeLocSet::const_iterator iter,enditer;@@ -1333,7 +1335,7 @@ void Funcdata::findLinkedVarnodes(SymbolEntry *entry,vector<Varnode *> &res) conif (vn != (Varnode *)0)res.push_back(vn);}- else {+ else if (entry->isMapEntry()) {VarnodeLocSet::const_iterator iter = beginLoc(entry->getSize(),((MapEntry *)entry)->getAddr());VarnodeLocSet::const_iterator enditer = endLoc(entry->getSize(),((MapEntry *)entry)->getAddr());for(;iter!=enditer;++iter) {diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.ccindex 8035252d02..d053249640 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc@@ -2173,32 +2173,38 @@ void Heritage::splitJoinWrite(Varnode *vn,JoinRecord *joinrec)Varnode *mosthalf = nextlev[2*i];Varnode *leasthalf = nextlev[2*i+1];if (leasthalf == (Varnode *)0) continue; // Varnode didn't get split this level- PcodeOp *split;- if (vn->isInput())- split = fd->newOp(2,bb->getStart());- else- split = fd->newOp(2,op->getAddr());- fd->opSetOpcode(split,CPUI_SUBPIECE);- fd->opSetOutput(split,mosthalf);- fd->opSetInput(split,curvn,0);- fd->opSetInput(split,fd->newConstant(4,leasthalf->getSize()),1);- if (op == (PcodeOp *)0)- fd->opInsertBegin(split,bb);- else- fd->opInsertAfter(split,op);- op = split; // Keep -op- as the latest op in the split construction-- split = fd->newOp(2,op->getAddr());- fd->opSetOpcode(split,CPUI_SUBPIECE);- fd->opSetOutput(split,leasthalf);- fd->opSetInput(split,curvn,0);- fd->opSetInput(split,fd->newConstant(4,0),1);- fd->opInsertAfter(split,op);- if (isPrimitive) {- mosthalf->setPrecisHi(); // Make sure we set the precision flags to trigger "double precision" rules- leasthalf->setPrecisLo();+ if (!mosthalf->isConstant()) {+ PcodeOp *split;+ if (vn->isInput())+ split = fd->newOp(2,bb->getStart());+ else+ split = fd->newOp(2,op->getAddr());+ fd->opSetOpcode(split,CPUI_SUBPIECE);+ fd->opSetOutput(split,mosthalf);+ fd->opSetInput(split,curvn,0);+ fd->opSetInput(split,fd->newConstant(4,leasthalf->getSize()),1);+ if (op == (PcodeOp *)0)+ fd->opInsertBegin(split,bb);+ else+ fd->opInsertAfter(split,op);+ op = split; // Keep -op- as the latest op in the split construction+ }+ if (!leasthalf->isConstant()) {+ PcodeOp *split = fd->newOp(2,op->getAddr());+ fd->opSetOpcode(split,CPUI_SUBPIECE);+ fd->opSetOutput(split,leasthalf);+ fd->opSetInput(split,curvn,0);+ fd->opSetInput(split,fd->newConstant(4,0),1);+ if (op == (PcodeOp *)0)+ fd->opInsertBegin(split,bb);+ else+ fd->opInsertAfter(split,op);+ if (isPrimitive) {+ mosthalf->setPrecisHi(); // Make sure we set the precision flags to trigger "double precision" rules+ leasthalf->setPrecisLo();+ }+ op = split; // Keep -op- as the latest op in the split construction}- op = split; // Keep -op- as the latest op in the split construction}lastcombo.clear();@@ -2281,6 +2287,7 @@ void Heritage::processJoins(void)if (joinrec->getUnified().size != vn->getSize())throw LowlevelError("Joined varnode does not match size of record");if (vn->isFree()) {+ if (vn->hasNoDescend()) continue; // Its possible vn is deadif (joinrec->isFloatExtension())floatExtensionRead(vn,joinrec);elsediff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.ccindex 73fb5701a3..7e0bd70a28 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.cc@@ -1258,7 +1258,7 @@ AttributeId ATTRIB_WORDSIZE = AttributeId("wordsize",26);AttributeId ATTRIB_STORAGE = AttributeId("storage",149);AttributeId ATTRIB_STACKSPILL = AttributeId("stackspill",150);-AttributeId ATTRIB_UNKNOWN = AttributeId("XMLunknown",159); // Number serves as next open index+AttributeId ATTRIB_UNKNOWN = AttributeId("XMLunknown",161); // Number serves as next open indexElementId ELEM_DATA = ElementId("data",1);@@ -1272,6 +1272,6 @@ ElementId ELEM_VAL = ElementId("val",8);ElementId ELEM_VALUE = ElementId("value",9);ElementId ELEM_VOID = ElementId("void",10);-ElementId ELEM_UNKNOWN = ElementId("XMLunknown",291); // Number serves as next open index+ElementId ELEM_UNKNOWN = ElementId("XMLunknown",292); // Number serves as next open index} // End namespace ghidradiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/merge.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/merge.ccindex c9023e8efb..8a0493ccb4 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/merge.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/merge.cc@@ -1541,6 +1541,39 @@ void Merge::markInternalCopies(void)#endif}+/// \brief Mark COPYs to \e join space padding+///+/// Search for any COPY of #0 to a Varnode with an address marked a pure padding+/// and mark the COPY as \e non-printing.+void Merge::markPurePaddingCopies(void)++{+ AddrSpaceManager *glb = data.getArch();+ set<JoinRecord *,JoinRecordCompare>::const_iterator iter = glb->beginJoinPadding();+ while(iter != glb->endJoin()) {+ JoinRecord *joinRec = *iter;+ if (!joinRec->isPurePadding()) break;+ ++iter;+ Address addr = joinRec->getUnified().getAddr();+ int4 size = joinRec->getUnified().size;+ VarnodeLocSet::const_iterator viter = data.beginLoc(size,addr);+ VarnodeLocSet::const_iterator venditer = data.endLoc(size,addr);+ while(viter != venditer) {+ Varnode *vn = *viter;+ ++viter;+ if (vn->isWritten()) {+ PcodeOp *copy = vn->getDef();+ if (copy->code() == CPUI_COPY) {+ Varnode *cvn = copy->getIn(0);+ if (cvn->isConstant() && cvn->getOffset() == 0) {+ data.opMarkNonPrinting(copy);+ }+ }+ }+ }+ }+}+/// \brief Register an unmapped CONCAT stack with the merge process////// The given Varnode must be the root of a tree of CPUI_PIECE operations as produced bydiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/merge.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/merge.hhindex 4ff5c24802..91d63b90b7 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/merge.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/merge.hh@@ -132,6 +132,7 @@ public:bool hideShadows(HighVariable *high);void processCopyTrims(void);void markInternalCopies(void);+ void markPurePaddingCopies(void);void registerProtoPartialRoot(Varnode *vn);#ifdef MERGEMULTI_DEBUGvoid verifyHighCovers(void);diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/modelrules.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/modelrules.ccindex 612f1db483..c8cfffd21b 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/modelrules.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/modelrules.cc@@ -19,12 +19,15 @@namespace ghidra {AttributeId ATTRIB_SIZES = AttributeId("sizes",151);+AttributeId ATTRIB_BACKFILL = AttributeId("backfill", 152);AttributeId ATTRIB_MAX_PRIMITIVES = AttributeId("maxprimitives", 153);AttributeId ATTRIB_REVERSESIGNIF = AttributeId("reversesignif", 154);AttributeId ATTRIB_MATCHSIZE = AttributeId("matchsize", 155);AttributeId ATTRIB_AFTER_BYTES = AttributeId("afterbytes", 156);AttributeId ATTRIB_AFTER_STORAGE = AttributeId("afterstorage", 157);AttributeId ATTRIB_FILL_ALTERNATE = AttributeId("fillalternate", 158);+AttributeId ATTRIB_MINELEMENTS = AttributeId("minelements", 159);+AttributeId ATTRIB_MAXELEMENTS = AttributeId("maxelements", 160);ElementId ELEM_DATATYPE = ElementId("datatype",273);ElementId ELEM_CONSUME = ElementId("consume",274);@@ -144,13 +147,14 @@ bool PrimitiveExtractor::handleUnion(TypeUnion *dt,int4 max,int4 offset)int4 num = dt->numDepend();if (num == 0)return false;+ bool isArrayPrimitive = (flags & array_primitive) != 0;const TypeField *curField = dt->getField(0);- PrimitiveExtractor common(curField->type,false,offset + curField->offset,max);+ PrimitiveExtractor common(curField->type,false,isArrayPrimitive,offset + curField->offset,max);if (!common.isValid())return false;for(int4 i=1;i<num;++i) {curField = dt->getField(i);- PrimitiveExtractor next(curField->type,false,offset + curField->offset,max);+ PrimitiveExtractor next(curField->type,false,isArrayPrimitive,offset + curField->offset,max);if (!next.isValid())return false;if (!commonRefinement(common.primitives,next.primitives))@@ -175,8 +179,9 @@ bool PrimitiveExtractor::handleUnion(TypeUnion *dt,int4 max,int4 offset)/// \param dt is the given data-type to extract primitives from/// \param max is the maximum number of primitives to extract before giving up/// \param offset is the starting offset to associate with the first primitive+/// \param depth is the current depth of recursion/// \return \b true if all primitives were extracted-bool PrimitiveExtractor::extract(Datatype *dt,int4 max,int4 offset)+bool PrimitiveExtractor::extract(Datatype *dt,int4 max,int4 offset,int4 depth){switch(dt->getMetatype()) {@@ -196,10 +201,16 @@ bool PrimitiveExtractor::extract(Datatype *dt,int4 max,int4 offset)return true;case TYPE_ARRAY:{+ if ((flags & array_primitive) != 0 && depth != 0) {+ if (primitives.size() >= max)+ return false;+ primitives.emplace_back(dt,offset);+ return true;+ }int4 numEls = ((TypeArray *)dt)->numElements();Datatype *base = ((TypeArray *)dt)->getBase();for(int4 i=0;i<numEls;++i) {- if (!extract(base,max,offset))+ if (!extract(base,max,offset,depth+1))return false;offset += base->getAlignSize();}@@ -228,7 +239,7 @@ bool PrimitiveExtractor::extract(Datatype *dt,int4 max,int4 offset)if (expectedOff != curOff) {flags |= extra_space;}- if (!extract(compDt,max,curOff))+ if (!extract(compDt,max,curOff,depth+1))return false;expectedOff = curOff + compDt->getAlignSize();}@@ -237,13 +248,16 @@ bool PrimitiveExtractor::extract(Datatype *dt,int4 max,int4 offset)/// \param dt is data-type extract from/// \param unionIllegal is \b true if unions encountered during extraction are considered illegal+/// \param arrayPrimitive is \b true if arrays should be treated as primitives/// \param offset is the starting offset to associate with the data-type/// \param max is the maximum number of primitives to extract before giving up-PrimitiveExtractor::PrimitiveExtractor(Datatype *dt,bool unionIllegal,int offset,int4 max)+PrimitiveExtractor::PrimitiveExtractor(Datatype *dt,bool unionIllegal,bool arrayPrimitive,int offset,int4 max){flags = unionIllegal ? union_invalid : 0;- if (!extract(dt,max,offset))+ if (arrayPrimitive)+ flags |= array_primitive;+ if (!extract(dt,max,offset,0))flags |= invalid;}@@ -264,7 +278,10 @@ DatatypeFilter *DatatypeFilter::decodeFilter(Decoder &decoder)else {// If no other name matches, assume this is a metatypetype_metatype meta = string2metatype(nm);- filter.reset(new MetaTypeFilter(meta));+ if (meta == TYPE_ARRAY)+ filter.reset(new ArrayFilter());+ else+ filter.reset(new MetaTypeFilter(meta));}filter->decode(decoder);decoder.closeElement(elemId);@@ -388,6 +405,48 @@ bool MetaTypeFilter::filter(Datatype *dt) constreturn filterOnSize(dt);}+ArrayFilter::ArrayFilter(int4 min,int4 max,int4 minEl,int4 maxEl)+ : SizeRestrictedFilter(min,max)+{+ minElements = minEl;+ maxElements = maxEl;+}++ArrayFilter::ArrayFilter(const ArrayFilter &op2)+ : SizeRestrictedFilter(op2)+{+ minElements = op2.minElements;+ maxElements = op2.maxElements;+}++bool ArrayFilter::filter(Datatype *dt) const++{+ if (dt->getMetatype() != TYPE_ARRAY) return false;+ if (!filterOnSize(dt)) return false;+ TypeArray *arr = (TypeArray *)dt;+ if (arr->numElements() < minElements)+ return false;+ if (maxElements != 0 && arr->numElements() > maxElements)+ return false;+ return true;+}++void ArrayFilter::decode(Decoder &decoder)++{+ SizeRestrictedFilter::decode(decoder);+ decoder.rewindAttributes();+ for(;;) {+ uint4 attribId = decoder.getNextAttributeId();+ if (attribId == 0) break;+ if (attribId == ATTRIB_MINELEMENTS)+ minElements = decoder.readUnsignedInteger();+ else if (attribId == ATTRIB_MAXELEMENTS)+ maxElements = decoder.readUnsignedInteger();+ }+}+HomogeneousAggregate::HomogeneousAggregate(type_metatype meta){@@ -415,7 +474,7 @@ bool HomogeneousAggregate::filter(Datatype *dt) consttype_metatype meta = dt->getMetatype();if (meta != TYPE_ARRAY && meta != TYPE_STRUCT)return false;- PrimitiveExtractor primitives(dt,true,0,maxPrimitives);+ PrimitiveExtractor primitives(dt,true,false,0,maxPrimitives);if (!primitives.isValid() || primitives.size() == 0 || primitives.containsUnknown()|| !primitives.isAligned() || primitives.containsHoles())return false;@@ -430,18 +489,18 @@ bool HomogeneousAggregate::filter(Datatype *dt) const}void HomogeneousAggregate::decode(Decoder &decoder)+{- SizeRestrictedFilter::decode(decoder);- decoder.rewindAttributes();- for(;;) {- uint4 attribId = decoder.getNextAttributeId();- if (attribId == 0) break;- if (attribId == ATTRIB_MAX_PRIMITIVES) {- uint4 xmlMaxPrim = decoder.readUnsignedInteger();- if (xmlMaxPrim > 0) maxPrimitives = xmlMaxPrim;- }- }-+ SizeRestrictedFilter::decode(decoder);+ decoder.rewindAttributes();+ for(;;) {+ uint4 attribId = decoder.getNextAttributeId();+ if (attribId == 0) break;+ if (attribId == ATTRIB_MAX_PRIMITIVES) {+ uint4 xmlMaxPrim = decoder.readUnsignedInteger();+ if (xmlMaxPrim > 0) maxPrimitives = xmlMaxPrim;+ }+ }}/// If the next element is a qualifier filter, decode it from the stream and return it.@@ -609,7 +668,7 @@ AssignAction *AssignAction::decodeAction(Decoder &decoder,const ParamListStandaraction.reset(new HiddenReturnAssign(res,hiddenret_specialreg));}else if (elemId == ELEM_JOIN_PER_PRIMITIVE) {- action.reset(new MultiMemberAssign(TYPECLASS_GENERAL,false,res->isBigEndian(),res));+ action.reset(new MultiMemberAssign(TYPECLASS_GENERAL,true,false,res->isBigEndian(),res));}else if (elemId == ELEM_JOIN_DUAL_CLASS) {action.reset(new MultiSlotDualAssign(res));@@ -782,6 +841,44 @@ void ConvertToPointer::decode(Decoder &decoder)decoder.closeElement(elemId);}+/// Test if a data-type of the given size will fit starting at a particular entry within+/// the resource list. If necessary, check+/// 1) If the type will be properly aligned+/// 2) If there are enough remaining registers, up to the end of the resource list, to+/// cover the data-type and that have not been consumed.+///+/// \param iter is the first resource entry to use for the data-type+/// \param sizeLeft initially holds the size of the data-type to cover in bytes+/// \param align is the alignment requirement for the data-type+/// \param resourcesConsumed is the number of bytes in resources already consumed/skipped+/// \param tmpStatus is the current consumption status for the resource list+/// \return \b true if the data-type will fit+bool MultiSlotAssign::checkFit(int4 iter, int4 sizeLeft, int4 align, int4 resourcesConsumed,vector<int4> &tmpStatus) const++{+ const ParamEntry *entry = tiles[iter];+ if (tmpStatus[entry->getGroup()] != 0) {+ return false;+ }+ if (enforceAlignment) {+ int4 regSize = entry->getSize();+ if (align > regSize && (resourcesConsumed % align) != 0) {+ return false;+ }+ }+ if (!adjacentEntries) {+ return true;+ }+ while (iter != tiles.size() && sizeLeft > 0) {+ entry = tiles[iter];+ if (tmpStatus[entry->getGroup()] != 0) {+ return false;+ }+ sizeLeft -= entry->getSize();+ }+ return true;+}+/// Find the first ParamEntry matching the \b resourceType, and the ParamEntry/// corresponding to the \e stack if \b consumeFromStack is set.void MultiSlotAssign::initializeEntries(void)@@ -809,6 +906,8 @@ MultiSlotAssign::MultiSlotAssign(const ParamListStandard *res)consumeMostSig = false;enforceAlignment = false;justifyRight = false;+ adjacentEntries = true;+ allowBackfill = false;if (isBigEndian) {consumeMostSig = true;justifyRight = true;@@ -816,7 +915,8 @@ MultiSlotAssign::MultiSlotAssign(const ParamListStandard *res)stackEntry = (const ParamEntry *)0;}-MultiSlotAssign::MultiSlotAssign(type_class store,bool stack,bool mostSig,bool align,bool justRight,const ParamListStandard *res)+MultiSlotAssign::MultiSlotAssign(type_class store,bool stack,bool mostSig,bool align,bool justRight,bool backfill,+ const ParamListStandard *res): AssignAction(res){resourceType = store;@@ -826,6 +926,8 @@ MultiSlotAssign::MultiSlotAssign(type_class store,bool stack,bool mostSig,bool aconsumeMostSig = mostSig;enforceAlignment = align;justifyRight = justRight;+ adjacentEntries = true;+ allowBackfill = backfill;stackEntry = (const ParamEntry *)0;initializeEntries();}@@ -838,19 +940,18 @@ uint4 MultiSlotAssign::assignAddress(Datatype *dt,const PrototypePieces &proto,iint4 sizeLeft = dt->getSize();int4 align = dt->getAlignment();int4 iter = 0;- if (enforceAlignment) {- int4 resourcesConsumed = 0;- while(iter != tiles.size()) {- const ParamEntry *entry = tiles[iter];- if (tmpStatus[entry->getGroup()] == 0) { // Not consumed- int4 regSize = entry->getSize();- if (align <= regSize || (resourcesConsumed % align) == 0)- break;- tmpStatus[entry->getGroup()] = -1; // Consume unaligned register- }- resourcesConsumed += entry->getSize();- ++iter;+ int4 resourcesConsumed = 0;+ while(iter != tiles.size()) {+ if (checkFit(iter, sizeLeft, align, resourcesConsumed, tmpStatus)) {+ break;}+ const ParamEntry *entry = tiles[iter];+ if (!allowBackfill) {+ tmpStatus[entry->getGroup()] = -1; // Consume unaligned register+ }++ resourcesConsumed += entry->getSize();+ ++iter;}while(sizeLeft > 0 && iter != tiles.size()) {const ParamEntry *entry = tiles[iter];@@ -860,10 +961,7 @@ uint4 MultiSlotAssign::assignAddress(Datatype *dt,const PrototypePieces &proto,iint4 trialSize = entry->getSize();Address addr = entry->getAddrBySlot(tmpStatus[entry->getGroup()], trialSize,align,resource->getManager());tmpStatus[entry->getGroup()] = -1; // Consume the register- pieces.push_back(VarnodeData());- pieces.back().space = addr.getSpace();- pieces.back().offset = addr.getOffset();- pieces.back().size = trialSize;+ pieces.emplace_back(addr,trialSize);sizeLeft -= trialSize;align = 1; // Treat remaining partial pieces as having no alignment requirement}@@ -875,10 +973,7 @@ uint4 MultiSlotAssign::assignAddress(Datatype *dt,const PrototypePieces &proto,iAddress addr = stackEntry->getAddrBySlot(tmpStatus[grp],sizeLeft,align,justifyRight,resource->getManager());if (addr.isInvalid())return fail;- pieces.push_back(VarnodeData());- pieces.back().space = addr.getSpace();- pieces.back().offset = addr.getOffset();- pieces.back().size = sizeLeft;+ pieces.emplace_back(addr,sizeLeft);}else if (sizeLeft < 0) { // Have odd data-type sizeif (resourceType == TYPECLASS_FLOAT && pieces.size() == 1) {@@ -948,7 +1043,7 @@ bool MultiSlotAssign::fillinOutputMap(ParamActive *active) const}if (count==0) return false;if (consumeMostSig)- active->setJoinReverse();+ active->setJoinMostSigFirst();return true;}@@ -976,17 +1071,38 @@ void MultiSlotAssign::decode(Decoder &decoder)else if (attribId == ATTRIB_STACKSPILL) {consumeFromStack = decoder.readBool();}+ else if (attribId == ATTRIB_BACKFILL) {+ allowBackfill = decoder.readBool();+ }}decoder.closeElement(elemId);initializeEntries(); // Need new firstIter}-MultiMemberAssign::MultiMemberAssign(type_class store,bool stack,bool mostSig,const ParamListStandard *res)+/// \param pieces is the set of pieces+/// \param pad is the number bytes of the padding element+/// \return \b true if the padding request was met+bool MultiMemberAssign::addPadding(vector<VarnodeData> &pieces,int4 pad) const++{+ if (pad == 0)+ return true;+ if (pad < 0)+ return false;++ pieces.emplace_back(constSpace,0,pad);+ return true;+}++MultiMemberAssign::MultiMemberAssign(type_class store,bool recurse,bool stack,bool mostSig,const ParamListStandard *res): AssignAction(res){+ constSpace = res->getManager()->getConstantSpace();resourceType = store;+ isRecursive = recurse;consumeFromStack = stack;consumeMostSig = mostSig;+ stackEntry = res->getStackEntry();fillinOutputActive = true;}@@ -995,19 +1111,47 @@ uint4 MultiMemberAssign::assignAddress(Datatype *dt,const PrototypePieces &proto{vector<int4> tmpStatus = status;vector<VarnodeData> pieces;- PrimitiveExtractor primitives(dt,false,0,16);+ PrimitiveExtractor primitives(dt,false,isRecursive,0,16);if (!primitives.isValid() || primitives.size() == 0 || primitives.containsUnknown()|| !primitives.isAligned() || primitives.containsHoles())return fail;ParameterPieces param;- for(int4 i=0;i<primitives.size();++i) {- Datatype *curType = primitives.get(i).dt;- if (resource->assignAddressFallback(resourceType, curType, !consumeFromStack, tmpStatus,param) == fail)+ if (isRecursive) { // Recursive assignments+ if (primitives.size() == 1) {+ if (primitives.get(0).dt == dt)+ return fail; // Prevent infinite recursion+ }+ int4 size = 0;+ int4 before = (stackEntry != (const ParamEntry *)0) ? tmpStatus[stackEntry->getGroup()] : -1;+ for (int4 i = 0; i < primitives.size(); ++i) {+ const PrimitiveExtractor::Primitive &primitive(primitives.get(i));+ if (resource->assignAddress(primitive.dt, proto, pos, tlist, tmpStatus,param) == fail)+ return fail;+ if (!consumeFromStack && stackEntry != (const ParamEntry *)0) {+ if (before != tmpStatus[stackEntry->getGroup()])+ return fail;+ }+ if (!addPadding(pieces, primitive.offset - size))+ return fail;+ size = primitive.offset + primitive.dt->getSize();+ pieces.emplace_back(param.addr,primitive.dt->getSize());+ }+ if (!addPadding(pieces, dt->getSize() - size))+ return fail;+ }+ else { // Assignment from a specific resourceType+ int4 size = 0;+ for (int4 i = 0; i < primitives.size(); ++i) {+ const PrimitiveExtractor::Primitive &primitive(primitives.get(i));+ if (resource->assignAddressFallback(resourceType, primitive.dt, !consumeFromStack,tmpStatus,param) == fail)+ return fail;+ if (!addPadding(pieces, primitive.offset - size))+ return fail;+ size = primitive.offset + primitive.dt->getSize();+ pieces.emplace_back(param.addr,primitive.dt->getSize());+ }+ if (!addPadding(pieces, dt->getSize() - size))return fail;- pieces.push_back(VarnodeData());- pieces.back().space = param.addr.getSpace();- pieces.back().offset = param.addr.getOffset();- pieces.back().size = curType->getSize();}status = tmpStatus; // Commit resource usage for all the pieces@@ -1020,6 +1164,8 @@ uint4 MultiMemberAssign::assignAddress(Datatype *dt,const PrototypePieces &protobool MultiMemberAssign::fillinOutputMap(ParamActive *active) const{+ if (isRecursive)+ return false;int4 count = 0;int4 curGroup = -1;for(int4 i=0;i<active->getNumTrials();++i) {@@ -1043,19 +1189,21 @@ bool MultiMemberAssign::fillinOutputMap(ParamActive *active) const}if (count==0) return false;if (consumeMostSig)- active->setJoinReverse();+ active->setJoinMostSigFirst();return true;}void MultiMemberAssign::decode(Decoder &decoder){+ isRecursive = true;uint4 elemId = decoder.openElement(ELEM_JOIN_PER_PRIMITIVE);for(;;) {uint4 attribId = decoder.getNextAttributeId();if (attribId == 0) break;if (attribId == ATTRIB_STORAGE) {resourceType = string2typeclass(decoder.readString());+ isRecursive = false;}}decoder.closeElement(elemId);@@ -1175,7 +1323,7 @@ MultiSlotDualAssign::MultiSlotDualAssign(type_class baseStore,type_class altStoruint4 MultiSlotDualAssign::assignAddress(Datatype *dt,const PrototypePieces &proto,int4 pos,TypeFactory &tlist,vector<int4> &status,ParameterPieces &res) const{- PrimitiveExtractor primitives(dt,false,0,1024);+ PrimitiveExtractor primitives(dt,false,false,0,1024);if (!primitives.isValid() || primitives.size() == 0 || primitives.containsHoles())return fail;int4 primitiveIndex = 0;@@ -1212,10 +1360,7 @@ uint4 MultiSlotDualAssign::assignAddress(Datatype *dt,const PrototypePieces &proint4 trialSize = entry->getSize();Address addr = entry->getAddrBySlot(tmpStatus[entry->getGroup()], trialSize,1,resource->getManager());tmpStatus[entry->getGroup()] = -1; // Consume the register- pieces.push_back(VarnodeData());- pieces.back().space = addr.getSpace();- pieces.back().offset = addr.getOffset();- pieces.back().size = trialSize;+ pieces.emplace_back(addr,trialSize);sizeLeft -= trialSize;}if (sizeLeft > 0) {@@ -1226,10 +1371,7 @@ uint4 MultiSlotDualAssign::assignAddress(Datatype *dt,const PrototypePieces &proAddress addr = stackEntry->getAddrBySlot(tmpStatus[grp],sizeLeft,align,justifyRight,resource->getManager());if (addr.isInvalid())return fail;- pieces.push_back(VarnodeData());- pieces.back().space = addr.getSpace();- pieces.back().offset = addr.getOffset();- pieces.back().size = sizeLeft;+ pieces.emplace_back(addr,sizeLeft);}if (sizeLeft < 0) { // Have odd data-type sizejustifyPieces(pieces, -sizeLeft, isBigEndian, consumeMostSig, justifyRight);@@ -1295,7 +1437,7 @@ bool MultiSlotDualAssign::fillinOutputMap(ParamActive *active) const}if (count==0) return false;if (consumeMostSig)- active->setJoinReverse();+ active->setJoinMostSigFirst();return true;}@@ -1710,4 +1852,124 @@ void ModelRule::decode(Decoder &decoder,const ParamListStandard *res)decoder.closeElement(elemId);}+HiddenReturnAction::HiddenReturnAction(const ProtoModel *m)+ : SharedAction(m)+{+ inputResource = (const ParamListStandard *) m->getInputResource();+}++void HiddenReturnAction::applyBefore(const PrototypePieces &proto,TypeFactory &tlist,vector<ParameterPieces> ¶ms,+ vector<int4> &inputStatus,vector<int4> &outputStatus) const+{+ if (params.size() == 2) { // Check for hidden parameters defined by the output list+ ParameterPieces &last(params[params.size() - 1]);+ if ((last.flags & ParameterPieces::hiddenretparm)!=0) {+ // Need to pull from registers marked as hiddenret+ inputResource->assignAddressFallback(TYPECLASS_HIDDENRET, last.type, false,inputStatus, last);+ }+ else {+ // Assign as a regular first input pointer parameter+ inputResource->assignAddress(last.type, proto, 0, tlist, inputStatus, last);+ }+ last.flags |= ParameterPieces::hiddenretparm ;+ }+}++void HiddenReturnAction::applyAfter(const PrototypePieces &proto,TypeFactory &tlist,vector<ParameterPieces> ¶ms,+ vector<int4> &inputStatus,vector<int4> &outputStatus) const+{+ if (model->hasThisPointer() && params.size() > 1) {+ int4 thisIndex = 1;+ if ((params[1].flags & ParameterPieces::hiddenretparm)!=0 && params.size() > 2) {+ if (inputResource->isThisBeforeRetPointer()) {+ // pointer has been bumped by auto-return-storage+ params[1].swapMarkup(params[2]); // must swap storage and position for slots 1 and 2+ }+ else {+ thisIndex = 2;+ }+ }+ params[thisIndex].flags |= ParameterPieces::isthis ;+ }+}++SharedAction *HiddenReturnAction::clone(ProtoModel *newModel) const++{+ return new HiddenReturnAction(newModel);+}++ShareStackAction::ShareStackAction(const ProtoModel *m)+ : SharedAction(m)+{+ const ParamList *inputResource = m->getInputResource();+ const ParamList *outputResource = m->getOutputResource();+ inputStackEntry = ((const ParamListStandard *) inputResource)->getStackEntry();+ outputStackEntry = ((const ParamListStandard *) outputResource)->getStackEntry();+ inputFirst = false;+ extraAlign = 0;+}++void ShareStackAction::applyBefore(const PrototypePieces &proto,TypeFactory &tlist,vector<ParameterPieces> ¶ms,+ vector<int4> &inputStatus,vector<int4> &outputStatus) const+{+ if (inputFirst)+ return;+ // Treat output consumed stack as input consumed stack+ int4 slotnum = outputStatus[outputStackEntry->getGroup()];+ if (extraAlign > inputStackEntry->getAlign()) {+ int4 tmp = (slotnum * inputStackEntry->getAlign()) % extraAlign;+ if (tmp != 0)+ slotnum += (extraAlign - tmp) / inputStackEntry->getAlign();+ }+ inputStatus[inputStackEntry->getGroup()] = slotnum;+}++void ShareStackAction::applyAfter(const PrototypePieces &proto,TypeFactory &tlist,vector<ParameterPieces> ¶ms,+ vector<int4> &inputStatus,vector<int4> &outputStatus) const+{+ if (!inputFirst)+ return;+ if (params.empty())+ return;+ ParameterPieces &outEntry(params[0]);+ if (outEntry.addr.isInvalid() || outEntry.addr.getSpace() != outputStackEntry->getSpace())+ return;++ int4 slotnum = inputStatus[inputStackEntry->getGroup()];+ if (extraAlign > inputStackEntry->getAlign()) {+ int4 tmp = (slotnum * inputStackEntry->getAlign()) % extraAlign;+ if (tmp != 0)+ slotnum += (extraAlign - tmp) / inputStackEntry->getAlign();+ }+ outEntry.addr = inputStackEntry->getAddrBySlot(slotnum, outEntry.type->getSize(),outEntry.type->getAlignment(),+ model->getArch());+}++SharedAction *ShareStackAction::clone(ProtoModel *newModel) const++{+ return new ShareStackAction(newModel);+}++void ShareStackAction::decode(Decoder &decoder)++{+ uint4 mainid = decoder.openElement(ELEM_SHARESTACK);+ inputFirst = false;+ extraAlign = 0;+ for(;;) {+ uint4 attribId = decoder.getNextAttributeId();+ if (attribId == 0) break;+ else if (attribId == ATTRIB_FIRST) {+ string pos = decoder.readString();+ inputFirst = pos == "input";+ }+ else if (attribId == ATTRIB_ALIGN) {+ extraAlign = decoder.readUnsignedInteger();+ }+ }+ decoder.closeElement(mainid);+}+} // End namespace ghidradiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/modelrules.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/modelrules.hhindex 572a133be4..d2310046cd 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/modelrules.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/modelrules.hh@@ -23,19 +23,22 @@namespace ghidra {+class ProtoModel;class ParameterPieces;class ParamListStandard;class ParamEntry;class ParamActive;extern AttributeId ATTRIB_SIZES; ///< Marshaling attribute "sizes"+extern AttributeId ATTRIB_BACKFILL; ///< Marshaling attribute "backfill"extern AttributeId ATTRIB_MAX_PRIMITIVES; ///< Marshaling attribute "maxprimitives"extern AttributeId ATTRIB_REVERSESIGNIF; ///< Marshaling attribute "reversesignif"extern AttributeId ATTRIB_MATCHSIZE; ///< Marshaling attribute "matchsize"extern AttributeId ATTRIB_AFTER_BYTES; ///< Marshaling attribute "afterbytes"extern AttributeId ATTRIB_AFTER_STORAGE; ///< Marshaling attribute "afterstorage"-extern AttributeId ATTRIB_FILL_ALTERNATE; ///< Marshalling attribute "fillalternate"-+extern AttributeId ATTRIB_FILL_ALTERNATE; ///< Marshaling attribute "fillalternate"+extern AttributeId ATTRIB_MINELEMENTS; ///< Marshaling attribute "minelements"+extern AttributeId ATTRIB_MAXELEMENTS; ///< Marshaling attribute "maxelements"extern ElementId ELEM_DATATYPE; ///< Marshaling element \<datatype>extern ElementId ELEM_CONSUME; ///< Marshaling element \<consume>extern ElementId ELEM_CONSUME_EXTRA; ///< Marshaling element \<consume_extra>@@ -61,7 +64,8 @@ class PrimitiveExtractor {unaligned = 2, ///< At least one primitive is not properly alignedextra_space = 4, ///< Data-type contains empty space not attributable to alignment paddinginvalid = 8, ///< Data-type exceeded maximum or contained illegal elements- union_invalid = 16 ///< Unions are treated as an illegal element+ union_invalid = 16, ///< Unions are treated as an illegal element+ array_primitive = 32 ///< Arrays are treated as primitive};public:/// \brief A primitive data-type and its offset within the containing data-type@@ -77,9 +81,9 @@ private:int4 checkOverlap(vector<Primitive> &res,vector<Primitive> &small,int4 point,Primitive &big);bool commonRefinement(vector<Primitive> &first,vector<Primitive> &second);bool handleUnion(TypeUnion *dt,int4 max,int4 offset); ///< Add primitives representing a union data-type- bool extract(Datatype *dt,int4 max,int4 offset); ///< Extract list of primitives from given data-type+ bool extract(Datatype *dt,int4 max,int4 offset,int4 depth); ///< Extract list of primitives from given data-typepublic:- PrimitiveExtractor(Datatype *dt,bool unionIllegal,int4 offset,int4 max); ///< Constructor+ PrimitiveExtractor(Datatype *dt,bool unionIllegal,bool arrayPrimitive,int4 offset,int4 max); ///< Constructorint4 size(void) const { return primitives.size(); } ///< Return the number of primitives extractedconst Primitive &get(int4 i) const { return primitives[i]; } ///< Get a particular primitivebool isValid(void) const { return (flags & invalid) == 0; } ///< Return \b true if primitives were successfully extracted@@ -150,6 +154,21 @@ public:virtual bool filter(Datatype *dt) const;};+/// \brief Filter on an array data-type+///+/// Filters on TYPE_ARRAY specifically. Additional filtering on size and number of elements can be configured.+class ArrayFilter : public SizeRestrictedFilter {+ int4 minElements; ///< Minimum number of elements in the array+ int4 maxElements; ///< Maximum number of elements+public:+ ArrayFilter(void) { minElements = 0; maxElements = 0; } ///< Constructor for use with decode()+ ArrayFilter(int4 min,int4 max,int4 minEl,int4 maxEl); ///< Constructor+ ArrayFilter(const ArrayFilter &op2); ///< Copy constructor+ virtual DatatypeFilter *clone(void) const { return new ArrayFilter(*this); }+ virtual bool filter(Datatype *dt) const;+ virtual void decode(Decoder &decoder);+};+/// \brief Filter on a homogeneous aggregate data-type////// All primitive data-types must be the same.@@ -361,14 +380,19 @@ class MultiSlotAssign : public AssignAction {bool consumeMostSig; ///< True if resources are consumed starting with most significant bytesbool enforceAlignment; ///< True if register resources are discarded to match alignmentbool justifyRight; ///< True if initial bytes are padding for odd data-type sizes+ bool adjacentEntries; ///< True if an assignment should only consume adjacent entries in the list+ bool allowBackfill; ///< True if entries skipped for alignment can be reused for later paramsvector<const ParamEntry *> tiles; ///< List of registers that can be joinedconst ParamEntry *stackEntry; ///< The stack resource+ bool checkFit(int4 iter,int4 sizeLeft,int4 align,int4 resourcesConsumed,vector<int4> &tmpStatus) const;void initializeEntries(void); ///< Cache specific ParamEntry needed by the actionpublic:MultiSlotAssign(const ParamListStandard *res); ///< Constructor for use with decode- MultiSlotAssign(type_class store,bool stack,bool mostSig,bool align,bool justRight,const ParamListStandard *res); ///< Constructor+ MultiSlotAssign(type_class store,bool stack,bool mostSig,bool align,bool justRight,bool backfill,+ const ParamListStandard *res); ///< Constructorvirtual AssignAction *clone(const ParamListStandard *newResource) const {- return new MultiSlotAssign(resourceType,consumeFromStack,consumeMostSig,enforceAlignment,justifyRight,newResource); }+ return new MultiSlotAssign(resourceType,consumeFromStack,consumeMostSig,enforceAlignment,justifyRight,allowBackfill,+ newResource); }virtual uint4 assignAddress(Datatype *dt,const PrototypePieces &proto,int4 pos,TypeFactory &tlist,vector<int4> &status,ParameterPieces &res) const;virtual bool fillinOutputMap(ParamActive *active) const;@@ -378,16 +402,22 @@ public:/// \brief Consume a register per primitive member of an aggregate data-type////// The data-type is split up into its underlying primitive elements, and each one-/// is assigned a register from the specific resource list. There must be no padding between-/// elements. No packing of elements into a single register occurs.+/// is assigned storage as if it were a separate parameter, or alternately assigned a+/// register from a specific resource list. The storage elements are returned together+/// as a single Address in \e join space. Constant zeroes are used as placeholders for any+/// padding between elements. No packing of elements into a single register occurs.class MultiMemberAssign : public AssignAction {- type_class resourceType; ///< Resource list from which to consume+ type_class resourceType; ///< Resource list from which to consume (if not recursive)+ bool isRecursive; ///< \b true if primitives are assigned recursivelybool consumeFromStack; ///< True if resources should be consumed from the stackbool consumeMostSig; ///< True if resources are consumed starting with most significant bytes+ const ParamEntry *stackEntry; ///< Parameter Entry corresponding to the stack+ AddrSpace *constSpace; ///< Address space used for padding+ bool addPadding(vector<VarnodeData> &pieces,int4 pad) const; ///< Add a padding element to the set of piecespublic:- MultiMemberAssign(type_class store,bool stack,bool mostSig,const ParamListStandard *res); ///< Constructor+ MultiMemberAssign(type_class store,bool recurse,bool stack,bool mostSig,const ParamListStandard *res); ///< Constructorvirtual AssignAction *clone(const ParamListStandard *newResource) const {- return new MultiMemberAssign(resourceType,consumeFromStack,consumeMostSig,newResource); }+ return new MultiMemberAssign(resourceType,isRecursive,consumeFromStack,consumeMostSig,newResource); }virtual uint4 assignAddress(Datatype *dt,const PrototypePieces &proto,int4 pos,TypeFactory &tlist,vector<int4> &status,ParameterPieces &res) const;virtual bool fillinOutputMap(ParamActive *active) const;@@ -553,6 +583,84 @@ public:void decode(Decoder &decoder,const ParamListStandard *res); ///< Decode \b this rule from stream};+/// \brief An action that happens during parameter allocation+///+/// It sees resource usage for both input and output parameters and can make changes.+/// It can act either right after output parameter assignment or after input parameter assignment.+class SharedAction {+protected:+ const ProtoModel *model; ///< The model owning/performing \b this action+public:+ SharedAction(const ProtoModel *m) { model = m; } ///< Constructor+ virtual ~SharedAction(void) {} ///< Destructor++ /// \brief Implement resource sharing between the input and output parameter assignments+ ///+ /// Called once before input parameters have been assigned (after output assignment).+ /// \param proto is the list of data-types+ /// \param tlist is the data-type factory+ /// \param params is the current set of allocated parameters+ /// \param inputStatus is the consume status for this (input resources)+ /// \param outputStatus is the consume status for output resources+ virtual void applyBefore(const PrototypePieces &proto,TypeFactory &tlist,vector<ParameterPieces> ¶ms,+ vector<int4> &inputStatus,vector<int4> &outputStatus) const=0;++ /// \brief Implement resource sharing after input and output parameter assignments+ ///+ /// Called once after both input and output parameters have been assigned.+ /// \param proto is the list of data-types+ /// \param tlist is the data-type factory+ /// \param params is the current set of allocated parameters+ /// \param inputStatus is the consume status for this (input resources)+ /// \param outputStatus is the consume status for output resources+ virtual void applyAfter(const PrototypePieces &proto,TypeFactory &tlist,vector<ParameterPieces> ¶ms,+ vector<int4> &inputStatus,vector<int4> &outputStatus) const=0;++ /// \brief Clone \b this action onto a new prototype model+ ///+ /// \param newModel is the new model+ /// \return the cloned action+ virtual SharedAction *clone(ProtoModel *newModel) const=0;++ /// \brief Decode the action from a stream+ ///+ /// \param decoder is the stream+ virtual void decode(Decoder &decoder)=0;+};++/// \brief Action that allocates a pointer, required for the output, from the input resources.+class HiddenReturnAction : public SharedAction {+ const ParamListStandard *inputResource; ///< Input parameter resources+public:+ HiddenReturnAction(const ProtoModel *m); ///< Constructor++ virtual void applyBefore(const PrototypePieces &proto,TypeFactory &tlist,vector<ParameterPieces> ¶ms,+ vector<int4> &inputStatus,vector<int4> &outputStatus) const;+ virtual void applyAfter(const PrototypePieces &proto,TypeFactory &tlist,vector<ParameterPieces> ¶ms,+ vector<int4> &inputStatus,vector<int4> &outputStatus) const;+ virtual SharedAction *clone(ProtoModel *newModel) const;+ virtual void decode(Decoder &decoder) {}+};++/// \brief Parameter assignment action that lets input and output parameters share stack space+///+/// Any input stack allocations can happen either before or after any output stack allocation.+/// The action can optionally add an extra stack alignment between the first allocations and second allocations.+class ShareStackAction : public SharedAction {+ const ParamEntry *inputStackEntry; ///< Stack resource for input parameters+ const ParamEntry *outputStackEntry; ///< Stack resource for output parameters+ bool inputFirst; ///< \b true if input stack populated before output stack+ int4 extraAlign; ///< Extra alignment added between first stack allocations and second+public:+ ShareStackAction(const ProtoModel *m); ///< Constructor+ virtual void applyBefore(const PrototypePieces &proto,TypeFactory &tlist,vector<ParameterPieces> ¶ms,+ vector<int4> &inputStatus,vector<int4> &outputStatus) const;+ virtual void applyAfter(const PrototypePieces &proto,TypeFactory &tlist,vector<ParameterPieces> ¶ms,+ vector<int4> &inputStatus,vector<int4> &outputStatus) const;+ virtual SharedAction *clone(ProtoModel *newModel) const;+ virtual void decode(Decoder &decoder);+};+/// If the assign action could produce the trials as return value storage, return \b true/// \param active is the set of trials/// \return \b true if the trials form a return valuediff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/paramid.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/paramid.ccindex 935f2fc2db..3de0841960 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/paramid.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/paramid.cc@@ -192,6 +192,7 @@ ParamIDAnalysis::ParamIDAnalysis( Funcdata *fd_in, bool justproto )int4 num = fproto.numParams();for(int4 i=0;i<num;++i) {ProtoParameter *param = fproto.getParam(i);+ if (!param->hasStorage()) continue;InputParamMeasures.push_back( ParamMeasure(param->getAddress(),param->getSize(),param->getType(),ParamMeasure::INPUT) );Varnode *vn = fd->findVarnodeInput(param->getSize(),param->getAddress());@@ -200,7 +201,7 @@ ParamIDAnalysis::ParamIDAnalysis( Funcdata *fd_in, bool justproto )}ProtoParameter *outparam = fproto.getOutput();- if (!outparam->getAddress().isInvalid()) { // If we don't have a void type+ if (outparam->hasStorage()) { // If we don't have a void typeOutputParamMeasures.push_back( ParamMeasure( outparam->getAddress(),outparam->getSize(),outparam->getType(),ParamMeasure::OUTPUT) );list<PcodeOp *>::const_iterator rtn_iter = fd->beginOp( CPUI_RETURN );diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/pcoderaw.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/pcoderaw.hhindex b3e48b5379..778ddb2e17 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/pcoderaw.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/pcoderaw.hh@@ -36,6 +36,10 @@ struct VarnodeData {AddrSpace *space; ///< The address spaceuintb offset; ///< The offset within the spaceuint4 size; ///< The number of bytes in the location++ VarnodeData(void) {} ///< Construct uninitialized+ VarnodeData(AddrSpace *spc,uintb off,uint4 sz) { space = spc; offset = off; size = sz; } ///< Constructor+ VarnodeData(const Address &addr,int4 sz) { space = addr.getSpace(); offset = addr.getOffset(); size = sz; } ///< Construct from Addressbool operator<(const VarnodeData &op2) const; ///< An ordering for VarnodeDatabool operator==(const VarnodeData &op2) const; ///< Compare for equalitybool operator!=(const VarnodeData &op2) const; ///< Compare for inequality@@ -57,6 +61,9 @@ struct VarnodeData {/// Is \b this contiguous (as the most significant piece) with the given VarnodeDatabool isContiguous(const VarnodeData &lo) const;++ /// Print a description of \b this memory region to a stream for debugging/logging+ void printRaw(ostream &s) const;};/// VarnodeData can be sorted in terms of the space its in@@ -102,6 +109,12 @@ inline AddrSpace *VarnodeData::getSpaceFromConst(void) const {return (AddrSpace *)(uintp)offset;}+/// \param s is the stream+inline void VarnodeData::printRaw(ostream &s) const {+ Address addr(space,offset);+ addr.printRaw(s,size);+}+/// \brief A low-level representation of a single pcode operation////// This is just the minimum amount of data to represent a pcode operationdiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.ccindex 735484399e..19b3496d84 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc@@ -787,6 +787,8 @@ Varnode *RulePullsubMulti::buildSubpiece(Varnode *basevn,uint4 outsize,uint4 shiuint4 skipleft = shift;for(int4 i=joinrec->numPieces()-1;i>=0;--i) { // Move from least significant to mostconst VarnodeData &vdata(joinrec->getPiece(i));+ if (vdata.space->getType() == IPTR_CONSTANT)+ throw LowlevelError("Join space padding is propagating");if (skipleft >= vdata.size) {skipleft -= vdata.size;}@@ -7356,7 +7358,7 @@ Datatype *RulePieceStructure::determineDatatype(Varnode *vn,int4 &baseOffset)if (ct->getSize() != vn->getSize()) { // vn is a partialSymbolEntry *entry = vn->getSymbolEntry();- if (entry->isDynamic())+ if (!entry->isMapEntry())return (Datatype *)0;baseOffset = vn->getAddr().overlap(0,((MapEntry *)entry)->getAddr(),ct->getSize());if (baseOffset < 0)diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/space.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/space.ccindex 6a8cc94d63..bc3c3c0bb6 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/space.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/space.cc@@ -467,13 +467,16 @@ JoinSpace::JoinSpace(AddrSpaceManager *m,const Translate *t,int4 ind)int4 JoinSpace::overlapJoin(uintb offset,int4 size,AddrSpace *pointSpace,uintb pointOffset,int4 pointSkip) const{+if (this == pointSpace) {- // If the point is in the join space, translate the point into the piece address space- JoinRecord *pieceRecord = manage->findJoin(pointOffset);- int4 pos;- Address addr = pieceRecord->getEquivalentAddress(pointOffset + pointSkip, pos);- pointSpace = addr.getSpace();- pointOffset = addr.getOffset();+ // If the point is in the join space, we can treat it as a normal overlap+ pointOffset = pointSpace->wrapOffset(pointOffset + pointSkip);+ if (pointOffset < offset)+ return -1;+ uintb diff = pointOffset - offset;+ if (diff >= size)+ return -1;+ return (int4)diff;}else {if (pointSpace->getType() == IPTR_CONSTANT)@@ -612,7 +615,10 @@ void JoinSpace::printRaw(ostream &s,uintb offset) constszsum += vdat.size;if (i!=0)s << ',';- vdat.space->printRaw(s,vdat.offset);+ if (vdat.space->getType() == IPTR_CONSTANT)+ s << "pad:" << dec << vdat.size;+ else+ vdat.printRaw(s);}if (num == 1) {szsum = rec->getUnified().size;diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/space.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/space.hhindex 75b24463e5..ce5e50b8a2 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/space.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/space.hh@@ -233,7 +233,7 @@ public:////// Some logical variables are split across non-contiguous regions of memory. This space/// creates a virtual place for these logical variables to exist. Any memory location within this-/// space is backed by 2 or more memory locations in other spaces that physically hold the pieces+/// space is backed (usually by 2 or more) memory locations in other spaces that physically hold the pieces/// of the logical value. The database controlling symbols is responsible for keeping track of/// mapping the logical address in this space to its physical pieces. Offsets into this space do not/// have an absolute meaning, the database may vary what offset is assigned to what set of pieces.diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/subflow.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/subflow.ccindex 84fd99a260..5769939f8d 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/subflow.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/subflow.cc@@ -2591,7 +2591,7 @@ void SplitDatatype::buildOutConcats(Varnode *rootVn,PcodeOp *previousOp,vector<Vif (i<=0) break;preOp = concatOp;int4 sz = vn->getSize() + outVarnodes[i]->getSize();- Address addr = outVarnodes[i]->getAddr();+ Address addr = baseAddr + (rootVn->getSize() - sz);addr.renormalize(sz);vn = data.newVarnodeOut(sz,addr,concatOp);if (!addressTied)diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/translate.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/translate.ccindex 12c6a245d1..6d8df699a4 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/translate.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/translate.cc@@ -135,12 +135,14 @@ void SpacebaseSpace::decode(Decoder &decoder)}/// The \e join space range maps to the underlying pieces in a natural endian aware way.-/// Given an offset in the range, figure out what address it is mapping to.-/// The particular piece is passed back as an index, and the Address is returned.+/// Given an offset in the range, figure out what address it is mapping to and return it.+/// The particular piece is passed back as an index, and the number of bytes that \b offset is+/// into the piece is passed back. If the offset is outside the range an \e invalid Address is returned./// \param offset is the offset within \b this range to map/// \param pos will hold the passed back piece index+/// \param trunc will hold the number of bytes \b offset is into its piece/// \return the Address mapped to-Address JoinRecord::getEquivalentAddress(uintb offset,int4 &pos) const+Address JoinRecord::getEquivalentAddress(uintb offset,int4 &pos,int4 &trunc) const{if (offset < unified.offset)@@ -166,6 +168,9 @@ Address JoinRecord::getEquivalentAddress(uintb offset,int4 &pos) constif (pos < 0)return Address(); // offset comes after this range}+ trunc = smallOff;+ if (pieces[pos].space->getType() == IPTR_CONSTANT)+ return Address(pieces[pos].space,0);return Address(pieces[pos].space,pieces[pos].offset + smallOff);}@@ -174,20 +179,16 @@ Address JoinRecord::getEquivalentAddress(uintb offset,int4 &pos) constbool JoinRecord::operator<(const JoinRecord &op2) const{- // Some joins may have same piece but different unified size (floating point)- if (unified.size != op2.unified.size) // Compare size first- return (unified.size < op2.unified.size);+ if (pieces.size() != op2.pieces.size())+ return pieces.size() < op2.pieces.size(); // Less pieces comes first// Lexigraphic sort on pieces- int4 i=0;- for(;;) {- if (pieces.size()==i) {- return (op2.pieces.size()>i); // If more pieces in op2, it is bigger (return true), if same number this==op2, return false- }- if (op2.pieces.size()==i) return false; // More pieces in -this-, so it is bigger, return false+ for(int4 i=0;i<pieces.size();++i) {if (pieces[i] != op2.pieces[i])return (pieces[i] < op2.pieces[i]);- i += 1;}+ // All pieces are equal+ // Some joins may have same piece but different unified size (floating point)+ return (unified.size < op2.unified.size);}/// Assuming the given list of VarnodeData go from most significant to least significant,@@ -198,23 +199,21 @@ bool JoinRecord::operator<(const JoinRecord &op2) constvoid JoinRecord::mergeSequence(vector<VarnodeData> &seq,const Translate *trans){- int4 i=1;- while(i<seq.size()) {+ int4 i;+ for(i=1;i<seq.size();++i) {VarnodeData &hi(seq[i-1]);VarnodeData &lo(seq[i]);- if (hi.isContiguous(lo))+ if (hi.space->getType() != IPTR_CONSTANT && hi.isContiguous(lo))break;- i += 1;}if (i >= seq.size()) return;vector<VarnodeData> res;- i = 1;res.push_back(seq.front());bool lastIsInformal = false;- while(i<seq.size()) {+ for(i=1;i<seq.size();++i) {VarnodeData &hi(res.back());VarnodeData &lo(seq[i]);- if (hi.isContiguous(lo)) {+ if (hi.space->getType() != IPTR_CONSTANT && hi.isContiguous(lo)) {hi.offset = hi.space->isBigEndian() ? hi.offset : lo.offset;hi.size += lo.size;if (hi.space->getType() != IPTR_SPACEBASE) {@@ -226,7 +225,6 @@ void JoinRecord::mergeSequence(vector<VarnodeData> &seq,const Translate *trans)break;res.push_back(lo);}- i += 1;}if (lastIsInformal) // If the merge contains an informal registerreturn; // throw it out and keep the original sequence@@ -689,7 +687,7 @@ JoinRecord *AddrSpaceManager::findAddJoin(const vector<VarnodeData> &pieces,uint// If -logicalsize- is 0, calculate logical size as sum of piecesif (pieces.size() == 0)throw LowlevelError("Cannot create a join without pieces");- if ((pieces.size()==1)&&(logicalsize==0))+ if (pieces.size()==1 && logicalsize==0 && pieces[0].space->getType() != IPTR_CONSTANT)throw LowlevelError("Cannot create a single piece join without a logical size");uint4 totalsize;@@ -716,7 +714,7 @@ JoinRecord *AddrSpaceManager::findAddJoin(const vector<VarnodeData> &pieces,uintreturn *iter;JoinRecord *newjoin = new JoinRecord();- newjoin->pieces = pieces;+ newjoin->pieces.swap(testnode.pieces);uint4 roundsize = (totalsize + 15) & ~((uint4)0xf); // Next biggest multiple of 16@@ -776,6 +774,33 @@ JoinRecord *AddrSpaceManager::findJoin(uintb offset) constthrow LowlevelError("Unlinked join address");}+/// If the pieces are contiguous, the starting address of the first piece is returned.+/// Otherwise, an address is created in the \e join space to represent logical whole+/// and that is returned.+/// \param pieces are the given pieces+/// \param trans is the language to use for register info+/// \return the representative address+Address AddrSpaceManager::joinPieces(vector<VarnodeData> &pieces,const Translate *trans) const++{+ JoinRecord::mergeSequence(pieces,trans);+ if (pieces.size() == 1) {+ return pieces[0].getAddr();+ }+ JoinRecord *joinRecord = findAddJoin(pieces, 0);+ return joinRecord->getUnified().getAddr();+}++/// \return an iterator to the first JoinRecord marked isPurePadding() in the sorted list.+set<JoinRecord *,JoinRecordCompare>::const_iterator AddrSpaceManager::beginJoinPadding(void) const++{+ JoinRecord testJoin;+ testJoin.pieces.emplace_back(constantspace,0,~((uint4)0)); // Biggest possible padding+ testJoin.unified.size = 1;+ return splitset.lower_bound(&testJoin);+}+/// Set the number of passes for a specific AddrSpace before deadcode removal is allowed/// for that space./// \param spc is the AddrSpace to change@@ -833,45 +858,11 @@ Address AddrSpaceManager::constructJoinAddress(const Translate *translate,const Address &hiaddr,int4 hisz,const Address &loaddr,int4 losz) const{- spacetype hitp = hiaddr.getSpace()->getType();- spacetype lotp = loaddr.getSpace()->getType();- bool usejoinspace = true;- if (((hitp != IPTR_SPACEBASE)&&(hitp != IPTR_PROCESSOR))||- ((lotp != IPTR_SPACEBASE)&&(lotp != IPTR_PROCESSOR)))- throw LowlevelError("Trying to join in appropriate locations");- if ((hitp == IPTR_SPACEBASE)||(lotp == IPTR_SPACEBASE)||- (hiaddr.getSpace() == getDefaultCodeSpace())||- (loaddr.getSpace() == getDefaultCodeSpace()))- usejoinspace = false;- if (hiaddr.isContiguous(hisz,loaddr,losz)) { // If we are contiguous- if (!usejoinspace) { // and in a mappable space, just return the earliest address- if (hiaddr.isBigEndian())- return hiaddr;- return loaddr;- }- else { // If we are in a non-mappable (register) space, check to see if a parent register exists- if (hiaddr.isBigEndian()) {- if (translate->getRegisterName(hiaddr.getSpace(),hiaddr.getOffset(),(hisz+losz)).size() != 0)- return hiaddr;- }- else {- if (translate->getRegisterName(loaddr.getSpace(),loaddr.getOffset(),(hisz+losz)).size() != 0)- return loaddr;- }- }- }// Otherwise construct a formal JoinRecordvector<VarnodeData> pieces;- pieces.emplace_back();- pieces.emplace_back();- pieces[0].space = hiaddr.getSpace();- pieces[0].offset = hiaddr.getOffset();- pieces[0].size = hisz;- pieces[1].space = loaddr.getSpace();- pieces[1].offset = loaddr.getOffset();- pieces[1].size = losz;- JoinRecord *join = findAddJoin(pieces,0);- return join->getUnified().getAddr();+ pieces.emplace_back(hiaddr,hisz);+ pieces.emplace_back(loaddr,losz);+ return joinPieces(pieces, translate);}/// Check if the address space allows wrapped ranges. If so, construct a \e joined address@@ -922,19 +913,18 @@ void AddrSpaceManager::renormalizeJoinAddress(Address &addr,int4 size) constthrow LowlevelError("Join address not covered by a JoinRecord");if (addr.getOffset() == joinRecord->unified.offset && size == joinRecord->unified.size)return; // JoinRecord matches perfectly, no change necessary- int4 pos1;- Address addr1 = joinRecord->getEquivalentAddress(addr.getOffset(), pos1);- int4 pos2;- Address addr2 = joinRecord->getEquivalentAddress(addr.getOffset() + (size-1), pos2);- if (addr2.isInvalid())+ int4 pos1,sizeTrunc1;+ Address addr1 = joinRecord->getEquivalentAddress(addr.getOffset(), pos1, sizeTrunc1);+ int4 pos2,sizeTrunc2;+ Address addr2 = joinRecord->getEquivalentAddress(addr.getOffset() + (size-1), pos2, sizeTrunc2);+ if (addr1.isInvalid() || addr2.isInvalid())throw LowlevelError("Join address range not covered");- if (pos1 == pos2) {+ if (pos1 == pos2 && !addr1.isConstant()) {addr = addr1;return;}vector<VarnodeData> newPieces;- int4 sizeTrunc1 = (int4)(addr1.getOffset() - joinRecord->pieces[pos1].offset);- int4 sizeTrunc2 = joinRecord->pieces[pos2].size - (int4)(addr2.getOffset() - joinRecord->pieces[pos2].offset) - 1;+ sizeTrunc2 = joinRecord->pieces[pos2].size - sizeTrunc2 - 1;if (pos2 < pos1) { // Little endiannewPieces.push_back(joinRecord->pieces[pos2]);diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/translate.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/translate.hhindex 09018f491e..32101193fd 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/translate.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/translate.hh@@ -192,17 +192,20 @@ public:////// The decompiler can describe a logical value that is stored split across multiple/// physical memory locations. This record describes such a split. The pieces must be listed-/// from \e most \e significant to \e least \e significant.+/// from \e most \e significant to \e least \e significant. Pieces can be any addressable region in the+/// processor model. Internally a zero constant with size represents formal padding (to model structures+/// with aligned fields that are passed in registers).class JoinRecord {friend class AddrSpaceManager;vector<VarnodeData> pieces; ///< All the physical pieces of the symbol, most significant to leastVarnodeData unified; ///< Special entry representing entire symbol in one chunkpublic:int4 numPieces(void) const { return pieces.size(); } ///< Get number of pieces in this record- bool isFloatExtension(void) const { return (pieces.size() == 1); } ///< Does this record extend a float varnode+ bool isFloatExtension(void) const { return pieces.size() == 1 && pieces[0].space->getType() != IPTR_CONSTANT; } ///< Does this record extend a float varnode+ bool isPurePadding(void) const { return pieces.size() == 1 && pieces[0].space->getType() == IPTR_CONSTANT; } ///< Is \b this only paddingconst VarnodeData &getPiece(int4 i) const { return pieces[i]; } ///< Get the i-th piececonst VarnodeData &getUnified(void) const { return unified; } ///< Get the Varnode whole- Address getEquivalentAddress(uintb offset,int4 &pos) const; ///< Given offset in \e join space, get equivalent address of piece+ Address getEquivalentAddress(uintb offset,int4 &pos,int4 &trunc) const; ///< Given offset in \e join space, get equivalent address of piecebool operator<(const JoinRecord &op2) const; ///< Compare records lexigraphically by piecesstatic void mergeSequence(vector<VarnodeData> &seq,const Translate *trans); ///< Merge any contiguous ranges in a sequence};@@ -272,6 +275,9 @@ public:AddrSpace *getNextSpaceInOrder(AddrSpace *spc) const; ///< Get the next \e contiguous address spaceJoinRecord *findAddJoin(const vector<VarnodeData> &pieces,uint4 logicalsize) const; ///< Get (or create) JoinRecord for \e piecesJoinRecord *findJoin(uintb offset) const; ///< Find JoinRecord for \e offset in the join space+ Address joinPieces(vector<VarnodeData> &pieces,const Translate *trans) const; ///< Create an Address representing given \e pieces+ set<JoinRecord *,JoinRecordCompare>::const_iterator beginJoinPadding(void) const; ///< Beginning of pure padding JoinRecords+ set<JoinRecord *,JoinRecordCompare>::const_iterator endJoin(void) const { return splitset.end(); } ///< End of JoinRecord listvoid setDeadcodeDelay(AddrSpace *spc,int4 delaydelta); ///< Set the deadcodedelay for a specific spacevoid truncateSpace(const TruncationTag &tag); ///< Mark a space as truncated from its original sizediff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/variable.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/variable.ccindex 9b9d9e8f5a..c20ac6c24c 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/variable.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/variable.cc@@ -260,6 +260,8 @@ void HighVariable::setSymbol(Varnode *vn) const}else if (entry->isDynamic()) // Dynamic symbols (that aren't partials) match whole variablesymboloffset = -1;+ else if (!entry->isMapEntry())+ symboloffset = -1;else if (symbol->getCategory() == Symbol::equate)symboloffset = -1; // For equates, we don't care about sizeelse if (symbol->getType()->getSize() == vn->getSize() &&diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.ccindex 4fe7abd530..e9df4ad553 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.cc@@ -430,7 +430,7 @@ void ScopeLocal::collectNameRecs(void)// If the "this" pointer points to a class, try to preserve the data-type// even though the symbol is not preserved.SymbolEntry *entry = sym->getFirstWholeMap();- if (!entry->isDynamic())+ if (entry->isMapEntry())addTypeRecommendation(((MapEntry *)entry)->getAddr(), dt);}}@@ -1594,7 +1594,7 @@ SymbolEntry *ScopeLocal::remapSymbol(Symbol *sym,const Address &addr,const Addre{SymbolEntry *entry = sym->getFirstWholeMap();int4 size = entry->getSize();- if (!entry->isDynamic()) {+ if (entry->isMapEntry()) {if (((MapEntry *)entry)->getAddr() == addr) {if (usepoint.isInvalid() && entry->getFirstUseAddress().isInvalid())return entry;@@ -1744,7 +1744,7 @@ void ScopeLocal::addRecommendName(Symbol *sym)if (entry->isDynamic()) {dynRecommend.emplace_back(entry->getFirstUseAddress(), ((DynamicEntry *)entry)->getHash(), sym->getName(), sym->getId());}- else {+ else if (entry->isMapEntry()) {Address usepoint((AddrSpace *)0,0);if (!entry->getUseLimit().empty()) {const Range *range = entry->getUseLimit().getFirstRange();diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.ccindex 51a0751b5d..437606a8f4 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.cc@@ -726,37 +726,6 @@ Address Varnode::getUsePoint(const Funcdata &fd) const// return loc.getSpace()->getTrans()->constant(0);}-/// Print to the stream either the name of the Varnode, such as a register name, if it exists-/// or print a shortcut character representing the AddrSpace and a hex representation of the offset.-/// This function also computes and returns the \e expected size of the identifier it prints-/// to facilitate the printing of size modifiers by other print routines-/// \param s is the output stream-/// \return the expected size-int4 Varnode::printRawNoMarkup(ostream &s) const--{- AddrSpace *spc = loc.getSpace();- const Translate *trans = spc->getTrans();- string name;- int4 expect;-- name = trans->getRegisterName(spc,loc.getOffset(),size);- if (name.size()!=0) {- const VarnodeData &point(trans->getRegister(name));- uintb off = loc.getOffset()-point.offset;- s << name;- expect = point.size;- if (off != 0)- s << '+' << dec << off;- }- else {- s << loc.getShortcut(); // Print type shortcut character- expect = trans->getDefaultSize();- loc.printRaw(s);- }- return expect;-}-/// Print textual information about this Varnode including a base identifier along with enough/// size and attribute information to uniquely identify the Varnode within a text SSA listing/// In particular, the identifiers have either "i" or defining op SeqNum information appendeddiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.hhindex a04b619e14..f7aa598a02 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.hh@@ -214,7 +214,7 @@ public:void clearConsumeVacuous(void) { addlflags &= ~Varnode::vacconsume; } ///< Clear marker used by dead-code algorithmPcodeOp *loneDescend(void) const; ///< Return unique reading PcodeOp, or \b null if there are zero or more than 1Address getUsePoint(const Funcdata &fd) const; ///< Get Address when this Varnode first comes into scope- int4 printRawNoMarkup(ostream &s) const; ///< Print a simple identifier for the Varnode+ int4 printRawNoMarkup(ostream &s) const { return loc.printRaw(s,size); } ///< Print a simple identifier for the Varnodevoid printRaw(ostream &s) const; ///< Print a simple identifier plus additional info identifying Varnode with SSA formvoid printCover(ostream &s) const; ///< Print raw coverage info about the Varnodevoid printInfo(ostream &s) const; ///< Print raw attribute info about the Varnodediff --git a/Ghidra/Features/Decompiler/src/decompile/datatests/wraprange.xml b/Ghidra/Features/Decompiler/src/decompile/datatests/wraprange.xmlindex e9319228f0..72d4310836 100644--- a/Ghidra/Features/Decompiler/src/decompile/datatests/wraprange.xml+++ b/Ghidra/Features/Decompiler/src/decompile/datatests/wraprange.xml@@ -14,6 +14,6 @@<com>print raw</com><com>quit</com></script>-<stringmatch name="Wrapped range #1" min="1" max="1">j\{0x00000000,0xfffffffffffffffc\}.* = CONCAT44\(s0x00000000:4\(i\),s0xfffffffffffffffc:4\(i\)\)</stringmatch>-<stringmatch name="Wrapped range #2" min="1" max="1">RAX\(.*\) = j\{0x00000000,0xfffffffffffffffc\}</stringmatch>+<stringmatch name="Wrapped range #1" min="1" max="1">j\{s0x00000000,s0xfffffffffffffffc\}.* = CONCAT44\(s0x00000000:4\(i\),s0xfffffffffffffffc:4\(i\)\)</stringmatch>+<stringmatch name="Wrapped range #2" min="1" max="1">RAX\(.*\) = j\{s0x00000000,s0xfffffffffffffffc\}</stringmatch></decompilertest>diff --git a/Ghidra/Features/Decompiler/src/decompile/unittests/testparamstore.cc b/Ghidra/Features/Decompiler/src/decompile/unittests/testparamstore.ccindex 1c1ef1f4b7..5857bf7035 100644--- a/Ghidra/Features/Decompiler/src/decompile/unittests/testparamstore.cc+++ b/Ghidra/Features/Decompiler/src/decompile/unittests/testparamstore.cc@@ -145,6 +145,17 @@ void ParamStoreEnvironment::parseStore(ProtoModel *model,const string &name,VarnparseJoin(model,name,res);return;}+ else if (name.compare(0,4,"pad:",4) == 0) {+ int4 sz = 0;+ istringstream s(name.substr(4));+ s >> dec >> sz;+ if (sz != 0) {+ res.space = model->getArch()->getConstantSpace();+ res.offset = 0;+ res.size = sz;+ return;+ }+ }string regname;size_t pos = name.find(':');int4 sz = 0;@@ -168,6 +179,14 @@ void ParamStoreEnvironment::parseStores(ProtoModel *model,vector<VarnodeData> &r{size_t pos = 0;for(;;) {+ while(pos < names.size()) {+ char c = names[pos];+ if (c != ' ' && c != '\t' && c != '\n')+ break;+ pos += 1;+ }+ if (pos >= names.size())+ return;size_t nextpos = names.find(',',pos);string el;if (nextpos == string::npos)@@ -258,6 +277,30 @@ TEST(paramstore_x64) {ASSERT(theEnviron.test(model, "doubleintintint func(void);", "RAX,RDI"));}+TEST(paramstore_x64_go) {+ ProtoModel *model = theEnviron.getModel("x86:LE:64:default:golang","abi-internal");+ ASSERT(theEnviron.test(model, "void func(int2 a,int8 b,char c);", "void,AX,RBX,CL"));+ ASSERT(theEnviron.test(model, "void func(float4 a,float4 b);", "void,XMM0:4,XMM1:4"));+ ASSERT(theEnviron.test(model, "void func(float4 a,float8 c);", "void,XMM0:4,XMM1:8"));+ ASSERT(theEnviron.test(model,+ "void func(int8 a,int8 b,int8 c,int8 d,int8 e,float4 f,float4 g,int8 h,int8 i,int8 j,int8 k,int8 l);",+ "void,RAX,RBX,RCX,RDI,RSI,XMM0:4,XMM1:4,R8,R9,R10,R11,stack8:8"));+ ASSERT(theEnviron.test(model,+ "void func(float4 a,float4 b,float4 c,float4 d,float4 e,float4 f,float4 g,float4 h, \+ float4 i,float4 j,float4 k,float4 l,float4 m,float4 n,float4 o,float4 p);",+ "void,XMM0:4,XMM1:4,XMM2:4,XMM3:4,XMM4:4,XMM5:4,XMM6:4,XMM7:4,XMM8:4,XMM9:4,XMM10:4,\+ XMM11:4,XMM12:4,XMM13:4,XMM14:4,stack8:4"));+ ASSERT(theEnviron.test(model, "void func(int8 a,int8 b[3],int8 c);", "void,RAX,stack8:24,RBX"));+ theEnviron.parseType(model,"struct Person { bool a; int4 b; };");+ theEnviron.parseType(model,"struct Myriad { int8 a; bool b; Person c; float4 d; };");+ ASSERT(theEnviron.test(model, "void func(Person a);", "void,join EBX pad:3 AL"));+ ASSERT(theEnviron.test(model, "void func(bool a,Myriad b);", "void,AL,join XMM0:4 ESI pad:3 DIL pad:3 CL RBX"));+ ASSERT(theEnviron.test(model, "Person func(void);", "join EBX pad:3 AL"));+ ASSERT(theEnviron.test(model, "float8 func(int8 a,int8 b[10])[5];", "stack58:40,RAX,stack8:80"));+ ASSERT(theEnviron.test(model, "char func(char a[3],char b[7],int8 c)[5];", "stack18:5,stack8:3,stackb:7,RAX"));+ ASSERT(theEnviron.test(model, "void func(float4 a,int8 b[1],int8 c[2]);","void,XMM0:4,RAX,stack8:16"));+}+TEST(paramstore_ppc64be_stdcall) {ProtoModel *model = theEnviron.getModel("PowerPC:BE:64:default:default","__stdcall");ASSERT(theEnviron.test(model,"void func(int4 a,float4 b,float8 c);","void,r3:4,join f1,f2"));diff --git a/Ghidra/Framework/SoftwareModeling/data/languages/compiler_spec.rxg b/Ghidra/Framework/SoftwareModeling/data/languages/compiler_spec.rxgindex 69b1f35320..1b26bf05cc 100644--- a/Ghidra/Framework/SoftwareModeling/data/languages/compiler_spec.rxg+++ b/Ghidra/Framework/SoftwareModeling/data/languages/compiler_spec.rxg@@ -348,6 +348,8 @@<optional><attribute name="minsize"/></optional><optional><attribute name="maxsize"/></optional><optional><attribute name="sizes"/></optional>+ <optional><attribute name="minelements"/></optional>+ <optional><attribute name="maxelements"/></optional></define><define name="modelrule_type">@@ -623,6 +625,15 @@</element></optional>+ <optional>+ <element name="sharestack">+ <optional>+ <attribute name="first"/>+ <attribute name="align"/>+ </optional>+ </element>+ </optional>+</interleave></define>diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamEntry.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamEntry.javaindex c7755f505c..a744b06cda 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamEntry.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamEntry.java@@ -41,7 +41,8 @@ public class ParamEntry {//private static final int EXTRACHECK_HIGH = 128;//private static final int EXTRACHECK_LOW = 256;private static final int IS_GROUPED = 512; // The entry is grouped with other entries- private static final int OVERLAPPING = 0x100; // This overlaps an earlier entry+ private static final int OVERLAPPING = 0x400; // This overlaps an earlier entry+ private static final int IS_STACKSPILL = 0x1000; // This is the final stack entry holding parameter spillprivate int flags;private StorageClass type; // Restriction on DataType this entry must match@@ -87,6 +88,10 @@ public class ParamEntry {return type;}+ public boolean isStackSpill() {+ return ((flags & IS_STACKSPILL) != 0);+ }+public boolean isExclusion() {return (alignment == 0);}@@ -616,9 +621,10 @@ public class ParamEntry {flags |= IS_BIG_ENDIAN;}if (alignment != 0) {-// if ((addressbase % alignment) != 0)-// throw new XmlParseException("Stack <pentry> address must match alignment");numslots = size / alignment;+ if (spaceid.isStackSpace()) {+ flags |= IS_STACKSPILL;+ }}if (spaceid.isStackSpace() && (!cspec.isStackRightJustified()) && isbigendian) {flags |= FORCE_LEFT_JUSTIFY;diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamList.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamList.javaindex a8bac12430..5f77cbf4c1 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamList.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamList.java@@ -38,13 +38,20 @@ public interface ParamList {}/**- * Given a list of datatypes, calculate the storage locations used for passing those data-types- * @param proto is the list of datatypes+ * Allocate an initialized resource state object for this resource set+ * @return the state object+ */+ public int[] allocateStatus();++ /**+ * Given a list of data-types, calculate the storage locations used for passing those data-types+ * @param proto is the list of data-types* @param dtManage is the data-type manager+ * @param status is the state (which resources are consumed) for this resource set* @param res is the vector for holding the storage locations and other parameter properties* @param addAutoParams if true add/process auto-parameters*/- public void assignMap(PrototypePieces proto, DataTypeManager dtManage,+ public void assignMap(PrototypePieces proto, DataTypeManager dtManage, int[] status,ArrayList<ParameterPieces> res, boolean addAutoParams);public void encode(Encoder encoder, boolean isInput) throws IOException;diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamListRegisterOut.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamListRegisterOut.javaindex c589af6b9c..a6f6dade78 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamListRegisterOut.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamListRegisterOut.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -30,12 +30,8 @@ import ghidra.program.model.data.VoidDataType;public class ParamListRegisterOut extends ParamListStandardOut {@Override- public void assignMap(PrototypePieces proto, DataTypeManager dtManager,+ public void assignMap(PrototypePieces proto, DataTypeManager dtManager, int[] status,ArrayList<ParameterPieces> res, boolean addAutoParams) {- int[] status = new int[numgroup];- for (int i = 0; i < numgroup; ++i) {- status[i] = 0;- }ParameterPieces store = new ParameterPieces();res.add(store);if (VoidDataType.isVoidDataType(proto.outtype)) {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamListStandard.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamListStandard.javaindex 274cde0bd8..3cb642e45d 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamListStandard.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamListStandard.java@@ -54,18 +54,19 @@ public class ParamListStandard implements ParamList {* Find the (first) entry containing range* @param loc is base address of the range* @param size is the size of the range in bytes- * @return the index of entry or -1 if we didn't find container+ * @param just is true if caller wants to test if range is justified+ * @return the containing entry or null*/- private int findEntry(Address loc, int size) {- for (int i = 0; i < entry.length; ++i) {- if (entry[i].getMinSize() > size) {+ public ParamEntry findEntry(Address loc, int size, boolean just) {+ for (ParamEntry cur : entry) {+ if (cur.getMinSize() > size) {continue;}- if (entry[i].justifiedContain(loc, size) == 0) {- return i;- }+ int res = cur.justifiedContain(loc, size);+ if (res == 0 || (!just && res != 1))+ return cur;}- return -1;+ return null;}/**@@ -133,7 +134,11 @@ public class ParamListStandard implements ParamList {{if (dt.isZeroLength()) {- return AssignAction.NO_ASSIGNMENT;+ if (dt.isNotYetDefined())+ return AssignAction.NO_ASSIGNMENT;+ res.address = Address.NO_ADDRESS;+ res.type = dt;+ return AssignAction.SUCCESS;}if (dt == DataType.DEFAULT) {return AssignAction.NO_ASSIGNMENT;@@ -175,25 +180,17 @@ public class ParamListStandard implements ParamList {}@Override- public void assignMap(PrototypePieces proto, DataTypeManager dtManager,- ArrayList<ParameterPieces> res, boolean addAutoParams) {+ public int[] allocateStatus() {int[] status = new int[numgroup];for (int i = 0; i < numgroup; ++i) {status[i] = 0;}+ return status;+ }- if (addAutoParams && res.size() == 2) { // Check for hidden parameters defined by the output list- ParameterPieces last = res.get(res.size() - 1);- if (last.hiddenReturnPtr) {- // Need to pull from registers marked as hiddenret- assignAddressFallback(StorageClass.HIDDENRET, last.type, false, status, last);- }- else {- // Assign as a regular first input pointer parameter- assignAddress(last.type, proto, 0, dtManager, status, last);- }- last.hiddenReturnPtr = true;- }+ @Override+ public void assignMap(PrototypePieces proto, DataTypeManager dtManager, int[] status,+ ArrayList<ParameterPieces> res, boolean addAutoParams) {for (int i = 0; i < proto.intypes.size(); ++i) {ParameterPieces store = new ParameterPieces();res.add(store);@@ -447,11 +444,10 @@ public class ParamListStandard implements ParamList {if (loc == null) {return false;}- int num = findEntry(loc, size);- if (num == -1) {+ ParamEntry curentry = findEntry(loc, size, true);+ if (curentry == null) {return false;}- ParamEntry curentry = entry[num];res.slot = curentry.getSlot(loc, 0);if (curentry.isExclusion()) {res.slotsize = curentry.getAllGroups().length;@@ -538,12 +534,11 @@ public class ParamListStandard implements ParamList {* If there is a ParamEntry corresponding to the stack resource in this list, return it.* @return the stack ParamEntry or null*/- public ParamEntry extractStack() {- for (int i = entry.length - 1; i >= 0; --i) {- ParamEntry pentry = entry[i];- if (!pentry.isExclusion() && pentry.getSpace().isStackSpace()) {- return pentry;- }+ public ParamEntry getStackEntry() {+ if (entry.length > 0) {+ ParamEntry res = entry[entry.length - 1];+ if (res.isStackSpill())+ return res;}return null;}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamListStandardOut.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamListStandardOut.javaindex 7d357a0b3f..e8964aad1b 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamListStandardOut.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/ParamListStandardOut.java@@ -35,14 +35,9 @@ import ghidra.program.model.lang.protorules.AssignAction;public class ParamListStandardOut extends ParamListStandard {@Override- public void assignMap(PrototypePieces proto, DataTypeManager dtManager,+ public void assignMap(PrototypePieces proto, DataTypeManager dtManager, int[] status,ArrayList<ParameterPieces> res, boolean addAutoParams) {- int[] status = new int[numgroup];- for (int i = 0; i < numgroup; ++i) {- status[i] = 0;- }-ParameterPieces store = new ParameterPieces();res.add(store);if (VoidDataType.isVoidDataType(proto.outtype)) {diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/PrototypeModel.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/PrototypeModel.javaindex b1195a8e22..404cc126a7 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/PrototypeModel.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/PrototypeModel.java@@ -24,6 +24,7 @@ import java.util.ArrayList;import ghidra.program.database.SpecExtension;import ghidra.program.model.address.*;import ghidra.program.model.data.*;+import ghidra.program.model.lang.protorules.*;import ghidra.program.model.listing.*;import ghidra.program.model.pcode.*;import ghidra.util.SystemUtilities;@@ -46,6 +47,7 @@ public class PrototypeModel {// due to call mechanismprivate ParamList inputParams; // (possible) parameter locationsprivate ParamList outputParams;+ private SharedAction[] sharedActions;private Varnode[] unaffected; // Memory ranges unaffected by callsprivate Varnode[] killedbycall; // Memory ranges definitely affected by callsprivate Varnode[] returnaddress; // Memory used to store the return address@@ -78,6 +80,10 @@ public class PrototypeModel {inputListType = model.inputListType;inputParams = model.inputParams;outputParams = model.outputParams;+ sharedActions = new SharedAction[model.sharedActions.length];+ for (int i = 0; i < sharedActions.length; ++i) {+ sharedActions[i] = model.sharedActions[i].clone(this);+ }unaffected = model.unaffected;killedbycall = model.killedbycall;returnaddress = model.returnaddress;@@ -99,6 +105,7 @@ public class PrototypeModel {stackshift = -1;inputParams = null;outputParams = null;+ sharedActions = null;unaffected = null;killedbycall = null;returnaddress = null;@@ -113,6 +120,20 @@ public class PrototypeModel {hasUponReturn = false;}+ /**+ * @return object describing resources available for passing input parameters+ */+ public ParamList getInputResource() {+ return inputParams;+ }++ /**+ * @return object describing resources available for passing output parameters+ */+ public ParamList getOutputResource() {+ return outputParams;+ }+/*** @return list of registers unaffected by called functions*/@@ -253,7 +274,8 @@ public class PrototypeModel {DataType clone = dataType.clone(program.getDataTypeManager());PrototypePieces proto = new PrototypePieces(this, clone);ArrayList<ParameterPieces> res = new ArrayList<>();- outputParams.assignMap(proto, program.getDataTypeManager(), res, false);+ int[] status = outputParams.allocateStatus();+ outputParams.assignMap(proto, program.getDataTypeManager(), status, res, false);if (res.size() > 0) {return res.get(0).getVariableStorage(program);}@@ -365,21 +387,15 @@ public class PrototypeModel {*/public void assignParameterStorage(PrototypePieces proto, DataTypeManager dtManager,ArrayList<ParameterPieces> res, boolean addAutoParams) {- outputParams.assignMap(proto, dtManager, res, addAutoParams);- inputParams.assignMap(proto, dtManager, res, addAutoParams);-- if (hasThis && addAutoParams && res.size() > 1) {- int thisIndex = 1;- if (res.get(1).hiddenReturnPtr && res.size() > 2) {- if (inputParams.isThisBeforeRetPointer()) {- // pointer has been bumped by auto-return-storage- res.get(1).swapMarkup(res.get(2)); // must swap storage and position for slots 1 and 2- }- else {- thisIndex = 2;- }- }- res.get(thisIndex).isThisPointer = true;+ int[] inputStatus = inputParams.allocateStatus();+ int[] outputStatus = outputParams.allocateStatus();+ outputParams.assignMap(proto, dtManager, outputStatus, res, addAutoParams);+ for (SharedAction action : sharedActions) {+ action.applyBefore(proto, dtManager, addAutoParams, res, inputStatus, outputStatus);+ }+ inputParams.assignMap(proto, dtManager, inputStatus, res, addAutoParams);+ for (SharedAction action : sharedActions) {+ action.applyAfter(proto, dtManager, addAutoParams, res, inputStatus, outputStatus);}}@@ -476,7 +492,8 @@ public class PrototypeModel {return false;}- private void buildParamList(String strategy) throws XmlParseException {+ private void buildParamList(String strategy)+ throws XmlParseException {if (strategy == null || strategy.equals("standard")) {inputParams = new ParamListStandard();outputParams = new ParamListStandardOut();@@ -526,6 +543,9 @@ public class PrototypeModel {}inputParams.encode(encoder, true);outputParams.encode(encoder, false);+ for (SharedAction action : sharedActions) {+ action.encode(encoder);+ }if (hasUponEntry || hasUponReturn) {InjectPayload payload =injectLibrary.getPayload(InjectPayload.CALLMECHANISM_TYPE, getInjectName());@@ -693,7 +713,11 @@ public class PrototypeModel {isConstruct = SpecXmlUtils.decodeBoolean(constructString);}+ ArrayList<SharedAction> actions = new ArrayList<>();buildParamList(protoElement.getAttribute(ATTRIB_STRATEGY.name()));+ if (inputParams instanceof ParamListStandard) {+ actions.add(new HiddenReturnAction(this));+ }while (parser.peek().isStart()) {XmlElement subel = parser.peek();String elName = subel.getName();@@ -737,12 +761,19 @@ public class PrototypeModel {else if (elName.equals(ELEM_PARAMRANGE.name())) {paramRange = readAddressSet(parser, cspec);}+ else if (elName.equals(ELEM_SHARESTACK.name())) {+ ShareStackAction action = new ShareStackAction(this);+ action.restoreXml(parser);+ actions.add(action);+ }else {subel = parser.start();parser.discardSubTree(subel);}}parser.end(protoElement);+ sharedActions = new SharedAction[actions.size()];+ actions.toArray(sharedActions);}/**@@ -830,6 +861,13 @@ public class PrototypeModel {if (!outputParams.isEquivalent(obj.outputParams)) {return false;}+ if (sharedActions.length != obj.sharedActions.length) {+ return false;+ }+ for (int i = 0; i < sharedActions.length; ++i) {+ if (!sharedActions[i].isEquivalent(obj.sharedActions[i]))+ return false;+ }if (!SystemUtilities.isArrayEqual(unaffected, obj.unaffected)) {return false;}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/ArrayFilter.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/ArrayFilter.javanew file mode 100644index 0000000000..debee3052a--- /dev/null+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/ArrayFilter.java@@ -0,0 +1,125 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.program.model.lang.protorules;++import static ghidra.program.model.pcode.AttributeId.*;+import static ghidra.program.model.pcode.ElementId.*;++import java.io.IOException;+import java.util.Iterator;+import java.util.Map.Entry;++import ghidra.program.model.data.Array;+import ghidra.program.model.data.DataType;+import ghidra.program.model.pcode.Encoder;+import ghidra.util.xml.SpecXmlUtils;+import ghidra.xml.*;++public class ArrayFilter extends SizeRestrictedFilter {+ private int minElements; // Minimum number of elements in the array+ private int maxElements; // Maximum number of elements++ public ArrayFilter() {+ super();+ minElements = 0;+ maxElements = 0;+ }++ public ArrayFilter(ArrayFilter op2) {+ super(op2);+ minElements = op2.minElements;+ maxElements = op2.maxElements;+ }++ @Override+ public DatatypeFilter clone() {+ return new ArrayFilter(this);+ }++ @Override+ public boolean isEquivalent(DatatypeFilter op) {+ if (this.getClass() != op.getClass()) {+ return false;+ }+ ArrayFilter otherFilter = (ArrayFilter) op;+ if (!super.isEquivalent(otherFilter)) {+ return false;+ }+ if (minElements != otherFilter.minElements) {+ return false;+ }+ if (maxElements != otherFilter.maxElements) {+ return false;+ }+ return true;+ }++ @Override+ public boolean filter(DataType dt) {+ if (!(dt instanceof Array)) {+ return false;+ }+ if (!filterOnSize(dt)) {+ return false;+ }+ Array arr = (Array) dt;+ if (arr.getNumElements() < minElements) {+ return false;+ }+ if (maxElements != 0 && arr.getNumElements() > maxElements) {+ return false;+ }+ return true;+ }++ @Override+ protected void encodeAttributes(Encoder encoder) throws IOException {+ super.encodeAttributes(encoder);+ encoder.writeUnsignedInteger(ATTRIB_MINELEMENTS, minElements);+ encoder.writeUnsignedInteger(ATTRIB_MAXELEMENTS, maxElements);+ }++ @Override+ public void encode(Encoder encoder) throws IOException {+ encoder.openElement(ELEM_DATATYPE);+ encoder.writeString(ATTRIB_NAME, "array");+ encodeAttributes(encoder);+ encoder.closeElement(ELEM_DATATYPE);+ }++ @Override+ protected void restoreAttributesXml(XmlElement el) throws XmlParseException {+ super.restoreAttributesXml(el);+ Iterator<Entry<String, String>> iter = el.getAttributes().entrySet().iterator();+ while (iter.hasNext()) {+ Entry<String, String> attrib = iter.next();+ String nm = attrib.getKey();+ if (nm.equals(ATTRIB_MINELEMENTS.name())) {+ minElements = SpecXmlUtils.decodeInt(attrib.getValue());+ }+ else if (nm.equals(ATTRIB_MAXELEMENTS.name())) {+ maxElements = SpecXmlUtils.decodeInt(attrib.getValue());+ }+ }+ }++ @Override+ public void restoreXml(XmlPullParser parser) throws XmlParseException {+ XmlElement elem = parser.start(ELEM_DATATYPE.name());+ restoreAttributesXml(elem);+ parser.end(elem);+ }+}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/AssignAction.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/AssignAction.javaindex 629f96192f..b9699a4ec0 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/AssignAction.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/AssignAction.java@@ -127,7 +127,8 @@ public abstract class AssignAction {action = new HiddenReturnAssign(res, HIDDENRET_SPECIALREG);}else if (nm.equals(ELEM_JOIN_PER_PRIMITIVE.name())) {- action = new MultiMemberAssign(StorageClass.GENERAL, false, res.isBigEndian(), res);+ action =+ new MultiMemberAssign(StorageClass.GENERAL, true, false, res.isBigEndian(), res);}else if (nm.equals(ELEM_JOIN_DUAL_CLASS.name())) {action = new MultiSlotDualAssign(res);diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/DatatypeFilter.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/DatatypeFilter.javaindex f42241fe36..66102263d9 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/DatatypeFilter.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/DatatypeFilter.java@@ -85,7 +85,12 @@ public interface DatatypeFilter {else {// If no other name matches, assume this is a decompiler metatypeint meta = PcodeDataTypeManager.getMetatype(nm);- filter = new MetaTypeFilter(meta);+ if (meta == PcodeDataTypeManager.TYPE_ARRAY) {+ filter = new ArrayFilter();+ }+ else {+ filter = new MetaTypeFilter(meta);+ }}filter.restoreXml(parser);return filter;diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/ExtraStack.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/ExtraStack.javaindex b500ef275b..f4bfe4fffd 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/ExtraStack.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/ExtraStack.java@@ -49,13 +49,7 @@ public class ExtraStack extends AssignAction {* @throws InvalidInputException if there is no stack entry*/private void initializeEntry() throws InvalidInputException {- for (int i = 0; i < resource.getNumParamEntry(); ++i) {- ParamEntry entry = resource.getEntry(i);- if (!entry.isExclusion() && entry.getSpace().isStackSpace()) {- stackEntry = entry;- break;- }- }+ stackEntry = resource.getStackEntry();if (stackEntry == null) {throw new InvalidInputException("Cannot find matching <pentry> for action: extra_stack");diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/GotoStack.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/GotoStack.javaindex 2ec1a93315..7bee20fe0b 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/GotoStack.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/GotoStack.java@@ -34,13 +34,7 @@ public class GotoStack extends AssignAction {private ParamEntry stackEntry; // Parameter Entry corresponding to the stackprivate void initializeEntry() throws InvalidInputException {- for (int i = 0; i < resource.getNumParamEntry(); ++i) {- ParamEntry entry = resource.getEntry(i);- if (!entry.isExclusion() && entry.getSpace().isStackSpace()) {- stackEntry = entry;- break;- }- }+ stackEntry = resource.getStackEntry();if (stackEntry == null) {throw new InvalidInputException("Cannot find matching <pentry> for action: goto_stack");}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/HiddenReturnAction.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/HiddenReturnAction.javanew file mode 100644index 0000000000..2a975615d9--- /dev/null+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/HiddenReturnAction.java@@ -0,0 +1,97 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.program.model.lang.protorules;++import java.io.IOException;+import java.util.ArrayList;++import ghidra.program.model.data.DataTypeManager;+import ghidra.program.model.lang.*;+import ghidra.program.model.pcode.Encoder;+import ghidra.xml.XmlPullParser;++/**+ * Action that allocates a pointer, required for the output, from the input resources.+ */+public class HiddenReturnAction extends SharedAction {+ private ParamListStandard inputResource; // Input parameter resources++ public HiddenReturnAction(PrototypeModel model) {+ super(model);+ ParamList inputs = model.getInputResource();+ this.inputResource = (ParamListStandard) inputs;+ }++ @Override+ public void applyBefore(PrototypePieces proto, DataTypeManager dtManager, boolean addAutoParams,+ ArrayList<ParameterPieces> params, int[] inputStatus, int[] outputStatus) {+ if (params.size() == 2) { // Check for hidden parameters defined by the output list+ ParameterPieces last = params.get(params.size() - 1);+ if (last.hiddenReturnPtr) {+ // Need to pull from registers marked as hiddenret+ inputResource.assignAddressFallback(StorageClass.HIDDENRET, last.type, false,+ inputStatus, last);+ }+ else {+ // Assign as a regular first input pointer parameter+ inputResource.assignAddress(last.type, proto, 0, dtManager, inputStatus, last);+ }+ last.hiddenReturnPtr = true;+ }+ }++ @Override+ public void applyAfter(PrototypePieces proto, DataTypeManager dtManager, boolean addAutoParams,+ ArrayList<ParameterPieces> params, int[] inputStatus, int[] outputStatus) {+ if (model.hasThisPointer() && addAutoParams && params.size() > 1) {+ int thisIndex = 1;+ if (params.get(1).hiddenReturnPtr && params.size() > 2) {+ if (inputResource.isThisBeforeRetPointer()) {+ // pointer has been bumped by auto-return-storage+ params.get(1).swapMarkup(params.get(2)); // must swap storage and position for slots 1 and 2+ }+ else {+ thisIndex = 2;+ }+ }+ params.get(thisIndex).isThisPointer = true;+ }+ }++ @Override+ public SharedAction clone(PrototypeModel newModel) {+ return new HiddenReturnAction(newModel);+ }++ @Override+ public boolean isEquivalent(SharedAction obj) {+ if (getClass() != obj.getClass()) {+ return false;+ }+ return true;+ }++ @Override+ public void encode(Encoder encoder) throws IOException {+ // Nothing to encode+ }++ @Override+ public void restoreXml(XmlPullParser parser) {+ // Nothing to restore+ }++}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/HomogeneousAggregate.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/HomogeneousAggregate.javaindex 98158aa4fb..778fdd02b5 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/HomogeneousAggregate.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/HomogeneousAggregate.java@@ -74,13 +74,34 @@ public class HomogeneousAggregate extends SizeRestrictedFilter {return new HomogeneousAggregate(this);}+ @Override+ public boolean isEquivalent(DatatypeFilter op) {+ if (this.getClass() != op.getClass()) {+ return false;+ }+ HomogeneousAggregate otherFilter = (HomogeneousAggregate) op;+ if (!super.isEquivalent(otherFilter)) {+ return false;+ }+ if (!name.equals(otherFilter.name)) {+ return false;+ }+ if (metaType != otherFilter.metaType) {+ return false;+ }+ if (maxPrimitives != otherFilter.maxPrimitives) {+ return false;+ }+ return true;+ }+@Overridepublic boolean filter(DataType dt) {int meta = PcodeDataTypeManager.getMetatype(dt);if (meta != PcodeDataTypeManager.TYPE_ARRAY && meta != PcodeDataTypeManager.TYPE_STRUCT) {return false;}- PrimitiveExtractor primitives = new PrimitiveExtractor(dt, true, 0, maxPrimitives);+ PrimitiveExtractor primitives = new PrimitiveExtractor(dt, true, false, 0, maxPrimitives);if (!primitives.isValid() || primitives.size() == 0 || primitives.containsUnknown() ||!primitives.isAligned() || primitives.containsHoles()) {return false;diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/MultiMemberAssign.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/MultiMemberAssign.javaindex 3ba44ed184..f2967536f9 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/MultiMemberAssign.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/MultiMemberAssign.java@@ -21,9 +21,11 @@ import static ghidra.program.model.pcode.ElementId.*;import java.io.IOException;import java.util.ArrayList;+import ghidra.program.model.address.AddressSpace;import ghidra.program.model.data.DataType;import ghidra.program.model.data.DataTypeManager;import ghidra.program.model.lang.*;+import ghidra.program.model.lang.protorules.PrimitiveExtractor.Primitive;import ghidra.program.model.pcode.Encoder;import ghidra.program.model.pcode.Varnode;import ghidra.util.exception.InvalidInputException;@@ -33,26 +35,36 @@ import ghidra.xml.*;* Consume a register per primitive member of an aggregate data-type** The data-type is split up into its underlying primitive elements, and each one- * is assigned a register from the specific resource list. There must be no padding between- * elements. No packing of elements into a single register occurs.+ * is assigned storage as if it were a separate parameter, or alternately assigned a+ * register from a specific resource list. The storage elements are returned together+ * as a single Address in JOIN space. Constant zeroes are used as placeholders for any+ * padding between elements. No packing of elements into a single register occurs.*/public class MultiMemberAssign extends AssignAction {private StorageClass resourceType; // Resource list from which to consume+ private boolean isRecursive; // True if primitives are assigned recursivelyprivate boolean consumeFromStack; // True if resources should be consumed from the stackprivate boolean consumeMostSig; // True if resources are consumed starting with most significant bytes+ private ParamEntry stackEntry; // Parameter Entry corresponding to the stack+ private AddressSpace constSpace; // Address space used for padding- public MultiMemberAssign(StorageClass store, boolean stack, boolean mostSig,+ public MultiMemberAssign(StorageClass store, boolean recurse, boolean stack, boolean mostSig,ParamListStandard res) {super(res);+ constSpace = resource.getLanguage().getAddressFactory().getConstantSpace();+resourceType = store;+ isRecursive = recurse;consumeFromStack = stack;consumeMostSig = mostSig;+ stackEntry = resource.getStackEntry();}@Overridepublic AssignAction clone(ParamListStandard newResource) throws InvalidInputException {- return new MultiMemberAssign(resourceType, consumeFromStack, consumeMostSig, newResource);+ return new MultiMemberAssign(resourceType, isRecursive, consumeFromStack, consumeMostSig,+ newResource);}@Override@@ -64,31 +76,87 @@ public class MultiMemberAssign extends AssignAction {if (resourceType != otherOp.resourceType) {return false;}+ if (isRecursive != otherOp.isRecursive) {+ return false;+ }if (consumeFromStack != otherOp.consumeFromStack) {return false;}return consumeMostSig == otherOp.consumeMostSig;}+ private boolean addPadding(ArrayList<Varnode> pieces, int pad)++ {+ if (pad == 0)+ return true;+ if (pad < 0)+ return false;++ Varnode vn = new Varnode(constSpace.getAddress(0), pad);+ pieces.add(vn);+ return true;+ }+@Overridepublic int assignAddress(DataType dt, PrototypePieces proto, int pos, DataTypeManager dtManager,int[] status, ParameterPieces res) {int[] tmpStatus = status.clone();ArrayList<Varnode> pieces = new ArrayList<>();ParameterPieces param = new ParameterPieces();- PrimitiveExtractor primitives = new PrimitiveExtractor(dt, false, 0, 16);+ // If we are recursive, treat arrays as primitive so other rules can (recursively) apply+ PrimitiveExtractor primitives = new PrimitiveExtractor(dt, false, isRecursive, 0, 16);if (!primitives.isValid() || primitives.size() == 0 || primitives.containsUnknown() ||!primitives.isAligned() || primitives.containsHoles()) {return FAIL;}- for (int i = 0; i < primitives.size(); ++i) {- DataType curType = primitives.get(i).dt;- if (resource.assignAddressFallback(resourceType, curType, !consumeFromStack, tmpStatus,- param) == FAIL) {+ if (isRecursive) { // Recursive assignments+ if (primitives.size() == 1) {+ if (primitives.get(0).dt == dt)+ return FAIL; // Prevent infinite recursion+ }+ int size = 0;+ int before = (stackEntry != null) ? tmpStatus[stackEntry.getGroup()] : -1;+ for (int i = 0; i < primitives.size(); ++i) {+ Primitive primitive = primitives.get(i);+ if (resource.assignAddress(primitive.dt, proto, pos, dtManager, tmpStatus,+ param) == FAIL) {+ return FAIL;+ }+ if (!consumeFromStack && stackEntry != null) {+ if (before != tmpStatus[stackEntry.getGroup()])+ return FAIL;+ }+ if (!addPadding(pieces, primitive.offset - size)) {+ return FAIL;+ }+ size = primitive.offset + primitive.dt.getLength();+ Varnode vn = new Varnode(param.address, primitive.dt.getLength());+ pieces.add(vn);+ }+ if (!addPadding(pieces, dt.getLength() - size)) {+ return FAIL;+ }+ }+ else { // Assignment from a specific resourceType+ int size = 0;+ for (int i = 0; i < primitives.size(); ++i) {+ Primitive primitive = primitives.get(i);+ if (resource.assignAddressFallback(resourceType, primitive.dt, !consumeFromStack,+ tmpStatus,+ param) == FAIL) {+ return FAIL;+ }+ if (!addPadding(pieces, primitive.offset - size)) {+ return FAIL;+ }+ size = primitive.offset + primitive.dt.getLength();+ Varnode vn = new Varnode(param.address, primitive.dt.getLength());+ pieces.add(vn);+ }+ if (!addPadding(pieces, dt.getLength() - size)) {return FAIL;}- Varnode vn = new Varnode(param.address, curType.getLength());- pieces.add(vn);}System.arraycopy(tmpStatus, 0, status, 0, tmpStatus.length); // Commit resource usage for all the pieces@@ -100,7 +168,7 @@ public class MultiMemberAssign extends AssignAction {@Overridepublic void encode(Encoder encoder) throws IOException {encoder.openElement(ELEM_JOIN_PER_PRIMITIVE);- if (resourceType != StorageClass.GENERAL) {+ if (!isRecursive) {encoder.writeString(ATTRIB_STORAGE, resourceType.toString());}encoder.closeElement(ELEM_JOIN_PER_PRIMITIVE);@@ -108,10 +176,12 @@ public class MultiMemberAssign extends AssignAction {@Overridepublic void restoreXml(XmlPullParser parser) throws XmlParseException {+ isRecursive = true;XmlElement elem = parser.start(ELEM_JOIN_PER_PRIMITIVE.name());String attribString = elem.getAttribute(ATTRIB_STORAGE.name());if (attribString != null) {resourceType = StorageClass.getClass(attribString);+ isRecursive = false;}parser.end(elem);}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/MultiSlotAssign.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/MultiSlotAssign.javaindex 74f7cf5266..72184ccca3 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/MultiSlotAssign.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/MultiSlotAssign.java@@ -59,7 +59,7 @@ public class MultiSlotAssign extends AssignAction {*/private void initializeEntries() throws InvalidInputException {tiles = resource.extractTiles(resourceType);- stackEntry = resource.extractStack();+ stackEntry = resource.getStackEntry();if (tiles.length == 0) {throw new InvalidInputException("Could not find matching resources for action: join");}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/MultiSlotDualAssign.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/MultiSlotDualAssign.javaindex 83b4fff43e..861433a904 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/MultiSlotDualAssign.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/MultiSlotDualAssign.java@@ -58,7 +58,7 @@ public class MultiSlotDualAssign extends AssignAction {private void initializeEntries() throws InvalidInputException {baseTiles = resource.extractTiles(baseType);altTiles = resource.extractTiles(altType);- stackEntry = resource.extractStack();+ stackEntry = resource.getStackEntry();if (baseTiles.length == 0 || altTiles.length == 0) {throw new InvalidInputException("Could not find matching resources for action: join_dual_class");@@ -235,7 +235,7 @@ public class MultiSlotDualAssign extends AssignAction {@Overridepublic int assignAddress(DataType dt, PrototypePieces proto, int pos, DataTypeManager dtManager,int[] status, ParameterPieces res) {- PrimitiveExtractor primitives = new PrimitiveExtractor(dt, false, 0, 1024);+ PrimitiveExtractor primitives = new PrimitiveExtractor(dt, false, false, 0, 1024);if (!primitives.isValid() || primitives.size() == 0 || primitives.containsHoles()) {return FAIL;}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/PrimitiveExtractor.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/PrimitiveExtractor.javaindex 19e3bc6ede..f7691aa4f3 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/PrimitiveExtractor.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/PrimitiveExtractor.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -38,6 +38,7 @@ public class PrimitiveExtractor {private boolean unknownElements; // True if at least one TYPE_UNKNOWN primitiveprivate boolean extraSpace; // True if extra space not attributable to paddingprivate boolean unionInvalid; // True if unions are treated as invalid primitive+ private boolean arrayIsPrimitive; // True if arrays are treated as primitive/*** Check that a big Primitive properly overlaps smaller Primitives@@ -159,7 +160,7 @@ public class PrimitiveExtractor {DataTypeComponent curField = dt.getComponent(0);PrimitiveExtractor common = new PrimitiveExtractor(curField.getDataType(), false,- offset + curField.getOffset(), max);+ arrayIsPrimitive, offset + curField.getOffset(), max);if (!common.isValid()) {return false;}@@ -167,7 +168,7 @@ public class PrimitiveExtractor {curField = dt.getComponent(i);PrimitiveExtractor next = new PrimitiveExtractor(curField.getDataType(), false,- offset + curField.getOffset(), max);+ arrayIsPrimitive, offset + curField.getOffset(), max);if (!next.isValid()) {return false;}@@ -197,9 +198,10 @@ public class PrimitiveExtractor {* @param dt is the given data-type to extract primitives from* @param max is the maximum number of primitives to extract before giving up* @param offset is the starting offset to associate with the first primitive+ * @param depth is the current depth of recursion* @return true if all primitives were extracted*/- private boolean extract(DataType dt, int max, int offset) {+ private boolean extract(DataType dt, int max, int offset, int depth) {if (dt instanceof TypeDef) {dt = ((TypeDef) dt).getBaseDataType();}@@ -221,10 +223,17 @@ public class PrimitiveExtractor {primitives.add(new Primitive(dt, offset));return true;case PcodeDataTypeManager.TYPE_ARRAY: {+ if (arrayIsPrimitive && depth != 0) {+ if (primitives.size() >= max) {+ return false;+ }+ primitives.add(new Primitive(dt, offset));+ return true;+ }int numEls = ((Array) dt).getNumElements();DataType base = ((Array) dt).getDataType();for (int i = 0; i < numEls; ++i) {- if (!extract(base, max, offset)) {+ if (!extract(base, max, offset, depth + 1)) {return false;}offset += base.getAlignedLength();@@ -258,7 +267,7 @@ public class PrimitiveExtractor {extraSpace = true;}}- if (!extract(compDT, max, curOff)) {+ if (!extract(compDT, max, curOff, depth + 1)) {return false;}expectedOff = curOff + compDT.getAlignedLength();@@ -269,17 +278,20 @@ public class PrimitiveExtractor {/*** @param dt is data-type extract from* @param unionIllegal is true if unions encountered during extraction are considered illegal+ * @param arrayPrimitive is true if arrays should be treated as primitives* @param offset is the starting offset to associate with the data-type* @param max is the maximum number of primitives to extract before giving up*/- public PrimitiveExtractor(DataType dt, boolean unionIllegal, int offset, int max) {+ public PrimitiveExtractor(DataType dt, boolean unionIllegal, boolean arrayPrimitive,+ int offset, int max) {primitives = new ArrayList<>();valid = true;aligned = true;unknownElements = false;extraSpace = false;unionInvalid = unionIllegal;- if (!extract(dt, max, offset)) {+ arrayIsPrimitive = arrayPrimitive;+ if (!extract(dt, max, offset, 0)) {valid = false;}}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/ShareStackAction.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/ShareStackAction.javanew file mode 100644index 0000000000..7e72d34b4a--- /dev/null+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/ShareStackAction.java@@ -0,0 +1,133 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.program.model.lang.protorules;++import static ghidra.program.model.pcode.AttributeId.*;+import static ghidra.program.model.pcode.ElementId.*;++import java.io.IOException;+import java.util.ArrayList;++import ghidra.program.model.data.DataTypeManager;+import ghidra.program.model.lang.*;+import ghidra.program.model.pcode.Encoder;+import ghidra.util.xml.SpecXmlUtils;+import ghidra.xml.XmlElement;+import ghidra.xml.XmlPullParser;++/**+ * Parameter assignment action that lets input and output parameters share stack space for+ * spilling. Any input stack allocations can happen either before or after any+ * output stack allocation. The action can optionally add an extra stack alignment+ * between the first allocations and second allocations.+ */+public class ShareStackAction extends SharedAction {++ private ParamEntry inputStackEntry; // Stack resource for input parameters+ private ParamEntry outputStackEntry; // Stack resource for output parameters+ private boolean inputFirst; // True if input stack populated before output stack+ private int extraAlign; // Extra alignment added between first stack allocations and second++ public ShareStackAction(PrototypeModel model) {+ super(model);+ ParamList inputResource = model.getInputResource();+ ParamList outputResource = model.getOutputResource();+ inputStackEntry = ((ParamListStandard) inputResource).getStackEntry();+ outputStackEntry = ((ParamListStandard) outputResource).getStackEntry();+ }++ @Override+ public void applyBefore(PrototypePieces proto, DataTypeManager dtManager, boolean addAutoParams,+ ArrayList<ParameterPieces> params, int[] inputStatus, int[] outputStatus) {+ if (inputFirst) {+ return;+ }+ // Treat output consumed stack as input consumed stack+ int slotnum = outputStatus[outputStackEntry.getGroup()];+ if (extraAlign > inputStackEntry.getAlign()) {+ int tmp = (slotnum * inputStackEntry.getAlign()) % extraAlign;+ if (tmp != 0) {+ slotnum += (extraAlign - tmp) / inputStackEntry.getAlign();+ }+ }+ inputStatus[inputStackEntry.getGroup()] = slotnum;+ }++ @Override+ public void applyAfter(PrototypePieces proto, DataTypeManager dtManager, boolean addAutoParams,+ ArrayList<ParameterPieces> params, int[] inputStatus, int[] outputStatus) {+ if (!inputFirst) {+ return;+ }+ if (params.isEmpty()) {+ return;+ }+ ParameterPieces outEntry = params.get(0);+ if (outEntry.address == null ||+ outEntry.address.getAddressSpace() != outputStackEntry.getSpace()) {+ return;+ }+ int slotnum = inputStatus[inputStackEntry.getGroup()];+ if (extraAlign > inputStackEntry.getAlign()) {+ int tmp = (slotnum * inputStackEntry.getAlign()) % extraAlign;+ if (tmp != 0) {+ slotnum += (extraAlign - tmp) / inputStackEntry.getAlign();+ }+ }+ inputStackEntry.getAddrBySlot(slotnum, outEntry.type.getLength(),+ outEntry.type.getAlignment(), outEntry);+ }++ @Override+ public SharedAction clone(PrototypeModel newModel) {+ return new ShareStackAction(newModel);+ }++ @Override+ public boolean isEquivalent(SharedAction obj) {+ if (getClass() != obj.getClass()) {+ return false;+ }+ ShareStackAction otherAction = (ShareStackAction) obj;+ if (inputFirst != otherAction.inputFirst)+ return false;+ if (extraAlign != otherAction.extraAlign)+ return false;+ return true;+ }++ @Override+ public void encode(Encoder encoder) throws IOException {+ encoder.openElement(ELEM_SHARESTACK);+ encoder.writeString(ATTRIB_FIRST, inputFirst ? "input" : "output");+ if (extraAlign != 0) {+ encoder.writeUnsignedInteger(ATTRIB_ALIGN, extraAlign);+ }+ encoder.closeElement(ELEM_SHARESTACK);+ }++ @Override+ public void restoreXml(XmlPullParser parser) {+ XmlElement mainel = parser.start();+ String attribute = mainel.getAttribute(ATTRIB_FIRST.name());+ inputFirst = false;+ if (attribute != null) {+ inputFirst = attribute.equals("input");+ }+ extraAlign = SpecXmlUtils.decodeInt(mainel.getAttribute(ATTRIB_ALIGN.name()));+ parser.end(mainel);+ }+}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/SharedAction.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/SharedAction.javanew file mode 100644index 0000000000..eeae165dee--- /dev/null+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/SharedAction.java@@ -0,0 +1,78 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.program.model.lang.protorules;++import java.io.IOException;+import java.util.ArrayList;++import ghidra.program.model.data.DataTypeManager;+import ghidra.program.model.lang.*;+import ghidra.program.model.pcode.Encoder;+import ghidra.xml.XmlPullParser;++/**+ * An action that happens during parameter allocation. It sees resource usage for+ * both input and output parameters and can make changes. It can act either right after+ * output parameter assignment or after input parameter assignment.+ */+public abstract class SharedAction {+ protected PrototypeModel model; // The model owning/performing this action++ public SharedAction(PrototypeModel model) {+ this.model = model;+ }++ /**+ * Implement resource sharing between the input and output parameter assignments.+ * Called once before input parameters have been assigned (after output assignment).+ * @param proto is the list of data-types+ * @param dtManager is the data-type manager+ * @param addAutoParams is true if the this pointer and other auto-params should be added+ * @param params is the current set of allocated parameters+ * @param inputStatus is the consume status for this (input resources)+ * @param outputStatus is the consume status for output resources+ */+ public abstract void applyBefore(PrototypePieces proto, DataTypeManager dtManager,+ boolean addAutoParams,+ ArrayList<ParameterPieces> params, int[] inputStatus, int[] outputStatus);++ /**+ * Implement resource sharing after input and output parameter assignments.+ * Called once after both input and output parameters have been assigned.+ * @param proto is the list of data-types+ * @param dtManager is the data-type manager+ * @param addAutoParams is true if the this pointer and other auto-params should be added+ * @param params is the current set of allocated parameters+ * @param inputStatus is the consume status for this (input resources)+ * @param outputStatus is the consume status for output resources+ */+ public abstract void applyAfter(PrototypePieces proto, DataTypeManager dtManager,+ boolean addAutoParams,+ ArrayList<ParameterPieces> params, int[] inputStatus, int[] outputStatus);++ /**+ * Clone this action onto a new prototype model+ * @param newModel is the new model+ * @return the cloned action+ */+ public abstract SharedAction clone(PrototypeModel newModel);++ public abstract boolean isEquivalent(SharedAction obj);++ public abstract void encode(Encoder encoder) throws IOException;++ public abstract void restoreXml(XmlPullParser parser);+}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/listing/VariableStorage.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/listing/VariableStorage.javaindex bb32384b12..66c72b552b 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/listing/VariableStorage.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/listing/VariableStorage.java@@ -30,12 +30,17 @@ import ghidra.util.exception.InvalidInputException;* function parameter or local variable. For big-endian the first element corresponds* to the most-significant varnode, while for little-endian the first element* corresponds to the least-significant varnode.+ *+ * Portions of the variable that are not actively being stored, such as structure padding+ * or alignment bytes, are represented by constant varnodes in order to maintain+ * correspondence between the varnodes and the data-type layout.*/public class VariableStorage implements Comparable<VariableStorage> {private static final String BAD = "<BAD>";private static final String UNASSIGNED = "<UNASSIGNED>";private static final String VOID = "<VOID>";+ private static final String PADDING = "pad";/*** <code>BAD_STORAGE</code> used to identify variable storage which is no longer@@ -198,13 +203,12 @@ public class VariableStorage implements Comparable<VariableStorage> {"Unsupported varnode size: " + varnode.getSize());}- boolean isRegister = false;+ boolean isRegisterOrPad = false;Address storageAddr = varnode.getAddress();- if (storageAddr.isHashAddress() || storageAddr.isUniqueAddress() ||- storageAddr.isConstantAddress()) {+ if (storageAddr.isHashAddress() || storageAddr.isUniqueAddress()) {if (varnodes.length != 1) {throw new InvalidInputException(- "Hash, Unique and Constant storage may only use a single varnode");+ "Hash and Unique storage may only use a single varnode");}}else {@@ -217,11 +221,21 @@ public class VariableStorage implements Comparable<VariableStorage> {}}- if (!storageAddr.isStackAddress()) {+ if (storageAddr.isConstantAddress()) {+ // Any single constant is allowed+ if (varnodes.length != 1) {+ // A constant with other varnodes is considered padding and must be zero+ if (storageAddr.getOffset() != 0) {+ throw new InvalidInputException("Constant not allowed as VariableStorage");+ }+ isRegisterOrPad = true;+ }+ }+ else if (!storageAddr.isStackAddress()) {Register reg =programArch.getLanguage().getRegister(storageAddr, varnode.getSize());if (reg != null && !(reg instanceof UnknownRegister)) {- isRegister = true;+ isRegisterOrPad = true;if (registers == null) {registers = new ArrayList<Register>();}@@ -245,13 +259,13 @@ public class VariableStorage implements Comparable<VariableStorage> {}}if (programArch.getLanguage().isBigEndian()) {- if (i < (varnodes.length - 1) && !isRegister) {+ if (i < (varnodes.length - 1) && !isRegisterOrPad) {throw new InvalidInputException("Compound storage must use registers except for last BE varnode");}}else {- if (i > 0 && !isRegister) {+ if (i > 0 && !isRegisterOrPad) {throw new InvalidInputException("Compound storage must use registers except for first LE varnode");}@@ -259,6 +273,9 @@ public class VariableStorage implements Comparable<VariableStorage> {size += varnode.getSize();}for (int i = 0; i < varnodes.length; i++) {+ if (varnodes[i].isConstant()) {+ continue;+ }for (int j = i + 1; j < varnodes.length; j++) {if (varnodes[i].intersects(varnodes[j])) {throw new InvalidInputException("One or more conflicting storage varnodes");@@ -346,6 +363,9 @@ public class VariableStorage implements Comparable<VariableStorage> {return register.toString();}}+ if (address.isConstantAddress() && varnodes.length != 1) {+ return PADDING;+ }return address.toString();}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/AddressXML.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/AddressXML.javaindex d4fc461e17..897be374e7 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/AddressXML.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/AddressXML.java@@ -592,7 +592,7 @@ public class AddressXML {*/public static void encode(Encoder encoder, Varnode[] varnodes, long logicalsize)throws IOException {- if (varnodes == null) {+ if (varnodes == null || varnodes.length == 0) {encoder.openElement(ELEM_ADDR);encoder.closeElement(ELEM_ADDR);return;diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/AttributeId.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/AttributeId.javaindex 0f18141a79..cafc03d051 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/AttributeId.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/AttributeId.java@@ -253,7 +253,9 @@ public record AttributeId(String name, int id) {public static final AttributeId ATTRIB_AFTER_BYTES = new AttributeId("afterbytes", 156);public static final AttributeId ATTRIB_AFTER_STORAGE = new AttributeId("afterstorage", 157);public static final AttributeId ATTRIB_FILL_ALTERNATE = new AttributeId("fillalternate", 158);+ public static final AttributeId ATTRIB_MINELEMENTS = new AttributeId("minelements", 159);+ public static final AttributeId ATTRIB_MAXELEMENTS = new AttributeId("maxelements", 160);- public static final AttributeId ATTRIB_UNKNOWN = new AttributeId("XMLunknown", 159);+ public static final AttributeId ATTRIB_UNKNOWN = new AttributeId("XMLunknown", 161);}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/ElementId.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/ElementId.javaindex cd6b429893..1805d362bf 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/ElementId.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/ElementId.java@@ -270,6 +270,7 @@ public record ElementId(String name, int id) {public static final ElementId ELEM_RETURNSYM = new ElementId("returnsym", 172);public static final ElementId ELEM_UNAFFECTED = new ElementId("unaffected", 173);public static final ElementId ELEM_INTERNAL_STORAGE = new ElementId("internal_storage", 286);+ public static final ElementId ELEM_SHARESTACK = new ElementId("sharestack", 291);// optionspublic static final ElementId ELEM_ALIASBLOCK = new ElementId("aliasblock", 174);@@ -461,5 +462,5 @@ public record ElementId(String name, int id) {public static final ElementId ELEM_EXTRA_STACK = new ElementId("extra_stack", 287);public static final ElementId ELEM_CONSUME_REMAINING = new ElementId("consume_remaining", 288);- public static final ElementId ELEM_UNKNOWN = new ElementId("XMLunknown", 291);+ public static final ElementId ELEM_UNKNOWN = new ElementId("XMLunknown", 292);}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/LocalSymbolMap.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/LocalSymbolMap.javaindex 516717daf5..90c81bed86 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/LocalSymbolMap.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/LocalSymbolMap.java@@ -213,11 +213,12 @@ public class LocalSymbolMap {boolean internalInvalid = false;for (int i = 0; i < p.length; ++i) {Parameter var = p[i];- if (!var.isValid()) {- internalInvalid = true;+ DataType dt = var.getDataType();+ if (!var.isValid() && (!dt.isZeroLength() || dt.isNotYetDefined())) {+ // If storage is invalid and data-type does not formally have zero-length+ internalInvalid = true; // We don't trust storage for the entire prototypebreak;}- DataType dt = var.getDataType();String name = var.getName();if (name.length() > 2 && name.charAt(name.length() - 2) == '$') {// An indication of names like "name", "name@1", "name@2"@@ -481,8 +482,11 @@ public class LocalSymbolMap {}}if (sym.entryList[0] instanceof MappedEntry) {- MappedVarKey key = new MappedVarKey(sym.getStorage(), sym.getPCAddress());- addrMappedSymbols.put(key, sym);+ VariableStorage storage = sym.getStorage();+ if (storage.getVarnodeCount() != 0) {+ MappedVarKey key = new MappedVarKey(storage, sym.getPCAddress());+ addrMappedSymbols.put(key, sym);+ }}symbolMap.put(uniqueId, sym);}diff --git a/Ghidra/Processors/x86/data/languages/x86-64-golang.cspec b/Ghidra/Processors/x86/data/languages/x86-64-golang.cspecindex bba1208a18..bba336e25e 100644--- a/Ghidra/Processors/x86/data/languages/x86-64-golang.cspec+++ b/Ghidra/Processors/x86/data/languages/x86-64-golang.cspec@@ -63,6 +63,27 @@<pentry minsize="4" maxsize="8" metatype="float"><register name="XMM7_Qa"/></pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM8_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM9_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM10_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM11_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM12_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM13_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM14_Qa"/>+ </pentry><pentry minsize="1" maxsize="8"><register name="RAX"/>@@ -92,44 +113,139 @@<register name="R11"/></pentry>- <pentry minsize="1" maxsize="500" align="8">+ <pentry minsize="1" maxsize="500" align="1"><addr offset="8" space="stack"/></pentry>+ <rule>+ <datatype name="int" minsize="9" maxsize="16"/>+ <join/>+ </rule>+ <rule>+ <datatype name="uint" minsize="9" maxsize="16"/>+ <join/>+ </rule>+ <rule>+ <datatype name="float"/>+ <consume storage="float"/>+ </rule>+ <rule>+ <datatype name="float"/>+ <goto_stack/> <!-- Once float registers are used don't use general purpose-->+ </rule>+ <rule>+ <datatype name="array" minelements="2"/> <!-- Array with 1 element treated like structure -->+ <goto_stack/>+ </rule>+ <rule>+ <datatype name="any"/>+ <join_per_primitive/>+ </rule></input><output><pentry minsize="4" maxsize="8" metatype="float"><register name="XMM0_Qa"/></pentry>-+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM1_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM2_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM3_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM4_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM5_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM6_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM7_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM8_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM9_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM10_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM11_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM12_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM13_Qa"/>+ </pentry>+ <pentry minsize="4" maxsize="8" metatype="float">+ <register name="XMM14_Qa"/>+ </pentry>+<pentry minsize="1" maxsize="8"><register name="RAX"/></pentry>- <pentry minsize="9" maxsize="16">- <addr space="join" piece2="RAX" piece1="RBX"/>+ <pentry minsize="1" maxsize="8">+ <register name="RBX"/></pentry>- <pentry minsize="17" maxsize="24">- <addr space="join" piece3="RAX" piece2="RBX" piece1="RCX"/>+ <pentry minsize="1" maxsize="8">+ <register name="RCX"/></pentry>- <pentry minsize="25" maxsize="32">- <addr space="join" piece4="RAX" piece3="RBX" piece2="RCX" piece1="RDI"/>+ <pentry minsize="1" maxsize="8">+ <register name="RDI"/></pentry>- <pentry minsize="33" maxsize="40">- <addr space="join" piece5="RAX" piece4="RBX" piece3="RCX" piece2="RDI" piece1="RSI"/>+ <pentry minsize="1" maxsize="8">+ <register name="RSI"/></pentry>- <pentry minsize="41" maxsize="48">- <addr space="join" piece6="RAX" piece5="RBX" piece4="RCX" piece3="RDI" piece2="RSI" piece1="R8"/>+ <pentry minsize="1" maxsize="8">+ <register name="R8"/></pentry>- <pentry minsize="49" maxsize="56">- <addr space="join" piece7="RAX" piece6="RBX" piece5="RCX" piece4="RDI" piece3="RSI" piece2="R8" piece1="R9"/>+ <pentry minsize="1" maxsize="8">+ <register name="R9"/>+ </pentry>+ <pentry minsize="1" maxsize="8">+ <register name="R10"/></pentry>- <pentry minsize="57" maxsize="64">- <addr space="join" piece8="RAX" piece7="RBX" piece6="RCX" piece5="RDI" piece4="RSI" piece3="R8" piece2="R9" piece1="R10"/>+ <pentry minsize="1" maxsize="8">+ <register name="R11"/></pentry>- <pentry minsize="65" maxsize="72">- <addr space="join" piece9="RAX" piece8="RBX" piece7="RCX" piece6="RDI" piece5="RSI" piece4="R8" piece3="R9" piece2="R10" piece1="R11"/>++ <pentry minsize="1" maxsize="500" align="1">+ <addr offset="8" space="stack"/></pentry>+ <rule>+ <datatype name="int" minsize="9" maxsize="16"/>+ <join/>+ </rule>+ <rule>+ <datatype name="uint" minsize="9" maxsize="16"/>+ <join/>+ </rule>+ <rule>+ <datatype name="float"/>+ <consume storage="float"/>+ </rule>+ <rule>+ <datatype name="float"/>+ <goto_stack/>+ </rule>+ <rule>+ <datatype name="array" minelements="2"/>+ <goto_stack/>+ </rule>+ <rule>+ <datatype name="any"/>+ <join_per_primitive/>+ </rule></output>+ <sharestack first="input" align="8"/><killedbycall><register name="RAX"/>diff --git a/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/program/model/lang/AbstractProtoModelTest.java b/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/program/model/lang/AbstractProtoModelTest.javaindex 0b33a62623..9e779b14b9 100644--- a/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/program/model/lang/AbstractProtoModelTest.java+++ b/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/program/model/lang/AbstractProtoModelTest.java@@ -102,6 +102,8 @@ public class AbstractProtoModelTest extends AbstractGenericTest {dtManager.addDataType(new Float16DataType(), null);dtManager.addDataType(new Undefined4DataType(), null);dtManager.addDataType(new Undefined8DataType(), null);+ dtManager.addDataType(new BooleanDataType(), null);+ dtManager.addDataType(new Int32TDataType(), null);}finally {dtManager.endTransaction(txID, true);@@ -166,6 +168,18 @@ public class AbstractProtoModelTest extends AbstractGenericTest {parseJoin(name, res);return res;}+ else if (name.startsWith("pad")) {+ int pos = name.indexOf(':');+ int size = 1;+ if (pos != -1) {+ String sizeString = name.substring(pos + 1);+ size = Integer.parseInt(sizeString);+ }+ AddressSpace spc = cspec.getAddressSpace("const");+ Varnode vn = new Varnode(spc.getAddress(0), size);+ res.add(vn);+ return res;+ }String regname;int pos = name.indexOf(':');int sz = 0;@@ -204,7 +218,7 @@ public class AbstractProtoModelTest extends AbstractGenericTest {protected void parseStores(ArrayList<ArrayList<Varnode>> res, String names) {String[] split = names.split(",");for (String el : split) {- ArrayList<Varnode> vnList = parseStore(el);+ ArrayList<Varnode> vnList = parseStore(el.trim());res.add(vnList);}}diff --git a/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/program/model/lang/X64GoPrototypeModelTest.java b/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/program/model/lang/X64GoPrototypeModelTest.javanew file mode 100644index 0000000000..159166c107--- /dev/null+++ b/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/program/model/lang/X64GoPrototypeModelTest.java@@ -0,0 +1,54 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.program.model.lang;++import org.junit.Before;+import org.junit.Test;++public class X64GoPrototypeModelTest extends AbstractProtoModelTest {++ @Before+ public void setUp() throws Exception {+ buildArchitecture("x86:LE:64:default:golang");+ }++ @Test+ public void testAbiInternal() throws Exception {+ PrototypeModel model = cspec.getCallingConvention("abi-internal");++ test(model, "void func(short a,int b,char c)", "void,AX,RBX,CL");+ test(model, "void func(float a,float b)", "void,XMM0:4,XMM1:4");+ test(model, "void func(float a,double c)", "void,XMM0:4,XMM1:8");+ test(model,+ "void func(int a,int b,int c,int d,int e,float f,float g,int h,int i,int j,int k,int l)",+ "void,RAX,RBX,RCX,RDI,RSI,XMM0:4,XMM1:4,R8,R9,R10,R11,stack8:8");+ test(model,+ "void func(float a,float b,float c,float d,float e,float f,float g,float h," ++ "float i,float j,float k,float l,float m,float n,float o,float p)",+ "void,XMM0:4,XMM1:4,XMM2:4,XMM3:4,XMM4:4,XMM5:4,XMM6:4,XMM7:4,XMM8:4," ++ "XMM9:4,XMM10:4,XMM11:4,XMM12:4,XMM13:4,XMM14:4,stack8:4");+ test(model, "void func(int a,int[3] b,int c)", "void,RAX,stack8:24,RBX");+ parseStructure("Person", "bool,int32_t");+ parseStructure("Myriad", "int,bool,Person,float");+ test(model, "void func(Person a)", "void,join EBX pad:3 AL");+ test(model, "void func(bool a,Myriad b)", "void,AL,join XMM0:4 ESI pad:3 DIL pad:3 CL RBX");+ test(model, "Person func(void)", "join EBX pad:3 AL");+ test(model, "double[5] func(int a,int[10] b)", "stack58:40,RAX,stack8:80");+ test(model, "char[5] func(char[3] a,char[7] b,int c)", "stack18:5,stack8:3,stackb:7,RAX");+ test(model, "void func(float a,int[1] b,int[2] c)", "void,XMM0:4,RAX,stack8:16");+ }++}