Home | History | Annotate | Download | only in bfd
      1 /* BFD back-end for IBM RS/6000 "XCOFF" files.
      2    Copyright (C) 1990-2016 Free Software Foundation, Inc.
      3    Written by Metin G. Ozisik, Mimi Phuong-Thao Vo, and John Gilmore.
      4    Archive support from Damon A. Permezel.
      5    Contributed by IBM Corporation and Cygnus Support.
      6 
      7    This file is part of BFD, the Binary File Descriptor library.
      8 
      9    This program is free software; you can redistribute it and/or modify
     10    it under the terms of the GNU General Public License as published by
     11    the Free Software Foundation; either version 3 of the License, or
     12    (at your option) any later version.
     13 
     14    This program is distributed in the hope that it will be useful,
     15    but WITHOUT ANY WARRANTY; without even the implied warranty of
     16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17    GNU General Public License for more details.
     18 
     19    You should have received a copy of the GNU General Public License
     20    along with this program; if not, write to the Free Software
     21    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     22    MA 02110-1301, USA.  */
     23 
     24 #include "sysdep.h"
     25 #include "libiberty.h"
     26 #include "bfd.h"
     27 #include "bfdlink.h"
     28 #include "libbfd.h"
     29 #include "coff/internal.h"
     30 #include "coff/xcoff.h"
     31 #include "coff/rs6000.h"
     32 #include "libcoff.h"
     33 #include "libxcoff.h"
     34 
     35 extern bfd_boolean _bfd_xcoff_mkobject (bfd *);
     36 extern bfd_boolean _bfd_xcoff_copy_private_bfd_data (bfd *, bfd *);
     37 extern bfd_boolean _bfd_xcoff_is_local_label_name (bfd *, const char *);
     38 extern reloc_howto_type *_bfd_xcoff_reloc_type_lookup
     39   (bfd *, bfd_reloc_code_real_type);
     40 extern bfd_boolean _bfd_xcoff_slurp_armap (bfd *);
     41 extern const bfd_target *_bfd_xcoff_archive_p (bfd *);
     42 extern void * _bfd_xcoff_read_ar_hdr (bfd *);
     43 extern bfd *_bfd_xcoff_openr_next_archived_file (bfd *, bfd *);
     44 extern int _bfd_xcoff_stat_arch_elt (bfd *, struct stat *);
     45 extern bfd_boolean _bfd_xcoff_write_armap
     46   (bfd *, unsigned int, struct orl *, unsigned int, int);
     47 extern bfd_boolean _bfd_xcoff_write_archive_contents (bfd *);
     48 extern int _bfd_xcoff_sizeof_headers (bfd *, struct bfd_link_info *);
     49 extern void _bfd_xcoff_swap_sym_in (bfd *, void *, void *);
     50 extern unsigned int _bfd_xcoff_swap_sym_out (bfd *, void *, void *);
     51 extern void _bfd_xcoff_swap_aux_in (bfd *, void *, int, int, int, int, void *);
     52 extern unsigned int _bfd_xcoff_swap_aux_out
     53   (bfd *, void *, int, int, int, int, void *);
     54 static void xcoff_swap_reloc_in (bfd *, void *, void *);
     55 static unsigned int xcoff_swap_reloc_out (bfd *, void *, void *);
     56 
     57 /* Forward declare xcoff_rtype2howto for coffcode.h macro.  */
     58 void xcoff_rtype2howto (arelent *, struct internal_reloc *);
     59 
     60 /* coffcode.h needs these to be defined.  */
     61 #define RS6000COFF_C 1
     62 
     63 #define SELECT_RELOC(internal, howto)					\
     64   {									\
     65     internal.r_type = howto->type;					\
     66     internal.r_size =							\
     67       ((howto->complain_on_overflow == complain_overflow_signed		\
     68 	? 0x80								\
     69 	: 0)								\
     70        | (howto->bitsize - 1));						\
     71   }
     72 
     73 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
     74 #define COFF_LONG_FILENAMES
     75 #define NO_COFF_SYMBOLS
     76 #define RTYPE2HOWTO(cache_ptr, dst) xcoff_rtype2howto (cache_ptr, dst)
     77 #define coff_mkobject _bfd_xcoff_mkobject
     78 #define coff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
     79 #ifdef AIX_CORE
     80 extern const bfd_target * rs6000coff_core_p (bfd *abfd);
     81 extern bfd_boolean rs6000coff_core_file_matches_executable_p
     82   (bfd *cbfd, bfd *ebfd);
     83 extern char *rs6000coff_core_file_failing_command (bfd *abfd);
     84 extern int rs6000coff_core_file_failing_signal (bfd *abfd);
     85 #define CORE_FILE_P rs6000coff_core_p
     86 #define coff_core_file_failing_command \
     87   rs6000coff_core_file_failing_command
     88 #define coff_core_file_failing_signal \
     89   rs6000coff_core_file_failing_signal
     90 #define coff_core_file_matches_executable_p \
     91   rs6000coff_core_file_matches_executable_p
     92 #define coff_core_file_pid \
     93   _bfd_nocore_core_file_pid
     94 #else
     95 #define CORE_FILE_P _bfd_dummy_target
     96 #define coff_core_file_failing_command \
     97   _bfd_nocore_core_file_failing_command
     98 #define coff_core_file_failing_signal \
     99   _bfd_nocore_core_file_failing_signal
    100 #define coff_core_file_matches_executable_p \
    101   _bfd_nocore_core_file_matches_executable_p
    102 #define coff_core_file_pid \
    103   _bfd_nocore_core_file_pid
    104 #endif
    105 #define coff_SWAP_sym_in _bfd_xcoff_swap_sym_in
    106 #define coff_SWAP_sym_out _bfd_xcoff_swap_sym_out
    107 #define coff_SWAP_aux_in _bfd_xcoff_swap_aux_in
    108 #define coff_SWAP_aux_out _bfd_xcoff_swap_aux_out
    109 #define coff_swap_reloc_in xcoff_swap_reloc_in
    110 #define coff_swap_reloc_out xcoff_swap_reloc_out
    111 #define NO_COFF_RELOCS
    112 
    113 #ifndef bfd_pe_print_pdata
    114 #define bfd_pe_print_pdata	NULL
    115 #endif
    116 
    117 #include <stdint.h>
    118 #include "coffcode.h"
    119 
    120 /* The main body of code is in coffcode.h.  */
    121 
    122 static const char *normalize_filename (bfd *);
    123 static bfd_boolean xcoff_write_armap_old
    124   (bfd *, unsigned int, struct orl *, unsigned int, int);
    125 static bfd_boolean xcoff_write_armap_big
    126   (bfd *, unsigned int, struct orl *, unsigned int, int);
    127 static bfd_boolean xcoff_write_archive_contents_old (bfd *);
    128 static bfd_boolean xcoff_write_archive_contents_big (bfd *);
    129 static void xcoff_swap_ldhdr_in (bfd *, const void *, struct internal_ldhdr *);
    130 static void xcoff_swap_ldhdr_out (bfd *, const struct internal_ldhdr *, void *);
    131 static void xcoff_swap_ldsym_in (bfd *, const void *, struct internal_ldsym *);
    132 static void xcoff_swap_ldsym_out (bfd *, const struct internal_ldsym *, void *);
    133 static void xcoff_swap_ldrel_in (bfd *, const void *, struct internal_ldrel *);
    134 static void xcoff_swap_ldrel_out (bfd *, const struct internal_ldrel *, void *);
    135 static bfd_boolean xcoff_ppc_relocate_section
    136   (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
    137    struct internal_reloc *, struct internal_syment *, asection **);
    138 static bfd_boolean _bfd_xcoff_put_ldsymbol_name
    139   (bfd *, struct xcoff_loader_info *, struct internal_ldsym *, const char *);
    140 static asection *xcoff_create_csect_from_smclas
    141   (bfd *, union internal_auxent *, const char *);
    142 static bfd_boolean xcoff_is_lineno_count_overflow (bfd *, bfd_vma);
    143 static bfd_boolean xcoff_is_reloc_count_overflow (bfd *, bfd_vma);
    144 static bfd_vma xcoff_loader_symbol_offset (bfd *, struct internal_ldhdr *);
    145 static bfd_vma xcoff_loader_reloc_offset (bfd *, struct internal_ldhdr *);
    146 static bfd_boolean xcoff_generate_rtinit
    147   (bfd *, const char *, const char *, bfd_boolean);
    148 static bfd_boolean do_pad (bfd *, unsigned int);
    149 static bfd_boolean do_copy (bfd *, bfd *);
    150 
    151 /* Relocation functions */
    152 static bfd_boolean xcoff_reloc_type_br (XCOFF_RELOC_FUNCTION_ARGS);
    153 
    154 static bfd_boolean xcoff_complain_overflow_dont_func
    155   (XCOFF_COMPLAIN_FUNCTION_ARGS);
    156 static bfd_boolean xcoff_complain_overflow_bitfield_func
    157   (XCOFF_COMPLAIN_FUNCTION_ARGS);
    158 static bfd_boolean xcoff_complain_overflow_signed_func
    159   (XCOFF_COMPLAIN_FUNCTION_ARGS);
    160 static bfd_boolean xcoff_complain_overflow_unsigned_func
    161   (XCOFF_COMPLAIN_FUNCTION_ARGS);
    162 
    163 bfd_boolean (*xcoff_calculate_relocation[XCOFF_MAX_CALCULATE_RELOCATION])
    164   (XCOFF_RELOC_FUNCTION_ARGS) =
    165 {
    166   xcoff_reloc_type_pos,	 /* R_POS   (0x00) */
    167   xcoff_reloc_type_neg,	 /* R_NEG   (0x01) */
    168   xcoff_reloc_type_rel,	 /* R_REL   (0x02) */
    169   xcoff_reloc_type_toc,	 /* R_TOC   (0x03) */
    170   xcoff_reloc_type_fail, /* R_RTB   (0x04) */
    171   xcoff_reloc_type_toc,	 /* R_GL    (0x05) */
    172   xcoff_reloc_type_toc,	 /* R_TCL   (0x06) */
    173   xcoff_reloc_type_fail, /*	    (0x07) */
    174   xcoff_reloc_type_ba,	 /* R_BA    (0x08) */
    175   xcoff_reloc_type_fail, /*	    (0x09) */
    176   xcoff_reloc_type_br,	 /* R_BR    (0x0a) */
    177   xcoff_reloc_type_fail, /*	    (0x0b) */
    178   xcoff_reloc_type_pos,	 /* R_RL    (0x0c) */
    179   xcoff_reloc_type_pos,	 /* R_RLA   (0x0d) */
    180   xcoff_reloc_type_fail, /*	    (0x0e) */
    181   xcoff_reloc_type_noop, /* R_REF   (0x0f) */
    182   xcoff_reloc_type_fail, /*	    (0x10) */
    183   xcoff_reloc_type_fail, /*	    (0x11) */
    184   xcoff_reloc_type_toc,	 /* R_TRL   (0x12) */
    185   xcoff_reloc_type_toc,	 /* R_TRLA  (0x13) */
    186   xcoff_reloc_type_fail, /* R_RRTBI (0x14) */
    187   xcoff_reloc_type_fail, /* R_RRTBA (0x15) */
    188   xcoff_reloc_type_ba,	 /* R_CAI   (0x16) */
    189   xcoff_reloc_type_crel, /* R_CREL  (0x17) */
    190   xcoff_reloc_type_ba,	 /* R_RBA   (0x18) */
    191   xcoff_reloc_type_ba,	 /* R_RBAC  (0x19) */
    192   xcoff_reloc_type_br,	 /* R_RBR   (0x1a) */
    193   xcoff_reloc_type_ba,	 /* R_RBRC  (0x1b) */
    194 };
    195 
    196 bfd_boolean (*xcoff_complain_overflow[XCOFF_MAX_COMPLAIN_OVERFLOW])
    197   (XCOFF_COMPLAIN_FUNCTION_ARGS) =
    198 {
    199   xcoff_complain_overflow_dont_func,
    200   xcoff_complain_overflow_bitfield_func,
    201   xcoff_complain_overflow_signed_func,
    202   xcoff_complain_overflow_unsigned_func,
    203 };
    204 
    205 /* Information about one member of an archive.  */
    206 struct member_layout {
    207   /* The archive member that this structure describes.  */
    208   bfd *member;
    209 
    210   /* The number of bytes of padding that must be inserted before the
    211      start of the member in order to ensure that the section contents
    212      are correctly aligned.  */
    213   unsigned int leading_padding;
    214 
    215   /* The offset of MEMBER from the start of the archive (i.e. the end
    216      of the leading padding).  */
    217   file_ptr offset;
    218 
    219   /* The normalized name of MEMBER.  */
    220   const char *name;
    221 
    222   /* The length of NAME, without padding.  */
    223   bfd_size_type namlen;
    224 
    225   /* The length of NAME, with padding.  */
    226   bfd_size_type padded_namlen;
    227 
    228   /* The size of MEMBER's header, including the name and magic sequence.  */
    229   bfd_size_type header_size;
    230 
    231   /* The size of the MEMBER's contents.  */
    232   bfd_size_type contents_size;
    233 
    234   /* The number of bytes of padding that must be inserted after MEMBER
    235      in order to preserve even alignment.  */
    236   bfd_size_type trailing_padding;
    237 };
    238 
    239 /* A structure used for iterating over the members of an archive.  */
    240 struct archive_iterator {
    241   /* The archive itself.  */
    242   bfd *archive;
    243 
    244   /* Information about the current archive member.  */
    245   struct member_layout current;
    246 
    247   /* Information about the next archive member.  MEMBER is null if there
    248      are no more archive members, in which case OFFSET is the offset of
    249      the first unused byte.  */
    250   struct member_layout next;
    251 };
    252 
    253 /* Initialize INFO so that it describes member MEMBER of archive ARCHIVE.
    254    OFFSET is the even-padded offset of MEMBER, not including any leading
    255    padding needed for section alignment.  */
    256 
    257 static void
    258 member_layout_init (struct member_layout *info, bfd *archive,
    259 		    bfd *member, file_ptr offset)
    260 {
    261   info->member = member;
    262   info->leading_padding = 0;
    263   if (member)
    264     {
    265       info->name = normalize_filename (member);
    266       info->namlen = strlen (info->name);
    267       info->padded_namlen = info->namlen + (info->namlen & 1);
    268       if (xcoff_big_format_p (archive))
    269 	info->header_size = SIZEOF_AR_HDR_BIG;
    270       else
    271 	info->header_size = SIZEOF_AR_HDR;
    272       info->header_size += info->padded_namlen + SXCOFFARFMAG;
    273       info->contents_size = arelt_size (member);
    274       info->trailing_padding = info->contents_size & 1;
    275 
    276       if (bfd_check_format (member, bfd_object)
    277 	  && bfd_get_flavour (member) == bfd_target_xcoff_flavour
    278 	  && (member->flags & DYNAMIC) != 0)
    279 	info->leading_padding
    280 	  = (-(offset + info->header_size)
    281 	     & ((1 << bfd_xcoff_text_align_power (member)) - 1));
    282     }
    283   info->offset = offset + info->leading_padding;
    284 }
    285 
    286 /* Set up ITERATOR to iterate through archive ARCHIVE.  */
    287 
    288 static void
    289 archive_iterator_begin (struct archive_iterator *iterator,
    290 			bfd *archive)
    291 {
    292   iterator->archive = archive;
    293   member_layout_init (&iterator->next, archive, archive->archive_head,
    294 		      xcoff_big_format_p (archive)
    295 		      ? SIZEOF_AR_FILE_HDR_BIG
    296 		      : SIZEOF_AR_FILE_HDR);
    297 }
    298 
    299 /* Make ITERATOR visit the first unvisited archive member.  Return true
    300    on success; return false if all members have been visited.  */
    301 
    302 static bfd_boolean
    303 archive_iterator_next (struct archive_iterator *iterator)
    304 {
    305   if (!iterator->next.member)
    306     return FALSE;
    307 
    308   iterator->current = iterator->next;
    309   member_layout_init (&iterator->next, iterator->archive,
    310 		      iterator->current.member->archive_next,
    311 		      iterator->current.offset
    312 		      + iterator->current.header_size
    313 		      + iterator->current.contents_size
    314 		      + iterator->current.trailing_padding);
    315   return TRUE;
    316 }
    317 
    318 /* We use our own tdata type.  Its first field is the COFF tdata type,
    319    so the COFF routines are compatible.  */
    320 
    321 bfd_boolean
    322 _bfd_xcoff_mkobject (bfd *abfd)
    323 {
    324   coff_data_type *coff;
    325   bfd_size_type amt = sizeof (struct xcoff_tdata);
    326 
    327   abfd->tdata.xcoff_obj_data = (struct xcoff_tdata *) bfd_zalloc (abfd, amt);
    328   if (abfd->tdata.xcoff_obj_data == NULL)
    329     return FALSE;
    330   coff = coff_data (abfd);
    331   coff->symbols = (coff_symbol_type *) NULL;
    332   coff->conversion_table = (unsigned int *) NULL;
    333   coff->raw_syments = (struct coff_ptr_struct *) NULL;
    334   coff->relocbase = 0;
    335 
    336   xcoff_data (abfd)->modtype = ('1' << 8) | 'L';
    337 
    338   /* We set cputype to -1 to indicate that it has not been
    339      initialized.  */
    340   xcoff_data (abfd)->cputype = -1;
    341 
    342   xcoff_data (abfd)->csects = NULL;
    343   xcoff_data (abfd)->debug_indices = NULL;
    344 
    345   /* text section alignment is different than the default */
    346   bfd_xcoff_text_align_power (abfd) = 2;
    347 
    348   return TRUE;
    349 }
    350 
    351 /* Copy XCOFF data from one BFD to another.  */
    352 
    353 bfd_boolean
    354 _bfd_xcoff_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
    355 {
    356   struct xcoff_tdata *ix, *ox;
    357   asection *sec;
    358 
    359   if (ibfd->xvec != obfd->xvec)
    360     return TRUE;
    361   ix = xcoff_data (ibfd);
    362   ox = xcoff_data (obfd);
    363   ox->full_aouthdr = ix->full_aouthdr;
    364   ox->toc = ix->toc;
    365   if (ix->sntoc == 0)
    366     ox->sntoc = 0;
    367   else
    368     {
    369       sec = coff_section_from_bfd_index (ibfd, ix->sntoc);
    370       if (sec == NULL)
    371 	ox->sntoc = 0;
    372       else
    373 	ox->sntoc = sec->output_section->target_index;
    374     }
    375   if (ix->snentry == 0)
    376     ox->snentry = 0;
    377   else
    378     {
    379       sec = coff_section_from_bfd_index (ibfd, ix->snentry);
    380       if (sec == NULL)
    381 	ox->snentry = 0;
    382       else
    383 	ox->snentry = sec->output_section->target_index;
    384     }
    385   bfd_xcoff_text_align_power (obfd) = bfd_xcoff_text_align_power (ibfd);
    386   bfd_xcoff_data_align_power (obfd) = bfd_xcoff_data_align_power (ibfd);
    387   ox->modtype = ix->modtype;
    388   ox->cputype = ix->cputype;
    389   ox->maxdata = ix->maxdata;
    390   ox->maxstack = ix->maxstack;
    391   return TRUE;
    392 }
    393 
    394 /* I don't think XCOFF really has a notion of local labels based on
    395    name.  This will mean that ld -X doesn't actually strip anything.
    396    The AIX native linker does not have a -X option, and it ignores the
    397    -x option.  */
    398 
    399 bfd_boolean
    400 _bfd_xcoff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
    401                                 const char *name ATTRIBUTE_UNUSED)
    402 {
    403   return FALSE;
    404 }
    405 
    406 void
    408 _bfd_xcoff_swap_sym_in (bfd *abfd, void * ext1, void * in1)
    409 {
    410   SYMENT *ext = (SYMENT *)ext1;
    411   struct internal_syment * in = (struct internal_syment *)in1;
    412 
    413   if (ext->e.e_name[0] != 0)
    414     {
    415       memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
    416     }
    417   else
    418     {
    419       in->_n._n_n._n_zeroes = 0;
    420       in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
    421     }
    422 
    423   in->n_value = H_GET_32 (abfd, ext->e_value);
    424   in->n_scnum = (short) H_GET_16 (abfd, ext->e_scnum);
    425   in->n_type = H_GET_16 (abfd, ext->e_type);
    426   in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
    427   in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
    428 }
    429 
    430 unsigned int
    431 _bfd_xcoff_swap_sym_out (bfd *abfd, void * inp, void * extp)
    432 {
    433   struct internal_syment *in = (struct internal_syment *)inp;
    434   SYMENT *ext =(SYMENT *)extp;
    435 
    436   if (in->_n._n_name[0] != 0)
    437     {
    438       memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
    439     }
    440   else
    441     {
    442       H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
    443       H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
    444     }
    445 
    446   H_PUT_32 (abfd, in->n_value, ext->e_value);
    447   H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
    448   H_PUT_16 (abfd, in->n_type, ext->e_type);
    449   H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
    450   H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
    451   return bfd_coff_symesz (abfd);
    452 }
    453 
    454 void
    455 _bfd_xcoff_swap_aux_in (bfd *abfd, void * ext1, int type, int in_class,
    456                         int indx, int numaux, void * in1)
    457 {
    458   AUXENT * ext = (AUXENT *)ext1;
    459   union internal_auxent *in = (union internal_auxent *)in1;
    460 
    461   switch (in_class)
    462     {
    463     case C_FILE:
    464       if (ext->x_file.x_n.x_fname[0] == 0)
    465 	{
    466 	  in->x_file.x_n.x_zeroes = 0;
    467 	  in->x_file.x_n.x_offset =
    468 	    H_GET_32 (abfd, ext->x_file.x_n.x_n.x_offset);
    469 	}
    470       else
    471 	{
    472 	  if (numaux > 1)
    473 	    {
    474 	      if (indx == 0)
    475 		memcpy (in->x_file.x_fname, ext->x_file.x_n.x_fname,
    476 			numaux * sizeof (AUXENT));
    477 	    }
    478 	  else
    479 	    {
    480 	      memcpy (in->x_file.x_fname, ext->x_file.x_n.x_fname, FILNMLEN);
    481 	    }
    482 	}
    483       goto end;
    484 
    485       /* RS/6000 "csect" auxents */
    486     case C_EXT:
    487     case C_AIX_WEAKEXT:
    488     case C_HIDEXT:
    489       if (indx + 1 == numaux)
    490 	{
    491 	  in->x_csect.x_scnlen.l = H_GET_32 (abfd, ext->x_csect.x_scnlen);
    492 	  in->x_csect.x_parmhash = H_GET_32 (abfd, ext->x_csect.x_parmhash);
    493 	  in->x_csect.x_snhash   = H_GET_16 (abfd, ext->x_csect.x_snhash);
    494 	  /* We don't have to hack bitfields in x_smtyp because it's
    495 	     defined by shifts-and-ands, which are equivalent on all
    496 	     byte orders.  */
    497 	  in->x_csect.x_smtyp    = H_GET_8 (abfd, ext->x_csect.x_smtyp);
    498 	  in->x_csect.x_smclas   = H_GET_8 (abfd, ext->x_csect.x_smclas);
    499 	  in->x_csect.x_stab     = H_GET_32 (abfd, ext->x_csect.x_stab);
    500 	  in->x_csect.x_snstab   = H_GET_16 (abfd, ext->x_csect.x_snstab);
    501 	  goto end;
    502 	}
    503       break;
    504 
    505     case C_STAT:
    506     case C_LEAFSTAT:
    507     case C_HIDDEN:
    508       if (type == T_NULL)
    509 	{
    510 	  in->x_scn.x_scnlen = H_GET_32 (abfd, ext->x_scn.x_scnlen);
    511 	  in->x_scn.x_nreloc = H_GET_16 (abfd, ext->x_scn.x_nreloc);
    512 	  in->x_scn.x_nlinno = H_GET_16 (abfd, ext->x_scn.x_nlinno);
    513 	  /* PE defines some extra fields; we zero them out for
    514 	     safety.  */
    515 	  in->x_scn.x_checksum = 0;
    516 	  in->x_scn.x_associated = 0;
    517 	  in->x_scn.x_comdat = 0;
    518 
    519 	  goto end;
    520 	}
    521       break;
    522     }
    523 
    524   in->x_sym.x_tagndx.l = H_GET_32 (abfd, ext->x_sym.x_tagndx);
    525   in->x_sym.x_tvndx = H_GET_16 (abfd, ext->x_sym.x_tvndx);
    526 
    527   if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
    528       || ISTAG (in_class))
    529     {
    530       in->x_sym.x_fcnary.x_fcn.x_lnnoptr =
    531 	H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
    532       in->x_sym.x_fcnary.x_fcn.x_endndx.l =
    533 	H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_endndx);
    534     }
    535   else
    536     {
    537       in->x_sym.x_fcnary.x_ary.x_dimen[0] =
    538 	H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
    539       in->x_sym.x_fcnary.x_ary.x_dimen[1] =
    540 	H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
    541       in->x_sym.x_fcnary.x_ary.x_dimen[2] =
    542 	H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
    543       in->x_sym.x_fcnary.x_ary.x_dimen[3] =
    544 	H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
    545     }
    546 
    547   if (ISFCN (type))
    548     {
    549       in->x_sym.x_misc.x_fsize = H_GET_32 (abfd, ext->x_sym.x_misc.x_fsize);
    550     }
    551   else
    552     {
    553       in->x_sym.x_misc.x_lnsz.x_lnno =
    554 	H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_lnno);
    555       in->x_sym.x_misc.x_lnsz.x_size =
    556 	H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_size);
    557     }
    558 
    559  end: ;
    560   /* The semicolon is because MSVC doesn't like labels at
    561      end of block.  */
    562 }
    563 
    564 unsigned int
    565 _bfd_xcoff_swap_aux_out (bfd *abfd, void * inp, int type, int in_class,
    566                          int indx ATTRIBUTE_UNUSED,
    567                          int numaux ATTRIBUTE_UNUSED,
    568                          void * extp)
    569 {
    570   union internal_auxent *in = (union internal_auxent *)inp;
    571   AUXENT *ext = (AUXENT *)extp;
    572 
    573   memset (ext, 0, bfd_coff_auxesz (abfd));
    574   switch (in_class)
    575     {
    576     case C_FILE:
    577       if (in->x_file.x_fname[0] == 0)
    578 	{
    579 	  H_PUT_32 (abfd, 0, ext->x_file.x_n.x_n.x_zeroes);
    580 	  H_PUT_32 (abfd, in->x_file.x_n.x_offset,
    581                     ext->x_file.x_n.x_n.x_offset);
    582 	}
    583       else
    584 	{
    585 	  memcpy (ext->x_file.x_n.x_fname, in->x_file.x_fname, FILNMLEN);
    586 	}
    587       goto end;
    588 
    589       /* RS/6000 "csect" auxents */
    590     case C_EXT:
    591     case C_AIX_WEAKEXT:
    592     case C_HIDEXT:
    593       if (indx + 1 == numaux)
    594 	{
    595 	  H_PUT_32 (abfd, in->x_csect.x_scnlen.l, ext->x_csect.x_scnlen);
    596 	  H_PUT_32 (abfd, in->x_csect.x_parmhash, ext->x_csect.x_parmhash);
    597 	  H_PUT_16 (abfd, in->x_csect.x_snhash, ext->x_csect.x_snhash);
    598 	  /* We don't have to hack bitfields in x_smtyp because it's
    599 	     defined by shifts-and-ands, which are equivalent on all
    600 	     byte orders.  */
    601 	  H_PUT_8 (abfd, in->x_csect.x_smtyp, ext->x_csect.x_smtyp);
    602 	  H_PUT_8 (abfd, in->x_csect.x_smclas, ext->x_csect.x_smclas);
    603 	  H_PUT_32 (abfd, in->x_csect.x_stab, ext->x_csect.x_stab);
    604 	  H_PUT_16 (abfd, in->x_csect.x_snstab, ext->x_csect.x_snstab);
    605 	  goto end;
    606 	}
    607       break;
    608 
    609     case C_STAT:
    610     case C_LEAFSTAT:
    611     case C_HIDDEN:
    612       if (type == T_NULL)
    613 	{
    614 	  H_PUT_32 (abfd, in->x_scn.x_scnlen, ext->x_scn.x_scnlen);
    615 	  H_PUT_16 (abfd, in->x_scn.x_nreloc, ext->x_scn.x_nreloc);
    616 	  H_PUT_16 (abfd, in->x_scn.x_nlinno, ext->x_scn.x_nlinno);
    617 	  goto end;
    618 	}
    619       break;
    620     }
    621 
    622   H_PUT_32 (abfd, in->x_sym.x_tagndx.l, ext->x_sym.x_tagndx);
    623   H_PUT_16 (abfd, in->x_sym.x_tvndx, ext->x_sym.x_tvndx);
    624 
    625   if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
    626       || ISTAG (in_class))
    627     {
    628       H_PUT_32 (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr,
    629 		ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
    630       H_PUT_32 (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l,
    631 		ext->x_sym.x_fcnary.x_fcn.x_endndx);
    632     }
    633   else
    634     {
    635       H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
    636 		ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
    637       H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
    638 		ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
    639       H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
    640 		ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
    641       H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
    642 		ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
    643     }
    644 
    645   if (ISFCN (type))
    646     H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize, ext->x_sym.x_misc.x_fsize);
    647   else
    648     {
    649       H_PUT_16 (abfd, in->x_sym.x_misc.x_lnsz.x_lnno,
    650 		ext->x_sym.x_misc.x_lnsz.x_lnno);
    651       H_PUT_16 (abfd, in->x_sym.x_misc.x_lnsz.x_size,
    652 		ext->x_sym.x_misc.x_lnsz.x_size);
    653     }
    654 
    655 end:
    656   return bfd_coff_auxesz (abfd);
    657 }
    658 
    659 
    660 
    661 /* The XCOFF reloc table.  Actually, XCOFF relocations specify the
    663    bitsize and whether they are signed or not, along with a
    664    conventional type.  This table is for the types, which are used for
    665    different algorithms for putting in the reloc.  Many of these
    666    relocs need special_function entries, which I have not written.  */
    667 
    668 
    669 reloc_howto_type xcoff_howto_table[] =
    670 {
    671   /* 0x00: Standard 32 bit relocation.  */
    672   HOWTO (R_POS,			/* type */
    673 	 0,			/* rightshift */
    674 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    675 	 32,			/* bitsize */
    676 	 FALSE,			/* pc_relative */
    677 	 0,			/* bitpos */
    678 	 complain_overflow_bitfield, /* complain_on_overflow */
    679 	 0,			/* special_function */
    680 	 "R_POS",		/* name */
    681 	 TRUE,			/* partial_inplace */
    682 	 0xffffffff,		/* src_mask */
    683 	 0xffffffff,		/* dst_mask */
    684 	 FALSE),		/* pcrel_offset */
    685 
    686   /* 0x01: 32 bit relocation, but store negative value.  */
    687   HOWTO (R_NEG,			/* type */
    688 	 0,			/* rightshift */
    689 	 -2,			/* size (0 = byte, 1 = short, 2 = long) */
    690 	 32,			/* bitsize */
    691 	 FALSE,			/* pc_relative */
    692 	 0,			/* bitpos */
    693 	 complain_overflow_bitfield, /* complain_on_overflow */
    694 	 0,			/* special_function */
    695 	 "R_NEG",		/* name */
    696 	 TRUE,			/* partial_inplace */
    697 	 0xffffffff,		/* src_mask */
    698 	 0xffffffff,		/* dst_mask */
    699 	 FALSE),		/* pcrel_offset */
    700 
    701   /* 0x02: 32 bit PC relative relocation.  */
    702   HOWTO (R_REL,			/* type */
    703 	 0,			/* rightshift */
    704 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    705 	 32,			/* bitsize */
    706 	 TRUE,			/* pc_relative */
    707 	 0,			/* bitpos */
    708 	 complain_overflow_signed, /* complain_on_overflow */
    709 	 0,			/* special_function */
    710 	 "R_REL",		/* name */
    711 	 TRUE,			/* partial_inplace */
    712 	 0xffffffff,		/* src_mask */
    713 	 0xffffffff,		/* dst_mask */
    714 	 FALSE),		/* pcrel_offset */
    715 
    716   /* 0x03: 16 bit TOC relative relocation.  */
    717   HOWTO (R_TOC,			/* type */
    718 	 0,			/* rightshift */
    719 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    720 	 16,			/* bitsize */
    721 	 FALSE,			/* pc_relative */
    722 	 0,			/* bitpos */
    723 	 complain_overflow_bitfield, /* complain_on_overflow */
    724 	 0,			/* special_function */
    725 	 "R_TOC",		/* name */
    726 	 TRUE,			/* partial_inplace */
    727 	 0xffff,		/* src_mask */
    728 	 0xffff,		/* dst_mask */
    729 	 FALSE),		/* pcrel_offset */
    730 
    731   /* 0x04: I don't really know what this is.  */
    732   HOWTO (R_RTB,			/* type */
    733 	 1,			/* rightshift */
    734 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    735 	 32,			/* bitsize */
    736 	 FALSE,			/* pc_relative */
    737 	 0,			/* bitpos */
    738 	 complain_overflow_bitfield, /* complain_on_overflow */
    739 	 0,			/* special_function */
    740 	 "R_RTB",		/* name */
    741 	 TRUE,			/* partial_inplace */
    742 	 0xffffffff,		/* src_mask */
    743 	 0xffffffff,		/* dst_mask */
    744 	 FALSE),		/* pcrel_offset */
    745 
    746   /* 0x05: External TOC relative symbol.  */
    747   HOWTO (R_GL,			/* type */
    748 	 0,			/* rightshift */
    749 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    750 	 16,			/* bitsize */
    751 	 FALSE,			/* pc_relative */
    752 	 0,			/* bitpos */
    753 	 complain_overflow_bitfield, /* complain_on_overflow */
    754 	 0,			/* special_function */
    755 	 "R_GL",		/* name */
    756 	 TRUE,			/* partial_inplace */
    757 	 0xffff,		/* src_mask */
    758 	 0xffff,		/* dst_mask */
    759 	 FALSE),		/* pcrel_offset */
    760 
    761   /* 0x06: Local TOC relative symbol.	 */
    762   HOWTO (R_TCL,			/* type */
    763 	 0,			/* rightshift */
    764 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    765 	 16,			/* bitsize */
    766 	 FALSE,			/* pc_relative */
    767 	 0,			/* bitpos */
    768 	 complain_overflow_bitfield, /* complain_on_overflow */
    769 	 0,			/* special_function */
    770 	 "R_TCL",		/* name */
    771 	 TRUE,			/* partial_inplace */
    772 	 0xffff,		/* src_mask */
    773 	 0xffff,		/* dst_mask */
    774 	 FALSE),		/* pcrel_offset */
    775 
    776   EMPTY_HOWTO (7),
    777 
    778   /* 0x08: Non modifiable absolute branch.  */
    779   HOWTO (R_BA,			/* type */
    780 	 0,			/* rightshift */
    781 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    782 	 26,			/* bitsize */
    783 	 FALSE,			/* pc_relative */
    784 	 0,			/* bitpos */
    785 	 complain_overflow_bitfield, /* complain_on_overflow */
    786 	 0,			/* special_function */
    787 	 "R_BA_26",		/* name */
    788 	 TRUE,			/* partial_inplace */
    789 	 0x03fffffc,		/* src_mask */
    790 	 0x03fffffc,		/* dst_mask */
    791 	 FALSE),		/* pcrel_offset */
    792 
    793   EMPTY_HOWTO (9),
    794 
    795   /* 0x0a: Non modifiable relative branch.  */
    796   HOWTO (R_BR,			/* type */
    797 	 0,			/* rightshift */
    798 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    799 	 26,			/* bitsize */
    800 	 TRUE,			/* pc_relative */
    801 	 0,			/* bitpos */
    802 	 complain_overflow_signed, /* complain_on_overflow */
    803 	 0,			/* special_function */
    804 	 "R_BR",		/* name */
    805 	 TRUE,			/* partial_inplace */
    806 	 0x03fffffc,		/* src_mask */
    807 	 0x03fffffc,		/* dst_mask */
    808 	 FALSE),		/* pcrel_offset */
    809 
    810   EMPTY_HOWTO (0xb),
    811 
    812   /* 0x0c: Indirect load.  */
    813   HOWTO (R_RL,			/* type */
    814 	 0,			/* rightshift */
    815 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    816 	 16,			/* bitsize */
    817 	 FALSE,			/* pc_relative */
    818 	 0,			/* bitpos */
    819 	 complain_overflow_bitfield, /* complain_on_overflow */
    820 	 0,			/* special_function */
    821 	 "R_RL",		/* name */
    822 	 TRUE,			/* partial_inplace */
    823 	 0xffff,		/* src_mask */
    824 	 0xffff,		/* dst_mask */
    825 	 FALSE),		/* pcrel_offset */
    826 
    827   /* 0x0d: Load address.  */
    828   HOWTO (R_RLA,			/* type */
    829 	 0,			/* rightshift */
    830 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    831 	 16,			/* bitsize */
    832 	 FALSE,			/* pc_relative */
    833 	 0,			/* bitpos */
    834 	 complain_overflow_bitfield, /* complain_on_overflow */
    835 	 0,			/* special_function */
    836 	 "R_RLA",		/* name */
    837 	 TRUE,			/* partial_inplace */
    838 	 0xffff,		/* src_mask */
    839 	 0xffff,		/* dst_mask */
    840 	 FALSE),		/* pcrel_offset */
    841 
    842   EMPTY_HOWTO (0xe),
    843 
    844   /* 0x0f: Non-relocating reference.  Bitsize is 1 so that r_rsize is 0.  */
    845   HOWTO (R_REF,			/* type */
    846 	 0,			/* rightshift */
    847 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    848 	 1,			/* bitsize */
    849 	 FALSE,			/* pc_relative */
    850 	 0,			/* bitpos */
    851 	 complain_overflow_dont, /* complain_on_overflow */
    852 	 0,			/* special_function */
    853 	 "R_REF",		/* name */
    854 	 FALSE,			/* partial_inplace */
    855 	 0,			/* src_mask */
    856 	 0,			/* dst_mask */
    857 	 FALSE),		/* pcrel_offset */
    858 
    859   EMPTY_HOWTO (0x10),
    860   EMPTY_HOWTO (0x11),
    861 
    862   /* 0x12: TOC relative indirect load.  */
    863   HOWTO (R_TRL,			/* type */
    864 	 0,			/* rightshift */
    865 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    866 	 16,			/* bitsize */
    867 	 FALSE,			/* pc_relative */
    868 	 0,			/* bitpos */
    869 	 complain_overflow_bitfield, /* complain_on_overflow */
    870 	 0,			/* special_function */
    871 	 "R_TRL",		/* name */
    872 	 TRUE,			/* partial_inplace */
    873 	 0xffff,		/* src_mask */
    874 	 0xffff,		/* dst_mask */
    875 	 FALSE),		/* pcrel_offset */
    876 
    877   /* 0x13: TOC relative load address.  */
    878   HOWTO (R_TRLA,		/* type */
    879 	 0,			/* rightshift */
    880 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    881 	 16,			/* bitsize */
    882 	 FALSE,			/* pc_relative */
    883 	 0,			/* bitpos */
    884 	 complain_overflow_bitfield, /* complain_on_overflow */
    885 	 0,			/* special_function */
    886 	 "R_TRLA",		/* name */
    887 	 TRUE,			/* partial_inplace */
    888 	 0xffff,		/* src_mask */
    889 	 0xffff,		/* dst_mask */
    890 	 FALSE),		/* pcrel_offset */
    891 
    892   /* 0x14: Modifiable relative branch.  */
    893   HOWTO (R_RRTBI,		 /* type */
    894 	 1,			/* rightshift */
    895 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    896 	 32,			/* bitsize */
    897 	 FALSE,			/* pc_relative */
    898 	 0,			/* bitpos */
    899 	 complain_overflow_bitfield, /* complain_on_overflow */
    900 	 0,			/* special_function */
    901 	 "R_RRTBI",		/* name */
    902 	 TRUE,			/* partial_inplace */
    903 	 0xffffffff,		/* src_mask */
    904 	 0xffffffff,		/* dst_mask */
    905 	 FALSE),		/* pcrel_offset */
    906 
    907   /* 0x15: Modifiable absolute branch.  */
    908   HOWTO (R_RRTBA,		 /* type */
    909 	 1,			/* rightshift */
    910 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    911 	 32,			/* bitsize */
    912 	 FALSE,			/* pc_relative */
    913 	 0,			/* bitpos */
    914 	 complain_overflow_bitfield, /* complain_on_overflow */
    915 	 0,			/* special_function */
    916 	 "R_RRTBA",		/* name */
    917 	 TRUE,			/* partial_inplace */
    918 	 0xffffffff,		/* src_mask */
    919 	 0xffffffff,		/* dst_mask */
    920 	 FALSE),		/* pcrel_offset */
    921 
    922   /* 0x16: Modifiable call absolute indirect.  */
    923   HOWTO (R_CAI,			/* type */
    924 	 0,			/* rightshift */
    925 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    926 	 16,			/* bitsize */
    927 	 FALSE,			/* pc_relative */
    928 	 0,			/* bitpos */
    929 	 complain_overflow_bitfield, /* complain_on_overflow */
    930 	 0,			/* special_function */
    931 	 "R_CAI",		/* name */
    932 	 TRUE,			/* partial_inplace */
    933 	 0xffff,		/* src_mask */
    934 	 0xffff,		/* dst_mask */
    935 	 FALSE),		/* pcrel_offset */
    936 
    937   /* 0x17: Modifiable call relative.  */
    938   HOWTO (R_CREL,		/* type */
    939 	 0,			/* rightshift */
    940 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    941 	 16,			/* bitsize */
    942 	 FALSE,			/* pc_relative */
    943 	 0,			/* bitpos */
    944 	 complain_overflow_bitfield, /* complain_on_overflow */
    945 	 0,			/* special_function */
    946 	 "R_CREL",		/* name */
    947 	 TRUE,			/* partial_inplace */
    948 	 0xffff,		/* src_mask */
    949 	 0xffff,		/* dst_mask */
    950 	 FALSE),		/* pcrel_offset */
    951 
    952   /* 0x18: Modifiable branch absolute.  */
    953   HOWTO (R_RBA,			/* type */
    954 	 0,			/* rightshift */
    955 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    956 	 26,			/* bitsize */
    957 	 FALSE,			/* pc_relative */
    958 	 0,			/* bitpos */
    959 	 complain_overflow_bitfield, /* complain_on_overflow */
    960 	 0,			/* special_function */
    961 	 "R_RBA",		/* name */
    962 	 TRUE,			/* partial_inplace */
    963 	 0x03fffffc,		/* src_mask */
    964 	 0x03fffffc,		/* dst_mask */
    965 	 FALSE),		/* pcrel_offset */
    966 
    967   /* 0x19: Modifiable branch absolute.  */
    968   HOWTO (R_RBAC,		/* type */
    969 	 0,			/* rightshift */
    970 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    971 	 32,			/* bitsize */
    972 	 FALSE,			/* pc_relative */
    973 	 0,			/* bitpos */
    974 	 complain_overflow_bitfield, /* complain_on_overflow */
    975 	 0,			/* special_function */
    976 	 "R_RBAC",		/* name */
    977 	 TRUE,			/* partial_inplace */
    978 	 0xffffffff,		/* src_mask */
    979 	 0xffffffff,		/* dst_mask */
    980 	 FALSE),		/* pcrel_offset */
    981 
    982   /* 0x1a: Modifiable branch relative.  */
    983   HOWTO (R_RBR,			/* type */
    984 	 0,			/* rightshift */
    985 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    986 	 26,			/* bitsize */
    987 	 FALSE,			/* pc_relative */
    988 	 0,			/* bitpos */
    989 	 complain_overflow_signed, /* complain_on_overflow */
    990 	 0,			/* special_function */
    991 	 "R_RBR_26",		/* name */
    992 	 TRUE,			/* partial_inplace */
    993 	 0x03fffffc,		/* src_mask */
    994 	 0x03fffffc,		/* dst_mask */
    995 	 FALSE),		/* pcrel_offset */
    996 
    997   /* 0x1b: Modifiable branch absolute.  */
    998   HOWTO (R_RBRC,		/* type */
    999 	 0,			/* rightshift */
   1000 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1001 	 16,			/* bitsize */
   1002 	 FALSE,			/* pc_relative */
   1003 	 0,			/* bitpos */
   1004 	 complain_overflow_bitfield, /* complain_on_overflow */
   1005 	 0,			/* special_function */
   1006 	 "R_RBRC",		/* name */
   1007 	 TRUE,			/* partial_inplace */
   1008 	 0xffff,		/* src_mask */
   1009 	 0xffff,		/* dst_mask */
   1010 	 FALSE),		/* pcrel_offset */
   1011 
   1012   /* 0x1c: 16 bit Non modifiable absolute branch.  */
   1013   HOWTO (R_BA,			/* type */
   1014 	 0,			/* rightshift */
   1015 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1016 	 16,			/* bitsize */
   1017 	 FALSE,			/* pc_relative */
   1018 	 0,			/* bitpos */
   1019 	 complain_overflow_bitfield, /* complain_on_overflow */
   1020 	 0,			/* special_function */
   1021 	 "R_BA_16",		/* name */
   1022 	 TRUE,			/* partial_inplace */
   1023 	 0xfffc,		/* src_mask */
   1024 	 0xfffc,		/* dst_mask */
   1025 	 FALSE),		/* pcrel_offset */
   1026 
   1027   /* 0x1d: Modifiable branch relative.  */
   1028   HOWTO (R_RBR,			/* type */
   1029 	 0,			/* rightshift */
   1030 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1031 	 16,			/* bitsize */
   1032 	 TRUE,			/* pc_relative */
   1033 	 0,			/* bitpos */
   1034 	 complain_overflow_signed, /* complain_on_overflow */
   1035 	 0,			/* special_function */
   1036 	 "R_RBR_16",		/* name */
   1037 	 TRUE,			/* partial_inplace */
   1038 	 0xfffc,		/* src_mask */
   1039 	 0xfffc,		/* dst_mask */
   1040 	 FALSE),		/* pcrel_offset */
   1041 
   1042   /* 0x1e: Modifiable branch relative.  */
   1043   HOWTO (R_RBA,			/* type */
   1044 	 0,			/* rightshift */
   1045 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1046 	 16,			/* bitsize */
   1047 	 FALSE,			/* pc_relative */
   1048 	 0,			/* bitpos */
   1049 	 complain_overflow_signed, /* complain_on_overflow */
   1050 	 0,			/* special_function */
   1051 	 "R_RBA_16",		/* name */
   1052 	 TRUE,			/* partial_inplace */
   1053 	 0xffff,		/* src_mask */
   1054 	 0xffff,		/* dst_mask */
   1055 	 FALSE),		/* pcrel_offset */
   1056 };
   1057 
   1058 void
   1059 xcoff_rtype2howto (arelent *relent, struct internal_reloc *internal)
   1060 {
   1061   if (internal->r_type > R_RBRC)
   1062     abort ();
   1063 
   1064   /* Default howto layout works most of the time */
   1065   relent->howto = &xcoff_howto_table[internal->r_type];
   1066 
   1067   /* Special case some 16 bit reloc */
   1068   if (15 == (internal->r_size & 0x1f))
   1069     {
   1070       if (R_BA == internal->r_type)
   1071 	relent->howto = &xcoff_howto_table[0x1c];
   1072       else if (R_RBR == internal->r_type)
   1073 	relent->howto = &xcoff_howto_table[0x1d];
   1074       else if (R_RBA == internal->r_type)
   1075 	relent->howto = &xcoff_howto_table[0x1e];
   1076     }
   1077 
   1078   /* The r_size field of an XCOFF reloc encodes the bitsize of the
   1079      relocation, as well as indicating whether it is signed or not.
   1080      Doublecheck that the relocation information gathered from the
   1081      type matches this information.  The bitsize is not significant
   1082      for R_REF relocs.  */
   1083   if (relent->howto->dst_mask != 0
   1084       && (relent->howto->bitsize
   1085 	  != ((unsigned int) internal->r_size & 0x1f) + 1))
   1086     abort ();
   1087 }
   1088 
   1089 reloc_howto_type *
   1090 _bfd_xcoff_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   1091                               bfd_reloc_code_real_type code)
   1092 {
   1093   switch (code)
   1094     {
   1095     case BFD_RELOC_PPC_B26:
   1096       return &xcoff_howto_table[0xa];
   1097     case BFD_RELOC_PPC_BA16:
   1098       return &xcoff_howto_table[0x1c];
   1099     case BFD_RELOC_PPC_BA26:
   1100       return &xcoff_howto_table[8];
   1101     case BFD_RELOC_PPC_TOC16:
   1102       return &xcoff_howto_table[3];
   1103     case BFD_RELOC_16:
   1104       /* Note that this relocation is only internally used by gas.  */
   1105       return &xcoff_howto_table[0xc];
   1106     case BFD_RELOC_PPC_B16:
   1107       return &xcoff_howto_table[0x1d];
   1108     case BFD_RELOC_32:
   1109     case BFD_RELOC_CTOR:
   1110       return &xcoff_howto_table[0];
   1111     case BFD_RELOC_NONE:
   1112       return &xcoff_howto_table[0xf];
   1113     default:
   1114       return NULL;
   1115     }
   1116 }
   1117 
   1118 static reloc_howto_type *
   1119 _bfd_xcoff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   1120 			      const char *r_name)
   1121 {
   1122   unsigned int i;
   1123 
   1124   for (i = 0;
   1125        i < sizeof (xcoff_howto_table) / sizeof (xcoff_howto_table[0]);
   1126        i++)
   1127     if (xcoff_howto_table[i].name != NULL
   1128 	&& strcasecmp (xcoff_howto_table[i].name, r_name) == 0)
   1129       return &xcoff_howto_table[i];
   1130 
   1131   return NULL;
   1132 }
   1133 
   1134 /* XCOFF archive support.  The original version of this code was by
   1136    Damon A. Permezel.  It was enhanced to permit cross support, and
   1137    writing archive files, by Ian Lance Taylor, Cygnus Support.
   1138 
   1139    XCOFF uses its own archive format.  Everything is hooked together
   1140    with file offset links, so it is possible to rapidly update an
   1141    archive in place.  Of course, we don't do that.  An XCOFF archive
   1142    has a real file header, not just an ARMAG string.  The structure of
   1143    the file header and of each archive header appear below.
   1144 
   1145    An XCOFF archive also has a member table, which is a list of
   1146    elements in the archive (you can get that by looking through the
   1147    linked list, but you have to read a lot more of the file).  The
   1148    member table has a normal archive header with an empty name.  It is
   1149    normally (and perhaps must be) the second to last entry in the
   1150    archive.  The member table data is almost printable ASCII.  It
   1151    starts with a 12 character decimal string which is the number of
   1152    entries in the table.  For each entry it has a 12 character decimal
   1153    string which is the offset in the archive of that member.  These
   1154    entries are followed by a series of null terminated strings which
   1155    are the member names for each entry.
   1156 
   1157    Finally, an XCOFF archive has a global symbol table, which is what
   1158    we call the armap.  The global symbol table has a normal archive
   1159    header with an empty name.  It is normally (and perhaps must be)
   1160    the last entry in the archive.  The contents start with a four byte
   1161    binary number which is the number of entries.  This is followed by
   1162    a that many four byte binary numbers; each is the file offset of an
   1163    entry in the archive.  These numbers are followed by a series of
   1164    null terminated strings, which are symbol names.
   1165 
   1166    AIX 4.3 introduced a new archive format which can handle larger
   1167    files and also 32- and 64-bit objects in the same archive.  The
   1168    things said above remain true except that there is now more than
   1169    one global symbol table.  The one is used to index 32-bit objects,
   1170    the other for 64-bit objects.
   1171 
   1172    The new archives (recognizable by the new ARMAG string) has larger
   1173    field lengths so that we cannot really share any code.  Also we have
   1174    to take care that we are not generating the new form of archives
   1175    on AIX 4.2 or earlier systems.  */
   1176 
   1177 /* XCOFF archives use this as a magic string.  Note that both strings
   1178    have the same length.  */
   1179 
   1180 /* Set the magic for archive.  */
   1181 
   1182 bfd_boolean
   1183 bfd_xcoff_ar_archive_set_magic (bfd *abfd ATTRIBUTE_UNUSED,
   1184                                 char *magic ATTRIBUTE_UNUSED)
   1185 {
   1186   /* Not supported yet.  */
   1187   return FALSE;
   1188  /* bfd_xcoff_archive_set_magic (abfd, magic); */
   1189 }
   1190 
   1191 /* Read in the armap of an XCOFF archive.  */
   1192 
   1193 bfd_boolean
   1194 _bfd_xcoff_slurp_armap (bfd *abfd)
   1195 {
   1196   file_ptr off;
   1197   size_t namlen;
   1198   bfd_size_type sz;
   1199   bfd_byte *contents, *cend;
   1200   bfd_vma c, i;
   1201   carsym *arsym;
   1202   bfd_byte *p;
   1203 
   1204   if (xcoff_ardata (abfd) == NULL)
   1205     {
   1206       bfd_has_map (abfd) = FALSE;
   1207       return TRUE;
   1208     }
   1209 
   1210   if (! xcoff_big_format_p (abfd))
   1211     {
   1212       /* This is for the old format.  */
   1213       struct xcoff_ar_hdr hdr;
   1214 
   1215       off = strtol (xcoff_ardata (abfd)->symoff, (char **) NULL, 10);
   1216       if (off == 0)
   1217 	{
   1218 	  bfd_has_map (abfd) = FALSE;
   1219 	  return TRUE;
   1220 	}
   1221 
   1222       if (bfd_seek (abfd, off, SEEK_SET) != 0)
   1223 	return FALSE;
   1224 
   1225       /* The symbol table starts with a normal archive header.  */
   1226       if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
   1227 	  != SIZEOF_AR_HDR)
   1228 	return FALSE;
   1229 
   1230       /* Skip the name (normally empty).  */
   1231       namlen = strtol (hdr.namlen, (char **) NULL, 10);
   1232       off = ((namlen + 1) & ~ (size_t) 1) + SXCOFFARFMAG;
   1233       if (bfd_seek (abfd, off, SEEK_CUR) != 0)
   1234 	return FALSE;
   1235 
   1236       sz = strtol (hdr.size, (char **) NULL, 10);
   1237 
   1238       /* Read in the entire symbol table.  */
   1239       contents = (bfd_byte *) bfd_alloc (abfd, sz);
   1240       if (contents == NULL)
   1241 	return FALSE;
   1242       if (bfd_bread (contents, sz, abfd) != sz)
   1243 	return FALSE;
   1244 
   1245       /* The symbol table starts with a four byte count.  */
   1246       c = H_GET_32 (abfd, contents);
   1247 
   1248       if (c * 4 >= sz)
   1249 	{
   1250 	  bfd_set_error (bfd_error_bad_value);
   1251 	  return FALSE;
   1252 	}
   1253 
   1254       bfd_ardata (abfd)->symdefs =
   1255 	((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
   1256       if (bfd_ardata (abfd)->symdefs == NULL)
   1257 	return FALSE;
   1258 
   1259       /* After the count comes a list of four byte file offsets.  */
   1260       for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4;
   1261 	   i < c;
   1262 	   ++i, ++arsym, p += 4)
   1263 	arsym->file_offset = H_GET_32 (abfd, p);
   1264     }
   1265   else
   1266     {
   1267       /* This is for the new format.  */
   1268       struct xcoff_ar_hdr_big hdr;
   1269 
   1270       off = strtol (xcoff_ardata_big (abfd)->symoff, (char **) NULL, 10);
   1271       if (off == 0)
   1272 	{
   1273 	  bfd_has_map (abfd) = FALSE;
   1274 	  return TRUE;
   1275 	}
   1276 
   1277       if (bfd_seek (abfd, off, SEEK_SET) != 0)
   1278 	return FALSE;
   1279 
   1280       /* The symbol table starts with a normal archive header.  */
   1281       if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR_BIG, abfd)
   1282 	  != SIZEOF_AR_HDR_BIG)
   1283 	return FALSE;
   1284 
   1285       /* Skip the name (normally empty).  */
   1286       namlen = strtol (hdr.namlen, (char **) NULL, 10);
   1287       off = ((namlen + 1) & ~ (size_t) 1) + SXCOFFARFMAG;
   1288       if (bfd_seek (abfd, off, SEEK_CUR) != 0)
   1289 	return FALSE;
   1290 
   1291       /* XXX This actually has to be a call to strtoll (at least on 32-bit
   1292 	 machines) since the field width is 20 and there numbers with more
   1293 	 than 32 bits can be represented.  */
   1294       sz = strtol (hdr.size, (char **) NULL, 10);
   1295 
   1296       /* Read in the entire symbol table.  */
   1297       contents = (bfd_byte *) bfd_alloc (abfd, sz);
   1298       if (contents == NULL)
   1299 	return FALSE;
   1300       if (bfd_bread (contents, sz, abfd) != sz)
   1301 	return FALSE;
   1302 
   1303       /* The symbol table starts with an eight byte count.  */
   1304       c = H_GET_64 (abfd, contents);
   1305 
   1306       if (c * 8 >= sz)
   1307 	{
   1308 	  bfd_set_error (bfd_error_bad_value);
   1309 	  return FALSE;
   1310 	}
   1311 
   1312       bfd_ardata (abfd)->symdefs =
   1313 	((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
   1314       if (bfd_ardata (abfd)->symdefs == NULL)
   1315 	return FALSE;
   1316 
   1317       /* After the count comes a list of eight byte file offsets.  */
   1318       for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 8;
   1319 	   i < c;
   1320 	   ++i, ++arsym, p += 8)
   1321 	arsym->file_offset = H_GET_64 (abfd, p);
   1322     }
   1323 
   1324   /* After the file offsets come null terminated symbol names.  */
   1325   cend = contents + sz;
   1326   for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
   1327        i < c;
   1328        ++i, ++arsym, p += strlen ((char *) p) + 1)
   1329     {
   1330       if (p >= cend)
   1331 	{
   1332 	  bfd_set_error (bfd_error_bad_value);
   1333 	  return FALSE;
   1334 	}
   1335       arsym->name = (char *) p;
   1336     }
   1337 
   1338   bfd_ardata (abfd)->symdef_count = c;
   1339   bfd_has_map (abfd) = TRUE;
   1340 
   1341   return TRUE;
   1342 }
   1343 
   1344 /* See if this is an XCOFF archive.  */
   1345 
   1346 const bfd_target *
   1347 _bfd_xcoff_archive_p (bfd *abfd)
   1348 {
   1349   struct artdata *tdata_hold;
   1350   char magic[SXCOFFARMAG];
   1351   bfd_size_type amt = SXCOFFARMAG;
   1352 
   1353   if (bfd_bread (magic, amt, abfd) != amt)
   1354     {
   1355       if (bfd_get_error () != bfd_error_system_call)
   1356 	bfd_set_error (bfd_error_wrong_format);
   1357       return NULL;
   1358     }
   1359 
   1360   if (strncmp (magic, XCOFFARMAG, SXCOFFARMAG) != 0
   1361       && strncmp (magic, XCOFFARMAGBIG, SXCOFFARMAG) != 0)
   1362     {
   1363       bfd_set_error (bfd_error_wrong_format);
   1364       return NULL;
   1365     }
   1366 
   1367   tdata_hold = bfd_ardata (abfd);
   1368 
   1369   amt = sizeof (struct artdata);
   1370   bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt);
   1371   if (bfd_ardata (abfd) == (struct artdata *) NULL)
   1372     goto error_ret_restore;
   1373 
   1374   /* Cleared by bfd_zalloc above.
   1375      bfd_ardata (abfd)->cache = NULL;
   1376      bfd_ardata (abfd)->archive_head = NULL;
   1377      bfd_ardata (abfd)->symdefs = NULL;
   1378      bfd_ardata (abfd)->extended_names = NULL;
   1379      bfd_ardata (abfd)->extended_names_size = 0;  */
   1380 
   1381   /* Now handle the two formats.  */
   1382   if (magic[1] != 'b')
   1383     {
   1384       /* This is the old format.  */
   1385       struct xcoff_ar_file_hdr hdr;
   1386 
   1387       /* Copy over the magic string.  */
   1388       memcpy (hdr.magic, magic, SXCOFFARMAG);
   1389 
   1390       /* Now read the rest of the file header.  */
   1391       amt = SIZEOF_AR_FILE_HDR - SXCOFFARMAG;
   1392       if (bfd_bread (&hdr.memoff, amt, abfd) != amt)
   1393 	{
   1394 	  if (bfd_get_error () != bfd_error_system_call)
   1395 	    bfd_set_error (bfd_error_wrong_format);
   1396 	  goto error_ret;
   1397 	}
   1398 
   1399       bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff,
   1400 						      (char **) NULL, 10);
   1401 
   1402       amt = SIZEOF_AR_FILE_HDR;
   1403       bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
   1404       if (bfd_ardata (abfd)->tdata == NULL)
   1405 	goto error_ret;
   1406 
   1407       memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR);
   1408     }
   1409   else
   1410     {
   1411       /* This is the new format.  */
   1412       struct xcoff_ar_file_hdr_big hdr;
   1413 
   1414       /* Copy over the magic string.  */
   1415       memcpy (hdr.magic, magic, SXCOFFARMAG);
   1416 
   1417       /* Now read the rest of the file header.  */
   1418       amt = SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG;
   1419       if (bfd_bread (&hdr.memoff, amt, abfd) != amt)
   1420 	{
   1421 	  if (bfd_get_error () != bfd_error_system_call)
   1422 	    bfd_set_error (bfd_error_wrong_format);
   1423 	  goto error_ret;
   1424 	}
   1425 
   1426       bfd_ardata (abfd)->first_file_filepos = bfd_scan_vma (hdr.firstmemoff,
   1427 							    (const char **) 0,
   1428 							    10);
   1429 
   1430       amt = SIZEOF_AR_FILE_HDR_BIG;
   1431       bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
   1432       if (bfd_ardata (abfd)->tdata == NULL)
   1433 	goto error_ret;
   1434 
   1435       memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR_BIG);
   1436     }
   1437 
   1438   if (! _bfd_xcoff_slurp_armap (abfd))
   1439     {
   1440     error_ret:
   1441       bfd_release (abfd, bfd_ardata (abfd));
   1442     error_ret_restore:
   1443       bfd_ardata (abfd) = tdata_hold;
   1444       return NULL;
   1445     }
   1446 
   1447   return abfd->xvec;
   1448 }
   1449 
   1450 /* Read the archive header in an XCOFF archive.  */
   1451 
   1452 void *
   1453 _bfd_xcoff_read_ar_hdr (bfd *abfd)
   1454 {
   1455   bfd_size_type namlen;
   1456   struct areltdata *ret;
   1457   bfd_size_type amt = sizeof (struct areltdata);
   1458 
   1459   ret = (struct areltdata *) bfd_zmalloc (amt);
   1460   if (ret == NULL)
   1461     return NULL;
   1462 
   1463   if (! xcoff_big_format_p (abfd))
   1464     {
   1465       struct xcoff_ar_hdr hdr;
   1466       struct xcoff_ar_hdr *hdrp;
   1467 
   1468       if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
   1469 	  != SIZEOF_AR_HDR)
   1470 	{
   1471 	  free (ret);
   1472 	  return NULL;
   1473 	}
   1474 
   1475       namlen = strtol (hdr.namlen, (char **) NULL, 10);
   1476       amt = SIZEOF_AR_HDR + namlen + 1;
   1477       hdrp = (struct xcoff_ar_hdr *) bfd_alloc (abfd, amt);
   1478       if (hdrp == NULL)
   1479 	{
   1480 	  free (ret);
   1481 	  return NULL;
   1482 	}
   1483       memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
   1484       if (bfd_bread ((char *) hdrp + SIZEOF_AR_HDR, namlen, abfd) != namlen)
   1485 	{
   1486 	  free (ret);
   1487 	  return NULL;
   1488 	}
   1489       ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
   1490 
   1491       ret->arch_header = (char *) hdrp;
   1492       ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
   1493       ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
   1494     }
   1495   else
   1496     {
   1497       struct xcoff_ar_hdr_big hdr;
   1498       struct xcoff_ar_hdr_big *hdrp;
   1499 
   1500       if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR_BIG, abfd)
   1501 	  != SIZEOF_AR_HDR_BIG)
   1502 	{
   1503 	  free (ret);
   1504 	  return NULL;
   1505 	}
   1506 
   1507       namlen = strtol (hdr.namlen, (char **) NULL, 10);
   1508       amt = SIZEOF_AR_HDR_BIG + namlen + 1;
   1509       hdrp = (struct xcoff_ar_hdr_big *) bfd_alloc (abfd, amt);
   1510       if (hdrp == NULL)
   1511 	{
   1512 	  free (ret);
   1513 	  return NULL;
   1514 	}
   1515       memcpy (hdrp, &hdr, SIZEOF_AR_HDR_BIG);
   1516       if (bfd_bread ((char *) hdrp + SIZEOF_AR_HDR_BIG, namlen, abfd) != namlen)
   1517 	{
   1518 	  free (ret);
   1519 	  return NULL;
   1520 	}
   1521       ((char *) hdrp)[SIZEOF_AR_HDR_BIG + namlen] = '\0';
   1522 
   1523       ret->arch_header = (char *) hdrp;
   1524       /* XXX This actually has to be a call to strtoll (at least on 32-bit
   1525 	 machines) since the field width is 20 and there numbers with more
   1526 	 than 32 bits can be represented.  */
   1527       ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
   1528       ret->filename = (char *) hdrp + SIZEOF_AR_HDR_BIG;
   1529     }
   1530 
   1531   /* Skip over the XCOFFARFMAG at the end of the file name.  */
   1532   if (bfd_seek (abfd, (file_ptr) ((namlen & 1) + SXCOFFARFMAG), SEEK_CUR) != 0)
   1533     return NULL;
   1534 
   1535   return ret;
   1536 }
   1537 
   1538 /* Open the next element in an XCOFF archive.  */
   1539 
   1540 bfd *
   1541 _bfd_xcoff_openr_next_archived_file (bfd *archive, bfd *last_file)
   1542 {
   1543   file_ptr filestart;
   1544 
   1545   if (xcoff_ardata (archive) == NULL)
   1546     {
   1547       bfd_set_error (bfd_error_invalid_operation);
   1548       return NULL;
   1549     }
   1550 
   1551   if (! xcoff_big_format_p (archive))
   1552     {
   1553       if (last_file == NULL)
   1554 	filestart = bfd_ardata (archive)->first_file_filepos;
   1555       else
   1556 	filestart = strtol (arch_xhdr (last_file)->nextoff, (char **) NULL,
   1557 			    10);
   1558 
   1559       if (filestart == 0
   1560 	  || filestart == strtol (xcoff_ardata (archive)->memoff,
   1561 				  (char **) NULL, 10)
   1562 	  || filestart == strtol (xcoff_ardata (archive)->symoff,
   1563 				  (char **) NULL, 10))
   1564 	{
   1565 	  bfd_set_error (bfd_error_no_more_archived_files);
   1566 	  return NULL;
   1567 	}
   1568     }
   1569   else
   1570     {
   1571       if (last_file == NULL)
   1572 	filestart = bfd_ardata (archive)->first_file_filepos;
   1573       else
   1574 	/* XXX These actually have to be a calls to strtoll (at least
   1575 	   on 32-bit machines) since the fields's width is 20 and
   1576 	   there numbers with more than 32 bits can be represented.  */
   1577 	filestart = strtol (arch_xhdr_big (last_file)->nextoff, (char **) NULL,
   1578 			    10);
   1579 
   1580       /* XXX These actually have to be calls to strtoll (at least on 32-bit
   1581 	 machines) since the fields's width is 20 and there numbers with more
   1582 	 than 32 bits can be represented.  */
   1583       if (filestart == 0
   1584 	  || filestart == strtol (xcoff_ardata_big (archive)->memoff,
   1585 				  (char **) NULL, 10)
   1586 	  || filestart == strtol (xcoff_ardata_big (archive)->symoff,
   1587 				  (char **) NULL, 10))
   1588 	{
   1589 	  bfd_set_error (bfd_error_no_more_archived_files);
   1590 	  return NULL;
   1591 	}
   1592     }
   1593 
   1594   return _bfd_get_elt_at_filepos (archive, filestart);
   1595 }
   1596 
   1597 /* Stat an element in an XCOFF archive.  */
   1598 
   1599 int
   1600 _bfd_xcoff_stat_arch_elt (bfd *abfd, struct stat *s)
   1601 {
   1602   if (abfd->arelt_data == NULL)
   1603     {
   1604       bfd_set_error (bfd_error_invalid_operation);
   1605       return -1;
   1606     }
   1607 
   1608   if (! xcoff_big_format_p (abfd->my_archive))
   1609     {
   1610       struct xcoff_ar_hdr *hdrp = arch_xhdr (abfd);
   1611 
   1612       s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
   1613       s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
   1614       s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
   1615       s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
   1616       s->st_size = arch_eltdata (abfd)->parsed_size;
   1617     }
   1618   else
   1619     {
   1620       struct xcoff_ar_hdr_big *hdrp = arch_xhdr_big (abfd);
   1621 
   1622       s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
   1623       s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
   1624       s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
   1625       s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
   1626       s->st_size = arch_eltdata (abfd)->parsed_size;
   1627     }
   1628 
   1629   return 0;
   1630 }
   1631 
   1632 /* Normalize a file name for inclusion in an archive.  */
   1633 
   1634 static const char *
   1635 normalize_filename (bfd *abfd)
   1636 {
   1637   const char *file;
   1638   const char *filename;
   1639 
   1640   file = bfd_get_filename (abfd);
   1641   filename = strrchr (file, '/');
   1642   if (filename != NULL)
   1643     filename++;
   1644   else
   1645     filename = file;
   1646   return filename;
   1647 }
   1648 
   1649 /* Write out an XCOFF armap.  */
   1650 
   1651 static bfd_boolean
   1652 xcoff_write_armap_old (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
   1653                        struct orl *map, unsigned int orl_count, int stridx)
   1654 {
   1655   struct archive_iterator iterator;
   1656   struct xcoff_ar_hdr hdr;
   1657   char *p;
   1658   unsigned char buf[4];
   1659   unsigned int i;
   1660 
   1661   memset (&hdr, 0, sizeof hdr);
   1662   sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
   1663   sprintf (hdr.nextoff, "%d", 0);
   1664   memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, XCOFFARMAG_ELEMENT_SIZE);
   1665   sprintf (hdr.date, "%d", 0);
   1666   sprintf (hdr.uid, "%d", 0);
   1667   sprintf (hdr.gid, "%d", 0);
   1668   sprintf (hdr.mode, "%d", 0);
   1669   sprintf (hdr.namlen, "%d", 0);
   1670 
   1671   /* We need spaces, not null bytes, in the header.  */
   1672   for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
   1673     if (*p == '\0')
   1674       *p = ' ';
   1675 
   1676   if (bfd_bwrite (&hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
   1677       != SIZEOF_AR_HDR
   1678       || (bfd_bwrite (XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG, abfd)
   1679 	  != SXCOFFARFMAG))
   1680     return FALSE;
   1681 
   1682   H_PUT_32 (abfd, orl_count, buf);
   1683   if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
   1684     return FALSE;
   1685 
   1686   i = 0;
   1687   archive_iterator_begin (&iterator, abfd);
   1688   while (i < orl_count && archive_iterator_next (&iterator))
   1689     while (map[i].u.abfd == iterator.current.member)
   1690       {
   1691 	H_PUT_32 (abfd, iterator.current.offset, buf);
   1692 	if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
   1693 	  return FALSE;
   1694 	++i;
   1695       }
   1696 
   1697   for (i = 0; i < orl_count; i++)
   1698     {
   1699       const char *name;
   1700       size_t namlen;
   1701 
   1702       name = *map[i].name;
   1703       namlen = strlen (name);
   1704       if (bfd_bwrite (name, (bfd_size_type) (namlen + 1), abfd) != namlen + 1)
   1705 	return FALSE;
   1706     }
   1707 
   1708   if ((stridx & 1) != 0)
   1709     {
   1710       char b;
   1711 
   1712       b = '\0';
   1713       if (bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
   1714 	return FALSE;
   1715     }
   1716 
   1717   return TRUE;
   1718 }
   1719 
   1720 static char buff20[XCOFFARMAGBIG_ELEMENT_SIZE + 1];
   1721 #define FMT20  "%-20lld"
   1722 #define FMT12  "%-12d"
   1723 #define FMT12_OCTAL  "%-12o"
   1724 #define FMT4  "%-4d"
   1725 #define PRINT20(d, v) \
   1726   sprintf (buff20, FMT20, (long long)(v)), \
   1727   memcpy ((void *) (d), buff20, 20)
   1728 
   1729 #define PRINT12(d, v) \
   1730   sprintf (buff20, FMT12, (int)(v)), \
   1731   memcpy ((void *) (d), buff20, 12)
   1732 
   1733 #define PRINT12_OCTAL(d, v) \
   1734   sprintf (buff20, FMT12_OCTAL, (unsigned int)(v)), \
   1735   memcpy ((void *) (d), buff20, 12)
   1736 
   1737 #define PRINT4(d, v) \
   1738   sprintf (buff20, FMT4, (int)(v)), \
   1739   memcpy ((void *) (d), buff20, 4)
   1740 
   1741 #define READ20(d, v) \
   1742   buff20[20] = 0, \
   1743   memcpy (buff20, (d), 20), \
   1744   (v) = bfd_scan_vma (buff20, (const char **) NULL, 10)
   1745 
   1746 static bfd_boolean
   1747 do_pad (bfd *abfd, unsigned int number)
   1748 {
   1749   bfd_byte b = 0;
   1750 
   1751   /* Limit pad to <= 4096.  */
   1752   if (number > 4096)
   1753     return FALSE;
   1754 
   1755   while (number--)
   1756     if (bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
   1757       return FALSE;
   1758 
   1759   return TRUE;
   1760 }
   1761 
   1762 static bfd_boolean
   1763 do_copy (bfd *out_bfd, bfd *in_bfd)
   1764 {
   1765   bfd_size_type remaining;
   1766   bfd_byte buffer[DEFAULT_BUFFERSIZE];
   1767 
   1768   if (bfd_seek (in_bfd, (file_ptr) 0, SEEK_SET) != 0)
   1769     return FALSE;
   1770 
   1771   remaining = arelt_size (in_bfd);
   1772 
   1773   while (remaining >= DEFAULT_BUFFERSIZE)
   1774     {
   1775       if (bfd_bread (buffer, DEFAULT_BUFFERSIZE, in_bfd) != DEFAULT_BUFFERSIZE
   1776 	  || bfd_bwrite (buffer, DEFAULT_BUFFERSIZE, out_bfd) != DEFAULT_BUFFERSIZE)
   1777 	return FALSE;
   1778 
   1779       remaining -= DEFAULT_BUFFERSIZE;
   1780     }
   1781 
   1782   if (remaining)
   1783     {
   1784       if (bfd_bread (buffer, remaining, in_bfd) != remaining
   1785 	  || bfd_bwrite (buffer, remaining, out_bfd) != remaining)
   1786 	return FALSE;
   1787     }
   1788 
   1789   return TRUE;
   1790 }
   1791 
   1792 static bfd_boolean
   1793 xcoff_write_armap_big (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
   1794                        struct orl *map, unsigned int orl_count, int stridx)
   1795 {
   1796   struct archive_iterator iterator;
   1797   struct xcoff_ar_file_hdr_big *fhdr;
   1798   bfd_vma i, sym_32, sym_64, str_32, str_64;
   1799   const bfd_arch_info_type *arch_info;
   1800   bfd *current_bfd;
   1801   size_t string_length;
   1802   file_ptr nextoff, prevoff;
   1803 
   1804   /* First, we look through the symbols and work out which are
   1805      from 32-bit objects and which from 64-bit ones.  */
   1806   sym_32 = sym_64 = str_32 = str_64 = 0;
   1807 
   1808   i = 0;
   1809   for (current_bfd = abfd->archive_head;
   1810        current_bfd != NULL && i < orl_count;
   1811        current_bfd = current_bfd->archive_next)
   1812     {
   1813       arch_info = bfd_get_arch_info (current_bfd);
   1814       while (map[i].u.abfd == current_bfd)
   1815 	{
   1816 	  string_length = strlen (*map[i].name) + 1;
   1817 	  if (arch_info->bits_per_address == 64)
   1818 	    {
   1819 	      sym_64++;
   1820 	      str_64 += string_length;
   1821 	    }
   1822 	  else
   1823 	    {
   1824 	      sym_32++;
   1825 	      str_32 += string_length;
   1826 	    }
   1827 	  i++;
   1828 	}
   1829     }
   1830 
   1831   /* A quick sanity check... */
   1832   BFD_ASSERT (sym_64 + sym_32 == orl_count);
   1833   /* Explicit cast to int for compiler.  */
   1834   BFD_ASSERT ((int)(str_64 + str_32) == stridx);
   1835 
   1836   fhdr = xcoff_ardata_big (abfd);
   1837 
   1838   /* xcoff_write_archive_contents_big passes nextoff in symoff. */
   1839   READ20 (fhdr->memoff, prevoff);
   1840   READ20 (fhdr->symoff, nextoff);
   1841 
   1842   BFD_ASSERT (nextoff == bfd_tell (abfd));
   1843 
   1844   /* Write out the symbol table.
   1845      Layout :
   1846 
   1847      standard big archive header
   1848      0x0000		      ar_size	[0x14]
   1849      0x0014		      ar_nxtmem [0x14]
   1850      0x0028		      ar_prvmem [0x14]
   1851      0x003C		      ar_date	[0x0C]
   1852      0x0048		      ar_uid	[0x0C]
   1853      0x0054		      ar_gid	[0x0C]
   1854      0x0060		      ar_mod	[0x0C]
   1855      0x006C		      ar_namelen[0x04]
   1856      0x0070		      ar_fmag	[SXCOFFARFMAG]
   1857 
   1858      Symbol table
   1859      0x0072		      num_syms	[0x08], binary
   1860      0x0078		      offsets	[0x08 * num_syms], binary
   1861      0x0086 + 0x08 * num_syms names	[??]
   1862      ??			      pad to even bytes.
   1863   */
   1864 
   1865   if (sym_32)
   1866     {
   1867       struct xcoff_ar_hdr_big *hdr;
   1868       char *symbol_table;
   1869       char *st;
   1870 
   1871       bfd_vma symbol_table_size =
   1872 	SIZEOF_AR_HDR_BIG
   1873 	+ SXCOFFARFMAG
   1874 	+ 8
   1875 	+ 8 * sym_32
   1876 	+ str_32 + (str_32 & 1);
   1877 
   1878       symbol_table = bfd_zmalloc (symbol_table_size);
   1879       if (symbol_table == NULL)
   1880 	return FALSE;
   1881 
   1882       hdr = (struct xcoff_ar_hdr_big *) symbol_table;
   1883 
   1884       PRINT20 (hdr->size, 8 + 8 * sym_32 + str_32 + (str_32 & 1));
   1885 
   1886       if (sym_64)
   1887 	PRINT20 (hdr->nextoff, nextoff + symbol_table_size);
   1888       else
   1889 	PRINT20 (hdr->nextoff, 0);
   1890 
   1891       PRINT20 (hdr->prevoff, prevoff);
   1892       PRINT12 (hdr->date, 0);
   1893       PRINT12 (hdr->uid, 0);
   1894       PRINT12 (hdr->gid, 0);
   1895       PRINT12 (hdr->mode, 0);
   1896       PRINT4 (hdr->namlen, 0) ;
   1897 
   1898       st = symbol_table + SIZEOF_AR_HDR_BIG;
   1899       memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
   1900       st += SXCOFFARFMAG;
   1901 
   1902       bfd_h_put_64 (abfd, sym_32, st);
   1903       st += 8;
   1904 
   1905       /* loop over the 32 bit offsets */
   1906       i = 0;
   1907       archive_iterator_begin (&iterator, abfd);
   1908       while (i < orl_count && archive_iterator_next (&iterator))
   1909 	{
   1910 	  arch_info = bfd_get_arch_info (iterator.current.member);
   1911 	  while (map[i].u.abfd == iterator.current.member)
   1912 	    {
   1913 	      if (arch_info->bits_per_address == 32)
   1914 		{
   1915 		  bfd_h_put_64 (abfd, iterator.current.offset, st);
   1916 		  st += 8;
   1917 		}
   1918 	      i++;
   1919 	    }
   1920 	}
   1921 
   1922       /* loop over the 32 bit symbol names */
   1923       i = 0;
   1924       for (current_bfd = abfd->archive_head;
   1925 	   current_bfd != NULL && i < orl_count;
   1926 	   current_bfd = current_bfd->archive_next)
   1927 	{
   1928 	  arch_info = bfd_get_arch_info (current_bfd);
   1929 	  while (map[i].u.abfd == current_bfd)
   1930 	    {
   1931 	      if (arch_info->bits_per_address == 32)
   1932 		{
   1933 		  string_length = sprintf (st, "%s", *map[i].name);
   1934 		  st += string_length + 1;
   1935 		}
   1936 	      i++;
   1937 	    }
   1938 	}
   1939 
   1940       bfd_bwrite (symbol_table, symbol_table_size, abfd);
   1941 
   1942       free (symbol_table);
   1943 
   1944       prevoff = nextoff;
   1945       nextoff = nextoff + symbol_table_size;
   1946     }
   1947   else
   1948     PRINT20 (fhdr->symoff, 0);
   1949 
   1950   if (sym_64)
   1951     {
   1952       struct xcoff_ar_hdr_big *hdr;
   1953       char *symbol_table;
   1954       char *st;
   1955 
   1956       bfd_vma symbol_table_size =
   1957 	SIZEOF_AR_HDR_BIG
   1958 	+ SXCOFFARFMAG
   1959 	+ 8
   1960 	+ 8 * sym_64
   1961 	+ str_64 + (str_64 & 1);
   1962 
   1963       symbol_table = bfd_zmalloc (symbol_table_size);
   1964       if (symbol_table == NULL)
   1965 	return FALSE;
   1966 
   1967       hdr = (struct xcoff_ar_hdr_big *) symbol_table;
   1968 
   1969       PRINT20 (hdr->size, 8 + 8 * sym_64 + str_64 + (str_64 & 1));
   1970       PRINT20 (hdr->nextoff, 0);
   1971       PRINT20 (hdr->prevoff, prevoff);
   1972       PRINT12 (hdr->date, 0);
   1973       PRINT12 (hdr->uid, 0);
   1974       PRINT12 (hdr->gid, 0);
   1975       PRINT12 (hdr->mode, 0);
   1976       PRINT4 (hdr->namlen, 0);
   1977 
   1978       st = symbol_table + SIZEOF_AR_HDR_BIG;
   1979       memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
   1980       st += SXCOFFARFMAG;
   1981 
   1982       bfd_h_put_64 (abfd, sym_64, st);
   1983       st += 8;
   1984 
   1985       /* loop over the 64 bit offsets */
   1986       i = 0;
   1987       archive_iterator_begin (&iterator, abfd);
   1988       while (i < orl_count && archive_iterator_next (&iterator))
   1989 	{
   1990 	  arch_info = bfd_get_arch_info (iterator.current.member);
   1991 	  while (map[i].u.abfd == iterator.current.member)
   1992 	    {
   1993 	      if (arch_info->bits_per_address == 64)
   1994 		{
   1995 		  bfd_h_put_64 (abfd, iterator.current.offset, st);
   1996 		  st += 8;
   1997 		}
   1998 	      i++;
   1999 	    }
   2000 	}
   2001 
   2002       /* loop over the 64 bit symbol names */
   2003       i = 0;
   2004       for (current_bfd = abfd->archive_head;
   2005 	   current_bfd != NULL && i < orl_count;
   2006 	   current_bfd = current_bfd->archive_next)
   2007 	{
   2008 	  arch_info = bfd_get_arch_info (current_bfd);
   2009 	  while (map[i].u.abfd == current_bfd)
   2010 	    {
   2011 	      if (arch_info->bits_per_address == 64)
   2012 		{
   2013 		  string_length = sprintf (st, "%s", *map[i].name);
   2014 		  st += string_length + 1;
   2015 		}
   2016 	      i++;
   2017 	    }
   2018 	}
   2019 
   2020       bfd_bwrite (symbol_table, symbol_table_size, abfd);
   2021 
   2022       free (symbol_table);
   2023 
   2024       PRINT20 (fhdr->symoff64, nextoff);
   2025     }
   2026   else
   2027     PRINT20 (fhdr->symoff64, 0);
   2028 
   2029   return TRUE;
   2030 }
   2031 
   2032 bfd_boolean
   2033 _bfd_xcoff_write_armap (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
   2034                         struct orl *map, unsigned int orl_count, int stridx)
   2035 {
   2036   if (! xcoff_big_format_p (abfd))
   2037     return xcoff_write_armap_old (abfd, elength, map, orl_count, stridx);
   2038   else
   2039     return xcoff_write_armap_big (abfd, elength, map, orl_count, stridx);
   2040 }
   2041 
   2042 /* Write out an XCOFF archive.  We always write an entire archive,
   2043    rather than fussing with the freelist and so forth.  */
   2044 
   2045 static bfd_boolean
   2046 xcoff_write_archive_contents_old (bfd *abfd)
   2047 {
   2048   struct archive_iterator iterator;
   2049   struct xcoff_ar_file_hdr fhdr;
   2050   bfd_size_type count;
   2051   bfd_size_type total_namlen;
   2052   file_ptr *offsets;
   2053   bfd_boolean makemap;
   2054   bfd_boolean hasobjects;
   2055   file_ptr prevoff, nextoff;
   2056   bfd *sub;
   2057   size_t i;
   2058   struct xcoff_ar_hdr ahdr;
   2059   bfd_size_type size;
   2060   char *p;
   2061   char decbuf[XCOFFARMAG_ELEMENT_SIZE + 1];
   2062 
   2063   memset (&fhdr, 0, sizeof fhdr);
   2064   (void) strncpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);
   2065   sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR);
   2066   sprintf (fhdr.freeoff, "%d", 0);
   2067 
   2068   count = 0;
   2069   total_namlen = 0;
   2070   for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
   2071     {
   2072       ++count;
   2073       total_namlen += strlen (normalize_filename (sub)) + 1;
   2074       if (sub->arelt_data == NULL)
   2075 	{
   2076 	  sub->arelt_data = bfd_zmalloc (sizeof (struct areltdata));
   2077 	  if (sub->arelt_data == NULL)
   2078 	    return FALSE;
   2079 	}
   2080       if (arch_xhdr (sub) == NULL)
   2081 	{
   2082 	  struct xcoff_ar_hdr *ahdrp;
   2083 	  struct stat s;
   2084 
   2085 	  if (stat (bfd_get_filename (sub), &s) != 0)
   2086 	    {
   2087 	      bfd_set_error (bfd_error_system_call);
   2088 	      return FALSE;
   2089 	    }
   2090 
   2091 	  ahdrp = bfd_zalloc (sub, sizeof (*ahdrp));
   2092 	  if (ahdrp == NULL)
   2093 	    return FALSE;
   2094 
   2095 	  sprintf (ahdrp->size, "%ld", (long) s.st_size);
   2096 	  sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
   2097 	  sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
   2098 	  sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
   2099 	  sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
   2100 
   2101 	  arch_eltdata (sub)->arch_header = (char *) ahdrp;
   2102 	  arch_eltdata (sub)->parsed_size = s.st_size;
   2103 	}
   2104     }
   2105   offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
   2106   if (offsets == NULL)
   2107     return FALSE;
   2108 
   2109   if (bfd_seek (abfd, (file_ptr) SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
   2110     return FALSE;
   2111 
   2112   makemap = bfd_has_map (abfd);
   2113   hasobjects = FALSE;
   2114   prevoff = 0;
   2115   for (archive_iterator_begin (&iterator, abfd), i = 0;
   2116        archive_iterator_next (&iterator);
   2117        i++)
   2118     {
   2119       bfd_size_type namlen;
   2120       struct xcoff_ar_hdr *ahdrp;
   2121 
   2122       if (makemap && ! hasobjects)
   2123 	{
   2124 	  if (bfd_check_format (iterator.current.member, bfd_object))
   2125 	    hasobjects = TRUE;
   2126 	}
   2127 
   2128       ahdrp = arch_xhdr (iterator.current.member);
   2129       sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
   2130       sprintf (ahdrp->namlen, "%ld", (long) iterator.current.namlen);
   2131       sprintf (ahdrp->nextoff, "%ld", (long) iterator.next.offset);
   2132 
   2133       /* We need spaces, not null bytes, in the header.  */
   2134       for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
   2135 	if (*p == '\0')
   2136 	  *p = ' ';
   2137 
   2138       if (!do_pad (abfd, iterator.current.leading_padding))
   2139 	return FALSE;
   2140 
   2141       BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
   2142       namlen = iterator.current.padded_namlen;
   2143       if (bfd_bwrite (ahdrp, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
   2144 	  || bfd_bwrite (iterator.current.name, namlen, abfd) != namlen
   2145 	  || bfd_bwrite (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
   2146 	  || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
   2147 	  || !do_copy (abfd, iterator.current.member)
   2148 	  || !do_pad (abfd, iterator.current.trailing_padding))
   2149 	return FALSE;
   2150 
   2151       offsets[i] = iterator.current.offset;
   2152       prevoff = iterator.current.offset;
   2153     }
   2154 
   2155   sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
   2156 
   2157   /* Write out the member table.  */
   2158 
   2159   nextoff = iterator.next.offset;
   2160   BFD_ASSERT (nextoff == bfd_tell (abfd));
   2161   sprintf (fhdr.memoff, "%ld", (long) nextoff);
   2162 
   2163   memset (&ahdr, 0, sizeof ahdr);
   2164   sprintf (ahdr.size, "%ld", (long) (XCOFFARMAG_ELEMENT_SIZE
   2165 				     + count * XCOFFARMAG_ELEMENT_SIZE
   2166 				     + total_namlen));
   2167   sprintf (ahdr.prevoff, "%ld", (long) prevoff);
   2168   sprintf (ahdr.date, "%d", 0);
   2169   sprintf (ahdr.uid, "%d", 0);
   2170   sprintf (ahdr.gid, "%d", 0);
   2171   sprintf (ahdr.mode, "%d", 0);
   2172   sprintf (ahdr.namlen, "%d", 0);
   2173 
   2174   size = (SIZEOF_AR_HDR
   2175 	  + XCOFFARMAG_ELEMENT_SIZE
   2176 	  + count * XCOFFARMAG_ELEMENT_SIZE
   2177 	  + total_namlen
   2178 	  + SXCOFFARFMAG);
   2179 
   2180   prevoff = nextoff;
   2181   nextoff += size + (size & 1);
   2182 
   2183   if (makemap && hasobjects)
   2184     sprintf (ahdr.nextoff, "%ld", (long) nextoff);
   2185   else
   2186     sprintf (ahdr.nextoff, "%d", 0);
   2187 
   2188   /* We need spaces, not null bytes, in the header.  */
   2189   for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
   2190     if (*p == '\0')
   2191       *p = ' ';
   2192 
   2193   if ((bfd_bwrite (&ahdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
   2194        != SIZEOF_AR_HDR)
   2195       || (bfd_bwrite (XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG, abfd)
   2196 	  != SXCOFFARFMAG))
   2197     return FALSE;
   2198 
   2199   sprintf (decbuf, "%-12ld", (long) count);
   2200   if (bfd_bwrite (decbuf, (bfd_size_type) XCOFFARMAG_ELEMENT_SIZE, abfd)
   2201       != XCOFFARMAG_ELEMENT_SIZE)
   2202     return FALSE;
   2203   for (i = 0; i < (size_t) count; i++)
   2204     {
   2205       sprintf (decbuf, "%-12ld", (long) offsets[i]);
   2206       if (bfd_bwrite (decbuf, (bfd_size_type) XCOFFARMAG_ELEMENT_SIZE,
   2207 		      abfd) != XCOFFARMAG_ELEMENT_SIZE)
   2208 	return FALSE;
   2209     }
   2210   for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
   2211     {
   2212       const char *name;
   2213       bfd_size_type namlen;
   2214 
   2215       name = normalize_filename (sub);
   2216       namlen = strlen (name);
   2217       if (bfd_bwrite (name, namlen + 1, abfd) != namlen + 1)
   2218 	return FALSE;
   2219     }
   2220 
   2221   if (! do_pad (abfd, size & 1))
   2222     return FALSE;
   2223 
   2224   /* Write out the armap, if appropriate.  */
   2225   if (! makemap || ! hasobjects)
   2226     sprintf (fhdr.symoff, "%d", 0);
   2227   else
   2228     {
   2229       BFD_ASSERT (nextoff == bfd_tell (abfd));
   2230       sprintf (fhdr.symoff, "%ld", (long) nextoff);
   2231       bfd_ardata (abfd)->tdata = &fhdr;
   2232       if (! _bfd_compute_and_write_armap (abfd, 0))
   2233 	return FALSE;
   2234     }
   2235 
   2236   /* Write out the archive file header.  */
   2237 
   2238   /* We need spaces, not null bytes, in the header.  */
   2239   for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR; p++)
   2240     if (*p == '\0')
   2241       *p = ' ';
   2242 
   2243   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
   2244       || (bfd_bwrite (&fhdr, (bfd_size_type) SIZEOF_AR_FILE_HDR, abfd)
   2245 	  != SIZEOF_AR_FILE_HDR))
   2246     return FALSE;
   2247 
   2248   return TRUE;
   2249 }
   2250 
   2251 static bfd_boolean
   2252 xcoff_write_archive_contents_big (bfd *abfd)
   2253 {
   2254   struct xcoff_ar_file_hdr_big fhdr;
   2255   bfd_size_type count;
   2256   bfd_size_type total_namlen;
   2257   file_ptr *offsets;
   2258   bfd_boolean makemap;
   2259   bfd_boolean hasobjects;
   2260   file_ptr prevoff, nextoff;
   2261   bfd *current_bfd;
   2262   size_t i;
   2263   struct xcoff_ar_hdr_big *hdr;
   2264   bfd_size_type size;
   2265   char *member_table, *mt;
   2266   bfd_vma member_table_size;
   2267   struct archive_iterator iterator;
   2268 
   2269   memset (&fhdr, 0, SIZEOF_AR_FILE_HDR_BIG);
   2270   memcpy (fhdr.magic, XCOFFARMAGBIG, SXCOFFARMAG);
   2271 
   2272   if (bfd_seek (abfd, (file_ptr) SIZEOF_AR_FILE_HDR_BIG, SEEK_SET) != 0)
   2273     return FALSE;
   2274 
   2275   /* Calculate count and total_namlen.  */
   2276   makemap = bfd_has_map (abfd);
   2277   hasobjects = FALSE;
   2278   for (current_bfd = abfd->archive_head, count = 0, total_namlen = 0;
   2279        current_bfd != NULL;
   2280        current_bfd = current_bfd->archive_next, count++)
   2281     {
   2282       total_namlen += strlen (normalize_filename (current_bfd)) + 1;
   2283 
   2284       if (makemap
   2285 	  && ! hasobjects
   2286 	  && bfd_check_format (current_bfd, bfd_object))
   2287 	hasobjects = TRUE;
   2288 
   2289       if (current_bfd->arelt_data == NULL)
   2290 	{
   2291 	  size = sizeof (struct areltdata);
   2292 	  current_bfd->arelt_data = bfd_zmalloc (size);
   2293 	  if (current_bfd->arelt_data == NULL)
   2294 	    return FALSE;
   2295 	}
   2296 
   2297       if (arch_xhdr_big (current_bfd) == NULL)
   2298 	{
   2299 	  struct xcoff_ar_hdr_big *ahdrp;
   2300 	  struct stat s;
   2301 
   2302 	  /* XXX This should actually be a call to stat64 (at least on
   2303 	     32-bit machines).
   2304 	     XXX This call will fail if the original object is not found.  */
   2305 	  if (stat (bfd_get_filename (current_bfd), &s) != 0)
   2306 	    {
   2307 	      bfd_set_error (bfd_error_system_call);
   2308 	      return FALSE;
   2309 	    }
   2310 
   2311 	  ahdrp = bfd_zalloc (current_bfd, sizeof (*ahdrp));
   2312 	  if (ahdrp == NULL)
   2313 	    return FALSE;
   2314 
   2315 	  PRINT20 (ahdrp->size, s.st_size);
   2316 	  PRINT12 (ahdrp->date, s.st_mtime);
   2317 	  PRINT12 (ahdrp->uid,  s.st_uid);
   2318 	  PRINT12 (ahdrp->gid,  s.st_gid);
   2319 	  PRINT12_OCTAL (ahdrp->mode, s.st_mode);
   2320 
   2321 	  arch_eltdata (current_bfd)->arch_header = (char *) ahdrp;
   2322 	  arch_eltdata (current_bfd)->parsed_size = s.st_size;
   2323 	}
   2324     }
   2325 
   2326   offsets = NULL;
   2327   if (count)
   2328     {
   2329       offsets = (file_ptr *) bfd_malloc (count * sizeof (file_ptr));
   2330       if (offsets == NULL)
   2331 	return FALSE;
   2332     }
   2333 
   2334   prevoff = 0;
   2335   for (archive_iterator_begin (&iterator, abfd), i = 0;
   2336        archive_iterator_next (&iterator);
   2337        i++)
   2338     {
   2339       bfd_size_type namlen;
   2340       struct xcoff_ar_hdr_big *ahdrp;
   2341 
   2342       ahdrp = arch_xhdr_big (iterator.current.member);
   2343       PRINT20 (ahdrp->prevoff, prevoff);
   2344       PRINT4 (ahdrp->namlen, iterator.current.namlen);
   2345       PRINT20 (ahdrp->nextoff, iterator.next.offset);
   2346 
   2347       if (!do_pad (abfd, iterator.current.leading_padding))
   2348 	{
   2349 	  free (offsets);
   2350 	  return FALSE;
   2351 	}
   2352 
   2353       BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
   2354       namlen = iterator.current.padded_namlen;
   2355       if (bfd_bwrite (ahdrp, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG
   2356 	  || bfd_bwrite (iterator.current.name, namlen, abfd) != namlen
   2357 	  || bfd_bwrite (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
   2358 	  || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
   2359 	  || !do_copy (abfd, iterator.current.member)
   2360 	  || !do_pad (abfd, iterator.current.trailing_padding))
   2361 	{
   2362 	  free (offsets);
   2363 	  return FALSE;
   2364 	}
   2365 
   2366       offsets[i] = iterator.current.offset;
   2367       prevoff = iterator.current.offset;
   2368     }
   2369 
   2370   if (count)
   2371     {
   2372       PRINT20 (fhdr.firstmemoff, offsets[0]);
   2373       PRINT20 (fhdr.lastmemoff, prevoff);
   2374     }
   2375 
   2376   /* Write out the member table.
   2377      Layout :
   2378 
   2379      standard big archive header
   2380      0x0000		      ar_size	[0x14]
   2381      0x0014		      ar_nxtmem [0x14]
   2382      0x0028		      ar_prvmem [0x14]
   2383      0x003C		      ar_date	[0x0C]
   2384      0x0048		      ar_uid	[0x0C]
   2385      0x0054		      ar_gid	[0x0C]
   2386      0x0060		      ar_mod	[0x0C]
   2387      0x006C		      ar_namelen[0x04]
   2388      0x0070		      ar_fmag	[0x02]
   2389 
   2390      Member table
   2391      0x0072		      count	[0x14]
   2392      0x0086		      offsets	[0x14 * counts]
   2393      0x0086 + 0x14 * counts   names	[??]
   2394      ??			      pad to even bytes.
   2395    */
   2396 
   2397   nextoff = iterator.next.offset;
   2398   BFD_ASSERT (nextoff == bfd_tell (abfd));
   2399 
   2400   member_table_size = (SIZEOF_AR_HDR_BIG
   2401 		       + SXCOFFARFMAG
   2402 		       + XCOFFARMAGBIG_ELEMENT_SIZE
   2403 		       + count * XCOFFARMAGBIG_ELEMENT_SIZE
   2404 		       + total_namlen);
   2405 
   2406   member_table_size += member_table_size & 1;
   2407   member_table = bfd_zmalloc (member_table_size);
   2408   if (member_table == NULL)
   2409     {
   2410       free (offsets);
   2411       return FALSE;
   2412     }
   2413 
   2414   hdr = (struct xcoff_ar_hdr_big *) member_table;
   2415 
   2416   PRINT20 (hdr->size, (XCOFFARMAGBIG_ELEMENT_SIZE
   2417 		       + count * XCOFFARMAGBIG_ELEMENT_SIZE
   2418 		       + total_namlen + (total_namlen & 1)));
   2419   if (makemap && hasobjects)
   2420     PRINT20 (hdr->nextoff, nextoff + member_table_size);
   2421   else
   2422     PRINT20 (hdr->nextoff, 0);
   2423   PRINT20 (hdr->prevoff, prevoff);
   2424   PRINT12 (hdr->date, 0);
   2425   PRINT12 (hdr->uid, 0);
   2426   PRINT12 (hdr->gid, 0);
   2427   PRINT12 (hdr->mode, 0);
   2428   PRINT4 (hdr->namlen, 0);
   2429 
   2430   mt = member_table + SIZEOF_AR_HDR_BIG;
   2431   memcpy (mt, XCOFFARFMAG, SXCOFFARFMAG);
   2432   mt += SXCOFFARFMAG;
   2433 
   2434   PRINT20 (mt, count);
   2435   mt += XCOFFARMAGBIG_ELEMENT_SIZE;
   2436   for (i = 0; i < (size_t) count; i++)
   2437     {
   2438       PRINT20 (mt, offsets[i]);
   2439       mt += XCOFFARMAGBIG_ELEMENT_SIZE;
   2440     }
   2441 
   2442   if (count)
   2443     {
   2444       free (offsets);
   2445       offsets = NULL;
   2446     }
   2447 
   2448   for (current_bfd = abfd->archive_head;
   2449        current_bfd != NULL;
   2450        current_bfd = current_bfd->archive_next)
   2451     {
   2452       const char *name;
   2453       size_t namlen;
   2454 
   2455       name = normalize_filename (current_bfd);
   2456       namlen = sprintf (mt, "%s", name);
   2457       mt += namlen + 1;
   2458     }
   2459 
   2460   if (bfd_bwrite (member_table, member_table_size, abfd) != member_table_size)
   2461     return FALSE;
   2462 
   2463   free (member_table);
   2464 
   2465   PRINT20 (fhdr.memoff, nextoff);
   2466 
   2467   prevoff = nextoff;
   2468   nextoff += member_table_size;
   2469 
   2470   /* Write out the armap, if appropriate.  */
   2471 
   2472   if (! makemap || ! hasobjects)
   2473     PRINT20 (fhdr.symoff, 0);
   2474   else
   2475     {
   2476       BFD_ASSERT (nextoff == bfd_tell (abfd));
   2477 
   2478       /* Save nextoff in fhdr.symoff so the armap routine can use it.  */
   2479       PRINT20 (fhdr.symoff, nextoff);
   2480 
   2481       bfd_ardata (abfd)->tdata = &fhdr;
   2482       if (! _bfd_compute_and_write_armap (abfd, 0))
   2483 	return FALSE;
   2484     }
   2485 
   2486   /* Write out the archive file header.  */
   2487 
   2488   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
   2489       || (bfd_bwrite (&fhdr, (bfd_size_type) SIZEOF_AR_FILE_HDR_BIG,
   2490 		      abfd) != SIZEOF_AR_FILE_HDR_BIG))
   2491     return FALSE;
   2492 
   2493   return TRUE;
   2494 }
   2495 
   2496 bfd_boolean
   2497 _bfd_xcoff_write_archive_contents (bfd *abfd)
   2498 {
   2499   if (! xcoff_big_format_p (abfd))
   2500     return xcoff_write_archive_contents_old (abfd);
   2501   else
   2502     return xcoff_write_archive_contents_big (abfd);
   2503 }
   2504 
   2505 /* We can't use the usual coff_sizeof_headers routine, because AIX
   2507    always uses an a.out header.  */
   2508 
   2509 int
   2510 _bfd_xcoff_sizeof_headers (bfd *abfd,
   2511 			   struct bfd_link_info *info ATTRIBUTE_UNUSED)
   2512 {
   2513   int size;
   2514 
   2515   size = FILHSZ;
   2516   if (xcoff_data (abfd)->full_aouthdr)
   2517     size += AOUTSZ;
   2518   else
   2519     size += SMALL_AOUTSZ;
   2520   size += abfd->section_count * SCNHSZ;
   2521 
   2522   if (info->strip != strip_all)
   2523     {
   2524       /* There can be additional sections just for dealing with overflow in
   2525 	 reloc and lineno counts. But the numbers of relocs and lineno aren't
   2526 	 known when bfd_sizeof_headers is called, so we compute them by
   2527 	 summing the numbers from input sections.  */
   2528       struct nbr_reloc_lineno
   2529       {
   2530 	unsigned int reloc_count;
   2531 	unsigned int lineno_count;
   2532       };
   2533       struct nbr_reloc_lineno *n_rl;
   2534       bfd *sub;
   2535       unsigned int max_index;
   2536       asection *s;
   2537 
   2538       /* Although the number of sections is known, the maximum value of
   2539 	 section->index isn't (because some sections may have been removed).
   2540 	 Don't try to renumber sections, just compute the upper bound.  */
   2541       max_index = 0;
   2542       for (s = abfd->sections; s != NULL; s = s->next)
   2543 	if (s->index > max_index)
   2544 	  max_index = s->index;
   2545 
   2546       /* Allocate the per section counters. It could be possible to use a
   2547 	 preallocated array as the number of sections is limited on XCOFF,
   2548 	 but this creates a maintainance issue.  */
   2549       n_rl = bfd_zmalloc ((max_index + 1) * sizeof (*n_rl));
   2550       if (n_rl == NULL)
   2551 	return -1;
   2552 
   2553       /* Sum.  */
   2554       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   2555 	for (s = sub->sections; s != NULL; s = s->next)
   2556 	  {
   2557 	    struct nbr_reloc_lineno *e = &n_rl[s->output_section->index];
   2558 	    e->reloc_count += s->reloc_count;
   2559 	    e->lineno_count += s->lineno_count;
   2560 	  }
   2561 
   2562       /* Add the size of a section for each section with an overflow.  */
   2563       for (s = abfd->sections; s != NULL; s = s->next)
   2564 	{
   2565 	  struct nbr_reloc_lineno *e = &n_rl[s->index];
   2566 
   2567 	  if (e->reloc_count >= 0xffff
   2568 	      || (e->lineno_count >= 0xffff && info->strip != strip_debugger))
   2569 	    size += SCNHSZ;
   2570 	}
   2571 
   2572       free (n_rl);
   2573     }
   2574 
   2575   return size;
   2576 }
   2577 
   2578 /* Routines to swap information in the XCOFF .loader section.  If we
   2580    ever need to write an XCOFF loader, this stuff will need to be
   2581    moved to another file shared by the linker (which XCOFF calls the
   2582    ``binder'') and the loader.  */
   2583 
   2584 /* Swap in the ldhdr structure.  */
   2585 
   2586 static void
   2587 xcoff_swap_ldhdr_in (bfd *abfd, const void * s, struct internal_ldhdr *dst)
   2588 {
   2589   const struct external_ldhdr *src = (const struct external_ldhdr *) s;
   2590 
   2591   dst->l_version = bfd_get_32 (abfd, src->l_version);
   2592   dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
   2593   dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
   2594   dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
   2595   dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
   2596   dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
   2597   dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
   2598   dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
   2599 }
   2600 
   2601 /* Swap out the ldhdr structure.  */
   2602 
   2603 static void
   2604 xcoff_swap_ldhdr_out (bfd *abfd, const struct internal_ldhdr *src, void * d)
   2605 {
   2606   struct external_ldhdr *dst = (struct external_ldhdr *) d;
   2607 
   2608   bfd_put_32 (abfd, (bfd_vma) src->l_version, dst->l_version);
   2609   bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
   2610   bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
   2611   bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
   2612   bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
   2613   bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
   2614   bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
   2615   bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
   2616 }
   2617 
   2618 /* Swap in the ldsym structure.  */
   2619 
   2620 static void
   2621 xcoff_swap_ldsym_in (bfd *abfd, const void * s, struct internal_ldsym *dst)
   2622 {
   2623   const struct external_ldsym *src = (const struct external_ldsym *) s;
   2624 
   2625   if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0) {
   2626     memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
   2627   } else {
   2628     dst->_l._l_l._l_zeroes = 0;
   2629     dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
   2630   }
   2631   dst->l_value = bfd_get_32 (abfd, src->l_value);
   2632   dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
   2633   dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
   2634   dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
   2635   dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
   2636   dst->l_parm = bfd_get_32 (abfd, src->l_parm);
   2637 }
   2638 
   2639 /* Swap out the ldsym structure.  */
   2640 
   2641 static void
   2642 xcoff_swap_ldsym_out (bfd *abfd, const struct internal_ldsym *src, void * d)
   2643 {
   2644   struct external_ldsym *dst = (struct external_ldsym *) d;
   2645 
   2646   if (src->_l._l_l._l_zeroes != 0)
   2647     memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
   2648   else
   2649     {
   2650       bfd_put_32 (abfd, (bfd_vma) 0, dst->_l._l_l._l_zeroes);
   2651       bfd_put_32 (abfd, (bfd_vma) src->_l._l_l._l_offset,
   2652 		  dst->_l._l_l._l_offset);
   2653     }
   2654   bfd_put_32 (abfd, src->l_value, dst->l_value);
   2655   bfd_put_16 (abfd, (bfd_vma) src->l_scnum, dst->l_scnum);
   2656   bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
   2657   bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
   2658   bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
   2659   bfd_put_32 (abfd, src->l_parm, dst->l_parm);
   2660 }
   2661 
   2662 static void
   2663 xcoff_swap_reloc_in (bfd *abfd, void * s, void * d)
   2664 {
   2665   struct external_reloc *src = (struct external_reloc *) s;
   2666   struct internal_reloc *dst = (struct internal_reloc *) d;
   2667 
   2668   memset (dst, 0, sizeof (struct internal_reloc));
   2669 
   2670   dst->r_vaddr = bfd_get_32 (abfd, src->r_vaddr);
   2671   dst->r_symndx = bfd_get_32 (abfd, src->r_symndx);
   2672   dst->r_size = bfd_get_8 (abfd, src->r_size);
   2673   dst->r_type = bfd_get_8 (abfd, src->r_type);
   2674 }
   2675 
   2676 static unsigned int
   2677 xcoff_swap_reloc_out (bfd *abfd, void * s, void * d)
   2678 {
   2679   struct internal_reloc *src = (struct internal_reloc *) s;
   2680   struct external_reloc *dst = (struct external_reloc *) d;
   2681 
   2682   bfd_put_32 (abfd, src->r_vaddr, dst->r_vaddr);
   2683   bfd_put_32 (abfd, src->r_symndx, dst->r_symndx);
   2684   bfd_put_8 (abfd, src->r_type, dst->r_type);
   2685   bfd_put_8 (abfd, src->r_size, dst->r_size);
   2686 
   2687   return bfd_coff_relsz (abfd);
   2688 }
   2689 
   2690 /* Swap in the ldrel structure.  */
   2691 
   2692 static void
   2693 xcoff_swap_ldrel_in (bfd *abfd, const void * s, struct internal_ldrel *dst)
   2694 {
   2695   const struct external_ldrel *src = (const struct external_ldrel *) s;
   2696 
   2697   dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
   2698   dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
   2699   dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
   2700   dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
   2701 }
   2702 
   2703 /* Swap out the ldrel structure.  */
   2704 
   2705 static void
   2706 xcoff_swap_ldrel_out (bfd *abfd, const struct internal_ldrel *src, void * d)
   2707 {
   2708   struct external_ldrel *dst = (struct external_ldrel *) d;
   2709 
   2710   bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
   2711   bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
   2712   bfd_put_16 (abfd, (bfd_vma) src->l_rtype, dst->l_rtype);
   2713   bfd_put_16 (abfd, (bfd_vma) src->l_rsecnm, dst->l_rsecnm);
   2714 }
   2715 
   2716 
   2718 bfd_boolean
   2719 xcoff_reloc_type_noop (bfd *input_bfd ATTRIBUTE_UNUSED,
   2720                        asection *input_section ATTRIBUTE_UNUSED,
   2721                        bfd *output_bfd ATTRIBUTE_UNUSED,
   2722                        struct internal_reloc *rel ATTRIBUTE_UNUSED,
   2723                        struct internal_syment *sym ATTRIBUTE_UNUSED,
   2724                        struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
   2725                        bfd_vma val ATTRIBUTE_UNUSED,
   2726                        bfd_vma addend ATTRIBUTE_UNUSED,
   2727                        bfd_vma *relocation ATTRIBUTE_UNUSED,
   2728                        bfd_byte *contents ATTRIBUTE_UNUSED)
   2729 {
   2730   return TRUE;
   2731 }
   2732 
   2733 bfd_boolean
   2734 xcoff_reloc_type_fail (bfd *input_bfd,
   2735                        asection *input_section ATTRIBUTE_UNUSED,
   2736                        bfd *output_bfd ATTRIBUTE_UNUSED,
   2737                        struct internal_reloc *rel,
   2738                        struct internal_syment *sym ATTRIBUTE_UNUSED,
   2739                        struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
   2740                        bfd_vma val ATTRIBUTE_UNUSED,
   2741                        bfd_vma addend ATTRIBUTE_UNUSED,
   2742                        bfd_vma *relocation ATTRIBUTE_UNUSED,
   2743                        bfd_byte *contents ATTRIBUTE_UNUSED)
   2744 {
   2745   (*_bfd_error_handler)
   2746     (_("%s: unsupported relocation type 0x%02x"),
   2747      bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
   2748   bfd_set_error (bfd_error_bad_value);
   2749   return FALSE;
   2750 }
   2751 
   2752 bfd_boolean
   2753 xcoff_reloc_type_pos (bfd *input_bfd ATTRIBUTE_UNUSED,
   2754                       asection *input_section ATTRIBUTE_UNUSED,
   2755                       bfd *output_bfd ATTRIBUTE_UNUSED,
   2756                       struct internal_reloc *rel ATTRIBUTE_UNUSED,
   2757                       struct internal_syment *sym ATTRIBUTE_UNUSED,
   2758                       struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
   2759                       bfd_vma val,
   2760                       bfd_vma addend,
   2761                       bfd_vma *relocation,
   2762                       bfd_byte *contents ATTRIBUTE_UNUSED)
   2763 {
   2764   *relocation = val + addend;
   2765   return TRUE;
   2766 }
   2767 
   2768 bfd_boolean
   2769 xcoff_reloc_type_neg (bfd *input_bfd ATTRIBUTE_UNUSED,
   2770                       asection *input_section ATTRIBUTE_UNUSED,
   2771                       bfd *output_bfd ATTRIBUTE_UNUSED,
   2772                       struct internal_reloc *rel ATTRIBUTE_UNUSED,
   2773                       struct internal_syment *sym ATTRIBUTE_UNUSED,
   2774                       struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
   2775                       bfd_vma val,
   2776                       bfd_vma addend,
   2777                       bfd_vma *relocation,
   2778                       bfd_byte *contents ATTRIBUTE_UNUSED)
   2779 {
   2780   *relocation = addend - val;
   2781   return TRUE;
   2782 }
   2783 
   2784 bfd_boolean
   2785 xcoff_reloc_type_rel (bfd *input_bfd ATTRIBUTE_UNUSED,
   2786                       asection *input_section,
   2787                       bfd *output_bfd ATTRIBUTE_UNUSED,
   2788                       struct internal_reloc *rel ATTRIBUTE_UNUSED,
   2789                       struct internal_syment *sym ATTRIBUTE_UNUSED,
   2790                       struct reloc_howto_struct *howto,
   2791                       bfd_vma val,
   2792                       bfd_vma addend,
   2793                       bfd_vma *relocation,
   2794                       bfd_byte *contents ATTRIBUTE_UNUSED)
   2795 {
   2796   howto->pc_relative = TRUE;
   2797 
   2798   /* A PC relative reloc includes the section address.  */
   2799   addend += input_section->vma;
   2800 
   2801   *relocation = val + addend;
   2802   *relocation -= (input_section->output_section->vma
   2803 		  + input_section->output_offset);
   2804   return TRUE;
   2805 }
   2806 
   2807 bfd_boolean
   2808 xcoff_reloc_type_toc (bfd *input_bfd,
   2809                       asection *input_section ATTRIBUTE_UNUSED,
   2810                       bfd *output_bfd,
   2811                       struct internal_reloc *rel,
   2812                       struct internal_syment *sym,
   2813                       struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
   2814                       bfd_vma val,
   2815                       bfd_vma addend ATTRIBUTE_UNUSED,
   2816                       bfd_vma *relocation,
   2817                       bfd_byte *contents ATTRIBUTE_UNUSED)
   2818 {
   2819   struct xcoff_link_hash_entry *h;
   2820 
   2821   if (0 > rel->r_symndx)
   2822     return FALSE;
   2823 
   2824   h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
   2825 
   2826   if (h != NULL && h->smclas != XMC_TD)
   2827     {
   2828       if (h->toc_section == NULL)
   2829 	{
   2830 	  (*_bfd_error_handler)
   2831 	    (_("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry"),
   2832 	     bfd_get_filename (input_bfd), rel->r_vaddr,
   2833 	     h->root.root.string);
   2834 	  bfd_set_error (bfd_error_bad_value);
   2835 	  return FALSE;
   2836 	}
   2837 
   2838       BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
   2839       val = (h->toc_section->output_section->vma
   2840 	      + h->toc_section->output_offset);
   2841     }
   2842 
   2843   *relocation = ((val - xcoff_data (output_bfd)->toc)
   2844 		 - (sym->n_value - xcoff_data (input_bfd)->toc));
   2845   return TRUE;
   2846 }
   2847 
   2848 bfd_boolean
   2849 xcoff_reloc_type_ba (bfd *input_bfd ATTRIBUTE_UNUSED,
   2850                      asection *input_section ATTRIBUTE_UNUSED,
   2851                      bfd *output_bfd ATTRIBUTE_UNUSED,
   2852                      struct internal_reloc *rel ATTRIBUTE_UNUSED,
   2853                      struct internal_syment *sym ATTRIBUTE_UNUSED,
   2854                      struct reloc_howto_struct *howto,
   2855                      bfd_vma val,
   2856                      bfd_vma addend,
   2857                      bfd_vma *relocation,
   2858                      bfd_byte *contents ATTRIBUTE_UNUSED)
   2859 {
   2860   howto->src_mask &= ~3;
   2861   howto->dst_mask = howto->src_mask;
   2862 
   2863   *relocation = val + addend;
   2864 
   2865   return TRUE;
   2866 }
   2867 
   2868 static bfd_boolean
   2869 xcoff_reloc_type_br (bfd *input_bfd,
   2870                      asection *input_section,
   2871                      bfd *output_bfd ATTRIBUTE_UNUSED,
   2872                      struct internal_reloc *rel,
   2873                      struct internal_syment *sym ATTRIBUTE_UNUSED,
   2874                      struct reloc_howto_struct *howto,
   2875                      bfd_vma val,
   2876                      bfd_vma addend,
   2877                      bfd_vma *relocation,
   2878                      bfd_byte *contents)
   2879 {
   2880   struct xcoff_link_hash_entry *h;
   2881   bfd_vma section_offset;
   2882 
   2883   if (0 > rel->r_symndx)
   2884     return FALSE;
   2885 
   2886   h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
   2887   section_offset = rel->r_vaddr - input_section->vma;
   2888 
   2889   /* If we see an R_BR or R_RBR reloc which is jumping to global
   2890      linkage code, and it is followed by an appropriate cror nop
   2891      instruction, we replace the cror with lwz r2,20(r1).  This
   2892      restores the TOC after the glink code.  Contrariwise, if the
   2893      call is followed by a lwz r2,20(r1), but the call is not
   2894      going to global linkage code, we can replace the load with a
   2895      cror.  */
   2896   if (NULL != h
   2897       && (bfd_link_hash_defined == h->root.type
   2898 	  || bfd_link_hash_defweak == h->root.type)
   2899       && section_offset + 8 <= input_section->size)
   2900     {
   2901       bfd_byte *pnext;
   2902       unsigned long next;
   2903 
   2904       pnext = contents + section_offset + 4;
   2905       next = bfd_get_32 (input_bfd, pnext);
   2906 
   2907       /* The _ptrgl function is magic.  It is used by the AIX
   2908 	 compiler to call a function through a pointer.  */
   2909       if (h->smclas == XMC_GL || strcmp (h->root.root.string, "._ptrgl") == 0)
   2910 	{
   2911 	  if (next == 0x4def7b82			/* cror 15,15,15 */
   2912 	      || next == 0x4ffffb82			/* cror 31,31,31 */
   2913 	      || next == 0x60000000)			/* ori r0,r0,0 */
   2914 	    bfd_put_32 (input_bfd, 0x80410014, pnext);	/* lwz r2,20(r1) */
   2915 
   2916 	}
   2917       else
   2918 	{
   2919 	  if (next == 0x80410014)			/* lwz r2,20(r1) */
   2920 	    bfd_put_32 (input_bfd, 0x60000000, pnext);	/* ori r0,r0,0 */
   2921 	}
   2922     }
   2923   else if (NULL != h && bfd_link_hash_undefined == h->root.type)
   2924     {
   2925       /* Normally, this relocation is against a defined symbol.  In the
   2926 	 case where this is a partial link and the output section offset
   2927 	 is greater than 2^25, the linker will return an invalid error
   2928 	 message that the relocation has been truncated.  Yes it has been
   2929 	 truncated but no it not important.  For this case, disable the
   2930 	 overflow checking. */
   2931 
   2932       howto->complain_on_overflow = complain_overflow_dont;
   2933     }
   2934 
   2935   /* The original PC-relative relocation is biased by -r_vaddr, so adding
   2936      the value below will give the absolute target address.  */
   2937   *relocation = val + addend + rel->r_vaddr;
   2938 
   2939   howto->src_mask &= ~3;
   2940   howto->dst_mask = howto->src_mask;
   2941 
   2942   if (h != NULL
   2943       && (h->root.type == bfd_link_hash_defined
   2944 	  || h->root.type == bfd_link_hash_defweak)
   2945       && bfd_is_abs_section (h->root.u.def.section)
   2946       && section_offset + 4 <= input_section->size)
   2947     {
   2948       bfd_byte *ptr;
   2949       bfd_vma insn;
   2950 
   2951       /* Turn the relative branch into an absolute one by setting the
   2952 	 AA bit.  */
   2953       ptr = contents + section_offset;
   2954       insn = bfd_get_32 (input_bfd, ptr);
   2955       insn |= 2;
   2956       bfd_put_32 (input_bfd, insn, ptr);
   2957 
   2958       /* Make the howto absolute too.  */
   2959       howto->pc_relative = FALSE;
   2960       howto->complain_on_overflow = complain_overflow_bitfield;
   2961     }
   2962   else
   2963     {
   2964       /* Use a PC-relative howto and subtract the instruction's address
   2965 	 from the target address we calculated above.  */
   2966       howto->pc_relative = TRUE;
   2967       *relocation -= (input_section->output_section->vma
   2968 		      + input_section->output_offset
   2969 		      + section_offset);
   2970     }
   2971   return TRUE;
   2972 }
   2973 
   2974 bfd_boolean
   2975 xcoff_reloc_type_crel (bfd *input_bfd ATTRIBUTE_UNUSED,
   2976                        asection *input_section,
   2977                        bfd *output_bfd ATTRIBUTE_UNUSED,
   2978                        struct internal_reloc *rel ATTRIBUTE_UNUSED,
   2979                        struct internal_syment *sym ATTRIBUTE_UNUSED,
   2980                        struct reloc_howto_struct *howto,
   2981                        bfd_vma val ATTRIBUTE_UNUSED,
   2982                        bfd_vma addend,
   2983                        bfd_vma *relocation,
   2984                        bfd_byte *contents ATTRIBUTE_UNUSED)
   2985 {
   2986   howto->pc_relative = TRUE;
   2987   howto->src_mask &= ~3;
   2988   howto->dst_mask = howto->src_mask;
   2989 
   2990   /* A PC relative reloc includes the section address.  */
   2991   addend += input_section->vma;
   2992 
   2993   *relocation = val + addend;
   2994   *relocation -= (input_section->output_section->vma
   2995 		  + input_section->output_offset);
   2996   return TRUE;
   2997 }
   2998 
   2999 static bfd_boolean
   3000 xcoff_complain_overflow_dont_func (bfd *input_bfd ATTRIBUTE_UNUSED,
   3001                                    bfd_vma val ATTRIBUTE_UNUSED,
   3002                                    bfd_vma relocation ATTRIBUTE_UNUSED,
   3003                                    struct reloc_howto_struct *
   3004                                       howto ATTRIBUTE_UNUSED)
   3005 {
   3006   return FALSE;
   3007 }
   3008 
   3009 static bfd_boolean
   3010 xcoff_complain_overflow_bitfield_func (bfd *input_bfd,
   3011                                        bfd_vma val,
   3012                                        bfd_vma relocation,
   3013                                        struct reloc_howto_struct *howto)
   3014 {
   3015   bfd_vma fieldmask, signmask, ss;
   3016   bfd_vma a, b, sum;
   3017 
   3018   /* Get the values to be added together.  For signed and unsigned
   3019      relocations, we assume that all values should be truncated to
   3020      the size of an address.  For bitfields, all the bits matter.
   3021      See also bfd_check_overflow.  */
   3022   fieldmask = N_ONES (howto->bitsize);
   3023   a = relocation;
   3024   b = val & howto->src_mask;
   3025 
   3026   /* Much like unsigned, except no trimming with addrmask.  In
   3027      addition, the sum overflows if there is a carry out of
   3028      the bfd_vma, i.e., the sum is less than either input
   3029      operand.  */
   3030   a >>= howto->rightshift;
   3031   b >>= howto->bitpos;
   3032 
   3033   /* Bitfields are sometimes used for signed numbers; for
   3034      example, a 13-bit field sometimes represents values in
   3035      0..8191 and sometimes represents values in -4096..4095.
   3036      If the field is signed and a is -4095 (0x1001) and b is
   3037      -1 (0x1fff), the sum is -4096 (0x1000), but (0x1001 +
   3038      0x1fff is 0x3000).  It's not clear how to handle this
   3039      everywhere, since there is not way to know how many bits
   3040      are significant in the relocation, but the original code
   3041      assumed that it was fully sign extended, and we will keep
   3042      that assumption.  */
   3043   signmask = (fieldmask >> 1) + 1;
   3044 
   3045   if ((a & ~ fieldmask) != 0)
   3046     {
   3047       /* Some bits out of the field are set.  This might not
   3048 	 be a problem: if this is a signed bitfield, it is OK
   3049 	 iff all the high bits are set, including the sign
   3050 	 bit.  We'll try setting all but the most significant
   3051 	 bit in the original relocation value: if this is all
   3052 	 ones, we are OK, assuming a signed bitfield.  */
   3053       ss = (signmask << howto->rightshift) - 1;
   3054       if ((ss | relocation) != ~ (bfd_vma) 0)
   3055 	return TRUE;
   3056       a &= fieldmask;
   3057     }
   3058 
   3059   /* We just assume (b & ~ fieldmask) == 0.  */
   3060 
   3061   /* We explicitly permit wrap around if this relocation
   3062      covers the high bit of an address.  The Linux kernel
   3063      relies on it, and it is the only way to write assembler
   3064      code which can run when loaded at a location 0x80000000
   3065      away from the location at which it is linked.  */
   3066   if (howto->bitsize + howto->rightshift
   3067       == bfd_arch_bits_per_address (input_bfd))
   3068     return FALSE;
   3069 
   3070   sum = a + b;
   3071   if (sum < a || (sum & ~ fieldmask) != 0)
   3072     {
   3073       /* There was a carry out, or the field overflow.  Test
   3074 	 for signed operands again.  Here is the overflow test
   3075 	 is as for complain_overflow_signed.  */
   3076       if (((~ (a ^ b)) & (a ^ sum)) & signmask)
   3077 	return TRUE;
   3078     }
   3079 
   3080   return FALSE;
   3081 }
   3082 
   3083 static bfd_boolean
   3084 xcoff_complain_overflow_signed_func (bfd *input_bfd,
   3085                                      bfd_vma val,
   3086                                      bfd_vma relocation,
   3087                                      struct reloc_howto_struct *howto)
   3088 {
   3089   bfd_vma addrmask, fieldmask, signmask, ss;
   3090   bfd_vma a, b, sum;
   3091 
   3092   /* Get the values to be added together.  For signed and unsigned
   3093      relocations, we assume that all values should be truncated to
   3094      the size of an address.  For bitfields, all the bits matter.
   3095      See also bfd_check_overflow.  */
   3096   fieldmask = N_ONES (howto->bitsize);
   3097   addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
   3098   a = relocation;
   3099   b = val & howto->src_mask;
   3100 
   3101   a = (a & addrmask) >> howto->rightshift;
   3102 
   3103   /* If any sign bits are set, all sign bits must be set.
   3104      That is, A must be a valid negative address after
   3105      shifting.  */
   3106   signmask = ~ (fieldmask >> 1);
   3107   ss = a & signmask;
   3108   if (ss != 0 && ss != ((addrmask >> howto->rightshift) & signmask))
   3109     return TRUE;
   3110 
   3111   /* We only need this next bit of code if the sign bit of B
   3112      is below the sign bit of A.  This would only happen if
   3113      SRC_MASK had fewer bits than BITSIZE.  Note that if
   3114      SRC_MASK has more bits than BITSIZE, we can get into
   3115      trouble; we would need to verify that B is in range, as
   3116      we do for A above.  */
   3117   signmask = ((~ howto->src_mask) >> 1) & howto->src_mask;
   3118   if ((b & signmask) != 0)
   3119     {
   3120       /* Set all the bits above the sign bit.  */
   3121       b -= signmask <<= 1;
   3122     }
   3123 
   3124   b = (b & addrmask) >> howto->bitpos;
   3125 
   3126   /* Now we can do the addition.  */
   3127   sum = a + b;
   3128 
   3129   /* See if the result has the correct sign.  Bits above the
   3130      sign bit are junk now; ignore them.  If the sum is
   3131      positive, make sure we did not have all negative inputs;
   3132      if the sum is negative, make sure we did not have all
   3133      positive inputs.  The test below looks only at the sign
   3134      bits, and it really just
   3135      SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
   3136   */
   3137   signmask = (fieldmask >> 1) + 1;
   3138   if (((~ (a ^ b)) & (a ^ sum)) & signmask)
   3139     return TRUE;
   3140 
   3141   return FALSE;
   3142 }
   3143 
   3144 static bfd_boolean
   3145 xcoff_complain_overflow_unsigned_func (bfd *input_bfd,
   3146                                        bfd_vma val,
   3147                                        bfd_vma relocation,
   3148                                        struct reloc_howto_struct *howto)
   3149 {
   3150   bfd_vma addrmask, fieldmask;
   3151   bfd_vma a, b, sum;
   3152 
   3153   /* Get the values to be added together.  For signed and unsigned
   3154      relocations, we assume that all values should be truncated to
   3155      the size of an address.  For bitfields, all the bits matter.
   3156      See also bfd_check_overflow.  */
   3157   fieldmask = N_ONES (howto->bitsize);
   3158   addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
   3159   a = relocation;
   3160   b = val & howto->src_mask;
   3161 
   3162   /* Checking for an unsigned overflow is relatively easy:
   3163      trim the addresses and add, and trim the result as well.
   3164      Overflow is normally indicated when the result does not
   3165      fit in the field.  However, we also need to consider the
   3166      case when, e.g., fieldmask is 0x7fffffff or smaller, an
   3167      input is 0x80000000, and bfd_vma is only 32 bits; then we
   3168      will get sum == 0, but there is an overflow, since the
   3169      inputs did not fit in the field.  Instead of doing a
   3170      separate test, we can check for this by or-ing in the
   3171      operands when testing for the sum overflowing its final
   3172      field.  */
   3173   a = (a & addrmask) >> howto->rightshift;
   3174   b = (b & addrmask) >> howto->bitpos;
   3175   sum = (a + b) & addrmask;
   3176   if ((a | b | sum) & ~ fieldmask)
   3177     return TRUE;
   3178 
   3179   return FALSE;
   3180 }
   3181 
   3182 /* This is the relocation function for the RS/6000/POWER/PowerPC.
   3183    This is currently the only processor which uses XCOFF; I hope that
   3184    will never change.
   3185 
   3186    I took the relocation type definitions from two documents:
   3187    the PowerPC AIX Version 4 Application Binary Interface, First
   3188    Edition (April 1992), and the PowerOpen ABI, Big-Endian
   3189    32-Bit Hardware Implementation (June 30, 1994).  Differences
   3190    between the documents are noted below.
   3191 
   3192    Unsupported r_type's
   3193 
   3194    R_RTB:
   3195    R_RRTBI:
   3196    R_RRTBA:
   3197 
   3198    These relocs are defined by the PowerPC ABI to be
   3199    relative branches which use half of the difference
   3200    between the symbol and the program counter.  I can't
   3201    quite figure out when this is useful.  These relocs are
   3202    not defined by the PowerOpen ABI.
   3203 
   3204    Supported r_type's
   3205 
   3206    R_POS:
   3207    Simple positive relocation.
   3208 
   3209    R_NEG:
   3210    Simple negative relocation.
   3211 
   3212    R_REL:
   3213    Simple PC relative relocation.
   3214 
   3215    R_TOC:
   3216    TOC relative relocation.  The value in the instruction in
   3217    the input file is the offset from the input file TOC to
   3218    the desired location.  We want the offset from the final
   3219    TOC to the desired location.  We have:
   3220    isym = iTOC + in
   3221    iinsn = in + o
   3222    osym = oTOC + on
   3223    oinsn = on + o
   3224    so we must change insn by on - in.
   3225 
   3226    R_GL:
   3227    GL linkage relocation.  The value of this relocation
   3228    is the address of the entry in the TOC section.
   3229 
   3230    R_TCL:
   3231    Local object TOC address.  I can't figure out the
   3232    difference between this and case R_GL.
   3233 
   3234    R_TRL:
   3235    TOC relative relocation.  A TOC relative load instruction
   3236    which may be changed to a load address instruction.
   3237    FIXME: We don't currently implement this optimization.
   3238 
   3239    R_TRLA:
   3240    TOC relative relocation.  This is a TOC relative load
   3241    address instruction which may be changed to a load
   3242    instruction.  FIXME: I don't know if this is the correct
   3243    implementation.
   3244 
   3245    R_BA:
   3246    Absolute branch.  We don't want to mess with the lower
   3247    two bits of the instruction.
   3248 
   3249    R_CAI:
   3250    The PowerPC ABI defines this as an absolute call which
   3251    may be modified to become a relative call.  The PowerOpen
   3252    ABI does not define this relocation type.
   3253 
   3254    R_RBA:
   3255    Absolute branch which may be modified to become a
   3256    relative branch.
   3257 
   3258    R_RBAC:
   3259    The PowerPC ABI defines this as an absolute branch to a
   3260    fixed address which may be modified to an absolute branch
   3261    to a symbol.  The PowerOpen ABI does not define this
   3262    relocation type.
   3263 
   3264    R_RBRC:
   3265    The PowerPC ABI defines this as an absolute branch to a
   3266    fixed address which may be modified to a relative branch.
   3267    The PowerOpen ABI does not define this relocation type.
   3268 
   3269    R_BR:
   3270    Relative branch.  We don't want to mess with the lower
   3271    two bits of the instruction.
   3272 
   3273    R_CREL:
   3274    The PowerPC ABI defines this as a relative call which may
   3275    be modified to become an absolute call.  The PowerOpen
   3276    ABI does not define this relocation type.
   3277 
   3278    R_RBR:
   3279    A relative branch which may be modified to become an
   3280    absolute branch.
   3281 
   3282    R_RL:
   3283    The PowerPC AIX ABI describes this as a load which may be
   3284    changed to a load address.  The PowerOpen ABI says this
   3285    is the same as case R_POS.
   3286 
   3287    R_RLA:
   3288    The PowerPC AIX ABI describes this as a load address
   3289    which may be changed to a load.  The PowerOpen ABI says
   3290    this is the same as R_POS.
   3291 */
   3292 
   3293 bfd_boolean
   3294 xcoff_ppc_relocate_section (bfd *output_bfd,
   3295                             struct bfd_link_info *info,
   3296                             bfd *input_bfd,
   3297                             asection *input_section,
   3298                             bfd_byte *contents,
   3299                             struct internal_reloc *relocs,
   3300                             struct internal_syment *syms,
   3301                             asection **sections)
   3302 {
   3303   struct internal_reloc *rel;
   3304   struct internal_reloc *relend;
   3305 
   3306   rel = relocs;
   3307   relend = rel + input_section->reloc_count;
   3308   for (; rel < relend; rel++)
   3309     {
   3310       long symndx;
   3311       struct xcoff_link_hash_entry *h;
   3312       struct internal_syment *sym;
   3313       bfd_vma addend;
   3314       bfd_vma val;
   3315       struct reloc_howto_struct howto;
   3316       bfd_vma relocation;
   3317       bfd_vma value_to_relocate;
   3318       bfd_vma address;
   3319       bfd_byte *location;
   3320 
   3321       /* Relocation type R_REF is a special relocation type which is
   3322 	 merely used to prevent garbage collection from occurring for
   3323 	 the csect including the symbol which it references.  */
   3324       if (rel->r_type == R_REF)
   3325 	continue;
   3326 
   3327       /* howto */
   3328       howto.type = rel->r_type;
   3329       howto.rightshift = 0;
   3330       howto.bitsize = (rel->r_size & 0x1f) + 1;
   3331       howto.size = howto.bitsize > 16 ? 2 : 1;
   3332       howto.pc_relative = FALSE;
   3333       howto.bitpos = 0;
   3334       howto.complain_on_overflow = (rel->r_size & 0x80
   3335 				    ? complain_overflow_signed
   3336 				    : complain_overflow_bitfield);
   3337       howto.special_function = NULL;
   3338       howto.name = "internal";
   3339       howto.partial_inplace = TRUE;
   3340       howto.src_mask = howto.dst_mask = N_ONES (howto.bitsize);
   3341       howto.pcrel_offset = FALSE;
   3342 
   3343       /* symbol */
   3344       val = 0;
   3345       addend = 0;
   3346       h = NULL;
   3347       sym = NULL;
   3348       symndx = rel->r_symndx;
   3349 
   3350       if (-1 != symndx)
   3351 	{
   3352 	  asection *sec;
   3353 
   3354 	  h = obj_xcoff_sym_hashes (input_bfd)[symndx];
   3355 	  sym = syms + symndx;
   3356 	  addend = - sym->n_value;
   3357 
   3358 	  if (NULL == h)
   3359 	    {
   3360 	      sec = sections[symndx];
   3361 	      /* Hack to make sure we use the right TOC anchor value
   3362 		 if this reloc is against the TOC anchor.  */
   3363 	      if (sec->name[3] == '0'
   3364 		  && strcmp (sec->name, ".tc0") == 0)
   3365 		val = xcoff_data (output_bfd)->toc;
   3366 	      else
   3367 		val = (sec->output_section->vma
   3368 		       + sec->output_offset
   3369 		       + sym->n_value
   3370 		       - sec->vma);
   3371 	    }
   3372 	  else
   3373 	    {
   3374 	      if (info->unresolved_syms_in_objects != RM_IGNORE
   3375 		  && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
   3376 		(*info->callbacks->undefined_symbol)
   3377 		  (info, h->root.root.string,
   3378 		   input_bfd, input_section,
   3379 		   rel->r_vaddr - input_section->vma,
   3380 		   info->unresolved_syms_in_objects == RM_GENERATE_ERROR);
   3381 
   3382 	      if (h->root.type == bfd_link_hash_defined
   3383 		  || h->root.type == bfd_link_hash_defweak)
   3384 		{
   3385 		  sec = h->root.u.def.section;
   3386 		  val = (h->root.u.def.value
   3387 			 + sec->output_section->vma
   3388 			 + sec->output_offset);
   3389 		}
   3390 	      else if (h->root.type == bfd_link_hash_common)
   3391 		{
   3392 		  sec = h->root.u.c.p->section;
   3393 		  val = (sec->output_section->vma
   3394 			 + sec->output_offset);
   3395 
   3396 		}
   3397 	      else
   3398 		{
   3399 		  BFD_ASSERT (bfd_link_relocatable (info)
   3400 			      || (info->static_link
   3401 				  && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
   3402 			      || (h->flags & XCOFF_DEF_DYNAMIC) != 0
   3403 			      || (h->flags & XCOFF_IMPORT) != 0);
   3404 		}
   3405 	    }
   3406 	}
   3407 
   3408       if (rel->r_type >= XCOFF_MAX_CALCULATE_RELOCATION
   3409 	  || !((*xcoff_calculate_relocation[rel->r_type])
   3410 	       (input_bfd, input_section, output_bfd, rel, sym, &howto, val,
   3411 		addend, &relocation, contents)))
   3412 	return FALSE;
   3413 
   3414       /* address */
   3415       address = rel->r_vaddr - input_section->vma;
   3416       location = contents + address;
   3417 
   3418       if (address > input_section->size)
   3419 	abort ();
   3420 
   3421       /* Get the value we are going to relocate.  */
   3422       if (1 == howto.size)
   3423 	value_to_relocate = bfd_get_16 (input_bfd, location);
   3424       else
   3425 	value_to_relocate = bfd_get_32 (input_bfd, location);
   3426 
   3427       /* overflow.
   3428 
   3429 	 FIXME: We may drop bits during the addition
   3430 	 which we don't check for.  We must either check at every single
   3431 	 operation, which would be tedious, or we must do the computations
   3432 	 in a type larger than bfd_vma, which would be inefficient.  */
   3433 
   3434       if ((unsigned int) howto.complain_on_overflow
   3435 	  >= XCOFF_MAX_COMPLAIN_OVERFLOW)
   3436 	abort ();
   3437 
   3438       if (((*xcoff_complain_overflow[howto.complain_on_overflow])
   3439 	   (input_bfd, value_to_relocate, relocation, &howto)))
   3440 	{
   3441 	  const char *name;
   3442 	  char buf[SYMNMLEN + 1];
   3443 	  char reloc_type_name[10];
   3444 
   3445 	  if (symndx == -1)
   3446 	    {
   3447 	      name = "*ABS*";
   3448 	    }
   3449 	  else if (h != NULL)
   3450 	    {
   3451 	      name = NULL;
   3452 	    }
   3453 	  else
   3454 	    {
   3455 	      name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
   3456 	      if (name == NULL)
   3457 		name = "UNKNOWN";
   3458 	    }
   3459 	  sprintf (reloc_type_name, "0x%02x", rel->r_type);
   3460 
   3461 	  (*info->callbacks->reloc_overflow)
   3462 	    (info, (h ? &h->root : NULL), name, reloc_type_name,
   3463 	     (bfd_vma) 0, input_bfd, input_section,
   3464 	     rel->r_vaddr - input_section->vma);
   3465 	}
   3466 
   3467       /* Add RELOCATION to the right bits of VALUE_TO_RELOCATE.  */
   3468       value_to_relocate = ((value_to_relocate & ~howto.dst_mask)
   3469 			   | (((value_to_relocate & howto.src_mask)
   3470 			       + relocation) & howto.dst_mask));
   3471 
   3472       /* Put the value back in the object file.  */
   3473       if (1 == howto.size)
   3474 	bfd_put_16 (input_bfd, value_to_relocate, location);
   3475       else
   3476 	bfd_put_32 (input_bfd, value_to_relocate, location);
   3477     }
   3478 
   3479   return TRUE;
   3480 }
   3481 
   3482 static bfd_boolean
   3483 _bfd_xcoff_put_ldsymbol_name (bfd *abfd ATTRIBUTE_UNUSED,
   3484                               struct xcoff_loader_info *ldinfo,
   3485                               struct internal_ldsym *ldsym,
   3486                               const char *name)
   3487 {
   3488   size_t len;
   3489   len = strlen (name);
   3490 
   3491   if (len <= SYMNMLEN)
   3492     strncpy (ldsym->_l._l_name, name, SYMNMLEN);
   3493   else
   3494     {
   3495       if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
   3496 	{
   3497 	  bfd_size_type newalc;
   3498 	  char *newstrings;
   3499 
   3500 	  newalc = ldinfo->string_alc * 2;
   3501 	  if (newalc == 0)
   3502 	    newalc = 32;
   3503 	  while (ldinfo->string_size + len + 3 > newalc)
   3504 	    newalc *= 2;
   3505 
   3506 	  newstrings = bfd_realloc (ldinfo->strings, newalc);
   3507 	  if (newstrings == NULL)
   3508 	    {
   3509 	      ldinfo->failed = TRUE;
   3510 	      return FALSE;
   3511 	    }
   3512 	  ldinfo->string_alc = newalc;
   3513 	  ldinfo->strings = newstrings;
   3514 	}
   3515 
   3516       bfd_put_16 (ldinfo->output_bfd, (bfd_vma) (len + 1),
   3517 		  ldinfo->strings + ldinfo->string_size);
   3518       strcpy (ldinfo->strings + ldinfo->string_size + 2, name);
   3519       ldsym->_l._l_l._l_zeroes = 0;
   3520       ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
   3521       ldinfo->string_size += len + 3;
   3522     }
   3523 
   3524   return TRUE;
   3525 }
   3526 
   3527 static bfd_boolean
   3528 _bfd_xcoff_put_symbol_name (struct bfd_link_info *info,
   3529 			    struct bfd_strtab_hash *strtab,
   3530 			    struct internal_syment *sym,
   3531 			    const char *name)
   3532 {
   3533   if (strlen (name) <= SYMNMLEN)
   3534     {
   3535       strncpy (sym->_n._n_name, name, SYMNMLEN);
   3536     }
   3537   else
   3538     {
   3539       bfd_boolean hash;
   3540       bfd_size_type indx;
   3541 
   3542       hash = !info->traditional_format;
   3543       indx = _bfd_stringtab_add (strtab, name, hash, FALSE);
   3544       if (indx == (bfd_size_type) -1)
   3545 	return FALSE;
   3546       sym->_n._n_n._n_zeroes = 0;
   3547       sym->_n._n_n._n_offset = STRING_SIZE_SIZE + indx;
   3548     }
   3549   return TRUE;
   3550 }
   3551 
   3552 static asection *
   3553 xcoff_create_csect_from_smclas (bfd *abfd,
   3554                                 union internal_auxent *aux,
   3555                                 const char *symbol_name)
   3556 {
   3557   asection *return_value = NULL;
   3558 
   3559   /* .sv64 = x_smclas == 17
   3560      This is an invalid csect for 32 bit apps.  */
   3561   static const char * const names[] =
   3562     {
   3563       ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo", /* 0 - 7 */
   3564       ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0", /* 8 - 15 */
   3565       ".td", NULL, ".sv3264", NULL, ".tl", ".ul", ".te"
   3566     };
   3567 
   3568   if ((aux->x_csect.x_smclas < ARRAY_SIZE (names))
   3569       && (NULL != names[aux->x_csect.x_smclas]))
   3570     {
   3571       return_value = bfd_make_section_anyway
   3572 	(abfd, names[aux->x_csect.x_smclas]);
   3573     }
   3574   else
   3575     {
   3576       (*_bfd_error_handler)
   3577 	(_("%B: symbol `%s' has unrecognized smclas %d"),
   3578 	 abfd, symbol_name, aux->x_csect.x_smclas);
   3579       bfd_set_error (bfd_error_bad_value);
   3580     }
   3581 
   3582   return return_value;
   3583 }
   3584 
   3585 static bfd_boolean
   3586 xcoff_is_lineno_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
   3587 {
   3588   if (0xffff <= value)
   3589     return TRUE;
   3590 
   3591   return FALSE;
   3592 }
   3593 
   3594 static bfd_boolean
   3595 xcoff_is_reloc_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
   3596 {
   3597   if (0xffff <= value)
   3598     return TRUE;
   3599 
   3600   return FALSE;
   3601 }
   3602 
   3603 static bfd_vma
   3604 xcoff_loader_symbol_offset (bfd *abfd,
   3605                             struct internal_ldhdr *ldhdr ATTRIBUTE_UNUSED)
   3606 {
   3607   return bfd_xcoff_ldhdrsz (abfd);
   3608 }
   3609 
   3610 static bfd_vma
   3611 xcoff_loader_reloc_offset (bfd *abfd, struct internal_ldhdr *ldhdr)
   3612 {
   3613   return bfd_xcoff_ldhdrsz (abfd) + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (abfd);
   3614 }
   3615 
   3616 static bfd_boolean
   3617 xcoff_generate_rtinit  (bfd *abfd, const char *init, const char *fini,
   3618                         bfd_boolean rtld)
   3619 {
   3620   bfd_byte filehdr_ext[FILHSZ];
   3621   bfd_byte scnhdr_ext[SCNHSZ];
   3622   bfd_byte syment_ext[SYMESZ * 10];
   3623   bfd_byte reloc_ext[RELSZ * 3];
   3624   bfd_byte *data_buffer;
   3625   bfd_size_type data_buffer_size;
   3626   bfd_byte *string_table = NULL, *st_tmp = NULL;
   3627   bfd_size_type string_table_size;
   3628   bfd_vma val;
   3629   size_t initsz, finisz;
   3630   struct internal_filehdr filehdr;
   3631   struct internal_scnhdr scnhdr;
   3632   struct internal_syment syment;
   3633   union internal_auxent auxent;
   3634   struct internal_reloc reloc;
   3635 
   3636   char *data_name = ".data";
   3637   char *rtinit_name = "__rtinit";
   3638   char *rtld_name = "__rtld";
   3639 
   3640   if (! bfd_xcoff_rtinit_size (abfd))
   3641     return FALSE;
   3642 
   3643   initsz = (init == NULL ? 0 : 1 + strlen (init));
   3644   finisz = (fini == NULL ? 0 : 1 + strlen (fini));
   3645 
   3646   /* file header */
   3647   memset (filehdr_ext, 0, FILHSZ);
   3648   memset (&filehdr, 0, sizeof (struct internal_filehdr));
   3649   filehdr.f_magic = bfd_xcoff_magic_number (abfd);
   3650   filehdr.f_nscns = 1;
   3651   filehdr.f_timdat = 0;
   3652   filehdr.f_nsyms = 0;  /* at least 6, no more than 10 */
   3653   filehdr.f_symptr = 0; /* set below */
   3654   filehdr.f_opthdr = 0;
   3655   filehdr.f_flags = 0;
   3656 
   3657   /* section header */
   3658   memset (scnhdr_ext, 0, SCNHSZ);
   3659   memset (&scnhdr, 0, sizeof (struct internal_scnhdr));
   3660   memcpy (scnhdr.s_name, data_name, strlen (data_name));
   3661   scnhdr.s_paddr = 0;
   3662   scnhdr.s_vaddr = 0;
   3663   scnhdr.s_size = 0;    /* set below */
   3664   scnhdr.s_scnptr = FILHSZ + SCNHSZ;
   3665   scnhdr.s_relptr = 0;  /* set below */
   3666   scnhdr.s_lnnoptr = 0;
   3667   scnhdr.s_nreloc = 0;  /* either 1 or 2 */
   3668   scnhdr.s_nlnno = 0;
   3669   scnhdr.s_flags = STYP_DATA;
   3670 
   3671   /* .data
   3672      0x0000	      0x00000000 : rtl
   3673      0x0004	      0x00000010 : offset to init, or 0
   3674      0x0008	      0x00000028 : offset to fini, or 0
   3675      0x000C	      0x0000000C : size of descriptor
   3676      0x0010	      0x00000000 : init, needs a reloc
   3677      0x0014	      0x00000040 : offset to init name
   3678      0x0018	      0x00000000 : flags, padded to a word
   3679      0x001C	      0x00000000 : empty init
   3680      0x0020	      0x00000000 :
   3681      0x0024	      0x00000000 :
   3682      0x0028	      0x00000000 : fini, needs a reloc
   3683      0x002C	      0x00000??? : offset to fini name
   3684      0x0030	      0x00000000 : flags, padded to a word
   3685      0x0034	      0x00000000 : empty fini
   3686      0x0038	      0x00000000 :
   3687      0x003C	      0x00000000 :
   3688      0x0040	      init name
   3689      0x0040 + initsz  fini name */
   3690 
   3691   data_buffer_size = 0x0040 + initsz + finisz;
   3692   data_buffer_size = (data_buffer_size + 7) &~ (bfd_size_type) 7;
   3693   data_buffer = NULL;
   3694   data_buffer = (bfd_byte *) bfd_zmalloc (data_buffer_size);
   3695   if (data_buffer == NULL)
   3696     return FALSE;
   3697 
   3698   if (initsz)
   3699     {
   3700       val = 0x10;
   3701       bfd_h_put_32 (abfd, val, &data_buffer[0x04]);
   3702       val = 0x40;
   3703       bfd_h_put_32 (abfd, val, &data_buffer[0x14]);
   3704       memcpy (&data_buffer[val], init, initsz);
   3705     }
   3706 
   3707   if (finisz)
   3708     {
   3709       val = 0x28;
   3710       bfd_h_put_32 (abfd, val, &data_buffer[0x08]);
   3711       val = 0x40 + initsz;
   3712       bfd_h_put_32 (abfd, val, &data_buffer[0x2C]);
   3713       memcpy (&data_buffer[val], fini, finisz);
   3714     }
   3715 
   3716   val = 0x0C;
   3717   bfd_h_put_32 (abfd, val, &data_buffer[0x0C]);
   3718 
   3719   scnhdr.s_size = data_buffer_size;
   3720 
   3721   /* string table */
   3722   string_table_size = 0;
   3723   if (initsz > 9)
   3724     string_table_size += initsz;
   3725   if (finisz > 9)
   3726     string_table_size += finisz;
   3727   if (string_table_size)
   3728     {
   3729       string_table_size += 4;
   3730       string_table = (bfd_byte *) bfd_zmalloc (string_table_size);
   3731       if (string_table == NULL)
   3732 	return FALSE;
   3733 
   3734       val = string_table_size;
   3735       bfd_h_put_32 (abfd, val, &string_table[0]);
   3736       st_tmp = string_table + 4;
   3737     }
   3738 
   3739   /* symbols
   3740      0. .data csect
   3741      2. __rtinit
   3742      4. init function
   3743      6. fini function
   3744      8. __rtld  */
   3745   memset (syment_ext, 0, 10 * SYMESZ);
   3746   memset (reloc_ext, 0, 3 * RELSZ);
   3747 
   3748   /* .data csect */
   3749   memset (&syment, 0, sizeof (struct internal_syment));
   3750   memset (&auxent, 0, sizeof (union internal_auxent));
   3751   memcpy (syment._n._n_name, data_name, strlen (data_name));
   3752   syment.n_scnum = 1;
   3753   syment.n_sclass = C_HIDEXT;
   3754   syment.n_numaux = 1;
   3755   auxent.x_csect.x_scnlen.l = data_buffer_size;
   3756   auxent.x_csect.x_smtyp = 3 << 3 | XTY_SD;
   3757   auxent.x_csect.x_smclas = XMC_RW;
   3758   bfd_coff_swap_sym_out (abfd, &syment,
   3759 			 &syment_ext[filehdr.f_nsyms * SYMESZ]);
   3760   bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
   3761 			 syment.n_numaux,
   3762 			 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
   3763   filehdr.f_nsyms += 2;
   3764 
   3765   /* __rtinit */
   3766   memset (&syment, 0, sizeof (struct internal_syment));
   3767   memset (&auxent, 0, sizeof (union internal_auxent));
   3768   memcpy (syment._n._n_name, rtinit_name, strlen (rtinit_name));
   3769   syment.n_scnum = 1;
   3770   syment.n_sclass = C_EXT;
   3771   syment.n_numaux = 1;
   3772   auxent.x_csect.x_smtyp = XTY_LD;
   3773   auxent.x_csect.x_smclas = XMC_RW;
   3774   bfd_coff_swap_sym_out (abfd, &syment,
   3775 			 &syment_ext[filehdr.f_nsyms * SYMESZ]);
   3776   bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
   3777 			 syment.n_numaux,
   3778 			 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
   3779   filehdr.f_nsyms += 2;
   3780 
   3781   /* init */
   3782   if (initsz)
   3783     {
   3784       memset (&syment, 0, sizeof (struct internal_syment));
   3785       memset (&auxent, 0, sizeof (union internal_auxent));
   3786 
   3787       if (initsz > 9)
   3788 	{
   3789 	  syment._n._n_n._n_offset = st_tmp - string_table;
   3790 	  memcpy (st_tmp, init, initsz);
   3791 	  st_tmp += initsz;
   3792 	}
   3793       else
   3794 	memcpy (syment._n._n_name, init, initsz - 1);
   3795 
   3796       syment.n_sclass = C_EXT;
   3797       syment.n_numaux = 1;
   3798       bfd_coff_swap_sym_out (abfd, &syment,
   3799 			     &syment_ext[filehdr.f_nsyms * SYMESZ]);
   3800       bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
   3801 			     syment.n_numaux,
   3802 			     &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
   3803 
   3804       /* reloc */
   3805       memset (&reloc, 0, sizeof (struct internal_reloc));
   3806       reloc.r_vaddr = 0x0010;
   3807       reloc.r_symndx = filehdr.f_nsyms;
   3808       reloc.r_type = R_POS;
   3809       reloc.r_size = 31;
   3810       bfd_coff_swap_reloc_out (abfd, &reloc, &reloc_ext[0]);
   3811 
   3812       filehdr.f_nsyms += 2;
   3813       scnhdr.s_nreloc += 1;
   3814     }
   3815 
   3816   /* fini */
   3817   if (finisz)
   3818     {
   3819       memset (&syment, 0, sizeof (struct internal_syment));
   3820       memset (&auxent, 0, sizeof (union internal_auxent));
   3821 
   3822       if (finisz > 9)
   3823 	{
   3824 	  syment._n._n_n._n_offset = st_tmp - string_table;
   3825 	  memcpy (st_tmp, fini, finisz);
   3826 	  st_tmp += finisz;
   3827 	}
   3828       else
   3829 	memcpy (syment._n._n_name, fini, finisz - 1);
   3830 
   3831       syment.n_sclass = C_EXT;
   3832       syment.n_numaux = 1;
   3833       bfd_coff_swap_sym_out (abfd, &syment,
   3834 			     &syment_ext[filehdr.f_nsyms * SYMESZ]);
   3835       bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
   3836 			     syment.n_numaux,
   3837 			     &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
   3838 
   3839       /* reloc */
   3840       memset (&reloc, 0, sizeof (struct internal_reloc));
   3841       reloc.r_vaddr = 0x0028;
   3842       reloc.r_symndx = filehdr.f_nsyms;
   3843       reloc.r_type = R_POS;
   3844       reloc.r_size = 31;
   3845       bfd_coff_swap_reloc_out (abfd, &reloc,
   3846 			       &reloc_ext[scnhdr.s_nreloc * RELSZ]);
   3847 
   3848       filehdr.f_nsyms += 2;
   3849       scnhdr.s_nreloc += 1;
   3850     }
   3851 
   3852   if (rtld)
   3853     {
   3854       memset (&syment, 0, sizeof (struct internal_syment));
   3855       memset (&auxent, 0, sizeof (union internal_auxent));
   3856       memcpy (syment._n._n_name, rtld_name, strlen (rtld_name));
   3857       syment.n_sclass = C_EXT;
   3858       syment.n_numaux = 1;
   3859       bfd_coff_swap_sym_out (abfd, &syment,
   3860 			     &syment_ext[filehdr.f_nsyms * SYMESZ]);
   3861       bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
   3862 			     syment.n_numaux,
   3863 			     &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
   3864 
   3865       /* reloc */
   3866       memset (&reloc, 0, sizeof (struct internal_reloc));
   3867       reloc.r_vaddr = 0x0000;
   3868       reloc.r_symndx = filehdr.f_nsyms;
   3869       reloc.r_type = R_POS;
   3870       reloc.r_size = 31;
   3871       bfd_coff_swap_reloc_out (abfd, &reloc,
   3872 			       &reloc_ext[scnhdr.s_nreloc * RELSZ]);
   3873 
   3874       filehdr.f_nsyms += 2;
   3875       scnhdr.s_nreloc += 1;
   3876     }
   3877 
   3878   scnhdr.s_relptr = scnhdr.s_scnptr + data_buffer_size;
   3879   filehdr.f_symptr = scnhdr.s_relptr + scnhdr.s_nreloc * RELSZ;
   3880 
   3881   bfd_coff_swap_filehdr_out (abfd, &filehdr, filehdr_ext);
   3882   bfd_bwrite (filehdr_ext, FILHSZ, abfd);
   3883   bfd_coff_swap_scnhdr_out (abfd, &scnhdr, scnhdr_ext);
   3884   bfd_bwrite (scnhdr_ext, SCNHSZ, abfd);
   3885   bfd_bwrite (data_buffer, data_buffer_size, abfd);
   3886   bfd_bwrite (reloc_ext, scnhdr.s_nreloc * RELSZ, abfd);
   3887   bfd_bwrite (syment_ext, filehdr.f_nsyms * SYMESZ, abfd);
   3888   bfd_bwrite (string_table, string_table_size, abfd);
   3889 
   3890   free (data_buffer);
   3891   data_buffer = NULL;
   3892 
   3893   return TRUE;
   3894 }
   3895 
   3896 
   3897 static reloc_howto_type xcoff_dynamic_reloc =
   3898 HOWTO (0,			/* type */
   3899        0,			/* rightshift */
   3900        2,			/* size (0 = byte, 1 = short, 2 = long) */
   3901        32,			/* bitsize */
   3902        FALSE,			/* pc_relative */
   3903        0,			/* bitpos */
   3904        complain_overflow_bitfield, /* complain_on_overflow */
   3905        0,			/* special_function */
   3906        "R_POS",			/* name */
   3907        TRUE,			/* partial_inplace */
   3908        0xffffffff,		/* src_mask */
   3909        0xffffffff,		/* dst_mask */
   3910        FALSE);			/* pcrel_offset */
   3911 
   3912 /*  glink
   3913 
   3914    The first word of global linkage code must be modified by filling in
   3915    the correct TOC offset.  */
   3916 
   3917 static unsigned long xcoff_glink_code[9] =
   3918   {
   3919     0x81820000,	/* lwz r12,0(r2) */
   3920     0x90410014,	/* stw r2,20(r1) */
   3921     0x800c0000,	/* lwz r0,0(r12) */
   3922     0x804c0004,	/* lwz r2,4(r12) */
   3923     0x7c0903a6,	/* mtctr r0 */
   3924     0x4e800420,	/* bctr */
   3925     0x00000000,	/* start of traceback table */
   3926     0x000c8000,	/* traceback table */
   3927     0x00000000,	/* traceback table */
   3928   };
   3929 
   3930 /* Table to convert DWARF flags to section names.  */
   3931 
   3932 const struct xcoff_dwsect_name xcoff_dwsect_names[] = {
   3933   { SSUBTYP_DWINFO,  ".dwinfo",   TRUE },
   3934   { SSUBTYP_DWLINE,  ".dwline",   TRUE },
   3935   { SSUBTYP_DWPBNMS, ".dwpbnms",  TRUE },
   3936   { SSUBTYP_DWPBTYP, ".dwpbtyp",  TRUE },
   3937   { SSUBTYP_DWARNGE, ".dwarnge",  TRUE },
   3938   { SSUBTYP_DWABREV, ".dwabrev",  FALSE },
   3939   { SSUBTYP_DWSTR,   ".dwstr",    TRUE },
   3940   { SSUBTYP_DWRNGES, ".dwrnges",  TRUE }
   3941 };
   3942 
   3943 /* For generic entry points.  */
   3944 #define _bfd_xcoff_close_and_cleanup _bfd_archive_close_and_cleanup
   3945 #define _bfd_xcoff_bfd_free_cached_info bfd_true
   3946 #define _bfd_xcoff_new_section_hook coff_new_section_hook
   3947 #define _bfd_xcoff_get_section_contents _bfd_generic_get_section_contents
   3948 #define _bfd_xcoff_get_section_contents_in_window \
   3949   _bfd_generic_get_section_contents_in_window
   3950 
   3951 /* For copy private data entry points.  */
   3952 #define _bfd_xcoff_bfd_copy_private_bfd_data \
   3953   _bfd_xcoff_copy_private_bfd_data
   3954 #define _bfd_xcoff_bfd_merge_private_bfd_data \
   3955   _bfd_generic_bfd_merge_private_bfd_data
   3956 #define _bfd_xcoff_bfd_copy_private_section_data \
   3957   _bfd_generic_bfd_copy_private_section_data
   3958 #define _bfd_xcoff_bfd_copy_private_symbol_data \
   3959    _bfd_generic_bfd_copy_private_symbol_data
   3960 #define _bfd_xcoff_bfd_copy_private_header_data \
   3961    _bfd_generic_bfd_copy_private_header_data
   3962 #define _bfd_xcoff_bfd_set_private_flags \
   3963    _bfd_generic_bfd_set_private_flags
   3964 #define _bfd_xcoff_bfd_print_private_bfd_data \
   3965    _bfd_generic_bfd_print_private_bfd_data
   3966 
   3967 /* For archive entry points.  */
   3968 #define _bfd_xcoff_slurp_extended_name_table \
   3969    _bfd_noarchive_slurp_extended_name_table
   3970 #define _bfd_xcoff_construct_extended_name_table \
   3971    _bfd_noarchive_construct_extended_name_table
   3972 #define _bfd_xcoff_truncate_arname bfd_dont_truncate_arname
   3973 #define _bfd_xcoff_write_ar_hdr _bfd_generic_write_ar_hdr
   3974 #define _bfd_xcoff_get_elt_at_index _bfd_generic_get_elt_at_index
   3975 #define _bfd_xcoff_generic_stat_arch_elt _bfd_xcoff_stat_arch_elt
   3976 #define _bfd_xcoff_update_armap_timestamp bfd_true
   3977 
   3978 /* For symbols entry points.  */
   3979 #define _bfd_xcoff_get_symtab_upper_bound coff_get_symtab_upper_bound
   3980 #define _bfd_xcoff_canonicalize_symtab coff_canonicalize_symtab
   3981 #define _bfd_xcoff_make_empty_symbol coff_make_empty_symbol
   3982 #define _bfd_xcoff_print_symbol coff_print_symbol
   3983 #define _bfd_xcoff_get_symbol_info coff_get_symbol_info
   3984 #define _bfd_xcoff_get_symbol_version_string \
   3985   _bfd_nosymbols_get_symbol_version_string
   3986 #define _bfd_xcoff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
   3987 #define _bfd_xcoff_bfd_is_target_special_symbol \
   3988   coff_bfd_is_target_special_symbol
   3989 #define _bfd_xcoff_get_lineno coff_get_lineno
   3990 #define _bfd_xcoff_find_nearest_line coff_find_nearest_line
   3991 #define _bfd_xcoff_find_line coff_find_line
   3992 #define _bfd_xcoff_find_inliner_info coff_find_inliner_info
   3993 #define _bfd_xcoff_bfd_make_debug_symbol coff_bfd_make_debug_symbol
   3994 #define _bfd_xcoff_read_minisymbols _bfd_generic_read_minisymbols
   3995 #define _bfd_xcoff_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
   3996 
   3997 /* For reloc entry points.  */
   3998 #define _bfd_xcoff_get_reloc_upper_bound coff_get_reloc_upper_bound
   3999 #define _bfd_xcoff_canonicalize_reloc coff_canonicalize_reloc
   4000 #define _bfd_xcoff_bfd_reloc_type_lookup _bfd_xcoff_reloc_type_lookup
   4001 #define _bfd_xcoff_bfd_reloc_name_lookup _bfd_xcoff_reloc_name_lookup
   4002 
   4003 /* For link entry points.  */
   4004 #define _bfd_xcoff_bfd_get_relocated_section_contents \
   4005   bfd_generic_get_relocated_section_contents
   4006 #define _bfd_xcoff_bfd_relax_section bfd_generic_relax_section
   4007 #define _bfd_xcoff_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
   4008 #define _bfd_xcoff_bfd_link_just_syms _bfd_generic_link_just_syms
   4009 #define _bfd_xcoff_bfd_copy_link_hash_symbol_type \
   4010   _bfd_generic_copy_link_hash_symbol_type
   4011 #define _bfd_xcoff_bfd_link_split_section _bfd_generic_link_split_section
   4012 #define _bfd_xcoff_bfd_gc_sections bfd_generic_gc_sections
   4013 #define _bfd_xcoff_bfd_lookup_section_flags bfd_generic_lookup_section_flags
   4014 #define _bfd_xcoff_bfd_merge_sections bfd_generic_merge_sections
   4015 #define _bfd_xcoff_bfd_is_group_section bfd_generic_is_group_section
   4016 #define _bfd_xcoff_bfd_discard_group bfd_generic_discard_group
   4017 #define _bfd_xcoff_section_already_linked _bfd_generic_section_already_linked
   4018 #define _bfd_xcoff_bfd_define_common_symbol _bfd_xcoff_define_common_symbol
   4019 #define _bfd_xcoff_bfd_link_check_relocs    _bfd_generic_link_check_relocs
   4020 
   4021 /* For dynamic symbols and relocs entry points.  */
   4022 #define _bfd_xcoff_get_synthetic_symtab _bfd_nodynamic_get_synthetic_symtab
   4023 
   4024 static const struct xcoff_backend_data_rec bfd_xcoff_backend_data =
   4025   {
   4026     { /* COFF backend, defined in libcoff.h.  */
   4027       _bfd_xcoff_swap_aux_in,
   4028       _bfd_xcoff_swap_sym_in,
   4029       coff_swap_lineno_in,
   4030       _bfd_xcoff_swap_aux_out,
   4031       _bfd_xcoff_swap_sym_out,
   4032       coff_swap_lineno_out,
   4033       xcoff_swap_reloc_out,
   4034       coff_swap_filehdr_out,
   4035       coff_swap_aouthdr_out,
   4036       coff_swap_scnhdr_out,
   4037       FILHSZ,
   4038       AOUTSZ,
   4039       SCNHSZ,
   4040       SYMESZ,
   4041       AUXESZ,
   4042       RELSZ,
   4043       LINESZ,
   4044       FILNMLEN,
   4045       TRUE,			/* _bfd_coff_long_filenames */
   4046       XCOFF_NO_LONG_SECTION_NAMES,  /* _bfd_coff_long_section_names */
   4047       3,			/* _bfd_coff_default_section_alignment_power */
   4048       FALSE,			/* _bfd_coff_force_symnames_in_strings */
   4049       2,			/* _bfd_coff_debug_string_prefix_length */
   4050       32768,			/* _bfd_coff_max_nscns */
   4051       coff_swap_filehdr_in,
   4052       coff_swap_aouthdr_in,
   4053       coff_swap_scnhdr_in,
   4054       xcoff_swap_reloc_in,
   4055       coff_bad_format_hook,
   4056       coff_set_arch_mach_hook,
   4057       coff_mkobject_hook,
   4058       styp_to_sec_flags,
   4059       coff_set_alignment_hook,
   4060       coff_slurp_symbol_table,
   4061       symname_in_debug_hook,
   4062       coff_pointerize_aux_hook,
   4063       coff_print_aux,
   4064       dummy_reloc16_extra_cases,
   4065       dummy_reloc16_estimate,
   4066       NULL,			/* bfd_coff_sym_is_global */
   4067       coff_compute_section_file_positions,
   4068       NULL,			/* _bfd_coff_start_final_link */
   4069       xcoff_ppc_relocate_section,
   4070       coff_rtype_to_howto,
   4071       NULL,			/* _bfd_coff_adjust_symndx */
   4072       _bfd_generic_link_add_one_symbol,
   4073       coff_link_output_has_begun,
   4074       coff_final_link_postscript,
   4075       NULL			/* print_pdata.  */
   4076     },
   4077 
   4078     0x01DF,			/* magic number */
   4079     bfd_arch_rs6000,
   4080     bfd_mach_rs6k,
   4081 
   4082     /* Function pointers to xcoff specific swap routines.  */
   4083     xcoff_swap_ldhdr_in,
   4084     xcoff_swap_ldhdr_out,
   4085     xcoff_swap_ldsym_in,
   4086     xcoff_swap_ldsym_out,
   4087     xcoff_swap_ldrel_in,
   4088     xcoff_swap_ldrel_out,
   4089 
   4090     /* Sizes.  */
   4091     LDHDRSZ,
   4092     LDSYMSZ,
   4093     LDRELSZ,
   4094     12,				/* _xcoff_function_descriptor_size */
   4095     SMALL_AOUTSZ,
   4096 
   4097     /* Versions.  */
   4098     1,				/* _xcoff_ldhdr_version */
   4099 
   4100     _bfd_xcoff_put_symbol_name,
   4101     _bfd_xcoff_put_ldsymbol_name,
   4102     &xcoff_dynamic_reloc,
   4103     xcoff_create_csect_from_smclas,
   4104 
   4105     /* Lineno and reloc count overflow.  */
   4106     xcoff_is_lineno_count_overflow,
   4107     xcoff_is_reloc_count_overflow,
   4108 
   4109     xcoff_loader_symbol_offset,
   4110     xcoff_loader_reloc_offset,
   4111 
   4112     /* glink.  */
   4113     &xcoff_glink_code[0],
   4114     36,				/* _xcoff_glink_size */
   4115 
   4116     /* rtinit */
   4117     64,				/* _xcoff_rtinit_size */
   4118     xcoff_generate_rtinit,
   4119   };
   4120 
   4121 /* The transfer vector that leads the outside world to all of the above.  */
   4122 const bfd_target rs6000_xcoff_vec =
   4123   {
   4124     "aixcoff-rs6000",
   4125     bfd_target_xcoff_flavour,
   4126     BFD_ENDIAN_BIG,		/* data byte order is big */
   4127     BFD_ENDIAN_BIG,		/* header byte order is big */
   4128 
   4129     (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
   4130      | HAS_SYMS | HAS_LOCALS | WP_TEXT),
   4131 
   4132     SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
   4133     0,				/* leading char */
   4134     '/',			/* ar_pad_char */
   4135     15,				/* ar_max_namelen */
   4136     0,				/* match priority.  */
   4137 
   4138     /* data */
   4139     bfd_getb64,
   4140     bfd_getb_signed_64,
   4141     bfd_putb64,
   4142     bfd_getb32,
   4143     bfd_getb_signed_32,
   4144     bfd_putb32,
   4145     bfd_getb16,
   4146     bfd_getb_signed_16,
   4147     bfd_putb16,
   4148 
   4149     /* hdrs */
   4150     bfd_getb64,
   4151     bfd_getb_signed_64,
   4152     bfd_putb64,
   4153     bfd_getb32,
   4154     bfd_getb_signed_32,
   4155     bfd_putb32,
   4156     bfd_getb16,
   4157     bfd_getb_signed_16,
   4158     bfd_putb16,
   4159 
   4160     { /* bfd_check_format */
   4161       _bfd_dummy_target,
   4162       coff_object_p,
   4163       _bfd_xcoff_archive_p,
   4164       CORE_FILE_P
   4165     },
   4166 
   4167     { /* bfd_set_format */
   4168       bfd_false,
   4169       coff_mkobject,
   4170       _bfd_generic_mkarchive,
   4171       bfd_false
   4172     },
   4173 
   4174     {/* bfd_write_contents */
   4175       bfd_false,
   4176       coff_write_object_contents,
   4177       _bfd_xcoff_write_archive_contents,
   4178       bfd_false
   4179     },
   4180 
   4181     BFD_JUMP_TABLE_GENERIC (_bfd_xcoff),
   4182     BFD_JUMP_TABLE_COPY (_bfd_xcoff),
   4183     BFD_JUMP_TABLE_CORE (coff),
   4184     BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
   4185     BFD_JUMP_TABLE_SYMBOLS (_bfd_xcoff),
   4186     BFD_JUMP_TABLE_RELOCS (_bfd_xcoff),
   4187     BFD_JUMP_TABLE_WRITE (coff),
   4188     BFD_JUMP_TABLE_LINK (_bfd_xcoff),
   4189     BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
   4190 
   4191     /* Opposite endian version, none exists */
   4192     NULL,
   4193 
   4194     & bfd_xcoff_backend_data,
   4195   };
   4196 
   4197 /* xcoff-powermac target
   4198    Old target.
   4199    Only difference between this target and the rs6000 target is the
   4200    the default architecture and machine type used in coffcode.h
   4201 
   4202    PowerPC Macs use the same magic numbers as RS/6000
   4203    (because that's how they were bootstrapped originally),
   4204    but they are always PowerPC architecture.  */
   4205 static const struct xcoff_backend_data_rec bfd_pmac_xcoff_backend_data =
   4206   {
   4207     { /* COFF backend, defined in libcoff.h.  */
   4208       _bfd_xcoff_swap_aux_in,
   4209       _bfd_xcoff_swap_sym_in,
   4210       coff_swap_lineno_in,
   4211       _bfd_xcoff_swap_aux_out,
   4212       _bfd_xcoff_swap_sym_out,
   4213       coff_swap_lineno_out,
   4214       xcoff_swap_reloc_out,
   4215       coff_swap_filehdr_out,
   4216       coff_swap_aouthdr_out,
   4217       coff_swap_scnhdr_out,
   4218       FILHSZ,
   4219       AOUTSZ,
   4220       SCNHSZ,
   4221       SYMESZ,
   4222       AUXESZ,
   4223       RELSZ,
   4224       LINESZ,
   4225       FILNMLEN,
   4226       TRUE,			/* _bfd_coff_long_filenames */
   4227       XCOFF_NO_LONG_SECTION_NAMES,  /* _bfd_coff_long_section_names */
   4228       3,			/* _bfd_coff_default_section_alignment_power */
   4229       FALSE,			/* _bfd_coff_force_symnames_in_strings */
   4230       2,			/* _bfd_coff_debug_string_prefix_length */
   4231       32768,			/* _bfd_coff_max_nscns */
   4232       coff_swap_filehdr_in,
   4233       coff_swap_aouthdr_in,
   4234       coff_swap_scnhdr_in,
   4235       xcoff_swap_reloc_in,
   4236       coff_bad_format_hook,
   4237       coff_set_arch_mach_hook,
   4238       coff_mkobject_hook,
   4239       styp_to_sec_flags,
   4240       coff_set_alignment_hook,
   4241       coff_slurp_symbol_table,
   4242       symname_in_debug_hook,
   4243       coff_pointerize_aux_hook,
   4244       coff_print_aux,
   4245       dummy_reloc16_extra_cases,
   4246       dummy_reloc16_estimate,
   4247       NULL,			/* bfd_coff_sym_is_global */
   4248       coff_compute_section_file_positions,
   4249       NULL,			/* _bfd_coff_start_final_link */
   4250       xcoff_ppc_relocate_section,
   4251       coff_rtype_to_howto,
   4252       NULL,			/* _bfd_coff_adjust_symndx */
   4253       _bfd_generic_link_add_one_symbol,
   4254       coff_link_output_has_begun,
   4255       coff_final_link_postscript,
   4256       NULL			/* print_pdata.  */
   4257     },
   4258 
   4259     0x01DF,			/* magic number */
   4260     bfd_arch_powerpc,
   4261     bfd_mach_ppc,
   4262 
   4263     /* Function pointers to xcoff specific swap routines.  */
   4264     xcoff_swap_ldhdr_in,
   4265     xcoff_swap_ldhdr_out,
   4266     xcoff_swap_ldsym_in,
   4267     xcoff_swap_ldsym_out,
   4268     xcoff_swap_ldrel_in,
   4269     xcoff_swap_ldrel_out,
   4270 
   4271     /* Sizes.  */
   4272     LDHDRSZ,
   4273     LDSYMSZ,
   4274     LDRELSZ,
   4275     12,				/* _xcoff_function_descriptor_size */
   4276     SMALL_AOUTSZ,
   4277 
   4278     /* Versions.  */
   4279     1,				/* _xcoff_ldhdr_version */
   4280 
   4281     _bfd_xcoff_put_symbol_name,
   4282     _bfd_xcoff_put_ldsymbol_name,
   4283     &xcoff_dynamic_reloc,
   4284     xcoff_create_csect_from_smclas,
   4285 
   4286     /* Lineno and reloc count overflow.  */
   4287     xcoff_is_lineno_count_overflow,
   4288     xcoff_is_reloc_count_overflow,
   4289 
   4290     xcoff_loader_symbol_offset,
   4291     xcoff_loader_reloc_offset,
   4292 
   4293     /* glink.  */
   4294     &xcoff_glink_code[0],
   4295     36,				/* _xcoff_glink_size */
   4296 
   4297     /* rtinit */
   4298     0,				/* _xcoff_rtinit_size */
   4299     xcoff_generate_rtinit,
   4300   };
   4301 
   4302 /* The transfer vector that leads the outside world to all of the above.  */
   4303 const bfd_target powerpc_xcoff_vec =
   4304   {
   4305     "xcoff-powermac",
   4306     bfd_target_xcoff_flavour,
   4307     BFD_ENDIAN_BIG,		/* data byte order is big */
   4308     BFD_ENDIAN_BIG,		/* header byte order is big */
   4309 
   4310     (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
   4311      | HAS_SYMS | HAS_LOCALS | WP_TEXT),
   4312 
   4313     SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
   4314     0,				/* leading char */
   4315     '/',			/* ar_pad_char */
   4316     15,				/* ar_max_namelen */
   4317     0,				/* match priority.  */
   4318 
   4319     /* data */
   4320     bfd_getb64,
   4321     bfd_getb_signed_64,
   4322     bfd_putb64,
   4323     bfd_getb32,
   4324     bfd_getb_signed_32,
   4325     bfd_putb32,
   4326     bfd_getb16,
   4327     bfd_getb_signed_16,
   4328     bfd_putb16,
   4329 
   4330     /* hdrs */
   4331     bfd_getb64,
   4332     bfd_getb_signed_64,
   4333     bfd_putb64,
   4334     bfd_getb32,
   4335     bfd_getb_signed_32,
   4336     bfd_putb32,
   4337     bfd_getb16,
   4338     bfd_getb_signed_16,
   4339     bfd_putb16,
   4340 
   4341     { /* bfd_check_format */
   4342       _bfd_dummy_target,
   4343       coff_object_p,
   4344       _bfd_xcoff_archive_p,
   4345       CORE_FILE_P
   4346     },
   4347 
   4348     { /* bfd_set_format */
   4349       bfd_false,
   4350       coff_mkobject,
   4351       _bfd_generic_mkarchive,
   4352       bfd_false
   4353     },
   4354 
   4355     {/* bfd_write_contents */
   4356       bfd_false,
   4357       coff_write_object_contents,
   4358       _bfd_xcoff_write_archive_contents,
   4359       bfd_false
   4360     },
   4361 
   4362     BFD_JUMP_TABLE_GENERIC (_bfd_xcoff),
   4363     BFD_JUMP_TABLE_COPY (_bfd_xcoff),
   4364     BFD_JUMP_TABLE_CORE (coff),
   4365     BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
   4366     BFD_JUMP_TABLE_SYMBOLS (_bfd_xcoff),
   4367     BFD_JUMP_TABLE_RELOCS (_bfd_xcoff),
   4368     BFD_JUMP_TABLE_WRITE (coff),
   4369     BFD_JUMP_TABLE_LINK (_bfd_xcoff),
   4370     BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
   4371 
   4372     /* Opposite endian version, none exists */
   4373     NULL,
   4374 
   4375     & bfd_pmac_xcoff_backend_data,
   4376   };
   4377