-
Notifications
You must be signed in to change notification settings - Fork 5
/
x86.c
125 lines (115 loc) · 2.47 KB
/
x86.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "compile.h"
#include "imm.h"
#include "operand.h"
#include "virtual_opcodes.h"
#include "util.h"
static int32_t voperand_cast_i32(voperand_t *o)
{
assert(o->type == VOPERAND_IMMEDIATE);
return imm_cast_int32_t(&o->imm);
}
typedef enum
{
EAX,
ECX,
EDX,
EBX,
ESP,
EBP,
ESI,
EDI
} X86_REGISTER;
typedef enum
{
PUSH32r,
PUSH32i8,
PUSH32i32,
PUSH32rmm
} x86_instr_t;
void add(heap_string *s, voperand_t *dst, voperand_t *src)
{
// TODO: build table
// https://qbdi.readthedocs.io/en/stable/architecture_support.html
// of e.g all the ADD specific instructions then match them by their operands (maybe fix VOPERAND types and add
// float/double to that aswell, since it's based on size for now) then just loop through the mappings e.g
// static map_t mappings[] = {{ADD32ri, VOPERAND_REGISTER, VOPERAND_IMMEDIATE32/VOPERAND_IMMEDIATE8};
switch(dst->type)
{
case VOPERAND_REGISTER:
//TODO:
break;
}
}
void push(heap_string *s, voperand_t *op)
{
switch(op->type)
{
case VOPERAND_REGISTER:
db(s, 0x50 + op->reg.index);
break;
case VOPERAND_IMMEDIATE:
db(s, 0x68);
dd(s, voperand_cast_i32(op));
break;
default:
perror("unhandled");
break;
}
}
bool x86(function_t *f, heap_string *s)
{
for(size_t i = 0; i < f->instruction_index; ++i)
{
vinstr_t* instr = &f->instructions[i];
voperand_t* op = &instr->operands[0];
switch(instr->opcode)
{
case VOP_CALL:
db(s, 0xe8);
dd(s, 0x0); // TODO: replace
break;
case VOP_SUB:
assert(instr->numoperands == 2);
sub(s, &instr->operands[0], &instr->operands[1]);
break;
case VOP_MUL:
assert(instr->numoperands == 2);
mul(s, &instr->operands[0], &instr->operands[1]);
break;
case VOP_DIV:
assert(instr->numoperands == 2);
div(s, &instr->operands[0], &instr->operands[1]);
break;
case VOP_ADD:
assert(instr->numoperands == 2);
add(s, &instr->operands[0], &instr->operands[1]);
break;
case VOP_PUSH:
push(s, op);
break;
case VOP_ALLOCA:
{
i32 numbytes = voperand_cast_i32(&instr->operands[0]);
numbytes += 16 - (numbytes % 16);
db(s, 0x81);
db(s, 0xec);
dd(s, numbytes);
}
break;
case VOP_ENTER:
db(s, 0x55); //push ebp
db(s, 0x89); //mov ebp, esp
db(s, 0xe5);
break;
case VOP_LEAVE:
db(s, 0x5d); //pop ebp
db(s, 0x89); //mov esp, ebp
db(s, 0xec);
break;
default:
printf("unhandled opcode %s", vopcode_names[instr->opcode]);
return false;
}
}
return true;
}