Home | History | Annotate | Line # | Download | only in elf2aout
elf2aout.c revision 1.20
      1  1.20     skrll /*	$NetBSD: elf2aout.c,v 1.20 2019/04/26 07:35:21 skrll Exp $	*/
      2   1.2  jonathan 
      3   1.1  jonathan /*
      4   1.1  jonathan  * Copyright (c) 1995
      5   1.1  jonathan  *	Ted Lemon (hereinafter referred to as the author)
      6   1.1  jonathan  *
      7   1.1  jonathan  * Redistribution and use in source and binary forms, with or without
      8   1.1  jonathan  * modification, are permitted provided that the following conditions
      9   1.1  jonathan  * are met:
     10   1.1  jonathan  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jonathan  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jonathan  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jonathan  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jonathan  *    documentation and/or other materials provided with the distribution.
     15   1.1  jonathan  * 3. The name of the author may not be used to endorse or promote products
     16   1.1  jonathan  *    derived from this software without specific prior written permission.
     17   1.1  jonathan  *
     18   1.1  jonathan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
     19   1.1  jonathan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20   1.1  jonathan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21   1.1  jonathan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
     22   1.1  jonathan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23   1.1  jonathan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24   1.1  jonathan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25   1.1  jonathan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26   1.1  jonathan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27   1.1  jonathan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28   1.1  jonathan  * SUCH DAMAGE.
     29   1.1  jonathan  */
     30   1.1  jonathan 
     31   1.1  jonathan /* elf2aout.c
     32   1.1  jonathan 
     33   1.1  jonathan    This program converts an elf executable to a NetBSD a.out executable.
     34   1.1  jonathan    The minimal symbol table is copied, but the debugging symbols and
     35   1.1  jonathan    other informational sections are not. */
     36   1.1  jonathan 
     37  1.15   tsutsui #if HAVE_NBTOOL_CONFIG_H
     38  1.15   tsutsui #include "nbtool_config.h"
     39  1.15   tsutsui #endif
     40  1.15   tsutsui 
     41  1.15   tsutsui #ifndef TARGET_BYTE_ORDER
     42  1.15   tsutsui #define TARGET_BYTE_ORDER	BYTE_ORDER
     43  1.15   tsutsui #endif
     44  1.15   tsutsui 
     45   1.1  jonathan #include <sys/types.h>
     46   1.5     lukem #include <sys/exec_aout.h>
     47   1.5     lukem #include <sys/exec_elf.h>
     48   1.5     lukem 
     49   1.5     lukem #include <a.out.h>
     50   1.5     lukem #include <err.h>
     51   1.5     lukem #include <errno.h>
     52   1.1  jonathan #include <fcntl.h>
     53   1.5     lukem #include <limits.h>
     54   1.1  jonathan #include <stdio.h>
     55   1.5     lukem #include <stdlib.h>
     56   1.1  jonathan #include <string.h>
     57   1.5     lukem #include <unistd.h>
     58   1.1  jonathan 
     59   1.3  jonathan 
     60   1.1  jonathan struct sect {
     61  1.15   tsutsui 	/* should be unsigned long, but assume no a.out binaries on LP64 */
     62  1.15   tsutsui 	uint32_t vaddr;
     63  1.15   tsutsui 	uint32_t len;
     64   1.1  jonathan };
     65   1.1  jonathan 
     66  1.13   tsutsui void	combine(struct sect *, struct sect *, int);
     67  1.13   tsutsui int	phcmp(const void *, const void *);
     68  1.17  christos void   *saveRead(int file, off_t offset, size_t len, const char *name);
     69  1.13   tsutsui void	copy(int, int, off_t, off_t);
     70  1.13   tsutsui void	translate_syms(int, int, off_t, off_t, off_t, off_t);
     71   1.5     lukem 
     72  1.15   tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
     73  1.15   tsutsui void	bswap32_region(int32_t* , int);
     74  1.15   tsutsui #endif
     75  1.15   tsutsui 
     76   1.5     lukem int    *symTypeTable;
     77   1.5     lukem 
     78   1.5     lukem int
     79   1.5     lukem main(int argc, char **argv)
     80   1.1  jonathan {
     81   1.5     lukem 	Elf32_Ehdr ex;
     82   1.5     lukem 	Elf32_Phdr *ph;
     83   1.5     lukem 	Elf32_Shdr *sh;
     84   1.5     lukem 	char   *shstrtab;
     85  1.17  christos 	ssize_t i, strtabix, symtabix;
     86   1.5     lukem 	struct sect text, data, bss;
     87   1.5     lukem 	struct exec aex;
     88   1.5     lukem 	int     infile, outfile;
     89  1.15   tsutsui 	uint32_t cur_vma = UINT32_MAX;
     90  1.15   tsutsui 	uint32_t mid;
     91   1.5     lukem 	int     symflag = 0;
     92   1.5     lukem 
     93   1.5     lukem 	strtabix = symtabix = 0;
     94   1.5     lukem 	text.len = data.len = bss.len = 0;
     95   1.5     lukem 	text.vaddr = data.vaddr = bss.vaddr = 0;
     96   1.5     lukem 
     97   1.5     lukem 	/* Check args... */
     98   1.5     lukem 	if (argc < 3 || argc > 4) {
     99   1.5     lukem usage:
    100   1.5     lukem 		fprintf(stderr,
    101  1.17  christos 		    "Usage: %s <elf executable> <a.out executable> [-s]\n",
    102  1.17  christos 		    getprogname());
    103  1.17  christos 		exit(EXIT_FAILURE);
    104   1.5     lukem 	}
    105   1.5     lukem 	if (argc == 4) {
    106   1.5     lukem 		if (strcmp(argv[3], "-s"))
    107   1.5     lukem 			goto usage;
    108   1.5     lukem 		symflag = 1;
    109   1.5     lukem 	}
    110   1.5     lukem 	/* Try the input file... */
    111  1.17  christos 	if ((infile = open(argv[1], O_RDONLY)) < 0)
    112  1.17  christos 		err(EXIT_FAILURE, "Can't open `%s' for read", argv[1]);
    113  1.17  christos 
    114   1.5     lukem 	/* Read the header, which is at the beginning of the file... */
    115   1.5     lukem 	i = read(infile, &ex, sizeof ex);
    116   1.5     lukem 	if (i != sizeof ex) {
    117  1.17  christos 		if (i == -1)
    118  1.17  christos 			err(EXIT_FAILURE, "Error reading `%s'", argv[1]);
    119  1.17  christos 		else
    120  1.17  christos 			errx(EXIT_FAILURE, "End of file reading `%s'", argv[1]);
    121   1.5     lukem 	}
    122  1.15   tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
    123  1.15   tsutsui 	ex.e_type	= bswap16(ex.e_type);
    124  1.15   tsutsui 	ex.e_machine	= bswap16(ex.e_machine);
    125  1.15   tsutsui 	ex.e_version	= bswap32(ex.e_version);
    126  1.15   tsutsui 	ex.e_entry 	= bswap32(ex.e_entry);
    127  1.15   tsutsui 	ex.e_phoff	= bswap32(ex.e_phoff);
    128  1.15   tsutsui 	ex.e_shoff	= bswap32(ex.e_shoff);
    129  1.15   tsutsui 	ex.e_flags	= bswap32(ex.e_flags);
    130  1.15   tsutsui 	ex.e_ehsize	= bswap16(ex.e_ehsize);
    131  1.15   tsutsui 	ex.e_phentsize	= bswap16(ex.e_phentsize);
    132  1.15   tsutsui 	ex.e_phnum	= bswap16(ex.e_phnum);
    133  1.15   tsutsui 	ex.e_shentsize	= bswap16(ex.e_shentsize);
    134  1.15   tsutsui 	ex.e_shnum	= bswap16(ex.e_shnum);
    135  1.15   tsutsui 	ex.e_shstrndx	= bswap16(ex.e_shstrndx);
    136  1.15   tsutsui #endif
    137   1.5     lukem 	/* Read the program headers... */
    138  1.17  christos 	ph = saveRead(infile, ex.e_phoff,
    139  1.17  christos 	    (size_t)ex.e_phnum * sizeof(Elf32_Phdr), "ph");
    140  1.15   tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
    141  1.15   tsutsui 	bswap32_region((int32_t*)ph, sizeof(Elf32_Phdr) * ex.e_phnum);
    142  1.15   tsutsui #endif
    143   1.5     lukem 	/* Read the section headers... */
    144  1.17  christos 	sh = saveRead(infile, ex.e_shoff,
    145  1.17  christos 	    (size_t)ex.e_shnum * sizeof(Elf32_Shdr), "sh");
    146  1.15   tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
    147  1.15   tsutsui 	bswap32_region((int32_t*)sh, sizeof(Elf32_Shdr) * ex.e_shnum);
    148  1.15   tsutsui #endif
    149   1.5     lukem 	/* Read in the section string table. */
    150   1.5     lukem 	shstrtab = saveRead(infile, sh[ex.e_shstrndx].sh_offset,
    151  1.17  christos 	    (size_t)sh[ex.e_shstrndx].sh_size, "shstrtab");
    152   1.5     lukem 
    153   1.5     lukem 	/* Find space for a table matching ELF section indices to a.out symbol
    154   1.5     lukem 	 * types. */
    155  1.13   tsutsui 	symTypeTable = malloc(ex.e_shnum * sizeof(int));
    156  1.17  christos 	if (symTypeTable == NULL)
    157  1.17  christos 		err(EXIT_FAILURE, "symTypeTable: can't allocate");
    158   1.5     lukem 	memset(symTypeTable, 0, ex.e_shnum * sizeof(int));
    159   1.5     lukem 
    160   1.5     lukem 	/* Look for the symbol table and string table... Also map section
    161   1.5     lukem 	 * indices to symbol types for a.out */
    162   1.5     lukem 	for (i = 0; i < ex.e_shnum; i++) {
    163   1.5     lukem 		char   *name = shstrtab + sh[i].sh_name;
    164   1.5     lukem 		if (!strcmp(name, ".symtab"))
    165   1.5     lukem 			symtabix = i;
    166   1.5     lukem 		else
    167   1.5     lukem 			if (!strcmp(name, ".strtab"))
    168   1.5     lukem 				strtabix = i;
    169   1.5     lukem 			else
    170   1.5     lukem 				if (!strcmp(name, ".text") || !strcmp(name, ".rodata"))
    171   1.5     lukem 					symTypeTable[i] = N_TEXT;
    172   1.5     lukem 				else
    173   1.5     lukem 					if (!strcmp(name, ".data") || !strcmp(name, ".sdata") ||
    174   1.5     lukem 					    !strcmp(name, ".lit4") || !strcmp(name, ".lit8"))
    175   1.5     lukem 						symTypeTable[i] = N_DATA;
    176   1.5     lukem 					else
    177   1.5     lukem 						if (!strcmp(name, ".bss") || !strcmp(name, ".sbss"))
    178   1.5     lukem 							symTypeTable[i] = N_BSS;
    179   1.5     lukem 	}
    180   1.5     lukem 
    181   1.5     lukem 	/* Figure out if we can cram the program header into an a.out
    182   1.5     lukem 	 * header... Basically, we can't handle anything but loadable
    183   1.5     lukem 	 * segments, but we can ignore some kinds of segments.   We can't
    184   1.5     lukem 	 * handle holes in the address space, and we handle start addresses
    185   1.5     lukem 	 * other than 0x1000 by hoping that the loader will know where to load
    186   1.5     lukem 	 * - a.out doesn't have an explicit load address.   Segments may be
    187   1.5     lukem 	 * out of order, so we sort them first. */
    188   1.5     lukem 	qsort(ph, ex.e_phnum, sizeof(Elf32_Phdr), phcmp);
    189   1.5     lukem 	for (i = 0; i < ex.e_phnum; i++) {
    190   1.5     lukem 		/* Section types we can ignore... */
    191   1.7  drochner 		if (ph[i].p_type == PT_NULL || ph[i].p_type == PT_NOTE ||
    192   1.7  drochner 		    ph[i].p_type == PT_PHDR || ph[i].p_type == PT_MIPS_REGINFO)
    193   1.5     lukem 			continue;
    194   1.5     lukem 		/* Section types we can't handle... */
    195   1.5     lukem 		else
    196   1.7  drochner 			if (ph[i].p_type != PT_LOAD)
    197  1.17  christos 				errx(EXIT_FAILURE, "Program header %zd "
    198  1.17  christos 				    "type %d can't be converted.",
    199  1.17  christos 				    i, ph[i].p_type);
    200   1.5     lukem 		/* Writable (data) segment? */
    201   1.5     lukem 		if (ph[i].p_flags & PF_W) {
    202   1.5     lukem 			struct sect ndata, nbss;
    203   1.5     lukem 
    204   1.5     lukem 			ndata.vaddr = ph[i].p_vaddr;
    205   1.5     lukem 			ndata.len = ph[i].p_filesz;
    206   1.5     lukem 			nbss.vaddr = ph[i].p_vaddr + ph[i].p_filesz;
    207   1.5     lukem 			nbss.len = ph[i].p_memsz - ph[i].p_filesz;
    208   1.5     lukem 
    209   1.5     lukem 			combine(&data, &ndata, 0);
    210   1.5     lukem 			combine(&bss, &nbss, 1);
    211   1.5     lukem 		} else {
    212   1.5     lukem 			struct sect ntxt;
    213   1.5     lukem 
    214   1.5     lukem 			ntxt.vaddr = ph[i].p_vaddr;
    215   1.5     lukem 			ntxt.len = ph[i].p_filesz;
    216   1.5     lukem 
    217   1.5     lukem 			combine(&text, &ntxt, 0);
    218   1.1  jonathan 		}
    219   1.5     lukem 		/* Remember the lowest segment start address. */
    220   1.5     lukem 		if (ph[i].p_vaddr < cur_vma)
    221   1.5     lukem 			cur_vma = ph[i].p_vaddr;
    222   1.5     lukem 	}
    223   1.5     lukem 
    224   1.5     lukem 	/* Sections must be in order to be converted... */
    225   1.5     lukem 	if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr ||
    226  1.17  christos 	    text.vaddr + text.len > data.vaddr ||
    227  1.17  christos 	    data.vaddr + data.len > bss.vaddr)
    228  1.17  christos 		errx(EXIT_FAILURE, "Sections ordering prevents a.out "
    229  1.17  christos 		    "conversion.");
    230   1.5     lukem 	/* If there's a data section but no text section, then the loader
    231   1.5     lukem 	 * combined everything into one section.   That needs to be the text
    232   1.5     lukem 	 * section, so just make the data section zero length following text. */
    233  1.13   tsutsui 	if (data.len && text.len == 0) {
    234   1.5     lukem 		text = data;
    235   1.5     lukem 		data.vaddr = text.vaddr + text.len;
    236   1.5     lukem 		data.len = 0;
    237   1.5     lukem 	}
    238   1.5     lukem 	/* If there is a gap between text and data, we'll fill it when we copy
    239   1.5     lukem 	 * the data, so update the length of the text segment as represented
    240   1.5     lukem 	 * in a.out to reflect that, since a.out doesn't allow gaps in the
    241   1.5     lukem 	 * program address space. */
    242   1.5     lukem 	if (text.vaddr + text.len < data.vaddr)
    243   1.5     lukem 		text.len = data.vaddr - text.vaddr;
    244   1.5     lukem 
    245   1.5     lukem 	/* We now have enough information to cons up an a.out header... */
    246  1.14   tsutsui 	switch (ex.e_machine) {
    247  1.14   tsutsui 	case EM_SPARC:
    248  1.14   tsutsui 		mid = MID_SPARC;
    249  1.14   tsutsui 		break;
    250  1.14   tsutsui 	case EM_386:
    251  1.14   tsutsui 		mid = MID_PC386;
    252  1.14   tsutsui 		break;
    253  1.14   tsutsui 	case EM_68K:
    254  1.14   tsutsui 		mid = MID_M68K;
    255  1.14   tsutsui 		break;
    256  1.14   tsutsui 	case EM_MIPS:
    257  1.14   tsutsui 		if (ex.e_ident[EI_DATA] == ELFDATA2LSB)
    258  1.14   tsutsui 			mid = MID_PMAX;
    259  1.14   tsutsui 		else
    260  1.14   tsutsui 			mid = MID_MIPS;
    261  1.14   tsutsui 		break;
    262  1.14   tsutsui 	case EM_PPC:
    263  1.14   tsutsui 		mid = MID_POWERPC;
    264  1.14   tsutsui 		break;
    265  1.14   tsutsui 	case EM_ARM:
    266  1.14   tsutsui 		mid = MID_ARM6;
    267  1.14   tsutsui 		break;
    268  1.14   tsutsui 	case EM_VAX:
    269  1.14   tsutsui 		mid = MID_VAX;
    270  1.14   tsutsui 		break;
    271  1.14   tsutsui 	case EM_NONE:
    272  1.14   tsutsui 	default:
    273  1.14   tsutsui 		mid = MID_ZERO;
    274  1.14   tsutsui 	}
    275  1.20     skrll 	aex.a_midmag = (u_long)htonl(((u_long)symflag << 26)
    276  1.20     skrll 	    | ((u_long)mid << 16) | ZMAGIC);
    277  1.13   tsutsui 
    278   1.5     lukem 	aex.a_text = text.len;
    279   1.5     lukem 	aex.a_data = data.len;
    280   1.5     lukem 	aex.a_bss = bss.len;
    281   1.5     lukem 	aex.a_entry = ex.e_entry;
    282   1.5     lukem 	aex.a_syms = (sizeof(struct nlist) *
    283   1.5     lukem 	    (symtabix != -1
    284   1.5     lukem 		? sh[symtabix].sh_size / sizeof(Elf32_Sym) : 0));
    285   1.5     lukem 	aex.a_trsize = 0;
    286   1.5     lukem 	aex.a_drsize = 0;
    287  1.15   tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
    288  1.15   tsutsui 	aex.a_text = bswap32(aex.a_text);
    289  1.15   tsutsui 	aex.a_data = bswap32(aex.a_data);
    290  1.15   tsutsui 	aex.a_bss = bswap32(aex.a_bss);
    291  1.15   tsutsui 	aex.a_entry = bswap32(aex.a_entry);
    292  1.15   tsutsui 	aex.a_syms = bswap32(aex.a_syms);
    293  1.15   tsutsui 	aex.a_trsize = bswap32(aex.a_trsize);
    294  1.15   tsutsui 	aex.a_drsize = bswap32(aex.a_drsize);
    295  1.15   tsutsui #endif
    296   1.5     lukem 
    297   1.5     lukem 	/* Make the output file... */
    298  1.17  christos 	if ((outfile = open(argv[2], O_WRONLY | O_CREAT, 0777)) < 0)
    299  1.17  christos 		err(EXIT_FAILURE, "Unable to create `%s'", argv[2]);
    300   1.6    simonb 	/* Truncate file... */
    301   1.6    simonb 	if (ftruncate(outfile, 0)) {
    302   1.6    simonb 		warn("ftruncate %s", argv[2]);
    303   1.6    simonb 	}
    304   1.5     lukem 	/* Write the header... */
    305   1.5     lukem 	i = write(outfile, &aex, sizeof aex);
    306  1.17  christos 	if (i != sizeof aex)
    307  1.17  christos 		err(EXIT_FAILURE, "Can't write `%s'", argv[2]);
    308   1.5     lukem 	/* Copy the loadable sections.   Zero-fill any gaps less than 64k;
    309   1.5     lukem 	 * complain about any zero-filling, and die if we're asked to
    310   1.5     lukem 	 * zero-fill more than 64k. */
    311   1.5     lukem 	for (i = 0; i < ex.e_phnum; i++) {
    312   1.5     lukem 		/* Unprocessable sections were handled above, so just verify
    313   1.5     lukem 		 * that the section can be loaded before copying. */
    314   1.7  drochner 		if (ph[i].p_type == PT_LOAD && ph[i].p_filesz) {
    315   1.5     lukem 			if (cur_vma != ph[i].p_vaddr) {
    316  1.15   tsutsui 				uint32_t gap = ph[i].p_vaddr - cur_vma;
    317   1.5     lukem 				char    obuf[1024];
    318   1.5     lukem 				if (gap > 65536)
    319  1.17  christos 					errx(EXIT_FAILURE,
    320  1.17  christos 			"Intersegment gap (%u bytes) too large", gap);
    321   1.6    simonb #ifdef DEBUG
    322  1.17  christos 				warnx("%u byte intersegment gap", gap);
    323   1.6    simonb #endif
    324   1.5     lukem 				memset(obuf, 0, sizeof obuf);
    325   1.5     lukem 				while (gap) {
    326  1.17  christos 					ssize_t count = write(outfile, obuf,
    327  1.17  christos 					    (gap > sizeof obuf
    328  1.17  christos 					    ? sizeof obuf : gap));
    329  1.17  christos 					if (count < 0)
    330  1.17  christos 						err(EXIT_FAILURE,
    331  1.17  christos 						    "Error writing gap");
    332  1.17  christos 					gap -= (uint32_t)count;
    333   1.5     lukem 				}
    334   1.5     lukem 			}
    335   1.5     lukem 			copy(outfile, infile, ph[i].p_offset, ph[i].p_filesz);
    336   1.5     lukem 			cur_vma = ph[i].p_vaddr + ph[i].p_filesz;
    337   1.1  jonathan 		}
    338   1.5     lukem 	}
    339   1.5     lukem 
    340   1.5     lukem 	/* Copy and translate the symbol table... */
    341   1.5     lukem 	translate_syms(outfile, infile,
    342   1.5     lukem 	    sh[symtabix].sh_offset, sh[symtabix].sh_size,
    343   1.5     lukem 	    sh[strtabix].sh_offset, sh[strtabix].sh_size);
    344   1.1  jonathan 
    345  1.17  christos 	free(ph);
    346  1.17  christos 	free(sh);
    347  1.17  christos 	free(shstrtab);
    348  1.17  christos 	free(symTypeTable);
    349   1.5     lukem 	/* Looks like we won... */
    350  1.17  christos 	return EXIT_SUCCESS;
    351   1.1  jonathan }
    352   1.1  jonathan /* translate_syms (out, in, offset, size)
    353   1.1  jonathan 
    354   1.1  jonathan    Read the ELF symbol table from in at offset; translate it into a.out
    355   1.1  jonathan    nlist format and write it to out. */
    356   1.1  jonathan 
    357   1.5     lukem void
    358  1.13   tsutsui translate_syms(int out, int in, off_t symoff, off_t symsize,
    359  1.13   tsutsui     off_t stroff, off_t strsize)
    360   1.1  jonathan {
    361   1.5     lukem #define SYMS_PER_PASS	64
    362   1.5     lukem 	Elf32_Sym inbuf[64];
    363   1.5     lukem 	struct nlist outbuf[64];
    364  1.17  christos 	ssize_t i, remaining, cur;
    365   1.5     lukem 	char   *oldstrings;
    366   1.5     lukem 	char   *newstrings, *nsp;
    367  1.20     skrll 	size_t  newstringsize;
    368  1.20     skrll 	uint32_t stringsizebuf;
    369   1.5     lukem 
    370   1.5     lukem 	/* Zero the unused fields in the output buffer.. */
    371   1.5     lukem 	memset(outbuf, 0, sizeof outbuf);
    372   1.5     lukem 
    373   1.5     lukem 	/* Find number of symbols to process... */
    374  1.19    martin 	remaining = (ssize_t)(symsize / (off_t)sizeof(Elf32_Sym));
    375   1.5     lukem 
    376   1.5     lukem 	/* Suck in the old string table... */
    377  1.17  christos 	oldstrings = saveRead(in, stroff, (size_t)strsize, "string table");
    378   1.5     lukem 
    379  1.20     skrll 	/*
    380  1.20     skrll 	 * Allocate space for the new one.  We will increase the space if
    381  1.20     skrll 	 * this is too small
    382  1.20     skrll 	 */
    383  1.17  christos 	newstringsize = (size_t)(strsize + remaining);
    384  1.13   tsutsui 	newstrings = malloc(newstringsize);
    385  1.17  christos 	if (newstrings == NULL)
    386  1.17  christos 		err(EXIT_FAILURE, "No memory for new string table!");
    387   1.5     lukem 	/* Initialize the table pointer... */
    388   1.5     lukem 	nsp = newstrings;
    389   1.5     lukem 
    390  1.10     soren 	/* Go the start of the ELF symbol table... */
    391  1.17  christos 	if (lseek(in, symoff, SEEK_SET) < 0)
    392  1.17  christos 		err(EXIT_FAILURE, "Can't seek");
    393   1.5     lukem 	/* Translate and copy symbols... */
    394  1.20     skrll 	for (; remaining; remaining -= cur) {
    395   1.5     lukem 		cur = remaining;
    396   1.5     lukem 		if (cur > SYMS_PER_PASS)
    397   1.5     lukem 			cur = SYMS_PER_PASS;
    398  1.17  christos 		if ((i = read(in, inbuf, (size_t)cur * sizeof(Elf32_Sym)))
    399  1.15   tsutsui 		    != cur * (ssize_t)sizeof(Elf32_Sym)) {
    400   1.5     lukem 			if (i < 0)
    401  1.17  christos 				err(EXIT_FAILURE, "%s: read error", __func__);
    402   1.5     lukem 			else
    403  1.17  christos 				errx(EXIT_FAILURE, "%s: premature end of file",
    404  1.17  christos 					__func__);
    405   1.5     lukem 		}
    406   1.5     lukem 		/* Do the translation... */
    407   1.5     lukem 		for (i = 0; i < cur; i++) {
    408   1.5     lukem 			int     binding, type;
    409  1.20     skrll 			size_t off, len;
    410   1.5     lukem 
    411  1.15   tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
    412  1.15   tsutsui 			inbuf[i].st_name  = bswap32(inbuf[i].st_name);
    413  1.15   tsutsui 			inbuf[i].st_value = bswap32(inbuf[i].st_value);
    414  1.15   tsutsui 			inbuf[i].st_size  = bswap32(inbuf[i].st_size);
    415  1.15   tsutsui 			inbuf[i].st_shndx = bswap16(inbuf[i].st_shndx);
    416  1.15   tsutsui #endif
    417  1.20     skrll 			off = (size_t)(nsp - newstrings);
    418  1.20     skrll 
    419  1.20     skrll 			/* length of this symbol with leading '_' and trailing '\0' */
    420  1.20     skrll 			len = strlen(oldstrings + inbuf[i].st_name) + 1 + 1;
    421  1.20     skrll 
    422  1.20     skrll 			/* Does it fit? If not make more space */
    423  1.20     skrll 			if (newstringsize - off < len) {
    424  1.20     skrll 				char *nns;
    425  1.20     skrll 
    426  1.20     skrll 				newstringsize += (size_t)(remaining) * len;
    427  1.20     skrll 				nns = realloc(newstrings, newstringsize);
    428  1.20     skrll 				if (nns == NULL)
    429  1.20     skrll 					err(EXIT_FAILURE, "No memory for new string table!");
    430  1.20     skrll 				newstrings = nns;
    431  1.20     skrll 				nsp = newstrings + off;
    432  1.20     skrll 			}
    433   1.5     lukem 			/* Copy the symbol into the new table, but prepend an
    434   1.5     lukem 			 * underscore. */
    435   1.5     lukem 			*nsp = '_';
    436   1.5     lukem 			strcpy(nsp + 1, oldstrings + inbuf[i].st_name);
    437   1.5     lukem 			outbuf[i].n_un.n_strx = nsp - newstrings + 4;
    438  1.20     skrll 			nsp += len;
    439   1.5     lukem 
    440   1.7  drochner 			type = ELF32_ST_TYPE(inbuf[i].st_info);
    441   1.7  drochner 			binding = ELF32_ST_BIND(inbuf[i].st_info);
    442   1.5     lukem 
    443   1.5     lukem 			/* Convert ELF symbol type/section/etc info into a.out
    444   1.5     lukem 			 * type info. */
    445   1.7  drochner 			if (type == STT_FILE)
    446   1.5     lukem 				outbuf[i].n_type = N_FN;
    447   1.5     lukem 			else
    448   1.7  drochner 				if (inbuf[i].st_shndx == SHN_UNDEF)
    449   1.5     lukem 					outbuf[i].n_type = N_UNDF;
    450   1.5     lukem 				else
    451   1.7  drochner 					if (inbuf[i].st_shndx == SHN_ABS)
    452   1.5     lukem 						outbuf[i].n_type = N_ABS;
    453   1.5     lukem 					else
    454   1.7  drochner 						if (inbuf[i].st_shndx == SHN_COMMON ||
    455   1.7  drochner 						    inbuf[i].st_shndx == SHN_MIPS_ACOMMON)
    456   1.5     lukem 							outbuf[i].n_type = N_COMM;
    457   1.5     lukem 						else
    458  1.17  christos 							outbuf[i].n_type = (unsigned char)symTypeTable[inbuf[i].st_shndx];
    459   1.7  drochner 			if (binding == STB_GLOBAL)
    460   1.5     lukem 				outbuf[i].n_type |= N_EXT;
    461   1.5     lukem 			/* Symbol values in executables should be compatible. */
    462   1.5     lukem 			outbuf[i].n_value = inbuf[i].st_value;
    463  1.15   tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
    464  1.15   tsutsui 			outbuf[i].n_un.n_strx = bswap32(outbuf[i].n_un.n_strx);
    465  1.15   tsutsui 			outbuf[i].n_desc      = bswap16(outbuf[i].n_desc);
    466  1.15   tsutsui 			outbuf[i].n_value     = bswap32(outbuf[i].n_value);
    467  1.15   tsutsui #endif
    468   1.5     lukem 		}
    469   1.5     lukem 		/* Write out the symbols... */
    470  1.17  christos 		if ((i = write(out, outbuf, (size_t)cur * sizeof(struct nlist)))
    471  1.17  christos 		    != cur * (ssize_t)sizeof(struct nlist))
    472  1.17  christos 			err(EXIT_FAILURE, "%s: write failed", __func__);
    473   1.5     lukem 	}
    474   1.5     lukem 	/* Write out the string table length... */
    475  1.15   tsutsui 	stringsizebuf = newstringsize;
    476  1.15   tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
    477  1.15   tsutsui 	stringsizebuf = bswap32(stringsizebuf);
    478  1.15   tsutsui #endif
    479  1.15   tsutsui 	if (write(out, &stringsizebuf, sizeof stringsizebuf)
    480  1.17  christos 	    != sizeof stringsizebuf)
    481  1.17  christos 		err(EXIT_FAILURE, "%s: newstringsize: write failed", __func__);
    482   1.5     lukem 	/* Write out the string table... */
    483  1.17  christos 	if (write(out, newstrings, newstringsize) != (ssize_t)newstringsize)
    484  1.17  christos 		err(EXIT_FAILURE, "%s: newstrings: write failed", __func__);
    485  1.17  christos 	free(newstrings);
    486  1.16    martin 	free(oldstrings);
    487   1.1  jonathan }
    488   1.5     lukem 
    489   1.5     lukem void
    490  1.13   tsutsui copy(int out, int in, off_t offset, off_t size)
    491   1.1  jonathan {
    492   1.5     lukem 	char    ibuf[4096];
    493  1.17  christos 	ssize_t remaining, cur, count;
    494   1.1  jonathan 
    495  1.20     skrll 	/* Go to the start of the segment... */
    496  1.17  christos 	if (lseek(in, offset, SEEK_SET) < 0)
    497  1.17  christos 		err(EXIT_FAILURE, "%s: lseek failed", __func__);
    498  1.18    martin 	if (size > SSIZE_MAX)
    499  1.18    martin 		err(EXIT_FAILURE, "%s: can not copy this much", __func__);
    500  1.18    martin 	remaining = (ssize_t)size;
    501   1.5     lukem 	while (remaining) {
    502   1.5     lukem 		cur = remaining;
    503  1.15   tsutsui 		if (cur > (int)sizeof ibuf)
    504   1.5     lukem 			cur = sizeof ibuf;
    505   1.5     lukem 		remaining -= cur;
    506  1.17  christos 		if ((count = read(in, ibuf, (size_t)cur)) != cur) {
    507  1.17  christos 			if (count < 0)
    508  1.17  christos 				err(EXIT_FAILURE, "%s: read error", __func__);
    509  1.17  christos 			else
    510  1.17  christos 				errx(EXIT_FAILURE, "%s: premature end of file",
    511  1.17  christos 					__func__);
    512   1.5     lukem 		}
    513  1.17  christos 		if ((count = write(out, ibuf, (size_t)cur)) != cur)
    514  1.17  christos 			err(EXIT_FAILURE, "%s: write failed", __func__);
    515   1.1  jonathan 	}
    516   1.1  jonathan }
    517   1.1  jonathan /* Combine two segments, which must be contiguous.   If pad is true, it's
    518   1.1  jonathan    okay for there to be padding between. */
    519   1.5     lukem void
    520  1.13   tsutsui combine(struct sect *base, struct sect *new, int pad)
    521   1.1  jonathan {
    522  1.13   tsutsui 
    523  1.13   tsutsui 	if (base->len == 0)
    524   1.5     lukem 		*base = *new;
    525   1.5     lukem 	else
    526   1.5     lukem 		if (new->len) {
    527   1.5     lukem 			if (base->vaddr + base->len != new->vaddr) {
    528   1.5     lukem 				if (pad)
    529   1.5     lukem 					base->len = new->vaddr - base->vaddr;
    530  1.17  christos 				else
    531  1.17  christos 					errx(EXIT_FAILURE, "Non-contiguous "
    532  1.17  christos 					    "data can't be converted");
    533   1.5     lukem 			}
    534   1.5     lukem 			base->len += new->len;
    535   1.5     lukem 		}
    536   1.1  jonathan }
    537   1.1  jonathan 
    538   1.3  jonathan int
    539  1.13   tsutsui phcmp(const void *vh1, const void *vh2)
    540   1.1  jonathan {
    541  1.12    dogcow 	const Elf32_Phdr *h1, *h2;
    542  1.13   tsutsui 
    543  1.13   tsutsui 	h1 = (const Elf32_Phdr *)vh1;
    544  1.13   tsutsui 	h2 = (const Elf32_Phdr *)vh2;
    545   1.5     lukem 
    546   1.5     lukem 	if (h1->p_vaddr > h2->p_vaddr)
    547   1.5     lukem 		return 1;
    548   1.5     lukem 	else
    549   1.5     lukem 		if (h1->p_vaddr < h2->p_vaddr)
    550   1.5     lukem 			return -1;
    551   1.5     lukem 		else
    552   1.5     lukem 			return 0;
    553   1.1  jonathan }
    554   1.1  jonathan 
    555  1.17  christos void *
    556  1.17  christos saveRead(int file, off_t offset, size_t len, const char *name)
    557   1.1  jonathan {
    558   1.5     lukem 	char   *tmp;
    559  1.17  christos 	ssize_t count;
    560   1.5     lukem 	off_t   off;
    561  1.17  christos 
    562  1.17  christos 	if ((off = lseek(file, offset, SEEK_SET)) < 0)
    563  1.17  christos 		errx(EXIT_FAILURE, "%s: seek failed", name);
    564  1.13   tsutsui 	if ((tmp = malloc(len)) == NULL)
    565  1.17  christos 		errx(EXIT_FAILURE,
    566  1.17  christos 		    "%s: Can't allocate %jd bytes.", name, (intmax_t)len);
    567   1.5     lukem 	count = read(file, tmp, len);
    568  1.17  christos 	if ((size_t)count != len) {
    569  1.17  christos 		if (count < 0)
    570  1.17  christos 			err(EXIT_FAILURE, "%s: read error", name);
    571  1.17  christos 		else
    572  1.17  christos 			errx(EXIT_FAILURE, "%s: premature end of file",
    573  1.17  christos 			    name);
    574   1.5     lukem 	}
    575   1.5     lukem 	return tmp;
    576   1.1  jonathan }
    577  1.15   tsutsui 
    578  1.15   tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
    579  1.15   tsutsui /* swap a 32bit region */
    580  1.15   tsutsui void
    581  1.15   tsutsui bswap32_region(int32_t* p, int len)
    582  1.15   tsutsui {
    583  1.15   tsutsui 	size_t i;
    584  1.15   tsutsui 
    585  1.15   tsutsui 	for (i = 0; i < len / sizeof(int32_t); i++, p++)
    586  1.15   tsutsui 		*p = bswap32(*p);
    587  1.15   tsutsui }
    588  1.15   tsutsui #endif
    589