/*
* 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. */
/* 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);
/* 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);
/* 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;
}
/*
* Figure out if we can cram the program header into an ECOFF
* 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,
* so we sort them first.
*/
for (i = 0; i < ex.e_phnum; i++) {
switch (ph[i].p_type) {
case PT_NOTE:
case PT_NULL:
case PT_PHDR:
case PT_MIPS_ABIFLAGS:
case PT_MIPS_REGINFO:
/* Section types we can ignore... */
if (debug) {
fprintf(stderr, " skipping PH %zu type %#x "
"flags %#x\n",
i, ph[i].p_type, ph[i].p_flags);
}
continue;
default:
/* Section types we can't handle... */
if (ph[i].p_type != PT_LOAD)
errx(1, "Program header %zu type %#x can't be "
"converted", i, ph[i].p_type);
}
/* Writable (data) segment? */
if (ph[i].p_flags & PF_W) {
struct sect ndata, nbss;
if (debug) {
fprintf(stderr, " combining PH %zu type %d "
"flags %#x with data, ndata = %d, "
"nbss =%d\n", i, ph[i].p_type,
ph[i].p_flags, ndata.len, nbss.len);
}
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) {
fprintf(stderr, " combining PH %zu type %d "
"flags %#x with text, len = %d\n",
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;
}
/* Sections must be in order to be converted... */
if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr ||
text.vaddr + text.len > data.vaddr ||
data.vaddr + data.len > bss.vaddr)
errx(1, "Sections ordering prevents a.out conversion");
/* 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. */
if (data.len && text.len == 0) {
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);
ep.a.gp_value = 0; /* unused. */
if (mipsel)
ep.f.f_magic = ECOFF_MAGIC_MIPSEL;
else
ep.f.f_magic = ECOFF_MAGIC_MIPSEB;
safewrite(outfile, &esecs, sizeof(esecs[0]) * nsecs, "esecs: write");
if (debug)
fprintf(stderr, "wrote %zu bytes of section headers.\n",
sizeof(esecs[0]) * nsecs);
pad = ((sizeof ep.f + sizeof ep.a + sizeof esecs) & 15);
if (pad) {
pad = 16 - pad;
pad16(outfile, pad, "ipad: write");
if (debug)
fprintf(stderr, "wrote %d byte pad.\n", pad);
}
/* 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) {
if (cur_vma != ph[i].p_vaddr) {
uint32_t gap = ph[i].p_vaddr - cur_vma;
char obuf[1024];
if (gap > 65536)
errx(1, "Intersegment gap (%d bytes) "
"too large", gap);
if (debug)
fprintf(stderr, "Warning: %d byte "
"intersegment gap.\n", gap);
memset(obuf, 0, sizeof obuf);
while (gap) {
int count = write(outfile, obuf,
(gap > sizeof obuf
? sizeof obuf : gap));
if (count < 0)
err(1, "Error writing gap");
gap -= count;
}
}
if (debug)
fprintf(stderr, "writing %d bytes...\n",
ph[i].p_filesz);
copy(outfile, infile, ph[i].p_offset, ph[i].p_filesz);
cur_vma = ph[i].p_vaddr + ph[i].p_filesz;
}
}
if (debug)
fprintf(stderr, "writing syms at offset %#x\n",
(uint32_t)(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);
if (write(outfile, obuf, sizeof(obuf)) != sizeof(obuf))
err(1, "Error writing PROM padding");
}
/* Looks like we won... */
return 0;
}
static void
copy(int out, int in, off_t offset, off_t size)
{
char ibuf[4096];
size_t remaining, cur, count;
/* Go to the start of the ELF symbol table... */
if (lseek(in, offset, SEEK_SET) < 0)
err(1, "copy: lseek");
remaining = size;
while (remaining) {
cur = remaining;
if (cur > sizeof ibuf)
cur = sizeof ibuf;
remaining -= cur;
if ((count = read(in, ibuf, cur)) != cur)
err(1, "copy: short read");
safewrite(out, ibuf, cur, "copy: write");
}
}
/* Combine two segments, which must be contiguous. If pad is true, it's
okay for there to be padding between. */
static void
combine(struct sect *base, struct sect *new, int pad)
{
if (base->len == 0)
*base = *new;
else
if (new->len) {
if (base->vaddr + base->len != new->vaddr) {
if (pad)
base->len = new->vaddr - base->vaddr;
else
errx(1, "Non-contiguous data can't be "
"converted");
}
base->len += new->len;
}
}
static int
phcmp(Elf32_Phdr *h1, Elf32_Phdr *h2)
{
if (h1->p_vaddr > h2->p_vaddr)
return 1;
else
if (h1->p_vaddr < h2->p_vaddr)
return -1;
else
return 0;
}
written = write(outfile, buf, len);
if (written != len)
err(1, "%s", msg);
}
/*
* Output only three ECOFF sections, corresponding to ELF psecs
* for text, data, and bss.
*/
static int
make_ecoff_section_hdrs(struct ecoff32_exechdr *ep, struct ecoff32_scnhdr *esecs)
{
/*
* 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;
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).
*/
static void
write_ecoff_symhdr(int out, struct ecoff32_exechdr *ep,
struct ecoff32_symhdr *symhdrp, int32_t nesyms,
int32_t extsymoff, int32_t extstroff, int32_t strsize)
{
if (debug)
fprintf(stderr,
"writing symhdr for %d entries at offset %#x\n",
nesyms, ep->f.f_symptr);
/* we are going to be no bigger than the ELF symbol table. */
ecoffp->stringsize = elfp->stringsize;
ecoffp->stringtab = malloc(elfp->stringsize);
newstrings = (char *) ecoffp->stringtab;
nsp = (char *) ecoffp->stringtab;
if (newstrings == NULL)
errx(1, "No memory for new string table");
/* Copy and translate symbols... */
idx = 0;
for (i = 0; i < nsyms; i++) {
int binding;
/* 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;
/* translate symbol types to ECOFF XXX */
ecoffp->ecoff_syms[idx].es_type = 1;
ecoffp->ecoff_syms[idx].es_class = 5;
/* 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;
idx++;
}
ecoffp->nsymbols = idx;
ecoffp->stringsize = nsp - newstrings;
}
/*
* pad to a 16-byte boundary
*/
static void
pad16(int fd, int size, const char *msg)
{