Home | History | Annotate | Line # | Download | only in elf2ecoff
elf2ecoff.c revision 1.10
      1  1.10     perry /*	$NetBSD: elf2ecoff.c,v 1.10 1998/08/10 03:11:07 perry Exp $	*/
      2   1.2  jonathan 
      3   1.1  jonathan /*
      4   1.8  jonathan  * Copyright (c) 1997 Jonathan Stone
      5   1.8  jonathan  *    All rights reserved.
      6   1.1  jonathan  * Copyright (c) 1995
      7   1.1  jonathan  *	Ted Lemon (hereinafter referred to as the author)
      8   1.1  jonathan  *
      9   1.1  jonathan  * Redistribution and use in source and binary forms, with or without
     10   1.1  jonathan  * modification, are permitted provided that the following conditions
     11   1.1  jonathan  * are met:
     12   1.1  jonathan  * 1. Redistributions of source code must retain the above copyright
     13   1.1  jonathan  *    notice, this list of conditions and the following disclaimer.
     14   1.1  jonathan  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1  jonathan  *    notice, this list of conditions and the following disclaimer in the
     16   1.1  jonathan  *    documentation and/or other materials provided with the distribution.
     17   1.1  jonathan  * 3. The name of the author may not be used to endorse or promote products
     18   1.1  jonathan  *    derived from this software without specific prior written permission.
     19   1.1  jonathan  *
     20   1.1  jonathan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
     21   1.1  jonathan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.1  jonathan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.1  jonathan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
     24   1.1  jonathan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.1  jonathan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.1  jonathan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.1  jonathan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.1  jonathan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.1  jonathan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.1  jonathan  * SUCH DAMAGE.
     31   1.1  jonathan  */
     32   1.1  jonathan 
     33   1.1  jonathan /* elf2ecoff.c
     34   1.1  jonathan 
     35   1.1  jonathan    This program converts an elf executable to an ECOFF executable.
     36   1.1  jonathan    No symbol table is retained.   This is useful primarily in building
     37   1.1  jonathan    net-bootable kernels for machines (e.g., DECstation and Alpha) which
     38   1.1  jonathan    only support the ECOFF object file format. */
     39   1.1  jonathan 
     40   1.1  jonathan #include <sys/types.h>
     41   1.1  jonathan #include <fcntl.h>
     42   1.1  jonathan #include <unistd.h>
     43   1.5   thorpej #include <sys/exec.h>
     44   1.3  jonathan #include <sys/exec_elf.h>
     45   1.3  jonathan #include <sys/exec_aout.h>
     46   1.1  jonathan #include <stdio.h>
     47   1.1  jonathan #include <sys/exec_ecoff.h>
     48   1.1  jonathan #include <sys/errno.h>
     49   1.6  jonathan #include <stdlib.h>
     50   1.1  jonathan #include <string.h>
     51   1.1  jonathan #include <limits.h>
     52   1.1  jonathan 
     53   1.3  jonathan 
     54   1.3  jonathan /* Elf Program segment permissions, in program header flags field */
     55   1.3  jonathan 
     56   1.9     lukem #define PF_X            (1 << 0)/* Segment is executable */
     57   1.9     lukem #define PF_W            (1 << 1)/* Segment is writable */
     58   1.9     lukem #define PF_R            (1 << 2)/* Segment is readable */
     59   1.9     lukem #define PF_MASKPROC     0xF0000000	/* Processor-specific reserved bits */
     60   1.3  jonathan 
     61   1.6  jonathan 
     62   1.6  jonathan #define	ISLAST(p)	(p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
     63   1.6  jonathan 
     64   1.1  jonathan struct sect {
     65   1.9     lukem 	unsigned long vaddr;
     66   1.9     lukem 	unsigned long len;
     67   1.1  jonathan };
     68   1.1  jonathan 
     69   1.8  jonathan struct elf_syms {
     70   1.9     lukem 	int     nsymbols;
     71   1.8  jonathan 	Elf32_Sym *elf_syms;
     72   1.9     lukem 	off_t   stringsize;
     73   1.9     lukem 	char   *stringtab;
     74   1.8  jonathan };
     75   1.8  jonathan 
     76   1.8  jonathan struct ecoff_syms {
     77   1.9     lukem 	int     nsymbols;
     78   1.8  jonathan 	struct ecoff_extsym *ecoff_syms;
     79   1.9     lukem 	off_t   stringsize;
     80   1.9     lukem 	char   *stringtab;
     81   1.8  jonathan };
     82   1.8  jonathan 
     83   1.9     lukem int     debug = 0;
     84   1.6  jonathan 
     85   1.9     lukem int     phcmp(Elf32_Phdr * h1, Elf32_Phdr * h2);
     86   1.6  jonathan 
     87   1.8  jonathan 
     88   1.9     lukem char   *saveRead(int file, off_t offset, off_t len, char *name);
     89   1.9     lukem void    safewrite(int outfile, void *buf, off_t len, const char *msg);
     90   1.9     lukem void    copy(int, int, off_t, off_t);
     91   1.9     lukem void    combine(struct sect * base, struct sect * new, int paddable);
     92   1.9     lukem void    translate_syms(struct elf_syms *, struct ecoff_syms *);
     93   1.9     lukem void
     94   1.9     lukem elf_symbol_table_to_ecoff(int out, int in,
     95   1.9     lukem     struct ecoff_exechdr * ep,
     96   1.9     lukem     off_t symoff, off_t symsize,
     97   1.9     lukem     off_t stroff, off_t strsize);
     98   1.9     lukem 
     99   1.9     lukem 
    100   1.9     lukem int
    101   1.9     lukem make_ecoff_section_hdrs(struct ecoff_exechdr * ep,
    102   1.9     lukem     struct ecoff_scnhdr * esecs);
    103   1.9     lukem 
    104   1.9     lukem void
    105   1.9     lukem write_ecoff_symhdr(int outfile, struct ecoff_exechdr * ep,
    106   1.9     lukem     struct ecoff_symhdr * symhdrp,
    107   1.9     lukem     long nesyms, long extsymoff, long extstroff,
    108   1.9     lukem     long strsize);
    109   1.8  jonathan 
    110   1.9     lukem void    pad16(int fd, int size, const char *msg);
    111   1.6  jonathan 
    112   1.1  jonathan extern int errno;
    113   1.9     lukem int    *symTypeTable;
    114   1.1  jonathan 
    115   1.8  jonathan 
    116   1.8  jonathan 
    117   1.8  jonathan 
    118   1.8  jonathan void
    119   1.9     lukem elf_read_syms(struct elf_syms * elfsymsp, int infile,
    120   1.9     lukem     off_t symoff, off_t symsize, off_t stroff, off_t strsize);
    121   1.8  jonathan 
    122   1.8  jonathan 
    123   1.6  jonathan int
    124   1.9     lukem main(int argc, char **argv, char **envp)
    125   1.1  jonathan {
    126   1.9     lukem 	Elf32_Ehdr ex;
    127   1.9     lukem 	Elf32_Phdr *ph;
    128   1.9     lukem 	Elf32_Shdr *sh;
    129   1.9     lukem 	char   *shstrtab;
    130   1.9     lukem 	int     strtabix, symtabix;
    131   1.9     lukem 	int     i, pad;
    132   1.9     lukem 	struct sect text, data, bss;	/* a.out-compatible sections */
    133   1.9     lukem 	struct sect rdata, sdata, sbss;	/* ECOFF-only sections */
    134   1.9     lukem 
    135   1.9     lukem 	struct ecoff_exechdr ep;
    136   1.9     lukem 	struct ecoff_scnhdr esecs[6];
    137   1.9     lukem 	struct ecoff_symhdr symhdr;
    138   1.9     lukem 
    139   1.9     lukem 	int     infile, outfile;
    140   1.9     lukem 	unsigned long cur_vma = ULONG_MAX;
    141   1.9     lukem 	int     symflag = 0;
    142   1.9     lukem 	int     nsecs = 0;
    143   1.9     lukem 
    144   1.9     lukem 	text.len = data.len = bss.len = 0;
    145   1.9     lukem 	text.vaddr = data.vaddr = bss.vaddr = 0;
    146   1.9     lukem 
    147   1.9     lukem 	rdata.len = sdata.len = sbss.len = 0;
    148   1.9     lukem 	rdata.vaddr = sdata.vaddr = sbss.vaddr = 0;
    149   1.9     lukem 
    150   1.9     lukem 	/* Check args... */
    151   1.9     lukem 	if (argc < 3 || argc > 4) {
    152   1.9     lukem usage:
    153   1.9     lukem 		fprintf(stderr,
    154   1.9     lukem 		    "usage: elf2ecoff <elf executable> <ECOFF executable> [-s]\n");
    155   1.9     lukem 		exit(1);
    156   1.9     lukem 	}
    157   1.9     lukem 	if (argc == 4) {
    158   1.9     lukem 		if (strcmp(argv[3], "-s"))
    159   1.9     lukem 			goto usage;
    160   1.9     lukem 		symflag = 1;
    161   1.1  jonathan 	}
    162   1.9     lukem 	/* Try the input file... */
    163   1.9     lukem 	if ((infile = open(argv[1], O_RDONLY)) < 0) {
    164   1.9     lukem 		fprintf(stderr, "Can't open %s for read: %s\n",
    165   1.9     lukem 		    argv[1], strerror(errno));
    166   1.9     lukem 		exit(1);
    167   1.9     lukem 	}
    168   1.9     lukem 	/* Read the header, which is at the beginning of the file... */
    169   1.9     lukem 	i = read(infile, &ex, sizeof ex);
    170   1.9     lukem 	if (i != sizeof ex) {
    171   1.9     lukem 		fprintf(stderr, "ex: %s: %s.\n",
    172   1.9     lukem 		    argv[1], i ? strerror(errno) : "End of file reached");
    173   1.9     lukem 		exit(1);
    174   1.9     lukem 	}
    175   1.9     lukem 	/* Read the program headers... */
    176   1.9     lukem 	ph = (Elf32_Phdr *) saveRead(infile, ex.e_phoff,
    177   1.9     lukem 	    ex.e_phnum * sizeof(Elf32_Phdr), "ph");
    178   1.9     lukem 	/* Read the section headers... */
    179   1.9     lukem 	sh = (Elf32_Shdr *) saveRead(infile, ex.e_shoff,
    180   1.9     lukem 	    ex.e_shnum * sizeof(Elf32_Shdr), "sh");
    181   1.9     lukem 	/* Read in the section string table. */
    182   1.9     lukem 	shstrtab = saveRead(infile, sh[ex.e_shstrndx].sh_offset,
    183   1.9     lukem 	    sh[ex.e_shstrndx].sh_size, "shstrtab");
    184   1.9     lukem 	/* Read in the section string table. */
    185   1.9     lukem 	shstrtab = saveRead(infile, sh[ex.e_shstrndx].sh_offset,
    186   1.9     lukem 	    sh[ex.e_shstrndx].sh_size, "shstrtab");
    187   1.9     lukem 
    188   1.9     lukem 
    189   1.9     lukem 	/* Look for the symbol table and string table... Also map section
    190   1.9     lukem 	 * indices to symbol types for a.out */
    191   1.9     lukem 	symtabix = 0;
    192   1.9     lukem 	strtabix = 0;
    193   1.9     lukem 	for (i = 0; i < ex.e_shnum; i++) {
    194   1.9     lukem 		char   *name = shstrtab + sh[i].sh_name;
    195   1.9     lukem 		if (!strcmp(name, ".symtab"))
    196   1.9     lukem 			symtabix = i;
    197   1.9     lukem 		else
    198   1.9     lukem 			if (!strcmp(name, ".strtab"))
    199   1.9     lukem 				strtabix = i;
    200   1.1  jonathan 
    201   1.9     lukem 	}
    202   1.6  jonathan 
    203   1.9     lukem 	/* Figure out if we can cram the program header into an ECOFF
    204   1.9     lukem 	 * header...  Basically, we can't handle anything but loadable
    205   1.9     lukem 	 * segments, but we can ignore some kinds of segments.  We can't
    206   1.9     lukem 	 * handle holes in the address space.  Segments may be out of order,
    207   1.9     lukem 	 * so we sort them first. */
    208   1.9     lukem 
    209   1.9     lukem 	qsort(ph, ex.e_phnum, sizeof(Elf32_Phdr),
    210   1.9     lukem 	    (int (*) (const void *, const void *)) phcmp);
    211   1.9     lukem 
    212   1.9     lukem 	for (i = 0; i < ex.e_phnum; i++) {
    213   1.9     lukem 		/* Section types we can ignore... */
    214   1.9     lukem 		if (ph[i].p_type == Elf_pt_null || ph[i].p_type == Elf_pt_note ||
    215   1.9     lukem 		    ph[i].p_type == Elf_pt_phdr ||
    216   1.9     lukem 		    ph[i].p_type == Elf_pt_mips_reginfo) {
    217   1.9     lukem 
    218   1.9     lukem 			if (debug) {
    219   1.9     lukem 				fprintf(stderr, "  skipping PH %d type %d flags 0x%x\n",
    220   1.9     lukem 				    i, ph[i].p_type, ph[i].p_flags);
    221   1.9     lukem 			}
    222   1.9     lukem 			continue;
    223   1.9     lukem 		}
    224   1.9     lukem 		/* Section types we can't handle... */
    225   1.9     lukem 		else
    226   1.9     lukem 			if (ph[i].p_type != Elf_pt_load) {
    227   1.9     lukem 				fprintf(stderr, "Program header %d type %d can't be converted.\n",
    228   1.9     lukem 				    i, ph[i].p_type);
    229   1.9     lukem 				exit(1);
    230   1.9     lukem 			}
    231   1.9     lukem 		/* Writable (data) segment? */
    232   1.9     lukem 		if (ph[i].p_flags & PF_W) {
    233   1.9     lukem 			struct sect ndata, nbss;
    234   1.9     lukem 
    235   1.9     lukem 			ndata.vaddr = ph[i].p_vaddr;
    236   1.9     lukem 			ndata.len = ph[i].p_filesz;
    237   1.9     lukem 			nbss.vaddr = ph[i].p_vaddr + ph[i].p_filesz;
    238   1.9     lukem 			nbss.len = ph[i].p_memsz - ph[i].p_filesz;
    239   1.9     lukem 
    240   1.9     lukem 			if (debug) {
    241   1.9     lukem 				fprintf(stderr,
    242   1.9     lukem 				    "  combinining PH %d type %d flags 0x%x with data, ndata = %ld, nbss =%ld\n", i, ph[i].p_type, ph[i].p_flags, ndata.len, nbss.len);
    243   1.9     lukem 			}
    244   1.9     lukem 			combine(&data, &ndata, 0);
    245   1.9     lukem 			combine(&bss, &nbss, 1);
    246   1.9     lukem 		} else {
    247   1.9     lukem 			struct sect ntxt;
    248   1.9     lukem 
    249   1.9     lukem 			ntxt.vaddr = ph[i].p_vaddr;
    250   1.9     lukem 			ntxt.len = ph[i].p_filesz;
    251   1.9     lukem 			if (debug) {
    252   1.9     lukem 
    253   1.9     lukem 				fprintf(stderr,
    254   1.9     lukem 				    "  combinining PH %d type %d flags 0x%x with text, len = %ld\n",
    255   1.9     lukem 				    i, ph[i].p_type, ph[i].p_flags, ntxt.len);
    256   1.9     lukem 			}
    257   1.9     lukem 			combine(&text, &ntxt, 0);
    258   1.9     lukem 		}
    259   1.9     lukem 		/* Remember the lowest segment start address. */
    260   1.9     lukem 		if (ph[i].p_vaddr < cur_vma)
    261   1.9     lukem 			cur_vma = ph[i].p_vaddr;
    262   1.1  jonathan 	}
    263   1.1  jonathan 
    264   1.9     lukem 	/* Sections must be in order to be converted... */
    265   1.9     lukem 	if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr ||
    266   1.9     lukem 	    text.vaddr + text.len > data.vaddr || data.vaddr + data.len > bss.vaddr) {
    267   1.9     lukem 		fprintf(stderr, "Sections ordering prevents a.out conversion.\n");
    268   1.9     lukem 		exit(1);
    269   1.9     lukem 	}
    270   1.9     lukem 	/* If there's a data section but no text section, then the loader
    271   1.9     lukem 	 * combined everything into one section.   That needs to be the text
    272   1.9     lukem 	 * section, so just make the data section zero length following text. */
    273   1.9     lukem 	if (data.len && !text.len) {
    274   1.9     lukem 		text = data;
    275   1.9     lukem 		data.vaddr = text.vaddr + text.len;
    276   1.9     lukem 		data.len = 0;
    277   1.9     lukem 	}
    278   1.9     lukem 	/* If there is a gap between text and data, we'll fill it when we copy
    279   1.9     lukem 	 * the data, so update the length of the text segment as represented
    280   1.9     lukem 	 * in a.out to reflect that, since a.out doesn't allow gaps in the
    281   1.9     lukem 	 * program address space. */
    282   1.9     lukem 	if (text.vaddr + text.len < data.vaddr)
    283   1.9     lukem 		text.len = data.vaddr - text.vaddr;
    284   1.9     lukem 
    285   1.9     lukem 	/* We now have enough information to cons up an a.out header... */
    286   1.9     lukem 	ep.a.magic = ECOFF_OMAGIC;
    287   1.9     lukem 	ep.a.vstamp = 2 * 256 + 10;	/* compatible with version 2.10 */
    288   1.9     lukem 	ep.a.tsize = text.len;
    289   1.9     lukem 	ep.a.dsize = data.len;
    290   1.9     lukem 	ep.a.bsize = bss.len;
    291   1.9     lukem 	ep.a.entry = ex.e_entry;
    292   1.9     lukem 	ep.a.text_start = text.vaddr;
    293   1.9     lukem 	ep.a.data_start = data.vaddr;
    294   1.9     lukem 	ep.a.bss_start = bss.vaddr;
    295   1.9     lukem 	ep.a.gprmask = 0xf3fffffe;
    296  1.10     perry 	memset(&ep.a.cprmask, 0, sizeof ep.a.cprmask);
    297   1.9     lukem 	ep.a.gp_value = 0;	/* unused. */
    298   1.9     lukem 
    299   1.9     lukem 	ep.f.f_magic = ECOFF_MAGIC_MIPSEL;
    300   1.9     lukem 	ep.f.f_nscns = 6;
    301   1.9     lukem 	ep.f.f_timdat = 0;	/* bogus */
    302   1.9     lukem 	ep.f.f_symptr = 0;
    303   1.9     lukem 	ep.f.f_nsyms = sizeof(struct ecoff_symhdr);
    304   1.9     lukem 	ep.f.f_opthdr = sizeof ep.a;
    305   1.9     lukem 	ep.f.f_flags = 0x100f;	/* Stripped, not sharable. */
    306   1.9     lukem 
    307  1.10     perry 	memset(esecs, 0, sizeof(esecs));
    308   1.9     lukem 
    309   1.9     lukem 	/* Make  ECOFF section headers, with empty stubs for
    310   1.9     lukem 	 * .rdata/.sdata/.sbss. */
    311   1.9     lukem 	make_ecoff_section_hdrs(&ep, esecs);
    312   1.9     lukem 
    313   1.9     lukem 	nsecs = ep.f.f_nscns;
    314   1.9     lukem 
    315   1.9     lukem 	/* Make the output file... */
    316   1.9     lukem 	if ((outfile = open(argv[2], O_WRONLY | O_CREAT, 0777)) < 0) {
    317   1.9     lukem 		fprintf(stderr, "Unable to create %s: %s\n", argv[2], strerror(errno));
    318   1.9     lukem 		exit(1);
    319   1.9     lukem 	}
    320   1.9     lukem 	/* Write the headers... */
    321   1.9     lukem 	safewrite(outfile, &ep.f, sizeof(ep.f), "ep.f: write: %s\n");
    322   1.9     lukem 	fprintf(stderr, "wrote %d byte file header.\n", sizeof(ep.f));
    323   1.9     lukem 
    324   1.9     lukem 	safewrite(outfile, &ep.a, sizeof(ep.a), "ep.a: write: %s\n");
    325   1.9     lukem 	fprintf(stderr, "wrote %d byte a.out header.\n", sizeof(ep.a));
    326   1.9     lukem 
    327   1.9     lukem 	safewrite(outfile, &esecs, sizeof(esecs[0]) * nsecs,
    328   1.9     lukem 	    "esecs: write: %s\n");
    329   1.9     lukem 	fprintf(stderr, "wrote %d bytes of section headers.\n",
    330   1.9     lukem 	    sizeof(esecs[0]) * nsecs);
    331   1.9     lukem 
    332   1.9     lukem 
    333   1.9     lukem 	pad = ((sizeof ep.f + sizeof ep.a + sizeof esecs) & 15);
    334   1.9     lukem 	if (pad) {
    335   1.9     lukem 		pad = 16 - pad;
    336   1.9     lukem 		pad16(outfile, pad, "ipad: write: %s\n");
    337   1.9     lukem 		fprintf(stderr, "wrote %d byte pad.\n", pad);
    338   1.9     lukem 	}
    339   1.9     lukem 	/* Copy the loadable sections.   Zero-fill any gaps less than 64k;
    340   1.9     lukem 	 * complain about any zero-filling, and die if we're asked to
    341   1.9     lukem 	 * zero-fill more than 64k. */
    342   1.9     lukem 	for (i = 0; i < ex.e_phnum; i++) {
    343   1.9     lukem 		/* Unprocessable sections were handled above, so just verify
    344   1.9     lukem 		 * that the section can be loaded before copying. */
    345   1.9     lukem 		if (ph[i].p_type == Elf_pt_load && ph[i].p_filesz) {
    346   1.9     lukem 			if (cur_vma != ph[i].p_vaddr) {
    347   1.9     lukem 				unsigned long gap = ph[i].p_vaddr - cur_vma;
    348   1.9     lukem 				char    obuf[1024];
    349   1.9     lukem 				if (gap > 65536) {
    350   1.9     lukem 					fprintf(stderr, "Intersegment gap (%ld bytes) too large.\n",
    351   1.9     lukem 					    gap);
    352   1.9     lukem 					exit(1);
    353   1.9     lukem 				}
    354   1.9     lukem 				fprintf(stderr, "Warning: %ld byte intersegment gap.\n", gap);
    355   1.9     lukem 				memset(obuf, 0, sizeof obuf);
    356   1.9     lukem 				while (gap) {
    357   1.9     lukem 					int     count = write(outfile, obuf, (gap > sizeof obuf
    358   1.9     lukem 						? sizeof obuf : gap));
    359   1.9     lukem 					if (count < 0) {
    360   1.9     lukem 						fprintf(stderr, "Error writing gap: %s\n",
    361   1.9     lukem 						    strerror(errno));
    362   1.9     lukem 						exit(1);
    363   1.9     lukem 					}
    364   1.9     lukem 					gap -= count;
    365   1.9     lukem 				}
    366   1.9     lukem 			}
    367   1.9     lukem 			fprintf(stderr, "writing %d bytes...\n", ph[i].p_filesz);
    368   1.9     lukem 			copy(outfile, infile, ph[i].p_offset, ph[i].p_filesz);
    369   1.9     lukem 			cur_vma = ph[i].p_vaddr + ph[i].p_filesz;
    370   1.9     lukem 		}
    371   1.9     lukem 	}
    372   1.9     lukem 
    373   1.9     lukem 
    374   1.9     lukem 	if (debug)
    375   1.9     lukem 		fprintf(stderr, "writing syms at offset 0x%lx\n",
    376   1.9     lukem 		    (u_long) ep.f.f_symptr + sizeof(symhdr));
    377   1.9     lukem 
    378   1.9     lukem 	/* Copy and translate the symbol table... */
    379   1.9     lukem 	elf_symbol_table_to_ecoff(outfile, infile, &ep,
    380   1.9     lukem 	    sh[symtabix].sh_offset, sh[symtabix].sh_size,
    381   1.9     lukem 	    sh[strtabix].sh_offset, sh[strtabix].sh_size);
    382   1.9     lukem 
    383   1.9     lukem 	/*
    384   1.9     lukem          * Write a page of padding for boot PROMS that read entire pages.
    385   1.9     lukem          * Without this, they may attempt to read past the end of the
    386   1.9     lukem          * data section, incur an error, and refuse to boot.
    387   1.9     lukem          */
    388   1.1  jonathan 	{
    389   1.9     lukem 		char    obuf[4096];
    390   1.9     lukem 		memset(obuf, 0, sizeof obuf);
    391   1.9     lukem 		if (write(outfile, obuf, sizeof(obuf)) != sizeof(obuf)) {
    392   1.9     lukem 			fprintf(stderr, "Error writing PROM padding: %s\n",
    393   1.9     lukem 			    strerror(errno));
    394   1.9     lukem 			exit(1);
    395   1.1  jonathan 		}
    396   1.9     lukem 	}
    397   1.6  jonathan 
    398   1.9     lukem 	/* Looks like we won... */
    399   1.9     lukem 	exit(0);
    400   1.1  jonathan }
    401   1.1  jonathan 
    402   1.6  jonathan void
    403   1.9     lukem copy(out, in, offset, size)
    404   1.9     lukem 	int     out, in;
    405   1.9     lukem 	off_t   offset, size;
    406   1.9     lukem {
    407   1.9     lukem 	char    ibuf[4096];
    408   1.9     lukem 	int     remaining, cur, count;
    409   1.9     lukem 
    410   1.9     lukem 	/* Go the the start of the ELF symbol table... */
    411   1.9     lukem 	if (lseek(in, offset, SEEK_SET) < 0) {
    412   1.9     lukem 		perror("copy: lseek");
    413   1.9     lukem 		exit(1);
    414   1.9     lukem 	}
    415   1.9     lukem 	remaining = size;
    416   1.9     lukem 	while (remaining) {
    417   1.9     lukem 		cur = remaining;
    418   1.9     lukem 		if (cur > sizeof ibuf)
    419   1.9     lukem 			cur = sizeof ibuf;
    420   1.9     lukem 		remaining -= cur;
    421   1.9     lukem 		if ((count = read(in, ibuf, cur)) != cur) {
    422   1.9     lukem 			fprintf(stderr, "copy: read: %s\n",
    423   1.9     lukem 			    count ? strerror(errno) : "premature end of file");
    424   1.9     lukem 			exit(1);
    425   1.9     lukem 		}
    426   1.9     lukem 		safewrite(out, ibuf, cur, "copy: write: %s\n");
    427   1.1  jonathan 	}
    428   1.1  jonathan }
    429   1.1  jonathan /* Combine two segments, which must be contiguous.   If pad is true, it's
    430   1.1  jonathan    okay for there to be padding between. */
    431   1.6  jonathan void
    432   1.9     lukem combine(base, new, pad)
    433   1.9     lukem 	struct sect *base, *new;
    434   1.9     lukem 	int     pad;
    435   1.9     lukem {
    436   1.9     lukem 	if (!base->len)
    437   1.9     lukem 		*base = *new;
    438   1.9     lukem 	else
    439   1.9     lukem 		if (new->len) {
    440   1.9     lukem 			if (base->vaddr + base->len != new->vaddr) {
    441   1.9     lukem 				if (pad)
    442   1.9     lukem 					base->len = new->vaddr - base->vaddr;
    443   1.9     lukem 				else {
    444   1.9     lukem 					fprintf(stderr,
    445   1.9     lukem 					    "Non-contiguous data can't be converted.\n");
    446   1.9     lukem 					exit(1);
    447   1.9     lukem 				}
    448   1.9     lukem 			}
    449   1.9     lukem 			base->len += new->len;
    450   1.9     lukem 		}
    451   1.1  jonathan }
    452   1.1  jonathan 
    453   1.3  jonathan int
    454   1.9     lukem phcmp(h1, h2)
    455   1.9     lukem 	Elf32_Phdr *h1, *h2;
    456   1.1  jonathan {
    457   1.9     lukem 	if (h1->p_vaddr > h2->p_vaddr)
    458   1.9     lukem 		return 1;
    459   1.9     lukem 	else
    460   1.9     lukem 		if (h1->p_vaddr < h2->p_vaddr)
    461   1.9     lukem 			return -1;
    462   1.9     lukem 		else
    463   1.9     lukem 			return 0;
    464   1.1  jonathan }
    465   1.1  jonathan 
    466   1.8  jonathan char
    467   1.9     lukem        *
    468   1.9     lukem saveRead(int file, off_t offset, off_t len, char *name)
    469   1.1  jonathan {
    470   1.9     lukem 	char   *tmp;
    471   1.9     lukem 	int     count;
    472   1.9     lukem 	off_t   off;
    473   1.9     lukem 	if ((off = lseek(file, offset, SEEK_SET)) < 0) {
    474   1.9     lukem 		fprintf(stderr, "%s: fseek: %s\n", name, strerror(errno));
    475   1.9     lukem 		exit(1);
    476   1.9     lukem 	}
    477   1.9     lukem 	if (!(tmp = (char *) malloc(len))) {
    478   1.9     lukem 		fprintf(stderr, "%s: Can't allocate %ld bytes.\n", name, (long) len);
    479   1.9     lukem 		exit(1);
    480   1.9     lukem 	}
    481   1.9     lukem 	count = read(file, tmp, len);
    482   1.9     lukem 	if (count != len) {
    483   1.9     lukem 		fprintf(stderr, "%s: read: %s.\n",
    484   1.9     lukem 		    name, count ? strerror(errno) : "End of file reached");
    485   1.9     lukem 		exit(1);
    486   1.9     lukem 	}
    487   1.9     lukem 	return tmp;
    488   1.6  jonathan }
    489   1.6  jonathan 
    490   1.8  jonathan void
    491   1.9     lukem safewrite(int outfile, void *buf, off_t len, const char *msg)
    492   1.8  jonathan {
    493   1.9     lukem 	int     written;
    494   1.9     lukem 	written = write(outfile, (char *) buf, len);
    495   1.8  jonathan 	if (written != len) {
    496   1.9     lukem 		fprintf(stderr, msg, strerror(errno));
    497   1.9     lukem 		exit(1);
    498   1.8  jonathan 	}
    499   1.8  jonathan }
    500   1.8  jonathan 
    501   1.6  jonathan 
    502   1.8  jonathan /*
    503   1.8  jonathan  * Output only three ECOFF sections, corresponding to ELF psecs
    504   1.8  jonathan  * for text, data, and bss.
    505   1.6  jonathan  */
    506   1.6  jonathan int
    507   1.6  jonathan make_ecoff_section_hdrs(ep, esecs)
    508   1.6  jonathan 	struct ecoff_exechdr *ep;
    509   1.6  jonathan 	struct ecoff_scnhdr *esecs;
    510   1.6  jonathan 
    511   1.6  jonathan {
    512   1.8  jonathan 	ep->f.f_nscns = 6;	/* XXX */
    513   1.6  jonathan 
    514   1.9     lukem 	strcpy(esecs[0].s_name, ".text");
    515   1.9     lukem 	strcpy(esecs[1].s_name, ".data");
    516   1.9     lukem 	strcpy(esecs[2].s_name, ".bss");
    517   1.9     lukem 
    518   1.9     lukem 	esecs[0].s_paddr = esecs[0].s_vaddr = ep->a.text_start;
    519   1.9     lukem 	esecs[1].s_paddr = esecs[1].s_vaddr = ep->a.data_start;
    520   1.9     lukem 	esecs[2].s_paddr = esecs[2].s_vaddr = ep->a.bss_start;
    521   1.9     lukem 	esecs[0].s_size = ep->a.tsize;
    522   1.9     lukem 	esecs[1].s_size = ep->a.dsize;
    523   1.9     lukem 	esecs[2].s_size = ep->a.bsize;
    524   1.6  jonathan 
    525   1.9     lukem 	esecs[0].s_scnptr = ECOFF_TXTOFF(ep);
    526   1.9     lukem 	esecs[1].s_scnptr = ECOFF_DATOFF(ep);
    527   1.6  jonathan #if 0
    528   1.9     lukem 	esecs[2].s_scnptr = esecs[1].s_scnptr +
    529   1.9     lukem 	    ECOFF_ROUND(esecs[1].s_size, ECOFF_SEGMENT_ALIGNMENT(ep));
    530   1.6  jonathan #endif
    531   1.6  jonathan 
    532   1.9     lukem 	esecs[0].s_relptr = esecs[1].s_relptr = esecs[2].s_relptr = 0;
    533   1.9     lukem 	esecs[0].s_lnnoptr = esecs[1].s_lnnoptr = esecs[2].s_lnnoptr = 0;
    534   1.9     lukem 	esecs[0].s_nreloc = esecs[1].s_nreloc = esecs[2].s_nreloc = 0;
    535   1.9     lukem 	esecs[0].s_nlnno = esecs[1].s_nlnno = esecs[2].s_nlnno = 0;
    536   1.8  jonathan 
    537   1.8  jonathan 	esecs[1].s_flags = 0x100;	/* ECOFF rdata */
    538   1.8  jonathan 	esecs[3].s_flags = 0x200;	/* ECOFF sdata */
    539   1.8  jonathan 	esecs[4].s_flags = 0x400;	/* ECOFF sbss */
    540   1.8  jonathan 
    541   1.8  jonathan 	/*
    542   1.8  jonathan 	 * Set the symbol-table offset  to point at the end of any
    543   1.8  jonathan 	 * sections we loaded above, so later code can use it to write
    544   1.8  jonathan 	 * symbol table info..
    545   1.8  jonathan 	 */
    546   1.8  jonathan 	ep->f.f_symptr = esecs[1].s_scnptr + esecs[1].s_size;
    547   1.9     lukem 	return (ep->f.f_nscns);
    548   1.8  jonathan }
    549   1.8  jonathan 
    550   1.8  jonathan 
    551   1.8  jonathan /*
    552   1.8  jonathan  * Write the ECOFF symbol header.
    553   1.8  jonathan  * Guess at how big the symbol table will be.
    554   1.8  jonathan  * Mark all symbols as EXTERN (for now).
    555   1.8  jonathan  */
    556   1.8  jonathan void
    557   1.8  jonathan write_ecoff_symhdr(out, ep, symhdrp, nesyms, extsymoff, extstroff, strsize)
    558   1.9     lukem 	int     out;
    559   1.8  jonathan 	struct ecoff_exechdr *ep;
    560   1.8  jonathan 	struct ecoff_symhdr *symhdrp;
    561   1.9     lukem 	long    nesyms, extsymoff, extstroff, strsize;
    562   1.8  jonathan {
    563   1.8  jonathan 	if (debug)
    564   1.9     lukem 		fprintf(stderr, "writing symhdr for %ld entries at offset 0x%lx\n",
    565   1.9     lukem 		    nesyms, (u_long) ep->f.f_symptr);
    566   1.8  jonathan 
    567   1.9     lukem 	ep->f.f_nsyms = sizeof(struct ecoff_symhdr);
    568   1.8  jonathan 
    569  1.10     perry 	memset(symhdrp, 0, sizeof(*symhdrp));
    570   1.8  jonathan 	symhdrp->esymMax = nesyms;
    571   1.9     lukem 	symhdrp->magic = 0x7009;/* XXX */
    572   1.8  jonathan 	symhdrp->cbExtOffset = extsymoff;
    573   1.8  jonathan 	symhdrp->cbSsExtOffset = extstroff;
    574   1.8  jonathan 
    575   1.8  jonathan 	symhdrp->issExtMax = strsize;
    576   1.8  jonathan 	if (debug)
    577   1.8  jonathan 		fprintf(stderr,
    578   1.8  jonathan 		    "ECOFF symhdr: symhdr %x, strsize %lx, symsize %lx\n",
    579   1.8  jonathan 		    sizeof(*symhdrp), strsize,
    580   1.8  jonathan 		    (nesyms * sizeof(struct ecoff_extsym)));
    581   1.8  jonathan 
    582   1.8  jonathan 	safewrite(out, symhdrp, sizeof(*symhdrp),
    583   1.8  jonathan 	    "writing symbol header: %s\n");
    584   1.8  jonathan }
    585   1.8  jonathan 
    586   1.8  jonathan 
    587   1.8  jonathan void
    588   1.8  jonathan elf_read_syms(elfsymsp, in, symoff, symsize, stroff, strsize)
    589   1.8  jonathan 	struct elf_syms *elfsymsp;
    590   1.9     lukem 	int     in;
    591   1.9     lukem 	off_t   symoff, symsize;
    592   1.9     lukem 	off_t   stroff, strsize;
    593   1.8  jonathan {
    594   1.8  jonathan 	register int nsyms;
    595   1.9     lukem 	nsyms = symsize / sizeof(Elf32_Sym);
    596   1.8  jonathan 
    597   1.8  jonathan 	/* Suck in the ELF symbol list... */
    598   1.8  jonathan 	elfsymsp->elf_syms = (Elf32_Sym *)
    599   1.9     lukem 	    saveRead(in, symoff, nsyms * sizeof(Elf32_Sym),
    600   1.9     lukem 	    "ELF symboltable");
    601   1.8  jonathan 	elfsymsp->nsymbols = nsyms;
    602   1.8  jonathan 
    603   1.8  jonathan 	/* Suck in the ELF string table... */
    604   1.8  jonathan 	elfsymsp->stringtab = (char *)
    605   1.9     lukem 	    saveRead(in, stroff, strsize, "ELF string table");
    606   1.8  jonathan 	elfsymsp->stringsize = strsize;
    607   1.8  jonathan }
    608   1.8  jonathan 
    609   1.8  jonathan 
    610   1.8  jonathan /*
    611   1.9     lukem  *
    612   1.8  jonathan  */
    613   1.8  jonathan void
    614   1.8  jonathan elf_symbol_table_to_ecoff(out, in, ep, symoff, symsize, stroff, strsize)
    615   1.9     lukem 	int     out, in;
    616   1.8  jonathan 	struct ecoff_exechdr *ep;
    617   1.9     lukem 	off_t   symoff, symsize;
    618   1.9     lukem 	off_t   stroff, strsize;
    619   1.8  jonathan {
    620   1.8  jonathan 
    621   1.8  jonathan 	struct elf_syms elfsymtab;
    622   1.8  jonathan 	struct ecoff_syms ecoffsymtab;
    623   1.8  jonathan 	register u_long ecoff_symhdr_off, symtaboff, stringtaboff;
    624   1.8  jonathan 	register u_long nextoff, symtabsize, ecoff_strsize;
    625   1.9     lukem 	int     nsyms;
    626   1.8  jonathan 	struct ecoff_symhdr symhdr;
    627   1.9     lukem 	int     padding;
    628   1.9     lukem 
    629   1.8  jonathan 	/* Read in the ELF symbols. */
    630   1.8  jonathan 	elf_read_syms(&elfsymtab, in, symoff, symsize, stroff, strsize);
    631   1.8  jonathan 
    632   1.8  jonathan 	/* Approximate translation to ECOFF. */
    633   1.8  jonathan 	translate_syms(&elfsymtab, &ecoffsymtab);
    634   1.8  jonathan 	nsyms = ecoffsymtab.nsymbols;
    635   1.8  jonathan 
    636   1.9     lukem 	/* Compute output ECOFF symbol- and string-table offsets. */
    637   1.8  jonathan 	ecoff_symhdr_off = ep->f.f_symptr;
    638   1.8  jonathan 
    639   1.8  jonathan 	nextoff = ecoff_symhdr_off + sizeof(struct ecoff_symhdr);
    640   1.8  jonathan 	stringtaboff = nextoff;
    641   1.8  jonathan 	ecoff_strsize = ECOFF_ROUND(ecoffsymtab.stringsize,
    642   1.9     lukem 	    (ECOFF_SEGMENT_ALIGNMENT(ep)));
    643   1.8  jonathan 
    644   1.8  jonathan 
    645   1.8  jonathan 	nextoff = stringtaboff + ecoff_strsize;
    646   1.8  jonathan 	symtaboff = nextoff;
    647   1.8  jonathan 	symtabsize = nsyms * sizeof(struct ecoff_extsym);
    648   1.8  jonathan 	symtabsize = ECOFF_ROUND(symtabsize, ECOFF_SEGMENT_ALIGNMENT(ep));
    649   1.8  jonathan 
    650   1.8  jonathan 	/* Write out the symbol header ... */
    651   1.9     lukem 	write_ecoff_symhdr(out, ep, &symhdr, nsyms, symtaboff,
    652   1.9     lukem 	    stringtaboff, ecoffsymtab.stringsize);
    653   1.8  jonathan 
    654   1.8  jonathan 	/* Write out the string table... */
    655   1.8  jonathan 	padding = ecoff_strsize - ecoffsymtab.stringsize;
    656   1.9     lukem 	safewrite(out, ecoffsymtab.stringtab, ecoffsymtab.stringsize,
    657   1.9     lukem 	    "string table: write: %s\n");
    658   1.8  jonathan 	if (padding)
    659   1.8  jonathan 		pad16(out, padding, "string table: padding: %s\n");
    660   1.8  jonathan 
    661   1.8  jonathan 
    662   1.8  jonathan 	/* Write out the symbol table... */
    663   1.9     lukem 	padding = symtabsize - (nsyms * sizeof(struct ecoff_extsym));
    664   1.9     lukem 	safewrite(out, ecoffsymtab.ecoff_syms,
    665   1.8  jonathan 	    nsyms * sizeof(struct ecoff_extsym),
    666   1.8  jonathan 	    "symbol table: write: %s\n");
    667   1.8  jonathan 	if (padding)
    668   1.8  jonathan 		pad16(out, padding, "symbols: padding: %s\n");
    669   1.8  jonathan }
    670   1.8  jonathan 
    671   1.8  jonathan 
    672   1.8  jonathan 
    673   1.8  jonathan /*
    674   1.8  jonathan  * In-memory translation of ELF symbosl to ECOFF.
    675   1.8  jonathan  */
    676   1.8  jonathan void
    677   1.9     lukem translate_syms(elfp, ecoffp)
    678   1.8  jonathan 	struct elf_syms *elfp;
    679   1.8  jonathan 	struct ecoff_syms *ecoffp;
    680   1.8  jonathan {
    681   1.6  jonathan 
    682   1.9     lukem 	int     i;
    683   1.9     lukem 	char   *oldstringbase;
    684   1.9     lukem 	char   *newstrings, *nsp;
    685   1.9     lukem 
    686   1.9     lukem 	int     nsyms, idx;
    687   1.9     lukem 
    688   1.9     lukem 	nsyms = elfp->nsymbols;
    689   1.9     lukem 	oldstringbase = elfp->stringtab;
    690   1.9     lukem 
    691   1.9     lukem 	/* Allocate space for corresponding ECOFF symbols. */
    692  1.10     perry 	memset(ecoffp, 0, sizeof(*ecoffp));
    693   1.9     lukem 
    694   1.9     lukem 	ecoffp->nsymbols = 0;
    695   1.9     lukem 	ecoffp->ecoff_syms = malloc(sizeof(struct ecoff_extsym) * nsyms);
    696   1.9     lukem 
    697   1.9     lukem 	/* we are going to be no bigger than the ELF symbol table. */
    698   1.9     lukem 	ecoffp->stringsize = elfp->stringsize;
    699   1.9     lukem 	ecoffp->stringtab = malloc(elfp->stringsize);
    700   1.9     lukem 
    701   1.9     lukem 	newstrings = (char *) ecoffp->stringtab;
    702   1.9     lukem 	nsp = (char *) ecoffp->stringtab;
    703   1.9     lukem 	if (!newstrings) {
    704   1.9     lukem 		fprintf(stderr, "No memory for new string table!\n");
    705   1.9     lukem 		exit(1);
    706   1.9     lukem 	}
    707   1.9     lukem 	/* Copy and translate  symbols... */
    708   1.9     lukem 	idx = 0;
    709   1.9     lukem 	for (i = 0; i < nsyms; i++) {
    710   1.9     lukem 		int     binding, type;
    711   1.9     lukem 
    712   1.9     lukem 		binding = ELF_SYM_BIND((elfp->elf_syms[i].st_info));
    713   1.9     lukem 		type = ELF_SYM_TYPE((elfp->elf_syms[i].st_info));
    714   1.9     lukem 
    715   1.9     lukem 		/* skip strange symbols */
    716   1.9     lukem 		if (binding == 0) {
    717   1.9     lukem 			continue;
    718   1.9     lukem 		}
    719   1.9     lukem 		/* Copy the symbol into the new table */
    720   1.9     lukem 		strcpy(nsp, oldstringbase + elfp->elf_syms[i].st_name);
    721   1.9     lukem 		ecoffp->ecoff_syms[idx].es_strindex = nsp - newstrings;
    722   1.9     lukem 		nsp += strlen(nsp) + 1;
    723   1.9     lukem 
    724   1.9     lukem 		/* translate symbol types to ECOFF XXX */
    725   1.9     lukem 		ecoffp->ecoff_syms[idx].es_type = 1;
    726   1.9     lukem 		ecoffp->ecoff_syms[idx].es_class = 5;
    727   1.9     lukem 
    728   1.9     lukem 		/* Symbol values in executables should be compatible. */
    729   1.9     lukem 		ecoffp->ecoff_syms[idx].es_value = elfp->elf_syms[i].st_value;
    730   1.9     lukem 		ecoffp->ecoff_syms[idx].es_symauxindex = 0xfffff;
    731   1.8  jonathan 
    732   1.9     lukem 		idx++;
    733   1.8  jonathan 	}
    734   1.8  jonathan 
    735   1.9     lukem 	ecoffp->nsymbols = idx;
    736   1.9     lukem 	ecoffp->stringsize = nsp - newstrings;
    737   1.8  jonathan }
    738   1.8  jonathan /*
    739   1.8  jonathan  * pad to a 16-byte boundary
    740   1.8  jonathan  */
    741   1.8  jonathan void
    742   1.8  jonathan pad16(int fd, int size, const char *msg)
    743   1.8  jonathan {
    744   1.9     lukem 	safewrite(fd, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0", size, msg);
    745   1.1  jonathan }
    746