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