Home | History | Annotate | Line # | Download | only in elf2ecoff
elf2ecoff.c revision 1.2
      1  1.2  jonathan /* $NetBSD: elf2ecoff.c,v 1.2 1996/09/29 22:01:45 jonathan Exp $ */
      2  1.2  jonathan 
      3  1.1  jonathan /*
      4  1.1  jonathan  * Copyright (c) 1995
      5  1.1  jonathan  *	Ted Lemon (hereinafter referred to as the author)
      6  1.1  jonathan  *
      7  1.1  jonathan  * Redistribution and use in source and binary forms, with or without
      8  1.1  jonathan  * modification, are permitted provided that the following conditions
      9  1.1  jonathan  * are met:
     10  1.1  jonathan  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jonathan  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jonathan  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jonathan  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jonathan  *    documentation and/or other materials provided with the distribution.
     15  1.1  jonathan  * 3. The name of the author may not be used to endorse or promote products
     16  1.1  jonathan  *    derived from this software without specific prior written permission.
     17  1.1  jonathan  *
     18  1.1  jonathan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
     19  1.1  jonathan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  1.1  jonathan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  1.1  jonathan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
     22  1.1  jonathan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.1  jonathan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  1.1  jonathan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.1  jonathan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.1  jonathan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1  jonathan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1  jonathan  * SUCH DAMAGE.
     29  1.1  jonathan  */
     30  1.1  jonathan 
     31  1.1  jonathan /* elf2ecoff.c
     32  1.1  jonathan 
     33  1.1  jonathan    This program converts an elf executable to an ECOFF executable.
     34  1.1  jonathan    No symbol table is retained.   This is useful primarily in building
     35  1.1  jonathan    net-bootable kernels for machines (e.g., DECstation and Alpha) which
     36  1.1  jonathan    only support the ECOFF object file format. */
     37  1.1  jonathan 
     38  1.1  jonathan #include <sys/types.h>
     39  1.1  jonathan #include <fcntl.h>
     40  1.1  jonathan #include <unistd.h>
     41  1.1  jonathan #include <machine/elf.h>
     42  1.1  jonathan #include <stdio.h>
     43  1.1  jonathan #include <sys/exec_ecoff.h>
     44  1.1  jonathan #include <sys/errno.h>
     45  1.1  jonathan #include <string.h>
     46  1.1  jonathan #include <limits.h>
     47  1.1  jonathan 
     48  1.1  jonathan struct sect {
     49  1.1  jonathan   unsigned long vaddr;
     50  1.1  jonathan   unsigned long len;
     51  1.1  jonathan };
     52  1.1  jonathan 
     53  1.1  jonathan int phcmp ();
     54  1.1  jonathan char *saveRead (int file, off_t offset, off_t len, char *name);
     55  1.1  jonathan int copy (int, int, off_t, off_t);
     56  1.1  jonathan int translate_syms (int, int, off_t, off_t, off_t, off_t);
     57  1.1  jonathan extern int errno;
     58  1.1  jonathan int *symTypeTable;
     59  1.1  jonathan 
     60  1.1  jonathan main (int argc, char **argv, char **envp)
     61  1.1  jonathan {
     62  1.1  jonathan   struct ehdr ex;
     63  1.1  jonathan   struct phdr *ph;
     64  1.1  jonathan   struct shdr *sh;
     65  1.1  jonathan   struct sym *symtab;
     66  1.1  jonathan   char *shstrtab;
     67  1.1  jonathan   int strtabix, symtabix;
     68  1.1  jonathan   int i, pad;
     69  1.1  jonathan   struct sect text, data, bss;
     70  1.2  jonathan   struct ecoff_exechdr ep;
     71  1.1  jonathan   struct ecoff_scnhdr esecs [3];
     72  1.1  jonathan   int infile, outfile;
     73  1.1  jonathan   unsigned long cur_vma = ULONG_MAX;
     74  1.1  jonathan   int symflag = 0;
     75  1.1  jonathan 
     76  1.1  jonathan   text.len = data.len = bss.len = 0;
     77  1.1  jonathan   text.vaddr = data.vaddr = bss.vaddr = 0;
     78  1.1  jonathan 
     79  1.1  jonathan   /* Check args... */
     80  1.1  jonathan   if (argc < 3 || argc > 4)
     81  1.1  jonathan     {
     82  1.1  jonathan     usage:
     83  1.1  jonathan       fprintf (stderr,
     84  1.1  jonathan 	       "usage: elf2aout <elf executable> <a.out executable> [-s]\n");
     85  1.1  jonathan       exit (1);
     86  1.1  jonathan     }
     87  1.1  jonathan   if (argc == 4)
     88  1.1  jonathan     {
     89  1.1  jonathan       if (strcmp (argv [3], "-s"))
     90  1.1  jonathan 	goto usage;
     91  1.1  jonathan       symflag = 1;
     92  1.1  jonathan     }
     93  1.1  jonathan 
     94  1.1  jonathan   /* Try the input file... */
     95  1.1  jonathan   if ((infile = open (argv [1], O_RDONLY)) < 0)
     96  1.1  jonathan     {
     97  1.1  jonathan       fprintf (stderr, "Can't open %s for read: %s\n",
     98  1.1  jonathan 	       argv [1], strerror (errno));
     99  1.1  jonathan       exit (1);
    100  1.1  jonathan     }
    101  1.1  jonathan 
    102  1.1  jonathan   /* Read the header, which is at the beginning of the file... */
    103  1.1  jonathan   i = read (infile, &ex, sizeof ex);
    104  1.1  jonathan   if (i != sizeof ex)
    105  1.1  jonathan     {
    106  1.1  jonathan       fprintf (stderr, "ex: %s: %s.\n",
    107  1.1  jonathan 	       argv [1], i ? strerror (errno) : "End of file reached");
    108  1.1  jonathan       exit (1);
    109  1.1  jonathan     }
    110  1.1  jonathan 
    111  1.1  jonathan   /* Read the program headers... */
    112  1.1  jonathan   ph = (struct phdr *)saveRead (infile, ex.phoff,
    113  1.1  jonathan 				ex.phcount * sizeof (struct phdr), "ph");
    114  1.1  jonathan   /* Read the section headers... */
    115  1.1  jonathan   sh = (struct shdr *)saveRead (infile, ex.shoff,
    116  1.1  jonathan 				ex.shcount * sizeof (struct shdr), "sh");
    117  1.1  jonathan   /* Read in the section string table. */
    118  1.1  jonathan   shstrtab = saveRead (infile, sh [ex.shstrndx].offset,
    119  1.1  jonathan 		       sh [ex.shstrndx].size, "shstrtab");
    120  1.1  jonathan 
    121  1.1  jonathan   /* Figure out if we can cram the program header into an ECOFF
    122  1.1  jonathan      header...  Basically, we can't handle anything but loadable
    123  1.1  jonathan      segments, but we can ignore some kinds of segments.  We can't
    124  1.1  jonathan      handle holes in the address space.  Segments may be out of order,
    125  1.1  jonathan      so we sort them first. */
    126  1.1  jonathan 
    127  1.1  jonathan   qsort (ph, ex.phcount, sizeof (struct phdr), phcmp);
    128  1.1  jonathan 
    129  1.1  jonathan   for (i = 0; i < ex.phcount; i++)
    130  1.1  jonathan     {
    131  1.1  jonathan       /* Section types we can ignore... */
    132  1.1  jonathan       if (ph [i].type == PT_NULL || ph [i].type == PT_NOTE ||
    133  1.1  jonathan 	  ph [i].type == PT_PHDR || ph [i].type == PT_MIPS_REGINFO)
    134  1.1  jonathan 	continue;
    135  1.1  jonathan       /* Section types we can't handle... */
    136  1.1  jonathan       else if (ph [i].type != PT_LOAD)
    137  1.1  jonathan         {
    138  1.1  jonathan 	  fprintf (stderr, "Program header %d type %d can't be converted.\n");
    139  1.1  jonathan 	  exit (1);
    140  1.1  jonathan 	}
    141  1.1  jonathan       /* Writable (data) segment? */
    142  1.1  jonathan       if (ph [i].flags & PF_W)
    143  1.1  jonathan 	{
    144  1.1  jonathan 	  struct sect ndata, nbss;
    145  1.1  jonathan 
    146  1.1  jonathan 	  ndata.vaddr = ph [i].vaddr;
    147  1.1  jonathan 	  ndata.len = ph [i].filesz;
    148  1.1  jonathan 	  nbss.vaddr = ph [i].vaddr + ph [i].filesz;
    149  1.1  jonathan 	  nbss.len = ph [i].memsz - ph [i].filesz;
    150  1.1  jonathan 
    151  1.1  jonathan 	  combine (&data, &ndata, 0);
    152  1.1  jonathan 	  combine (&bss, &nbss, 1);
    153  1.1  jonathan 	}
    154  1.1  jonathan       else
    155  1.1  jonathan 	{
    156  1.1  jonathan 	  struct sect ntxt;
    157  1.1  jonathan 
    158  1.1  jonathan 	  ntxt.vaddr = ph [i].vaddr;
    159  1.1  jonathan 	  ntxt.len = ph [i].filesz;
    160  1.1  jonathan 
    161  1.1  jonathan 	  combine (&text, &ntxt);
    162  1.1  jonathan 	}
    163  1.1  jonathan       /* Remember the lowest segment start address. */
    164  1.1  jonathan       if (ph [i].vaddr < cur_vma)
    165  1.1  jonathan 	cur_vma = ph [i].vaddr;
    166  1.1  jonathan     }
    167  1.1  jonathan 
    168  1.1  jonathan   /* Sections must be in order to be converted... */
    169  1.1  jonathan   if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr ||
    170  1.1  jonathan       text.vaddr + text.len > data.vaddr || data.vaddr + data.len > bss.vaddr)
    171  1.1  jonathan     {
    172  1.1  jonathan       fprintf (stderr, "Sections ordering prevents a.out conversion.\n");
    173  1.1  jonathan       exit (1);
    174  1.1  jonathan     }
    175  1.1  jonathan 
    176  1.1  jonathan   /* If there's a data section but no text section, then the loader
    177  1.1  jonathan      combined everything into one section.   That needs to be the
    178  1.1  jonathan      text section, so just make the data section zero length following
    179  1.1  jonathan      text. */
    180  1.1  jonathan   if (data.len && !text.len)
    181  1.1  jonathan     {
    182  1.1  jonathan       text = data;
    183  1.1  jonathan       data.vaddr = text.vaddr + text.len;
    184  1.1  jonathan       data.len = 0;
    185  1.1  jonathan     }
    186  1.1  jonathan 
    187  1.1  jonathan   /* If there is a gap between text and data, we'll fill it when we copy
    188  1.1  jonathan      the data, so update the length of the text segment as represented in
    189  1.1  jonathan      a.out to reflect that, since a.out doesn't allow gaps in the program
    190  1.1  jonathan      address space. */
    191  1.1  jonathan   if (text.vaddr + text.len < data.vaddr)
    192  1.1  jonathan     text.len = data.vaddr - text.vaddr;
    193  1.1  jonathan 
    194  1.1  jonathan   /* We now have enough information to cons up an a.out header... */
    195  1.2  jonathan   ep.a.magic = ECOFF_OMAGIC;
    196  1.2  jonathan   ep.a.vstamp = 200;
    197  1.2  jonathan   ep.a.tsize = text.len;
    198  1.2  jonathan   ep.a.dsize = data.len;
    199  1.2  jonathan   ep.a.bsize = bss.len;
    200  1.2  jonathan   ep.a.entry = ex.entry;
    201  1.2  jonathan   ep.a.text_start = text.vaddr;
    202  1.2  jonathan   ep.a.data_start = data.vaddr;
    203  1.2  jonathan   ep.a.bss_start = bss.vaddr;
    204  1.2  jonathan   ep.a.gprmask = 0xf3fffffe;
    205  1.2  jonathan   bzero (&ep.a.cprmask, sizeof ep.a.cprmask);
    206  1.2  jonathan   ep.a.gp_value = 0; /* unused. */
    207  1.2  jonathan 
    208  1.2  jonathan   ep.f.f_magic = ECOFF_MAGIC_MIPSEL;
    209  1.2  jonathan   ep.f.f_nscns = 3;
    210  1.2  jonathan   ep.f.f_timdat = 0;	/* bogus */
    211  1.2  jonathan   ep.f.f_symptr = 0;
    212  1.2  jonathan   ep.f.f_nsyms = 0;
    213  1.2  jonathan   ep.f.f_opthdr = sizeof ep.a;
    214  1.2  jonathan   ep.f.f_flags = 0x100f; /* Stripped, not sharable. */
    215  1.2  jonathan 
    216  1.2  jonathan   strcpy (esecs [0].s_name, ".text");
    217  1.2  jonathan   strcpy (esecs [1].s_name, ".data");
    218  1.2  jonathan   strcpy (esecs [2].s_name, ".bss");
    219  1.2  jonathan   esecs [0].s_paddr = esecs [0].s_vaddr = ep.a.text_start;
    220  1.2  jonathan   esecs [1].s_paddr = esecs [1].s_vaddr = ep.a.data_start;
    221  1.2  jonathan   esecs [2].s_paddr = esecs [2].s_vaddr = ep.a.bss_start;
    222  1.2  jonathan   esecs [0].s_size = ep.a.tsize;
    223  1.2  jonathan   esecs [1].s_size = ep.a.dsize;
    224  1.2  jonathan   esecs [2].s_size = ep.a.bsize;
    225  1.2  jonathan 
    226  1.2  jonathan   esecs [0].s_scnptr = ECOFF_TXTOFF (&ep);
    227  1.2  jonathan   esecs [1].s_scnptr = ECOFF_DATOFF (&ep);
    228  1.2  jonathan   esecs [2].s_scnptr = esecs [1].s_scnptr +
    229  1.2  jonathan 	  ECOFF_ROUND (esecs [1].s_size, ECOFF_SEGMENT_ALIGNMENT (&ep));
    230  1.2  jonathan   esecs [0].s_relptr = esecs [1].s_relptr
    231  1.2  jonathan 	  = esecs [2].s_relptr = 0;
    232  1.2  jonathan   esecs [0].s_lnnoptr = esecs [1].s_lnnoptr
    233  1.2  jonathan 	  = esecs [2].s_lnnoptr = 0;
    234  1.2  jonathan   esecs [0].s_nreloc = esecs [1].s_nreloc = esecs [2].s_nreloc = 0;
    235  1.2  jonathan   esecs [0].s_nlnno = esecs [1].s_nlnno = esecs [2].s_nlnno = 0;
    236  1.2  jonathan   esecs [0].s_flags = 0x20;
    237  1.2  jonathan   esecs [1].s_flags = 0x40;
    238  1.2  jonathan   esecs [2].s_flags = 0x82;
    239  1.1  jonathan 
    240  1.1  jonathan   /* Make the output file... */
    241  1.1  jonathan   if ((outfile = open (argv [2], O_WRONLY | O_CREAT, 0777)) < 0)
    242  1.1  jonathan     {
    243  1.1  jonathan       fprintf (stderr, "Unable to create %s: %s\n", argv [2], strerror (errno));
    244  1.1  jonathan       exit (1);
    245  1.1  jonathan     }
    246  1.1  jonathan 
    247  1.1  jonathan   /* Write the headers... */
    248  1.2  jonathan   i = write (outfile, &ep.f, sizeof ep.f);
    249  1.2  jonathan   if (i != sizeof ep.f)
    250  1.1  jonathan     {
    251  1.2  jonathan       perror ("ep.f: write");
    252  1.1  jonathan       exit (1);
    253  1.1  jonathan 
    254  1.1  jonathan   for (i = 0; i < 6; i++)
    255  1.1  jonathan     {
    256  1.1  jonathan       printf ("Section %d: %s phys %x  size %x  file offset %x\n",
    257  1.2  jonathan 	      i, esecs [i].s_name, esecs [i].s_paddr,
    258  1.2  jonathan 	      esecs [i].s_size, esecs [i].s_scnptr);
    259  1.1  jonathan     }
    260  1.1  jonathan     }
    261  1.1  jonathan   fprintf (stderr, "wrote %d byte file header.\n", i);
    262  1.1  jonathan 
    263  1.2  jonathan   i = write (outfile, &ep.a, sizeof ep.a);
    264  1.2  jonathan   if (i != sizeof ep.a)
    265  1.1  jonathan     {
    266  1.2  jonathan       perror ("ep.a: write");
    267  1.1  jonathan       exit (1);
    268  1.1  jonathan     }
    269  1.1  jonathan   fprintf (stderr, "wrote %d byte a.out header.\n", i);
    270  1.1  jonathan 
    271  1.1  jonathan   i = write (outfile, &esecs, sizeof esecs);
    272  1.1  jonathan   if (i != sizeof esecs)
    273  1.1  jonathan     {
    274  1.1  jonathan       perror ("esecs: write");
    275  1.1  jonathan       exit (1);
    276  1.1  jonathan     }
    277  1.1  jonathan   fprintf (stderr, "wrote %d bytes of section headers.\n", i);
    278  1.1  jonathan 
    279  1.2  jonathan   if (pad = ((sizeof ep.f + sizeof ep.a + sizeof esecs) & 15))
    280  1.1  jonathan     {
    281  1.1  jonathan       pad = 16 - pad;
    282  1.1  jonathan       i = write (outfile, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0", pad);
    283  1.1  jonathan       if (i < 0)
    284  1.1  jonathan 	{
    285  1.1  jonathan 	  perror ("ipad: write");
    286  1.1  jonathan 	  exit (1);
    287  1.1  jonathan 	}
    288  1.1  jonathan       fprintf (stderr, "wrote %d byte pad.\n", i);
    289  1.1  jonathan     }
    290  1.1  jonathan 
    291  1.1  jonathan   /* Copy the loadable sections.   Zero-fill any gaps less than 64k;
    292  1.1  jonathan      complain about any zero-filling, and die if we're asked to zero-fill
    293  1.1  jonathan      more than 64k. */
    294  1.1  jonathan   for (i = 0; i < ex.phcount; i++)
    295  1.1  jonathan     {
    296  1.1  jonathan       /* Unprocessable sections were handled above, so just verify that
    297  1.1  jonathan 	 the section can be loaded before copying. */
    298  1.1  jonathan       if (ph [i].type == PT_LOAD && ph [i].filesz)
    299  1.1  jonathan 	{
    300  1.1  jonathan 	  if (cur_vma != ph [i].vaddr)
    301  1.1  jonathan 	    {
    302  1.1  jonathan 	      unsigned long gap = ph [i].vaddr - cur_vma;
    303  1.1  jonathan 	      char obuf [1024];
    304  1.1  jonathan 	      if (gap > 65536)
    305  1.1  jonathan 		{
    306  1.1  jonathan 		  fprintf (stderr, "Intersegment gap (%d bytes) too large.\n",
    307  1.1  jonathan 			   gap);
    308  1.1  jonathan 		  exit (1);
    309  1.1  jonathan 		}
    310  1.1  jonathan 	      fprintf (stderr, "Warning: %d byte intersegment gap.\n", gap);
    311  1.1  jonathan 	      memset (obuf, 0, sizeof obuf);
    312  1.1  jonathan 	      while (gap)
    313  1.1  jonathan 		{
    314  1.1  jonathan 		  int count = write (outfile, obuf, (gap > sizeof obuf
    315  1.1  jonathan 						     ? sizeof obuf : gap));
    316  1.1  jonathan 		  if (count < 0)
    317  1.1  jonathan 		    {
    318  1.1  jonathan 		      fprintf (stderr, "Error writing gap: %s\n",
    319  1.1  jonathan 			       strerror (errno));
    320  1.1  jonathan 		      exit (1);
    321  1.1  jonathan 		    }
    322  1.1  jonathan 		  gap -= count;
    323  1.1  jonathan 		}
    324  1.1  jonathan 	    }
    325  1.1  jonathan fprintf (stderr, "writing %d bytes...\n", ph [i].filesz);
    326  1.1  jonathan 	  copy (outfile, infile, ph [i].offset, ph [i].filesz);
    327  1.1  jonathan 	  cur_vma = ph [i].vaddr + ph [i].filesz;
    328  1.1  jonathan 	}
    329  1.1  jonathan     }
    330  1.1  jonathan 
    331  1.1  jonathan   /* Looks like we won... */
    332  1.1  jonathan   exit (0);
    333  1.1  jonathan }
    334  1.1  jonathan 
    335  1.1  jonathan copy (out, in, offset, size)
    336  1.1  jonathan      int out, in;
    337  1.1  jonathan      off_t offset, size;
    338  1.1  jonathan {
    339  1.1  jonathan   char ibuf [4096];
    340  1.1  jonathan   int remaining, cur, count;
    341  1.1  jonathan 
    342  1.1  jonathan   /* Go the the start of the ELF symbol table... */
    343  1.1  jonathan   if (lseek (in, offset, SEEK_SET) < 0)
    344  1.1  jonathan     {
    345  1.1  jonathan       perror ("copy: lseek");
    346  1.1  jonathan       exit (1);
    347  1.1  jonathan     }
    348  1.1  jonathan 
    349  1.1  jonathan   remaining = size;
    350  1.1  jonathan   while (remaining)
    351  1.1  jonathan     {
    352  1.1  jonathan       cur = remaining;
    353  1.1  jonathan       if (cur > sizeof ibuf)
    354  1.1  jonathan 	cur = sizeof ibuf;
    355  1.1  jonathan       remaining -= cur;
    356  1.1  jonathan       if ((count = read (in, ibuf, cur)) != cur)
    357  1.1  jonathan 	{
    358  1.1  jonathan 	  fprintf (stderr, "copy: read: %s\n",
    359  1.1  jonathan 		   count ? strerror (errno) : "premature end of file");
    360  1.1  jonathan 	  exit (1);
    361  1.1  jonathan 	}
    362  1.1  jonathan       if ((count = write (out, ibuf, cur)) != cur)
    363  1.1  jonathan 	{
    364  1.1  jonathan 	  perror ("copy: write");
    365  1.1  jonathan 	  exit (1);
    366  1.1  jonathan 	}
    367  1.1  jonathan     }
    368  1.1  jonathan }
    369  1.1  jonathan 
    370  1.1  jonathan /* Combine two segments, which must be contiguous.   If pad is true, it's
    371  1.1  jonathan    okay for there to be padding between. */
    372  1.1  jonathan combine (base, new, pad)
    373  1.1  jonathan      struct sect *base, *new;
    374  1.1  jonathan      int pad;
    375  1.1  jonathan {
    376  1.1  jonathan   if (!base -> len)
    377  1.1  jonathan     *base = *new;
    378  1.1  jonathan   else if (new -> len)
    379  1.1  jonathan     {
    380  1.1  jonathan       if (base -> vaddr + base -> len != new -> vaddr)
    381  1.1  jonathan 	{
    382  1.1  jonathan 	  if (pad)
    383  1.1  jonathan 	    base -> len = new -> vaddr - base -> vaddr;
    384  1.1  jonathan 	  else
    385  1.1  jonathan 	    {
    386  1.1  jonathan 	      fprintf (stderr,
    387  1.1  jonathan 		       "Non-contiguous data can't be converted.\n");
    388  1.1  jonathan 	      exit (1);
    389  1.1  jonathan 	    }
    390  1.1  jonathan 	}
    391  1.1  jonathan       base -> len += new -> len;
    392  1.1  jonathan     }
    393  1.1  jonathan }
    394  1.1  jonathan 
    395  1.1  jonathan phcmp (h1, h2)
    396  1.1  jonathan      struct phdr *h1, *h2;
    397  1.1  jonathan {
    398  1.1  jonathan   if (h1 -> vaddr > h2 -> vaddr)
    399  1.1  jonathan     return 1;
    400  1.1  jonathan   else if (h1 -> vaddr < h2 -> vaddr)
    401  1.1  jonathan     return -1;
    402  1.1  jonathan   else
    403  1.1  jonathan     return 0;
    404  1.1  jonathan }
    405  1.1  jonathan 
    406  1.1  jonathan char *saveRead (int file, off_t offset, off_t len, char *name)
    407  1.1  jonathan {
    408  1.1  jonathan   char *tmp;
    409  1.1  jonathan   int count;
    410  1.1  jonathan   off_t off;
    411  1.1  jonathan   if ((off = lseek (file, offset, SEEK_SET)) < 0)
    412  1.1  jonathan     {
    413  1.1  jonathan       fprintf (stderr, "%s: fseek: %s\n", name, strerror (errno));
    414  1.1  jonathan       exit (1);
    415  1.1  jonathan     }
    416  1.1  jonathan   if (!(tmp = (char *)malloc (len)))
    417  1.1  jonathan     {
    418  1.1  jonathan       fprintf (stderr, "%s: Can't allocate %d bytes.\n", name, len);
    419  1.1  jonathan       exit (1);
    420  1.1  jonathan     }
    421  1.1  jonathan   count = read (file, tmp, len);
    422  1.1  jonathan   if (count != len)
    423  1.1  jonathan     {
    424  1.1  jonathan       fprintf (stderr, "%s: read: %s.\n",
    425  1.1  jonathan 	       name, count ? strerror (errno) : "End of file reached");
    426  1.1  jonathan       exit (1);
    427  1.1  jonathan     }
    428  1.1  jonathan   return tmp;
    429  1.1  jonathan }
    430