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