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