3 #ifndef ROSE_BinaryAnalysis_AssemblerX86_H
4 #define ROSE_BinaryAnalysis_AssemblerX86_H
6 #include <featureTests.h>
7 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
11 namespace BinaryAnalysis {
32 : honor_operand_types(
false) {
48 honor_operand_types = b;
54 return honor_operand_types;
72 static const unsigned od_e_mask = 0x00000070;
73 static const unsigned od_e_pres = 0x00000080;
74 static const unsigned od_e0 = 0x00000000 | od_e_pres;
75 static const unsigned od_e1 = 0x00000010 | od_e_pres;
76 static const unsigned od_e2 = 0x00000020 | od_e_pres;
77 static const unsigned od_e3 = 0x00000030 | od_e_pres;
78 static const unsigned od_e4 = 0x00000040 | od_e_pres;
79 static const unsigned od_e5 = 0x00000050 | od_e_pres;
80 static const unsigned od_e6 = 0x00000060 | od_e_pres;
81 static const unsigned od_e7 = 0x00000070 | od_e_pres;
82 static size_t od_e_val(
unsigned opcode_mods) {
return (opcode_mods & od_e_mask)>>4; }
87 static const unsigned od_rex_pres = 0x00000001;
88 static const unsigned od_rex_mask = 0x00000f00;
89 static const unsigned od_rex = 0x00000000 | od_rex_pres;
90 static const unsigned od_rexb = 0x00000100 | od_rex_pres;
91 static const unsigned od_rexx = 0x00000200 | od_rex_pres;
92 static const unsigned od_rexxb = 0x00000300 | od_rex_pres;
93 static const unsigned od_rexr = 0x00000400 | od_rex_pres;
94 static const unsigned od_rexrb = 0x00000500 | od_rex_pres;
95 static const unsigned od_rexrx = 0x00000600 | od_rex_pres;
96 static const unsigned od_rexrxb = 0x00000700 | od_rex_pres;
97 static const unsigned od_rexw = 0x00000800 | od_rex_pres;
98 static const unsigned od_rexwb = 0x00000900 | od_rex_pres;
99 static const unsigned od_rexwx = 0x00000a00 | od_rex_pres;
100 static const unsigned od_rexwxb = 0x00000b00 | od_rex_pres;
101 static const unsigned od_rexwr = 0x00000c00 | od_rex_pres;
102 static const unsigned od_rexwrb = 0x00000d00 | od_rex_pres;
103 static const unsigned od_rexwrx = 0x00000e00 | od_rex_pres;
104 static const unsigned od_rexwrxb = 0x00000f00 | od_rex_pres;
105 static uint8_t od_rex_byte(
unsigned opcode_mods) {
return 0x40 | ((opcode_mods & od_rex_mask) >> 8); }
109 static const unsigned od_modrm = 0x00000002;
113 static const unsigned od_c_mask = 0x00007000;
114 static const unsigned od_cb = 0x00001000;
115 static const unsigned od_cw = 0x00002000;
116 static const unsigned od_cd = 0x00003000;
117 static const unsigned od_cp = 0x00004000;
118 static const unsigned od_co = 0x00005000;
119 static const unsigned od_ct = 0x00006000;
123 static const unsigned od_i_mask = 0x00070000;
124 static const unsigned od_ib = 0x00010000;
125 static const unsigned od_iw = 0x00020000;
126 static const unsigned od_id = 0x00030000;
127 static const unsigned od_io = 0x00040000;
131 static const unsigned od_r_mask = 0x00700000;
132 static const unsigned od_rb = 0x00100000;
133 static const unsigned od_rw = 0x00200000;
134 static const unsigned od_rd = 0x00300000;
135 static const unsigned od_ro = 0x00400000;
140 static const unsigned od_i = 0x00000004;
327 static const unsigned COMPAT_LEGACY = 0x01;
328 static const unsigned COMPAT_64 = 0x02;
331 static uint8_t build_modrm(
unsigned mod,
unsigned reg,
unsigned rm) {
332 return ((mod&0x3)<<6) | ((reg&0x7)<<3) | (rm&0x7);
336 static unsigned modrm_mod(uint8_t modrm) {
return modrm>>6; }
339 static unsigned modrm_reg(uint8_t modrm) {
return (modrm>>3) & 0x7; }
342 static unsigned modrm_rm(uint8_t modrm) {
return modrm & 0x7; }
345 static uint8_t build_sib(
unsigned ss,
unsigned index,
unsigned base) {
346 return ((ss&0x3)<<6) | ((index&0x7)<<3) | (base&0x7);
350 static unsigned sib_ss(uint8_t sib) {
return sib>>6; }
353 static unsigned sib_index(uint8_t sib) {
return (sib>>3) & 0x7; }
356 static unsigned sib_base(uint8_t sib) {
return sib & 0x7; }
363 InsnDefn(
const std::string &mnemonic,
X86InstructionKind kind,
unsigned compatibility, uint64_t opcode,
364 unsigned opcode_modifiers, OperandDefn op1=od_none, OperandDefn op2=od_none, OperandDefn op3=od_none,
365 OperandDefn op4=od_none)
366 : mnemonic(mnemonic), kind(kind), compatibility(compatibility), opcode(opcode), opcode_modifiers(opcode_modifiers) {
367 if (op1) operands.push_back(op1);
368 if (op2) operands.push_back(op2);
369 if (op3) operands.push_back(op3);
370 if (op4) operands.push_back(op4);
372 std::string to_str()
const;
373 void set_location(
const std::string &s) {
376 std::string mnemonic;
378 unsigned compatibility;
380 unsigned opcode_modifiers;
381 std::vector<OperandDefn> operands;
382 std::string location;
385 enum MemoryReferencePattern
398 typedef std::vector<const InsnDefn*> DictionaryPage;
401 typedef std::map<X86InstructionKind, DictionaryPage> InsnDictionary;
404 static void initAssemblyRules();
405 static void initAssemblyRules_part1();
406 static void initAssemblyRules_part2();
407 static void initAssemblyRules_part3();
408 static void initAssemblyRules_part4();
409 static void initAssemblyRules_part5();
410 static void initAssemblyRules_part6();
411 static void initAssemblyRules_part7();
412 static void initAssemblyRules_part8();
413 static void initAssemblyRules_part9();
416 static void define(
const InsnDefn *d) {
417 defns[d->kind].push_back(d);
430 SgUnsignedCharList fixup_prefix_bytes(
SgAsmX86Instruction *insn, SgUnsignedCharList source);
439 void matches(
const InsnDefn *defn,
SgAsmX86Instruction *insn, int64_t *disp, int64_t *imm)
const;
458 uint8_t *sib, int64_t *displacement, uint8_t *rex)
const;
461 void build_modreg(
const InsnDefn*,
SgAsmX86Instruction*,
size_t argno, uint8_t *modrm, uint8_t *rex)
const;
467 static InsnDictionary defns;
468 bool honor_operand_types;
Base class for references to a machine register.
Base class for machine instructions.
bool get_honor_operand_types() const
Returns true if the assembler is honoring operand data types, or false if the assembler is using the ...
Main namespace for the ROSE library.
This class contains methods for assembling x86 instructions (SgAsmX86Instruction).
Reference to memory locations.
virtual SgUnsignedCharList assembleOne(SgAsmInstruction *)
Assemble an instruction (SgAsmInstruction) into byte code.
Represents one Intel x86 machine instruction.
Base class for expressions.
virtual SgUnsignedCharList assembleProgram(const std::string &source)
Assemble an x86 program from assembly source code using the nasm assembler.
X86InstructionKind
List of all x86 instructions known to the ROSE disassembler/assembler.
void set_honor_operand_types(bool b)
Causes the assembler to honor (if true) or disregard (if false) the data types of operands when assem...
Virtual base class for instruction assemblers.