My Project
codedata.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 __CPUI_CODEDATA__
17 #define __CPUI_CODEDATA__
18 
19 #include "ifacedecomp.hh"
20 
22  static IfaceCodeDataCapability ifaceCodeDataCapability; // Singleton instance
23  IfaceCodeDataCapability(void); // Singleton
24  IfaceCodeDataCapability(const IfaceCodeDataCapability &op2); // Not implemented
25  IfaceCodeDataCapability &operator=(const IfaceCodeDataCapability &op2); // Not implemented
26 public:
27  virtual void registerCommands(IfaceStatus *status);
28 };
29 
30 class CodeDataAnalysis; // Forward declaration
31 
32 class CodeUnit {
33 public:
34  enum {
35  fallthru = 1,
36  jump = 2,
37  call = 4,
38  notcode = 8,
39  hit_by_fallthru = 16,
40  hit_by_jump = 32,
41  hit_by_call = 64,
42  errantstart = 128,
43  targethit = 256,
44  thunkhit = 512
45  };
46  uint4 flags;
47  int4 size;
48 };
49 
51  bool success;
52  int4 length;
53  uint4 flags;
54  Address jumpaddress;
55  uintb targethit;
56 };
57 
58 
59 class DisassemblyEngine : public PcodeEmit {
60  const Translate *trans;
61  vector<Address> jumpaddr;
62  set<uintb> targetoffsets;
63  OpCode lastop;
64  bool hascall;
65  bool hitsaddress;
66  uintb targethit;
67 public:
68  void init(const Translate *t);
69  virtual void dump(const Address &addr,OpCode opc,VarnodeData *outvar,VarnodeData *vars,int4 isize);
70  void disassemble(const Address &addr,DisassemblyResult &res);
71  void addTarget(const Address &addr) { targetoffsets.insert( addr.getOffset() ); }
72 };
73 
74 class TargetHit {
75 public:
76  Address funcstart; // Starting address of function making target call
77  Address codeaddr; // Address of instruction refering to target call
78  Address thunkaddr; // The target call
79  uint4 mask; // Mask associated with this target
80  bool operator<(const TargetHit &op2) const { return (funcstart < op2.funcstart); }
81 };
82 
83 struct TargetFeature {
84  string name; // Name of the target function
85  uint4 featuremask; // id of this target for ORing into a mask
86 };
87 
88 struct AddrLink {
89  Address a;
90  Address b;
91  AddrLink(Address i) { a = i; b=Address(); }
92  AddrLink(Address i,Address j) { a=i; b=j; }
93  bool operator<(const AddrLink &op2) const {
94  if (a != op2.a) return (a < op2.a);
95  return (b < op2.b);
96  }
97 };
98 
99 class CodeDataAnalysis : public IfaceData {
100 public:
101  int4 alignment; // Alignment of instructions
102  Architecture *glb;
103  DisassemblyEngine disengine;
104  RangeList modelhits;
105  map<Address,CodeUnit> codeunit;
106  map<AddrLink,uint4> fromto_crossref;
107  map<AddrLink,uint4> tofrom_crossref;
108  list<map<Address,CodeUnit>::iterator> taintlist;
109  list<Address> unlinkedstarts;
110  list<TargetHit> targethits;
111  map<Address,TargetFeature> targets;
112  virtual ~CodeDataAnalysis(void) {}
113  void init(Architecture *g);
114  void pushTaintAddress(const Address &addr);
115  void processTaint(void);
116  Address commitCodeVec(const Address &addr,vector<CodeUnit> &codevec,map<AddrLink,uint4> &fromto_vec);
117  void clearHitBy(void);
118  void clearCrossRefs(const Address &addr,const Address &endaddr);
119  void clearCodeUnits(const Address &addr,const Address &endaddr);
120  void addTarget(const string &nm,const Address &addr,uint4 mask);
121  int4 getNumTargets(void) const { return targets.size(); }
122  Address disassembleBlock(const Address &addr,const Address &endaddr);
123  void disassembleRange(const Range &range);
124  void disassembleRangeList(const RangeList &rangelist);
125  void findNotCodeUnits(void);
126  void markFallthruHits(void);
127  void markCrossHits(void);
128  void addTargetHit(const Address &codeaddr,uintb targethit);
129  void resolveThunkHit(const Address &codeaddr,uintb targethit);
130  void findUnlinked(void);
131  bool checkErrantStart(map<Address,CodeUnit>::iterator iter);
132  bool repairJump(const Address &addr,int4 max);
133  void findOffCut(void);
134  Address findFunctionStart(const Address &addr) const;
135  const list<TargetHit> &getTargetHits(void) const { return targethits; }
136  void dumpModelHits(ostream &s) const;
137  void dumpCrossRefs(ostream &s) const;
138  void dumpFunctionStarts(ostream &s) const;
139  void dumpUnlinked(ostream &s) const;
140  void dumpTargetHits(ostream &s) const;
141  void runModel(void);
142 };
143 
145 protected:
146  IfaceStatus *status;
147  IfaceDecompData *dcp;
148  CodeDataAnalysis *codedata;
149 public:
150  virtual void setData(IfaceStatus *root,IfaceData *data);
151  virtual string getModule(void) const { return "codedata"; }
152  virtual IfaceData *createData(void) { return new CodeDataAnalysis(); }
153 };
154 
156 public:
157  virtual void execute(istream &s);
158 };
159 
161 public:
162  virtual void execute(istream &s);
163 };
164 
166 public:
167  virtual void execute(istream &s);
168 };
169 
171 public:
172  virtual void execute(istream &s);
173 };
174 
176 public:
177  virtual void execute(istream &s);
178 };
179 
181 public:
182  virtual void execute(istream &s);
183 };
184 
186 public:
187  virtual void execute(istream &s);
188 };
189 
191 public:
192  virtual void execute(istream &s);
193 };
194 
195 #endif
Definition: interface.hh:62
Definition: codedata.hh:59
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
Definition: codedata.hh:74
Definition: codedata.hh:32
A contiguous range of bytes in some address space.
Definition: address.hh:161
A disjoint set of Ranges, possibly across multiple address spaces.
Definition: address.hh:203
Definition: codedata.hh:160
Definition: codedata.hh:21
Definition: ifacedecomp.hh:39
Definition: codedata.hh:144
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
Definition: codedata.hh:50
Definition: codedata.hh:190
Definition: codedata.hh:165
The interface to a translation engine for a processor.
Definition: translate.hh:293
Manager for all the major decompiler subsystems.
Definition: architecture.hh:117
Definition: codedata.hh:180
Definition: codedata.hh:170
uintb getOffset(void) const
Get the address offset.
Definition: address.hh:300
Definition: interface.hh:108
Definition: codedata.hh:99
Definition: interface.hh:67
Definition: interface.hh:96
Definition: codedata.hh:175
Definition: codedata.hh:155
Definition: codedata.hh:83
Definition: codedata.hh:185
Data defining a specific memory location.
Definition: pcoderaw.hh:33