Ghidra Decompiler Analysis Engine
database_ghidra.hh
Go to the documentation of this file.
1 /* ###
2  * IP: GHIDRA
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
18 
19 #ifndef __DATABASE_GHIDRA__
20 #define __DATABASE_GHIDRA__
21 
22 #include "database.hh"
23 #include "ghidra_arch.hh"
24 
35 class ScopeGhidra : public Scope {
36  ArchitectureGhidra *ghidra;
37  mutable ScopeInternal *cache;
38  mutable RangeList holes;
39  vector<int4> spacerange;
40  partmap<Address,uint4> flagbaseDefault;
41  mutable bool cacheDirty;
42  Symbol *dump2Cache(Document *doc) const;
43  Symbol *removeQuery(const Address &addr) const;
44  void processHole(const Element *el) const;
45  Scope *reresolveScope(uint8 id) const;
46  virtual void addRange(AddrSpace *spc,uintb first,uintb last);
47  virtual void removeRange(AddrSpace *spc,uintb first,uintb last) {
48  throw LowlevelError("remove_range should not be performed on ghidra scope");
49  }
50  virtual Scope *buildSubScope(uint8 id,const string &nm);
51  virtual void addSymbolInternal(Symbol *sym) { throw LowlevelError("add_symbol_internal unimplemented"); }
52  virtual SymbolEntry *addMapInternal(Symbol *sym,uint4 exfl,const Address &addr,int4 off,int4 sz,
53  const RangeList &uselim) { throw LowlevelError("addMap unimplemented"); }
54  virtual SymbolEntry *addDynamicMapInternal(Symbol *sym,uint4 exfl,uint8 hash,int4 off,int4 sz,
55  const RangeList &uselim) { throw LowlevelError("addMap unimplemented"); }
56 public:
58 
65  void lockDefaultProperties(void) { flagbaseDefault = ghidra->symboltab->getProperties(); cacheDirty = false; }
66  virtual ~ScopeGhidra(void);
67  virtual void clear(void);
68  virtual SymbolEntry *addSymbol(const string &name,Datatype *ct,
69  const Address &addr,const Address &usepoint);
70  virtual string buildVariableName(const Address &addr,
71  const Address &pc,
72  Datatype *ct,int4 &index,uint4 flags) const {
73  return cache->buildVariableName(addr,pc,ct,index,flags); }
74  virtual string buildUndefinedName(void) const { return cache->buildUndefinedName(); }
75  virtual void setAttribute(Symbol *sym,uint4 attr) { cache->setAttribute(sym,attr); }
76  virtual void clearAttribute(Symbol *sym,uint4 attr) { cache->clearAttribute(sym,attr); }
77  virtual void setDisplayFormat(Symbol *sym,uint4 attr) { cache->setDisplayFormat(sym,attr); }
78 
79  virtual void adjustCaches(void) { cache->adjustCaches(); }
80  virtual SymbolEntry *findAddr(const Address &addr,const Address &usepoint) const;
81  virtual SymbolEntry *findContainer(const Address &addr,int4 size,
82  const Address &usepoint) const;
83  virtual SymbolEntry *findClosestFit(const Address &addr,int4 size,
84  const Address &usepoint) const {
85  throw LowlevelError("findClosestFit unimplemented"); }
86  virtual Funcdata *findFunction(const Address &addr) const;
87  virtual ExternRefSymbol *findExternalRef(const Address &addr) const;
88  virtual LabSymbol *findCodeLabel(const Address &addr) const;
90 
91  virtual SymbolEntry *findOverlap(const Address &addr,int4 size) const { throw LowlevelError("findOverlap unimplemented"); }
92  virtual void findByName(const string &name,vector<Symbol *> &res) const { throw LowlevelError("findByName unimplemented"); }
93  virtual bool isNameUsed(const string &nm,const Scope *op2) const { throw LowlevelError("isNameUsed unimplemented"); }
94 
95  virtual MapIterator begin(void) const { throw LowlevelError("begin unimplemented"); }
96  virtual MapIterator end(void) const { throw LowlevelError("end unimplemented"); }
97  virtual list<SymbolEntry>::const_iterator beginDynamic(void) const { throw LowlevelError("beginDynamic unimplemented"); }
98  virtual list<SymbolEntry>::const_iterator endDynamic(void) const { throw LowlevelError("endDynamic unimplemented"); }
99  virtual list<SymbolEntry>::iterator beginDynamic(void) { throw LowlevelError("beginDynamic unimplemented"); }
100  virtual list<SymbolEntry>::iterator endDynamic(void) { throw LowlevelError("endDynamic unimplemented"); }
101  virtual void clearCategory(int4 cat) { throw LowlevelError("clearCategory unimplemented"); }
102  virtual void clearUnlockedCategory(int4 cat) { throw LowlevelError("clearUnlockedCategory unimplemented"); }
103  virtual void clearUnlocked(void) { throw LowlevelError("clearUnlocked unimplemented"); }
104  virtual void restrictScope(Funcdata *f) { throw LowlevelError("restrictScope unimplemented"); }
105  virtual void removeSymbolMappings(Symbol *symbol) { throw LowlevelError("removeSymbolMappings unimplemented"); }
106  virtual void removeSymbol(Symbol *symbol) { throw LowlevelError("removeSymbol unimplemented"); }
107  virtual void renameSymbol(Symbol *sym,const string &newname) { throw LowlevelError("renameSymbol unimplemented"); }
108  virtual void retypeSymbol(Symbol *sym,Datatype *ct) { throw LowlevelError("retypeSymbol unimplemented"); }
109  virtual string makeNameUnique(const string &nm) const { throw LowlevelError("makeNameUnique unimplemented"); }
110  virtual void saveXml(ostream &s) const { throw LowlevelError("saveXml unimplemented"); }
111  virtual void restoreXml(const Element *el) { throw LowlevelError("restoreXml unimplemented"); }
112  virtual void printEntries(ostream &s) const { throw LowlevelError("printEntries unimplemented"); }
113  virtual int4 getCategorySize(int4 cat) const { throw LowlevelError("getCategorySize unimplemented"); }
114  virtual Symbol *getCategorySymbol(int4 cat,int4 ind) const { throw LowlevelError("getCategorySymbol unimplemented"); }
115  virtual void setCategory(Symbol *sym,int4 cat,int4 ind) { throw LowlevelError("setCategory unimplemented"); }
116 };
117 
128  friend class ScopeGhidra;
129  ArchitectureGhidra *ghidra;
130 protected:
131  virtual SymbolEntry *addMapInternal(Symbol *sym,uint4 exfl,const Address &addr,int4 off,int4 sz,
132  const RangeList &uselim);
133 public:
134  ScopeGhidraNamespace(uint8 id,const string &nm,ArchitectureGhidra *g)
135  : ScopeInternal(id,nm,g) { ghidra = g; }
136 
137  virtual bool isNameUsed(const string &nm,const Scope *op2) const;
138 };
139 
140 #endif
ghidra_arch.hh
Ghidra specific architecture information and connection to a Ghidra client.
AddrSpace
A region where processor data is stored.
Definition: space.hh:73
ScopeGhidra::clearAttribute
virtual void clearAttribute(Symbol *sym, uint4 attr)
Clear boolean Varnode properties on a Symbol.
Definition: database_ghidra.hh:76
LowlevelError
The lowest level error generated by the decompiler.
Definition: error.hh:44
ScopeInternal::ScopeInternal
ScopeInternal(uint8 id, const string &nm, Architecture *g)
Construct the Scope.
Definition: database.cc:1861
ScopeInternal::adjustCaches
virtual void adjustCaches(void)
Let scopes internally adjust any caches.
Definition: database.cc:2017
ExternRefSymbol
A function Symbol referring to an external location.
Definition: database.hh:296
ScopeGhidra::removeSymbolMappings
virtual void removeSymbolMappings(Symbol *symbol)
Remove all SymbolEntrys from the given Symbol.
Definition: database_ghidra.hh:105
ScopeGhidraNamespace::isNameUsed
virtual bool isNameUsed(const string &nm, const Scope *op2) const
Check if the given name is occurs within the given scope path.
Definition: database_ghidra.cc:404
ScopeInternal
An in-memory implementation of the Scope interface.
Definition: database.hh:726
ScopeGhidra::clearCategory
virtual void clearCategory(int4 cat)
Clear all symbols of the given category from this scope.
Definition: database_ghidra.hh:101
ScopeGhidra::begin
virtual MapIterator begin(void) const
Beginning iterator to mapped SymbolEntrys.
Definition: database_ghidra.hh:95
ScopeGhidra::setCategory
virtual void setCategory(Symbol *sym, int4 cat, int4 ind)
Set the category and index for the given Symbol.
Definition: database_ghidra.hh:115
MapIterator
An iterator over SymbolEntry objects in multiple address spaces.
Definition: database.hh:330
ScopeGhidra::printEntries
virtual void printEntries(ostream &s) const
Dump a description of all SymbolEntry objects to a stream.
Definition: database_ghidra.hh:112
Document
A complete in-memory XML document.
Definition: xml.hh:206
Scope
A collection of Symbol objects within a single (namespace or functional) scope.
Definition: database.hh:402
ScopeGhidra::buildUndefinedName
virtual string buildUndefinedName(void) const
Build a formal undefined name, used internally when a Symbol is not given a name.
Definition: database_ghidra.hh:74
ScopeGhidra::renameSymbol
virtual void renameSymbol(Symbol *sym, const string &newname)
Rename a Symbol within this Scope.
Definition: database_ghidra.hh:107
ScopeGhidra::findOverlap
virtual SymbolEntry * findOverlap(const Address &addr, int4 size) const
Find first Symbol overlapping the given memory range.
Definition: database_ghidra.hh:91
Element
An XML element. A node in the DOM tree.
Definition: xml.hh:150
ScopeGhidra::findExternalRef
virtual ExternRefSymbol * findExternalRef(const Address &addr) const
Find an external reference at the given address.
Definition: database_ghidra.cc:303
ScopeGhidra::findAddr
virtual SymbolEntry * findAddr(const Address &addr, const Address &usepoint) const
Find a Symbol at a given address and usepoint.
Definition: database_ghidra.cc:262
ScopeGhidra::clearUnlockedCategory
virtual void clearUnlockedCategory(int4 cat)
Clear unlocked symbols of the given category from this scope.
Definition: database_ghidra.hh:102
SymbolEntry
A storage location for a particular Symbol.
Definition: database.hh:51
ScopeInternal::setDisplayFormat
virtual void setDisplayFormat(Symbol *sym, uint4 attr)
Set the display format for a Symbol.
Definition: database.cc:2119
ScopeGhidraNamespace::addMapInternal
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.
Definition: database_ghidra.cc:395
ScopeGhidra::findContainer
virtual SymbolEntry * findContainer(const Address &addr, int4 size, const Address &usepoint) const
Find the smallest Symbol containing the given memory range.
Definition: database_ghidra.cc:282
ScopeGhidra::setDisplayFormat
virtual void setDisplayFormat(Symbol *sym, uint4 attr)
Set the display format for a Symbol.
Definition: database_ghidra.hh:77
ScopeGhidra::ScopeGhidra
ScopeGhidra(ArchitectureGhidra *g)
Constructor.
Definition: database_ghidra.cc:27
ScopeGhidra::addSymbol
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.
Definition: database_ghidra.cc:386
RangeList
A disjoint set of Ranges, possibly across multiple address spaces.
Definition: address.hh:203
ScopeGhidra::findFunction
virtual Funcdata * findFunction(const Address &addr) const
Find the function starting at the given address.
Definition: database_ghidra.cc:319
ScopeGhidra
An implementation of the Scope interface by querying a Ghidra client for Symbol information.
Definition: database_ghidra.hh:35
ScopeGhidra::restrictScope
virtual void restrictScope(Funcdata *f)
Convert this to a local Scope.
Definition: database_ghidra.hh:104
ScopeGhidra::saveXml
virtual void saveXml(ostream &s) const
Write out this as a <scope> XML tag.
Definition: database_ghidra.hh:110
ScopeGhidra::buildVariableName
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.
Definition: database_ghidra.hh:70
ScopeGhidra::clearUnlocked
virtual void clearUnlocked(void)
Clear all unlocked symbols from this scope.
Definition: database_ghidra.hh:103
ScopeGhidra::adjustCaches
virtual void adjustCaches(void)
Let scopes internally adjust any caches.
Definition: database_ghidra.hh:79
Address
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
Funcdata
Container for data structures associated with a single function.
Definition: funcdata.hh:45
Datatype
The base datatype class for the decompiler.
Definition: type.hh:62
ScopeInternal::setAttribute
virtual void setAttribute(Symbol *sym, uint4 attr)
Set boolean Varnode properties on a Symbol.
Definition: database.cc:2103
ScopeGhidra::endDynamic
virtual list< SymbolEntry >::iterator endDynamic(void)
Ending iterator to dynamic SymbolEntrys.
Definition: database_ghidra.hh:100
ScopeGhidra::clear
virtual void clear(void)
Clear all symbols from this scope.
Definition: database_ghidra.cc:251
database.hh
Symbol and Scope objects for the decompiler.
ScopeGhidra::end
virtual MapIterator end(void) const
Ending iterator to mapped SymbolEntrys.
Definition: database_ghidra.hh:96
ScopeGhidra::getCategorySize
virtual int4 getCategorySize(int4 cat) const
Get the number of Symbols in the given category.
Definition: database_ghidra.hh:113
ScopeGhidra::restoreXml
virtual void restoreXml(const Element *el)
Restore this Scope from a <scope> XML tag.
Definition: database_ghidra.hh:111
ScopeGhidra::retypeSymbol
virtual void retypeSymbol(Symbol *sym, Datatype *ct)
Change the data-type of a Symbol within this Scope.
Definition: database_ghidra.hh:108
ScopeGhidra::makeNameUnique
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.
Definition: database_ghidra.hh:109
LabSymbol
A Symbol that labels code internal to a function.
Definition: database.hh:281
ScopeGhidraNamespace
A global namespace Scope.
Definition: database_ghidra.hh:127
ScopeInternal::clearAttribute
virtual void clearAttribute(Symbol *sym, uint4 attr)
Clear boolean Varnode properties on a Symbol.
Definition: database.cc:2111
Scope::name
string name
Name of this scope.
Definition: database.hh:414
ArchitectureGhidra
An implementation of the Architecture interface and connection to a Ghidra client.
Definition: ghidra_arch.hh:60
ScopeGhidra::lockDefaultProperties
void lockDefaultProperties(void)
Lock in the default state of the boolean property map.
Definition: database_ghidra.hh:65
ScopeInternal::buildVariableName
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.
Definition: database.cc:2335
ScopeGhidra::setAttribute
virtual void setAttribute(Symbol *sym, uint4 attr)
Set boolean Varnode properties on a Symbol.
Definition: database_ghidra.hh:75
ScopeGhidra::isNameUsed
virtual bool isNameUsed(const string &nm, const Scope *op2) const
Check if the given name is occurs within the given scope path.
Definition: database_ghidra.hh:93
ScopeGhidra::resolveExternalRefFunction
virtual Funcdata * resolveExternalRefFunction(ExternRefSymbol *sym) const
Convert an external reference to the referenced function.
Definition: database_ghidra.cc:356
partmap< Address, uint4 >
ScopeGhidra::removeSymbol
virtual void removeSymbol(Symbol *symbol)
Remove the given Symbol from this Scope.
Definition: database_ghidra.hh:106
ScopeGhidra::findCodeLabel
virtual LabSymbol * findCodeLabel(const Address &addr) const
Find a label Symbol at the given address.
Definition: database_ghidra.cc:337
ScopeInternal::buildUndefinedName
virtual string buildUndefinedName(void) const
Build a formal undefined name, used internally when a Symbol is not given a name.
Definition: database.cc:2421
ScopeGhidra::findByName
virtual void findByName(const string &name, vector< Symbol * > &res) const
Find a Symbol by name within this Scope.
Definition: database_ghidra.hh:92
Architecture::symboltab
Database * symboltab
Memory map of global variables and functions.
Definition: architecture.hh:140
Symbol
The base class for a symbol in a symbol table or scope.
Definition: database.hh:152
ScopeGhidra::beginDynamic
virtual list< SymbolEntry >::const_iterator beginDynamic(void) const
Beginning iterator to dynamic SymbolEntrys.
Definition: database_ghidra.hh:97
ScopeGhidra::findClosestFit
virtual SymbolEntry * findClosestFit(const Address &addr, int4 size, const Address &usepoint) const
Find Symbol which is the closest fit to the given memory range.
Definition: database_ghidra.hh:83
ScopeGhidra::getCategorySymbol
virtual Symbol * getCategorySymbol(int4 cat, int4 ind) const
Retrieve a Symbol by index within a specific category.
Definition: database_ghidra.hh:114
ScopeGhidra::endDynamic
virtual list< SymbolEntry >::const_iterator endDynamic(void) const
Ending iterator to dynamic SymbolEntrys.
Definition: database_ghidra.hh:98
ScopeGhidra::beginDynamic
virtual list< SymbolEntry >::iterator beginDynamic(void)
Beginning iterator to dynamic SymbolEntrys.
Definition: database_ghidra.hh:99