5 * This work is licensed under the terms of 3-clause BSD license.
6 * See COPYING file in the top-level directory.
13 #include "my_assert.h"
18 unsigned int callsites:1;
20 unsigned int ignore_missing:1;
23 static int cmp_sym(const void *p1_, const void *p2_)
25 const struct sl_item *p1 = p1_, *p2 = p2_;
26 const char *s1 = p1->name, *s2 = p2->name;
35 if ((s1[i] | s2[i]) == 0)
41 if (s1[i] == 0 && s2[i] == '@')
43 if (s1[i] == '@' && s2[i] == 0)
52 static int cmp_sym_sort(const void *p1_, const void *p2_)
54 const struct sl_item *p1 = p1_, *p2 = p2_;
55 const char *s1 = p1->name, *s2 = p2->name;
58 ret = cmp_sym(p1_, p2_);
60 printf("%s: dupe sym: '%s' '%s'\n", __func__, s1, s2);
66 void read_list(struct sl_item **sl_in, int *cnt, int *alloc,
67 FILE *f, int callsites, int ignore_missing)
69 struct sl_item *sl = *sl_in;
74 while (fgets(line, sizeof(line), f) != NULL) {
75 next_word(word, sizeof(word), line);
76 if (word[0] == 0 || word[0] == ';' || word[0] == '#')
79 sl[c].name = strdup(word);
80 sl[c].callsites = callsites;
81 sl[c].ignore_missing = ignore_missing;
87 sl = realloc(sl, *alloc * sizeof(sl[0]));
88 my_assert_not(sl, NULL);
89 memset(sl + c, 0, (*alloc - c) * sizeof(sl[0]));
97 const char *sym_use(const struct sl_item *sym, int is_rm)
99 static char buf[256+3];
102 ret = snprintf(buf, sizeof(buf), "%s%s",
103 is_rm ? "rm_" : "", sym->name);
104 if (ret >= sizeof(buf)) {
105 printf("truncation detected: '%s'\n", buf);
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))
116 int main(int argc, char *argv[])
118 struct sl_item *symlist, *sym, ssym = { NULL, };
119 int patch_callsites = 0;
120 int ignore_missing = 0;
121 FILE *fout, *fin, *f;
136 printf("usage:\n%s <asmf_out> <asmf_in> [[-c][-i] <listf>]*>\n",
138 printf(" -c - patch callsites\n"
139 " -i - ignore missing syms\n");
145 symlist = calloc(symlist_alloc, sizeof(symlist[0]));
146 my_assert_not(symlist, NULL);
148 for (i = 3; i < argc; i++) {
149 if (strcmp(argv[i], "-c") == 0) {
153 if (strcmp(argv[i], "-i") == 0) {
158 f = fopen(argv[i], "r");
159 my_assert_not(f, NULL);
160 read_list(&symlist, &symlist_cnt, &symlist_alloc,
161 f, patch_callsites, ignore_missing);
168 qsort(symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym_sort);
171 printf("symlist:\n");
172 for (i = 0; i < symlist_cnt; i++)
173 printf("%d '%s'\n", symlist[i].callsites, symlist[i].name);
176 fin = fopen(argv[2], "r");
177 my_assert_not(fin, NULL);
179 fout = fopen(argv[1], "w");
180 my_assert_not(fout, NULL);
182 while (fgets(line, sizeof(line), fin))
185 if (*p == 0 || *p == ';')
188 p = sskip(next_word(word, sizeof(word), p));
189 if (*p == 0 || *p == ';')
190 goto pass; // need at least 2 words
192 p = next_word(word2, sizeof(word2), p);
194 if (IS_OR2(word2, "proc", "endp")) {
195 if (IS(word2, "proc"))
201 sym = bsearch(&ssym, symlist, symlist_cnt,
202 sizeof(symlist[0]), cmp_sym);
205 fprintf(fout, "%s\t%s%s", sym_use(sym, 1), word2, p);
210 if (IS_OR2(word, "call", "jmp")) {
212 sym = bsearch(&ssym, symlist, symlist_cnt,
213 sizeof(symlist[0]), cmp_sym);
215 fprintf(fout, "\t\t%s\t%s%s", word,
216 sym_use(sym, sym->callsites || IS(word2, func)), p);
221 if (IS(word, "public")) {
223 sym = bsearch(&ssym, symlist, symlist_cnt,
224 sizeof(symlist[0]), cmp_sym);
226 fprintf(fout, "\t\tpublic %s%s", sym_use(sym, 1), p);
232 if (*p == 0 || *p == ';')
233 goto pass; // need at least 3 words
235 p = next_word(word3, sizeof(word3), p);
238 if (IS(word, "push") && IS(word2, "offset")) {
240 sym = bsearch(&ssym, symlist, symlist_cnt,
241 sizeof(symlist[0]), cmp_sym);
243 fprintf(fout, "\t\t%s %s %s%s", word, word2,
244 sym_use(sym, sym->callsites || IS(word3, func)), p);
250 if (word[0] == 'j' && IS(word2, "short") && !IS(word3, "exit")) {
252 sym = bsearch(&ssym, symlist, symlist_cnt,
253 sizeof(symlist[0]), cmp_sym);
255 fprintf(fout, "\t\t%s ", word);
256 // for conditional "call", don't print 'short'
258 fprintf(fout, "short ");
259 fprintf(fout, "%s%s",
260 sym_use(sym, sym->callsites || IS(word3, func)), p);
267 && (IS(word2, "offset") || strstr(p, "offset")))
269 fprintf(fout, "\t\tdd");
270 p = next_word(word, sizeof(word), line);
275 if (*p == 0 || *p == ';')
276 goto pass; // need at least 4 words
278 p = next_word(word4, sizeof(word4), p);
280 // <name> dd offset <sym>
282 && (IS(word3, "offset") || strstr(p, "offset")))
284 fprintf(fout, "%s\tdd", word);
285 p = next_word(word, sizeof(word), line);
286 p = next_word(word, sizeof(word), p);
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")) ) {
295 sym = bsearch(&ssym, symlist, symlist_cnt,
296 sizeof(symlist[0]), cmp_sym);
298 fprintf(fout, "\t\t%s\t%s %s %s%s",
300 sym_use(sym, sym->callsites), p);
306 if (*p == 0 || *p == ';')
307 goto pass; // need at least 5 words
309 p = next_word(word5, sizeof(word5), p);
312 if (*p == 0 || *p == ';')
313 goto pass; // need at least 6 words
315 p = next_word(word6, sizeof(word6), p);
317 // <op> dword ptr <something>, offset <sym>
318 if ( IS(word2, "dword") && IS(word3, "ptr")
319 && IS(word5, "offset") ) {
321 sym = bsearch(&ssym, symlist, symlist_cnt,
322 sizeof(symlist[0]), cmp_sym);
324 fprintf(fout, "\t\t%s\tdword ptr %s offset %s%s",
325 word, word4, sym_use(sym, sym->callsites), p);
331 fwrite(line, 1, strlen(line), fout);
336 p2 = next_word(word, sizeof(word), p);
337 if (word[0] == 0 || word[0] == ';') {
340 if (!IS(word, "offset")) {
342 p2 = strstr(p, "offset");
345 fwrite(p, 1, p2 - p, fout);
346 p2 = next_word(word, sizeof(word), p2);
348 p = next_word(word, sizeof(word), p2);
349 p2 = strchr(word, ',');
354 sym = bsearch(&ssym, symlist, symlist_cnt,
355 sizeof(symlist[0]), cmp_sym);
356 fprintf(fout, " offset %s%s",
357 (sym != NULL) ? sym_use(sym, sym->callsites) : word,
360 fprintf(fout, "%s", p);
364 for (i = 0; i < symlist_cnt; i++) {
365 if (!symlist[i].found && !symlist[i].ignore_missing)
366 printf("warning: sym '%s' not found\n", symlist[i].name);
375 // vim:ts=2:shiftwidth=2