ROSE 0.11.145.134
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 // Fix the mnemonic to be something more human readable.
72 std::string fixMnemonic(const std::string&, arm_cc);
73
74 // Make a ROSE instruction operand from a Capstone operand
75 SgAsmExpression* makeOperand(const cs_insn&, const cs_arm_op&);
76
77 // Add shift or rotate operations to an expression based on the Capstone operand.
78 SgAsmExpression* shiftOrRotate(SgAsmExpression*, const cs_arm_op&);
79
80 // Change a memory reference expression's address by wrapping it in a SgAsmPreIncrementExpression or SgAsmPostIncrement
81 // expression if necessary.
82 void wrapPrePostIncrement(SgAsmAarch32Instruction*, const cs_insn&, const uint32_t insnWord);
83
84 // Convert a Capstone register number to a ROSE register descriptor. ROSE's register descriptor are a lot more than just an
85 // ID number.
86 RegisterDescriptor makeRegister(arm_reg);
87
88 // Convert a Capstone system register number to a ROSE expression.
89 SgAsmExpression* makeSystemRegister(arm_sysreg);
90
91 // Create a register descriptor for a coprocessor register.
92 RegisterDescriptor makeCoprocRegister(int registerNumber);
93
94 // Restrict a register to just part of a register
95 RegisterDescriptor subRegister(RegisterDescriptor, int idx);
96
97 // Return a type for register.
98 SgAsmType* registerType(RegisterDescriptor);
99
100 // Capstone doesn't return information about how much memory is read for a memory read operand. Therefore, we need to
101 // partially decode instructions ourselves to get this information.
102 SgAsmType* typeForMemoryRead(const cs_insn&);
103
104 // Returns the opcode as a 32-bit value.
105 uint32_t opcode(const cs_insn&);
106};
107
108} // namespace
109} // namespace
110} // namespace
111
112#endif
113#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.