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
|
#include "instructions.h"
#include "registers.h"
#include "write_elf.c"
#include <sys/syscall.h>
static const unsigned char exit_2[] = {
// Exit syscall
MOVWI(REG_EAX, SYS_exit),
// Exit code
MOVWI(REG_EDI, 0x02),
SYSCALL,
};
static const unsigned char some_opcodes[] = {
// rbx 00000001
MOVDI(REG_EBX, 0x1),
// rcx 00000002
MOVBI(REG_CL, 0x2),
// rcx 00000003
ADDB(REG_BL, REG_CL),
// rcx 00000004
ADDW(REG_BX, REG_CX),
// rcx 00000005
ADDD(REG_EBX, REG_ECX),
// rcx 00000006
ADDQ(REG_EBX, REG_ECX),
// rdi 00000006
MOVD(REG_ECX, REG_EDI),
// Exit syscall
MOVWI(REG_EAX, SYS_exit),
SYSCALL,
};
// (x86-64) syscall argument order, rax=syscall:
// rdi rsi rdx r10 r8 r9
static const unsigned char hello_world[] = {
// Write
MOVWI(REG_EDI, 0x1), // stdout
MOVDI(REG_ESI, 0x40101d), // buffer
MOVWI(REG_EDX, 0xc), // count/buffer len
MOVWI(REG_EAX, SYS_write),
SYSCALL,
// Exit 0
MOVWI(REG_EDI, 0x00),
MOVWI(REG_EAX, SYS_exit),
SYSCALL,
// 'hello world\n'
0x68,
0x65,
0x6c,
0x6c,
0x6f,
0x20,
0x77,
0x6f,
0x72,
0x6c,
0x64,
0x0a,
};
static const unsigned char inc_dec_test[] = {
INCB(REG_BL), INCW(REG_BX), INCD(REG_EBX), INCQ(REG_RBX),
DECB(REG_BL), DECW(REG_BX), DECD(REG_EBX), DECQ(REG_RBX),
};
static const unsigned char pushpop[] = {
PUSHBI(0x42), PUSHDI(0x42431337), PUSHW(REG_BX),
PUSHQ(REG_RBX), POPW(REG_BX), POPQ(REG_RBX),
};
int
main(int argc, char* argv[]) {
write_elf("test.out", sizeof(pushpop) / sizeof(pushpop[0]), (char*)pushpop);
return 0;
}
|