Subject: new elf2elf.c
To: None <port-pmax@NetBSD.ORG>
From: Arne H. Juul <arnej@pvv.unit.no>
List: port-pmax
Date: 10/24/1995 15:50:10
Since people has seemed to experience no serious problems with this,
here's a version that can take more than one file, and checks magic
numbers a little bit.

Again, no guarantees that this works reliably, but if you're using
NetBSD/pmax you're hopefully willing to take your chances with it
anyway :-)

  - Arne H. J.


/*
 * 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.
 */


/* elf2elf.c - a strip that works by only taking care of loadable segments */

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <sys/types.h>
#include <machine/elf.h>

int phcmp ();

void *saveRead(int file, off_t offset, int len, char *name);
int placeWrite(int file, off_t offset, int len, char *name, void *data);
int dostrip(char *filename);


int main (int argc, char **argv)
{
  int retval = 0;
  /* Check args... */
  if (argc < 2)
    {
	fprintf (stderr, "usage: elf2elf <elf executable> ...\n");
      exit (1);
    }
  while (argc>1) {
    argv++;
    argc--;
    retval |= dostrip(*argv);
  }
  exit (retval);
}


/*
 * dostrip - actually strip a file.
 * returns:  0 - stripped ok
 *           1 - can't open
 *           2 - bad magic (not an ELF executable)
 *           4 - corrupt input from file (probably damaged)
 *           8 - problem writing to file (certainly damaged)
 */

int dostrip(char *filename)
{
  struct ehdr ex;
  struct phdr *ph;
  int i;
  static int infile = -1;
  unsigned long lastp;
  struct phdr **indt;
  void *segment;

  /* A kludge */
  if (infile > -1) {
    close(infile);
  }

  /* Try the input file... */
  if ((infile = open (filename, O_RDWR)) < 0)
    {
      fprintf (stderr, "Can't open %s for read/write: %s\n",
	       filename, strerror (errno));
      return (1);
    }

  /* Read the header, which is at the beginning of the file... */

  /*
  ** printf("elf header from file 0 size %x\n", sizeof ex);
  */

  errno = 0;
  i = read (infile, &ex, sizeof ex);
  if (i != sizeof ex)
    {
      fprintf (stderr, "read ex: %s: %s.\n", filename,
	       (i<0 || errno ) ? strerror (errno) : "End of file reached");
      return 2;
    }
  /* Check magic numbers */
  if (strncmp(ex.elf_magic, "\177ELF", 4))
  {
	fprintf (stderr, "elf2elf: %s: bad magic\n", filename);
	return 2;
  }

  /* Devalidate section headers */
  ex.shoff = 0;
  ex.shcount = 0;
  ex.shsize = 0;
  ex.shstrndx = 0;
  placeWrite (infile, 0, sizeof ex, "ex", &ex);


  /* Read the program headers... */
  /*
  ** printf("program headers from file %x size %x\n",
  **        ex.phoff, ex.phcount * sizeof (struct phdr));
  */
  lastp = ex.phoff + ex.phcount * sizeof (struct phdr);

  ph = (struct phdr *)saveRead (infile, ex.phoff,
				ex.phcount * sizeof (struct phdr), "ph");
  if (!ph) {
    return 4;
  }
  indt = (struct phdr **)malloc(ex.phcount * sizeof(struct phdr **));
  if (!indt) {
    fprintf(stderr, "elf2elf: malloc failed\n");
    exit (1);
  }
  for (i=0; i<ex.phcount; i++) {
	indt[i] = ph+i;
  }

  qsort (indt, ex.phcount, sizeof (struct phdr **), phcmp);

  for (i = 0; i < ex.phcount; i++)
    {
      struct phdr *php = indt[i];

      /* Section types we can ignore... */
      if (php->type == PT_NULL || php->type == PT_NOTE ||
	  php->type == PT_PHDR || php->type == PT_MIPS_REGINFO)
      {
	fprintf(stderr, "segment type %ld ignored file %lx size %lx\n",
		php->type, php->offset, php->filesz);
	continue;
      }
      /* Section types we can't handle... */
      else if (php->type != PT_LOAD)
        {
	  fprintf (stderr, "Program header %d type %ld can't be converted.\n",
			php-ph, php->type);
	  return 4;
	}

	/*
	** printf("segment %d load file %x size %x end %x\n",
	**	php-ph,
	**	php->offset, php->filesz, php->offset + php->filesz);
	*/

	if (php->offset < lastp) {
	  fprintf (stderr, "Program segments overlap\n");
	  return 4;
	}
	/* should use PAGESIZE here ? */
	if ((php->offset - lastp) > 0x2000) {
		int pg;

		pg = ((php->offset - lastp) / 0x2000);
		pg *= 0x2000;

  		segment = saveRead (infile, php->offset,
					php->filesz, "segment");
		if (!segment) return 4;
		php->offset -= pg;
  		if (placeWrite (infile, php->offset,
  					php->filesz, "segment", segment))
			return 8;
		/*
		** printf("Move segment %d shift %x to %x\n", 
		**	php-ph, pg, php->offset);
		*/
	}
	lastp = php->offset + php->filesz;
     }
  if (placeWrite (infile, ex.phoff, ex.phcount*sizeof (struct phdr), "ph", ph))
	return 8;
  if (ftruncate(infile, lastp)) {
	fprintf(stderr, "ftruncate %s to 0x%lx failed: %s\n",
		filename, lastp, strerror(errno));
  }

  /* Looks like we won... */
  return (0);
}

int
phcmp (h1, h2)
     struct phdr **h1, **h2;
{
  if ((*h1) -> offset > (*h2) -> offset)
    return 1;
  else if ((*h1) -> offset < (*h2) -> offset)
    return -1;
  else
    return 0;
}

void *
saveRead (int file, off_t offset, int len, char *name)
{
  void *tmp;
  int count;
  off_t off;
  if ((off = lseek (file, offset, SEEK_SET)) < 0)
    {
      fprintf (stderr, "%s: lseek: %s\n", name, strerror (errno));
      return 0;
    }
  if (!(tmp = malloc (len)))
    {
      fprintf (stderr, "%s: Can't allocate %d bytes.\n", name, len);
      exit (1);
    }
  count = read (file, tmp, len);
  errno = 0;
  if (count != len)
    {
      fprintf (stderr, "%s: read: %s.\n", name,
               (count<0 || errno) ? strerror (errno) : "End of file reached");
      free(tmp);
      return 0;
    }
  return tmp;
}

int
placeWrite (int file, off_t offset, int len, char *name, void *data)
{
  int count;
  off_t off;
  if ((off = lseek (file, offset, SEEK_SET)) < 0)
    {
      fprintf (stderr, "%s: lseek: %s\n", name, strerror (errno));
      return -1;
    }
  count = write (file, data, len);
  if (count != len)
    {
      fprintf (stderr, "%s: write: %s.\n", name, strerror (errno));
      return -1;
    }
  return 0;
}