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