Ghidra is a software reverse engineering (SRE) framework
Add dynamic symbols to recommend mechanism
caheckmanb2a422329c3d
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.ccindex b5a01672d8..91edc6d58c 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.cc@@ -312,23 +312,26 @@ ScopeLocal::ScopeLocal(AddrSpace *spc,Funcdata *fd,Architecture *g) : ScopeIntervoid ScopeLocal::collectNameRecs(void){- SymbolNameTree::iterator iter;-nameRecommend.clear(); // Clear out any old name recommendations+ dynRecommend.clear();- iter = nametree.begin();+ SymbolNameTree::iterator iter = nametree.begin();while(iter!=nametree.end()) {Symbol *sym = *iter++;if (sym->isNameLocked()&&(!sym->isTypeLocked())) {SymbolEntry *entry = sym->getFirstWholeMap();if (entry != (SymbolEntry *)0) {- if (entry->isDynamic()) continue; // Don't collect names for dynamic mappings- Address usepoint;- if (!entry->getUseLimit().empty()) {- const Range *range = entry->getUseLimit().getFirstRange();- usepoint = Address(range->getSpace(),range->getFirst());+ if (entry->isDynamic()) {+ addDynamicRecommend(entry->getFirstUseAddress(), entry->getHash(), sym->getName());+ }+ else {+ Address usepoint;+ if (!entry->getUseLimit().empty()) {+ const Range *range = entry->getUseLimit().getFirstRange();+ usepoint = Address(range->getSpace(),range->getFirst());+ }+ addRecommendName( entry->getAddr(), usepoint, sym->getName(), entry->getSize() );}- addRecommendName( entry->getAddr(), usepoint, sym->getName(), entry->getSize() );if (sym->getCategory()<0)removeSymbol(sym);}@@ -1218,6 +1221,25 @@ void ScopeLocal::makeNameRecommendationsForSymbols(vector<string> &resname,vecto++biter;}}++ if (dynRecommend.empty()) return;++ list<DynamicRecommend>::const_iterator dyniter;+ DynamicHash dhash;+ for(dyniter=dynRecommend.begin();dyniter!=dynRecommend.end();++dyniter) {+ dhash.clear();+ const DynamicRecommend &dynEntry(*dyniter);+ Varnode *vn = dhash.findVarnode(fd, dynEntry.getAddress(), dynEntry.getHash());+ if (vn == (Varnode *)0) continue;+ if (vn->isAnnotation()) continue;+ Symbol *sym = vn->getHigh()->getSymbol();+ if (sym != (Symbol *)0) {+ if (sym->isNameUndefined()) {+ resname.push_back( dynEntry.getName() );+ ressym.push_back(sym);+ }+ }+ }}/// \brief Add a new recommended name to the list@@ -1233,3 +1255,16 @@ void ScopeLocal::addRecommendName(const Address &addr,const Address &usepoint,co{nameRecommend[ AddressUsePointPair(addr,usepoint,sz) ] = nm;}++/// \brief Add a new recommended name for a dynamic storage location to the list+///+/// This recommended name is assigned a storage location via the DynamicHash mechanism.+/// The name may be reattached to a Symbol after decompilation.+/// \param addr is the address of the code use point+/// \param hash is the hash encoding context for identifying the storage location+/// \param nm is the recommended name+void ScopeLocal::addDynamicRecommend(const Address &usepoint,uint8 hash,const string &nm)++{+ dynRecommend.push_back(DynamicRecommend(usepoint,hash,nm));+}diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.hhindex f85e0c8153..996baadf35 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.hh@@ -40,6 +40,25 @@ public:bool operator==(const AddressUsePointPair &op2) const; ///< Test for equality};+/// \brief A name recommendation for a particular dynamic location+///+/// A recommendation for a symbol name whose storage is dynamic. The storage+/// is identified using the DynamicHash mechanism and may or may not exist.+class DynamicRecommend {+ Address usePoint; ///< Use point of the Symbol+ uint8 hash; ///< Hash encoding the Symbols environment+ string name; ///< The local symbol name recommendation+public:+ DynamicRecommend(const Address &addr,uint8 h,const string &nm) :+ usePoint(addr) {+ hash = h;+ name = nm;+ } ///< Constructor+ const Address &getAddress(void) const { return usePoint; } ///< Get the use point address+ uint8 getHash(void) const { return hash; } ///< Get the dynamic hash+ string getName(void) const { return name; } ///< Get the recommended name+};+/// \brief Partial data-type information mapped to a specific range of bytes////// This object gives a hint about the data-type for a sequence of bytes@@ -160,6 +179,7 @@ class ScopeLocal : public ScopeInternal {AddrSpace *space; ///< Address space containing the local stackRangeList localRange; ///< The set of addresses that might hold mapped locals (not parameters)map<AddressUsePointPair,string> nameRecommend; ///< Symbol name recommendations for specific addresses+ list<DynamicRecommend> dynRecommend; ///< Symbol name recommendations for dynamic locationsbool stackGrowsNegative; ///< Marked \b true if the stack is considered to \e grow towards smaller offsetsbool rangeLocked; ///< True if the subset of addresses \e mapped to \b this scope has been lockedbool adjustFit(RangeHint &a) const; ///< Make the given RangeHint fit in the current Symbol map@@ -167,6 +187,8 @@ class ScopeLocal : public ScopeInternal {bool restructure(MapState &state); ///< Merge hints into a formal Symbol layout of the address spacevoid markUnaliased(const vector<uintb> &alias); ///< Mark all local symbols for which there are no aliasesvoid fakeInputSymbols(void); ///< Make sure all stack inputs have an associated Symbol+ void addRecommendName(const Address &addr,const Address &usepoint,const string &nm,int4 sz);+ void addDynamicRecommend(const Address &usepoint,uint8 hash,const string &nm);void collectNameRecs(void); ///< Collect names of unlocked Symbols on the stackpublic:ScopeLocal(AddrSpace *spc,Funcdata *fd,Architecture *g); ///< Constructor@@ -193,7 +215,6 @@ public:void restructureVarnode(bool aliasyes); ///< Layout mapped symbols based on Varnode informationvoid restructureHigh(void); ///< Layout mapped symbols based on HighVariable informationvoid makeNameRecommendationsForSymbols(vector<string> &resname,vector<Symbol *> &ressym) const;- void addRecommendName(const Address &addr,const Address &usepoint,const string &nm,int4 sz);};#endif