retdec
module.h
Go to the documentation of this file.
1 
7 #ifndef RETDEC_LLVMIR2HLL_IR_MODULE_H
8 #define RETDEC_LLVMIR2HLL_IR_MODULE_H
9 
10 #include <cstddef>
11 #include <functional>
12 #include <map>
13 #include <string>
14 
19 
20 namespace llvm {
21 
22 class Module;
23 
24 } // namespace llvm
25 
26 namespace retdec {
27 namespace llvmir2hll {
28 
29 class Config;
30 class Expression;
31 class Function;
32 class GlobalVarDef;
33 class Semantics;
34 class Variable;
35 
42 class Module final: private retdec::utils::NonCopyable {
43 public:
45  using global_var_iterator = GlobalVarDefVector::const_iterator;
46 
48  using func_iterator = FuncVector::const_iterator;
49 
52 
53 public:
54  Module(const llvm::Module *llvmModule, const std::string &identifier,
56 
57  const llvm::Module *getLLVMModule() const;
58  std::string getIdentifier(bool stripDirs = true) const;
60  ShPtr<Config> getConfig() const;
61 
64  void addGlobalVar(ShPtr<Variable> var, ShPtr<Expression> init = nullptr);
66  bool isGlobalVar(ShPtr<Variable> var) const;
67  bool isGlobalVarStoringStringLiteral(const std::string &varName) const;
69  ShPtr<Variable> getGlobalVarByName(const std::string &varName) const;
70  bool hasGlobalVars() const;
71  bool hasGlobalVar(const std::string &name) const;
72  VarSet getGlobalVars() const;
74  std::string getRegisterForGlobalVar(ShPtr<Variable> var) const;
76 
80 
83  void addFunc(ShPtr<Function> func);
84  void removeFunc(ShPtr<Function> func);
85  bool funcExists(ShPtr<Function> func) const;
86  ShPtr<Function> getFuncByName(const std::string &funcName) const;
87  bool hasFuncWithName(const std::string &funcName) const;
88  bool correspondsToFunc(ShPtr<Variable> var) const;
89  bool hasMainFunc() const;
90  bool isMainFunc(ShPtr<Function> func) const;
91  VarSet getVarsForFuncs() const;
92  std::size_t getNumOfFuncDefinitions() const;
93  bool hasFuncDefinitions() const;
94 
95  bool hasDecompilerDefinedFuncs() const;
97 
98  bool hasUserDefinedFuncs() const;
100 
101  bool hasStaticallyLinkedFuncs() const;
104 
105  bool hasDynamicallyLinkedFuncs() const;
107 
108  bool hasSyscallFuncs() const;
109  FuncSet getSyscallFuncs() const;
110 
111  bool hasInstructionIdiomFuncs() const;
113 
114  bool isExportedFunc(ShPtr<Function> func) const;
115 
116  std::string getRealNameForFunc(ShPtr<Function> func) const;
117  std::string getDeclarationStringForFunc(ShPtr<Function> func) const;
118  std::string getCommentForFunc(ShPtr<Function> func) const;
120  std::string getWrappedFuncName(ShPtr<Function> func) const;
121  std::string getDemangledNameOfFunc(ShPtr<Function> func) const;
122 
124  bool hasAddressRange(ShPtr<Function> func) const;
126 
128  bool hasLineRange(ShPtr<Function> func) const;
129  bool allFuncDefinitionsHaveLineRange() const;
130 
131  func_iterator func_begin() const;
132  func_iterator func_end() const;
133 
136 
140 
143  std::string comesFromGlobalVar(ShPtr<Function> func, ShPtr<Variable> var) const;
145 
148  bool hasClasses() const;
149  StringSet getClassNames() const;
150  std::string getClassForFunc(ShPtr<Function> func) const;
151  std::string getTypeOfFuncInClass(ShPtr<Function> func,
152  const std::string &cl) const;
153  StringVector getBaseClassNames(const std::string &cl) const;
154  std::string getDemangledNameOfClass(const std::string &cl) const;
156 
159  bool isDebugInfoAvailable() const;
160 
161  std::string getDebugModuleNameForFunc(ShPtr<Function> func) const;
164 
165  std::string getDebugNameForGlobalVar(ShPtr<Variable> var) const;
166  std::string getDebugNameForLocalVar(ShPtr<Function> func,
167  ShPtr<Variable> var) const;
168 
169  std::string getDebugNameForVar(ShPtr<Variable> var) const;
170  bool hasAssignedDebugName(ShPtr<Variable> var) const;
171  void addDebugNameForVar(ShPtr<Variable> var, const std::string &name);
173 
176  std::size_t getNumberOfFuncsDetectedInFrontend() const;
177  std::string getDetectedCompilerOrPacker() const;
178  std::string getDetectedLanguage() const;
181 
182 private:
184  using FuncAddressRangeMap = std::map<ShPtr<Function>, AddressRange>;
185 
186 private:
188  const llvm::Module *llvmModule;
189 
191  std::string identifier;
192 
195 
198 
201 
204 
207 
208 private:
210  std::function<bool (ShPtr<Function>)> pred
211  ) const;
213  std::function<bool (ShPtr<Function>)> pred
214  ) const;
215 };
216 
217 } // namespace llvmir2hll
218 } // namespace retdec
219 
220 #endif
Definition: range.h:45
A representation of a complete module.
Definition: module.h:42
bool isExportedFunc(ShPtr< Function > func) const
Is the given function exported?
Definition: module.cpp:531
std::string getDebugModuleNameForFunc(ShPtr< Function > func) const
Returns the module name for the given function using debug information.
Definition: module.cpp:821
StringSet getDebugModuleNames() const
Returns the set of all module names for all functions.
Definition: module.cpp:838
std::string getRegisterForGlobalVar(ShPtr< Variable > var) const
Returns the name of the register corresponding to the given global variable.
Definition: module.cpp:188
std::string getWrappedFuncName(ShPtr< Function > func) const
Returns the name of a function that func wraps.
Definition: module.cpp:578
func_filter_iterator func_definition_begin() const
Returns a constant iterator to the first function definition.
Definition: module.cpp:606
void markFuncAsStaticallyLinked(ShPtr< Function > func)
Marks the given function as statically linked.
Definition: module.cpp:450
bool hasClasses() const
Have any classes been found?
Definition: module.cpp:730
StringSet getDetectedCryptoPatternsForFunc(ShPtr< Function > func) const
Returns a set of names of detected cryptographic patterns that the given function uses.
Definition: module.cpp:569
FuncSet getFuncsSatisfyingPredicate(std::function< bool(ShPtr< Function >)> pred) const
Returns a set of functions satisfying the given predicate.
Definition: module.cpp:711
FuncSet getSyscallFuncs() const
Returns all syscall functions in the module.
Definition: module.cpp:493
FuncSet getDecompilerDefinedFuncs() const
Returns all decompiler-defined functions in the module.
Definition: module.cpp:392
const llvm::Module * llvmModule
Original module from which this module has been created.
Definition: module.h:188
bool allFuncDefinitionsHaveAddressRange() const
Checks if all function definitions have an address range.
Definition: module.cpp:656
std::string getDeclarationStringForFunc(ShPtr< Function > func) const
Returns a C declaration string for the given function.
Definition: module.cpp:551
void removeFunc(ShPtr< Function > func)
Removes the given function from the module.
Definition: module.cpp:280
std::string getDetectedLanguage() const
Returns the detected language.
Definition: module.cpp:888
void addFunc(ShPtr< Function > func)
Adds the given function to the module.
Definition: module.cpp:269
bool hasFuncSatisfyingPredicate(std::function< bool(ShPtr< Function >)> pred) const
Is there a function satisfying the given predicate?
Definition: module.cpp:698
std::size_t getNumberOfFuncsDetectedInFrontend() const
Returns the number of functions detected in the front-end.
Definition: module.cpp:862
GlobalVarDefVector globalVars
Global variables.
Definition: module.h:200
void addGlobalVar(ShPtr< Variable > var, ShPtr< Expression > init=nullptr)
Adds a new global variable to the module.
Definition: module.cpp:63
std::string getDebugNameForLocalVar(ShPtr< Function > func, ShPtr< Variable > var) const
Returns the name for the given local variable using debug information.
Definition: module.cpp:792
ShPtr< Semantics > semantics
The used semantics.
Definition: module.h:194
func_filter_iterator func_declaration_end() const
Returns a constant iterator past the last function declaration.
Definition: module.cpp:633
func_filter_iterator func_declaration_begin() const
Returns a constant iterator to the first function declaration.
Definition: module.cpp:623
bool isGlobalVar(ShPtr< Variable > var) const
Returns true if var is a global variable, false otherwise.
Definition: module.cpp:100
ShPtr< Config > getConfig() const
Definition: module.cpp:223
std::string getTypeOfFuncInClass(ShPtr< Function > func, const std::string &cl) const
Returns the type of the given function in the given class.
Definition: module.cpp:771
FuncSet getStaticallyLinkedFuncs() const
Returns all statically linked functions in the module.
Definition: module.cpp:438
func_filter_iterator func_definition_end() const
Returns a constant iterator past the last function definition.
Definition: module.cpp:616
bool funcExists(ShPtr< Function > func) const
Returns true if func exists in the module, false otherwise.
Definition: module.cpp:289
ShPtr< Semantics > getSemantics() const
Returns the semantics of the module.
Definition: module.cpp:219
StringVector getBaseClassNames(const std::string &cl) const
Returns the names of base classes of the given class.
Definition: module.cpp:744
bool hasFuncWithName(const std::string &funcName) const
Returns true if there is a function named funcName, false otherwise.
Definition: module.cpp:335
std::string getDemangledNameOfClass(const std::string &cl) const
Returns the demangled named of the given class.
Definition: module.cpp:751
GlobalVarDefVector::const_iterator global_var_iterator
Global variable iterator.
Definition: module.h:45
func_iterator func_begin() const
Returns a constant iterator to the first function.
Definition: module.cpp:592
bool hasGlobalVar(const std::string &name) const
Checks if the module contains a global variable with the given name.
Definition: module.cpp:239
ShPtr< Variable > getGlobalVarByName(const std::string &varName) const
Returns the global variable named varName.
Definition: module.cpp:146
std::string getClassForFunc(ShPtr< Function > func) const
Returns the name of a class to which the given function belongs.
Definition: module.cpp:760
bool hasLineRange(ShPtr< Function > func) const
Has the given function a line range?
Definition: module.cpp:678
std::string identifier
Identifier of the module.
Definition: module.h:191
ShPtr< Function > getFuncByName(const std::string &funcName) const
Returns the function named funcName.
Definition: module.cpp:322
ShPtr< Expression > getInitForGlobalVar(ShPtr< Variable > var) const
Returns the initializer for the given global variable var.
Definition: module.cpp:127
bool hasSyscallFuncs() const
Are there any syscall functions in the module?
Definition: module.cpp:481
bool hasMainFunc() const
Returns true if there is a main function in the module, false otherwise.
Definition: module.cpp:299
VarSet getExternalGlobalVars() const
Returns the set of external global variables.
Definition: module.cpp:174
FuncVector::const_iterator func_iterator
Function iterator.
Definition: module.h:48
std::string getIdentifier(bool stripDirs=true) const
Returns the identifier of the module.
Definition: module.cpp:212
global_var_iterator global_var_begin() const
Returns a constant iterator to the first global variable.
Definition: module.cpp:251
bool hasUserDefinedFuncs() const
Are there any user-defied functions in the module?
Definition: module.cpp:403
ShPtr< Config > config
The used config.
Definition: module.h:197
FuncVector funcs
Functions.
Definition: module.h:203
VarSet getGlobalVars() const
Returns all global variables (without their initializer).
Definition: module.cpp:163
bool isGlobalVarStoringStringLiteral(const std::string &varName) const
Is a variable with the given name global and stores a string literal?
Definition: module.cpp:112
std::string getCommentForFunc(ShPtr< Function > func) const
Returns a comment for func.
Definition: module.cpp:558
bool hasInstructionIdiomFuncs() const
Are there any syscall functions in the module?
Definition: module.cpp:505
std::string getRealNameForFunc(ShPtr< Function > func) const
Returns the real name of the given function.
Definition: module.cpp:541
StringSet getClassNames() const
Returns the set of found class names.
Definition: module.cpp:737
std::map< ShPtr< Function >, AddressRange > FuncAddressRangeMap
Mapping of a function into an address range.
Definition: module.h:184
std::size_t getNumOfFuncDefinitions() const
Returns the number of defined functions in the module.
Definition: module.cpp:361
FuncSet getDynamicallyLinkedFuncs() const
Returns all dynamically linked functions in the module.
Definition: module.cpp:469
global_var_iterator global_var_end() const
Returns a constant iterator past the last global variable.
Definition: module.cpp:258
bool hasDecompilerDefinedFuncs() const
Are there any decompiler-defined functions in the module?
Definition: module.cpp:380
StringSet getSelectedButNotFoundFuncs() const
Returns a set of functions that were selected to be decompiled but were not found.
Definition: module.cpp:879
std::string getDetectedCryptoPatternForGlobalVar(ShPtr< Variable > var) const
Returns a description of the detected cryptographic pattern for the given global variable.
Definition: module.cpp:196
FuncSet getUserDefinedFuncs() const
Returns all user-defined functions in the module.
Definition: module.cpp:415
bool hasAssignedDebugName(ShPtr< Variable > var) const
Returns true if the given variable has assigned a name from debug information, false otherwise.
Definition: module.cpp:846
Module(const llvm::Module *llvmModule, const std::string &identifier, ShPtr< Semantics > semantics, ShPtr< Config > config)
Constructs a new module.
Definition: module.cpp:43
bool isMainFunc(ShPtr< Function > func) const
Returns true if the given function is the main function, false otherwise.
Definition: module.cpp:310
VarSet getVarsForFuncs() const
Returns variables that correspond to functions in the module.
Definition: module.cpp:350
void removeGlobalVar(ShPtr< Variable > var)
Removes the given variable from the module.
Definition: module.cpp:85
std::string getDebugNameForGlobalVar(ShPtr< Variable > var) const
Returns the name for the given global variable using debug information.
Definition: module.cpp:782
bool isDebugInfoAvailable() const
Is debugging information available?
Definition: module.cpp:810
bool hasFuncDefinitions() const
Returns true if the module contains at least one function definition, false otherwise.
Definition: module.cpp:371
void addDebugNameForVar(ShPtr< Variable > var, const std::string &name)
Adds a name for the given variable using debug information.
Definition: module.cpp:855
bool hasAssignedDebugModuleName(ShPtr< Function > func) const
Has the given function assigned a module name from debug information?
Definition: module.cpp:828
bool hasGlobalVars() const
Returns true if the module contains at least one global variable, false otherwise.
Definition: module.cpp:231
std::string getDetectedCompilerOrPacker() const
Returns the detected compiler/packer.
Definition: module.cpp:871
bool hasAddressRange(ShPtr< Function > func) const
Has the given function an address range?
Definition: module.cpp:649
AddressRange getAddressRangeForFunc(ShPtr< const Function > func) const
Returns the address range for the given function.
Definition: module.cpp:642
const llvm::Module * getLLVMModule() const
Returns the underlying LLVM module of the module.
Definition: module.cpp:203
std::string getDebugNameForVar(ShPtr< Variable > var) const
Returns the name for the given variable using debug information.
Definition: module.cpp:803
bool correspondsToFunc(ShPtr< Variable > var) const
Does the given variable correspond to a function?
Definition: module.cpp:342
LineRange getLineRangeForFunc(ShPtr< Function > func) const
Returns the line range for the given function.
Definition: module.cpp:671
bool hasStaticallyLinkedFuncs() const
Are there any statically linked functions in the module?
Definition: module.cpp:426
std::string getDemangledNameOfFunc(ShPtr< Function > func) const
Returns the demangled name of func.
Definition: module.cpp:585
bool allFuncDefinitionsHaveLineRange() const
Checks if all function definitions have a line range.
Definition: module.cpp:685
std::string comesFromGlobalVar(ShPtr< Function > func, ShPtr< Variable > var) const
Returns the name of a global variable from which the given local variable comes from.
Definition: module.cpp:723
func_iterator func_end() const
Returns a constant iterator past the last function.
Definition: module.cpp:599
bool hasDynamicallyLinkedFuncs() const
Are there any dynamically linked functions in the module?
Definition: module.cpp:457
FuncSet getInstructionIdiomFuncs() const
Returns all syscall functions in the module.
Definition: module.cpp:517
VarStringMap debugVarNameMap
Mapping of a variable into its name in the debug information.
Definition: module.h:206
An adapter of an iterator range in which some elements of the range are skipped.
Definition: filter_iterator.h:40
A mixin to make classes non-copyable.
Definition: non_copyable.h:27
An adapter of an iterator range in which some elements of the range are skipped.
Definition: itanium_ast_ctypes_parser.h:12
A library providing API for working with back-end IR.
std::vector< ShPtr< GlobalVarDef > > GlobalVarDefVector
Vector of global variable definitions.
Definition: types.h:108
std::vector< std::string > StringVector
Vector of strings.
Definition: types.h:87
std::shared_ptr< T > ShPtr
An alias for a shared pointer.
Definition: smart_ptr.h:18
std::vector< ShPtr< Function > > FuncVector
Vector of functions.
Definition: types.h:105
std::set< ShPtr< Variable > > VarSet
Set of variables.
Definition: types.h:57
std::set< std::string > StringSet
Set of strings.
Definition: types.h:51
std::set< ShPtr< Function > > FuncSet
Set of functions.
Definition: types.h:78
std::map< ShPtr< Variable >, std::string > VarStringMap
Mapping of a variable into a string.
Definition: types.h:126
std::pair< std::uint64_t, std::uint64_t > LineRange
Line range.
Definition: types.h:45
std::string stripDirs(const std::string &path)
Strips all directories from the given path.
Definition: string.cpp:799
Definition: archive_wrapper.h:19
A mixin to make classes non-copyable.
Declarations, aliases, macros, etc. for the use of smart pointers.
Aliases for several useful types.