My Project
architecture.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 #ifndef __CPUI_ARCHITECTURE__
19 #define __CPUI_ARCHITECTURE__
20 
21 #include "capability.hh"
22 #include "varmap.hh"
23 #include "action.hh"
24 #include "database.hh"
25 #include "pcodeinject.hh"
26 #include "fspec.hh"
27 #include "translate.hh"
28 #include "loadimage.hh"
29 #include "globalcontext.hh"
30 #include "comment.hh"
31 #include "userop.hh"
32 #include "options.hh"
33 #include "transform.hh"
34 #include "prefersplit.hh"
35 
36 #ifdef CPUI_STATISTICS
37 class Statistics {
42  uintb numfunc;
43  uintb numvar;
44  uintb coversum;
45  uintb coversumsq;
46  uintb lastcastcount;
47  uintb castcount;
48  uintb castcountsq;
49  //void process_cover(const Funcdata &data);
50  void process_cast(const Funcdata &data);
51 public:
52  Statistics(void);
53  ~Statistics(void);
54  void countCast(void) { castcount += 1; }
55  void process(const Funcdata &fd);
56  void printResults(ostream &s);
57 };
58 
59 #endif
60 
61 class Architecture;
62 
71  static const uint4 majorversion;
72  static const uint4 minorversion;
73  static vector<ArchitectureCapability *> thelist;
74 protected:
75  string name;
76 public:
77  const string &getName(void) const { return name; }
78  virtual void initialize(void);
79 
88  virtual Architecture *buildArchitecture(const string &filename,const string &target,ostream *estream)=0;
89 
94  virtual bool isFileMatch(const string &filename) const=0;
95 
102  virtual bool isXmlMatch(Document *doc) const=0;
103 
104  static ArchitectureCapability *findCapability(const string &filename);
105  static ArchitectureCapability *findCapability(Document *doc);
106  static void sortCapabilities(void);
107  static uint4 getMajorVersion(void) { return majorversion; }
108  static uint4 getMinorVersion(void) { return minorversion; }
109 };
110 
118 public:
119  string archid;
120 
121  // Configuration data
130  vector<AddrSpace *> inferPtrSpaces;
132  uint4 flowoptions;
134  vector<Rule *> extra_pool_rules;
135 
138  map<string,ProtoModel *> protoModels;
151  vector<PrintLanguage *> printlist;
153  vector<TypeOp *> inst;
155  vector<PreferSplitRecord> splitrecords;
156  vector<LanedRegister> lanerecords;
159 #ifdef CPUI_STATISTICS
160  Statistics *stats;
161 #endif
162 #ifdef OPACTION_DEBUG
163  ostream *debugstream;
164 #endif
165  Architecture(void);
166  void init(DocumentStorage &store);
167  ProtoModel *getModel(const string &nm) const;
168  bool hasModel(const string &nm) const;
169  bool highPtrPossible(const Address &loc,int4 size) const;
170  AddrSpace *getSpaceBySpacebase(const Address &loc,int4 size) const;
171  const LanedRegister *getLanedRegister(const Address &loc,int4 size) const;
172  int4 getMinimumLanedRegisterSize(void) const;
173  void setDefaultModel(const string &nm);
174  void clearAnalysis(Funcdata *fd);
175  void readLoaderSymbols(void);
176  void collectBehaviors(vector<OpBehavior *> &behave) const;
177  SegmentOp *getSegmentOp(AddrSpace *spc) const;
178  void setPrototype(const PrototypePieces &pieces);
179  void setPrintLanguage(const string &nm);
180  void globalify(void);
181  void restoreFlowOverride(const Element *el);
182  virtual ~Architecture(void);
183 
184  virtual string getDescription(void) const { return archid; }
185 
190  virtual void printMessage(const string &message) const=0;
191  virtual void saveXml(ostream &s) const;
192  virtual void restoreXml(DocumentStorage &store);
193  virtual void nameFunction(const Address &addr,string &name) const;
194 #ifdef OPACTION_DEBUG
195  void setDebugStream(ostream *s) { debugstream = s; }
196  void printDebug(const string &message) const { *debugstream << message << endl; }
197 #endif
198 protected:
199  void addSpacebase(AddrSpace *basespace,const string &nm,const VarnodeData &ptrdata,
200  int4 truncSize,bool isreversejustified,bool stackGrowth);
201  void addNoHighPtr(const Range &rng);
202 
203  // Factory routines for building this architecture
204  virtual Scope *buildGlobalScope(void);
205 
212  virtual Translate *buildTranslator(DocumentStorage &store)=0;
213 
217  virtual void buildLoader(DocumentStorage &store)=0;
218 
223  virtual PcodeInjectLibrary *buildPcodeInjectLibrary(void)=0;
224 
225  virtual void buildTypegrp(DocumentStorage &store);
226  virtual void buildCommentDB(DocumentStorage &store);
227  virtual void buildConstantPool(DocumentStorage &store);
228  virtual void buildInstructions(DocumentStorage &store);
229  virtual void buildAction(DocumentStorage &store);
230  virtual void buildContext(DocumentStorage &store);
231 
236  virtual void buildSpecFile(DocumentStorage &store)=0;
237 
243  virtual void modifySpaces(Translate *trans)=0;
244 
245  virtual void postSpecFile(void);
246 
247  virtual void resolveArchitecture(void)=0;
248 
249  void restoreFromSpec(DocumentStorage &store);
250  void fillinReadOnlyFromLoader(void);
251  void initializeSegments();
252  void cacheAddrSpaceProperties(void);
253 
254  void parseProcessorConfig(DocumentStorage &store);
255  void parseCompilerConfig(DocumentStorage &store);
256  void parseExtraRules(DocumentStorage &store);
257 
258  void parseDynamicRule(const Element *el);
259  ProtoModel *parseProto(const Element *el);
260  void parseProtoEval(const Element *el);
261  void parseDefaultProto(const Element *el);
262  void parseGlobal(const Element *el);
263  void addOtherSpace(void);
264  void parseReadOnly(const Element *el);
265  void parseVolatile(const Element *el);
266  void parseReturnAddress(const Element *el);
267  void parseIncidentalCopy(const Element *el);
268  void parseLaneSizes(const Element *el);
269  void parseStackPointer(const Element *el);
270  void parseDeadcodeDelay(const Element *el);
271  void parseFuncPtrAlign(const Element *el);
272  void parseSpacebase(const Element *el);
273  void parseNoHighPtr(const Element *el);
274  void parsePreferSplit(const Element *el);
275  void parseAggressiveTrim(const Element *el);
276 };
277 
284  Architecture *glb;
285  AddrSpace *spc;
286  SegmentOp *segop;
287 public:
292  SegmentedResolver(Architecture *g,AddrSpace *sp,SegmentOp *sop) { glb=g; spc=sp; segop=sop; }
293  virtual Address resolve(uintb val,int4 sz,const Address &point,uintb &fullEncoding);
294 };
295 
304 inline bool Architecture::highPtrPossible(const Address &loc,int4 size) const {
305  if (loc.getSpace()->getType() == IPTR_INTERNAL) return false;
306  return !nohighptr.inRange(loc,size);
307 }
308 
309 #endif
A container for parsed XML documents.
Definition: xml.hh:249
bool highPtrPossible(const Address &loc, int4 size) const
Are pointers possible to the given location?
Definition: architecture.hh:304
LoadImage * loader
Method for loading portions of binary.
Definition: architecture.hh:145
A region where processor data is stored.
Definition: space.hh:73
Classes for managing p-code injection.
int4 trim_recurse_max
How many levels to let parameter trims recurse.
Definition: architecture.hh:122
Classes for building large scale transforms of function data-flow.
A manager for different address spaces.
Definition: translate.hh:218
Abstract extension point for building Architecture objects.
Definition: architecture.hh:70
A collection of p-code injection payloads.
Definition: pcodeinject.hh:162
An interface into a particular binary executable image.
Definition: loadimage.hh:71
The segmented address operator.
Definition: userop.hh:199
Definitions for specifying functions prototypes.
An interface to a container of comments.
Definition: comment.hh:84
Container for data structures associated with a single function.
Definition: funcdata.hh:45
vector< AddrSpace * > inferPtrSpaces
Set of address spaces in which a pointer constant is inferable.
Definition: architecture.hh:130
int4 max_implied_ref
Maximum number of references to an implied var.
Definition: architecture.hh:123
vector< TypeOp * > inst
Registered p-code instructions.
Definition: architecture.hh:153
A manager for symbol scopes for a whole executable.
Definition: database.hh:838
ProtoModel * evalfp_current
Function proto to use when evaluating current function.
Definition: architecture.hh:141
A contiguous range of bytes in some address space.
Definition: address.hh:161
A prototype model: a model for passing parameters between functions.
Definition: fspec.hh:615
OptionDatabase * options
Options that can be configured.
Definition: architecture.hh:152
const string & getName(void) const
Get the capability identifier.
Definition: architecture.hh:77
static uint4 getMinorVersion(void)
Get minor decompiler version.
Definition: architecture.hh:108
Infrastructure for discovering code extensions to the decompiler.
virtual string getDescription(void) const
Get a string describing this architecture.
Definition: architecture.hh:184
A disjoint set of Ranges, possibly across multiple address spaces.
Definition: address.hh:203
int4 max_basetype_size
Maximum size of an "integer" type before creating an array type.
Definition: architecture.hh:125
Raw components of a function prototype (obtained from parsing source code)
Definition: fspec.hh:1127
ConstantPool * cpool
Deferred constant values.
Definition: architecture.hh:149
Classes for processing architecture configuration options.
Database * symboltab
Memory map of global variables and functions.
Definition: architecture.hh:136
SegmentedResolver(Architecture *g, AddrSpace *sp, SegmentOp *sop)
Definition: architecture.hh:292
An interface to a database of disassembly/decompiler context information.
Definition: globalcontext.hh:108
spacetype getType(void) const
Get the type of space.
Definition: space.hh:289
Abstract class for converting native constants to addresses.
Definition: translate.hh:140
vector< PreferSplitRecord > splitrecords
registers that we would prefer to see split for this processor
Definition: architecture.hh:155
CommentDatabase * commentdb
Comments for this architecture.
Definition: architecture.hh:148
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
VarnodeData defaultReturnAddr
Default storage location of return address (for current function)
Definition: architecture.hh:140
Action, Rule, and other associates classes supporting transformations on function data-flow...
vector< LanedRegister > lanerecords
Vector registers that have preferred lane sizes.
Definition: architecture.hh:156
AddrSpace * getSpace(void) const
Get the address space.
Definition: address.hh:294
The interface to a translation engine for a processor.
Definition: translate.hh:293
static uint4 getMajorVersion(void)
Get major decompiler version.
Definition: architecture.hh:107
int4 min_funcsymbol_size
Minimum size of a function symbol.
Definition: architecture.hh:126
A Dispatcher for possible ArchOption commands.
Definition: options.hh:63
UserOpManage userops
Specifically registered user-defined p-code ops.
Definition: architecture.hh:154
Manager for all the major decompiler subsystems.
Definition: architecture.hh:117
Manager/container for description objects (UserPcodeOp) of user defined p-code ops.
Definition: userop.hh:250
An interface to the pool of constant objects for byte-code languages.
Definition: cpool.hh:94
map< string, ProtoModel * > protoModels
Parsed forms of possible prototypes.
Definition: architecture.hh:138
ProtoModel * defaultfp
Parsed form of default prototype.
Definition: architecture.hh:139
string archid
ID string uniquely describing this architecture.
Definition: architecture.hh:119
An XML element. A node in the DOM tree.
Definition: xml.hh:150
bool readonlypropagate
true if readonly values should be treated as constants
Definition: architecture.hh:128
A complete in-memory XML document.
Definition: xml.hh:206
RangeList nohighptr
Ranges for which high-level pointers are not possible.
Definition: architecture.hh:147
vector< Rule * > extra_pool_rules
Extra rules that go in the main pool (cpu specific, experimental)
Definition: architecture.hh:134
const Translate * translate
Translation method for this binary.
Definition: architecture.hh:144
PcodeInjectLibrary * pcodeinjectlib
Pcode injection manager.
Definition: architecture.hh:146
ContextDatabase * context
Map from addresses to context settings.
Definition: architecture.hh:137
Internally managed temporary space.
Definition: space.hh:32
Classes for more detailed definitions of user defined p-code operations.
bool aggressive_ext_trim
Aggressively trim inputs that look like they are sign extended.
Definition: architecture.hh:127
string name
Identifier for this capability.
Definition: architecture.hh:75
Symbol and Scope objects for the decompiler.
int4 funcptr_align
How many bits of alignment a function ptr has.
Definition: architecture.hh:131
Container class for all Datatype objects in an Architecture.
Definition: type.hh:380
int4 max_term_duplication
Max terms duplicated without a new variable.
Definition: architecture.hh:124
ProtoModel * evalfp_called
Function proto to use when evaluating called functions.
Definition: architecture.hh:142
Classes for keeping track of local variables and reconstructing stack layout.
ActionDatabase allacts
Actions that can be applied in this architecture.
Definition: architecture.hh:157
Class for automatically registering extension points to the decompiler.
Definition: capability.hh:36
Database of root Action objects that can be used to transform a function.
Definition: action.hh:294
uint4 flowoptions
options passed to flow following engine
Definition: architecture.hh:132
A resolver for segmented architectures.
Definition: architecture.hh:283
int4 alias_block_level
Aliases blocked by 0=none, 1=struct, 2=array, 3=all.
Definition: architecture.hh:133
bool loadersymbols_parsed
True if loader symbols have been read.
Definition: architecture.hh:158
vector< PrintLanguage * > printlist
List of high-level language printers supported.
Definition: architecture.hh:151
Utilities for getting address-based context to the disassembler and decompiler.
PrintLanguage * print
Current high-level language printer.
Definition: architecture.hh:150
bool infer_pointers
True if we should infer pointers from constants that are likely addresses.
Definition: architecture.hh:129
A database interface for high-level language comments.
Classes for disassembly and pcode generation.
The base class API for emitting a high-level language.
Definition: printlanguage.hh:134
TypeFactory * types
List of types for this binary.
Definition: architecture.hh:143
Describes a (register) storage location and the ways it might be split into lanes.
Definition: transform.hh:88
Data defining a specific memory location.
Definition: pcoderaw.hh:33
A collection of Symbol objects within a single (namespace or functional) scope.
Definition: database.hh:413
Classes and API for accessing a binary load image.