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