| 1 |
/* |
| 2 |
* omfdump.c |
| 3 |
* |
| 4 |
* Very simple program to dump the contents of an OMF (OBJ) file |
| 5 |
* |
| 6 |
* This assumes a littleendian, unaligned-load-capable host and a |
| 7 |
* C compiler which handles basic C99. |
| 8 |
* |
| 9 |
* Copyright (c) 1996-2010, the NASM Authors |
| 10 |
* Copyright (c) 2023, Bernd Boeckmann |
| 11 |
* |
| 12 |
* see LICENCE file for licensing terms |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "compiler.h" |
| 16 |
#include "bytesex.h" |
| 17 |
|
| 18 |
#include <ctype.h> |
| 19 |
|
| 20 |
typedef struct { |
| 21 |
uint16_t name_idx; |
| 22 |
uint16_t class_idx; |
| 23 |
uint16_t ovl_idx; |
| 24 |
} segdef_t; |
| 25 |
|
| 26 |
typedef struct { |
| 27 |
uint16_t name_idx; |
| 28 |
} grpdef_t; |
| 29 |
|
| 30 |
const char *progname; |
| 31 |
|
| 32 |
static const char *record_types[256] = { |
| 33 |
[0x80] = "THEADR", |
| 34 |
[0x82] = "LHEADR", |
| 35 |
[0x88] = "COMENT", |
| 36 |
[0x8a] = "MODEND16", |
| 37 |
[0x8b] = "MODEND32", |
| 38 |
[0x8c] = "EXTDEF", |
| 39 |
[0x8e] = "TYPDEF", |
| 40 |
[0x90] = "PUBDEF16", |
| 41 |
[0x91] = "PUBDEF32", |
| 42 |
[0x94] = "LINNUM16", |
| 43 |
[0x95] = "LINNUM32", |
| 44 |
[0x96] = "LNAMES", |
| 45 |
[0x98] = "SEGDEF16", |
| 46 |
[0x99] = "SEGDEF32", |
| 47 |
[0x9a] = "GRPDEF", |
| 48 |
[0x9c] = "FIXUPP16", |
| 49 |
[0x9d] = "FIXUPP32", |
| 50 |
[0xa0] = "LEDATA16", |
| 51 |
[0xa1] = "LEDATA32", |
| 52 |
[0xa2] = "LIDATA16", |
| 53 |
[0xa3] = "LIDATA32", |
| 54 |
[0xb0] = "COMDEF", |
| 55 |
[0xb2] = "BAKPAT16", |
| 56 |
[0xb3] = "BAKPAT32", |
| 57 |
[0xb4] = "LEXTDEF", |
| 58 |
[0xb6] = "LPUBDEF16", |
| 59 |
[0xb7] = "LPUBDEF32", |
| 60 |
[0xb8] = "LCOMDEF", |
| 61 |
[0xbc] = "CEXTDEF", |
| 62 |
[0xc2] = "COMDAT16", |
| 63 |
[0xc3] = "COMDAT32", |
| 64 |
[0xc4] = "LINSYM16", |
| 65 |
[0xc5] = "LINSYM32", |
| 66 |
[0xc6] = "ALIAS", |
| 67 |
[0xc8] = "NBKPAT16", |
| 68 |
[0xc9] = "NBKPAT32", |
| 69 |
[0xca] = "LLNAMES", |
| 70 |
[0xcc] = "VERNUM", |
| 71 |
[0xce] = "VENDEXT", |
| 72 |
[0xf0] = "LIBHDR", |
| 73 |
[0xf1] = "LIBEND", |
| 74 |
}; |
| 75 |
|
| 76 |
typedef void (*dump_func)(uint8_t, const uint8_t *, size_t); |
| 77 |
|
| 78 |
/* Ordered collection type */ |
| 79 |
struct collection { |
| 80 |
size_t n; /* Elements in collection (not including 0) */ |
| 81 |
size_t s; /* Elements allocated (not including 0) */ |
| 82 |
void const **p; /* Element pointers */ |
| 83 |
}; |
| 84 |
|
| 85 |
struct collection c_names, c_lsegs, c_groups, c_extsym; |
| 86 |
|
| 87 |
static void *nulldie(void *ptr) |
| 88 |
{ |
| 89 |
if (likely(ptr)) |
| 90 |
return ptr; |
| 91 |
|
| 92 |
fprintf(stderr, "%s: memory allocation error\n", progname); |
| 93 |
exit(1); |
| 94 |
abort(); /* This should never happen */ |
| 95 |
} |
| 96 |
|
| 97 |
static void *xmalloc(size_t size) |
| 98 |
{ |
| 99 |
return nulldie(malloc(size)); |
| 100 |
} |
| 101 |
|
| 102 |
static void *xrealloc(void *ptr, size_t size) |
| 103 |
{ |
| 104 |
return nulldie(realloc(ptr, size)); |
| 105 |
} |
| 106 |
|
| 107 |
/* Make sure there is enough space for at least one additional element */ |
| 108 |
static void *expand_buffer(void *buf, size_t *bufsizep, size_t datasize, |
| 109 |
size_t elemsize) |
| 110 |
{ |
| 111 |
size_t bufsize = *bufsizep; |
| 112 |
|
| 113 |
if (likely(bufsize < datasize)) |
| 114 |
return buf; |
| 115 |
|
| 116 |
if (bufsize < BUFSIZ) |
| 117 |
bufsize = BUFSIZ; |
| 118 |
else |
| 119 |
bufsize <<= 1; |
| 120 |
|
| 121 |
if (bufsize <= datasize) |
| 122 |
bufsize = datasize+1; |
| 123 |
|
| 124 |
buf = xrealloc(buf, bufsize * elemsize); |
| 125 |
*bufsizep = bufsize; |
| 126 |
|
| 127 |
return buf; |
| 128 |
} |
| 129 |
|
| 130 |
static void add_collection(struct collection *c, const void *p) |
| 131 |
{ |
| 132 |
c->p = expand_buffer(c->p, &c->s, c->n, sizeof(const void *)); |
| 133 |
c->p[c->n++] = p; |
| 134 |
} |
| 135 |
|
| 136 |
static const void *get_collection(struct collection *c, size_t index, |
| 137 |
const void *invalid) |
| 138 |
{ |
| 139 |
if (index == 0 || index > c->n) |
| 140 |
return invalid; |
| 141 |
|
| 142 |
return c->p[index - 1]; |
| 143 |
} |
| 144 |
|
| 145 |
static uint8_t *copy_name(size_t len, const uint8_t *data) |
| 146 |
{ |
| 147 |
uint8_t *r = xmalloc(len + 1); |
| 148 |
memcpy(r, data, len); |
| 149 |
r[len] = '\0'; |
| 150 |
return r; |
| 151 |
} |
| 152 |
|
| 153 |
static void hexdump_data(unsigned int offset, const uint8_t *data, |
| 154 |
size_t n, size_t field) |
| 155 |
{ |
| 156 |
unsigned int i, j; |
| 157 |
|
| 158 |
for (i = 0; i < n; i += 16) { |
| 159 |
printf(" %04x: ", i + offset); |
| 160 |
for (j = 0; j < 16; j++) { |
| 161 |
char sep = (j == 7) ? '-' : ' '; |
| 162 |
if (i + j < field) |
| 163 |
printf("%02x%c", data[i + j], sep); |
| 164 |
else if (i + j < n) |
| 165 |
printf("xx%c", sep); /* Beyond end of... */ |
| 166 |
else |
| 167 |
printf(" "); /* No separator */ |
| 168 |
} |
| 169 |
printf(" : "); |
| 170 |
for (j = 0; j < 16; j++) { |
| 171 |
if (i + j < n) |
| 172 |
putchar((i + j >= field) ? 'x' : |
| 173 |
isprint(data[i + j]) ? data[i + j] : '.'); |
| 174 |
} |
| 175 |
putchar('\n'); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
static void dump_unknown(uint8_t type, const uint8_t *data, size_t n) |
| 180 |
{ |
| 181 |
(void)type; |
| 182 |
hexdump_data(0, data, n, n); |
| 183 |
} |
| 184 |
|
| 185 |
static void print_dostime(const uint8_t *p) |
| 186 |
{ |
| 187 |
uint16_t da = (p[3] << 8) + p[2]; |
| 188 |
uint16_t ti = (p[1] << 8) + p[0]; |
| 189 |
|
| 190 |
printf("%04u-%02u-%02u %02u:%02u:%02u", |
| 191 |
(da >> 9) + 1980, (da >> 5) & 15, da & 31, |
| 192 |
(ti >> 11), (ti >> 5) & 63, (ti << 1) & 63); |
| 193 |
} |
| 194 |
|
| 195 |
static void dump_coment_depfile(uint8_t type, const uint8_t *data, size_t n) |
| 196 |
{ |
| 197 |
(void)type; |
| 198 |
|
| 199 |
if (n > 4 && data[4] == n - 5) { |
| 200 |
printf(" # "); |
| 201 |
print_dostime(data); |
| 202 |
printf(" %.*s\n", (int)(n - 5), data + 5); |
| 203 |
} |
| 204 |
|
| 205 |
hexdump_data(2, data, n, n); |
| 206 |
} |
| 207 |
|
| 208 |
static const dump_func dump_coment_class[256] = { |
| 209 |
[0xe9] = dump_coment_depfile |
| 210 |
}; |
| 211 |
|
| 212 |
static void dump_coment(uint8_t type, const uint8_t *data, size_t n) |
| 213 |
{ |
| 214 |
uint8_t class; |
| 215 |
static const char *coment_class[256] = { |
| 216 |
[0x00] = "Translator", |
| 217 |
[0x01] = "Copyright", |
| 218 |
[0x81] = "Library specifier", |
| 219 |
[0x9c] = "MS-DOS version", |
| 220 |
[0x9d] = "Memory model", |
| 221 |
[0x9e] = "DOSSEG", |
| 222 |
[0x9f] = "Library search", |
| 223 |
[0xa0] = "OMF extensions", |
| 224 |
[0xa1] = "New OMF extension", |
| 225 |
[0xa2] = "Link pass separator", |
| 226 |
[0xa3] = "LIBMOD", |
| 227 |
[0xa4] = "EXESTR", |
| 228 |
[0xa6] = "INCERR", |
| 229 |
[0xa7] = "NOPAD", |
| 230 |
[0xa8] = "WKEXT", |
| 231 |
[0xa9] = "LZEXT", |
| 232 |
[0xda] = "Comment", |
| 233 |
[0xdb] = "Compiler", |
| 234 |
[0xdc] = "Date", |
| 235 |
[0xdd] = "Timestamp", |
| 236 |
[0xdf] = "User", |
| 237 |
[0xe3] = "Type definition", |
| 238 |
[0xe8] = "Filename", |
| 239 |
[0xe9] = "Dependency file", |
| 240 |
[0xff] = "Command line" |
| 241 |
}; |
| 242 |
|
| 243 |
if (n < 2) { |
| 244 |
hexdump_data(type, data, 2, n); |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
type = data[0]; |
| 249 |
class = data[1]; |
| 250 |
|
| 251 |
printf(" [NP=%d NL=%d UD=%02X] %02X %s\n", |
| 252 |
(type >> 7) & 1, |
| 253 |
(type >> 6) & 1, |
| 254 |
type & 0x3f, |
| 255 |
class, coment_class[class] ? coment_class[class] : "???"); |
| 256 |
|
| 257 |
if (dump_coment_class[class]) |
| 258 |
dump_coment_class[class] (class, data + 2, n - 2); |
| 259 |
else |
| 260 |
hexdump_data(2, data + 2, n - 2, n - 2); |
| 261 |
} |
| 262 |
|
| 263 |
/* Parse an index field */ |
| 264 |
static uint16_t get_index(const uint8_t **pp) |
| 265 |
{ |
| 266 |
uint8_t c; |
| 267 |
|
| 268 |
c = *(*pp)++; |
| 269 |
if (c & 0x80) { |
| 270 |
return ((c & 0x7f) << 8) + *(*pp)++; |
| 271 |
} else { |
| 272 |
return c; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
static uint16_t get_16(const uint8_t **pp) |
| 277 |
{ |
| 278 |
uint16_t v = getu16(*pp); |
| 279 |
(*pp) += 2; |
| 280 |
|
| 281 |
return v; |
| 282 |
} |
| 283 |
|
| 284 |
static uint32_t get_32(const uint8_t **pp) |
| 285 |
{ |
| 286 |
const uint32_t v = getu32(*pp); |
| 287 |
(*pp) += 4; |
| 288 |
|
| 289 |
return v; |
| 290 |
} |
| 291 |
|
| 292 |
/* Returns a name as a constant, persistent C string */ |
| 293 |
static const char *lname(int index) |
| 294 |
{ |
| 295 |
return get_collection(&c_names, index, ""); |
| 296 |
} |
| 297 |
|
| 298 |
static const char *segment_name(uint16_t segment_idx) |
| 299 |
{ |
| 300 |
const segdef_t *seg; |
| 301 |
seg = get_collection(&c_lsegs, segment_idx, NULL); |
| 302 |
if (!seg) |
| 303 |
return ""; |
| 304 |
return lname(seg->name_idx); |
| 305 |
} |
| 306 |
|
| 307 |
static const char *group_name(uint16_t group_idx) |
| 308 |
{ |
| 309 |
const grpdef_t *grp; |
| 310 |
grp = get_collection(&c_groups, group_idx, NULL); |
| 311 |
if (!grp) |
| 312 |
return ""; |
| 313 |
return lname(grp->name_idx); |
| 314 |
} |
| 315 |
|
| 316 |
static const char *external_name(uint16_t idx) |
| 317 |
{ |
| 318 |
return get_collection(&c_extsym, idx, ""); |
| 319 |
} |
| 320 |
|
| 321 |
/* LNAMES or LLNAMES */ |
| 322 |
static void dump_lnames(uint8_t type, const uint8_t *data, size_t n) |
| 323 |
{ |
| 324 |
const uint8_t *p = data; |
| 325 |
const uint8_t *end = data + n; |
| 326 |
uint8_t *s; |
| 327 |
|
| 328 |
(void)type; |
| 329 |
|
| 330 |
while (p < end) { |
| 331 |
size_t l = *p; |
| 332 |
if (l > n) { |
| 333 |
s = copy_name(n, p+1); |
| 334 |
add_collection(&c_names, s); |
| 335 |
printf(" # %4zu 0x%04zx: \"%.*s... <%zu missing bytes>\n", |
| 336 |
c_names.n, c_names.n, (int)(n - 1), p + 1, l - n); |
| 337 |
} else { |
| 338 |
s = copy_name(l, p+1); |
| 339 |
add_collection(&c_names, s); |
| 340 |
printf(" [%04zx] '%s'\n", c_names.n, s); |
| 341 |
} |
| 342 |
hexdump_data(p - data, p, l + 1, n); |
| 343 |
p += l + 1; |
| 344 |
n -= l + 1; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
/* SEGDEF16 or SEGDEF32 */ |
| 349 |
static void dump_segdef(uint8_t type, const uint8_t *data, size_t n) |
| 350 |
{ |
| 351 |
bool big = type & 1; |
| 352 |
const uint8_t *p = data; |
| 353 |
const uint8_t *end = data + n; |
| 354 |
uint8_t attr; |
| 355 |
static const char *const alignment[8] = |
| 356 |
{ "ABS", "BYTE", "WORD", "PARA", "PAGE", "DWORD", "LTL", "?ALIGN" }; |
| 357 |
static const char *const combine[8] = |
| 358 |
{ "PRIVATE", "?COMMON", "PUBLIC", "?COMBINE", "?PUBLIC", "STACK", |
| 359 |
"COMMON", "?PUBLIC" }; |
| 360 |
const char *s; |
| 361 |
|
| 362 |
segdef_t *seg = xmalloc(sizeof(segdef_t)); |
| 363 |
|
| 364 |
if (p >= end) |
| 365 |
return; |
| 366 |
|
| 367 |
attr = *p++; |
| 368 |
|
| 369 |
printf(" %s (A%u) %s (C%u) %s%s", |
| 370 |
alignment[(attr >> 5) & 7], (attr >> 5) & 7, |
| 371 |
combine[(attr >> 2) & 7], (attr >> 2) & 7, |
| 372 |
(attr & 0x02) ? "MAXSIZE " : "", (attr & 0x01) ? "USE32" : "USE16"); |
| 373 |
|
| 374 |
if (((attr >> 5) & 7) == 0) { |
| 375 |
/* Absolute segment */ |
| 376 |
if (p + 3 > end) |
| 377 |
goto dump; |
| 378 |
printf(" AT %04x:", get_16(&p)); |
| 379 |
printf("%02x", *p++); |
| 380 |
} |
| 381 |
|
| 382 |
if (big) { |
| 383 |
if (p + 4 > end) |
| 384 |
goto dump; |
| 385 |
printf(" size %08x", get_32(&p)); |
| 386 |
} else { |
| 387 |
if (p + 2 > end) |
| 388 |
goto dump; |
| 389 |
printf(" size %04x", get_16(&p)); |
| 390 |
} |
| 391 |
puts(""); |
| 392 |
|
| 393 |
seg->name_idx = get_index(&p); |
| 394 |
if (p > end) |
| 395 |
goto dump; |
| 396 |
s = lname(seg->name_idx); |
| 397 |
printf(" name '%s'", s); |
| 398 |
|
| 399 |
seg->class_idx = get_index(&p); |
| 400 |
if (p > end) |
| 401 |
goto dump; |
| 402 |
s = lname(seg->class_idx); |
| 403 |
if (*s) { |
| 404 |
printf(", class '%s'", s); |
| 405 |
} |
| 406 |
|
| 407 |
seg->ovl_idx = get_index(&p); |
| 408 |
if (p > end) |
| 409 |
goto dump; |
| 410 |
s = lname(seg->ovl_idx); |
| 411 |
if (*s) { /* s points to empty string if no overlay is given */ |
| 412 |
printf(", ovl '%s'", s); |
| 413 |
} |
| 414 |
|
| 415 |
add_collection(&c_lsegs, seg); |
| 416 |
|
| 417 |
dump: |
| 418 |
putchar('\n'); |
| 419 |
hexdump_data(0, data, n, n); |
| 420 |
} |
| 421 |
|
| 422 |
static void dump_grpdef(uint8_t type, const uint8_t *data, size_t n) |
| 423 |
{ |
| 424 |
const uint8_t *p = data; |
| 425 |
const uint8_t *end = data + n; |
| 426 |
const char *s; |
| 427 |
uint16_t name_idx; |
| 428 |
grpdef_t *grp; |
| 429 |
|
| 430 |
(void)type; |
| 431 |
|
| 432 |
grp = xmalloc(sizeof(grpdef_t)); |
| 433 |
|
| 434 |
name_idx = get_index(&p); |
| 435 |
grp->name_idx = name_idx; |
| 436 |
|
| 437 |
if (p > end) |
| 438 |
goto dump; |
| 439 |
|
| 440 |
add_collection(&c_groups, grp); |
| 441 |
|
| 442 |
s = lname(name_idx); |
| 443 |
printf(" name '%s'\n", s); |
| 444 |
|
| 445 |
while (p < end) { |
| 446 |
if (*p == 0xff) { /* segment */ |
| 447 |
p++; |
| 448 |
printf(" segment '%s'\n", segment_name(get_index(&p))); |
| 449 |
} else if (*p == 0xfe) { |
| 450 |
p++; |
| 451 |
name_idx = get_index(&p); |
| 452 |
s = lname(name_idx); |
| 453 |
printf(" external '%s'\n", s); |
| 454 |
} else |
| 455 |
goto dump; |
| 456 |
} |
| 457 |
|
| 458 |
dump: |
| 459 |
hexdump_data(0, data, n, n); |
| 460 |
} |
| 461 |
|
| 462 |
static const char *const method_base[16] = { |
| 463 |
"SEGDEF", "GRPDEF", "EXTDEF", "frame#", |
| 464 |
"SEGDEF", "GRPDEF", "EXTDEF", "unsupported", |
| 465 |
"SEGDEF", "GRPDEF", "EXTDEF", "frame#", |
| 466 |
"prev. LEDATA SEG index", "TARGET index", "invalid", "unsupported" |
| 467 |
}; |
| 468 |
|
| 469 |
static const char *location_descr[16] = { |
| 470 |
"low-order byte", "16-bit offset", "16-bit segment", "32-bit far ptr", |
| 471 |
"hi-order byte", "16-bit ldr-resolved offset", "reserved", "reserved", |
| 472 |
"unknown", "32-bit offset", "unknown", "48-bit ptr", "unknown", |
| 473 |
"32-bit ldr-resolved offset", "unknown", "unknown" |
| 474 |
}; |
| 475 |
|
| 476 |
/* helper routine used in fixup and modend dumps */ |
| 477 |
static void dump_fixdat(bool big, const uint8_t **data) |
| 478 |
{ |
| 479 |
const uint8_t *p = *data; |
| 480 |
|
| 481 |
uint8_t fix; |
| 482 |
uint16_t idx; |
| 483 |
|
| 484 |
fix = *p++; |
| 485 |
|
| 486 |
/* frame decoding */ |
| 487 |
printf("\n frame %s%d%s", |
| 488 |
(fix & 0x80) ? "thread " : "method F", |
| 489 |
((fix & 0x70) >> 4), ((fix & 0xc0) == 0xc0) ? "?" : ""); |
| 490 |
if ((fix & 0x80) == 0) |
| 491 |
printf(" (%s)", method_base[((fix >> 4) & 7) + 8]); |
| 492 |
if ((fix & 0xc0) == 0) { |
| 493 |
idx = get_index(&p); |
| 494 |
printf(" index %04x", idx); |
| 495 |
if ((fix >> 4) == 0) { |
| 496 |
printf(" '%s'", segment_name(idx)); |
| 497 |
} else if ((fix >> 4) == 1) { |
| 498 |
printf(" '%s'", group_name(idx)); |
| 499 |
} else if ((fix >> 4) == 2) { |
| 500 |
printf(" '%s'", external_name(idx)); |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
/* target decoding */ |
| 505 |
printf("\n target %s%d", |
| 506 |
(fix & 0x08) ? "thread " : "method T", fix & 7); |
| 507 |
if ((fix & 0x08) == 0) |
| 508 |
printf(" (%s)", method_base[(fix & 7)]); |
| 509 |
if ((fix & 0x08) == 0) { |
| 510 |
idx = get_index(&p); |
| 511 |
printf(" index %04x", idx); |
| 512 |
if ((fix & 3) == 0) { |
| 513 |
printf(" '%s'", segment_name(idx)); |
| 514 |
} else if ((fix & 3) == 1) { |
| 515 |
printf(" '%s'", group_name(idx)); |
| 516 |
} else if ((fix & 3) == 2) { |
| 517 |
printf(" '%s'", external_name(idx)); |
| 518 |
} |
| 519 |
} |
| 520 |
if ((fix & 0x04) == 0) { |
| 521 |
if (big) { |
| 522 |
printf(" disp %08x", get_32(&p)); |
| 523 |
} else { |
| 524 |
printf(" disp %04x", get_16(&p)); |
| 525 |
} |
| 526 |
} |
| 527 |
putchar('\n'); |
| 528 |
|
| 529 |
*data = p; |
| 530 |
} |
| 531 |
|
| 532 |
/* FIXUPP16 or FIXUPP32 */ |
| 533 |
static void dump_fixupp(uint8_t type, const uint8_t *data, size_t n) |
| 534 |
{ |
| 535 |
bool big = type & 1; |
| 536 |
const uint8_t *p = data; |
| 537 |
const uint8_t *end = data + n; |
| 538 |
|
| 539 |
while (p < end) { |
| 540 |
const uint8_t *start = p; |
| 541 |
uint8_t op = *p++; |
| 542 |
|
| 543 |
if (!(op & 0x80)) { |
| 544 |
/* THREAD record */ |
| 545 |
bool frame = !!(op & 0x40); |
| 546 |
|
| 547 |
printf(" THREAD %-7s%d%s method %c%d (%s)", |
| 548 |
frame ? "frame" : "target", op & 3, |
| 549 |
(op & 0x20) ? " +flag5?" : "", |
| 550 |
(op & 0x40) ? 'F' : 'T', |
| 551 |
((op & 0x1c) >> 2), |
| 552 |
method_base[((op & 0x40) >> 3) | ((op & 0x1c) >> 2)]); |
| 553 |
|
| 554 |
if ((op & 0x50) != 0x50) { |
| 555 |
printf(" index %04x", get_index(&p)); |
| 556 |
} |
| 557 |
putchar('\n'); |
| 558 |
} else { |
| 559 |
/* FIXUP subrecord */ |
| 560 |
printf(" FIXUP %s-relative, type %d (%s)\n" |
| 561 |
" record offset %04x", |
| 562 |
(op & 0x40) ? "segment" : "self", |
| 563 |
(op & 0x3c) >> 2, |
| 564 |
location_descr[(op & 0x3c) >> 2], ((op & 3) << 8) + *p++); |
| 565 |
|
| 566 |
dump_fixdat(big, &p); |
| 567 |
} |
| 568 |
hexdump_data(start - data, start, p - start, n - (start - data)); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
static void dump_ledata(uint8_t type, const uint8_t *data, size_t n) |
| 573 |
{ |
| 574 |
bool big = type & 1; |
| 575 |
const uint8_t *p = data; |
| 576 |
const segdef_t *seg = get_collection(&c_lsegs, get_index(&p), NULL); |
| 577 |
const char *seg_name = "(null)"; |
| 578 |
size_t data_len; |
| 579 |
|
| 580 |
if (seg != NULL) |
| 581 |
seg_name = lname(seg->name_idx); |
| 582 |
|
| 583 |
printf(" segment '%s', ", seg_name); |
| 584 |
|
| 585 |
if (big) { |
| 586 |
printf("offset %08x\n", get_32(&p)); |
| 587 |
} else { |
| 588 |
printf("offset %04x\n", get_16(&p)); |
| 589 |
} |
| 590 |
|
| 591 |
data_len = n - (p - data); |
| 592 |
hexdump_data(0, p, data_len, data_len); |
| 593 |
} |
| 594 |
|
| 595 |
static void dump_pubdef(uint8_t type, const uint8_t *data, size_t n) |
| 596 |
{ |
| 597 |
static int pub_names_count; |
| 598 |
bool big = type & 1; |
| 599 |
const uint8_t *p = data; |
| 600 |
const uint8_t *end = data + n; |
| 601 |
uint16_t grp_idx; |
| 602 |
uint16_t seg_idx; |
| 603 |
uint16_t type_idx; |
| 604 |
int name_len; |
| 605 |
|
| 606 |
grp_idx = get_index(&p); |
| 607 |
if (p > end) |
| 608 |
goto dump; |
| 609 |
|
| 610 |
seg_idx = get_index(&p); |
| 611 |
if (p > end) |
| 612 |
goto dump; |
| 613 |
|
| 614 |
if (grp_idx == 0 && seg_idx == 0) { |
| 615 |
printf(" frame %04x", get_16(&p)); |
| 616 |
} else { |
| 617 |
printf(" "); |
| 618 |
if (seg_idx) { |
| 619 |
printf(" segment '%s'", segment_name(seg_idx)); |
| 620 |
} |
| 621 |
if (grp_idx) { |
| 622 |
printf(" group '%s'", group_name(grp_idx)); |
| 623 |
} |
| 624 |
} |
| 625 |
puts(""); |
| 626 |
|
| 627 |
while (p < end) { |
| 628 |
pub_names_count++; |
| 629 |
|
| 630 |
name_len = *p++; |
| 631 |
printf(" [%04x] public name '%.*s' ", pub_names_count, name_len, p); |
| 632 |
p += name_len; |
| 633 |
|
| 634 |
if (big) { |
| 635 |
printf("offset %08x", get_32(&p)); |
| 636 |
} else { |
| 637 |
printf("offset %04x", get_16(&p)); |
| 638 |
} |
| 639 |
type_idx = get_index(&p); |
| 640 |
if (type_idx) { |
| 641 |
printf(", type %d", type_idx); |
| 642 |
} |
| 643 |
puts(""); |
| 644 |
} |
| 645 |
|
| 646 |
dump: |
| 647 |
hexdump_data(0, data, n, n); |
| 648 |
} |
| 649 |
|
| 650 |
static void dump_extdef(uint8_t type, const uint8_t *data, size_t n) |
| 651 |
{ |
| 652 |
static int ext_names_count; |
| 653 |
const uint8_t *p = data; |
| 654 |
const uint8_t *end = data + n; |
| 655 |
uint16_t type_idx; |
| 656 |
int name_len; |
| 657 |
uint8_t *name; |
| 658 |
|
| 659 |
(void)type; |
| 660 |
|
| 661 |
while (p < end) { |
| 662 |
ext_names_count++; |
| 663 |
name_len = *p++; |
| 664 |
name = copy_name(name_len, p); |
| 665 |
add_collection(&c_extsym, name); |
| 666 |
p += name_len; |
| 667 |
printf(" [%04x] external name '%s'", ext_names_count, name); |
| 668 |
type_idx = get_index(&p); |
| 669 |
if (type_idx) { |
| 670 |
printf(", type %d", type_idx); |
| 671 |
} |
| 672 |
puts(""); |
| 673 |
} |
| 674 |
|
| 675 |
hexdump_data(0, data, n, n); |
| 676 |
} |
| 677 |
|
| 678 |
static void dump_modend(uint8_t type, const uint8_t *data, size_t n) |
| 679 |
{ |
| 680 |
const uint8_t *p = data; |
| 681 |
|
| 682 |
if (*p & 0x80) |
| 683 |
printf(" main module\n"); |
| 684 |
if (*p & 0x40) { |
| 685 |
printf(" start address:"); |
| 686 |
p++; |
| 687 |
dump_fixdat(type & 1, &p); |
| 688 |
} |
| 689 |
hexdump_data(0, data, n, n); |
| 690 |
} |
| 691 |
|
| 692 |
static const dump_func dump_type[256] = { |
| 693 |
[0x88] = dump_coment, |
| 694 |
[0x8a] = dump_modend, |
| 695 |
[0x8c] = dump_extdef, |
| 696 |
[0x90] = dump_pubdef, |
| 697 |
[0x91] = dump_pubdef, |
| 698 |
[0x96] = dump_lnames, |
| 699 |
[0x98] = dump_segdef, |
| 700 |
[0x99] = dump_segdef, |
| 701 |
[0x9a] = dump_grpdef, |
| 702 |
[0x9c] = dump_fixupp, |
| 703 |
[0x9d] = dump_fixupp, |
| 704 |
[0xa0] = dump_ledata, |
| 705 |
[0xa1] = dump_ledata, |
| 706 |
[0xca] = dump_lnames, |
| 707 |
}; |
| 708 |
|
| 709 |
static uint8_t *read_file(FILE *f, size_t *sz) |
| 710 |
{ |
| 711 |
uint8_t *buf = NULL; |
| 712 |
size_t bufsize = 0; |
| 713 |
size_t bytes; |
| 714 |
size_t size = 0; |
| 715 |
|
| 716 |
if (fseek(f, 0, SEEK_END)) { |
| 717 |
/* Try to get the size of the file to preallocate buffer */ |
| 718 |
bufsize = ftell(f); |
| 719 |
if (bufsize) |
| 720 |
buf = xmalloc(bufsize); |
| 721 |
} |
| 722 |
rewind(f); |
| 723 |
|
| 724 |
do { |
| 725 |
buf = expand_buffer(buf, &bufsize, size, 1); |
| 726 |
bytes = fread(buf + size, 1, bufsize - size, f); |
| 727 |
if (ferror(f)) { |
| 728 |
free(buf); |
| 729 |
*sz = 0; |
| 730 |
return NULL; |
| 731 |
} |
| 732 |
|
| 733 |
size += bytes; |
| 734 |
} while (bytes && !feof(f)); |
| 735 |
|
| 736 |
*sz = size; |
| 737 |
if (size != bufsize) |
| 738 |
buf = xrealloc(buf, size); |
| 739 |
|
| 740 |
return buf; |
| 741 |
} |
| 742 |
|
| 743 |
static int dump_omf(FILE *f) |
| 744 |
{ |
| 745 |
size_t len, n; |
| 746 |
uint8_t type; |
| 747 |
const uint8_t *p, *data; |
| 748 |
|
| 749 |
data = read_file(f, &len); |
| 750 |
if (!data) |
| 751 |
return -1; |
| 752 |
|
| 753 |
p = data; |
| 754 |
while (len >= 3 && *p != 0x1a) { |
| 755 |
uint8_t csum; |
| 756 |
int i; |
| 757 |
|
| 758 |
type = p[0]; |
| 759 |
n = *(uint16_t *) (p + 1); |
| 760 |
|
| 761 |
printf("%02x %-10s %4zd bytes", |
| 762 |
type, record_types[type] ? record_types[type] : "???", n); |
| 763 |
|
| 764 |
if (len < n + 3) { |
| 765 |
printf("\n (truncated, only %zd bytes left)\n", len - 3); |
| 766 |
break; /* Truncated */ |
| 767 |
} |
| 768 |
|
| 769 |
p += 3; /* Header doesn't count in the length */ |
| 770 |
n--; /* Remove checksum byte */ |
| 771 |
|
| 772 |
csum = 0; |
| 773 |
for (i = -3; i < (int)n; i++) |
| 774 |
csum -= p[i]; |
| 775 |
|
| 776 |
printf(", checksum %02X", p[i]); |
| 777 |
if (csum == p[i]) |
| 778 |
printf(" (valid)\n"); |
| 779 |
else |
| 780 |
printf(" (actual = %02X)\n", csum); |
| 781 |
|
| 782 |
if (dump_type[type]) |
| 783 |
dump_type[type] (type, p, n); |
| 784 |
else |
| 785 |
dump_unknown(type, p, n); |
| 786 |
|
| 787 |
p += n + 1; |
| 788 |
len -= (n + 4); |
| 789 |
} |
| 790 |
|
| 791 |
free((void *)data); |
| 792 |
return 0; |
| 793 |
} |
| 794 |
|
| 795 |
int main(int argc, char *argv[]) |
| 796 |
{ |
| 797 |
FILE *f; |
| 798 |
int i; |
| 799 |
|
| 800 |
progname = argv[0]; |
| 801 |
|
| 802 |
if (argc < 2) { |
| 803 |
fputs("omfdump - dump contents of object module files to stdout\n" |
| 804 |
"Usage: omfdump file...\n", stdout); |
| 805 |
return 0; |
| 806 |
} |
| 807 |
|
| 808 |
for (i = 1; i < argc; i++) { |
| 809 |
f = fopen(argv[i], "rb"); |
| 810 |
if (!f || dump_omf(f)) { |
| 811 |
perror(argv[i]); |
| 812 |
return 1; |
| 813 |
} |
| 814 |
fclose(f); |
| 815 |
} |
| 816 |
|
| 817 |
return 0; |
| 818 |
} |