10 unsigned int callsites:1;
12 unsigned int ignore_missing:1;
15 static int cmp_sym(const void *p1_, const void *p2_)
17 const struct sl_item *p1 = p1_, *p2 = p2_;
18 const char *s1 = p1->name, *s2 = p2->name;
27 if ((s1[i] | s2[i]) == 0)
33 if (s1[i] == 0 && s2[i] == '@')
35 if (s1[i] == '@' && s2[i] == 0)
44 static int cmp_sym_sort(const void *p1_, const void *p2_)
46 const struct sl_item *p1 = p1_, *p2 = p2_;
47 const char *s1 = p1->name, *s2 = p2->name;
50 ret = cmp_sym(p1_, p2_);
52 printf("%s: dupe sym: '%s' '%s'\n", __func__, s1, s2);
58 void read_list(struct sl_item **sl_in, int *cnt, int *alloc,
59 FILE *f, int callsites, int ignore_missing)
61 struct sl_item *sl = *sl_in;
66 while (fgets(line, sizeof(line), f) != NULL) {
67 next_word(word, sizeof(word), line);
68 if (word[0] == 0 || word[0] == ';' || word[0] == '#')
71 sl[c].name = strdup(word);
72 sl[c].callsites = callsites;
73 sl[c].ignore_missing = ignore_missing;
79 sl = realloc(sl, *alloc * sizeof(sl[0]));
80 my_assert_not(sl, NULL);
81 memset(sl + c, 0, (*alloc - c) * sizeof(sl[0]));
89 const char *sym_use(const struct sl_item *sym, int is_rm)
91 static char buf[256+3];
94 ret = snprintf(buf, sizeof(buf), "%s%s",
95 is_rm ? "rm_" : "", sym->name);
96 if (ret >= sizeof(buf)) {
97 printf("truncation detected: '%s'\n", buf);
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))
108 int main(int argc, char *argv[])
110 struct sl_item *symlist, *sym, ssym = { NULL, };
111 int patch_callsites = 0;
112 int ignore_missing = 0;
113 FILE *fout, *fin, *f;
128 printf("usage:\n%s <asmf_out> <asmf_in> [[-c][-i] <listf>]*>\n",
130 printf(" -c - patch callsites\n"
131 " -i - ignore missing syms\n");
137 symlist = calloc(symlist_alloc, sizeof(symlist[0]));
138 my_assert_not(symlist, NULL);
140 for (i = 3; i < argc; i++) {
141 if (strcmp(argv[i], "-c") == 0) {
145 if (strcmp(argv[i], "-i") == 0) {
150 f = fopen(argv[i], "r");
151 my_assert_not(f, NULL);
152 read_list(&symlist, &symlist_cnt, &symlist_alloc,
153 f, patch_callsites, ignore_missing);
160 qsort(symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym_sort);
163 printf("symlist:\n");
164 for (i = 0; i < symlist_cnt; i++)
165 printf("%d '%s'\n", symlist[i].callsites, symlist[i].name);
168 fin = fopen(argv[2], "r");
169 my_assert_not(fin, NULL);
171 fout = fopen(argv[1], "w");
172 my_assert_not(fout, NULL);
174 while (fgets(line, sizeof(line), fin))
177 if (*p == 0 || *p == ';')
180 p = sskip(next_word(word, sizeof(word), p));
181 if (*p == 0 || *p == ';')
182 goto pass; // need at least 2 words
184 p = next_word(word2, sizeof(word2), p);
186 if (IS_OR2(word2, "proc", "endp")) {
187 if (IS(word2, "proc"))
193 sym = bsearch(&ssym, symlist, symlist_cnt,
194 sizeof(symlist[0]), cmp_sym);
197 fprintf(fout, "%s\t%s%s", sym_use(sym, 1), word2, p);
202 if (IS_OR2(word, "call", "jmp")) {
204 sym = bsearch(&ssym, symlist, symlist_cnt,
205 sizeof(symlist[0]), cmp_sym);
207 fprintf(fout, "\t\t%s\t%s%s", word,
208 sym_use(sym, sym->callsites || IS(word2, func)), p);
213 if (IS(word, "public")) {
215 sym = bsearch(&ssym, symlist, symlist_cnt,
216 sizeof(symlist[0]), cmp_sym);
218 fprintf(fout, "\t\tpublic %s%s", sym_use(sym, 1), p);
224 if (*p == 0 || *p == ';')
225 goto pass; // need at least 3 words
227 p = next_word(word3, sizeof(word3), p);
230 if (IS(word, "push") && IS(word2, "offset")) {
232 sym = bsearch(&ssym, symlist, symlist_cnt,
233 sizeof(symlist[0]), cmp_sym);
235 fprintf(fout, "\t\t%s %s %s%s", word, word2,
236 sym_use(sym, sym->callsites || IS(word3, func)), p);
242 if (word[0] == 'j' && IS(word2, "short") && !IS(word3, "exit")) {
244 sym = bsearch(&ssym, symlist, symlist_cnt,
245 sizeof(symlist[0]), cmp_sym);
247 fprintf(fout, "\t\t%s ", word);
248 // for conditional "call", don't print 'short'
250 fprintf(fout, "short ");
251 fprintf(fout, "%s%s",
252 sym_use(sym, sym->callsites || IS(word3, func)), p);
259 && (IS(word2, "offset") || strstr(p, "offset")))
261 fprintf(fout, "\t\tdd");
262 p = next_word(word, sizeof(word), line);
267 if (*p == 0 || *p == ';')
268 goto pass; // need at least 4 words
270 p = next_word(word4, sizeof(word4), p);
272 // <name> dd offset <sym>
274 && (IS(word3, "offset") || strstr(p, "offset")))
276 fprintf(fout, "%s\tdd", word);
277 p = next_word(word, sizeof(word), line);
278 p = next_word(word, sizeof(word), p);
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")) ) {
287 sym = bsearch(&ssym, symlist, symlist_cnt,
288 sizeof(symlist[0]), cmp_sym);
290 fprintf(fout, "\t\t%s\t%s %s %s%s",
292 sym_use(sym, sym->callsites), p);
298 if (*p == 0 || *p == ';')
299 goto pass; // need at least 5 words
301 p = next_word(word5, sizeof(word5), p);
304 if (*p == 0 || *p == ';')
305 goto pass; // need at least 6 words
307 p = next_word(word6, sizeof(word6), p);
309 // <op> dword ptr <something>, offset <sym>
310 if ( IS(word2, "dword") && IS(word3, "ptr")
311 && IS(word5, "offset") ) {
313 sym = bsearch(&ssym, symlist, symlist_cnt,
314 sizeof(symlist[0]), cmp_sym);
316 fprintf(fout, "\t\t%s\tdword ptr %s offset %s%s",
317 word, word4, sym_use(sym, sym->callsites), p);
323 fwrite(line, 1, strlen(line), fout);
328 p2 = next_word(word, sizeof(word), p);
329 if (word[0] == 0 || word[0] == ';') {
332 if (!IS(word, "offset")) {
334 p2 = strstr(p, "offset");
337 fwrite(p, 1, p2 - p, fout);
338 p2 = next_word(word, sizeof(word), p2);
340 p = next_word(word, sizeof(word), p2);
341 p2 = strchr(word, ',');
346 sym = bsearch(&ssym, symlist, symlist_cnt,
347 sizeof(symlist[0]), cmp_sym);
348 fprintf(fout, " offset %s%s",
349 (sym != NULL) ? sym_use(sym, sym->callsites) : word,
352 fprintf(fout, "%s", p);
356 for (i = 0; i < symlist_cnt; i++) {
357 if (!symlist[i].found && !symlist[i].ignore_missing)
358 printf("warning: sym '%s' not found\n", symlist[i].name);
367 // vim:ts=2:shiftwidth=2