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