Home | History | Annotate | Line # | Download | only in bfd
coff-rs6000.c revision 1.1.1.11
      1 /* BFD back-end for IBM RS/6000 "XCOFF" files.
      2    Copyright (C) 1990-2025 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   char magic[SXCOFFARMAG];
   1507   size_t amt = SXCOFFARMAG;
   1508 
   1509   if (bfd_read (magic, amt, abfd) != amt)
   1510     {
   1511       if (bfd_get_error () != bfd_error_system_call)
   1512 	bfd_set_error (bfd_error_wrong_format);
   1513       return NULL;
   1514     }
   1515 
   1516   if (strncmp (magic, XCOFFARMAG, SXCOFFARMAG) != 0
   1517       && strncmp (magic, XCOFFARMAGBIG, SXCOFFARMAG) != 0)
   1518     {
   1519       bfd_set_error (bfd_error_wrong_format);
   1520       return NULL;
   1521     }
   1522 
   1523   amt = sizeof (struct artdata) + sizeof (struct xcoff_artdata);
   1524   bfd_ardata (abfd) = bfd_zalloc (abfd, amt);
   1525   if (bfd_ardata (abfd) == NULL)
   1526     return NULL;
   1527 
   1528   bfd_ardata (abfd)->tdata = (void *) ((struct artdata *) bfd_ardata (abfd) + 1);
   1529 
   1530   /* Now handle the two formats.  */
   1531   if (magic[1] != 'b')
   1532     {
   1533       /* This is the old format.  */
   1534       struct xcoff_ar_file_hdr hdr;
   1535 
   1536       /* Copy over the magic string.  */
   1537       memcpy (hdr.magic, magic, SXCOFFARMAG);
   1538 
   1539       /* Now read the rest of the file header.  */
   1540       amt = SIZEOF_AR_FILE_HDR - SXCOFFARMAG;
   1541       if (bfd_read (&hdr.memoff, amt, abfd) != amt)
   1542 	{
   1543 	  if (bfd_get_error () != bfd_error_system_call)
   1544 	    bfd_set_error (bfd_error_wrong_format);
   1545 	  goto error_ret;
   1546 	}
   1547 
   1548       GET_VALUE_IN_FIELD (bfd_ardata (abfd)->first_file_filepos,
   1549 			  hdr.firstmemoff, 10);
   1550 
   1551       memcpy (&x_artdata (abfd)->u.hdr, &hdr, SIZEOF_AR_FILE_HDR);
   1552     }
   1553   else
   1554     {
   1555       /* This is the new format.  */
   1556       struct xcoff_ar_file_hdr_big hdr;
   1557 
   1558       /* Copy over the magic string.  */
   1559       memcpy (hdr.magic, magic, SXCOFFARMAG);
   1560 
   1561       /* Now read the rest of the file header.  */
   1562       amt = SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG;
   1563       if (bfd_read (&hdr.memoff, amt, abfd) != amt)
   1564 	{
   1565 	  if (bfd_get_error () != bfd_error_system_call)
   1566 	    bfd_set_error (bfd_error_wrong_format);
   1567 	  goto error_ret;
   1568 	}
   1569 
   1570       bfd_ardata (abfd)->first_file_filepos = bfd_scan_vma (hdr.firstmemoff,
   1571 							    (const char **) 0,
   1572 							    10);
   1573 
   1574       memcpy (&x_artdata (abfd)->u.bhdr, &hdr, SIZEOF_AR_FILE_HDR_BIG);
   1575     }
   1576 
   1577   if (! _bfd_xcoff_slurp_armap (abfd))
   1578     {
   1579     error_ret:
   1580       bfd_release (abfd, bfd_ardata (abfd));
   1581       return NULL;
   1582     }
   1583 
   1584   return _bfd_no_cleanup;
   1585 }
   1586 
   1587 /* Track file ranges occupied by elements.  Add [START,END) to the
   1588    list of ranges and return TRUE if there is no overlap between the
   1589    new and any other element or the archive file header.  This is
   1590    aimed at preventing infinite looping on malformed archives, for
   1591    "ar" and similar which typically use code like:
   1592    .  for (last = bfd_openr_next_archived_file (archive, NULL);
   1593    .       last;
   1594    .       last = next)
   1595    .    {
   1596    .      do_something_with (last);
   1597    .      next = bfd_openr_next_archived_file (archive, last);
   1598    .      bfd_close (last);
   1599    .    }
   1600    The check implemented here is only possible due to the fact that
   1601    for XCOFF archives bfd_openr_next_archived_file is the only code
   1602    path leading to _bfd_read_ar_hdr.  _bfd_read_ar_hdr is not called
   1603    when reading the armap, nor do XCOFF archives use the extended name
   1604    scheme implemented in archive.c.
   1605 
   1606    Note that the check relies on the previous element being closed,
   1607    and there is one case where add_range might fail but I think it is
   1608    sufficently unusual that it doesn't warrant fixing:
   1609    If the loop body above called bfd_openr_next_archived_file twice
   1610    with the same arguments and the element returned is bfd_close'd
   1611    between those calls then we'll return false here for the second
   1612    call.  (For why this is so see _bfd_look_for_bfd_in_cache in
   1613    _bfd_get_elt_at_filepos, and know that bfd_close removes elements
   1614    from the cache.)  */
   1615 
   1616 static bool
   1617 add_range (bfd *abfd, ufile_ptr start, ufile_ptr end)
   1618 {
   1619   if (end <= start)
   1620     {
   1621     err:
   1622       bfd_set_error (bfd_error_malformed_archive);
   1623       return false;
   1624     }
   1625 
   1626   /* This list is kept sorted by address.  Find the highest address
   1627      range on the list that ends before the new range starts.  Exit
   1628      the loop with that range in LO, and the mext higher range in HI.  */
   1629   struct ar_ranges *hi = &x_artdata (abfd)->ranges;
   1630   struct ar_ranges *lo = NULL;
   1631   while (hi && hi->end <= start)
   1632     {
   1633       lo = hi;
   1634       hi = hi->next;
   1635     }
   1636 
   1637   if (lo == NULL)
   1638     /* Start overlaps the file header or elements adjacent to it.  */
   1639     goto err;
   1640 
   1641   if (hi && hi->start < end)
   1642     /* Overlap with another element.  */
   1643     goto err;
   1644 
   1645   /* A zero size element with a one char name is this big.  */
   1646   unsigned min_elt = x_artdata (abfd)->ar_hdr_size + 2 + SXCOFFARFMAG;
   1647   if (start - lo->end < min_elt)
   1648     {
   1649       /* Merge into an existing range.  */
   1650       lo->end = end;
   1651       if (hi && hi->start - end < min_elt)
   1652 	{
   1653 	  /* In fact, we can merge two ranges.  */
   1654 	  lo->end = hi->end;
   1655 	  lo->next = hi->next;
   1656 	  /* The list uses bfd_alloc so don't free HI.  */
   1657 	}
   1658       return true;
   1659     }
   1660 
   1661   if (hi && hi->start - end < min_elt)
   1662     {
   1663       /* Merge into an existing range.  */
   1664       hi->start = start;
   1665       return true;
   1666     }
   1667 
   1668   struct ar_ranges *newr = bfd_alloc (abfd, sizeof (*newr));
   1669   if (newr == NULL)
   1670     return false;
   1671   newr->start = start;
   1672   newr->end = end;
   1673   newr->next = hi;
   1674   lo->next = newr;
   1675   return true;
   1676 }
   1677 
   1678 /* Read the archive header in an XCOFF archive.  */
   1679 
   1680 void *
   1681 _bfd_xcoff_read_ar_hdr (bfd *abfd)
   1682 {
   1683   bfd_size_type namlen;
   1684   struct areltdata *ret;
   1685   bfd_size_type amt;
   1686   ufile_ptr start = abfd->where;
   1687 
   1688   if (! xcoff_big_format_p (abfd))
   1689     {
   1690       struct xcoff_ar_hdr hdr;
   1691       struct xcoff_ar_hdr *hdrp;
   1692 
   1693       if (bfd_read (&hdr, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR)
   1694 	return NULL;
   1695 
   1696       GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
   1697       if (namlen > bfd_get_file_size (abfd))
   1698 	return NULL;
   1699       amt = sizeof (struct areltdata) + SIZEOF_AR_HDR + namlen + 1;
   1700       ret = (struct areltdata *) bfd_malloc (amt);
   1701       if (ret == NULL)
   1702 	return ret;
   1703 
   1704       hdrp = (struct xcoff_ar_hdr *) (ret + 1);
   1705       memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
   1706       if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR, namlen, abfd) != namlen)
   1707 	{
   1708 	  free (ret);
   1709 	  return NULL;
   1710 	}
   1711       ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
   1712 
   1713       ret->arch_header = (char *) hdrp;
   1714       GET_VALUE_IN_FIELD (ret->parsed_size, hdr.size, 10);
   1715       ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
   1716     }
   1717   else
   1718     {
   1719       struct xcoff_ar_hdr_big hdr;
   1720       struct xcoff_ar_hdr_big *hdrp;
   1721 
   1722       if (bfd_read (&hdr, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG)
   1723 	return NULL;
   1724 
   1725       GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
   1726       if (namlen > bfd_get_file_size (abfd))
   1727 	return NULL;
   1728       amt = sizeof (struct areltdata) + SIZEOF_AR_HDR_BIG + namlen + 1;
   1729       ret = (struct areltdata *) bfd_malloc (amt);
   1730       if (ret == NULL)
   1731 	return ret;
   1732 
   1733       hdrp = (struct xcoff_ar_hdr_big *) (ret + 1);
   1734       memcpy (hdrp, &hdr, SIZEOF_AR_HDR_BIG);
   1735       if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR_BIG, namlen, abfd) != namlen)
   1736 	{
   1737 	  free (ret);
   1738 	  return NULL;
   1739 	}
   1740       ((char *) hdrp)[SIZEOF_AR_HDR_BIG + namlen] = '\0';
   1741 
   1742       ret->arch_header = (char *) hdrp;
   1743       GET_VALUE_IN_FIELD (ret->parsed_size, hdr.size, 10);
   1744       ret->filename = (char *) hdrp + SIZEOF_AR_HDR_BIG;
   1745     }
   1746 
   1747   /* Size occupied by the header above that covered in the fixed
   1748      SIZEOF_AR_HDR or SIZEOF_AR_HDR_BIG.  */
   1749   ret->extra_size = namlen + (namlen & 1) + SXCOFFARFMAG;
   1750 
   1751   /* Skip over the XCOFFARFMAG at the end of the file name.  */
   1752   if (bfd_seek (abfd, (namlen & 1) + SXCOFFARFMAG, SEEK_CUR) != 0
   1753       || !add_range (abfd, start, abfd->where + ret->parsed_size))
   1754     {
   1755       free (ret);
   1756       return NULL;
   1757     }
   1758 
   1759   return ret;
   1760 }
   1761 
   1762 /* Open the next element in an XCOFF archive.  */
   1763 
   1764 bfd *
   1765 _bfd_xcoff_openr_next_archived_file (bfd *archive, bfd *last_file)
   1766 {
   1767   ufile_ptr filestart;
   1768 
   1769   if (x_artdata (archive) == NULL)
   1770     {
   1771       bfd_set_error (bfd_error_invalid_operation);
   1772       return NULL;
   1773     }
   1774 
   1775   if (! xcoff_big_format_p (archive))
   1776     {
   1777       if (last_file == NULL)
   1778 	{
   1779 	  /* If we are scanning over elements twice in an open archive,
   1780 	     which can happen in gdb after a fork, ensure we start the
   1781 	     second scan with clean ranges.  */
   1782 	  x_artdata (archive)->ranges.start = 0;
   1783 	  x_artdata (archive)->ranges.end = SIZEOF_AR_FILE_HDR;
   1784 	  x_artdata (archive)->ranges.next = NULL;
   1785 	  x_artdata (archive)->ar_hdr_size = SIZEOF_AR_HDR;
   1786 	  filestart = bfd_ardata (archive)->first_file_filepos;
   1787 	}
   1788       else
   1789 	GET_VALUE_IN_FIELD (filestart, arch_xhdr (last_file)->nextoff, 10);
   1790 
   1791       if (filestart == 0
   1792 	  || EQ_VALUE_IN_FIELD (filestart,
   1793 				x_artdata (archive)->u.hdr.memoff, 10)
   1794 	  || EQ_VALUE_IN_FIELD (filestart,
   1795 				x_artdata (archive)->u.hdr.symoff, 10))
   1796 	{
   1797 	  bfd_set_error (bfd_error_no_more_archived_files);
   1798 	  return NULL;
   1799 	}
   1800     }
   1801   else
   1802     {
   1803       if (last_file == NULL)
   1804 	{
   1805 	  x_artdata (archive)->ranges.start = 0;
   1806 	  x_artdata (archive)->ranges.end = SIZEOF_AR_FILE_HDR_BIG;
   1807 	  x_artdata (archive)->ranges.next = NULL;
   1808 	  x_artdata (archive)->ar_hdr_size = SIZEOF_AR_HDR_BIG;
   1809 	  filestart = bfd_ardata (archive)->first_file_filepos;
   1810 	}
   1811       else
   1812 	GET_VALUE_IN_FIELD (filestart, arch_xhdr_big (last_file)->nextoff, 10);
   1813 
   1814       if (filestart == 0
   1815 	  || EQ_VALUE_IN_FIELD (filestart,
   1816 				x_artdata (archive)->u.bhdr.memoff, 10)
   1817 	  || EQ_VALUE_IN_FIELD (filestart,
   1818 				x_artdata (archive)->u.bhdr.symoff, 10))
   1819 	{
   1820 	  bfd_set_error (bfd_error_no_more_archived_files);
   1821 	  return NULL;
   1822 	}
   1823     }
   1824 
   1825   /* Check that we aren't pointing back at the last element.  This is
   1826      necessary depite the add_range checking in _bfd_xcoff_read_ar_hdr
   1827      because archive.c leaves the last element open and thus in the
   1828      archive element cache until the next element is opened.  */
   1829   if (last_file != NULL)
   1830     {
   1831       ufile_ptr laststart = last_file->proxy_origin;
   1832       laststart -= x_artdata (archive)->ar_hdr_size;
   1833       laststart -= arch_eltdata (last_file)->extra_size;
   1834       if (filestart == laststart)
   1835 	{
   1836 	  bfd_set_error (bfd_error_malformed_archive);
   1837 	  return NULL;
   1838 	}
   1839     }
   1840 
   1841   return _bfd_get_elt_at_filepos (archive, filestart, NULL);
   1842 }
   1843 
   1844 /* Stat an element in an XCOFF archive.  */
   1845 
   1846 int
   1847 _bfd_xcoff_stat_arch_elt (bfd *abfd, struct stat *s)
   1848 {
   1849   if (abfd->arelt_data == NULL)
   1850     {
   1851       bfd_set_error (bfd_error_invalid_operation);
   1852       return -1;
   1853     }
   1854 
   1855   if (! xcoff_big_format_p (abfd->my_archive))
   1856     {
   1857       struct xcoff_ar_hdr *hdrp = arch_xhdr (abfd);
   1858 
   1859       GET_VALUE_IN_FIELD (s->st_mtime, hdrp->date, 10);
   1860       GET_VALUE_IN_FIELD (s->st_uid, hdrp->uid, 10);
   1861       GET_VALUE_IN_FIELD (s->st_gid, hdrp->gid, 10);
   1862       GET_VALUE_IN_FIELD (s->st_mode, hdrp->mode, 8);
   1863       s->st_size = arch_eltdata (abfd)->parsed_size;
   1864     }
   1865   else
   1866     {
   1867       struct xcoff_ar_hdr_big *hdrp = arch_xhdr_big (abfd);
   1868 
   1869       GET_VALUE_IN_FIELD (s->st_mtime, hdrp->date, 10);
   1870       GET_VALUE_IN_FIELD (s->st_uid, hdrp->uid, 10);
   1871       GET_VALUE_IN_FIELD (s->st_gid, hdrp->gid, 10);
   1872       GET_VALUE_IN_FIELD (s->st_mode, hdrp->mode, 8);
   1873       s->st_size = arch_eltdata (abfd)->parsed_size;
   1874     }
   1875 
   1876   return 0;
   1877 }
   1878 
   1879 /* Normalize a file name for inclusion in an archive.  */
   1880 
   1881 static const char *
   1882 normalize_filename (bfd *abfd)
   1883 {
   1884   const char *file;
   1885   const char *filename;
   1886 
   1887   file = bfd_get_filename (abfd);
   1888   filename = strrchr (file, '/');
   1889   if (filename != NULL)
   1890     filename++;
   1891   else
   1892     filename = file;
   1893   return filename;
   1894 }
   1895 
   1896 /* Write out an XCOFF armap.  */
   1897 
   1898 static bool
   1899 xcoff_write_armap_old (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
   1900 		       struct orl *map, unsigned int orl_count, int stridx)
   1901 {
   1902   struct archive_iterator iterator;
   1903   struct xcoff_ar_hdr hdr;
   1904   char *p;
   1905   unsigned char buf[4];
   1906   unsigned int i;
   1907 
   1908   memset (&hdr, 0, sizeof hdr);
   1909   sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
   1910   sprintf (hdr.nextoff, "%d", 0);
   1911   memcpy (hdr.prevoff, x_artdata (abfd)->u.hdr.memoff,
   1912 	  XCOFFARMAG_ELEMENT_SIZE);
   1913   sprintf (hdr.date, "%d", 0);
   1914   sprintf (hdr.uid, "%d", 0);
   1915   sprintf (hdr.gid, "%d", 0);
   1916   sprintf (hdr.mode, "%d", 0);
   1917   sprintf (hdr.namlen, "%d", 0);
   1918 
   1919   /* We need spaces, not null bytes, in the header.  */
   1920   for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
   1921     if (*p == '\0')
   1922       *p = ' ';
   1923 
   1924   if (bfd_write (&hdr, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
   1925       || bfd_write (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG)
   1926     return false;
   1927 
   1928   H_PUT_32 (abfd, orl_count, buf);
   1929   if (bfd_write (buf, 4, abfd) != 4)
   1930     return false;
   1931 
   1932   i = 0;
   1933   archive_iterator_begin (&iterator, abfd);
   1934   while (i < orl_count && archive_iterator_next (&iterator))
   1935     while (map[i].u.abfd == iterator.current.member)
   1936       {
   1937 	H_PUT_32 (abfd, iterator.current.offset, buf);
   1938 	if (bfd_write (buf, 4, abfd) != 4)
   1939 	  return false;
   1940 	++i;
   1941       }
   1942 
   1943   for (i = 0; i < orl_count; i++)
   1944     {
   1945       const char *name;
   1946       size_t namlen;
   1947 
   1948       name = *map[i].name;
   1949       namlen = strlen (name);
   1950       if (bfd_write (name, namlen + 1, abfd) != namlen + 1)
   1951 	return false;
   1952     }
   1953 
   1954   if ((stridx & 1) != 0)
   1955     {
   1956       char b;
   1957 
   1958       b = '\0';
   1959       if (bfd_write (&b, 1, abfd) != 1)
   1960 	return false;
   1961     }
   1962 
   1963   return true;
   1964 }
   1965 
   1966 static char buff20[XCOFFARMAGBIG_ELEMENT_SIZE + 1];
   1967 #define FMT20  "%-20" PRId64
   1968 #define FMT12  "%-12d"
   1969 #define FMT12_OCTAL  "%-12o"
   1970 #define FMT4  "%-4d"
   1971 #define PRINT20(d, v) \
   1972   sprintf (buff20, FMT20, (uint64_t) (v)), \
   1973   memcpy ((void *) (d), buff20, 20)
   1974 
   1975 #define PRINT12(d, v) \
   1976   sprintf (buff20, FMT12, (int)(v)), \
   1977   memcpy ((void *) (d), buff20, 12)
   1978 
   1979 #define PRINT12_OCTAL(d, v) \
   1980   sprintf (buff20, FMT12_OCTAL, (unsigned int)(v)), \
   1981   memcpy ((void *) (d), buff20, 12)
   1982 
   1983 #define PRINT4(d, v) \
   1984   sprintf (buff20, FMT4, (int)(v)), \
   1985   memcpy ((void *) (d), buff20, 4)
   1986 
   1987 #define READ20(d, v) \
   1988   buff20[20] = 0, \
   1989   memcpy (buff20, (d), 20), \
   1990   (v) = bfd_scan_vma (buff20, (const char **) NULL, 10)
   1991 
   1992 static bool
   1993 do_pad (bfd *abfd, unsigned int number)
   1994 {
   1995   bfd_byte b = 0;
   1996 
   1997   /* Limit pad to <= 4096.  */
   1998   if (number > 4096)
   1999     return false;
   2000 
   2001   while (number--)
   2002     if (bfd_write (&b, 1, abfd) != 1)
   2003       return false;
   2004 
   2005   return true;
   2006 }
   2007 
   2008 static bool
   2009 do_copy (bfd *out_bfd, bfd *in_bfd)
   2010 {
   2011   bfd_size_type remaining;
   2012   bfd_byte buffer[8 * 1024];
   2013 
   2014   if (bfd_seek (in_bfd, 0, SEEK_SET) != 0)
   2015     return false;
   2016 
   2017   remaining = arelt_size (in_bfd);
   2018 
   2019   while (remaining >= sizeof (buffer))
   2020     {
   2021       if (bfd_read (buffer, sizeof (buffer), in_bfd) != sizeof (buffer)
   2022 	  || bfd_write (buffer, sizeof (buffer), out_bfd) != sizeof (buffer))
   2023 	return false;
   2024 
   2025       remaining -= sizeof (buffer);
   2026     }
   2027 
   2028   if (remaining)
   2029     {
   2030       if (bfd_read (buffer, remaining, in_bfd) != remaining
   2031 	  || bfd_write (buffer, remaining, out_bfd) != remaining)
   2032 	return false;
   2033     }
   2034 
   2035   return true;
   2036 }
   2037 
   2038 static bool
   2039 xcoff_write_armap_big (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
   2040 		       struct orl *map, unsigned int orl_count, int stridx)
   2041 {
   2042   struct archive_iterator iterator;
   2043   struct xcoff_ar_file_hdr_big *fhdr;
   2044   bfd_vma i, sym_32, sym_64, str_32, str_64;
   2045   const bfd_arch_info_type *arch_info;
   2046   bfd *current_bfd;
   2047   size_t string_length;
   2048   file_ptr nextoff, prevoff;
   2049 
   2050   /* First, we look through the symbols and work out which are
   2051      from 32-bit objects and which from 64-bit ones.  */
   2052   sym_32 = sym_64 = str_32 = str_64 = 0;
   2053 
   2054   i = 0;
   2055   for (current_bfd = abfd->archive_head;
   2056        current_bfd != NULL && i < orl_count;
   2057        current_bfd = current_bfd->archive_next)
   2058     {
   2059       arch_info = bfd_get_arch_info (current_bfd);
   2060       while (map[i].u.abfd == current_bfd)
   2061 	{
   2062 	  string_length = strlen (*map[i].name) + 1;
   2063 	  if (arch_info->bits_per_address == 64)
   2064 	    {
   2065 	      sym_64++;
   2066 	      str_64 += string_length;
   2067 	    }
   2068 	  else
   2069 	    {
   2070 	      sym_32++;
   2071 	      str_32 += string_length;
   2072 	    }
   2073 	  i++;
   2074 	}
   2075     }
   2076 
   2077   /* A quick sanity check... */
   2078   BFD_ASSERT (sym_64 + sym_32 == orl_count);
   2079   /* Explicit cast to int for compiler.  */
   2080   BFD_ASSERT ((int)(str_64 + str_32) == stridx);
   2081 
   2082   fhdr = &x_artdata (abfd)->u.bhdr;
   2083 
   2084   /* xcoff_write_archive_contents_big passes nextoff in symoff. */
   2085   READ20 (fhdr->memoff, prevoff);
   2086   READ20 (fhdr->symoff, nextoff);
   2087 
   2088   BFD_ASSERT (nextoff == bfd_tell (abfd));
   2089 
   2090   /* Write out the symbol table.
   2091      Layout :
   2092 
   2093      standard big archive header
   2094      0x0000		      ar_size	[0x14]
   2095      0x0014		      ar_nxtmem [0x14]
   2096      0x0028		      ar_prvmem [0x14]
   2097      0x003C		      ar_date	[0x0C]
   2098      0x0048		      ar_uid	[0x0C]
   2099      0x0054		      ar_gid	[0x0C]
   2100      0x0060		      ar_mod	[0x0C]
   2101      0x006C		      ar_namelen[0x04]
   2102      0x0070		      ar_fmag	[SXCOFFARFMAG]
   2103 
   2104      Symbol table
   2105      0x0072		      num_syms	[0x08], binary
   2106      0x0078		      offsets	[0x08 * num_syms], binary
   2107      0x0086 + 0x08 * num_syms names	[??]
   2108      ??			      pad to even bytes.
   2109   */
   2110 
   2111   if (sym_32)
   2112     {
   2113       struct xcoff_ar_hdr_big *hdr;
   2114       char *symbol_table;
   2115       char *st;
   2116 
   2117       bfd_vma symbol_table_size =
   2118 	SIZEOF_AR_HDR_BIG
   2119 	+ SXCOFFARFMAG
   2120 	+ 8
   2121 	+ 8 * sym_32
   2122 	+ str_32 + (str_32 & 1);
   2123 
   2124       symbol_table = bfd_zmalloc (symbol_table_size);
   2125       if (symbol_table == NULL)
   2126 	return false;
   2127 
   2128       hdr = (struct xcoff_ar_hdr_big *) symbol_table;
   2129 
   2130       PRINT20 (hdr->size, 8 + 8 * sym_32 + str_32 + (str_32 & 1));
   2131 
   2132       if (sym_64)
   2133 	PRINT20 (hdr->nextoff, nextoff + symbol_table_size);
   2134       else
   2135 	PRINT20 (hdr->nextoff, 0);
   2136 
   2137       PRINT20 (hdr->prevoff, prevoff);
   2138       PRINT12 (hdr->date, 0);
   2139       PRINT12 (hdr->uid, 0);
   2140       PRINT12 (hdr->gid, 0);
   2141       PRINT12 (hdr->mode, 0);
   2142       PRINT4 (hdr->namlen, 0) ;
   2143 
   2144       st = symbol_table + SIZEOF_AR_HDR_BIG;
   2145       memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
   2146       st += SXCOFFARFMAG;
   2147 
   2148       bfd_h_put_64 (abfd, sym_32, st);
   2149       st += 8;
   2150 
   2151       /* loop over the 32 bit offsets */
   2152       i = 0;
   2153       archive_iterator_begin (&iterator, abfd);
   2154       while (i < orl_count && archive_iterator_next (&iterator))
   2155 	{
   2156 	  arch_info = bfd_get_arch_info (iterator.current.member);
   2157 	  while (map[i].u.abfd == iterator.current.member)
   2158 	    {
   2159 	      if (arch_info->bits_per_address == 32)
   2160 		{
   2161 		  bfd_h_put_64 (abfd, iterator.current.offset, st);
   2162 		  st += 8;
   2163 		}
   2164 	      i++;
   2165 	    }
   2166 	}
   2167 
   2168       /* loop over the 32 bit symbol names */
   2169       i = 0;
   2170       for (current_bfd = abfd->archive_head;
   2171 	   current_bfd != NULL && i < orl_count;
   2172 	   current_bfd = current_bfd->archive_next)
   2173 	{
   2174 	  arch_info = bfd_get_arch_info (current_bfd);
   2175 	  while (map[i].u.abfd == current_bfd)
   2176 	    {
   2177 	      if (arch_info->bits_per_address == 32)
   2178 		{
   2179 		  string_length = sprintf (st, "%s", *map[i].name);
   2180 		  st += string_length + 1;
   2181 		}
   2182 	      i++;
   2183 	    }
   2184 	}
   2185 
   2186       if (bfd_write (symbol_table, symbol_table_size, abfd)
   2187 	  != symbol_table_size)
   2188 	{
   2189 	  free (symbol_table);
   2190 	  return false;
   2191 	}
   2192       free (symbol_table);
   2193 
   2194       prevoff = nextoff;
   2195       nextoff = nextoff + symbol_table_size;
   2196     }
   2197   else
   2198     PRINT20 (fhdr->symoff, 0);
   2199 
   2200   if (sym_64)
   2201     {
   2202       struct xcoff_ar_hdr_big *hdr;
   2203       char *symbol_table;
   2204       char *st;
   2205 
   2206       bfd_vma symbol_table_size =
   2207 	SIZEOF_AR_HDR_BIG
   2208 	+ SXCOFFARFMAG
   2209 	+ 8
   2210 	+ 8 * sym_64
   2211 	+ str_64 + (str_64 & 1);
   2212 
   2213       symbol_table = bfd_zmalloc (symbol_table_size);
   2214       if (symbol_table == NULL)
   2215 	return false;
   2216 
   2217       hdr = (struct xcoff_ar_hdr_big *) symbol_table;
   2218 
   2219       PRINT20 (hdr->size, 8 + 8 * sym_64 + str_64 + (str_64 & 1));
   2220       PRINT20 (hdr->nextoff, 0);
   2221       PRINT20 (hdr->prevoff, prevoff);
   2222       PRINT12 (hdr->date, 0);
   2223       PRINT12 (hdr->uid, 0);
   2224       PRINT12 (hdr->gid, 0);
   2225       PRINT12 (hdr->mode, 0);
   2226       PRINT4 (hdr->namlen, 0);
   2227 
   2228       st = symbol_table + SIZEOF_AR_HDR_BIG;
   2229       memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
   2230       st += SXCOFFARFMAG;
   2231 
   2232       bfd_h_put_64 (abfd, sym_64, st);
   2233       st += 8;
   2234 
   2235       /* loop over the 64 bit offsets */
   2236       i = 0;
   2237       archive_iterator_begin (&iterator, abfd);
   2238       while (i < orl_count && archive_iterator_next (&iterator))
   2239 	{
   2240 	  arch_info = bfd_get_arch_info (iterator.current.member);
   2241 	  while (map[i].u.abfd == iterator.current.member)
   2242 	    {
   2243 	      if (arch_info->bits_per_address == 64)
   2244 		{
   2245 		  bfd_h_put_64 (abfd, iterator.current.offset, st);
   2246 		  st += 8;
   2247 		}
   2248 	      i++;
   2249 	    }
   2250 	}
   2251 
   2252       /* loop over the 64 bit symbol names */
   2253       i = 0;
   2254       for (current_bfd = abfd->archive_head;
   2255 	   current_bfd != NULL && i < orl_count;
   2256 	   current_bfd = current_bfd->archive_next)
   2257 	{
   2258 	  arch_info = bfd_get_arch_info (current_bfd);
   2259 	  while (map[i].u.abfd == current_bfd)
   2260 	    {
   2261 	      if (arch_info->bits_per_address == 64)
   2262 		{
   2263 		  string_length = sprintf (st, "%s", *map[i].name);
   2264 		  st += string_length + 1;
   2265 		}
   2266 	      i++;
   2267 	    }
   2268 	}
   2269 
   2270       if (bfd_write (symbol_table, symbol_table_size, abfd)
   2271 	  != symbol_table_size)
   2272 	{
   2273 	  free (symbol_table);
   2274 	  return false;
   2275 	}
   2276       free (symbol_table);
   2277 
   2278       PRINT20 (fhdr->symoff64, nextoff);
   2279     }
   2280   else
   2281     PRINT20 (fhdr->symoff64, 0);
   2282 
   2283   return true;
   2284 }
   2285 
   2286 bool
   2287 _bfd_xcoff_write_armap (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
   2288 			struct orl *map, unsigned int orl_count, int stridx)
   2289 {
   2290   if (! xcoff_big_format_p (abfd))
   2291     return xcoff_write_armap_old (abfd, elength, map, orl_count, stridx);
   2292   else
   2293     return xcoff_write_armap_big (abfd, elength, map, orl_count, stridx);
   2294 }
   2295 
   2296 /* Write out an XCOFF archive.  We always write an entire archive,
   2297    rather than fussing with the freelist and so forth.  */
   2298 
   2299 static bool
   2300 xcoff_write_archive_contents_old (bfd *abfd)
   2301 {
   2302   struct archive_iterator iterator;
   2303   struct xcoff_artdata xtdata;
   2304   struct xcoff_ar_file_hdr *fhdr = &xtdata.u.hdr;
   2305   bfd_size_type count;
   2306   bfd_size_type total_namlen;
   2307   file_ptr *offsets;
   2308   bool makemap;
   2309   bool hasobjects;
   2310   file_ptr prevoff, nextoff;
   2311   bfd *sub;
   2312   size_t i;
   2313   struct xcoff_ar_hdr ahdr;
   2314   bfd_size_type size;
   2315   char *p;
   2316   char decbuf[XCOFFARMAG_ELEMENT_SIZE + 1];
   2317 
   2318   memset (&xtdata, 0, sizeof (xtdata));
   2319   memcpy (fhdr->magic, XCOFFARMAG, SXCOFFARMAG);
   2320   sprintf (fhdr->firstmemoff, "%zu", SIZEOF_AR_FILE_HDR);
   2321   sprintf (fhdr->freeoff, "%d", 0);
   2322 
   2323   count = 0;
   2324   total_namlen = 0;
   2325   for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
   2326     {
   2327       ++count;
   2328       total_namlen += strlen (normalize_filename (sub)) + 1;
   2329       if (sub->arelt_data == NULL)
   2330 	{
   2331 	  sub->arelt_data = bfd_zmalloc (sizeof (struct areltdata));
   2332 	  if (sub->arelt_data == NULL)
   2333 	    return false;
   2334 	}
   2335       if (arch_xhdr (sub) == NULL)
   2336 	{
   2337 	  struct xcoff_ar_hdr *ahdrp;
   2338 	  struct stat s;
   2339 
   2340 	  if ((sub->flags & BFD_IN_MEMORY) != 0)
   2341 	    {
   2342 	      /* Assume we just "made" the member, and fake it.  */
   2343 	      struct bfd_in_memory *bim
   2344 		= (struct bfd_in_memory *) sub->iostream;
   2345 	      time (&s.st_mtime);
   2346 	      s.st_uid = getuid ();
   2347 	      s.st_gid = getgid ();
   2348 	      s.st_mode = 0644;
   2349 	      s.st_size = bim->size;
   2350 	    }
   2351 	  else if (stat (bfd_get_filename (sub), &s) != 0)
   2352 	    {
   2353 	      bfd_set_input_error (sub, bfd_error_system_call);
   2354 	      return false;
   2355 	    }
   2356 
   2357 	  if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
   2358 	    {
   2359 	      s.st_mtime = 0;
   2360 	      s.st_uid = 0;
   2361 	      s.st_gid = 0;
   2362 	      s.st_mode = 0644;
   2363 	    }
   2364 
   2365 	  ahdrp = bfd_zalloc (sub, sizeof (*ahdrp));
   2366 	  if (ahdrp == NULL)
   2367 	    return false;
   2368 
   2369 	  sprintf (ahdrp->size, "%ld", (long) s.st_size);
   2370 	  sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
   2371 	  sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
   2372 	  sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
   2373 	  sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
   2374 
   2375 	  arch_eltdata (sub)->arch_header = (char *) ahdrp;
   2376 	  arch_eltdata (sub)->parsed_size = s.st_size;
   2377 	}
   2378     }
   2379   offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
   2380   if (offsets == NULL)
   2381     return false;
   2382 
   2383   if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
   2384     return false;
   2385 
   2386   makemap = bfd_has_map (abfd);
   2387   hasobjects = false;
   2388   prevoff = 0;
   2389   for (archive_iterator_begin (&iterator, abfd), i = 0;
   2390        archive_iterator_next (&iterator);
   2391        i++)
   2392     {
   2393       bfd_size_type namlen;
   2394       struct xcoff_ar_hdr *ahdrp;
   2395 
   2396       if (makemap && ! hasobjects)
   2397 	{
   2398 	  if (bfd_check_format (iterator.current.member, bfd_object))
   2399 	    hasobjects = true;
   2400 	}
   2401 
   2402       ahdrp = arch_xhdr (iterator.current.member);
   2403       sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
   2404       sprintf (ahdrp->namlen, "%ld", (long) iterator.current.namlen);
   2405       sprintf (ahdrp->nextoff, "%ld", (long) iterator.next.offset);
   2406 
   2407       /* We need spaces, not null bytes, in the header.  */
   2408       for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
   2409 	if (*p == '\0')
   2410 	  *p = ' ';
   2411 
   2412       if (!do_pad (abfd, iterator.current.leading_padding))
   2413 	return false;
   2414 
   2415       BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
   2416       namlen = iterator.current.padded_namlen;
   2417       if (bfd_write (ahdrp, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
   2418 	  || bfd_write (iterator.current.name, namlen, abfd) != namlen
   2419 	  || bfd_write (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
   2420 	  || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
   2421 	  || !do_copy (abfd, iterator.current.member)
   2422 	  || !do_pad (abfd, iterator.current.trailing_padding))
   2423 	return false;
   2424 
   2425       offsets[i] = iterator.current.offset;
   2426       prevoff = iterator.current.offset;
   2427     }
   2428 
   2429   sprintf (fhdr->lastmemoff, "%ld", (long) prevoff);
   2430 
   2431   /* Write out the member table.  */
   2432 
   2433   nextoff = iterator.next.offset;
   2434   BFD_ASSERT (nextoff == bfd_tell (abfd));
   2435   sprintf (fhdr->memoff, "%ld", (long) nextoff);
   2436 
   2437   memset (&ahdr, 0, sizeof ahdr);
   2438   sprintf (ahdr.size, "%ld", (long) (XCOFFARMAG_ELEMENT_SIZE
   2439 				     + count * XCOFFARMAG_ELEMENT_SIZE
   2440 				     + total_namlen));
   2441   sprintf (ahdr.prevoff, "%ld", (long) prevoff);
   2442   sprintf (ahdr.date, "%d", 0);
   2443   sprintf (ahdr.uid, "%d", 0);
   2444   sprintf (ahdr.gid, "%d", 0);
   2445   sprintf (ahdr.mode, "%d", 0);
   2446   sprintf (ahdr.namlen, "%d", 0);
   2447 
   2448   size = (SIZEOF_AR_HDR
   2449 	  + XCOFFARMAG_ELEMENT_SIZE
   2450 	  + count * XCOFFARMAG_ELEMENT_SIZE
   2451 	  + total_namlen
   2452 	  + SXCOFFARFMAG);
   2453 
   2454   prevoff = nextoff;
   2455   nextoff += size + (size & 1);
   2456 
   2457   if (makemap && hasobjects)
   2458     sprintf (ahdr.nextoff, "%ld", (long) nextoff);
   2459   else
   2460     sprintf (ahdr.nextoff, "%d", 0);
   2461 
   2462   /* We need spaces, not null bytes, in the header.  */
   2463   for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
   2464     if (*p == '\0')
   2465       *p = ' ';
   2466 
   2467   if ((bfd_write (&ahdr, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR)
   2468       || bfd_write (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG)
   2469     return false;
   2470 
   2471   sprintf (decbuf, "%-12ld", (long) count);
   2472   if (bfd_write (decbuf, XCOFFARMAG_ELEMENT_SIZE, abfd)
   2473       != XCOFFARMAG_ELEMENT_SIZE)
   2474     return false;
   2475   for (i = 0; i < (size_t) count; i++)
   2476     {
   2477       sprintf (decbuf, "%-12ld", (long) offsets[i]);
   2478       if (bfd_write (decbuf, XCOFFARMAG_ELEMENT_SIZE, abfd)
   2479 	  != XCOFFARMAG_ELEMENT_SIZE)
   2480 	return false;
   2481     }
   2482   for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
   2483     {
   2484       const char *name;
   2485       bfd_size_type namlen;
   2486 
   2487       name = normalize_filename (sub);
   2488       namlen = strlen (name);
   2489       if (bfd_write (name, namlen + 1, abfd) != namlen + 1)
   2490 	return false;
   2491     }
   2492 
   2493   if (! do_pad (abfd, size & 1))
   2494     return false;
   2495 
   2496   /* Write out the armap, if appropriate.  */
   2497   if (! makemap || ! hasobjects)
   2498     sprintf (fhdr->symoff, "%d", 0);
   2499   else
   2500     {
   2501       BFD_ASSERT (nextoff == bfd_tell (abfd));
   2502       sprintf (fhdr->symoff, "%ld", (long) nextoff);
   2503       bfd_ardata (abfd)->tdata = &xtdata;
   2504       bool ret = _bfd_compute_and_write_armap (abfd, 0);
   2505       bfd_ardata (abfd)->tdata = NULL;
   2506       if (!ret)
   2507 	return false;
   2508     }
   2509 
   2510   /* Write out the archive file header.  */
   2511 
   2512   /* We need spaces, not null bytes, in the header.  */
   2513   for (p = (char *) fhdr; p < (char *) fhdr + SIZEOF_AR_FILE_HDR; p++)
   2514     if (*p == '\0')
   2515       *p = ' ';
   2516 
   2517   if (bfd_seek (abfd, 0, SEEK_SET) != 0
   2518       || (bfd_write (fhdr, SIZEOF_AR_FILE_HDR, abfd) != SIZEOF_AR_FILE_HDR))
   2519     return false;
   2520 
   2521   return true;
   2522 }
   2523 
   2524 static bool
   2525 xcoff_write_archive_contents_big (bfd *abfd)
   2526 {
   2527   struct xcoff_artdata xtdata;
   2528   struct xcoff_ar_file_hdr_big *fhdr = &xtdata.u.bhdr;
   2529   bfd_size_type count;
   2530   bfd_size_type total_namlen;
   2531   file_ptr *offsets;
   2532   bool makemap;
   2533   bool hasobjects;
   2534   file_ptr prevoff, nextoff;
   2535   bfd *current_bfd;
   2536   size_t i;
   2537   struct xcoff_ar_hdr_big *hdr;
   2538   bfd_size_type size;
   2539   char *member_table, *mt;
   2540   bfd_vma member_table_size;
   2541   struct archive_iterator iterator;
   2542 
   2543   memset (&xtdata, 0, sizeof (xtdata));
   2544   memcpy (fhdr->magic, XCOFFARMAGBIG, SXCOFFARMAG);
   2545 
   2546   if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR_BIG, SEEK_SET) != 0)
   2547     return false;
   2548 
   2549   /* Calculate count and total_namlen.  */
   2550   makemap = bfd_has_map (abfd);
   2551   hasobjects = false;
   2552   for (current_bfd = abfd->archive_head, count = 0, total_namlen = 0;
   2553        current_bfd != NULL;
   2554        current_bfd = current_bfd->archive_next, count++)
   2555     {
   2556       total_namlen += strlen (normalize_filename (current_bfd)) + 1;
   2557 
   2558       if (makemap
   2559 	  && ! hasobjects
   2560 	  && bfd_check_format (current_bfd, bfd_object))
   2561 	hasobjects = true;
   2562 
   2563       if (current_bfd->arelt_data == NULL)
   2564 	{
   2565 	  size = sizeof (struct areltdata);
   2566 	  current_bfd->arelt_data = bfd_zmalloc (size);
   2567 	  if (current_bfd->arelt_data == NULL)
   2568 	    return false;
   2569 	}
   2570 
   2571       if (arch_xhdr_big (current_bfd) == NULL)
   2572 	{
   2573 	  struct xcoff_ar_hdr_big *ahdrp;
   2574 	  struct stat s;
   2575 
   2576 	  if ((current_bfd->flags & BFD_IN_MEMORY) != 0)
   2577 	    {
   2578 	      /* Assume we just "made" the member, and fake it.  */
   2579 	      struct bfd_in_memory *bim
   2580 		= (struct bfd_in_memory *) current_bfd->iostream;
   2581 	      time (&s.st_mtime);
   2582 	      s.st_uid = getuid ();
   2583 	      s.st_gid = getgid ();
   2584 	      s.st_mode = 0644;
   2585 	      s.st_size = bim->size;
   2586 	    }
   2587 	  else if (stat (bfd_get_filename (current_bfd), &s) != 0)
   2588 	    {
   2589 	      bfd_set_input_error (current_bfd, bfd_error_system_call);
   2590 	      return false;
   2591 	    }
   2592 
   2593 	  if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
   2594 	    {
   2595 	      s.st_mtime = 0;
   2596 	      s.st_uid = 0;
   2597 	      s.st_gid = 0;
   2598 	      s.st_mode = 0644;
   2599 	    }
   2600 
   2601 	  ahdrp = bfd_zalloc (current_bfd, sizeof (*ahdrp));
   2602 	  if (ahdrp == NULL)
   2603 	    return false;
   2604 
   2605 	  PRINT20 (ahdrp->size, s.st_size);
   2606 	  PRINT12 (ahdrp->date, s.st_mtime);
   2607 	  PRINT12 (ahdrp->uid,  s.st_uid);
   2608 	  PRINT12 (ahdrp->gid,  s.st_gid);
   2609 	  PRINT12_OCTAL (ahdrp->mode, s.st_mode);
   2610 
   2611 	  arch_eltdata (current_bfd)->arch_header = (char *) ahdrp;
   2612 	  arch_eltdata (current_bfd)->parsed_size = s.st_size;
   2613 	}
   2614     }
   2615 
   2616   offsets = NULL;
   2617   if (count)
   2618     {
   2619       offsets = (file_ptr *) bfd_malloc (count * sizeof (file_ptr));
   2620       if (offsets == NULL)
   2621 	return false;
   2622     }
   2623 
   2624   prevoff = 0;
   2625   for (archive_iterator_begin (&iterator, abfd), i = 0;
   2626        archive_iterator_next (&iterator);
   2627        i++)
   2628     {
   2629       bfd_size_type namlen;
   2630       struct xcoff_ar_hdr_big *ahdrp;
   2631 
   2632       ahdrp = arch_xhdr_big (iterator.current.member);
   2633       PRINT20 (ahdrp->prevoff, prevoff);
   2634       PRINT4 (ahdrp->namlen, iterator.current.namlen);
   2635       PRINT20 (ahdrp->nextoff, iterator.next.offset);
   2636 
   2637       if (!do_pad (abfd, iterator.current.leading_padding))
   2638 	{
   2639 	  free (offsets);
   2640 	  return false;
   2641 	}
   2642 
   2643       BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
   2644       namlen = iterator.current.padded_namlen;
   2645       if (bfd_write (ahdrp, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG
   2646 	  || bfd_write (iterator.current.name, namlen, abfd) != namlen
   2647 	  || bfd_write (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
   2648 	  || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
   2649 	  || !do_copy (abfd, iterator.current.member)
   2650 	  || !do_pad (abfd, iterator.current.trailing_padding))
   2651 	{
   2652 	  free (offsets);
   2653 	  return false;
   2654 	}
   2655 
   2656       offsets[i] = iterator.current.offset;
   2657       prevoff = iterator.current.offset;
   2658     }
   2659 
   2660   if (count)
   2661     {
   2662       PRINT20 (fhdr->firstmemoff, offsets[0]);
   2663       PRINT20 (fhdr->lastmemoff, prevoff);
   2664     }
   2665 
   2666   /* Write out the member table.
   2667      Layout :
   2668 
   2669      standard big archive header
   2670      0x0000		      ar_size	[0x14]
   2671      0x0014		      ar_nxtmem [0x14]
   2672      0x0028		      ar_prvmem [0x14]
   2673      0x003C		      ar_date	[0x0C]
   2674      0x0048		      ar_uid	[0x0C]
   2675      0x0054		      ar_gid	[0x0C]
   2676      0x0060		      ar_mod	[0x0C]
   2677      0x006C		      ar_namelen[0x04]
   2678      0x0070		      ar_fmag	[0x02]
   2679 
   2680      Member table
   2681      0x0072		      count	[0x14]
   2682      0x0086		      offsets	[0x14 * counts]
   2683      0x0086 + 0x14 * counts   names	[??]
   2684      ??			      pad to even bytes.
   2685    */
   2686 
   2687   nextoff = iterator.next.offset;
   2688   BFD_ASSERT (nextoff == bfd_tell (abfd));
   2689 
   2690   member_table_size = (SIZEOF_AR_HDR_BIG
   2691 		       + SXCOFFARFMAG
   2692 		       + XCOFFARMAGBIG_ELEMENT_SIZE
   2693 		       + count * XCOFFARMAGBIG_ELEMENT_SIZE
   2694 		       + total_namlen);
   2695 
   2696   member_table_size += member_table_size & 1;
   2697   member_table = bfd_zmalloc (member_table_size);
   2698   if (member_table == NULL)
   2699     {
   2700       free (offsets);
   2701       return false;
   2702     }
   2703 
   2704   hdr = (struct xcoff_ar_hdr_big *) member_table;
   2705 
   2706   PRINT20 (hdr->size, (XCOFFARMAGBIG_ELEMENT_SIZE
   2707 		       + count * XCOFFARMAGBIG_ELEMENT_SIZE
   2708 		       + total_namlen + (total_namlen & 1)));
   2709   if (makemap && hasobjects)
   2710     PRINT20 (hdr->nextoff, nextoff + member_table_size);
   2711   else
   2712     PRINT20 (hdr->nextoff, 0);
   2713   PRINT20 (hdr->prevoff, prevoff);
   2714   PRINT12 (hdr->date, 0);
   2715   PRINT12 (hdr->uid, 0);
   2716   PRINT12 (hdr->gid, 0);
   2717   PRINT12 (hdr->mode, 0);
   2718   PRINT4 (hdr->namlen, 0);
   2719 
   2720   mt = member_table + SIZEOF_AR_HDR_BIG;
   2721   memcpy (mt, XCOFFARFMAG, SXCOFFARFMAG);
   2722   mt += SXCOFFARFMAG;
   2723 
   2724   PRINT20 (mt, count);
   2725   mt += XCOFFARMAGBIG_ELEMENT_SIZE;
   2726   for (i = 0; i < (size_t) count; i++)
   2727     {
   2728       PRINT20 (mt, offsets[i]);
   2729       mt += XCOFFARMAGBIG_ELEMENT_SIZE;
   2730     }
   2731 
   2732   if (count)
   2733     {
   2734       free (offsets);
   2735       offsets = NULL;
   2736     }
   2737 
   2738   for (current_bfd = abfd->archive_head;
   2739        current_bfd != NULL;
   2740        current_bfd = current_bfd->archive_next)
   2741     {
   2742       const char *name;
   2743       size_t namlen;
   2744 
   2745       name = normalize_filename (current_bfd);
   2746       namlen = sprintf (mt, "%s", name);
   2747       mt += namlen + 1;
   2748     }
   2749 
   2750   if (bfd_write (member_table, member_table_size, abfd) != member_table_size)
   2751     return false;
   2752 
   2753   free (member_table);
   2754 
   2755   PRINT20 (fhdr->memoff, nextoff);
   2756 
   2757   prevoff = nextoff;
   2758   nextoff += member_table_size;
   2759 
   2760   /* Write out the armap, if appropriate.  */
   2761 
   2762   if (! makemap || ! hasobjects)
   2763     PRINT20 (fhdr->symoff, 0);
   2764   else
   2765     {
   2766       BFD_ASSERT (nextoff == bfd_tell (abfd));
   2767 
   2768       /* Save nextoff in fhdr->symoff so the armap routine can use it.  */
   2769       PRINT20 (fhdr->symoff, nextoff);
   2770 
   2771       bfd_ardata (abfd)->tdata = &xtdata;
   2772       bool ret = _bfd_compute_and_write_armap (abfd, 0);
   2773       bfd_ardata (abfd)->tdata = NULL;
   2774       if (!ret)
   2775 	return false;
   2776     }
   2777 
   2778   /* Write out the archive file header.  */
   2779 
   2780   if (bfd_seek (abfd, 0, SEEK_SET) != 0
   2781       || (bfd_write (fhdr, SIZEOF_AR_FILE_HDR_BIG, abfd)
   2782 	  != SIZEOF_AR_FILE_HDR_BIG))
   2783     return false;
   2784 
   2785   return true;
   2786 }
   2787 
   2788 bool
   2789 _bfd_xcoff_write_archive_contents (bfd *abfd)
   2790 {
   2791   if (! xcoff_big_format_p (abfd))
   2792     return xcoff_write_archive_contents_old (abfd);
   2793   else
   2794     return xcoff_write_archive_contents_big (abfd);
   2795 }
   2796 
   2797 /* We can't use the usual coff_sizeof_headers routine, because AIX
   2799    always uses an a.out header.  */
   2800 
   2801 int
   2802 _bfd_xcoff_sizeof_headers (bfd *abfd,
   2803 			   struct bfd_link_info *info ATTRIBUTE_UNUSED)
   2804 {
   2805   int size;
   2806 
   2807   size = FILHSZ;
   2808   if (xcoff_data (abfd)->full_aouthdr)
   2809     size += AOUTSZ;
   2810   else
   2811     size += SMALL_AOUTSZ;
   2812   size += abfd->section_count * SCNHSZ;
   2813 
   2814   if (info->strip != strip_all)
   2815     {
   2816       /* There can be additional sections just for dealing with overflow in
   2817 	 reloc and lineno counts. But the numbers of relocs and lineno aren't
   2818 	 known when bfd_sizeof_headers is called, so we compute them by
   2819 	 summing the numbers from input sections.  */
   2820       struct nbr_reloc_lineno
   2821       {
   2822 	unsigned int reloc_count;
   2823 	unsigned int lineno_count;
   2824       };
   2825       struct nbr_reloc_lineno *n_rl;
   2826       bfd *sub;
   2827       unsigned int max_index;
   2828       asection *s;
   2829 
   2830       /* Although the number of sections is known, the maximum value of
   2831 	 section->index isn't (because some sections may have been removed).
   2832 	 Don't try to renumber sections, just compute the upper bound.  */
   2833       max_index = 0;
   2834       for (s = abfd->sections; s != NULL; s = s->next)
   2835 	if (s->index > max_index)
   2836 	  max_index = s->index;
   2837 
   2838       /* Allocate the per section counters. It could be possible to use a
   2839 	 preallocated array as the number of sections is limited on XCOFF,
   2840 	 but this creates a maintainance issue.  */
   2841       n_rl = bfd_zmalloc ((max_index + 1) * sizeof (*n_rl));
   2842       if (n_rl == NULL)
   2843 	return -1;
   2844 
   2845       /* Sum.  */
   2846       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   2847 	for (s = sub->sections; s != NULL; s = s->next)
   2848 	  if (s->output_section->owner == abfd
   2849 	      && !bfd_section_removed_from_list (abfd, s->output_section))
   2850 	    {
   2851 	      struct nbr_reloc_lineno *e = &n_rl[s->output_section->index];
   2852 	      e->reloc_count += s->reloc_count;
   2853 	      e->lineno_count += s->lineno_count;
   2854 	    }
   2855 
   2856       /* Add the size of a section for each section with an overflow.  */
   2857       for (s = abfd->sections; s != NULL; s = s->next)
   2858 	{
   2859 	  struct nbr_reloc_lineno *e = &n_rl[s->index];
   2860 
   2861 	  if (e->reloc_count >= 0xffff
   2862 	      || (e->lineno_count >= 0xffff && info->strip != strip_debugger))
   2863 	    size += SCNHSZ;
   2864 	}
   2865 
   2866       free (n_rl);
   2867     }
   2868 
   2869   return size;
   2870 }
   2871 
   2872 /* Routines to swap information in the XCOFF .loader section.  If we
   2874    ever need to write an XCOFF loader, this stuff will need to be
   2875    moved to another file shared by the linker (which XCOFF calls the
   2876    ``binder'') and the loader.  */
   2877 
   2878 /* Swap in the ldhdr structure.  */
   2879 
   2880 static void
   2881 xcoff_swap_ldhdr_in (bfd *abfd, const void * s, struct internal_ldhdr *dst)
   2882 {
   2883   const struct external_ldhdr *src = (const struct external_ldhdr *) s;
   2884 
   2885   dst->l_version = bfd_get_32 (abfd, src->l_version);
   2886   dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
   2887   dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
   2888   dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
   2889   dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
   2890   dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
   2891   dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
   2892   dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
   2893 }
   2894 
   2895 /* Swap out the ldhdr structure.  */
   2896 
   2897 static void
   2898 xcoff_swap_ldhdr_out (bfd *abfd, const struct internal_ldhdr *src, void * d)
   2899 {
   2900   struct external_ldhdr *dst = (struct external_ldhdr *) d;
   2901 
   2902   bfd_put_32 (abfd, (bfd_vma) src->l_version, dst->l_version);
   2903   bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
   2904   bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
   2905   bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
   2906   bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
   2907   bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
   2908   bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
   2909   bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
   2910 }
   2911 
   2912 /* Swap in the ldsym structure.  */
   2913 
   2914 static void
   2915 xcoff_swap_ldsym_in (bfd *abfd, const void * s, struct internal_ldsym *dst)
   2916 {
   2917   const struct external_ldsym *src = (const struct external_ldsym *) s;
   2918 
   2919   if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0) {
   2920     memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
   2921   } else {
   2922     dst->_l._l_l._l_zeroes = 0;
   2923     dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
   2924   }
   2925   dst->l_value = bfd_get_32 (abfd, src->l_value);
   2926   dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
   2927   dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
   2928   dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
   2929   dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
   2930   dst->l_parm = bfd_get_32 (abfd, src->l_parm);
   2931 }
   2932 
   2933 /* Swap out the ldsym structure.  */
   2934 
   2935 static void
   2936 xcoff_swap_ldsym_out (bfd *abfd, const struct internal_ldsym *src, void * d)
   2937 {
   2938   struct external_ldsym *dst = (struct external_ldsym *) d;
   2939 
   2940   if (src->_l._l_l._l_zeroes != 0)
   2941     memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
   2942   else
   2943     {
   2944       bfd_put_32 (abfd, (bfd_vma) 0, dst->_l._l_l._l_zeroes);
   2945       bfd_put_32 (abfd, (bfd_vma) src->_l._l_l._l_offset,
   2946 		  dst->_l._l_l._l_offset);
   2947     }
   2948   bfd_put_32 (abfd, src->l_value, dst->l_value);
   2949   bfd_put_16 (abfd, (bfd_vma) src->l_scnum, dst->l_scnum);
   2950   bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
   2951   bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
   2952   bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
   2953   bfd_put_32 (abfd, src->l_parm, dst->l_parm);
   2954 }
   2955 
   2956 static void
   2957 xcoff_swap_reloc_in (bfd *abfd, void * s, void * d)
   2958 {
   2959   struct external_reloc *src = (struct external_reloc *) s;
   2960   struct internal_reloc *dst = (struct internal_reloc *) d;
   2961 
   2962   memset (dst, 0, sizeof (struct internal_reloc));
   2963 
   2964   dst->r_vaddr = bfd_get_32 (abfd, src->r_vaddr);
   2965   dst->r_symndx = bfd_get_32 (abfd, src->r_symndx);
   2966   dst->r_size = bfd_get_8 (abfd, src->r_size);
   2967   dst->r_type = bfd_get_8 (abfd, src->r_type);
   2968 }
   2969 
   2970 static unsigned int
   2971 xcoff_swap_reloc_out (bfd *abfd, void * s, void * d)
   2972 {
   2973   struct internal_reloc *src = (struct internal_reloc *) s;
   2974   struct external_reloc *dst = (struct external_reloc *) d;
   2975 
   2976   bfd_put_32 (abfd, src->r_vaddr, dst->r_vaddr);
   2977   bfd_put_32 (abfd, src->r_symndx, dst->r_symndx);
   2978   bfd_put_8 (abfd, src->r_type, dst->r_type);
   2979   bfd_put_8 (abfd, src->r_size, dst->r_size);
   2980 
   2981   return bfd_coff_relsz (abfd);
   2982 }
   2983 
   2984 /* Swap in the ldrel structure.  */
   2985 
   2986 static void
   2987 xcoff_swap_ldrel_in (bfd *abfd, const void * s, struct internal_ldrel *dst)
   2988 {
   2989   const struct external_ldrel *src = (const struct external_ldrel *) s;
   2990 
   2991   dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
   2992   dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
   2993   dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
   2994   dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
   2995 }
   2996 
   2997 /* Swap out the ldrel structure.  */
   2998 
   2999 static void
   3000 xcoff_swap_ldrel_out (bfd *abfd, const struct internal_ldrel *src, void * d)
   3001 {
   3002   struct external_ldrel *dst = (struct external_ldrel *) d;
   3003 
   3004   bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
   3005   bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
   3006   bfd_put_16 (abfd, (bfd_vma) src->l_rtype, dst->l_rtype);
   3007   bfd_put_16 (abfd, (bfd_vma) src->l_rsecnm, dst->l_rsecnm);
   3008 }
   3009 
   3010 
   3012 bool
   3013 xcoff_reloc_type_noop (bfd *input_bfd ATTRIBUTE_UNUSED,
   3014 		       asection *input_section ATTRIBUTE_UNUSED,
   3015 		       bfd *output_bfd ATTRIBUTE_UNUSED,
   3016 		       struct internal_reloc *rel ATTRIBUTE_UNUSED,
   3017 		       struct internal_syment *sym ATTRIBUTE_UNUSED,
   3018 		       struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
   3019 		       bfd_vma val ATTRIBUTE_UNUSED,
   3020 		       bfd_vma addend ATTRIBUTE_UNUSED,
   3021 		       bfd_vma *relocation ATTRIBUTE_UNUSED,
   3022 		       bfd_byte *contents ATTRIBUTE_UNUSED,
   3023 		       struct bfd_link_info *info ATTRIBUTE_UNUSED)
   3024 {
   3025   return true;
   3026 }
   3027 
   3028 bool
   3029 xcoff_reloc_type_fail (bfd *input_bfd,
   3030 		       asection *input_section ATTRIBUTE_UNUSED,
   3031 		       bfd *output_bfd ATTRIBUTE_UNUSED,
   3032 		       struct internal_reloc *rel,
   3033 		       struct internal_syment *sym ATTRIBUTE_UNUSED,
   3034 		       struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
   3035 		       bfd_vma val ATTRIBUTE_UNUSED,
   3036 		       bfd_vma addend ATTRIBUTE_UNUSED,
   3037 		       bfd_vma *relocation ATTRIBUTE_UNUSED,
   3038 		       bfd_byte *contents ATTRIBUTE_UNUSED,
   3039 		       struct bfd_link_info *info ATTRIBUTE_UNUSED)
   3040 {
   3041   _bfd_error_handler
   3042     /* xgettext: c-format */
   3043     (_("%pB: unsupported relocation type %#x"),
   3044      input_bfd, (unsigned int) rel->r_type);
   3045   bfd_set_error (bfd_error_bad_value);
   3046   return false;
   3047 }
   3048 
   3049 bool
   3050 xcoff_reloc_type_pos (bfd *input_bfd ATTRIBUTE_UNUSED,
   3051 		      asection *input_section ATTRIBUTE_UNUSED,
   3052 		      bfd *output_bfd ATTRIBUTE_UNUSED,
   3053 		      struct internal_reloc *rel ATTRIBUTE_UNUSED,
   3054 		      struct internal_syment *sym ATTRIBUTE_UNUSED,
   3055 		      struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
   3056 		      bfd_vma val,
   3057 		      bfd_vma addend,
   3058 		      bfd_vma *relocation,
   3059 		      bfd_byte *contents ATTRIBUTE_UNUSED,
   3060 		      struct bfd_link_info *info ATTRIBUTE_UNUSED)
   3061 {
   3062   *relocation = val + addend;
   3063   return true;
   3064 }
   3065 
   3066 bool
   3067 xcoff_reloc_type_neg (bfd *input_bfd ATTRIBUTE_UNUSED,
   3068 		      asection *input_section ATTRIBUTE_UNUSED,
   3069 		      bfd *output_bfd ATTRIBUTE_UNUSED,
   3070 		      struct internal_reloc *rel ATTRIBUTE_UNUSED,
   3071 		      struct internal_syment *sym ATTRIBUTE_UNUSED,
   3072 		      struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
   3073 		      bfd_vma val,
   3074 		      bfd_vma addend,
   3075 		      bfd_vma *relocation,
   3076 		      bfd_byte *contents ATTRIBUTE_UNUSED,
   3077 		      struct bfd_link_info *info ATTRIBUTE_UNUSED)
   3078 {
   3079   *relocation = - val - addend;
   3080   return true;
   3081 }
   3082 
   3083 bool
   3084 xcoff_reloc_type_rel (bfd *input_bfd ATTRIBUTE_UNUSED,
   3085 		      asection *input_section,
   3086 		      bfd *output_bfd ATTRIBUTE_UNUSED,
   3087 		      struct internal_reloc *rel ATTRIBUTE_UNUSED,
   3088 		      struct internal_syment *sym ATTRIBUTE_UNUSED,
   3089 		      struct reloc_howto_struct *howto,
   3090 		      bfd_vma val,
   3091 		      bfd_vma addend,
   3092 		      bfd_vma *relocation,
   3093 		      bfd_byte *contents ATTRIBUTE_UNUSED,
   3094 		      struct bfd_link_info *info ATTRIBUTE_UNUSED)
   3095 {
   3096   howto->pc_relative = true;
   3097 
   3098   /* A PC relative reloc includes the section address.  */
   3099   addend += input_section->vma;
   3100 
   3101   *relocation = val + addend;
   3102   *relocation -= (input_section->output_section->vma
   3103 		  + input_section->output_offset);
   3104   return true;
   3105 }
   3106 
   3107 bool
   3108 xcoff_reloc_type_toc (bfd *input_bfd,
   3109 		      asection *input_section ATTRIBUTE_UNUSED,
   3110 		      bfd *output_bfd,
   3111 		      struct internal_reloc *rel,
   3112 		      struct internal_syment *sym ATTRIBUTE_UNUSED,
   3113 		      struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
   3114 		      bfd_vma val,
   3115 		      bfd_vma addend ATTRIBUTE_UNUSED,
   3116 		      bfd_vma *relocation,
   3117 		      bfd_byte *contents ATTRIBUTE_UNUSED,
   3118 		      struct bfd_link_info *info ATTRIBUTE_UNUSED)
   3119 {
   3120   struct xcoff_link_hash_entry *h;
   3121 
   3122   if (0 > rel->r_symndx)
   3123     return false;
   3124 
   3125   h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
   3126 
   3127   if (h != NULL && h->smclas != XMC_TD)
   3128     {
   3129       if (h->toc_section == NULL)
   3130 	{
   3131 	  _bfd_error_handler
   3132 	    /* xgettext: c-format */
   3133 	    (_("%pB: TOC reloc at %#" PRIx64 " to symbol `%s' with no TOC entry"),
   3134 	     input_bfd, (uint64_t) rel->r_vaddr, h->root.root.string);
   3135 	  bfd_set_error (bfd_error_bad_value);
   3136 	  return false;
   3137 	}
   3138 
   3139       BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
   3140       val = (h->toc_section->output_section->vma
   3141 	      + h->toc_section->output_offset);
   3142     }
   3143 
   3144   /* We can't use the preexisting value written down by the
   3145      assembly, as R_TOCU needs to be adjusted when the final
   3146      R_TOCL value is signed.  */
   3147   *relocation = val - xcoff_data (output_bfd)->toc;
   3148 
   3149   if (rel->r_type == R_TOCU)
   3150     *relocation = ((*relocation + 0x8000) >> 16) & 0xffff;
   3151   if (rel->r_type == R_TOCL)
   3152     *relocation = *relocation & 0x0000ffff;
   3153 
   3154   return true;
   3155 }
   3156 
   3157 bool
   3158 xcoff_reloc_type_ba (bfd *input_bfd ATTRIBUTE_UNUSED,
   3159 		     asection *input_section ATTRIBUTE_UNUSED,
   3160 		     bfd *output_bfd ATTRIBUTE_UNUSED,
   3161 		     struct internal_reloc *rel ATTRIBUTE_UNUSED,
   3162 		     struct internal_syment *sym ATTRIBUTE_UNUSED,
   3163 		     struct reloc_howto_struct *howto,
   3164 		     bfd_vma val,
   3165 		     bfd_vma addend,
   3166 		     bfd_vma *relocation,
   3167 		     bfd_byte *contents ATTRIBUTE_UNUSED,
   3168 		     struct bfd_link_info *info ATTRIBUTE_UNUSED)
   3169 {
   3170   howto->src_mask &= ~3;
   3171   howto->dst_mask = howto->src_mask;
   3172 
   3173   *relocation = val + addend;
   3174 
   3175   return true;
   3176 }
   3177 
   3178 static bool
   3179 xcoff_reloc_type_br (bfd *input_bfd,
   3180 		     asection *input_section,
   3181 		     bfd *output_bfd ATTRIBUTE_UNUSED,
   3182 		     struct internal_reloc *rel,
   3183 		     struct internal_syment *sym ATTRIBUTE_UNUSED,
   3184 		     struct reloc_howto_struct *howto,
   3185 		     bfd_vma val,
   3186 		     bfd_vma addend,
   3187 		     bfd_vma *relocation,
   3188 		     bfd_byte *contents,
   3189 		     struct bfd_link_info *info)
   3190 {
   3191   struct xcoff_link_hash_entry *h;
   3192   bfd_vma section_offset;
   3193   struct xcoff_stub_hash_entry *stub_entry = NULL;
   3194   enum xcoff_stub_type stub_type;
   3195 
   3196   if (0 > rel->r_symndx)
   3197     return false;
   3198 
   3199   h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
   3200   section_offset = rel->r_vaddr - input_section->vma;
   3201 
   3202   /* If we see an R_BR or R_RBR reloc which is jumping to global
   3203      linkage code, and it is followed by an appropriate cror nop
   3204      instruction, we replace the cror with lwz r2,20(r1).  This
   3205      restores the TOC after the glink code.  Contrariwise, if the
   3206      call is followed by a lwz r2,20(r1), but the call is not
   3207      going to global linkage code, we can replace the load with a
   3208      cror.  */
   3209   if (NULL != h
   3210       && (bfd_link_hash_defined == h->root.type
   3211 	  || bfd_link_hash_defweak == h->root.type)
   3212       && section_offset + 8 <= input_section->size)
   3213     {
   3214       bfd_byte *pnext;
   3215       unsigned long next;
   3216 
   3217       pnext = contents + section_offset + 4;
   3218       next = bfd_get_32 (input_bfd, pnext);
   3219 
   3220       /* The _ptrgl function is magic.  It is used by the AIX
   3221 	 compiler to call a function through a pointer.  */
   3222       if (h->smclas == XMC_GL || strcmp (h->root.root.string, "._ptrgl") == 0)
   3223 	{
   3224 	  if (next == 0x4def7b82			/* cror 15,15,15 */
   3225 	      || next == 0x4ffffb82			/* cror 31,31,31 */
   3226 	      || next == 0x60000000)			/* ori r0,r0,0 */
   3227 	    bfd_put_32 (input_bfd, 0x80410014, pnext);	/* lwz r2,20(r1) */
   3228 
   3229 	}
   3230       else
   3231 	{
   3232 	  if (next == 0x80410014)			/* lwz r2,20(r1) */
   3233 	    bfd_put_32 (input_bfd, 0x60000000, pnext);	/* ori r0,r0,0 */
   3234 	}
   3235     }
   3236   else if (NULL != h && bfd_link_hash_undefined == h->root.type)
   3237     {
   3238       /* Normally, this relocation is against a defined symbol.  In the
   3239 	 case where this is a partial link and the output section offset
   3240 	 is greater than 2^25, the linker will return an invalid error
   3241 	 message that the relocation has been truncated.  Yes it has been
   3242 	 truncated but no it not important.  For this case, disable the
   3243 	 overflow checking. */
   3244 
   3245       howto->complain_on_overflow = complain_overflow_dont;
   3246     }
   3247 
   3248   /* Check if a stub is needed.  */
   3249   stub_type = bfd_xcoff_type_of_stub (input_section, rel, val, h);
   3250   if (stub_type != xcoff_stub_none)
   3251     {
   3252       asection *stub_csect;
   3253 
   3254       stub_entry = bfd_xcoff_get_stub_entry (input_section, h, info);
   3255       if (stub_entry == NULL)
   3256 	{
   3257 	  _bfd_error_handler (_("Unable to find the stub entry targeting %s"),
   3258 			      h->root.root.string);
   3259 	  bfd_set_error (bfd_error_bad_value);
   3260 	  return false;
   3261 	}
   3262 
   3263       stub_csect = stub_entry->hcsect->root.u.def.section;
   3264       val = (stub_entry->stub_offset
   3265 	     + stub_csect->output_section->vma
   3266 	     + stub_csect->output_offset);
   3267     }
   3268 
   3269   /* The original PC-relative relocation is biased by -r_vaddr, so adding
   3270      the value below will give the absolute target address.  */
   3271   *relocation = val + addend + rel->r_vaddr;
   3272 
   3273   howto->src_mask &= ~3;
   3274   howto->dst_mask = howto->src_mask;
   3275 
   3276   if (h != NULL
   3277       && (h->root.type == bfd_link_hash_defined
   3278 	  || h->root.type == bfd_link_hash_defweak)
   3279       && bfd_is_abs_section (h->root.u.def.section)
   3280       && section_offset + 4 <= input_section->size)
   3281     {
   3282       bfd_byte *ptr;
   3283       bfd_vma insn;
   3284 
   3285       /* Turn the relative branch into an absolute one by setting the
   3286 	 AA bit.  */
   3287       ptr = contents + section_offset;
   3288       insn = bfd_get_32 (input_bfd, ptr);
   3289       insn |= 2;
   3290       bfd_put_32 (input_bfd, insn, ptr);
   3291 
   3292       /* Make the howto absolute too.  */
   3293       howto->pc_relative = false;
   3294       howto->complain_on_overflow = complain_overflow_bitfield;
   3295     }
   3296   else
   3297     {
   3298       /* Use a PC-relative howto and subtract the instruction's address
   3299 	 from the target address we calculated above.  */
   3300       howto->pc_relative = true;
   3301       *relocation -= (input_section->output_section->vma
   3302 		      + input_section->output_offset
   3303 		      + section_offset);
   3304     }
   3305   return true;
   3306 }
   3307 
   3308 bool
   3309 xcoff_reloc_type_crel (bfd *input_bfd ATTRIBUTE_UNUSED,
   3310 		       asection *input_section,
   3311 		       bfd *output_bfd ATTRIBUTE_UNUSED,
   3312 		       struct internal_reloc *rel ATTRIBUTE_UNUSED,
   3313 		       struct internal_syment *sym ATTRIBUTE_UNUSED,
   3314 		       struct reloc_howto_struct *howto,
   3315 		       bfd_vma val ATTRIBUTE_UNUSED,
   3316 		       bfd_vma addend,
   3317 		       bfd_vma *relocation,
   3318 		       bfd_byte *contents ATTRIBUTE_UNUSED,
   3319 		       struct bfd_link_info *info ATTRIBUTE_UNUSED)
   3320 {
   3321   howto->pc_relative = true;
   3322   howto->src_mask &= ~3;
   3323   howto->dst_mask = howto->src_mask;
   3324 
   3325   /* A PC relative reloc includes the section address.  */
   3326   addend += input_section->vma;
   3327 
   3328   *relocation = val + addend;
   3329   *relocation -= (input_section->output_section->vma
   3330 		  + input_section->output_offset);
   3331   return true;
   3332 }
   3333 
   3334 bool
   3335 xcoff_reloc_type_tls (bfd *input_bfd ATTRIBUTE_UNUSED,
   3336 		      asection *input_section ATTRIBUTE_UNUSED,
   3337 		      bfd *output_bfd ATTRIBUTE_UNUSED,
   3338 		      struct internal_reloc *rel ATTRIBUTE_UNUSED,
   3339 		      struct internal_syment *sym ATTRIBUTE_UNUSED,
   3340 		      struct reloc_howto_struct *howto,
   3341 		      bfd_vma val,
   3342 		      bfd_vma addend,
   3343 		      bfd_vma *relocation,
   3344 		      bfd_byte *contents ATTRIBUTE_UNUSED,
   3345 		      struct bfd_link_info *info ATTRIBUTE_UNUSED)
   3346 {
   3347   struct xcoff_link_hash_entry *h;
   3348 
   3349   if (0 > rel->r_symndx)
   3350     return false;
   3351 
   3352   h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
   3353 
   3354   /* R_TLSML is handled by the loader but must be from a
   3355      TOC entry targeting itslef.  This is already verified in
   3356      xcoff_link_add_symbols.
   3357      The value must be 0.  */
   3358   if (howto->type == R_TLSML)
   3359     {
   3360       *relocation = 0;
   3361       return true;
   3362     }
   3363 
   3364   /* The target symbol should always be available even if it's not
   3365      exported.  */
   3366   BFD_ASSERT (h != NULL);
   3367 
   3368   /* TLS relocations must target a TLS symbol.  */
   3369   if (h->smclas != XMC_TL && h->smclas != XMC_UL)
   3370     {
   3371       _bfd_error_handler
   3372 	(_("%pB: TLS relocation at 0x%" PRIx64 " over non-TLS symbol %s (0x%x)\n"),
   3373 	 input_bfd, (uint64_t) rel->r_vaddr, h->root.root.string, h->smclas);
   3374       return false;
   3375     }
   3376 
   3377   /* Local TLS relocations must target a local symbol, ie
   3378      non-imported. */
   3379   if ((rel->r_type == R_TLS_LD || rel->r_type == R_TLS_LE)
   3380       && (((h->flags & XCOFF_DEF_REGULAR) == 0
   3381        && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
   3382 	  || (h->flags & XCOFF_IMPORT) != 0))
   3383     {
   3384       _bfd_error_handler
   3385 	(_("%pB: TLS local relocation at 0x%" PRIx64 " over imported symbol %s\n"),
   3386 	 input_bfd, (uint64_t) rel->r_vaddr, h->root.root.string);
   3387       return false;
   3388     }
   3389 
   3390   /* R_TLSM are relocations used by the loader.
   3391      The value must be 0.  */
   3392   if (howto->type == R_TLSM)
   3393     {
   3394       *relocation = 0;
   3395       return true;
   3396     }
   3397 
   3398   /* Other TLS relocations aims to put offsets from TLS pointers
   3399      starting at -0x7c00 (or -0x7800 in XCOFF64).  It becomes a
   3400      simple R_POS relocation as long as .tdata and .tbss addresses
   3401      start at the same value. This is done in aix ld scripts.
   3402      TODO: implement optimization when tls size is < 62K.  */
   3403   *relocation = val + addend;
   3404 
   3405   return true;
   3406 }
   3407 
   3408 static bool
   3409 xcoff_complain_overflow_dont_func (bfd *input_bfd ATTRIBUTE_UNUSED,
   3410 				   bfd_vma val ATTRIBUTE_UNUSED,
   3411 				   bfd_vma relocation ATTRIBUTE_UNUSED,
   3412 				   struct reloc_howto_struct *
   3413 				      howto ATTRIBUTE_UNUSED)
   3414 {
   3415   return false;
   3416 }
   3417 
   3418 static bool
   3419 xcoff_complain_overflow_bitfield_func (bfd *input_bfd,
   3420 				       bfd_vma val,
   3421 				       bfd_vma relocation,
   3422 				       struct reloc_howto_struct *howto)
   3423 {
   3424   bfd_vma fieldmask, signmask, ss;
   3425   bfd_vma a, b, sum;
   3426 
   3427   /* Get the values to be added together.  For signed and unsigned
   3428      relocations, we assume that all values should be truncated to
   3429      the size of an address.  For bitfields, all the bits matter.
   3430      See also bfd_check_overflow.  */
   3431   fieldmask = N_ONES (howto->bitsize);
   3432   a = relocation;
   3433   b = val & howto->src_mask;
   3434 
   3435   /* Much like unsigned, except no trimming with addrmask.  In
   3436      addition, the sum overflows if there is a carry out of
   3437      the bfd_vma, i.e., the sum is less than either input
   3438      operand.  */
   3439   a >>= howto->rightshift;
   3440   b >>= howto->bitpos;
   3441 
   3442   /* Bitfields are sometimes used for signed numbers; for
   3443      example, a 13-bit field sometimes represents values in
   3444      0..8191 and sometimes represents values in -4096..4095.
   3445      If the field is signed and a is -4095 (0x1001) and b is
   3446      -1 (0x1fff), the sum is -4096 (0x1000), but (0x1001 +
   3447      0x1fff is 0x3000).  It's not clear how to handle this
   3448      everywhere, since there is not way to know how many bits
   3449      are significant in the relocation, but the original code
   3450      assumed that it was fully sign extended, and we will keep
   3451      that assumption.  */
   3452   signmask = (fieldmask >> 1) + 1;
   3453 
   3454   if ((a & ~ fieldmask) != 0)
   3455     {
   3456       /* Some bits out of the field are set.  This might not
   3457 	 be a problem: if this is a signed bitfield, it is OK
   3458 	 iff all the high bits are set, including the sign
   3459 	 bit.  We'll try setting all but the most significant
   3460 	 bit in the original relocation value: if this is all
   3461 	 ones, we are OK, assuming a signed bitfield.  */
   3462       ss = (signmask << howto->rightshift) - 1;
   3463       if ((ss | relocation) != ~ (bfd_vma) 0)
   3464 	return true;
   3465       a &= fieldmask;
   3466     }
   3467 
   3468   /* We just assume (b & ~ fieldmask) == 0.  */
   3469 
   3470   /* We explicitly permit wrap around if this relocation
   3471      covers the high bit of an address.  The Linux kernel
   3472      relies on it, and it is the only way to write assembler
   3473      code which can run when loaded at a location 0x80000000
   3474      away from the location at which it is linked.  */
   3475   if ((unsigned) howto->bitsize + howto->rightshift
   3476       == bfd_arch_bits_per_address (input_bfd))
   3477     return false;
   3478 
   3479   sum = a + b;
   3480   if (sum < a || (sum & ~ fieldmask) != 0)
   3481     {
   3482       /* There was a carry out, or the field overflow.  Test
   3483 	 for signed operands again.  Here is the overflow test
   3484 	 is as for complain_overflow_signed.  */
   3485       if (((~ (a ^ b)) & (a ^ sum)) & signmask)
   3486 	return true;
   3487     }
   3488 
   3489   return false;
   3490 }
   3491 
   3492 static bool
   3493 xcoff_complain_overflow_signed_func (bfd *input_bfd,
   3494 				     bfd_vma val,
   3495 				     bfd_vma relocation,
   3496 				     struct reloc_howto_struct *howto)
   3497 {
   3498   bfd_vma addrmask, fieldmask, signmask, ss;
   3499   bfd_vma a, b, sum;
   3500 
   3501   /* Get the values to be added together.  For signed and unsigned
   3502      relocations, we assume that all values should be truncated to
   3503      the size of an address.  For bitfields, all the bits matter.
   3504      See also bfd_check_overflow.  */
   3505   fieldmask = N_ONES (howto->bitsize);
   3506   addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
   3507   a = relocation;
   3508   b = val & howto->src_mask;
   3509 
   3510   a = (a & addrmask) >> howto->rightshift;
   3511 
   3512   /* If any sign bits are set, all sign bits must be set.
   3513      That is, A must be a valid negative address after
   3514      shifting.  */
   3515   signmask = ~ (fieldmask >> 1);
   3516   ss = a & signmask;
   3517   if (ss != 0 && ss != ((addrmask >> howto->rightshift) & signmask))
   3518     return true;
   3519 
   3520   /* We only need this next bit of code if the sign bit of B
   3521      is below the sign bit of A.  This would only happen if
   3522      SRC_MASK had fewer bits than BITSIZE.  Note that if
   3523      SRC_MASK has more bits than BITSIZE, we can get into
   3524      trouble; we would need to verify that B is in range, as
   3525      we do for A above.  */
   3526   signmask = ((~ howto->src_mask) >> 1) & howto->src_mask;
   3527   if ((b & signmask) != 0)
   3528     {
   3529       /* Set all the bits above the sign bit.  */
   3530       b -= signmask <<= 1;
   3531     }
   3532 
   3533   b = (b & addrmask) >> howto->bitpos;
   3534 
   3535   /* Now we can do the addition.  */
   3536   sum = a + b;
   3537 
   3538   /* See if the result has the correct sign.  Bits above the
   3539      sign bit are junk now; ignore them.  If the sum is
   3540      positive, make sure we did not have all negative inputs;
   3541      if the sum is negative, make sure we did not have all
   3542      positive inputs.  The test below looks only at the sign
   3543      bits, and it really just
   3544      SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
   3545   */
   3546   signmask = (fieldmask >> 1) + 1;
   3547   if (((~ (a ^ b)) & (a ^ sum)) & signmask)
   3548     return true;
   3549 
   3550   return false;
   3551 }
   3552 
   3553 static bool
   3554 xcoff_complain_overflow_unsigned_func (bfd *input_bfd,
   3555 				       bfd_vma val,
   3556 				       bfd_vma relocation,
   3557 				       struct reloc_howto_struct *howto)
   3558 {
   3559   bfd_vma addrmask, fieldmask;
   3560   bfd_vma a, b, sum;
   3561 
   3562   /* Get the values to be added together.  For signed and unsigned
   3563      relocations, we assume that all values should be truncated to
   3564      the size of an address.  For bitfields, all the bits matter.
   3565      See also bfd_check_overflow.  */
   3566   fieldmask = N_ONES (howto->bitsize);
   3567   addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
   3568   a = relocation;
   3569   b = val & howto->src_mask;
   3570 
   3571   /* Checking for an unsigned overflow is relatively easy:
   3572      trim the addresses and add, and trim the result as well.
   3573      Overflow is normally indicated when the result does not
   3574      fit in the field.  However, we also need to consider the
   3575      case when, e.g., fieldmask is 0x7fffffff or smaller, an
   3576      input is 0x80000000, and bfd_vma is only 32 bits; then we
   3577      will get sum == 0, but there is an overflow, since the
   3578      inputs did not fit in the field.  Instead of doing a
   3579      separate test, we can check for this by or-ing in the
   3580      operands when testing for the sum overflowing its final
   3581      field.  */
   3582   a = (a & addrmask) >> howto->rightshift;
   3583   b = (b & addrmask) >> howto->bitpos;
   3584   sum = (a + b) & addrmask;
   3585   if ((a | b | sum) & ~ fieldmask)
   3586     return true;
   3587 
   3588   return false;
   3589 }
   3590 
   3591 /* This is the relocation function for the RS/6000/POWER/PowerPC.
   3592    This is currently the only processor which uses XCOFF; I hope that
   3593    will never change.
   3594 
   3595    The original version was based on two documents:
   3596    the PowerPC AIX Version 4 Application Binary Interface, First
   3597    Edition (April 1992), and the PowerOpen ABI, Big-Endian
   3598    32-Bit Hardware Implementation (June 30, 1994).  Differences
   3599    between the documents are noted below.
   3600    Now, IBM has released an official documentation about XCOFF
   3601    format:
   3602    https://www.ibm.com/support/knowledgecenter/ssw_aix_72/filesreference/XCOFF.html
   3603 
   3604    Unsupported r_type's
   3605 
   3606    R_RTB:
   3607    R_RRTBI:
   3608    R_RRTBA:
   3609 
   3610    These relocs are defined by the PowerPC ABI to be
   3611    relative branches which use half of the difference
   3612    between the symbol and the program counter.  I can't
   3613    quite figure out when this is useful.  These relocs are
   3614    not defined by the PowerOpen ABI.
   3615 
   3616    Supported r_type's
   3617 
   3618    R_POS:
   3619    Simple positive relocation.
   3620 
   3621    R_NEG:
   3622    Simple negative relocation.
   3623 
   3624    R_REL:
   3625    Simple PC relative relocation.
   3626 
   3627    R_TOC:
   3628    TOC relative relocation.  The value in the instruction in
   3629    the input file is the offset from the input file TOC to
   3630    the desired location.  We want the offset from the final
   3631    TOC to the desired location.  We have:
   3632    isym = iTOC + in
   3633    iinsn = in + o
   3634    osym = oTOC + on
   3635    oinsn = on + o
   3636    so we must change insn by on - in.
   3637    This relocation allows the linker to perform optimizations
   3638    by transforming a load instruction into a add-immediate
   3639    when possible. The relocation is, then, changed to R_TRLA
   3640    in the output file.
   3641    TODO: Currently, the optimisation isn't implemented.
   3642 
   3643    R_TRL:
   3644    TOC relative relocation.  Same as R_TOC, except that
   3645    the optimization isn't allowed
   3646 
   3647    R_TRLA:
   3648    TOC relative relocation.  This is a TOC relative load
   3649    address instruction which have been changed to an add-
   3650    immediate instruction.
   3651 
   3652    R_GL:
   3653    GL linkage relocation.  The value of this relocation
   3654    is the address of the external symbol in the TOC
   3655    section.
   3656 
   3657    R_TCL:
   3658    Local object TOC address.  I can't figure out the
   3659    difference between this and case R_GL.
   3660 
   3661    R_RL:
   3662    The PowerPC AIX ABI describes this as a load which may be
   3663    changed to a load address.  The PowerOpen ABI says this
   3664    is the same as case R_POS.
   3665 
   3666    R_RLA:
   3667    The PowerPC AIX ABI describes this as a load address
   3668    which may be changed to a load.  The PowerOpen ABI says
   3669    this is the same as R_POS.
   3670 
   3671    R_REF:
   3672    Not a relocation but a way to prevent the garbage
   3673    collector of AIX linker to remove symbols.
   3674    This is not needed in our case.
   3675 
   3676    R_BA:
   3677    The PowerOpen ABI says this is the same as R_RBA.
   3678 
   3679    R_RBA:
   3680    Absolute branch which may be modified to become a
   3681    relative branch.
   3682 
   3683    R_BR:
   3684    The PowerOpen ABI says this is the same as R_RBR.
   3685 
   3686    R_RBR:
   3687    A relative branch which may be modified to become an
   3688    absolute branch.
   3689 
   3690    R_CAI:
   3691    The PowerPC ABI defines this as an absolute call which
   3692    may be modified to become a relative call.  The PowerOpen
   3693    ABI does not define this relocation type.
   3694 
   3695    R_CREL:
   3696    The PowerPC ABI defines this as a relative call which may
   3697    be modified to become an absolute call.  The PowerOpen
   3698    ABI does not define this relocation type.
   3699 
   3700    R_RBAC:
   3701    The PowerPC ABI defines this as an absolute branch to a
   3702    fixed address which may be modified to an absolute branch
   3703    to a symbol.  The PowerOpen ABI does not define this
   3704    relocation type.
   3705 
   3706    R_RBRC:
   3707    The PowerPC ABI defines this as an absolute branch to a
   3708    fixed address which may be modified to a relative branch.
   3709    The PowerOpen ABI does not define this relocation type.
   3710 
   3711    R_TLS:
   3712    Thread-local storage relocation using general-dynamic
   3713    model.
   3714 
   3715    R_TLS_IE:
   3716    Thread-local storage relocation using initial-exec model.
   3717 
   3718    R_TLS_LD:
   3719    Thread-local storage relocation using local-dynamic model.
   3720 
   3721    R_TLS_LE:
   3722    Thread-local storage relocation using local-exec model.
   3723 
   3724    R_TLSM:
   3725    Tread-local storage relocation used by the loader.
   3726 
   3727    R_TLSML:
   3728    Tread-local storage relocation used by the loader.
   3729 
   3730    R_TOCU:
   3731    Upper TOC relative relocation. The value is the
   3732    high-order 16 bit of a TOC relative relocation.
   3733 
   3734    R_TOCL:
   3735    Lower TOC relative relocation. The value is the
   3736    low-order 16 bit of a TOC relative relocation.
   3737 */
   3738 
   3739 bool
   3740 xcoff_ppc_relocate_section (bfd *output_bfd,
   3741 			    struct bfd_link_info *info,
   3742 			    bfd *input_bfd,
   3743 			    asection *input_section,
   3744 			    bfd_byte *contents,
   3745 			    struct internal_reloc *relocs,
   3746 			    struct internal_syment *syms,
   3747 			    asection **sections)
   3748 {
   3749   struct internal_reloc *rel;
   3750   struct internal_reloc *relend;
   3751 
   3752   rel = relocs;
   3753   relend = rel + input_section->reloc_count;
   3754   for (; rel < relend; rel++)
   3755     {
   3756       long symndx;
   3757       struct xcoff_link_hash_entry *h;
   3758       struct internal_syment *sym;
   3759       bfd_vma addend;
   3760       bfd_vma val;
   3761       struct reloc_howto_struct howto;
   3762       bfd_vma relocation;
   3763       bfd_vma value_to_relocate;
   3764       bfd_vma address;
   3765       bfd_byte *location;
   3766 
   3767       /* Relocation type R_REF is a special relocation type which is
   3768 	 merely used to prevent garbage collection from occurring for
   3769 	 the csect including the symbol which it references.  */
   3770       if (rel->r_type == R_REF)
   3771 	continue;
   3772 
   3773       /* Retrieve default value in HOWTO table and fix up according
   3774 	 to r_size field, if it can be different.
   3775 	 This should be made during relocation reading but the algorithms
   3776 	 are expecting constant howtos.  */
   3777       memcpy (&howto, &xcoff_howto_table[rel->r_type], sizeof (howto));
   3778       if (howto.bitsize != (rel->r_size & 0x1f) + 1)
   3779 	{
   3780 	  switch (rel->r_type)
   3781 	    {
   3782 	    case R_POS:
   3783 	    case R_NEG:
   3784 	      howto.bitsize = (rel->r_size & 0x1f) + 1;
   3785 	      howto.size = HOWTO_RSIZE (howto.bitsize > 16 ? 4 : 2);
   3786 	      howto.src_mask = howto.dst_mask = N_ONES (howto.bitsize);
   3787 	      break;
   3788 
   3789 	    default:
   3790 	      _bfd_error_handler
   3791 		(_("%pB: relocation (%d) at 0x%" PRIx64 " has wrong r_rsize (0x%x)\n"),
   3792 		 input_bfd, rel->r_type, (uint64_t) rel->r_vaddr, rel->r_size);
   3793 	      return false;
   3794 	    }
   3795 	}
   3796 
   3797       howto.complain_on_overflow = (rel->r_size & 0x80
   3798 				    ? complain_overflow_signed
   3799 				    : complain_overflow_bitfield);
   3800 
   3801       /* symbol */
   3802       val = 0;
   3803       addend = 0;
   3804       h = NULL;
   3805       sym = NULL;
   3806       symndx = rel->r_symndx;
   3807 
   3808       if (-1 != symndx)
   3809 	{
   3810 	  asection *sec;
   3811 
   3812 	  h = obj_xcoff_sym_hashes (input_bfd)[symndx];
   3813 	  sym = syms + symndx;
   3814 	  addend = - sym->n_value;
   3815 
   3816 	  if (NULL == h)
   3817 	    {
   3818 	      sec = sections[symndx];
   3819 	      /* Hack to make sure we use the right TOC anchor value
   3820 		 if this reloc is against the TOC anchor.  */
   3821 	      if (sec->name[3] == '0'
   3822 		  && strcmp (sec->name, ".tc0") == 0)
   3823 		val = xcoff_data (output_bfd)->toc;
   3824 	      else
   3825 		val = (sec->output_section->vma
   3826 		       + sec->output_offset
   3827 		       + sym->n_value
   3828 		       - sec->vma);
   3829 	    }
   3830 	  else
   3831 	    {
   3832 	      if (info->unresolved_syms_in_objects != RM_IGNORE
   3833 		  && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
   3834 		(*info->callbacks->undefined_symbol)
   3835 		  (info, h->root.root.string,
   3836 		   input_bfd, input_section,
   3837 		   rel->r_vaddr - input_section->vma,
   3838 		   info->unresolved_syms_in_objects == RM_DIAGNOSE &&
   3839 		       !info->warn_unresolved_syms);
   3840 
   3841 	      if (h->root.type == bfd_link_hash_defined
   3842 		  || h->root.type == bfd_link_hash_defweak)
   3843 		{
   3844 		  sec = h->root.u.def.section;
   3845 		  val = (h->root.u.def.value
   3846 			 + sec->output_section->vma
   3847 			 + sec->output_offset);
   3848 		}
   3849 	      else if (h->root.type == bfd_link_hash_common)
   3850 		{
   3851 		  sec = h->root.u.c.p->section;
   3852 		  val = (sec->output_section->vma
   3853 			 + sec->output_offset);
   3854 
   3855 		}
   3856 	      else
   3857 		{
   3858 		  BFD_ASSERT (bfd_link_relocatable (info)
   3859 			      || (info->static_link
   3860 				  && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
   3861 			      || (h->flags & XCOFF_DEF_DYNAMIC) != 0
   3862 			      || (h->flags & XCOFF_IMPORT) != 0);
   3863 		}
   3864 	    }
   3865 	}
   3866 
   3867       if (rel->r_type >= XCOFF_MAX_CALCULATE_RELOCATION
   3868 	  || !((*xcoff_calculate_relocation[rel->r_type])
   3869 	       (input_bfd, input_section, output_bfd, rel, sym, &howto, val,
   3870 		addend, &relocation, contents, info)))
   3871 	return false;
   3872 
   3873       /* address */
   3874       address = rel->r_vaddr - input_section->vma;
   3875       location = contents + address;
   3876 
   3877       if (address > input_section->size)
   3878 	abort ();
   3879 
   3880       /* Get the value we are going to relocate.  */
   3881       if (2 == bfd_get_reloc_size (&howto))
   3882 	value_to_relocate = bfd_get_16 (input_bfd, location);
   3883       else
   3884 	value_to_relocate = bfd_get_32 (input_bfd, location);
   3885 
   3886       /* overflow.
   3887 
   3888 	 FIXME: We may drop bits during the addition
   3889 	 which we don't check for.  We must either check at every single
   3890 	 operation, which would be tedious, or we must do the computations
   3891 	 in a type larger than bfd_vma, which would be inefficient.  */
   3892 
   3893       if (((*xcoff_complain_overflow[howto.complain_on_overflow])
   3894 	   (input_bfd, value_to_relocate, relocation, &howto)))
   3895 	{
   3896 	  const char *name;
   3897 	  char buf[SYMNMLEN + 1];
   3898 	  char reloc_type_name[10];
   3899 
   3900 	  if (symndx == -1)
   3901 	    {
   3902 	      name = "*ABS*";
   3903 	    }
   3904 	  else if (h != NULL)
   3905 	    {
   3906 	      name = NULL;
   3907 	    }
   3908 	  else
   3909 	    {
   3910 	      name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
   3911 	      if (name == NULL)
   3912 		name = "UNKNOWN";
   3913 	    }
   3914 	  sprintf (reloc_type_name, "0x%02x", rel->r_type);
   3915 
   3916 	  (*info->callbacks->reloc_overflow)
   3917 	    (info, (h ? &h->root : NULL), name, reloc_type_name,
   3918 	     (bfd_vma) 0, input_bfd, input_section,
   3919 	     rel->r_vaddr - input_section->vma);
   3920 	}
   3921 
   3922       /* Add RELOCATION to the right bits of VALUE_TO_RELOCATE.  */
   3923       value_to_relocate = ((value_to_relocate & ~howto.dst_mask)
   3924 			   | (((value_to_relocate & howto.src_mask)
   3925 			       + relocation) & howto.dst_mask));
   3926 
   3927       /* Put the value back in the object file.  */
   3928       if (2 == bfd_get_reloc_size (&howto))
   3929 	bfd_put_16 (input_bfd, value_to_relocate, location);
   3930       else
   3931 	bfd_put_32 (input_bfd, value_to_relocate, location);
   3932     }
   3933 
   3934   return true;
   3935 }
   3936 
   3937 /* gcc-8 warns (*) on all the strncpy calls in this function about
   3938    possible string truncation.  The "truncation" is not a bug.  We
   3939    have an external representation of structs with fields that are not
   3940    necessarily NULL terminated and corresponding internal
   3941    representation fields that are one larger so that they can always
   3942    be NULL terminated.
   3943    gcc versions between 4.2 and 4.6 do not allow pragma control of
   3944    diagnostics inside functions, giving a hard error if you try to use
   3945    the finer control available with later versions.
   3946    gcc prior to 4.2 warns about diagnostic push and pop.
   3947    gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
   3948    unless you also add #pragma GCC diagnostic ignored "-Wpragma".
   3949    (*) Depending on your system header files!  */
   3950 #if GCC_VERSION >= 8000
   3951 # pragma GCC diagnostic push
   3952 # pragma GCC diagnostic ignored "-Wstringop-truncation"
   3953 #endif
   3954 static bool
   3955 _bfd_xcoff_put_ldsymbol_name (bfd *abfd ATTRIBUTE_UNUSED,
   3956 			      struct xcoff_loader_info *ldinfo,
   3957 			      struct internal_ldsym *ldsym,
   3958 			      const char *name)
   3959 {
   3960   size_t len;
   3961   len = strlen (name);
   3962 
   3963   if (len <= SYMNMLEN)
   3964     strncpy (ldsym->_l._l_name, name, SYMNMLEN);
   3965   else
   3966     {
   3967       if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
   3968 	{
   3969 	  bfd_size_type newalc;
   3970 	  char *newstrings;
   3971 
   3972 	  newalc = ldinfo->string_alc * 2;
   3973 	  if (newalc == 0)
   3974 	    newalc = 32;
   3975 	  while (ldinfo->string_size + len + 3 > newalc)
   3976 	    newalc *= 2;
   3977 
   3978 	  newstrings = bfd_realloc (ldinfo->strings, newalc);
   3979 	  if (newstrings == NULL)
   3980 	    {
   3981 	      ldinfo->failed = true;
   3982 	      return false;
   3983 	    }
   3984 	  ldinfo->string_alc = newalc;
   3985 	  ldinfo->strings = newstrings;
   3986 	}
   3987 
   3988       ldinfo->strings[ldinfo->string_size] = ((len + 1) >> 8) & 0xff;
   3989       ldinfo->strings[ldinfo->string_size + 1] = ((len + 1)) & 0xff;
   3990       strcpy (ldinfo->strings + ldinfo->string_size + 2, name);
   3991       ldsym->_l._l_l._l_zeroes = 0;
   3992       ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
   3993       ldinfo->string_size += len + 3;
   3994     }
   3995 
   3996   return true;
   3997 }
   3998 
   3999 static bool
   4000 _bfd_xcoff_put_symbol_name (struct bfd_link_info *info,
   4001 			    struct bfd_strtab_hash *strtab,
   4002 			    struct internal_syment *sym,
   4003 			    const char *name)
   4004 {
   4005   if (strlen (name) <= SYMNMLEN)
   4006     {
   4007       strncpy (sym->_n._n_name, name, SYMNMLEN);
   4008     }
   4009   else
   4010     {
   4011       bool hash;
   4012       bfd_size_type indx;
   4013 
   4014       hash = !info->traditional_format;
   4015       indx = _bfd_stringtab_add (strtab, name, hash, false);
   4016       if (indx == (bfd_size_type) -1)
   4017 	return false;
   4018       sym->_n._n_n._n_zeroes = 0;
   4019       sym->_n._n_n._n_offset = STRING_SIZE_SIZE + indx;
   4020     }
   4021   return true;
   4022 }
   4023 #if GCC_VERSION >= 8000
   4024 # pragma GCC diagnostic pop
   4025 #endif
   4026 
   4027 static asection *
   4028 xcoff_create_csect_from_smclas (bfd *abfd,
   4029 				union internal_auxent *aux,
   4030 				const char *symbol_name)
   4031 {
   4032   asection *return_value = NULL;
   4033 
   4034   /* .sv64 = x_smclas == 17
   4035      This is an invalid csect for 32 bit apps.  */
   4036   static const char * const names[] =
   4037     {
   4038       ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo", /* 0 - 7 */
   4039       ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0", /* 8 - 15 */
   4040       ".td", NULL, ".sv3264", NULL, ".tl", ".ul", ".te"
   4041     };
   4042 
   4043   if ((aux->x_csect.x_smclas < ARRAY_SIZE (names))
   4044       && (NULL != names[aux->x_csect.x_smclas]))
   4045     {
   4046       return_value = bfd_make_section_anyway
   4047 	(abfd, names[aux->x_csect.x_smclas]);
   4048     }
   4049   else
   4050     {
   4051       _bfd_error_handler
   4052 	/* xgettext: c-format */
   4053 	(_("%pB: symbol `%s' has unrecognized smclas %d"),
   4054 	 abfd, symbol_name, aux->x_csect.x_smclas);
   4055       bfd_set_error (bfd_error_bad_value);
   4056     }
   4057 
   4058   return return_value;
   4059 }
   4060 
   4061 static bool
   4062 xcoff_is_lineno_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
   4063 {
   4064   if (0xffff <= value)
   4065     return true;
   4066 
   4067   return false;
   4068 }
   4069 
   4070 static bool
   4071 xcoff_is_reloc_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
   4072 {
   4073   if (0xffff <= value)
   4074     return true;
   4075 
   4076   return false;
   4077 }
   4078 
   4079 static bfd_vma
   4080 xcoff_loader_symbol_offset (bfd *abfd,
   4081 			    struct internal_ldhdr *ldhdr ATTRIBUTE_UNUSED)
   4082 {
   4083   return bfd_xcoff_ldhdrsz (abfd);
   4084 }
   4085 
   4086 static bfd_vma
   4087 xcoff_loader_reloc_offset (bfd *abfd, struct internal_ldhdr *ldhdr)
   4088 {
   4089   return bfd_xcoff_ldhdrsz (abfd) + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (abfd);
   4090 }
   4091 
   4092 static bool
   4093 xcoff_generate_rtinit  (bfd *abfd, const char *init, const char *fini,
   4094 			bool rtld)
   4095 {
   4096   bfd_byte filehdr_ext[FILHSZ];
   4097   bfd_byte scnhdr_ext[SCNHSZ];
   4098   bfd_byte syment_ext[SYMESZ * 10];
   4099   bfd_byte reloc_ext[RELSZ * 3];
   4100   bfd_byte *data_buffer;
   4101   bfd_size_type data_buffer_size;
   4102   bfd_byte *string_table = NULL, *st_tmp = NULL;
   4103   bfd_size_type string_table_size;
   4104   bfd_vma val;
   4105   size_t initsz, finisz;
   4106   struct internal_filehdr filehdr;
   4107   struct internal_scnhdr scnhdr;
   4108   struct internal_syment syment;
   4109   union internal_auxent auxent;
   4110   struct internal_reloc reloc;
   4111 
   4112   char *data_name = ".data";
   4113   char *rtinit_name = "__rtinit";
   4114   char *rtld_name = "__rtld";
   4115 
   4116   if (! bfd_xcoff_rtinit_size (abfd))
   4117     return false;
   4118 
   4119   initsz = (init == NULL ? 0 : 1 + strlen (init));
   4120   finisz = (fini == NULL ? 0 : 1 + strlen (fini));
   4121 
   4122   /* file header */
   4123   memset (filehdr_ext, 0, FILHSZ);
   4124   memset (&filehdr, 0, sizeof (struct internal_filehdr));
   4125   filehdr.f_magic = bfd_xcoff_magic_number (abfd);
   4126   filehdr.f_nscns = 1;
   4127   filehdr.f_timdat = 0;
   4128   filehdr.f_nsyms = 0;  /* at least 6, no more than 10 */
   4129   filehdr.f_symptr = 0; /* set below */
   4130   filehdr.f_opthdr = 0;
   4131   filehdr.f_flags = 0;
   4132 
   4133   /* section header */
   4134   memset (scnhdr_ext, 0, SCNHSZ);
   4135   memset (&scnhdr, 0, sizeof (struct internal_scnhdr));
   4136   memcpy (scnhdr.s_name, data_name, strlen (data_name));
   4137   scnhdr.s_paddr = 0;
   4138   scnhdr.s_vaddr = 0;
   4139   scnhdr.s_size = 0;    /* set below */
   4140   scnhdr.s_scnptr = FILHSZ + SCNHSZ;
   4141   scnhdr.s_relptr = 0;  /* set below */
   4142   scnhdr.s_lnnoptr = 0;
   4143   scnhdr.s_nreloc = 0;  /* either 1 or 2 */
   4144   scnhdr.s_nlnno = 0;
   4145   scnhdr.s_flags = STYP_DATA;
   4146 
   4147   /* .data
   4148      0x0000	      0x00000000 : rtl
   4149      0x0004	      0x00000010 : offset to init, or 0
   4150      0x0008	      0x00000028 : offset to fini, or 0
   4151      0x000C	      0x0000000C : size of descriptor
   4152      0x0010	      0x00000000 : init, needs a reloc
   4153      0x0014	      0x00000040 : offset to init name
   4154      0x0018	      0x00000000 : flags, padded to a word
   4155      0x001C	      0x00000000 : empty init
   4156      0x0020	      0x00000000 :
   4157      0x0024	      0x00000000 :
   4158      0x0028	      0x00000000 : fini, needs a reloc
   4159      0x002C	      0x00000??? : offset to fini name
   4160      0x0030	      0x00000000 : flags, padded to a word
   4161      0x0034	      0x00000000 : empty fini
   4162      0x0038	      0x00000000 :
   4163      0x003C	      0x00000000 :
   4164      0x0040	      init name
   4165      0x0040 + initsz  fini name */
   4166 
   4167   data_buffer_size = 0x0040 + initsz + finisz;
   4168   data_buffer_size = (data_buffer_size + 7) &~ (bfd_size_type) 7;
   4169   data_buffer = NULL;
   4170   data_buffer = (bfd_byte *) bfd_zmalloc (data_buffer_size);
   4171   if (data_buffer == NULL)
   4172     return false;
   4173 
   4174   if (initsz)
   4175     {
   4176       val = 0x10;
   4177       bfd_h_put_32 (abfd, val, &data_buffer[0x04]);
   4178       val = 0x40;
   4179       bfd_h_put_32 (abfd, val, &data_buffer[0x14]);
   4180       memcpy (&data_buffer[val], init, initsz);
   4181     }
   4182 
   4183   if (finisz)
   4184     {
   4185       val = 0x28;
   4186       bfd_h_put_32 (abfd, val, &data_buffer[0x08]);
   4187       val = 0x40 + initsz;
   4188       bfd_h_put_32 (abfd, val, &data_buffer[0x2C]);
   4189       memcpy (&data_buffer[val], fini, finisz);
   4190     }
   4191 
   4192   val = 0x0C;
   4193   bfd_h_put_32 (abfd, val, &data_buffer[0x0C]);
   4194 
   4195   scnhdr.s_size = data_buffer_size;
   4196 
   4197   /* string table */
   4198   string_table_size = 0;
   4199   if (initsz > 9)
   4200     string_table_size += initsz;
   4201   if (finisz > 9)
   4202     string_table_size += finisz;
   4203   if (string_table_size)
   4204     {
   4205       string_table_size += 4;
   4206       string_table = (bfd_byte *) bfd_zmalloc (string_table_size);
   4207       if (string_table == NULL)
   4208 	{
   4209 	  free (data_buffer);
   4210 	  return false;
   4211 	}
   4212 
   4213       val = string_table_size;
   4214       bfd_h_put_32 (abfd, val, &string_table[0]);
   4215       st_tmp = string_table + 4;
   4216     }
   4217 
   4218   /* symbols
   4219      0. .data csect
   4220      2. __rtinit
   4221      4. init function
   4222      6. fini function
   4223      8. __rtld  */
   4224   memset (syment_ext, 0, 10 * SYMESZ);
   4225   memset (reloc_ext, 0, 3 * RELSZ);
   4226 
   4227   /* .data csect */
   4228   memset (&syment, 0, sizeof (struct internal_syment));
   4229   memset (&auxent, 0, sizeof (union internal_auxent));
   4230   memcpy (syment._n._n_name, data_name, strlen (data_name));
   4231   syment.n_scnum = 1;
   4232   syment.n_sclass = C_HIDEXT;
   4233   syment.n_numaux = 1;
   4234   auxent.x_csect.x_scnlen.u64 = data_buffer_size;
   4235   auxent.x_csect.x_smtyp = 3 << 3 | XTY_SD;
   4236   auxent.x_csect.x_smclas = XMC_RW;
   4237   bfd_coff_swap_sym_out (abfd, &syment,
   4238 			 &syment_ext[filehdr.f_nsyms * SYMESZ]);
   4239   bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
   4240 			 syment.n_numaux,
   4241 			 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
   4242   filehdr.f_nsyms += 2;
   4243 
   4244   /* __rtinit */
   4245   memset (&syment, 0, sizeof (struct internal_syment));
   4246   memset (&auxent, 0, sizeof (union internal_auxent));
   4247   memcpy (syment._n._n_name, rtinit_name, strlen (rtinit_name));
   4248   syment.n_scnum = 1;
   4249   syment.n_sclass = C_EXT;
   4250   syment.n_numaux = 1;
   4251   auxent.x_csect.x_smtyp = XTY_LD;
   4252   auxent.x_csect.x_smclas = XMC_RW;
   4253   bfd_coff_swap_sym_out (abfd, &syment,
   4254 			 &syment_ext[filehdr.f_nsyms * SYMESZ]);
   4255   bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
   4256 			 syment.n_numaux,
   4257 			 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
   4258   filehdr.f_nsyms += 2;
   4259 
   4260   /* init */
   4261   if (initsz)
   4262     {
   4263       memset (&syment, 0, sizeof (struct internal_syment));
   4264       memset (&auxent, 0, sizeof (union internal_auxent));
   4265 
   4266       if (initsz > 9)
   4267 	{
   4268 	  syment._n._n_n._n_offset = st_tmp - string_table;
   4269 	  memcpy (st_tmp, init, initsz);
   4270 	  st_tmp += initsz;
   4271 	}
   4272       else
   4273 	memcpy (syment._n._n_name, init, initsz - 1);
   4274 
   4275       syment.n_sclass = C_EXT;
   4276       syment.n_numaux = 1;
   4277       bfd_coff_swap_sym_out (abfd, &syment,
   4278 			     &syment_ext[filehdr.f_nsyms * SYMESZ]);
   4279       bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
   4280 			     syment.n_numaux,
   4281 			     &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
   4282 
   4283       /* reloc */
   4284       memset (&reloc, 0, sizeof (struct internal_reloc));
   4285       reloc.r_vaddr = 0x0010;
   4286       reloc.r_symndx = filehdr.f_nsyms;
   4287       reloc.r_type = R_POS;
   4288       reloc.r_size = 31;
   4289       bfd_coff_swap_reloc_out (abfd, &reloc, &reloc_ext[0]);
   4290 
   4291       filehdr.f_nsyms += 2;
   4292       scnhdr.s_nreloc += 1;
   4293     }
   4294 
   4295   /* fini */
   4296   if (finisz)
   4297     {
   4298       memset (&syment, 0, sizeof (struct internal_syment));
   4299       memset (&auxent, 0, sizeof (union internal_auxent));
   4300 
   4301       if (finisz > 9)
   4302 	{
   4303 	  syment._n._n_n._n_offset = st_tmp - string_table;
   4304 	  memcpy (st_tmp, fini, finisz);
   4305 	  st_tmp += finisz;
   4306 	}
   4307       else
   4308 	memcpy (syment._n._n_name, fini, finisz - 1);
   4309 
   4310       syment.n_sclass = C_EXT;
   4311       syment.n_numaux = 1;
   4312       bfd_coff_swap_sym_out (abfd, &syment,
   4313 			     &syment_ext[filehdr.f_nsyms * SYMESZ]);
   4314       bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
   4315 			     syment.n_numaux,
   4316 			     &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
   4317 
   4318       /* reloc */
   4319       memset (&reloc, 0, sizeof (struct internal_reloc));
   4320       reloc.r_vaddr = 0x0028;
   4321       reloc.r_symndx = filehdr.f_nsyms;
   4322       reloc.r_type = R_POS;
   4323       reloc.r_size = 31;
   4324       bfd_coff_swap_reloc_out (abfd, &reloc,
   4325 			       &reloc_ext[scnhdr.s_nreloc * RELSZ]);
   4326 
   4327       filehdr.f_nsyms += 2;
   4328       scnhdr.s_nreloc += 1;
   4329     }
   4330 
   4331   if (rtld)
   4332     {
   4333       memset (&syment, 0, sizeof (struct internal_syment));
   4334       memset (&auxent, 0, sizeof (union internal_auxent));
   4335       memcpy (syment._n._n_name, rtld_name, strlen (rtld_name));
   4336       syment.n_sclass = C_EXT;
   4337       syment.n_numaux = 1;
   4338       bfd_coff_swap_sym_out (abfd, &syment,
   4339 			     &syment_ext[filehdr.f_nsyms * SYMESZ]);
   4340       bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
   4341 			     syment.n_numaux,
   4342 			     &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
   4343 
   4344       /* reloc */
   4345       memset (&reloc, 0, sizeof (struct internal_reloc));
   4346       reloc.r_vaddr = 0x0000;
   4347       reloc.r_symndx = filehdr.f_nsyms;
   4348       reloc.r_type = R_POS;
   4349       reloc.r_size = 31;
   4350       bfd_coff_swap_reloc_out (abfd, &reloc,
   4351 			       &reloc_ext[scnhdr.s_nreloc * RELSZ]);
   4352 
   4353       filehdr.f_nsyms += 2;
   4354       scnhdr.s_nreloc += 1;
   4355     }
   4356 
   4357   scnhdr.s_relptr = scnhdr.s_scnptr + data_buffer_size;
   4358   filehdr.f_symptr = scnhdr.s_relptr + scnhdr.s_nreloc * RELSZ;
   4359 
   4360   bfd_coff_swap_filehdr_out (abfd, &filehdr, filehdr_ext);
   4361   bfd_coff_swap_scnhdr_out (abfd, &scnhdr, scnhdr_ext);
   4362   bool ret = true;
   4363   if (bfd_write (filehdr_ext, FILHSZ, abfd) != FILHSZ
   4364       || bfd_write (scnhdr_ext, SCNHSZ, abfd) != SCNHSZ
   4365       || bfd_write (data_buffer, data_buffer_size, abfd) != data_buffer_size
   4366       || (bfd_write (reloc_ext, scnhdr.s_nreloc * RELSZ, abfd)
   4367 	  != scnhdr.s_nreloc * RELSZ)
   4368       || (bfd_write (syment_ext, filehdr.f_nsyms * SYMESZ, abfd)
   4369 	  != (bfd_size_type) filehdr.f_nsyms * SYMESZ)
   4370       || bfd_write (string_table, string_table_size, abfd) != string_table_size)
   4371     ret = false;
   4372 
   4373   free (string_table);
   4374   free (data_buffer);
   4375   return ret;
   4376 }
   4377 
   4378 
   4379 static reloc_howto_type xcoff_dynamic_reloc =
   4380 HOWTO (0,			/* type */
   4381        0,			/* rightshift */
   4382        4,			/* size */
   4383        32,			/* bitsize */
   4384        false,			/* pc_relative */
   4385        0,			/* bitpos */
   4386        complain_overflow_bitfield, /* complain_on_overflow */
   4387        0,			/* special_function */
   4388        "R_POS",			/* name */
   4389        true,			/* partial_inplace */
   4390        0xffffffff,		/* src_mask */
   4391        0xffffffff,		/* dst_mask */
   4392        false);			/* pcrel_offset */
   4393 
   4394 /* Indirect call stub
   4395    The first word of the code must be modified by filling in
   4396    the correct TOC offset.  */
   4397 
   4398 static const unsigned long xcoff_stub_indirect_call_code[4] =
   4399   {
   4400     0x81820000,	/* lwz r12,0(r2) */
   4401     0x800c0000,	/* lwz r0,0(r12) */
   4402     0x7c0903a6,	/* mtctr r0 */
   4403     0x4e800420,	/* bctr */
   4404   };
   4405 
   4406 /*  Shared call stub
   4407     The first word of the code must be modified by filling in
   4408     the correct TOC offset.
   4409     This is exactly as the glink code but without the traceback,
   4410     as it won't be an independent function.  */
   4411 
   4412 static const unsigned long xcoff_stub_shared_call_code[6] =
   4413   {
   4414     0x81820000,	/* lwz r12,0(r2) */
   4415     0x90410014,	/* stw r2,20(r1) */
   4416     0x800c0000,	/* lwz r0,0(r12) */
   4417     0x804c0004,	/* lwz r2,4(r12) */
   4418     0x7c0903a6,	/* mtctr r0 */
   4419     0x4e800420,	/* bctr */
   4420   };
   4421 
   4422 /*  glink
   4423 
   4424    The first word of global linkage code must be modified by filling in
   4425    the correct TOC offset.  */
   4426 
   4427 static const unsigned long xcoff_glink_code[9] =
   4428   {
   4429     0x81820000,	/* lwz r12,0(r2) */
   4430     0x90410014,	/* stw r2,20(r1) */
   4431     0x800c0000,	/* lwz r0,0(r12) */
   4432     0x804c0004,	/* lwz r2,4(r12) */
   4433     0x7c0903a6,	/* mtctr r0 */
   4434     0x4e800420,	/* bctr */
   4435     0x00000000,	/* start of traceback table */
   4436     0x000c8000,	/* traceback table */
   4437     0x00000000,	/* traceback table */
   4438   };
   4439 
   4440 /* Table to convert DWARF flags to section names.
   4441    Remember to update binutils/dwarf.c:debug_displays
   4442    if new DWARF sections are supported by XCOFF.  */
   4443 
   4444 const struct xcoff_dwsect_name xcoff_dwsect_names[] = {
   4445   { SSUBTYP_DWINFO,  ".dwinfo",  ".debug_info",     true },
   4446   { SSUBTYP_DWLINE,  ".dwline",  ".debug_line",     true },
   4447   { SSUBTYP_DWPBNMS, ".dwpbnms", ".debug_pubnames", true },
   4448   { SSUBTYP_DWPBTYP, ".dwpbtyp", ".debug_pubtypes", true },
   4449   { SSUBTYP_DWARNGE, ".dwarnge", ".debug_aranges",  true },
   4450   { SSUBTYP_DWABREV, ".dwabrev", ".debug_abbrev",   false },
   4451   { SSUBTYP_DWSTR,   ".dwstr",   ".debug_str",      true },
   4452   { SSUBTYP_DWRNGES, ".dwrnges", ".debug_ranges",   true },
   4453   { SSUBTYP_DWLOC,   ".dwloc",   ".debug_loc",      true },
   4454   { SSUBTYP_DWFRAME, ".dwframe", ".debug_frame",    true },
   4455   { SSUBTYP_DWMAC,   ".dwmac",   ".debug_macro",    true }
   4456 };
   4457 
   4458 /* For generic entry points.  */
   4459 #define _bfd_xcoff_close_and_cleanup coff_close_and_cleanup
   4460 #define _bfd_xcoff_bfd_free_cached_info coff_bfd_free_cached_info
   4461 #define _bfd_xcoff_new_section_hook coff_new_section_hook
   4462 #define _bfd_xcoff_get_section_contents _bfd_generic_get_section_contents
   4463 
   4464 /* For copy private data entry points.  */
   4465 #define _bfd_xcoff_bfd_copy_private_bfd_data \
   4466   _bfd_xcoff_copy_private_bfd_data
   4467 #define _bfd_xcoff_bfd_merge_private_bfd_data \
   4468   _bfd_generic_bfd_merge_private_bfd_data
   4469 #define _bfd_xcoff_bfd_copy_private_section_data \
   4470   _bfd_generic_bfd_copy_private_section_data
   4471 #define _bfd_xcoff_bfd_copy_private_symbol_data \
   4472    _bfd_generic_bfd_copy_private_symbol_data
   4473 #define _bfd_xcoff_bfd_copy_private_header_data \
   4474    _bfd_generic_bfd_copy_private_header_data
   4475 #define _bfd_xcoff_bfd_set_private_flags \
   4476    _bfd_generic_bfd_set_private_flags
   4477 #define _bfd_xcoff_bfd_print_private_bfd_data \
   4478    _bfd_generic_bfd_print_private_bfd_data
   4479 
   4480 /* For archive entry points.  */
   4481 #define _bfd_xcoff_slurp_extended_name_table \
   4482    _bfd_noarchive_slurp_extended_name_table
   4483 #define _bfd_xcoff_construct_extended_name_table \
   4484    _bfd_noarchive_construct_extended_name_table
   4485 #define _bfd_xcoff_truncate_arname bfd_dont_truncate_arname
   4486 #define _bfd_xcoff_write_ar_hdr _bfd_generic_write_ar_hdr
   4487 #define _bfd_xcoff_get_elt_at_index _bfd_generic_get_elt_at_index
   4488 #define _bfd_xcoff_generic_stat_arch_elt _bfd_xcoff_stat_arch_elt
   4489 #define _bfd_xcoff_update_armap_timestamp _bfd_bool_bfd_true
   4490 
   4491 /* For symbols entry points.  */
   4492 #define _bfd_xcoff_get_symtab_upper_bound coff_get_symtab_upper_bound
   4493 #define _bfd_xcoff_canonicalize_symtab coff_canonicalize_symtab
   4494 #define _bfd_xcoff_make_empty_symbol coff_make_empty_symbol
   4495 #define _bfd_xcoff_print_symbol coff_print_symbol
   4496 #define _bfd_xcoff_get_symbol_info coff_get_symbol_info
   4497 #define _bfd_xcoff_get_symbol_version_string \
   4498   _bfd_nosymbols_get_symbol_version_string
   4499 #define _bfd_xcoff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
   4500 #define _bfd_xcoff_bfd_is_target_special_symbol \
   4501   coff_bfd_is_target_special_symbol
   4502 #define _bfd_xcoff_get_lineno coff_get_lineno
   4503 #define _bfd_xcoff_find_nearest_line coff_find_nearest_line
   4504 #define _bfd_xcoff_find_nearest_line_with_alt \
   4505 coff_find_nearest_line_with_alt
   4506 #define _bfd_xcoff_find_line coff_find_line
   4507 #define _bfd_xcoff_find_inliner_info coff_find_inliner_info
   4508 #define _bfd_xcoff_bfd_make_debug_symbol coff_bfd_make_debug_symbol
   4509 #define _bfd_xcoff_read_minisymbols _bfd_generic_read_minisymbols
   4510 #define _bfd_xcoff_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
   4511 
   4512 /* For reloc entry points.  */
   4513 #define _bfd_xcoff_get_reloc_upper_bound coff_get_reloc_upper_bound
   4514 #define _bfd_xcoff_canonicalize_reloc coff_canonicalize_reloc
   4515 #define _bfd_xcoff_set_reloc _bfd_generic_set_reloc
   4516 #define _bfd_xcoff_bfd_reloc_type_lookup _bfd_xcoff_reloc_type_lookup
   4517 #define _bfd_xcoff_bfd_reloc_name_lookup _bfd_xcoff_reloc_name_lookup
   4518 
   4519 /* For link entry points.  */
   4520 #define _bfd_xcoff_bfd_get_relocated_section_contents \
   4521   bfd_generic_get_relocated_section_contents
   4522 #define _bfd_xcoff_bfd_relax_section bfd_generic_relax_section
   4523 #define _bfd_xcoff_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
   4524 #define _bfd_xcoff_bfd_link_just_syms _bfd_generic_link_just_syms
   4525 #define _bfd_xcoff_bfd_copy_link_hash_symbol_type \
   4526   _bfd_generic_copy_link_hash_symbol_type
   4527 #define _bfd_xcoff_bfd_link_split_section _bfd_generic_link_split_section
   4528 #define _bfd_xcoff_bfd_gc_sections bfd_generic_gc_sections
   4529 #define _bfd_xcoff_bfd_lookup_section_flags bfd_generic_lookup_section_flags
   4530 #define _bfd_xcoff_bfd_merge_sections bfd_generic_merge_sections
   4531 #define _bfd_xcoff_bfd_is_group_section bfd_generic_is_group_section
   4532 #define _bfd_xcoff_bfd_group_name bfd_generic_group_name
   4533 #define _bfd_xcoff_bfd_discard_group bfd_generic_discard_group
   4534 #define _bfd_xcoff_section_already_linked _bfd_generic_section_already_linked
   4535 #define _bfd_xcoff_bfd_define_common_symbol _bfd_xcoff_define_common_symbol
   4536 #define _bfd_xcoff_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
   4537 #define _bfd_xcoff_bfd_define_start_stop    bfd_generic_define_start_stop
   4538 #define _bfd_xcoff_bfd_link_check_relocs    _bfd_generic_link_check_relocs
   4539 
   4540 /* For dynamic symbols and relocs entry points.  */
   4541 #define _bfd_xcoff_get_synthetic_symtab _bfd_nodynamic_get_synthetic_symtab
   4542 
   4543 static const struct xcoff_backend_data_rec bfd_xcoff_backend_data =
   4544   {
   4545     { /* COFF backend, defined in libcoff.h.  */
   4546       _bfd_xcoff_swap_aux_in,
   4547       _bfd_xcoff_swap_sym_in,
   4548       coff_swap_lineno_in,
   4549       _bfd_xcoff_swap_aux_out,
   4550       _bfd_xcoff_swap_sym_out,
   4551       coff_swap_lineno_out,
   4552       xcoff_swap_reloc_out,
   4553       coff_swap_filehdr_out,
   4554       coff_swap_aouthdr_out,
   4555       coff_swap_scnhdr_out,
   4556       FILHSZ,
   4557       AOUTSZ,
   4558       SCNHSZ,
   4559       SYMESZ,
   4560       AUXESZ,
   4561       RELSZ,
   4562       LINESZ,
   4563       FILNMLEN,
   4564       true,			/* _bfd_coff_long_filenames */
   4565       XCOFF_NO_LONG_SECTION_NAMES,  /* _bfd_coff_long_section_names */
   4566       3,			/* _bfd_coff_default_section_alignment_power */
   4567       false,			/* _bfd_coff_force_symnames_in_strings */
   4568       2,			/* _bfd_coff_debug_string_prefix_length */
   4569       32768,			/* _bfd_coff_max_nscns */
   4570       coff_swap_filehdr_in,
   4571       coff_swap_aouthdr_in,
   4572       coff_swap_scnhdr_in,
   4573       xcoff_swap_reloc_in,
   4574       coff_bad_format_hook,
   4575       coff_set_arch_mach_hook,
   4576       coff_mkobject_hook,
   4577       styp_to_sec_flags,
   4578       coff_set_alignment_hook,
   4579       coff_slurp_symbol_table,
   4580       symname_in_debug_hook,
   4581       coff_pointerize_aux_hook,
   4582       coff_print_aux,
   4583       dummy_reloc16_extra_cases,
   4584       dummy_reloc16_estimate,
   4585       NULL,			/* bfd_coff_sym_is_global */
   4586       coff_compute_section_file_positions,
   4587       NULL,			/* _bfd_coff_start_final_link */
   4588       xcoff_ppc_relocate_section,
   4589       coff_rtype_to_howto,
   4590       NULL,			/* _bfd_coff_adjust_symndx */
   4591       coff_link_output_has_begun,
   4592       coff_final_link_postscript,
   4593       NULL			/* print_pdata.  */
   4594     },
   4595 
   4596     0x01DF,			/* magic number */
   4597     bfd_arch_rs6000,
   4598     bfd_mach_rs6k,
   4599 
   4600     /* Function pointers to xcoff specific swap routines.  */
   4601     xcoff_swap_ldhdr_in,
   4602     xcoff_swap_ldhdr_out,
   4603     xcoff_swap_ldsym_in,
   4604     xcoff_swap_ldsym_out,
   4605     xcoff_swap_ldrel_in,
   4606     xcoff_swap_ldrel_out,
   4607 
   4608     /* Sizes.  */
   4609     LDHDRSZ,
   4610     LDSYMSZ,
   4611     LDRELSZ,
   4612     12,				/* _xcoff_function_descriptor_size */
   4613     SMALL_AOUTSZ,
   4614 
   4615     /* Versions.  */
   4616     1,				/* _xcoff_ldhdr_version */
   4617 
   4618     _bfd_xcoff_put_symbol_name,
   4619     _bfd_xcoff_put_ldsymbol_name,
   4620     &xcoff_dynamic_reloc,
   4621     xcoff_create_csect_from_smclas,
   4622 
   4623     /* Lineno and reloc count overflow.  */
   4624     xcoff_is_lineno_count_overflow,
   4625     xcoff_is_reloc_count_overflow,
   4626 
   4627     xcoff_loader_symbol_offset,
   4628     xcoff_loader_reloc_offset,
   4629 
   4630     /* glink.  */
   4631     &xcoff_glink_code[0],
   4632     36,				/* _xcoff_glink_size */
   4633 
   4634     /* rtinit */
   4635     64,				/* _xcoff_rtinit_size */
   4636     xcoff_generate_rtinit,
   4637 
   4638     /* Stub indirect call.  */
   4639     &xcoff_stub_indirect_call_code[0],
   4640     16,				/* _xcoff_stub_indirect_call_size */
   4641 
   4642     /* Stub shared call.  */
   4643     &xcoff_stub_shared_call_code[0],
   4644     24,				/* _xcoff_stub_shared_call_size */
   4645   };
   4646 
   4647 /* The transfer vector that leads the outside world to all of the above.  */
   4648 const bfd_target rs6000_xcoff_vec =
   4649   {
   4650     "aixcoff-rs6000",
   4651     bfd_target_xcoff_flavour,
   4652     BFD_ENDIAN_BIG,		/* data byte order is big */
   4653     BFD_ENDIAN_BIG,		/* header byte order is big */
   4654 
   4655     (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
   4656      | HAS_SYMS | HAS_LOCALS | WP_TEXT),
   4657 
   4658     SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
   4659     0,				/* leading char */
   4660     '/',			/* ar_pad_char */
   4661     15,				/* ar_max_namelen */
   4662     0,				/* match priority.  */
   4663     TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols.  */
   4664 
   4665     /* data */
   4666     bfd_getb64,
   4667     bfd_getb_signed_64,
   4668     bfd_putb64,
   4669     bfd_getb32,
   4670     bfd_getb_signed_32,
   4671     bfd_putb32,
   4672     bfd_getb16,
   4673     bfd_getb_signed_16,
   4674     bfd_putb16,
   4675 
   4676     /* hdrs */
   4677     bfd_getb64,
   4678     bfd_getb_signed_64,
   4679     bfd_putb64,
   4680     bfd_getb32,
   4681     bfd_getb_signed_32,
   4682     bfd_putb32,
   4683     bfd_getb16,
   4684     bfd_getb_signed_16,
   4685     bfd_putb16,
   4686 
   4687     { /* bfd_check_format */
   4688       _bfd_dummy_target,
   4689       coff_object_p,
   4690       _bfd_xcoff_archive_p,
   4691       CORE_FILE_P
   4692     },
   4693 
   4694     { /* bfd_set_format */
   4695       _bfd_bool_bfd_false_error,
   4696       coff_mkobject,
   4697       _bfd_generic_mkarchive,
   4698       _bfd_bool_bfd_false_error
   4699     },
   4700 
   4701     {/* bfd_write_contents */
   4702       _bfd_bool_bfd_false_error,
   4703       coff_write_object_contents,
   4704       _bfd_xcoff_write_archive_contents,
   4705       _bfd_bool_bfd_false_error
   4706     },
   4707 
   4708     BFD_JUMP_TABLE_GENERIC (_bfd_xcoff),
   4709     BFD_JUMP_TABLE_COPY (_bfd_xcoff),
   4710     BFD_JUMP_TABLE_CORE (coff),
   4711     BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
   4712     BFD_JUMP_TABLE_SYMBOLS (_bfd_xcoff),
   4713     BFD_JUMP_TABLE_RELOCS (_bfd_xcoff),
   4714     BFD_JUMP_TABLE_WRITE (coff),
   4715     BFD_JUMP_TABLE_LINK (_bfd_xcoff),
   4716     BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
   4717 
   4718     /* Opposite endian version, none exists */
   4719     NULL,
   4720 
   4721     & bfd_xcoff_backend_data,
   4722   };
   4723 
   4724 /* xcoff-powermac target
   4725    Old target.
   4726    Only difference between this target and the rs6000 target is the
   4727    the default architecture and machine type used in coffcode.h
   4728 
   4729    PowerPC Macs use the same magic numbers as RS/6000
   4730    (because that's how they were bootstrapped originally),
   4731    but they are always PowerPC architecture.  */
   4732 static const struct xcoff_backend_data_rec bfd_pmac_xcoff_backend_data =
   4733   {
   4734     { /* COFF backend, defined in libcoff.h.  */
   4735       _bfd_xcoff_swap_aux_in,
   4736       _bfd_xcoff_swap_sym_in,
   4737       coff_swap_lineno_in,
   4738       _bfd_xcoff_swap_aux_out,
   4739       _bfd_xcoff_swap_sym_out,
   4740       coff_swap_lineno_out,
   4741       xcoff_swap_reloc_out,
   4742       coff_swap_filehdr_out,
   4743       coff_swap_aouthdr_out,
   4744       coff_swap_scnhdr_out,
   4745       FILHSZ,
   4746       AOUTSZ,
   4747       SCNHSZ,
   4748       SYMESZ,
   4749       AUXESZ,
   4750       RELSZ,
   4751       LINESZ,
   4752       FILNMLEN,
   4753       true,			/* _bfd_coff_long_filenames */
   4754       XCOFF_NO_LONG_SECTION_NAMES,  /* _bfd_coff_long_section_names */
   4755       3,			/* _bfd_coff_default_section_alignment_power */
   4756       false,			/* _bfd_coff_force_symnames_in_strings */
   4757       2,			/* _bfd_coff_debug_string_prefix_length */
   4758       32768,			/* _bfd_coff_max_nscns */
   4759       coff_swap_filehdr_in,
   4760       coff_swap_aouthdr_in,
   4761       coff_swap_scnhdr_in,
   4762       xcoff_swap_reloc_in,
   4763       coff_bad_format_hook,
   4764       coff_set_arch_mach_hook,
   4765       coff_mkobject_hook,
   4766       styp_to_sec_flags,
   4767       coff_set_alignment_hook,
   4768       coff_slurp_symbol_table,
   4769       symname_in_debug_hook,
   4770       coff_pointerize_aux_hook,
   4771       coff_print_aux,
   4772       dummy_reloc16_extra_cases,
   4773       dummy_reloc16_estimate,
   4774       NULL,			/* bfd_coff_sym_is_global */
   4775       coff_compute_section_file_positions,
   4776       NULL,			/* _bfd_coff_start_final_link */
   4777       xcoff_ppc_relocate_section,
   4778       coff_rtype_to_howto,
   4779       NULL,			/* _bfd_coff_adjust_symndx */
   4780       coff_link_output_has_begun,
   4781       coff_final_link_postscript,
   4782       NULL			/* print_pdata.  */
   4783     },
   4784 
   4785     0x01DF,			/* magic number */
   4786     bfd_arch_powerpc,
   4787     bfd_mach_ppc,
   4788 
   4789     /* Function pointers to xcoff specific swap routines.  */
   4790     xcoff_swap_ldhdr_in,
   4791     xcoff_swap_ldhdr_out,
   4792     xcoff_swap_ldsym_in,
   4793     xcoff_swap_ldsym_out,
   4794     xcoff_swap_ldrel_in,
   4795     xcoff_swap_ldrel_out,
   4796 
   4797     /* Sizes.  */
   4798     LDHDRSZ,
   4799     LDSYMSZ,
   4800     LDRELSZ,
   4801     12,				/* _xcoff_function_descriptor_size */
   4802     SMALL_AOUTSZ,
   4803 
   4804     /* Versions.  */
   4805     1,				/* _xcoff_ldhdr_version */
   4806 
   4807     _bfd_xcoff_put_symbol_name,
   4808     _bfd_xcoff_put_ldsymbol_name,
   4809     &xcoff_dynamic_reloc,
   4810     xcoff_create_csect_from_smclas,
   4811 
   4812     /* Lineno and reloc count overflow.  */
   4813     xcoff_is_lineno_count_overflow,
   4814     xcoff_is_reloc_count_overflow,
   4815 
   4816     xcoff_loader_symbol_offset,
   4817     xcoff_loader_reloc_offset,
   4818 
   4819     /* glink.  */
   4820     &xcoff_glink_code[0],
   4821     36,				/* _xcoff_glink_size */
   4822 
   4823     /* rtinit */
   4824     0,				/* _xcoff_rtinit_size */
   4825     xcoff_generate_rtinit,
   4826 
   4827     /* Stub indirect call.  */
   4828     &xcoff_stub_indirect_call_code[0],
   4829     16,				/* _xcoff_stub_indirect_call_size */
   4830 
   4831     /* Stub shared call.  */
   4832     &xcoff_stub_shared_call_code[0],
   4833     24,				/* _xcoff_stub_shared_call_size */
   4834   };
   4835 
   4836 /* The transfer vector that leads the outside world to all of the above.  */
   4837 const bfd_target powerpc_xcoff_vec =
   4838   {
   4839     "xcoff-powermac",
   4840     bfd_target_xcoff_flavour,
   4841     BFD_ENDIAN_BIG,		/* data byte order is big */
   4842     BFD_ENDIAN_BIG,		/* header byte order is big */
   4843 
   4844     (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
   4845      | HAS_SYMS | HAS_LOCALS | WP_TEXT),
   4846 
   4847     SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
   4848     0,				/* leading char */
   4849     '/',			/* ar_pad_char */
   4850     15,				/* ar_max_namelen */
   4851     0,				/* match priority.  */
   4852     TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols.  */
   4853 
   4854     /* data */
   4855     bfd_getb64,
   4856     bfd_getb_signed_64,
   4857     bfd_putb64,
   4858     bfd_getb32,
   4859     bfd_getb_signed_32,
   4860     bfd_putb32,
   4861     bfd_getb16,
   4862     bfd_getb_signed_16,
   4863     bfd_putb16,
   4864 
   4865     /* hdrs */
   4866     bfd_getb64,
   4867     bfd_getb_signed_64,
   4868     bfd_putb64,
   4869     bfd_getb32,
   4870     bfd_getb_signed_32,
   4871     bfd_putb32,
   4872     bfd_getb16,
   4873     bfd_getb_signed_16,
   4874     bfd_putb16,
   4875 
   4876     { /* bfd_check_format */
   4877       _bfd_dummy_target,
   4878       coff_object_p,
   4879       _bfd_xcoff_archive_p,
   4880       CORE_FILE_P
   4881     },
   4882 
   4883     { /* bfd_set_format */
   4884       _bfd_bool_bfd_false_error,
   4885       coff_mkobject,
   4886       _bfd_generic_mkarchive,
   4887       _bfd_bool_bfd_false_error
   4888     },
   4889 
   4890     {/* bfd_write_contents */
   4891       _bfd_bool_bfd_false_error,
   4892       coff_write_object_contents,
   4893       _bfd_xcoff_write_archive_contents,
   4894       _bfd_bool_bfd_false_error
   4895     },
   4896 
   4897     BFD_JUMP_TABLE_GENERIC (_bfd_xcoff),
   4898     BFD_JUMP_TABLE_COPY (_bfd_xcoff),
   4899     BFD_JUMP_TABLE_CORE (coff),
   4900     BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
   4901     BFD_JUMP_TABLE_SYMBOLS (_bfd_xcoff),
   4902     BFD_JUMP_TABLE_RELOCS (_bfd_xcoff),
   4903     BFD_JUMP_TABLE_WRITE (coff),
   4904     BFD_JUMP_TABLE_LINK (_bfd_xcoff),
   4905     BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
   4906 
   4907     /* Opposite endian version, none exists */
   4908     NULL,
   4909 
   4910     & bfd_pmac_xcoff_backend_data,
   4911   };
   4912