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