7637b6cc |
1 | /* |
2 | * ia32rtools |
3 | * (C) notaz, 2013,2014 |
4 | * |
5 | * This work is licensed under the terms of 3-clause BSD license. |
6 | * See COPYING file in the top-level directory. |
7 | */ |
8 | |
57e4efe9 |
9 | #include <stdio.h> |
10 | #include <stdlib.h> |
11 | #include <string.h> |
12 | |
13 | #include "my_assert.h" |
14 | #include "my_str.h" |
15 | |
232aca37 |
16 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) |
3e52f54c |
17 | #define IS(w, y) !strcmp(w, y) |
232aca37 |
18 | |
c36e914d |
19 | #include "protoparse.h" |
232aca37 |
20 | |
1f84f6b3 |
21 | static const char *c_save_regs[] = { "ebx", "esi", "edi", "ebp" }; |
22 | |
232aca37 |
23 | static int is_x86_reg_saved(const char *reg) |
24 | { |
25 | static const char *nosave_regs[] = { "eax", "edx", "ecx" }; |
26 | int nosave = 0; |
27 | int r; |
28 | |
29 | for (r = 0; r < ARRAY_SIZE(nosave_regs); r++) |
30 | if (strcmp(reg, nosave_regs[r]) == 0) |
31 | nosave = 1; |
32 | |
33 | return !nosave; |
34 | } |
35 | |
bd2eb956 |
36 | // output decorated name |
37 | static const char *pp_to_name(const struct parsed_proto *pp) |
38 | { |
39 | static char buf[256]; |
40 | char atval[16]; |
41 | |
42 | if (!pp->is_fastcall && pp->argc_reg != 0) { |
43 | // can only be handled by __cdecl C func |
44 | snprintf(buf, sizeof(buf), "_%s", pp->name); |
45 | return buf; |
46 | } |
47 | |
48 | atval[0] = 0; |
49 | if (pp->is_stdcall) { |
50 | snprintf(atval, sizeof(atval), "@%d", |
51 | pp->argc * 4); |
52 | } |
53 | snprintf(buf, sizeof(buf), "%s%s%s", |
54 | pp->is_fastcall ? "@" : "_", |
55 | pp->name, atval); |
56 | |
57 | return buf; |
58 | } |
59 | |
60 | static void out_toasm_x86(FILE *f, const char *sym_out, |
61 | const struct parsed_proto *pp) |
232aca37 |
62 | { |
232aca37 |
63 | int must_save = 0; |
64 | int sarg_ofs = 1; // stack offset to args, in DWORDs |
65 | int args_repushed = 0; |
4f12f671 |
66 | int argc_repush; |
bd2eb956 |
67 | const char *name; |
232aca37 |
68 | int i; |
69 | |
4f12f671 |
70 | argc_repush = pp->argc; |
71 | if (pp->is_vararg) |
72 | argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough? |
73 | |
c36e914d |
74 | for (i = 0; i < pp->argc; i++) { |
75 | if (pp->arg[i].reg != NULL) |
76 | must_save |= is_x86_reg_saved(pp->arg[i].reg); |
232aca37 |
77 | } |
78 | |
bd2eb956 |
79 | name = pp_to_name(pp); |
80 | fprintf(f, ".global %s\n", name); |
81 | fprintf(f, "%s:\n", name); |
232aca37 |
82 | |
c0050df6 |
83 | if (pp->argc_reg == 0 || pp->is_fastcall) { |
84 | fprintf(f, "\t# %s\n", |
85 | pp->is_fastcall ? "__fastcall" : |
86 | (pp->is_stdcall ? "__stdcall" : "__cdecl")); |
54e763a1 |
87 | fprintf(f, "\tjmp %s\n\n", sym_out); |
232aca37 |
88 | return; |
89 | } |
90 | |
4f12f671 |
91 | if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall |
1f84f6b3 |
92 | && !pp->is_vararg && !pp->has_retreg) |
4f12f671 |
93 | { |
232aca37 |
94 | // load arg regs |
c36e914d |
95 | for (i = 0; i < pp->argc; i++) { |
232aca37 |
96 | fprintf(f, "\tmovl %d(%%esp), %%%s\n", |
c36e914d |
97 | (i + sarg_ofs) * 4, pp->arg[i].reg); |
232aca37 |
98 | } |
54e763a1 |
99 | fprintf(f, "\tjmp %s\n\n", sym_out); |
232aca37 |
100 | return; |
101 | } |
102 | |
1f84f6b3 |
103 | // asm_stack_args | saved_regs | ra | args_from_c |
104 | |
232aca37 |
105 | // save the regs |
1f84f6b3 |
106 | // because we don't always know what we are calling, |
107 | // be safe and save everything that has to be saved in __cdecl |
108 | for (i = 0; i < ARRAY_SIZE(c_save_regs); i++) { |
109 | fprintf(f, "\tpushl %%%s\n", c_save_regs[i]); |
110 | sarg_ofs++; |
232aca37 |
111 | } |
112 | |
1f84f6b3 |
113 | // reconstruct arg stack for asm |
4f12f671 |
114 | for (i = argc_repush - 1; i >= 0; i--) { |
c36e914d |
115 | if (pp->arg[i].reg == NULL) { |
232aca37 |
116 | fprintf(f, "\tmovl %d(%%esp), %%eax\n", |
117 | (i + sarg_ofs) * 4); |
118 | fprintf(f, "\tpushl %%eax\n"); |
119 | sarg_ofs++; |
120 | args_repushed++; |
121 | } |
122 | } |
232aca37 |
123 | |
124 | // load arg regs |
c36e914d |
125 | for (i = 0; i < pp->argc; i++) { |
126 | if (pp->arg[i].reg != NULL) { |
232aca37 |
127 | fprintf(f, "\tmovl %d(%%esp), %%%s\n", |
c36e914d |
128 | (i + sarg_ofs) * 4, pp->arg[i].reg); |
1f84f6b3 |
129 | if (pp->arg[i].type.is_retreg) |
130 | fprintf(f, "\tmovl (%%%s), %%%s\n", |
131 | pp->arg[i].reg, pp->arg[i].reg); |
232aca37 |
132 | } |
133 | } |
134 | |
c36e914d |
135 | fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl"); |
54e763a1 |
136 | fprintf(f, "\tcall %s\n\n", sym_out); |
232aca37 |
137 | |
1f84f6b3 |
138 | if (args_repushed && !pp->is_stdcall) { |
a51421fa |
139 | fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4); |
1f84f6b3 |
140 | sarg_ofs -= args_repushed; |
141 | } |
232aca37 |
142 | |
1f84f6b3 |
143 | // update the retreg regs |
144 | if (pp->has_retreg) { |
145 | for (i = 0; i < pp->argc; i++) { |
146 | if (pp->arg[i].type.is_retreg) { |
147 | fprintf(f, "\tmovl %d(%%esp), %%ecx\n" |
148 | "\tmovl %%%s, (%%ecx)\n", |
149 | (i + sarg_ofs) * 4, pp->arg[i].reg); |
150 | } |
151 | } |
232aca37 |
152 | } |
153 | |
1f84f6b3 |
154 | // restore regs |
155 | for (i = ARRAY_SIZE(c_save_regs) - 1; i >= 0; i--) |
156 | fprintf(f, "\tpopl %%%s\n", c_save_regs[i]); |
157 | |
232aca37 |
158 | fprintf(f, "\tret\n\n"); |
159 | } |
160 | |
54e763a1 |
161 | static void out_fromasm_x86(FILE *f, const char *sym, |
162 | const struct parsed_proto *pp) |
232aca37 |
163 | { |
1f84f6b3 |
164 | int reg_ofs[ARRAY_SIZE(pp->arg)]; |
232aca37 |
165 | int sarg_ofs = 1; // stack offset to args, in DWORDs |
7ae48d73 |
166 | int saved_regs = 0; |
1f84f6b3 |
167 | int ecx_ofs = -1; |
168 | int edx_ofs = -1; |
f2de0a97 |
169 | int c_is_stdcall; |
4f12f671 |
170 | int argc_repush; |
232aca37 |
171 | int stack_args; |
7ae48d73 |
172 | int ret64; |
232aca37 |
173 | int i; |
174 | |
4f12f671 |
175 | argc_repush = pp->argc; |
176 | stack_args = pp->argc_stack; |
177 | if (pp->is_vararg) { |
178 | argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough? |
179 | stack_args = argc_repush - pp->argc_reg; |
180 | } |
181 | |
7ae48d73 |
182 | ret64 = strstr(pp->ret_type.name, "int64") != NULL; |
183 | |
c0050df6 |
184 | fprintf(f, "# %s", |
185 | pp->is_fastcall ? "__fastcall" : |
186 | (pp->is_stdcall ? "__stdcall" : "__cdecl")); |
7ae48d73 |
187 | if (ret64) |
188 | fprintf(f, " ret64"); |
189 | fprintf(f, "\n.global %s\n", sym); |
232aca37 |
190 | fprintf(f, "%s:\n", sym); |
191 | |
f2de0a97 |
192 | if ((pp->argc_reg == 0 || pp->is_fastcall) |
193 | && !IS(pp->name, "storm_491")) // wants edx save :( |
194 | { |
bd2eb956 |
195 | fprintf(f, "\tjmp %s\n\n", pp_to_name(pp)); |
232aca37 |
196 | return; |
197 | } |
198 | |
f2de0a97 |
199 | c_is_stdcall = (pp->argc_reg == 0 && pp->is_stdcall); |
200 | |
7ae48d73 |
201 | // at least sc sub_47B150 needs edx to be preserved |
202 | // int64 returns use edx:eax - no edx save |
203 | // we use ecx also as scratch |
204 | fprintf(f, "\tpushl %%ecx\n"); |
205 | saved_regs++; |
232aca37 |
206 | sarg_ofs++; |
1f84f6b3 |
207 | ecx_ofs = sarg_ofs; |
7ae48d73 |
208 | if (!ret64) { |
209 | fprintf(f, "\tpushl %%edx\n"); |
210 | saved_regs++; |
211 | sarg_ofs++; |
1f84f6b3 |
212 | edx_ofs = sarg_ofs; |
213 | } |
214 | |
215 | // need space for retreg args |
216 | if (pp->has_retreg) { |
217 | for (i = 0; i < pp->argc; i++) { |
218 | if (!pp->arg[i].type.is_retreg) |
219 | continue; |
220 | if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) { |
221 | reg_ofs[i] = ecx_ofs; |
222 | continue; |
223 | } |
224 | if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) { |
225 | reg_ofs[i] = edx_ofs; |
226 | continue; |
227 | } |
228 | fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg); |
229 | saved_regs++; |
230 | sarg_ofs++; |
231 | reg_ofs[i] = sarg_ofs; |
232 | } |
7ae48d73 |
233 | } |
232aca37 |
234 | |
235 | // construct arg stack |
4f12f671 |
236 | for (i = argc_repush - 1; i >= 0; i--) { |
c36e914d |
237 | if (pp->arg[i].reg == NULL) { |
7ae48d73 |
238 | fprintf(f, "\tmovl %d(%%esp), %%ecx\n", |
232aca37 |
239 | (sarg_ofs + stack_args - 1) * 4); |
7ae48d73 |
240 | fprintf(f, "\tpushl %%ecx\n"); |
232aca37 |
241 | stack_args--; |
242 | } |
243 | else { |
1f84f6b3 |
244 | const char *reg = pp->arg[i].reg; |
245 | if (pp->arg[i].type.is_retreg) { |
246 | reg = "ecx"; |
247 | fprintf(f, "\tlea %d(%%esp), %%ecx\n", |
248 | (sarg_ofs - reg_ofs[i]) * 4); |
249 | } |
250 | else if (IS(reg, "ecx")) |
7ae48d73 |
251 | // must reload original ecx |
252 | fprintf(f, "\tmovl %d(%%esp), %%ecx\n", |
f3d05b09 |
253 | (sarg_ofs - 2) * 4); |
254 | |
1f84f6b3 |
255 | fprintf(f, "\tpushl %%%s\n", reg); |
232aca37 |
256 | } |
257 | sarg_ofs++; |
258 | } |
259 | |
bd2eb956 |
260 | fprintf(f, "\n\tcall %s\n\n", pp_to_name(pp)); |
232aca37 |
261 | |
f2de0a97 |
262 | if (!c_is_stdcall && sarg_ofs > saved_regs + 1) |
7ae48d73 |
263 | fprintf(f, "\tadd $%d,%%esp\n", |
264 | (sarg_ofs - (saved_regs + 1)) * 4); |
232aca37 |
265 | |
1f84f6b3 |
266 | // pop retregs |
267 | if (pp->has_retreg) { |
268 | for (i = pp->argc - 1; i >= 0; i--) { |
269 | if (!pp->arg[i].type.is_retreg) |
270 | continue; |
271 | if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) { |
272 | continue; |
273 | } |
274 | if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) { |
275 | continue; |
276 | } |
277 | fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg); |
278 | } |
279 | } |
280 | |
7ae48d73 |
281 | if (!ret64) |
282 | fprintf(f, "\tpopl %%edx\n"); |
283 | fprintf(f, "\tpopl %%ecx\n"); |
232aca37 |
284 | |
c36e914d |
285 | if (pp->is_stdcall && pp->argc_stack) |
286 | fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4); |
232aca37 |
287 | else |
288 | fprintf(f, "\tret\n\n"); |
289 | } |
290 | |
57e4efe9 |
291 | int main(int argc, char *argv[]) |
292 | { |
232aca37 |
293 | FILE *fout, *fsyms_to, *fsyms_from, *fhdr; |
bd96f656 |
294 | const struct parsed_proto *pp; |
57e4efe9 |
295 | char line[256]; |
54e763a1 |
296 | char sym_noat[256]; |
57e4efe9 |
297 | char sym[256]; |
54e763a1 |
298 | char *p; |
299 | int ret = 1; |
57e4efe9 |
300 | |
232aca37 |
301 | if (argc != 5) { |
302 | printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n", |
57e4efe9 |
303 | argv[0]); |
304 | return 1; |
305 | } |
306 | |
232aca37 |
307 | hdrfn = argv[4]; |
57e4efe9 |
308 | fhdr = fopen(hdrfn, "r"); |
309 | my_assert_not(fhdr, NULL); |
310 | |
232aca37 |
311 | fsyms_from = fopen(argv[3], "r"); |
312 | my_assert_not(fsyms_from, NULL); |
313 | |
314 | fsyms_to = fopen(argv[2], "r"); |
315 | my_assert_not(fsyms_to, NULL); |
57e4efe9 |
316 | |
317 | fout = fopen(argv[1], "w"); |
318 | my_assert_not(fout, NULL); |
319 | |
320 | fprintf(fout, ".text\n\n"); |
232aca37 |
321 | fprintf(fout, "# to asm\n\n"); |
57e4efe9 |
322 | |
232aca37 |
323 | while (fgets(line, sizeof(line), fsyms_to)) |
57e4efe9 |
324 | { |
325 | next_word(sym, sizeof(sym), line); |
326 | if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#') |
327 | continue; |
328 | |
54e763a1 |
329 | // IDA asm doesn't do '@' notation.. |
330 | strcpy(sym_noat, sym); |
331 | p = strchr(sym_noat, '@'); |
332 | if (p != NULL) |
333 | *p = 0; |
334 | |
36595fd2 |
335 | pp = proto_parse(fhdr, sym_noat, 0); |
bd96f656 |
336 | if (pp == NULL) |
232aca37 |
337 | goto out; |
57e4efe9 |
338 | |
bd2eb956 |
339 | out_toasm_x86(fout, sym_noat, pp); |
232aca37 |
340 | } |
57e4efe9 |
341 | |
232aca37 |
342 | fprintf(fout, "# from asm\n\n"); |
57e4efe9 |
343 | |
232aca37 |
344 | while (fgets(line, sizeof(line), fsyms_from)) |
345 | { |
346 | next_word(sym, sizeof(sym), line); |
347 | if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#') |
348 | continue; |
57e4efe9 |
349 | |
36595fd2 |
350 | pp = proto_parse(fhdr, sym, 0); |
bd96f656 |
351 | if (pp == NULL) |
232aca37 |
352 | goto out; |
57e4efe9 |
353 | |
bd96f656 |
354 | out_fromasm_x86(fout, sym, pp); |
57e4efe9 |
355 | } |
356 | |
232aca37 |
357 | ret = 0; |
358 | out: |
57e4efe9 |
359 | fclose(fout); |
232aca37 |
360 | fclose(fsyms_to); |
361 | fclose(fsyms_from); |
362 | fclose(fhdr); |
363 | if (ret) |
364 | remove(argv[1]); |
365 | |
366 | return ret; |
57e4efe9 |
367 | } |