elf2ecoff.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:18k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * Copyright (c) 1995
  3.  * Ted Lemon (hereinafter referred to as the author)
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. The name of the author may not be used to endorse or promote products
  14.  *    derived from this software without specific prior written permission.
  15.  *
  16.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
  17.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
  20.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26.  * SUCH DAMAGE.
  27.  */
  28. /* elf2ecoff.c
  29.    This program converts an elf executable to an ECOFF executable.
  30.    No symbol table is retained.   This is useful primarily in building
  31.    net-bootable kernels for machines (e.g., DECstation and Alpha) which
  32.    only support the ECOFF object file format. */
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <errno.h>
  36. #include <sys/types.h>
  37. #include <fcntl.h>
  38. #include <unistd.h>
  39. #include <elf.h>
  40. #include <limits.h>
  41. #include "ecoff.h"
  42. /*
  43.  * Some extra ELF definitions
  44.  */
  45. #define PT_MIPS_REGINFO 0x70000000      /* Register usage information */
  46. /* -------------------------------------------------------------------- */
  47. struct sect {
  48.   unsigned long vaddr;
  49.   unsigned long len;
  50. };
  51. int phcmp ();
  52. char *saveRead (int file, off_t offset, off_t len, char *name);
  53. int copy (int, int, off_t, off_t);
  54. int translate_syms (int, int, off_t, off_t, off_t, off_t);
  55. void convert_elf_hdr (Elf32_Ehdr *);
  56. void convert_elf_phdrs (Elf32_Phdr *, int);
  57. void convert_elf_shdrs (Elf32_Shdr *, int);
  58. void convert_ecoff_filehdr(struct filehdr *);
  59. void convert_ecoff_aouthdr(struct aouthdr *);
  60. void convert_ecoff_esecs(struct scnhdr *, int);
  61. extern int errno;
  62. int *symTypeTable;
  63. int must_convert_endian = 0;
  64. int format_bigendian = 0;
  65. main (int argc, char **argv, char **envp)
  66. {
  67.   Elf32_Ehdr ex;
  68.   Elf32_Phdr *ph;
  69.   Elf32_Shdr *sh;
  70.   Elf32_Sym *symtab;
  71.   char *shstrtab;
  72.   int strtabix, symtabix;
  73.   int i, pad;
  74.   struct sect text, data, bss;
  75.   struct filehdr efh;
  76.   struct aouthdr eah;
  77.   struct scnhdr esecs [6];
  78.   int infile, outfile;
  79.   unsigned long cur_vma = ULONG_MAX;
  80.   int addflag = 0;
  81.   int nosecs;
  82.   text.len = data.len = bss.len = 0;
  83.   text.vaddr = data.vaddr = bss.vaddr = 0;
  84.   /* Check args... */
  85.   if (argc < 3 || argc > 4)
  86.     {
  87.     usage:
  88.       fprintf (stderr,
  89.        "usage: elf2aout <elf executable> <a.out executable> [-a]n");
  90.       exit (1);
  91.     }
  92.   if (argc == 4)
  93.     {
  94.       if (strcmp (argv [3], "-a"))
  95. goto usage;
  96.       addflag = 1;
  97.     }
  98.   /* Try the input file... */
  99.   if ((infile = open (argv [1], O_RDONLY)) < 0)
  100.     {
  101.       fprintf (stderr, "Can't open %s for read: %sn",
  102.        argv [1], strerror (errno));
  103.       exit (1);
  104.     }
  105.   /* Read the header, which is at the beginning of the file... */
  106.   i = read (infile, &ex, sizeof ex);
  107.   if (i != sizeof ex)
  108.     {
  109.       fprintf (stderr, "ex: %s: %s.n",
  110.        argv [1], i ? strerror (errno) : "End of file reached");
  111.       exit (1);
  112.     }
  113.   if (ex.e_ident[EI_DATA] == ELFDATA2MSB)
  114. format_bigendian = 1;
  115.   if (ntohs (0xaa55) == 0xaa55) {
  116. if (!format_bigendian)
  117. must_convert_endian = 1;
  118.   } else {
  119. if (format_bigendian)
  120. must_convert_endian = 1;
  121.   }
  122.   if (must_convert_endian)
  123. convert_elf_hdr (&ex);
  124.   /* Read the program headers... */
  125.   ph = (Elf32_Phdr *)saveRead (infile, ex.e_phoff,
  126. ex.e_phnum * sizeof (Elf32_Phdr), "ph");
  127.   if (must_convert_endian)
  128. convert_elf_phdrs (ph, ex.e_phnum);
  129.   /* Read the section headers... */
  130.   sh = (Elf32_Shdr *)saveRead (infile, ex.e_shoff,
  131. ex.e_shnum * sizeof (Elf32_Shdr), "sh");
  132.   if (must_convert_endian)
  133. convert_elf_shdrs (sh, ex.e_shnum);
  134.   /* Read in the section string table. */
  135.   shstrtab = saveRead (infile, sh [ex.e_shstrndx].sh_offset,
  136.        sh [ex.e_shstrndx].sh_size, "shstrtab");
  137.   /* Figure out if we can cram the program header into an ECOFF
  138.      header...  Basically, we can't handle anything but loadable
  139.      segments, but we can ignore some kinds of segments.  We can't
  140.      handle holes in the address space.  Segments may be out of order,
  141.      so we sort them first. */
  142.   qsort (ph, ex.e_phnum, sizeof (Elf32_Phdr), phcmp);
  143.   for (i = 0; i < ex.e_phnum; i++)
  144.     {
  145.       /* Section types we can ignore... */
  146.       if (ph [i].p_type == PT_NULL || ph [i].p_type == PT_NOTE ||
  147.   ph [i].p_type == PT_PHDR || ph [i].p_type == PT_MIPS_REGINFO)
  148. continue;
  149.       /* Section types we can't handle... */
  150.       else if (ph [i].p_type != PT_LOAD)
  151.         {
  152.   fprintf (stderr, "Program header %d type %d can't be converted.n");
  153.   exit (1);
  154. }
  155.       /* Writable (data) segment? */
  156.       if (ph [i].p_flags & PF_W)
  157. {
  158.   struct sect ndata, nbss;
  159.   ndata.vaddr = ph [i].p_vaddr;
  160.   ndata.len = ph [i].p_filesz;
  161.   nbss.vaddr = ph [i].p_vaddr + ph [i].p_filesz;
  162.   nbss.len = ph [i].p_memsz - ph [i].p_filesz;
  163.   combine (&data, &ndata, 0);
  164.   combine (&bss, &nbss, 1);
  165. }
  166.       else
  167. {
  168.   struct sect ntxt;
  169.   ntxt.vaddr = ph [i].p_vaddr;
  170.   ntxt.len = ph [i].p_filesz;
  171.   combine (&text, &ntxt, 0);
  172. }
  173.       /* Remember the lowest segment start address. */
  174.       if (ph [i].p_vaddr < cur_vma)
  175. cur_vma = ph [i].p_vaddr;
  176.     }
  177.   /* Sections must be in order to be converted... */
  178.   if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr ||
  179.       text.vaddr + text.len > data.vaddr || data.vaddr + data.len > bss.vaddr)
  180.     {
  181.       fprintf (stderr, "Sections ordering prevents a.out conversion.n");
  182.       exit (1);
  183.     }
  184.   /* If there's a data section but no text section, then the loader
  185.      combined everything into one section.   That needs to be the
  186.      text section, so just make the data section zero length following
  187.      text. */
  188.   if (data.len && !text.len)
  189.     {
  190.       text = data;
  191.       data.vaddr = text.vaddr + text.len;
  192.       data.len = 0;
  193.     }
  194.   /* If there is a gap between text and data, we'll fill it when we copy
  195.      the data, so update the length of the text segment as represented in
  196.      a.out to reflect that, since a.out doesn't allow gaps in the program
  197.      address space. */
  198.   if (text.vaddr + text.len < data.vaddr)
  199.     text.len = data.vaddr - text.vaddr;
  200.   /* We now have enough information to cons up an a.out header... */
  201.   eah.magic = OMAGIC;
  202.   eah.vstamp = 200;
  203.   eah.tsize = text.len;
  204.   eah.dsize = data.len;
  205.   eah.bsize = bss.len;
  206.   eah.entry = ex.e_entry;
  207.   eah.text_start = text.vaddr;
  208.   eah.data_start = data.vaddr;
  209.   eah.bss_start = bss.vaddr;
  210.   eah.gprmask = 0xf3fffffe;
  211.   memset (&eah.cprmask, '', sizeof eah.cprmask);
  212.   eah.gp_value = 0; /* unused. */
  213.   if (format_bigendian)
  214.     efh.f_magic = MIPSEBMAGIC;
  215.   else
  216.     efh.f_magic = MIPSELMAGIC;
  217.   if (addflag)
  218.     nosecs = 6;
  219.   else
  220.     nosecs = 3;
  221.   efh.f_nscns = nosecs;
  222.   efh.f_timdat = 0; /* bogus */
  223.   efh.f_symptr = 0;
  224.   efh.f_nsyms = 0;
  225.   efh.f_opthdr = sizeof eah;
  226.   efh.f_flags = 0x100f; /* Stripped, not sharable. */
  227.   memset (esecs, 0, sizeof esecs);
  228.   strcpy (esecs [0].s_name, ".text");
  229.   strcpy (esecs [1].s_name, ".data");
  230.   strcpy (esecs [2].s_name, ".bss");
  231.   if (addflag) {
  232.     strcpy (esecs [3].s_name, ".rdata");
  233.     strcpy (esecs [4].s_name, ".sdata");
  234.     strcpy (esecs [5].s_name, ".sbss");
  235.   }
  236.   esecs [0].s_paddr = esecs [0].s_vaddr = eah.text_start;
  237.   esecs [1].s_paddr = esecs [1].s_vaddr = eah.data_start;
  238.   esecs [2].s_paddr = esecs [2].s_vaddr = eah.bss_start;
  239.   if (addflag) {
  240.     esecs [3].s_paddr = esecs [3].s_vaddr = 0;
  241.     esecs [4].s_paddr = esecs [4].s_vaddr = 0;
  242.     esecs [5].s_paddr = esecs [5].s_vaddr = 0;
  243.   }
  244.   esecs [0].s_size = eah.tsize;
  245.   esecs [1].s_size = eah.dsize;
  246.   esecs [2].s_size = eah.bsize;
  247.   if (addflag) {
  248.     esecs [3].s_size = 0;
  249.     esecs [4].s_size = 0;
  250.     esecs [5].s_size = 0;
  251.   }
  252.   esecs [0].s_scnptr = N_TXTOFF (efh, eah);
  253.   esecs [1].s_scnptr = N_DATOFF (efh, eah);
  254. #define ECOFF_SEGMENT_ALIGNMENT(a) 0x10
  255. #define ECOFF_ROUND(s,a) (((s)+(a)-1)&~((a)-1))
  256.   esecs [2].s_scnptr = esecs [1].s_scnptr +
  257.   ECOFF_ROUND (esecs [1].s_size, ECOFF_SEGMENT_ALIGNMENT (&eah));
  258.   if (addflag) {
  259.     esecs [3].s_scnptr = 0;
  260.     esecs [4].s_scnptr = 0;
  261.     esecs [5].s_scnptr = 0;
  262.   }
  263.   esecs [0].s_relptr = esecs [1].s_relptr
  264.   = esecs [2].s_relptr = 0;
  265.   esecs [0].s_lnnoptr = esecs [1].s_lnnoptr
  266.   = esecs [2].s_lnnoptr = 0;
  267.   esecs [0].s_nreloc = esecs [1].s_nreloc = esecs [2].s_nreloc = 0;
  268.   esecs [0].s_nlnno = esecs [1].s_nlnno = esecs [2].s_nlnno = 0;
  269.   if (addflag) {
  270.     esecs [3].s_relptr = esecs [4].s_relptr 
  271.      = esecs [5].s_relptr = 0;
  272.     esecs [3].s_lnnoptr = esecs [4].s_lnnoptr
  273.   = esecs [5].s_lnnoptr = 0;
  274.     esecs [3].s_nreloc = esecs [4].s_nreloc = esecs [5].s_nreloc = 0;
  275.     esecs [3].s_nlnno = esecs [4].s_nlnno = esecs [5].s_nlnno = 0;
  276.   }
  277.   esecs [0].s_flags = 0x20;
  278.   esecs [1].s_flags = 0x40;
  279.   esecs [2].s_flags = 0x82;
  280.   if (addflag) {
  281.     esecs [3].s_flags = 0x100;
  282.     esecs [4].s_flags = 0x200;
  283.     esecs [5].s_flags = 0x400;
  284.   }
  285.   /* Make the output file... */
  286.   if ((outfile = open (argv [2], O_WRONLY | O_CREAT, 0777)) < 0)
  287.     {
  288.       fprintf (stderr, "Unable to create %s: %sn", argv [2], strerror (errno));
  289.       exit (1);
  290.     }
  291.   if (must_convert_endian)
  292. convert_ecoff_filehdr (&efh);
  293.   /* Write the headers... */
  294.   i = write (outfile, &efh, sizeof efh);
  295.   if (i != sizeof efh)
  296.     {
  297.       perror ("efh: write");
  298.       exit (1);
  299.     for (i = 0; i < nosecs; i++)
  300.       {
  301.         printf ("Section %d: %s phys %x  size %x  file offset %xn", 
  302.       i, esecs [i].s_name, esecs [i].s_paddr,
  303.       esecs [i].s_size, esecs [i].s_scnptr);
  304.       }
  305.     }
  306.   fprintf (stderr, "wrote %d byte file header.n", i);
  307.   if (must_convert_endian)
  308. convert_ecoff_aouthdr (&eah);
  309.   i = write (outfile, &eah, sizeof eah);
  310.   if (i != sizeof eah)
  311.     {
  312.       perror ("eah: write");
  313.       exit (1);
  314.     }
  315.   fprintf (stderr, "wrote %d byte a.out header.n", i);
  316.   if (must_convert_endian)
  317. convert_ecoff_esecs (&esecs[0], nosecs);
  318.   i = write (outfile, &esecs, nosecs * sizeof(struct scnhdr));
  319.   if (i != nosecs * sizeof(struct scnhdr))
  320.     {
  321.       perror ("esecs: write");
  322.       exit (1);
  323.     }
  324.   fprintf (stderr, "wrote %d bytes of section headers.n", i);
  325.   if (pad = ((sizeof efh + sizeof eah + nosecs * sizeof(struct scnhdr)) & 15))
  326.     {
  327.       pad = 16 - pad;
  328.       i = write (outfile, "", pad);
  329.       if (i < 0)
  330. {
  331.   perror ("ipad: write");
  332.   exit (1);
  333. }
  334.       fprintf (stderr, "wrote %d byte pad.n", i);
  335.     }
  336.   /* Copy the loadable sections.   Zero-fill any gaps less than 64k;
  337.      complain about any zero-filling, and die if we're asked to zero-fill
  338.      more than 64k. */
  339.   for (i = 0; i < ex.e_phnum; i++)
  340.     {
  341.       /* Unprocessable sections were handled above, so just verify that
  342.  the section can be loaded before copying. */
  343.       if (ph [i].p_type == PT_LOAD && ph [i].p_filesz)
  344. {
  345.   if (cur_vma != ph [i].p_vaddr)
  346.     {
  347.       unsigned long gap = ph [i].p_vaddr - cur_vma;
  348.       char obuf [1024];
  349.       if (gap > 65536)
  350. {
  351.   fprintf (stderr, "Intersegment gap (%d bytes) too large.n",
  352.    gap);
  353.   exit (1);
  354. }
  355.       fprintf (stderr, "Warning: %d byte intersegment gap.n", gap);
  356.       memset (obuf, 0, sizeof obuf);
  357.       while (gap)
  358. {
  359.   int count = write (outfile, obuf, (gap > sizeof obuf
  360.      ? sizeof obuf : gap));
  361.   if (count < 0)
  362.     {
  363.       fprintf (stderr, "Error writing gap: %sn",
  364.        strerror (errno));
  365.       exit (1);
  366.     }
  367.   gap -= count;
  368. }
  369.     }
  370. fprintf (stderr, "writing %d bytes...n", ph [i].p_filesz);
  371.   copy (outfile, infile, ph [i].p_offset, ph [i].p_filesz);
  372.   cur_vma = ph [i].p_vaddr + ph [i].p_filesz;
  373. }
  374.     }
  375.    /*
  376.      * Write a page of padding for boot PROMS that read entire pages.
  377.      * Without this, they may attempt to read past the end of the
  378.      * data section, incur an error, and refuse to boot.
  379.      */
  380.     {
  381. char obuf[4096];
  382. memset(obuf, 0, sizeof obuf);
  383. if (write(outfile, obuf, sizeof(obuf)) != sizeof(obuf)) {
  384.     fprintf(stderr, "Error writing PROM padding: %sn",
  385.     strerror(errno));
  386.     exit(1);
  387. }
  388.     }
  389.   /* Looks like we won... */
  390.   exit (0);
  391. }
  392. copy (out, in, offset, size)
  393.      int out, in;
  394.      off_t offset, size;
  395. {
  396.   char ibuf [4096];
  397.   int remaining, cur, count;
  398.   /* Go to the start of the ELF symbol table... */
  399.   if (lseek (in, offset, SEEK_SET) < 0)
  400.     {
  401.       perror ("copy: lseek");
  402.       exit (1);
  403.     }
  404.   remaining = size;
  405.   while (remaining)
  406.     {
  407.       cur = remaining;
  408.       if (cur > sizeof ibuf)
  409. cur = sizeof ibuf;
  410.       remaining -= cur;
  411.       if ((count = read (in, ibuf, cur)) != cur)
  412. {
  413.   fprintf (stderr, "copy: read: %sn",
  414.    count ? strerror (errno) : "premature end of file");
  415.   exit (1);
  416. }
  417.       if ((count = write (out, ibuf, cur)) != cur)
  418. {
  419.   perror ("copy: write");
  420.   exit (1);
  421. }
  422.     }
  423. }
  424. /* Combine two segments, which must be contiguous.   If pad is true, it's
  425.    okay for there to be padding between. */
  426. combine (base, new, pad)
  427.      struct sect *base, *new;
  428.      int pad;
  429. {
  430.   if (!base -> len)
  431.     *base = *new;
  432.   else if (new -> len)
  433.     {
  434.       if (base -> vaddr + base -> len != new -> vaddr)
  435. {
  436.   if (pad)
  437.     base -> len = new -> vaddr - base -> vaddr;
  438.   else
  439.     {
  440.       fprintf (stderr,
  441.        "Non-contiguous data can't be converted.n");
  442.       exit (1);
  443.     }
  444. }
  445.       base -> len += new -> len;
  446.     }
  447. }
  448. phcmp (h1, h2)
  449.      Elf32_Phdr *h1, *h2;
  450. {
  451.   if (h1 -> p_vaddr > h2 -> p_vaddr)
  452.     return 1;
  453.   else if (h1 -> p_vaddr < h2 -> p_vaddr)
  454.     return -1;
  455.   else
  456.     return 0;
  457. }
  458. char *saveRead (int file, off_t offset, off_t len, char *name)
  459. {
  460.   char *tmp;
  461.   int count;
  462.   off_t off;
  463.   if ((off = lseek (file, offset, SEEK_SET)) < 0)
  464.     {
  465.       fprintf (stderr, "%s: fseek: %sn", name, strerror (errno));
  466.       exit (1);
  467.     }
  468.   if (!(tmp = (char *)malloc (len)))
  469.     {
  470.       fprintf (stderr, "%s: Can't allocate %d bytes.n", name, len);
  471.       exit (1);
  472.     }
  473.   count = read (file, tmp, len);
  474.   if (count != len)
  475.     {
  476.       fprintf (stderr, "%s: read: %s.n",
  477.        name, count ? strerror (errno) : "End of file reached");
  478.       exit (1);
  479.     }
  480.   return tmp;
  481. }
  482. #define swab16(x) 
  483. ((unsigned short)( 
  484. (((unsigned short)(x) & (unsigned short)0x00ffU) << 8) | 
  485. (((unsigned short)(x) & (unsigned short)0xff00U) >> 8) ))
  486. #define swab32(x) 
  487. ((unsigned int)( 
  488. (((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) | 
  489. (((unsigned int)(x) & (unsigned int)0x0000ff00UL) <<  8) | 
  490. (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >>  8) | 
  491. (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24) ))
  492. void convert_elf_hdr (Elf32_Ehdr *e)
  493. {
  494. e->e_type      = swab16(e->e_type);
  495. e->e_machine   = swab16(e->e_machine);
  496. e->e_version   = swab32(e->e_version);
  497. e->e_entry     = swab32(e->e_entry);
  498. e->e_phoff     = swab32(e->e_phoff);
  499. e->e_shoff     = swab32(e->e_shoff);
  500. e->e_flags     = swab32(e->e_flags);
  501. e->e_ehsize    = swab16(e->e_ehsize);
  502. e->e_phentsize = swab16(e->e_phentsize);
  503. e->e_phnum     = swab16(e->e_phnum);
  504. e->e_shentsize = swab16(e->e_shentsize);
  505. e->e_shnum     = swab16(e->e_shnum);
  506. e->e_shstrndx  = swab16(e->e_shstrndx);
  507. }
  508. void convert_elf_phdrs (Elf32_Phdr *p, int num)
  509. {
  510. int i;
  511. for (i = 0; i < num; i++,p++) {
  512. p->p_type   = swab32(p->p_type);
  513. p->p_offset = swab32(p->p_offset);
  514. p->p_vaddr  = swab32(p->p_vaddr);
  515. p->p_paddr  = swab32(p->p_paddr);
  516. p->p_filesz = swab32(p->p_filesz);
  517. p->p_memsz  = swab32(p->p_memsz);
  518. p->p_flags  = swab32(p->p_flags);
  519. p->p_align  = swab32(p->p_align);
  520. }
  521. }
  522. void convert_elf_shdrs (Elf32_Shdr *s, int num)
  523. {
  524. int i;
  525. for (i = 0; i < num; i++,s++) {
  526. s->sh_name      = swab32(s->sh_name);
  527. s->sh_type      = swab32(s->sh_type);
  528. s->sh_flags     = swab32(s->sh_flags);
  529. s->sh_addr      = swab32(s->sh_addr);
  530. s->sh_offset    = swab32(s->sh_offset);
  531. s->sh_size      = swab32(s->sh_size);
  532. s->sh_link      = swab32(s->sh_link);
  533. s->sh_info      = swab32(s->sh_info);
  534. s->sh_addralign = swab32(s->sh_addralign);
  535. s->sh_entsize   = swab32(s->sh_entsize);
  536. }
  537. }
  538. void convert_ecoff_filehdr(struct filehdr *f)
  539. {
  540. f->f_magic  = swab16(f->f_magic);
  541. f->f_nscns  = swab16(f->f_nscns);
  542. f->f_timdat = swab32(f->f_timdat);
  543. f->f_symptr = swab32(f->f_symptr);
  544. f->f_nsyms  = swab32(f->f_nsyms);
  545. f->f_opthdr = swab16(f->f_opthdr);
  546. f->f_flags  = swab16(f->f_flags);
  547. }
  548. void convert_ecoff_aouthdr(struct aouthdr *a)
  549. {
  550. a->magic      = swab16(a->magic);
  551. a->vstamp     = swab16(a->vstamp);
  552. a->tsize      = swab32(a->tsize);
  553. a->dsize      = swab32(a->dsize);
  554. a->bsize      = swab32(a->bsize);
  555. a->entry      = swab32(a->entry);
  556. a->text_start = swab32(a->text_start);
  557. a->data_start = swab32(a->data_start);
  558. a->bss_start  = swab32(a->bss_start);
  559. a->gprmask    = swab32(a->gprmask);
  560. a->cprmask[0] = swab32(a->cprmask[0]);
  561. a->cprmask[1] = swab32(a->cprmask[1]);
  562. a->cprmask[2] = swab32(a->cprmask[2]);
  563. a->cprmask[3] = swab32(a->cprmask[3]);
  564. a->gp_value   = swab32(a->gp_value);
  565. }
  566. void convert_ecoff_esecs(struct scnhdr *s, int num)
  567. {
  568. int i;
  569. for (i = 0; i < num; i++, s++) {
  570. s->s_paddr   = swab32(s->s_paddr);
  571. s->s_vaddr   = swab32(s->s_vaddr);
  572. s->s_size    = swab32(s->s_size);
  573. s->s_scnptr  = swab32(s->s_scnptr);
  574. s->s_relptr  = swab32(s->s_relptr);
  575. s->s_lnnoptr = swab32(s->s_lnnoptr);
  576. s->s_nreloc  = swab16(s->s_nreloc);
  577. s->s_nlnno   = swab16(s->s_nlnno);
  578. s->s_flags   = swab32(s->s_flags);
  579. }
  580. }