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