7 static int my_isblank(char c)
9 return c == '\t' || c == ' ' || c == '\r' || c == '\n';
12 static char *sskip(char *s)
14 while (my_isblank(*s))
20 static char *next_word(char *w, size_t wsize, char *s)
26 for (i = 0; i < wsize - 1; i++) {
27 if (*s == 0 || my_isblank(s[i]))
33 if (*s != 0 && !my_isblank(s[i]))
34 printf("warning: '%s' truncated\n", w);
41 unsigned int callsites:1;
45 static int cmp_sym(const void *p1_, const void *p2_)
47 const struct sl_item *p1 = p1_, *p2 = p2_;
48 const char *s1 = p1->name, *s2 = p2->name;
52 if ((s1[i] | s2[i]) == 0)
58 if (s1[i] == 0 && s2[i] == '@')
60 if (s1[i] == '@' && s2[i] == 0)
69 static int cmp_sym_sort(const void *p1_, const void *p2_)
71 const struct sl_item *p1 = p1_, *p2 = p2_;
72 const char *s1 = p1->name, *s2 = p2->name;
75 ret = cmp_sym(p1_, p2_);
77 printf("%s: dupe sym: '%s' '%s'\n", __func__, s1, s2);
83 void read_list(struct sl_item **sl_in, int *cnt, int *alloc,
84 FILE *f, int callsites)
86 struct sl_item *sl = *sl_in;
91 while (fgets(line, sizeof(line), f) != NULL) {
92 next_word(word, sizeof(word), line);
93 if (word[0] == 0 || word[0] == ';' || word[0] == '#')
96 sl[c].name = strdup(word);
97 sl[c].callsites = callsites;
103 sl = realloc(sl, *alloc * sizeof(sl[0]));
104 my_assert_not(sl, NULL);
105 memset(sl + c, 0, (*alloc - c) * sizeof(sl[0]));
113 const char *sym_use(const struct sl_item *sym)
115 static char buf[256+3];
118 ret = snprintf(buf, sizeof(buf), "rm_%s", sym->name);
119 if (ret >= sizeof(buf)) {
120 printf("truncation detected: '%s'\n", buf);
127 #define IS(w, y) !strcasecmp(w, y)
128 #define IS_OR2(w, x, y) (IS(w, x) || IS(w, y))
129 #define IS_OR3(w, x, y, z) (IS(w, x) || IS(w, y) || IS(w, z))
131 int main(int argc, char *argv[])
133 struct sl_item *symlist, *sym, ssym = { NULL, };
134 int patch_callsites = 0;
135 FILE *fout, *fin, *f;
149 // -c - patch callsites
150 printf("usage:\n%s <asmf_out> <asmf_in> [[-c] <listf>]*>\n",
157 symlist = calloc(symlist_alloc, sizeof(symlist[0]));
158 my_assert_not(symlist, NULL);
160 for (i = 3; i < argc; i++) {
161 if (strcmp(argv[i], "-c") == 0) {
166 f = fopen(argv[i], "r");
167 my_assert_not(f, NULL);
168 read_list(&symlist, &symlist_cnt, &symlist_alloc,
175 qsort(symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym_sort);
178 printf("symlist:\n");
179 for (i = 0; i < symlist_cnt; i++)
180 printf("%d '%s'\n", symlist[i].callsites, symlist[i].name);
183 fin = fopen(argv[2], "r");
184 my_assert_not(fin, NULL);
186 fout = fopen(argv[1], "w");
187 my_assert_not(fout, NULL);
189 while (fgets(line, sizeof(line), fin))
192 if (*p == 0 || *p == ';')
195 p = sskip(next_word(word, sizeof(word), p));
196 if (*p == 0 || *p == ';')
197 goto pass; // need at least 2 words
199 p = next_word(word2, sizeof(word2), p);
201 if (IS_OR2(word2, "proc", "endp")) {
203 sym = bsearch(&ssym, symlist, symlist_cnt,
204 sizeof(symlist[0]), cmp_sym);
207 fprintf(fout, "rm_%s\t%s%s", word, word2, p);
212 if (IS_OR3(word, "call", "jmp", "public")) {
214 sym = bsearch(&ssym, symlist, symlist_cnt,
215 sizeof(symlist[0]), cmp_sym);
216 if (sym != NULL && sym->callsites) {
217 fprintf(fout, "\t\t%s\t%s%s", word,
224 if (*p == 0 || *p == ';')
225 goto pass; // need at least 3 words
227 p = next_word(word3, sizeof(word3), p);
232 if ( (IS_OR2(word, "dd", "push") && IS(word2, "offset"))
233 || (word[0] == 'j' && IS(word2, "short")) ) {
235 sym = bsearch(&ssym, symlist, symlist_cnt,
236 sizeof(symlist[0]), cmp_sym);
237 if (sym != NULL && sym->callsites) {
238 fprintf(fout, "\t\t%s %s %s%s",
239 word, word2, sym_use(sym), p);
245 if (*p == 0 || *p == ';')
246 goto pass; // need at least 4 words
248 p = next_word(word4, sizeof(word4), p);
250 // <name> dd offset <sym>
251 if (IS(word2, "dd") && IS(word3, "offset")) {
253 sym = bsearch(&ssym, symlist, symlist_cnt,
254 sizeof(symlist[0]), cmp_sym);
255 if (sym != NULL && sym->callsites) {
256 fprintf(fout, "%s\tdd offset %s%s", word,
262 // mov <something>, offset <sym>
263 // jcc <some> ptr <sym>
264 if ( (IS(word, "mov") && IS(word3, "offset"))
265 || (word[0] == 'j' && IS(word3, "ptr")) ) {
267 sym = bsearch(&ssym, symlist, symlist_cnt,
268 sizeof(symlist[0]), cmp_sym);
269 if (sym != NULL && sym->callsites) {
270 fprintf(fout, "\t\t%s\t%s %s %s%s",
278 if (*p == 0 || *p == ';')
279 goto pass; // need at least 5 words
281 p = next_word(word5, sizeof(word5), p);
284 if (*p == 0 || *p == ';')
285 goto pass; // need at least 6 words
287 p = next_word(word6, sizeof(word6), p);
289 // <op> dword ptr <something>, offset <sym>
290 if ( IS(word2, "dword") && IS(word3, "ptr")
291 && IS(word5, "offset") ) {
293 sym = bsearch(&ssym, symlist, symlist_cnt,
294 sizeof(symlist[0]), cmp_sym);
295 if (sym != NULL && sym->callsites) {
296 fprintf(fout, "\t\t%s\tdword ptr %s offset %s%s",
297 word, word4, sym_use(sym), p);
303 fwrite(line, 1, strlen(line), fout);
306 for (i = 0; i < symlist_cnt; i++) {
307 if (!symlist[i].found)
308 printf("warning: sym '%s' not found\n", symlist[i].name);