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