My Project
varmap.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 __CPUI_VARMAP__
20 #define __CPUI_VARMAP__
21 
22 #include "database.hh"
23 
30  Address addr;
31  Address useaddr;
32  int4 size;
33  string name;
34  uint8 symbolId;
35 public:
36  NameRecommend(const Address &ad,const Address &use,int4 sz,const string &nm,uint8 id) :
37  addr(ad), useaddr(use), size(sz), name(nm), symbolId(id) {}
38  const Address &getAddr(void) const { return addr; }
39  const Address &getUseAddr(void) const { return useaddr; }
40  int4 getSize(void) const { return size; }
41  string getName(void) const { return name; }
42  uint8 getSymbolId(void) const { return symbolId; }
43 };
44 
50  Address usePoint;
51  uint8 hash;
52  string name;
53  uint8 symbolId;
54 public:
55  DynamicRecommend(const Address &addr,uint8 h,const string &nm,uint8 id) :
56  usePoint(addr), hash(h), name(nm), symbolId(id) {}
57  const Address &getAddress(void) const { return usePoint; }
58  uint8 getHash(void) const { return hash; }
59  string getName(void) const { return name; }
60  uint8 getSymbolId(void) const { return symbolId; }
61 };
62 
69 class RangeHint {
70  friend class MapState;
71  friend class ScopeLocal;
72 public:
74  enum RangeType {
75  fixed = 0,
76  open = 1,
77  endpoint = 2
78  };
79 private:
80  uintb start;
81  int4 size;
82  intb sstart;
83  Datatype *type;
84  uint4 flags;
85  RangeType rangeType;
86  int4 highind;
87 public:
88  RangeHint(void) {}
89  RangeHint(uintb st,int4 sz,intb sst,Datatype *ct,uint4 fl,RangeType rt,int4 hi) {
90  start=st; size=sz; sstart=sst; type=ct; flags=fl; rangeType = rt; highind=hi; }
91  bool reconcile(const RangeHint *b) const;
92  bool contain(const RangeHint *b) const;
93  bool preferred(const RangeHint *b,bool reconcile) const;
94  bool absorb(RangeHint *b);
95  bool merge(RangeHint *b,AddrSpace *space,TypeFactory *typeFactory);
96  int4 compare(const RangeHint &op2) const;
97  static bool compareRanges(const RangeHint *a,const RangeHint *b) { return (a->compare(*b) < 0); }
98 };
99 
100 class ProtoModel;
101 class LoadGuard;
102 
109 public:
111  struct AddBase {
114  AddBase(Varnode *b,Varnode *i) { base=b; index=i; }
115  };
116 private:
117  const Funcdata *fd;
118  AddrSpace *space;
119  mutable vector<AddBase> addBase;
120  mutable vector<uintb> alias;
121  mutable bool calculated;
122  uintb localExtreme;
123  uintb localBoundary;
124  mutable uintb aliasBoundary;
125  int4 direction;
126  void deriveBoundaries(const FuncProto &proto);
127  void gatherInternal(void) const;
128 public:
129  AliasChecker() { fd = (const Funcdata *)0; space = (AddrSpace *)0; calculated=false; }
130  void gather(const Funcdata *f,AddrSpace *spc,bool defer);
131  bool hasLocalAlias(Varnode *vn) const;
132  void sortAlias(void) const;
133  const vector<AddBase> &getAddBase(void) const { return addBase; }
134  const vector<uintb> &getAlias(void) const { return alias; }
135  static void gatherAdditiveBase(Varnode *startvn,vector<AddBase> &addbase);
136  static uintb gatherOffset(Varnode *vn);
137 };
138 
145 class MapState {
146  AddrSpace *spaceid;
147  RangeList range;
148  vector<RangeHint *> maplist;
149  vector<RangeHint *>::iterator iter;
150  Datatype *defaultType;
151  AliasChecker checker;
152  void addGuard(const LoadGuard &guard,OpCode opc,TypeFactory *typeFactory);
153  void addRange(uintb st,Datatype *ct,uint4 fl,RangeHint::RangeType rt,int4 hi);
154  void reconcileDatatypes(void);
155 public:
156 #ifdef OPACTION_DEBUG
157  mutable bool debugon;
158  mutable Architecture *glb;
159  void turnOnDebug(Architecture *g) const { debugon = true; glb=g; }
160  void turnOffDebug(void) const { debugon = false; }
161 #endif
162  MapState(AddrSpace *spc,const RangeList &rn,const RangeList &pm,Datatype *dt);
163  ~MapState(void);
164  bool initialize(void);
165  void sortAlias(void) { checker.sortAlias(); }
166  const vector<uintb> &getAlias(void) { return checker.getAlias(); }
167  void gatherSymbols(const EntryMap *rangemap);
168  void gatherVarnodes(const Funcdata &fd);
169  void gatherHighs(const Funcdata &fd);
170  void gatherOpen(const Funcdata &fd);
171  RangeHint *next(void) { return *iter; }
172  bool getNext(void) { ++iter; if (iter==maplist.end()) return false; return true; }
173 };
174 
182 class ScopeLocal : public ScopeInternal {
183  AddrSpace *space;
184  RangeList localRange;
185  list<NameRecommend> nameRecommend;
186  list<DynamicRecommend> dynRecommend;
187  bool stackGrowsNegative;
188  bool rangeLocked;
189  bool adjustFit(RangeHint &a) const;
190  void createEntry(const RangeHint &a);
191  bool restructure(MapState &state);
192  void markUnaliased(const vector<uintb> &alias);
193  void fakeInputSymbols(void);
194  void addRecommendName(Symbol *sym);
195  void collectNameRecs(void);
196 public:
198  virtual ~ScopeLocal(void) {}
199 
200  AddrSpace *getSpaceId(void) const { return space; }
201 
206  bool isUnaffectedStorage(Varnode *vn) const { return (vn->getSpace() == space); }
207 
208  void markNotMapped(AddrSpace *spc,uintb first,int4 sz,bool param);
209 
210  // Routines that are specific to one address space
211  virtual void saveXml(ostream &s) const;
212  virtual void restoreXml(const Element *el);
213  virtual string buildVariableName(const Address &addr,
214  const Address &pc,
215  Datatype *ct,
216  int4 &index,uint4 flags) const;
217  void resetLocalWindow(void);
218  void restructureVarnode(bool aliasyes);
219  void restructureHigh(void);
220  SymbolEntry *remapSymbol(Symbol *sym,const Address &addr,const Address &usepoint);
221  SymbolEntry *remapSymbolDynamic(Symbol *sym,uint8 hash,const Address &usepoint);
222  void recoverNameRecommendationsForSymbols(void);
223 };
224 
225 #endif
A region where processor data is stored.
Definition: space.hh:73
const Address & getAddr(void) const
Get the storage address.
Definition: varmap.hh:38
The base datatype class for the decompiler.
Definition: type.hh:62
A storage location for a particular Symbol.
Definition: database.hh:51
OpCode
The op-code defining a specific p-code operation (PcodeOp)
Definition: opcodes.hh:35
AliasChecker()
Constructor.
Definition: varmap.hh:129
Container for data structures associated with a single function.
Definition: funcdata.hh:45
Varnode * index
The index value or NULL.
Definition: varmap.hh:113
int4 getSize(void) const
Get the optional size.
Definition: varmap.hh:40
A helper class holding a Varnode pointer reference and a possible index added to it.
Definition: varmap.hh:111
const vector< uintb > & getAlias(void) const
Get the list of alias starting offsets.
Definition: varmap.hh:134
A prototype model: a model for passing parameters between functions.
Definition: fspec.hh:615
int4 compare(const RangeHint &op2) const
Order this with another RangeHint.
Definition: varmap.cc:247
string getName(void) const
Get the recommended name.
Definition: varmap.hh:41
const Address & getAddress(void) const
Get the use point address.
Definition: varmap.hh:57
const vector< uintb > & getAlias(void)
Get the list of alias starting offsets.
Definition: varmap.hh:166
void sortAlias(void) const
Sort the alias starting offsets.
Definition: varmap.cc:568
DynamicRecommend(const Address &addr, uint8 h, const string &nm, uint8 id)
Constructor.
Definition: varmap.hh:55
bool isUnaffectedStorage(Varnode *vn) const
Is this a storage location for unaffected registers.
Definition: varmap.hh:206
static bool compareRanges(const RangeHint *a, const RangeHint *b)
Compare two RangeHint pointers.
Definition: varmap.hh:97
A disjoint set of Ranges, possibly across multiple address spaces.
Definition: address.hh:203
string getName(void) const
Get the recommended name.
Definition: varmap.hh:59
bool getNext(void)
Advance the iterator, return true if another hint is available.
Definition: varmap.hh:172
RangeHint(void)
Uninitialized constructor.
Definition: varmap.hh:88
RangeHint(uintb st, int4 sz, intb sst, Datatype *ct, uint4 fl, RangeType rt, int4 hi)
Initialized constructor.
Definition: varmap.hh:89
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
RangeType
The basic categorization of the range.
Definition: varmap.hh:74
A name recommendation for a particular dynamic storage location.
Definition: varmap.hh:49
Manager for all the major decompiler subsystems.
Definition: architecture.hh:117
Description of a LOAD operation that needs to be guarded.
Definition: heritage.hh:105
const Address & getUseAddr(void) const
Get the use point address.
Definition: varmap.hh:39
uint8 getSymbolId(void) const
Get the original Symbol id.
Definition: varmap.hh:42
A low-level variable or contiguous set of bytes described by an Address and a size.
Definition: varnode.hh:65
An XML element. A node in the DOM tree.
Definition: xml.hh:150
AddrSpace * getSpaceId(void) const
Get the associated (stack) address space.
Definition: varmap.hh:200
A light-weight class for analyzing pointers and aliasing on the stack.
Definition: varmap.hh:108
AddrSpace * getSpace(void) const
Get the AddrSpace storing this Varnode.
Definition: varnode.hh:168
An interval map container.
Definition: rangemap.hh:65
An in-memory implementation of the Scope interface.
Definition: database.hh:721
void sortAlias(void)
Sort the alias starting offsets.
Definition: varmap.hh:165
A symbol name recommendation with its associated storage location.
Definition: varmap.hh:29
Symbol and Scope objects for the decompiler.
const vector< AddBase > & getAddBase(void) const
Get the collection of pointer Varnodes.
Definition: varmap.hh:133
A function prototype.
Definition: fspec.hh:1147
Container class for all Datatype objects in an Architecture.
Definition: type.hh:380
The base class for a symbol in a symbol table or scope.
Definition: database.hh:153
RangeHint * next(void)
Get the current RangeHint in the collection.
Definition: varmap.hh:171
virtual ~ScopeLocal(void)
Destructor.
Definition: varmap.hh:198
Partial data-type information mapped to a specific range of bytes.
Definition: varmap.hh:69
AddBase(Varnode *b, Varnode *i)
Constructor.
Definition: varmap.hh:114
NameRecommend(const Address &ad, const Address &use, int4 sz, const string &nm, uint8 id)
Constructor.
Definition: varmap.hh:36
uint8 getSymbolId(void) const
Get the original Symbol id.
Definition: varmap.hh:60
Varnode * base
The Varnode holding the base pointer.
Definition: varmap.hh:112
uint8 getHash(void) const
Get the dynamic hash.
Definition: varmap.hh:58
A Symbol scope for local variables of a particular function.
Definition: varmap.hh:182
A container for hints about the data-type layout of an address space.
Definition: varmap.hh:145