Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-7063 New detection of symbol conflicts

caheckman6740b8992610
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/constseq.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/constseq.cc
index 046f60f9d9..5029faff88 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/constseq.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/constseq.cc
@@ -185,7 +185,7 @@ uint4 ArraySequence::selectStringCopyFunction(int4 &index)
/// \param ent is the given Symbol
/// \param root is the COPY holding the constant
/// \param addr is the Address being COPYed into
-StringSequence::StringSequence(Funcdata &fdata,Datatype *ct,SymbolEntry *ent,PcodeOp *root,const Address &addr)
+StringSequence::StringSequence(Funcdata &fdata,Datatype *ct,MapEntry *ent,PcodeOp *root,const Address &addr)
: ArraySequence(fdata,ct,root)
{
rootAddr = addr;
@@ -987,8 +987,8 @@ int4 RuleStringCopy::applyOp(PcodeOp *op,Funcdata &data)
if (!ct->isCharPrint()) return 0; // Copied to a "char" data-type Varnode
if (ct->isOpaqueString()) return 0;
if (!outvn->isAddrTied()) return 0;
- SymbolEntry *entry = data.getScopeLocal()->queryContainer(outvn->getAddr(), outvn->getSize(), op->getAddr());
- if (entry == (SymbolEntry *)0)
+ MapEntry *entry = data.getScopeLocal()->queryContainer(outvn->getAddr(), outvn->getSize(), op->getAddr());
+ if (entry == (MapEntry *)0)
return 0;
StringSequence sequence(data,ct,entry,op,outvn->getAddr());
if (!sequence.isValid())
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/constseq.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/constseq.hh
index 153c4d6d57..0e6a0ba8e5 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/constseq.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/constseq.hh
@@ -22,6 +22,8 @@
namespace ghidra {
+class MapEntry;
+
/// \brief A sequence of PcodeOps that move data in-to/out-of an array data-type.
///
/// A container for a sequence of PcodeOps within a basic block where we are trying to determine if the sequence
@@ -66,7 +68,7 @@ public:
class StringSequence : public ArraySequence {
Address rootAddr; ///< Address within the memory region associated with the root PcodeOp
Address startAddr; ///< Starting address of the memory region
- SymbolEntry *entry; ///< Symbol at the root Address
+ MapEntry *entry; ///< Symbol at the root Address
bool collectCopyOps(int size); ///< Collect ops COPYing constants into the memory region
PcodeOp *buildStringCopy(void); ///< Build the strncpy,wcsncpy, or memcpy function with string as input
static void removeForward(const WriteNode &curNode,map<PcodeOp *,list<WriteNode>::iterator> &xref,
@@ -74,7 +76,7 @@ class StringSequence : public ArraySequence {
void removeCopyOps(PcodeOp *replaceOp); ///< Remove all the COPY ops from the basic block
Varnode *constructTypedPointer(PcodeOp *insertPoint);
public:
- StringSequence(Funcdata &fdata,Datatype *ct,SymbolEntry *ent,PcodeOp *root,const Address &addr);
+ StringSequence(Funcdata &fdata,Datatype *ct,MapEntry *ent,PcodeOp *root,const Address &addr);
bool transform(void); ///< Transform COPYs into a single memcpy user-op
};
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
index f6fe8b2deb..2a4af3462b 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
@@ -1071,9 +1071,8 @@ bool ActionConstantPtr::checkCopy(PcodeOp *op,Funcdata &data)
/// \param fullEncoding will hold the full pointer encoding being passed back
/// \param data is the function being analyzed
/// \return the recovered symbol or NULL
-SymbolEntry *ActionConstantPtr::isPointer(AddrSpace *spc,Varnode *vn,PcodeOp *op,int4 slot,
- Address &rampoint,uintb &fullEncoding,Funcdata &data)
-
+MapEntry *ActionConstantPtr::isPointer(AddrSpace *spc,Varnode *vn,PcodeOp *op,int4 slot,
+ Address &rampoint,uintb &fullEncoding,Funcdata &data)
{
bool needexacthit;
vn->setSymbolCheck(Varnode::symcheck_complete);
@@ -1084,7 +1083,7 @@ SymbolEntry *ActionConstantPtr::isPointer(AddrSpace *spc,Varnode *vn,PcodeOp *op
needexacthit = false;
}
else {
- if (vn->isTypeLock()) return (SymbolEntry *)0; // Locked as NOT a pointer
+ if (vn->isTypeLock()) return (MapEntry *)0; // Locked as NOT a pointer
needexacthit = true;
// Check if the constant is involved in a potential pointer expression
// as the base
@@ -1093,22 +1092,22 @@ SymbolEntry *ActionConstantPtr::isPointer(AddrSpace *spc,Varnode *vn,PcodeOp *op
case CPUI_CALLIND:
{
if (slot==0)
- return (SymbolEntry *)0;
+ return (MapEntry *)0;
// A constant parameter could be a pointer
FuncCallSpecs *fc = data.getCallSpecs(op);
if (fc != (FuncCallSpecs *)0 && fc->isInputLocked() && fc->numParams() > slot-1) {
type_metatype meta = fc->getParam(slot-1)->getType()->getMetatype();
if (meta != TYPE_PTR && meta != TYPE_UNKNOWN) {
- return (SymbolEntry *)0; // Definitely not passing a pointer
+ return (MapEntry *)0; // Definitely not passing a pointer
}
}
else if (!glb->infer_pointers)
- return (SymbolEntry *)0;
+ return (MapEntry *)0;
break;
}
case CPUI_COPY:
if (!checkCopy(op, data))
- return (SymbolEntry *)0;
+ return (MapEntry *)0;
break;
case CPUI_PIECE:
// Pointers get concatenated in structures
@@ -1118,43 +1117,43 @@ SymbolEntry *ActionConstantPtr::isPointer(AddrSpace *spc,Varnode *vn,PcodeOp *op
case CPUI_INT_LESSEQUAL:
// A comparison with a constant could be a pointer
if (!glb->infer_pointers)
- return (SymbolEntry *)0;
+ return (MapEntry *)0;
break;
case CPUI_INT_ADD:
outvn = op->getOut();
if (outvn->getTypeDefFacing()->getMetatype()==TYPE_PTR) {
// Is there another pointer base in this expression
if (op->getIn(1-slot)->getTypeReadFacing(op)->getMetatype()==TYPE_PTR)
- return (SymbolEntry *)0; // If so, we are not a pointer
+ return (MapEntry *)0; // If so, we are not a pointer
// FIXME: need to fully explore additive tree
needexacthit = false;
}
else if (!glb->infer_pointers)
- return (SymbolEntry *)0;
+ return (MapEntry *)0;
break;
case CPUI_STORE:
if (slot != 2)
- return (SymbolEntry *)0;
+ return (MapEntry *)0;
break;
default:
- return (SymbolEntry *)0;
+ return (MapEntry *)0;
}
// Make sure the constant is in the expected range for a pointer
if (spc->getPointerLowerBound() > vn->getOffset())
- return (SymbolEntry *)0;
+ return (MapEntry *)0;
if (spc->getPointerUpperBound() < vn->getOffset())
- return (SymbolEntry *)0;
+ return (MapEntry *)0;
// Check if the constant looks like a single bit or mask
if (bit_transitions(vn->getOffset(),vn->getSize()) < 3)
- return (SymbolEntry *)0;
+ return (MapEntry *)0;
rampoint = glb->resolveConstant(spc,vn->getOffset(),vn->getSize(),op->getAddr(),fullEncoding);
}
- if (rampoint.isInvalid()) return (SymbolEntry *)0;
+ if (rampoint.isInvalid()) return (MapEntry *)0;
// Since we are looking for a global address
// Assume it is address tied and use empty usepoint
- SymbolEntry *entry = data.getScopeLocal()->getParent()->queryContainer(rampoint,1,Address());
- if (entry != (SymbolEntry *)0) {
+ MapEntry *entry = data.getScopeLocal()->getParent()->queryContainer(rampoint,1,Address());
+ if (entry != (MapEntry *)0) {
Datatype *ptrType = entry->getSymbol()->getType();
if (ptrType->getMetatype() == TYPE_ARRAY) {
Datatype *ct = ((TypeArray *)ptrType)->getBase();
@@ -1165,7 +1164,7 @@ SymbolEntry *ActionConstantPtr::isPointer(AddrSpace *spc,Varnode *vn,PcodeOp *op
}
if (needexacthit && entry->getAddr() != rampoint) {
vn->setSymbolCheck(Varnode::symcheck_incomplete); // May need to retest if we later discover vn is a pointer
- return (SymbolEntry *)0;
+ return (MapEntry *)0;
}
}
return entry;
@@ -1183,7 +1182,7 @@ int4 ActionConstantPtr::apply(Funcdata &data)
VarnodeLocSet::const_iterator begiter,enditer;
Architecture *glb = data.getArch();
AddrSpace *cspc = glb->getConstantSpace();
- SymbolEntry *entry;
+ MapEntry *entry;
Varnode *vn;
begiter = data.beginLoc(cspc);
@@ -1214,7 +1213,7 @@ int4 ActionConstantPtr::apply(Funcdata &data)
Address rampoint;
uintb fullEncoding;
entry = isPointer(rspc,vn,op,slot,rampoint,fullEncoding,data);
- if (entry != (SymbolEntry *)0) {
+ if (entry != (MapEntry *)0) {
data.spacebaseConstant(op,slot,entry,rampoint,fullEncoding,vn->getSize());
if ((opc == CPUI_INT_ADD)&&(slot==1))
data.opSwapInput(op,0,1);
@@ -5066,12 +5065,12 @@ int4 ActionDynamicMapping::apply(Funcdata &data)
{
ScopeLocal *localmap = data.getScopeLocal();
- list<SymbolEntry>::iterator iter,enditer;
+ list<DynamicEntry *>::iterator iter,enditer;
iter = localmap->beginDynamic();
enditer = localmap->endDynamic();
DynamicHash dhash;
while(iter != enditer) {
- SymbolEntry *entry = &(*iter);
+ DynamicEntry *entry = *iter;
++iter;
if (data.attemptDynamicMapping(entry,dhash))
count += 1;
@@ -5083,12 +5082,12 @@ int4 ActionDynamicSymbols::apply(Funcdata &data)
{
ScopeLocal *localmap = data.getScopeLocal();
- list<SymbolEntry>::iterator iter,enditer;
+ list<DynamicEntry *>::iterator iter,enditer;
iter = localmap->beginDynamic();
enditer = localmap->endDynamic();
DynamicHash dhash;
while(iter != enditer) {
- SymbolEntry *entry = &(*iter);
+ DynamicEntry *entry = *iter;
++iter;
if (data.attemptDynamicMappingLate(entry, dhash))
count += 1;
@@ -5233,7 +5232,9 @@ void ActionInferTypes::buildLocaltypes(Funcdata &data)
bool needsBlock = false;
SymbolEntry *entry = vn->getSymbolEntry();
if (entry != (SymbolEntry *)0 && !vn->isTypeLock() && entry->getSymbol()->isTypeLocked()) {
- int4 curOff = (vn->getAddr().getOffset() - entry->getAddr().getOffset()) + entry->getOffset();
+ int4 curOff = entry->getOffset();
+ if (!entry->isDynamic())
+ 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 UNKNOWN
ct = vn->getLocalType(needsBlock); // Let data-type float, even though parent symbol is type-locked
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hh
index b1cc32f523..a676275cb5 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hh
@@ -190,8 +190,8 @@ class ActionConstantPtr : public Action {
static AddrSpace *searchForSpaceAttribute(Varnode *vn,PcodeOp *op);
static AddrSpace *selectInferSpace(Varnode *vn,PcodeOp *op,const vector<AddrSpace *> &spaceList);
static bool checkCopy(PcodeOp *op,Funcdata &data);
- static SymbolEntry *isPointer(AddrSpace *spc,Varnode *vn,PcodeOp *op,int4 slot,
- Address &rampoint,uintb &fullEncoding,Funcdata &data);
+ static MapEntry *isPointer(AddrSpace *spc,Varnode *vn,PcodeOp *op,int4 slot,
+ Address &rampoint,uintb &fullEncoding,Funcdata &data);
public:
ActionConstantPtr(const string &g) : Action(0,"constantptr",g) {} ///< Constructor
virtual void reset(Funcdata &data) { localcount = 0; }
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/database.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/database.cc
index 0125d99de2..26b68a62af 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/database.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/database.cc
@@ -44,68 +44,49 @@ ElementId ELEM_SYMBOLLIST = ElementId("symbollist",81);
uint8 Symbol::ID_BASE = 0x4000000000000000L;
-/// This SymbolEntry is unintegrated. An address or hash must be provided
-/// either directly or via decode().
+/// Get data used to sub-sort entries (in a rangemap) at the same address
+/// \return the sub-sort object
+SymbolRange::subsorttype SymbolRange::getSubsort(void) const
+
+{
+ subsorttype res; // Minimal subsort
+ if ((entry->getSymbol()->getFlags()&Varnode::addrtied)==0) {
+ const Range *range = entry->getUseLimit().getFirstRange();
+ if (range == (const Range *)0)
+ throw LowlevelError("Map entry with empty uselimit");
+ res.useindex = range->getSpace()->getIndex();
+ res.useoffset = range->getFirst();
+ }
+ return res;
+}
+
+/// For use with decode().
/// \param sym is the Symbol \b this will be a map for
SymbolEntry::SymbolEntry(Symbol *sym)
: symbol(sym)
{
extraflags = 0;
offset = 0;
- hash = 0;
size = -1;
+ entrytype = map_entry;
+ is_piece = false;
}
-/// This is used specifically for \e dynamic Symbol objects, where the storage location
-/// is attached to a temporary register or a constant. The main address field (\b addr)
-/// is set to \e invalid, and the \b hash becomes the primary location information.
-/// \param sym is the underlying Symbol
-/// \param exfl are the Varnode flags associated with the storage location
-/// \param h is the hash
-/// \param off if the offset into the Symbol for this (piece of) storage
-/// \param sz is the size in bytes of this (piece of) storage
-/// \param rnglist is the set of code addresses where \b this SymbolEntry represents the Symbol
-SymbolEntry::SymbolEntry(Symbol *sym,uint4 exfl,uint8 h,int4 off,int4 sz,const RangeList &rnglist)
+/// \param sym is the Symbol \b this will be a map for
+/// \param exflags are Varnode flags to be associated with the Symbol
+/// \param sz is the number of bytes needed to store the Symbol (or \b this part of the Symbol) in memory
+/// \param off is the byte offset into the Symbol, if \b this represents a piece
+/// \param use is the range of code addresses where \b this mapping applies
+SymbolEntry::SymbolEntry(Symbol *sym,uint4 exflags,int4 sz,int4 off,const RangeList &use)
{
+ size = sz;
symbol = sym;
- extraflags = exfl;
- addr = Address();
- hash = h;
+ extraflags = exflags;
offset = off;
- size = sz;
- uselimit = rnglist;
-}
-
-/// Establish the boundary offsets and fill in additional data
-/// \param data contains the raw initialization data
-/// \param a is the starting offset of the entry
-/// \param b is the ending offset of the entry
-SymbolEntry::SymbolEntry(const EntryInitData &data,uintb a,uintb b)
-
-{
- addr = Address(data.space,a);
- size = (b-a)+1;
- symbol = data.symbol;
- extraflags = data.extraflags;
- offset = data.offset;
- uselimit = data.uselimit;
-}
-
-/// Get data used to sub-sort entries (in a rangemap) at the same address
-/// \return the sub-sort object
-SymbolEntry::subsorttype SymbolEntry::getSubsort(void) const
-
-{
- subsorttype res; // Minimal subsort
- if ((symbol->getFlags()&Varnode::addrtied)==0) {
- const Range *range = uselimit.getFirstRange();
- if (range == (const Range *)0)
- throw LowlevelError("Map entry with empty uselimit");
- res.useindex = range->getSpace()->getIndex();
- res.useoffset = range->getFirst();
- }
- return res;
+ uselimit = use;
+ entrytype = map_entry;
+ is_piece = false;
}
/// This storage location may only hold the Symbol value for a limited portion of the code.
@@ -143,36 +124,31 @@ bool SymbolEntry::updateType(Varnode *vn) const
return false;
}
-/// Return the data-type that matches the given size and address within \b this storage.
-/// NULL is returned if there is no valid sub-type matching the size.
-/// \param inaddr is the given address
-/// \param sz is the given size (in bytes)
-/// \return the matching data-type or NULL
-Datatype *SymbolEntry::getSizedType(const Address &inaddr,int4 sz) const
-
+/// \param sym is the Symbol \b this will be a map for
+/// \param exflags are Varnode flags to be associated with the Symbol
+/// \param ad is the starting address of the mapped range
+/// \param sz is the number of bytes in the range
+/// \param off is the byte offset into the Symbol, if \b this represents a piece
+/// \param use is the range of code addresses where \b this mapping applies
+MapEntry::MapEntry(Symbol *sym,uint4 exflags,const Address &ad,int4 sz,int4 off,const RangeList &use)
+ : SymbolEntry(sym,exflags,sz,off,use), addr(ad)
{
- int4 off;
+}
- if (isDynamic())
- off = offset;
- else
- off = (int4)(inaddr.getOffset() - addr.getOffset()) + offset;
+Datatype *MapEntry::getSizedType(const Address &inaddr,int4 sz) const
+
+{
+ int4 off = (int4)(inaddr.getOffset() - addr.getOffset()) + offset;
Datatype *cur = symbol->getType();
return symbol->getScope()->getArch()->types->getExactPiece(cur, off, sz);
}
-/// Give a contained one-line description of \b this storage, suitable for a debug console
-/// \param s is the output stream
-void SymbolEntry::printEntry(ostream &s) const
+void MapEntry::printEntry(ostream &s) const
{
s << symbol->getName() << " : ";
- if (addr.isInvalid())
- s << "<dynamic>";
- else {
- s << addr.getShortcut();
- addr.printRaw(s);
- }
+ s << addr.getShortcut();
+ addr.printRaw(s);
s << ':' << dec << (uint4) symbol->getType()->getSize();
s << ' ';
symbol->getType()->printRaw(s);
@@ -180,46 +156,120 @@ void SymbolEntry::printEntry(ostream &s) const
uselimit.printBounds(s);
}
-/// This writes elements internal to the \<mapsym> element associated with the Symbol.
-/// It encodes the address element (or the \<hash> element for dynamic symbols) and
-/// a \<rangelist> element associated with the \b uselimit.
-/// \param encoder is the stream encoder
-void SymbolEntry::encode(Encoder &encoder) const
+void MapEntry::encode(Encoder &encoder) const
{
if (isPiece()) return; // Don't save a piece
- if (addr.isInvalid()) {
- encoder.openElement(ELEM_HASH);
- encoder.writeUnsignedInteger(ATTRIB_VAL, hash);
- encoder.closeElement(ELEM_HASH);
- }
- else
- addr.encode(encoder);
+ addr.encode(encoder);
uselimit.encode(encoder);
}
-/// Parse either an \<addr> element for storage information or a \<hash> element
-/// if the symbol is dynamic. Then parse the \b uselimit describing the valid
-/// range of code addresses.
-/// \param decoder is the stream decoder
-/// \return the advanced iterator
-void SymbolEntry::decode(Decoder &decoder)
+void MapEntry::decode(Decoder &decoder)
{
- uint4 elemId = decoder.peekElement();
- if (elemId == ELEM_HASH) {
- decoder.openElement();
- hash = decoder.readUnsignedInteger(ATTRIB_VAL);
- addr = Address();
- decoder.closeElement(elemId);
- }
- else {
- addr = Address::decode(decoder);
- hash = 0;
- }
+ addr = Address::decode(decoder);
+ if (addr.isInvalid())
+ throw LowlevelError("Invalid address decoding MapEntry");
+ uselimit.decode(decoder);
+}
+
+/// \param sym is the Symbol \b this will be a map for
+/// \param vn is the Varnode with a storage conflict (must be written)
+MapEntryConflict::MapEntryConflict(Symbol *sym,Varnode *vn)
+ : MapEntry(sym)
+{
+ addr = vn->getAddr();
+ size = vn->getSize();
+ const SeqNum &seqNum(vn->getDef()->getSeqNum());
+ uniq = seqNum.getTime();
+
+ uselimit.insertRange(seqNum.getAddr().getSpace(),seqNum.getAddr().getOffset(),seqNum.getAddr().getOffset());
+ entrytype = conflict_entry;
+}
+
+void MapEntryConflict::printEntry(ostream &s) const
+
+{
+ MapEntry::printEntry(s);
+ s << " (conflicts)";
+}
+
+void MapEntryConflict::encode(Encoder &encoder) const
+
+{
+ if (isPiece()) return; // Don't save a piece
+ encoder.openElement(ELEM_HASH);
+ encoder.writeUnsignedInteger(ATTRIB_VAL, 0); // Write as an incomplete DynamicEntry
+ encoder.closeElement(ELEM_HASH);
+ uselimit.encode(encoder);
+}
+
+void MapEntryConflict::decode(Decoder &decoder)
+
+{
+ throw LowlevelError("Cannot decode MapEntryConflict");
+}
+
+/// This is used specifically for \e dynamic Symbol objects, where the storage location
+/// is attached to a temporary register or a constant. The main address field (\b addr)
+/// is set to \e invalid, and the \b hash becomes the primary location information.
+/// \param sym is the underlying Symbol
+/// \param exfl are the Varnode flags associated with the storage location
+/// \param h is the hash
+/// \param off if the offset into the Symbol for this (piece of) storage
+/// \param sz is the size in bytes of this (piece of) storage
+/// \param rnglist is the set of code addresses where \b this SymbolEntry represents the Symbol
+DynamicEntry::DynamicEntry(Symbol *sym,uint4 exfl,uint8 h,int4 off,int4 sz,const RangeList &rnglist)
+ : SymbolEntry(sym,exfl,sz,off,rnglist)
+{
+ hash = h;
+ entrytype = dynamic_entry;
+}
+
+Datatype *DynamicEntry::getSizedType(const Address &inaddr,int4 sz) const
+
+{
+ Datatype *cur = symbol->getType();
+ return symbol->getScope()->getArch()->types->getExactPiece(cur, offset, sz);
+}
+
+void DynamicEntry::printEntry(ostream &s) const
+
+{
+ s << symbol->getName() << " : <dynamic>:";
+ s << dec << (uint4) symbol->getType()->getSize();
+ s << ' ';
+ symbol->getType()->printRaw(s);
+ s << " : ";
+ uselimit.printBounds(s);
+}
+
+void DynamicEntry::encode(Encoder &encoder) const
+
+{
+ if (isPiece()) return; // Don't save a piece
+ encoder.openElement(ELEM_HASH);
+ encoder.writeUnsignedInteger(ATTRIB_VAL, hash);
+ encoder.closeElement(ELEM_HASH);
+ uselimit.encode(encoder);
+}
+
+void DynamicEntry::decode(Decoder &decoder)
+
+{
+ uint4 elemId = decoder.openElement(ELEM_HASH);
+ hash = decoder.readUnsignedInteger(ATTRIB_VAL);
+ decoder.closeElement(elemId);
uselimit.decode(decoder);
}
+Symbol::~Symbol(void)
+
+{
+ for(vector<SymbolEntry *>::iterator iter=mapentry.begin();iter!=mapentry.end();++iter)
+ delete *iter;
+}
+
/// Examine the data-type to decide if the Symbol has the special property
/// called \b size_typelock, which indicates the \e size of the Symbol
/// is locked, but the data-type is not locked (and can float)
@@ -270,19 +320,20 @@ SymbolEntry *Symbol::getFirstWholeMap(void) const
{
if (mapentry.empty())
throw LowlevelError("No mapping for symbol: "+name);
- return &(*mapentry[0]);
+ return mapentry[0];
}
/// This method may return a \e partial entry, where the SymbolEntry is only holding
/// part of the whole Symbol.
/// \param addr is an address within the desired storage location of the Symbol
/// \return the first matching SymbolEntry
-SymbolEntry *Symbol::getMapEntry(const Address &addr) const
+MapEntry *Symbol::getMapEntry(const Address &addr) const
{
- SymbolEntry *res;
for(int4 i=0;i<mapentry.size();++i) {
- res = &(*mapentry[i]);
+ SymbolEntry *tmp = mapentry[i];
+ if (tmp->isDynamic()) continue;
+ MapEntry *res = (MapEntry *)tmp;
const Address &entryaddr( res->getAddr() );
if (addr.getSpace() != entryaddr.getSpace()) continue;
if (addr.getOffset() < entryaddr.getOffset()) continue;
@@ -291,7 +342,7 @@ SymbolEntry *Symbol::getMapEntry(const Address &addr) const
return res;
}
// throw LowlevelError("No mapping at desired address for symbol: "+name);
- return (SymbolEntry *)0;
+ return (MapEntry *)0;
}
/// Among all the SymbolEntrys that map \b this entire Symbol, calculate
@@ -303,7 +354,7 @@ int4 Symbol::getMapEntryPosition(const SymbolEntry *entry) const
{
int4 pos = 0;
for(int4 i=0;i<mapentry.size();++i) {
- const SymbolEntry *tmp = &(*mapentry[i]);
+ const SymbolEntry *tmp = mapentry[i];
if (tmp == entry)
return pos;
if (entry->getSize() == type->getSize())
@@ -558,7 +609,7 @@ Funcdata *FunctionSymbol::getFunction(void)
{
if (fd != (Funcdata *)0) return fd;
- SymbolEntry *entry = getFirstWholeMap();
+ MapEntry *entry = (MapEntry *)getFirstWholeMap();
fd = new Funcdata(name,displayName,scope,entry->getAddr(),this);
return fd;
}
@@ -914,13 +965,13 @@ const Scope *Scope::stackAddr(const Scope *scope1,
const Scope *scope2,
const Address &addr,
const Address &usepoint,
- SymbolEntry **addrmatch)
+ MapEntry **addrmatch)
{
- SymbolEntry *entry;
+ MapEntry *entry;
if (addr.isConstant()) return (const Scope *)0;
while((scope1 != (const Scope *)0)&&(scope1 != scope2)) {
entry = scope1->findAddr(addr,usepoint);
- if (entry != (SymbolEntry *)0) {
+ if (entry != (MapEntry *)0) {
*addrmatch = entry;
return scope1;
}
@@ -948,13 +999,13 @@ const Scope *Scope::stackContainer(const Scope *scope1,
const Scope *scope2,
const Address &addr,int4 size,
const Address &usepoint,
- SymbolEntry **addrmatch)
+ MapEntry **addrmatch)
{
- SymbolEntry *entry;
+ MapEntry *entry;
if (addr.isConstant()) return (const Scope *)0;
while((scope1 != (const Scope *)0)&&(scope1 != scope2)) {
entry = scope1->findContainer(addr,size,usepoint);
- if (entry != (SymbolEntry *)0) {
+ if (entry != (MapEntry *)0) {
*addrmatch = entry;
return scope1;
}
@@ -982,13 +1033,13 @@ const Scope *Scope::stackClosestFit(const Scope *scope1,
const Scope *scope2,
const Address &addr,int4 size,
const Address &usepoint,
- SymbolEntry **addrmatch)
+ MapEntry **addrmatch)
{
- SymbolEntry *entry;
+ MapEntry *entry;
if (addr.isConstant()) return (const Scope *)0;
while((scope1 != (const Scope *)0)&&(scope1 != scope2)) {
entry = scope1->findClosestFit(addr,size,usepoint);
- if (entry != (SymbolEntry *)0) {
+ if (entry != (MapEntry *)0) {
*addrmatch = entry;
return scope1;
}
@@ -1121,66 +1172,78 @@ void Scope::removeRange(AddrSpace *spc,uintb first,uintb last)
rangetree.removeRange(spc,first,last);
}
-/// The mapping is given as an unintegrated SymbolEntry object. Memory
-/// may be specified in terms of join addresses, which this method must unravel.
-/// The \b offset, \b size, and \b extraflags fields of the SymbolEntry are not used.
-/// In particular, the SymbolEntry is assumed to map the entire Symbol.
+/// MapEntry is assumed to map the entire Symbol.
+/// The \e offset, \e size, and \e flags are filled in.
+/// Memory may be specified in terms of join addresses, which this method must unravel.
/// \param entry is the given SymbolEntry
-/// \return a SymbolEntry which has been fully integrated
-SymbolEntry *Scope::addMap(SymbolEntry &entry)
+void Scope::addMap(MapEntry *entry)
{
// First set properties of this symbol based on scope
// entry.symbol->flags |= Varnode::mapped;
if (isGlobal())
- entry.symbol->flags |= Varnode::persist;
- else if (!entry.addr.isInvalid()) {
+ entry->symbol->flags |= Varnode::persist;
// If this is not a global scope, but the address is in the global discovery range
// we still mark the symbol as persistent
- Scope *glbScope = glb->symboltab->getGlobalScope();
- Address addr;
- if (glbScope->inScope(entry.addr, 1, addr)) {
- entry.symbol->flags |= Varnode::persist;
- entry.uselimit.clear(); // FIXME: Kludge for incorrectly generated XML
- }
- }
-
- SymbolEntry *res;
- int4 consumeSize = entry.symbol->getBytesConsumed();
- if (entry.addr.isInvalid())
- res = addDynamicMapInternal(entry.symbol,Varnode::mapped,entry.hash,0,consumeSize,entry.uselimit);
- else {
- if (entry.uselimit.empty()) {
- entry.symbol->flags |= Varnode::addrtied;
- // Global properties (like readonly and volatile)
- // can only happen if use is not limited
- entry.symbol->flags |= glb->symboltab->getProperty(entry.addr);
- }
- res = addMapInternal(entry.symbol,Varnode::mapped,entry.addr,0,consumeSize,entry.uselimit);
- if (entry.addr.isJoin()) {
- // The address is a join, we add extra SymbolEntry maps for each of the pieces
- JoinRecord *rec = glb->findJoin(entry.addr.getOffset());
- uint4 exfl;
- int4 num = rec->numPieces();
- uintb off = 0;
- bool bigendian = entry.addr.isBigEndian();
- for(int4 j=0;j<num;++j) {
- int4 i = bigendian ? j : (num-1-j); // Take pieces in endian order
- const VarnodeData &vdat(rec->getPiece(i));
+ Scope *glbScope = glb->symboltab->getGlobalScope();
+ Address addr;
+ if (glbScope->inScope(entry->addr, 1, addr)) {
+ entry->symbol->flags |= Varnode::persist;
+ entry->uselimit.clear(); // FIXME: Kludge for incorrectly generated XML
+ }
+
+ entry->extraflags = Varnode::mapped;
+ entry->offset = 0;
+ entry->size = entry->symbol->getBytesConsumed();
+ if (entry->uselimit.empty()) {
+ entry->symbol->flags |= Varnode::addrtied;
+ // Global properties (like readonly and volatile)
+ // can only happen if use is not limited
+ entry->symbol->flags |= glb->symboltab->getProperty(entry->addr);
+ }
+ addMapInternal(entry->symbol,entry);
+ if (entry->addr.isJoin()) {
+ // The address is a join, we add extra SymbolEntry maps for each of the pieces
+ JoinRecord *rec = glb->findJoin(entry->addr.getOffset());
+ uint4 exfl;
+ int4 num = rec->numPieces();
+ uintb off = 0;
+ bool bigendian = entry->addr.isBigEndian();
+ for(int4 j=0;j<num;++j) {
+ int4 i = bigendian ? j : (num-1-j); // Take pieces in endian order
+ const 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
- // NOTE: we do not turn on the mapped flag for the pieces
- addMapInternal(entry.symbol,exfl,vdat.getAddr(),off,vdat.size,entry.uselimit);
- off += vdat.size;
}
- // Note: we fall thru here so that we return a SymbolEntry for the unified symbol
+ // 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;
}
}
- return res;
+}
+
+/// The \e offset, \e size, and \e flags are filled in.
+/// \param entry is the given SymbolEntry
+void Scope::addDynamic(DynamicEntry *entry)
+
+{
+ // First set properties of this symbol based on scope
+ // entry.symbol->flags |= Varnode::mapped;
+ if (isGlobal())
+ entry->symbol->flags |= Varnode::persist;
+
+ entry->size = entry->symbol->getBytesConsumed();
+ entry->extraflags = Varnode::mapped;
+ entry->offset = 0;
+ addDynamicMapInternal(entry->symbol,entry);
}
Scope::~Scope(void)
@@ -1232,10 +1295,10 @@ Funcdata *Scope::queryFunction(const string &nm) const
/// \param addr is the given address
/// \param usepoint is the point at which code accesses that address (may be \e invalid)
/// \return the matching SymbolEntry
-SymbolEntry *Scope::queryByAddr(const Address &addr,
- const Address &usepoint) const
+MapEntry *Scope::queryByAddr(const Address &addr,const Address &usepoint) const
+
{
- SymbolEntry *res = (SymbolEntry *)0;
+ MapEntry *res = (MapEntry *)0;
const Scope *basescope = glb->symboltab->mapScope(this,addr,usepoint);
stackAddr(basescope,(const Scope *)0,addr,usepoint,&res);
return res;
@@ -1247,10 +1310,10 @@ SymbolEntry *Scope::queryByAddr(const Address &addr,
/// \param size is the number of bytes in the range
/// \param usepoint is a point at which the Symbol is accessed (may be \e invalid)
/// \return the matching SymbolEntry or NULL
-SymbolEntry *Scope::queryContainer(const Address &addr,int4 size,
- const Address &usepoint) const
+MapEntry *Scope::queryContainer(const Address &addr,int4 size,const Address &usepoint) const
+
{
- SymbolEntry *res = (SymbolEntry *)0;
+ MapEntry *res = (MapEntry *)0;
const Scope *basescope = glb->symboltab->mapScope(this,addr,usepoint);
stackContainer(basescope,(const Scope *)0,addr,size,usepoint,&res);
return res;
@@ -1264,10 +1327,10 @@ SymbolEntry *Scope::queryContainer(const Address &addr,int4 size,
/// \param usepoint is a point at which the memory range is accessed (may be \e invalid)
/// \param flags is a reference used to pass back the boolean properties of the memory range
/// \return the smallest SymbolEntry containing the range, or NULL
-SymbolEntry *Scope::queryProperties(const Address &addr,int4 size,
- const Address &usepoint,uint4 &flags) const
+MapEntry *Scope::queryProperties(const Address &addr,int4 size,const Address &usepoint,uint4 &flags) const
+
{
- SymbolEntry *res = (SymbolEntry *)0;
+ MapEntry *res = (MapEntry *)0;
const Scope *basescope = glb->symboltab->mapScope(this,addr,usepoint);
const Scope *finalscope = stackContainer(basescope,(const Scope *)0,addr,size,usepoint,&res);
if (res != (SymbolEntry *)0) // If we found a symbol
@@ -1531,9 +1594,8 @@ Symbol *Scope::addSymbol(const string &nm,Datatype *ct)
/// \param addr is the starting address of the Symbol storage
/// \param usepoint is the point accessing that storage (may be \e invalid)
/// \return the SymbolEntry matching the new mapping
-SymbolEntry *Scope::addSymbol(const string &nm,Datatype *ct,
- const Address &addr,
- const Address &usepoint)
+MapEntry *Scope::addSymbol(const string &nm,Datatype *ct,const Address &addr,const Address &usepoint)
+
{
Symbol *sym;
@@ -1549,15 +1611,15 @@ SymbolEntry *Scope::addSymbol(const string &nm,Datatype *ct,
/// \param addr is the given address to map to
/// \param usepoint is a point at which the Symbol is accessed at that address
/// \return the SymbolEntry representing the new mapping
-SymbolEntry *Scope::addMapPoint(Symbol *sym,
- const Address &addr,
- const Address &usepoint)
+MapEntry *Scope::addMapPoint(Symbol *sym,const Address &addr,const Address &usepoint)
+
{
- SymbolEntry entry(sym);
+ MapEntry *entry = new MapEntry(sym);
+ entry->addr = addr;
if (!usepoint.isInvalid()) // Restrict maps use if necessary
- entry.uselimit.insertRange(usepoint.getSpace(),usepoint.getOffset(),usepoint.getOffset());
- entry.addr = addr;
- return addMap(entry);
+ entry->uselimit.insertRange(usepoint.getSpace(),usepoint.getOffset(),usepoint.getOffset());
+ addMap(entry);
+ return entry;
}
/// A Symbol element is parsed first, followed by sequences of \<addr> elements or
@@ -1594,16 +1656,25 @@ Symbol *Scope::addMapSym(Decoder &decoder)
throw;
}
addSymbolInternal(sym); // This routine may throw, but it will delete sym in this case
- while(decoder.peekElement() != 0) {
- SymbolEntry entry(sym);
- entry.decode(decoder);
- if (entry.isInvalid()) {
- glb->printWarning("Throwing out symbol with invalid mapping: "+sym->getName());
- removeSymbol(sym);
- decoder.closeElement(elemId);
- return (Symbol *)0;
+ try {
+ for(;;) {
+ uint4 entryId = decoder.peekElement();
+ if (entryId == 0) break;
+ if (entryId == ELEM_HASH) {
+ unique_ptr<DynamicEntry> entry(new DynamicEntry(sym));
+ entry->decode(decoder);
+ addDynamic(entry.release());
+ }
+ else {
+ unique_ptr<MapEntry> entry(new MapEntry(sym));
+ entry->decode(decoder);
+ addMap(entry.release());
+ }
}
- addMap(entry);
+ } catch(...) {
+ glb->printWarning("Throwing out symbol with invalid mapping: "+sym->getName());
+ removeSymbol(sym);
+ sym = (Symbol *)0;
}
decoder.closeElement(elemId);
return sym;
@@ -1704,7 +1775,8 @@ Symbol *Scope::addDynamicSymbol(const string &nm,Datatype *ct,const Address &cad
RangeList rnglist;
if (!caddr.isInvalid())
rnglist.insertRange(caddr.getSpace(),caddr.getOffset(),caddr.getOffset());
- addDynamicMapInternal(sym,Varnode::mapped,hash,0,ct->getSize(),rnglist);
+ DynamicEntry *entry = new DynamicEntry(sym,Varnode::mapped,hash,0,ct->getSize(),rnglist);
+ addDynamicMapInternal(sym,entry);
return sym;
}
@@ -1726,7 +1798,8 @@ Symbol *Scope::addEquateSymbol(const string &nm,uint4 format,uintb value,const A
RangeList rnglist;
if (!addr.isInvalid())
rnglist.insertRange(addr.getSpace(),addr.getOffset(),addr.getOffset());
- addDynamicMapInternal(sym,Varnode::mapped,hash,0,1,rnglist);
+ DynamicEntry *entry = new DynamicEntry(sym,Varnode::mapped,hash,0,1,rnglist);
+ addDynamicMapInternal(sym,entry);
return sym;
}
@@ -1749,9 +1822,29 @@ Symbol *Scope::addUnionFacetSymbol(const string &nm,Datatype *dt,int4 fieldNum,c
RangeList rnglist;
if (!addr.isInvalid())
rnglist.insertRange(addr.getSpace(),addr.getOffset(),addr.getOffset());
- addDynamicMapInternal(sym,Varnode::mapped,hash,0,1,rnglist);
+ DynamicEntry *entry = new DynamicEntry(sym,Varnode::mapped,hash,0,1,rnglist);
+ addDynamicMapInternal(sym,entry);
return sym;
}
+/// \brief Add Symbol with storage conflicts
+///
+/// \param nm is the new name of the Symbol
+/// \param ct is the data-type of the new Symbol
+/// \param vn is the Varnode to map the Symbol onto (must be written to)
+/// \return the new MapEntryConflict
+SymbolEntry *Scope::addSymbolWithConflict(const string &nm,Datatype *ct,Varnode *vn)
+
+{
+ if (!vn->isWritten())
+ throw LowlevelError("Conflict symbol created on illegal Varnode");
+ if (ct->hasStripped())
+ ct = ct->getStripped();
+ Symbol *sym = new Symbol(owner,nm,ct);
+ addSymbolInternal(sym);
+ MapEntryConflict *entry = new MapEntryConflict(sym,vn);
+ addMap(entry);
+ return entry;
+}
/// Create default name given information in the Symbol and possibly a representative Varnode.
/// This method extracts the crucial properties and then uses the buildVariableName method to
@@ -1777,8 +1870,10 @@ string Scope::buildDefaultName(Symbol *sym,int4 &base,Varnode *vn) const
return buildVariableName(vn->getAddr(),usepoint,sym->getType(),base,vn->getFlags());
}
if (sym->numEntries() != 0) {
+ Address addr;
SymbolEntry *entry = sym->getMapEntry(0);
- Address addr = entry->getAddr();
+ if (!entry->isDynamic())
+ addr = ((MapEntry *)entry)->getAddr();
Address usepoint = entry->getFirstUseAddress();
uint4 flags = usepoint.isInvalid() ? Varnode::addrtied : 0;
if (sym->getCategory() == Symbol::function_parameter) {
@@ -1847,50 +1942,44 @@ void ScopeInternal::addSymbolInternal(Symbol *sym)
}
}
-SymbolEntry *ScopeInternal::addMapInternal(Symbol *sym,uint4 exfl,const Address &addr,int4 off,int4 sz,
- const RangeList &uselim)
+void ScopeInternal::addMapInternal(Symbol *sym,MapEntry *entry)
{
+ sym->mapentry.push_back(entry);
// Find or create the appropriate rangemap
- AddrSpace *spc = addr.getSpace();
+ AddrSpace *spc = entry->addr.getSpace();
EntryMap *rangemap = maptable[spc->getIndex()];
if (rangemap == (EntryMap *)0) {
rangemap = new EntryMap();
maptable[spc->getIndex()] = rangemap;
}
// Insert the new map
- SymbolEntry::inittype initdata(sym,exfl,addr.getSpace(),off,uselim);
- Address lastaddress = addr + (sz-1);
- if (lastaddress.getOffset() < addr.getOffset()) {
+ Address lastaddress = entry->addr + (entry->size-1);
+ if (lastaddress.getOffset() < entry->addr.getOffset()) {
string msg = "Symbol ";
msg += sym->getName();
msg += " extends beyond the end of the address space";
throw LowlevelError(msg);
}
- list<SymbolEntry>::iterator iter = rangemap->insert(initdata,addr.getOffset(),lastaddress.getOffset());
- // Store reference to map in symbol
- sym->mapentry.push_back(iter);
- if (sz == sym->type->getSize()) {
+ entry->mapIterator = rangemap->insert(entry,entry->addr.getOffset(),lastaddress.getOffset());
+ if (entry->size == sym->type->getSize()) {
sym->wholeCount += 1;
if (sym->wholeCount == 2)
multiEntrySet.insert(sym);
}
- return &(*iter);
}
-SymbolEntry *ScopeInternal::addDynamicMapInternal(Symbol *sym,uint4 exfl,uint8 hash,int4 off,int4 sz,
- const RangeList &uselim)
+void ScopeInternal::addDynamicMapInternal(Symbol *sym,DynamicEntry *entry)
{
- dynamicentry.push_back(SymbolEntry(sym,exfl,hash,off,sz,uselim));
- list<SymbolEntry>::iterator iter = dynamicentry.end();
- --iter;
- sym->mapentry.push_back(iter); // Store reference to map entry in symbol
- if (sz == sym->type->getSize()) {
+ sym->mapentry.push_back(entry);
+ dynamicentry.push_back(entry);
+ entry->dynIterator = dynamicentry.end();
+ --entry->dynIterator;
+ if (entry->size == sym->type->getSize()) {
sym->wholeCount += 1;
if (sym->wholeCount == 2)
multiEntrySet.insert(sym);
}
- return &dynamicentry.back();
}
MapIterator ScopeInternal::begin(void) const
@@ -1901,7 +1990,7 @@ MapIterator ScopeInternal::begin(void) const
iter = maptable.begin();
while((iter!=maptable.end())&&((*iter)==(EntryMap *)0))
++iter;
- list<SymbolEntry>::const_iterator curiter;
+ list<SymbolRange>::const_iterator curiter;
if (iter!=maptable.end()) {
curiter = (*iter)->begin_list();
if (curiter == (*iter)->end_list()) {
@@ -1921,29 +2010,29 @@ MapIterator ScopeInternal::begin(void) const
MapIterator ScopeInternal::end(void) const
{
- list<SymbolEntry>::const_iterator curiter;
+ list<SymbolRange>::const_iterator curiter;
return MapIterator(&maptable,maptable.end(),curiter);
}
-list<SymbolEntry>::const_iterator ScopeInternal::beginDynamic(void) const
+list<DynamicEntry *>::const_iterator ScopeInternal::beginDynamic(void) const
{
return dynamicentry.begin();
}
-list<SymbolEntry>::const_iterator ScopeInternal::endDynamic(void) const
+list<DynamicEntry *>::const_iterator ScopeInternal::endDynamic(void) const
{
return dynamicentry.end();
}
-list<SymbolEntry>::iterator ScopeInternal::beginDynamic(void)
+list<DynamicEntry *>::iterator ScopeInternal::beginDynamic(void)
{
return dynamicentry.begin();
}
-list<SymbolEntry>::iterator ScopeInternal::endDynamic(void)
+list<DynamicEntry *>::iterator ScopeInternal::endDynamic(void)
{
return dynamicentry.end();
@@ -2124,19 +2213,21 @@ void ScopeInternal::adjustCaches(void)
void ScopeInternal::removeSymbolMappings(Symbol *symbol)
{
- vector<list<SymbolEntry>::iterator>::iterator iter;
+ vector<SymbolEntry *>::iterator iter;
if (symbol->wholeCount > 1)
multiEntrySet.erase(symbol);
// Remove each mapping of the symbol
for(iter=symbol->mapentry.begin();iter!=symbol->mapentry.end();++iter) {
- AddrSpace *spc = (*(*iter)).getAddr().getSpace();
- if (spc == (AddrSpace *)0) // A null address indicates a dynamic mapping
- dynamicentry.erase( *iter );
- else {
+ SymbolEntry *entry = *iter;
+ if (!entry->isDynamic()) {
+ AddrSpace *spc = ((MapEntry *)entry)->getAddr().getSpace();
EntryMap *rangemap = maptable[spc->getIndex()];
- rangemap->erase( *iter );
+ rangemap->erase( ((MapEntry *)entry)->mapIterator );
}
+ else
+ dynamicentry.erase( ((DynamicEntry *)entry)->dynIterator );
+ delete entry;
}
symbol->wholeCount = 0;
symbol->mapentry.clear();
@@ -2181,17 +2272,15 @@ void ScopeInternal::retypeSymbol(Symbol *sym,Datatype *ct)
sym->checkSizeTypeLock();
return;
}
- else if (sym->mapentry.size()==1) {
- list<SymbolEntry>::iterator iter = sym->mapentry.back();
- if ((*iter).isAddrTied()) {
- // Save the starting address of map
- Address addr((*iter).getAddr());
-
- // Find the correct rangemap
- EntryMap *rangemap = maptable[ (*iter).getAddr().getSpace()->getIndex() ];
- // Remove the map entry
- rangemap->erase(iter);
+ if (sym->mapentry.size()==1) {
+ SymbolEntry *entry = sym->mapentry.back();
+ if (!entry->isDynamic() && entry->isAddrTied()) {
+ Address addr(((MapEntry *)entry)->getAddr()); // Save the starting address of map
+
+ EntryMap *rangemap = maptable[ addr.getSpace()->getIndex() ]; // Find the correct rangemap
+ rangemap->erase(((MapEntry *)entry)->mapIterator); // Remove the map entry
sym->mapentry.pop_back(); // Remove reference to map entry
+ delete entry;
sym->wholeCount = 0;
// Now we are ready to change the type
@@ -2228,7 +2317,7 @@ void ScopeInternal::setDisplayFormat(Symbol *sym,uint4 attr)
sym->setDisplayFormat(attr);
}
-SymbolEntry *ScopeInternal::findAddr(const Address &addr,const Address &usepoint) const
+MapEntry *ScopeInternal::findAddr(const Address &addr,const Address &usepoint) const
{
EntryMap *rangemap = maptable[ addr.getSpace()->getIndex() ];
@@ -2244,20 +2333,20 @@ SymbolEntry *ScopeInternal::findAddr(const Address &addr,const Address &usepoint
EntryMap::subsorttype(usepoint));
while(res.first != res.second) {
--res.second;
- SymbolEntry *entry = &(*res.second);
+ MapEntry *entry = (*res.second).entry;
if (entry->getAddr().getOffset() == addr.getOffset()) {
if (entry->inUse(usepoint))
return entry;
}
}
}
- return (SymbolEntry *)0;
+ return (MapEntry *)0;
}
-SymbolEntry *ScopeInternal::findContainer(const Address &addr,int4 size,
- const Address &usepoint) const
+MapEntry *ScopeInternal::findContainer(const Address &addr,int4 size,const Address &usepoint) const
+
{
- SymbolEntry *bestentry = (SymbolEntry *)0;
+ MapEntry *bestentry = (MapEntry *)0;
EntryMap *rangemap = maptable[ addr.getSpace()->getIndex() ];
if (rangemap != (EntryMap *)0) {
pair<EntryMap::const_iterator,EntryMap::const_iterator> res;
@@ -2273,7 +2362,7 @@ SymbolEntry *ScopeInternal::findContainer(const Address &addr,int4 size,
uintb end = addr.getOffset() + size -1;
while(res.first != res.second) {
--res.second;
- SymbolEntry *entry = &(*res.second);
+ MapEntry *entry = (*res.second).entry;
if (entry->getLast() >= end) { // We contain the range
if ((entry->getSize()<oldsize)||(oldsize==-1)) {
if (entry->inUse(usepoint)) {
@@ -2288,10 +2377,10 @@ SymbolEntry *ScopeInternal::findContainer(const Address &addr,int4 size,
return bestentry;
}
-SymbolEntry *ScopeInternal::findClosestFit(const Address &addr,int4 size,
- const Address &usepoint) const
+MapEntry *ScopeInternal::findClosestFit(const Address &addr,int4 size,const Address &usepoint) const
+
{
- SymbolEntry *bestentry = (SymbolEntry *)0;
+ MapEntry *bestentry = (MapEntry *)0;
EntryMap *rangemap = maptable[ addr.getSpace()->getIndex() ];
if (rangemap != (EntryMap *)0) {
pair<EntryMap::const_iterator,EntryMap::const_iterator> res;
@@ -2308,7 +2397,7 @@ SymbolEntry *ScopeInternal::findClosestFit(const Address &addr,int4 size,
while(res.first != res.second) {
--res.second;
- SymbolEntry *entry = &(*res.second);
+ MapEntry *entry = (*res.second).entry;
if (entry->getLast() >= addr.getOffset()) { // We contain start
newdiff = entry->getSize() - size;
if (((olddiff<0)&&(newdiff>olddiff))||
@@ -2334,7 +2423,7 @@ Funcdata *ScopeInternal::findFunction(const Address &addr) const
pair<EntryMap::const_iterator,EntryMap::const_iterator> res;
res = rangemap->find(addr.getOffset());
while(res.first != res.second) {
- SymbolEntry *entry = &(*res.first);
+ MapEntry *entry = (*res.first).entry;
if (entry->getAddr().getOffset() == addr.getOffset()) {
sym = dynamic_cast<FunctionSymbol *>(entry->getSymbol());
if (sym != (FunctionSymbol *)0)
@@ -2355,7 +2444,7 @@ ExternRefSymbol *ScopeInternal::findExternalRef(const Address &addr) const
pair<EntryMap::const_iterator,EntryMap::const_iterator> res;
res = rangemap->find(addr.getOffset());
while(res.first != res.second) {
- SymbolEntry *entry = &(*res.first);
+ MapEntry *entry = (*res.first).entry;
if (entry->getAddr().getOffset() == addr.getOffset()) {
sym = dynamic_cast<ExternRefSymbol *>(entry->getSymbol());
break;
@@ -2384,7 +2473,7 @@ LabSymbol *ScopeInternal::findCodeLabel(const Address &addr) const
EntryMap::subsorttype(addr));
while(res.first != res.second) {
--res.second;
- SymbolEntry *entry = &(*res.second);
+ MapEntry *entry = (*res.second).entry;
if (entry->getAddr().getOffset() == addr.getOffset()) {
if (entry->inUse(addr)) {
sym = dynamic_cast<LabSymbol *>(entry->getSymbol());
@@ -2396,7 +2485,7 @@ LabSymbol *ScopeInternal::findCodeLabel(const Address &addr) const
return sym;
}
-SymbolEntry *ScopeInternal::findOverlap(const Address &addr,int4 size) const
+MapEntry *ScopeInternal::findOverlap(const Address &addr,int4 size) const
{
EntryMap *rangemap = maptable[ addr.getSpace()->getIndex() ];
@@ -2404,9 +2493,9 @@ SymbolEntry *ScopeInternal::findOverlap(const Address &addr,int4 size) const
EntryMap::const_iterator iter;
iter = rangemap->find_overlap(addr.getOffset(),addr.getOffset()+size-1);
if (iter != rangemap->end())
- return &(*iter);
+ return (*iter).entry;
}
- return (SymbolEntry *)0;
+ return (MapEntry *)0;
}
void ScopeInternal::findByName(const string &nm,vector<Symbol *> &res) const
@@ -2638,25 +2727,13 @@ void ScopeInternal::encode(Encoder &encoder) const
SymbolNameTree::const_iterator iter;
for(iter=nametree.begin();iter!=nametree.end();++iter) {
Symbol *sym = *iter;
- int4 symbolType = 0;
- if (!sym->mapentry.empty()) {
- const SymbolEntry &entry( *sym->mapentry.front() );
- if (entry.isDynamic()) {
- if (sym->getCategory() == Symbol::union_facet)
- continue; // Don't save override
- symbolType = (sym->getCategory() == Symbol::equate) ? 2 : 1;
- }
- }
+ if (sym->getCategory() == Symbol::union_facet)
+ continue; // Don't save override
encoder.openElement(ELEM_MAPSYM);
- if (symbolType == 1)
- encoder.writeString(ATTRIB_TYPE, "dynamic");
- else if (symbolType == 2)
- encoder.writeString(ATTRIB_TYPE, "equate");
sym->encode(encoder);
- vector<list<SymbolEntry>::iterator>::const_iterator miter;
+ vector<SymbolEntry *>::const_iterator miter;
for(miter=sym->mapentry.begin();miter!=sym->mapentry.end();++miter) {
- const SymbolEntry &entry((*(*miter)));
- entry.encode(encoder);
+ (*miter)->encode(encoder);
}
encoder.closeElement(ELEM_MAPSYM);
}
@@ -2779,13 +2856,19 @@ void ScopeInternal::decode(Decoder &decoder)
Symbol *sym = addMapSym(decoder);
if (rangeequalssymbols) {
SymbolEntry *e = sym->getFirstWholeMap();
- glb->symboltab->addRange(this,e->getAddr().getSpace(),e->getFirst(),e->getLast());
+ if (!e->isDynamic()) {
+ MapEntry *map = (MapEntry *)e;
+ glb->symboltab->addRange(this,map->getAddr().getSpace(),map->getFirst(),map->getLast());
+ }
}
uint4 props = sym->getFlags() & (Varnode::readonly | Varnode::volatil);
if (props != 0) {
SymbolEntry *e = sym->getFirstWholeMap();
- Range rng(e->getAddr().getSpace(),e->getFirst(),e->getLast());
- glb->symboltab->setPropertyRange(props,rng);
+ if (!e->isDynamic()) {
+ MapEntry *map = (MapEntry *)e;
+ Range rng(map->getAddr().getSpace(),map->getFirst(),map->getLast());
+ glb->symboltab->setPropertyRange(props,rng);
+ }
}
}
else if (symId == ELEM_HOLE)
@@ -2808,11 +2891,11 @@ void ScopeInternal::printEntries(ostream &s) const
for(int4 i=0;i<maptable.size();++i) {
EntryMap *rangemap = maptable[i];
if (rangemap == (EntryMap *)0) continue;
- list<SymbolEntry>::const_iterator iter,enditer;
+ list<SymbolRange>::const_iterator iter,enditer;
iter = rangemap->begin_list();
enditer = rangemap->end_list();
for(;iter!=enditer;++iter)
- (*iter).printEntry(s);
+ (*iter).entry->printEntry(s);
}
}
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/database.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/database.hh
index ce7fc7059d..1ea7b84ce8 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/database.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/database.hh
@@ -32,6 +32,7 @@ namespace ghidra {
class Architecture;
class Funcdata;
class Scope;
+class MapEntry;
class Database;
class Symbol;
class PrintLanguage;
@@ -58,54 +59,18 @@ extern ElementId ELEM_RANGEEQUALSSYMBOLS; ///< Marshaling element \<rangeequalss
extern ElementId ELEM_SCOPE; ///< Marshaling element \<scope>
extern ElementId ELEM_SYMBOLLIST; ///< Marshaling element \<symbollist>
-/// \brief A storage location for a particular Symbol
-///
-/// Where a Symbol is stored, as a byte address and a size, is of particular importance
-/// to the decompiler. This class encapsulates this storage meta-data. A single Symbol split
-/// across multiple storage locations is supported by the \b offset and \b size fields. The
-/// \b hash field supports \e dynamic storage, where a Symbol is represented by a constant
-/// or a temporary register. In this case, storage must be tied to the particular p-code
-/// operators using the value.
-///
-/// A particular memory address does \b not have to represent the symbol across all code. Storage
-/// may get recycled for different Symbols at different points in the code. The \b uselimit object
-/// defines the range of instruction addresses over which a particular memory address does
-/// represent a Symbol, with the convention that an empty \b uselimit indicates the storage
-/// holds the Symbol across \e all code.
-class SymbolEntry {
- friend class Scope;
- Symbol *symbol; ///< Symbol object being mapped
- uint4 extraflags; ///< Varnode flags specific to this storage location
- Address addr; ///< Starting address of the storage location
- uint8 hash; ///< A dynamic storage address (an alternative to \b addr for dynamic symbols)
- int4 offset; ///< Offset into the Symbol that \b this covers
- int4 size; ///< Number of bytes consumed by \b this (piece of the) storage
- RangeList uselimit; ///< Code address ranges where this storage is valid
- SymbolEntry(Symbol *sym); ///< Construct a mapping for a Symbol without an address
+/// \brief A helper class for storing MapEntry objects in an interval map
+class SymbolRange {
public:
- /// \brief Initialization data for a SymbolEntry to facilitate a rangemap
- ///
- /// This is all the raw pieces of a SymbolEntry for a (non-dynamic) Symbol except
- /// the offset of the main address and the size, which are provided by the
- /// (rangemap compatible) SymbolEntry constructor.
- class EntryInitData {
- friend class SymbolEntry;
- AddrSpace *space; ///< The address space of the main SymbolEntry starting address
- Symbol *symbol; ///< The symbol being mapped
- uint4 extraflags; ///< Varnode flags specific to the storage location
- int4 offset; ///< Starting offset of the portion of the Symbol being covered
- const RangeList &uselimit; ///< Reference to the range of code addresses for which the storage is valid
- public:
- EntryInitData(Symbol *sym,uint4 exfl,AddrSpace *spc,int4 off,const RangeList &ul)
- : uselimit(ul) { symbol = sym; extraflags=exfl; space = spc; offset = off; } ///< Constructor
- };
-
+ uintb first; ///< Starting offset of range covered by the MapEntry
+ uintb last; ///< Last offset of range
+ MapEntry *entry; ///< Pointer to the MapEntry
/// \brief Class for sub-sorting different SymbolEntry objects at the same address
///
- /// This is built from the SymbolEntry \b uselimit object (see SymbolEntry::getSubsort())
+ /// This is built from the SymbolEntry \b uselimit object
/// Relevant portions of an Address object or pulled out for smaller storage and quick comparisons.
class EntrySubsort {
- friend class SymbolEntry;
+ friend class SymbolRange;
int4 useindex; ///< Index of the sub-sorting address space
uintb useoffset; ///< Offset into the sub-sorting address space
public:
@@ -132,23 +97,63 @@ public:
return (useoffset < op2.useoffset);
}
};
+
typedef uintb linetype; ///< The linear element for a rangemap of SymbolEntry
typedef EntrySubsort subsorttype; ///< The sub-sort object for a rangemap
- typedef EntryInitData inittype; ///< Initialization data for a SymbolEntry in a rangemap
+ typedef MapEntry *inittype; ///< Initialization data for a SymbolEntry in a rangemap
- SymbolEntry(const EntryInitData &data,uintb a,uintb b); ///< Fully initialize \b this
- SymbolEntry(Symbol *sym,uint4 exfl,uint8 h,int4 off,int4 sz,const RangeList &rnglist); ///< Construct a dynamic SymbolEntry
- bool isPiece(void) const { return ((extraflags&(Varnode::precislo|Varnode::precishi))!=0); } ///< Is \b this a high or low piece of the whole Symbol
- bool isDynamic(void) const { return addr.isInvalid(); } ///< Is \b storage \e dynamic
- bool isInvalid(void) const { return (addr.isInvalid() && (hash==0)); } ///< Is \b this storage \e invalid
+ SymbolRange(MapEntry *e,uintb f,uintb l) { entry=e; first = f; last = l; } ///< Constructor
+ uintb getFirst(void) const { return first; } ///< Get the first offset of \b this storage location
+ uintb getLast(void) const { return last; } ///< Get the last offset of \b this storage location
+ subsorttype getSubsort(void) const;
+};
+
+/// \brief A storage location for a particular Symbol
+///
+/// Storage can be a fixed Address or a dynamically located Varnode representing the Symbol.
+/// Storage may get recycled for different Symbols at different points in the code. The \b uselimit object
+/// defines the range of instruction addresses over which a particular SymbolEntry does
+/// represent a Symbol, with the convention that an empty \b uselimit indicates the storage
+/// holds the Symbol across \e all code.
+class SymbolEntry {
+ friend class Scope;
+ friend class ScopeInternal;
+ /// \brief The type (derived class) of SymbolEntry
+protected:
+ enum {
+ map_entry = 0, ///< Is a MapEntry
+ conflict_entry = 1, ///< Is a MapEntryConflict
+ dynamic_entry = 2 ///< Is a DynamicEntry
+ };
+ Symbol *symbol; ///< Symbol object being mapped
+ RangeList uselimit; ///< Code address ranges where this storage is valid
+ uint4 extraflags; ///< Varnode flags specific to this storage location
+ int4 offset; ///< Offset into the Symbol that \b this covers
+ int4 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 symbol
+public:
+ SymbolEntry(Symbol *sym); ///< Construct an uninitialized SymbolEntry
+ SymbolEntry(Symbol *sym,uint4 exflags,int4 sz,int4 off,const RangeList &use); ///< Constructor
+ virtual ~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
+
+ /// \brief Get the data-type associated with (a piece of) \b this
+ ///
+ /// Return the data-type that matches the given size and address within \b this storage.
+ /// NULL is returned if there is no valid sub-type matching the size.
+ /// \param addr is the given address
+ /// \param sz is the given size (in bytes)
+ /// \return the matching data-type or NULL
+ virtual Datatype *getSizedType(const Address &addr,int4 sz) const=0;
+ virtual void printEntry(ostream &s) const=0; ///< Print a one-line description of \b this to a debug stream
+ virtual void encode(Encoder &encoder) const=0; ///< Encode storage element internal to \<mapsym>
+ virtual void decode(Decoder &decoder)=0; ///< Decode storage element internal to \<mapsym>
uint4 getAllFlags(void) const; ///< Get all Varnode flags for \b this storage
int4 getOffset(void) const { return offset; } ///< Get offset of \b this within the Symbol
- uintb getFirst(void) const { return addr.getOffset(); } ///< Get the first offset of \b this storage location
- uintb getLast(void) const { return (addr.getOffset()+size-1); } ///< Get the last offset of \b this storage location
- subsorttype getSubsort(void) const; ///< Get the sub-sort object
Symbol *getSymbol(void) const { return symbol; } ///< Get the Symbol associated with \b this
- const Address &getAddr(void) const { return addr; } ///< Get the starting address of \b this storage
- uint8 getHash(void) const { return hash; } ///< Get the hash used to identify \b this storage
int4 getSize(void) const { return size; } ///< Get the number of bytes consumed by \b this storage
bool inUse(const Address &usepoint) const; ///< Is \b this storage valid for the given code address
const RangeList &getUseLimit(void) const { return uselimit; } ///< Get the set of valid code addresses for \b this storage
@@ -156,12 +161,66 @@ public:
void setUseLimit(const RangeList &uselim) { uselimit = uselim; } ///< Set the range of code addresses where \b this is valid
bool isAddrTied(void) const; ///< Is \b this storage address tied
bool updateType(Varnode *vn) const; ///< Update a Varnode data-type from \b this
- Datatype *getSizedType(const Address &addr,int4 sz) const; ///< Get the data-type associated with (a piece of) \b this
- void printEntry(ostream &s) const; ///< Dump a description of \b this to a stream
- void encode(Encoder &encoder) const; ///< Encode \b this to a stream
- void decode(Decoder &decoder); ///< Decode \b this from a stream
+
+};
+typedef rangemap<SymbolRange> EntryMap; ///< A rangemap of MapEntry
+
+/// \brief A mapping from a Symbol to storage Address
+///
+/// This object holds a (starting) Address of the Symbol and can be integrated into an EntryMap.
+/// A single Symbol split across multiple storage locations is supported by the \b offset and \b size fields.
+class MapEntry : public SymbolEntry {
+ friend class Scope;
+ friend class ScopeInternal;
+protected:
+ Address addr; ///< Starting address of the storage location
+ list<SymbolRange>::iterator mapIterator; ///< Position within container
+public:
+ 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()
+ 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);
+ uintb getFirst(void) const { return addr.getOffset(); } ///< Get the first offset of \b this storage location
+ uintb getLast(void) const { return (addr.getOffset()+size-1); } ///< Get the last offset of \b this storage location
+ const Address &getAddr(void) const { return addr; } ///< Get the starting address of \b this storage
+};
+
+/// \brief A mapping from Symbol to Address when there are conflicting uses of the Address
+///
+/// If storage is (potentially) used by other Symbols at the same code point (\b uselimit),
+/// the Varnode is mapped using \b this, which allows potential deconfliction later,
+/// by converting to a DynamicEntry.
+class MapEntryConflict : public MapEntry {
+ uintm uniq; ///< Unique id of PcodeOp defining mapped Varnode
+public:
+ MapEntryConflict(Symbol *sym,Varnode *vn); ///< Constructor
+ uintm getUnique(void) const { return uniq; } ///< Get id of PcodeOp defining the conflicting Varnode
+ virtual void printEntry(ostream &s) const;
+ virtual void encode(Encoder &encoder) const;
+ virtual void decode(Decoder &decoder);
+};
+
+/// \brief A mapping from a Symbol to a specific Varnode
+///
+/// For a constant or temporary register that is not strongly linked to a particular storage address,
+/// \b this allows a representative Varnode to be discovered dynamically.
+/// Storage for the Symbol is tied to the particular Varnode and p-code operator using the value.
+class DynamicEntry : public SymbolEntry {
+ friend class Scope;
+ friend class ScopeInternal;
+ uint8 hash; ///< A dynamic storage address
+ list<DynamicEntry *>::iterator dynIterator; ///< Position within container
+public:
+ 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()
+ 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);
+ uint8 getHash(void) const { return hash; } ///< Get the hash used to identify \b this storage
};
-typedef rangemap<SymbolEntry> EntryMap; ///< A rangemap of SymbolEntry
/// \brief The base class for a symbol in a symbol table or scope
///
@@ -186,11 +245,11 @@ protected:
int2 category; ///< Special category (\b function_parameter, \b equate, etc.)
uint2 catindex; ///< Index within category
uint8 symbolId; ///< Unique id, 0=unassigned
- vector<list<SymbolEntry>::iterator> mapentry; ///< List of storage locations labeled with \b this Symbol
+ vector<SymbolEntry *> mapentry; ///< List of storage locations labeled with \b this Symbol
mutable const Scope *depthScope; ///< Scope associated with current depth resolution
mutable int4 depthResolution; ///< Number of namespace elements required to resolve symbol in current scope
uint4 wholeCount; ///< Number of SymbolEntries that map to the whole Symbol
- virtual ~Symbol(void) {} ///< Destructor
+ virtual ~Symbol(void); ///< Destructor
void setDisplayFormat(uint4 val); ///< Set the display format for \b this Symbol
void checkSizeTypeLock(void); ///< Calculate if \b size_typelock property is on
void setThisPointer(bool val); ///< Toggle whether \b this is the "this" pointer for a class method
@@ -242,7 +301,7 @@ public:
void setIsolated(bool val); ///< Set whether \b this Symbol should be speculatively merged
Scope *getScope(void) const { return scope; } ///< Get the scope owning \b this Symbol
SymbolEntry *getFirstWholeMap(void) const; ///< Get the first entire mapping of the symbol
- SymbolEntry *getMapEntry(const Address &addr) const; ///< Get first mapping of the symbol that contains the given Address
+ MapEntry *getMapEntry(const Address &addr) const; ///< Get first mapping of the symbol that contains the given Address
int4 numEntries(void) const { return mapentry.size(); } ///< Return the number of SymbolEntrys
SymbolEntry *getMapEntry(int4 i) const { return &(*mapentry[i]); } ///< Return the i-th SymbolEntry for \b this Symbol
int4 getMapEntryPosition(const SymbolEntry *entry) const; ///< Position of given SymbolEntry within \b this multi-entry Symbol
@@ -381,7 +440,7 @@ typedef set<Symbol *,SymbolCompareName> SymbolNameTree; ///< A set of Symbol ob
class MapIterator {
const vector<EntryMap *> *map; ///< The list of EntryMaps, one per address space
vector<EntryMap *>::const_iterator curmap; ///< Current EntryMap being iterated
- list<SymbolEntry>::const_iterator curiter; ///< Current SymbolEntry being iterated
+ list<SymbolRange>::const_iterator curiter; ///< Current SymbolEntry being iterated
public:
MapIterator(void) { map = (const vector<EntryMap *> *)0; } ///< Construct an uninitialized iterator
@@ -392,7 +451,7 @@ public:
/// \param ci is the position of the iterator within the specific EntryMap
MapIterator(const vector<EntryMap *> *m,
vector<EntryMap *>::const_iterator cm,
- list<SymbolEntry>::const_iterator ci) {
+ list<SymbolRange>::const_iterator ci) {
map = m; curmap = cm; curiter = ci;
}
@@ -400,7 +459,7 @@ public:
MapIterator(const MapIterator &op2) {
map = op2.map; curmap = op2.curmap; curiter = op2.curiter;
}
- const SymbolEntry *operator*(void) const { return &(*curiter); } ///< Return the SymbolEntry being pointed at
+ const MapEntry *operator*(void) const { return (*curiter).entry; } ///< Return the SymbolEntry being pointed at
MapIterator &operator++(void); ///< Pre-increment the iterator
MapIterator operator++(int4 i); ///< Post-increment the iterator
@@ -481,17 +540,17 @@ protected:
const Scope *scope2,
const Address &addr,
const Address &usepoint,
- SymbolEntry **addrmatch);
+ MapEntry **addrmatch);
static const Scope *stackContainer(const Scope *scope1,
const Scope *scope2,
const Address &addr,int4 size,
const Address &usepoint,
- SymbolEntry **addrmatch);
+ MapEntry **addrmatch);
static const Scope *stackClosestFit(const Scope *scope1,
const Scope *scope2,
const Address &addr,int4 size,
const Address &usepoint,
- SymbolEntry **addrmatch);
+ MapEntry **addrmatch);
static const Scope *stackFunction(const Scope *scope1,
const Scope *scope2,
const Address &addr,
@@ -528,34 +587,19 @@ protected:
/// \param sym is the preconstructed Symbol
virtual void addSymbolInternal(Symbol *sym)=0;
- /// \brief Create a new SymbolEntry for a Symbol given a memory range
+ /// \brief Add a new MapEntry to a Symbol, update the map from Address to Symbol
///
- /// The SymbolEntry is specified in terms of a memory range and \b usepoint
/// \param sym is the given Symbol being mapped
- /// \param exfl are any boolean Varnode properties specific to the memory range
- /// \param addr is the starting address of the given memory range
- /// \param off is the byte offset of the new SymbolEntry (relative to the whole Symbol)
- /// \param sz is the number of bytes in the range
- /// \param uselim is the given \b usepoint (which may be \e invalid)
- /// \return the newly created SymbolEntry
- virtual SymbolEntry *addMapInternal(Symbol *sym,uint4 exfl,const Address &addr,int4 off,int4 sz,
- const RangeList &uselim)=0;
-
+ /// \param entry is the MapEntry to add
+ virtual void addMapInternal(Symbol *sym,MapEntry *entry)=0;
- /// \brief Create a new SymbolEntry for a Symbol given a dynamic hash
+ /// \brief Add a new DynamicEntry to a Symbol
///
- /// The SymbolEntry is specified in terms of a \b hash and \b usepoint, which describe how
- /// to find the temporary Varnode holding the symbol value.
/// \param sym is the given Symbol being mapped
- /// \param exfl are any boolean Varnode properties
- /// \param hash is the given dynamic hash
- /// \param off is the byte offset of the new SymbolEntry (relative to the whole Symbol)
- /// \param sz is the number of bytes occupied by the Varnode
- /// \param uselim is the given \b usepoint
- /// \return the newly created SymbolEntry
- virtual SymbolEntry *addDynamicMapInternal(Symbol *sym,uint4 exfl,uint8 hash,int4 off,int4 sz,
- const RangeList &uselim)=0;
- SymbolEntry *addMap(SymbolEntry &entry); ///< Integrate a SymbolEntry into the range maps
+ /// \param entry is the DynamicEntry to add
+ virtual void addDynamicMapInternal(Symbol *sym,DynamicEntry *entry)=0;
+ void addMap(MapEntry *entry); ///< Integrate a MapEntry into the range maps
+ void addDynamic(DynamicEntry *entry); ///< Integrate a DynamicEntry into the Scope
void setSymbolId(Symbol *sym,uint8 id) const { sym->symbolId = id; } ///< Adjust the id associated with a symbol
void setDisplayName(const string &nm) { displayName = nm; } ///< Change name displayed in output
public:
@@ -574,10 +618,10 @@ public:
virtual ~Scope(void); ///< Destructor
virtual MapIterator begin(void) const=0; ///< Beginning iterator to mapped SymbolEntrys
virtual MapIterator end(void) const=0; ///< Ending iterator to mapped SymbolEntrys
- virtual list<SymbolEntry>::const_iterator beginDynamic(void) const=0; ///< Beginning iterator to dynamic SymbolEntrys
- virtual list<SymbolEntry>::const_iterator endDynamic(void) const=0; ///< Ending iterator to dynamic SymbolEntrys
- virtual list<SymbolEntry>::iterator beginDynamic(void)=0; ///< Beginning iterator to dynamic SymbolEntrys
- virtual list<SymbolEntry>::iterator endDynamic(void)=0; ///< Ending iterator to dynamic SymbolEntrys
+ virtual list<DynamicEntry *>::const_iterator beginDynamic(void) const=0; ///< Beginning iterator to dynamic SymbolEntrys
+ virtual list<DynamicEntry *>::const_iterator endDynamic(void) const=0; ///< Ending iterator to dynamic SymbolEntrys
+ virtual list<DynamicEntry *>::iterator beginDynamic(void)=0; ///< Beginning iterator to dynamic SymbolEntrys
+ virtual list<DynamicEntry *>::iterator endDynamic(void)=0; ///< Ending iterator to dynamic SymbolEntrys
virtual void clear(void)=0; ///< Clear all symbols from \b this scope
virtual void clearCategory(int4 cat)=0; ///< Clear all symbols of the given category from \b this scope
virtual void clearUnlocked(void)=0; ///< Clear all unlocked symbols from \b this scope
@@ -620,7 +664,7 @@ public:
/// \param addr is the given address
/// \param usepoint is the point at which the Symbol is accessed (may be \e invalid)
/// \return the matching SymbolEntry or NULL
- virtual SymbolEntry *findAddr(const Address &addr,const Address &usepoint) const=0;
+ virtual MapEntry *findAddr(const Address &addr,const Address &usepoint) const=0;
/// \brief Find the smallest Symbol containing the given memory range
///
@@ -628,8 +672,7 @@ public:
/// \param size is the number of bytes in the range
/// \param usepoint is the point at which the Symbol is accessed (may be \e invalid)
/// \return the matching SymbolEntry or NULL
- virtual SymbolEntry *findContainer(const Address &addr,int4 size,
- const Address &usepoint) const=0;
+ virtual MapEntry *findContainer(const Address &addr,int4 size,const Address &usepoint) const=0;
/// \brief Find Symbol which is the closest fit to the given memory range
///
@@ -637,8 +680,7 @@ public:
/// \param size is the number of bytes in the range
/// \param usepoint is the point at which the Symbol is accessed (may be \e invalid)
/// \return the matching SymbolEntry or NULL
- virtual SymbolEntry *findClosestFit(const Address &addr,int4 size,
- const Address &usepoint) const=0;
+ virtual MapEntry *findClosestFit(const Address &addr,int4 size,const Address &usepoint) const=0;
/// \brief Find the function starting at the given address
///
@@ -663,7 +705,7 @@ public:
/// \param addr is the starting address of the given range
/// \param size is the number of bytes in the range
/// \return an overlapping SymbolEntry or NULL if none exists
- virtual SymbolEntry *findOverlap(const Address &addr,int4 size) const=0;
+ virtual MapEntry *findOverlap(const Address &addr,int4 size) const=0;
/// \brief Find a Symbol by name within \b this Scope
///
@@ -741,8 +783,7 @@ public:
/// \param ind is, for the function_parameter category, the index position to set, and is unused for other categories
virtual void setCategory(Symbol *sym,int4 cat,int4 ind)=0;
- virtual SymbolEntry *addSymbol(const string &nm,Datatype *ct,
- const Address &addr,const Address &usepoint);
+ virtual MapEntry *addSymbol(const string &nm,Datatype *ct,const Address &addr,const Address &usepoint);
const string &getName(void) const { return name; } ///< Get the name of the Scope
const string &getDisplayName(void) const { return displayName; } ///< Get name displayed in output
@@ -752,12 +793,11 @@ public:
// The main global querying routines
void queryByName(const string &nm,vector<Symbol *> &res) const; ///< Look-up symbols by name
Funcdata *queryFunction(const string &nm) const; ///< Look-up a function by name
- SymbolEntry *queryByAddr(const Address &addr,
- const Address &usepoint) const; ///< Get Symbol with matching address
- SymbolEntry *queryContainer(const Address &addr,int4 size,
- const Address &usepoint) const; ///< Find the smallest containing Symbol
- SymbolEntry *queryProperties(const Address &addr,int4 size,
- const Address &usepoint,uint4 &flags) const; ///< Find a Symbol or properties at the given address
+ MapEntry *queryByAddr(const Address &addr,const Address &usepoint) const; ///< Get Symbol with matching address
+ MapEntry *queryContainer(const Address &addr,int4 size,
+ const Address &usepoint) const; ///< Find the smallest containing Symbol
+ MapEntry *queryProperties(const Address &addr,int4 size,
+ const Address &usepoint,uint4 &flags) const; ///< Find a Symbol or properties at the given address
Funcdata *queryFunction(const Address &addr) const; ///< Look-up a function by address
Funcdata *queryExternalRefFunction(const Address &addr) const; ///< Look-up a function thru an \e external \e reference
LabSymbol *queryCodeLabel(const Address &addr) const; ///< Look-up a code label by address
@@ -777,8 +817,7 @@ public:
Architecture *getArch(void) const { return glb; } ///< Get the Architecture associated with \b this
Scope *getParent(void) const { return parent; } ///< Get the parent Scope (or NULL if \b this is the global Scope)
Symbol *addSymbol(const string &nm,Datatype *ct); ///< Add a new Symbol \e without mapping it to an address
- SymbolEntry *addMapPoint(Symbol *sym,const Address &addr,
- const Address &usepoint); ///< Map a Symbol to a specific address
+ MapEntry *addMapPoint(Symbol *sym,const Address &addr,const Address &usepoint); ///< Map a Symbol to a specific address
Symbol *addMapSym(Decoder &decoder); ///< Parse a mapped Symbol from a \<mapsym> element
FunctionSymbol *addFunction(const Address &addr,const string &nm);
ExternRefSymbol *addExternalRef(const Address &addr,const Address &refaddr,const string &nm);
@@ -786,6 +825,7 @@ public:
Symbol *addDynamicSymbol(const string &nm,Datatype *ct,const Address &caddr,uint8 hash);
Symbol *addEquateSymbol(const string &nm,uint4 format,uintb value,const Address &addr,uint8 hash);
Symbol *addUnionFacetSymbol(const string &nm,Datatype *dt,int4 fieldNum,const Address &addr,uint8 hash);
+ SymbolEntry *addSymbolWithConflict(const string &nm,Datatype *ct,Varnode *vn);
string buildDefaultName(Symbol *sym,int4 &base,Varnode *vn) const; ///< Create a default name for the given Symbol
bool isReadOnly(const Address &addr,int4 size,const Address &usepoint) const;
void printBounds(ostream &s) const { rangetree.printBounds(s); } ///< Print a description of \b this Scope's \e owned memory ranges
@@ -805,13 +845,12 @@ class ScopeInternal : public Scope {
protected:
virtual Scope *buildSubScope(uint8 id,const string &nm); ///< Build an unattached Scope to be associated as a sub-scope of \b this
virtual void addSymbolInternal(Symbol *sym);
- virtual SymbolEntry *addMapInternal(Symbol *sym,uint4 exfl,const Address &addr,int4 off,int4 sz,const RangeList &uselim);
- virtual SymbolEntry *addDynamicMapInternal(Symbol *sym,uint4 exfl,uint8 hash,int4 off,int4 sz,
- const RangeList &uselim);
+ virtual void addMapInternal(Symbol *sym,MapEntry *entry);
+ virtual void addDynamicMapInternal(Symbol *sym,DynamicEntry *entry);
SymbolNameTree nametree; ///< The set of Symbol objects, sorted by name
vector<EntryMap *> maptable; ///< Rangemaps of SymbolEntry, one map for each address space
vector<vector<Symbol *> > category; ///< References to Symbol objects organized by category
- list<SymbolEntry> dynamicentry; ///< Dynamic symbol entries
+ list<DynamicEntry *> dynamicentry; ///< Dynamic symbol entries
SymbolNameTree multiEntrySet; ///< Set of symbols with multiple entries
uint8 nextUniqueId; ///< Next available symbol id
public:
@@ -826,10 +865,10 @@ public:
virtual ~ScopeInternal(void);
virtual MapIterator begin(void) const;
virtual MapIterator end(void) const;
- virtual list<SymbolEntry>::const_iterator beginDynamic(void) const;
- virtual list<SymbolEntry>::const_iterator endDynamic(void) const;
- virtual list<SymbolEntry>::iterator beginDynamic(void);
- virtual list<SymbolEntry>::iterator endDynamic(void);
+ virtual list<DynamicEntry *>::const_iterator beginDynamic(void) const;
+ virtual list<DynamicEntry *>::const_iterator endDynamic(void) const;
+ virtual list<DynamicEntry *>::iterator beginDynamic(void);
+ virtual list<DynamicEntry *>::iterator endDynamic(void);
virtual void removeSymbolMappings(Symbol *symbol);
virtual void removeSymbol(Symbol *symbol);
virtual void renameSymbol(Symbol *sym,const string &newname);
@@ -838,15 +877,13 @@ public:
virtual void clearAttribute(Symbol *sym,uint4 attr);
virtual void setDisplayFormat(Symbol *sym,uint4 attr);
- virtual SymbolEntry *findAddr(const Address &addr,const Address &usepoint) const;
- virtual SymbolEntry *findContainer(const Address &addr,int4 size,
- const Address &usepoint) const;
- virtual SymbolEntry *findClosestFit(const Address &addr,int4 size,
- const Address &usepoint) const;
+ virtual MapEntry *findAddr(const Address &addr,const Address &usepoint) const;
+ virtual MapEntry *findContainer(const Address &addr,int4 size,const Address &usepoint) const;
+ virtual MapEntry *findClosestFit(const Address &addr,int4 size,const Address &usepoint) const;
virtual Funcdata *findFunction(const Address &addr) const;
virtual ExternRefSymbol *findExternalRef(const Address &addr) const;
virtual LabSymbol *findCodeLabel(const Address &addr) const;
- virtual SymbolEntry *findOverlap(const Address &addr,int4 size) const;
+ virtual MapEntry *findOverlap(const Address &addr,int4 size) const;
virtual void findByName(const string &nm,vector<Symbol *> &res) const;
virtual bool isNameUsed(const string &nm,const Scope *op2) const;
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/database_ghidra.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/database_ghidra.cc
index 7e7e59c5fc..03515cec8f 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/database_ghidra.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/database_ghidra.cc
@@ -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.
@@ -141,8 +141,9 @@ Symbol *ScopeGhidra::dump2Cache(Decoder &decoder) const
throw LowlevelError("DuplicateFunctionError, but could not recover original symbol");
}
if (sym != (Symbol *)0) {
- SymbolEntry *entry = sym->getFirstWholeMap();
- if (entry != (SymbolEntry *)0) {
+ SymbolEntry *ent = sym->getFirstWholeMap();
+ if (ent != (SymbolEntry *)0 && !ent->isDynamic()) {
+ 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't
// go up again. With this limitation, a function can only refer to a single global symbol at a
@@ -230,45 +231,45 @@ void ScopeGhidra::clear(void)
}
}
-SymbolEntry *ScopeGhidra::findAddr(const Address &addr,
- const Address &usepoint) const
+MapEntry *ScopeGhidra::findAddr(const Address &addr,const Address &usepoint) const
+
{
- SymbolEntry *entry;
+ MapEntry *entry;
entry = cache->findAddr(addr,usepoint);
- if (entry == (SymbolEntry *)0) { // Didn't find symbol
+ if (entry == (MapEntry *)0) { // Didn't find symbol
entry = cache->findContainer(addr,1,Address());
- if (entry != (SymbolEntry *)0)
- return (SymbolEntry *)0; // Address is already queried, but symbol doesn't start at our address
+ if (entry != (MapEntry *)0)
+ return (MapEntry *)0; // Address is already queried, but symbol doesn't start at our address
Symbol *sym = removeQuery(addr); // Query server
if (sym != (Symbol *)0)
entry = sym->getMapEntry(addr);
// entry may be null for certain queries, ghidra may return symbol of size <8 with
// address equal to START of function, even though the query was for an address INTERNAL to the function
}
- if ((entry != (SymbolEntry *)0)&&(entry->getAddr()==addr))
+ if ((entry != (MapEntry *)0)&&(entry->getAddr()==addr))
return entry;
- return (SymbolEntry *)0;
+ return (MapEntry *)0;
}
-SymbolEntry *ScopeGhidra::findContainer(const Address &addr,int4 size,
- const Address &usepoint) const
+MapEntry *ScopeGhidra::findContainer(const Address &addr,int4 size,const Address &usepoint) const
+
{
- SymbolEntry *entry;
+ MapEntry *entry;
entry = cache->findClosestFit(addr,size,usepoint);
- if (entry == (SymbolEntry *)0) {
+ if (entry == (MapEntry *)0) {
Symbol *sym = removeQuery(addr);
if (sym != (Symbol *)0)
entry = sym->getMapEntry(addr);
// entry may be null for certain queries, ghidra may return symbol of size <8 with
// address equal to START of function, even though the query was for an address INTERNAL to the function
}
- if (entry != (SymbolEntry *)0) {
+ if (entry != (MapEntry *)0) {
// Entry contains addr, does it contain addr+size
uintb last = entry->getAddr().getOffset() + entry->getSize() -1;
if (last >= addr.getOffset() + size-1)
return entry;
}
- return (SymbolEntry *)0;
+ return (MapEntry *)0;
}
ExternRefSymbol *ScopeGhidra::findExternalRef(const Address &addr) const
@@ -340,7 +341,7 @@ Funcdata *ScopeGhidra::resolveExternalRefFunction(ExternRefSymbol *sym) const
// If the function isn't in cache, we use the special
// getExternalRefXML interface to recover the external function
PackedDecode decoder(ghidra);
- SymbolEntry *entry = sym->getFirstWholeMap();
+ MapEntry *entry = (MapEntry *)sym->getFirstWholeMap();
if (ghidra->getExternalRef(entry->getAddr(),decoder)) {
FunctionSymbol *funcSym;
// Make sure referenced function is cached
@@ -352,8 +353,8 @@ Funcdata *ScopeGhidra::resolveExternalRefFunction(ExternRefSymbol *sym) const
return resFd;
}
-SymbolEntry *ScopeGhidra::addSymbol(const string &nm,Datatype *ct,
- const Address &addr,const Address &usepoint)
+MapEntry *ScopeGhidra::addSymbol(const string &nm,Datatype *ct,const Address &addr,const Address &usepoint)
+
{
// We do not inform Ghidra of the new symbol, we just
// stick it in the cache. This allows the mapglobals action
@@ -361,13 +362,10 @@ SymbolEntry *ScopeGhidra::addSymbol(const string &nm,Datatype *ct,
return cache->addSymbol(nm,ct,addr,usepoint);
}
-SymbolEntry *ScopeGhidraNamespace::addMapInternal(Symbol *sym,uint4 exfl,const Address &addr,int4 off,int4 sz,
- const RangeList &uselim)
+void ScopeGhidraNamespace::addMapInternal(Symbol *sym,MapEntry *entry)
{
- SymbolEntry *res;
- res = ScopeInternal::addMapInternal(sym,exfl,addr,off,sz,uselim);
- glb->symboltab->addRange(this,res->getAddr().getSpace(),res->getFirst(),res->getLast());
- return res;
+ ScopeInternal::addMapInternal(sym,entry);
+ glb->symboltab->addRange(this,entry->getAddr().getSpace(),entry->getFirst(),entry->getLast());
}
bool ScopeGhidraNamespace::isNameUsed(const string &nm,const Scope *op2) const
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/database_ghidra.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/database_ghidra.hh
index 7b8c5f8e4a..60f4e08627 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/database_ghidra.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/database_ghidra.hh
@@ -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.
@@ -50,11 +50,9 @@ class ScopeGhidra : public Scope {
throw LowlevelError("remove_range should not be performed on ghidra scope");
}
virtual Scope *buildSubScope(uint8 id,const string &nm);
- virtual void addSymbolInternal(Symbol *sym) { throw LowlevelError("add_symbol_internal unimplemented"); }
- virtual SymbolEntry *addMapInternal(Symbol *sym,uint4 exfl,const Address &addr,int4 off,int4 sz,
- const RangeList &uselim) { throw LowlevelError("addMap unimplemented"); }
- virtual SymbolEntry *addDynamicMapInternal(Symbol *sym,uint4 exfl,uint8 hash,int4 off,int4 sz,
- const RangeList &uselim) { throw LowlevelError("addMap unimplemented"); }
+ virtual void addSymbolInternal(Symbol *sym) { throw LowlevelError("addSymbolInternal unimplemented"); }
+ virtual void addMapInternal(Symbol *sym,MapEntry *entry) { throw LowlevelError("addMapInternal unimplemented"); }
+ virtual void addDynamicMapInternal(Symbol *sym,DynamicEntry *entry) { throw LowlevelError("addMapInternal unimplemented"); }
public:
ScopeGhidra(ArchitectureGhidra *g); ///< Constructor
@@ -67,8 +65,7 @@ public:
void lockDefaultProperties(void) { flagbaseDefault = ghidra->symboltab->getProperties(); cacheDirty = false; }
virtual ~ScopeGhidra(void);
virtual void clear(void);
- virtual SymbolEntry *addSymbol(const string &nm,Datatype *ct,
- const Address &addr,const Address &usepoint);
+ virtual MapEntry *addSymbol(const string &nm,Datatype *ct,const Address &addr,const Address &usepoint);
virtual string buildVariableName(const Address &addr,
const Address &pc,
Datatype *ct,int4 &index,uint4 flags) const {
@@ -79,10 +76,10 @@ public:
virtual void setDisplayFormat(Symbol *sym,uint4 attr) { cache->setDisplayFormat(sym,attr); }
virtual void adjustCaches(void) { cache->adjustCaches(); }
- virtual SymbolEntry *findAddr(const Address &addr,const Address &usepoint) const;
- virtual SymbolEntry *findContainer(const Address &addr,int4 size,
+ virtual MapEntry *findAddr(const Address &addr,const Address &usepoint) const;
+ virtual MapEntry *findContainer(const Address &addr,int4 size,
const Address &usepoint) const;
- virtual SymbolEntry *findClosestFit(const Address &addr,int4 size,
+ virtual MapEntry *findClosestFit(const Address &addr,int4 size,
const Address &usepoint) const {
throw LowlevelError("findClosestFit unimplemented"); }
virtual Funcdata *findFunction(const Address &addr) const;
@@ -90,16 +87,16 @@ public:
virtual LabSymbol *findCodeLabel(const Address &addr) const;
virtual Funcdata *resolveExternalRefFunction(ExternRefSymbol *sym) const;
- virtual SymbolEntry *findOverlap(const Address &addr,int4 size) const { throw LowlevelError("findOverlap unimplemented"); }
+ virtual MapEntry *findOverlap(const Address &addr,int4 size) const { throw LowlevelError("findOverlap unimplemented"); }
virtual void findByName(const string &nm,vector<Symbol *> &res) const { throw LowlevelError("findByName unimplemented"); }
virtual bool isNameUsed(const string &nm,const Scope *op2) const { throw LowlevelError("isNameUsed unimplemented"); }
virtual MapIterator begin(void) const { throw LowlevelError("begin unimplemented"); }
virtual MapIterator end(void) const { throw LowlevelError("end unimplemented"); }
- virtual list<SymbolEntry>::const_iterator beginDynamic(void) const { throw LowlevelError("beginDynamic unimplemented"); }
- virtual list<SymbolEntry>::const_iterator endDynamic(void) const { throw LowlevelError("endDynamic unimplemented"); }
- virtual list<SymbolEntry>::iterator beginDynamic(void) { throw LowlevelError("beginDynamic unimplemented"); }
- virtual list<SymbolEntry>::iterator endDynamic(void) { throw LowlevelError("endDynamic unimplemented"); }
+ virtual list<DynamicEntry *>::const_iterator beginDynamic(void) const { throw LowlevelError("beginDynamic unimplemented"); }
+ virtual list<DynamicEntry *>::const_iterator endDynamic(void) const { throw LowlevelError("endDynamic unimplemented"); }
+ virtual list<DynamicEntry *>::iterator beginDynamic(void) { throw LowlevelError("beginDynamic unimplemented"); }
+ virtual list<DynamicEntry *>::iterator endDynamic(void) { throw LowlevelError("endDynamic unimplemented"); }
virtual void clearCategory(int4 cat) { throw LowlevelError("clearCategory unimplemented"); }
virtual void clearUnlockedCategory(int4 cat) { throw LowlevelError("clearUnlockedCategory unimplemented"); }
virtual void clearUnlocked(void) { throw LowlevelError("clearUnlocked unimplemented"); }
@@ -130,8 +127,7 @@ class ScopeGhidraNamespace : public ScopeInternal {
friend class ScopeGhidra;
ArchitectureGhidra *ghidra; ///< Connection to the Ghidra client
protected:
- virtual SymbolEntry *addMapInternal(Symbol *sym,uint4 exfl,const Address &addr,int4 off,int4 sz,
- const RangeList &uselim);
+ virtual void addMapInternal(Symbol *sym,MapEntry *entry);
public:
ScopeGhidraNamespace(uint8 id,const string &nm,ArchitectureGhidra *g)
: ScopeInternal(id,nm,g) { ghidra = g; } ///< Constructor
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.cc
index cafdd93eab..5752fa72d1 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.cc
@@ -2993,7 +2993,10 @@ Datatype *ParameterSymbol::getType(void) const
Address ParameterSymbol::getAddress(void) const
{
- return sym->getFirstWholeMap()->getAddr();
+ SymbolEntry *entry = sym->getFirstWholeMap();
+ if (entry->isDynamic())
+ return Address();
+ return ((MapEntry *)entry)->getAddr();
}
int4 ParameterSymbol::getSize(void) const
@@ -3149,7 +3152,6 @@ ProtoParameter *ProtoStoreSymbol::setInput(int4 i, const string &nm,const Parame
{
ParameterSymbol *res = getSymbolBacked(i);
res->sym = scope->getCategorySymbol(Symbol::function_parameter,i);
- SymbolEntry *entry;
Address usepoint;
bool isindirect = (pieces.flags & ParameterPieces::indirectstorage) != 0;
@@ -3157,8 +3159,8 @@ ProtoParameter *ProtoStoreSymbol::setInput(int4 i, const string &nm,const Parame
bool istypelock = (pieces.flags & ParameterPieces::typelock) != 0;
bool isnamelock = (pieces.flags & ParameterPieces::namelock) != 0;
if (res->sym != (Symbol *)0) {
- entry = res->sym->getFirstWholeMap();
- if ((entry->getAddr() != pieces.addr)||(entry->getSize() != pieces.type->getSize())) {
+ SymbolEntry *entry = res->sym->getFirstWholeMap();
+ if (((MapEntry *)entry)->getAddr() != pieces.addr || entry->getSize() != pieces.type->getSize()) {
scope->removeSymbol(res->sym);
res->sym = (Symbol *)0;
}
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.cc
index 224302f135..da9ecd6e97 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.cc
@@ -355,7 +355,7 @@ Varnode *Funcdata::constructConstSpacebase(AddrSpace *id)
/// \param rampoint is the constant pointer interpreted as an Address
/// \param origval is the constant
/// \param origsize is the size of the constant
-void Funcdata::spacebaseConstant(PcodeOp *op,int4 slot,SymbolEntry *entry,const Address &rampoint,uintb origval,int4 origsize)
+void Funcdata::spacebaseConstant(PcodeOp *op,int4 slot,MapEntry *entry,const Address &rampoint,uintb origval,int4 origsize)
{
int4 sz = rampoint.getAddrSize();
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh
index f417560951..f394e91ff1 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh
@@ -103,15 +103,14 @@ class Funcdata {
// Low level Varnode functions
void setVarnodeProperties(Varnode *vn) const; ///< Look-up boolean properties and data-type information
HighVariable *assignHigh(Varnode *vn); ///< Assign a new HighVariable to a Varnode
- Symbol *handleSymbolConflict(SymbolEntry *entry,Varnode *vn); ///< Handle two variables with matching storage
bool syncVarnodesWithSymbol(VarnodeLocSet::const_iterator &iter,uint4 fl,Datatype *ct);
bool descend2Undef(Varnode *vn); ///< Transform all reads of the given Varnode to a special \b undefined constant
void splitUses(Varnode *vn); ///< Make all reads of the given Varnode unique
Varnode *cloneVarnode(const Varnode *vn); ///< Clone a Varnode (between copies of the function)
void destroyVarnode(Varnode *vn); ///< Delete the given Varnode from \b this function
- void coverVarnodes(SymbolEntry *entry,vector<Varnode *> &list);
- bool applyUnionFacet(SymbolEntry *entry,DynamicHash &dhash);
+ void coverVarnodes(MapEntry *entry,vector<Varnode *> &list);
+ bool applyUnionFacet(DynamicEntry *entry,DynamicHash &dhash);
// Low level op functions
void opZeroMulti(PcodeOp *op); ///< Transform trivial CPUI_MULTIEQUAL to CPUI_COPY
// Low level block functions
@@ -231,7 +230,7 @@ public:
Varnode *findSpacebaseInput(AddrSpace *id) const;
Varnode *constructSpacebaseInput(AddrSpace *id);
Varnode *constructConstSpacebase(AddrSpace *id);
- void spacebaseConstant(PcodeOp *op,int4 slot,SymbolEntry *entry,const Address &rampoint,uintb origval,int4 origsize);
+ void spacebaseConstant(PcodeOp *op,int4 slot,MapEntry *entry,const Address &rampoint,uintb origval,int4 origsize);
int4 getHeritagePass(void) const { return heritage.getPass(); } ///< Get overall count of heritage passes
@@ -434,6 +433,8 @@ public:
void clearDeadOps(void) { obank.destroyDead(); } ///< Delete any dead PcodeOps
void remapVarnode(Varnode *vn,Symbol *sym,const Address &usepoint);
void remapDynamicVarnode(Varnode *vn,Symbol *sym,const Address &usepoint,uint8 hash);
+ void remapConflictSymbol(Symbol *sym); ///< Convert any MapEntryConflict on the Symbol into a DynamicEntry
+ bool detectSymbolConflicts(Varnode *vn); ///< Detect potential symbol conflicts
void linkProtoPartial(Varnode *vn); ///< Find or create Symbol and a partial mapping
Symbol *linkSymbol(Varnode *vn); ///< Find or create Symbol associated with given Varnode
Symbol *linkSymbolReference(Varnode *vn); ///< Discover and attach Symbol to a constant reference
@@ -441,8 +442,8 @@ public:
void findLinkedVarnodes(SymbolEntry *entry,vector<Varnode *> &res) const; ///< Find Varnodes that map to the given SymbolEntry
void buildDynamicSymbol(Varnode *vn); ///< Build a \e dynamic Symbol associated with the given Varnode
bool testForReturnAddress(Varnode *vn); ///< Test if the given Varnode is (derived from) the return address
- bool attemptDynamicMapping(SymbolEntry *entry,DynamicHash &dhash);
- bool attemptDynamicMappingLate(SymbolEntry *entry,DynamicHash &dhash);
+ bool attemptDynamicMapping(DynamicEntry *entry,DynamicHash &dhash);
+ bool attemptDynamicMappingLate(DynamicEntry *entry,DynamicHash &dhash);
Merge &getMerge(void) { return covermerge; } ///< Get the Merge object for \b this function
Varnode *getInternalString(const uint1 *buf,int4 size,Datatype *ptrType,PcodeOp *readOp);
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_varnode.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_varnode.cc
index ae98900d54..077d687050 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_varnode.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_varnode.cc
@@ -1021,46 +1021,6 @@ bool Funcdata::syncVarnodesWithSymbols(const ScopeLocal *lm,bool updateDatatypes
return updateoccurred;
}
-/// A Varnode overlaps the given SymbolEntry. Make sure the Varnode is part of the variable
-/// underlying the Symbol. If not, remap things so that the Varnode maps to a distinct Symbol.
-/// In either case, attach the appropriate Symbol to the Varnode
-/// \param entry is the given SymbolEntry
-/// \param vn is the overlapping Varnode
-/// \return the Symbol attached to the Varnode
-Symbol *Funcdata::handleSymbolConflict(SymbolEntry *entry,Varnode *vn)
-
-{
- if (vn->isInput() || vn->isAddrTied() ||
- vn->isPersist() || vn->isConstant() || entry->isDynamic()) {
- vn->setSymbolEntry(entry);
- return entry->getSymbol();
- }
- HighVariable *high = vn->getHigh();
- Varnode *otherVn;
- HighVariable *otherHigh = (HighVariable *)0;
- // Look for a conflicting HighVariable
- VarnodeLocSet::const_iterator iter = beginLoc(entry->getSize(),entry->getAddr());
- while(iter != endLoc()) {
- otherVn = *iter;
- if (otherVn->getSize() != entry->getSize()) break;
- if (otherVn->getAddr() != entry->getAddr()) break;
- HighVariable *tmpHigh = otherVn->getHigh();
- if (tmpHigh != high) {
- otherHigh = tmpHigh;
- break;
- }
- ++iter;
- }
- if (otherHigh == (HighVariable *)0) {
- vn->setSymbolEntry(entry);
- return entry->getSymbol();
- }
-
- // If we reach here, we have a conflicting variable
- buildDynamicSymbol(vn);
- return vn->getSymbolEntry()->getSymbol();
-}
-
/// \brief Update properties (and the data-type) for a set of Varnodes associated with one Symbol
///
/// The set of Varnodes with the same size and address all have their boolean properties
@@ -1158,6 +1118,65 @@ void Funcdata::remapDynamicVarnode(Varnode *vn,Symbol *sym,const Address &usepoi
vn->setSymbolEntry(entry);
}
+/// If there is no conflict, nothing happens. If there is a conflict but it can't be resolved,
+/// throw an exception. Otherwise a DynamicEntry is generated and becomes the primary SymbolEntry
+/// \param sym is the Symbol to resolve
+void Funcdata::remapConflictSymbol(Symbol *sym)
+
+{
+ SymbolEntry *entry = sym->getFirstWholeMap();
+ if (!entry->isConflict()) return;
+ MapEntryConflict *map = (MapEntryConflict *)entry;
+ Varnode *vn = findVarnodeWritten(map->getSize(), map->getAddr(), map->getFirstUseAddress(), map->getUnique());
+ if (vn == (Varnode *)0)
+ throw LowlevelError("Cannot resolve SymbolEntry conflict on "+sym->getName()+": no varnode");
+ DynamicHash dhash;
+
+ dhash.uniqueHash(vn,this); // Calculate the dynamic hash
+ if (dhash.getHash() == 0)
+ throw LowlevelError("Cannot resolve SymbolEntry conflict on "+sym->getName()+": cannot generate hash");
+ remapDynamicVarnode(vn,sym,vn->getUsePoint(*this),dhash.getHash());
+}
+
+/// If the given Varnode shares the same storage and instruction as another Varnode, but with
+/// a different HighVariable, return \b true.
+/// \param vn is the given Varnode
+/// \return \b true if the Varnode storage is shared across different HighVariables
+bool Funcdata::detectSymbolConflicts(Varnode *vn)
+
+{
+ if (vn->isAddrTied()) return false;
+ if (!vn->isWritten()) return false;
+ if (vn->getSpace()->getType() == IPTR_INTERNAL)
+ return true; // Temporary registers are automatically assumed to be in conflict
+ VarnodeDefSet::const_iterator iter = vn->defiter;
+ VarnodeDefSet::const_iterator enditer = vbank.endDef();
+ ++iter;
+ while(iter != enditer) {
+ Varnode *otherVn = *iter;
+ if (!otherVn->isWritten()) break;
+ if (vn->def->getSeqNum().getAddr() != otherVn->def->getSeqNum().getAddr()) break;
+ if (vn->intersects(*otherVn)) {
+ if (vn->high != otherVn->high && !vn->high->isSameGroup(otherVn->high))
+ return true;
+ }
+ ++iter;
+ }
+ iter = vn->defiter;
+ enditer = vbank.beginDef();
+ while(iter != enditer) {
+ --iter;
+ Varnode *otherVn = *iter;
+ if (!otherVn->isWritten()) break;
+ if (vn->def->getSeqNum().getAddr() != otherVn->def->getSeqNum().getAddr()) break;
+ if (vn->intersects(*otherVn)) {
+ if (vn->high != otherVn->high && !vn->high->isSameGroup(otherVn->high))
+ return true;
+ }
+ }
+ return false;
+}
+
/// PIECE operations put the given Varnode into a larger structure. Find the resulting
/// whole Varnode, make sure it has a symbol assigned, and then assign the same symbol
/// to the given Varnode piece. If the given Varnode has been merged with something
@@ -1192,25 +1211,29 @@ Symbol *Funcdata::linkSymbol(Varnode *vn)
if (vn->isProtoPartial())
linkProtoPartial(vn);
HighVariable *high = vn->getHigh();
- SymbolEntry *entry;
- uint4 fl = 0;
Symbol *sym = high->getSymbol();
if (sym != (Symbol *)0) return sym; // Symbol already assigned
+ uint4 fl = 0;
Address usepoint = vn->getUsePoint(*this);
// Find any entry overlapping base address
- entry = localmap->queryProperties(vn->getAddr(), 1, usepoint, fl);
- if (entry != (SymbolEntry *) 0) {
- sym = handleSymbolConflict(entry, vn);
+ SymbolEntry *entry = localmap->queryProperties(vn->getAddr(), 1, usepoint, fl);
+ if (entry != (SymbolEntry *)0 && !entry->isConflict()) {
+ vn->setSymbolEntry(entry);
+ return entry->getSymbol();
}
- else { // Must create a symbol entry
- if (!vn->isPersist()) { // Only create local symbol
+ if (!vn->isPersist()) { // Only create local symbol
+ if (detectSymbolConflicts(vn)) {
+ entry = localmap->addSymbolWithConflict("", high->getType(), vn);
+ }
+ else {
+ Address usepoint = vn->getUsePoint(*this);
if (vn->isAddrTied())
usepoint = Address();
entry = localmap->addSymbol("", high->getType(), vn->getAddr(), usepoint);
- sym = entry->getSymbol();
- vn->setSymbolEntry(entry);
}
+ sym = entry->getSymbol();
+ vn->setSymbolEntry(entry);
}
return sym;
@@ -1237,8 +1260,8 @@ Symbol *Funcdata::linkSymbolReference(Varnode *vn)
Address addr = sb->getAddress(vn->getOffset(),in0->getSize(),op->getAddr());
if (addr.isInvalid())
throw LowlevelError("Unable to generate proper address from spacebase");
- SymbolEntry *entry = scope->queryContainer(addr,1,Address());
- if (entry == (SymbolEntry *)0)
+ MapEntry *entry = scope->queryContainer(addr,1,Address());
+ if (entry == (MapEntry *)0)
return (Symbol *)0;
int4 off = (int4)(addr.getOffset() - entry->getAddr().getOffset()) + entry->getOffset();
vn->setSymbolReference(entry, off);
@@ -1253,18 +1276,19 @@ Varnode *Funcdata::findLinkedVarnode(SymbolEntry *entry) const
{
if (entry->isDynamic()) {
DynamicHash dhash;
- Varnode *vn = dhash.findVarnode(this, entry->getFirstUseAddress(), entry->getHash());
+ Varnode *vn = dhash.findVarnode(this, entry->getFirstUseAddress(), ((DynamicEntry *)entry)->getHash());
if (vn == (Varnode *)0 || vn->isAnnotation())
return (Varnode *)0;
return vn;
}
+ MapEntry *mapentry = (MapEntry *)entry;
VarnodeLocSet::const_iterator iter,enditer;
- Address usestart = entry->getFirstUseAddress();
- enditer = vbank.endLoc(entry->getSize(),entry->getAddr());
+ Address usestart = mapentry->getFirstUseAddress();
+ enditer = vbank.endLoc(mapentry->getSize(),mapentry->getAddr());
if (usestart.isInvalid()) {
- iter = vbank.beginLoc(entry->getSize(),entry->getAddr());
+ iter = vbank.beginLoc(mapentry->getSize(),mapentry->getAddr());
if (iter == enditer)
return (Varnode *)0;
Varnode *vn = *iter;
@@ -1272,12 +1296,12 @@ Varnode *Funcdata::findLinkedVarnode(SymbolEntry *entry) const
return (Varnode *)0; // Varnode(s) must be address tied in order to match this symbol
return vn;
}
- iter = vbank.beginLoc(entry->getSize(),entry->getAddr(),usestart,~((uintm)0));
+ iter = vbank.beginLoc(mapentry->getSize(),mapentry->getAddr(),usestart,~((uintm)0));
// TODO: Use a better end iterator
for(;iter!=enditer;++iter) {
Varnode *vn = *iter;
Address usepoint = vn->getUsePoint(*this);
- if (entry->inUse(usepoint))
+ if (mapentry->inUse(usepoint))
return vn;
}
return (Varnode *)0;
@@ -1292,13 +1316,13 @@ void Funcdata::findLinkedVarnodes(SymbolEntry *entry,vector<Varnode *> &res) con
{
if (entry->isDynamic()) {
DynamicHash dhash;
- Varnode *vn = dhash.findVarnode(this,entry->getFirstUseAddress(),entry->getHash());
+ Varnode *vn = dhash.findVarnode(this,entry->getFirstUseAddress(),((DynamicEntry *)entry)->getHash());
if (vn != (Varnode *)0)
res.push_back(vn);
}
else {
- VarnodeLocSet::const_iterator iter = beginLoc(entry->getSize(),entry->getAddr());
- VarnodeLocSet::const_iterator enditer = endLoc(entry->getSize(),entry->getAddr());
+ VarnodeLocSet::const_iterator iter = beginLoc(entry->getSize(),((MapEntry *)entry)->getAddr());
+ VarnodeLocSet::const_iterator enditer = endLoc(entry->getSize(),((MapEntry *)entry)->getAddr());
for(;iter!=enditer;++iter) {
Varnode *vn = *iter;
Address addr = vn->getUsePoint(*this);
@@ -1316,8 +1340,6 @@ void Funcdata::findLinkedVarnodes(SymbolEntry *entry,vector<Varnode *> &res) con
void Funcdata::buildDynamicSymbol(Varnode *vn)
{
- if (vn->isTypeLock()||vn->isNameLock())
- throw RecovError("Trying to build dynamic symbol on locked varnode");
if (!isHighOn())
throw RecovError("Cannot create dynamic symbols until decompile has completed");
HighVariable *high = vn->getHigh();
@@ -1344,7 +1366,7 @@ void Funcdata::buildDynamicSymbol(Varnode *vn)
/// \param entry is the (dynamic) Symbol entry
/// \param dhash is the dynamic mapping information
/// \return \b true if a Varnode was adjusted
-bool Funcdata::attemptDynamicMapping(SymbolEntry *entry,DynamicHash &dhash)
+bool Funcdata::attemptDynamicMapping(DynamicEntry *entry,DynamicHash &dhash)
{
Symbol *sym = entry->getSymbol();
@@ -1377,7 +1399,7 @@ bool Funcdata::attemptDynamicMapping(SymbolEntry *entry,DynamicHash &dhash)
/// \param entry is the (dynamic) Symbol entry
/// \param dhash is the dynamic mapping information
/// \return \b true if a Varnode was adjusted
-bool Funcdata::attemptDynamicMappingLate(SymbolEntry *entry,DynamicHash &dhash)
+bool Funcdata::attemptDynamicMappingLate(DynamicEntry *entry,DynamicHash &dhash)
{
dhash.clear();
@@ -1636,7 +1658,7 @@ Address Funcdata::findDisjointCover(Varnode *vn,int4 &sz)
/// overlaps its first byte (to guarantee a link). If one doesn't exist it is created.
/// \param entry is the existing Symbol entry
/// \param list is the list of Varnodes
-void Funcdata::coverVarnodes(SymbolEntry *entry,vector<Varnode *> &list)
+void Funcdata::coverVarnodes(MapEntry *entry,vector<Varnode *> &list)
{
Scope *scope = entry->getSymbol()->getScope();
@@ -1647,8 +1669,8 @@ void Funcdata::coverVarnodes(SymbolEntry *entry,vector<Varnode *> &list)
if (i+1<list.size() && list[i+1]->getAddr() == vn->getAddr())
continue;
Address usepoint = vn->getUsePoint(*this);
- SymbolEntry *overlapEntry = scope->findContainer(vn->getAddr(), vn->getSize(), usepoint);
- if (overlapEntry == (SymbolEntry *)0) {
+ MapEntry *overlapEntry = scope->findContainer(vn->getAddr(), vn->getSize(), usepoint);
+ if (overlapEntry == (MapEntry *)0) {
int4 diff = (int4)(vn->getOffset() - entry->getAddr().getOffset());
ostringstream s;
s << entry->getSymbol()->getName() << '_' << diff;
@@ -1667,7 +1689,7 @@ void Funcdata::coverVarnodes(SymbolEntry *entry,vector<Varnode *> &list)
/// \param entry is the given SymbolEntry
/// \param dhash is preallocated storage for calculating the dynamic hash
/// \return \b true if the UnionFacetSymbol is successfully cached
-bool Funcdata::applyUnionFacet(SymbolEntry *entry,DynamicHash &dhash)
+bool Funcdata::applyUnionFacet(DynamicEntry *entry,DynamicHash &dhash)
{
UnionFacetSymbol *sym = (UnionFacetSymbol *)entry->getSymbol();
@@ -1711,7 +1733,7 @@ bool Funcdata::applyUnionFacet(SymbolEntry *entry,DynamicHash &dhash)
void Funcdata::mapGlobals(void)
{
- SymbolEntry *entry;
+ MapEntry *entry;
VarnodeLocSet::const_iterator iter,enditer;
Varnode *vn,*maxvn;
Datatype *ct;
@@ -1757,7 +1779,7 @@ void Funcdata::mapGlobals(void)
Address usepoint;
// Find any entry overlapping base address
entry = localmap->queryProperties(addr,1,usepoint,fl);
- if (entry==(SymbolEntry *)0) {
+ if (entry==(MapEntry *)0) {
Scope *discover = localmap->discoverScope(addr,ct->getSize(),usepoint);
if (discover == (Scope *)0)
throw LowlevelError("Could not discover scope");
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.cc
index 385d74c8f1..aeebe9dfc4 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.cc
@@ -564,16 +564,15 @@ void IfcMapaddress::execute(istream &s)
sym->getScope()->setAttribute(sym,Varnode::namelock|Varnode::typelock);
}
else {
- Symbol *sym;
uint4 flags = Varnode::namelock|Varnode::typelock;
flags |= dcp->conf->symboltab->getProperty(addr); // Inherit existing properties
string basename;
Scope *scope = dcp->conf->symboltab->findCreateScopeFromSymbolName(name, basename, (Scope *)0);
- sym = scope->addSymbol(basename,ct,addr,Address())->getSymbol();
+ MapEntry *entry = scope->addSymbol(basename,ct,addr,Address());
+ Symbol *sym = entry->getSymbol();
sym->getScope()->setAttribute(sym,flags);
if (scope->getParent() != (Scope *)0) { // If this is a global namespace scope
- SymbolEntry *e = sym->getFirstWholeMap(); // Adjust range
- dcp->conf->symboltab->addRange(scope,e->getAddr().getSpace(),e->getFirst(),e->getLast());
+ dcp->conf->symboltab->addRange(scope,entry->getAddr().getSpace(),entry->getFirst(),entry->getLast());
}
}
@@ -1354,6 +1353,7 @@ void IfcRename::execute(istream &s)
if (sym->getCategory() == Symbol::function_parameter)
dcp->fd->getFuncProto().setInputLock(true);
+ dcp->fd->remapConflictSymbol(sym);
sym->getScope()->renameSymbol(sym,newname);
sym->getScope()->setAttribute(sym,Varnode::namelock|Varnode::typelock);
}
@@ -1412,6 +1412,7 @@ void IfcRetype::execute(istream &s)
if (sym->getCategory()==Symbol::function_parameter)
dcp->fd->getFuncProto().setInputLock(true);
+ dcp->fd->remapConflictSymbol(sym);
sym->getScope()->retypeSymbol(sym,ct);
sym->getScope()->setAttribute(sym,Varnode::typelock);
if ((newname.size()!=0)&&(newname != name)) {
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.cc
index f0d81cac65..e22e1ab8b5 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.cc
@@ -1174,10 +1174,10 @@ void JumpBasic::calcRange(Varnode *vn,CircleRange &rng) const
}
}
-/// \brief Return \b true if the \b newRange is preferred over the \b oldRange
+/// \brief Return \b true if the \b newRange is preferred over the current range
///
/// If the ranges are equal keep the old. Prefer a range with a smaller step,min,or size.
-/// \param oldRange is the old range
+/// \param pos is the position of Varnode associated with the \b newRange
/// \param newRange is the new range
/// \return \b true if the new range is preferred
bool JumpBasic::isPreferredRange(int4 pos,const CircleRange &newRange)
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/options.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/options.cc
index ea9f8f5c96..c95fb051b8 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/options.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/options.cc
@@ -784,7 +784,7 @@ string OptionErrorTooManyInstructions::apply(Architecture *glb,const string &p1,
return res;
}
-/// \class OptionErrorBadData
+/// \class OptionBadDataCount
/// \brief Toggle whether disassembling bytes that can't be decoded or at a non-existent address is considered a fatal error.
///
/// If the first parameter is "on" then any BadDataError encountered while following flow will cause a fatal error.
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/override.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/override.hh
index edb75eec7b..9696fc89b5 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/override.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/override.hh
@@ -85,7 +85,7 @@ public:
virtual void performOverride(const Address &addr,Funcdata &data) const;
virtual void encode(Encoder &encoder,const Address &addr) const;
virtual void printRaw(ostream &s,const Address &addr) const;
- static const string NAME;
+ static const string NAME; ///< Name of the branch override record
};
/// \brief Override a BRANCH, BRANCHIND, or RETURN to become a CALL or CALLIND operation
@@ -94,7 +94,7 @@ public:
virtual void performOverride(const Address &addr,Funcdata &data) const;
virtual void encode(Encoder &encoder,const Address &addr) const;
virtual void printRaw(ostream &s,const Address &addr) const;
- static const string NAME;
+ static const string NAME; ///< Name of the call override record
};
/// \brief Override a BRANCH, BRANCHIND, or RETURN to become a CALL or CALLIND, followed by a RETURN
@@ -103,7 +103,7 @@ public:
virtual void performOverride(const Address &addr,Funcdata &data) const;
virtual void encode(Encoder &encoder,const Address &addr) const;
virtual void printRaw(ostream &s,const Address &addr) const;
- static const string NAME;
+ static const string NAME; ///< Name of the callreturn override record
};
/// \brief Override a BRANCHIND or CALLIND to become a RETURN
@@ -112,40 +112,40 @@ public:
virtual void performOverride(const Address &addr,Funcdata &data) const;
virtual void encode(Encoder &encoder,const Address &addr) const;
virtual void printRaw(ostream &s,const Address &addr) const;
- static const string NAME;
+ static const string NAME; ///< Name of the return override record
};
/// \brief Convert a CALLOTHER to a direct CALL
class CallotherCall : public Record {
Address callAddress; ///< Address of direct CALL
public:
- CallotherCall(const Address &dest) : callAddress(dest) {}
+ CallotherCall(const Address &dest) : callAddress(dest) {} ///< Constructor
virtual void performOverride(const Address &addr,Funcdata &data) const;
virtual void encode(Encoder &encoder,const Address &addr) const;
virtual void printRaw(ostream &s,const Address &addr) const;
- static const string NAME;
+ static const string NAME; ///< Name of the callother -> call override record
};
/// \brief Convert a CALLOTHER to a BRANCH
class CallotherBranch : public Record {
Address branchAddress; ///< Destination of BRANCH
public:
- CallotherBranch(const Address &dest) : branchAddress(dest) {}
+ CallotherBranch(const Address &dest) : branchAddress(dest) {} ///< Constructor
virtual void performOverride(const Address &addr,Funcdata &data) const;
virtual void encode(Encoder &encoder,const Address &addr) const;
virtual void printRaw(ostream &s,const Address &addr) const;
- static const string NAME;
+ static const string NAME; ///< Name of the callther -> branch override record
};
/// \brief Convert CALL or CALLIND into a CALL with a new destination address
class CallCall : public Record {
Address callAddress; ///< Address of (new) CALL
public:
- CallCall(const Address &dest) : callAddress(dest) {}
+ CallCall(const Address &dest) : callAddress(dest) {} ///< Constructor
virtual void performOverride(const Address &addr,Funcdata &data) const;
virtual void encode(Encoder &encoder,const Address &addr) const;
virtual void printRaw(ostream &s,const Address &addr) const;
- static const string NAME;
+ static const string NAME; ///< Name of the call -> call override record
};
private:
map<Address,Record *> pcodeover; ///< Raw p-code overrides for instructions
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/printc.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/printc.cc
index 00411be63a..f0d530d1de 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/printc.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/printc.cc
@@ -1929,18 +1929,18 @@ void PrintC::pushAnnotation(const Varnode *vn,const PcodeOp *op)
int4 userind = (int4)op->getIn(0)->getOffset();
size = glb->userops.getOp(userind)->extractAnnotationSize(vn, op);
}
- SymbolEntry *entry;
+ MapEntry *entry;
if (size != 0)
entry = symScope->queryContainer(vn->getAddr(),size,op->getAddr());
else {
entry = symScope->queryContainer(vn->getAddr(),1,op->getAddr());
- if (entry != (SymbolEntry *)0)
+ if (entry != (MapEntry *)0)
size = entry->getSize();
else
size = vn->getSize();
}
- if (entry != (SymbolEntry *)0) {
+ if (entry != (MapEntry *)0) {
if (entry->getSize() == size)
pushSymbol(entry->getSymbol(),vn,op);
else {
@@ -2083,9 +2083,7 @@ void PrintC::pushPartialSymbol(const Symbol *sym,int4 off,int4 sz,
}
else if (allowCast) {
Datatype *outtype = vn->getHigh()->getType();
- AddrSpace *spc = sym->getFirstWholeMap()->getAddr().getSpace();
- if (spc == (AddrSpace *)0)
- spc = vn->getSpace();
+ AddrSpace *spc = vn->getHigh()->getNameRepresentative()->getSpace();
if (castStrategy->isSubpieceCastEndian(outtype,ct,off,spc->isBigEndian())) {
// Treat truncation as SUBPIECE style cast
finalcast = outtype;
@@ -2667,7 +2665,7 @@ bool PrintC::emitScopeVarDecls(const Scope *symScope,int4 cat)
MapIterator iter = symScope->begin();
MapIterator enditer = symScope->end();
for(;iter!=enditer;++iter) {
- const SymbolEntry *entry = *iter;
+ const MapEntry *entry = *iter;
if (entry->isPiece()) continue; // Don't do a partial entry
Symbol *sym = entry->getSymbol();
if (sym->getCategory() != cat) continue;
@@ -2683,18 +2681,14 @@ bool PrintC::emitScopeVarDecls(const Scope *symScope,int4 cat)
notempty = true;
emitVarDeclStatement(sym);
}
- list<SymbolEntry>::const_iterator iter_d = symScope->beginDynamic();
- list<SymbolEntry>::const_iterator enditer_d = symScope->endDynamic();
+ list<DynamicEntry *>::const_iterator iter_d = symScope->beginDynamic();
+ list<DynamicEntry *>::const_iterator enditer_d = symScope->endDynamic();
for(;iter_d!=enditer_d;++iter_d) {
- const SymbolEntry *entry = &(*iter_d);
+ const DynamicEntry *entry = *iter_d;
if (entry->isPiece()) continue; // Don't do a partial entry
- Symbol *sym = (*iter_d).getSymbol();
+ Symbol *sym = entry->getSymbol();
if (sym->getCategory() != cat) continue;
if (sym->getName().size() == 0) continue;
- if (dynamic_cast<FunctionSymbol *>(sym) != (FunctionSymbol *)0)
- continue;
- if (dynamic_cast<LabSymbol *>(sym) != (LabSymbol *)0)
- continue;
if (sym->isMultiEntry()) {
if (sym->getFirstWholeMap() != entry)
continue;
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
index cc79bf9e77..a9db3b8008 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
@@ -7499,9 +7499,11 @@ Datatype *RulePieceStructure::determineDatatype(Varnode *vn,int4 &baseOffset)
if (ct->getSize() != vn->getSize()) { // vn is a partial
SymbolEntry *entry = vn->getSymbolEntry();
- baseOffset = vn->getAddr().overlap(0,entry->getAddr(),ct->getSize());
+ if (entry->isDynamic())
+ return (Datatype *)0;
+ baseOffset = vn->getAddr().overlap(0,((MapEntry *)entry)->getAddr(),ct->getSize());
if (baseOffset < 0)
- return (Datatype*)0;
+ return (Datatype *)0;
baseOffset += entry->getOffset();
// Find concrete sub-type that matches the size of the Varnode
Datatype *subType = ct;
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc
index 4720263081..5b5bbf83fe 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc
@@ -3450,13 +3450,13 @@ Datatype *TypeSpacebase::getSubType(int8 off,int8 *newoff) const
Address nullPoint;
uintb fullEncoding;
Address addr = glb->resolveConstant(spaceid, addrOff, -1, nullPoint, fullEncoding);
- SymbolEntry *smallest;
+ MapEntry *smallest;
// Assume symbol being referenced is address tied so we use a null point of context
// FIXME: A valid point of context may be necessary in the future
smallest = scope->queryContainer(addr,1,nullPoint);
- if (smallest == (SymbolEntry *)0) {
+ if (smallest == (MapEntry *)0) {
*newoff = 0;
return glb->types->getBase(1,TYPE_UNKNOWN);
}
@@ -3474,10 +3474,10 @@ int8 TypeSpacebase::nearestArrayedComponentForward(int8 off,int8 max,int8 *newof
Address nullPoint;
uintb fullEncoding;
Address addr = glb->resolveConstant(spaceid, addrOff, -1, nullPoint, fullEncoding);
- SymbolEntry *smallest = scope->queryContainer(addr,1,nullPoint);
+ MapEntry *smallest = scope->queryContainer(addr,1,nullPoint);
Address nextAddr;
Datatype *symbolType;
- if (smallest == (SymbolEntry *)0 || smallest->getOffset() != 0)
+ if (smallest == (MapEntry *)0 || smallest->getOffset() != 0)
nextAddr = addr + 32;
else {
symbolType = smallest->getSymbol()->getType();
@@ -3496,7 +3496,7 @@ int8 TypeSpacebase::nearestArrayedComponentForward(int8 off,int8 max,int8 *newof
if (nextAddr < addr)
return -1; // Don't let the address wrap
smallest = scope->queryContainer(nextAddr,1,nullPoint);
- if (smallest == (SymbolEntry *)0 || smallest->getOffset() != 0)
+ if (smallest == (MapEntry *)0 || smallest->getOffset() != 0)
return -1;
symbolType = smallest->getSymbol()->getType();
*newoff = addr.getOffset() - smallest->getAddr().getOffset();
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/variable.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/variable.cc
index 18af26c1ff..458fb54bba 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/variable.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/variable.cc
@@ -263,10 +263,10 @@ void HighVariable::setSymbol(Varnode *vn) const
else if (symbol->getCategory() == Symbol::equate)
symboloffset = -1; // For equates, we don't care about size
else if (symbol->getType()->getSize() == vn->getSize() &&
- entry->getAddr() == vn->getAddr() && !entry->isPiece())
+ ((MapEntry *)entry)->getAddr() == vn->getAddr() && !entry->isPiece())
symboloffset = -1; // A matching entry
else {
- symboloffset = vn->getAddr().overlapJoin(0,entry->getAddr(),symbol->getType()->getSize()) + entry->getOffset();
+ symboloffset = vn->getAddr().overlapJoin(0,((MapEntry *)entry)->getAddr(),symbol->getType()->getSize()) + entry->getOffset();
}
if (type != (Datatype *)0 && type->getMetatype() == TYPE_PARTIALUNION)
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.cc
index 27330a23c0..6d51a72cd2 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.cc
@@ -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.
@@ -371,7 +371,8 @@ 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();
- addTypeRecommendation(entry->getAddr(), dt);
+ if (!entry->isDynamic())
+ addTypeRecommendation(((MapEntry *)entry)->getAddr(), dt);
}
}
}
@@ -597,7 +598,7 @@ bool ScopeLocal::adjustFit(RangeHint &a) const
a.size = (int4)maxsize;
}
// We want ANY symbol that might be within this range
- SymbolEntry *entry = findOverlap(addr,a.size);
+ MapEntry *entry = findOverlap(addr,a.size);
if (entry == (SymbolEntry *)0)
return true;
if (entry->getAddr() <= addr) {
@@ -1044,14 +1045,15 @@ void MapState::addGuard(const LoadGuard &guard,OpCode opc,TypeFactory *typeFacto
void MapState::gatherSymbols(const EntryMap *rangemap)
{
- list<SymbolEntry>::const_iterator riter;
+ list<SymbolRange>::const_iterator riter;
Symbol *sym;
if (rangemap == (EntryMap *)0) return;
for(riter=rangemap->begin_list();riter!=rangemap->end_list();++riter) {
- sym = (*riter).getSymbol();
+ const MapEntry *entry = (*riter).entry;
+ sym = entry->getSymbol();
if (sym == (Symbol *)0) continue;
// if ((*iter).isPiece()) continue; // This should probably never happen
- uintb start = (*riter).getAddr().getOffset();
+ uintb start = entry->getAddr().getOffset();
Datatype *ct = sym->getType();
uint4 flags = sym->isTypeLocked() ? RangeHint::typelock : 0;
addRange(start,ct,flags,RangeHint::fixed,-1);
@@ -1334,7 +1336,7 @@ void ScopeLocal::markUnaliased(const vector<uintb> &alias)
{
EntryMap *rangemap = maptable[space->getIndex()];
if (rangemap == (EntryMap *)0) return;
- list<SymbolEntry>::iterator iter,enditer;
+ list<SymbolRange>::iterator iter,enditer;
set<Range>::const_iterator rangeIter, rangeEndIter;
rangeIter = getRangeTree().begin();
rangeEndIter = getRangeTree().end();
@@ -1348,8 +1350,9 @@ void ScopeLocal::markUnaliased(const vector<uintb> &alias)
enditer = rangemap->end_list();
while(iter!=enditer) {
- SymbolEntry &entry(*iter++);
- uintb curoff = entry.getAddr().getOffset() + entry.getSize() - 1;
+ const MapEntry *entry = (*iter).entry;
+ ++iter;
+ uintb curoff = entry->getAddr().getOffset() + entry->getSize() - 1;
while ((i<alias.size()) && (alias[i] <= curoff)) {
aliason = true;
curalias = alias[i++];
@@ -1366,7 +1369,7 @@ void ScopeLocal::markUnaliased(const vector<uintb> &alias)
}
++rangeIter;
}
- Symbol *symbol = entry.getSymbol();
+ Symbol *symbol = entry->getSymbol();
// Test if there is enough distance between symbol
// and last alias to warrant ignoring the alias
// NOTE: this is primarily to reset aliasing between
@@ -1460,7 +1463,7 @@ SymbolEntry *ScopeLocal::remapSymbol(Symbol *sym,const Address &addr,const Addre
SymbolEntry *entry = sym->getFirstWholeMap();
int4 size = entry->getSize();
if (!entry->isDynamic()) {
- if (entry->getAddr() == addr) {
+ if (((MapEntry *)entry)->getAddr() == addr) {
if (usepoint.isInvalid() && entry->getFirstUseAddress().isInvalid())
return entry;
if (entry->getFirstUseAddress() == usepoint)
@@ -1471,7 +1474,9 @@ SymbolEntry *ScopeLocal::remapSymbol(Symbol *sym,const Address &addr,const Addre
RangeList rnglist;
if (!usepoint.isInvalid())
rnglist.insertRange(usepoint.getSpace(),usepoint.getOffset(),usepoint.getOffset());
- return addMapInternal(sym,Varnode::mapped,addr,0,size,rnglist);
+ MapEntry *res = new MapEntry(sym,Varnode::mapped,addr,size,0,rnglist);
+ addMapInternal(sym,res);
+ return res;
}
/// \brief Make the primary mapping for the given Symbol, dynamic
@@ -1488,14 +1493,16 @@ SymbolEntry *ScopeLocal::remapSymbolDynamic(Symbol *sym,uint8 hash,const Address
SymbolEntry *entry = sym->getFirstWholeMap();
int4 size = entry->getSize();
if (entry->isDynamic()) {
- if (entry->getHash() == hash && entry->getFirstUseAddress() == usepoint)
+ if (((DynamicEntry *)entry)->getHash() == hash && entry->getFirstUseAddress() == usepoint)
return entry;
}
removeSymbolMappings(sym);
RangeList rnglist;
if (!usepoint.isInvalid())
rnglist.insertRange(usepoint.getSpace(),usepoint.getOffset(),usepoint.getOffset());
- return addDynamicMapInternal(sym,Varnode::mapped,hash,0,size,rnglist);
+ DynamicEntry *newEntry = new DynamicEntry(sym,Varnode::mapped,hash,0,size,rnglist);
+ addDynamicMapInternal(sym,newEntry);
+ return newEntry;
}
/// \brief Run through name recommendations, checking if any match unnamed symbols
@@ -1516,7 +1523,7 @@ void ScopeLocal::recoverNameRecommendationsForSymbols(void)
Symbol *sym;
Varnode *vn = (Varnode *)0;
if (usepoint.isInvalid()) {
- SymbolEntry *entry = findOverlap(addr, size); // Recover any Symbol regardless of usepoint
+ MapEntry *entry = findOverlap(addr, size); // Recover any Symbol regardless of usepoint
if (entry == (SymbolEntry *)0) continue;
if (entry->getAddr() != addr) // Make sure Symbol has matching address
continue;
@@ -1603,7 +1610,7 @@ void ScopeLocal::addRecommendName(Symbol *sym)
SymbolEntry *entry = sym->getFirstWholeMap();
if (entry == (SymbolEntry *) 0) return;
if (entry->isDynamic()) {
- dynRecommend.emplace_back(entry->getFirstUseAddress(), entry->getHash(), sym->getName(), sym->getId());
+ dynRecommend.emplace_back(entry->getFirstUseAddress(), ((DynamicEntry *)entry)->getHash(), sym->getName(), sym->getId());
}
else {
Address usepoint((AddrSpace *)0,0);
@@ -1611,7 +1618,7 @@ void ScopeLocal::addRecommendName(Symbol *sym)
const Range *range = entry->getUseLimit().getFirstRange();
usepoint = Address(range->getSpace(), range->getFirst());
}
- nameRecommend.emplace_back(entry->getAddr(),usepoint, entry->getSize(), sym->getName(), sym->getId());
+ nameRecommend.emplace_back(((MapEntry *)entry)->getAddr(),usepoint, entry->getSize(), sym->getName(), sym->getId());
}
if (sym->getCategory() < 0)
removeSymbol(sym);
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.cc
index f444a6632b..51a0751b5d 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.cc
@@ -397,6 +397,11 @@ void Varnode::clearFlags(uint4 fl) const
void Varnode::clearSymbolLinks(void)
{
+ if (high == (HighVariable *)0) {
+ mapentry = (SymbolEntry *)0;
+ clearFlags(Varnode::namelock | Varnode::typelock | Varnode::mapped);
+ return;
+ }
bool foundEntry = false;
for(int4 i=0;i<high->numInstances();++i) {
Varnode *vn = high->getInstance(i);
diff --git a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/DecompilerHighSymbolTest.java b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/DecompilerHighSymbolTest.java
index 21a0bd474e..05c845f91c 100644
--- a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/DecompilerHighSymbolTest.java
+++ b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/DecompilerHighSymbolTest.java
@@ -427,4 +427,32 @@ public class DecompilerHighSymbolTest extends AbstractDecompilerTest {
assertEquals(eqSymbol.getConvert(), 0);
assertEquals(eqSymbol.getValue(), eqValue);
}
+
+ @Test
+ public void testHighSymbol_resolveStorage() {
+ decompile("1002825");
+ ClangTextField line = getLineContaining("DAT_0100579c");
+ FieldLocation loc = loc(line.getLineNumber(), 3);
+ ClangToken token = line.getToken(loc);
+ HighVariable variable = token.getHighVariable();
+ assertTrue(variable instanceof HighLocal);
+ HighSymbol highSymbol = variable.getSymbol();
+ SymbolEntry entry = highSymbol.getFirstWholeMap();
+ if (entry instanceof DynamicEntry) {
+ assertTrue(((DynamicEntry) entry).getHash() == 0); // Initially hash is not calculuated
+ }
+ else {
+ assertTrue(entry instanceof MappedEntry);
+ VariableStorage storage = ((MappedEntry) entry).getStorage();
+ assertTrue(storage.getMinAddress().isUniqueAddress());
+ }
+ renameVariable(highSymbol, token, "newDynamic");
+ line = getLineContaining("DAT_0100579c");
+ loc = loc(line.getLineNumber(), 3);
+ token = line.getToken(loc);
+ highSymbol = token.getHighVariable().getSymbol();
+ entry = highSymbol.getFirstWholeMap();
+ assertTrue(entry instanceof DynamicEntry);
+ assertTrue(((DynamicEntry) entry).getHash() != 0); // With rename hash is calculated
+ }
}
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunctionDBUtil.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunctionDBUtil.java
index ca5dcf1fed..6657ceddce 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunctionDBUtil.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunctionDBUtil.java
@@ -535,16 +535,11 @@ public class HighFunctionDBUtil {
return null;
}
+ highSymbol.resolveStorage();
VariableStorage storage = highSymbol.getStorage();
Address pcAddr = highSymbol.getPCAddress();
Variable localVariable = getLocalVariable(function, storage, pcAddr);
- if (!storage.isHashStorage() && highVar != null && highVar.requiresDynamicStorage()) {
- DynamicEntry entry = DynamicEntry.build(highVar.getRepresentative());
- storage = entry.getStorage();
- pcAddr = entry.getPCAdress(); // The address may change from original Varnode
- }
-
if (localVariable != null) {
return localVariable;
}
@@ -605,24 +600,17 @@ public class HighFunctionDBUtil {
}
else if (!highSymbol.isGlobal()) {
Variable[] varList = null;
+ highSymbol.resolveStorage();
VariableStorage storage = highSymbol.getStorage();
Address pcAddr = highSymbol.getPCAddress();
- HighVariable tmpHigh = highSymbol.getHighVariable();
- if (!storage.isHashStorage() && tmpHigh != null && tmpHigh.requiresDynamicStorage()) {
- DynamicEntry entry = DynamicEntry.build(tmpHigh.getRepresentative());
- storage = entry.getStorage();
- pcAddr = entry.getPCAdress(); // The address may change from original Varnode
- }
- else {
- Variable var = clearConflictingLocalVariables(function, storage, pcAddr);
- if (var != null) {
- if (!resized) {
- varList = gatherMergeSet(function, var); // Cannot resize a whole multi-merge
- }
- else {
- varList = new Variable[1];
- varList[0] = var;
- }
+ Variable conflictVar = clearConflictingLocalVariables(function, storage, pcAddr);
+ if (conflictVar != null) {
+ if (!resized) {
+ varList = gatherMergeSet(function, conflictVar); // Cannot resize a whole multi-merge
+ }
+ else {
+ varList = new Variable[1];
+ varList[0] = conflictVar;
}
}
boolean usesHashStorage = storage.isHashStorage();
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighSymbol.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighSymbol.java
index 8bd432d808..c235d8113c 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighSymbol.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighSymbol.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.
@@ -377,6 +377,29 @@ public class HighSymbol {
return entryList[0].getStorage();
}
+ /**
+ * Prepare the symbol for generating VariableStorage. Calculate dynamic storage if necessary.
+ */
+ public void resolveStorage() {
+ if (entryList != null && entryList[0] instanceof DynamicEntry) {
+ if (((DynamicEntry) entryList[0]).getHash() != 0) {
+ return;
+ }
+ }
+ else if (highVariable == null) {
+ return;
+ }
+ else {
+ if (!highVariable.requiresDynamicStorage()) {
+ return;
+ }
+ }
+ if (entryList.length != 1) {
+ entryList = new SymbolEntry[1];
+ }
+ entryList[0] = DynamicEntry.build(highVariable.getRepresentative());
+ }
+
/**
* Encode attributes for the base symbol element
* @param encoder is the stream encoder