01a8f56629d2bef4591496c781c54d7b4c229293
[ia32rtools.git] / tools / mkbridge.c
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
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "my_assert.h"
14 #include "my_str.h"
15
16 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
17 #define IS(w, y) !strcmp(w, y)
18
19 #include "protoparse.h"
20
21 static const char *c_save_regs[] = { "ebx", "esi", "edi", "ebp" };
22
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
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)
62 {
63         int must_save = 0;
64         int sarg_ofs = 1; // stack offset to args, in DWORDs
65         int args_repushed = 0;
66         int argc_repush;
67         const char *name;
68         int i;
69
70         argc_repush = pp->argc;
71         if (pp->is_vararg)
72                 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
73
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);
77         }
78
79         name = pp_to_name(pp);
80         fprintf(f, ".global %s\n", name);
81         fprintf(f, "%s:\n", name);
82
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"));
87                 fprintf(f, "\tjmp %s\n\n", sym_out);
88                 return;
89         }
90
91         if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall
92              && !pp->is_vararg && !pp->has_retreg)
93         {
94                 // load arg regs
95                 for (i = 0; i < pp->argc; i++) {
96                         fprintf(f, "\tmovl %d(%%esp), %%%s\n",
97                                 (i + sarg_ofs) * 4, pp->arg[i].reg);
98                 }
99                 fprintf(f, "\tjmp %s\n\n", sym_out);
100                 return;
101         }
102
103         // asm_stack_args | saved_regs | ra | args_from_c
104
105         // save the regs
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++;
111         }
112
113         // reconstruct arg stack for asm
114         for (i = argc_repush - 1; i >= 0; i--) {
115                 if (pp->arg[i].reg == NULL) {
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         }
123
124         // load arg regs
125         for (i = 0; i < pp->argc; i++) {
126                 if (pp->arg[i].reg != NULL) {
127                         fprintf(f, "\tmovl %d(%%esp), %%%s\n",
128                                 (i + sarg_ofs) * 4, pp->arg[i].reg);
129                         if (pp->arg[i].type.is_retreg)
130                                 fprintf(f, "\tmovl (%%%s), %%%s\n",
131                                         pp->arg[i].reg, pp->arg[i].reg);
132                 }
133         }
134
135         fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
136         fprintf(f, "\tcall %s\n\n", sym_out);
137
138         if (args_repushed && !pp->is_stdcall) {
139                 fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
140                 sarg_ofs -= args_repushed;
141         }
142
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                 }
152         }
153
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
158         fprintf(f, "\tret\n\n");
159 }
160
161 static void out_fromasm_x86(FILE *f, const char *sym,
162         const struct parsed_proto *pp)
163 {
164         int reg_ofs[ARRAY_SIZE(pp->arg)];
165         int sarg_ofs = 1; // stack offset to args, in DWORDs
166         int saved_regs = 0;
167         int ecx_ofs = -1;
168         int edx_ofs = -1;
169         int c_is_stdcall;
170         int argc_repush;
171         int stack_args;
172         int ret64;
173         int i;
174
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
182         ret64 = strstr(pp->ret_type.name, "int64") != NULL;
183
184         fprintf(f, "# %s",
185           pp->is_fastcall ? "__fastcall" :
186           (pp->is_stdcall ? "__stdcall" : "__cdecl"));
187         if (ret64)
188                  fprintf(f, " ret64");
189         fprintf(f, "\n.global %s\n", sym);
190         fprintf(f, "%s:\n", sym);
191
192         if ((pp->argc_reg == 0 || pp->is_fastcall)
193             && !IS(pp->name, "storm_491")) // wants edx save :(
194         {
195                 fprintf(f, "\tjmp %s\n\n", pp_to_name(pp));
196                 return;
197         }
198
199         c_is_stdcall = (pp->argc_reg == 0 && pp->is_stdcall);
200
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++;
206         sarg_ofs++;
207         ecx_ofs = sarg_ofs;
208         if (!ret64) {
209                 fprintf(f, "\tpushl %%edx\n");
210                 saved_regs++;
211                 sarg_ofs++;
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                 }
233         }
234
235         // construct arg stack
236         for (i = argc_repush - 1; i >= 0; i--) {
237                 if (pp->arg[i].reg == NULL) {
238                         fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
239                                 (sarg_ofs + stack_args - 1) * 4);
240                         fprintf(f, "\tpushl %%ecx\n");
241                         stack_args--;
242                 }
243                 else {
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"))
251                                 // must reload original ecx
252                                 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
253                                         (sarg_ofs - 2) * 4);
254
255                         fprintf(f, "\tpushl %%%s\n", reg);
256                 }
257                 sarg_ofs++;
258         }
259
260         fprintf(f, "\n\tcall %s\n\n", pp_to_name(pp));
261
262         if (!c_is_stdcall && sarg_ofs > saved_regs + 1)
263                 fprintf(f, "\tadd $%d,%%esp\n",
264                         (sarg_ofs - (saved_regs + 1)) * 4);
265
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
281         if (!ret64)
282                 fprintf(f, "\tpopl %%edx\n");
283         fprintf(f, "\tpopl %%ecx\n");
284
285         if (pp->is_stdcall && pp->argc_stack)
286                 fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
287         else
288                 fprintf(f, "\tret\n\n");
289 }
290
291 int main(int argc, char *argv[])
292 {
293         FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
294         const struct parsed_proto *pp;
295         char line[256];
296         char sym_noat[256];
297         char sym[256];
298         char *p;
299         int ret = 1;
300
301         if (argc != 5) {
302                 printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
303                         argv[0]);
304                 return 1;
305         }
306
307         hdrfn = argv[4];
308         fhdr = fopen(hdrfn, "r");
309         my_assert_not(fhdr, NULL);
310
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);
316
317         fout = fopen(argv[1], "w");
318         my_assert_not(fout, NULL);
319
320         fprintf(fout, ".text\n\n");
321         fprintf(fout, "# to asm\n\n");
322
323         while (fgets(line, sizeof(line), fsyms_to))
324         {
325                 next_word(sym, sizeof(sym), line);
326                 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
327                         continue;
328
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
335                 pp = proto_parse(fhdr, sym_noat, 0);
336                 if (pp == NULL)
337                         goto out;
338
339                 out_toasm_x86(fout, sym_noat, pp);
340         }
341
342         fprintf(fout, "# from asm\n\n");
343
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;
349
350                 pp = proto_parse(fhdr, sym, 0);
351                 if (pp == NULL)
352                         goto out;
353
354                 out_fromasm_x86(fout, sym, pp);
355         }
356
357         ret = 0;
358 out:
359         fclose(fout);
360         fclose(fsyms_to);
361         fclose(fsyms_from);
362         fclose(fhdr);
363         if (ret)
364                 remove(argv[1]);
365
366         return ret;
367 }