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