retdec
abi.h
Go to the documentation of this file.
1 
7 #ifndef RETDEC_BIN2LLVMIR_PROVIDERS_ABI_ABI_H
8 #define RETDEC_BIN2LLVMIR_PROVIDERS_ABI_ABI_H
9 
10 #include <map>
11 #include <memory>
12 #include <set>
13 #include <vector>
14 
15 #include <llvm/IR/Module.h>
16 
20 
21 //#include "retdec/capstone2llvmir/x86/x86_defs.h"
22 
23 namespace retdec {
24 namespace bin2llvmir {
25 
26 class Abi
27 {
28  // Constants.
29  //
30  public:
31  static const uint32_t REG_INVALID;
32  static const unsigned DEFAULT_ADDR_SPACE;
33 
34  // Ctors, dtors.
35  //
36  public:
37  Abi(llvm::Module* m, Config* c);
38  virtual ~Abi() = default;
39 
40  // Registers.
41  //
42  public:
43  bool isRegister(const llvm::Value* val) const;
44  bool isRegister(const llvm::Value* val, uint32_t r) const;
45  bool isFlagRegister(const llvm::Value* val);
46  bool isStackPointerRegister(const llvm::Value* val) const;
47  bool isZeroRegister(const llvm::Value* val);
48  virtual bool isGeneralPurposeRegister(const llvm::Value* val) const = 0;
49 
50  llvm::GlobalVariable* getRegister(uint32_t r, bool use = true) const;
51  uint32_t getRegisterId(const llvm::Value* r) const;
52  const std::vector<llvm::GlobalVariable*>& getRegisters() const;
53  llvm::GlobalVariable* getStackPointerRegister() const;
54  llvm::GlobalVariable* getZeroRegister() const;
55 
56  std::size_t getRegisterByteSize(uint32_t r) const;
57 
58  void addRegister(uint32_t id, llvm::GlobalVariable* reg);
59 
60  llvm::GlobalVariable* getSyscallIdRegister();
61  llvm::GlobalVariable* getSyscallReturnRegister();
62  llvm::GlobalVariable* getSyscallArgumentRegister(unsigned n);
63 
64  // Stacks.
65  //
66  public:
67  bool isStackVariable(const llvm::Value* val) const;
68 
69  // Instructions.
70  //
71  public:
73  virtual bool isNopInstruction(cs_insn* insn) = 0;
74 
75  // Types.
76  //
77  public:
78  virtual std::size_t getTypeByteSize(llvm::Type* t) const;
79  virtual std::size_t getTypeBitSize(llvm::Type* t) const;
80  llvm::IntegerType* getDefaultType() const;
81  llvm::PointerType* getDefaultPointerType() const;
82  std::size_t getWordSize() const;
83 
84  static std::size_t getTypeByteSize(llvm::Module* m, llvm::Type* t);
85  static std::size_t getTypeBitSize(llvm::Module* m, llvm::Type* t);
86  static llvm::IntegerType* getDefaultType(llvm::Module* m);
87  static llvm::Type* getDefaultFPType(llvm::Module* m);
88  static llvm::PointerType* getDefaultPointerType(llvm::Module* m);
89  static std::size_t getWordSize(llvm::Module* m);
90 
91  // Architectures.
92  //
93  public:
94  bool isMips() const;
95  bool isMips64() const;
96  bool isArm() const;
97  bool isArm64() const;
98  bool isX86() const;
99  bool isX64() const;
100  bool isPowerPC() const;
101  bool isPowerPC64() const;
102  bool isPic32() const;
103 
104  // Calling conventions.
105  //
106  public:
108  const CallingConvention::ID& cc);
110  const CallingConvention::ID& cc);
113 
114  // Config.
115  //
116  public:
117  Config* getConfig() const;
118 
119  // Private data - misc.
120  //
121  protected:
122  llvm::Module* _module = nullptr;
123  Config* _config = nullptr;
124 
125  // Private data - registers.
126  //
127  protected:
130  std::vector<llvm::GlobalVariable*> _regs;
132  std::vector<llvm::GlobalVariable*> _id2regs;
135  std::map<const llvm::Value*, uint32_t> _regs2id;
136 
141 
143  std::vector<uint32_t> _syscallRegs;
150 
151  // Private data - calling convention
152  //
153  protected:
154  std::map<CallingConvention::ID, CallingConvention::Ptr> _id2cc;
156 
157 };
158 
160 {
161  public:
162  static Abi* addAbi(
163  llvm::Module* m,
164  Config* c);
165  static Abi* getAbi(llvm::Module* m);
166  static bool getAbi(llvm::Module* m, Abi*& abi);
167  static void clear();
168 
169  private:
170  static std::map<llvm::Module*, std::unique_ptr<Abi>> _module2abi;
171 };
172 
173 } // namespace bin2llvmir
174 } // namespace retdec
175 
176 #endif
Mapping of LLVM instructions to underlying ASM instructions.
Calling convention information.
Config DB provider for bin2llvmirl.
Definition: abi.h:160
static std::map< llvm::Module *, std::unique_ptr< Abi > > _module2abi
Definition: abi.h:170
static void clear()
Definition: abi.cpp:371
static Abi * getAbi(llvm::Module *m)
Definition: abi.cpp:359
static Abi * addAbi(llvm::Module *m, Config *c)
Definition: abi.cpp:302
Definition: abi.h:27
virtual std::size_t getTypeByteSize(llvm::Type *t) const
Definition: abi.cpp:152
CallingConvention::ID _defcc
Definition: abi.h:155
bool isArm64() const
Definition: abi.cpp:232
std::vector< llvm::GlobalVariable * > _regs
Definition: abi.h:130
llvm::Module * _module
Definition: abi.h:122
virtual std::size_t getTypeBitSize(llvm::Type *t) const
Definition: abi.cpp:157
std::size_t getRegisterByteSize(uint32_t r) const
Definition: abi.cpp:103
bool isMips() const
Definition: abi.cpp:217
CallingConvention * getDefaultCallingConvention()
Definition: abi.cpp:272
bool isX64() const
Definition: abi.cpp:242
void addRegister(uint32_t id, llvm::GlobalVariable *reg)
Definition: abi.cpp:116
virtual bool isNopInstruction(cs_insn *insn)=0
Config * _config
Definition: abi.h:123
uint32_t _regStackPointerId
ID of stack pointer register.
Definition: abi.h:138
llvm::GlobalVariable * getStackPointerRegister() const
Definition: abi.cpp:93
std::size_t getWordSize() const
Definition: abi.cpp:172
bool isArm() const
Definition: abi.cpp:227
bool isZeroRegister(const llvm::Value *val)
Definition: abi.cpp:59
const std::vector< llvm::GlobalVariable * > & getRegisters() const
Definition: abi.cpp:88
bool isFlagRegister(const llvm::Value *val)
Definition: abi.cpp:48
uint32_t _regSyscallId
Register used to pass system call ID.
Definition: abi.h:147
bool isPic32() const
Definition: abi.cpp:257
bool supportsCallingConvention(const CallingConvention::ID &cc)
Definition: abi.cpp:262
std::map< CallingConvention::ID, CallingConvention::Ptr > _id2cc
Definition: abi.h:154
CallingConvention * getCallingConvention(const CallingConvention::ID &cc)
Definition: abi.cpp:277
std::map< const llvm::Value *, uint32_t > _regs2id
Definition: abi.h:135
bool isPowerPC() const
Definition: abi.cpp:247
static llvm::Type * getDefaultFPType(llvm::Module *m)
Definition: abi.cpp:200
uint32_t getRegisterId(const llvm::Value *r) const
Definition: abi.cpp:82
llvm::GlobalVariable * getZeroRegister() const
Definition: abi.cpp:98
llvm::GlobalVariable * getSyscallReturnRegister()
Definition: abi.cpp:132
llvm::GlobalVariable * getRegister(uint32_t r, bool use=true) const
Definition: abi.cpp:72
llvm::IntegerType * getDefaultType() const
Definition: abi.cpp:162
Abi(llvm::Module *m, Config *c)
Definition: abi.cpp:31
bool isPowerPC64() const
Definition: abi.cpp:252
static const unsigned DEFAULT_ADDR_SPACE
Definition: abi.h:32
llvm::GlobalVariable * getSyscallArgumentRegister(unsigned n)
Definition: abi.cpp:137
uint32_t _regFunctionReturnId
ID of register where function return values are stored.
Definition: abi.h:140
Config * getConfig() const
Definition: abi.cpp:289
std::vector< llvm::GlobalVariable * > _id2regs
Fast "capstone id -> LLVM value" search.
Definition: abi.h:132
bool isNopInstruction(AsmInstruction ai)
Definition: abi.cpp:147
std::vector< uint32_t > _syscallRegs
Ordered list of registers used in system calls.
Definition: abi.h:143
bool isRegister(const llvm::Value *val) const
Definition: abi.cpp:38
uint32_t _regZeroReg
Register that is always equal to zero - not every arch have this.
Definition: abi.h:149
uint32_t _regSyscallReturn
Register used for returning values from system calls.
Definition: abi.h:145
llvm::PointerType * getDefaultPointerType() const
Definition: abi.cpp:167
llvm::GlobalVariable * getSyscallIdRegister()
Definition: abi.cpp:127
virtual ~Abi()=default
bool isStackPointerRegister(const llvm::Value *val) const
Definition: abi.cpp:54
CallingConvention::ID getDefaultCallingConventionID() const
Definition: abi.cpp:267
bool isX86() const
Definition: abi.cpp:237
bool isMips64() const
Definition: abi.cpp:222
virtual bool isGeneralPurposeRegister(const llvm::Value *val) const =0
static const uint32_t REG_INVALID
Definition: abi.h:31
bool isStackVariable(const llvm::Value *val) const
Definition: abi.cpp:142
Definition: asm_instruction.h:34
Definition: calling_convention.h:22
Definition: config.h:24
eCC
Definition: calling_convention.h:24
The frontend-end part of the decompiler.
Definition: archive_wrapper.h:19