| 1 | /* |
| 2 | * ia32rtools |
| 3 | * (C) notaz, 2013,2014 |
| 4 | * |
| 5 | * This work is licensed under the terms of 3-clause BSD license. |
| 6 | * See COPYING file in the top-level directory. |
| 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | |
| 13 | #include "my_assert.h" |
| 14 | #include "my_str.h" |
| 15 | |
| 16 | struct sl_item { |
| 17 | char *name; |
| 18 | unsigned int callsites:1; |
| 19 | unsigned int found:1; |
| 20 | unsigned int ignore_missing:1; |
| 21 | }; |
| 22 | |
| 23 | static int cmp_sym(const void *p1_, const void *p2_) |
| 24 | { |
| 25 | const struct sl_item *p1 = p1_, *p2 = p2_; |
| 26 | const char *s1 = p1->name, *s2 = p2->name; |
| 27 | int i; |
| 28 | |
| 29 | if (*s1 == '_') |
| 30 | s1++; |
| 31 | if (*s2 == '_') |
| 32 | s2++; |
| 33 | |
| 34 | for (i = 0; ; i++) { |
| 35 | if ((s1[i] | s2[i]) == 0) |
| 36 | break; |
| 37 | |
| 38 | if (s1[i] == s2[i]) |
| 39 | continue; |
| 40 | |
| 41 | if (s1[i] == 0 && s2[i] == '@') |
| 42 | break; |
| 43 | if (s1[i] == '@' && s2[i] == 0) |
| 44 | break; |
| 45 | |
| 46 | return s1[i] - s2[i]; |
| 47 | } |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int cmp_sym_sort(const void *p1_, const void *p2_) |
| 53 | { |
| 54 | const struct sl_item *p1 = p1_, *p2 = p2_; |
| 55 | const char *s1 = p1->name, *s2 = p2->name; |
| 56 | int ret; |
| 57 | |
| 58 | ret = cmp_sym(p1_, p2_); |
| 59 | if (ret == 0) { |
| 60 | printf("%s: dupe sym: '%s' '%s'\n", __func__, s1, s2); |
| 61 | exit(1); |
| 62 | } |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | void read_list(struct sl_item **sl_in, int *cnt, int *alloc, |
| 67 | FILE *f, int callsites, int ignore_missing) |
| 68 | { |
| 69 | struct sl_item *sl = *sl_in; |
| 70 | int c = *cnt; |
| 71 | char line[256]; |
| 72 | char word[256]; |
| 73 | |
| 74 | while (fgets(line, sizeof(line), f) != NULL) { |
| 75 | next_word(word, sizeof(word), line); |
| 76 | if (word[0] == 0 || word[0] == ';' || word[0] == '#') |
| 77 | continue; |
| 78 | |
| 79 | sl[c].name = strdup(word); |
| 80 | sl[c].callsites = callsites; |
| 81 | sl[c].ignore_missing = ignore_missing; |
| 82 | sl[c].found = 0; |
| 83 | c++; |
| 84 | |
| 85 | if (c >= *alloc) { |
| 86 | *alloc *= 2; |
| 87 | sl = realloc(sl, *alloc * sizeof(sl[0])); |
| 88 | my_assert_not(sl, NULL); |
| 89 | memset(sl + c, 0, (*alloc - c) * sizeof(sl[0])); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | *sl_in = sl; |
| 94 | *cnt = c; |
| 95 | } |
| 96 | |
| 97 | const char *sym_use(const struct sl_item *sym, int is_rm) |
| 98 | { |
| 99 | static char buf[256+3]; |
| 100 | int ret; |
| 101 | |
| 102 | ret = snprintf(buf, sizeof(buf), "%s%s", |
| 103 | is_rm ? "rm_" : "", sym->name); |
| 104 | if (ret >= sizeof(buf)) { |
| 105 | printf("truncation detected: '%s'\n", buf); |
| 106 | exit(1); |
| 107 | } |
| 108 | |
| 109 | return buf; |
| 110 | } |
| 111 | |
| 112 | #define IS(w, y) !strcasecmp(w, y) |
| 113 | #define IS_OR2(w, x, y) (IS(w, x) || IS(w, y)) |
| 114 | #define IS_OR3(w, x, y, z) (IS(w, x) || IS(w, y) || IS(w, z)) |
| 115 | |
| 116 | int main(int argc, char *argv[]) |
| 117 | { |
| 118 | struct sl_item *symlist, *sym, ssym = { NULL, }; |
| 119 | int patch_callsites = 0; |
| 120 | int ignore_missing = 0; |
| 121 | FILE *fout, *fin, *f; |
| 122 | int symlist_alloc; |
| 123 | int symlist_cnt; |
| 124 | char line[256]; |
| 125 | char word[256]; |
| 126 | char word2[256]; |
| 127 | char word3[256]; |
| 128 | char word4[256]; |
| 129 | char word5[256]; |
| 130 | char word6[256]; |
| 131 | char func[256]; |
| 132 | char *p, *p2; |
| 133 | int i; |
| 134 | |
| 135 | if (argc < 4) { |
| 136 | printf("usage:\n%s <asmf_out> <asmf_in> [[-c][-i] <listf>]*>\n", |
| 137 | argv[0]); |
| 138 | printf(" -c - patch callsites\n" |
| 139 | " -i - ignore missing syms\n"); |
| 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | symlist_alloc = 16; |
| 144 | symlist_cnt = 0; |
| 145 | symlist = calloc(symlist_alloc, sizeof(symlist[0])); |
| 146 | my_assert_not(symlist, NULL); |
| 147 | |
| 148 | for (i = 3; i < argc; i++) { |
| 149 | if (strcmp(argv[i], "-c") == 0) { |
| 150 | patch_callsites = 1; |
| 151 | continue; |
| 152 | } |
| 153 | if (strcmp(argv[i], "-i") == 0) { |
| 154 | ignore_missing = 1; |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | f = fopen(argv[i], "r"); |
| 159 | my_assert_not(f, NULL); |
| 160 | read_list(&symlist, &symlist_cnt, &symlist_alloc, |
| 161 | f, patch_callsites, ignore_missing); |
| 162 | fclose(f); |
| 163 | |
| 164 | patch_callsites = 0; |
| 165 | ignore_missing = 0; |
| 166 | } |
| 167 | |
| 168 | qsort(symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym_sort); |
| 169 | |
| 170 | #if 0 |
| 171 | printf("symlist:\n"); |
| 172 | for (i = 0; i < symlist_cnt; i++) |
| 173 | printf("%d '%s'\n", symlist[i].callsites, symlist[i].name); |
| 174 | #endif |
| 175 | |
| 176 | fin = fopen(argv[2], "r"); |
| 177 | my_assert_not(fin, NULL); |
| 178 | |
| 179 | fout = fopen(argv[1], "w"); |
| 180 | my_assert_not(fout, NULL); |
| 181 | |
| 182 | while (fgets(line, sizeof(line), fin)) |
| 183 | { |
| 184 | p = sskip(line); |
| 185 | if (*p == 0 || *p == ';') |
| 186 | goto pass; |
| 187 | |
| 188 | p = sskip(next_word(word, sizeof(word), p)); |
| 189 | if (*p == 0 || *p == ';') |
| 190 | goto pass; // need at least 2 words |
| 191 | |
| 192 | p = next_word(word2, sizeof(word2), p); |
| 193 | |
| 194 | if (IS_OR2(word2, "proc", "endp")) { |
| 195 | if (IS(word2, "proc")) |
| 196 | strcpy(func, word); |
| 197 | else |
| 198 | func[0] = 0; |
| 199 | |
| 200 | ssym.name = word; |
| 201 | sym = bsearch(&ssym, symlist, symlist_cnt, |
| 202 | sizeof(symlist[0]), cmp_sym); |
| 203 | if (sym != NULL) { |
| 204 | sym->found = 1; |
| 205 | fprintf(fout, "%s\t%s%s", sym_use(sym, 1), word2, p); |
| 206 | continue; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (IS_OR2(word, "call", "jmp")) { |
| 211 | ssym.name = word2; |
| 212 | sym = bsearch(&ssym, symlist, symlist_cnt, |
| 213 | sizeof(symlist[0]), cmp_sym); |
| 214 | if (sym != NULL) { |
| 215 | fprintf(fout, "\t\t%s\t%s%s", word, |
| 216 | sym_use(sym, sym->callsites || IS(word2, func)), p); |
| 217 | continue; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if (IS(word, "public")) { |
| 222 | ssym.name = word2; |
| 223 | sym = bsearch(&ssym, symlist, symlist_cnt, |
| 224 | sizeof(symlist[0]), cmp_sym); |
| 225 | if (sym != NULL) { |
| 226 | fprintf(fout, "\t\tpublic %s%s", sym_use(sym, 1), p); |
| 227 | continue; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | p = sskip(p); |
| 232 | if (*p == 0 || *p == ';') |
| 233 | goto pass; // need at least 3 words |
| 234 | |
| 235 | p = next_word(word3, sizeof(word3), p); |
| 236 | |
| 237 | // push offset <sym> |
| 238 | if (IS(word, "push") && IS(word2, "offset")) { |
| 239 | ssym.name = word3; |
| 240 | sym = bsearch(&ssym, symlist, symlist_cnt, |
| 241 | sizeof(symlist[0]), cmp_sym); |
| 242 | if (sym != NULL) { |
| 243 | fprintf(fout, "\t\t%s %s %s%s", word, word2, |
| 244 | sym_use(sym, sym->callsites || IS(word3, func)), p); |
| 245 | continue; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // jcc short <sym> |
| 250 | if (word[0] == 'j' && IS(word2, "short") && !IS(word3, "exit")) { |
| 251 | ssym.name = word3; |
| 252 | sym = bsearch(&ssym, symlist, symlist_cnt, |
| 253 | sizeof(symlist[0]), cmp_sym); |
| 254 | if (sym != NULL) { |
| 255 | fprintf(fout, "\t\t%s ", word); |
| 256 | // for conditional "call", don't print 'short' |
| 257 | if (IS(word3, func)) |
| 258 | fprintf(fout, "short "); |
| 259 | fprintf(fout, "%s%s", |
| 260 | sym_use(sym, sym->callsites || IS(word3, func)), p); |
| 261 | continue; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // dd offset <sym> |
| 266 | if (IS(word, "dd") |
| 267 | && (IS(word2, "offset") || strstr(p, "offset"))) |
| 268 | { |
| 269 | fprintf(fout, "\t\tdd"); |
| 270 | p = next_word(word, sizeof(word), line); |
| 271 | goto offset_loop; |
| 272 | } |
| 273 | |
| 274 | p = sskip(p); |
| 275 | if (*p == 0 || *p == ';') |
| 276 | goto pass; // need at least 4 words |
| 277 | |
| 278 | p = next_word(word4, sizeof(word4), p); |
| 279 | |
| 280 | // <name> dd offset <sym> |
| 281 | if (IS(word2, "dd") |
| 282 | && (IS(word3, "offset") || strstr(p, "offset"))) |
| 283 | { |
| 284 | fprintf(fout, "%s\tdd", word); |
| 285 | p = next_word(word, sizeof(word), line); |
| 286 | p = next_word(word, sizeof(word), p); |
| 287 | goto offset_loop; |
| 288 | } |
| 289 | |
| 290 | // mov <something>, offset <sym> |
| 291 | // jcc <some> ptr <sym> |
| 292 | if ( (IS(word, "mov") && IS(word3, "offset")) |
| 293 | || (word[0] == 'j' && IS(word3, "ptr")) ) { |
| 294 | ssym.name = word4; |
| 295 | sym = bsearch(&ssym, symlist, symlist_cnt, |
| 296 | sizeof(symlist[0]), cmp_sym); |
| 297 | if (sym != NULL) { |
| 298 | fprintf(fout, "\t\t%s\t%s %s %s%s", |
| 299 | word, word2, word3, |
| 300 | sym_use(sym, sym->callsites), p); |
| 301 | continue; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | p = sskip(p); |
| 306 | if (*p == 0 || *p == ';') |
| 307 | goto pass; // need at least 5 words |
| 308 | |
| 309 | p = next_word(word5, sizeof(word5), p); |
| 310 | |
| 311 | p = sskip(p); |
| 312 | if (*p == 0 || *p == ';') |
| 313 | goto pass; // need at least 6 words |
| 314 | |
| 315 | p = next_word(word6, sizeof(word6), p); |
| 316 | |
| 317 | // <op> dword ptr <something>, offset <sym> |
| 318 | if ( IS(word2, "dword") && IS(word3, "ptr") |
| 319 | && IS(word5, "offset") ) { |
| 320 | ssym.name = word6; |
| 321 | sym = bsearch(&ssym, symlist, symlist_cnt, |
| 322 | sizeof(symlist[0]), cmp_sym); |
| 323 | if (sym != NULL) { |
| 324 | fprintf(fout, "\t\t%s\tdword ptr %s offset %s%s", |
| 325 | word, word4, sym_use(sym, sym->callsites), p); |
| 326 | continue; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | pass: |
| 331 | fwrite(line, 1, strlen(line), fout); |
| 332 | continue; |
| 333 | |
| 334 | offset_loop: |
| 335 | while (1) { |
| 336 | p2 = next_word(word, sizeof(word), p); |
| 337 | if (word[0] == 0 || word[0] == ';') { |
| 338 | break; |
| 339 | } |
| 340 | if (!IS(word, "offset")) { |
| 341 | // pass through |
| 342 | p2 = strstr(p, "offset"); |
| 343 | if (p2 == NULL) |
| 344 | break; |
| 345 | fwrite(p, 1, p2 - p, fout); |
| 346 | p2 = next_word(word, sizeof(word), p2); |
| 347 | } |
| 348 | p = next_word(word, sizeof(word), p2); |
| 349 | p2 = strchr(word, ','); |
| 350 | if (p2) |
| 351 | *p2 = 0; |
| 352 | |
| 353 | ssym.name = word; |
| 354 | sym = bsearch(&ssym, symlist, symlist_cnt, |
| 355 | sizeof(symlist[0]), cmp_sym); |
| 356 | fprintf(fout, " offset %s%s", |
| 357 | (sym != NULL) ? sym_use(sym, sym->callsites) : word, |
| 358 | p2 ? "," : ""); |
| 359 | } |
| 360 | fprintf(fout, "%s", p); |
| 361 | continue; |
| 362 | } |
| 363 | |
| 364 | for (i = 0; i < symlist_cnt; i++) { |
| 365 | if (!symlist[i].found && !symlist[i].ignore_missing) |
| 366 | printf("warning: sym '%s' not found\n", symlist[i].name); |
| 367 | } |
| 368 | |
| 369 | fclose(fin); |
| 370 | fclose(fout); |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | // vim:ts=2:shiftwidth=2 |