Home | History | Annotate | Line # | Download | only in elf2aout
elf2aout.c revision 1.14
      1  1.14   tsutsui /*	$NetBSD: elf2aout.c,v 1.14 2011/07/10 04:56:31 tsutsui 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.1  jonathan #include <sys/types.h>
     38   1.5     lukem #include <sys/exec_aout.h>
     39   1.5     lukem #include <sys/exec_elf.h>
     40   1.5     lukem 
     41   1.5     lukem #include <a.out.h>
     42   1.5     lukem #include <err.h>
     43   1.5     lukem #include <errno.h>
     44   1.1  jonathan #include <fcntl.h>
     45   1.5     lukem #include <limits.h>
     46   1.1  jonathan #include <stdio.h>
     47   1.5     lukem #include <stdlib.h>
     48   1.1  jonathan #include <string.h>
     49   1.5     lukem #include <unistd.h>
     50   1.1  jonathan 
     51   1.3  jonathan 
     52   1.1  jonathan struct sect {
     53   1.5     lukem 	unsigned long vaddr;
     54   1.5     lukem 	unsigned long len;
     55   1.1  jonathan };
     56   1.1  jonathan 
     57  1.13   tsutsui void	combine(struct sect *, struct sect *, int);
     58  1.13   tsutsui int	phcmp(const void *, const void *);
     59  1.13   tsutsui char   *saveRead(int file, off_t offset, off_t len, const char *name);
     60  1.13   tsutsui void	copy(int, int, off_t, off_t);
     61  1.13   tsutsui void	translate_syms(int, int, off_t, off_t, off_t, off_t);
     62   1.5     lukem 
     63   1.5     lukem int    *symTypeTable;
     64   1.5     lukem 
     65   1.5     lukem int
     66   1.5     lukem main(int argc, char **argv)
     67   1.1  jonathan {
     68   1.5     lukem 	Elf32_Ehdr ex;
     69   1.5     lukem 	Elf32_Phdr *ph;
     70   1.5     lukem 	Elf32_Shdr *sh;
     71   1.5     lukem 	char   *shstrtab;
     72   1.5     lukem 	int     strtabix, symtabix;
     73   1.5     lukem 	int     i;
     74   1.5     lukem 	struct sect text, data, bss;
     75   1.5     lukem 	struct exec aex;
     76   1.5     lukem 	int     infile, outfile;
     77   1.5     lukem 	unsigned long cur_vma = ULONG_MAX;
     78  1.14   tsutsui 	unsigned long mid;
     79   1.5     lukem 	int     symflag = 0;
     80   1.5     lukem 
     81   1.5     lukem 	strtabix = symtabix = 0;
     82   1.5     lukem 	text.len = data.len = bss.len = 0;
     83   1.5     lukem 	text.vaddr = data.vaddr = bss.vaddr = 0;
     84   1.5     lukem 
     85   1.5     lukem 	/* Check args... */
     86   1.5     lukem 	if (argc < 3 || argc > 4) {
     87   1.5     lukem usage:
     88   1.5     lukem 		fprintf(stderr,
     89   1.5     lukem 		    "usage: elf2aout <elf executable> <a.out executable> [-s]\n");
     90   1.5     lukem 		exit(1);
     91   1.5     lukem 	}
     92   1.5     lukem 	if (argc == 4) {
     93   1.5     lukem 		if (strcmp(argv[3], "-s"))
     94   1.5     lukem 			goto usage;
     95   1.5     lukem 		symflag = 1;
     96   1.5     lukem 	}
     97   1.5     lukem 	/* Try the input file... */
     98   1.5     lukem 	if ((infile = open(argv[1], O_RDONLY)) < 0) {
     99   1.5     lukem 		fprintf(stderr, "Can't open %s for read: %s\n",
    100   1.5     lukem 		    argv[1], strerror(errno));
    101   1.5     lukem 		exit(1);
    102   1.5     lukem 	}
    103   1.5     lukem 	/* Read the header, which is at the beginning of the file... */
    104   1.5     lukem 	i = read(infile, &ex, sizeof ex);
    105   1.5     lukem 	if (i != sizeof ex) {
    106   1.5     lukem 		fprintf(stderr, "ex: %s: %s.\n",
    107   1.5     lukem 		    argv[1], i ? strerror(errno) : "End of file reached");
    108   1.5     lukem 		exit(1);
    109   1.5     lukem 	}
    110   1.5     lukem 	/* Read the program headers... */
    111   1.5     lukem 	ph = (Elf32_Phdr *) saveRead(infile, ex.e_phoff,
    112   1.5     lukem 	    ex.e_phnum * sizeof(Elf32_Phdr), "ph");
    113   1.5     lukem 	/* Read the section headers... */
    114   1.5     lukem 	sh = (Elf32_Shdr *) saveRead(infile, ex.e_shoff,
    115   1.5     lukem 	    ex.e_shnum * sizeof(Elf32_Shdr), "sh");
    116   1.5     lukem 	/* Read in the section string table. */
    117   1.5     lukem 	shstrtab = saveRead(infile, sh[ex.e_shstrndx].sh_offset,
    118   1.5     lukem 	    sh[ex.e_shstrndx].sh_size, "shstrtab");
    119   1.5     lukem 
    120   1.5     lukem 	/* Find space for a table matching ELF section indices to a.out symbol
    121   1.5     lukem 	 * types. */
    122  1.13   tsutsui 	symTypeTable = malloc(ex.e_shnum * sizeof(int));
    123  1.13   tsutsui 	if (symTypeTable == NULL) {
    124   1.5     lukem 		fprintf(stderr, "symTypeTable: can't allocate.\n");
    125   1.5     lukem 		exit(1);
    126   1.5     lukem 	}
    127   1.5     lukem 	memset(symTypeTable, 0, ex.e_shnum * sizeof(int));
    128   1.5     lukem 
    129   1.5     lukem 	/* Look for the symbol table and string table... Also map section
    130   1.5     lukem 	 * indices to symbol types for a.out */
    131   1.5     lukem 	for (i = 0; i < ex.e_shnum; i++) {
    132   1.5     lukem 		char   *name = shstrtab + sh[i].sh_name;
    133   1.5     lukem 		if (!strcmp(name, ".symtab"))
    134   1.5     lukem 			symtabix = i;
    135   1.5     lukem 		else
    136   1.5     lukem 			if (!strcmp(name, ".strtab"))
    137   1.5     lukem 				strtabix = i;
    138   1.5     lukem 			else
    139   1.5     lukem 				if (!strcmp(name, ".text") || !strcmp(name, ".rodata"))
    140   1.5     lukem 					symTypeTable[i] = N_TEXT;
    141   1.5     lukem 				else
    142   1.5     lukem 					if (!strcmp(name, ".data") || !strcmp(name, ".sdata") ||
    143   1.5     lukem 					    !strcmp(name, ".lit4") || !strcmp(name, ".lit8"))
    144   1.5     lukem 						symTypeTable[i] = N_DATA;
    145   1.5     lukem 					else
    146   1.5     lukem 						if (!strcmp(name, ".bss") || !strcmp(name, ".sbss"))
    147   1.5     lukem 							symTypeTable[i] = N_BSS;
    148   1.5     lukem 	}
    149   1.5     lukem 
    150   1.5     lukem 	/* Figure out if we can cram the program header into an a.out
    151   1.5     lukem 	 * header... Basically, we can't handle anything but loadable
    152   1.5     lukem 	 * segments, but we can ignore some kinds of segments.   We can't
    153   1.5     lukem 	 * handle holes in the address space, and we handle start addresses
    154   1.5     lukem 	 * other than 0x1000 by hoping that the loader will know where to load
    155   1.5     lukem 	 * - a.out doesn't have an explicit load address.   Segments may be
    156   1.5     lukem 	 * out of order, so we sort them first. */
    157   1.5     lukem 	qsort(ph, ex.e_phnum, sizeof(Elf32_Phdr), phcmp);
    158   1.5     lukem 	for (i = 0; i < ex.e_phnum; i++) {
    159   1.5     lukem 		/* Section types we can ignore... */
    160   1.7  drochner 		if (ph[i].p_type == PT_NULL || ph[i].p_type == PT_NOTE ||
    161   1.7  drochner 		    ph[i].p_type == PT_PHDR || ph[i].p_type == PT_MIPS_REGINFO)
    162   1.5     lukem 			continue;
    163   1.5     lukem 		/* Section types we can't handle... */
    164   1.5     lukem 		else
    165   1.7  drochner 			if (ph[i].p_type != PT_LOAD)
    166   1.5     lukem 				errx(1, "Program header %d type %d can't be converted.", i, ph[i].p_type);
    167   1.5     lukem 		/* Writable (data) segment? */
    168   1.5     lukem 		if (ph[i].p_flags & PF_W) {
    169   1.5     lukem 			struct sect ndata, nbss;
    170   1.5     lukem 
    171   1.5     lukem 			ndata.vaddr = ph[i].p_vaddr;
    172   1.5     lukem 			ndata.len = ph[i].p_filesz;
    173   1.5     lukem 			nbss.vaddr = ph[i].p_vaddr + ph[i].p_filesz;
    174   1.5     lukem 			nbss.len = ph[i].p_memsz - ph[i].p_filesz;
    175   1.5     lukem 
    176   1.5     lukem 			combine(&data, &ndata, 0);
    177   1.5     lukem 			combine(&bss, &nbss, 1);
    178   1.5     lukem 		} else {
    179   1.5     lukem 			struct sect ntxt;
    180   1.5     lukem 
    181   1.5     lukem 			ntxt.vaddr = ph[i].p_vaddr;
    182   1.5     lukem 			ntxt.len = ph[i].p_filesz;
    183   1.5     lukem 
    184   1.5     lukem 			combine(&text, &ntxt, 0);
    185   1.1  jonathan 		}
    186   1.5     lukem 		/* Remember the lowest segment start address. */
    187   1.5     lukem 		if (ph[i].p_vaddr < cur_vma)
    188   1.5     lukem 			cur_vma = ph[i].p_vaddr;
    189   1.5     lukem 	}
    190   1.5     lukem 
    191   1.5     lukem 	/* Sections must be in order to be converted... */
    192   1.5     lukem 	if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr ||
    193   1.5     lukem 	    text.vaddr + text.len > data.vaddr || data.vaddr + data.len > bss.vaddr) {
    194   1.5     lukem 		fprintf(stderr, "Sections ordering prevents a.out conversion.\n");
    195   1.5     lukem 		exit(1);
    196   1.5     lukem 	}
    197   1.5     lukem 	/* If there's a data section but no text section, then the loader
    198   1.5     lukem 	 * combined everything into one section.   That needs to be the text
    199   1.5     lukem 	 * section, so just make the data section zero length following text. */
    200  1.13   tsutsui 	if (data.len && text.len == 0) {
    201   1.5     lukem 		text = data;
    202   1.5     lukem 		data.vaddr = text.vaddr + text.len;
    203   1.5     lukem 		data.len = 0;
    204   1.5     lukem 	}
    205   1.5     lukem 	/* If there is a gap between text and data, we'll fill it when we copy
    206   1.5     lukem 	 * the data, so update the length of the text segment as represented
    207   1.5     lukem 	 * in a.out to reflect that, since a.out doesn't allow gaps in the
    208   1.5     lukem 	 * program address space. */
    209   1.5     lukem 	if (text.vaddr + text.len < data.vaddr)
    210   1.5     lukem 		text.len = data.vaddr - text.vaddr;
    211   1.5     lukem 
    212   1.5     lukem 	/* We now have enough information to cons up an a.out header... */
    213  1.14   tsutsui 	switch (ex.e_machine) {
    214  1.14   tsutsui 	case EM_SPARC:
    215  1.14   tsutsui 		mid = MID_SPARC;
    216  1.14   tsutsui 		break;
    217  1.14   tsutsui 	case EM_386:
    218  1.14   tsutsui 		mid = MID_PC386;
    219  1.14   tsutsui 		break;
    220  1.14   tsutsui 	case EM_68K:
    221  1.14   tsutsui 		mid = MID_M68K;
    222  1.14   tsutsui 		break;
    223  1.14   tsutsui 	case EM_MIPS:
    224  1.14   tsutsui 		if (ex.e_ident[EI_DATA] == ELFDATA2LSB)
    225  1.14   tsutsui 			mid = MID_PMAX;
    226  1.14   tsutsui 		else
    227  1.14   tsutsui 			mid = MID_MIPS;
    228  1.14   tsutsui 		break;
    229  1.14   tsutsui 	case EM_PPC:
    230  1.14   tsutsui 		mid = MID_POWERPC;
    231  1.14   tsutsui 		break;
    232  1.14   tsutsui 	case EM_ARM:
    233  1.14   tsutsui 		mid = MID_ARM6;
    234  1.14   tsutsui 		break;
    235  1.14   tsutsui 	case EM_VAX:
    236  1.14   tsutsui 		mid = MID_VAX;
    237  1.14   tsutsui 		break;
    238  1.14   tsutsui 	case EM_NONE:
    239  1.14   tsutsui 	default:
    240  1.14   tsutsui 		mid = MID_ZERO;
    241  1.14   tsutsui 	}
    242  1.14   tsutsui 	aex.a_midmag = htonl((symflag << 26) | (mid << 16) | OMAGIC);
    243  1.13   tsutsui 
    244   1.5     lukem 	aex.a_text = text.len;
    245   1.5     lukem 	aex.a_data = data.len;
    246   1.5     lukem 	aex.a_bss = bss.len;
    247   1.5     lukem 	aex.a_entry = ex.e_entry;
    248   1.5     lukem 	aex.a_syms = (sizeof(struct nlist) *
    249   1.5     lukem 	    (symtabix != -1
    250   1.5     lukem 		? sh[symtabix].sh_size / sizeof(Elf32_Sym) : 0));
    251   1.5     lukem 	aex.a_trsize = 0;
    252   1.5     lukem 	aex.a_drsize = 0;
    253   1.5     lukem 
    254   1.5     lukem 	/* Make the output file... */
    255   1.5     lukem 	if ((outfile = open(argv[2], O_WRONLY | O_CREAT, 0777)) < 0) {
    256   1.5     lukem 		fprintf(stderr, "Unable to create %s: %s\n", argv[2], strerror(errno));
    257   1.5     lukem 		exit(1);
    258   1.5     lukem 	}
    259   1.6    simonb 	/* Truncate file... */
    260   1.6    simonb 	if (ftruncate(outfile, 0)) {
    261   1.6    simonb 		warn("ftruncate %s", argv[2]);
    262   1.6    simonb 	}
    263   1.5     lukem 	/* Write the header... */
    264   1.5     lukem 	i = write(outfile, &aex, sizeof aex);
    265   1.5     lukem 	if (i != sizeof aex) {
    266   1.5     lukem 		perror("aex: write");
    267   1.5     lukem 		exit(1);
    268   1.5     lukem 	}
    269   1.5     lukem 	/* Copy the loadable sections.   Zero-fill any gaps less than 64k;
    270   1.5     lukem 	 * complain about any zero-filling, and die if we're asked to
    271   1.5     lukem 	 * zero-fill more than 64k. */
    272   1.5     lukem 	for (i = 0; i < ex.e_phnum; i++) {
    273   1.5     lukem 		/* Unprocessable sections were handled above, so just verify
    274   1.5     lukem 		 * that the section can be loaded before copying. */
    275   1.7  drochner 		if (ph[i].p_type == PT_LOAD && ph[i].p_filesz) {
    276   1.5     lukem 			if (cur_vma != ph[i].p_vaddr) {
    277   1.5     lukem 				unsigned long gap = ph[i].p_vaddr - cur_vma;
    278   1.5     lukem 				char    obuf[1024];
    279   1.5     lukem 				if (gap > 65536)
    280   1.5     lukem 					errx(1,
    281   1.5     lukem 			"Intersegment gap (%ld bytes) too large.", (long) gap);
    282   1.6    simonb #ifdef DEBUG
    283   1.5     lukem 				warnx("Warning: %ld byte intersegment gap.",
    284   1.5     lukem 				    (long)gap);
    285   1.6    simonb #endif
    286   1.5     lukem 				memset(obuf, 0, sizeof obuf);
    287   1.5     lukem 				while (gap) {
    288   1.5     lukem 					int     count = write(outfile, obuf, (gap > sizeof obuf
    289   1.5     lukem 						? sizeof obuf : gap));
    290   1.5     lukem 					if (count < 0) {
    291   1.5     lukem 						fprintf(stderr, "Error writing gap: %s\n",
    292   1.5     lukem 						    strerror(errno));
    293   1.5     lukem 						exit(1);
    294   1.5     lukem 					}
    295   1.5     lukem 					gap -= count;
    296   1.5     lukem 				}
    297   1.5     lukem 			}
    298   1.5     lukem 			copy(outfile, infile, ph[i].p_offset, ph[i].p_filesz);
    299   1.5     lukem 			cur_vma = ph[i].p_vaddr + ph[i].p_filesz;
    300   1.1  jonathan 		}
    301   1.5     lukem 	}
    302   1.5     lukem 
    303   1.5     lukem 	/* Copy and translate the symbol table... */
    304   1.5     lukem 	translate_syms(outfile, infile,
    305   1.5     lukem 	    sh[symtabix].sh_offset, sh[symtabix].sh_size,
    306   1.5     lukem 	    sh[strtabix].sh_offset, sh[strtabix].sh_size);
    307   1.1  jonathan 
    308   1.5     lukem 	/* Looks like we won... */
    309   1.5     lukem 	exit(0);
    310   1.1  jonathan }
    311   1.1  jonathan /* translate_syms (out, in, offset, size)
    312   1.1  jonathan 
    313   1.1  jonathan    Read the ELF symbol table from in at offset; translate it into a.out
    314   1.1  jonathan    nlist format and write it to out. */
    315   1.1  jonathan 
    316   1.5     lukem void
    317  1.13   tsutsui translate_syms(int out, int in, off_t symoff, off_t symsize,
    318  1.13   tsutsui     off_t stroff, off_t strsize)
    319   1.1  jonathan {
    320   1.5     lukem #define SYMS_PER_PASS	64
    321   1.5     lukem 	Elf32_Sym inbuf[64];
    322   1.5     lukem 	struct nlist outbuf[64];
    323   1.5     lukem 	int     i, remaining, cur;
    324   1.5     lukem 	char   *oldstrings;
    325   1.5     lukem 	char   *newstrings, *nsp;
    326   1.5     lukem 	int     newstringsize;
    327   1.5     lukem 
    328   1.5     lukem 	/* Zero the unused fields in the output buffer.. */
    329   1.5     lukem 	memset(outbuf, 0, sizeof outbuf);
    330   1.5     lukem 
    331   1.5     lukem 	/* Find number of symbols to process... */
    332   1.5     lukem 	remaining = symsize / sizeof(Elf32_Sym);
    333   1.5     lukem 
    334   1.5     lukem 	/* Suck in the old string table... */
    335   1.5     lukem 	oldstrings = saveRead(in, stroff, strsize, "string table");
    336   1.5     lukem 
    337   1.5     lukem 	/* Allocate space for the new one.   XXX We make the wild assumption
    338   1.5     lukem 	 * that no two symbol table entries will point at the same place in
    339   1.5     lukem 	 * the string table - if that assumption is bad, this could easily
    340   1.5     lukem 	 * blow up. */
    341   1.5     lukem 	newstringsize = strsize + remaining;
    342  1.13   tsutsui 	newstrings = malloc(newstringsize);
    343  1.13   tsutsui 	if (newstrings == NULL) {
    344   1.5     lukem 		fprintf(stderr, "No memory for new string table!\n");
    345   1.5     lukem 		exit(1);
    346   1.5     lukem 	}
    347   1.5     lukem 	/* Initialize the table pointer... */
    348   1.5     lukem 	nsp = newstrings;
    349   1.5     lukem 
    350  1.10     soren 	/* Go the start of the ELF symbol table... */
    351   1.5     lukem 	if (lseek(in, symoff, SEEK_SET) < 0) {
    352   1.5     lukem 		perror("translate_syms: lseek");
    353   1.5     lukem 		exit(1);
    354   1.5     lukem 	}
    355   1.5     lukem 	/* Translate and copy symbols... */
    356   1.5     lukem 	while (remaining) {
    357   1.5     lukem 		cur = remaining;
    358   1.5     lukem 		if (cur > SYMS_PER_PASS)
    359   1.5     lukem 			cur = SYMS_PER_PASS;
    360   1.5     lukem 		remaining -= cur;
    361   1.5     lukem 		if ((i = read(in, inbuf, cur * sizeof(Elf32_Sym)))
    362  1.12    dogcow 		    != cur * (long)sizeof(Elf32_Sym)) {
    363   1.5     lukem 			if (i < 0)
    364   1.5     lukem 				perror("translate_syms");
    365   1.5     lukem 			else
    366   1.5     lukem 				fprintf(stderr, "translate_syms: premature end of file.\n");
    367   1.5     lukem 			exit(1);
    368   1.5     lukem 		}
    369   1.5     lukem 		/* Do the translation... */
    370   1.5     lukem 		for (i = 0; i < cur; i++) {
    371   1.5     lukem 			int     binding, type;
    372   1.5     lukem 
    373   1.5     lukem 			/* Copy the symbol into the new table, but prepend an
    374   1.5     lukem 			 * underscore. */
    375   1.5     lukem 			*nsp = '_';
    376   1.5     lukem 			strcpy(nsp + 1, oldstrings + inbuf[i].st_name);
    377   1.5     lukem 			outbuf[i].n_un.n_strx = nsp - newstrings + 4;
    378   1.5     lukem 			nsp += strlen(nsp) + 1;
    379   1.5     lukem 
    380   1.7  drochner 			type = ELF32_ST_TYPE(inbuf[i].st_info);
    381   1.7  drochner 			binding = ELF32_ST_BIND(inbuf[i].st_info);
    382   1.5     lukem 
    383   1.5     lukem 			/* Convert ELF symbol type/section/etc info into a.out
    384   1.5     lukem 			 * type info. */
    385   1.7  drochner 			if (type == STT_FILE)
    386   1.5     lukem 				outbuf[i].n_type = N_FN;
    387   1.5     lukem 			else
    388   1.7  drochner 				if (inbuf[i].st_shndx == SHN_UNDEF)
    389   1.5     lukem 					outbuf[i].n_type = N_UNDF;
    390   1.5     lukem 				else
    391   1.7  drochner 					if (inbuf[i].st_shndx == SHN_ABS)
    392   1.5     lukem 						outbuf[i].n_type = N_ABS;
    393   1.5     lukem 					else
    394   1.7  drochner 						if (inbuf[i].st_shndx == SHN_COMMON ||
    395   1.7  drochner 						    inbuf[i].st_shndx == SHN_MIPS_ACOMMON)
    396   1.5     lukem 							outbuf[i].n_type = N_COMM;
    397   1.5     lukem 						else
    398   1.5     lukem 							outbuf[i].n_type = symTypeTable[inbuf[i].st_shndx];
    399   1.7  drochner 			if (binding == STB_GLOBAL)
    400   1.5     lukem 				outbuf[i].n_type |= N_EXT;
    401   1.5     lukem 			/* Symbol values in executables should be compatible. */
    402   1.5     lukem 			outbuf[i].n_value = inbuf[i].st_value;
    403   1.5     lukem 		}
    404   1.5     lukem 		/* Write out the symbols... */
    405   1.5     lukem 		if ((i = write(out, outbuf, cur * sizeof(struct nlist)))
    406  1.12    dogcow 		    != cur * (long)sizeof(struct nlist)) {
    407   1.5     lukem 			fprintf(stderr, "translate_syms: write: %s\n", strerror(errno));
    408   1.5     lukem 			exit(1);
    409   1.5     lukem 		}
    410   1.5     lukem 	}
    411   1.5     lukem 	/* Write out the string table length... */
    412   1.5     lukem 	if (write(out, &newstringsize, sizeof newstringsize)
    413   1.5     lukem 	    != sizeof newstringsize) {
    414   1.5     lukem 		fprintf(stderr,
    415   1.5     lukem 		    "translate_syms: newstringsize: %s\n", strerror(errno));
    416   1.5     lukem 		exit(1);
    417   1.5     lukem 	}
    418   1.5     lukem 	/* Write out the string table... */
    419   1.5     lukem 	if (write(out, newstrings, newstringsize) != newstringsize) {
    420   1.5     lukem 		fprintf(stderr, "translate_syms: newstrings: %s\n", strerror(errno));
    421   1.5     lukem 		exit(1);
    422   1.5     lukem 	}
    423   1.1  jonathan }
    424   1.5     lukem 
    425   1.5     lukem void
    426  1.13   tsutsui copy(int out, int in, off_t offset, off_t size)
    427   1.1  jonathan {
    428   1.5     lukem 	char    ibuf[4096];
    429   1.5     lukem 	int     remaining, cur, count;
    430   1.1  jonathan 
    431  1.11    simonb 	/* Go to the start of the ELF symbol table... */
    432   1.5     lukem 	if (lseek(in, offset, SEEK_SET) < 0) {
    433   1.5     lukem 		perror("copy: lseek");
    434   1.5     lukem 		exit(1);
    435   1.5     lukem 	}
    436   1.5     lukem 	remaining = size;
    437   1.5     lukem 	while (remaining) {
    438   1.5     lukem 		cur = remaining;
    439  1.12    dogcow 		if (cur > (long)sizeof ibuf)
    440   1.5     lukem 			cur = sizeof ibuf;
    441   1.5     lukem 		remaining -= cur;
    442   1.5     lukem 		if ((count = read(in, ibuf, cur)) != cur) {
    443   1.5     lukem 			fprintf(stderr, "copy: read: %s\n",
    444   1.5     lukem 			    count ? strerror(errno) : "premature end of file");
    445   1.5     lukem 			exit(1);
    446   1.5     lukem 		}
    447   1.5     lukem 		if ((count = write(out, ibuf, cur)) != cur) {
    448   1.5     lukem 			perror("copy: write");
    449   1.5     lukem 			exit(1);
    450   1.5     lukem 		}
    451   1.1  jonathan 	}
    452   1.1  jonathan }
    453   1.1  jonathan /* Combine two segments, which must be contiguous.   If pad is true, it's
    454   1.1  jonathan    okay for there to be padding between. */
    455   1.5     lukem void
    456  1.13   tsutsui combine(struct sect *base, struct sect *new, int pad)
    457   1.1  jonathan {
    458  1.13   tsutsui 
    459  1.13   tsutsui 	if (base->len == 0)
    460   1.5     lukem 		*base = *new;
    461   1.5     lukem 	else
    462   1.5     lukem 		if (new->len) {
    463   1.5     lukem 			if (base->vaddr + base->len != new->vaddr) {
    464   1.5     lukem 				if (pad)
    465   1.5     lukem 					base->len = new->vaddr - base->vaddr;
    466   1.5     lukem 				else {
    467   1.5     lukem 					fprintf(stderr,
    468   1.5     lukem 					    "Non-contiguous data can't be converted.\n");
    469   1.5     lukem 					exit(1);
    470   1.5     lukem 				}
    471   1.5     lukem 			}
    472   1.5     lukem 			base->len += new->len;
    473   1.5     lukem 		}
    474   1.1  jonathan }
    475   1.1  jonathan 
    476   1.3  jonathan int
    477  1.13   tsutsui phcmp(const void *vh1, const void *vh2)
    478   1.1  jonathan {
    479  1.12    dogcow 	const Elf32_Phdr *h1, *h2;
    480  1.13   tsutsui 
    481  1.13   tsutsui 	h1 = (const Elf32_Phdr *)vh1;
    482  1.13   tsutsui 	h2 = (const Elf32_Phdr *)vh2;
    483   1.5     lukem 
    484   1.5     lukem 	if (h1->p_vaddr > h2->p_vaddr)
    485   1.5     lukem 		return 1;
    486   1.5     lukem 	else
    487   1.5     lukem 		if (h1->p_vaddr < h2->p_vaddr)
    488   1.5     lukem 			return -1;
    489   1.5     lukem 		else
    490   1.5     lukem 			return 0;
    491   1.1  jonathan }
    492   1.1  jonathan 
    493  1.13   tsutsui char *
    494  1.12    dogcow saveRead(int file, off_t offset, off_t len, const char *name)
    495   1.1  jonathan {
    496   1.5     lukem 	char   *tmp;
    497   1.5     lukem 	int     count;
    498   1.5     lukem 	off_t   off;
    499   1.5     lukem 	if ((off = lseek(file, offset, SEEK_SET)) < 0) {
    500   1.5     lukem 		fprintf(stderr, "%s: fseek: %s\n", name, strerror(errno));
    501   1.5     lukem 		exit(1);
    502   1.5     lukem 	}
    503  1.13   tsutsui 	if ((tmp = malloc(len)) == NULL)
    504   1.5     lukem 		errx(1, "%s: Can't allocate %ld bytes.", name, (long)len);
    505   1.5     lukem 	count = read(file, tmp, len);
    506   1.5     lukem 	if (count != len) {
    507   1.5     lukem 		fprintf(stderr, "%s: read: %s.\n",
    508   1.5     lukem 		    name, count ? strerror(errno) : "End of file reached");
    509   1.5     lukem 		exit(1);
    510   1.5     lukem 	}
    511   1.5     lukem 	return tmp;
    512   1.1  jonathan }
    513