My Project
database.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  */
22 
23 #ifndef __CPUI_DATABASE__
24 #define __CPUI_DATABASE__
25 
26 #include "variable.hh"
27 #include "partmap.hh"
28 #include "rangemap.hh"
29 
30 class Architecture;
31 class Funcdata;
32 class Scope;
33 class Database;
34 class Symbol;
35 class PrintLanguage;
36 
51 class SymbolEntry {
52  friend class Scope;
53  Symbol *symbol;
54  uint4 extraflags;
55  Address addr;
56  uint8 hash;
57  int4 offset;
58  int4 size;
59  RangeList uselimit;
60  SymbolEntry(Symbol *sym);
61 public:
67  class EntryInitData {
68  friend class SymbolEntry;
69  AddrSpace *space;
70  Symbol *symbol;
71  uint4 extraflags;
72  int4 offset;
73  const RangeList &uselimit;
74  public:
75  EntryInitData(Symbol *sym,uint4 exfl,AddrSpace *spc,int4 off,const RangeList &ul)
76  : uselimit(ul) { symbol = sym; extraflags=exfl; space = spc; offset = off; }
77  };
78 
83  class EntrySubsort {
84  friend class SymbolEntry;
85  int4 useindex;
86  uintb useoffset;
87  public:
88  EntrySubsort(const Address &addr) {
89  useindex = addr.getSpace()->getIndex(); useoffset = addr.getOffset(); }
90  EntrySubsort(void) { useindex=0; useoffset=0; }
91 
95  EntrySubsort(bool val) {
96  if (val) { useindex=0xffff; } // Greater than any real values
97  else { useindex=0; useoffset=0; } // Less than any real values
98  }
101  useindex = op2.useindex;
102  useoffset = op2.useoffset;
103  }
105  bool operator<(const EntrySubsort &op2) {
106  if (useindex != op2.useindex)
107  return (useindex < op2.useindex);
108  return (useoffset < op2.useoffset);
109  }
110  };
111  typedef uintb linetype;
114 
115  SymbolEntry(void) {}
116  SymbolEntry(Symbol *sym,uint4 exfl,uint8 h,int4 off,int4 sz,const RangeList &rnglist);
117  bool isPiece(void) const { return ((extraflags&(Varnode::precislo|Varnode::precishi))!=0); }
118  bool isDynamic(void) const { return addr.isInvalid(); }
119  bool isInvalid(void) const { return (addr.isInvalid() && (hash==0)); }
120  uint4 getAllFlags(void) const;
121  int4 getOffset(void) const { return offset; }
122  uintb getFirst(void) const { return addr.getOffset(); }
123  uintb getLast(void) const { return (addr.getOffset()+size-1); }
124  subsorttype getSubsort(void) const;
125  void initialize(const EntryInitData &data,uintb a,uintb b);
126  Symbol *getSymbol(void) const { return symbol; }
127  const Address &getAddr(void) const { return addr; }
128  uint8 getHash(void) const { return hash; }
129  int4 getSize(void) const { return size; }
130  bool inUse(const Address &usepoint) const;
131  const RangeList &getUseLimit(void) const { return uselimit; }
132  Address getFirstUseAddress(void) const;
133  void setUseLimit(const RangeList &uselim) { uselimit = uselim; }
134  bool isAddrTied(void) const;
135  bool updateType(Varnode *vn) const;
136  Datatype *getSizedType(const Address &addr,int4 sz) const;
137  void printEntry(ostream &s) const;
138  void saveXml(ostream &s) const;
139  List::const_iterator restoreXml(List::const_iterator iter,const AddrSpaceManager *manage);
140 };
142 
153 class Symbol {
154  friend class Scope;
155  friend class ScopeInternal;
156  friend class SymbolCompareName;
157 protected:
159  string name;
161  uint4 nameDedup;
162  uint4 flags;
163  // only typelock,namelock,readonly,externref
164  // addrtied, persist inherited from scope
165  uint4 dispflags;
166  int2 category;
167  uint2 catindex;
168  uint8 symbolId;
169  vector<list<SymbolEntry>::iterator> mapentry;
170  uint4 wholeCount;
171  virtual ~Symbol(void) {}
172  void setDisplayFormat(uint4 val);
173  void checkSizeTypeLock(void);
174 public:
176  enum {
177  force_hex = 1,
178  force_dec = 2,
179  force_oct = 3,
180  force_bin = 4,
181  force_char = 5,
182  size_typelock = 8,
183  isolate = 16,
184  merge_problems = 32
185  };
187  Symbol(Scope *sc,const string &nm,Datatype *ct)
188  { scope=sc; name=nm; nameDedup=0; type=ct; flags=0; dispflags=0; category=-1; symbolId=0; wholeCount=0; }
189 
191  Symbol(Scope *sc) { scope=sc; nameDedup=0; flags=0; dispflags=0; category=-1; symbolId = 0; wholeCount=0; }
192 
193  const string &getName(void) const { return name; }
194  Datatype *getType(void) const { return type; }
195  uint8 getId(void) const { return symbolId; }
196  uint4 getFlags(void) const { return flags; }
197  uint4 getDisplayFormat(void) const { return (dispflags & 7); }
198  int2 getCategory(void) const { return category; }
199  uint2 getCategoryIndex(void) const { return catindex; }
200  bool isTypeLocked(void) const { return ((flags&Varnode::typelock)!=0); }
201  bool isNameLocked(void) const { return ((flags&Varnode::namelock)!=0); }
202  bool isSizeTypeLocked(void) const { return ((dispflags & size_typelock)!=0); }
203  bool isIndirectStorage(void) const { return ((flags&Varnode::indirectstorage)!=0); }
204  bool isHiddenReturn(void) const { return ((flags&Varnode::hiddenretparm)!=0); }
205  bool isNameUndefined(void) const;
206  bool isMultiEntry(void) const { return (wholeCount > 1); }
207  bool hasMergeProblems(void) const { return ((dispflags & merge_problems)!=0); }
208  void setMergeProblems(void) { dispflags |= merge_problems; }
209  bool isIsolated(void) const { return ((dispflags & isolate)!=0); }
210  void setIsolated(bool val);
211  Scope *getScope(void) const { return scope; }
212  SymbolEntry *getFirstWholeMap(void) const;
213  SymbolEntry *getMapEntry(const Address &addr) const;
214  int4 numEntries(void) const { return mapentry.size(); }
215  SymbolEntry *getMapEntry(int4 i) const { return &(*mapentry[i]); }
216  int4 getMapEntryPosition(const SymbolEntry *entry) const;
217  int4 getResolutionDepth(const Scope *useScope) const;
218  void saveXmlHeader(ostream &s) const;
219  void restoreXmlHeader(const Element *el);
220  void saveXmlBody(ostream &s) const;
221  void restoreXmlBody(List::const_iterator iter);
222  virtual void saveXml(ostream &s) const;
223  virtual void restoreXml(const Element *el);
224  virtual int4 getBytesConsumed(void) const;
225  static uint8 ID_BASE;
226 };
227 
230 inline void Symbol::setDisplayFormat(uint4 val)
231 
232 {
233  dispflags &= 0xfffffff8;
234  dispflags |= val;
235 }
236 
239 inline uint4 SymbolEntry::getAllFlags(void) const {
240  return extraflags | symbol->getFlags();
241 }
242 
243 inline bool SymbolEntry::isAddrTied(void) const {
244  return ((symbol->getFlags()&Varnode::addrtied)!=0);
245 }
246 
251 class FunctionSymbol : public Symbol {
252  Funcdata *fd;
253  int4 consumeSize;
254  virtual ~FunctionSymbol(void);
255  void buildType(void);
256 public:
257  FunctionSymbol(Scope *sc,const string &nm,int4 size);
258  FunctionSymbol(Scope *sc,int4 size);
259  Funcdata *getFunction(void);
260  virtual void saveXml(ostream &s) const;
261  virtual void restoreXml(const Element *el);
262  virtual int4 getBytesConsumed(void) const { return consumeSize; }
263 };
264 
270 class EquateSymbol : public Symbol {
271  uintb value;
272 public:
273  EquateSymbol(Scope *sc) : Symbol(sc) { value = 0; category = 1; }
274  uintb getValue(void) const { return value; }
275  bool isValueClose(uintb op2Value,int4 size) const;
276  virtual void saveXml(ostream &s) const;
277  virtual void restoreXml(const Element *el);
278 };
279 
281 class LabSymbol : public Symbol {
282  void buildType(void);
283 public:
284  LabSymbol(Scope *sc,const string &nm);
285  LabSymbol(Scope *sc);
286  virtual void saveXml(ostream &s) const;
287  virtual void restoreXml(const Element *el);
288 };
289 
296 class ExternRefSymbol : public Symbol {
297  Address refaddr;
298  void buildNameType(void);
299  virtual ~ExternRefSymbol(void) {}
300 public:
301  ExternRefSymbol(Scope *sc,const Address &ref,const string &nm);
302  ExternRefSymbol(Scope *sc) : Symbol(sc) {}
303  const Address &getRefAddr(void) const { return refaddr; }
304  virtual void saveXml(ostream &s) const;
305  virtual void restoreXml(const Element *el);
306 };
307 
310 public:
317  bool operator()(const Symbol *sym1,const Symbol *sym2) const {
318  int4 comp = sym1->name.compare(sym2->name);
319  if (comp < 0) return true;
320  if (comp > 0) return false;
321  return (sym1->nameDedup < sym2->nameDedup);
322  }
323 };
324 typedef set<Symbol *,SymbolCompareName> SymbolNameTree;
325 
330 class MapIterator {
331  const vector<EntryMap *> *map;
332  vector<EntryMap *>::const_iterator curmap;
333  list<SymbolEntry>::const_iterator curiter;
334 public:
335  MapIterator(void) { map = (const vector<EntryMap *> *)0; }
336 
342  MapIterator(const vector<EntryMap *> *m,
343  vector<EntryMap *>::const_iterator cm,
344  list<SymbolEntry>::const_iterator ci) {
345  map = m; curmap = cm; curiter = ci;
346  }
347 
349  MapIterator(const MapIterator &op2) {
350  map = op2.map; curmap = op2.curmap; curiter = op2.curiter;
351  }
352  const SymbolEntry *operator*(void) const { return &(*curiter); }
353  MapIterator &operator++(void);
354  MapIterator operator++(int4 i);
355 
358  map = op2.map;
359  curmap = op2.curmap;
360  curiter = op2.curiter;
361  return *this;
362  }
363 
365  bool operator==(const MapIterator &op2) const {
366  if (curmap != op2.curmap) return false;
367  if (curmap == map->end()) return true;
368  return (curiter==op2.curiter);
369  }
370 
372  bool operator!=(const MapIterator &op2) const {
373  if (curmap != op2.curmap) return true;
374  if (curmap == map->end()) return false;
375  return (curiter!=op2.curiter);
376  }
377 };
378 
383 class ScopeKey {
384  string name;
385  uintb dedupId;
386 public:
387  ScopeKey(const string &nm,uint4 id) { name = nm; dedupId = id; }
388  bool operator<(const ScopeKey &op2) const;
389 };
390 typedef map<ScopeKey,Scope *> ScopeMap;
391 
413 class Scope {
414  friend class Database;
415  friend class ScopeCompare;
416  RangeList rangetree;
417  Scope *parent;
418  ScopeMap children;
419  void attachScope(Scope *child);
420  void detachScope(ScopeMap::iterator iter);
421  void assignId(uint4 val) { uniqueId = val; }
422 
423 protected:
425  string name;
427  uint4 uniqueId;
428  static const Scope *stackAddr(const Scope *scope1,
429  const Scope *scope2,
430  const Address &addr,
431  const Address &usepoint,
432  SymbolEntry **addrmatch);
433  static const Scope *stackContainer(const Scope *scope1,
434  const Scope *scope2,
435  const Address &addr,int4 size,
436  const Address &usepoint,
437  SymbolEntry **addrmatch);
438  static const Scope *stackClosestFit(const Scope *scope1,
439  const Scope *scope2,
440  const Address &addr,int4 size,
441  const Address &usepoint,
442  SymbolEntry **addrmatch);
443  static const Scope *stackFunction(const Scope *scope1,
444  const Scope *scope2,
445  const Address &addr,
446  Funcdata **addrmatch);
447  static const Scope *stackExternalRef(const Scope *scope1,
448  const Scope *scope2,
449  const Address &addr,
450  ExternRefSymbol **addrmatch);
451  static const Scope *stackCodeLabel(const Scope *scope1,
452  const Scope *scope2,
453  const Address &addr,
454  LabSymbol **addrmatch);
455 
456  const RangeList &getRangeTree(void) const { return rangetree; }
457  virtual void restrictScope(Funcdata *f);
458 
459  // These add/remove range are for scope \b discovery, i.e. we may
460  // know an address belongs to a certain scope, without knowing any symbol
461  virtual void addRange(AddrSpace *spc,uintb first,uintb last);
462  virtual void removeRange(AddrSpace *spc,uintb first,uintb last);
463 
467  virtual void addSymbolInternal(Symbol *sym)=0;
468 
479  virtual SymbolEntry *addMapInternal(Symbol *sym,uint4 exfl,const Address &addr,int4 off,int4 sz,
480  const RangeList &uselim)=0;
481 
482 
494  virtual SymbolEntry *addDynamicMapInternal(Symbol *sym,uint4 exfl,uint8 hash,int4 off,int4 sz,
495  const RangeList &uselim)=0;
496  SymbolEntry *addMap(const SymbolEntry &entry);
497  void setSymbolId(Symbol *sym,uint8 id) const { sym->symbolId = id; }
498 public:
499 #ifdef OPACTION_DEBUG
500  mutable bool debugon;
501  void turnOnDebug(void) const { debugon = true; }
502  void turnOffDebug(void) const { debugon = false; }
503 #endif
504  Scope(const string &nm,Architecture *g) {
506  name = nm; glb = g; parent = (Scope *)0; fd = (Funcdata *)0; uniqueId = 0;
507 #ifdef OPACTION_DEBUG
508  debugon = false;
509 #endif
510  }
511  virtual ~Scope(void);
512  virtual MapIterator begin(void) const=0;
513  virtual MapIterator end(void) const=0;
514  virtual list<SymbolEntry>::const_iterator beginDynamic(void) const=0;
515  virtual list<SymbolEntry>::const_iterator endDynamic(void) const=0;
516  virtual list<SymbolEntry>::iterator beginDynamic(void)=0;
517  virtual list<SymbolEntry>::iterator endDynamic(void)=0;
518  virtual void clear(void)=0;
519  virtual void clearCategory(int4 cat)=0;
520  virtual void clearUnlocked(void)=0;
521  virtual void clearUnlockedCategory(int4 cat)=0;
522 
531  virtual bool inScope(const Address &addr,int4 size, const Address &usepoint) const {
532  return rangetree.inRange(addr,size); }
533 
534  virtual void removeSymbolMappings(Symbol *symbol)=0;
535  virtual void removeSymbol(Symbol *symbol)=0;
536  virtual void renameSymbol(Symbol *sym,const string &newname)=0;
537 
543  virtual void retypeSymbol(Symbol *sym,Datatype *ct)=0;
544  virtual void setAttribute(Symbol *sym,uint4 attr)=0;
545  virtual void clearAttribute(Symbol *sym,uint4 attr)=0;
546  virtual void setDisplayFormat(Symbol *sym,uint4 attr)=0;
547 
548  // Find routines only search the scope itself
549 
555  virtual SymbolEntry *findAddr(const Address &addr,const Address &usepoint) const=0;
556 
563  virtual SymbolEntry *findContainer(const Address &addr,int4 size,
564  const Address &usepoint) const=0;
565 
572  virtual SymbolEntry *findClosestFit(const Address &addr,int4 size,
573  const Address &usepoint) const=0;
574 
579  virtual Funcdata *findFunction(const Address &addr) const=0;
580 
585  virtual ExternRefSymbol *findExternalRef(const Address &addr) const=0;
586 
591  virtual LabSymbol *findCodeLabel(const Address &addr) const=0;
592 
598  virtual SymbolEntry *findOverlap(const Address &addr,int4 size) const=0;
599 
605  virtual void findByName(const string &name,vector<Symbol *> &res) const=0;
606 
613  virtual bool isNameUsed(const string &name) const=0;
614 
619  virtual Funcdata *resolveExternalRefFunction(ExternRefSymbol *sym) const=0;
620 
629  virtual string buildVariableName(const Address &addr,
630  const Address &pc,
631  Datatype *ct,int4 &index,uint4 flags) const=0;
632 
636  virtual string buildUndefinedName(void) const=0;
637 
642  virtual string makeNameUnique(const string &nm) const=0;
643 
644  virtual void saveXml(ostream &s) const=0;
645  virtual void restoreXml(const Element *el)=0;
646  virtual void printEntries(ostream &s) const=0;
647 
652  virtual int4 getCategorySize(int4 cat) const=0;
653 
659  virtual Symbol *getCategorySymbol(int4 cat,int4 ind) const=0;
660 
666  virtual void setCategory(Symbol *sym,int4 cat,int4 ind)=0;
667 
668  virtual SymbolEntry *addSymbol(const string &name,Datatype *ct,
669  const Address &addr,const Address &usepoint);
670 
671  const string &getName(void) const { return name; }
672  bool isGlobal(void) const { return (fd == (Funcdata *)0); }
673 
674  // The main global querying routines
675  void queryByName(const string &name,vector<Symbol *> &res) const;
676  Funcdata *queryFunction(const string &name) const;
677  SymbolEntry *queryByAddr(const Address &addr,
678  const Address &usepoint) const;
679  SymbolEntry *queryContainer(const Address &addr,int4 size,
680  const Address &usepoint) const;
681  SymbolEntry *queryProperties(const Address &addr,int4 size,
682  const Address &usepoint,uint4 &flags) const;
683  Funcdata *queryFunction(const Address &addr) const;
684  Funcdata *queryExternalRefFunction(const Address &addr) const;
685  LabSymbol *queryCodeLabel(const Address &addr) const;
686 
687  Scope *resolveScope(const string &name) const;
688  Scope *discoverScope(const Address &addr,int4 sz,const Address &usepoint);
689  ScopeMap::const_iterator childrenBegin() const { return children.begin(); }
690  ScopeMap::const_iterator childrenEnd() const { return children.end(); }
691  void saveXmlRecursive(ostream &s,bool onlyGlobal) const;
692  void overrideSizeLockType(Symbol *sym,Datatype *ct);
693  void resetSizeLockType(Symbol *sym);
694  bool isSubScope(const Scope *scp) const;
695  string getFullName(void) const;
696  void getNameSegments(vector<string> &vec) const;
697  void getScopePath(vector<Scope *> &vec) const;
698  bool isNameUsed(const string &nm,const Scope *op2) const;
699  const Scope *findDistinguishingScope(const Scope *op2) const;
700  Architecture *getArch(void) const { return glb; }
701  Scope *getParent(void) const { return parent; }
702  Symbol *addSymbol(const string &name,Datatype *ct);
703  SymbolEntry *addMapPoint(Symbol *sym,const Address &addr,
704  const Address &usepoint);
705  Symbol *addMapSym(const Element *el);
706  FunctionSymbol *addFunction(const Address &addr,const string &nm);
707  ExternRefSymbol *addExternalRef(const Address &addr,const Address &refaddr,const string &nm);
708  LabSymbol *addCodeLabel(const Address &addr,const string &nm);
709  Symbol *addDynamicSymbol(const string &nm,Datatype *ct,const Address &caddr,uint8 hash);
710  string buildDefaultName(Symbol *sym,int4 &base,Varnode *vn) const;
711  bool isReadOnly(const Address &addr,int4 size,const Address &usepoint) const;
712  void printBounds(ostream &s) const { rangetree.printBounds(s); }
713 };
714 
721 class ScopeInternal : public Scope {
722  void processHole(const Element *el);
723  void insertNameTree(Symbol *sym);
724  SymbolNameTree::const_iterator findFirstByName(const string &name) const;
725 protected:
726  virtual void addSymbolInternal(Symbol *sym);
727  virtual SymbolEntry *addMapInternal(Symbol *sym,uint4 exfl,const Address &addr,int4 off,int4 sz,const RangeList &uselim);
728  virtual SymbolEntry *addDynamicMapInternal(Symbol *sym,uint4 exfl,uint8 hash,int4 off,int4 sz,
729  const RangeList &uselim);
731  vector<EntryMap *> maptable;
732  vector<vector<Symbol *> > category;
733  list<SymbolEntry> dynamicentry;
735  uint8 nextUniqueId;
736 public:
737  ScopeInternal(const string &nm,Architecture *g);
738  virtual void clear(void);
739  virtual void categorySanity(void);
740  virtual void clearCategory(int4 cat);
741  virtual void clearUnlocked(void);
742  virtual void clearUnlockedCategory(int4 cat);
743  virtual ~ScopeInternal(void);
744  virtual MapIterator begin(void) const;
745  virtual MapIterator end(void) const;
746  virtual list<SymbolEntry>::const_iterator beginDynamic(void) const;
747  virtual list<SymbolEntry>::const_iterator endDynamic(void) const;
748  virtual list<SymbolEntry>::iterator beginDynamic(void);
749  virtual list<SymbolEntry>::iterator endDynamic(void);
750  virtual void removeSymbolMappings(Symbol *symbol);
751  virtual void removeSymbol(Symbol *symbol);
752  virtual void renameSymbol(Symbol *sym,const string &newname);
753  virtual void retypeSymbol(Symbol *sym,Datatype *ct);
754  virtual void setAttribute(Symbol *sym,uint4 attr);
755  virtual void clearAttribute(Symbol *sym,uint4 attr);
756  virtual void setDisplayFormat(Symbol *sym,uint4 attr);
757 
758  virtual SymbolEntry *findAddr(const Address &addr,const Address &usepoint) const;
759  virtual SymbolEntry *findContainer(const Address &addr,int4 size,
760  const Address &usepoint) const;
761  virtual SymbolEntry *findClosestFit(const Address &addr,int4 size,
762  const Address &usepoint) const;
763  virtual Funcdata *findFunction(const Address &addr) const;
764  virtual ExternRefSymbol *findExternalRef(const Address &addr) const;
765  virtual LabSymbol *findCodeLabel(const Address &addr) const;
766  virtual SymbolEntry *findOverlap(const Address &addr,int4 size) const;
767 
768  virtual void findByName(const string &name,vector<Symbol *> &res) const;
769  virtual bool isNameUsed(const string &name) const;
770  virtual Funcdata *resolveExternalRefFunction(ExternRefSymbol *sym) const;
771 
772  virtual string buildVariableName(const Address &addr,
773  const Address &pc,
774  Datatype *ct,int4 &index,uint4 flags) const;
775  virtual string buildUndefinedName(void) const;
776  virtual string makeNameUnique(const string &nm) const;
777  virtual void saveXml(ostream &s) const;
778  virtual void restoreXml(const Element *el);
779  virtual void printEntries(ostream &s) const;
780  virtual int4 getCategorySize(int4 cat) const;
781  virtual Symbol *getCategorySymbol(int4 cat,int4 ind) const;
782  virtual void setCategory(Symbol *sym,int4 cat,int4 ind);
783  void assignDefaultNames(int4 &base);
784  set<Symbol *>::const_iterator beginMultiEntry(void) const { return multiEntrySet.begin(); }
785  set<Symbol *>::const_iterator endMultiEntry(void) const { return multiEntrySet.end(); }
786  static void savePathXml(ostream &s,const vector<string> &vec);
787  static void restorePathXml(vector<string> &vec,const Element *el);
788 };
789 
795 class ScopeMapper {
796  friend class Database;
798  class NullSubsort {
799  public:
800  NullSubsort(void) {}
801  NullSubsort(bool val) {}
802  NullSubsort(const NullSubsort &op2) {}
803  bool operator<(const NullSubsort &op2) { return false; }
804  };
805 public:
806  typedef Address linetype;
807  typedef NullSubsort subsorttype;
808  typedef Scope *inittype;
809 private:
810  Scope *scope;
811  Address first;
812  Address last;
813 public:
814  ScopeMapper(void) {}
815  Address getFirst(void) const { return first; }
816  Address getLast(void) const { return last; }
817  NullSubsort getSubsort(void) const { return NullSubsort(); }
818  Scope *getScope(void) const { return scope; }
819  void initialize(const inittype &data,const Address &f,const Address &l) {
820  scope = data; first = f; last = l; }
821 };
823 
838 class Database {
839  Architecture *glb;
840  Scope *globalscope;
841  ScopeResolve resolvemap;
842  partmap<Address,uint4> flagbase;
843  uint4 nextScopeId;
844  void clearResolve(Scope *scope);
845  void clearResolveRecursive(Scope *scope);
846  void fillResolve(Scope *scope);
847  static void parseParentTag(const Element *el,string &name,vector<string> &parnames);
848 public:
849  Database(Architecture *g) { glb=g; globalscope=(Scope *)0; flagbase.defaultValue()=0; nextScopeId=1; }
850  ~Database(void);
851  Architecture *getArch(void) const { return glb; }
852  void attachScope(Scope *newscope,Scope *parent);
853  void deleteScope(Scope *scope);
854  void deleteSubScopes(Scope *scope);
855  void clearUnlocked(Scope *scope);
856  void setRange(Scope *scope,const RangeList &rlist);
857  void addRange(Scope *scope,AddrSpace *spc,uintb first,uintb last);
858  void removeRange(Scope *scope,AddrSpace *spc,uintb first,uintb last);
859  Scope *getGlobalScope(void) const { return globalscope; }
860  Scope *resolveScope(const vector<string> &subnames) const;
861  Scope *resolveScopeSymbolName(const string &fullname,const string &delim,string &basename,Scope *start) const;
862  const Scope *mapScope(const Scope *qpoint,const Address &addr,const Address &usepoint) const;
863  Scope *mapScope(Scope *qpoint,const Address &addr,const Address &usepoint);
864  uint4 getProperty(const Address &addr) const { return flagbase.getValue(addr); }
865  void setPropertyRange(uint4 flags,const Range &range);
866  void setProperties(const partmap<Address,uint4> &newflags) { flagbase = newflags; }
867  const partmap<Address,uint4> &getProperties(void) const { return flagbase; }
868  void saveXml(ostream &s) const;
869  void restoreXml(const Element *el);
870  void restoreXmlScope(const Element *el,Scope *new_scope);
871 };
872 
873 #endif
vector< vector< Symbol * > > category
References to Symbol objects organized by category.
Definition: database.hh:732
Symbol(Scope *sc)
Construct for use with restoreXml()
Definition: database.hh:191
uint2 catindex
Index within category.
Definition: database.hh:167
bool inUse(const Address &usepoint) const
Is this storage valid for the given code address.
Definition: database.cc:90
bool isHiddenReturn(void) const
Is this a reference to the function return value.
Definition: database.hh:204
A region where processor data is stored.
Definition: space.hh:73
const _valuetype & defaultValue(void) const
Get the default value object.
Definition: partmap.hh:61
uint4 dispflags
Flags affecting the display of this symbol.
Definition: database.hh:165
void printBounds(ostream &s) const
Print a description of this Scope&#39;s owned memory ranges.
Definition: database.hh:712
bool isPiece(void) const
Is this a high or low piece of the whole Symbol.
Definition: database.hh:117
The base datatype class for the decompiler.
Definition: type.hh:62
static uint8 ID_BASE
Base of internal ID&#39;s.
Definition: database.hh:225
void saveXml(ostream &s) const
Save this to an XML stream.
Definition: database.cc:176
uint4 getAllFlags(void) const
Get all Varnode flags for this storage.
Definition: database.hh:239
list< SymbolEntry > dynamicentry
Dynamic symbol entries.
Definition: database.hh:733
A storage location for a particular Symbol.
Definition: database.hh:51
int4 getSize(void) const
Get the number of bytes consumed by this storage.
Definition: database.hh:129
Is this Varnode the high part of a double precision value.
Definition: varnode.hh:104
bool isInvalid(void) const
Is this storage invalid.
Definition: database.hh:119
A manager for different address spaces.
Definition: translate.hh:218
uint8 nextUniqueId
Next available symbol id.
Definition: database.hh:735
virtual ~Symbol(void)
Destructor.
Definition: database.hh:171
Datatype * getSizedType(const Address &addr, int4 sz) const
Get the data-type associated with (a piece of) this.
Definition: database.cc:127
Address linetype
The linear element for a rangemap.
Definition: database.hh:806
Datatype * getType(void) const
Get the data-type.
Definition: database.hh:194
EntrySubsort(const Address &addr)
Construct given a sub-sorting address.
Definition: database.hh:88
Container for data structures associated with a single function.
Definition: funcdata.hh:45
void printEntry(ostream &s) const
Dump a description of this to a stream.
Definition: database.cc:155
const Address & getAddr(void) const
Get the starting address of this storage.
Definition: database.hh:127
Architecture * getArch(void) const
Get the Architecture associate with this.
Definition: database.hh:851
A function Symbol referring to an external location.
Definition: database.hh:296
A manager for symbol scopes for a whole executable.
Definition: database.hh:838
bool isIsolated(void) const
Return true if this is isolated from speculative merging.
Definition: database.hh:209
Scope * inittype
Initialization data for a ScopeMapper.
Definition: database.hh:808
A contiguous range of bytes in some address space.
Definition: address.hh:161
uint8 getId(void) const
Get a unique id for the symbol.
Definition: database.hh:195
const RangeList & getRangeTree(void) const
Access the address ranges owned by this Scope.
Definition: database.hh:456
Definitions for high-level variables.
bool isMultiEntry(void) const
Does this have more than one entire mapping.
Definition: database.hh:206
int2 category
Special category (-1==none 0=parameter 1=equate)
Definition: database.hh:166
A disjoint set of Ranges, possibly across multiple address spaces.
Definition: address.hh:203
void setDisplayFormat(uint4 val)
Set the display format for this Symbol.
Definition: database.hh:230
ScopeMap::const_iterator childrenBegin() const
Beginning iterator of child scopes.
Definition: database.hh:689
SymbolNameTree multiEntrySet
Set of symbols with multiple entries.
Definition: database.hh:734
_valuetype & getValue(const _linetype &pnt)
Get the value object at a point.
Definition: partmap.hh:81
map< ScopeKey, Scope * > ScopeMap
A map from ScopeKey to Scope.
Definition: database.hh:390
Funcdata * fd
(If non-null) the function which this is the local Scope for
Definition: database.hh:426
string name
The local name of the symbol.
Definition: database.hh:159
Scope * getGlobalScope(void) const
Get the global Scope.
Definition: database.hh:859
An iterator over SymbolEntry objects in multiple address spaces.
Definition: database.hh:330
Architecture * glb
Architecture of this scope.
Definition: database.hh:424
MapIterator(const MapIterator &op2)
Copy constructor.
Definition: database.hh:349
bool isNameLocked(void) const
Is the Symbol name-locked.
Definition: database.hh:201
Datatype * type
The symbol&#39;s data-type.
Definition: database.hh:160
rangemap< SymbolEntry > EntryMap
A rangemap of SymbolEntry.
Definition: database.hh:141
bool isSizeTypeLocked(void) const
Is the Symbol size type-locked.
Definition: database.hh:202
A Symbol representing an executable function.
Definition: database.hh:251
EquateSymbol(Scope *sc)
Constructor for use with restoreXml.
Definition: database.hh:273
Templates to define interval map containers.
EntryInitData inittype
Initialization data for a SymbolEntry in a rangemap.
Definition: database.hh:113
Scope * getScope(void) const
Get the Scope owning this address range.
Definition: database.hh:818
bool isGlobal(void) const
Return true if this scope is global.
Definition: database.hh:672
subsorttype getSubsort(void) const
Get the sub-sort object.
Definition: database.cc:73
SymbolNameTree nametree
The set of Symbol objects, sorted by name.
Definition: database.hh:730
vector< list< SymbolEntry >::iterator > mapentry
List of storage locations labeled with this Symbol.
Definition: database.hh:169
bool inRange(const Address &addr, int4 size) const
Check containment an address range.
Definition: address.cc:402
An Address range associated with the symbol Scope that owns it.
Definition: database.hh:795
A Symbol that labels code internal to a function.
Definition: database.hh:281
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
MapIterator(void)
Construct an uninitialized iterator.
Definition: database.hh:335
Database(Architecture *g)
Constructor.
Definition: database.hh:849
const Address & getRefAddr(void) const
Return the placeholder address.
Definition: database.hh:303
EntrySubsort(void)
Construct earliest possible sub-sort.
Definition: database.hh:90
Symbol(Scope *sc, const string &nm, Datatype *ct)
Construct given a name and data-type.
Definition: database.hh:187
EntrySubsort subsorttype
The sub-sort object for a rangemap.
Definition: database.hh:112
uint4 uniqueId
Unique id for the scope, for deduping scope names, assigning symbol ids.
Definition: database.hh:427
void initialize(const inittype &data, const Address &f, const Address &l)
Initialize the range (with the owning Scope)
Definition: database.hh:819
AddrSpace * getSpace(void) const
Get the address space.
Definition: address.hh:294
List::const_iterator restoreXml(List::const_iterator iter, const AddrSpaceManager *manage)
Restore this from an XML stream.
Definition: database.cc:195
Is this Varnode storing a pointer to the actual symbol.
Definition: varnode.hh:105
bool hasMergeProblems(void) const
Were some SymbolEntrys not merged.
Definition: database.hh:207
High-level variable is tied to address.
Definition: varnode.hh:92
void setSymbolId(Symbol *sym, uint8 id) const
Adjust the id associated with a symbol.
Definition: database.hh:497
The partmap<> template mapping a linear space to value objects.
Manager for all the major decompiler subsystems.
Definition: architecture.hh:117
const RangeList & getUseLimit(void) const
Get the set of valid code addresses for this storage.
Definition: database.hh:131
bool operator==(const MapIterator &op2) const
Equality operator.
Definition: database.hh:365
The Name of the Varnode is locked.
Definition: varnode.hh:83
A low-level variable or contiguous set of bytes described by an Address and a size.
Definition: varnode.hh:65
set< Symbol * >::const_iterator endMultiEntry(void) const
End of symbols with more than one entry.
Definition: database.hh:785
void setUseLimit(const RangeList &uselim)
Set the range of code addresses where this is valid.
Definition: database.hh:133
bool updateType(Varnode *vn) const
Update a Varnode data-type from this.
Definition: database.cc:111
uint8 symbolId
Unique id, 0=unassigned.
Definition: database.hh:168
bool isInvalid(void) const
Is the address invalid?
Definition: address.hh:256
string name
Name of this scope.
Definition: database.hh:425
An XML element. A node in the DOM tree.
Definition: xml.hh:150
Address getFirstUseAddress(void) const
Get the first code address where this storage is valid.
Definition: database.cc:98
virtual int4 getBytesConsumed(void) const
Get number of bytes consumed within the address->symbol map.
Definition: database.hh:262
uintb getOffset(void) const
Get the address offset.
Definition: address.hh:300
A key for looking up child symbol scopes within a parent, based on name.
Definition: database.hh:383
NullSubsort subsorttype
The sub-sort object for a rangemap.
Definition: database.hh:807
int2 getCategory(void) const
Get the Symbol category.
Definition: database.hh:198
void printBounds(ostream &s) const
Print a description of this RangeList to stream.
Definition: address.cc:522
virtual bool inScope(const Address &addr, int4 size, const Address &usepoint) const
Query if the given range is owned by this Scope.
Definition: database.hh:531
EntrySubsort(const EntrySubsort &op2)
Copy constructor.
Definition: database.hh:100
Does this varnode point to the return value storage location.
Definition: varnode.hh:106
void setProperties(const partmap< Address, uint4 > &newflags)
Replace the property map.
Definition: database.hh:866
A Symbol that holds equate information for a constant.
Definition: database.hh:270
Address getLast(void) const
Get the last address in the range.
Definition: database.hh:816
uintb getLast(void) const
Get the last offset of this storage location.
Definition: database.hh:123
An interval map container.
Definition: rangemap.hh:65
uintb linetype
The linear element for a rangemap of SymbolEntry.
Definition: database.hh:111
An in-memory implementation of the Scope interface.
Definition: database.hh:721
Initialization data for a SymbolEntry to facilitate a rangemap.
Definition: database.hh:67
void initialize(const EntryInitData &data, uintb a, uintb b)
Fully initialize this.
Definition: database.cc:60
uint4 wholeCount
Number of SymbolEntries that map to the whole Symbol.
Definition: database.hh:170
uint4 getDisplayFormat(void) const
Get the format to display the Symbol in.
Definition: database.hh:197
Class for sub-sorting different SymbolEntry objects at the same address.
Definition: database.hh:83
bool isIndirectStorage(void) const
Is storage really a pointer to the true Symbol.
Definition: database.hh:203
uint4 getProperty(const Address &addr) const
Get boolean properties at the given address.
Definition: database.hh:864
vector< EntryMap * > maptable
Rangemaps of SymbolEntry, one map for each address space.
Definition: database.hh:731
EntryInitData(Symbol *sym, uint4 exfl, AddrSpace *spc, int4 off, const RangeList &ul)
Constructor.
Definition: database.hh:75
Symbol * getSymbol(void) const
Get the Symbol associated with this.
Definition: database.hh:126
uintb getFirst(void) const
Get the first offset of this storage location.
Definition: database.hh:122
const string & getName(void) const
Get the local name of the symbol.
Definition: database.hh:193
bool isAddrTied(void) const
Is this storage address tied.
Definition: database.hh:243
bool operator!=(const MapIterator &op2) const
Inequality operator.
Definition: database.hh:372
MapIterator(const vector< EntryMap *> *m, vector< EntryMap *>::const_iterator cm, list< SymbolEntry >::const_iterator ci)
Construct iterator at a specific position.
Definition: database.hh:342
The base class for a symbol in a symbol table or scope.
Definition: database.hh:153
bool isTypeLocked(void) const
Is the Symbol type-locked.
Definition: database.hh:200
bool isDynamic(void) const
Is storage dynamic.
Definition: database.hh:118
Comparator for sorting Symbol objects by name.
Definition: database.hh:309
The Dataype of the Varnode is locked.
Definition: varnode.hh:82
uint8 getHash(void) const
Get the hash used to identify this storage.
Definition: database.hh:128
set< Symbol * >::const_iterator beginMultiEntry(void) const
Start of symbols with more than one entry.
Definition: database.hh:784
MapIterator & operator=(const MapIterator &op2)
Assignment operator.
Definition: database.hh:357
Address getFirst(void) const
Get the first address in the range.
Definition: database.hh:815
void setMergeProblems(void)
Mark that some SymbolEntrys could not be merged.
Definition: database.hh:208
Scope * scope
The scope that owns this symbol.
Definition: database.hh:158
bool operator<(const EntrySubsort &op2)
Compare this with another sub-sort.
Definition: database.hh:105
int4 getIndex(void) const
Get the integer identifier.
Definition: space.hh:317
NullSubsort getSubsort(void) const
Get the sub-subsort object.
Definition: database.hh:817
EntrySubsort(bool val)
Given a boolean value, construct the earliest/latest possible sub-sort.
Definition: database.hh:95
Architecture * getArch(void) const
Get the Architecture associated with this.
Definition: database.hh:700
uint2 getCategoryIndex(void) const
Get the position of the Symbol within its category.
Definition: database.hh:199
uint4 getFlags(void) const
Get the boolean properties of the Symbol.
Definition: database.hh:196
int4 getOffset(void) const
Get offset of this within the Symbol.
Definition: database.hh:121
ScopeMapper(void)
Constructor for use with rangemap.
Definition: database.hh:814
uint4 nameDedup
id to distinguish symbols with the same name
Definition: database.hh:161
const SymbolEntry * operator*(void) const
Return the SymbolEntry being pointed at.
Definition: database.hh:352
bool operator()(const Symbol *sym1, const Symbol *sym2) const
Compare two Symbol pointers.
Definition: database.hh:317
ScopeMap::const_iterator childrenEnd() const
Ending iterator of child scopes.
Definition: database.hh:690
Scope * getScope(void) const
Get the scope owning this Symbol.
Definition: database.hh:211
set< Symbol *, SymbolCompareName > SymbolNameTree
A set of Symbol objects sorted by name.
Definition: database.hh:324
ScopeKey(const string &nm, uint4 id)
Construct given a name and id.
Definition: database.hh:387
SymbolEntry * getMapEntry(int4 i) const
Return the i-th SymbolEntry for this Symbol.
Definition: database.hh:215
The base class API for emitting a high-level language.
Definition: printlanguage.hh:134
const string & getName(void) const
Get the name of the Scope.
Definition: database.hh:671
int4 numEntries(void) const
Return the number of SymbolEntrys.
Definition: database.hh:214
rangemap< ScopeMapper > ScopeResolve
A map from address to the owning Scope.
Definition: database.hh:822
uint4 flags
Varnode-like properties of the symbol.
Definition: database.hh:162
Scope * getParent(void) const
Get the parent Scope (or NULL if this is the global Scope)
Definition: database.hh:701
SymbolEntry(void)
Constructor for use with rangemap.
Definition: database.hh:115
ExternRefSymbol(Scope *sc)
For use with restoreXml.
Definition: database.hh:302
uintb getValue(void) const
Get the constant value.
Definition: database.hh:274
const partmap< Address, uint4 > & getProperties(void) const
Get the entire property map.
Definition: database.hh:867
A collection of Symbol objects within a single (namespace or functional) scope.
Definition: database.hh:413
Is this Varnode the low part of a double precision value.
Definition: varnode.hh:103