Home | History | Annotate | Line # | Download | only in elf2ecoff
elf2ecoff.c revision 1.3
      1  1.3  jonathan /* $NetBSD: elf2ecoff.c,v 1.3 1996/10/16 00:27:08 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.3  jonathan #include <sys/exec_elf.h>
     42  1.3  jonathan #include <sys/exec_aout.h>
     43  1.1  jonathan #include <stdio.h>
     44  1.1  jonathan #include <sys/exec_ecoff.h>
     45  1.1  jonathan #include <sys/errno.h>
     46  1.1  jonathan #include <string.h>
     47  1.1  jonathan #include <limits.h>
     48  1.1  jonathan 
     49  1.3  jonathan 
     50  1.3  jonathan /* Elf Program segment permissions, in program header flags field */
     51  1.3  jonathan 
     52  1.3  jonathan #define PF_X            (1 << 0)        /* Segment is executable */
     53  1.3  jonathan #define PF_W            (1 << 1)        /* Segment is writable */
     54  1.3  jonathan #define PF_R            (1 << 2)        /* Segment is readable */
     55  1.3  jonathan #define PF_MASKPROC     0xF0000000      /* Processor-specific reserved bits */
     56  1.3  jonathan 
     57  1.1  jonathan struct sect {
     58  1.1  jonathan   unsigned long vaddr;
     59  1.1  jonathan   unsigned long len;
     60  1.1  jonathan };
     61  1.1  jonathan 
     62  1.1  jonathan int phcmp ();
     63  1.1  jonathan char *saveRead (int file, off_t offset, off_t len, char *name);
     64  1.1  jonathan int copy (int, int, off_t, off_t);
     65  1.1  jonathan int translate_syms (int, int, off_t, off_t, off_t, off_t);
     66  1.1  jonathan extern int errno;
     67  1.1  jonathan int *symTypeTable;
     68  1.1  jonathan 
     69  1.1  jonathan main (int argc, char **argv, char **envp)
     70  1.1  jonathan {
     71  1.3  jonathan   Elf32_Ehdr ex;
     72  1.3  jonathan   Elf32_Phdr *ph;
     73  1.3  jonathan   Elf32_Shdr *sh;
     74  1.3  jonathan   Elf32_Sym *symtab;
     75  1.1  jonathan   char *shstrtab;
     76  1.1  jonathan   int strtabix, symtabix;
     77  1.1  jonathan   int i, pad;
     78  1.1  jonathan   struct sect text, data, bss;
     79  1.2  jonathan   struct ecoff_exechdr ep;
     80  1.1  jonathan   struct ecoff_scnhdr esecs [3];
     81  1.1  jonathan   int infile, outfile;
     82  1.1  jonathan   unsigned long cur_vma = ULONG_MAX;
     83  1.1  jonathan   int symflag = 0;
     84  1.1  jonathan 
     85  1.1  jonathan   text.len = data.len = bss.len = 0;
     86  1.1  jonathan   text.vaddr = data.vaddr = bss.vaddr = 0;
     87  1.1  jonathan 
     88  1.1  jonathan   /* Check args... */
     89  1.1  jonathan   if (argc < 3 || argc > 4)
     90  1.1  jonathan     {
     91  1.1  jonathan     usage:
     92  1.1  jonathan       fprintf (stderr,
     93  1.1  jonathan 	       "usage: elf2aout <elf executable> <a.out executable> [-s]\n");
     94  1.1  jonathan       exit (1);
     95  1.1  jonathan     }
     96  1.1  jonathan   if (argc == 4)
     97  1.1  jonathan     {
     98  1.1  jonathan       if (strcmp (argv [3], "-s"))
     99  1.1  jonathan 	goto usage;
    100  1.1  jonathan       symflag = 1;
    101  1.1  jonathan     }
    102  1.1  jonathan 
    103  1.1  jonathan   /* Try the input file... */
    104  1.1  jonathan   if ((infile = open (argv [1], O_RDONLY)) < 0)
    105  1.1  jonathan     {
    106  1.1  jonathan       fprintf (stderr, "Can't open %s for read: %s\n",
    107  1.1  jonathan 	       argv [1], strerror (errno));
    108  1.1  jonathan       exit (1);
    109  1.1  jonathan     }
    110  1.1  jonathan 
    111  1.1  jonathan   /* Read the header, which is at the beginning of the file... */
    112  1.1  jonathan   i = read (infile, &ex, sizeof ex);
    113  1.1  jonathan   if (i != sizeof ex)
    114  1.1  jonathan     {
    115  1.1  jonathan       fprintf (stderr, "ex: %s: %s.\n",
    116  1.1  jonathan 	       argv [1], i ? strerror (errno) : "End of file reached");
    117  1.1  jonathan       exit (1);
    118  1.1  jonathan     }
    119  1.1  jonathan 
    120  1.1  jonathan   /* Read the program headers... */
    121  1.3  jonathan   ph = (Elf32_Phdr *)saveRead (infile, ex.e_phoff,
    122  1.3  jonathan 				ex.e_phnum * sizeof (Elf32_Phdr), "ph");
    123  1.1  jonathan   /* Read the section headers... */
    124  1.3  jonathan   sh = (Elf32_Shdr *)saveRead (infile, ex.e_shoff,
    125  1.3  jonathan 				ex.e_shnum * sizeof (Elf32_Shdr), "sh");
    126  1.1  jonathan   /* Read in the section string table. */
    127  1.3  jonathan   shstrtab = saveRead (infile, sh [ex.e_shstrndx].sh_offset,
    128  1.3  jonathan 		       sh [ex.e_shstrndx].sh_size, "shstrtab");
    129  1.1  jonathan 
    130  1.1  jonathan   /* Figure out if we can cram the program header into an ECOFF
    131  1.1  jonathan      header...  Basically, we can't handle anything but loadable
    132  1.1  jonathan      segments, but we can ignore some kinds of segments.  We can't
    133  1.1  jonathan      handle holes in the address space.  Segments may be out of order,
    134  1.1  jonathan      so we sort them first. */
    135  1.1  jonathan 
    136  1.3  jonathan   qsort (ph, ex.e_phnum, sizeof (Elf32_Phdr), phcmp);
    137  1.1  jonathan 
    138  1.3  jonathan   for (i = 0; i < ex.e_phnum; i++)
    139  1.1  jonathan     {
    140  1.1  jonathan       /* Section types we can ignore... */
    141  1.3  jonathan       if (ph [i].p_type == Elf_pt_null || ph [i].p_type == Elf_pt_note ||
    142  1.3  jonathan 	  ph [i].p_type == Elf_pt_phdr || ph [i].p_type == Elf_pt_mips_reginfo)
    143  1.1  jonathan 	continue;
    144  1.1  jonathan       /* Section types we can't handle... */
    145  1.3  jonathan       else if (ph [i].p_type != Elf_pt_load)
    146  1.1  jonathan         {
    147  1.1  jonathan 	  fprintf (stderr, "Program header %d type %d can't be converted.\n");
    148  1.1  jonathan 	  exit (1);
    149  1.1  jonathan 	}
    150  1.1  jonathan       /* Writable (data) segment? */
    151  1.3  jonathan       if (ph [i].p_flags & PF_W)
    152  1.1  jonathan 	{
    153  1.1  jonathan 	  struct sect ndata, nbss;
    154  1.1  jonathan 
    155  1.3  jonathan 	  ndata.vaddr = ph [i].p_vaddr;
    156  1.3  jonathan 	  ndata.len = ph [i].p_filesz;
    157  1.3  jonathan 	  nbss.vaddr = ph [i].p_vaddr + ph [i].p_filesz;
    158  1.3  jonathan 	  nbss.len = ph [i].p_memsz - ph [i].p_filesz;
    159  1.1  jonathan 
    160  1.1  jonathan 	  combine (&data, &ndata, 0);
    161  1.1  jonathan 	  combine (&bss, &nbss, 1);
    162  1.1  jonathan 	}
    163  1.1  jonathan       else
    164  1.1  jonathan 	{
    165  1.1  jonathan 	  struct sect ntxt;
    166  1.1  jonathan 
    167  1.3  jonathan 	  ntxt.vaddr = ph [i].p_vaddr;
    168  1.3  jonathan 	  ntxt.len = ph [i].p_filesz;
    169  1.1  jonathan 
    170  1.1  jonathan 	  combine (&text, &ntxt);
    171  1.1  jonathan 	}
    172  1.1  jonathan       /* Remember the lowest segment start address. */
    173  1.3  jonathan       if (ph [i].p_vaddr < cur_vma)
    174  1.3  jonathan 	cur_vma = ph [i].p_vaddr;
    175  1.1  jonathan     }
    176  1.1  jonathan 
    177  1.1  jonathan   /* Sections must be in order to be converted... */
    178  1.1  jonathan   if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr ||
    179  1.1  jonathan       text.vaddr + text.len > data.vaddr || data.vaddr + data.len > bss.vaddr)
    180  1.1  jonathan     {
    181  1.1  jonathan       fprintf (stderr, "Sections ordering prevents a.out conversion.\n");
    182  1.1  jonathan       exit (1);
    183  1.1  jonathan     }
    184  1.1  jonathan 
    185  1.1  jonathan   /* If there's a data section but no text section, then the loader
    186  1.1  jonathan      combined everything into one section.   That needs to be the
    187  1.1  jonathan      text section, so just make the data section zero length following
    188  1.1  jonathan      text. */
    189  1.1  jonathan   if (data.len && !text.len)
    190  1.1  jonathan     {
    191  1.1  jonathan       text = data;
    192  1.1  jonathan       data.vaddr = text.vaddr + text.len;
    193  1.1  jonathan       data.len = 0;
    194  1.1  jonathan     }
    195  1.1  jonathan 
    196  1.1  jonathan   /* If there is a gap between text and data, we'll fill it when we copy
    197  1.1  jonathan      the data, so update the length of the text segment as represented in
    198  1.1  jonathan      a.out to reflect that, since a.out doesn't allow gaps in the program
    199  1.1  jonathan      address space. */
    200  1.1  jonathan   if (text.vaddr + text.len < data.vaddr)
    201  1.1  jonathan     text.len = data.vaddr - text.vaddr;
    202  1.1  jonathan 
    203  1.1  jonathan   /* We now have enough information to cons up an a.out header... */
    204  1.2  jonathan   ep.a.magic = ECOFF_OMAGIC;
    205  1.2  jonathan   ep.a.vstamp = 200;
    206  1.2  jonathan   ep.a.tsize = text.len;
    207  1.2  jonathan   ep.a.dsize = data.len;
    208  1.2  jonathan   ep.a.bsize = bss.len;
    209  1.3  jonathan   ep.a.entry = ex.e_entry;
    210  1.2  jonathan   ep.a.text_start = text.vaddr;
    211  1.2  jonathan   ep.a.data_start = data.vaddr;
    212  1.2  jonathan   ep.a.bss_start = bss.vaddr;
    213  1.2  jonathan   ep.a.gprmask = 0xf3fffffe;
    214  1.2  jonathan   bzero (&ep.a.cprmask, sizeof ep.a.cprmask);
    215  1.2  jonathan   ep.a.gp_value = 0; /* unused. */
    216  1.2  jonathan 
    217  1.2  jonathan   ep.f.f_magic = ECOFF_MAGIC_MIPSEL;
    218  1.2  jonathan   ep.f.f_nscns = 3;
    219  1.2  jonathan   ep.f.f_timdat = 0;	/* bogus */
    220  1.2  jonathan   ep.f.f_symptr = 0;
    221  1.2  jonathan   ep.f.f_nsyms = 0;
    222  1.2  jonathan   ep.f.f_opthdr = sizeof ep.a;
    223  1.2  jonathan   ep.f.f_flags = 0x100f; /* Stripped, not sharable. */
    224  1.2  jonathan 
    225  1.2  jonathan   strcpy (esecs [0].s_name, ".text");
    226  1.2  jonathan   strcpy (esecs [1].s_name, ".data");
    227  1.2  jonathan   strcpy (esecs [2].s_name, ".bss");
    228  1.2  jonathan   esecs [0].s_paddr = esecs [0].s_vaddr = ep.a.text_start;
    229  1.2  jonathan   esecs [1].s_paddr = esecs [1].s_vaddr = ep.a.data_start;
    230  1.2  jonathan   esecs [2].s_paddr = esecs [2].s_vaddr = ep.a.bss_start;
    231  1.2  jonathan   esecs [0].s_size = ep.a.tsize;
    232  1.2  jonathan   esecs [1].s_size = ep.a.dsize;
    233  1.2  jonathan   esecs [2].s_size = ep.a.bsize;
    234  1.2  jonathan 
    235  1.2  jonathan   esecs [0].s_scnptr = ECOFF_TXTOFF (&ep);
    236  1.2  jonathan   esecs [1].s_scnptr = ECOFF_DATOFF (&ep);
    237  1.2  jonathan   esecs [2].s_scnptr = esecs [1].s_scnptr +
    238  1.2  jonathan 	  ECOFF_ROUND (esecs [1].s_size, ECOFF_SEGMENT_ALIGNMENT (&ep));
    239  1.2  jonathan   esecs [0].s_relptr = esecs [1].s_relptr
    240  1.2  jonathan 	  = esecs [2].s_relptr = 0;
    241  1.2  jonathan   esecs [0].s_lnnoptr = esecs [1].s_lnnoptr
    242  1.2  jonathan 	  = esecs [2].s_lnnoptr = 0;
    243  1.2  jonathan   esecs [0].s_nreloc = esecs [1].s_nreloc = esecs [2].s_nreloc = 0;
    244  1.2  jonathan   esecs [0].s_nlnno = esecs [1].s_nlnno = esecs [2].s_nlnno = 0;
    245  1.2  jonathan   esecs [0].s_flags = 0x20;
    246  1.2  jonathan   esecs [1].s_flags = 0x40;
    247  1.2  jonathan   esecs [2].s_flags = 0x82;
    248  1.1  jonathan 
    249  1.1  jonathan   /* Make the output file... */
    250  1.1  jonathan   if ((outfile = open (argv [2], O_WRONLY | O_CREAT, 0777)) < 0)
    251  1.1  jonathan     {
    252  1.1  jonathan       fprintf (stderr, "Unable to create %s: %s\n", argv [2], strerror (errno));
    253  1.1  jonathan       exit (1);
    254  1.1  jonathan     }
    255  1.1  jonathan 
    256  1.1  jonathan   /* Write the headers... */
    257  1.2  jonathan   i = write (outfile, &ep.f, sizeof ep.f);
    258  1.2  jonathan   if (i != sizeof ep.f)
    259  1.1  jonathan     {
    260  1.2  jonathan       perror ("ep.f: write");
    261  1.1  jonathan       exit (1);
    262  1.1  jonathan 
    263  1.1  jonathan   for (i = 0; i < 6; i++)
    264  1.1  jonathan     {
    265  1.1  jonathan       printf ("Section %d: %s phys %x  size %x  file offset %x\n",
    266  1.2  jonathan 	      i, esecs [i].s_name, esecs [i].s_paddr,
    267  1.2  jonathan 	      esecs [i].s_size, esecs [i].s_scnptr);
    268  1.1  jonathan     }
    269  1.1  jonathan     }
    270  1.1  jonathan   fprintf (stderr, "wrote %d byte file header.\n", i);
    271  1.1  jonathan 
    272  1.2  jonathan   i = write (outfile, &ep.a, sizeof ep.a);
    273  1.2  jonathan   if (i != sizeof ep.a)
    274  1.1  jonathan     {
    275  1.2  jonathan       perror ("ep.a: write");
    276  1.1  jonathan       exit (1);
    277  1.1  jonathan     }
    278  1.1  jonathan   fprintf (stderr, "wrote %d byte a.out header.\n", i);
    279  1.1  jonathan 
    280  1.1  jonathan   i = write (outfile, &esecs, sizeof esecs);
    281  1.1  jonathan   if (i != sizeof esecs)
    282  1.1  jonathan     {
    283  1.1  jonathan       perror ("esecs: write");
    284  1.1  jonathan       exit (1);
    285  1.1  jonathan     }
    286  1.1  jonathan   fprintf (stderr, "wrote %d bytes of section headers.\n", i);
    287  1.1  jonathan 
    288  1.2  jonathan   if (pad = ((sizeof ep.f + sizeof ep.a + sizeof esecs) & 15))
    289  1.1  jonathan     {
    290  1.1  jonathan       pad = 16 - pad;
    291  1.1  jonathan       i = write (outfile, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0", pad);
    292  1.1  jonathan       if (i < 0)
    293  1.1  jonathan 	{
    294  1.1  jonathan 	  perror ("ipad: write");
    295  1.1  jonathan 	  exit (1);
    296  1.1  jonathan 	}
    297  1.1  jonathan       fprintf (stderr, "wrote %d byte pad.\n", i);
    298  1.1  jonathan     }
    299  1.1  jonathan 
    300  1.1  jonathan   /* Copy the loadable sections.   Zero-fill any gaps less than 64k;
    301  1.1  jonathan      complain about any zero-filling, and die if we're asked to zero-fill
    302  1.1  jonathan      more than 64k. */
    303  1.3  jonathan   for (i = 0; i < ex.e_phnum; i++)
    304  1.1  jonathan     {
    305  1.1  jonathan       /* Unprocessable sections were handled above, so just verify that
    306  1.1  jonathan 	 the section can be loaded before copying. */
    307  1.3  jonathan       if (ph [i].p_type == Elf_pt_load && ph [i].p_filesz)
    308  1.1  jonathan 	{
    309  1.3  jonathan 	  if (cur_vma != ph [i].p_vaddr)
    310  1.1  jonathan 	    {
    311  1.3  jonathan 	      unsigned long gap = ph [i].p_vaddr - cur_vma;
    312  1.1  jonathan 	      char obuf [1024];
    313  1.1  jonathan 	      if (gap > 65536)
    314  1.1  jonathan 		{
    315  1.1  jonathan 		  fprintf (stderr, "Intersegment gap (%d bytes) too large.\n",
    316  1.1  jonathan 			   gap);
    317  1.1  jonathan 		  exit (1);
    318  1.1  jonathan 		}
    319  1.1  jonathan 	      fprintf (stderr, "Warning: %d byte intersegment gap.\n", gap);
    320  1.1  jonathan 	      memset (obuf, 0, sizeof obuf);
    321  1.1  jonathan 	      while (gap)
    322  1.1  jonathan 		{
    323  1.1  jonathan 		  int count = write (outfile, obuf, (gap > sizeof obuf
    324  1.1  jonathan 						     ? sizeof obuf : gap));
    325  1.1  jonathan 		  if (count < 0)
    326  1.1  jonathan 		    {
    327  1.1  jonathan 		      fprintf (stderr, "Error writing gap: %s\n",
    328  1.1  jonathan 			       strerror (errno));
    329  1.1  jonathan 		      exit (1);
    330  1.1  jonathan 		    }
    331  1.1  jonathan 		  gap -= count;
    332  1.1  jonathan 		}
    333  1.1  jonathan 	    }
    334  1.3  jonathan fprintf (stderr, "writing %d bytes...\n", ph [i].p_filesz);
    335  1.3  jonathan 	  copy (outfile, infile, ph [i].p_offset, ph [i].p_filesz);
    336  1.3  jonathan 	  cur_vma = ph [i].p_vaddr + ph [i].p_filesz;
    337  1.1  jonathan 	}
    338  1.1  jonathan     }
    339  1.1  jonathan 
    340  1.1  jonathan   /* Looks like we won... */
    341  1.1  jonathan   exit (0);
    342  1.1  jonathan }
    343  1.1  jonathan 
    344  1.1  jonathan copy (out, in, offset, size)
    345  1.1  jonathan      int out, in;
    346  1.1  jonathan      off_t offset, size;
    347  1.1  jonathan {
    348  1.1  jonathan   char ibuf [4096];
    349  1.1  jonathan   int remaining, cur, count;
    350  1.1  jonathan 
    351  1.1  jonathan   /* Go the the start of the ELF symbol table... */
    352  1.1  jonathan   if (lseek (in, offset, SEEK_SET) < 0)
    353  1.1  jonathan     {
    354  1.1  jonathan       perror ("copy: lseek");
    355  1.1  jonathan       exit (1);
    356  1.1  jonathan     }
    357  1.1  jonathan 
    358  1.1  jonathan   remaining = size;
    359  1.1  jonathan   while (remaining)
    360  1.1  jonathan     {
    361  1.1  jonathan       cur = remaining;
    362  1.1  jonathan       if (cur > sizeof ibuf)
    363  1.1  jonathan 	cur = sizeof ibuf;
    364  1.1  jonathan       remaining -= cur;
    365  1.1  jonathan       if ((count = read (in, ibuf, cur)) != cur)
    366  1.1  jonathan 	{
    367  1.1  jonathan 	  fprintf (stderr, "copy: read: %s\n",
    368  1.1  jonathan 		   count ? strerror (errno) : "premature end of file");
    369  1.1  jonathan 	  exit (1);
    370  1.1  jonathan 	}
    371  1.1  jonathan       if ((count = write (out, ibuf, cur)) != cur)
    372  1.1  jonathan 	{
    373  1.1  jonathan 	  perror ("copy: write");
    374  1.1  jonathan 	  exit (1);
    375  1.1  jonathan 	}
    376  1.1  jonathan     }
    377  1.1  jonathan }
    378  1.1  jonathan 
    379  1.1  jonathan /* Combine two segments, which must be contiguous.   If pad is true, it's
    380  1.1  jonathan    okay for there to be padding between. */
    381  1.1  jonathan combine (base, new, pad)
    382  1.1  jonathan      struct sect *base, *new;
    383  1.1  jonathan      int pad;
    384  1.1  jonathan {
    385  1.1  jonathan   if (!base -> len)
    386  1.1  jonathan     *base = *new;
    387  1.1  jonathan   else if (new -> len)
    388  1.1  jonathan     {
    389  1.1  jonathan       if (base -> vaddr + base -> len != new -> vaddr)
    390  1.1  jonathan 	{
    391  1.1  jonathan 	  if (pad)
    392  1.1  jonathan 	    base -> len = new -> vaddr - base -> vaddr;
    393  1.1  jonathan 	  else
    394  1.1  jonathan 	    {
    395  1.1  jonathan 	      fprintf (stderr,
    396  1.1  jonathan 		       "Non-contiguous data can't be converted.\n");
    397  1.1  jonathan 	      exit (1);
    398  1.1  jonathan 	    }
    399  1.1  jonathan 	}
    400  1.1  jonathan       base -> len += new -> len;
    401  1.1  jonathan     }
    402  1.1  jonathan }
    403  1.1  jonathan 
    404  1.3  jonathan int
    405  1.1  jonathan phcmp (h1, h2)
    406  1.3  jonathan      Elf32_Phdr *h1, *h2;
    407  1.1  jonathan {
    408  1.3  jonathan   if (h1 -> p_vaddr > h2 -> p_vaddr)
    409  1.1  jonathan     return 1;
    410  1.3  jonathan   else if (h1 -> p_vaddr < h2 -> p_vaddr)
    411  1.1  jonathan     return -1;
    412  1.1  jonathan   else
    413  1.1  jonathan     return 0;
    414  1.1  jonathan }
    415  1.1  jonathan 
    416  1.1  jonathan char *saveRead (int file, off_t offset, off_t len, char *name)
    417  1.1  jonathan {
    418  1.1  jonathan   char *tmp;
    419  1.1  jonathan   int count;
    420  1.1  jonathan   off_t off;
    421  1.1  jonathan   if ((off = lseek (file, offset, SEEK_SET)) < 0)
    422  1.1  jonathan     {
    423  1.1  jonathan       fprintf (stderr, "%s: fseek: %s\n", name, strerror (errno));
    424  1.1  jonathan       exit (1);
    425  1.1  jonathan     }
    426  1.1  jonathan   if (!(tmp = (char *)malloc (len)))
    427  1.1  jonathan     {
    428  1.1  jonathan       fprintf (stderr, "%s: Can't allocate %d bytes.\n", name, len);
    429  1.1  jonathan       exit (1);
    430  1.1  jonathan     }
    431  1.1  jonathan   count = read (file, tmp, len);
    432  1.1  jonathan   if (count != len)
    433  1.1  jonathan     {
    434  1.1  jonathan       fprintf (stderr, "%s: read: %s.\n",
    435  1.1  jonathan 	       name, count ? strerror (errno) : "End of file reached");
    436  1.1  jonathan       exit (1);
    437  1.1  jonathan     }
    438  1.1  jonathan   return tmp;
    439  1.1  jonathan }
    440