-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-thunks.py
executable file
·146 lines (126 loc) · 3.68 KB
/
make-thunks.py
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
#!/usr/bin/env python3
# CSV format: name, address (ignored), type of thunk, where defined originally
import csv
from dataclasses import dataclass
from enum import auto, Enum
import sys
class Type(Enum):
ABORT = auto()
EMU = auto()
NOOP = auto()
MINISYS = auto()
STUB = auto()
STUB_DUMMY_ALLOC = auto()
@dataclass
class Entry:
index: int
name: str
c_name: str
type: Type
IN, MINISYS_C_OUT, MINISYS_H_OUT, NATIVE_OUT = sys.argv[1:]
with open(IN, "rt") as f, open(MINISYS_C_OUT, "wt") as mcf, open(MINISYS_H_OUT, "wt") as mhf, open(NATIVE_OUT, "wt") as nf:
reader = csv.reader(f)
next(reader)
name_map = {}
nf.write("""/* GENERATED FILE, DO NOT EDIT */
#include "svc_handlers.hpp"
""")
mcf.write("""/* GENERATED FILE, DO NOT EDIT */
#include "thunks.h"
""")
mhf.write("""/* GENERATED FILE, DO NOT EDIT */
#include "thunksdef.h"
""")
next_svc = 0
all_ = []
for name, type, origin, prototype in reader:
type = Type[type]
decl_in_minisys = type not in {Type.EMU, Type.MINISYS} # EMU and MINISYS provide concrete prototypes; otherwise use default generic
stub_in_minisys = type in {Type.ABORT, Type.NOOP, Type.STUB, Type.STUB_DUMMY_ALLOC}
if stub_in_minisys:
c_name = "stub_" + name
else:
c_name = name
all_.append(Entry(next_svc, name, c_name, type))
# miniSYS thunk proto
# if type in {Type.NOOP}:
# proto = f"void {c_name}(void);"
# mhf.write(proto + "\n")
# elif type in {Type.stub}:
if decl_in_minisys:
minisys_proto = f"int {c_name}(int arg1, int arg2, int arg3, int arg4)"
mhf.write(minisys_proto + ";\n")
else:
minisys_proto = None
# minisys thunk
if type in {Type.ABORT}:
thunk = f"""
{minisys_proto} {{
ccos_panic("Unimplemented call {name}");
}}
"""
mcf.write(thunk)
if type in {Type.EMU}:
assert prototype
thunk = f"""
__attribute__((naked)) {prototype} {{
__asm(
"svc {next_svc} \\r\\n"
"bx lr \\r\\n"
);
}}
"""
mcf.write(thunk)
elif type in {Type.STUB}:
thunk = f"""
{minisys_proto} {{
ccos_trace_stub({name});
return 0xCCCCCCCC;
}}
"""
mcf.write(thunk)
elif type in {Type.STUB_DUMMY_ALLOC}:
thunk = f"""
{minisys_proto} {{
ccos_trace_stub({name});
return (int) malloc(1);
}}
"""
mcf.write(thunk)
elif type in {Type.NOOP}:
thunk = f"""
{minisys_proto} {{
return 0xCCCCCCCC;
}}
"""
mcf.write(thunk)
if type in {Type.EMU}:
# generate native prototype
proto = f"""
void Svc_{name}(uc_engine* uc);
"""
nf.write(proto)
# elif type in {Type.stub}:
# # generate native handler
# thunk = f"""
# __attribute__((weak)) int Svc_{name}(uc_engine* uc, int arg0, int arg1, int arg2, int arg3) {{
# printf("THUNK {name} (%08X, %08X, %08X, %08X)\\n", arg0, arg1, arg2, arg3);
# abort();
# }}
# """
# nf.write(thunk)
if type == Type.EMU:
next_svc += 1
# generate native handler table
nf.write("SvcHandler svc_handler_table[] = {\n")
for e in all_:
if e.type == Type.EMU:
nf.write(f" &Svc_{e.name},\n")
nf.write("};\n")
mhf.write("\n")
mhf.write("extern OsFunction os_function_table[];\n")
mcf.write("OsFunction os_function_table[] = {\n")
for e in all_:
mcf.write(f""" {{"{e.name}", (int (*)(int, int, int, int)) &{e.c_name}}},\n""")
mcf.write(" {0, 0},\n")
mcf.write("};\n")