Home | History | Annotate | Line # | Download | only in aout2hux
aout2hux.c revision 1.10
      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.10 2009/03/18 16:00:16 cegger 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(struct exec_info *inf1, struct exec_info *inf2)
    443 {
    444 
    445 	if (inf1->text_size != inf2->text_size ||
    446 	    inf1->text_pad != inf2->text_pad ||
    447 	    inf1->data_size != inf2->data_size ||
    448 	    inf1->bss_size != inf2->bss_size)
    449 		return -1;
    450 
    451 	return 0;
    452 }
    453 
    454 /* allocation unit (in bytes) of relocation table */
    455 #define RELTBL_CHUNK	8192
    456 
    457 /*
    458  * add an entry to the relocation table
    459  */
    460 #define ADD_RELTBL(adr)	\
    461 	if (relsize + sizeof(struct relinf_l) > relallocsize)		    \
    462 		reltbl = do_realloc(reltbl, relallocsize += RELTBL_CHUNK);  \
    463 	if ((adr) < reladdr + HUX_MINLREL) {				    \
    464 		struct relinf_s *r = (struct relinf_s *)(reltbl + relsize); \
    465 		put_uint16(&r->locoff_s, (unsigned)((adr) - reladdr));	    \
    466 		relsize += sizeof(struct relinf_s);			    \
    467 		DPRINTF(("short"));					    \
    468 	} else {							    \
    469 		struct relinf_l *r = (struct relinf_l *)(reltbl + relsize); \
    470 		put_uint16(&r->lrelmag, HUXLRELMAGIC);			    \
    471 		put_uint32((be_uint32_t *)r->locoff_l, (adr) - reladdr);    \
    472 		relsize += sizeof(struct relinf_l);			    \
    473 		DPRINTF(("long "));					    \
    474 	}								    \
    475 	DPRINTF((" reloc 0x%06x", (adr)));				    \
    476 	reladdr = (adr);
    477 
    478 #define ERR1	{ if (ferror(fpa1)) perror(fn1);			\
    479 		  else fprintf(stderr, "%s: unexpected EOF\n", fn1);	\
    480 		  goto out; }
    481 #define ERR2	{ if (ferror(fpa2)) perror(fn2);			\
    482 		  else fprintf(stderr, "%s: unexpected EOF\n", fn2);	\
    483 		  goto out; }
    484 #define ERRC	{ fprintf(stderr, "files %s and %s are inconsistent\n",	\
    485 				  fn1, fn2);				\
    486 		  goto out; }
    487 
    488 /*
    489  * read input executables and output .x body
    490  * and create relocation table
    491  */
    492 #define CREATE_RELOCATION(segsize)	\
    493 	while (segsize > 0 || nbuf) {					\
    494 		if (nbuf == 0) {					\
    495 			if (fread(&b1.half[0], SIZE_16, 1, fpa1) != 1)	\
    496 				ERR1					\
    497 			if (fread(&b2.half[0], SIZE_16, 1, fpa2) != 1)	\
    498 				ERR2					\
    499 			nbuf = 1;					\
    500 			segsize -= SIZE_16;				\
    501 		} else if (nbuf == 1) {					\
    502 			if (segsize == 0) {				\
    503 				if (b1.half[0].hostval != b2.half[0].hostval) \
    504 					ERRC				\
    505 				fwrite(&b1.half[0], SIZE_16, 1, fpx);	\
    506 				nbuf = 0;				\
    507 				addr += SIZE_16;			\
    508 			} else {					\
    509 				if (fread(&b1.half[1], SIZE_16, 1, fpa1) != 1)\
    510 					ERR1				\
    511 				if (fread(&b2.half[1], SIZE_16, 1, fpa2) != 1)\
    512 					ERR2				\
    513 				nbuf = 2;				\
    514 				segsize -= SIZE_16;			\
    515 			}						\
    516 		} else /* if (nbuf == 2) */ {				\
    517 			if (b1.hostval != b2.hostval &&			\
    518 			    get_uint32(&b1) - loadadr1			\
    519 					== get_uint32(&b2) - loadadr2) {\
    520 				/* do relocation */			\
    521 				ADD_RELTBL(addr)			\
    522 									\
    523 				put_uint32(&b1, get_uint32(&b1) - loadadr1);  \
    524 				DPRINTF((" v 0x%08x\t", get_uint32(&b1)));    \
    525 				fwrite(&b1, SIZE_32, 1, fpx);		\
    526 				nbuf = 0;				\
    527 				addr += SIZE_32;			\
    528 			} else if (b1.half[0].hostval == b2.half[0].hostval) {\
    529 				fwrite(&b1.half[0], SIZE_16, 1, fpx);	\
    530 				addr += SIZE_16;			\
    531 				b1.half[0] = b1.half[1];		\
    532 				b2.half[0] = b2.half[1];		\
    533 				nbuf = 1;				\
    534 			} else						\
    535 				ERRC					\
    536 		}							\
    537 	}
    538 
    539 int
    540 aout2hux(const char *fn1, const char *fn2, u_int32_t loadadr1, u_int32_t loadadr2, const char *fnx)
    541 {
    542 	int status = 1;			/* the default is "failed" */
    543 	FILE *fpa1 = NULL, *fpa2 = NULL;
    544 	struct exec_info inf1, inf2;
    545 	FILE *fpx = NULL;
    546 	struct huxhdr xhdr;
    547 	u_int32_t textsize, datasize, paddingsize, execoff;
    548 
    549 	/* for relocation */
    550 	be_uint32_t b1, b2;
    551 	int nbuf;
    552 	u_int32_t addr;
    553 
    554 	/* for relocation table */
    555 	size_t relsize, relallocsize;
    556 	u_int32_t reladdr;
    557 	char *reltbl = NULL;
    558 
    559 
    560 	/*
    561 	 * check load addresses
    562 	 */
    563 	if (loadadr1 == loadadr2) {
    564 		fprintf(stderr, "two load addresses must be different\n");
    565 		return 1;
    566 	}
    567 
    568 	/*
    569 	 * open input executables and check them
    570 	 */
    571 	if (!(fpa1 = open_exec(fn1, &inf1)) || !(fpa2 = open_exec(fn2, &inf2)))
    572 		goto out;
    573 
    574 	/*
    575 	 * check for consistency
    576 	 */
    577 	if (check_2_exec_inf(&inf1, &inf2)) {
    578 		fprintf(stderr, "files %s and %s are incompatible\n",
    579 				fn1, fn2);
    580 		goto out;
    581 	}
    582 	/* check entry address */
    583 	if (inf1.entry_addr - loadadr1 != inf2.entry_addr - loadadr2) {
    584 		fprintf(stderr, "address of %s or %s may be incorrect\n",
    585 				fn1, fn2);
    586 		goto out;
    587 	}
    588 
    589 	/*
    590 	 * get information of the executables
    591 	 */
    592 	textsize = inf1.text_size;
    593 	paddingsize = inf1.text_pad;
    594 	datasize = inf1.data_size;
    595 	execoff = inf1.entry_addr - loadadr1;
    596 
    597 	DPRINTF(("text: %u, data: %u, pad: %u, bss: %u, exec: %u\n",
    598 		textsize, datasize, paddingsize, inf1.bss_size, execoff));
    599 
    600 	if (textsize & 1) {
    601 		fprintf(stderr, "text size is not even\n");
    602 		goto out;
    603 	}
    604 	if (datasize & 1) {
    605 		fprintf(stderr, "data size is not even\n");
    606 		goto out;
    607 	}
    608 	if (execoff >= textsize &&
    609 	    (execoff < textsize + paddingsize ||
    610 	     execoff >= textsize + paddingsize + datasize)) {
    611 		fprintf(stderr, "exec addr is not in text or data segment\n");
    612 		goto out;
    613 	}
    614 
    615 	/*
    616 	 * prepare for .x header
    617 	 */
    618 	memset((void *) &xhdr, 0, sizeof xhdr);
    619 	put_uint16(&xhdr.x_magic, HUXMAGIC);
    620 	put_uint32(&xhdr.x_entry, execoff);
    621 	put_uint32(&xhdr.x_text, textsize + paddingsize);
    622 	put_uint32(&xhdr.x_data, inf1.data_size);
    623 	put_uint32(&xhdr.x_bss, inf1.bss_size);
    624 
    625 	/*
    626 	 * create output file
    627 	 */
    628 	if (!(fpx = fopen(fnx, "w")) ||
    629 	    fseek(fpx, (foff_t) sizeof xhdr, SEEK_SET)) { /* skip header */
    630 		perror(fnx);
    631 		goto out;
    632 	}
    633 
    634 	addr = 0;
    635 	nbuf = 0;
    636 
    637 	relsize = relallocsize = 0;
    638 	reladdr = 0;
    639 
    640 	/*
    641 	 * text segment
    642 	 */
    643 	if (fseek(fpa1, inf1.text_off, SEEK_SET)) {
    644 		perror(fn1);
    645 		goto out;
    646 	}
    647 	if (fseek(fpa2, inf2.text_off, SEEK_SET)) {
    648 		perror(fn2);
    649 		goto out;
    650 	}
    651 	CREATE_RELOCATION(textsize)
    652 
    653 	/*
    654 	 * page boundary
    655 	 */
    656 	addr += paddingsize;
    657 	while (paddingsize--)
    658 		putc('\0', fpx);
    659 
    660 	/*
    661 	 * data segment
    662 	 */
    663 	if (fseek(fpa1, inf1.data_off, SEEK_SET)) {
    664 		perror(fn1);
    665 		goto out;
    666 	}
    667 	if (fseek(fpa2, inf2.data_off, SEEK_SET)) {
    668 		perror(fn2);
    669 		goto out;
    670 	}
    671 	CREATE_RELOCATION(datasize)
    672 
    673 	/*
    674 	 * error check of the above
    675 	 */
    676 	if (ferror(fpx)) {
    677 		fprintf(stderr, "%s: write failure\n", fnx);
    678 		goto out;
    679 	}
    680 
    681 	/*
    682 	 * write relocation table
    683 	 */
    684 	if (relsize > 0) {
    685 		DPRINTF(("\n"));
    686 		if (fwrite(reltbl, 1, relsize, fpx) != relsize) {
    687 			perror(fnx);
    688 			goto out;
    689 		}
    690 	}
    691 
    692 	/*
    693 	 * write .x header at the top of the output file
    694 	 */
    695 	put_uint32(&xhdr.x_rsize, relsize);
    696 	if (fseek(fpx, (foff_t) 0, SEEK_SET) ||
    697 	    fwrite(&xhdr, sizeof xhdr, 1, fpx) != 1) {
    698 		perror(fnx);
    699 		goto out;
    700 	}
    701 
    702 	status = 0;	/* all OK */
    703 
    704 out:	/*
    705 	 * cleanup
    706 	 */
    707 	if (fpa1)
    708 		fclose(fpa1);
    709 	if (fpa2)
    710 		fclose(fpa2);
    711 	if (fpx) {
    712 		if (fclose(fpx) && status == 0) {
    713 			/* Alas, final flush failed! */
    714 			perror(fnx);
    715 			status = 1;
    716 		}
    717 		if (status)
    718 			remove(fnx);
    719 	}
    720 	if (reltbl)
    721 		free(reltbl);
    722 
    723 	return status;
    724 }
    725 
    726 #ifndef NO_BIST
    727 void bist PROTO((void));
    728 
    729 /*
    730  * built-in self test
    731  */
    732 void
    733 bist(void)
    734 {
    735 	be_uint16_t be16;
    736 	be_uint32_t be32;
    737 	be_uint32_t be32x2[2];
    738 
    739 	be16.val[0] = 0x12; be16.val[1] = 0x34;
    740 	be32.val[0] = 0xfe; be32.val[1] = 0xdc;
    741 	be32.val[2] = 0xba; be32.val[3] = 0x98;
    742 
    743 	put_uint16(&be32x2[0].half[1], 0x4567);
    744 	put_uint32(&be32x2[1], 0xa9876543);
    745 
    746 	if (sizeof(u_int8_t) != 1 || sizeof(u_int16_t) != 2 ||
    747 	    sizeof(u_int32_t) != 4 ||
    748 	    SIZE_16 != 2 || SIZE_32 != 4 || sizeof be32x2 != 8 ||
    749 	    sizeof(struct relinf_s) != 2 || sizeof(struct relinf_l) != 6 ||
    750 	    SIZE_ELF68K_HDR != 52 || SIZE_ELF68K_SHDR != 40 ||
    751 	    SIZE_ELF68K_PHDR != 32 ||
    752 	    get_uint16(&be16) != 0x1234 || get_uint32(&be32) != 0xfedcba98 ||
    753 	    get_uint16(&be32x2[0].half[1]) != 0x4567 ||
    754 	    get_uint32(&be32x2[1]) != 0xa9876543) {
    755 		fprintf(stderr, "BIST failed\n");
    756 		exit(1);
    757 	}
    758 }
    759 #endif
    760 
    761 int
    762 gethex(u_int32_t *pval, const char *str)
    763 {
    764 	const unsigned char *p = (const unsigned char *) str;
    765 	u_int32_t val;
    766 	int over;
    767 
    768 	/* skip leading "0x" if exists */
    769 	if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
    770 		p += 2;
    771 
    772 	if (!*p)
    773 		goto bad;
    774 
    775 	for (val = 0, over = 0; *p; p++) {
    776 		int digit;
    777 
    778 		switch (*p) {
    779 		case '0': case '1': case '2': case '3': case '4':
    780 		case '5': case '6': case '7': case '8': case '9':
    781 			digit = *p - '0';
    782 			break;
    783 		case 'a': case 'A':	digit = 10; break;
    784 		case 'b': case 'B':	digit = 11; break;
    785 		case 'c': case 'C':	digit = 12; break;
    786 		case 'd': case 'D':	digit = 13; break;
    787 		case 'e': case 'E':	digit = 14; break;
    788 		case 'f': case 'F':	digit = 15; break;
    789 		default:
    790 			goto bad;
    791 		}
    792 		if (val >= 0x10000000)
    793 			over = 1;
    794 		val = (val << 4) | digit;
    795 	}
    796 
    797 	if (over)
    798 		fprintf(stderr, "warning: %s: constant overflow\n", str);
    799 
    800 	*pval = val;
    801 
    802 	DPRINTF(("gethex: %s -> 0x%x\n", str, val));
    803 
    804 	return 0;
    805 
    806 bad:
    807 	fprintf(stderr, "%s: not a hexadecimal number\n", str);
    808 	return 1;
    809 }
    810 
    811 void
    812 usage(const char *name)
    813 {
    814 
    815 	fprintf(stderr, "\
    816 usage: %s [ -o output.x ] a.out1 loadaddr1 a.out2 loadaddr2\n\n\
    817 The input files must be static OMAGIC/NMAGIC m68k a.out executables\n\
    818 or m68k ELF executables.\n\
    819 Two executables must have different loading addresses.\n\
    820 Each of the load address must be a hexadecimal number.\n\
    821 The default output filename is \"%s\".\n" ,name, DEFAULT_OUTPUT_FILE);
    822 
    823 	exit(1);
    824 }
    825 
    826 int
    827 main(int argc, char *argv[])
    828 {
    829 	const char *outfile = DEFAULT_OUTPUT_FILE;
    830 	u_int32_t adr1, adr2;
    831 
    832 #ifndef NO_BIST
    833 	bist();
    834 #endif
    835 
    836 	if (argc > 2 && argv[1][0] == '-' && argv[1][1] == 'o' && !argv[1][2]) {
    837 		outfile = argv[2];
    838 		argv += 2;
    839 		argc -= 2;
    840 	}
    841 
    842 	if (argc != 5)
    843 		usage(argv[0]);
    844 
    845 	if (gethex(&adr1, argv[2]) || gethex(&adr2, argv[4]))
    846 		usage(argv[0]);
    847 
    848 	return aout2hux(argv[1], argv[3], adr1, adr2, outfile);
    849 }
    850