Ghidra Decompiler Analysis Engine
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 struct 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  uintb pointerUpperBound;
102  char shortcut;
103 protected:
104  string name;
105  uint4 addressSize;
106  uint4 wordsize;
108  int4 index;
109  int4 delay;
111  void calcScaleMask(void);
112  void setFlags(uint4 fl);
113  void clearFlags(uint4 fl);
114  void saveBasicAttributes(ostream &s) const;
115  void truncateSpace(uint4 newsize);
116 public:
117  AddrSpace(AddrSpaceManager *m,const Translate *t,spacetype tp,const string &nm,uint4 size,uint4 ws,int4 ind,uint4 fl,int4 dl);
119  virtual ~AddrSpace(void) {}
120  const string &getName(void) const;
121  AddrSpaceManager *getManager(void) const;
122  const Translate *getTrans(void) const;
123  spacetype getType(void) const;
124  int4 getDelay(void) const;
125  int4 getDeadcodeDelay(void) const;
126  int4 getIndex(void) const;
127  uint4 getWordSize(void) const;
128  uint4 getAddrSize(void) const;
129  uintb getHighest(void) const;
130  uintb getPointerLowerBound(void) const;
131  uintb getPointerUpperBound(void) const;
132  int4 getMinimumPtrSize(void) const;
133  uintb wrapOffset(uintb off) const;
134  char getShortcut(void) const;
135  bool isHeritaged(void) const;
136  bool doesDeadcode(void) const;
137  bool hasPhysical(void) const;
138  bool isBigEndian(void) const;
139  bool isReverseJustified(void) const;
140  bool isOverlay(void) const;
141  bool isOverlayBase(void) const;
142  bool isOtherSpace(void) const;
143  bool isTruncated(void) const;
144  bool hasNearPointers(void) const;
145  void printOffset(ostream &s,uintb offset) const;
146 
147  virtual int4 numSpacebase(void) const;
148  virtual const VarnodeData &getSpacebase(int4 i) const;
149  virtual const VarnodeData &getSpacebaseFull(int4 i) const;
150  virtual bool stackGrowsNegative(void) const;
151  virtual AddrSpace *getContain(void) const;
152  virtual void saveXmlAttributes(ostream &s,uintb offset) const;
153  virtual void saveXmlAttributes(ostream &s,uintb offset,int4 size) const;
154  virtual uintb restoreXmlAttributes(const Element *el,uint4 &size) const;
155  virtual void printRaw(ostream &s,uintb offset) const;
156  virtual uintb read(const string &s,int4 &size) const;
157  virtual void saveXml(ostream &s) const;
158  virtual void restoreXml(const Element *el);
159 
160  static uintb addressToByte(uintb val,uint4 ws);
161  static uintb byteToAddress(uintb val,uint4 ws);
162  static int4 addressToByteInt(int4 val,uint4 ws);
163  static int4 byteToAddressInt(int4 val,uint4 ws);
164  static bool compareByIndex(const AddrSpace *a,const AddrSpace *b);
165 };
166 
179 class ConstantSpace : public AddrSpace {
180 public:
181  ConstantSpace(AddrSpaceManager *m,const Translate *t,const string &nm,int4 ind);
182  virtual void printRaw(ostream &s,uintb offset) const;
183  virtual void saveXml(ostream &s) const;
184  virtual void restoreXml(const Element *el);
185 };
186 
188 class OtherSpace : public AddrSpace {
189 public:
190  OtherSpace(AddrSpaceManager *m, const Translate *t, const string &nm, int4 ind);
191  OtherSpace(AddrSpaceManager *m, const Translate *t);
192  virtual void printRaw(ostream &s, uintb offset) const;
193  virtual void saveXml(ostream &s) const;
194 };
195 
205 class UniqueSpace : public AddrSpace {
206 public:
207  UniqueSpace(AddrSpaceManager *m,const Translate *t,const string &nm,int4 ind,uint4 fl);
208  UniqueSpace(AddrSpaceManager *m,const Translate *t);
209  virtual void saveXml(ostream &s) const;
210 };
211 
220 class JoinSpace : public AddrSpace {
221 public:
222  JoinSpace(AddrSpaceManager *m,const Translate *t,const string &nm,int4 ind);
223  virtual void saveXmlAttributes(ostream &s,uintb offset) const;
224  virtual void saveXmlAttributes(ostream &s,uintb offset,int4 size) const;
225  virtual uintb restoreXmlAttributes(const Element *el,uint4 &size) const;
226  virtual void printRaw(ostream &s,uintb offset) const;
227  virtual uintb read(const string &s,int4 &size) const;
228  virtual void saveXml(ostream &s) const;
229  virtual void restoreXml(const Element *el);
230 };
231 
240 class OverlaySpace : public AddrSpace {
241  AddrSpace *baseSpace;
242 public:
244  AddrSpace *getBaseSpace(void) const;
245  virtual void saveXml(ostream &s) const;
246  virtual void restoreXml(const Element *el);
247 };
248 
251 inline void AddrSpace::setFlags(uint4 fl) {
252  flags |= fl;
253 }
254 
257 inline void AddrSpace::clearFlags(uint4 fl) {
258  flags &= ~fl;
259 }
260 
264 inline const string &AddrSpace::getName(void) const {
265  return name;
266 }
267 
272  return manage;
273 }
274 
279 inline const Translate *AddrSpace::getTrans(void) const {
280  return trans;
281 }
282 
291 inline spacetype AddrSpace::getType(void) const {
292  return type;
293 }
294 
302 inline int4 AddrSpace::getDelay(void) const {
303  return delay;
304 }
305 
312 inline int4 AddrSpace::getDeadcodeDelay(void) const {
313  return deadcodedelay;
314 }
315 
319 inline int4 AddrSpace::getIndex(void) const {
320  return index;
321 }
322 
327 inline uint4 AddrSpace::getWordSize(void) const {
328  return wordsize;
329 }
330 
335 inline uint4 AddrSpace::getAddrSize(void) const {
336  return addressSize;
337 }
338 
341 inline uintb AddrSpace::getHighest(void) const {
342  return highest;
343 }
344 
348 inline uintb AddrSpace::getPointerLowerBound(void) const {
349  return pointerLowerBound;
350 }
351 
355 inline uintb AddrSpace::getPointerUpperBound(void) const {
356  return pointerUpperBound;
357 }
358 
361 inline int4 AddrSpace::getMinimumPtrSize(void) const {
362  return minimumPointerSize;
363 }
364 
370 inline uintb AddrSpace::wrapOffset(uintb off) const {
371  if (off <= highest) // Comparison is unsigned
372  return off;
373  intb mod = (intb)(highest+1);
374  intb res = (intb)off % mod; // remainder is signed
375  if (res<0) // Remainder may be negative
376  res += mod; // Adding mod guarantees res is in (0,mod)
377  return (uintb)res;
378 }
379 
384 inline char AddrSpace::getShortcut(void) const {
385  return shortcut;
386 }
387 
394 inline bool AddrSpace::isHeritaged(void) const {
395  return ((flags & heritaged)!=0);
396 }
397 
402 inline bool AddrSpace::doesDeadcode(void) const {
403  return ((flags & does_deadcode)!=0);
404 }
405 
410 inline bool AddrSpace::hasPhysical(void) const {
411  return ((flags & hasphysical) !=0);
412 }
413 
417 inline bool AddrSpace::isBigEndian(void) const {
418  return ((flags&big_endian)!=0);
419 }
420 
426 inline bool AddrSpace::isReverseJustified(void) const {
427  return ((flags&reverse_justification)!=0);
428 }
429 
430 inline bool AddrSpace::isOverlay(void) const {
431  return ((flags&overlay)!=0);
432 }
433 
434 inline bool AddrSpace::isOverlayBase(void) const {
435  return ((flags&overlaybase)!=0);
436 }
437 
438 inline bool AddrSpace::isOtherSpace(void) const {
439  return ((flags&is_otherspace)!=0);
440 }
441 
444 inline bool AddrSpace::isTruncated(void) const {
445  return ((flags&truncated)!=0);
446 }
447 
448 inline bool AddrSpace::hasNearPointers(void) const {
449  return ((flags&has_nearpointers)!=0);
450 }
451 
456 inline int4 AddrSpace::numSpacebase(void) const {
457  return 0;
458 }
459 
464 inline const VarnodeData &AddrSpace::getSpacebase(int4 i) const {
465  throw LowlevelError(name+" space is not virtual and has no associated base register");
466 }
467 
472 inline const VarnodeData &AddrSpace::getSpacebaseFull(int4 i) const {
473  throw LowlevelError(name+" has no truncated registers");
474 }
475 
479 inline bool AddrSpace::stackGrowsNegative(void) const {
480  return true;
481 }
482 
487 inline AddrSpace *AddrSpace::getContain(void) const {
488  return (AddrSpace *)0;
489 }
490 
496 inline uintb AddrSpace::addressToByte(uintb val,uint4 ws) {
497  return val*ws;
498 }
499 
505 inline uintb AddrSpace::byteToAddress(uintb val,uint4 ws) {
506  return val/ws;
507 }
508 
514 inline int4 AddrSpace::addressToByteInt(int4 val,uint4 ws) {
515  return val*ws;
516 }
517 
523 inline int4 AddrSpace::byteToAddressInt(int4 val,uint4 ws) {
524  return val/ws;
525 }
526 
531 inline bool AddrSpace::compareByIndex(const AddrSpace *a,const AddrSpace *b) {
532  return (a->index < b->index);
533 }
534 
535 #endif
AddrSpace::AddrSpace
AddrSpace(AddrSpaceManager *m, const Translate *t, spacetype tp, const string &nm, uint4 size, uint4 ws, int4 ind, uint4 fl, int4 dl)
Definition: space.cc:40
AddrSpace::reverse_justification
@ reverse_justification
Justification within aligned word is opposite of endianness.
Definition: space.hh:81
AddrSpace::big_endian
@ big_endian
Space is big endian if set, little endian otherwise.
Definition: space.hh:77
AddrSpace::index
int4 index
An integer identifier for the space.
Definition: space.hh:108
AddrSpace::getDeadcodeDelay
int4 getDeadcodeDelay(void) const
Get number of passes before deadcode removal is allowed.
Definition: space.hh:312
AddrSpace::compareByIndex
static bool compareByIndex(const AddrSpace *a, const AddrSpace *b)
Compare two spaces by their index.
Definition: space.hh:531
AddrSpace::byteToAddress
static uintb byteToAddress(uintb val, uint4 ws)
Scale from byte units to addressable units.
Definition: space.hh:505
AddrSpace::getHighest
uintb getHighest(void) const
Get the highest byte-scaled address.
Definition: space.hh:341
OtherSpace::saveXml
virtual void saveXml(ostream &s) const
Write the details of this space as XML.
Definition: space.cc:421
AddrSpace::printRaw
virtual void printRaw(ostream &s, uintb offset) const
Write an address in this space to a stream.
Definition: space.cc:189
OverlaySpace::getBaseSpace
AddrSpace * getBaseSpace(void) const
Get the address space being overlayed.
Definition: space.cc:657
JoinSpace::saveXml
virtual void saveXml(ostream &s) const
Write the details of this space as XML.
Definition: space.cc:635
AddrSpace::name
string name
Name of this space.
Definition: space.hh:104
AddrSpace
A region where processor data is stored.
Definition: space.hh:73
AddrSpace::getWordSize
uint4 getWordSize(void) const
Get the addressable unit size.
Definition: space.hh:327
AddrSpace::addressSize
uint4 addressSize
Size of an address into this space in bytes.
Definition: space.hh:105
AddrSpace::getMinimumPtrSize
int4 getMinimumPtrSize(void) const
Get the minimum pointer size for this space.
Definition: space.hh:361
OverlaySpace::OverlaySpace
OverlaySpace(AddrSpaceManager *m, const Translate *t)
Constructor.
Definition: space.cc:649
AddrSpace::delay
int4 delay
Delay in heritaging this space.
Definition: space.hh:109
AddrSpace::getSpacebase
virtual const VarnodeData & getSpacebase(int4 i) const
Get a base register that creates this virtual space.
Definition: space.hh:464
LowlevelError
The lowest level error generated by the decompiler.
Definition: error.hh:44
AddrSpace::programspecific
@ programspecific
Space is specific to a particular loadimage.
Definition: space.hh:80
AddrSpace::getAddrSize
uint4 getAddrSize(void) const
Get the size of the space.
Definition: space.hh:335
AddrSpace::setFlags
void setFlags(uint4 fl)
Set a cached attribute.
Definition: space.hh:251
OtherSpace
Special AddrSpace for special/user-defined address spaces.
Definition: space.hh:188
OverlaySpace
An overlay space.
Definition: space.hh:240
error.hh
Base class for error handling facilities.
AddrSpace::isOtherSpace
bool isOtherSpace(void) const
Return true if this is the other address space.
Definition: space.hh:438
AddrSpace::read
virtual uintb read(const string &s, int4 &size) const
Read in an address (and possible size) from a string.
Definition: space.cc:238
AddrSpace::getShortcut
char getShortcut(void) const
Get the shortcut character.
Definition: space.hh:384
IPTR_SPACEBASE
@ IPTR_SPACEBASE
addresses = offsets off of base register
Definition: space.hh:31
AddrSpace::getPointerUpperBound
uintb getPointerUpperBound(void) const
Get upper bound for assuming an offset is a pointer.
Definition: space.hh:355
AddrSpace::does_deadcode
@ does_deadcode
Dead-code analysis is done on this space.
Definition: space.hh:79
AddrSpace::heritaged
@ heritaged
This space is heritaged.
Definition: space.hh:78
AddrSpace::truncated
@ truncated
Space is truncated from its original size, expect pointers larger than this size.
Definition: space.hh:84
AddrSpace::other_space_index
@ other_space_index
Reserved index for the other space.
Definition: space.hh:91
IPTR_FSPEC
@ IPTR_FSPEC
Special internal FuncCallSpecs reference.
Definition: space.hh:33
AddrSpace::isOverlayBase
bool isOverlayBase(void) const
Return true if other spaces overlay this space.
Definition: space.hh:434
AddrSpace::has_nearpointers
@ has_nearpointers
Does there exist near pointers into this space.
Definition: space.hh:87
AddrSpace::wordsize
uint4 wordsize
Size of unit being addressed (1=byte)
Definition: space.hh:106
IPTR_INTERNAL
@ IPTR_INTERNAL
Internally managed temporary space.
Definition: space.hh:32
UniqueSpace
The pool of temporary storage registers.
Definition: space.hh:205
AddrSpace::overlaybase
@ overlaybase
This is the base space for overlay space(s)
Definition: space.hh:83
AddrSpace::getType
spacetype getType(void) const
Get the type of space.
Definition: space.hh:291
AddrSpace::truncateSpace
void truncateSpace(uint4 newsize)
Definition: space.cc:105
OtherSpace::printRaw
virtual void printRaw(ostream &s, uintb offset) const
Write an address in this space to a stream.
Definition: space.cc:415
AddrSpace::saveXml
virtual void saveXml(ostream &s) const
Write the details of this space as XML.
Definition: space.cc:286
Element
An XML element. A node in the DOM tree.
Definition: xml.hh:150
AddrSpace::doesDeadcode
bool doesDeadcode(void) const
Return true if dead code analysis should be done on this space.
Definition: space.hh:402
AddrSpace::getSpacebaseFull
virtual const VarnodeData & getSpacebaseFull(int4 i) const
Return original spacebase register before truncation.
Definition: space.hh:472
IPTR_JOIN
@ IPTR_JOIN
Special virtual space to represent split variables.
Definition: space.hh:35
OverlaySpace::restoreXml
virtual void restoreXml(const Element *el)
Recover the details of this space from XML.
Definition: space.cc:673
AddrSpace::hasphysical
@ hasphysical
Has physical memory associated with it.
Definition: space.hh:85
AddrSpace::minimumPointerSize
int4 minimumPointerSize
Smallest size of a pointer into this space (in bytes)
Definition: space.hh:107
JoinSpace::printRaw
virtual void printRaw(ostream &s, uintb offset) const
Write an address in this space to a stream.
Definition: space.cc:579
JoinSpace
The pool of logically joined variables.
Definition: space.hh:220
AddrSpace::getPointerLowerBound
uintb getPointerLowerBound(void) const
Get lower bound for assuming an offset is a pointer.
Definition: space.hh:348
ConstantSpace::ConstantSpace
ConstantSpace(AddrSpaceManager *m, const Translate *t, const string &nm, int4 ind)
Only constructor.
Definition: space.cc:360
IPTR_CONSTANT
@ IPTR_CONSTANT
Special space to represent constants.
Definition: space.hh:29
JoinSpace::JoinSpace
JoinSpace(AddrSpaceManager *m, const Translate *t, const string &nm, int4 ind)
Definition: space.cc:464
AddrSpace::deadcodedelay
int4 deadcodedelay
Delay before deadcode removal is allowed on this space.
Definition: space.hh:110
AddrSpace::saveXmlAttributes
virtual void saveXmlAttributes(ostream &s, uintb offset) const
Save an address as XML.
Definition: space.cc:119
AddrSpace::stackGrowsNegative
virtual bool stackGrowsNegative(void) const
Return true if a stack in this space grows negative.
Definition: space.hh:479
JoinSpace::saveXmlAttributes
virtual void saveXmlAttributes(ostream &s, uintb offset) const
Definition: space.cc:477
AddrSpace::restoreXml
virtual void restoreXml(const Element *el)
Recover the details of this space from XML.
Definition: space.cc:298
IPTR_PROCESSOR
@ IPTR_PROCESSOR
Normal spaces modelled by processor.
Definition: space.hh:30
AddrSpace::getName
const string & getName(void) const
Get the name.
Definition: space.hh:264
IPTR_IOP
@ IPTR_IOP
Special internal PcodeOp reference.
Definition: space.hh:34
AddrSpace::addressToByte
static uintb addressToByte(uintb val, uint4 ws)
Scale from addressable units to byte units.
Definition: space.hh:496
JoinSpace::restoreXmlAttributes
virtual uintb restoreXmlAttributes(const Element *el, uint4 &size) const
Definition: space.cc:530
AddrSpace::addressToByteInt
static int4 addressToByteInt(int4 val, uint4 ws)
Scale int4 from addressable units to byte units.
Definition: space.hh:514
AddrSpace::getManager
AddrSpaceManager * getManager(void) const
Get the space manager.
Definition: space.hh:271
AddrSpace::getIndex
int4 getIndex(void) const
Get the integer identifier.
Definition: space.hh:319
AddrSpace::overlay
@ overlay
This space is an overlay of another space.
Definition: space.hh:82
AddrSpace::isTruncated
bool isTruncated(void) const
Return true if this space is truncated from its original size.
Definition: space.hh:444
UniqueSpace::saveXml
virtual void saveXml(ostream &s) const
Write the details of this space as XML.
Definition: space.cc:450
UniqueSpace::UniqueSpace
UniqueSpace(AddrSpaceManager *m, const Translate *t, const string &nm, int4 ind, uint4 fl)
Constructor.
Definition: space.cc:437
AddrSpace::clearFlags
void clearFlags(uint4 fl)
Clear a cached attribute.
Definition: space.hh:257
ConstantSpace::printRaw
virtual void printRaw(ostream &s, uintb offset) const
Definition: space.cc:371
Translate
The interface to a translation engine for a processor.
Definition: translate.hh:294
AddrSpace::restoreXmlAttributes
virtual uintb restoreXmlAttributes(const Element *el, uint4 &size) const
Recover an offset and size.
Definition: space.cc:149
AddrSpace::saveBasicAttributes
void saveBasicAttributes(ostream &s) const
Write the XML attributes of this space.
Definition: space.cc:88
AddrSpace::isHeritaged
bool isHeritaged(void) const
Return true if dataflow has been traced.
Definition: space.hh:394
AddrSpace::hasNearPointers
bool hasNearPointers(void) const
Return true if near (truncated) pointers into this space are possible.
Definition: space.hh:448
JoinSpace::restoreXml
virtual void restoreXml(const Element *el)
Recover the details of this space from XML.
Definition: space.cc:641
AddrSpace::isOverlay
bool isOverlay(void) const
Return true if this is an overlay space.
Definition: space.hh:430
AddrSpace::hasPhysical
bool hasPhysical(void) const
Return true if data is physically stored in this.
Definition: space.hh:410
xml.hh
Lightweight (and incomplete) XML parser for marshaling data to and from the decompiler.
AddrSpace::isBigEndian
bool isBigEndian(void) const
Return true if values in this space are big endian.
Definition: space.hh:417
OtherSpace::OtherSpace
OtherSpace(AddrSpaceManager *m, const Translate *t, const string &nm, int4 ind)
Constructor.
Definition: space.cc:400
AddrSpace::getTrans
const Translate * getTrans(void) const
Get the processor translator.
Definition: space.hh:279
AddrSpace::byteToAddressInt
static int4 byteToAddressInt(int4 val, uint4 ws)
Scale int4 from byte units to addressable units.
Definition: space.hh:523
spacetype
spacetype
Fundemental address space types.
Definition: space.hh:28
AddrSpace::getContain
virtual AddrSpace * getContain(void) const
Return this space's containing space (if any)
Definition: space.hh:487
ConstantSpace
Special AddrSpace for representing constants during analysis.
Definition: space.hh:179
AddrSpace::is_otherspace
@ is_otherspace
Quick check for the OtherSpace derived class.
Definition: space.hh:86
AddrSpace::calcScaleMask
void calcScaleMask(void)
Calculate scale and mask.
Definition: space.cc:21
OverlaySpace::saveXml
virtual void saveXml(ostream &s) const
Write the details of this space as XML.
Definition: space.cc:663
AddrSpace::numSpacebase
virtual int4 numSpacebase(void) const
Number of base registers associated with this space.
Definition: space.hh:456
VarnodeData
Data defining a specific memory location.
Definition: pcoderaw.hh:33
AddrSpace::wrapOffset
uintb wrapOffset(uintb off) const
Wrap -off- to the offset that fits into this space.
Definition: space.hh:370
ConstantSpace::restoreXml
virtual void restoreXml(const Element *el)
Definition: space.cc:387
AddrSpace::constant_space_index
@ constant_space_index
Reserved index for the constant space.
Definition: space.hh:90
AddrSpaceManager
A manager for different address spaces.
Definition: translate.hh:218
JoinSpace::read
virtual uintb read(const string &s, int4 &size) const
Read in an address (and possible size) from a string.
Definition: space.cc:600
AddrSpace::getDelay
int4 getDelay(void) const
Get number of heritage passes being delayed.
Definition: space.hh:302
ConstantSpace::saveXml
virtual void saveXml(ostream &s) const
Definition: space.cc:379
AddrSpace::printOffset
void printOffset(ostream &s, uintb offset) const
Write an address offset to a stream.
Definition: space.cc:177
AddrSpace::isReverseJustified
bool isReverseJustified(void) const
Return true if alignment justification does not match endianness.
Definition: space.hh:426