retdec
ir_modifier.h
Go to the documentation of this file.
1 
7 #ifndef RETDEC_BIN2LLVMIR_UTILS_IR_MODIFIER_H
8 #define RETDEC_BIN2LLVMIR_UTILS_IR_MODIFIER_H
9 
10 #include <llvm/IR/Instructions.h>
11 #include <llvm/IR/Module.h>
12 
16 
17 namespace retdec {
18 namespace bin2llvmir {
19 
21 {
22  public:
23  using FunctionPair = std::pair<llvm::Function*, retdec::common::Function*>;
24  using StackPair = std::pair<llvm::AllocaInst*, const retdec::common::Object*>;
25 
26  // Methods not using member data -> do not need instance of this class.
27  // Can be used simply like this: \c IrModifier::method().
28  //
29  public:
30  static llvm::AllocaInst* createAlloca(
31  llvm::Function* fnc,
32  llvm::Type* ty,
33  const std::string& name = std::string());
34 
35  static llvm::Value* convertValueToType(
36  llvm::Value* val,
37  llvm::Type* type,
38  llvm::Instruction* before);
39 
40  static llvm::Value* convertValueToTypeAfter(
41  llvm::Value* val,
42  llvm::Type* type,
43  llvm::Instruction* after);
44 
45  static llvm::Constant* convertConstantToType(
46  llvm::Constant* val,
47  llvm::Type* type);
48 
49  static llvm::CallInst* modifyCallInst(
50  llvm::CallInst* call,
51  llvm::Type* ret,
52  llvm::ArrayRef<llvm::Value*> args);
53 
54  static void eraseUnusedInstructionRecursive(llvm::Value* insn);
56  std::unordered_set<llvm::Value*>& insns);
57 
58  public:
59  IrModifier(llvm::Module* m, Config* c);
60 
61  // Methods using member data -> need instance of this class.
62  //
63  public:
65  llvm::Function* fnc,
66  const std::string& fncName);
67 
69  llvm::Function* fnc,
70  int offset,
71  llvm::Type* type,
72  const std::string& name = std::string(),
73  const std::string& realName = std::string(),
74  bool fromDebug = false);
75 
76  llvm::GlobalVariable* getGlobalVariable(
77  FileImage* objf,
78  DebugFormat* dbgf,
80  bool strict = false,
81  const std::string& name = std::string());
82 
83  llvm::Value* changeObjectType(
84  FileImage* objf,
85  llvm::Value* val,
86  llvm::Type* toType,
87  llvm::Constant* init = nullptr,
88  std::unordered_set<llvm::Instruction*>* instToErase = nullptr,
89  bool dbg = false,
90  bool wideString = false);
91 
93  llvm::Function* fnc,
94  llvm::Type* ret,
95  std::vector<llvm::Type*> args,
96  bool isVarArg = false,
97  const std::map<llvm::ReturnInst*, llvm::Value*>& rets2vals =
98  std::map<llvm::ReturnInst*, llvm::Value*>(),
99  const std::map<llvm::CallInst*, std::vector<llvm::Value*>>& calls2vals =
100  std::map<llvm::CallInst*, std::vector<llvm::Value*>>(),
101  llvm::Value* retVal = nullptr,
102  const std::vector<llvm::Value*>& argStores =
103  std::vector<llvm::Value*>(),
104  const std::vector<std::string>& argNames = std::vector<std::string>());
105 
106  llvm::Argument* modifyFunctionArgumentType(
107  llvm::Argument* arg,
108  llvm::Type* type);
109 
110  protected:
111  llvm::Value* changeObjectDeclarationType(
112  FileImage* objf,
113  llvm::Value* val,
114  llvm::Type* toType,
115  llvm::Constant* init = nullptr,
116  bool wideString = false);
117 
118  protected:
119  llvm::Module* _module = nullptr;
120  Config* _config = nullptr;
121 };
122 
123 } // namespace bin2llvmir
124 } // namespace retdec
125 
126 #endif
ABI information.
Config DB provider for bin2llvmirl.
Definition: config.h:24
Definition: debugformat.h:20
Definition: fileimage.h:27
Definition: ir_modifier.h:21
Config * _config
Definition: ir_modifier.h:120
llvm::Value * changeObjectDeclarationType(FileImage *objf, llvm::Value *val, llvm::Type *toType, llvm::Constant *init=nullptr, bool wideString=false)
Definition: ir_modifier.cpp:724
llvm::Module * _module
Definition: ir_modifier.h:119
static llvm::AllocaInst * createAlloca(llvm::Function *fnc, llvm::Type *ty, const std::string &name=std::string())
Definition: ir_modifier.cpp:1394
llvm::Value * changeObjectType(FileImage *objf, llvm::Value *val, llvm::Type *toType, llvm::Constant *init=nullptr, std::unordered_set< llvm::Instruction * > *instToErase=nullptr, bool dbg=false, bool wideString=false)
Definition: ir_modifier.cpp:812
FunctionPair renameFunction(llvm::Function *fnc, const std::string &fncName)
Definition: ir_modifier.cpp:505
static llvm::Value * convertValueToTypeAfter(llvm::Value *val, llvm::Type *type, llvm::Instruction *after)
Definition: ir_modifier.cpp:1434
llvm::GlobalVariable * getGlobalVariable(FileImage *objf, DebugFormat *dbgf, retdec::common::Address addr, bool strict=false, const std::string &name=std::string())
Definition: ir_modifier.cpp:596
IrModifier(llvm::Module *m, Config *c)
Definition: ir_modifier.cpp:498
StackPair getStackVariable(llvm::Function *fnc, int offset, llvm::Type *type, const std::string &name=std::string(), const std::string &realName=std::string(), bool fromDebug=false)
Definition: ir_modifier.cpp:544
static void eraseUnusedInstructionsRecursive(std::unordered_set< llvm::Value * > &insns)
Definition: ir_modifier.cpp:1562
static llvm::CallInst * modifyCallInst(llvm::CallInst *call, llvm::Type *ret, llvm::ArrayRef< llvm::Value * > args)
Definition: ir_modifier.cpp:1477
static llvm::Value * convertValueToType(llvm::Value *val, llvm::Type *type, llvm::Instruction *before)
Definition: ir_modifier.cpp:1420
std::pair< llvm::Function *, retdec::common::Function * > FunctionPair
Definition: ir_modifier.h:23
llvm::Argument * modifyFunctionArgumentType(llvm::Argument *arg, llvm::Type *type)
Definition: ir_modifier.cpp:1365
static void eraseUnusedInstructionRecursive(llvm::Value *insn)
Definition: ir_modifier.cpp:1557
static llvm::Constant * convertConstantToType(llvm::Constant *val, llvm::Type *type)
Definition: ir_modifier.cpp:1450
FunctionPair modifyFunction(llvm::Function *fnc, llvm::Type *ret, std::vector< llvm::Type * > args, bool isVarArg=false, const std::map< llvm::ReturnInst *, llvm::Value * > &rets2vals=std::map< llvm::ReturnInst *, llvm::Value * >(), const std::map< llvm::CallInst *, std::vector< llvm::Value * >> &calls2vals=std::map< llvm::CallInst *, std::vector< llvm::Value * >>(), llvm::Value *retVal=nullptr, const std::vector< llvm::Value * > &argStores=std::vector< llvm::Value * >(), const std::vector< std::string > &argNames=std::vector< std::string >())
Definition: ir_modifier.cpp:988
std::pair< llvm::AllocaInst *, const retdec::common::Object * > StackPair
Definition: ir_modifier.h:24
Definition: address.h:21
File image provider for bin2llvmirl.
The frontend-end part of the decompiler.
Definition: archive_wrapper.h:19