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