Ghidra Decompiler Analysis Engine
sleigh.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 __SLEIGH__
20 #define __SLEIGH__
21 
22 #include "sleighbase.hh"
23 
24 class LoadImage;
25 
36  uintb calling_index;
37 };
38 
42 struct PcodeData {
46  int4 isize;
47 };
48 
56 class PcodeCacher {
57  VarnodeData *poolstart;
58  VarnodeData *curpool;
59  VarnodeData *endpool;
60  vector<PcodeData> issued;
61  list<RelativeRecord> label_refs;
62  vector<uintb> labels;
63  VarnodeData *expandPool(uint4 size);
64 public:
65  PcodeCacher(void);
66  ~PcodeCacher(void);
67 
73  VarnodeData *newptr = curpool + size;
74  if (newptr <= endpool) {
75  VarnodeData *res = curpool;
76  curpool = newptr;
77  return res;
78  }
79  return expandPool(size);
80  }
81 
86  issued.emplace_back();
87  PcodeData *res = &issued.back();
88  res->outvar = (VarnodeData *)0;
89  res->invar = (VarnodeData *)0;
90  return res;
91  }
92  void addLabelRef(VarnodeData *ptr);
93  void addLabel(uint4 id);
94  void clear(void);
95  void resolveRelatives(void);
96  void emit(const Address &addr,PcodeEmit *emt) const;
97 };
98 
106  ContextCache *contextcache;
107  AddrSpace *constspace;
108  int4 minimumreuse;
109  uint4 mask;
110  ParserContext **list;
111  int4 nextfree;
112  ParserContext **hashtable;
113  void initialize(int4 min,int4 hashsize);
114  void free(void);
115 public:
116  DisassemblyCache(ContextCache *ccache,AddrSpace *cspace,int4 cachesize,int4 windowsize);
117  ~DisassemblyCache(void) { free(); }
118  ParserContext *getParserContext(const Address &addr);
119 };
120 
128 class SleighBuilder : public PcodeBuilder {
129  virtual void dump( OpTpl *op );
130  AddrSpace *const_space;
131  AddrSpace *uniq_space;
132  uintb uniquemask;
133  uintb uniqueoffset;
134  DisassemblyCache *discache;
135  PcodeCacher *cache;
136  void buildEmpty(Constructor *ct,int4 secnum);
137  void generateLocation(const VarnodeTpl *vntpl,VarnodeData &vn);
138  AddrSpace *generatePointer(const VarnodeTpl *vntpl,VarnodeData &vn);
139  void setUniqueOffset(const Address &addr);
140 public:
141  SleighBuilder(ParserWalker *w,DisassemblyCache *dcache,PcodeCacher *pc,AddrSpace *cspc,AddrSpace *uspc,uint4 umask);
142  virtual void appendBuild(OpTpl *bld,int4 secnum);
143  virtual void delaySlot(OpTpl *op);
144  virtual void setLabel(OpTpl *op);
145  virtual void appendCrossBuild(OpTpl *bld,int4 secnum);
146 };
147 
158 class Sleigh : public SleighBase {
159  LoadImage *loader;
160  ContextDatabase *context_db;
161  ContextCache *cache;
162  mutable DisassemblyCache *discache;
163  mutable PcodeCacher pcode_cache;
164  void clearForDelete(void);
165 protected:
166  ParserContext *obtainContext(const Address &addr,int4 state) const;
167  void resolve(ParserContext &pos) const;
168  void resolveHandles(ParserContext &pos) const;
169 public:
170  Sleigh(LoadImage *ld,ContextDatabase *c_db);
171  virtual ~Sleigh(void);
172  void reset(LoadImage *ld,ContextDatabase *c_db);
173  virtual void initialize(DocumentStorage &store);
174  virtual void registerContext(const string &name,int4 sbit,int4 ebit);
175  virtual void setContextDefault(const string &nm,uintm val);
176  virtual void allowContextSet(bool val) const;
177  virtual int4 instructionLength(const Address &baseaddr) const;
178  virtual int4 oneInstruction(PcodeEmit &emit,const Address &baseaddr) const;
179  virtual int4 printAssembly(AssemblyEmit &emit,const Address &baseaddr) const;
180 };
181 
523 #endif
PcodeEmit
Abstract class for emitting pcode to an application.
Definition: translate.hh:76
PcodeCacher::PcodeCacher
PcodeCacher(void)
Constructor.
Definition: sleigh.cc:19
Constructor
Definition: slghsymbol.hh:466
AddrSpace
A region where processor data is stored.
Definition: space.hh:73
SleighBuilder::SleighBuilder
SleighBuilder(ParserWalker *w, DisassemblyCache *dcache, PcodeCacher *pc, AddrSpace *cspc, AddrSpace *uspc, uint4 umask)
Constructor.
Definition: sleigh.cc:294
Sleigh::resolveHandles
void resolveHandles(ParserContext &pos) const
Prepare the parse tree for p-code generation.
Definition: sleigh.cc:620
RelativeRecord::dataptr
VarnodeData * dataptr
Varnode indicating relative offset.
Definition: sleigh.hh:35
PcodeData::invar
VarnodeData * invar
Array of input Varnode data.
Definition: sleigh.hh:45
AssemblyEmit
Abstract class for emitting disassembly to an application.
Definition: translate.hh:118
PcodeData::opc
OpCode opc
The op code.
Definition: sleigh.hh:43
VarnodeTpl
Definition: semantics.hh:77
PcodeData::isize
int4 isize
Number of input Varnodes.
Definition: sleigh.hh:46
Sleigh::obtainContext
ParserContext * obtainContext(const Address &addr, int4 state) const
Obtain a parse tree for the instruction at the given address.
Definition: sleigh.cc:546
ParserContext
Definition: context.hh:68
SleighBuilder
Build p-code from a pre-parsed instruction.
Definition: sleigh.hh:128
Sleigh::Sleigh
Sleigh(LoadImage *ld, ContextDatabase *c_db)
Constructor.
Definition: sleigh.cc:476
Sleigh::reset
void reset(LoadImage *ld, ContextDatabase *c_db)
Reset the engine for a new program.
Definition: sleigh.cc:504
OpTpl
Definition: semantics.hh:137
LoadImage
An interface into a particular binary executable image.
Definition: loadimage.hh:71
Sleigh::oneInstruction
virtual int4 oneInstruction(PcodeEmit &emit, const Address &baseaddr) const
Transform a single machine instruction into pcode.
Definition: sleigh.cc:697
PcodeCacher::~PcodeCacher
~PcodeCacher(void)
Destructor.
Definition: sleigh.cc:29
Sleigh
A full SLEIGH engine.
Definition: sleigh.hh:158
PcodeCacher::allocateVarnodes
VarnodeData * allocateVarnodes(uint4 size)
Allocate data objects for a new set of Varnodes.
Definition: sleigh.hh:72
PcodeCacher::resolveRelatives
void resolveRelatives(void)
Rewrite branch target Varnodes as relative offsets.
Definition: sleigh.cc:118
sleighbase.hh
Base class for applications that process SLEIGH format specifications.
PcodeCacher::emit
void emit(const Address &addr, PcodeEmit *emt) const
Pass the cached p-code data to the emitter.
Definition: sleigh.cc:137
ContextCache
A helper class for caching the active context blob to minimize database lookups.
Definition: globalcontext.hh:309
Sleigh::allowContextSet
virtual void allowContextSet(bool val) const
Toggle whether disassembly is allowed to affect context.
Definition: sleigh.cc:763
Address
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
PcodeCacher
Class for caching a chunk of p-code, prior to emitting.
Definition: sleigh.hh:56
Sleigh::~Sleigh
virtual ~Sleigh(void)
Destructor.
Definition: sleigh.cc:494
PcodeCacher::addLabel
void addLabel(uint4 id)
Attach a label to the next p-code instruction.
Definition: sleigh.cc:98
Sleigh::setContextDefault
virtual void setContextDefault(const string &nm, uintm val)
Set the default value for a particular context variable.
Definition: sleigh.cc:757
ContextDatabase
An interface to a database of disassembly/decompiler context information.
Definition: globalcontext.hh:108
Sleigh::instructionLength
virtual int4 instructionLength(const Address &baseaddr) const
Get the length of a machine instruction.
Definition: sleigh.cc:671
DisassemblyCache::DisassemblyCache
DisassemblyCache(ContextCache *ccache, AddrSpace *cspace, int4 cachesize, int4 windowsize)
Constructor.
Definition: sleigh.cc:441
Sleigh::resolve
void resolve(ParserContext &pos) const
Generate a parse tree suitable for disassembly.
Definition: sleigh.cc:565
OpCode
OpCode
The op-code defining a specific p-code operation (PcodeOp)
Definition: opcodes.hh:35
PcodeCacher::allocateInstruction
PcodeData * allocateInstruction(void)
Allocate a data object for a new p-code operation.
Definition: sleigh.hh:85
DisassemblyCache::getParserContext
ParserContext * getParserContext(const Address &addr)
Get the parser for a particular Address.
Definition: sleigh.cc:457
Sleigh::initialize
virtual void initialize(DocumentStorage &store)
Definition: sleigh.cc:517
DisassemblyCache
A container for disassembly context used by the SLEIGH engine.
Definition: sleigh.hh:105
SleighBase
Common core of classes that read or write SLEIGH specification files natively.
Definition: sleighbase.hh:57
Sleigh::registerContext
virtual void registerContext(const string &name, int4 sbit, int4 ebit)
Add a new context variable to the model for this processor.
Definition: sleigh.cc:751
Sleigh::printAssembly
virtual int4 printAssembly(AssemblyEmit &emit, const Address &baseaddr) const
Disassemble a single machine instruction.
Definition: sleigh.cc:678
VarnodeData
Data defining a specific memory location.
Definition: pcoderaw.hh:33
DocumentStorage
A container for parsed XML documents.
Definition: xml.hh:249
PcodeCacher::clear
void clear(void)
Reset the cache so that all objects are unallocated.
Definition: sleigh.cc:106
PcodeData::outvar
VarnodeData * outvar
Output Varnode data (or null)
Definition: sleigh.hh:44
PcodeBuilder
Definition: semantics.hh:192
ParserWalker
Definition: context.hh:124
PcodeData
Data for building one p-code instruction.
Definition: sleigh.hh:42
RelativeRecord
Class for describing a relative p-code branch destination.
Definition: sleigh.hh:34
RelativeRecord::calling_index
uintb calling_index
Index of instruction containing relative offset.
Definition: sleigh.hh:36
PcodeCacher::addLabelRef
void addLabelRef(VarnodeData *ptr)
Denote a Varnode holding a relative branch offset.
Definition: sleigh.cc:84