decompiler
1.0.0
|
A manager for symbol scopes for a whole executable. More...
#include <database.hh>
Public Member Functions | |
Database (Architecture *g, bool idByName) | |
Constructor. More... | |
~Database (void) | |
Destructor. | |
Architecture * | getArch (void) const |
Get the Architecture associate with this. | |
void | adjustCaches (void) |
Let scopes adjust after configuration is finished. More... | |
void | attachScope (Scope *newscope, Scope *parent) |
Register a new Scope. More... | |
void | deleteScope (Scope *scope) |
Delete the given Scope and all its sub-scopes. More... | |
void | deleteSubScopes (Scope *scope) |
Delete all sub-scopes of the given Scope. More... | |
void | clearUnlocked (Scope *scope) |
Clear unlocked Symbols owned by the given Scope. More... | |
void | setRange (Scope *scope, const RangeList &rlist) |
Set the ownership range for a Scope. More... | |
void | addRange (Scope *scope, AddrSpace *spc, uintb first, uintb last) |
Add an address range to the ownership of a Scope. More... | |
void | removeRange (Scope *scope, AddrSpace *spc, uintb first, uintb last) |
Remove an address range from ownership of a Scope. More... | |
Scope * | getGlobalScope (void) const |
Get the global Scope. | |
Scope * | resolveScope (uint8 id) const |
Look-up a Scope by id. More... | |
Scope * | resolveScopeFromSymbolName (const string &fullname, const string &delim, string &basename, Scope *start) const |
Get the Scope (and base name) associated with a qualified Symbol name. More... | |
Scope * | findCreateScope (uint8, const string &nm, Scope *parent) |
Scope * | findCreateScopeFromSymbolName (const string &fullname, const string &delim, string &basename, Scope *start) |
Find (and if not found create) a specific subscope. More... | |
const Scope * | mapScope (const Scope *qpoint, const Address &addr, const Address &usepoint) const |
Determine the lowest-level Scope which might contain the given address as a Symbol. More... | |
Scope * | mapScope (Scope *qpoint, const Address &addr, const Address &usepoint) |
A non-constant version of mapScope() More... | |
uint4 | getProperty (const Address &addr) const |
Get boolean properties at the given address. | |
void | setPropertyRange (uint4 flags, const Range &range) |
Set boolean properties over a given memory range. More... | |
void | clearPropertyRange (uint4 flags, const Range &range) |
Clear boolean properties over a given memory range. More... | |
void | setProperties (const partmap< Address, uint4 > &newflags) |
Replace the property map. | |
const partmap< Address, uint4 > & | getProperties (void) const |
Get the entire property map. | |
void | encode (Encoder &encoder) const |
Encode the whole Database to a stream. More... | |
void | decode (Decoder &decoder) |
Decode the whole database from a stream. More... | |
void | decodeScope (Decoder &decoder, Scope *newScope) |
Register and fill out a single Scope from an XML <scope> tag. More... | |
Scope * | decodeScopePath (Decoder &decoder) |
Decode a namespace path and make sure each namespace exists. More... | |
Private Member Functions | |
void | clearResolve (Scope *scope) |
Clear the ownership ranges associated with the given Scope. More... | |
void | clearReferences (Scope *scope) |
Clear any map references to the given Scope and its children. More... | |
void | fillResolve (Scope *scope) |
Add the ownership ranges of the given Scope to the map. More... | |
Scope * | parseParentTag (Decoder &decoder) |
Figure out parent scope given <parent> tag. More... | |
Private Attributes | |
Architecture * | glb |
Architecture to which this symbol table is attached. | |
Scope * | globalscope |
Quick reference to the global Scope. | |
ScopeResolve | resolvemap |
Address to namespace map. | |
ScopeMap | idmap |
Map from id to Scope. | |
partmap< Address, uint4 > | flagbase |
Map of global properties. | |
bool | idByNameHash |
True if scope ids are built from hash of name. | |
A manager for symbol scopes for a whole executable.
This is the highest level container for anything related to Scope and Symbol objects, it indirectly holds the Funcdata objects as well, through the FunctionSymbol. It acts as the formal symbol table for the decompiler. The API is mostly concerned with the management of Scope objects.
A Scope object is initially registered via attachScope(), then it can looked up by name. This class maintains the cross Scope search by address capability, implemented as a map from an Address to the Scope that owns it. For efficiency, this map is really only applied to namespace Scopes, the global Scope and function Scopes are not entered in the map. This class also maintains a set of boolean properties that label memory ranges. This allows important properties like read-only and volatile to be put down even if the Symbols aren't yet known.
ghidra::Database::Database | ( | Architecture * | g, |
bool | idByName | ||
) |
Constructor.
Initialize a new symbol table, with no initial scopes or symbols.
g | is the Architecture that owns the symbol table |
idByName | is true if scope ids are calculated as a hash of the scope name. |
References ghidra::Scope::glb.
Add an address range to the ownership of a Scope.
The new range will be merged with the existing ownership. The address to Scope map is updated
scope | is the given Scope |
spc | is the address space of the memory range being added |
first | is the offset of the first byte in the array |
last | is the offset of the last byte |
References ghidra::Scope::addRange().
Referenced by ghidra::ScopeGhidraNamespace::addMapInternal(), and ghidra::ScopeInternal::decode().
void ghidra::Database::adjustCaches | ( | void | ) |
Let scopes adjust after configuration is finished.
Give this database the chance to inform existing scopes of any change to the configuration, which may have changed since the initial scopes were created.
Register a new Scope.
The new Scope must be initially empty and this Database takes over ownership. Practically, this is just setting up the new Scope as a sub-scope of its parent. The parent Scope should already be registered with this Database, or NULL can be passed to register the global Scope.
References ghidra::Scope::attachScope(), ghidra::Scope::getFullName(), ghidra::Scope::name, and ghidra::Scope::uniqueId.
Referenced by ghidra::ArchitectureGhidra::buildDatabase(), ghidra::Funcdata::decode(), and ghidra::Funcdata::Funcdata().
void ghidra::Database::clearPropertyRange | ( | uint4 | flags, |
const Range & | range | ||
) |
Clear boolean properties over a given memory range.
The non-zero bits in the flags parameter indicate the boolean properties to be cleared. No other properties are altered.
flags | is the set of properties to clear |
range | is the memory range to clear |
References ghidra::partmap< _linetype, _valuetype >::begin(), ghidra::partmap< _linetype, _valuetype >::end(), ghidra::Range::getFirstAddr(), ghidra::Range::getLastAddrOpen(), ghidra::Scope::glb, and ghidra::Address::isInvalid().
Referenced by ghidra::ArchitectureGhidra::buildSymbols().
|
private |
Clear any map references to the given Scope and its children.
This recursively clears references in idmap or in resolvemap.
scope | is the given Scope to clear |
References ghidra::Scope::children, and ghidra::Scope::uniqueId.
|
private |
Clear the ownership ranges associated with the given Scope.
Check to make sure the Scope is a namespace then remove all its address ranges from the map.
scope | is the given Scope |
References ghidra::RangeList::begin(), ghidra::RangeList::end(), ghidra::Scope::fd, ghidra::Range::getFirstAddr(), and ghidra::Scope::rangetree.
void ghidra::Database::clearUnlocked | ( | Scope * | scope | ) |
Clear unlocked Symbols owned by the given Scope.
All unlocked symbols in this Scope, and recursively into its sub-scopes, are removed.
scope | is the given Scope |
References ghidra::Scope::children, ghidra::Scope::clearUnlocked(), and ghidra::ScopeInternal::clearUnlocked().
void ghidra::Database::decode | ( | Decoder & | decoder | ) |
Decode the whole database from a stream.
Parse a <db> element to recover Scope and Symbol objects.
decoder | is the stream decoder |
References ghidra::Decoder::closeElement(), ghidra::Scope::decode(), ghidra::VarnodeData::decodeFromAttributes(), ghidra::Scope::displayName, ghidra::VarnodeData::getAddr(), ghidra::Decoder::getNextAttributeId(), ghidra::Scope::name, ghidra::Decoder::openElement(), ghidra::Decoder::peekElement(), ghidra::Decoder::readBool(), ghidra::Decoder::readString(), ghidra::Decoder::readUnsignedInteger(), and ghidra::Scope::setDisplayName().
Register and fill out a single Scope from an XML <scope> tag.
This allows incremental building of the Database from multiple stream sources. An empty Scope must already be allocated. It is registered with this Database, and then populated with Symbol objects based as the content of a given element. The element can either be a <scope> itself, or another element that wraps a <scope> element as its first child.
decoder | is the stream decoder |
newScope | is the empty Scope |
References ghidra::Scope::attachScope(), ghidra::Decoder::closeElement(), ghidra::Scope::decode(), ghidra::Scope::decodeWrappingAttributes(), and ghidra::Decoder::openElement().
Referenced by ghidra::Funcdata::decode().
Decode a namespace path and make sure each namespace exists.
Some namespace objects may already exist. Create those that don't.
decoder | is the stream to decode the path from |
References ghidra::Decoder::closeElement(), ghidra::Decoder::closeElementSkipping(), ghidra::Scope::displayName, ghidra::Decoder::getNextAttributeId(), ghidra::Scope::name, ghidra::Decoder::openElement(), ghidra::Decoder::readString(), ghidra::Decoder::readUnsignedInteger(), and ghidra::Scope::setDisplayName().
Referenced by ghidra::ScopeGhidra::reresolveScope().
void ghidra::Database::deleteScope | ( | Scope * | scope | ) |
Delete the given Scope and all its sub-scopes.
scope | is the given Scope |
References ghidra::Scope::children, ghidra::Scope::detachScope(), ghidra::Scope::name, ghidra::Scope::parent, and ghidra::Scope::uniqueId.
Referenced by ghidra::Funcdata::~Funcdata().
void ghidra::Database::deleteSubScopes | ( | Scope * | scope | ) |
Delete all sub-scopes of the given Scope.
The given Scope is not deleted, only its children.
scope | is the given Scope |
References ghidra::Scope::children, and ghidra::Scope::detachScope().
void ghidra::Database::encode | ( | Encoder & | encoder | ) | const |
Encode the whole Database to a stream.
Encode a single <db> element to the stream, which contains child elements for each Scope (which contain Symbol children in turn).
encoder | is the stream encoder |
References ghidra::partmap< _linetype, _valuetype >::begin(), ghidra::Encoder::closeElement(), ghidra::AddrSpace::encodeAttributes(), ghidra::partmap< _linetype, _valuetype >::end(), ghidra::Address::getOffset(), ghidra::Address::getSpace(), ghidra::Encoder::openElement(), ghidra::Encoder::writeBool(), and ghidra::Encoder::writeUnsignedInteger().
|
private |
Add the ownership ranges of the given Scope to the map.
If the Scope is a namespace, iterate through all its ranges, adding each to the map
scope | is the given Scope to add |
References ghidra::RangeList::begin(), ghidra::RangeList::end(), ghidra::Scope::fd, ghidra::Range::getFirstAddr(), ghidra::Range::getLastAddr(), and ghidra::Scope::rangetree.
Look for a Scope by id. If it does not exist, create a new scope with the given name and parent scope.
id | is the global id of the Scope |
nm | is the given name of the Scope |
parent | is the given parent scope to search |
References ghidra::Scope::attachScope(), ghidra::Scope::buildSubScope(), and ghidra::Scope::resolveScope().
Scope * ghidra::Database::findCreateScopeFromSymbolName | ( | const string & | fullname, |
const string & | delim, | ||
string & | basename, | ||
Scope * | start | ||
) |
Find (and if not found create) a specific subscope.
Find and/or create Scopes associated with a qualified Symbol name.
The name is parsed using a delimiter that is passed in. The name can be only partially qualified by passing in a starting Scope, which the name is assumed to be relative to. Otherwise the name is assumed to be relative to the global Scope. The unqualified (base) name of the Symbol is passed back to the caller. Any missing scope in the path is created.
fullname | is the qualified Symbol name |
delim | is the delimiter separating names |
basename | will hold the passed back base Symbol name |
start | is the Scope to start drilling down from, or NULL for the global scope |
References ghidra::Scope::hashScopeName(), and ghidra::Scope::uniqueId.
const Scope * ghidra::Database::mapScope | ( | const Scope * | qpoint, |
const Address & | addr, | ||
const Address & | usepoint | ||
) | const |
Determine the lowest-level Scope which might contain the given address as a Symbol.
As currently implemented, this method can only find a namespace Scope. When searching for a Symbol by Address, the global Scope is always searched because it is the terminating Scope when recursively walking scopes through the parent relationship, so it isn't entered in this map. A function level Scope, also not entered in the map, is only returned as the Scope passed in as a default, when no namespace Scope claims the address.
qpoint | is the default Scope returned if no owner is found |
addr | is the address whose owner should be searched for |
usepoint | is a point in code where the address is being accessed (may be invalid) |
Scope * ghidra::Database::mapScope | ( | Scope * | qpoint, |
const Address & | addr, | ||
const Address & | usepoint | ||
) |
A non-constant version of mapScope()
qpoint | is the default Scope returned if no owner is found |
addr | is the address whose owner should be searched for |
usepoint | is a point in code where the address is being accessed (may be invalid) |
Figure out parent scope given <parent> tag.
Parse a <parent> element for the scope id of the parent namespace. Look up the parent scope and return it. Throw an error if there is no matching scope
decoder | is the stream decoder |
References ghidra::Decoder::closeElement(), ghidra::Decoder::openElement(), ghidra::Decoder::readUnsignedInteger(), and ghidra::Scope::resolveScope().
Remove an address range from ownership of a Scope.
Addresses owned by the Scope that are disjoint from the given range are not affected.
scope | is the given Scope |
spc | is the address space of the memory range being removed |
first | is the offset of the first byte in the array |
last | is the offset of the last byte |
References ghidra::Scope::removeRange().
Referenced by ghidra::ScopeLocal::markNotMapped().
Scope * ghidra::Database::resolveScope | ( | uint8 | id | ) | const |
Look-up a Scope by id.
Find a Scope object, given its global id. Return null if id is not mapped to a Scope.
id | is the global id |
Referenced by ghidra::ScopeGhidra::reresolveScope().
Scope * ghidra::Database::resolveScopeFromSymbolName | ( | const string & | fullname, |
const string & | delim, | ||
string & | basename, | ||
Scope * | start | ||
) | const |
Get the Scope (and base name) associated with a qualified Symbol name.
The name is parsed using a delimiter that is passed in. The name can be only partially qualified by passing in a starting Scope, which the name is assumed to be relative to. If the starting scope is null, or the name starts with the delimiter, the name is assumed to be relative to the global Scope. The unqualified (base) name of the Symbol is passed back to the caller.
fullname | is the qualified Symbol name |
delim | is the delimiter separating names |
basename | will hold the passed back base Symbol name |
start | is the Scope to start drilling down from, or NULL for the global scope |
References ghidra::Scope::resolveScope().
void ghidra::Database::setPropertyRange | ( | uint4 | flags, |
const Range & | range | ||
) |
Set boolean properties over a given memory range.
This allows the standard boolean Varnode properties like read-only and volatile to be put an a memory range, independent of whether a Symbol is there or not. These get picked up by the Scope::queryProperties() method in particular.
flags | is the set of boolean properties |
range | is the memory range to label |
References ghidra::partmap< _linetype, _valuetype >::begin(), ghidra::partmap< _linetype, _valuetype >::end(), ghidra::Range::getFirstAddr(), ghidra::Range::getLastAddrOpen(), ghidra::Scope::glb, and ghidra::Address::isInvalid().
Referenced by ghidra::ArchitectureGhidra::buildSymbols(), and ghidra::ScopeInternal::decodeHole().
Set the ownership range for a Scope.
Any existing ownership is completely replaced. The address to Scope map is updated.
scope | is the given Scope |
rlist | is the set of addresses to mark as owned |
References ghidra::Scope::rangetree.
Referenced by ghidra::ScopeInternal::decode(), and ghidra::ScopeLocal::resetLocalWindow().