-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-glb-ref.c
More file actions
156 lines (124 loc) · 2.46 KB
/
test-glb-ref.c
File metadata and controls
156 lines (124 loc) · 2.46 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <inttypes.h>
#include <capstone/capstone.h>
#include "/home/haosun01/php/php-src/ext/opcache/jit/dynasm/dasm_proto.h"
#include "/home/haosun01/php/php-src/ext/opcache/jit/dynasm/dasm_arm64.h"
int dump_disasm(void* start, size_t size)
{
csh handle;
cs_insn *insn;
size_t count;
if (cs_open(CS_ARCH_ARM64, CS_MODE_ARM, &handle) != CS_ERR_OK)
return -1;
count = cs_disasm(handle, start, size, (uintptr_t)start, 0, &insn);
if (count > 0) {
size_t j;
for (j = 0; j < count; j++) {
printf("0x%"PRIx64":\t%s\t\t%s\n", insn[j].address, insn[j].mnemonic,
insn[j].op_str);
}
cs_free(insn, count);
} else
printf("ERROR: Failed to disassemble given code!\n");
cs_close(&handle);
return 1;
}
static void* link_and_encode(dasm_State** d)
{
size_t sz;
void* buf;
dasm_link(d, &sz);
buf = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
dasm_encode(d, buf);
mprotect(buf, sz, PROT_READ | PROT_EXEC);
dump_disasm(buf, sz); // dump the disassemly
return buf;
}
|.arch arm64
|.section code, stub
|.globals lbl_
|.actionlist bf_actions
static int foo(dasm_State **Dst)
{
|.code
|->foo_0:
| brk #0
|->foo_1:
| brk #1
|->foo_2:
| brk #2
|->foo_3:
| brk #3
|->foo_4:
| brk #4
|->foo_5:
| brk #5
|->foo_6:
| brk #6
|->foo_7:
| brk #7
|->foo_8:
| brk #8
|->foo_9:
| brk #9
|->foo_10:
| brk #10
|->foo_11:
| brk #11
|->foo_12:
| brk #12
|->foo_13:
| brk #13
return 1;
}
void foo_stub()
{
dasm_State* d;
void* labels[lbl__MAX];
unsigned npc = 8;
unsigned nextpc = 0;
dasm_init(&d, DASM_MAXSECTION);
dasm_setupglobal(&d, labels, lbl__MAX);
dasm_setup(&d, bf_actions);
dasm_growpc(&d, npc);
foo(&d);
printf("function foo:\n");
link_and_encode(&d);
printf("\n\n");
dasm_free(&d);
}
static int bar(dasm_State **Dst)
{
|.code
| b ->foo_0
| b ->foo_9
| b ->foo_10
| b ->foo_13
return 1;
}
void bar_stub()
{
dasm_State* d;
void* labels[lbl__MAX];
unsigned npc = 8;
unsigned nextpc = 0;
dasm_init(&d, DASM_MAXSECTION);
dasm_setupglobal(&d, labels, lbl__MAX);
dasm_setup(&d, bf_actions);
dasm_growpc(&d, npc);
bar(&d);
printf("function bar:\n");
link_and_encode(&d);
printf("\n\n");
dasm_free(&d);
}
int main(int argc, char** argv)
{
printf("strat===\n\n");
foo_stub();
bar_stub();
printf("end===\n");
return 0;
}