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