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