1 /* 2 * aout2hux - convert a.out/ELF executable to Human68k .x format 3 * 4 * Read two a.out/ELF format executables with different load addresses 5 * and generate Human68k .x format executable. 6 * 7 * written by ITOH Yasufumi 8 * public domain 9 * 10 * usage: 11 * aout2hux [ -o output.x ] a.out1 loadaddr1 a.out2 loadaddr2 12 * 13 * The input files must be static OMAGIC/NMAGIC m68k a.out executables 14 * or m68k ELF executables. 15 * Two executables must have different loading addresses. 16 * Each of the load address must be a hexadecimal number. 17 * Load address shall be multiple of 4 for as / ld of NetBSD/m68k. 18 * 19 * example: 20 * % cc -N -static -Wl,-Ttext,0 -o aout1 *.o 21 * % cc -N -static -Wl,-Ttext,10203040 -o aout2 *.o 22 * % aout2hux -o foo.x aout1 0 aout2 10203040 23 * 24 * $NetBSD: aout2hux.c,v 1.14 2024/01/07 07:58:33 isaki Exp $ 25 */ 26 27 #include <sys/types.h> 28 #ifndef NO_UNISTD 29 # include <unistd.h> 30 #endif 31 #ifndef NO_STDLIB 32 # include <stdlib.h> 33 #endif 34 #include <stdio.h> 35 #include <string.h> 36 37 #include "type_local.h" 38 #include "aout68k.h" 39 #include "hux.h" 40 41 /* fseek() offset type */ 42 typedef long foff_t; 43 44 #ifndef DEFAULT_OUTPUT_FILE 45 # define DEFAULT_OUTPUT_FILE "out.x" 46 #endif 47 48 #ifdef DEBUG 49 # define DPRINTF(x) printf x 50 #else 51 # define DPRINTF(x) 52 #endif 53 54 struct exec_info { 55 foff_t text_off; /* file offset of text section */ 56 foff_t data_off; /* file offset of data section */ 57 u_int32_t text_size; /* size of text section */ 58 u_int32_t text_pad; /* pad between text and data */ 59 u_int32_t data_size; /* size of data section */ 60 u_int32_t bss_size; /* size of bss */ 61 u_int32_t entry_addr; /* entry point address */ 62 }; 63 64 unsigned get_uint16(be_uint16_t *be); 65 u_int32_t get_uint32(be_uint32_t *be); 66 void put_uint16(be_uint16_t *be, unsigned v); 67 void put_uint32(be_uint32_t *be, u_int32_t v); 68 void *do_realloc(void *p, size_t s); 69 70 static int open_aout(const char *fn, struct aout_m68k *hdr, 71 struct exec_info *inf); 72 static int open_elf(const char *fn, FILE *fp, struct elf_m68k_hdr *hdr, 73 struct exec_info *inf); 74 FILE *open_exec(const char *fn, struct exec_info *inf); 75 int check_2_exec_inf(struct exec_info *inf1, struct exec_info *inf2); 76 int aout2hux(const char *fn1, const char *fn2, 77 u_int32_t loadadr1, u_int32_t loadadr2, const char *fnx); 78 int gethex(u_int32_t *pval, const char *str); 79 void usage(const char *name); 80 81 #if !defined(bzero) && defined(__SVR4) 82 # define bzero(d, n) memset((d), 0, (n)) 83 #endif 84 85 /* 86 * read/write big-endian integer 87 */ 88 89 unsigned 90 get_uint16(be_uint16_t *be) 91 { 92 93 return be->val[0] << 8 | be->val[1]; 94 } 95 96 u_int32_t 97 get_uint32(be_uint32_t *be) 98 { 99 100 return be->val[0]<<24 | be->val[1]<<16 | be->val[2]<<8 | be->val[3]; 101 } 102 103 void 104 put_uint16(be_uint16_t *be, unsigned v) 105 { 106 107 be->val[0] = (u_int8_t) (v >> 8); 108 be->val[1] = (u_int8_t) v; 109 } 110 111 void 112 put_uint32(be_uint32_t *be, u_int32_t v) 113 { 114 115 be->val[0] = (u_int8_t) (v >> 24); 116 be->val[1] = (u_int8_t) (v >> 16); 117 be->val[2] = (u_int8_t) (v >> 8); 118 be->val[3] = (u_int8_t) v; 119 } 120 121 void * 122 do_realloc(void *p, size_t s) 123 { 124 125 p = p ? realloc(p, s) : malloc(s); /* for portability */ 126 127 if (!p) { 128 fprintf(stderr, "malloc failed\n"); 129 exit(1); 130 } 131 132 return p; 133 } 134 135 /* 136 * check a.out header 137 */ 138 static int 139 open_aout(const char *fn, struct aout_m68k *hdr, struct exec_info *inf) 140 { 141 int i; 142 143 DPRINTF(("%s: is an a.out\n", fn)); 144 145 if ((i = AOUT_GET_MID(hdr)) != AOUT_MID_M68K && i != AOUT_MID_M68K4K) { 146 fprintf(stderr, "%s: wrong architecture (mid %d)\n", fn, i); 147 return 1; 148 } 149 150 /* if unsolved relocations exist, not an executable but an object */ 151 if (hdr->a_trsize.hostval || hdr->a_drsize.hostval) { 152 fprintf(stderr, "%s: not an executable (object file?)\n", fn); 153 return 1; 154 } 155 156 if (AOUT_GET_FLAGS(hdr) & (AOUT_FLAG_PIC | AOUT_FLAG_DYNAMIC)) { 157 fprintf(stderr, "%s: PIC and DYNAMIC are not supported\n", fn); 158 return 1; 159 } 160 161 inf->text_size = get_uint32(&hdr->a_text); 162 inf->data_size = get_uint32(&hdr->a_data); 163 inf->bss_size = get_uint32(&hdr->a_bss); 164 inf->entry_addr = get_uint32(&hdr->a_entry); 165 inf->text_off = sizeof(struct aout_m68k); 166 inf->data_off = sizeof(struct aout_m68k) + inf->text_size; 167 inf->text_pad = -inf->text_size & (AOUT_PAGESIZE(hdr) - 1); 168 169 return 0; 170 } 171 172 /* 173 * digest ELF structure 174 */ 175 static int 176 open_elf(const char *fn, FILE *fp, struct elf_m68k_hdr *hdr, struct exec_info *inf) 177 { 178 int i; 179 size_t nphdr; 180 struct elf_m68k_phdr phdr[2]; 181 182 DPRINTF(("%s: is an ELF\n", fn)); 183 184 if (hdr->e_ident[EI_VERSION] != EV_CURRENT || 185 get_uint32(&hdr->e_version) != EV_CURRENT) { 186 fprintf(stderr, "%s: unknown ELF version\n", fn); 187 return 1; 188 } 189 190 if (get_uint16(&hdr->e_type) != ET_EXEC) { 191 fprintf(stderr, "%s: not an executable\n", fn); 192 return 1; 193 } 194 195 if ((i = get_uint16(&hdr->e_machine)) != EM_68K) { 196 fprintf(stderr, "%s: wrong architecture (%d)\n", fn, i); 197 return 1; 198 } 199 200 if ((i = get_uint16(&hdr->e_shentsize)) != SIZE_ELF68K_SHDR) { 201 fprintf(stderr, "%s: size shdr %d should be %d\n", fn, i, 202 (int)SIZE_ELF68K_SHDR); 203 return 1; 204 } 205 206 if ((i = get_uint16(&hdr->e_phentsize)) != SIZE_ELF68K_PHDR) { 207 fprintf(stderr, "%s: size phdr %d should be %d\n", fn, i, 208 (int)SIZE_ELF68K_PHDR); 209 return 1; 210 } 211 212 if ((nphdr = get_uint16(&hdr->e_phnum)) != 1 && nphdr != 2) { 213 fprintf(stderr, 214 "%s: has %lu loadable segments (should be 1 or 2)\n", 215 fn, (unsigned long)nphdr); 216 return 1; 217 } 218 219 /* Read ELF program header table. */ 220 if (fseek(fp, (foff_t) get_uint32(&hdr->e_phoff), SEEK_SET)) { 221 perror(fn); 222 return 1; 223 } 224 if (fread(phdr, sizeof phdr[0], nphdr, fp) != nphdr) { 225 fprintf(stderr, "%s: can't read ELF program header\n", fn); 226 return 1; 227 } 228 229 /* Just error checking. */ 230 for (i = 0; i < (int) nphdr; i++) { 231 if (get_uint32(&phdr[i].p_type) != PT_LOAD) { 232 fprintf(stderr, 233 "%s: program header #%d is not loadable\n", 234 fn, i); 235 return 1; 236 } 237 } 238 239 if (nphdr == 1 && (get_uint32(&phdr[0].p_flags) & PF_W)) { 240 /* 241 * Only one writable section --- probably "ld -N" executable. 242 * Find out the start of data segment. 243 */ 244 struct elf_m68k_shdr shdr; 245 int nshdr; 246 247 nshdr = get_uint16(&hdr->e_shnum); 248 249 /* section #0 always exists and reserved --- skip */ 250 if (nshdr > 1 && 251 fseek(fp, 252 (foff_t) (get_uint32(&hdr->e_shoff) + sizeof shdr), 253 SEEK_SET)) { 254 perror(fn); 255 return 1; 256 } 257 for (i = 1; i < nshdr; i++) { 258 if (fread(&shdr, sizeof shdr, 1, fp) != 1) { 259 fprintf(stderr, 260 "%s: can't read ELF section header\n", 261 fn); 262 return 1; 263 } 264 265 DPRINTF(("%s: section header #%d: flags 0x%x\n", 266 fn, i, get_uint32(&shdr.sh_flags))); 267 268 if (ELF68K_ISDATASEG(&shdr)) { 269 /* 270 * data section is found. 271 */ 272 DPRINTF(("%s: one section, data found\n", fn)); 273 inf->text_off = get_uint32(&phdr[0].p_offset); 274 inf->text_size = get_uint32(&shdr.sh_offset) - 275 inf->text_off; 276 inf->text_pad = 0; 277 inf->data_off = inf->text_off + inf->text_size; 278 inf->data_size = get_uint32(&phdr[0].p_filesz) - 279 inf->text_size; 280 inf->bss_size = get_uint32(&phdr[0].p_memsz) - 281 get_uint32(&phdr[0].p_filesz); 282 inf->entry_addr = get_uint32(&hdr->e_entry); 283 goto data_found; 284 } 285 } 286 /* 287 * No data section found --- probably text + bss. 288 */ 289 DPRINTF(("%s: one section, no data section\n", fn)); 290 inf->text_size = get_uint32(&phdr[0].p_filesz); 291 inf->data_size = 0; 292 inf->bss_size = get_uint32(&phdr[0].p_memsz) - inf->text_size; 293 inf->entry_addr = get_uint32(&hdr->e_entry); 294 inf->text_off = get_uint32(&phdr[0].p_offset); 295 inf->data_off = 0; 296 inf->text_pad = 0; 297 data_found:; 298 } else if (nphdr == 1) { 299 /* 300 * Only one non-writable section --- pure text program? 301 */ 302 DPRINTF(("%s: one RO section\n", fn)); 303 inf->text_size = get_uint32(&phdr[0].p_filesz); 304 inf->data_size = 0; 305 inf->bss_size = 0; 306 inf->entry_addr = get_uint32(&hdr->e_entry); 307 inf->text_off = get_uint32(&phdr[0].p_offset); 308 inf->data_off = 0; 309 inf->text_pad = get_uint32(&phdr[0].p_memsz) - inf->text_size; 310 } else { 311 /* 312 * two sections 313 * text + data assumed. 314 */ 315 int t = 0, d = 1, tmp; /* first guess */ 316 #define SWAP_T_D tmp = t, t = d, d = tmp 317 318 DPRINTF(("%s: two sections\n", fn)); 319 320 /* Find out text and data. */ 321 if (get_uint32(&phdr[t].p_vaddr) > get_uint32(&phdr[d].p_vaddr)) 322 SWAP_T_D; 323 324 if ((get_uint32(&phdr[t].p_flags) & PF_X) == 0 && 325 get_uint32(&phdr[d].p_flags) & PF_X) 326 SWAP_T_D; 327 328 if ((get_uint32(&phdr[d].p_flags) & PF_W) == 0 && 329 get_uint32(&phdr[t].p_flags) & PF_W) 330 SWAP_T_D; 331 #undef SWAP_T_D 332 333 /* Are the text/data sections correctly detected? */ 334 if (get_uint32(&phdr[t].p_vaddr) > 335 get_uint32(&phdr[d].p_vaddr)) { 336 fprintf(stderr, "%s: program sections not in order\n", 337 fn); 338 return 1; 339 } 340 341 if ((get_uint32(&phdr[t].p_flags) & PF_X) == 0) 342 fprintf(stderr, "%s: warning: text is not executable\n", 343 fn); 344 345 if ((get_uint32(&phdr[d].p_flags) & PF_W) == 0) 346 fprintf(stderr, "%s: warning: data is not writable\n", 347 fn); 348 349 inf->text_size = get_uint32(&phdr[t].p_filesz); 350 inf->data_size = get_uint32(&phdr[d].p_filesz); 351 inf->bss_size = get_uint32(&phdr[d].p_memsz) - inf->data_size; 352 inf->entry_addr = get_uint32(&hdr->e_entry); 353 inf->text_off = get_uint32(&phdr[t].p_offset); 354 inf->data_off = get_uint32(&phdr[d].p_offset); 355 inf->text_pad = get_uint32(&phdr[d].p_vaddr) - 356 (get_uint32(&phdr[t].p_vaddr) + inf->text_size); 357 } 358 359 return 0; 360 } 361 362 /* 363 * open an executable 364 */ 365 FILE * 366 open_exec(const char *fn, struct exec_info *inf) 367 { 368 FILE *fp; 369 int i; 370 union { 371 struct aout_m68k u_aout; 372 struct elf_m68k_hdr u_elf; 373 } buf; 374 #define hdra (&buf.u_aout) 375 #define hdre (&buf.u_elf) 376 377 if (!(fp = fopen(fn, "r"))) { 378 perror(fn); 379 return (FILE *) NULL; 380 } 381 382 /* 383 * Check for a.out. 384 */ 385 386 if (fread(hdra, sizeof(struct aout_m68k), 1, fp) != 1) { 387 fprintf(stderr, "%s: can't read a.out header\n", fn); 388 goto out; 389 } 390 391 if ((i = AOUT_GET_MAGIC(hdra)) != AOUT_OMAGIC && i != AOUT_NMAGIC) 392 goto notaout; 393 394 if (open_aout(fn, hdra, inf)) 395 goto out; 396 397 /* OK! */ 398 return fp; 399 400 notaout: 401 /* 402 * Check for ELF. 403 */ 404 405 if (hdre->e_ident[EI_MAG0] != ELFMAG0 || 406 hdre->e_ident[EI_MAG1] != ELFMAG1 || 407 hdre->e_ident[EI_MAG2] != ELFMAG2 || 408 hdre->e_ident[EI_MAG3] != ELFMAG3 || 409 hdre->e_ident[EI_CLASS] != ELFCLASS32 || 410 hdre->e_ident[EI_DATA] != ELFDATA2MSB) { 411 fprintf(stderr, 412 "%s: not an OMAGIC or NMAGIC a.out, or a 32bit BE ELF\n", 413 fn); 414 goto out; 415 } 416 417 /* ELF header is longer than a.out header. Read the rest. */ 418 if (fread(hdra + 1, 419 sizeof(struct elf_m68k_hdr) - sizeof(struct aout_m68k), 420 1, fp) != 1) { 421 fprintf(stderr, "%s: can't read ELF header\n", fn); 422 goto out; 423 } 424 425 if (open_elf(fn, fp, hdre, inf)) 426 goto out; 427 428 /* OK! */ 429 return fp; 430 431 out: fclose(fp); 432 return (FILE *) NULL; 433 #undef hdra 434 #undef hdre 435 } 436 437 /* 438 * compare two executables and check if they are compatible 439 */ 440 int 441 check_2_exec_inf(struct exec_info *inf1, struct exec_info *inf2) 442 { 443 444 if (inf1->text_size != inf2->text_size || 445 inf1->text_pad != inf2->text_pad || 446 inf1->data_size != inf2->data_size || 447 inf1->bss_size != inf2->bss_size) 448 return -1; 449 450 return 0; 451 } 452 453 /* allocation unit (in bytes) of relocation table */ 454 #define RELTBL_CHUNK 8192 455 456 /* 457 * add an entry to the relocation table 458 */ 459 #define ADD_RELTBL(adr) \ 460 if (relsize + sizeof(struct relinf_l) > relallocsize) \ 461 reltbl = do_realloc(reltbl, relallocsize += RELTBL_CHUNK); \ 462 if ((adr) < reladdr + HUX_MINLREL) { \ 463 struct relinf_s *r = (struct relinf_s *)(reltbl + relsize); \ 464 put_uint16(&r->locoff_s, (unsigned)((adr) - reladdr)); \ 465 relsize += sizeof(struct relinf_s); \ 466 DPRINTF(("short")); \ 467 } else { \ 468 struct relinf_l *r = (struct relinf_l *)(reltbl + relsize); \ 469 put_uint16(&r->lrelmag, HUXLRELMAGIC); \ 470 put_uint32((be_uint32_t *)r->locoff_l, (adr) - reladdr); \ 471 relsize += sizeof(struct relinf_l); \ 472 DPRINTF(("long ")); \ 473 } \ 474 DPRINTF((" reloc 0x%06x", (adr))); \ 475 reladdr = (adr); 476 477 #define ERR1 { if (ferror(fpa1)) perror(fn1); \ 478 else fprintf(stderr, "%s: unexpected EOF\n", fn1); \ 479 goto out; } 480 #define ERR2 { if (ferror(fpa2)) perror(fn2); \ 481 else fprintf(stderr, "%s: unexpected EOF\n", fn2); \ 482 goto out; } 483 #define ERRC { fprintf(stderr, "files %s and %s are inconsistent\n", \ 484 fn1, fn2); \ 485 goto out; } 486 487 /* 488 * read input executables and output .x body 489 * and create relocation table 490 */ 491 #define CREATE_RELOCATION(segsize) \ 492 while (segsize > 0 || nbuf) { \ 493 if (nbuf == 0) { \ 494 if (fread(&b1.half[0], SIZE_16, 1, fpa1) != 1) \ 495 ERR1 \ 496 if (fread(&b2.half[0], SIZE_16, 1, fpa2) != 1) \ 497 ERR2 \ 498 nbuf = 1; \ 499 segsize -= SIZE_16; \ 500 } else if (nbuf == 1) { \ 501 if (segsize == 0) { \ 502 if (b1.half[0].hostval != b2.half[0].hostval) \ 503 ERRC \ 504 fwrite(&b1.half[0], SIZE_16, 1, fpx); \ 505 nbuf = 0; \ 506 addr += SIZE_16; \ 507 } else { \ 508 if (fread(&b1.half[1], SIZE_16, 1, fpa1) != 1)\ 509 ERR1 \ 510 if (fread(&b2.half[1], SIZE_16, 1, fpa2) != 1)\ 511 ERR2 \ 512 nbuf = 2; \ 513 segsize -= SIZE_16; \ 514 } \ 515 } else /* if (nbuf == 2) */ { \ 516 if (b1.hostval != b2.hostval && \ 517 get_uint32(&b1) - loadadr1 \ 518 == get_uint32(&b2) - loadadr2) {\ 519 /* do relocation */ \ 520 ADD_RELTBL(addr) \ 521 \ 522 put_uint32(&b1, get_uint32(&b1) - loadadr1); \ 523 DPRINTF((" v 0x%08x\t", get_uint32(&b1))); \ 524 fwrite(&b1, SIZE_32, 1, fpx); \ 525 nbuf = 0; \ 526 addr += SIZE_32; \ 527 } else if (b1.half[0].hostval == b2.half[0].hostval) {\ 528 fwrite(&b1.half[0], SIZE_16, 1, fpx); \ 529 addr += SIZE_16; \ 530 b1.half[0] = b1.half[1]; \ 531 b2.half[0] = b2.half[1]; \ 532 nbuf = 1; \ 533 } else \ 534 ERRC \ 535 } \ 536 } 537 538 int 539 aout2hux(const char *fn1, const char *fn2, u_int32_t loadadr1, u_int32_t loadadr2, const char *fnx) 540 { 541 int status = 1; /* the default is "failed" */ 542 FILE *fpa1 = NULL, *fpa2 = NULL; 543 struct exec_info inf1, inf2; 544 FILE *fpx = NULL; 545 struct huxhdr xhdr; 546 u_int32_t textsize, datasize, paddingsize, execoff; 547 548 /* for relocation */ 549 be_uint32_t b1, b2; 550 int nbuf; 551 u_int32_t addr; 552 553 /* for relocation table */ 554 size_t relsize, relallocsize; 555 u_int32_t reladdr; 556 char *reltbl = NULL; 557 558 559 /* 560 * check load addresses 561 */ 562 if (loadadr1 == loadadr2) { 563 fprintf(stderr, "two load addresses must be different\n"); 564 return 1; 565 } 566 567 /* 568 * open input executables and check them 569 */ 570 if (!(fpa1 = open_exec(fn1, &inf1)) || !(fpa2 = open_exec(fn2, &inf2))) 571 goto out; 572 573 /* 574 * check for consistency 575 */ 576 if (check_2_exec_inf(&inf1, &inf2)) { 577 fprintf(stderr, "files %s and %s are incompatible\n", 578 fn1, fn2); 579 goto out; 580 } 581 /* check entry address */ 582 if (inf1.entry_addr - loadadr1 != inf2.entry_addr - loadadr2) { 583 fprintf(stderr, "address of %s or %s may be incorrect\n", 584 fn1, fn2); 585 goto out; 586 } 587 588 /* 589 * get information of the executables 590 */ 591 textsize = inf1.text_size; 592 paddingsize = inf1.text_pad; 593 datasize = inf1.data_size; 594 execoff = inf1.entry_addr - loadadr1; 595 596 DPRINTF(("text: %u, data: %u, pad: %u, bss: %u, exec: %u\n", 597 textsize, datasize, paddingsize, inf1.bss_size, execoff)); 598 599 if (textsize & 1) { 600 fprintf(stderr, "text size is not even\n"); 601 goto out; 602 } 603 if (datasize & 1) { 604 fprintf(stderr, "data size is not even\n"); 605 goto out; 606 } 607 if (execoff >= textsize && 608 (execoff < textsize + paddingsize || 609 execoff >= textsize + paddingsize + datasize)) { 610 fprintf(stderr, "exec addr is not in text or data segment\n"); 611 goto out; 612 } 613 614 /* 615 * prepare for .x header 616 */ 617 memset((void *) &xhdr, 0, sizeof xhdr); 618 put_uint16(&xhdr.x_magic, HUXMAGIC); 619 put_uint32(&xhdr.x_entry, execoff); 620 put_uint32(&xhdr.x_text, textsize + paddingsize); 621 put_uint32(&xhdr.x_data, inf1.data_size); 622 put_uint32(&xhdr.x_bss, inf1.bss_size); 623 624 /* 625 * create output file 626 */ 627 if (!(fpx = fopen(fnx, "w")) || 628 fseek(fpx, (foff_t) sizeof xhdr, SEEK_SET)) { /* skip header */ 629 perror(fnx); 630 goto out; 631 } 632 633 addr = 0; 634 nbuf = 0; 635 636 relsize = relallocsize = 0; 637 reladdr = 0; 638 639 /* 640 * text segment 641 */ 642 if (fseek(fpa1, inf1.text_off, SEEK_SET)) { 643 perror(fn1); 644 goto out; 645 } 646 if (fseek(fpa2, inf2.text_off, SEEK_SET)) { 647 perror(fn2); 648 goto out; 649 } 650 CREATE_RELOCATION(textsize) 651 652 /* 653 * page boundary 654 */ 655 addr += paddingsize; 656 while (paddingsize--) 657 putc('\0', fpx); 658 659 /* 660 * data segment 661 */ 662 if (fseek(fpa1, inf1.data_off, SEEK_SET)) { 663 perror(fn1); 664 goto out; 665 } 666 if (fseek(fpa2, inf2.data_off, SEEK_SET)) { 667 perror(fn2); 668 goto out; 669 } 670 CREATE_RELOCATION(datasize) 671 672 /* 673 * error check of the above 674 */ 675 if (ferror(fpx)) { 676 fprintf(stderr, "%s: write failure\n", fnx); 677 goto out; 678 } 679 680 /* 681 * write relocation table 682 */ 683 if (relsize > 0) { 684 DPRINTF(("\n")); 685 if (fwrite(reltbl, 1, relsize, fpx) != relsize) { 686 perror(fnx); 687 goto out; 688 } 689 } 690 691 /* 692 * write .x header at the top of the output file 693 */ 694 put_uint32(&xhdr.x_rsize, relsize); 695 if (fseek(fpx, (foff_t) 0, SEEK_SET) || 696 fwrite(&xhdr, sizeof xhdr, 1, fpx) != 1) { 697 perror(fnx); 698 goto out; 699 } 700 701 status = 0; /* all OK */ 702 703 out: /* 704 * cleanup 705 */ 706 if (fpa1) 707 fclose(fpa1); 708 if (fpa2) 709 fclose(fpa2); 710 if (fpx) { 711 if (fclose(fpx) && status == 0) { 712 /* Alas, final flush failed! */ 713 perror(fnx); 714 status = 1; 715 } 716 if (status) 717 remove(fnx); 718 } 719 if (reltbl) 720 free(reltbl); 721 722 return status; 723 } 724 725 #ifndef NO_BIST 726 void bist(void); 727 728 /* 729 * built-in self test 730 */ 731 void 732 bist(void) 733 { 734 be_uint16_t be16; 735 be_uint32_t be32; 736 be_uint32_t be32x2[2]; 737 738 be16.val[0] = 0x12; be16.val[1] = 0x34; 739 be32.val[0] = 0xfe; be32.val[1] = 0xdc; 740 be32.val[2] = 0xba; be32.val[3] = 0x98; 741 742 put_uint16(&be32x2[0].half[1], 0x4567); 743 put_uint32(&be32x2[1], 0xa9876543); 744 745 if (sizeof(u_int8_t) != 1 || sizeof(u_int16_t) != 2 || 746 sizeof(u_int32_t) != 4 || 747 SIZE_16 != 2 || SIZE_32 != 4 || sizeof be32x2 != 8 || 748 sizeof(struct relinf_s) != 2 || sizeof(struct relinf_l) != 6 || 749 SIZE_ELF68K_HDR != 52 || SIZE_ELF68K_SHDR != 40 || 750 SIZE_ELF68K_PHDR != 32 || 751 get_uint16(&be16) != 0x1234 || get_uint32(&be32) != 0xfedcba98 || 752 get_uint16(&be32x2[0].half[1]) != 0x4567 || 753 get_uint32(&be32x2[1]) != 0xa9876543) { 754 fprintf(stderr, "BIST failed\n"); 755 exit(1); 756 } 757 } 758 #endif 759 760 int 761 gethex(u_int32_t *pval, const char *str) 762 { 763 const unsigned char *p = (const unsigned char *) str; 764 u_int32_t val; 765 int over; 766 767 /* skip leading "0x" if exists */ 768 if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) 769 p += 2; 770 771 if (!*p) 772 goto bad; 773 774 for (val = 0, over = 0; *p; p++) { 775 int digit; 776 777 switch (*p) { 778 case '0': case '1': case '2': case '3': case '4': 779 case '5': case '6': case '7': case '8': case '9': 780 digit = *p - '0'; 781 break; 782 case 'a': case 'A': digit = 10; break; 783 case 'b': case 'B': digit = 11; break; 784 case 'c': case 'C': digit = 12; break; 785 case 'd': case 'D': digit = 13; break; 786 case 'e': case 'E': digit = 14; break; 787 case 'f': case 'F': digit = 15; break; 788 default: 789 goto bad; 790 } 791 if (val >= 0x10000000) 792 over = 1; 793 val = (val << 4) | digit; 794 } 795 796 if (over) 797 fprintf(stderr, "warning: %s: constant overflow\n", str); 798 799 *pval = val; 800 801 DPRINTF(("gethex: %s -> 0x%x\n", str, val)); 802 803 return 0; 804 805 bad: 806 fprintf(stderr, "%s: not a hexadecimal number\n", str); 807 return 1; 808 } 809 810 void 811 usage(const char *name) 812 { 813 814 fprintf(stderr, "\ 815 usage: %s [ -o output.x ] a.out1 loadaddr1 a.out2 loadaddr2\n\n\ 816 The input files must be static OMAGIC/NMAGIC m68k a.out executables\n\ 817 or m68k ELF executables.\n\ 818 Two executables must have different loading addresses.\n\ 819 Each of the load address must be a hexadecimal number.\n\ 820 The default output filename is \"%s\".\n" ,name, DEFAULT_OUTPUT_FILE); 821 822 exit(1); 823 } 824 825 int 826 main(int argc, char *argv[]) 827 { 828 const char *outfile = DEFAULT_OUTPUT_FILE; 829 u_int32_t adr1, adr2; 830 831 #ifndef NO_BIST 832 bist(); 833 #endif 834 835 if (argc > 2 && argv[1][0] == '-' && argv[1][1] == 'o' && !argv[1][2]) { 836 outfile = argv[2]; 837 argv += 2; 838 argc -= 2; 839 } 840 841 if (argc != 5) 842 usage(argv[0]); 843 844 if (gethex(&adr1, argv[2]) || gethex(&adr2, argv[4])) 845 usage(argv[0]); 846 847 return aout2hux(argv[1], argv[3], adr1, adr2, outfile); 848 } 849