Ghidra Decompiler Analysis Engine
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  TargetHit(const Address &func,const Address &code,const Address &thunk,uint4 m) :
81  funcstart(func), codeaddr(code), thunkaddr(thunk) { mask = m; }
82  bool operator<(const TargetHit &op2) const { return (funcstart < op2.funcstart); }
83 };
84 
85 struct TargetFeature {
86  string name; // Name of the target function
87  uint4 featuremask; // id of this target for ORing into a mask
88 };
89 
90 struct AddrLink {
91  Address a;
92  Address b;
93  AddrLink(Address i) { a = i; b=Address(); }
94  AddrLink(Address i,Address j) { a=i; b=j; }
95  bool operator<(const AddrLink &op2) const {
96  if (a != op2.a) return (a < op2.a);
97  return (b < op2.b);
98  }
99 };
100 
101 class CodeDataAnalysis : public IfaceData {
102 public:
103  int4 alignment; // Alignment of instructions
104  Architecture *glb;
105  DisassemblyEngine disengine;
106  RangeList modelhits;
107  map<Address,CodeUnit> codeunit;
108  map<AddrLink,uint4> fromto_crossref;
109  map<AddrLink,uint4> tofrom_crossref;
110  list<map<Address,CodeUnit>::iterator> taintlist;
111  list<Address> unlinkedstarts;
112  list<TargetHit> targethits;
113  map<Address,TargetFeature> targets;
114  virtual ~CodeDataAnalysis(void) {}
115  void init(Architecture *g);
116  void pushTaintAddress(const Address &addr);
117  void processTaint(void);
118  Address commitCodeVec(const Address &addr,vector<CodeUnit> &codevec,map<AddrLink,uint4> &fromto_vec);
119  void clearHitBy(void);
120  void clearCrossRefs(const Address &addr,const Address &endaddr);
121  void clearCodeUnits(const Address &addr,const Address &endaddr);
122  void addTarget(const string &nm,const Address &addr,uint4 mask);
123  int4 getNumTargets(void) const { return targets.size(); }
124  Address disassembleBlock(const Address &addr,const Address &endaddr);
125  void disassembleRange(const Range &range);
126  void disassembleRangeList(const RangeList &rangelist);
127  void findNotCodeUnits(void);
128  void markFallthruHits(void);
129  void markCrossHits(void);
130  void addTargetHit(const Address &codeaddr,uintb targethit);
131  void resolveThunkHit(const Address &codeaddr,uintb targethit);
132  void findUnlinked(void);
133  bool checkErrantStart(map<Address,CodeUnit>::iterator iter);
134  bool repairJump(const Address &addr,int4 max);
135  void findOffCut(void);
136  Address findFunctionStart(const Address &addr) const;
137  const list<TargetHit> &getTargetHits(void) const { return targethits; }
138  void dumpModelHits(ostream &s) const;
139  void dumpCrossRefs(ostream &s) const;
140  void dumpFunctionStarts(ostream &s) const;
141  void dumpUnlinked(ostream &s) const;
142  void dumpTargetHits(ostream &s) const;
143  void runModel(void);
144 };
145 
147 protected:
148  IfaceStatus *status;
149  IfaceDecompData *dcp;
150  CodeDataAnalysis *codedata;
151 public:
152  virtual void setData(IfaceStatus *root,IfaceData *data);
153  virtual string getModule(void) const { return "codedata"; }
154  virtual IfaceData *createData(void) { return new CodeDataAnalysis(); }
155 };
156 
158 public:
159  virtual void execute(istream &s);
160 };
161 
163 public:
164  virtual void execute(istream &s);
165 };
166 
168 public:
169  virtual void execute(istream &s);
170 };
171 
173 public:
174  virtual void execute(istream &s);
175 };
176 
178 public:
179  virtual void execute(istream &s);
180 };
181 
183 public:
184  virtual void execute(istream &s);
185 };
186 
188 public:
189  virtual void execute(istream &s);
190 };
191 
193 public:
194  virtual void execute(istream &s);
195 };
196 
197 #endif
IfcCodeDataDumpModelHits::execute
virtual void execute(istream &s)
Definition: codedata.cc:760
IfaceCodeDataCapability::registerCommands
virtual void registerCommands(IfaceStatus *status)
Register commands for this grouping.
Definition: codedata.cc:29
IfcCodeDataTarget
Definition: codedata.hh:162
IfcCodeDataDumpModelHits
Definition: codedata.hh:172
PcodeEmit
Abstract class for emitting pcode to an application.
Definition: translate.hh:76
IfaceCodeDataCommand::getModule
virtual string getModule(void) const
Get the formal module name to which this command belongs.
Definition: codedata.hh:153
IfcCodeDataDumpCrossRefs
Definition: codedata.hh:177
IfcCodeDataInit
Definition: codedata.hh:157
DisassemblyEngine::dump
virtual void dump(const Address &addr, OpCode opc, VarnodeData *outvar, VarnodeData *vars, int4 isize)
The main pcode emit method.
Definition: codedata.cc:50
CodeDataAnalysis
Definition: codedata.hh:101
ifacedecomp.hh
Console interface commands for the decompiler engine.
TargetHit
Definition: codedata.hh:74
IfaceCodeDataCapability
Definition: codedata.hh:21
IfcCodeDataDumpTargetHits
Definition: codedata.hh:192
Address::getOffset
uintb getOffset(void) const
Get the address offset.
Definition: address.hh:300
IfcCodeDataTarget::execute
virtual void execute(istream &s)
Definition: codedata.cc:730
IfaceStatus
A generic console mode interface and command executor.
Definition: interface.hh:195
IfaceDecompData
Common data shared by decompiler commands.
Definition: ifacedecomp.hh:42
Architecture
Manager for all the major decompiler subsystems.
Definition: architecture.hh:119
IfcCodeDataRun::execute
virtual void execute(istream &s)
Definition: codedata.cc:754
DisassemblyEngine
Definition: codedata.hh:59
CodeUnit
Definition: codedata.hh:32
IfcCodeDataDumpCrossRefs::execute
virtual void execute(istream &s)
Definition: codedata.cc:766
RangeList
A disjoint set of Ranges, possibly across multiple address spaces.
Definition: address.hh:203
IfcCodeDataDumpStarts
Definition: codedata.hh:182
IfaceCodeDataCommand::setData
virtual void setData(IfaceStatus *root, IfaceData *data)
Associate a specific data object with this command.
Definition: codedata.cc:716
Address
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
IfaceCodeDataCommand::createData
virtual IfaceData * createData(void)
Create a specialized data object for this command (and its module)
Definition: codedata.hh:154
Translate
The interface to a translation engine for a processor.
Definition: translate.hh:294
IfaceCommand
A command that can be executed from the command line.
Definition: interface.hh:97
IfaceCapability
Groups of console commands that are discovered by the loader.
Definition: interface.hh:161
IfcCodeDataDumpStarts::execute
virtual void execute(istream &s)
Definition: codedata.cc:772
Range
A contiguous range of bytes in some address space.
Definition: address.hh:161
OpCode
OpCode
The op-code defining a specific p-code operation (PcodeOp)
Definition: opcodes.hh:35
IfcCodeDataDumpUnlinked::execute
virtual void execute(istream &s)
Definition: codedata.cc:778
IfcCodeDataDumpTargetHits::execute
virtual void execute(istream &s)
Definition: codedata.cc:784
IfcCodeDataInit::execute
virtual void execute(istream &s)
Definition: codedata.cc:724
VarnodeData
Data defining a specific memory location.
Definition: pcoderaw.hh:33
TargetFeature
Definition: codedata.hh:85
IfcCodeDataDumpUnlinked
Definition: codedata.hh:187
IfaceData
Data specialized for a particular command module.
Definition: interface.hh:85
IfaceCodeDataCommand
Definition: codedata.hh:146
DisassemblyResult
Definition: codedata.hh:50
IfcCodeDataRun
Definition: codedata.hh:167