Home | History | Annotate | Line # | Download | only in elf2aout
elf2aout.c revision 1.3
      1  1.3  jonathan /* $NetBSD: elf2aout.c,v 1.3 1996/10/16 00:27:05 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 /* elf2aout.c
     32  1.1  jonathan 
     33  1.1  jonathan    This program converts an elf executable to a NetBSD a.out executable.
     34  1.1  jonathan    The minimal symbol table is copied, but the debugging symbols and
     35  1.1  jonathan    other informational sections are not. */
     36  1.1  jonathan 
     37  1.1  jonathan #include <sys/types.h>
     38  1.1  jonathan #include <fcntl.h>
     39  1.1  jonathan #include <unistd.h>
     40  1.3  jonathan #include <sys/exec_elf.h>
     41  1.3  jonathan #include <sys/exec_aout.h>
     42  1.1  jonathan #include <stdio.h>
     43  1.1  jonathan #include <a.out.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.3  jonathan 
     49  1.3  jonathan /* Elf Program segment permissions, in program header flags field */
     50  1.3  jonathan 
     51  1.3  jonathan #define PF_X            (1 << 0)        /* Segment is executable */
     52  1.3  jonathan #define PF_W            (1 << 1)        /* Segment is writable */
     53  1.3  jonathan #define PF_R            (1 << 2)        /* Segment is readable */
     54  1.3  jonathan #define PF_MASKPROC     0xF0000000      /* Processor-specific reserved bits */
     55  1.3  jonathan 
     56  1.1  jonathan struct sect {
     57  1.1  jonathan   unsigned long vaddr;
     58  1.1  jonathan   unsigned long len;
     59  1.1  jonathan };
     60  1.1  jonathan int phcmp ();
     61  1.1  jonathan char *saveRead (int file, off_t offset, off_t len, char *name);
     62  1.1  jonathan int copy (int, int, off_t, off_t);
     63  1.1  jonathan int translate_syms (int, int, off_t, off_t, off_t, off_t);
     64  1.1  jonathan extern int errno;
     65  1.1  jonathan int *symTypeTable;
     66  1.1  jonathan 
     67  1.1  jonathan main (int argc, char **argv, char **envp)
     68  1.1  jonathan {
     69  1.3  jonathan   Elf32_Ehdr ex;
     70  1.3  jonathan   Elf32_Phdr *ph;
     71  1.3  jonathan   Elf32_Shdr *sh;
     72  1.1  jonathan   struct sym *symtab;
     73  1.1  jonathan   char *shstrtab;
     74  1.1  jonathan   int strtabix, symtabix;
     75  1.1  jonathan   int i;
     76  1.1  jonathan   struct sect text, data, bss;
     77  1.1  jonathan   struct exec aex;
     78  1.1  jonathan   int infile, outfile;
     79  1.1  jonathan   unsigned long cur_vma = ULONG_MAX;
     80  1.1  jonathan   int symflag = 0;
     81  1.1  jonathan 
     82  1.1  jonathan   text.len = data.len = bss.len = 0;
     83  1.1  jonathan   text.vaddr = data.vaddr = bss.vaddr = 0;
     84  1.1  jonathan 
     85  1.1  jonathan   /* Check args... */
     86  1.1  jonathan   if (argc < 3 || argc > 4)
     87  1.1  jonathan     {
     88  1.1  jonathan     usage:
     89  1.1  jonathan       fprintf (stderr,
     90  1.1  jonathan 	       "usage: elf2aout <elf executable> <a.out executable> [-s]\n");
     91  1.1  jonathan       exit (1);
     92  1.1  jonathan     }
     93  1.1  jonathan   if (argc == 4)
     94  1.1  jonathan     {
     95  1.1  jonathan       if (strcmp (argv [3], "-s"))
     96  1.1  jonathan 	goto usage;
     97  1.1  jonathan       symflag = 1;
     98  1.1  jonathan     }
     99  1.1  jonathan 
    100  1.1  jonathan   /* Try the input file... */
    101  1.1  jonathan   if ((infile = open (argv [1], O_RDONLY)) < 0)
    102  1.1  jonathan     {
    103  1.1  jonathan       fprintf (stderr, "Can't open %s for read: %s\n",
    104  1.1  jonathan 	       argv [1], strerror (errno));
    105  1.1  jonathan       exit (1);
    106  1.1  jonathan     }
    107  1.1  jonathan 
    108  1.1  jonathan   /* Read the header, which is at the beginning of the file... */
    109  1.1  jonathan   i = read (infile, &ex, sizeof ex);
    110  1.1  jonathan   if (i != sizeof ex)
    111  1.1  jonathan     {
    112  1.1  jonathan       fprintf (stderr, "ex: %s: %s.\n",
    113  1.1  jonathan 	       argv [1], i ? strerror (errno) : "End of file reached");
    114  1.1  jonathan       exit (1);
    115  1.1  jonathan     }
    116  1.1  jonathan 
    117  1.1  jonathan   /* Read the program headers... */
    118  1.3  jonathan   ph = (Elf32_Phdr *)saveRead (infile, ex.e_phoff,
    119  1.3  jonathan 				ex.e_phnum * sizeof (Elf32_Phdr), "ph");
    120  1.1  jonathan   /* Read the section headers... */
    121  1.3  jonathan   sh = (Elf32_Shdr *)saveRead (infile, ex.e_shoff,
    122  1.3  jonathan 				ex.e_shnum * sizeof (Elf32_Shdr), "sh");
    123  1.1  jonathan   /* Read in the section string table. */
    124  1.3  jonathan   shstrtab = saveRead (infile, sh [ex.e_shstrndx].sh_offset,
    125  1.3  jonathan 		       sh [ex.e_shstrndx].sh_size, "shstrtab");
    126  1.1  jonathan 
    127  1.1  jonathan   /* Find space for a table matching ELF section indices to a.out symbol
    128  1.1  jonathan      types. */
    129  1.3  jonathan   symTypeTable = (int *)malloc (ex.e_shnum * sizeof (int));
    130  1.1  jonathan   if (!symTypeTable)
    131  1.1  jonathan     {
    132  1.1  jonathan       fprintf (stderr, "symTypeTable: can't allocate.\n");
    133  1.1  jonathan       exit (1);
    134  1.1  jonathan     }
    135  1.3  jonathan   memset (symTypeTable, 0, ex.e_shnum * sizeof (int));
    136  1.1  jonathan 
    137  1.1  jonathan   /* Look for the symbol table and string table...
    138  1.1  jonathan      Also map section indices to symbol types for a.out */
    139  1.3  jonathan   for (i = 0; i < ex.e_shnum; i++)
    140  1.1  jonathan     {
    141  1.3  jonathan       char *name = shstrtab + sh [i].sh_name;
    142  1.1  jonathan       if (!strcmp (name, ".symtab"))
    143  1.1  jonathan 	symtabix = i;
    144  1.1  jonathan       else if (!strcmp (name, ".strtab"))
    145  1.1  jonathan 	strtabix = i;
    146  1.1  jonathan       else if (!strcmp (name, ".text") || !strcmp (name, ".rodata"))
    147  1.1  jonathan 	symTypeTable [i] = N_TEXT;
    148  1.1  jonathan       else if (!strcmp (name, ".data") || !strcmp (name, ".sdata") ||
    149  1.1  jonathan 	       !strcmp (name, ".lit4") || !strcmp (name, ".lit8"))
    150  1.1  jonathan 	symTypeTable [i] = N_DATA;
    151  1.1  jonathan       else if (!strcmp (name, ".bss") || !strcmp (name, ".sbss"))
    152  1.1  jonathan 	symTypeTable [i] = N_BSS;
    153  1.1  jonathan     }
    154  1.1  jonathan 
    155  1.1  jonathan   /* Figure out if we can cram the program header into an a.out header...
    156  1.1  jonathan      Basically, we can't handle anything but loadable segments, but we
    157  1.1  jonathan      can ignore some kinds of segments.   We can't handle holes in the
    158  1.1  jonathan      address space, and we handle start addresses other than 0x1000 by
    159  1.1  jonathan      hoping that the loader will know where to load - a.out doesn't have
    160  1.1  jonathan      an explicit load address.   Segments may be out of order, so we
    161  1.1  jonathan      sort them first. */
    162  1.3  jonathan   qsort (ph, ex.e_phnum, sizeof (Elf32_Phdr), phcmp);
    163  1.3  jonathan   for (i = 0; i < ex.e_phnum; i++)
    164  1.1  jonathan     {
    165  1.1  jonathan       /* Section types we can ignore... */
    166  1.3  jonathan       if (ph [i].p_type == Elf_pt_null || ph [i].p_type == Elf_pt_note ||
    167  1.3  jonathan 	  ph [i].p_type == Elf_pt_phdr || ph [i].p_type == Elf_pt_mips_reginfo)
    168  1.1  jonathan 	continue;
    169  1.1  jonathan       /* Section types we can't handle... */
    170  1.3  jonathan       else if (ph [i].p_type != Elf_pt_load)
    171  1.1  jonathan         {
    172  1.1  jonathan 	  fprintf (stderr, "Program header %d type %d can't be converted.\n");
    173  1.1  jonathan 	  exit (1);
    174  1.1  jonathan 	}
    175  1.1  jonathan       /* Writable (data) segment? */
    176  1.3  jonathan       if (ph [i].p_flags & PF_W)
    177  1.1  jonathan 	{
    178  1.1  jonathan 	  struct sect ndata, nbss;
    179  1.1  jonathan 
    180  1.3  jonathan 	  ndata.vaddr = ph [i].p_vaddr;
    181  1.3  jonathan 	  ndata.len = ph [i].p_filesz;
    182  1.3  jonathan 	  nbss.vaddr = ph [i].p_vaddr + ph [i].p_filesz;
    183  1.3  jonathan 	  nbss.len = ph [i].p_memsz - ph [i].p_filesz;
    184  1.1  jonathan 
    185  1.1  jonathan 	  combine (&data, &ndata, 0);
    186  1.1  jonathan 	  combine (&bss, &nbss, 1);
    187  1.1  jonathan 	}
    188  1.1  jonathan       else
    189  1.1  jonathan 	{
    190  1.1  jonathan 	  struct sect ntxt;
    191  1.1  jonathan 
    192  1.3  jonathan 	  ntxt.vaddr = ph [i].p_vaddr;
    193  1.3  jonathan 	  ntxt.len = ph [i].p_filesz;
    194  1.1  jonathan 
    195  1.1  jonathan 	  combine (&text, &ntxt);
    196  1.1  jonathan 	}
    197  1.1  jonathan       /* Remember the lowest segment start address. */
    198  1.3  jonathan       if (ph [i].p_vaddr < cur_vma)
    199  1.3  jonathan 	cur_vma = ph [i].p_vaddr;
    200  1.1  jonathan     }
    201  1.1  jonathan 
    202  1.1  jonathan   /* Sections must be in order to be converted... */
    203  1.1  jonathan   if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr ||
    204  1.1  jonathan       text.vaddr + text.len > data.vaddr || data.vaddr + data.len > bss.vaddr)
    205  1.1  jonathan     {
    206  1.1  jonathan       fprintf (stderr, "Sections ordering prevents a.out conversion.\n");
    207  1.1  jonathan       exit (1);
    208  1.1  jonathan     }
    209  1.1  jonathan 
    210  1.1  jonathan   /* If there's a data section but no text section, then the loader
    211  1.1  jonathan      combined everything into one section.   That needs to be the
    212  1.1  jonathan      text section, so just make the data section zero length following
    213  1.1  jonathan      text. */
    214  1.1  jonathan   if (data.len && !text.len)
    215  1.1  jonathan     {
    216  1.1  jonathan       text = data;
    217  1.1  jonathan       data.vaddr = text.vaddr + text.len;
    218  1.1  jonathan       data.len = 0;
    219  1.1  jonathan     }
    220  1.1  jonathan 
    221  1.1  jonathan   /* If there is a gap between text and data, we'll fill it when we copy
    222  1.1  jonathan      the data, so update the length of the text segment as represented in
    223  1.1  jonathan      a.out to reflect that, since a.out doesn't allow gaps in the program
    224  1.1  jonathan      address space. */
    225  1.1  jonathan   if (text.vaddr + text.len < data.vaddr)
    226  1.1  jonathan     text.len = data.vaddr - text.vaddr;
    227  1.1  jonathan 
    228  1.1  jonathan   /* We now have enough information to cons up an a.out header... */
    229  1.1  jonathan   aex.a_midmag = htonl ((symflag << 26) | (MID_PMAX << 16) | OMAGIC);
    230  1.1  jonathan   aex.a_text = text.len;
    231  1.1  jonathan   aex.a_data = data.len;
    232  1.1  jonathan   aex.a_bss = bss.len;
    233  1.3  jonathan   aex.a_entry = ex.e_entry;
    234  1.1  jonathan   aex.a_syms = (sizeof (struct nlist) *
    235  1.1  jonathan 		(symtabix != -1
    236  1.3  jonathan 		 ? sh [symtabix].sh_size / sizeof (Elf32_Sym) : 0));
    237  1.1  jonathan   aex.a_trsize = 0;
    238  1.1  jonathan   aex.a_drsize = 0;
    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   /* Write the header... */
    247  1.1  jonathan   i = write (outfile, &aex, sizeof aex);
    248  1.1  jonathan   if (i != sizeof aex)
    249  1.1  jonathan     {
    250  1.1  jonathan       perror ("aex: write");
    251  1.1  jonathan       exit (1);
    252  1.1  jonathan     }
    253  1.1  jonathan 
    254  1.1  jonathan   /* Copy the loadable sections.   Zero-fill any gaps less than 64k;
    255  1.1  jonathan      complain about any zero-filling, and die if we're asked to zero-fill
    256  1.1  jonathan      more than 64k. */
    257  1.3  jonathan   for (i = 0; i < ex.e_phnum; i++)
    258  1.1  jonathan     {
    259  1.1  jonathan       /* Unprocessable sections were handled above, so just verify that
    260  1.1  jonathan 	 the section can be loaded before copying. */
    261  1.3  jonathan       if (ph [i].p_type == Elf_pt_load && ph [i].p_filesz)
    262  1.1  jonathan 	{
    263  1.3  jonathan 	  if (cur_vma != ph [i].p_vaddr)
    264  1.1  jonathan 	    {
    265  1.3  jonathan 	      unsigned long gap = ph [i].p_vaddr - cur_vma;
    266  1.1  jonathan 	      char obuf [1024];
    267  1.1  jonathan 	      if (gap > 65536)
    268  1.1  jonathan 		{
    269  1.1  jonathan 		  fprintf (stderr, "Intersegment gap (%d bytes) too large.\n",
    270  1.1  jonathan 			   gap);
    271  1.1  jonathan 		  exit (1);
    272  1.1  jonathan 		}
    273  1.1  jonathan 	      fprintf (stderr, "Warning: %d byte intersegment gap.\n", gap);
    274  1.1  jonathan 	      memset (obuf, 0, sizeof obuf);
    275  1.1  jonathan 	      while (gap)
    276  1.1  jonathan 		{
    277  1.1  jonathan 		  int count = write (outfile, obuf, (gap > sizeof obuf
    278  1.1  jonathan 						     ? sizeof obuf : gap));
    279  1.1  jonathan 		  if (count < 0)
    280  1.1  jonathan 		    {
    281  1.1  jonathan 		      fprintf (stderr, "Error writing gap: %s\n",
    282  1.1  jonathan 			       strerror (errno));
    283  1.1  jonathan 		      exit (1);
    284  1.1  jonathan 		    }
    285  1.1  jonathan 		  gap -= count;
    286  1.1  jonathan 		}
    287  1.1  jonathan 	    }
    288  1.3  jonathan 	  copy (outfile, infile, ph [i].p_offset, ph [i].p_filesz);
    289  1.3  jonathan 	  cur_vma = ph [i].p_vaddr + ph [i].p_filesz;
    290  1.1  jonathan 	}
    291  1.1  jonathan     }
    292  1.1  jonathan 
    293  1.1  jonathan   /* Copy and translate the symbol table... */
    294  1.3  jonathan   translate_syms (outfile, infile,
    295  1.3  jonathan 		  sh [symtabix].sh_offset, sh [symtabix].sh_size,
    296  1.3  jonathan 		  sh [strtabix].sh_offset, sh [strtabix].sh_size);
    297  1.1  jonathan 
    298  1.1  jonathan   /* Looks like we won... */
    299  1.1  jonathan   exit (0);
    300  1.1  jonathan }
    301  1.1  jonathan 
    302  1.1  jonathan /* translate_syms (out, in, offset, size)
    303  1.1  jonathan 
    304  1.1  jonathan    Read the ELF symbol table from in at offset; translate it into a.out
    305  1.1  jonathan    nlist format and write it to out. */
    306  1.1  jonathan 
    307  1.1  jonathan translate_syms (out, in, symoff, symsize, stroff, strsize)
    308  1.1  jonathan      int out, in;
    309  1.1  jonathan      off_t symoff, symsize;
    310  1.1  jonathan      off_t stroff, strsize;
    311  1.1  jonathan {
    312  1.1  jonathan # define SYMS_PER_PASS	64
    313  1.3  jonathan   Elf32_Sym inbuf [64];
    314  1.1  jonathan   struct nlist outbuf [64];
    315  1.1  jonathan   int i, remaining, cur;
    316  1.1  jonathan   char *oldstrings;
    317  1.1  jonathan   char *newstrings, *nsp;
    318  1.1  jonathan   int newstringsize;
    319  1.1  jonathan 
    320  1.1  jonathan   /* Zero the unused fields in the output buffer.. */
    321  1.1  jonathan   memset (outbuf, 0, sizeof outbuf);
    322  1.1  jonathan 
    323  1.1  jonathan   /* Find number of symbols to process... */
    324  1.3  jonathan   remaining = symsize / sizeof (Elf32_Sym);
    325  1.1  jonathan 
    326  1.1  jonathan   /* Suck in the old string table... */
    327  1.1  jonathan   oldstrings = saveRead (in, stroff, strsize, "string table");
    328  1.1  jonathan 
    329  1.1  jonathan   /* Allocate space for the new one.   XXX We make the wild assumption that
    330  1.1  jonathan      no two symbol table entries will point at the same place in the
    331  1.1  jonathan      string table - if that assumption is bad, this could easily blow up. */
    332  1.1  jonathan   newstringsize = strsize + remaining;
    333  1.1  jonathan   newstrings = (char *)malloc (newstringsize);
    334  1.1  jonathan   if (!newstrings)
    335  1.1  jonathan     {
    336  1.1  jonathan       fprintf (stderr, "No memory for new string table!\n");
    337  1.1  jonathan       exit (1);
    338  1.1  jonathan     }
    339  1.1  jonathan   /* Initialize the table pointer... */
    340  1.1  jonathan   nsp = newstrings;
    341  1.1  jonathan 
    342  1.1  jonathan   /* Go the the start of the ELF symbol table... */
    343  1.1  jonathan   if (lseek (in, symoff, SEEK_SET) < 0)
    344  1.1  jonathan     {
    345  1.1  jonathan       perror ("translate_syms: lseek");
    346  1.1  jonathan       exit (1);
    347  1.1  jonathan     }
    348  1.1  jonathan 
    349  1.1  jonathan   /* Translate and copy symbols... */
    350  1.1  jonathan   while (remaining)
    351  1.1  jonathan     {
    352  1.1  jonathan       cur = remaining;
    353  1.1  jonathan       if (cur > SYMS_PER_PASS)
    354  1.1  jonathan 	cur = SYMS_PER_PASS;
    355  1.1  jonathan       remaining -= cur;
    356  1.3  jonathan       if ((i = read (in, inbuf, cur * sizeof (Elf32_Sym)))
    357  1.3  jonathan 	  != cur * sizeof (Elf32_Sym))
    358  1.1  jonathan 	{
    359  1.1  jonathan 	  if (i < 0)
    360  1.1  jonathan 	    perror ("translate_syms");
    361  1.1  jonathan 	  else
    362  1.1  jonathan 	    fprintf (stderr, "translate_syms: premature end of file.\n");
    363  1.1  jonathan 	  exit (1);
    364  1.1  jonathan 	}
    365  1.1  jonathan 
    366  1.1  jonathan       /* Do the translation... */
    367  1.1  jonathan       for (i = 0; i < cur; i++)
    368  1.1  jonathan 	{
    369  1.3  jonathan 	  int binding, type;
    370  1.3  jonathan 
    371  1.1  jonathan 	  /* Copy the symbol into the new table, but prepend an underscore. */
    372  1.1  jonathan 	  *nsp = '_';
    373  1.3  jonathan 	  strcpy (nsp + 1, oldstrings + inbuf [i].st_name);
    374  1.1  jonathan 	  outbuf [i].n_un.n_strx = nsp - newstrings + 4;
    375  1.1  jonathan 	  nsp += strlen (nsp) + 1;
    376  1.1  jonathan 
    377  1.3  jonathan 	  type = ELF_SYM_TYPE(inbuf[i].st_info);
    378  1.3  jonathan 	  binding = ELF_SYM_BIND(inbuf[i].st_info);
    379  1.3  jonathan 
    380  1.1  jonathan 	  /* Convert ELF symbol type/section/etc info into a.out type info. */
    381  1.3  jonathan 	  if (type == Elf_estt_file)
    382  1.1  jonathan 	    outbuf [i].n_type = N_FN;
    383  1.3  jonathan 	  else if (inbuf [i].st_shndx == Elf_eshn_undefined)
    384  1.1  jonathan 	    outbuf [i].n_type = N_UNDF;
    385  1.3  jonathan 	  else if (inbuf [i].st_shndx == Elf_eshn_absolute)
    386  1.1  jonathan 	    outbuf [i].n_type = N_ABS;
    387  1.3  jonathan 	  else if (inbuf [i].st_shndx == Elf_eshn_common ||
    388  1.3  jonathan 		 inbuf [i].st_shndx == Elf_eshn_mips_acommon)
    389  1.1  jonathan 	    outbuf [i].n_type = N_COMM;
    390  1.1  jonathan 	  else
    391  1.3  jonathan 	    outbuf [i].n_type = symTypeTable [inbuf [i].st_shndx];
    392  1.3  jonathan 	  if (binding == Elf_estb_global)
    393  1.1  jonathan 	    outbuf [i].n_type |= N_EXT;
    394  1.1  jonathan 	  /* Symbol values in executables should be compatible. */
    395  1.3  jonathan 	  outbuf [i].n_value = inbuf [i].st_value;
    396  1.1  jonathan 	}
    397  1.1  jonathan       /* Write out the symbols... */
    398  1.1  jonathan       if ((i = write (out, outbuf, cur * sizeof (struct nlist)))
    399  1.1  jonathan 	  != cur * sizeof (struct nlist))
    400  1.1  jonathan 	{
    401  1.1  jonathan 	  fprintf (stderr, "translate_syms: write: %s\n", strerror (errno));
    402  1.1  jonathan 	  exit (1);
    403  1.1  jonathan 	}
    404  1.1  jonathan     }
    405  1.1  jonathan   /* Write out the string table length... */
    406  1.1  jonathan   if (write (out, &newstringsize, sizeof newstringsize)
    407  1.1  jonathan       != sizeof newstringsize)
    408  1.1  jonathan     {
    409  1.1  jonathan       fprintf (stderr,
    410  1.1  jonathan 	       "translate_syms: newstringsize: %s\n", strerror (errno));
    411  1.1  jonathan       exit (1);
    412  1.1  jonathan     }
    413  1.1  jonathan   /* Write out the string table... */
    414  1.1  jonathan   if (write (out, newstrings, newstringsize) != newstringsize)
    415  1.1  jonathan     {
    416  1.1  jonathan       fprintf (stderr, "translate_syms: newstrings: %s\n", strerror (errno));
    417  1.1  jonathan       exit (1);
    418  1.1  jonathan     }
    419  1.1  jonathan }
    420  1.1  jonathan 
    421  1.1  jonathan copy (out, in, offset, size)
    422  1.1  jonathan      int out, in;
    423  1.1  jonathan      off_t offset, size;
    424  1.1  jonathan {
    425  1.1  jonathan   char ibuf [4096];
    426  1.1  jonathan   int remaining, cur, count;
    427  1.1  jonathan 
    428  1.1  jonathan   /* Go the the start of the ELF symbol table... */
    429  1.1  jonathan   if (lseek (in, offset, SEEK_SET) < 0)
    430  1.1  jonathan     {
    431  1.1  jonathan       perror ("copy: lseek");
    432  1.1  jonathan       exit (1);
    433  1.1  jonathan     }
    434  1.1  jonathan 
    435  1.1  jonathan   remaining = size;
    436  1.1  jonathan   while (remaining)
    437  1.1  jonathan     {
    438  1.1  jonathan       cur = remaining;
    439  1.1  jonathan       if (cur > sizeof ibuf)
    440  1.1  jonathan 	cur = sizeof ibuf;
    441  1.1  jonathan       remaining -= cur;
    442  1.1  jonathan       if ((count = read (in, ibuf, cur)) != cur)
    443  1.1  jonathan 	{
    444  1.1  jonathan 	  fprintf (stderr, "copy: read: %s\n",
    445  1.1  jonathan 		   count ? strerror (errno) : "premature end of file");
    446  1.1  jonathan 	  exit (1);
    447  1.1  jonathan 	}
    448  1.1  jonathan       if ((count = write (out, ibuf, cur)) != cur)
    449  1.1  jonathan 	{
    450  1.1  jonathan 	  perror ("copy: write");
    451  1.1  jonathan 	  exit (1);
    452  1.1  jonathan 	}
    453  1.1  jonathan     }
    454  1.1  jonathan }
    455  1.1  jonathan 
    456  1.1  jonathan /* Combine two segments, which must be contiguous.   If pad is true, it's
    457  1.1  jonathan    okay for there to be padding between. */
    458  1.1  jonathan combine (base, new, pad)
    459  1.1  jonathan      struct sect *base, *new;
    460  1.1  jonathan      int pad;
    461  1.1  jonathan {
    462  1.1  jonathan   if (!base -> len)
    463  1.1  jonathan     *base = *new;
    464  1.1  jonathan   else if (new -> len)
    465  1.1  jonathan     {
    466  1.1  jonathan       if (base -> vaddr + base -> len != new -> vaddr)
    467  1.1  jonathan 	{
    468  1.1  jonathan 	  if (pad)
    469  1.1  jonathan 	    base -> len = new -> vaddr - base -> vaddr;
    470  1.1  jonathan 	  else
    471  1.1  jonathan 	    {
    472  1.1  jonathan 	      fprintf (stderr,
    473  1.1  jonathan 		       "Non-contiguous data can't be converted.\n");
    474  1.1  jonathan 	      exit (1);
    475  1.1  jonathan 	    }
    476  1.1  jonathan 	}
    477  1.1  jonathan       base -> len += new -> len;
    478  1.1  jonathan     }
    479  1.1  jonathan }
    480  1.1  jonathan 
    481  1.3  jonathan int
    482  1.1  jonathan phcmp (h1, h2)
    483  1.3  jonathan      Elf32_Phdr *h1, *h2;
    484  1.1  jonathan {
    485  1.3  jonathan   if (h1 -> p_vaddr > h2 -> p_vaddr)
    486  1.1  jonathan     return 1;
    487  1.3  jonathan   else if (h1 -> p_vaddr < h2 -> p_vaddr)
    488  1.1  jonathan     return -1;
    489  1.1  jonathan   else
    490  1.1  jonathan     return 0;
    491  1.1  jonathan }
    492  1.1  jonathan 
    493  1.1  jonathan char *saveRead (int file, off_t offset, off_t len, char *name)
    494  1.1  jonathan {
    495  1.1  jonathan   char *tmp;
    496  1.1  jonathan   int count;
    497  1.1  jonathan   off_t off;
    498  1.1  jonathan   if ((off = lseek (file, offset, SEEK_SET)) < 0)
    499  1.1  jonathan     {
    500  1.1  jonathan       fprintf (stderr, "%s: fseek: %s\n", name, strerror (errno));
    501  1.1  jonathan       exit (1);
    502  1.1  jonathan     }
    503  1.1  jonathan   if (!(tmp = (char *)malloc (len)))
    504  1.1  jonathan     {
    505  1.1  jonathan       fprintf (stderr, "%s: Can't allocate %d bytes.\n", name, len);
    506  1.1  jonathan       exit (1);
    507  1.1  jonathan     }
    508  1.1  jonathan   count = read (file, tmp, len);
    509  1.1  jonathan   if (count != len)
    510  1.1  jonathan     {
    511  1.1  jonathan       fprintf (stderr, "%s: read: %s.\n",
    512  1.1  jonathan 	       name, count ? strerror (errno) : "End of file reached");
    513  1.1  jonathan       exit (1);
    514  1.1  jonathan     }
    515  1.1  jonathan   return tmp;
    516  1.1  jonathan }
    517