Ghidra Decompiler Analysis Engine
callgraph.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_CALLGRAPH__
17 #define __CPUI_CALLGRAPH__
18 
19 #include "address.hh"
20 
21 // Forward declarations
22 class Architecture;
23 class Funcdata;
24 class CallGraphNode;
25 class CallGraph;
26 
28 public:
29  enum {
30  cycle = 1, // Edge that was snipped to eliminate cycles
31  dontfollow = 2 // Edge that is not in the spanning tree
32  };
33 private:
34  friend class CallGraphNode;
35  friend class CallGraph;
36  CallGraphNode *from; // Node of the caller
37  CallGraphNode *to; // Node of the callee
38  Address callsiteaddr; // Address where call was made from
39  int4 complement; // Index of complementary edge
40  mutable uint4 flags;
41 public:
42  CallGraphEdge(void) { flags = 0; }
43  bool isCycle(void) const { return ((flags&1)!=0); }
44  void saveXml(ostream &s) const;
45  const Address &getCallSiteAddr(void) const { return callsiteaddr; }
46  static void restoreXml(const Element *el,CallGraph *graph);
47 };
48 
50 public:
51  enum {
52  mark = 1,
53  onlycyclein = 2,
54  currentcycle = 4,
55  entrynode = 8
56  };
57 private:
58  friend class CallGraph;
59  Address entryaddr; // Starting address of function
60  string name; // Name of the function if available
61  Funcdata *fd; // Pointer to funcdata if we have it
62  vector<CallGraphEdge> inedge;
63  vector<CallGraphEdge> outedge;
64  int4 parentedge; // Incoming edge for spanning tree
65  mutable uint4 flags;
66 public:
67  CallGraphNode(void) { fd = (Funcdata *)0; flags = 0; parentedge = -1; }
68  void clearMark(void) const { flags &= ~((uint4)mark); }
69  bool isMark(void) const { return ((flags&mark)!=0); }
70  const Address getAddr(void) const { return entryaddr; }
71  const string &getName(void) const { return name; }
72  Funcdata *getFuncdata(void) const { return fd; }
73  int4 numInEdge(void) const { return inedge.size(); }
74  const CallGraphEdge &getInEdge(int4 i) const { return inedge[i]; }
75  CallGraphNode *getInNode(int4 i) const { return inedge[i].from; }
76  int4 numOutEdge(void) const { return outedge.size(); }
77  const CallGraphEdge &getOutEdge(int4 i) const { return outedge[i]; }
78  CallGraphNode *getOutNode(int4 i) const { return outedge[i].to; }
79  void setFuncdata(Funcdata *f);
80  void saveXml(ostream &s) const;
81  static void restoreXml(const Element *el,CallGraph *graph);
82 };
83 
84 struct LeafIterator {
85  CallGraphNode *node;
86  int4 outslot;
87  LeafIterator(CallGraphNode *n) { node=n; outslot = 0; }
88 };
89 
90 class Scope; // forward declaration
91 class CallGraph {
92  Architecture *glb;
93  map<Address,CallGraphNode> graph; // Nodes in the graph sorted by address
94  vector<CallGraphNode *> seeds;
95  bool findNoEntry(vector<CallGraphNode *> &seeds);
96  void snipCycles(CallGraphNode *node);
97  void snipEdge(CallGraphNode *node,int4 i);
98  void clearMarks(void);
99  void cycleStructure(void);
100  CallGraphNode *popPossible(CallGraphNode *node,int4 &outslot);
101  CallGraphNode *pushPossible(CallGraphNode *node,int4 outslot);
102  CallGraphEdge &insertBlankEdge(CallGraphNode *node,int4 slot);
103  void iterateScopesRecursive(Scope *scope);
104  void iterateFunctionsAddrOrder(Scope *scope);
105 public:
106  CallGraph(Architecture *g) { glb = g; }
107  Architecture *getArch(void) const { return glb; }
108  CallGraphNode *addNode(Funcdata *f);
109  CallGraphNode *addNode(const Address &addr,const string &nm);
110  CallGraphNode *findNode(const Address &addr);
111  void addEdge(CallGraphNode *from,CallGraphNode *to,const Address &addr);
112  void deleteInEdge(CallGraphNode *node,int4 i);
113  CallGraphNode * initLeafWalk(void);
114  CallGraphNode *nextLeaf(CallGraphNode *node);
115  map<Address,CallGraphNode>::iterator begin(void) { return graph.begin(); }
116  map<Address,CallGraphNode>::iterator end(void) { return graph.end(); }
117  void buildAllNodes(void);
118  void buildEdges(Funcdata *fd);
119  void saveXml(ostream &s) const;
120  void restoreXml(const Element *el);
121 };
122 
123 #endif
Scope
A collection of Symbol objects within a single (namespace or functional) scope.
Definition: database.hh:402
CallGraph
Definition: callgraph.hh:91
Element
An XML element. A node in the DOM tree.
Definition: xml.hh:150
Architecture
Manager for all the major decompiler subsystems.
Definition: architecture.hh:119
LeafIterator
Definition: callgraph.hh:84
Address
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
CallGraphNode
Definition: callgraph.hh:49
Funcdata
Container for data structures associated with a single function.
Definition: funcdata.hh:45
CallGraphEdge
Definition: callgraph.hh:27
address.hh
Classes for specifying addresses and other low-level constants.