/*
* aout2hux - convert a.out/ELF executable to Human68k .x format
*
* Read two a.out/ELF format executables with different load addresses
* and generate Human68k .x format executable.
*
* written by ITOH Yasufumi
* public domain
*
* usage:
* aout2hux [ -o output.x ] a.out1 loadaddr1 a.out2 loadaddr2
*
* The input files must be static OMAGIC/NMAGIC m68k a.out executables
* or m68k ELF executables.
* Two executables must have different loading addresses.
* Each of the load address must be a hexadecimal number.
* Load address shall be multiple of 4 for as / ld of NetBSD/m68k.
*
* example:
* % cc -N -static -Wl,-Ttext,0 -o aout1 *.o
* % cc -N -static -Wl,-Ttext,10203040 -o aout2 *.o
* % aout2hux -o foo.x aout1 0 aout2 10203040
*
* $NetBSD: aout2hux.c,v 1.14 2024/01/07 07:58:33 isaki Exp $
*/
#include <sys/types.h>
#ifndef NO_UNISTD
# include <unistd.h>
#endif
#ifndef NO_STDLIB
# include <stdlib.h>
#endif
#include <stdio.h>
#include <string.h>
struct exec_info {
foff_t text_off; /* file offset of text section */
foff_t data_off; /* file offset of data section */
u_int32_t text_size; /* size of text section */
u_int32_t text_pad; /* pad between text and data */
u_int32_t data_size; /* size of data section */
u_int32_t bss_size; /* size of bss */
u_int32_t entry_addr; /* entry point address */
};
/* if unsolved relocations exist, not an executable but an object */
if (hdr->a_trsize.hostval || hdr->a_drsize.hostval) {
fprintf(stderr, "%s: not an executable (object file?)\n", fn);
return 1;
}
if (AOUT_GET_FLAGS(hdr) & (AOUT_FLAG_PIC | AOUT_FLAG_DYNAMIC)) {
fprintf(stderr, "%s: PIC and DYNAMIC are not supported\n", fn);
return 1;
}
if ((i = get_uint16(&hdr->e_shentsize)) != SIZE_ELF68K_SHDR) {
fprintf(stderr, "%s: size shdr %d should be %d\n", fn, i,
(int)SIZE_ELF68K_SHDR);
return 1;
}
if ((i = get_uint16(&hdr->e_phentsize)) != SIZE_ELF68K_PHDR) {
fprintf(stderr, "%s: size phdr %d should be %d\n", fn, i,
(int)SIZE_ELF68K_PHDR);
return 1;
}
if ((nphdr = get_uint16(&hdr->e_phnum)) != 1 && nphdr != 2) {
fprintf(stderr,
"%s: has %lu loadable segments (should be 1 or 2)\n",
fn, (unsigned long)nphdr);
return 1;
}
/* Read ELF program header table. */
if (fseek(fp, (foff_t) get_uint32(&hdr->e_phoff), SEEK_SET)) {
perror(fn);
return 1;
}
if (fread(phdr, sizeof phdr[0], nphdr, fp) != nphdr) {
fprintf(stderr, "%s: can't read ELF program header\n", fn);
return 1;
}
/* Just error checking. */
for (i = 0; i < (int) nphdr; i++) {
if (get_uint32(&phdr[i].p_type) != PT_LOAD) {
fprintf(stderr,
"%s: program header #%d is not loadable\n",
fn, i);
return 1;
}
}
if (nphdr == 1 && (get_uint32(&phdr[0].p_flags) & PF_W)) {
/*
* Only one writable section --- probably "ld -N" executable.
* Find out the start of data segment.
*/
struct elf_m68k_shdr shdr;
int nshdr;
nshdr = get_uint16(&hdr->e_shnum);
/* section #0 always exists and reserved --- skip */
if (nshdr > 1 &&
fseek(fp,
(foff_t) (get_uint32(&hdr->e_shoff) + sizeof shdr),
SEEK_SET)) {
perror(fn);
return 1;
}
for (i = 1; i < nshdr; i++) {
if (fread(&shdr, sizeof shdr, 1, fp) != 1) {
fprintf(stderr,
"%s: can't read ELF section header\n",
fn);
return 1;
}
DPRINTF(("%s: section header #%d: flags 0x%x\n",
fn, i, get_uint32(&shdr.sh_flags)));
if (ELF68K_ISDATASEG(&shdr)) {
/*
* data section is found.
*/
DPRINTF(("%s: one section, data found\n", fn));
inf->text_off = get_uint32(&phdr[0].p_offset);
inf->text_size = get_uint32(&shdr.sh_offset) -
inf->text_off;
inf->text_pad = 0;
inf->data_off = inf->text_off + inf->text_size;
inf->data_size = get_uint32(&phdr[0].p_filesz) -
inf->text_size;
inf->bss_size = get_uint32(&phdr[0].p_memsz) -
get_uint32(&phdr[0].p_filesz);
inf->entry_addr = get_uint32(&hdr->e_entry);
goto data_found;
}
}
/*
* No data section found --- probably text + bss.
*/
DPRINTF(("%s: one section, no data section\n", fn));
inf->text_size = get_uint32(&phdr[0].p_filesz);
inf->data_size = 0;
inf->bss_size = get_uint32(&phdr[0].p_memsz) - inf->text_size;
inf->entry_addr = get_uint32(&hdr->e_entry);
inf->text_off = get_uint32(&phdr[0].p_offset);
inf->data_off = 0;
inf->text_pad = 0;
data_found:;
} else if (nphdr == 1) {
/*
* Only one non-writable section --- pure text program?
*/
DPRINTF(("%s: one RO section\n", fn));
inf->text_size = get_uint32(&phdr[0].p_filesz);
inf->data_size = 0;
inf->bss_size = 0;
inf->entry_addr = get_uint32(&hdr->e_entry);
inf->text_off = get_uint32(&phdr[0].p_offset);
inf->data_off = 0;
inf->text_pad = get_uint32(&phdr[0].p_memsz) - inf->text_size;
} else {
/*
* two sections
* text + data assumed.
*/
int t = 0, d = 1, tmp; /* first guess */
#define SWAP_T_D tmp = t, t = d, d = tmp
DPRINTF(("%s: two sections\n", fn));
/* Find out text and data. */
if (get_uint32(&phdr[t].p_vaddr) > get_uint32(&phdr[d].p_vaddr))
SWAP_T_D;
/* Are the text/data sections correctly detected? */
if (get_uint32(&phdr[t].p_vaddr) >
get_uint32(&phdr[d].p_vaddr)) {
fprintf(stderr, "%s: program sections not in order\n",
fn);
return 1;
}
if ((get_uint32(&phdr[t].p_flags) & PF_X) == 0)
fprintf(stderr, "%s: warning: text is not executable\n",
fn);
if ((get_uint32(&phdr[d].p_flags) & PF_W) == 0)
fprintf(stderr, "%s: warning: data is not writable\n",
fn);
if (textsize & 1) {
fprintf(stderr, "text size is not even\n");
goto out;
}
if (datasize & 1) {
fprintf(stderr, "data size is not even\n");
goto out;
}
if (execoff >= textsize &&
(execoff < textsize + paddingsize ||
execoff >= textsize + paddingsize + datasize)) {
fprintf(stderr, "exec addr is not in text or data segment\n");
goto out;
}
/*
* write .x header at the top of the output file
*/
put_uint32(&xhdr.x_rsize, relsize);
if (fseek(fpx, (foff_t) 0, SEEK_SET) ||
fwrite(&xhdr, sizeof xhdr, 1, fpx) != 1) {
perror(fnx);
goto out;
}
status = 0; /* all OK */
out: /*
* cleanup
*/
if (fpa1)
fclose(fpa1);
if (fpa2)
fclose(fpa2);
if (fpx) {
if (fclose(fpx) && status == 0) {
/* Alas, final flush failed! */
perror(fnx);
status = 1;
}
if (status)
remove(fnx);
}
if (reltbl)
free(reltbl);
/* skip leading "0x" if exists */
if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
p += 2;
if (!*p)
goto bad;
for (val = 0, over = 0; *p; p++) {
int digit;
switch (*p) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
digit = *p - '0';
break;
case 'a': case 'A': digit = 10; break;
case 'b': case 'B': digit = 11; break;
case 'c': case 'C': digit = 12; break;
case 'd': case 'D': digit = 13; break;
case 'e': case 'E': digit = 14; break;
case 'f': case 'F': digit = 15; break;
default:
goto bad;
}
if (val >= 0x10000000)
over = 1;
val = (val << 4) | digit;
}
if (over)
fprintf(stderr, "warning: %s: constant overflow\n", str);
*pval = val;
DPRINTF(("gethex: %s -> 0x%x\n", str, val));
return 0;
bad:
fprintf(stderr, "%s: not a hexadecimal number\n", str);
return 1;
}
void
usage(const char *name)
{
fprintf(stderr, "\
usage: %s [ -o output.x ] a.out1 loadaddr1 a.out2 loadaddr2\n\n\
The input files must be static OMAGIC/NMAGIC m68k a.out executables\n\
or m68k ELF executables.\n\
Two executables must have different loading addresses.\n\
Each of the load address must be a hexadecimal number.\n\
The default output filename is \"%s\".\n" ,name, DEFAULT_OUTPUT_FILE);