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 | |
ede51255 |
9 | #include <stdio.h> |
10 | #include <stdlib.h> |
11 | #include <string.h> |
12 | |
13 | #include "my_assert.h" |
57e4efe9 |
14 | #include "my_str.h" |
ede51255 |
15 | |
16 | struct sl_item { |
17 | char *name; |
368deb23 |
18 | unsigned int callsites:1; |
8022df01 |
19 | unsigned int found:1; |
87ccd363 |
20 | unsigned int ignore_missing:1; |
ede51255 |
21 | }; |
22 | |
23 | static int cmp_sym(const void *p1_, const void *p2_) |
24 | { |
25 | const struct sl_item *p1 = p1_, *p2 = p2_; |
26 | const char *s1 = p1->name, *s2 = p2->name; |
27 | int i; |
28 | |
7aca4698 |
29 | if (*s1 == '_') |
30 | s1++; |
31 | if (*s2 == '_') |
32 | s2++; |
33 | |
ede51255 |
34 | for (i = 0; ; i++) { |
0c5547d1 |
35 | if ((s1[i] | s2[i]) == 0) |
36 | break; |
37 | |
ede51255 |
38 | if (s1[i] == s2[i]) |
39 | continue; |
40 | |
0c5547d1 |
41 | if (s1[i] == 0 && s2[i] == '@') |
42 | break; |
43 | if (s1[i] == '@' && s2[i] == 0) |
ede51255 |
44 | break; |
45 | |
46 | return s1[i] - s2[i]; |
47 | } |
48 | |
49 | return 0; |
50 | } |
51 | |
52 | static int cmp_sym_sort(const void *p1_, const void *p2_) |
53 | { |
54 | const struct sl_item *p1 = p1_, *p2 = p2_; |
55 | const char *s1 = p1->name, *s2 = p2->name; |
56 | int ret; |
57 | |
58 | ret = cmp_sym(p1_, p2_); |
59 | if (ret == 0) { |
60 | printf("%s: dupe sym: '%s' '%s'\n", __func__, s1, s2); |
61 | exit(1); |
62 | } |
63 | return ret; |
64 | } |
65 | |
368deb23 |
66 | void read_list(struct sl_item **sl_in, int *cnt, int *alloc, |
87ccd363 |
67 | FILE *f, int callsites, int ignore_missing) |
ede51255 |
68 | { |
69 | struct sl_item *sl = *sl_in; |
70 | int c = *cnt; |
71 | char line[256]; |
72 | char word[256]; |
ede51255 |
73 | |
74 | while (fgets(line, sizeof(line), f) != NULL) { |
0c5547d1 |
75 | next_word(word, sizeof(word), line); |
76 | if (word[0] == 0 || word[0] == ';' || word[0] == '#') |
ede51255 |
77 | continue; |
78 | |
79 | sl[c].name = strdup(word); |
368deb23 |
80 | sl[c].callsites = callsites; |
87ccd363 |
81 | sl[c].ignore_missing = ignore_missing; |
8022df01 |
82 | sl[c].found = 0; |
ede51255 |
83 | c++; |
84 | |
85 | if (c >= *alloc) { |
86 | *alloc *= 2; |
87 | sl = realloc(sl, *alloc * sizeof(sl[0])); |
88 | my_assert_not(sl, NULL); |
8022df01 |
89 | memset(sl + c, 0, (*alloc - c) * sizeof(sl[0])); |
ede51255 |
90 | } |
91 | } |
92 | |
93 | *sl_in = sl; |
94 | *cnt = c; |
95 | } |
96 | |
7aca4698 |
97 | const char *sym_use(const struct sl_item *sym, int is_rm) |
8022df01 |
98 | { |
99 | static char buf[256+3]; |
100 | int ret; |
101 | |
7aca4698 |
102 | ret = snprintf(buf, sizeof(buf), "%s%s", |
103 | is_rm ? "rm_" : "", sym->name); |
8022df01 |
104 | if (ret >= sizeof(buf)) { |
105 | printf("truncation detected: '%s'\n", buf); |
106 | exit(1); |
107 | } |
108 | |
109 | return buf; |
110 | } |
111 | |
28023f83 |
112 | #define IS(w, y) !strcasecmp(w, y) |
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 | |
ede51255 |
116 | int main(int argc, char *argv[]) |
117 | { |
8022df01 |
118 | struct sl_item *symlist, *sym, ssym = { NULL, }; |
368deb23 |
119 | int patch_callsites = 0; |
87ccd363 |
120 | int ignore_missing = 0; |
ede51255 |
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]; |
28023f83 |
129 | char word5[256]; |
130 | char word6[256]; |
d4e3b5db |
131 | char func[256]; |
de50b98b |
132 | char *p, *p2; |
8022df01 |
133 | int i; |
ede51255 |
134 | |
368deb23 |
135 | if (argc < 4) { |
87ccd363 |
136 | printf("usage:\n%s <asmf_out> <asmf_in> [[-c][-i] <listf>]*>\n", |
ede51255 |
137 | argv[0]); |
87ccd363 |
138 | printf(" -c - patch callsites\n" |
139 | " -i - ignore missing syms\n"); |
ede51255 |
140 | return 1; |
141 | } |
142 | |
143 | symlist_alloc = 16; |
144 | symlist_cnt = 0; |
8022df01 |
145 | symlist = calloc(symlist_alloc, sizeof(symlist[0])); |
ede51255 |
146 | my_assert_not(symlist, NULL); |
147 | |
368deb23 |
148 | for (i = 3; i < argc; i++) { |
0c5547d1 |
149 | if (strcmp(argv[i], "-c") == 0) { |
368deb23 |
150 | patch_callsites = 1; |
151 | continue; |
152 | } |
87ccd363 |
153 | if (strcmp(argv[i], "-i") == 0) { |
154 | ignore_missing = 1; |
155 | continue; |
156 | } |
368deb23 |
157 | |
158 | f = fopen(argv[i], "r"); |
159 | my_assert_not(f, NULL); |
160 | read_list(&symlist, &symlist_cnt, &symlist_alloc, |
87ccd363 |
161 | f, patch_callsites, ignore_missing); |
368deb23 |
162 | fclose(f); |
ede51255 |
163 | |
368deb23 |
164 | patch_callsites = 0; |
87ccd363 |
165 | ignore_missing = 0; |
368deb23 |
166 | } |
ede51255 |
167 | |
168 | qsort(symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym_sort); |
169 | |
8022df01 |
170 | #if 0 |
171 | printf("symlist:\n"); |
172 | for (i = 0; i < symlist_cnt; i++) |
368deb23 |
173 | printf("%d '%s'\n", symlist[i].callsites, symlist[i].name); |
8022df01 |
174 | #endif |
ede51255 |
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 | |
28023f83 |
194 | if (IS_OR2(word2, "proc", "endp")) { |
d4e3b5db |
195 | if (IS(word2, "proc")) |
196 | strcpy(func, word); |
197 | else |
198 | func[0] = 0; |
199 | |
ede51255 |
200 | ssym.name = word; |
201 | sym = bsearch(&ssym, symlist, symlist_cnt, |
202 | sizeof(symlist[0]), cmp_sym); |
203 | if (sym != NULL) { |
8022df01 |
204 | sym->found = 1; |
7aca4698 |
205 | fprintf(fout, "%s\t%s%s", sym_use(sym, 1), word2, p); |
ede51255 |
206 | continue; |
207 | } |
208 | } |
209 | |
de15d43b |
210 | if (IS_OR2(word, "call", "jmp")) { |
ede51255 |
211 | ssym.name = word2; |
212 | sym = bsearch(&ssym, symlist, symlist_cnt, |
213 | sizeof(symlist[0]), cmp_sym); |
7aca4698 |
214 | if (sym != NULL) { |
8022df01 |
215 | fprintf(fout, "\t\t%s\t%s%s", word, |
7aca4698 |
216 | sym_use(sym, sym->callsites || IS(word2, func)), p); |
ede51255 |
217 | continue; |
218 | } |
219 | } |
220 | |
de15d43b |
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) { |
7aca4698 |
226 | fprintf(fout, "\t\tpublic %s%s", sym_use(sym, 1), p); |
de15d43b |
227 | continue; |
228 | } |
229 | } |
230 | |
ede51255 |
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 | |
28023f83 |
237 | // push offset <sym> |
092f64e1 |
238 | if (IS(word, "push") && IS(word2, "offset")) { |
ede51255 |
239 | ssym.name = word3; |
240 | sym = bsearch(&ssym, symlist, symlist_cnt, |
241 | sizeof(symlist[0]), cmp_sym); |
7aca4698 |
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); |
ede51255 |
245 | continue; |
246 | } |
247 | } |
248 | |
092f64e1 |
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 | |
de50b98b |
265 | // dd offset <sym> |
a3047546 |
266 | if (IS(word, "dd") |
267 | && (IS(word2, "offset") || strstr(p, "offset"))) |
268 | { |
de50b98b |
269 | fprintf(fout, "\t\tdd"); |
a3047546 |
270 | p = next_word(word, sizeof(word), line); |
de50b98b |
271 | goto offset_loop; |
272 | } |
273 | |
ede51255 |
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 | |
28023f83 |
280 | // <name> dd offset <sym> |
a3047546 |
281 | if (IS(word2, "dd") |
282 | && (IS(word3, "offset") || strstr(p, "offset"))) |
283 | { |
de50b98b |
284 | fprintf(fout, "%s\tdd", word); |
a3047546 |
285 | p = next_word(word, sizeof(word), line); |
286 | p = next_word(word, sizeof(word), p); |
de50b98b |
287 | goto offset_loop; |
ede51255 |
288 | } |
289 | |
28023f83 |
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); |
7aca4698 |
297 | if (sym != NULL) { |
28023f83 |
298 | fprintf(fout, "\t\t%s\t%s %s %s%s", |
299 | word, word2, word3, |
7aca4698 |
300 | sym_use(sym, sym->callsites), p); |
28023f83 |
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); |
7aca4698 |
323 | if (sym != NULL) { |
28023f83 |
324 | fprintf(fout, "\t\t%s\tdword ptr %s offset %s%s", |
7aca4698 |
325 | word, word4, sym_use(sym, sym->callsites), p); |
28023f83 |
326 | continue; |
327 | } |
328 | } |
329 | |
ede51255 |
330 | pass: |
331 | fwrite(line, 1, strlen(line), fout); |
de50b98b |
332 | continue; |
333 | |
334 | offset_loop: |
335 | while (1) { |
a3047546 |
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); |
de50b98b |
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", |
7aca4698 |
357 | (sym != NULL) ? sym_use(sym, sym->callsites) : word, |
de50b98b |
358 | p2 ? "," : ""); |
de50b98b |
359 | } |
360 | fprintf(fout, "%s", p); |
361 | continue; |
ede51255 |
362 | } |
363 | |
8022df01 |
364 | for (i = 0; i < symlist_cnt; i++) { |
87ccd363 |
365 | if (!symlist[i].found && !symlist[i].ignore_missing) |
8022df01 |
366 | printf("warning: sym '%s' not found\n", symlist[i].name); |
367 | } |
368 | |
ede51255 |
369 | fclose(fin); |
370 | fclose(fout); |
371 | |
372 | return 0; |
373 | } |
de50b98b |
374 | |
375 | // vim:ts=2:shiftwidth=2 |