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 | |
21 | for (i = 0; ; i++) { |
0c5547d1 |
22 | if ((s1[i] | s2[i]) == 0) |
23 | break; |
24 | |
ede51255 |
25 | if (s1[i] == s2[i]) |
26 | continue; |
27 | |
0c5547d1 |
28 | if (s1[i] == 0 && s2[i] == '@') |
29 | break; |
30 | if (s1[i] == '@' && s2[i] == 0) |
ede51255 |
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 | |
368deb23 |
53 | void read_list(struct sl_item **sl_in, int *cnt, int *alloc, |
87ccd363 |
54 | FILE *f, int callsites, int ignore_missing) |
ede51255 |
55 | { |
56 | struct sl_item *sl = *sl_in; |
57 | int c = *cnt; |
58 | char line[256]; |
59 | char word[256]; |
ede51255 |
60 | |
61 | while (fgets(line, sizeof(line), f) != NULL) { |
0c5547d1 |
62 | next_word(word, sizeof(word), line); |
63 | if (word[0] == 0 || word[0] == ';' || word[0] == '#') |
ede51255 |
64 | continue; |
65 | |
66 | sl[c].name = strdup(word); |
368deb23 |
67 | sl[c].callsites = callsites; |
87ccd363 |
68 | sl[c].ignore_missing = ignore_missing; |
8022df01 |
69 | sl[c].found = 0; |
ede51255 |
70 | c++; |
71 | |
72 | if (c >= *alloc) { |
73 | *alloc *= 2; |
74 | sl = realloc(sl, *alloc * sizeof(sl[0])); |
75 | my_assert_not(sl, NULL); |
8022df01 |
76 | memset(sl + c, 0, (*alloc - c) * sizeof(sl[0])); |
ede51255 |
77 | } |
78 | } |
79 | |
80 | *sl_in = sl; |
81 | *cnt = c; |
82 | } |
83 | |
8022df01 |
84 | const char *sym_use(const struct sl_item *sym) |
85 | { |
86 | static char buf[256+3]; |
87 | int ret; |
88 | |
0c5547d1 |
89 | ret = snprintf(buf, sizeof(buf), "rm_%s", sym->name); |
8022df01 |
90 | if (ret >= sizeof(buf)) { |
91 | printf("truncation detected: '%s'\n", buf); |
92 | exit(1); |
93 | } |
94 | |
95 | return buf; |
96 | } |
97 | |
28023f83 |
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 | |
ede51255 |
102 | int main(int argc, char *argv[]) |
103 | { |
8022df01 |
104 | struct sl_item *symlist, *sym, ssym = { NULL, }; |
368deb23 |
105 | int patch_callsites = 0; |
87ccd363 |
106 | int ignore_missing = 0; |
ede51255 |
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]; |
28023f83 |
115 | char word5[256]; |
116 | char word6[256]; |
d4e3b5db |
117 | char func[256]; |
de50b98b |
118 | char *p, *p2; |
8022df01 |
119 | int i; |
ede51255 |
120 | |
368deb23 |
121 | if (argc < 4) { |
87ccd363 |
122 | printf("usage:\n%s <asmf_out> <asmf_in> [[-c][-i] <listf>]*>\n", |
ede51255 |
123 | argv[0]); |
87ccd363 |
124 | printf(" -c - patch callsites\n" |
125 | " -i - ignore missing syms\n"); |
ede51255 |
126 | return 1; |
127 | } |
128 | |
129 | symlist_alloc = 16; |
130 | symlist_cnt = 0; |
8022df01 |
131 | symlist = calloc(symlist_alloc, sizeof(symlist[0])); |
ede51255 |
132 | my_assert_not(symlist, NULL); |
133 | |
368deb23 |
134 | for (i = 3; i < argc; i++) { |
0c5547d1 |
135 | if (strcmp(argv[i], "-c") == 0) { |
368deb23 |
136 | patch_callsites = 1; |
137 | continue; |
138 | } |
87ccd363 |
139 | if (strcmp(argv[i], "-i") == 0) { |
140 | ignore_missing = 1; |
141 | continue; |
142 | } |
368deb23 |
143 | |
144 | f = fopen(argv[i], "r"); |
145 | my_assert_not(f, NULL); |
146 | read_list(&symlist, &symlist_cnt, &symlist_alloc, |
87ccd363 |
147 | f, patch_callsites, ignore_missing); |
368deb23 |
148 | fclose(f); |
ede51255 |
149 | |
368deb23 |
150 | patch_callsites = 0; |
87ccd363 |
151 | ignore_missing = 0; |
368deb23 |
152 | } |
ede51255 |
153 | |
154 | qsort(symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym_sort); |
155 | |
8022df01 |
156 | #if 0 |
157 | printf("symlist:\n"); |
158 | for (i = 0; i < symlist_cnt; i++) |
368deb23 |
159 | printf("%d '%s'\n", symlist[i].callsites, symlist[i].name); |
8022df01 |
160 | #endif |
ede51255 |
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 | |
28023f83 |
180 | if (IS_OR2(word2, "proc", "endp")) { |
d4e3b5db |
181 | if (IS(word2, "proc")) |
182 | strcpy(func, word); |
183 | else |
184 | func[0] = 0; |
185 | |
ede51255 |
186 | ssym.name = word; |
187 | sym = bsearch(&ssym, symlist, symlist_cnt, |
188 | sizeof(symlist[0]), cmp_sym); |
189 | if (sym != NULL) { |
8022df01 |
190 | sym->found = 1; |
ede51255 |
191 | fprintf(fout, "rm_%s\t%s%s", word, word2, p); |
192 | continue; |
193 | } |
194 | } |
195 | |
de15d43b |
196 | if (IS_OR2(word, "call", "jmp")) { |
ede51255 |
197 | ssym.name = word2; |
198 | sym = bsearch(&ssym, symlist, symlist_cnt, |
199 | sizeof(symlist[0]), cmp_sym); |
d4e3b5db |
200 | if (sym != NULL |
201 | && (sym->callsites || IS(word2, func))) |
202 | { |
8022df01 |
203 | fprintf(fout, "\t\t%s\t%s%s", word, |
204 | sym_use(sym), p); |
ede51255 |
205 | continue; |
206 | } |
207 | } |
208 | |
de15d43b |
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 | |
ede51255 |
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 | |
28023f83 |
225 | // push offset <sym> |
226 | // jcc short <sym> |
de50b98b |
227 | if ( (IS(word, "push") && IS(word2, "offset")) |
28023f83 |
228 | || (word[0] == 'j' && IS(word2, "short")) ) { |
ede51255 |
229 | ssym.name = word3; |
230 | sym = bsearch(&ssym, symlist, symlist_cnt, |
231 | sizeof(symlist[0]), cmp_sym); |
d4e3b5db |
232 | if (sym != NULL |
233 | && (sym->callsites || IS(word3, func))) |
234 | { |
28023f83 |
235 | fprintf(fout, "\t\t%s %s %s%s", |
236 | word, word2, sym_use(sym), p); |
ede51255 |
237 | continue; |
238 | } |
239 | } |
240 | |
de50b98b |
241 | // dd offset <sym> |
242 | if (IS(word, "dd") && IS(word2, "offset")) { |
243 | fprintf(fout, "\t\tdd"); |
244 | strcpy(word, word3); |
245 | goto offset_loop; |
246 | } |
247 | |
ede51255 |
248 | p = sskip(p); |
249 | if (*p == 0 || *p == ';') |
250 | goto pass; // need at least 4 words |
251 | |
252 | p = next_word(word4, sizeof(word4), p); |
253 | |
28023f83 |
254 | // <name> dd offset <sym> |
255 | if (IS(word2, "dd") && IS(word3, "offset")) { |
de50b98b |
256 | fprintf(fout, "%s\tdd", word); |
257 | strcpy(word, word4); |
258 | goto offset_loop; |
ede51255 |
259 | } |
260 | |
28023f83 |
261 | // mov <something>, offset <sym> |
262 | // jcc <some> ptr <sym> |
263 | if ( (IS(word, "mov") && IS(word3, "offset")) |
264 | || (word[0] == 'j' && IS(word3, "ptr")) ) { |
265 | ssym.name = word4; |
266 | sym = bsearch(&ssym, symlist, symlist_cnt, |
267 | sizeof(symlist[0]), cmp_sym); |
268 | if (sym != NULL && sym->callsites) { |
269 | fprintf(fout, "\t\t%s\t%s %s %s%s", |
270 | word, word2, word3, |
271 | sym_use(sym), p); |
272 | continue; |
273 | } |
274 | } |
275 | |
276 | p = sskip(p); |
277 | if (*p == 0 || *p == ';') |
278 | goto pass; // need at least 5 words |
279 | |
280 | p = next_word(word5, sizeof(word5), p); |
281 | |
282 | p = sskip(p); |
283 | if (*p == 0 || *p == ';') |
284 | goto pass; // need at least 6 words |
285 | |
286 | p = next_word(word6, sizeof(word6), p); |
287 | |
288 | // <op> dword ptr <something>, offset <sym> |
289 | if ( IS(word2, "dword") && IS(word3, "ptr") |
290 | && IS(word5, "offset") ) { |
291 | ssym.name = word6; |
292 | sym = bsearch(&ssym, symlist, symlist_cnt, |
293 | sizeof(symlist[0]), cmp_sym); |
294 | if (sym != NULL && sym->callsites) { |
295 | fprintf(fout, "\t\t%s\tdword ptr %s offset %s%s", |
296 | word, word4, sym_use(sym), p); |
297 | continue; |
298 | } |
299 | } |
300 | |
ede51255 |
301 | pass: |
302 | fwrite(line, 1, strlen(line), fout); |
de50b98b |
303 | continue; |
304 | |
305 | offset_loop: |
306 | while (1) { |
307 | p2 = strchr(word, ','); |
308 | if (p2) |
309 | *p2 = 0; |
310 | |
311 | ssym.name = word; |
312 | sym = bsearch(&ssym, symlist, symlist_cnt, |
313 | sizeof(symlist[0]), cmp_sym); |
314 | fprintf(fout, " offset %s%s", |
315 | (sym != NULL && sym->callsites) ? sym_use(sym) : word, |
316 | p2 ? "," : ""); |
317 | |
318 | p2 = next_word(word, sizeof(word), p); |
319 | if (word[0] == 0 || word[0] == ';') { |
320 | break; |
321 | } |
322 | if (!IS(word, "offset")) { |
323 | printf("could not handle offset array\n"); |
324 | break; |
325 | } |
326 | p = next_word(word, sizeof(word), p2); |
327 | } |
328 | fprintf(fout, "%s", p); |
329 | continue; |
ede51255 |
330 | } |
331 | |
8022df01 |
332 | for (i = 0; i < symlist_cnt; i++) { |
87ccd363 |
333 | if (!symlist[i].found && !symlist[i].ignore_missing) |
8022df01 |
334 | printf("warning: sym '%s' not found\n", symlist[i].name); |
335 | } |
336 | |
ede51255 |
337 | fclose(fin); |
338 | fclose(fout); |
339 | |
340 | return 0; |
341 | } |
de50b98b |
342 | |
343 | // vim:ts=2:shiftwidth=2 |