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;
22 if ((s1[i] | s2[i]) == 0)
28 if (s1[i] == 0 && s2[i] == '@')
30 if (s1[i] == '@' && s2[i] == 0)
39 static int cmp_sym_sort(const void *p1_, const void *p2_)
41 const struct sl_item *p1 = p1_, *p2 = p2_;
42 const char *s1 = p1->name, *s2 = p2->name;
45 ret = cmp_sym(p1_, p2_);
47 printf("%s: dupe sym: '%s' '%s'\n", __func__, s1, s2);
53 void read_list(struct sl_item **sl_in, int *cnt, int *alloc,
54 FILE *f, int callsites, int ignore_missing)
56 struct sl_item *sl = *sl_in;
61 while (fgets(line, sizeof(line), f) != NULL) {
62 next_word(word, sizeof(word), line);
63 if (word[0] == 0 || word[0] == ';' || word[0] == '#')
66 sl[c].name = strdup(word);
67 sl[c].callsites = callsites;
68 sl[c].ignore_missing = ignore_missing;
74 sl = realloc(sl, *alloc * sizeof(sl[0]));
75 my_assert_not(sl, NULL);
76 memset(sl + c, 0, (*alloc - c) * sizeof(sl[0]));
84 const char *sym_use(const struct sl_item *sym)
86 static char buf[256+3];
89 ret = snprintf(buf, sizeof(buf), "rm_%s", sym->name);
90 if (ret >= sizeof(buf)) {
91 printf("truncation detected: '%s'\n", buf);
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))
102 int main(int argc, char *argv[])
104 struct sl_item *symlist, *sym, ssym = { NULL, };
105 int patch_callsites = 0;
106 int ignore_missing = 0;
107 FILE *fout, *fin, *f;
122 printf("usage:\n%s <asmf_out> <asmf_in> [[-c][-i] <listf>]*>\n",
124 printf(" -c - patch callsites\n"
125 " -i - ignore missing syms\n");
131 symlist = calloc(symlist_alloc, sizeof(symlist[0]));
132 my_assert_not(symlist, NULL);
134 for (i = 3; i < argc; i++) {
135 if (strcmp(argv[i], "-c") == 0) {
139 if (strcmp(argv[i], "-i") == 0) {
144 f = fopen(argv[i], "r");
145 my_assert_not(f, NULL);
146 read_list(&symlist, &symlist_cnt, &symlist_alloc,
147 f, patch_callsites, ignore_missing);
154 qsort(symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym_sort);
157 printf("symlist:\n");
158 for (i = 0; i < symlist_cnt; i++)
159 printf("%d '%s'\n", symlist[i].callsites, symlist[i].name);
162 fin = fopen(argv[2], "r");
163 my_assert_not(fin, NULL);
165 fout = fopen(argv[1], "w");
166 my_assert_not(fout, NULL);
168 while (fgets(line, sizeof(line), fin))
171 if (*p == 0 || *p == ';')
174 p = sskip(next_word(word, sizeof(word), p));
175 if (*p == 0 || *p == ';')
176 goto pass; // need at least 2 words
178 p = next_word(word2, sizeof(word2), p);
180 if (IS_OR2(word2, "proc", "endp")) {
181 if (IS(word2, "proc"))
187 sym = bsearch(&ssym, symlist, symlist_cnt,
188 sizeof(symlist[0]), cmp_sym);
191 fprintf(fout, "rm_%s\t%s%s", word, word2, p);
196 if (IS_OR2(word, "call", "jmp")) {
198 sym = bsearch(&ssym, symlist, symlist_cnt,
199 sizeof(symlist[0]), cmp_sym);
201 && (sym->callsites || IS(word2, func)))
203 fprintf(fout, "\t\t%s\t%s%s", word,
209 if (IS(word, "public")) {
211 sym = bsearch(&ssym, symlist, symlist_cnt,
212 sizeof(symlist[0]), cmp_sym);
214 fprintf(fout, "\t\tpublic %s%s", sym_use(sym), p);
220 if (*p == 0 || *p == ';')
221 goto pass; // need at least 3 words
223 p = next_word(word3, sizeof(word3), p);
227 if ( (IS(word, "push") && IS(word2, "offset"))
228 || (word[0] == 'j' && IS(word2, "short")) ) {
230 sym = bsearch(&ssym, symlist, symlist_cnt,
231 sizeof(symlist[0]), cmp_sym);
233 && (sym->callsites || IS(word3, func)))
235 fprintf(fout, "\t\t%s %s %s%s",
236 word, word2, sym_use(sym), p);
242 if (IS(word, "dd") && IS(word2, "offset")) {
243 fprintf(fout, "\t\tdd");
249 if (*p == 0 || *p == ';')
250 goto pass; // need at least 4 words
252 p = next_word(word4, sizeof(word4), p);
254 // <name> dd offset <sym>
255 if (IS(word2, "dd") && IS(word3, "offset")) {
256 fprintf(fout, "%s\tdd", word);
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")) ) {
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",
277 if (*p == 0 || *p == ';')
278 goto pass; // need at least 5 words
280 p = next_word(word5, sizeof(word5), p);
283 if (*p == 0 || *p == ';')
284 goto pass; // need at least 6 words
286 p = next_word(word6, sizeof(word6), p);
288 // <op> dword ptr <something>, offset <sym>
289 if ( IS(word2, "dword") && IS(word3, "ptr")
290 && IS(word5, "offset") ) {
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);
302 fwrite(line, 1, strlen(line), fout);
307 p2 = strchr(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,
318 p2 = next_word(word, sizeof(word), p);
319 if (word[0] == 0 || word[0] == ';') {
322 if (!IS(word, "offset")) {
323 printf("could not handle offset array\n");
326 p = next_word(word, sizeof(word), p2);
328 fprintf(fout, "%s", p);
332 for (i = 0; i < symlist_cnt; i++) {
333 if (!symlist[i].found && !symlist[i].ignore_missing)
334 printf("warning: sym '%s' not found\n", symlist[i].name);
343 // vim:ts=2:shiftwidth=2