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