NetBSD/usr.bin/elf2ecoff/elf2ecoff.c

803 lines
23 KiB
C
Raw Normal View History

2011-08-24 00:27:22 +04:00
/* $NetBSD: elf2ecoff.c,v 1.28 2011/08/23 20:27:22 christos Exp $ */
/*
* Copyright (c) 1997 Jonathan Stone
* All rights reserved.
* Copyright (c) 1995
* Ted Lemon (hereinafter referred to as the author)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* elf2ecoff.c
This program converts an elf executable to an ECOFF executable.
No symbol table is retained. This is useful primarily in building
net-bootable kernels for machines (e.g., DECstation and Alpha) which
only support the ECOFF object file format. */
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
2002-04-25 22:16:49 +04:00
#endif
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/exec_elf.h>
#include <stdio.h>
#include <sys/exec_ecoff.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define ISLAST(p) (p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
struct sect {
1997-10-18 17:53:19 +04:00
unsigned long vaddr;
unsigned long len;
};
struct elf_syms {
1997-10-18 17:53:19 +04:00
int nsymbols;
Elf32_Sym *elf_syms;
1997-10-18 17:53:19 +04:00
off_t stringsize;
char *stringtab;
};
struct ecoff_syms {
1997-10-18 17:53:19 +04:00
int nsymbols;
struct ecoff_extsym *ecoff_syms;
1997-10-18 17:53:19 +04:00
off_t stringsize;
char *stringtab;
};
2011-08-24 00:27:22 +04:00
static int debug = 0;
static int needswap;
static int phcmp(Elf32_Phdr *, Elf32_Phdr *);
static char *saveRead(int, off_t, off_t, const char *);
static void safewrite(int, const void *, off_t, const char *);
static void copy(int, int, off_t, off_t);
static void combine(struct sect *, struct sect *, int);
static void translate_syms(struct elf_syms *, struct ecoff_syms *);
static void elf_symbol_table_to_ecoff(int, int, struct ecoff_exechdr *,
off_t, off_t, off_t, off_t);
static int make_ecoff_section_hdrs(struct ecoff_exechdr *,
struct ecoff_scnhdr *);
static void write_ecoff_symhdr(int, struct ecoff_exechdr *,
struct ecoff_symhdr *, long, long, long, long);
static void pad16(int, int, const char *);
static void bswap32_region(int32_t* , int);
static void elf_read_syms(struct elf_syms *, int, off_t, off_t, off_t,
off_t);
int
2011-08-24 00:27:22 +04:00
main(int argc, char **argv)
{
1997-10-18 17:53:19 +04:00
Elf32_Ehdr ex;
Elf32_Phdr *ph;
Elf32_Shdr *sh;
char *shstrtab;
int strtabix, symtabix;
size_t i;
int pad;
1997-10-18 17:53:19 +04:00
struct sect text, data, bss; /* a.out-compatible sections */
struct sect rdata, sdata, sbss; /* ECOFF-only sections */
struct ecoff_exechdr ep;
struct ecoff_scnhdr esecs[6];
struct ecoff_symhdr symhdr;
int infile, outfile;
unsigned long cur_vma = ULONG_MAX;
int symflag = 0;
int nsecs = 0;
int mipsel;
1997-10-18 17:53:19 +04:00
text.len = data.len = bss.len = 0;
text.vaddr = data.vaddr = bss.vaddr = 0;
rdata.len = sdata.len = sbss.len = 0;
rdata.vaddr = sdata.vaddr = sbss.vaddr = 0;
/* Check args... */
if (argc < 3 || argc > 4) {
usage:
fprintf(stderr,
2011-08-24 00:27:22 +04:00
"Usage: %s <elf executable> <ECOFF executable> [-s]\n",
getprogname());
1997-10-18 17:53:19 +04:00
exit(1);
}
1997-10-18 17:53:19 +04:00
if (argc == 4) {
if (strcmp(argv[3], "-s"))
goto usage;
symflag = 1;
}
/* Try the input file... */
2011-08-24 00:27:22 +04:00
if ((infile = open(argv[1], O_RDONLY)) < 0)
err(1, "Can't open %s for read", argv[1]);
1997-10-18 17:53:19 +04:00
/* Read the header, which is at the beginning of the file... */
i = read(infile, &ex, sizeof ex);
2011-08-24 00:27:22 +04:00
if (i != sizeof ex)
err(1, "Short header read from %s", argv[1]);
if (ex.e_ident[EI_DATA] == ELFDATA2LSB)
mipsel = 1;
else if (ex.e_ident[EI_DATA] == ELFDATA2MSB)
mipsel = 0;
2011-08-24 00:27:22 +04:00
else
errx(1, "invalid ELF byte order %d", ex.e_ident[EI_DATA]);
#if BYTE_ORDER == BIG_ENDIAN
if (mipsel)
needswap = 1;
else
needswap = 0;
#elif BYTE_ORDER == LITTLE_ENDIAN
if (mipsel)
needswap = 0;
else
needswap = 1;
#else
#error "unknown endian"
#endif
if (needswap) {
ex.e_type = bswap16(ex.e_type);
ex.e_machine = bswap16(ex.e_machine);
ex.e_version = bswap32(ex.e_version);
ex.e_entry = bswap32(ex.e_entry);
ex.e_phoff = bswap32(ex.e_phoff);
ex.e_shoff = bswap32(ex.e_shoff);
ex.e_flags = bswap32(ex.e_flags);
ex.e_ehsize = bswap16(ex.e_ehsize);
ex.e_phentsize = bswap16(ex.e_phentsize);
ex.e_phnum = bswap16(ex.e_phnum);
ex.e_shentsize = bswap16(ex.e_shentsize);
ex.e_shnum = bswap16(ex.e_shnum);
ex.e_shstrndx = bswap16(ex.e_shstrndx);
}
1997-10-18 17:53:19 +04:00
/* Read the program headers... */
ph = (Elf32_Phdr *) saveRead(infile, ex.e_phoff,
ex.e_phnum * sizeof(Elf32_Phdr), "ph");
if (needswap)
bswap32_region((int32_t*)ph, sizeof(Elf32_Phdr) * ex.e_phnum);
1997-10-18 17:53:19 +04:00
/* Read the section headers... */
sh = (Elf32_Shdr *) saveRead(infile, ex.e_shoff,
ex.e_shnum * sizeof(Elf32_Shdr), "sh");
if (needswap)
bswap32_region((int32_t*)sh, sizeof(Elf32_Shdr) * ex.e_shnum);
1997-10-18 17:53:19 +04:00
/* Read in the section string table. */
shstrtab = saveRead(infile, sh[ex.e_shstrndx].sh_offset,
sh[ex.e_shstrndx].sh_size, "shstrtab");
/* Look for the symbol table and string table... Also map section
* indices to symbol types for a.out */
symtabix = 0;
strtabix = 0;
for (i = 0; i < ex.e_shnum; i++) {
char *name = shstrtab + sh[i].sh_name;
if (!strcmp(name, ".symtab"))
symtabix = i;
else
if (!strcmp(name, ".strtab"))
strtabix = i;
1997-10-18 17:53:19 +04:00
}
2011-08-24 00:27:22 +04:00
/*
* Figure out if we can cram the program header into an ECOFF
1997-10-18 17:53:19 +04:00
* header... Basically, we can't handle anything but loadable
* segments, but we can ignore some kinds of segments. We can't
* handle holes in the address space. Segments may be out of order,
2011-08-24 00:27:22 +04:00
* so we sort them first.
*/
1997-10-18 17:53:19 +04:00
qsort(ph, ex.e_phnum, sizeof(Elf32_Phdr),
(int (*) (const void *, const void *)) phcmp);
for (i = 0; i < ex.e_phnum; i++) {
/* Section types we can ignore... */
if (ph[i].p_type == PT_NULL || ph[i].p_type == PT_NOTE ||
ph[i].p_type == PT_PHDR ||
ph[i].p_type == PT_MIPS_REGINFO) {
1997-10-18 17:53:19 +04:00
if (debug) {
2011-08-24 00:27:22 +04:00
fprintf(stderr, " skipping PH %zu type %d "
"flags 0x%x\n",
1997-10-18 17:53:19 +04:00
i, ph[i].p_type, ph[i].p_flags);
}
continue;
}
/* Section types we can't handle... */
else
2011-08-24 00:27:22 +04:00
if (ph[i].p_type != PT_LOAD)
errx(1, "Program header %zu type %d can't be "
"converted", i, ph[i].p_type);
1997-10-18 17:53:19 +04:00
/* Writable (data) segment? */
if (ph[i].p_flags & PF_W) {
struct sect ndata, nbss;
ndata.vaddr = ph[i].p_vaddr;
ndata.len = ph[i].p_filesz;
nbss.vaddr = ph[i].p_vaddr + ph[i].p_filesz;
nbss.len = ph[i].p_memsz - ph[i].p_filesz;
if (debug) {
2011-08-24 00:27:22 +04:00
fprintf(stderr, " combinining PH %zu 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);
1997-10-18 17:53:19 +04:00
}
combine(&data, &ndata, 0);
combine(&bss, &nbss, 1);
} else {
struct sect ntxt;
ntxt.vaddr = ph[i].p_vaddr;
ntxt.len = ph[i].p_filesz;
if (debug) {
2011-08-24 00:27:22 +04:00
fprintf(stderr, " combinining PH %zu type %d "
"flags 0x%x with text, len = %ld\n",
1997-10-18 17:53:19 +04:00
i, ph[i].p_type, ph[i].p_flags, ntxt.len);
}
combine(&text, &ntxt, 0);
}
/* Remember the lowest segment start address. */
if (ph[i].p_vaddr < cur_vma)
cur_vma = ph[i].p_vaddr;
}
1997-10-18 17:53:19 +04:00
/* Sections must be in order to be converted... */
if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr ||
2011-08-24 00:27:22 +04:00
text.vaddr + text.len > data.vaddr ||
data.vaddr + data.len > bss.vaddr)
errx(1, "Sections ordering prevents a.out conversion");
1997-10-18 17:53:19 +04:00
/* If there's a data section but no text section, then the loader
* combined everything into one section. That needs to be the text
* section, so just make the data section zero length following text. */
2011-06-28 17:13:15 +04:00
if (data.len && text.len == 0) {
1997-10-18 17:53:19 +04:00
text = data;
data.vaddr = text.vaddr + text.len;
data.len = 0;
}
/* If there is a gap between text and data, we'll fill it when we copy
* the data, so update the length of the text segment as represented
* in a.out to reflect that, since a.out doesn't allow gaps in the
* program address space. */
if (text.vaddr + text.len < data.vaddr)
text.len = data.vaddr - text.vaddr;
/* We now have enough information to cons up an a.out header... */
ep.a.magic = ECOFF_OMAGIC;
ep.a.vstamp = 2 * 256 + 10; /* compatible with version 2.10 */
ep.a.tsize = text.len;
ep.a.dsize = data.len;
ep.a.bsize = bss.len;
ep.a.entry = ex.e_entry;
ep.a.text_start = text.vaddr;
ep.a.data_start = data.vaddr;
ep.a.bss_start = bss.vaddr;
ep.a.gprmask = 0xf3fffffe;
memset(&ep.a.cprmask, 0, sizeof ep.a.cprmask);
1997-10-18 17:53:19 +04:00
ep.a.gp_value = 0; /* unused. */
if (mipsel)
ep.f.f_magic = ECOFF_MAGIC_MIPSEL;
else
ep.f.f_magic = ECOFF_MAGIC_MIPSEB;
1997-10-18 17:53:19 +04:00
ep.f.f_nscns = 6;
ep.f.f_timdat = 0; /* bogus */
ep.f.f_symptr = 0;
ep.f.f_nsyms = sizeof(struct ecoff_symhdr);
ep.f.f_opthdr = sizeof ep.a;
ep.f.f_flags = 0x100f; /* Stripped, not sharable. */
memset(esecs, 0, sizeof(esecs));
1997-10-18 17:53:19 +04:00
/* Make ECOFF section headers, with empty stubs for
* .rdata/.sdata/.sbss. */
make_ecoff_section_hdrs(&ep, esecs);
nsecs = ep.f.f_nscns;
if (needswap) {
ep.f.f_magic = bswap16(ep.f.f_magic);
ep.f.f_nscns = bswap16(ep.f.f_nscns);
ep.f.f_timdat = bswap32(ep.f.f_timdat);
ep.f.f_symptr = bswap32(ep.f.f_symptr);
ep.f.f_nsyms = bswap32(ep.f.f_nsyms);
ep.f.f_opthdr = bswap16(ep.f.f_opthdr);
ep.f.f_flags = bswap16(ep.f.f_flags);
ep.a.magic = bswap16(ep.a.magic);
ep.a.vstamp = bswap16(ep.a.vstamp);
ep.a.tsize = bswap32(ep.a.tsize);
ep.a.dsize = bswap32(ep.a.dsize);
ep.a.bsize = bswap32(ep.a.bsize);
ep.a.entry = bswap32(ep.a.entry);
ep.a.text_start = bswap32(ep.a.text_start);
ep.a.data_start = bswap32(ep.a.data_start);
ep.a.bss_start = bswap32(ep.a.bss_start);
ep.a.gprmask = bswap32(ep.a.gprmask);
bswap32_region((int32_t*)ep.a.cprmask, sizeof(ep.a.cprmask));
ep.a.gp_value = bswap32(ep.a.gp_value);
for (i = 0; i < sizeof(esecs) / sizeof(esecs[0]); i++) {
esecs[i].s_paddr = bswap32(esecs[i].s_paddr);
esecs[i].s_vaddr = bswap32(esecs[i].s_vaddr);
esecs[i].s_size = bswap32(esecs[i].s_size);
esecs[i].s_scnptr = bswap32(esecs[i].s_scnptr);
esecs[i].s_relptr = bswap32(esecs[i].s_relptr);
esecs[i].s_lnnoptr = bswap32(esecs[i].s_lnnoptr);
esecs[i].s_nreloc = bswap16(esecs[i].s_nreloc);
esecs[i].s_nlnno = bswap16(esecs[i].s_nlnno);
esecs[i].s_flags = bswap32(esecs[i].s_flags);
}
}
1997-10-18 17:53:19 +04:00
/* Make the output file... */
2011-08-24 00:27:22 +04:00
if ((outfile = open(argv[2], O_WRONLY | O_CREAT, 0777)) < 0)
err(1, "Unable to create %s", argv[2]);
/* Truncate file... */
if (ftruncate(outfile, 0)) {
warn("ftruncate %s", argv[2]);
}
1997-10-18 17:53:19 +04:00
/* Write the headers... */
2011-08-24 00:27:22 +04:00
safewrite(outfile, &ep.f, sizeof(ep.f), "ep.f: write");
if (debug)
2009-12-14 03:43:05 +03:00
fprintf(stderr, "wrote %zu byte file header.\n", sizeof(ep.f));
2011-08-24 00:27:22 +04:00
safewrite(outfile, &ep.a, sizeof(ep.a), "ep.a: write");
if (debug)
2009-12-14 03:43:05 +03:00
fprintf(stderr, "wrote %zu byte a.out header.\n", sizeof(ep.a));
2011-08-24 00:27:22 +04:00
safewrite(outfile, &esecs, sizeof(esecs[0]) * nsecs, "esecs: write");
if (debug)
2009-12-14 03:43:05 +03:00
fprintf(stderr, "wrote %zu bytes of section headers.\n",
sizeof(esecs[0]) * nsecs);
1997-10-18 17:53:19 +04:00
pad = ((sizeof ep.f + sizeof ep.a + sizeof esecs) & 15);
if (pad) {
pad = 16 - pad;
2011-08-24 00:27:22 +04:00
pad16(outfile, pad, "ipad: write");
if (debug)
fprintf(stderr, "wrote %d byte pad.\n", pad);
}
1997-10-18 17:53:19 +04:00
/* Copy the loadable sections. Zero-fill any gaps less than 64k;
* complain about any zero-filling, and die if we're asked to
* zero-fill more than 64k. */
for (i = 0; i < ex.e_phnum; i++) {
/* Unprocessable sections were handled above, so just verify
* that the section can be loaded before copying. */
if (ph[i].p_type == PT_LOAD && ph[i].p_filesz) {
1997-10-18 17:53:19 +04:00
if (cur_vma != ph[i].p_vaddr) {
unsigned long gap = ph[i].p_vaddr - cur_vma;
char obuf[1024];
2011-08-24 00:27:22 +04:00
if (gap > 65536)
errx(1, "Intersegment gap (%ld bytes) "
"too large", gap);
if (debug)
2011-08-24 00:27:22 +04:00
fprintf(stderr, "Warning: %ld byte "
"intersegment gap.\n", gap);
1997-10-18 17:53:19 +04:00
memset(obuf, 0, sizeof obuf);
while (gap) {
2011-08-24 00:27:22 +04:00
int count = write(outfile, obuf,
(gap > sizeof obuf
? sizeof obuf : gap));
if (count < 0)
err(1, "Error writing gap");
1997-10-18 17:53:19 +04:00
gap -= count;
}
}
if (debug)
2011-08-24 00:27:22 +04:00
fprintf(stderr, "writing %d bytes...\n",
ph[i].p_filesz);
1997-10-18 17:53:19 +04:00
copy(outfile, infile, ph[i].p_offset, ph[i].p_filesz);
cur_vma = ph[i].p_vaddr + ph[i].p_filesz;
}
1997-10-18 17:53:19 +04:00
}
if (debug)
fprintf(stderr, "writing syms at offset 0x%lx\n",
(u_long) ep.f.f_symptr + sizeof(symhdr));
/* Copy and translate the symbol table... */
elf_symbol_table_to_ecoff(outfile, infile, &ep,
sh[symtabix].sh_offset, sh[symtabix].sh_size,
sh[strtabix].sh_offset, sh[strtabix].sh_size);
/*
* Write a page of padding for boot PROMS that read entire pages.
* Without this, they may attempt to read past the end of the
* data section, incur an error, and refuse to boot.
*/
{
char obuf[4096];
memset(obuf, 0, sizeof obuf);
2011-08-24 00:27:22 +04:00
if (write(outfile, obuf, sizeof(obuf)) != sizeof(obuf))
err(1, "Error writing PROM padding");
}
1997-10-18 17:53:19 +04:00
/* Looks like we won... */
2011-08-24 00:27:22 +04:00
return 0;
}
2011-08-24 00:27:22 +04:00
static void
2011-06-28 17:13:15 +04:00
copy(int out, int in, off_t offset, off_t size)
{
1997-10-18 17:53:19 +04:00
char ibuf[4096];
size_t remaining, cur, count;
2000-03-14 01:55:05 +03:00
/* Go to the start of the ELF symbol table... */
2011-08-24 00:27:22 +04:00
if (lseek(in, offset, SEEK_SET) < 0)
err(1, "copy: lseek");
1997-10-18 17:53:19 +04:00
remaining = size;
while (remaining) {
cur = remaining;
if (cur > sizeof ibuf)
cur = sizeof ibuf;
remaining -= cur;
2011-08-24 00:27:22 +04:00
if ((count = read(in, ibuf, cur)) != cur)
err(1, "copy: short read");
safewrite(out, ibuf, cur, "copy: write");
1997-10-18 17:53:19 +04:00
}
}
2011-08-24 00:27:22 +04:00
/* Combine two segments, which must be contiguous. If pad is true, it's
okay for there to be padding between. */
2011-08-24 00:27:22 +04:00
static void
2011-06-28 17:13:15 +04:00
combine(struct sect *base, struct sect *new, int pad)
{
2011-06-28 17:13:15 +04:00
if (base->len == 0)
1997-10-18 17:53:19 +04:00
*base = *new;
else
if (new->len) {
if (base->vaddr + base->len != new->vaddr) {
if (pad)
base->len = new->vaddr - base->vaddr;
2011-08-24 00:27:22 +04:00
else
errx(1, "Non-contiguous data can't be "
"converted");
1997-10-18 17:53:19 +04:00
}
base->len += new->len;
}
}
2011-08-24 00:27:22 +04:00
static int
2011-06-28 17:13:15 +04:00
phcmp(Elf32_Phdr *h1, Elf32_Phdr *h2)
{
2011-06-28 17:13:15 +04:00
1997-10-18 17:53:19 +04:00
if (h1->p_vaddr > h2->p_vaddr)
return 1;
else
if (h1->p_vaddr < h2->p_vaddr)
return -1;
else
return 0;
}
2011-08-24 00:27:22 +04:00
static char *
saveRead(int file, off_t offset, off_t len, const char *name)
{
1997-10-18 17:53:19 +04:00
char *tmp;
int count;
off_t off;
2011-06-28 17:13:15 +04:00
2011-08-24 00:27:22 +04:00
if ((off = lseek(file, offset, SEEK_SET)) < 0)
err(1, "%s: fseek", name);
if ((tmp = malloc(len)) == NULL)
err(1, "%s: Can't allocate %ld bytes", name, (long) len);
1997-10-18 17:53:19 +04:00
count = read(file, tmp, len);
2011-08-24 00:27:22 +04:00
if (count != len)
err(1, "%s: short read", name);
1997-10-18 17:53:19 +04:00
return tmp;
}
2011-08-24 00:27:22 +04:00
static void
safewrite(int outfile, const void *buf, off_t len, const char *msg)
{
2011-08-24 00:27:22 +04:00
ssize_t written;
2011-06-28 17:13:15 +04:00
written = write(outfile, buf, len);
2011-08-24 00:27:22 +04:00
if (written != len)
err(1, "%s", msg);
}
/*
* Output only three ECOFF sections, corresponding to ELF psecs
* for text, data, and bss.
*/
2011-08-24 00:27:22 +04:00
static int
2011-06-28 17:13:15 +04:00
make_ecoff_section_hdrs(struct ecoff_exechdr *ep, struct ecoff_scnhdr *esecs)
{
2011-06-28 17:13:15 +04:00
ep->f.f_nscns = 6; /* XXX */
1997-10-18 17:53:19 +04:00
strcpy(esecs[0].s_name, ".text");
strcpy(esecs[1].s_name, ".data");
strcpy(esecs[2].s_name, ".bss");
1997-10-18 17:53:19 +04:00
esecs[0].s_paddr = esecs[0].s_vaddr = ep->a.text_start;
esecs[1].s_paddr = esecs[1].s_vaddr = ep->a.data_start;
esecs[2].s_paddr = esecs[2].s_vaddr = ep->a.bss_start;
esecs[0].s_size = ep->a.tsize;
esecs[1].s_size = ep->a.dsize;
esecs[2].s_size = ep->a.bsize;
1997-10-18 17:53:19 +04:00
esecs[0].s_scnptr = ECOFF_TXTOFF(ep);
esecs[1].s_scnptr = ECOFF_DATOFF(ep);
#if 0
1997-10-18 17:53:19 +04:00
esecs[2].s_scnptr = esecs[1].s_scnptr +
ECOFF_ROUND(esecs[1].s_size, ECOFF_SEGMENT_ALIGNMENT(ep));
#endif
1997-10-18 17:53:19 +04:00
esecs[0].s_relptr = esecs[1].s_relptr = esecs[2].s_relptr = 0;
esecs[0].s_lnnoptr = esecs[1].s_lnnoptr = esecs[2].s_lnnoptr = 0;
esecs[0].s_nreloc = esecs[1].s_nreloc = esecs[2].s_nreloc = 0;
esecs[0].s_nlnno = esecs[1].s_nlnno = esecs[2].s_nlnno = 0;
esecs[1].s_flags = 0x100; /* ECOFF rdata */
esecs[3].s_flags = 0x200; /* ECOFF sdata */
esecs[4].s_flags = 0x400; /* ECOFF sbss */
/*
* Set the symbol-table offset to point at the end of any
* sections we loaded above, so later code can use it to write
* symbol table info..
*/
ep->f.f_symptr = esecs[1].s_scnptr + esecs[1].s_size;
1997-10-18 17:53:19 +04:00
return (ep->f.f_nscns);
}
/*
* Write the ECOFF symbol header.
* Guess at how big the symbol table will be.
* Mark all symbols as EXTERN (for now).
*/
2011-08-24 00:27:22 +04:00
static void
2011-06-28 17:13:15 +04:00
write_ecoff_symhdr(int out, struct ecoff_exechdr *ep,
struct ecoff_symhdr *symhdrp, long nesyms,
long extsymoff, long extstroff, long strsize)
{
2011-06-28 17:13:15 +04:00
if (debug)
2011-08-24 00:27:22 +04:00
fprintf(stderr,
"writing symhdr for %ld entries at offset 0x%lx\n",
1997-10-18 17:53:19 +04:00
nesyms, (u_long) ep->f.f_symptr);
1997-10-18 17:53:19 +04:00
ep->f.f_nsyms = sizeof(struct ecoff_symhdr);
memset(symhdrp, 0, sizeof(*symhdrp));
symhdrp->esymMax = nesyms;
1997-10-18 17:53:19 +04:00
symhdrp->magic = 0x7009;/* XXX */
symhdrp->cbExtOffset = extsymoff;
symhdrp->cbSsExtOffset = extstroff;
symhdrp->issExtMax = strsize;
if (debug)
fprintf(stderr,
2009-12-14 03:43:05 +03:00
"ECOFF symhdr: symhdr %zx, strsize %lx, symsize %lx\n",
sizeof(*symhdrp), strsize,
(nesyms * sizeof(struct ecoff_extsym)));
if (needswap) {
bswap32_region(&symhdrp->ilineMax,
sizeof(*symhdrp) - sizeof(symhdrp->magic) -
sizeof(symhdrp->ilineMax));
symhdrp->magic = bswap16(symhdrp->magic);
symhdrp->ilineMax = bswap16(symhdrp->ilineMax);
}
2011-06-28 17:13:15 +04:00
safewrite(out, symhdrp, sizeof(*symhdrp),
2011-08-24 00:27:22 +04:00
"writing symbol header");
}
2011-08-24 00:27:22 +04:00
static void
2011-06-28 17:13:15 +04:00
elf_read_syms(struct elf_syms *elfsymsp, int in, off_t symoff, off_t symsize,
off_t stroff, off_t strsize)
{
register int nsyms;
int i;
1997-10-18 17:53:19 +04:00
nsyms = symsize / sizeof(Elf32_Sym);
/* Suck in the ELF symbol list... */
elfsymsp->elf_syms = (Elf32_Sym *)
1997-10-18 17:53:19 +04:00
saveRead(in, symoff, nsyms * sizeof(Elf32_Sym),
"ELF symboltable");
elfsymsp->nsymbols = nsyms;
if (needswap) {
for (i = 0; i < nsyms; i++) {
Elf32_Sym *s = &elfsymsp->elf_syms[i];
s->st_name = bswap32(s->st_name);
s->st_value = bswap32(s->st_value);
s->st_size = bswap32(s->st_size);
s->st_shndx = bswap16(s->st_shndx);
}
}
/* Suck in the ELF string table... */
elfsymsp->stringtab = (char *)
1997-10-18 17:53:19 +04:00
saveRead(in, stroff, strsize, "ELF string table");
elfsymsp->stringsize = strsize;
}
2011-08-24 00:27:22 +04:00
static void
2011-06-28 17:13:15 +04:00
elf_symbol_table_to_ecoff(int out, int in, struct ecoff_exechdr *ep,
off_t symoff, off_t symsize, off_t stroff, off_t strsize)
{
struct elf_syms elfsymtab;
struct ecoff_syms ecoffsymtab;
register u_long ecoff_symhdr_off, symtaboff, stringtaboff;
register u_long nextoff, symtabsize, ecoff_strsize;
int nsyms, i;
struct ecoff_symhdr symhdr;
1997-10-18 17:53:19 +04:00
int padding;
/* Read in the ELF symbols. */
elf_read_syms(&elfsymtab, in, symoff, symsize, stroff, strsize);
/* Approximate translation to ECOFF. */
translate_syms(&elfsymtab, &ecoffsymtab);
nsyms = ecoffsymtab.nsymbols;
1997-10-18 17:53:19 +04:00
/* Compute output ECOFF symbol- and string-table offsets. */
ecoff_symhdr_off = ep->f.f_symptr;
nextoff = ecoff_symhdr_off + sizeof(struct ecoff_symhdr);
stringtaboff = nextoff;
ecoff_strsize = ECOFF_ROUND(ecoffsymtab.stringsize,
1997-10-18 17:53:19 +04:00
(ECOFF_SEGMENT_ALIGNMENT(ep)));
nextoff = stringtaboff + ecoff_strsize;
symtaboff = nextoff;
symtabsize = nsyms * sizeof(struct ecoff_extsym);
symtabsize = ECOFF_ROUND(symtabsize, ECOFF_SEGMENT_ALIGNMENT(ep));
/* Write out the symbol header ... */
1997-10-18 17:53:19 +04:00
write_ecoff_symhdr(out, ep, &symhdr, nsyms, symtaboff,
stringtaboff, ecoffsymtab.stringsize);
/* Write out the string table... */
padding = ecoff_strsize - ecoffsymtab.stringsize;
1997-10-18 17:53:19 +04:00
safewrite(out, ecoffsymtab.stringtab, ecoffsymtab.stringsize,
2011-08-24 00:27:22 +04:00
"string table: write");
if (padding)
2011-08-24 00:27:22 +04:00
pad16(out, padding, "string table: padding");
/* Write out the symbol table... */
1997-10-18 17:53:19 +04:00
padding = symtabsize - (nsyms * sizeof(struct ecoff_extsym));
for (i = 0; i < nsyms; i++) {
struct ecoff_extsym *es = &ecoffsymtab.ecoff_syms[i];
es->es_flags = bswap16(es->es_flags);
es->es_ifd = bswap16(es->es_ifd);
bswap32_region(&es->es_strindex,
sizeof(*es) - sizeof(es->es_flags) - sizeof(es->es_ifd));
}
1997-10-18 17:53:19 +04:00
safewrite(out, ecoffsymtab.ecoff_syms,
nsyms * sizeof(struct ecoff_extsym),
2011-08-24 00:27:22 +04:00
"symbol table: write");
if (padding)
2011-08-24 00:27:22 +04:00
pad16(out, padding, "symbols: padding");
}
/*
* In-memory translation of ELF symbosl to ECOFF.
*/
2011-08-24 00:27:22 +04:00
static void
2011-06-28 17:13:15 +04:00
translate_syms(struct elf_syms *elfp, struct ecoff_syms *ecoffp)
{
1997-10-18 17:53:19 +04:00
int i;
char *oldstringbase;
char *newstrings, *nsp;
1997-10-18 17:53:19 +04:00
int nsyms, idx;
1997-10-18 17:53:19 +04:00
nsyms = elfp->nsymbols;
oldstringbase = elfp->stringtab;
1997-10-18 17:53:19 +04:00
/* Allocate space for corresponding ECOFF symbols. */
memset(ecoffp, 0, sizeof(*ecoffp));
1997-10-18 17:53:19 +04:00
ecoffp->nsymbols = 0;
ecoffp->ecoff_syms = malloc(sizeof(struct ecoff_extsym) * nsyms);
1997-10-18 17:53:19 +04:00
/* we are going to be no bigger than the ELF symbol table. */
ecoffp->stringsize = elfp->stringsize;
ecoffp->stringtab = malloc(elfp->stringsize);
1997-10-18 17:53:19 +04:00
newstrings = (char *) ecoffp->stringtab;
nsp = (char *) ecoffp->stringtab;
2011-08-24 00:27:22 +04:00
if (newstrings == NULL)
errx(1, "No memory for new string table");
1997-10-18 17:53:19 +04:00
/* Copy and translate symbols... */
idx = 0;
for (i = 0; i < nsyms; i++) {
int binding, type;
binding = ELF32_ST_BIND((elfp->elf_syms[i].st_info));
type = ELF32_ST_TYPE((elfp->elf_syms[i].st_info));
1997-10-18 17:53:19 +04:00
/* skip strange symbols */
if (binding == 0) {
continue;
}
/* Copy the symbol into the new table */
strcpy(nsp, oldstringbase + elfp->elf_syms[i].st_name);
ecoffp->ecoff_syms[idx].es_strindex = nsp - newstrings;
nsp += strlen(nsp) + 1;
1997-10-18 17:53:19 +04:00
/* translate symbol types to ECOFF XXX */
ecoffp->ecoff_syms[idx].es_type = 1;
ecoffp->ecoff_syms[idx].es_class = 5;
1997-10-18 17:53:19 +04:00
/* Symbol values in executables should be compatible. */
ecoffp->ecoff_syms[idx].es_value = elfp->elf_syms[i].st_value;
ecoffp->ecoff_syms[idx].es_symauxindex = 0xfffff;
1997-10-18 17:53:19 +04:00
idx++;
}
1997-10-18 17:53:19 +04:00
ecoffp->nsymbols = idx;
ecoffp->stringsize = nsp - newstrings;
}
/*
* pad to a 16-byte boundary
*/
2011-08-24 00:27:22 +04:00
static void
pad16(int fd, int size, const char *msg)
{
2011-06-28 17:13:15 +04:00
1997-10-18 17:53:19 +04:00
safewrite(fd, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0", size, msg);
}
/* swap a 32bit region */
2011-08-24 00:27:22 +04:00
static void
bswap32_region(int32_t* p, int len)
{
size_t i;
for (i = 0; i < len / sizeof(int32_t); i++, p++)
*p = bswap32(*p);
}