| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | #include "my_assert.h" |
| 6 | #include "my_str.h" |
| 7 | |
| 8 | struct sl_item { |
| 9 | char *name; |
| 10 | unsigned int callsites:1; |
| 11 | unsigned int found:1; |
| 12 | }; |
| 13 | |
| 14 | static int cmp_sym(const void *p1_, const void *p2_) |
| 15 | { |
| 16 | const struct sl_item *p1 = p1_, *p2 = p2_; |
| 17 | const char *s1 = p1->name, *s2 = p2->name; |
| 18 | int i; |
| 19 | |
| 20 | for (i = 0; ; i++) { |
| 21 | if ((s1[i] | s2[i]) == 0) |
| 22 | break; |
| 23 | |
| 24 | if (s1[i] == s2[i]) |
| 25 | continue; |
| 26 | |
| 27 | if (s1[i] == 0 && s2[i] == '@') |
| 28 | break; |
| 29 | if (s1[i] == '@' && s2[i] == 0) |
| 30 | break; |
| 31 | |
| 32 | return s1[i] - s2[i]; |
| 33 | } |
| 34 | |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | static int cmp_sym_sort(const void *p1_, const void *p2_) |
| 39 | { |
| 40 | const struct sl_item *p1 = p1_, *p2 = p2_; |
| 41 | const char *s1 = p1->name, *s2 = p2->name; |
| 42 | int ret; |
| 43 | |
| 44 | ret = cmp_sym(p1_, p2_); |
| 45 | if (ret == 0) { |
| 46 | printf("%s: dupe sym: '%s' '%s'\n", __func__, s1, s2); |
| 47 | exit(1); |
| 48 | } |
| 49 | return ret; |
| 50 | } |
| 51 | |
| 52 | void read_list(struct sl_item **sl_in, int *cnt, int *alloc, |
| 53 | FILE *f, int callsites) |
| 54 | { |
| 55 | struct sl_item *sl = *sl_in; |
| 56 | int c = *cnt; |
| 57 | char line[256]; |
| 58 | char word[256]; |
| 59 | |
| 60 | while (fgets(line, sizeof(line), f) != NULL) { |
| 61 | next_word(word, sizeof(word), line); |
| 62 | if (word[0] == 0 || word[0] == ';' || word[0] == '#') |
| 63 | continue; |
| 64 | |
| 65 | sl[c].name = strdup(word); |
| 66 | sl[c].callsites = callsites; |
| 67 | sl[c].found = 0; |
| 68 | c++; |
| 69 | |
| 70 | if (c >= *alloc) { |
| 71 | *alloc *= 2; |
| 72 | sl = realloc(sl, *alloc * sizeof(sl[0])); |
| 73 | my_assert_not(sl, NULL); |
| 74 | memset(sl + c, 0, (*alloc - c) * sizeof(sl[0])); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | *sl_in = sl; |
| 79 | *cnt = c; |
| 80 | } |
| 81 | |
| 82 | const char *sym_use(const struct sl_item *sym) |
| 83 | { |
| 84 | static char buf[256+3]; |
| 85 | int ret; |
| 86 | |
| 87 | ret = snprintf(buf, sizeof(buf), "rm_%s", sym->name); |
| 88 | if (ret >= sizeof(buf)) { |
| 89 | printf("truncation detected: '%s'\n", buf); |
| 90 | exit(1); |
| 91 | } |
| 92 | |
| 93 | return buf; |
| 94 | } |
| 95 | |
| 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)) |
| 99 | |
| 100 | int main(int argc, char *argv[]) |
| 101 | { |
| 102 | struct sl_item *symlist, *sym, ssym = { NULL, }; |
| 103 | int patch_callsites = 0; |
| 104 | FILE *fout, *fin, *f; |
| 105 | int symlist_alloc; |
| 106 | int symlist_cnt; |
| 107 | char line[256]; |
| 108 | char word[256]; |
| 109 | char word2[256]; |
| 110 | char word3[256]; |
| 111 | char word4[256]; |
| 112 | char word5[256]; |
| 113 | char word6[256]; |
| 114 | char *p; |
| 115 | int i; |
| 116 | |
| 117 | if (argc < 4) { |
| 118 | // -c - patch callsites |
| 119 | printf("usage:\n%s <asmf_out> <asmf_in> [[-c] <listf>]*>\n", |
| 120 | argv[0]); |
| 121 | return 1; |
| 122 | } |
| 123 | |
| 124 | symlist_alloc = 16; |
| 125 | symlist_cnt = 0; |
| 126 | symlist = calloc(symlist_alloc, sizeof(symlist[0])); |
| 127 | my_assert_not(symlist, NULL); |
| 128 | |
| 129 | for (i = 3; i < argc; i++) { |
| 130 | if (strcmp(argv[i], "-c") == 0) { |
| 131 | patch_callsites = 1; |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | f = fopen(argv[i], "r"); |
| 136 | my_assert_not(f, NULL); |
| 137 | read_list(&symlist, &symlist_cnt, &symlist_alloc, |
| 138 | f, patch_callsites); |
| 139 | fclose(f); |
| 140 | |
| 141 | patch_callsites = 0; |
| 142 | } |
| 143 | |
| 144 | qsort(symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym_sort); |
| 145 | |
| 146 | #if 0 |
| 147 | printf("symlist:\n"); |
| 148 | for (i = 0; i < symlist_cnt; i++) |
| 149 | printf("%d '%s'\n", symlist[i].callsites, symlist[i].name); |
| 150 | #endif |
| 151 | |
| 152 | fin = fopen(argv[2], "r"); |
| 153 | my_assert_not(fin, NULL); |
| 154 | |
| 155 | fout = fopen(argv[1], "w"); |
| 156 | my_assert_not(fout, NULL); |
| 157 | |
| 158 | while (fgets(line, sizeof(line), fin)) |
| 159 | { |
| 160 | p = sskip(line); |
| 161 | if (*p == 0 || *p == ';') |
| 162 | goto pass; |
| 163 | |
| 164 | p = sskip(next_word(word, sizeof(word), p)); |
| 165 | if (*p == 0 || *p == ';') |
| 166 | goto pass; // need at least 2 words |
| 167 | |
| 168 | p = next_word(word2, sizeof(word2), p); |
| 169 | |
| 170 | if (IS_OR2(word2, "proc", "endp")) { |
| 171 | ssym.name = word; |
| 172 | sym = bsearch(&ssym, symlist, symlist_cnt, |
| 173 | sizeof(symlist[0]), cmp_sym); |
| 174 | if (sym != NULL) { |
| 175 | sym->found = 1; |
| 176 | fprintf(fout, "rm_%s\t%s%s", word, word2, p); |
| 177 | continue; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (IS_OR3(word, "call", "jmp", "public")) { |
| 182 | ssym.name = word2; |
| 183 | sym = bsearch(&ssym, symlist, symlist_cnt, |
| 184 | sizeof(symlist[0]), cmp_sym); |
| 185 | if (sym != NULL && sym->callsites) { |
| 186 | fprintf(fout, "\t\t%s\t%s%s", word, |
| 187 | sym_use(sym), p); |
| 188 | continue; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | p = sskip(p); |
| 193 | if (*p == 0 || *p == ';') |
| 194 | goto pass; // need at least 3 words |
| 195 | |
| 196 | p = next_word(word3, sizeof(word3), p); |
| 197 | |
| 198 | // dd offset <sym> |
| 199 | // push offset <sym> |
| 200 | // jcc short <sym> |
| 201 | if ( (IS_OR2(word, "dd", "push") && IS(word2, "offset")) |
| 202 | || (word[0] == 'j' && IS(word2, "short")) ) { |
| 203 | ssym.name = word3; |
| 204 | sym = bsearch(&ssym, symlist, symlist_cnt, |
| 205 | sizeof(symlist[0]), cmp_sym); |
| 206 | if (sym != NULL && sym->callsites) { |
| 207 | fprintf(fout, "\t\t%s %s %s%s", |
| 208 | word, word2, sym_use(sym), p); |
| 209 | continue; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | p = sskip(p); |
| 214 | if (*p == 0 || *p == ';') |
| 215 | goto pass; // need at least 4 words |
| 216 | |
| 217 | p = next_word(word4, sizeof(word4), p); |
| 218 | |
| 219 | // <name> dd offset <sym> |
| 220 | if (IS(word2, "dd") && IS(word3, "offset")) { |
| 221 | ssym.name = word4; |
| 222 | sym = bsearch(&ssym, symlist, symlist_cnt, |
| 223 | sizeof(symlist[0]), cmp_sym); |
| 224 | if (sym != NULL && sym->callsites) { |
| 225 | fprintf(fout, "%s\tdd offset %s%s", word, |
| 226 | sym_use(sym), p); |
| 227 | continue; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // mov <something>, offset <sym> |
| 232 | // jcc <some> ptr <sym> |
| 233 | if ( (IS(word, "mov") && IS(word3, "offset")) |
| 234 | || (word[0] == 'j' && IS(word3, "ptr")) ) { |
| 235 | ssym.name = word4; |
| 236 | sym = bsearch(&ssym, symlist, symlist_cnt, |
| 237 | sizeof(symlist[0]), cmp_sym); |
| 238 | if (sym != NULL && sym->callsites) { |
| 239 | fprintf(fout, "\t\t%s\t%s %s %s%s", |
| 240 | word, word2, word3, |
| 241 | sym_use(sym), p); |
| 242 | continue; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | p = sskip(p); |
| 247 | if (*p == 0 || *p == ';') |
| 248 | goto pass; // need at least 5 words |
| 249 | |
| 250 | p = next_word(word5, sizeof(word5), p); |
| 251 | |
| 252 | p = sskip(p); |
| 253 | if (*p == 0 || *p == ';') |
| 254 | goto pass; // need at least 6 words |
| 255 | |
| 256 | p = next_word(word6, sizeof(word6), p); |
| 257 | |
| 258 | // <op> dword ptr <something>, offset <sym> |
| 259 | if ( IS(word2, "dword") && IS(word3, "ptr") |
| 260 | && IS(word5, "offset") ) { |
| 261 | ssym.name = word6; |
| 262 | sym = bsearch(&ssym, symlist, symlist_cnt, |
| 263 | sizeof(symlist[0]), cmp_sym); |
| 264 | if (sym != NULL && sym->callsites) { |
| 265 | fprintf(fout, "\t\t%s\tdword ptr %s offset %s%s", |
| 266 | word, word4, sym_use(sym), p); |
| 267 | continue; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | pass: |
| 272 | fwrite(line, 1, strlen(line), fout); |
| 273 | } |
| 274 | |
| 275 | for (i = 0; i < symlist_cnt; i++) { |
| 276 | if (!symlist[i].found) |
| 277 | printf("warning: sym '%s' not found\n", symlist[i].name); |
| 278 | } |
| 279 | |
| 280 | fclose(fin); |
| 281 | fclose(fout); |
| 282 | |
| 283 | return 0; |
| 284 | } |