home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / bfd / coffswap.h < prev    next >
C/C++ Source or Header  |  1996-09-28  |  28KB  |  823 lines

  1. /* Generic COFF swapping routines, for BFD.
  2.    Copyright 1990, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  20.  
  21. /* This file contains routines used to swap COFF data.  It is a header
  22.    file because the details of swapping depend on the details of the
  23.    structures used by each COFF implementation.  This is included by
  24.    coffcode.h, as well as by the ECOFF backend.
  25.  
  26.    Any file which uses this must first include "coff/internal.h" and
  27.    "coff/CPU.h".  The functions will then be correct for that CPU.  */
  28.  
  29. #ifndef IMAGE_BASE
  30. #define IMAGE_BASE 0
  31. #endif
  32.  
  33. #define PUTWORD bfd_h_put_32
  34. #define PUTHALF bfd_h_put_16
  35. #define    PUTBYTE bfd_h_put_8
  36.  
  37. #ifndef GET_FCN_LNNOPTR
  38. #define GET_FCN_LNNOPTR(abfd, ext)  bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_lnnoptr)
  39. #endif
  40.  
  41. #ifndef GET_FCN_ENDNDX
  42. #define GET_FCN_ENDNDX(abfd, ext)  bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_endndx)
  43. #endif
  44.  
  45. #ifndef PUT_FCN_LNNOPTR
  46. #define PUT_FCN_LNNOPTR(abfd, in, ext)  PUTWORD(abfd,  in, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_lnnoptr)
  47. #endif
  48. #ifndef PUT_FCN_ENDNDX
  49. #define PUT_FCN_ENDNDX(abfd, in, ext) PUTWORD(abfd, in, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_endndx)
  50. #endif
  51. #ifndef GET_LNSZ_LNNO
  52. #define GET_LNSZ_LNNO(abfd, ext) bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_misc.x_lnsz.x_lnno)
  53. #endif
  54. #ifndef GET_LNSZ_SIZE
  55. #define GET_LNSZ_SIZE(abfd, ext) bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_misc.x_lnsz.x_size)
  56. #endif
  57. #ifndef PUT_LNSZ_LNNO
  58. #define PUT_LNSZ_LNNO(abfd, in, ext) bfd_h_put_16(abfd, in, (bfd_byte *)ext->x_sym.x_misc.x_lnsz.x_lnno)
  59. #endif
  60. #ifndef PUT_LNSZ_SIZE
  61. #define PUT_LNSZ_SIZE(abfd, in, ext) bfd_h_put_16(abfd, in, (bfd_byte*) ext->x_sym.x_misc.x_lnsz.x_size)
  62. #endif
  63. #ifndef GET_SCN_SCNLEN
  64. #define GET_SCN_SCNLEN(abfd,  ext) bfd_h_get_32(abfd, (bfd_byte *) ext->x_scn.x_scnlen)
  65. #endif
  66. #ifndef GET_SCN_NRELOC
  67. #define GET_SCN_NRELOC(abfd,  ext) bfd_h_get_16(abfd, (bfd_byte *)ext->x_scn.x_nreloc)
  68. #endif
  69. #ifndef GET_SCN_NLINNO
  70. #define GET_SCN_NLINNO(abfd, ext)  bfd_h_get_16(abfd, (bfd_byte *)ext->x_scn.x_nlinno)
  71. #endif
  72. #ifndef PUT_SCN_SCNLEN
  73. #define PUT_SCN_SCNLEN(abfd,in, ext) bfd_h_put_32(abfd, in, (bfd_byte *) ext->x_scn.x_scnlen)
  74. #endif
  75. #ifndef PUT_SCN_NRELOC
  76. #define PUT_SCN_NRELOC(abfd,in, ext) bfd_h_put_16(abfd, in, (bfd_byte *)ext->x_scn.x_nreloc)
  77. #endif
  78. #ifndef PUT_SCN_NLINNO
  79. #define PUT_SCN_NLINNO(abfd,in, ext)  bfd_h_put_16(abfd,in, (bfd_byte  *) ext->x_scn.x_nlinno)
  80. #endif
  81. #ifndef GET_LINENO_LNNO
  82. #define GET_LINENO_LNNO(abfd, ext) bfd_h_get_16(abfd, (bfd_byte *) (ext->l_lnno));
  83. #endif
  84. #ifndef PUT_LINENO_LNNO
  85. #define PUT_LINENO_LNNO(abfd,val, ext) bfd_h_put_16(abfd,val,  (bfd_byte *) (ext->l_lnno));
  86. #endif
  87.  
  88. /* The f_symptr field in the filehdr is sometimes 64 bits.  */
  89. #ifndef GET_FILEHDR_SYMPTR
  90. #define GET_FILEHDR_SYMPTR bfd_h_get_32
  91. #endif
  92. #ifndef PUT_FILEHDR_SYMPTR
  93. #define PUT_FILEHDR_SYMPTR bfd_h_put_32
  94. #endif
  95.  
  96. /* Some fields in the aouthdr are sometimes 64 bits.  */
  97. #ifndef GET_AOUTHDR_TSIZE
  98. #define GET_AOUTHDR_TSIZE bfd_h_get_32
  99. #endif
  100. #ifndef PUT_AOUTHDR_TSIZE
  101. #define PUT_AOUTHDR_TSIZE bfd_h_put_32
  102. #endif
  103. #ifndef GET_AOUTHDR_DSIZE
  104. #define GET_AOUTHDR_DSIZE bfd_h_get_32
  105. #endif
  106. #ifndef PUT_AOUTHDR_DSIZE
  107. #define PUT_AOUTHDR_DSIZE bfd_h_put_32
  108. #endif
  109. #ifndef GET_AOUTHDR_BSIZE
  110. #define GET_AOUTHDR_BSIZE bfd_h_get_32
  111. #endif
  112. #ifndef PUT_AOUTHDR_BSIZE
  113. #define PUT_AOUTHDR_BSIZE bfd_h_put_32
  114. #endif
  115. #ifndef GET_AOUTHDR_ENTRY
  116. #define GET_AOUTHDR_ENTRY bfd_h_get_32
  117. #endif
  118. #ifndef PUT_AOUTHDR_ENTRY
  119. #define PUT_AOUTHDR_ENTRY bfd_h_put_32
  120. #endif
  121. #ifndef GET_AOUTHDR_TEXT_START
  122. #define GET_AOUTHDR_TEXT_START bfd_h_get_32
  123. #endif
  124. #ifndef PUT_AOUTHDR_TEXT_START
  125. #define PUT_AOUTHDR_TEXT_START bfd_h_put_32
  126. #endif
  127. #ifndef GET_AOUTHDR_DATA_START
  128. #define GET_AOUTHDR_DATA_START bfd_h_get_32
  129. #endif
  130. #ifndef PUT_AOUTHDR_DATA_START
  131. #define PUT_AOUTHDR_DATA_START bfd_h_put_32
  132. #endif
  133.  
  134. /* Some fields in the scnhdr are sometimes 64 bits.  */
  135. #ifndef GET_SCNHDR_PADDR
  136. #define GET_SCNHDR_PADDR bfd_h_get_32
  137. #endif
  138. #ifndef PUT_SCNHDR_PADDR
  139. #define PUT_SCNHDR_PADDR bfd_h_put_32
  140. #endif
  141. #ifndef GET_SCNHDR_VADDR
  142. #define GET_SCNHDR_VADDR bfd_h_get_32
  143. #endif
  144. #ifndef PUT_SCNHDR_VADDR
  145. #define PUT_SCNHDR_VADDR bfd_h_put_32
  146. #endif
  147. #ifndef GET_SCNHDR_SIZE
  148. #define GET_SCNHDR_SIZE bfd_h_get_32
  149. #endif
  150. #ifndef PUT_SCNHDR_SIZE
  151. #define PUT_SCNHDR_SIZE bfd_h_put_32
  152. #endif
  153. #ifndef GET_SCNHDR_SCNPTR
  154. #define GET_SCNHDR_SCNPTR bfd_h_get_32
  155. #endif
  156. #ifndef PUT_SCNHDR_SCNPTR
  157. #define PUT_SCNHDR_SCNPTR bfd_h_put_32
  158. #endif
  159. #ifndef GET_SCNHDR_RELPTR
  160. #define GET_SCNHDR_RELPTR bfd_h_get_32
  161. #endif
  162. #ifndef PUT_SCNHDR_RELPTR
  163. #define PUT_SCNHDR_RELPTR bfd_h_put_32
  164. #endif
  165. #ifndef GET_SCNHDR_LNNOPTR
  166. #define GET_SCNHDR_LNNOPTR bfd_h_get_32
  167. #endif
  168. #ifndef PUT_SCNHDR_LNNOPTR
  169. #define PUT_SCNHDR_LNNOPTR bfd_h_put_32
  170. #endif
  171.  
  172. #ifndef NO_COFF_RELOCS
  173.  
  174. static void
  175. coff_swap_reloc_in (abfd, src, dst)
  176.      bfd *abfd;
  177.      PTR src;
  178.      PTR dst;
  179. {
  180.   RELOC *reloc_src = (RELOC *) src;
  181.   struct internal_reloc *reloc_dst = (struct internal_reloc *) dst;
  182.  
  183.   reloc_dst->r_vaddr = bfd_h_get_32(abfd, (bfd_byte *)reloc_src->r_vaddr);
  184.   reloc_dst->r_symndx = bfd_h_get_signed_32(abfd, (bfd_byte *) reloc_src->r_symndx);
  185.  
  186. #ifdef RS6000COFF_C
  187.   reloc_dst->r_type = bfd_h_get_8(abfd, reloc_src->r_type);
  188.   reloc_dst->r_size = bfd_h_get_8(abfd, reloc_src->r_size);
  189. #else
  190.   reloc_dst->r_type = bfd_h_get_16(abfd, (bfd_byte *) reloc_src->r_type);
  191. #endif
  192.  
  193. #ifdef SWAP_IN_RELOC_OFFSET
  194.   reloc_dst->r_offset = SWAP_IN_RELOC_OFFSET(abfd,
  195.                          (bfd_byte *) reloc_src->r_offset);
  196. #endif
  197. }
  198.  
  199.  
  200. static unsigned int
  201. coff_swap_reloc_out (abfd, src, dst)
  202.      bfd       *abfd;
  203.      PTR    src;
  204.      PTR    dst;
  205. {
  206.   struct internal_reloc *reloc_src = (struct internal_reloc *)src;
  207.   struct external_reloc *reloc_dst = (struct external_reloc *)dst;
  208.   bfd_h_put_32(abfd, reloc_src->r_vaddr, (bfd_byte *) reloc_dst->r_vaddr);
  209.   bfd_h_put_32(abfd, reloc_src->r_symndx, (bfd_byte *) reloc_dst->r_symndx);
  210.  
  211. #ifdef RS6000COFF_C
  212.   bfd_h_put_8 (abfd, reloc_src->r_type, (bfd_byte *) reloc_dst->r_type);
  213.   bfd_h_put_8 (abfd, reloc_src->r_size, (bfd_byte *) reloc_dst->r_size);
  214. #else
  215.   bfd_h_put_16(abfd, reloc_src->r_type, (bfd_byte *)
  216.            reloc_dst->r_type);
  217. #endif
  218.  
  219. #ifdef SWAP_OUT_RELOC_OFFSET
  220.   SWAP_OUT_RELOC_OFFSET(abfd,
  221.             reloc_src->r_offset,
  222.             (bfd_byte *) reloc_dst->r_offset);
  223. #endif
  224. #ifdef SWAP_OUT_RELOC_EXTRA
  225.   SWAP_OUT_RELOC_EXTRA(abfd,reloc_src, reloc_dst);
  226. #endif
  227.  
  228.   return RELSZ;
  229. }
  230.  
  231. #endif /* NO_COFF_RELOCS */
  232.  
  233. static void
  234. coff_swap_filehdr_in (abfd, src, dst)
  235.      bfd            *abfd;
  236.      PTR         src;
  237.      PTR         dst;
  238. {
  239.   FILHDR *filehdr_src = (FILHDR *) src;
  240.   struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
  241.   filehdr_dst->f_magic = bfd_h_get_16(abfd, (bfd_byte *) filehdr_src->f_magic);
  242.   filehdr_dst->f_nscns = bfd_h_get_16(abfd, (bfd_byte *)filehdr_src-> f_nscns);
  243.   filehdr_dst->f_timdat = bfd_h_get_32(abfd, (bfd_byte *)filehdr_src-> f_timdat);
  244.   filehdr_dst->f_symptr =
  245.     GET_FILEHDR_SYMPTR (abfd, (bfd_byte *) filehdr_src->f_symptr);
  246.   filehdr_dst->f_nsyms = bfd_h_get_32(abfd, (bfd_byte *)filehdr_src-> f_nsyms);
  247.   filehdr_dst->f_opthdr = bfd_h_get_16(abfd, (bfd_byte *)filehdr_src-> f_opthdr);
  248.   filehdr_dst->f_flags = bfd_h_get_16(abfd, (bfd_byte *)filehdr_src-> f_flags);
  249. }
  250.  
  251. static  unsigned int
  252. coff_swap_filehdr_out (abfd, in, out)
  253.      bfd       *abfd;
  254.      PTR    in;
  255.      PTR    out;
  256. {
  257.   struct internal_filehdr *filehdr_in = (struct internal_filehdr *)in;
  258.   FILHDR *filehdr_out = (FILHDR *)out;
  259.  
  260.   bfd_h_put_16(abfd, filehdr_in->f_magic, (bfd_byte *) filehdr_out->f_magic);
  261.   bfd_h_put_16(abfd, filehdr_in->f_nscns, (bfd_byte *) filehdr_out->f_nscns);
  262.   bfd_h_put_32(abfd, filehdr_in->f_timdat, (bfd_byte *) filehdr_out->f_timdat);
  263.   PUT_FILEHDR_SYMPTR (abfd, (bfd_vma) filehdr_in->f_symptr,
  264.               (bfd_byte *) filehdr_out->f_symptr);
  265.   bfd_h_put_32(abfd, filehdr_in->f_nsyms, (bfd_byte *) filehdr_out->f_nsyms);
  266.   bfd_h_put_16(abfd, filehdr_in->f_opthdr, (bfd_byte *) filehdr_out->f_opthdr);
  267.   bfd_h_put_16(abfd, filehdr_in->f_flags, (bfd_byte *) filehdr_out->f_flags);
  268.  
  269.   return FILHSZ;
  270. }
  271.  
  272.  
  273. #ifndef NO_COFF_SYMBOLS
  274.  
  275. static void
  276. coff_swap_sym_in (abfd, ext1, in1)
  277.      bfd            *abfd;
  278.      PTR ext1;
  279.      PTR in1;
  280. {
  281.   SYMENT *ext = (SYMENT *)ext1;
  282.   struct internal_syment      *in = (struct internal_syment *)in1;
  283.  
  284.   if( ext->e.e_name[0] == 0) {
  285.     in->_n._n_n._n_zeroes = 0;
  286.     in->_n._n_n._n_offset = bfd_h_get_32(abfd, (bfd_byte *) ext->e.e.e_offset);
  287.   }
  288.   else {
  289. #if SYMNMLEN != E_SYMNMLEN
  290.    -> Error, we need to cope with truncating or extending SYMNMLEN!;
  291. #else
  292.     memcpy(in->_n._n_name, ext->e.e_name, SYMNMLEN);
  293. #endif
  294.   }
  295.   in->n_value = bfd_h_get_32(abfd, (bfd_byte *) ext->e_value); 
  296.   in->n_scnum = bfd_h_get_16(abfd, (bfd_byte *) ext->e_scnum);
  297.   if (sizeof(ext->e_type) == 2){
  298.     in->n_type = bfd_h_get_16(abfd, (bfd_byte *) ext->e_type);
  299.   }
  300.   else {
  301.     in->n_type = bfd_h_get_32(abfd, (bfd_byte *) ext->e_type);
  302.   }
  303.   in->n_sclass = bfd_h_get_8(abfd, ext->e_sclass);
  304.   in->n_numaux = bfd_h_get_8(abfd, ext->e_numaux);
  305. }
  306.  
  307. static unsigned int
  308. coff_swap_sym_out (abfd, inp, extp)
  309.      bfd       *abfd;
  310.      PTR    inp;
  311.      PTR    extp;
  312. {
  313.   struct internal_syment *in = (struct internal_syment *)inp;
  314.   SYMENT *ext =(SYMENT *)extp;
  315.   if(in->_n._n_name[0] == 0) {
  316.     bfd_h_put_32(abfd, 0, (bfd_byte *) ext->e.e.e_zeroes);
  317.     bfd_h_put_32(abfd, in->_n._n_n._n_offset, (bfd_byte *)  ext->e.e.e_offset);
  318.   }
  319.   else {
  320. #if SYMNMLEN != E_SYMNMLEN
  321.     -> Error, we need to cope with truncating or extending SYMNMLEN!;
  322. #else
  323.     memcpy(ext->e.e_name, in->_n._n_name, SYMNMLEN);
  324. #endif
  325.   }
  326.   bfd_h_put_32(abfd,  in->n_value , (bfd_byte *) ext->e_value);
  327.   bfd_h_put_16(abfd,  in->n_scnum , (bfd_byte *) ext->e_scnum);
  328.   if (sizeof(ext->e_type) == 2)
  329.       {
  330.     bfd_h_put_16(abfd,  in->n_type , (bfd_byte *) ext->e_type);
  331.       }
  332.   else
  333.       {
  334.     bfd_h_put_32(abfd,  in->n_type , (bfd_byte *) ext->e_type);
  335.       }
  336.   bfd_h_put_8(abfd,  in->n_sclass , ext->e_sclass);
  337.   bfd_h_put_8(abfd,  in->n_numaux , ext->e_numaux);
  338.   return SYMESZ;
  339. }
  340.  
  341. static void
  342. coff_swap_aux_in (abfd, ext1, type, class, indx, numaux, in1)
  343.      bfd            *abfd;
  344.      PTR           ext1;
  345.      int             type;
  346.      int             class;
  347.      int          indx;
  348.      int          numaux;
  349.      PTR           in1;
  350. {
  351.   AUXENT    *ext = (AUXENT *)ext1;
  352.   union internal_auxent *in = (union internal_auxent *)in1;
  353.  
  354.   switch (class) {
  355.     case C_FILE:
  356.       if (ext->x_file.x_fname[0] == 0) {
  357.       in->x_file.x_n.x_zeroes = 0;
  358.       in->x_file.x_n.x_offset = 
  359.        bfd_h_get_32(abfd, (bfd_byte *) ext->x_file.x_n.x_offset);
  360.     } else {
  361. #if FILNMLEN != E_FILNMLEN
  362.         -> Error, we need to cope with truncating or extending FILNMLEN!;
  363. #else
  364.         memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
  365. #endif
  366.       }
  367.       return;
  368.  
  369.       /* RS/6000 "csect" auxents */
  370. #ifdef RS6000COFF_C
  371.     case C_EXT:
  372.     case C_HIDEXT:
  373.       if (indx + 1 == numaux)
  374.     {
  375.       in->x_csect.x_scnlen.l = bfd_h_get_32 (abfd, ext->x_csect.x_scnlen);
  376.       in->x_csect.x_parmhash = bfd_h_get_32 (abfd,
  377.                          ext->x_csect.x_parmhash);
  378.       in->x_csect.x_snhash   = bfd_h_get_16 (abfd, ext->x_csect.x_snhash);
  379.       /* We don't have to hack bitfields in x_smtyp because it's
  380.          defined by shifts-and-ands, which are equivalent on all
  381.          byte orders.  */
  382.       in->x_csect.x_smtyp    = bfd_h_get_8  (abfd, ext->x_csect.x_smtyp);
  383.       in->x_csect.x_smclas   = bfd_h_get_8  (abfd, ext->x_csect.x_smclas);
  384.       in->x_csect.x_stab     = bfd_h_get_32 (abfd, ext->x_csect.x_stab);
  385.       in->x_csect.x_snstab   = bfd_h_get_16 (abfd, ext->x_csect.x_snstab);
  386.       return;
  387.     }
  388.       break;
  389. #endif
  390.  
  391.     case C_STAT:
  392. #ifdef C_LEAFSTAT
  393.     case C_LEAFSTAT:
  394. #endif
  395.     case C_HIDDEN:
  396.       if (type == T_NULL) {
  397.       in->x_scn.x_scnlen = GET_SCN_SCNLEN(abfd, ext);
  398.       in->x_scn.x_nreloc = GET_SCN_NRELOC(abfd, ext);
  399.       in->x_scn.x_nlinno = GET_SCN_NLINNO(abfd, ext);
  400.  
  401.       /* PE defines some extra fields; we zero them out for
  402.              safety.  */
  403.       in->x_scn.x_checksum = 0;
  404.       in->x_scn.x_associated = 0;
  405.       in->x_scn.x_comdat = 0;
  406.  
  407.       return;
  408.     }
  409.       break;
  410.     }
  411.  
  412.   in->x_sym.x_tagndx.l = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_tagndx);
  413. #ifndef NO_TVNDX
  414.   in->x_sym.x_tvndx = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_tvndx);
  415. #endif
  416.  
  417.   if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
  418.     {
  419.       in->x_sym.x_fcnary.x_fcn.x_lnnoptr = GET_FCN_LNNOPTR (abfd, ext);
  420.       in->x_sym.x_fcnary.x_fcn.x_endndx.l = GET_FCN_ENDNDX (abfd, ext);
  421.     }
  422.   else
  423.     {
  424. #if DIMNUM != E_DIMNUM
  425.  #error we need to cope with truncating or extending DIMNUM
  426. #endif
  427.       in->x_sym.x_fcnary.x_ary.x_dimen[0] =
  428.     bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
  429.       in->x_sym.x_fcnary.x_ary.x_dimen[1] =
  430.     bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
  431.       in->x_sym.x_fcnary.x_ary.x_dimen[2] =
  432.     bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
  433.       in->x_sym.x_fcnary.x_ary.x_dimen[3] =
  434.     bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
  435.     }
  436.  
  437.   if (ISFCN(type)) {
  438.     in->x_sym.x_misc.x_fsize = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_misc.x_fsize);
  439.   }
  440.   else {
  441.     in->x_sym.x_misc.x_lnsz.x_lnno = GET_LNSZ_LNNO(abfd, ext);
  442.     in->x_sym.x_misc.x_lnsz.x_size = GET_LNSZ_SIZE(abfd, ext);
  443.   }
  444. }
  445.  
  446. static unsigned int
  447. coff_swap_aux_out (abfd, inp, type, class, indx, numaux, extp)
  448.      bfd   *abfd;
  449.      PTR     inp;
  450.      int   type;
  451.      int   class;
  452.      int   indx;
  453.      int   numaux;
  454.      PTR    extp;
  455. {
  456.   union internal_auxent *in = (union internal_auxent *)inp;
  457.   AUXENT *ext = (AUXENT *)extp;
  458.  
  459.   memset((PTR)ext, 0, AUXESZ);
  460.   switch (class) {
  461.   case C_FILE:
  462.     if (in->x_file.x_fname[0] == 0) {
  463.       PUTWORD(abfd, 0, (bfd_byte *) ext->x_file.x_n.x_zeroes);
  464.       PUTWORD(abfd,
  465.           in->x_file.x_n.x_offset,
  466.           (bfd_byte *) ext->x_file.x_n.x_offset);
  467.     }
  468.     else {
  469. #if FILNMLEN != E_FILNMLEN
  470.       -> Error, we need to cope with truncating or extending FILNMLEN!;
  471. #else
  472.       memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
  473. #endif
  474.     }
  475.     return AUXESZ;
  476.  
  477. #ifdef RS6000COFF_C
  478.   /* RS/6000 "csect" auxents */
  479.   case C_EXT:
  480.   case C_HIDEXT:
  481.     if (indx + 1 == numaux)
  482.       {
  483.     PUTWORD (abfd, in->x_csect.x_scnlen.l,    ext->x_csect.x_scnlen);
  484.     PUTWORD (abfd, in->x_csect.x_parmhash,    ext->x_csect.x_parmhash);
  485.     PUTHALF (abfd, in->x_csect.x_snhash,    ext->x_csect.x_snhash);
  486.     /* We don't have to hack bitfields in x_smtyp because it's
  487.        defined by shifts-and-ands, which are equivalent on all
  488.        byte orders.  */
  489.     PUTBYTE (abfd, in->x_csect.x_smtyp,    ext->x_csect.x_smtyp);
  490.     PUTBYTE (abfd, in->x_csect.x_smclas,    ext->x_csect.x_smclas);
  491.     PUTWORD (abfd, in->x_csect.x_stab,    ext->x_csect.x_stab);
  492.     PUTHALF (abfd, in->x_csect.x_snstab,    ext->x_csect.x_snstab);
  493.     return AUXESZ;
  494.       }
  495.     break;
  496. #endif
  497.  
  498.   case C_STAT:
  499. #ifdef C_LEAFSTAT
  500.   case C_LEAFSTAT:
  501. #endif
  502.   case C_HIDDEN:
  503.     if (type == T_NULL) {
  504.       PUT_SCN_SCNLEN(abfd, in->x_scn.x_scnlen, ext);
  505.       PUT_SCN_NRELOC(abfd, in->x_scn.x_nreloc, ext);
  506.       PUT_SCN_NLINNO(abfd, in->x_scn.x_nlinno, ext);
  507.       return AUXESZ;
  508.     }
  509.     break;
  510.   }
  511.  
  512.   PUTWORD(abfd, in->x_sym.x_tagndx.l, (bfd_byte *) ext->x_sym.x_tagndx);
  513. #ifndef NO_TVNDX
  514.   bfd_h_put_16(abfd, in->x_sym.x_tvndx , (bfd_byte *) ext->x_sym.x_tvndx);
  515. #endif
  516.  
  517.   if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
  518.     {
  519.       PUT_FCN_LNNOPTR(abfd,  in->x_sym.x_fcnary.x_fcn.x_lnnoptr, ext);
  520.       PUT_FCN_ENDNDX(abfd,  in->x_sym.x_fcnary.x_fcn.x_endndx.l, ext);
  521.     }
  522.   else
  523.     {
  524. #if DIMNUM != E_DIMNUM
  525.  #error we need to cope with truncating or extending DIMNUM
  526. #endif
  527.       bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
  528.             (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
  529.       bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
  530.             (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
  531.       bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
  532.             (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
  533.       bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
  534.             (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
  535.     }
  536.  
  537.   if (ISFCN (type))
  538.     PUTWORD (abfd, in->x_sym.x_misc.x_fsize,
  539.          (bfd_byte *)  ext->x_sym.x_misc.x_fsize);
  540.   else
  541.     {
  542.       PUT_LNSZ_LNNO (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext);
  543.       PUT_LNSZ_SIZE (abfd, in->x_sym.x_misc.x_lnsz.x_size, ext);
  544.     }
  545.  
  546.   return AUXESZ;
  547. }
  548.  
  549. #endif /* NO_COFF_SYMBOLS */
  550.  
  551. #ifndef NO_COFF_LINENOS
  552.  
  553. static void
  554. coff_swap_lineno_in (abfd, ext1, in1)
  555.      bfd            *abfd;
  556.      PTR ext1;
  557.      PTR in1;
  558. {
  559.   LINENO *ext = (LINENO *)ext1;
  560.   struct internal_lineno      *in = (struct internal_lineno *)in1;
  561.  
  562.   in->l_addr.l_symndx = bfd_h_get_32(abfd, (bfd_byte *) ext->l_addr.l_symndx);
  563.   in->l_lnno = GET_LINENO_LNNO(abfd, ext);
  564. }
  565.  
  566. static unsigned int
  567. coff_swap_lineno_out (abfd, inp, outp)
  568.      bfd       *abfd;
  569.      PTR    inp;
  570.      PTR    outp;
  571. {
  572.   struct internal_lineno *in = (struct internal_lineno *)inp;
  573.   struct external_lineno *ext = (struct external_lineno *)outp;
  574.   PUTWORD(abfd, in->l_addr.l_symndx, (bfd_byte *)
  575.       ext->l_addr.l_symndx);
  576.  
  577.   PUT_LINENO_LNNO (abfd, in->l_lnno, ext);
  578.   return LINESZ;
  579. }
  580.  
  581. #endif /* NO_COFF_LINENOS */
  582.  
  583.  
  584. static void
  585. coff_swap_aouthdr_in (abfd, aouthdr_ext1, aouthdr_int1)
  586.      bfd            *abfd;
  587.      PTR aouthdr_ext1;
  588.      PTR aouthdr_int1;
  589. {
  590.   AOUTHDR        *aouthdr_ext = (AOUTHDR *) aouthdr_ext1;
  591.   struct internal_aouthdr *aouthdr_int = (struct internal_aouthdr *)aouthdr_int1;
  592.  
  593.   aouthdr_int->magic = bfd_h_get_16(abfd, (bfd_byte *) aouthdr_ext->magic);
  594.   aouthdr_int->vstamp = bfd_h_get_16(abfd, (bfd_byte *) aouthdr_ext->vstamp);
  595.   aouthdr_int->tsize =
  596.     GET_AOUTHDR_TSIZE (abfd, (bfd_byte *) aouthdr_ext->tsize);
  597.   aouthdr_int->dsize =
  598.     GET_AOUTHDR_DSIZE (abfd, (bfd_byte *) aouthdr_ext->dsize);
  599.   aouthdr_int->bsize =
  600.     GET_AOUTHDR_BSIZE (abfd, (bfd_byte *) aouthdr_ext->bsize);
  601.   aouthdr_int->entry =
  602.     GET_AOUTHDR_ENTRY (abfd, (bfd_byte *) aouthdr_ext->entry);
  603.   aouthdr_int->text_start =
  604.     GET_AOUTHDR_TEXT_START (abfd, (bfd_byte *) aouthdr_ext->text_start);
  605.   aouthdr_int->data_start =
  606.     GET_AOUTHDR_DATA_START (abfd, (bfd_byte *) aouthdr_ext->data_start);
  607.  
  608. #ifdef I960
  609.   aouthdr_int->tagentries = bfd_h_get_32(abfd, (bfd_byte *) aouthdr_ext->tagentries);
  610. #endif
  611.  
  612. #ifdef APOLLO_M68
  613.   bfd_h_put_32(abfd, aouthdr_int->o_inlib, (bfd_byte *) aouthdr_ext->o_inlib);
  614.   bfd_h_put_32(abfd, aouthdr_int->o_sri, (bfd_byte *) aouthdr_ext->o_sri);
  615.   bfd_h_put_32(abfd, aouthdr_int->vid[0], (bfd_byte *) aouthdr_ext->vid);
  616.   bfd_h_put_32(abfd, aouthdr_int->vid[1], (bfd_byte *) aouthdr_ext->vid + 4);
  617. #endif
  618.  
  619.  
  620. #ifdef RS6000COFF_C
  621.   aouthdr_int->o_toc = bfd_h_get_32(abfd, aouthdr_ext->o_toc);
  622.   aouthdr_int->o_snentry = bfd_h_get_16(abfd, aouthdr_ext->o_snentry);
  623.   aouthdr_int->o_sntext = bfd_h_get_16(abfd, aouthdr_ext->o_sntext);
  624.   aouthdr_int->o_sndata = bfd_h_get_16(abfd, aouthdr_ext->o_sndata);
  625.   aouthdr_int->o_sntoc = bfd_h_get_16(abfd, aouthdr_ext->o_sntoc);
  626.   aouthdr_int->o_snloader = bfd_h_get_16(abfd, aouthdr_ext->o_snloader);
  627.   aouthdr_int->o_snbss = bfd_h_get_16(abfd, aouthdr_ext->o_snbss);
  628.   aouthdr_int->o_algntext = bfd_h_get_16(abfd, aouthdr_ext->o_algntext);
  629.   aouthdr_int->o_algndata = bfd_h_get_16(abfd, aouthdr_ext->o_algndata);
  630.   aouthdr_int->o_modtype = bfd_h_get_16(abfd, aouthdr_ext->o_modtype);
  631.   aouthdr_int->o_cputype = bfd_h_get_16(abfd, aouthdr_ext->o_cputype);
  632.   aouthdr_int->o_maxstack = bfd_h_get_32(abfd, aouthdr_ext->o_maxstack);
  633.   aouthdr_int->o_maxdata = bfd_h_get_32(abfd, aouthdr_ext->o_maxdata);
  634. #endif
  635.  
  636. #ifdef MIPSECOFF
  637.   aouthdr_int->bss_start = bfd_h_get_32(abfd, aouthdr_ext->bss_start);
  638.   aouthdr_int->gp_value = bfd_h_get_32(abfd, aouthdr_ext->gp_value);
  639.   aouthdr_int->gprmask = bfd_h_get_32(abfd, aouthdr_ext->gprmask);
  640.   aouthdr_int->cprmask[0] = bfd_h_get_32(abfd, aouthdr_ext->cprmask[0]);
  641.   aouthdr_int->cprmask[1] = bfd_h_get_32(abfd, aouthdr_ext->cprmask[1]);
  642.   aouthdr_int->cprmask[2] = bfd_h_get_32(abfd, aouthdr_ext->cprmask[2]);
  643.   aouthdr_int->cprmask[3] = bfd_h_get_32(abfd, aouthdr_ext->cprmask[3]);
  644. #endif
  645.  
  646. #ifdef ALPHAECOFF
  647.   aouthdr_int->bss_start = bfd_h_get_64(abfd, aouthdr_ext->bss_start);
  648.   aouthdr_int->gp_value = bfd_h_get_64(abfd, aouthdr_ext->gp_value);
  649.   aouthdr_int->gprmask = bfd_h_get_32(abfd, aouthdr_ext->gprmask);
  650.   aouthdr_int->fprmask = bfd_h_get_32(abfd, aouthdr_ext->fprmask);
  651. #endif
  652. }
  653.  
  654. static unsigned int
  655. coff_swap_aouthdr_out (abfd, in, out)
  656.      bfd       *abfd;
  657.      PTR    in;
  658.      PTR    out;
  659. {
  660.   struct internal_aouthdr *aouthdr_in = (struct internal_aouthdr *)in;
  661.   AOUTHDR *aouthdr_out = (AOUTHDR *)out;
  662.  
  663.   bfd_h_put_16(abfd, aouthdr_in->magic, (bfd_byte *) aouthdr_out->magic);
  664.   bfd_h_put_16(abfd, aouthdr_in->vstamp, (bfd_byte *) aouthdr_out->vstamp);
  665.   PUT_AOUTHDR_TSIZE (abfd, aouthdr_in->tsize, (bfd_byte *) aouthdr_out->tsize);
  666.   PUT_AOUTHDR_DSIZE (abfd, aouthdr_in->dsize, (bfd_byte *) aouthdr_out->dsize);
  667.   PUT_AOUTHDR_BSIZE (abfd, aouthdr_in->bsize, (bfd_byte *) aouthdr_out->bsize);
  668.   PUT_AOUTHDR_ENTRY (abfd, aouthdr_in->entry, (bfd_byte *) aouthdr_out->entry);
  669.   PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start,
  670.               (bfd_byte *) aouthdr_out->text_start);
  671.   PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start,
  672.               (bfd_byte *) aouthdr_out->data_start);
  673.  
  674. #ifdef I960
  675.   bfd_h_put_32(abfd, aouthdr_in->tagentries, (bfd_byte *) aouthdr_out->tagentries);
  676. #endif
  677.  
  678. #ifdef RS6000COFF_C
  679.   bfd_h_put_32 (abfd, aouthdr_in->o_toc, aouthdr_out->o_toc);
  680.   bfd_h_put_16 (abfd, aouthdr_in->o_snentry, aouthdr_out->o_snentry);
  681.   bfd_h_put_16 (abfd, aouthdr_in->o_sntext, aouthdr_out->o_sntext);
  682.   bfd_h_put_16 (abfd, aouthdr_in->o_sndata, aouthdr_out->o_sndata);
  683.   bfd_h_put_16 (abfd, aouthdr_in->o_sntoc, aouthdr_out->o_sntoc);
  684.   bfd_h_put_16 (abfd, aouthdr_in->o_snloader, aouthdr_out->o_snloader);
  685.   bfd_h_put_16 (abfd, aouthdr_in->o_snbss, aouthdr_out->o_snbss);
  686.   bfd_h_put_16 (abfd, aouthdr_in->o_algntext, aouthdr_out->o_algntext);
  687.   bfd_h_put_16 (abfd, aouthdr_in->o_algndata, aouthdr_out->o_algndata);
  688.   bfd_h_put_16 (abfd, aouthdr_in->o_modtype, aouthdr_out->o_modtype);
  689.   bfd_h_put_16 (abfd, aouthdr_in->o_cputype, aouthdr_out->o_cputype);
  690.   bfd_h_put_32 (abfd, aouthdr_in->o_maxstack, aouthdr_out->o_maxstack);
  691.   bfd_h_put_32 (abfd, aouthdr_in->o_maxdata, aouthdr_out->o_maxdata);
  692.   memset (aouthdr_out->o_resv2, 0, sizeof aouthdr_out->o_resv2);
  693. #endif
  694.  
  695. #ifdef MIPSECOFF
  696.   bfd_h_put_32(abfd, aouthdr_in->bss_start, (bfd_byte *) aouthdr_out->bss_start);
  697.   bfd_h_put_32(abfd, aouthdr_in->gp_value, (bfd_byte *) aouthdr_out->gp_value);
  698.   bfd_h_put_32(abfd, aouthdr_in->gprmask, (bfd_byte *) aouthdr_out->gprmask);
  699.   bfd_h_put_32(abfd, aouthdr_in->cprmask[0], (bfd_byte *) aouthdr_out->cprmask[0]);
  700.   bfd_h_put_32(abfd, aouthdr_in->cprmask[1], (bfd_byte *) aouthdr_out->cprmask[1]);
  701.   bfd_h_put_32(abfd, aouthdr_in->cprmask[2], (bfd_byte *) aouthdr_out->cprmask[2]);
  702.   bfd_h_put_32(abfd, aouthdr_in->cprmask[3], (bfd_byte *) aouthdr_out->cprmask[3]);
  703. #endif
  704.  
  705. #ifdef ALPHAECOFF
  706.   /* FIXME: What does bldrev mean?  */
  707.   bfd_h_put_16(abfd, (bfd_vma) 2, (bfd_byte *) aouthdr_out->bldrev);
  708.   bfd_h_put_16(abfd, (bfd_vma) 0, (bfd_byte *) aouthdr_out->padding);
  709.   bfd_h_put_64(abfd, aouthdr_in->bss_start, (bfd_byte *) aouthdr_out->bss_start);
  710.   bfd_h_put_64(abfd, aouthdr_in->gp_value, (bfd_byte *) aouthdr_out->gp_value);
  711.   bfd_h_put_32(abfd, aouthdr_in->gprmask, (bfd_byte *) aouthdr_out->gprmask);
  712.   bfd_h_put_32(abfd, aouthdr_in->fprmask, (bfd_byte *) aouthdr_out->fprmask);
  713. #endif
  714.  
  715.   return AOUTSZ;
  716. }
  717.  
  718. static void
  719. coff_swap_scnhdr_in (abfd, ext, in)
  720.      bfd            *abfd;
  721.      PTR         ext;
  722.      PTR         in;
  723. {
  724.   SCNHDR *scnhdr_ext = (SCNHDR *) ext;
  725.   struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
  726.  
  727.   memcpy(scnhdr_int->s_name, scnhdr_ext->s_name, sizeof(scnhdr_int->s_name));
  728.   scnhdr_int->s_vaddr =
  729.     GET_SCNHDR_VADDR (abfd, (bfd_byte *) scnhdr_ext->s_vaddr);
  730.   scnhdr_int->s_paddr =
  731.     GET_SCNHDR_PADDR (abfd, (bfd_byte *) scnhdr_ext->s_paddr);
  732.   scnhdr_int->s_size =
  733.     GET_SCNHDR_SIZE (abfd, (bfd_byte *) scnhdr_ext->s_size);
  734.  
  735.   scnhdr_int->s_scnptr =
  736.     GET_SCNHDR_SCNPTR (abfd, (bfd_byte *) scnhdr_ext->s_scnptr);
  737.   scnhdr_int->s_relptr =
  738.     GET_SCNHDR_RELPTR (abfd, (bfd_byte *) scnhdr_ext->s_relptr);
  739.   scnhdr_int->s_lnnoptr =
  740.     GET_SCNHDR_LNNOPTR (abfd, (bfd_byte *) scnhdr_ext->s_lnnoptr);
  741.   scnhdr_int->s_flags = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_flags);
  742. #if defined(M88)
  743.   scnhdr_int->s_nreloc = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_nreloc);
  744.   scnhdr_int->s_nlnno = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_nlnno);
  745. #else
  746.   scnhdr_int->s_nreloc = bfd_h_get_16(abfd, (bfd_byte *) scnhdr_ext->s_nreloc);
  747.   scnhdr_int->s_nlnno = bfd_h_get_16(abfd, (bfd_byte *) scnhdr_ext->s_nlnno);
  748. #endif
  749. #ifdef I960
  750.   scnhdr_int->s_align = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_align);
  751. #endif
  752. }
  753.  
  754. static unsigned int
  755. coff_swap_scnhdr_out (abfd, in, out)
  756.      bfd       *abfd;
  757.      PTR    in;
  758.      PTR    out;
  759. {
  760.   struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *)in;
  761.   SCNHDR *scnhdr_ext = (SCNHDR *)out;
  762.   unsigned int ret = SCNHSZ;
  763.  
  764.   memcpy(scnhdr_ext->s_name, scnhdr_int->s_name, sizeof(scnhdr_int->s_name));
  765.  
  766.   PUT_SCNHDR_VADDR (abfd, scnhdr_int->s_vaddr,
  767.             (bfd_byte *) scnhdr_ext->s_vaddr);
  768.  
  769.  
  770.   PUT_SCNHDR_PADDR (abfd, scnhdr_int->s_paddr,
  771.             (bfd_byte *) scnhdr_ext->s_paddr);
  772.   PUT_SCNHDR_SIZE (abfd, scnhdr_int->s_size,
  773.            (bfd_byte *) scnhdr_ext->s_size);
  774.  
  775.   PUT_SCNHDR_SCNPTR (abfd, scnhdr_int->s_scnptr,
  776.              (bfd_byte *) scnhdr_ext->s_scnptr);
  777.   PUT_SCNHDR_RELPTR (abfd, scnhdr_int->s_relptr,
  778.              (bfd_byte *) scnhdr_ext->s_relptr);
  779.   PUT_SCNHDR_LNNOPTR (abfd, scnhdr_int->s_lnnoptr,
  780.               (bfd_byte *) scnhdr_ext->s_lnnoptr);
  781.   PUTWORD(abfd, scnhdr_int->s_flags, (bfd_byte *) scnhdr_ext->s_flags);
  782. #if defined(M88)
  783.   PUTWORD(abfd, scnhdr_int->s_nlnno, (bfd_byte *) scnhdr_ext->s_nlnno);
  784.   PUTWORD(abfd, scnhdr_int->s_nreloc, (bfd_byte *) scnhdr_ext->s_nreloc);
  785. #else
  786.   if (scnhdr_int->s_nlnno <= 0xffff)
  787.     PUTHALF(abfd, scnhdr_int->s_nlnno, (bfd_byte *) scnhdr_ext->s_nlnno);
  788.   else
  789.     {
  790.       char buf[sizeof (scnhdr_int->s_name) + 1];
  791.  
  792.       memcpy (buf, scnhdr_int->s_name, sizeof (scnhdr_int->s_name));
  793.       buf[sizeof (scnhdr_int->s_name)] = '\0';
  794.       (*_bfd_error_handler) ("%s: %s: line number overflow: 0x%lx > 0xffff",
  795.                  bfd_get_filename (abfd),
  796.                  buf, scnhdr_int->s_nlnno);
  797.       bfd_set_error (bfd_error_file_truncated);
  798.       PUTHALF (abfd, 0xffff, (bfd_byte *) scnhdr_ext->s_nlnno);
  799.       ret = 0;
  800.     }
  801.   if (scnhdr_int->s_nreloc <= 0xffff)
  802.     PUTHALF(abfd, scnhdr_int->s_nreloc, (bfd_byte *) scnhdr_ext->s_nreloc);
  803.   else
  804.     {
  805.       char buf[sizeof (scnhdr_int->s_name) + 1];
  806.  
  807.       memcpy (buf, scnhdr_int->s_name, sizeof (scnhdr_int->s_name));
  808.       buf[sizeof (scnhdr_int->s_name)] = '\0';
  809.       (*_bfd_error_handler) ("%s: %s: reloc overflow: 0x%lx > 0xffff",
  810.                  bfd_get_filename (abfd),
  811.                  buf, scnhdr_int->s_nreloc);
  812.       bfd_set_error (bfd_error_file_truncated);
  813.       PUTHALF (abfd, 0xffff, (bfd_byte *) scnhdr_ext->s_nreloc);
  814.       ret = 0;
  815.     }
  816. #endif
  817.  
  818. #if defined(I960)
  819.   PUTWORD(abfd, scnhdr_int->s_align, (bfd_byte *) scnhdr_ext->s_align);
  820. #endif
  821.   return ret;
  822. }
  823.