My Project
sleigh.hh
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  */
16 #ifndef __SLEIGH__
17 #define __SLEIGH__
18 
19 #include "sleighbase.hh"
20 
21 class LoadImage;
22 
24  VarnodeData *dataptr; // Record containing relative offset
25  uintb calling_index; // Index of instruction containing relative offset
26 };
27 
28 struct PcodeData { // Data for building one pcode instruction
29  OpCode opc;
30  VarnodeData *outvar; // Points to outvar is there is an output
31  VarnodeData *invar; // Inputs
32  int4 isize; // Number of inputs
33 };
34 
35 class PcodeCacher { // Cached chunk of pcode, prior to emitting
36  VarnodeData *poolstart;
37  VarnodeData *curpool;
38  VarnodeData *endpool;
39  vector<PcodeData> issued;
40  list<RelativeRecord> label_refs; // References to labels
41  vector<uintb> labels; // Locations of labels
42  VarnodeData *expandPool(uint4 size);
43 public:
44  PcodeCacher(void);
45  ~PcodeCacher(void);
46  VarnodeData *allocateVarnodes(uint4 size) {
47  VarnodeData *newptr = curpool + size;
48  if (newptr <= endpool) {
49  VarnodeData *res = curpool;
50  curpool = newptr;
51  return res;
52  }
53  return expandPool(size);
54  }
55  PcodeData *allocateInstruction(void) {
56  issued.push_back(PcodeData());
57  PcodeData *res = &issued.back();
58  res->outvar = (VarnodeData *)0;
59  res->invar = (VarnodeData *)0;
60  return res;
61  }
62  void addLabelRef(VarnodeData *ptr);
63  void addLabel(uint4 id);
64  void clear(void);
65  void resolveRelatives(void);
66  void emit(const Address &addr,PcodeEmit *emt) const;
67 };
68 
70  ContextCache *contextcache;
71  AddrSpace *constspace;
72  int4 minimumreuse; // Can call getParserContext this many times, before a ParserContext is reused
73  uint4 mask; // Size of the hashtable in form 2^n-1
74  ParserContext **list; // (circular) array of currently cached ParserContext objects
75  int4 nextfree; // Current end/beginning of circular list
76  ParserContext **hashtable; // Hashtable for looking up ParserContext via Address
77  void initialize(int4 min,int4 hashsize);
78  void free(void);
79 public:
80  DisassemblyCache(ContextCache *ccache,AddrSpace *cspace,int4 cachesize,int4 windowsize);
81  ~DisassemblyCache(void) { free(); }
82  ParserContext *getParserContext(const Address &addr);
83 };
84 
85 class SleighBuilder : public PcodeBuilder {
86  virtual void dump( OpTpl *op );
87  AddrSpace *const_space;
88  AddrSpace *uniq_space;
89  uintb uniquemask;
90  uintb uniqueoffset;
91  DisassemblyCache *discache;
92  PcodeCacher *cache;
93  void buildEmpty(Constructor *ct,int4 secnum);
94  void generateLocation(const VarnodeTpl *vntpl,VarnodeData &vn);
95  AddrSpace *generatePointer(const VarnodeTpl *vntpl,VarnodeData &vn);
96  void setUniqueOffset(const Address &addr);
97 public:
98  SleighBuilder(ParserWalker *w,DisassemblyCache *dcache,PcodeCacher *pc,AddrSpace *cspc,AddrSpace *uspc,uint4 umask);
99  virtual void appendBuild(OpTpl *bld,int4 secnum);
100  virtual void delaySlot(OpTpl *op);
101  virtual void setLabel(OpTpl *op);
102  virtual void appendCrossBuild(OpTpl *bld,int4 secnum);
103 };
104 
105 class Sleigh : public SleighBase {
106  LoadImage *loader;
107  ContextDatabase *context_db;
108  ContextCache *cache;
109  mutable DisassemblyCache *discache;
110  mutable PcodeCacher pcode_cache;
111  void clearForDelete(void);
112 protected:
113  ParserContext *obtainContext(const Address &addr,int4 state) const;
114  void resolve(ParserContext &pos) const;
115  void resolveHandles(ParserContext &pos) const;
116 public:
117  Sleigh(LoadImage *ld,ContextDatabase *c_db);
118  virtual ~Sleigh(void);
119  void reset(LoadImage *ld,ContextDatabase *c_db);
120  virtual void initialize(DocumentStorage &store);
121  virtual void registerContext(const string &name,int4 sbit,int4 ebit);
122  virtual void setContextDefault(const string &nm,uintm val);
123  virtual void allowContextSet(bool val) const;
124  virtual int4 instructionLength(const Address &baseaddr) const;
125  virtual int4 oneInstruction(PcodeEmit &emit,const Address &baseaddr) const;
126  virtual int4 printAssembly(AssemblyEmit &emit,const Address &baseaddr) const;
127 };
128 
470 #endif
A container for parsed XML documents.
Definition: xml.hh:249
A region where processor data is stored.
Definition: space.hh:73
Definition: semantics.hh:192
Definition: sleigh.hh:28
Definition: semantics.hh:137
OpCode
The op-code defining a specific p-code operation (PcodeOp)
Definition: opcodes.hh:35
Abstract class for emitting pcode to an application.
Definition: translate.hh:76
An interface into a particular binary executable image.
Definition: loadimage.hh:71
Common core of classes that read or write SLEIGH specification files natively.
Definition: sleighbase.hh:29
Definition: sleigh.hh:69
Definition: sleigh.hh:35
Definition: context.hh:68
An interface to a database of disassembly/decompiler context information.
Definition: globalcontext.hh:108
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
Definition: sleigh.hh:105
Base class for applications that process SLEIGH format specifications.
Definition: context.hh:124
Definition: sleigh.hh:85
A helper class for caching the active context blob to minimize database lookups.
Definition: globalcontext.hh:309
Abstract class for emitting disassembly to an application.
Definition: translate.hh:118
Definition: sleigh.hh:23
Definition: slghsymbol.hh:466
Definition: semantics.hh:77
Data defining a specific memory location.
Definition: pcoderaw.hh:33