Subject: Re: Cross-compilation, Part II
To: Arne Henrik Juul <arnej@phys.unit.no>
From: Ted Lemon <mellon@fugue.com>
List: port-pmax
Date: 05/11/1995 08:35:37
Here is the source to elf2aout and elfdump.   I've sent it to the whole list
on the theory that you might not be the only one who wants it.   This stuff
needs to wind up in the NetBSD source tree, but I haven't gotten around to
discussing that with the core team yet.   (Chris, are you listening?)

...some of the comments are out of date - sorry about that.

				_MelloN_

# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	elf2aout.c
#	Makefile
#	elfdump.c
#
echo x - elf2aout.c
sed 's/^X//' >elf2aout.c << 'END-of-elf2aout.c'
X/*
X * Copyright (c) 1995
X *	Ted Lemon (hereinafter referred to as the author)
X *
X * Redistribution and use in source and binary forms, with or without
X * modification, are permitted provided that the following conditions
X * are met:
X * 1. Redistributions of source code must retain the above copyright
X *    notice, this list of conditions and the following disclaimer.
X * 2. Redistributions in binary form must reproduce the above copyright
X *    notice, this list of conditions and the following disclaimer in the
X *    documentation and/or other materials provided with the distribution.
X * 3. The name of the author may not be used to endorse or promote products
X *    derived from this software without specific prior written permission.
X *
X * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
X * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
X * SUCH DAMAGE.
X */
X
X/* elf2aout.c
X
X   This program converts an elf executable to a NetBSD a.out executable.
X   The minimal symbol table is copied, but the debugging symbols and
X   other informational sections are not. */
X
X#include <sys/types.h>
X#include <fcntl.h>
X#include <unistd.h>
X#include <machine/elf.h>
X#include <stdio.h>
X#include <a.out.h>
X#include <sys/errno.h>
X#include <string.h>
X#include <limits.h>
X
Xstruct sect {
X  unsigned long vaddr;
X  unsigned long len;
X};
Xint phcmp ();
Xchar *saveRead (int file, off_t offset, off_t len, char *name);
Xint copy (int, int, off_t, off_t);
Xint translate_syms (int, int, off_t, off_t, off_t, off_t);
Xextern int errno;
Xint *symTypeTable;
X
Xmain (int argc, char **argv, char **envp)
X{
X  struct ehdr ex;
X  struct phdr *ph;
X  struct shdr *sh;
X  struct sym *symtab;
X  char *shstrtab;
X  int strtabix, symtabix;
X  int i;
X  struct sect text, data, bss;
X  struct exec aex;
X  int infile, outfile;
X  unsigned long cur_vma = ULONG_MAX;
X  int symflag = 0;
X
X  text.len = data.len = bss.len = 0;
X  text.vaddr = data.vaddr = bss.vaddr = 0;
X
X  /* Check args... */
X  if (argc < 3 || argc > 4)
X    {
X    usage:
X      fprintf (stderr,
X	       "usage: elf2aout <elf executable> <a.out executable> [-s]\n");
X      exit (1);
X    }
X  if (argc == 4)
X    {
X      if (strcmp (argv [3], "-s"))
X	goto usage;
X      symflag = 1;
X    }
X
X  /* Try the input file... */
X  if ((infile = open (argv [1], O_RDONLY)) < 0)
X    {
X      fprintf (stderr, "Can't open %s for read: %s\n",
X	       argv [1], strerror (errno));
X      exit (1);
X    }
X
X  /* Read the header, which is at the beginning of the file... */
X  i = read (infile, &ex, sizeof ex);
X  if (i != sizeof ex)
X    {
X      fprintf (stderr, "ex: %s: %s.\n",
X	       argv [1], i ? strerror (errno) : "End of file reached");
X      exit (1);
X    }
X
X  /* Read the program headers... */
X  ph = (struct phdr *)saveRead (infile, ex.phoff,
X				ex.phcount * sizeof (struct phdr), "ph");
X  /* Read the section headers... */
X  sh = (struct shdr *)saveRead (infile, ex.shoff,
X				ex.shcount * sizeof (struct shdr), "sh");
X  /* Read in the section string table. */
X  shstrtab = saveRead (infile, sh [ex.shstrndx].offset,
X		       sh [ex.shstrndx].size, "shstrtab");
X
X  /* Find space for a table matching ELF section indices to a.out symbol
X     types. */
X  symTypeTable = (int *)malloc (ex.shcount * sizeof (int));
X  if (!symTypeTable)
X    {
X      fprintf (stderr, "symTypeTable: can't allocate.\n");
X      exit (1);
X    }
X  memset (symTypeTable, 0, ex.shcount * sizeof (int));
X
X  /* Look for the symbol table and string table...
X     Also map section indices to symbol types for a.out */
X  for (i = 0; i < ex.shcount; i++)
X    {
X      char *name = shstrtab + sh [i].name;
X      if (!strcmp (name, ".symtab"))
X	symtabix = i;
X      else if (!strcmp (name, ".strtab"))
X	strtabix = i;
X      else if (!strcmp (name, ".text") || !strcmp (name, ".rodata"))
X	symTypeTable [i] = N_TEXT;
X      else if (!strcmp (name, ".data") || !strcmp (name, ".sdata") ||
X	       !strcmp (name, ".lit4") || !strcmp (name, ".lit8"))
X	symTypeTable [i] = N_DATA;
X      else if (!strcmp (name, ".bss") || !strcmp (name, ".sbss"))
X	symTypeTable [i] = N_BSS;
X    }
X
X  /* Figure out if we can cram the program header into an a.out header...
X     Basically, we can't handle anything but loadable segments, but we
X     can ignore some kinds of segments.   We can't handle holes in the
X     address space, and we handle start addresses other than 0x1000 by
X     hoping that the loader will know where to load - a.out doesn't have
X     an explicit load address.   Segments may be out of order, so we
X     sort them first. */
X  qsort (ph, ex.phcount, sizeof (struct phdr), phcmp);
X  for (i = 0; i < ex.phcount; i++)
X    {
X      /* Section types we can ignore... */
X      if (ph [i].type == PT_NULL || ph [i].type == PT_NOTE ||
X	  ph [i].type == PT_PHDR || ph [i].type == PT_MIPS_REGINFO)
X	continue;
X      /* Section types we can't handle... */
X      else if (ph [i].type != PT_LOAD)
X        {
X	  fprintf (stderr, "Program header %d type %d can't be converted.\n");
X	  exit (1);
X	}
X      /* Writable (data) segment? */
X      if (ph [i].flags & PF_W)
X	{
X	  struct sect ndata, nbss;
X
X	  ndata.vaddr = ph [i].vaddr;
X	  ndata.len = ph [i].filesz;
X	  nbss.vaddr = ph [i].vaddr + ph [i].filesz;
X	  nbss.len = ph [i].memsz - ph [i].filesz;
X
X	  combine (&data, &ndata, 0);
X	  combine (&bss, &nbss, 1);
X	}
X      else
X	{
X	  struct sect ntxt;
X
X	  ntxt.vaddr = ph [i].vaddr;
X	  ntxt.len = ph [i].filesz;
X
X	  combine (&text, &ntxt);
X	}
X      /* Remember the lowest segment start address. */
X      if (ph [i].vaddr < cur_vma)
X	cur_vma = ph [i].vaddr;
X    }
X
X  /* Sections must be in order to be converted... */
X  if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr ||
X      text.vaddr + text.len > data.vaddr || data.vaddr + data.len > bss.vaddr)
X    {
X      fprintf (stderr, "Sections ordering prevents a.out conversion.\n");
X      exit (1);
X    }
X
X  /* If there is a gap between text and data, we'll fill it when we copy
X     the data, so update the length of the text segment as represented in
X     a.out to reflect that, since a.out doesn't allow gaps in the program
X     address space. */
X  if (text.vaddr + text.len < data.vaddr)
X    text.len = data.vaddr - text.vaddr;
X
X  /* We now have enough information to cons up an a.out header... */
X  aex.a_midmag = htonl ((symflag << 26) | (MID_PMAX << 16) | OMAGIC);
X  aex.a_text = text.len;
X  aex.a_data = data.len;
X  aex.a_bss = bss.len;
X  aex.a_entry = ex.entry;
X  aex.a_syms = (sizeof (struct nlist) *
X		(symtabix != -1
X		 ? sh [symtabix].size / sizeof (struct sym) : 0));
X  aex.a_trsize = 0;
X  aex.a_drsize = 0;
X
X  /* Make the output file... */
X  if ((outfile = open (argv [2], O_WRONLY | O_CREAT, 0777)) < 0)
X    {
X      fprintf (stderr, "Unable to create %s: %s\n", argv [2], strerror (errno));
X      exit (1);
X    }
X  /* Write the header... */
X  i = write (outfile, &aex, sizeof aex);
X  if (i != sizeof aex)
X    {
X      perror ("aex: write");
X      exit (1);
X    }
X
X  /* Copy the loadable sections.   Zero-fill any gaps less than 64k;
X     complain about any zero-filling, and die if we're asked to zero-fill
X     more than 64k. */
X  for (i = 0; i < ex.phcount; i++)
X    {
X      /* Unprocessable sections were handled above, so just verify that
X	 the section can be loaded before copying. */
X      if (ph [i].type == PT_LOAD && ph [i].filesz)
X	{
X	  if (cur_vma != ph [i].vaddr)
X	    {
X	      unsigned long gap = ph [i].vaddr - cur_vma;
X	      char obuf [1024];
X	      if (gap > 65536)
X		{
X		  fprintf (stderr, "Intersegment gap (%d bytes) too large.\n",
X			   gap);
X		  exit (1);
X		}
X	      fprintf (stderr, "Warning: %d byte intersegment gap.\n", gap);
X	      memset (obuf, 0, sizeof obuf);
X	      while (gap)
X		{
X		  int count = write (outfile, obuf, (gap > sizeof obuf
X						     ? sizeof obuf : gap));
X		  if (count < 0)
X		    {
X		      fprintf (stderr, "Error writing gap: %s\n",
X			       strerror (errno));
X		      exit (1);
X		    }
X		  gap -= count;
X		}
X	    }
X	  copy (outfile, infile, ph [i].offset, ph [i].filesz);
X	  cur_vma = ph [i].vaddr + ph [i].filesz;
X	}
X    }
X
X  /* Copy and translate the symbol table... */
X  translate_syms (outfile, infile, sh [symtabix].offset, sh [symtabix].size,
X		  sh [strtabix].offset, sh [strtabix].size);
X
X  /* Looks like we won... */
X  exit (0);
X}
X
X/* translate_syms (out, in, offset, size)
X
X   Read the ELF symbol table from in at offset; translate it into a.out
X   nlist format and write it to out. */
X
Xtranslate_syms (out, in, symoff, symsize, stroff, strsize)
X     int out, in;
X     off_t symoff, symsize;
X     off_t stroff, strsize;
X{
X# define SYMS_PER_PASS	64
X  struct sym inbuf [64];
X  struct nlist outbuf [64];
X  int i, remaining, cur;
X  char *oldstrings;
X  char *newstrings, *nsp;
X  int newstringsize;
X
X  /* Zero the unused fields in the output buffer.. */
X  memset (outbuf, 0, sizeof outbuf);
X
X  /* Find number of symbols to process... */
X  remaining = symsize / sizeof (struct sym);
X
X  /* Suck in the old string table... */
X  oldstrings = saveRead (in, stroff, strsize, "string table");
X
X  /* Allocate space for the new one.   XXX We make the wild assumption that
X     no two symbol table entries will point at the same place in the
X     string table - if that assumption is bad, this could easily blow up. */
X  newstringsize = strsize + remaining;
X  newstrings = (char *)malloc (newstringsize);
X  if (!newstrings)
X    {
X      fprintf (stderr, "No memory for new string table!\n");
X      exit (1);
X    }
X  /* Initialize the table pointer... */
X  nsp = newstrings;
X
X  /* Go the the start of the ELF symbol table... */
X  if (lseek (in, symoff, SEEK_SET) < 0)
X    {
X      perror ("translate_syms: lseek");
X      exit (1);
X    }
X
X  /* Translate and copy symbols... */
X  while (remaining)
X    {
X      cur = remaining;
X      if (cur > SYMS_PER_PASS)
X	cur = SYMS_PER_PASS;
X      remaining -= cur;
X      if ((i = read (in, inbuf, cur * sizeof (struct sym)))
X	  != cur * sizeof (struct sym))
X	{
X	  if (i < 0)
X	    perror ("translate_syms");
X	  else
X	    fprintf (stderr, "translate_syms: premature end of file.\n");
X	  exit (1);
X	}
X
X      /* Do the translation... */
X      for (i = 0; i < cur; i++)
X	{
X	  /* Copy the symbol into the new table, but prepend an underscore. */
X	  *nsp = '_';
X	  strcpy (nsp + 1, oldstrings + inbuf [i].name);
X	  outbuf [i].n_un.n_strx = nsp - newstrings + 4;
X	  nsp += strlen (nsp) + 1;
X
X	  /* Convert ELF symbol type/section/etc info into a.out type info. */
X	  if (inbuf [i].type == STT_FILE)
X	    outbuf [i].n_type = N_FN;
X	  else if (inbuf [i].shndx == SHN_UNDEF)
X	    outbuf [i].n_type = N_UNDF;
X	  else if (inbuf [i].shndx == SHN_ABS)
X	    outbuf [i].n_type = N_ABS;
X	  else if (inbuf [i].shndx == SHN_COMMON ||
X		 inbuf [i].shndx == SHN_MIPS_ACOMMON)
X	    outbuf [i].n_type = N_COMM;
X	  else
X	    outbuf [i].n_type = symTypeTable [inbuf [i].shndx];
X	  if (inbuf [i].binding == STB_GLOBAL)
X	    outbuf [i].n_type |= N_EXT;
X	  /* Symbol values in executables should be compatible. */
X	  outbuf [i].n_value = inbuf [i].value;
X	}
X      /* Write out the symbols... */
X      if ((i = write (out, outbuf, cur * sizeof (struct nlist)))
X	  != cur * sizeof (struct nlist))
X	{
X	  fprintf (stderr, "translate_syms: write: %s\n", strerror (errno));
X	  exit (1);
X	}
X    }
X  /* Write out the string table length... */
X  if (write (out, &newstringsize, sizeof newstringsize)
X      != sizeof newstringsize)
X    {
X      fprintf (stderr,
X	       "translate_syms: newstringsize: %s\n", strerror (errno));
X      exit (1);
X    }
X  /* Write out the string table... */
X  if (write (out, newstrings, newstringsize) != newstringsize)
X    {
X      fprintf (stderr, "translate_syms: newstrings: %s\n", strerror (errno));
X      exit (1);
X    }
X}
X      
Xcopy (out, in, offset, size)
X     int out, in;
X     off_t offset, size;
X{
X  char ibuf [4096];
X  int remaining, cur, count;
X
X  /* Go the the start of the ELF symbol table... */
X  if (lseek (in, offset, SEEK_SET) < 0)
X    {
X      perror ("copy: lseek");
X      exit (1);
X    }
X
X  remaining = size;
X  while (remaining)
X    {
X      cur = remaining;
X      if (cur > sizeof ibuf)
X	cur = sizeof ibuf;
X      remaining -= cur;
X      if ((count = read (in, ibuf, cur)) != cur)
X	{
X	  fprintf (stderr, "copy: read: %s\n",
X		   count ? strerror (errno) : "premature end of file");
X	  exit (1);
X	}
X      if ((count = write (out, ibuf, cur)) != cur)
X	{
X	  perror ("copy: write");
X	  exit (1);
X	}
X    }
X}
X
X/* Combine two segments, which must be contiguous.   If pad is true, it's
X   okay for there to be padding between. */
Xcombine (base, new, pad)
X     struct sect *base, *new;
X     int pad;
X{
X  if (!base -> len)
X    *base = *new;
X  else if (new -> len)
X    {
X      if (base -> vaddr + base -> len != new -> vaddr)
X	{
X	  if (pad)
X	    base -> len = new -> vaddr - base -> vaddr;
X	  else
X	    {
X	      fprintf (stderr,
X		       "Non-contiguous data can't be converted.\n");
X	      exit (1);
X	    }
X	}
X      base -> len += new -> len;
X    }
X}
X
Xphcmp (h1, h2)
X     struct phdr *h1, *h2;
X{
X  if (h1 -> vaddr > h2 -> vaddr)
X    return 1;
X  else if (h1 -> vaddr < h2 -> vaddr)
X    return -1;
X  else
X    return 0;
X}
X
Xchar *saveRead (int file, off_t offset, off_t len, char *name)
X{
X  char *tmp;
X  int count;
X  off_t off;
X  if ((off = lseek (file, offset, SEEK_SET)) < 0)
X    {
X      fprintf (stderr, "%s: fseek: %s\n", name, strerror (errno));
X      exit (1);
X    }
X  if (!(tmp = (char *)malloc (len)))
X    {
X      fprintf (stderr, "%s: Can't allocate %d bytes.\n", name, len);
X      exit (1);
X    }
X  count = read (file, tmp, len);
X  if (count != len)
X    {
X      fprintf (stderr, "%s: read: %s.\n",
X	       name, count ? strerror (errno) : "End of file reached");
X      exit (1);
X    }
X  return tmp;
X}
END-of-elf2aout.c
echo x - Makefile
sed 's/^X//' >Makefile << 'END-of-Makefile'
Xall:	elfdump elf2aout
X
Xelfdump:	elfdump.c
X		gcc -g -O elfdump.c -o elfdump
X
Xelf2aout:	elf2aout.c
X		gcc -g -O elf2aout.c -o elf2aout
END-of-Makefile
echo x - elfdump.c
sed 's/^X//' >elfdump.c << 'END-of-elfdump.c'
X/* elf2aout.c
X
X   This program converts an elf executable to a NetBSD a.out executable.
X   The minimal symbol table is copied, but the debugging symbols and
X   other informational sections are not. */
X
X#include <sys/types.h>
X#include <fcntl.h>
X#include <unistd.h>
X#include <sys/exec_elf.h>
X
Xmain (int argc, char **argv, char **envp)
X{
X  int count;
X  struct ehdr ex;
X  struct phdr ph;
X  struct shdr sh [32];
X  char *contents [32];
X  struct sym *symtab;
X  int nsyms;
X  char *strtab;
X  int i;
X  off_t off;
X  unsigned char buf [4];
X
X  count = read (0, &ex, sizeof ex);
X  if (count != sizeof ex)
X    {
X      if (count < 0)
X        perror ("ex: read");
X      else
X	printf ("ex: short read: %d bytes instead of %d.\n",
X		count, sizeof ex);
X      exit (1);
X    }
X  printf ("magic %d %3.3s %x %x %x\n",
X	  ex.elf_magic [0], &ex.elf_magic [1],
X	  ex.magic [0], ex.magic [1], ex.magic [2]);
X  printf ("type %x  machine %x  version %x\n",
X	  ex.type, ex.machine, ex.version);
X  printf ("entry point %x  phoff %x  shoff %x  flags %x\n",
X	  ex.entry, ex.phoff, ex.shoff, ex.flags);
X  printf ("ehsize %x  phsize %x  phcount %x\n",
X	  ex.ehsize, ex.phsize, ex.phcount);
X  printf ("shsize %x  shcount %x  shstrndx %x\n",
X	  ex.shsize, ex.shcount, ex.shstrndx);
X  for (i = 0; i < ex.phcount; i++)
X    {
X      if ((off = lseek (0, ex.phoff + i * sizeof ph, SEEK_SET)) < 0)
X	{
X	  perror ("ph: lseek");
X	  exit (1);
X	}
X      if ((count = read (0, &ph, sizeof ph)) < 0)
X	{
X	  perror ("ph: read");
X	  exit (1);
X	}
X      else if (count != sizeof ph)
X	{
X	  printf ("ph: short read: %d bytes instead of %d.\n",
X		  count, sizeof ph);
X	  exit (1);
X	}
X      if (ph.type == PT_LOAD)
X	{
X	  if ((off = lseek (0, ph.offset, SEEK_SET)) < 0)
X            {
X              perror ("ph contents: lseek");
X              exit (1);
X            }
X	  if ((count = read (0, buf, sizeof buf)) < 0)
X	    {
X	      perror ("ph contents: read");
X	      exit (1);
X	    }
X	  else if (count != sizeof buf)
X	    {
X	      printf ("ph contents: short read: %d bytes instead of %d.\n",
X		      count, sizeof buf);
X	    }
X	  printf ("Section %d at %x, len %x  addr %x  size %x\n",
X		  i, ph.offset, ph.filesz, ph.vaddr, ph.memsz);
X	}
X    }
X  for (i = 0; i < ex.shcount; i++)
X    {
X      if ((off = lseek (0, ex.shoff + i * sizeof (struct shdr), SEEK_SET)) < 0)
X	{
X	  perror ("sh: lseek");
X	  exit (1);
X	}
X      if ((count = read (0, &sh [i], sizeof (struct shdr))) < 0)
X	{
X	  perror ("sh: read");
X	  exit (1);
X	}
X      else if (count != sizeof (struct shdr))
X	{
X	  printf ("sh: short read: %d bytes instead of %d.\n",
X		  count, sizeof (struct shdr));
X	  exit (1);
X	}
X      contents [i] = (char *)malloc (sh [i].size);
X      if (!contents [i])
X	{
X	  printf ("Can't allocate section %d contents\n", i);
X	  exit (1);
X	}
X      if ((off = lseek (0, sh [i].offset, SEEK_SET)) < 0)
X	{
X	  perror ("contents: lseek");
X	  exit (1);
X	}
X      if ((count = read (0, contents [i], sh [i].size)) < 0)
X	{
X	  perror ("contents: read");
X	  exit (1);
X	}
X      else if (count != sh [i].size)
X	{
X	  printf ("contents: short read: %d bytes instead of %d.\n",
X		  count, sh [i].size);
X	  exit (1);
X	}
X    }
X  for (i = 0; i < ex.shcount; i++)
X    {
X      char *name = contents [ex.shstrndx] + sh [i].name;
X      printf ("name %s  type %x  flags %x  addr %x\n",
X	      name, sh [i].type, sh [i].flags, sh [i].addr);
X      printf ("offset %x  size %x  link %x  info %x\n",
X	      sh [i].offset, sh [i].size, sh [i].link, sh [i].info);
X      printf ("align %x  esize %x\n", sh [i].align, sh [i].esize);
X      if (!strcmp (name, ".symtab"))
X	{
X	  symtab = (struct sym *)contents [i];
X	  nsyms = sh [i].size / sizeof (struct sym);
X        }
X      else if (!strcmp (name, ".strtab"))
X	{
X	  strtab = contents [i];
X        }
X    }
X  for (i = 0; i < nsyms; i++)
X    {
X      printf ("name = %s; value = %x; size = %x; ",
X	      strtab + symtab [i].name, symtab [i].value, symtab [i].size);
X      printf ("info = %x; other = %x; sect = %s\n",
X	      symtab [i].info, symtab [i].other,
X	      symtab [i].shndx < ex.shcount
X	      ? contents [ex.shstrndx] + sh [symtab [i].shndx].name
X	      : "--junk--");
X    }
X}
END-of-elfdump.c
exit