decompiler
1.0.0
|
An implementation of the Scope interface by querying a Ghidra client for Symbol information. More...
#include <database_ghidra.hh>
Public Member Functions | |
ScopeGhidra (ArchitectureGhidra *g) | |
Constructor. More... | |
void | lockDefaultProperties (void) |
Lock in the default state of the boolean property map. More... | |
virtual void | clear (void) |
Clear all symbols from this scope. | |
virtual SymbolEntry * | addSymbol (const string &name, Datatype *ct, const Address &addr, const Address &usepoint) |
Add a new Symbol to this Scope, given a name, data-type, and a single mapping. More... | |
virtual string | buildVariableName (const Address &addr, const Address &pc, Datatype *ct, int4 &index, uint4 flags) const |
Given an address and data-type, build a suitable generic symbol name. More... | |
virtual string | buildUndefinedName (void) const |
Build a formal undefined name, used internally when a Symbol is not given a name. More... | |
virtual void | setAttribute (Symbol *sym, uint4 attr) |
Set boolean Varnode properties on a Symbol. | |
virtual void | clearAttribute (Symbol *sym, uint4 attr) |
Clear boolean Varnode properties on a Symbol. | |
virtual void | setDisplayFormat (Symbol *sym, uint4 attr) |
Set the display format for a Symbol. | |
virtual SymbolEntry * | findAddr (const Address &addr, const Address &usepoint) const |
Find a Symbol at a given address and usepoint. More... | |
virtual SymbolEntry * | findContainer (const Address &addr, int4 size, const Address &usepoint) const |
Find the smallest Symbol containing the given memory range. More... | |
virtual SymbolEntry * | findClosestFit (const Address &addr, int4 size, const Address &usepoint) const |
Find Symbol which is the closest fit to the given memory range. More... | |
virtual Funcdata * | findFunction (const Address &addr) const |
Find the function starting at the given address. More... | |
virtual ExternRefSymbol * | findExternalRef (const Address &addr) const |
Find an external reference at the given address. More... | |
virtual LabSymbol * | findCodeLabel (const Address &addr) const |
Find a label Symbol at the given address. More... | |
virtual Funcdata * | resolveExternalRefFunction (ExternRefSymbol *sym) const |
Convert an external reference to the referenced function. More... | |
virtual SymbolEntry * | findOverlap (const Address &addr, int4 size) const |
Find first Symbol overlapping the given memory range. More... | |
virtual SymbolEntry * | findBefore (const Address &addr) const |
Find first Symbol before (but not containing) a given address. More... | |
virtual SymbolEntry * | findAfter (const Address &addr) const |
Find first Symbol after (but not containing) a given address. More... | |
virtual void | findByName (const string &name, vector< Symbol * > &res) const |
Find a Symbol by name within this Scope. More... | |
virtual MapIterator | begin (void) const |
Beginning iterator to mapped SymbolEntrys. | |
virtual MapIterator | end (void) const |
Ending iterator to mapped SymbolEntrys. | |
virtual list< SymbolEntry >::const_iterator | beginDynamic (void) const |
Beginning iterator to dynamic SymbolEntrys. | |
virtual list< SymbolEntry >::const_iterator | endDynamic (void) const |
Ending iterator to dynamic SymbolEntrys. | |
virtual list< SymbolEntry >::iterator | beginDynamic (void) |
Beginning iterator to dynamic SymbolEntrys. | |
virtual list< SymbolEntry >::iterator | endDynamic (void) |
Ending iterator to dynamic SymbolEntrys. | |
virtual void | clearCategory (int4 cat) |
Clear all symbols of the given category from this scope. | |
virtual void | clearUnlockedCategory (int4 cat) |
Clear unlocked symbols of the given category from this scope. | |
virtual void | clearUnlocked (void) |
Clear all unlocked symbols from this scope. | |
virtual void | restrictScope (Funcdata *f) |
Convert this to a local Scope. More... | |
virtual void | removeSymbol (Symbol *symbol) |
Remove the given Symbol from this Scope. | |
virtual void | renameSymbol (Symbol *sym, const string &newname) |
Rename a Symbol within this Scope. | |
virtual void | retypeSymbol (Symbol *sym, Datatype *ct) |
Change the data-type of a Symbol within this Scope. More... | |
virtual string | makeNameUnique (const string &nm) const |
Produce a version of the given symbol name that won't collide with other names in this Scope. More... | |
virtual void | saveXml (ostream &s) const |
Write out this as a <scope> XML tag. | |
virtual void | restoreXml (const Element *el) |
Restore this Scope from a <scope> XML tag. | |
virtual void | printEntries (ostream &s) const |
Dump a description of all SymbolEntry objects to a stream. | |
virtual int4 | getCategorySize (int4 cat) const |
Get the number of Symbols in the given category. More... | |
virtual Symbol * | getCategorySymbol (int4 cat, int4 ind) const |
Retrieve a Symbol by index within a specific category. More... | |
virtual void | setCategory (Symbol *sym, int4 cat, int4 ind) |
Set the category and index for the given Symbol. More... | |
Public Member Functions inherited from Scope | |
Scope (const string &nm, Architecture *g) | |
Construct an empty scope, given a name and Architecture. | |
virtual | ~Scope (void) |
Destructor. | |
virtual bool | inScope (const Address &addr, int4 size, const Address &usepoint) const |
Query if the given range is owned by this Scope. More... | |
const string & | getName (void) const |
Get the name of the Scope. | |
bool | isGlobal (void) const |
Return true if this scope is global. | |
void | queryByName (const string &name, vector< Symbol * > &res) const |
Look-up symbols by name. More... | |
Funcdata * | queryFunction (const string &name) const |
Look-up a function by name. More... | |
SymbolEntry * | queryByAddr (const Address &addr, const Address &usepoint) const |
Get Symbol with matching address. More... | |
SymbolEntry * | queryContainer (const Address &addr, int4 size, const Address &usepoint) const |
Find the smallest containing Symbol. More... | |
SymbolEntry * | queryProperties (const Address &addr, int4 size, const Address &usepoint, uint4 &flags) const |
Find a Symbol or properties at the given address. More... | |
Funcdata * | queryFunction (const Address &addr) const |
Look-up a function by address. More... | |
Funcdata * | queryExternalRefFunction (const Address &addr) const |
Look-up a function thru an external reference. More... | |
LabSymbol * | queryCodeLabel (const Address &addr) const |
Look-up a code label by address. More... | |
Scope * | resolveScope (const string &name) const |
Find a child Scope of this. More... | |
Scope * | discoverScope (const Address &addr, int4 sz, const Address &usepoint) |
Find the owning Scope of a given memory range. More... | |
ScopeMap::const_iterator | childrenBegin () const |
Beginning iterator of child scopes. | |
ScopeMap::const_iterator | childrenEnd () const |
Ending iterator of child scopes. | |
void | saveXmlRecursive (ostream &s, bool onlyGlobal) const |
Save all contained scopes as an XML stream. More... | |
void | overrideSizeLockType (Symbol *sym, Datatype *ct) |
Change the data-type of a Symbol that is sizelocked. More... | |
void | resetSizeLockType (Symbol *sym) |
Clear a Symbol's size-locked data-type. More... | |
bool | isSubScope (const Scope *scp) const |
Is this a sub-scope of the given Scope. More... | |
string | getFullName (void) const |
Get the full name of this Scope. | |
void | getNameSegments (vector< string > &vec) const |
Get the fullname of this in segments. More... | |
Architecture * | getArch (void) const |
Get the Architecture associated with this. | |
Scope * | getParent (void) const |
Get the parent Scope (or NULL if this is the global Scope) | |
Symbol * | addSymbol (const string &name, Datatype *ct) |
Add a new Symbol without mapping it to an address. More... | |
SymbolEntry * | addMapPoint (Symbol *sym, const Address &addr, const Address &usepoint) |
Map a Symbol to a specific address. More... | |
Symbol * | addMapSym (const Element *el) |
Add a mapped Symbol from a <mapsym> XML tag. More... | |
FunctionSymbol * | addFunction (const Address &addr, const string &nm) |
Create a function Symbol at the given address in this Scope. More... | |
ExternRefSymbol * | addExternalRef (const Address &addr, const Address &refaddr, const string &nm) |
LabSymbol * | addCodeLabel (const Address &addr, const string &nm) |
Create a code label at the given address in this Scope. More... | |
Symbol * | addDynamicSymbol (const string &nm, Datatype *ct, const Address &caddr, uint8 hash) |
Create a dynamically mapped Symbol attached to a specific data-flow. More... | |
bool | isReadOnly (const Address &addr, int4 size, const Address &usepoint) const |
Is the given memory range marked as read-only. More... | |
void | printBounds (ostream &s) const |
Print a description of this Scope's owned memory ranges. | |
Private Member Functions | |
Symbol * | dump2Cache (Document *doc) const |
Parse a response into the cache. More... | |
Symbol * | removeQuery (const Address &addr) const |
Process a query that missed the cache. More... | |
void | processHole (const Element *el) const |
Process a response describing a hole. More... | |
Scope * | createNewScope (const string &nm, Scope *par) const |
Create a global namespace Scope. More... | |
Scope * | reresolveScope (const vector< string > &path) const |
Find the Scope that will contain a result Symbol. More... | |
virtual void | addRange (AddrSpace *spc, uintb first, uintb last) |
Add a memory range to the ownership of this Scope. More... | |
virtual void | removeRange (AddrSpace *spc, uintb first, uintb last) |
Remove a memory range from the ownership of this Scope. More... | |
virtual void | addSymbolInternal (Symbol *sym) |
Put a Symbol into the name map. More... | |
virtual SymbolEntry * | addMapInternal (Symbol *sym, uint4 exfl, const Address &addr, int4 off, int4 sz, const RangeList &uselim) |
Create a new SymbolEntry for a Symbol given a memory range. More... | |
virtual SymbolEntry * | addDynamicMapInternal (Symbol *sym, uint4 exfl, uint8 hash, int4 off, int4 sz, const RangeList &uselim) |
Create a new SymbolEntry for a Symbol given a dynamic hash. More... | |
Private Attributes | |
ArchitectureGhidra * | ghidra |
Architecture and connection to the Ghidra client. | |
ScopeInternal * | cache |
An internal cache of previously fetched Symbol objects. | |
RangeList | holes |
List of (queried) memory ranges with no Symbol in them. | |
vector< int4 > | spacerange |
List of address spaces that are in the global range. | |
partmap< Address, uint4 > | flagbaseDefault |
Default boolean properties on memory. | |
bool | cacheDirty |
Is flagbaseDefault different from cache. | |
Additional Inherited Members | |
Protected Member Functions inherited from Scope | |
const RangeList & | getRangeTree (void) const |
Access the address ranges owned by this Scope. | |
SymbolEntry * | addMap (const SymbolEntry &entry) |
Integrate a SymbolEntry into the range maps. More... | |
Static Protected Member Functions inherited from Scope | |
static const Scope * | stackAddr (const Scope *scope1, const Scope *scope2, const Address &addr, const Address &usepoint, SymbolEntry **addrmatch) |
Query for Symbols starting at a given address, which match a given usepoint. More... | |
static const Scope * | stackContainer (const Scope *scope1, const Scope *scope2, const Address &addr, int4 size, const Address &usepoint, SymbolEntry **addrmatch) |
static const Scope * | stackClosestFit (const Scope *scope1, const Scope *scope2, const Address &addr, int4 size, const Address &usepoint, SymbolEntry **addrmatch) |
static const Scope * | stackFunction (const Scope *scope1, const Scope *scope2, const Address &addr, Funcdata **addrmatch) |
static const Scope * | stackExternalRef (const Scope *scope1, const Scope *scope2, const Address &addr, ExternRefSymbol **addrmatch) |
static const Scope * | stackCodeLabel (const Scope *scope1, const Scope *scope2, const Address &addr, LabSymbol **addrmatch) |
Protected Attributes inherited from Scope | |
Architecture * | glb |
Architecture of this scope. | |
string | name |
Name of this scope. | |
Funcdata * | fd |
(If non-null) the function which this is the local Scope for | |
uint4 | dedupId |
Id to dedup scopes with same name (when allowed) | |
An implementation of the Scope interface by querying a Ghidra client for Symbol information.
This object is generally instantiated once for an executable and acts as the global scope for the decompiler. Queries for symbol information are forwarded to the Ghidra client and the response is cached. This object fields queries for all scopes above functions. Responses may be for Symbol objects that are not global but belong to sub-scopes, like namespace and function Scopes. This object will build any new Scope or Funcdata, object as necessary and stick the Symbol in, returning as if the new Scope had caught the query in the first place.
ScopeGhidra::ScopeGhidra | ( | ArchitectureGhidra * | g | ) |
Constructor.
g | is the Architecture and connection to the Ghidra client |
References cache, cacheDirty, and ghidra.
|
inlineprivatevirtual |
Create a new SymbolEntry for a Symbol given a dynamic hash.
The SymbolEntry is specified in terms of a hash and usepoint, which describe how to find the temporary Varnode holding the symbol value.
sym | is the given Symbol being mapped |
exfl | are any boolean Varnode properties |
hash | is the given dynamic hash |
off | is the byte offset of the new SymbolEntry (relative to the whole Symbol) |
sz | is the number of bytes occupied by the Varnode |
uselim | is the given usepoint |
Implements Scope.
|
inlineprivatevirtual |
Create a new SymbolEntry for a Symbol given a memory range.
The SymbolEntry is specified in terms of a memory range and usepoint
sym | is the given Symbol being mapped |
exfl | are any boolean Varnode properties specific to the memory range |
addr | is the starting address of the given memory range |
off | is the byte offset of the new SymbolEntry (relative to the whole Symbol) |
sz | is the number of bytes in the range |
uselim | is the given usepoint (which may be invalid) |
Implements Scope.
|
privatevirtual |
Add a memory range to the ownership of this Scope.
spc | is the address space of the range |
first | is the offset of the first byte in the range |
last | is the offset of the last byte in the range |
Reimplemented from Scope.
References Scope::addRange(), AddrSpace::getIndex(), and spacerange.
|
virtual |
Add a new Symbol to this Scope, given a name, data-type, and a single mapping.
The Symbol object will be created with the given name and data-type. A single mapping (SymbolEntry) will be created for the Symbol based on a given storage address for the symbol and an address for code that accesses the Symbol at that storage location.
name | is the new name of the Symbol |
ct | is the data-type of the new Symbol |
addr | is the starting address of the Symbol storage |
usepoint | is the point accessing that storage (may be invalid) |
Reimplemented from Scope.
References Scope::addSymbol(), cache, and Scope::name.
|
inlineprivatevirtual |
|
inlinevirtual |
Build a formal undefined name, used internally when a Symbol is not given a name.
Implements Scope.
References ScopeInternal::buildUndefinedName(), and cache.
|
inlinevirtual |
Given an address and data-type, build a suitable generic symbol name.
addr | is the given address |
pc | is the address at which the name is getting used |
ct | is a data-type used to inform the name |
index | is a reference to an index used to make the name unique, which will be updated |
flags | are boolean properties of the variable we need the name for |
Implements Scope.
References ScopeInternal::buildVariableName(), and cache.
Create a global namespace Scope.
Ghidra may report that a Symbol is in a namespace. This method creates a dedicated Scope object to hold such a Symbol. The immediate parent for the new Scope must already exist.
nm | is the name of the new namespace |
par | is the parent Scope |
References Database::attachScope(), ghidra, and Architecture::symboltab.
Referenced by reresolveScope().
Parse a response into the cache.
Build the global object described by the XML document and put it in the cache. The XML can either be a <hole> tag, describing the absence of symbols at the queried address, or one of the symbol tags
doc | is the XML document |
References Scope::addMapSym(), cache, cacheDirty, LowlevelError::explain, SymbolEntry::getAddr(), Funcdata::getAddress(), SymbolEntry::getFirst(), Symbol::getFirstWholeMap(), Symbol::getFlags(), FunctionSymbol::getFunction(), SymbolEntry::getLast(), Address::getOffset(), SymbolEntry::getSize(), Address::getSpace(), ghidra, Scope::glb, holes, RangeList::insertRange(), processHole(), Scope::queryByName(), Varnode::readonly, reresolveScope(), Address::restoreXml(), Database::setPropertyRange(), Architecture::symboltab, and Varnode::volatil.
Referenced by removeQuery(), and resolveExternalRefFunction().
|
virtual |
Find a Symbol at a given address and usepoint.
addr | is the given address |
usepoint | is the point at which the Symbol is accessed (may be invalid) |
Implements Scope.
References cache, ScopeInternal::findAddr(), ScopeInternal::findContainer(), SymbolEntry::getAddr(), Symbol::getMapEntry(), and removeQuery().
|
inlinevirtual |
Find first Symbol after (but not containing) a given address.
addr | is the given address |
Implements Scope.
|
inlinevirtual |
Find first Symbol before (but not containing) a given address.
addr | is the given address |
Implements Scope.
|
inlinevirtual |
|
inlinevirtual |
Find Symbol which is the closest fit to the given memory range.
addr | is the starting address of the given memory range |
size | is the number of bytes in the range |
usepoint | is the point at which the Symbol is accessed (may be invalid) |
Implements Scope.
Find a label Symbol at the given address.
addr | is the given address |
Implements Scope.
References Scope::addCodeLabel(), cache, ScopeInternal::findAddr(), ScopeInternal::findCodeLabel(), ArchitectureGhidra::getCodeLabel(), and ghidra.
|
virtual |
Find the smallest Symbol containing the given memory range.
addr | is the starting address of the given memory range |
size | is the number of bytes in the range |
usepoint | is the point at which the Symbol is accessed (may be invalid) |
Implements Scope.
References cache, ScopeInternal::findClosestFit(), SymbolEntry::getAddr(), Symbol::getMapEntry(), Address::getOffset(), SymbolEntry::getSize(), and removeQuery().
|
virtual |
Find an external reference at the given address.
addr | is the given address |
Implements Scope.
References cache, ScopeInternal::findContainer(), ScopeInternal::findExternalRef(), and removeQuery().
Find the function starting at the given address.
addr | is the given starting address |
Implements Scope.
References cache, Scope::fd, ScopeInternal::findContainer(), ScopeInternal::findFunction(), FunctionSymbol::getFunction(), and removeQuery().
|
inlinevirtual |
Find first Symbol overlapping the given memory range.
addr | is the starting address of the given range |
size | is the number of bytes in the range |
Implements Scope.
|
inlinevirtual |
|
inlinevirtual |
|
inline |
Lock in the default state of the boolean property map.
When this Scope gets created, parsing of .pspec and .cspec files may lay down property information about memory before any the load-image is consulted. This method locks creates a copy of this state of memory, so the decompiler can reset to it before decompiling a new function.
References cacheDirty, flagbaseDefault, Database::getProperties(), ghidra, and Architecture::symboltab.
Referenced by ArchitectureGhidra::postSpecFile().
|
inlinevirtual |
|
private |
Process a response describing a hole.
The Ghidra client can respond to a query negatively by sending a <hole> tag, which describes the (largest) range of addresses containing the query address that do not have any Symbol mapped to them. This object stores this information in the holes map, which it consults to avoid sending queries for the same unmapped address repeatedly. The tag may also contain boolean property information about the memory range, which also gets stored.
el | is the <hole> element |
References cacheDirty, Range::getFirst(), Range::getLast(), Range::getSpace(), ghidra, holes, RangeList::insertRange(), Varnode::readonly, Range::restoreXml(), Database::setPropertyRange(), Architecture::symboltab, and Varnode::volatil.
Referenced by dump2Cache().
Process a query that missed the cache.
Determine if the given address should be sent to the Ghidra client at all, by checking the hole map and other factors. If it passes, send the query to the client, process the result, and update the cache. If a Symbol is ultimately recovered, return it.
addr | is the address to potentially query |
References dump2Cache(), AddrSpace::getIndex(), ArchitectureGhidra::getMappedSymbolsXML(), Address::getSpace(), ghidra, holes, RangeList::inRange(), and spacerange.
Referenced by findAddr(), findContainer(), findExternalRef(), and findFunction().
|
inlineprivatevirtual |
|
private |
Find the Scope that will contain a result Symbol.
The Ghidra client reports a namespace path associated with Symbol. Determine if this Scope already exists in the cache and built it if it isn't. This may mean creating a new namespace Scope.
path | is absolute path to the desired Scope |
References cache, createNewScope(), Database::getGlobalScope(), Scope::glb, Scope::resolveScope(), and Architecture::symboltab.
Referenced by dump2Cache().
|
virtual |
Convert an external reference to the referenced function.
sym | is the Symbol marking the external reference |
Implements Scope.
References cache, dump2Cache(), Scope::fd, ScopeInternal::findFunction(), SymbolEntry::getAddr(), ArchitectureGhidra::getExternalRefXML(), Symbol::getFirstWholeMap(), FunctionSymbol::getFunction(), ExternRefSymbol::getRefAddr(), ghidra, Database::mapScope(), Scope::stackFunction(), and Architecture::symboltab.
|
inlinevirtual |
|
inlinevirtual |