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