Ghidra Decompiler Analysis Engine
userop.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_USEROP__
20 #define __CPUI_USEROP__
21 
22 #include "typeop.hh"
23 
36 class UserPcodeOp {
37 protected:
38  string name;
39  int4 useropindex;
41 public:
42  UserPcodeOp(Architecture *g,const string &nm,int4 ind) {
43  name = nm; useropindex = ind; glb = g; }
44  const string &getName(void) const { return name; }
45  int4 getIndex(void) const { return useropindex; }
46  virtual ~UserPcodeOp(void) {}
47 
54  virtual string getOperatorName(const PcodeOp *op) const {
55  return name; }
56 
62  virtual void restoreXml(const Element *el)=0;
63 };
64 
71 public:
72  UnspecializedPcodeOp(Architecture *g,const string &nm,int4 ind)
73  : UserPcodeOp(g,nm,ind) {}
74  virtual void restoreXml(const Element *el) {}
75 };
76 
83 class InjectedUserOp : public UserPcodeOp {
84  uint4 injectid;
85 public:
86  InjectedUserOp(Architecture *g,const string &nm,int4 ind,int4 injid)
87  : UserPcodeOp(g,nm,ind) { injectid = injid; }
88  uint4 getInjectId(void) const { return injectid; }
89  virtual void restoreXml(const Element *el);
90 };
91 
99 class VolatileOp : public UserPcodeOp {
100 protected:
101  static string appendSize(const string &base,int4 size);
102 public:
103  VolatileOp(Architecture *g,const string &nm,int4 ind)
104  : UserPcodeOp(g,nm,ind) { }
105 };
106 
112 class VolatileReadOp : public VolatileOp {
113 public:
114  VolatileReadOp(Architecture *g,const string &nm,int4 ind)
115  : VolatileOp(g,nm,ind) {}
116  virtual string getOperatorName(const PcodeOp *op) const;
117  virtual void restoreXml(const Element *el);
118 };
119 
126 class VolatileWriteOp : public VolatileOp {
127 public:
128  VolatileWriteOp(Architecture *g,const string &nm,int4 ind)
129  : VolatileOp(g,nm,ind) {}
130  virtual string getOperatorName(const PcodeOp *op) const;
131  virtual void restoreXml(const Element *el);
132 };
133 
144 class TermPatternOp : public UserPcodeOp {
145 public:
146  TermPatternOp(Architecture *g,const string &nm,int4 ind) : UserPcodeOp(g,nm,ind) {}
147  virtual int4 getNumVariableTerms(void) const=0;
148 
155  virtual bool unify(Funcdata &data,PcodeOp *op,vector<Varnode *> &bindlist) const=0;
156 
161  virtual uintb execute(const vector<uintb> &input) const=0;
162 };
163 
169 struct OpFollow {
171  uintb val;
172  int4 slot;
173  OpFollow(void) {}
174  void restoreXml(const Element *el);
175 };
176 
199 class SegmentOp : public TermPatternOp {
200  AddrSpace *spc;
201  int4 injectId;
202  int4 baseinsize;
203  int4 innerinsize;
204  bool supportsfarpointer;
205  VarnodeData constresolve;
206 public:
207  SegmentOp(Architecture *g,const string &nm,int4 ind);
208  AddrSpace *getSpace(void) const { return spc; }
209  bool hasFarPointerSupport(void) const { return supportsfarpointer; }
210  int4 getBaseSize(void) const { return baseinsize; }
211  int4 getInnerSize(void) const { return innerinsize; }
212  const VarnodeData &getResolve(void) const { return constresolve; }
213  virtual int4 getNumVariableTerms(void) const { if (baseinsize!=0) return 2; return 1; }
214  virtual bool unify(Funcdata &data,PcodeOp *op,vector<Varnode *> &bindlist) const;
215  virtual uintb execute(const vector<uintb> &input) const;
216  virtual void restoreXml(const Element *el);
217 };
218 
229 class JumpAssistOp : public UserPcodeOp {
230  int4 index2case;
231  int4 index2addr;
232  int4 defaultaddr;
233  int4 calcsize;
234 public:
236  int4 getIndex2Case(void) const { return index2case; }
237  int4 getIndex2Addr(void) const { return index2addr; }
238  int4 getDefaultAddr(void) const { return defaultaddr; }
239  int4 getCalcSize(void) const { return calcsize; }
240  virtual void restoreXml(const Element *el);
241 };
242 
251  vector<UserPcodeOp *> useroplist;
252  map<string,UserPcodeOp *> useropmap;
253  vector<SegmentOp *> segmentop;
254  VolatileReadOp *vol_read;
255  VolatileWriteOp *vol_write;
256  void registerOp(UserPcodeOp *op);
257 public:
258  UserOpManage(void);
259  ~UserOpManage(void);
260  void initialize(Architecture *glb);
261  void setDefaults(Architecture *glb);
262  int4 numSegmentOps(void) const { return segmentop.size(); }
263 
267  UserPcodeOp *getOp(int4 i) const {
268  if (i>=useroplist.size()) return (UserPcodeOp *)0;
269  return useroplist[i];
270  }
271 
272  UserPcodeOp *getOp(const string &nm) const;
273 
277  SegmentOp *getSegmentOp(int4 i) const {
278  if (i>=segmentop.size()) return (SegmentOp *)0;
279  return segmentop[i];
280  }
281 
282  VolatileReadOp *getVolatileRead(void) const { return vol_read; }
283  VolatileWriteOp *getVolatileWrite(void) const { return vol_write; }
284  void parseSegmentOp(const Element *el,Architecture *glb);
285  void parseVolatile(const Element *el,Architecture *glb);
286  void parseCallOtherFixup(const Element *el,Architecture *glb);
287  void parseJumpAssist(const Element *el,Architecture *glb);
288  void manualCallOtherFixup(const string &useropname,const string &outname,
289  const vector<string> &inname,const string &snippet,Architecture *glb);
290 };
291 
292 #endif
SegmentOp::getNumVariableTerms
virtual int4 getNumVariableTerms(void) const
Get the number of input Varnodes expected.
Definition: userop.hh:213
AddrSpace
A region where processor data is stored.
Definition: space.hh:73
UserPcodeOp::name
string name
Low-level name of p-code operator.
Definition: userop.hh:38
UserOpManage::setDefaults
void setDefaults(Architecture *glb)
Create any required operations if they weren't explicitly defined.
Definition: userop.cc:333
UserOpManage::parseCallOtherFixup
void parseCallOtherFixup(const Element *el, Architecture *glb)
Parse a <callotherfixup> XML tag.
Definition: userop.cc:467
OpFollow::restoreXml
void restoreXml(const Element *el)
Restore this node from an XML stream.
Definition: userop.cc:84
UserOpManage::manualCallOtherFixup
void manualCallOtherFixup(const string &useropname, const string &outname, const vector< string > &inname, const string &snippet, Architecture *glb)
Manually install an InjectedUserOp given just names of the user defined op and the p-code snippet.
Definition: userop.cc:506
VolatileWriteOp::restoreXml
virtual void restoreXml(const Element *el)
Restore the detailed description from an XML stream.
Definition: userop.cc:75
UnspecializedPcodeOp
A user defined p-code op with no specialization.
Definition: userop.hh:70
SegmentOp::SegmentOp
SegmentOp(Architecture *g, const string &nm, int4 ind)
Constructor.
Definition: userop.cc:121
TermPatternOp::execute
virtual uintb execute(const vector< uintb > &input) const =0
Compute the output value of this operation, given constant inputs.
UserOpManage::initialize
void initialize(Architecture *glb)
Initialize description objects for all user defined ops.
Definition: userop.cc:318
VolatileOp
A base class for operations that access volatile memory.
Definition: userop.hh:99
UserOpManage::getOp
UserPcodeOp * getOp(int4 i) const
Definition: userop.hh:267
typeop.hh
Data-type and behavior information associated with specific p-code op-codes.
UserPcodeOp::getOperatorName
virtual string getOperatorName(const PcodeOp *op) const
Get the symbol representing this operation in decompiled code.
Definition: userop.hh:54
InjectedUserOp::restoreXml
virtual void restoreXml(const Element *el)
Restore the detailed description from an XML stream.
Definition: userop.cc:19
Element
An XML element. A node in the DOM tree.
Definition: xml.hh:150
SegmentOp::unify
virtual bool unify(Funcdata &data, PcodeOp *op, vector< Varnode * > &bindlist) const
Gather the formal input Varnode objects given the root PcodeOp.
Definition: userop.cc:127
PcodeOp
Lowest level operation of the p-code language.
Definition: op.hh:58
Architecture
Manager for all the major decompiler subsystems.
Definition: architecture.hh:119
OpFollow::val
uintb val
A possible constant second input.
Definition: userop.hh:171
UserOpManage
Manager/container for description objects (UserPcodeOp) of user defined p-code ops.
Definition: userop.hh:250
TermPatternOp::unify
virtual bool unify(Funcdata &data, PcodeOp *op, vector< Varnode * > &bindlist) const =0
Gather the formal input Varnode objects given the root PcodeOp.
OpFollow
A simple node used to dynamically define a sequence of operations.
Definition: userop.hh:169
UserOpManage::UserOpManage
UserOpManage(void)
Construct an empty manager.
Definition: userop.cc:296
VolatileReadOp::restoreXml
virtual void restoreXml(const Element *el)
Restore the detailed description from an XML stream.
Definition: userop.cc:62
UserPcodeOp
The base class for a detailed definition of a user-defined p-code operation.
Definition: userop.hh:36
VolatileWriteOp::getOperatorName
virtual string getOperatorName(const PcodeOp *op) const
Get the symbol representing this operation in decompiled code.
Definition: userop.cc:68
UserOpManage::parseJumpAssist
void parseJumpAssist(const Element *el, Architecture *glb)
Parse a <jumpassist> XML tag.
Definition: userop.cc:484
VolatileOp::appendSize
static string appendSize(const string &base, int4 size)
Append a suffix to a string encoding a specific size.
Definition: userop.cc:39
UserOpManage::~UserOpManage
~UserOpManage(void)
Destructor.
Definition: userop.cc:303
UserOpManage::parseSegmentOp
void parseSegmentOp(const Element *el, Architecture *glb)
Parse a <segmentop> XML tag.
Definition: userop.cc:417
OpFollow::opc
OpCode opc
The particular p-code operation.
Definition: userop.hh:170
SegmentOp::execute
virtual uintb execute(const vector< uintb > &input) const
Compute the output value of this operation, given constant inputs.
Definition: userop.cc:156
JumpAssistOp::restoreXml
virtual void restoreXml(const Element *el)
Restore the detailed description from an XML stream.
Definition: userop.cc:244
Funcdata
Container for data structures associated with a single function.
Definition: funcdata.hh:45
InjectedUserOp
A user defined operation that is injected with other p-code.
Definition: userop.hh:83
VolatileReadOp
An operation that reads from volatile memory.
Definition: userop.hh:112
TermPatternOp
A user defined p-code op that has a dynamically defined procedure.
Definition: userop.hh:144
JumpAssistOp
A user defined p-code op for assisting the recovery of jump tables.
Definition: userop.hh:229
UserOpManage::getSegmentOp
SegmentOp * getSegmentOp(int4 i) const
Definition: userop.hh:277
OpCode
OpCode
The op-code defining a specific p-code operation (PcodeOp)
Definition: opcodes.hh:35
OpFollow::slot
int4 slot
Slot to follow.
Definition: userop.hh:172
TermPatternOp::getNumVariableTerms
virtual int4 getNumVariableTerms(void) const =0
Get the number of input Varnodes expected.
UserOpManage::parseVolatile
void parseVolatile(const Element *el, Architecture *glb)
Parse a <volatile> XML tag.
Definition: userop.cc:435
VolatileWriteOp
An operation that writes to volatile memory.
Definition: userop.hh:126
UserPcodeOp::glb
Architecture * glb
Architecture owning the user defined op.
Definition: userop.hh:40
VarnodeData
Data defining a specific memory location.
Definition: pcoderaw.hh:33
UnspecializedPcodeOp::restoreXml
virtual void restoreXml(const Element *el)
Restore the detailed description from an XML stream.
Definition: userop.hh:74
UserPcodeOp::useropindex
int4 useropindex
Index passed in the CALLOTHER op.
Definition: userop.hh:39
VolatileReadOp::getOperatorName
virtual string getOperatorName(const PcodeOp *op) const
Get the symbol representing this operation in decompiled code.
Definition: userop.cc:55
JumpAssistOp::JumpAssistOp
JumpAssistOp(Architecture *g)
Constructor.
Definition: userop.cc:235
SegmentOp
The segmented address operator.
Definition: userop.hh:199
SegmentOp::restoreXml
virtual void restoreXml(const Element *el)
Restore the detailed description from an XML stream.
Definition: userop.cc:163
UserPcodeOp::restoreXml
virtual void restoreXml(const Element *el)=0
Restore the detailed description from an XML stream.