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