ROSE  0.11.145.0
Disassembler/Aarch32.h
1 #ifndef ROSE_BinaryAnalysis_Disassembler_Aarch32_H
2 #define ROSE_BinaryAnalysis_Disassembler_Aarch32_H
3 #include <Rose/BinaryAnalysis/Disassembler/Base.h>
4 #ifdef ROSE_ENABLE_ASM_AARCH32
5 
6 #include <capstone/capstone.h>
7 
8 namespace Rose {
9 namespace BinaryAnalysis {
10 namespace Disassembler {
11 
16 class Aarch32: public Base {
17 public:
19  using Ptr = Aarch32Ptr;
20 
22  enum class Mode {
23  ARM32 = CS_MODE_ARM, // probably zero, not really a bit flag
24  THUMB = CS_MODE_THUMB,
25  MCLASS = CS_MODE_MCLASS,
26  V8 = CS_MODE_V8
27  };
28 
30  using Modes = BitFlags<Mode>;
31 
32 private:
33  Modes modes_; // a subset of Capstone's cs_mode constants (warning: nonorthoganal concepts)
34  csh capstone_; // the capstone handle
35  bool capstoneOpened_ = false; // whether capstone_ is initialized
36 
37 protected:
38  // Constructor for specific architecture. */
39  explicit Aarch32(Modes = Modes());
40 
41 public:
43  static Ptr instanceA32();
44 
46  static Ptr instanceT32();
47 
49  static Ptr instance();
50 
51  ~Aarch32();
52 
53  // overrides
54  bool canDisassemble(SgAsmGenericHeader*) const override;
55  Base::Ptr clone() const override;
56  Unparser::BasePtr unparser() const override;
57  SgAsmInstruction* disassembleOne(const MemoryMap::Ptr&, rose_addr_t startVa, AddressSet *successors=nullptr) override;
58  SgAsmInstruction* makeUnknownInstruction(const Exception&) override;
59 
60 private:
61  // Open the capstone library connection
62  void openCapstone();
63 
64  // Convert the instruction bytes to a 32- or 16- bit integer.
65  uint32_t bytesToWord(size_t nBytes, const uint8_t *bytes);
66 
67  // Replace instruction-pointer expressions with constants if possible, leaving a comment in its place.
68  void commentIpRelative(SgAsmInstruction*);
69 
70  // Fix the mnemonic to be something more human readable.
71  std::string fixMnemonic(const std::string&, arm_cc);
72 
73  // Make a ROSE instruction operand from a Capstone operand
74  SgAsmExpression* makeOperand(const cs_insn&, const cs_arm_op&);
75 
76  // Add shift or rotate operations to an expression based on the Capstone operand.
77  SgAsmExpression* shiftOrRotate(SgAsmExpression*, const cs_arm_op&);
78 
79  // Change a memory reference expression's address by wrapping it in a SgAsmPreIncrementExpression or SgAsmPostIncrement
80  // expression if necessary.
81  void wrapPrePostIncrement(SgAsmAarch32Instruction*, const cs_insn&, const uint32_t insnWord);
82 
83  // Convert a Capstone register number to a ROSE register descriptor. ROSE's register descriptor are a lot more than just an
84  // ID number.
85  RegisterDescriptor makeRegister(arm_reg);
86 
87  // Convert a Capstone system register number to a ROSE expression.
88  SgAsmExpression* makeSystemRegister(arm_sysreg);
89 
90  // Create a register descriptor for a coprocessor register.
91  RegisterDescriptor makeCoprocRegister(int registerNumber);
92 
93  // Restrict a register to just part of a register
94  RegisterDescriptor subRegister(RegisterDescriptor, int idx);
95 
96  // Return a type for register.
97  SgAsmType* registerType(RegisterDescriptor);
98 
99  // Capstone doesn't return information about how much memory is read for a memory read operand. Therefore, we need to
100  // partially decode instructions ourselves to get this information.
101  SgAsmType* typeForMemoryRead(const cs_insn&);
102 
103  // Returns the opcode as a 32-bit value.
104  uint32_t opcode(const cs_insn&);
105 };
106 
107 } // namespace
108 } // namespace
109 } // namespace
110 
111 #endif
112 #endif
Base class for machine instructions.
Sawyer::SharedPointer< Aarch32 > Aarch32Ptr
Reference counted pointer for ARM Aarch32 decoder.
Main namespace for the ROSE library.
MemoryMapPtr Ptr
Reference counting pointer.
Definition: MemoryMap.h:115
Base class for container file headers.
const char * Mode(int64_t)
Convert Rose::AST::cmdline::graphviz_t::Mode enum constant to a string.
Base class for expressions.
Base class for binary types.
Sawyer::SharedPointer< Node > Ptr
Reference counting pointer.