minor fixes
[ia32rtools.git] / tools / asmproc.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 #include "common.h"
16
17 struct sl_item {
18         char *name;
19         unsigned int callsites:1;
20         unsigned int found:1;
21         unsigned int ignore_missing:1;
22 };
23
24 static int cmp_sym(const void *p1_, const void *p2_)
25 {
26         const struct sl_item *p1 = p1_, *p2 = p2_;
27         const char *s1 = p1->name, *s2 = p2->name;
28         int i;
29
30         if (*s1 == '_')
31                 s1++;
32         if (*s2 == '_')
33                 s2++;
34
35         for (i = 0; ; i++) {
36                 if ((s1[i] | s2[i]) == 0)
37                         break;
38
39                 if (s1[i] == s2[i])
40                         continue;
41
42                 if (s1[i] ==  0  && s2[i] == '@')
43                         break;
44                 if (s1[i] == '@' && s2[i] ==  0)
45                         break;
46
47                 return s1[i] - s2[i];
48         }
49
50         return 0;
51 }
52
53 static int cmp_sym_sort(const void *p1_, const void *p2_)
54 {
55         const struct sl_item *p1 = p1_, *p2 = p2_;
56         const char *s1 = p1->name, *s2 = p2->name;
57         int ret;
58         
59         ret = cmp_sym(p1_, p2_);
60         if (ret == 0) {
61                 printf("%s: dupe sym: '%s' '%s'\n", __func__, s1, s2);
62                 exit(1);
63         }
64         return ret;
65 }
66
67 void read_list(struct sl_item **sl_in, int *cnt, int *alloc,
68         FILE *f, int callsites, int ignore_missing)
69 {
70         struct sl_item *sl = *sl_in;
71         int c = *cnt;
72         char line[256];
73         char word[256];
74
75         while (fgets(line, sizeof(line), f) != NULL) {
76                 next_word(word, sizeof(word), line);
77                 if (word[0] == 0 || word[0] == ';' || word[0] == '#')
78                         continue;
79
80                 sl[c].name = strdup(word);
81                 sl[c].callsites = callsites;
82                 sl[c].ignore_missing = ignore_missing;
83                 sl[c].found = 0;
84                 c++;
85
86                 if (c >= *alloc) {
87                         *alloc *= 2;
88                         sl = realloc(sl, *alloc * sizeof(sl[0]));
89                         my_assert_not(sl, NULL);
90                         memset(sl + c, 0, (*alloc - c) * sizeof(sl[0]));
91                 }
92         }
93
94         *sl_in = sl;
95         *cnt = c;
96 }
97
98 const char *sym_use(const struct sl_item *sym, int is_rm)
99 {
100         static char buf[256+3];
101         int ret;
102
103         ret = snprintf(buf, sizeof(buf), "%s%s",
104           is_rm ? "rm_" : "", sym->name);
105         if (ret >= sizeof(buf)) {
106                 printf("truncation detected: '%s'\n", buf);
107                 exit(1);
108         }
109
110         return buf;
111 }
112
113 #define IS_OR2(w, x, y) (IS(w, x) || IS(w, y))
114 #define IS_OR3(w, x, y, z) (IS(w, x) || IS(w, y) || IS(w, z))
115
116 int main(int argc, char *argv[])
117 {
118         struct sl_item *symlist, *sym, ssym = { NULL, };
119         int patch_callsites = 0;
120         int ignore_missing = 0;
121         FILE *fout, *fin, *f;
122         int symlist_alloc;
123         int symlist_cnt;
124         char line[256];
125         char word[256];
126         char word2[256];
127         char word3[256];
128         char word4[256];
129         char word5[256];
130         char word6[256];
131         char func[256];
132         char *p, *p2;
133         int i;
134
135         if (argc < 4) {
136                 printf("usage:\n%s <asmf_out> <asmf_in> [[-c][-i] <listf>]*>\n",
137                         argv[0]);
138                 printf("  -c - patch callsites\n"
139                                          "  -i - ignore missing syms\n");
140                 return 1;
141         }
142
143         symlist_alloc = 16;
144         symlist_cnt = 0;
145         symlist = calloc(symlist_alloc, sizeof(symlist[0]));
146         my_assert_not(symlist, NULL);
147
148         for (i = 3; i < argc; i++) {
149                 if (strcmp(argv[i], "-c") == 0) {
150                         patch_callsites = 1;
151                         continue;
152                 }
153                 if (strcmp(argv[i], "-i") == 0) {
154                         ignore_missing = 1;
155                         continue;
156                 }
157
158                 f = fopen(argv[i], "r");
159                 my_assert_not(f, NULL);
160                 read_list(&symlist, &symlist_cnt, &symlist_alloc,
161                         f, patch_callsites, ignore_missing);
162                 fclose(f);
163
164                 patch_callsites = 0;
165                 ignore_missing = 0;
166         }
167
168         qsort(symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym_sort);
169
170 #if 0
171         printf("symlist:\n");
172         for (i = 0; i < symlist_cnt; i++)
173                 printf("%d '%s'\n", symlist[i].callsites, symlist[i].name);
174 #endif
175
176         fin = fopen(argv[2], "r");
177         my_assert_not(fin, NULL);
178
179         fout = fopen(argv[1], "w");
180         my_assert_not(fout, NULL);
181
182         while (fgets(line, sizeof(line), fin))
183         {
184                 p = sskip(line);
185                 if (*p == 0 || *p == ';')
186                         goto pass;
187
188                 p = sskip(next_word(word, sizeof(word), p));
189                 if (*p == 0 || *p == ';')
190                         goto pass; // need at least 2 words
191
192                 p = next_word(word2, sizeof(word2), p);
193
194                 if (IS_OR2(word2, "proc", "endp")) {
195                         if (IS(word2, "proc"))
196                                 strcpy(func, word);
197                         else
198                                 func[0] = 0;
199
200                         ssym.name = word;
201                         sym = bsearch(&ssym, symlist, symlist_cnt,
202                                 sizeof(symlist[0]), cmp_sym);
203                         if (sym != NULL) {
204                                 sym->found = 1;
205                                 fprintf(fout, "%s\t%s%s", sym_use(sym, 1), word2, p);
206                                 continue;
207                         }
208                 }
209
210                 if (IS_OR2(word, "call", "jmp")) {
211                         ssym.name = word2;
212                         sym = bsearch(&ssym, symlist, symlist_cnt,
213                                 sizeof(symlist[0]), cmp_sym);
214                         if (sym != NULL) {
215                                 fprintf(fout, "\t\t%s\t%s%s", word,
216                                         sym_use(sym, sym->callsites || IS(word2, func)), p);
217                                 continue;
218                         }
219                 }
220
221                 if (IS(word, "public")) {
222                         ssym.name = word2;
223                         sym = bsearch(&ssym, symlist, symlist_cnt,
224                                 sizeof(symlist[0]), cmp_sym);
225                         if (sym != NULL) {
226                                 fprintf(fout, "\t\tpublic %s%s", sym_use(sym, 1), p);
227                                 continue;
228                         }
229                 }
230
231                 p = sskip(p);
232                 if (*p == 0 || *p == ';')
233                         goto pass; // need at least 3 words
234
235                 p = next_word(word3, sizeof(word3), p);
236
237                 // push offset <sym>
238                 if (IS(word, "push") && IS(word2, "offset")) {
239                         ssym.name = word3;
240                         sym = bsearch(&ssym, symlist, symlist_cnt,
241                                 sizeof(symlist[0]), cmp_sym);
242                         if (sym != NULL) {
243                                 fprintf(fout, "\t\t%s %s %s%s", word, word2,
244                                   sym_use(sym, sym->callsites || IS(word3, func)), p);
245                                 continue;
246                         }
247                 }
248
249                 // jcc short <sym>
250                 if (word[0] == 'j' && IS(word2, "short") && !IS(word3, "exit")) {
251                         ssym.name = word3;
252                         sym = bsearch(&ssym, symlist, symlist_cnt,
253                                 sizeof(symlist[0]), cmp_sym);
254                         if (sym != NULL) {
255                                 fprintf(fout, "\t\t%s ", word);
256                                 // for conditional "call", don't print 'short'
257                                 if (IS(word3, func))
258                                         fprintf(fout, "short ");
259                                 fprintf(fout, "%s%s",
260                                   sym_use(sym, sym->callsites || IS(word3, func)), p);
261                                 continue;
262                         }
263                 }
264
265                 // dd offset <sym>
266                 if (IS(word, "dd")
267                         && (IS(word2, "offset") || strstr(p, "offset")))
268                 {
269                         fprintf(fout, "\t\tdd");
270                         p = next_word(word, sizeof(word), line);
271                         goto offset_loop;
272                 }
273
274                 p = sskip(p);
275                 if (*p == 0 || *p == ';')
276                         goto pass; // need at least 4 words
277
278                 p = next_word(word4, sizeof(word4), p);
279
280                 // <name> dd offset <sym>
281                 if (IS(word2, "dd")
282                         && (IS(word3, "offset") || strstr(p, "offset")))
283                 {
284                         fprintf(fout, "%s\tdd", word);
285                         p = next_word(word, sizeof(word), line);
286                         p = next_word(word, sizeof(word), p);
287                         goto offset_loop;
288                 }
289
290                 // mov <something>, offset <sym>
291                 // jcc <some> ptr <sym>
292                 if ( (IS(word, "mov") && IS(word3, "offset"))
293                   || (word[0] == 'j' && IS(word3, "ptr")) ) {
294                         ssym.name = word4;
295                         sym = bsearch(&ssym, symlist, symlist_cnt,
296                                 sizeof(symlist[0]), cmp_sym);
297                         if (sym != NULL) {
298                                 fprintf(fout, "\t\t%s\t%s %s %s%s",
299                                         word, word2, word3,
300                                         sym_use(sym, sym->callsites), p);
301                                 continue;
302                         }
303                 }
304
305                 p = sskip(p);
306                 if (*p == 0 || *p == ';')
307                         goto pass; // need at least 5 words
308
309                 p = next_word(word5, sizeof(word5), p);
310
311                 p = sskip(p);
312                 if (*p == 0 || *p == ';')
313                         goto pass; // need at least 6 words
314
315                 p = next_word(word6, sizeof(word6), p);
316
317                 // <op> dword ptr <something>, offset <sym>
318                 if ( IS(word2, "dword") && IS(word3, "ptr")
319                   && IS(word5, "offset") ) {
320                         ssym.name = word6;
321                         sym = bsearch(&ssym, symlist, symlist_cnt,
322                                 sizeof(symlist[0]), cmp_sym);
323                         if (sym != NULL) {
324                                 fprintf(fout, "\t\t%s\tdword ptr %s offset %s%s",
325                                         word, word4, sym_use(sym, sym->callsites), p);
326                                 continue;
327                         }
328                 }
329
330 pass:
331                 fwrite(line, 1, strlen(line), fout);
332                 continue;
333
334 offset_loop:
335                 while (1) {
336                         p2 = next_word(word, sizeof(word), p);
337                         if (word[0] == 0 || word[0] == ';') {
338                                 break;
339                         }
340                         if (!IS(word, "offset")) {
341                                 // pass through
342                                 p2 = strstr(p, "offset");
343                                 if (p2 == NULL)
344                                         break;
345                                 fwrite(p, 1, p2 - p, fout);
346                                 p2 = next_word(word, sizeof(word), p2);
347                         }
348                         p = next_word(word, sizeof(word), p2);
349                         p2 = strchr(word, ',');
350                         if (p2)
351                                 *p2 = 0;
352
353                         ssym.name = word;
354                         sym = bsearch(&ssym, symlist, symlist_cnt,
355                                 sizeof(symlist[0]), cmp_sym);
356                         fprintf(fout, " offset %s%s",
357                                 (sym != NULL) ? sym_use(sym, sym->callsites) : word,
358                                 p2 ? "," : "");
359                 }
360                 fprintf(fout, "%s", p);
361                 continue;
362         }
363
364         for (i = 0; i < symlist_cnt; i++) {
365                 if (!symlist[i].found && !symlist[i].ignore_missing)
366                         printf("warning: sym '%s' not found\n", symlist[i].name);
367         }
368
369         fclose(fin);
370         fclose(fout);
371
372         return 0;
373 }
374
375 // vim:ts=2:shiftwidth=2