10 unsigned int callsites:1;
14 static int cmp_sym(const void *p1_, const void *p2_)
16 const struct sl_item *p1 = p1_, *p2 = p2_;
17 const char *s1 = p1->name, *s2 = p2->name;
21 if ((s1[i] | s2[i]) == 0)
27 if (s1[i] == 0 && s2[i] == '@')
29 if (s1[i] == '@' && s2[i] == 0)
38 static int cmp_sym_sort(const void *p1_, const void *p2_)
40 const struct sl_item *p1 = p1_, *p2 = p2_;
41 const char *s1 = p1->name, *s2 = p2->name;
44 ret = cmp_sym(p1_, p2_);
46 printf("%s: dupe sym: '%s' '%s'\n", __func__, s1, s2);
52 void read_list(struct sl_item **sl_in, int *cnt, int *alloc,
53 FILE *f, int callsites)
55 struct sl_item *sl = *sl_in;
60 while (fgets(line, sizeof(line), f) != NULL) {
61 next_word(word, sizeof(word), line);
62 if (word[0] == 0 || word[0] == ';' || word[0] == '#')
65 sl[c].name = strdup(word);
66 sl[c].callsites = callsites;
72 sl = realloc(sl, *alloc * sizeof(sl[0]));
73 my_assert_not(sl, NULL);
74 memset(sl + c, 0, (*alloc - c) * sizeof(sl[0]));
82 const char *sym_use(const struct sl_item *sym)
84 static char buf[256+3];
87 ret = snprintf(buf, sizeof(buf), "rm_%s", sym->name);
88 if (ret >= sizeof(buf)) {
89 printf("truncation detected: '%s'\n", buf);
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))
100 int main(int argc, char *argv[])
102 struct sl_item *symlist, *sym, ssym = { NULL, };
103 int patch_callsites = 0;
104 FILE *fout, *fin, *f;
119 // -c - patch callsites
120 printf("usage:\n%s <asmf_out> <asmf_in> [[-c] <listf>]*>\n",
127 symlist = calloc(symlist_alloc, sizeof(symlist[0]));
128 my_assert_not(symlist, NULL);
130 for (i = 3; i < argc; i++) {
131 if (strcmp(argv[i], "-c") == 0) {
136 f = fopen(argv[i], "r");
137 my_assert_not(f, NULL);
138 read_list(&symlist, &symlist_cnt, &symlist_alloc,
145 qsort(symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym_sort);
148 printf("symlist:\n");
149 for (i = 0; i < symlist_cnt; i++)
150 printf("%d '%s'\n", symlist[i].callsites, symlist[i].name);
153 fin = fopen(argv[2], "r");
154 my_assert_not(fin, NULL);
156 fout = fopen(argv[1], "w");
157 my_assert_not(fout, NULL);
159 while (fgets(line, sizeof(line), fin))
162 if (*p == 0 || *p == ';')
165 p = sskip(next_word(word, sizeof(word), p));
166 if (*p == 0 || *p == ';')
167 goto pass; // need at least 2 words
169 p = next_word(word2, sizeof(word2), p);
171 if (IS_OR2(word2, "proc", "endp")) {
172 if (IS(word2, "proc"))
178 sym = bsearch(&ssym, symlist, symlist_cnt,
179 sizeof(symlist[0]), cmp_sym);
182 fprintf(fout, "rm_%s\t%s%s", word, word2, p);
187 if (IS_OR3(word, "call", "jmp", "public")) {
189 sym = bsearch(&ssym, symlist, symlist_cnt,
190 sizeof(symlist[0]), cmp_sym);
192 && (sym->callsites || IS(word2, func)))
194 fprintf(fout, "\t\t%s\t%s%s", word,
201 if (*p == 0 || *p == ';')
202 goto pass; // need at least 3 words
204 p = next_word(word3, sizeof(word3), p);
208 if ( (IS(word, "push") && IS(word2, "offset"))
209 || (word[0] == 'j' && IS(word2, "short")) ) {
211 sym = bsearch(&ssym, symlist, symlist_cnt,
212 sizeof(symlist[0]), cmp_sym);
214 && (sym->callsites || IS(word3, func)))
216 fprintf(fout, "\t\t%s %s %s%s",
217 word, word2, sym_use(sym), p);
223 if (IS(word, "dd") && IS(word2, "offset")) {
224 fprintf(fout, "\t\tdd");
230 if (*p == 0 || *p == ';')
231 goto pass; // need at least 4 words
233 p = next_word(word4, sizeof(word4), p);
235 // <name> dd offset <sym>
236 if (IS(word2, "dd") && IS(word3, "offset")) {
237 fprintf(fout, "%s\tdd", word);
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")) ) {
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",
258 if (*p == 0 || *p == ';')
259 goto pass; // need at least 5 words
261 p = next_word(word5, sizeof(word5), p);
264 if (*p == 0 || *p == ';')
265 goto pass; // need at least 6 words
267 p = next_word(word6, sizeof(word6), p);
269 // <op> dword ptr <something>, offset <sym>
270 if ( IS(word2, "dword") && IS(word3, "ptr")
271 && IS(word5, "offset") ) {
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);
283 fwrite(line, 1, strlen(line), fout);
288 p2 = strchr(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,
299 p2 = next_word(word, sizeof(word), p);
300 if (word[0] == 0 || word[0] == ';') {
303 if (!IS(word, "offset")) {
304 printf("could not handle offset array\n");
307 p = next_word(word, sizeof(word), p2);
309 fprintf(fout, "%s", p);
313 for (i = 0; i < symlist_cnt; i++) {
314 if (!symlist[i].found)
315 printf("warning: sym '%s' not found\n", symlist[i].name);
324 // vim:ts=2:shiftwidth=2