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