My Project
space.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_SPACE__
20 #define __CPUI_SPACE__
21 
22 #include "error.hh"
23 #include "xml.hh"
24 
28 enum spacetype {
33  IPTR_FSPEC = 4,
34  IPTR_IOP = 5,
35  IPTR_JOIN = 6
36 };
37 
38 class AddrSpace;
39 class AddrSpaceManager;
40 class VarnodeData;
41 class Translate;
42 
73 class AddrSpace {
74  friend class AddrSpaceManager; // Space container
75 public:
76  enum {
77  big_endian = 1,
78  heritaged = 2,
82  overlay = 32,
83  overlaybase = 64,
84  truncated = 128,
85  hasphysical = 256,
86  is_otherspace = 512,
88  };
89  enum {
92  };
93 private:
94  spacetype type;
95  AddrSpaceManager *manage;
96  const Translate *trans;
97  int4 refcount;
98  uint4 flags;
99  uintb highest;
100  uintb pointerLowerBound;
101  char shortcut;
102 protected:
103  string name;
104  uint4 addressSize;
105  uint4 wordsize;
107  int4 index;
108  int4 delay;
110  void calcScaleMask(void);
111  void setFlags(uint4 fl);
112  void clearFlags(uint4 fl);
113  void saveBasicAttributes(ostream &s) const;
114  void truncateSpace(uint4 newsize);
115 public:
116  AddrSpace(AddrSpaceManager *m,const Translate *t,spacetype tp,const string &nm,uint4 size,uint4 ws,int4 ind,uint4 fl,int4 dl);
118  virtual ~AddrSpace(void) {}
119  const string &getName(void) const;
120  AddrSpaceManager *getManager(void) const;
121  const Translate *getTrans(void) const;
122  spacetype getType(void) const;
123  int4 getDelay(void) const;
124  int4 getDeadcodeDelay(void) const;
125  int4 getIndex(void) const;
126  uint4 getWordSize(void) const;
127  uint4 getAddrSize(void) const;
128  uintb getHighest(void) const;
129  uintb getPointerLowerBound(void) const;
130  int4 getMinimumPtrSize(void) const;
131  uintb wrapOffset(uintb off) const;
132  char getShortcut(void) const;
133  bool isHeritaged(void) const;
134  bool doesDeadcode(void) const;
135  bool hasPhysical(void) const;
136  bool isBigEndian(void) const;
137  bool isReverseJustified(void) const;
138  bool isOverlay(void) const;
139  bool isOverlayBase(void) const;
140  bool isOtherSpace(void) const;
141  bool isTruncated(void) const;
142  bool hasNearPointers(void) const;
143  void printOffset(ostream &s,uintb offset) const;
144 
145  virtual int4 numSpacebase(void) const;
146  virtual const VarnodeData &getSpacebase(int4 i) const;
147  virtual const VarnodeData &getSpacebaseFull(int4 i) const;
148  virtual bool stackGrowsNegative(void) const;
149  virtual AddrSpace *getContain(void) const;
150  virtual void saveXmlAttributes(ostream &s,uintb offset) const;
151  virtual void saveXmlAttributes(ostream &s,uintb offset,int4 size) const;
152  virtual uintb restoreXmlAttributes(const Element *el,uint4 &size) const;
153  virtual void printRaw(ostream &s,uintb offset) const;
154  virtual uintb read(const string &s,int4 &size) const;
155  virtual void saveXml(ostream &s) const;
156  virtual void restoreXml(const Element *el);
157 
158  static uintb addressToByte(uintb val,uint4 ws);
159  static uintb byteToAddress(uintb val,uint4 ws);
160  static int4 addressToByteInt(int4 val,uint4 ws);
161  static int4 byteToAddressInt(int4 val,uint4 ws);
162  static bool compareByIndex(const AddrSpace *a,const AddrSpace *b);
163 };
164 
177 class ConstantSpace : public AddrSpace {
178 public:
179  ConstantSpace(AddrSpaceManager *m,const Translate *t,const string &nm,int4 ind);
180  virtual void printRaw(ostream &s,uintb offset) const;
181  virtual void saveXml(ostream &s) const;
182  virtual void restoreXml(const Element *el);
183 };
184 
186 class OtherSpace : public AddrSpace {
187 public:
188  OtherSpace(AddrSpaceManager *m, const Translate *t, const string &nm, int4 ind);
189  OtherSpace(AddrSpaceManager *m, const Translate *t);
190  virtual void printRaw(ostream &s, uintb offset) const;
191  virtual void saveXml(ostream &s) const;
192 };
193 
203 class UniqueSpace : public AddrSpace {
204 public:
205  UniqueSpace(AddrSpaceManager *m,const Translate *t,const string &nm,int4 ind,uint4 fl);
206  UniqueSpace(AddrSpaceManager *m,const Translate *t);
207  virtual void saveXml(ostream &s) const;
208 };
209 
218 class JoinSpace : public AddrSpace {
219 public:
220  JoinSpace(AddrSpaceManager *m,const Translate *t,const string &nm,int4 ind);
221  virtual void saveXmlAttributes(ostream &s,uintb offset) const;
222  virtual void saveXmlAttributes(ostream &s,uintb offset,int4 size) const;
223  virtual uintb restoreXmlAttributes(const Element *el,uint4 &size) const;
224  virtual void printRaw(ostream &s,uintb offset) const;
225  virtual uintb read(const string &s,int4 &size) const;
226  virtual void saveXml(ostream &s) const;
227  virtual void restoreXml(const Element *el);
228 };
229 
238 class OverlaySpace : public AddrSpace {
239  AddrSpace *baseSpace;
240 public:
242  AddrSpace *getBaseSpace(void) const;
243  virtual void saveXml(ostream &s) const;
244  virtual void restoreXml(const Element *el);
245 };
246 
249 inline void AddrSpace::setFlags(uint4 fl) {
250  flags |= fl;
251 }
252 
255 inline void AddrSpace::clearFlags(uint4 fl) {
256  flags &= ~fl;
257 }
258 
262 inline const string &AddrSpace::getName(void) const {
263  return name;
264 }
265 
270  return manage;
271 }
272 
277 inline const Translate *AddrSpace::getTrans(void) const {
278  return trans;
279 }
280 
289 inline spacetype AddrSpace::getType(void) const {
290  return type;
291 }
292 
300 inline int4 AddrSpace::getDelay(void) const {
301  return delay;
302 }
303 
310 inline int4 AddrSpace::getDeadcodeDelay(void) const {
311  return deadcodedelay;
312 }
313 
317 inline int4 AddrSpace::getIndex(void) const {
318  return index;
319 }
320 
325 inline uint4 AddrSpace::getWordSize(void) const {
326  return wordsize;
327 }
328 
333 inline uint4 AddrSpace::getAddrSize(void) const {
334  return addressSize;
335 }
336 
339 inline uintb AddrSpace::getHighest(void) const {
340  return highest;
341 }
342 
346 inline uintb AddrSpace::getPointerLowerBound(void) const {
347  return pointerLowerBound;
348 }
349 
352 inline int4 AddrSpace::getMinimumPtrSize(void) const {
353  return minimumPointerSize;
354 }
355 
361 inline uintb AddrSpace::wrapOffset(uintb off) const {
362  if (off <= highest) // Comparison is unsigned
363  return off;
364  intb mod = (intb)(highest+1);
365  intb res = (intb)off % mod; // remainder is signed
366  if (res<0) // Remainder may be negative
367  res += mod; // Adding mod guarantees res is in (0,mod)
368  return (uintb)res;
369 }
370 
375 inline char AddrSpace::getShortcut(void) const {
376  return shortcut;
377 }
378 
385 inline bool AddrSpace::isHeritaged(void) const {
386  return ((flags & heritaged)!=0);
387 }
388 
393 inline bool AddrSpace::doesDeadcode(void) const {
394  return ((flags & does_deadcode)!=0);
395 }
396 
401 inline bool AddrSpace::hasPhysical(void) const {
402  return ((flags & hasphysical) !=0);
403 }
404 
408 inline bool AddrSpace::isBigEndian(void) const {
409  return ((flags&big_endian)!=0);
410 }
411 
417 inline bool AddrSpace::isReverseJustified(void) const {
418  return ((flags&reverse_justification)!=0);
419 }
420 
421 inline bool AddrSpace::isOverlay(void) const {
422  return ((flags&overlay)!=0);
423 }
424 
425 inline bool AddrSpace::isOverlayBase(void) const {
426  return ((flags&overlaybase)!=0);
427 }
428 
429 inline bool AddrSpace::isOtherSpace(void) const {
430  return ((flags&is_otherspace)!=0);
431 }
432 
435 inline bool AddrSpace::isTruncated(void) const {
436  return ((flags&truncated)!=0);
437 }
438 
439 inline bool AddrSpace::hasNearPointers(void) const {
440  return ((flags&has_nearpointers)!=0);
441 }
442 
447 inline int4 AddrSpace::numSpacebase(void) const {
448  return 0;
449 }
450 
455 inline const VarnodeData &AddrSpace::getSpacebase(int4 i) const {
456  throw LowlevelError(name+" space is not virtual and has no associated base register");
457 }
458 
463 inline const VarnodeData &AddrSpace::getSpacebaseFull(int4 i) const {
464  throw LowlevelError(name+" has no truncated registers");
465 }
466 
470 inline bool AddrSpace::stackGrowsNegative(void) const {
471  return true;
472 }
473 
478 inline AddrSpace *AddrSpace::getContain(void) const {
479  return (AddrSpace *)0;
480 }
481 
487 inline uintb AddrSpace::addressToByte(uintb val,uint4 ws) {
488  return val*ws;
489 }
490 
496 inline uintb AddrSpace::byteToAddress(uintb val,uint4 ws) {
497  return val/ws;
498 }
499 
505 inline int4 AddrSpace::addressToByteInt(int4 val,uint4 ws) {
506  return val*ws;
507 }
508 
514 inline int4 AddrSpace::byteToAddressInt(int4 val,uint4 ws) {
515  return val/ws;
516 }
517 
522 inline bool AddrSpace::compareByIndex(const AddrSpace *a,const AddrSpace *b) {
523  return (a->index < b->index);
524 }
525 
526 #endif
Space is truncated from its original size, expect pointers larger than this size. ...
Definition: space.hh:84
void setFlags(uint4 fl)
Set a cached attribute.
Definition: space.hh:249
A region where processor data is stored.
Definition: space.hh:73
virtual void saveXml(ostream &s) const
Write the details of this space as XML.
Definition: space.cc:285
const Translate * getTrans(void) const
Get the processor translator.
Definition: space.hh:277
static uintb addressToByte(uintb val, uint4 ws)
Scale from addressable units to byte units.
Definition: space.hh:487
bool isHeritaged(void) const
Return true if dataflow has been traced.
Definition: space.hh:385
virtual uintb read(const string &s, int4 &size) const
Read in an address (and possible size) from a string.
Definition: space.cc:237
Reserved index for the other space.
Definition: space.hh:91
A manager for different address spaces.
Definition: translate.hh:218
Justification within aligned word is opposite of endianness.
Definition: space.hh:81
static int4 addressToByteInt(int4 val, uint4 ws)
Scale int4 from addressable units to byte units.
Definition: space.hh:505
The pool of logically joined variables.
Definition: space.hh:218
uint4 addressSize
Size of an address into this space in bytes.
Definition: space.hh:104
bool isOverlay(void) const
Return true if this is an overlay space.
Definition: space.hh:421
Has physical memory associated with it.
Definition: space.hh:85
spacetype
Fundemental address space types.
Definition: space.hh:28
Does there exist near pointers into this space.
Definition: space.hh:87
const string & getName(void) const
Get the name.
Definition: space.hh:262
virtual bool stackGrowsNegative(void) const
Return true if a stack in this space grows negative.
Definition: space.hh:470
Special space to represent constants.
Definition: space.hh:29
This is the base space for overlay space(s)
Definition: space.hh:83
static uintb byteToAddress(uintb val, uint4 ws)
Scale from byte units to addressable units.
Definition: space.hh:496
bool hasPhysical(void) const
Return true if data is physically stored in this.
Definition: space.hh:401
Quick check for the OtherSpace derived class.
Definition: space.hh:86
static int4 byteToAddressInt(int4 val, uint4 ws)
Scale int4 from byte units to addressable units.
Definition: space.hh:514
AddrSpace(AddrSpaceManager *m, const Translate *t, spacetype tp, const string &nm, uint4 size, uint4 ws, int4 ind, uint4 fl, int4 dl)
Definition: space.cc:39
addresses = offsets off of base register
Definition: space.hh:31
void clearFlags(uint4 fl)
Clear a cached attribute.
Definition: space.hh:255
Dead-code analysis is done on this space.
Definition: space.hh:79
virtual void printRaw(ostream &s, uintb offset) const
Write an address in this space to a stream.
Definition: space.cc:188
bool isTruncated(void) const
Return true if this space is truncated from its original size.
Definition: space.hh:435
This space is an overlay of another space.
Definition: space.hh:82
virtual int4 numSpacebase(void) const
Number of base registers associated with this space.
Definition: space.hh:447
int4 delay
Delay in heritaging this space.
Definition: space.hh:108
virtual const VarnodeData & getSpacebase(int4 i) const
Get a base register that creates this virtual space.
Definition: space.hh:455
Lightweight (and incomplete) XML parser for marshaling data to and from the decompiler.
spacetype getType(void) const
Get the type of space.
Definition: space.hh:289
Special internal PcodeOp reference.
Definition: space.hh:34
bool isOtherSpace(void) const
Return true if this is the other address space.
Definition: space.hh:429
bool hasNearPointers(void) const
Return true if near (truncated) pointers into this space are possible.
Definition: space.hh:439
Special internal FuncCallSpecs reference.
Definition: space.hh:33
Normal spaces modelled by processor.
Definition: space.hh:30
bool doesDeadcode(void) const
Return true if dead code analysis should be done on this space.
Definition: space.hh:393
Special AddrSpace for representing constants during analysis.
Definition: space.hh:177
uint4 wordsize
Size of unit being addressed (1=byte)
Definition: space.hh:105
The interface to a translation engine for a processor.
Definition: translate.hh:293
string name
Name of this space.
Definition: space.hh:103
An overlay space.
Definition: space.hh:238
uintb wrapOffset(uintb off) const
Wrap -off- to the offset that fits into this space.
Definition: space.hh:361
bool isReverseJustified(void) const
Return true if alignment justification does not match endianness.
Definition: space.hh:417
virtual ~AddrSpace(void)
The address space destructor.
Definition: space.hh:118
void truncateSpace(uint4 newsize)
Definition: space.cc:104
Special AddrSpace for special/user-defined address spaces.
Definition: space.hh:186
void calcScaleMask(void)
Calculate scale and mask.
Definition: space.cc:21
The lowest level error generated by the decompiler.
Definition: error.hh:44
Space is big endian if set, little endian otherwise.
Definition: space.hh:77
int4 getDelay(void) const
Get number of heritage passes being delayed.
Definition: space.hh:300
An XML element. A node in the DOM tree.
Definition: xml.hh:150
Space is specific to a particular loadimage.
Definition: space.hh:80
void printOffset(ostream &s, uintb offset) const
Write an address offset to a stream.
Definition: space.cc:176
Base class for error handling facilities.
uint4 getAddrSize(void) const
Get the size of the space.
Definition: space.hh:333
virtual AddrSpace * getContain(void) const
Return this space&#39;s containing space (if any)
Definition: space.hh:478
Internally managed temporary space.
Definition: space.hh:32
bool isOverlayBase(void) const
Return true if other spaces overlay this space.
Definition: space.hh:425
uintb getPointerLowerBound(void) const
Get lower bound for assuming an offset is a pointer.
Definition: space.hh:346
int4 getMinimumPtrSize(void) const
Get the minimum pointer size for this space.
Definition: space.hh:352
int4 index
An integer identifier for the space.
Definition: space.hh:107
int4 getDeadcodeDelay(void) const
Get number of passes before deadcode removal is allowed.
Definition: space.hh:310
virtual uintb restoreXmlAttributes(const Element *el, uint4 &size) const
Recover an offset and size.
Definition: space.cc:148
uintb getHighest(void) const
Get the highest byte-scaled address.
Definition: space.hh:339
The pool of temporary storage registers.
Definition: space.hh:203
uint4 getWordSize(void) const
Get the addressable unit size.
Definition: space.hh:325
virtual void saveXmlAttributes(ostream &s, uintb offset) const
Save an address as XML.
Definition: space.cc:118
virtual const VarnodeData & getSpacebaseFull(int4 i) const
Return original spacebase register before truncation.
Definition: space.hh:463
Reserved index for the constant space.
Definition: space.hh:90
virtual void restoreXml(const Element *el)
Recover the details of this space from XML.
Definition: space.cc:297
char getShortcut(void) const
Get the shortcut character.
Definition: space.hh:375
int4 getIndex(void) const
Get the integer identifier.
Definition: space.hh:317
bool isBigEndian(void) const
Return true if values in this space are big endian.
Definition: space.hh:408
AddrSpaceManager * getManager(void) const
Get the space manager.
Definition: space.hh:269
static bool compareByIndex(const AddrSpace *a, const AddrSpace *b)
Compare two spaces by their index.
Definition: space.hh:522
void saveBasicAttributes(ostream &s) const
Write the XML attributes of this space.
Definition: space.cc:87
int4 deadcodedelay
Delay before deadcode removal is allowed on this space.
Definition: space.hh:109
Special virtual space to represent split variables.
Definition: space.hh:35
This space is heritaged.
Definition: space.hh:78
int4 minimumPointerSize
Smallest size of a pointer into this space (in bytes)
Definition: space.hh:106
Data defining a specific memory location.
Definition: pcoderaw.hh:33