retdec
ctor_dtor.h
Go to the documentation of this file.
1 
7 #ifndef RETDEC_BIN2LLVMIR_ANALYSES_CTOR_DTOR_H
8 #define RETDEC_BIN2LLVMIR_ANALYSES_CTOR_DTOR_H
9 
10 #include <map>
11 #include <set>
12 
13 #include <llvm/IR/Module.h>
14 
17 
18 namespace retdec {
19 namespace bin2llvmir {
20 
21 class CtorDtor
22 {
23  public:
25  {
26  public:
28  std::vector<llvm::CallInst*> superMethods;
30  std::vector<int> superMethodOffsets;
32  std::vector<std::pair<llvm::StoreInst*,
35  std::vector<int> vftableOffsets;
36  bool ctor = false;
37  bool dtor = false;
38  };
39 
40  using FunctionSet = std::set<llvm::Function*>;
41  using FunctionToInfo = std::map<llvm::Function*, FunctionInfo>;
42  using StoreToVtable = std::map<llvm::StoreInst*, const common::Vtable*>;
43 
44  public:
45  void runOnModule(llvm::Module* m, Config* c, FileImage* i);
47 
48  private:
50  void analyseFunction(llvm::Function* fnc);
51  FunctionInfo analyseFunctionForward(llvm::Function* fnc);
52  FunctionInfo analyseFunctionBackward(llvm::Function* fnc);
53  int getOffset(llvm::Value* ecxStoreOp);
54  llvm::StoreInst* findPreviousStoreToECX(llvm::Instruction* inst);
55  void propagateCtorDtor();
56 
57  template<class T>
58  FunctionInfo analyseFunctionCommon(T begin, T end);
59 
60  private:
61  llvm::Module *module = nullptr;
62  Config* config = nullptr;
63  FileImage* image = nullptr;
64 
68 };
69 
70 template<class T>
72 {
73  enum
74  {
75  STEP_SUPER,
76  STEP_VTABLES
77  } step = STEP_SUPER;
78 
80 
81  for (T it = begin; it != end; ++it)
82  {
83  llvm::Instruction *i = &(*it);
84  if (step == STEP_SUPER)
85  {
86  if (llvm::CallInst *call = llvm::dyn_cast<llvm::CallInst>(i))
87  {
88  if (possibleCtorsDtors.count(call->getCalledFunction()))
89  {
90  result.superMethods.push_back(call);
91  }
92  }
93  }
94  if (step == STEP_SUPER || step == STEP_VTABLES)
95  {
96  if (llvm::StoreInst *store = llvm::dyn_cast<llvm::StoreInst>(i))
97  {
98  auto fIt = stores2vtables.find(store);
99  if (fIt != stores2vtables.end())
100  {
101  result.vftableStores.push_back( {store, fIt->second} );
102  step = STEP_VTABLES;
103  }
104  }
105  }
106  }
107 
108  return result;
109 }
110 
111 } // namespace bin2llvmir
112 } // namespace retdec
113 
114 #endif
Config DB provider for bin2llvmirl.
Definition: config.h:24
std::vector< int > superMethodOffsets
Super method offsets in order.
Definition: ctor_dtor.h:30
std::vector< int > vftableOffsets
Virtual table offsets in order.
Definition: ctor_dtor.h:35
bool dtor
Definition: ctor_dtor.h:37
bool ctor
Definition: ctor_dtor.h:36
std::vector< std::pair< llvm::StoreInst *, const common::Vtable * > > vftableStores
Virtual table stores in order.
Definition: ctor_dtor.h:33
std::vector< llvm::CallInst * > superMethods
Super method calls in order.
Definition: ctor_dtor.h:28
Definition: ctor_dtor.h:22
FunctionInfo analyseFunctionForward(llvm::Function *fnc)
Definition: ctor_dtor.cpp:148
FunctionSet possibleCtorsDtors
Definition: ctor_dtor.h:65
FileImage * image
Definition: ctor_dtor.h:63
FunctionInfo analyseFunctionBackward(llvm::Function *fnc)
Definition: ctor_dtor.cpp:160
int getOffset(llvm::Value *ecxStoreOp)
Definition: ctor_dtor.cpp:174
llvm::Module * module
Definition: ctor_dtor.h:61
StoreToVtable stores2vtables
Definition: ctor_dtor.h:66
FunctionToInfo & getResults()
Definition: ctor_dtor.cpp:39
std::map< llvm::StoreInst *, const common::Vtable * > StoreToVtable
Definition: ctor_dtor.h:42
void findPossibleCtorsDtors()
Definition: ctor_dtor.cpp:48
void analyseFunction(llvm::Function *fnc)
Definition: ctor_dtor.cpp:86
llvm::StoreInst * findPreviousStoreToECX(llvm::Instruction *inst)
Definition: ctor_dtor.cpp:208
FunctionInfo analyseFunctionCommon(T begin, T end)
Definition: ctor_dtor.h:71
std::map< llvm::Function *, FunctionInfo > FunctionToInfo
Definition: ctor_dtor.h:41
Config * config
Definition: ctor_dtor.h:62
FunctionToInfo function2info
Definition: ctor_dtor.h:67
void runOnModule(llvm::Module *m, Config *c, FileImage *i)
Definition: ctor_dtor.cpp:23
void propagateCtorDtor()
Definition: ctor_dtor.cpp:221
std::set< llvm::Function * > FunctionSet
Definition: ctor_dtor.h:40
Definition: fileimage.h:27
Definition: vtable.h:68
File image provider for bin2llvmirl.
The frontend-end part of the decompiler.
Definition: archive_wrapper.h:19