gas/
[external/binutils.git] / bfd / coff-rs6000.c
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 \f
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 \f
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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* 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   /* Modifiable branch relative.  */
1091   HOWTO (R_RBR,                 /* type */
1092          0,                     /* rightshift */
1093          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1094          16,                    /* bitsize */
1095          FALSE,                 /* 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          0xffff,                /* src_mask */
1102          0xffff,                /* dst_mask */
1103          FALSE),                /* pcrel_offset */
1104
1105   /* 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_32:
1170     case BFD_RELOC_CTOR:
1171       return &xcoff_howto_table[0];
1172     case BFD_RELOC_NONE:
1173       return &xcoff_howto_table[0xf];
1174     default:
1175       return NULL;
1176     }
1177 }
1178
1179 static reloc_howto_type *
1180 _bfd_xcoff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1181                               const char *r_name)
1182 {
1183   unsigned int i;
1184
1185   for (i = 0;
1186        i < sizeof (xcoff_howto_table) / sizeof (xcoff_howto_table[0]);
1187        i++)
1188     if (xcoff_howto_table[i].name != NULL
1189         && strcasecmp (xcoff_howto_table[i].name, r_name) == 0)
1190       return &xcoff_howto_table[i];
1191
1192   return NULL;
1193 }
1194 \f
1195 /* XCOFF archive support.  The original version of this code was by
1196    Damon A. Permezel.  It was enhanced to permit cross support, and
1197    writing archive files, by Ian Lance Taylor, Cygnus Support.
1198
1199    XCOFF uses its own archive format.  Everything is hooked together
1200    with file offset links, so it is possible to rapidly update an
1201    archive in place.  Of course, we don't do that.  An XCOFF archive
1202    has a real file header, not just an ARMAG string.  The structure of
1203    the file header and of each archive header appear below.
1204
1205    An XCOFF archive also has a member table, which is a list of
1206    elements in the archive (you can get that by looking through the
1207    linked list, but you have to read a lot more of the file).  The
1208    member table has a normal archive header with an empty name.  It is
1209    normally (and perhaps must be) the second to last entry in the
1210    archive.  The member table data is almost printable ASCII.  It
1211    starts with a 12 character decimal string which is the number of
1212    entries in the table.  For each entry it has a 12 character decimal
1213    string which is the offset in the archive of that member.  These
1214    entries are followed by a series of null terminated strings which
1215    are the member names for each entry.
1216
1217    Finally, an XCOFF archive has a global symbol table, which is what
1218    we call the armap.  The global symbol table has a normal archive
1219    header with an empty name.  It is normally (and perhaps must be)
1220    the last entry in the archive.  The contents start with a four byte
1221    binary number which is the number of entries.  This is followed by
1222    a that many four byte binary numbers; each is the file offset of an
1223    entry in the archive.  These numbers are followed by a series of
1224    null terminated strings, which are symbol names.
1225
1226    AIX 4.3 introduced a new archive format which can handle larger
1227    files and also 32- and 64-bit objects in the same archive.  The
1228    things said above remain true except that there is now more than
1229    one global symbol table.  The one is used to index 32-bit objects,
1230    the other for 64-bit objects.
1231
1232    The new archives (recognizable by the new ARMAG string) has larger
1233    field lengths so that we cannot really share any code.  Also we have
1234    to take care that we are not generating the new form of archives
1235    on AIX 4.2 or earlier systems.  */
1236
1237 /* XCOFF archives use this as a magic string.  Note that both strings
1238    have the same length.  */
1239
1240 /* Set the magic for archive.  */
1241
1242 bfd_boolean
1243 bfd_xcoff_ar_archive_set_magic (bfd *abfd ATTRIBUTE_UNUSED,
1244                                 char *magic ATTRIBUTE_UNUSED)
1245 {
1246   /* Not supported yet.  */
1247   return FALSE;
1248  /* bfd_xcoff_archive_set_magic (abfd, magic); */
1249 }
1250
1251 /* Read in the armap of an XCOFF archive.  */
1252
1253 bfd_boolean
1254 _bfd_xcoff_slurp_armap (bfd *abfd)
1255 {
1256   file_ptr off;
1257   size_t namlen;
1258   bfd_size_type sz;
1259   bfd_byte *contents, *cend;
1260   bfd_vma c, i;
1261   carsym *arsym;
1262   bfd_byte *p;
1263
1264   if (xcoff_ardata (abfd) == NULL)
1265     {
1266       bfd_has_map (abfd) = FALSE;
1267       return TRUE;
1268     }
1269
1270   if (! xcoff_big_format_p (abfd))
1271     {
1272       /* This is for the old format.  */
1273       struct xcoff_ar_hdr hdr;
1274
1275       off = strtol (xcoff_ardata (abfd)->symoff, (char **) NULL, 10);
1276       if (off == 0)
1277         {
1278           bfd_has_map (abfd) = FALSE;
1279           return TRUE;
1280         }
1281
1282       if (bfd_seek (abfd, off, SEEK_SET) != 0)
1283         return FALSE;
1284
1285       /* The symbol table starts with a normal archive header.  */
1286       if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
1287           != SIZEOF_AR_HDR)
1288         return FALSE;
1289
1290       /* Skip the name (normally empty).  */
1291       namlen = strtol (hdr.namlen, (char **) NULL, 10);
1292       off = ((namlen + 1) & ~ (size_t) 1) + SXCOFFARFMAG;
1293       if (bfd_seek (abfd, off, SEEK_CUR) != 0)
1294         return FALSE;
1295
1296       sz = strtol (hdr.size, (char **) NULL, 10);
1297
1298       /* Read in the entire symbol table.  */
1299       contents = (bfd_byte *) bfd_alloc (abfd, sz);
1300       if (contents == NULL)
1301         return FALSE;
1302       if (bfd_bread (contents, sz, abfd) != sz)
1303         return FALSE;
1304
1305       /* The symbol table starts with a four byte count.  */
1306       c = H_GET_32 (abfd, contents);
1307
1308       if (c * 4 >= sz)
1309         {
1310           bfd_set_error (bfd_error_bad_value);
1311           return FALSE;
1312         }
1313
1314       bfd_ardata (abfd)->symdefs =
1315         ((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
1316       if (bfd_ardata (abfd)->symdefs == NULL)
1317         return FALSE;
1318
1319       /* After the count comes a list of four byte file offsets.  */
1320       for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4;
1321            i < c;
1322            ++i, ++arsym, p += 4)
1323         arsym->file_offset = H_GET_32 (abfd, p);
1324     }
1325   else
1326     {
1327       /* This is for the new format.  */
1328       struct xcoff_ar_hdr_big hdr;
1329
1330       off = strtol (xcoff_ardata_big (abfd)->symoff, (char **) NULL, 10);
1331       if (off == 0)
1332         {
1333           bfd_has_map (abfd) = FALSE;
1334           return TRUE;
1335         }
1336
1337       if (bfd_seek (abfd, off, SEEK_SET) != 0)
1338         return FALSE;
1339
1340       /* The symbol table starts with a normal archive header.  */
1341       if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR_BIG, abfd)
1342           != SIZEOF_AR_HDR_BIG)
1343         return FALSE;
1344
1345       /* Skip the name (normally empty).  */
1346       namlen = strtol (hdr.namlen, (char **) NULL, 10);
1347       off = ((namlen + 1) & ~ (size_t) 1) + SXCOFFARFMAG;
1348       if (bfd_seek (abfd, off, SEEK_CUR) != 0)
1349         return FALSE;
1350
1351       /* XXX This actually has to be a call to strtoll (at least on 32-bit
1352          machines) since the field width is 20 and there numbers with more
1353          than 32 bits can be represented.  */
1354       sz = strtol (hdr.size, (char **) NULL, 10);
1355
1356       /* Read in the entire symbol table.  */
1357       contents = (bfd_byte *) bfd_alloc (abfd, sz);
1358       if (contents == NULL)
1359         return FALSE;
1360       if (bfd_bread (contents, sz, abfd) != sz)
1361         return FALSE;
1362
1363       /* The symbol table starts with an eight byte count.  */
1364       c = H_GET_64 (abfd, contents);
1365
1366       if (c * 8 >= sz)
1367         {
1368           bfd_set_error (bfd_error_bad_value);
1369           return FALSE;
1370         }
1371
1372       bfd_ardata (abfd)->symdefs =
1373         ((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
1374       if (bfd_ardata (abfd)->symdefs == NULL)
1375         return FALSE;
1376
1377       /* After the count comes a list of eight byte file offsets.  */
1378       for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 8;
1379            i < c;
1380            ++i, ++arsym, p += 8)
1381         arsym->file_offset = H_GET_64 (abfd, p);
1382     }
1383
1384   /* After the file offsets come null terminated symbol names.  */
1385   cend = contents + sz;
1386   for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
1387        i < c;
1388        ++i, ++arsym, p += strlen ((char *) p) + 1)
1389     {
1390       if (p >= cend)
1391         {
1392           bfd_set_error (bfd_error_bad_value);
1393           return FALSE;
1394         }
1395       arsym->name = (char *) p;
1396     }
1397
1398   bfd_ardata (abfd)->symdef_count = c;
1399   bfd_has_map (abfd) = TRUE;
1400
1401   return TRUE;
1402 }
1403
1404 /* See if this is an XCOFF archive.  */
1405
1406 const bfd_target *
1407 _bfd_xcoff_archive_p (bfd *abfd)
1408 {
1409   struct artdata *tdata_hold;
1410   char magic[SXCOFFARMAG];
1411   bfd_size_type amt = SXCOFFARMAG;
1412
1413   if (bfd_bread (magic, amt, abfd) != amt)
1414     {
1415       if (bfd_get_error () != bfd_error_system_call)
1416         bfd_set_error (bfd_error_wrong_format);
1417       return NULL;
1418     }
1419
1420   if (strncmp (magic, XCOFFARMAG, SXCOFFARMAG) != 0
1421       && strncmp (magic, XCOFFARMAGBIG, SXCOFFARMAG) != 0)
1422     {
1423       bfd_set_error (bfd_error_wrong_format);
1424       return NULL;
1425     }
1426
1427   tdata_hold = bfd_ardata (abfd);
1428
1429   amt = sizeof (struct artdata);
1430   bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt);
1431   if (bfd_ardata (abfd) == (struct artdata *) NULL)
1432     goto error_ret_restore;
1433
1434   /* Cleared by bfd_zalloc above.
1435      bfd_ardata (abfd)->cache = NULL;
1436      bfd_ardata (abfd)->archive_head = NULL;
1437      bfd_ardata (abfd)->symdefs = NULL;
1438      bfd_ardata (abfd)->extended_names = NULL;
1439      bfd_ardata (abfd)->extended_names_size = 0;  */
1440
1441   /* Now handle the two formats.  */
1442   if (magic[1] != 'b')
1443     {
1444       /* This is the old format.  */
1445       struct xcoff_ar_file_hdr hdr;
1446
1447       /* Copy over the magic string.  */
1448       memcpy (hdr.magic, magic, SXCOFFARMAG);
1449
1450       /* Now read the rest of the file header.  */
1451       amt = SIZEOF_AR_FILE_HDR - SXCOFFARMAG;
1452       if (bfd_bread (&hdr.memoff, amt, abfd) != amt)
1453         {
1454           if (bfd_get_error () != bfd_error_system_call)
1455             bfd_set_error (bfd_error_wrong_format);
1456           goto error_ret;
1457         }
1458
1459       bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff,
1460                                                       (char **) NULL, 10);
1461
1462       amt = SIZEOF_AR_FILE_HDR;
1463       bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
1464       if (bfd_ardata (abfd)->tdata == NULL)
1465         goto error_ret;
1466
1467       memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR);
1468     }
1469   else
1470     {
1471       /* This is the new format.  */
1472       struct xcoff_ar_file_hdr_big hdr;
1473
1474       /* Copy over the magic string.  */
1475       memcpy (hdr.magic, magic, SXCOFFARMAG);
1476
1477       /* Now read the rest of the file header.  */
1478       amt = SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG;
1479       if (bfd_bread (&hdr.memoff, amt, abfd) != amt)
1480         {
1481           if (bfd_get_error () != bfd_error_system_call)
1482             bfd_set_error (bfd_error_wrong_format);
1483           goto error_ret;
1484         }
1485
1486       bfd_ardata (abfd)->first_file_filepos = bfd_scan_vma (hdr.firstmemoff,
1487                                                             (const char **) 0,
1488                                                             10);
1489
1490       amt = SIZEOF_AR_FILE_HDR_BIG;
1491       bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
1492       if (bfd_ardata (abfd)->tdata == NULL)
1493         goto error_ret;
1494
1495       memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR_BIG);
1496     }
1497
1498   if (! _bfd_xcoff_slurp_armap (abfd))
1499     {
1500     error_ret:
1501       bfd_release (abfd, bfd_ardata (abfd));
1502     error_ret_restore:
1503       bfd_ardata (abfd) = tdata_hold;
1504       return NULL;
1505     }
1506
1507   return abfd->xvec;
1508 }
1509
1510 /* Read the archive header in an XCOFF archive.  */
1511
1512 void *
1513 _bfd_xcoff_read_ar_hdr (bfd *abfd)
1514 {
1515   bfd_size_type namlen;
1516   struct areltdata *ret;
1517   bfd_size_type amt = sizeof (struct areltdata);
1518
1519   ret = (struct areltdata *) bfd_zmalloc (amt);
1520   if (ret == NULL)
1521     return NULL;
1522
1523   if (! xcoff_big_format_p (abfd))
1524     {
1525       struct xcoff_ar_hdr hdr;
1526       struct xcoff_ar_hdr *hdrp;
1527
1528       if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
1529           != SIZEOF_AR_HDR)
1530         {
1531           free (ret);
1532           return NULL;
1533         }
1534
1535       namlen = strtol (hdr.namlen, (char **) NULL, 10);
1536       amt = SIZEOF_AR_HDR + namlen + 1;
1537       hdrp = (struct xcoff_ar_hdr *) bfd_alloc (abfd, amt);
1538       if (hdrp == NULL)
1539         {
1540           free (ret);
1541           return NULL;
1542         }
1543       memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
1544       if (bfd_bread ((char *) hdrp + SIZEOF_AR_HDR, namlen, abfd) != namlen)
1545         {
1546           free (ret);
1547           return NULL;
1548         }
1549       ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
1550
1551       ret->arch_header = (char *) hdrp;
1552       ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
1553       ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
1554     }
1555   else
1556     {
1557       struct xcoff_ar_hdr_big hdr;
1558       struct xcoff_ar_hdr_big *hdrp;
1559
1560       if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR_BIG, abfd)
1561           != SIZEOF_AR_HDR_BIG)
1562         {
1563           free (ret);
1564           return NULL;
1565         }
1566
1567       namlen = strtol (hdr.namlen, (char **) NULL, 10);
1568       amt = SIZEOF_AR_HDR_BIG + namlen + 1;
1569       hdrp = (struct xcoff_ar_hdr_big *) bfd_alloc (abfd, amt);
1570       if (hdrp == NULL)
1571         {
1572           free (ret);
1573           return NULL;
1574         }
1575       memcpy (hdrp, &hdr, SIZEOF_AR_HDR_BIG);
1576       if (bfd_bread ((char *) hdrp + SIZEOF_AR_HDR_BIG, namlen, abfd) != namlen)
1577         {
1578           free (ret);
1579           return NULL;
1580         }
1581       ((char *) hdrp)[SIZEOF_AR_HDR_BIG + namlen] = '\0';
1582
1583       ret->arch_header = (char *) hdrp;
1584       /* XXX This actually has to be a call to strtoll (at least on 32-bit
1585          machines) since the field width is 20 and there numbers with more
1586          than 32 bits can be represented.  */
1587       ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
1588       ret->filename = (char *) hdrp + SIZEOF_AR_HDR_BIG;
1589     }
1590
1591   /* Skip over the XCOFFARFMAG at the end of the file name.  */
1592   if (bfd_seek (abfd, (file_ptr) ((namlen & 1) + SXCOFFARFMAG), SEEK_CUR) != 0)
1593     return NULL;
1594
1595   return ret;
1596 }
1597
1598 /* Open the next element in an XCOFF archive.  */
1599
1600 bfd *
1601 _bfd_xcoff_openr_next_archived_file (bfd *archive, bfd *last_file)
1602 {
1603   file_ptr filestart;
1604
1605   if (xcoff_ardata (archive) == NULL)
1606     {
1607       bfd_set_error (bfd_error_invalid_operation);
1608       return NULL;
1609     }
1610
1611   if (! xcoff_big_format_p (archive))
1612     {
1613       if (last_file == NULL)
1614         filestart = bfd_ardata (archive)->first_file_filepos;
1615       else
1616         filestart = strtol (arch_xhdr (last_file)->nextoff, (char **) NULL,
1617                             10);
1618
1619       if (filestart == 0
1620           || filestart == strtol (xcoff_ardata (archive)->memoff,
1621                                   (char **) NULL, 10)
1622           || filestart == strtol (xcoff_ardata (archive)->symoff,
1623                                   (char **) NULL, 10))
1624         {
1625           bfd_set_error (bfd_error_no_more_archived_files);
1626           return NULL;
1627         }
1628     }
1629   else
1630     {
1631       if (last_file == NULL)
1632         filestart = bfd_ardata (archive)->first_file_filepos;
1633       else
1634         /* XXX These actually have to be a calls to strtoll (at least
1635            on 32-bit machines) since the fields's width is 20 and
1636            there numbers with more than 32 bits can be represented.  */
1637         filestart = strtol (arch_xhdr_big (last_file)->nextoff, (char **) NULL,
1638                             10);
1639
1640       /* XXX These actually have to be calls to strtoll (at least on 32-bit
1641          machines) since the fields's width is 20 and there numbers with more
1642          than 32 bits can be represented.  */
1643       if (filestart == 0
1644           || filestart == strtol (xcoff_ardata_big (archive)->memoff,
1645                                   (char **) NULL, 10)
1646           || filestart == strtol (xcoff_ardata_big (archive)->symoff,
1647                                   (char **) NULL, 10))
1648         {
1649           bfd_set_error (bfd_error_no_more_archived_files);
1650           return NULL;
1651         }
1652     }
1653
1654   return _bfd_get_elt_at_filepos (archive, filestart);
1655 }
1656
1657 /* Stat an element in an XCOFF archive.  */
1658
1659 int
1660 _bfd_xcoff_stat_arch_elt (bfd *abfd, struct stat *s)
1661 {
1662   if (abfd->arelt_data == NULL)
1663     {
1664       bfd_set_error (bfd_error_invalid_operation);
1665       return -1;
1666     }
1667
1668   if (! xcoff_big_format_p (abfd->my_archive))
1669     {
1670       struct xcoff_ar_hdr *hdrp = arch_xhdr (abfd);
1671
1672       s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
1673       s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
1674       s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
1675       s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
1676       s->st_size = arch_eltdata (abfd)->parsed_size;
1677     }
1678   else
1679     {
1680       struct xcoff_ar_hdr_big *hdrp = arch_xhdr_big (abfd);
1681
1682       s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
1683       s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
1684       s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
1685       s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
1686       s->st_size = arch_eltdata (abfd)->parsed_size;
1687     }
1688
1689   return 0;
1690 }
1691
1692 /* Normalize a file name for inclusion in an archive.  */
1693
1694 static const char *
1695 normalize_filename (bfd *abfd)
1696 {
1697   const char *file;
1698   const char *filename;
1699
1700   file = bfd_get_filename (abfd);
1701   filename = strrchr (file, '/');
1702   if (filename != NULL)
1703     filename++;
1704   else
1705     filename = file;
1706   return filename;
1707 }
1708
1709 /* Write out an XCOFF armap.  */
1710
1711 static bfd_boolean
1712 xcoff_write_armap_old (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
1713                        struct orl *map, unsigned int orl_count, int stridx)
1714 {
1715   struct archive_iterator iterator;
1716   struct xcoff_ar_hdr hdr;
1717   char *p;
1718   unsigned char buf[4];
1719   unsigned int i;
1720
1721   memset (&hdr, 0, sizeof hdr);
1722   sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
1723   sprintf (hdr.nextoff, "%d", 0);
1724   memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, XCOFFARMAG_ELEMENT_SIZE);
1725   sprintf (hdr.date, "%d", 0);
1726   sprintf (hdr.uid, "%d", 0);
1727   sprintf (hdr.gid, "%d", 0);
1728   sprintf (hdr.mode, "%d", 0);
1729   sprintf (hdr.namlen, "%d", 0);
1730
1731   /* We need spaces, not null bytes, in the header.  */
1732   for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
1733     if (*p == '\0')
1734       *p = ' ';
1735
1736   if (bfd_bwrite (&hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
1737       != SIZEOF_AR_HDR
1738       || (bfd_bwrite (XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG, abfd)
1739           != SXCOFFARFMAG))
1740     return FALSE;
1741
1742   H_PUT_32 (abfd, orl_count, buf);
1743   if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
1744     return FALSE;
1745
1746   i = 0;
1747   archive_iterator_begin (&iterator, abfd);
1748   while (i < orl_count && archive_iterator_next (&iterator))
1749     while (map[i].u.abfd == iterator.current.member)
1750       {
1751         H_PUT_32 (abfd, iterator.current.offset, buf);
1752         if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
1753           return FALSE;
1754         ++i;
1755       }
1756
1757   for (i = 0; i < orl_count; i++)
1758     {
1759       const char *name;
1760       size_t namlen;
1761
1762       name = *map[i].name;
1763       namlen = strlen (name);
1764       if (bfd_bwrite (name, (bfd_size_type) (namlen + 1), abfd) != namlen + 1)
1765         return FALSE;
1766     }
1767
1768   if ((stridx & 1) != 0)
1769     {
1770       char b;
1771
1772       b = '\0';
1773       if (bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
1774         return FALSE;
1775     }
1776
1777   return TRUE;
1778 }
1779
1780 static char buff20[XCOFFARMAGBIG_ELEMENT_SIZE + 1];
1781 #define FMT20  "%-20lld"
1782 #define FMT12  "%-12d"
1783 #define FMT12_OCTAL  "%-12o"
1784 #define FMT4  "%-4d"
1785 #define PRINT20(d, v) \
1786   sprintf (buff20, FMT20, (long long)(v)), \
1787   memcpy ((void *) (d), buff20, 20)
1788
1789 #define PRINT12(d, v) \
1790   sprintf (buff20, FMT12, (int)(v)), \
1791   memcpy ((void *) (d), buff20, 12)
1792
1793 #define PRINT12_OCTAL(d, v) \
1794   sprintf (buff20, FMT12_OCTAL, (unsigned int)(v)), \
1795   memcpy ((void *) (d), buff20, 12)
1796
1797 #define PRINT4(d, v) \
1798   sprintf (buff20, FMT4, (int)(v)), \
1799   memcpy ((void *) (d), buff20, 4)
1800
1801 #define READ20(d, v) \
1802   buff20[20] = 0, \
1803   memcpy (buff20, (d), 20), \
1804   (v) = bfd_scan_vma (buff20, (const char **) NULL, 10)
1805
1806 static bfd_boolean
1807 do_pad (bfd *abfd, unsigned int number)
1808 {
1809   bfd_byte b = 0;
1810
1811   /* Limit pad to <= 4096.  */
1812   if (number > 4096)
1813     return FALSE;
1814
1815   while (number--)
1816     if (bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
1817       return FALSE;
1818
1819   return TRUE;
1820 }
1821
1822 static bfd_boolean
1823 do_copy (bfd *out_bfd, bfd *in_bfd)
1824 {
1825   bfd_size_type remaining;
1826   bfd_byte buffer[DEFAULT_BUFFERSIZE];
1827
1828   if (bfd_seek (in_bfd, (file_ptr) 0, SEEK_SET) != 0)
1829     return FALSE;
1830
1831   remaining = arelt_size (in_bfd);
1832
1833   while (remaining >= DEFAULT_BUFFERSIZE)
1834     {
1835       if (bfd_bread (buffer, DEFAULT_BUFFERSIZE, in_bfd) != DEFAULT_BUFFERSIZE
1836           || bfd_bwrite (buffer, DEFAULT_BUFFERSIZE, out_bfd) != DEFAULT_BUFFERSIZE)
1837         return FALSE;
1838
1839       remaining -= DEFAULT_BUFFERSIZE;
1840     }
1841
1842   if (remaining)
1843     {
1844       if (bfd_bread (buffer, remaining, in_bfd) != remaining
1845           || bfd_bwrite (buffer, remaining, out_bfd) != remaining)
1846         return FALSE;
1847     }
1848
1849   return TRUE;
1850 }
1851
1852 static bfd_boolean
1853 xcoff_write_armap_big (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
1854                        struct orl *map, unsigned int orl_count, int stridx)
1855 {
1856   struct archive_iterator iterator;
1857   struct xcoff_ar_file_hdr_big *fhdr;
1858   bfd_vma i, sym_32, sym_64, str_32, str_64;
1859   const bfd_arch_info_type *arch_info;
1860   bfd *current_bfd;
1861   size_t string_length;
1862   file_ptr nextoff, prevoff;
1863
1864   /* First, we look through the symbols and work out which are
1865      from 32-bit objects and which from 64-bit ones.  */
1866   sym_32 = sym_64 = str_32 = str_64 = 0;
1867
1868   i = 0;
1869   for (current_bfd = abfd->archive_head;
1870        current_bfd != NULL && i < orl_count;
1871        current_bfd = current_bfd->archive_next)
1872     {
1873       arch_info = bfd_get_arch_info (current_bfd);
1874       while (map[i].u.abfd == current_bfd)
1875         {
1876           string_length = strlen (*map[i].name) + 1;
1877           if (arch_info->bits_per_address == 64)
1878             {
1879               sym_64++;
1880               str_64 += string_length;
1881             }
1882           else
1883             {
1884               sym_32++;
1885               str_32 += string_length;
1886             }
1887           i++;
1888         }
1889     }
1890
1891   /* A quick sanity check... */
1892   BFD_ASSERT (sym_64 + sym_32 == orl_count);
1893   /* Explicit cast to int for compiler.  */
1894   BFD_ASSERT ((int)(str_64 + str_32) == stridx);
1895
1896   fhdr = xcoff_ardata_big (abfd);
1897
1898   /* xcoff_write_archive_contents_big passes nextoff in symoff. */
1899   READ20 (fhdr->memoff, prevoff);
1900   READ20 (fhdr->symoff, nextoff);
1901
1902   BFD_ASSERT (nextoff == bfd_tell (abfd));
1903
1904   /* Write out the symbol table.
1905      Layout :
1906
1907      standard big archive header
1908      0x0000                   ar_size   [0x14]
1909      0x0014                   ar_nxtmem [0x14]
1910      0x0028                   ar_prvmem [0x14]
1911      0x003C                   ar_date   [0x0C]
1912      0x0048                   ar_uid    [0x0C]
1913      0x0054                   ar_gid    [0x0C]
1914      0x0060                   ar_mod    [0x0C]
1915      0x006C                   ar_namelen[0x04]
1916      0x0070                   ar_fmag   [SXCOFFARFMAG]
1917
1918      Symbol table
1919      0x0072                   num_syms  [0x08], binary
1920      0x0078                   offsets   [0x08 * num_syms], binary
1921      0x0086 + 0x08 * num_syms names     [??]
1922      ??                       pad to even bytes.
1923   */
1924
1925   if (sym_32)
1926     {
1927       struct xcoff_ar_hdr_big *hdr;
1928       char *symbol_table;
1929       char *st;
1930
1931       bfd_vma symbol_table_size =
1932         SIZEOF_AR_HDR_BIG
1933         + SXCOFFARFMAG
1934         + 8
1935         + 8 * sym_32
1936         + str_32 + (str_32 & 1);
1937
1938       symbol_table = bfd_zmalloc (symbol_table_size);
1939       if (symbol_table == NULL)
1940         return FALSE;
1941
1942       hdr = (struct xcoff_ar_hdr_big *) symbol_table;
1943
1944       PRINT20 (hdr->size, 8 + 8 * sym_32 + str_32 + (str_32 & 1));
1945
1946       if (sym_64)
1947         PRINT20 (hdr->nextoff, nextoff + symbol_table_size);
1948       else
1949         PRINT20 (hdr->nextoff, 0);
1950
1951       PRINT20 (hdr->prevoff, prevoff);
1952       PRINT12 (hdr->date, 0);
1953       PRINT12 (hdr->uid, 0);
1954       PRINT12 (hdr->gid, 0);
1955       PRINT12 (hdr->mode, 0);
1956       PRINT4 (hdr->namlen, 0) ;
1957
1958       st = symbol_table + SIZEOF_AR_HDR_BIG;
1959       memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
1960       st += SXCOFFARFMAG;
1961
1962       bfd_h_put_64 (abfd, sym_32, st);
1963       st += 8;
1964
1965       /* loop over the 32 bit offsets */
1966       i = 0;
1967       archive_iterator_begin (&iterator, abfd);
1968       while (i < orl_count && archive_iterator_next (&iterator))
1969         {
1970           arch_info = bfd_get_arch_info (iterator.current.member);
1971           while (map[i].u.abfd == iterator.current.member)
1972             {
1973               if (arch_info->bits_per_address == 32)
1974                 {
1975                   bfd_h_put_64 (abfd, iterator.current.offset, st);
1976                   st += 8;
1977                 }
1978               i++;
1979             }
1980         }
1981
1982       /* loop over the 32 bit symbol names */
1983       i = 0;
1984       for (current_bfd = abfd->archive_head;
1985            current_bfd != NULL && i < orl_count;
1986            current_bfd = current_bfd->archive_next)
1987         {
1988           arch_info = bfd_get_arch_info (current_bfd);
1989           while (map[i].u.abfd == current_bfd)
1990             {
1991               if (arch_info->bits_per_address == 32)
1992                 {
1993                   string_length = sprintf (st, "%s", *map[i].name);
1994                   st += string_length + 1;
1995                 }
1996               i++;
1997             }
1998         }
1999
2000       bfd_bwrite (symbol_table, symbol_table_size, abfd);
2001
2002       free (symbol_table);
2003
2004       prevoff = nextoff;
2005       nextoff = nextoff + symbol_table_size;
2006     }
2007   else
2008     PRINT20 (fhdr->symoff, 0);
2009
2010   if (sym_64)
2011     {
2012       struct xcoff_ar_hdr_big *hdr;
2013       char *symbol_table;
2014       char *st;
2015
2016       bfd_vma symbol_table_size =
2017         SIZEOF_AR_HDR_BIG
2018         + SXCOFFARFMAG
2019         + 8
2020         + 8 * sym_64
2021         + str_64 + (str_64 & 1);
2022
2023       symbol_table = bfd_zmalloc (symbol_table_size);
2024       if (symbol_table == NULL)
2025         return FALSE;
2026
2027       hdr = (struct xcoff_ar_hdr_big *) symbol_table;
2028
2029       PRINT20 (hdr->size, 8 + 8 * sym_64 + str_64 + (str_64 & 1));
2030       PRINT20 (hdr->nextoff, 0);
2031       PRINT20 (hdr->prevoff, prevoff);
2032       PRINT12 (hdr->date, 0);
2033       PRINT12 (hdr->uid, 0);
2034       PRINT12 (hdr->gid, 0);
2035       PRINT12 (hdr->mode, 0);
2036       PRINT4 (hdr->namlen, 0);
2037
2038       st = symbol_table + SIZEOF_AR_HDR_BIG;
2039       memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
2040       st += SXCOFFARFMAG;
2041
2042       bfd_h_put_64 (abfd, sym_64, st);
2043       st += 8;
2044
2045       /* loop over the 64 bit offsets */
2046       i = 0;
2047       archive_iterator_begin (&iterator, abfd);
2048       while (i < orl_count && archive_iterator_next (&iterator))
2049         {
2050           arch_info = bfd_get_arch_info (iterator.current.member);
2051           while (map[i].u.abfd == iterator.current.member)
2052             {
2053               if (arch_info->bits_per_address == 64)
2054                 {
2055                   bfd_h_put_64 (abfd, iterator.current.offset, st);
2056                   st += 8;
2057                 }
2058               i++;
2059             }
2060         }
2061
2062       /* loop over the 64 bit symbol names */
2063       i = 0;
2064       for (current_bfd = abfd->archive_head;
2065            current_bfd != NULL && i < orl_count;
2066            current_bfd = current_bfd->archive_next)
2067         {
2068           arch_info = bfd_get_arch_info (current_bfd);
2069           while (map[i].u.abfd == current_bfd)
2070             {
2071               if (arch_info->bits_per_address == 64)
2072                 {
2073                   string_length = sprintf (st, "%s", *map[i].name);
2074                   st += string_length + 1;
2075                 }
2076               i++;
2077             }
2078         }
2079
2080       bfd_bwrite (symbol_table, symbol_table_size, abfd);
2081
2082       free (symbol_table);
2083
2084       PRINT20 (fhdr->symoff64, nextoff);
2085     }
2086   else
2087     PRINT20 (fhdr->symoff64, 0);
2088
2089   return TRUE;
2090 }
2091
2092 bfd_boolean
2093 _bfd_xcoff_write_armap (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
2094                         struct orl *map, unsigned int orl_count, int stridx)
2095 {
2096   if (! xcoff_big_format_p (abfd))
2097     return xcoff_write_armap_old (abfd, elength, map, orl_count, stridx);
2098   else
2099     return xcoff_write_armap_big (abfd, elength, map, orl_count, stridx);
2100 }
2101
2102 /* Write out an XCOFF archive.  We always write an entire archive,
2103    rather than fussing with the freelist and so forth.  */
2104
2105 static bfd_boolean
2106 xcoff_write_archive_contents_old (bfd *abfd)
2107 {
2108   struct archive_iterator iterator;
2109   struct xcoff_ar_file_hdr fhdr;
2110   bfd_size_type count;
2111   bfd_size_type total_namlen;
2112   file_ptr *offsets;
2113   bfd_boolean makemap;
2114   bfd_boolean hasobjects;
2115   file_ptr prevoff, nextoff;
2116   bfd *sub;
2117   size_t i;
2118   struct xcoff_ar_hdr ahdr;
2119   bfd_size_type size;
2120   char *p;
2121   char decbuf[XCOFFARMAG_ELEMENT_SIZE + 1];
2122
2123   memset (&fhdr, 0, sizeof fhdr);
2124   (void) strncpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);
2125   sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR);
2126   sprintf (fhdr.freeoff, "%d", 0);
2127
2128   count = 0;
2129   total_namlen = 0;
2130   for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
2131     {
2132       ++count;
2133       total_namlen += strlen (normalize_filename (sub)) + 1;
2134       if (sub->arelt_data == NULL)
2135         {
2136           sub->arelt_data = bfd_zmalloc (sizeof (struct areltdata));
2137           if (sub->arelt_data == NULL)
2138             return FALSE;
2139         }
2140       if (arch_xhdr (sub) == NULL)
2141         {
2142           struct xcoff_ar_hdr *ahdrp;
2143           struct stat s;
2144
2145           if (stat (bfd_get_filename (sub), &s) != 0)
2146             {
2147               bfd_set_error (bfd_error_system_call);
2148               return FALSE;
2149             }
2150
2151           ahdrp = bfd_zalloc (sub, sizeof (*ahdrp));
2152           if (ahdrp == NULL)
2153             return FALSE;
2154
2155           sprintf (ahdrp->size, "%ld", (long) s.st_size);
2156           sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
2157           sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
2158           sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
2159           sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
2160
2161           arch_eltdata (sub)->arch_header = (char *) ahdrp;
2162           arch_eltdata (sub)->parsed_size = s.st_size;
2163         }
2164     }
2165   offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
2166   if (offsets == NULL)
2167     return FALSE;
2168
2169   if (bfd_seek (abfd, (file_ptr) SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
2170     return FALSE;
2171
2172   makemap = bfd_has_map (abfd);
2173   hasobjects = FALSE;
2174   prevoff = 0;
2175   for (archive_iterator_begin (&iterator, abfd), i = 0;
2176        archive_iterator_next (&iterator);
2177        i++)
2178     {
2179       bfd_size_type namlen;
2180       struct xcoff_ar_hdr *ahdrp;
2181
2182       if (makemap && ! hasobjects)
2183         {
2184           if (bfd_check_format (iterator.current.member, bfd_object))
2185             hasobjects = TRUE;
2186         }
2187
2188       ahdrp = arch_xhdr (iterator.current.member);
2189       sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
2190       sprintf (ahdrp->namlen, "%ld", (long) iterator.current.namlen);
2191       sprintf (ahdrp->nextoff, "%ld", (long) iterator.next.offset);
2192
2193       /* We need spaces, not null bytes, in the header.  */
2194       for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
2195         if (*p == '\0')
2196           *p = ' ';
2197
2198       if (!do_pad (abfd, iterator.current.leading_padding))
2199         return FALSE;
2200
2201       BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
2202       namlen = iterator.current.padded_namlen;
2203       if (bfd_bwrite (ahdrp, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
2204           || bfd_bwrite (iterator.current.name, namlen, abfd) != namlen
2205           || bfd_bwrite (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
2206           || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
2207           || !do_copy (abfd, iterator.current.member)
2208           || !do_pad (abfd, iterator.current.trailing_padding))
2209         return FALSE;
2210
2211       offsets[i] = iterator.current.offset;
2212       prevoff = iterator.current.offset;
2213     }
2214
2215   sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
2216
2217   /* Write out the member table.  */
2218
2219   nextoff = iterator.next.offset;
2220   BFD_ASSERT (nextoff == bfd_tell (abfd));
2221   sprintf (fhdr.memoff, "%ld", (long) nextoff);
2222
2223   memset (&ahdr, 0, sizeof ahdr);
2224   sprintf (ahdr.size, "%ld", (long) (XCOFFARMAG_ELEMENT_SIZE
2225                                      + count * XCOFFARMAG_ELEMENT_SIZE
2226                                      + total_namlen));
2227   sprintf (ahdr.prevoff, "%ld", (long) prevoff);
2228   sprintf (ahdr.date, "%d", 0);
2229   sprintf (ahdr.uid, "%d", 0);
2230   sprintf (ahdr.gid, "%d", 0);
2231   sprintf (ahdr.mode, "%d", 0);
2232   sprintf (ahdr.namlen, "%d", 0);
2233
2234   size = (SIZEOF_AR_HDR
2235           + XCOFFARMAG_ELEMENT_SIZE
2236           + count * XCOFFARMAG_ELEMENT_SIZE
2237           + total_namlen
2238           + SXCOFFARFMAG);
2239
2240   prevoff = nextoff;
2241   nextoff += size + (size & 1);
2242
2243   if (makemap && hasobjects)
2244     sprintf (ahdr.nextoff, "%ld", (long) nextoff);
2245   else
2246     sprintf (ahdr.nextoff, "%d", 0);
2247
2248   /* We need spaces, not null bytes, in the header.  */
2249   for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
2250     if (*p == '\0')
2251       *p = ' ';
2252
2253   if ((bfd_bwrite (&ahdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
2254        != SIZEOF_AR_HDR)
2255       || (bfd_bwrite (XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG, abfd)
2256           != SXCOFFARFMAG))
2257     return FALSE;
2258
2259   sprintf (decbuf, "%-12ld", (long) count);
2260   if (bfd_bwrite (decbuf, (bfd_size_type) XCOFFARMAG_ELEMENT_SIZE, abfd)
2261       != XCOFFARMAG_ELEMENT_SIZE)
2262     return FALSE;
2263   for (i = 0; i < (size_t) count; i++)
2264     {
2265       sprintf (decbuf, "%-12ld", (long) offsets[i]);
2266       if (bfd_bwrite (decbuf, (bfd_size_type) XCOFFARMAG_ELEMENT_SIZE,
2267                       abfd) != XCOFFARMAG_ELEMENT_SIZE)
2268         return FALSE;
2269     }
2270   for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
2271     {
2272       const char *name;
2273       bfd_size_type namlen;
2274
2275       name = normalize_filename (sub);
2276       namlen = strlen (name);
2277       if (bfd_bwrite (name, namlen + 1, abfd) != namlen + 1)
2278         return FALSE;
2279     }
2280
2281   if (! do_pad (abfd, size & 1))
2282     return FALSE;
2283
2284   /* Write out the armap, if appropriate.  */
2285   if (! makemap || ! hasobjects)
2286     sprintf (fhdr.symoff, "%d", 0);
2287   else
2288     {
2289       BFD_ASSERT (nextoff == bfd_tell (abfd));
2290       sprintf (fhdr.symoff, "%ld", (long) nextoff);
2291       bfd_ardata (abfd)->tdata = &fhdr;
2292       if (! _bfd_compute_and_write_armap (abfd, 0))
2293         return FALSE;
2294     }
2295
2296   /* Write out the archive file header.  */
2297
2298   /* We need spaces, not null bytes, in the header.  */
2299   for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR; p++)
2300     if (*p == '\0')
2301       *p = ' ';
2302
2303   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2304       || (bfd_bwrite (&fhdr, (bfd_size_type) SIZEOF_AR_FILE_HDR, abfd)
2305           != SIZEOF_AR_FILE_HDR))
2306     return FALSE;
2307
2308   return TRUE;
2309 }
2310
2311 static bfd_boolean
2312 xcoff_write_archive_contents_big (bfd *abfd)
2313 {
2314   struct xcoff_ar_file_hdr_big fhdr;
2315   bfd_size_type count;
2316   bfd_size_type total_namlen;
2317   file_ptr *offsets;
2318   bfd_boolean makemap;
2319   bfd_boolean hasobjects;
2320   file_ptr prevoff, nextoff;
2321   bfd *current_bfd;
2322   size_t i;
2323   struct xcoff_ar_hdr_big *hdr;
2324   bfd_size_type size;
2325   char *member_table, *mt;
2326   bfd_vma member_table_size;
2327   struct archive_iterator iterator;
2328
2329   memset (&fhdr, 0, SIZEOF_AR_FILE_HDR_BIG);
2330   memcpy (fhdr.magic, XCOFFARMAGBIG, SXCOFFARMAG);
2331
2332   if (bfd_seek (abfd, (file_ptr) SIZEOF_AR_FILE_HDR_BIG, SEEK_SET) != 0)
2333     return FALSE;
2334
2335   /* Calculate count and total_namlen.  */
2336   makemap = bfd_has_map (abfd);
2337   hasobjects = FALSE;
2338   for (current_bfd = abfd->archive_head, count = 0, total_namlen = 0;
2339        current_bfd != NULL;
2340        current_bfd = current_bfd->archive_next, count++)
2341     {
2342       total_namlen += strlen (normalize_filename (current_bfd)) + 1;
2343
2344       if (makemap
2345           && ! hasobjects
2346           && bfd_check_format (current_bfd, bfd_object))
2347         hasobjects = TRUE;
2348
2349       if (current_bfd->arelt_data == NULL)
2350         {
2351           size = sizeof (struct areltdata);
2352           current_bfd->arelt_data = bfd_zmalloc (size);
2353           if (current_bfd->arelt_data == NULL)
2354             return FALSE;
2355         }
2356
2357       if (arch_xhdr_big (current_bfd) == NULL)
2358         {
2359           struct xcoff_ar_hdr_big *ahdrp;
2360           struct stat s;
2361
2362           /* XXX This should actually be a call to stat64 (at least on
2363              32-bit machines).
2364              XXX This call will fail if the original object is not found.  */
2365           if (stat (bfd_get_filename (current_bfd), &s) != 0)
2366             {
2367               bfd_set_error (bfd_error_system_call);
2368               return FALSE;
2369             }
2370
2371           ahdrp = bfd_zalloc (current_bfd, sizeof (*ahdrp));
2372           if (ahdrp == NULL)
2373             return FALSE;
2374
2375           PRINT20 (ahdrp->size, s.st_size);
2376           PRINT12 (ahdrp->date, s.st_mtime);
2377           PRINT12 (ahdrp->uid,  s.st_uid);
2378           PRINT12 (ahdrp->gid,  s.st_gid);
2379           PRINT12_OCTAL (ahdrp->mode, s.st_mode);
2380
2381           arch_eltdata (current_bfd)->arch_header = (char *) ahdrp;
2382           arch_eltdata (current_bfd)->parsed_size = s.st_size;
2383         }
2384     }
2385
2386   offsets = NULL;
2387   if (count)
2388     {
2389       offsets = (file_ptr *) bfd_malloc (count * sizeof (file_ptr));
2390       if (offsets == NULL)
2391         return FALSE;
2392     }
2393
2394   prevoff = 0;
2395   for (archive_iterator_begin (&iterator, abfd), i = 0;
2396        archive_iterator_next (&iterator);
2397        i++)
2398     {
2399       bfd_size_type namlen;
2400       struct xcoff_ar_hdr_big *ahdrp;
2401
2402       ahdrp = arch_xhdr_big (iterator.current.member);
2403       PRINT20 (ahdrp->prevoff, prevoff);
2404       PRINT4 (ahdrp->namlen, iterator.current.namlen);
2405       PRINT20 (ahdrp->nextoff, iterator.next.offset);
2406
2407       if (!do_pad (abfd, iterator.current.leading_padding))
2408         return FALSE;
2409
2410       BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
2411       namlen = iterator.current.padded_namlen;
2412       if (bfd_bwrite (ahdrp, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG
2413           || bfd_bwrite (iterator.current.name, namlen, abfd) != namlen
2414           || bfd_bwrite (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
2415           || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
2416           || !do_copy (abfd, iterator.current.member)
2417           || !do_pad (abfd, iterator.current.trailing_padding))
2418         return FALSE;
2419
2420       offsets[i] = iterator.current.offset;
2421       prevoff = iterator.current.offset;
2422     }
2423
2424   if (count)
2425     {
2426       PRINT20 (fhdr.firstmemoff, offsets[0]);
2427       PRINT20 (fhdr.lastmemoff, prevoff);
2428     }
2429
2430   /* Write out the member table.
2431      Layout :
2432
2433      standard big archive header
2434      0x0000                   ar_size   [0x14]
2435      0x0014                   ar_nxtmem [0x14]
2436      0x0028                   ar_prvmem [0x14]
2437      0x003C                   ar_date   [0x0C]
2438      0x0048                   ar_uid    [0x0C]
2439      0x0054                   ar_gid    [0x0C]
2440      0x0060                   ar_mod    [0x0C]
2441      0x006C                   ar_namelen[0x04]
2442      0x0070                   ar_fmag   [0x02]
2443
2444      Member table
2445      0x0072                   count     [0x14]
2446      0x0086                   offsets   [0x14 * counts]
2447      0x0086 + 0x14 * counts   names     [??]
2448      ??                       pad to even bytes.
2449    */
2450
2451   nextoff = iterator.next.offset;
2452   BFD_ASSERT (nextoff == bfd_tell (abfd));
2453
2454   member_table_size = (SIZEOF_AR_HDR_BIG
2455                        + SXCOFFARFMAG
2456                        + XCOFFARMAGBIG_ELEMENT_SIZE
2457                        + count * XCOFFARMAGBIG_ELEMENT_SIZE
2458                        + total_namlen);
2459
2460   member_table_size += member_table_size & 1;
2461   member_table = bfd_zmalloc (member_table_size);
2462   if (member_table == NULL)
2463     return FALSE;
2464
2465   hdr = (struct xcoff_ar_hdr_big *) member_table;
2466
2467   PRINT20 (hdr->size, (XCOFFARMAGBIG_ELEMENT_SIZE
2468                        + count * XCOFFARMAGBIG_ELEMENT_SIZE
2469                        + total_namlen + (total_namlen & 1)));
2470   if (makemap && hasobjects)
2471     PRINT20 (hdr->nextoff, nextoff + member_table_size);
2472   else
2473     PRINT20 (hdr->nextoff, 0);
2474   PRINT20 (hdr->prevoff, prevoff);
2475   PRINT12 (hdr->date, 0);
2476   PRINT12 (hdr->uid, 0);
2477   PRINT12 (hdr->gid, 0);
2478   PRINT12 (hdr->mode, 0);
2479   PRINT4 (hdr->namlen, 0);
2480
2481   mt = member_table + SIZEOF_AR_HDR_BIG;
2482   memcpy (mt, XCOFFARFMAG, SXCOFFARFMAG);
2483   mt += SXCOFFARFMAG;
2484
2485   PRINT20 (mt, count);
2486   mt += XCOFFARMAGBIG_ELEMENT_SIZE;
2487   for (i = 0; i < (size_t) count; i++)
2488     {
2489       PRINT20 (mt, offsets[i]);
2490       mt += XCOFFARMAGBIG_ELEMENT_SIZE;
2491     }
2492
2493   if (count)
2494     {
2495       free (offsets);
2496       offsets = NULL;
2497     }
2498
2499   for (current_bfd = abfd->archive_head;
2500        current_bfd != NULL;
2501        current_bfd = current_bfd->archive_next)
2502     {
2503       const char *name;
2504       size_t namlen;
2505
2506       name = normalize_filename (current_bfd);
2507       namlen = sprintf (mt, "%s", name);
2508       mt += namlen + 1;
2509     }
2510
2511   if (bfd_bwrite (member_table, member_table_size, abfd) != member_table_size)
2512     return FALSE;
2513
2514   free (member_table);
2515
2516   PRINT20 (fhdr.memoff, nextoff);
2517
2518   prevoff = nextoff;
2519   nextoff += member_table_size;
2520
2521   /* Write out the armap, if appropriate.  */
2522
2523   if (! makemap || ! hasobjects)
2524     PRINT20 (fhdr.symoff, 0);
2525   else
2526     {
2527       BFD_ASSERT (nextoff == bfd_tell (abfd));
2528
2529       /* Save nextoff in fhdr.symoff so the armap routine can use it.  */
2530       PRINT20 (fhdr.symoff, nextoff);
2531
2532       bfd_ardata (abfd)->tdata = &fhdr;
2533       if (! _bfd_compute_and_write_armap (abfd, 0))
2534         return FALSE;
2535     }
2536
2537   /* Write out the archive file header.  */
2538
2539   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2540       || (bfd_bwrite (&fhdr, (bfd_size_type) SIZEOF_AR_FILE_HDR_BIG,
2541                       abfd) != SIZEOF_AR_FILE_HDR_BIG))
2542     return FALSE;
2543
2544   return TRUE;
2545 }
2546
2547 bfd_boolean
2548 _bfd_xcoff_write_archive_contents (bfd *abfd)
2549 {
2550   if (! xcoff_big_format_p (abfd))
2551     return xcoff_write_archive_contents_old (abfd);
2552   else
2553     return xcoff_write_archive_contents_big (abfd);
2554 }
2555 \f
2556 /* We can't use the usual coff_sizeof_headers routine, because AIX
2557    always uses an a.out header.  */
2558
2559 int
2560 _bfd_xcoff_sizeof_headers (bfd *abfd,
2561                            struct bfd_link_info *info ATTRIBUTE_UNUSED)
2562 {
2563   int size;
2564
2565   size = FILHSZ;
2566   if (xcoff_data (abfd)->full_aouthdr)
2567     size += AOUTSZ;
2568   else
2569     size += SMALL_AOUTSZ;
2570   size += abfd->section_count * SCNHSZ;
2571   return size;
2572 }
2573 \f
2574 /* Routines to swap information in the XCOFF .loader section.  If we
2575    ever need to write an XCOFF loader, this stuff will need to be
2576    moved to another file shared by the linker (which XCOFF calls the
2577    ``binder'') and the loader.  */
2578
2579 /* Swap in the ldhdr structure.  */
2580
2581 static void
2582 xcoff_swap_ldhdr_in (bfd *abfd, const void * s, struct internal_ldhdr *dst)
2583 {
2584   const struct external_ldhdr *src = (const struct external_ldhdr *) s;
2585
2586   dst->l_version = bfd_get_32 (abfd, src->l_version);
2587   dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
2588   dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
2589   dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
2590   dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
2591   dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
2592   dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
2593   dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
2594 }
2595
2596 /* Swap out the ldhdr structure.  */
2597
2598 static void
2599 xcoff_swap_ldhdr_out (bfd *abfd, const struct internal_ldhdr *src, void * d)
2600 {
2601   struct external_ldhdr *dst = (struct external_ldhdr *) d;
2602
2603   bfd_put_32 (abfd, (bfd_vma) src->l_version, dst->l_version);
2604   bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
2605   bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
2606   bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
2607   bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
2608   bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
2609   bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
2610   bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
2611 }
2612
2613 /* Swap in the ldsym structure.  */
2614
2615 static void
2616 xcoff_swap_ldsym_in (bfd *abfd, const void * s, struct internal_ldsym *dst)
2617 {
2618   const struct external_ldsym *src = (const struct external_ldsym *) s;
2619
2620   if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0) {
2621     memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2622   } else {
2623     dst->_l._l_l._l_zeroes = 0;
2624     dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
2625   }
2626   dst->l_value = bfd_get_32 (abfd, src->l_value);
2627   dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
2628   dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
2629   dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
2630   dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
2631   dst->l_parm = bfd_get_32 (abfd, src->l_parm);
2632 }
2633
2634 /* Swap out the ldsym structure.  */
2635
2636 static void
2637 xcoff_swap_ldsym_out (bfd *abfd, const struct internal_ldsym *src, void * d)
2638 {
2639   struct external_ldsym *dst = (struct external_ldsym *) d;
2640
2641   if (src->_l._l_l._l_zeroes != 0)
2642     memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2643   else
2644     {
2645       bfd_put_32 (abfd, (bfd_vma) 0, dst->_l._l_l._l_zeroes);
2646       bfd_put_32 (abfd, (bfd_vma) src->_l._l_l._l_offset,
2647                   dst->_l._l_l._l_offset);
2648     }
2649   bfd_put_32 (abfd, src->l_value, dst->l_value);
2650   bfd_put_16 (abfd, (bfd_vma) src->l_scnum, dst->l_scnum);
2651   bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
2652   bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
2653   bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
2654   bfd_put_32 (abfd, src->l_parm, dst->l_parm);
2655 }
2656
2657 static void
2658 xcoff_swap_reloc_in (bfd *abfd, void * s, void * d)
2659 {
2660   struct external_reloc *src = (struct external_reloc *) s;
2661   struct internal_reloc *dst = (struct internal_reloc *) d;
2662
2663   memset (dst, 0, sizeof (struct internal_reloc));
2664
2665   dst->r_vaddr = bfd_get_32 (abfd, src->r_vaddr);
2666   dst->r_symndx = bfd_get_32 (abfd, src->r_symndx);
2667   dst->r_size = bfd_get_8 (abfd, src->r_size);
2668   dst->r_type = bfd_get_8 (abfd, src->r_type);
2669 }
2670
2671 static unsigned int
2672 xcoff_swap_reloc_out (bfd *abfd, void * s, void * d)
2673 {
2674   struct internal_reloc *src = (struct internal_reloc *) s;
2675   struct external_reloc *dst = (struct external_reloc *) d;
2676
2677   bfd_put_32 (abfd, src->r_vaddr, dst->r_vaddr);
2678   bfd_put_32 (abfd, src->r_symndx, dst->r_symndx);
2679   bfd_put_8 (abfd, src->r_type, dst->r_type);
2680   bfd_put_8 (abfd, src->r_size, dst->r_size);
2681
2682   return bfd_coff_relsz (abfd);
2683 }
2684
2685 /* Swap in the ldrel structure.  */
2686
2687 static void
2688 xcoff_swap_ldrel_in (bfd *abfd, const void * s, struct internal_ldrel *dst)
2689 {
2690   const struct external_ldrel *src = (const struct external_ldrel *) s;
2691
2692   dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
2693   dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
2694   dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
2695   dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
2696 }
2697
2698 /* Swap out the ldrel structure.  */
2699
2700 static void
2701 xcoff_swap_ldrel_out (bfd *abfd, const struct internal_ldrel *src, void * d)
2702 {
2703   struct external_ldrel *dst = (struct external_ldrel *) d;
2704
2705   bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
2706   bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
2707   bfd_put_16 (abfd, (bfd_vma) src->l_rtype, dst->l_rtype);
2708   bfd_put_16 (abfd, (bfd_vma) src->l_rsecnm, dst->l_rsecnm);
2709 }
2710 \f
2711
2712 bfd_boolean
2713 xcoff_reloc_type_noop (bfd *input_bfd ATTRIBUTE_UNUSED,
2714                        asection *input_section ATTRIBUTE_UNUSED,
2715                        bfd *output_bfd ATTRIBUTE_UNUSED,
2716                        struct internal_reloc *rel ATTRIBUTE_UNUSED,
2717                        struct internal_syment *sym ATTRIBUTE_UNUSED,
2718                        struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2719                        bfd_vma val ATTRIBUTE_UNUSED,
2720                        bfd_vma addend ATTRIBUTE_UNUSED,
2721                        bfd_vma *relocation ATTRIBUTE_UNUSED,
2722                        bfd_byte *contents ATTRIBUTE_UNUSED)
2723 {
2724   return TRUE;
2725 }
2726
2727 bfd_boolean
2728 xcoff_reloc_type_fail (bfd *input_bfd,
2729                        asection *input_section ATTRIBUTE_UNUSED,
2730                        bfd *output_bfd ATTRIBUTE_UNUSED,
2731                        struct internal_reloc *rel,
2732                        struct internal_syment *sym ATTRIBUTE_UNUSED,
2733                        struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2734                        bfd_vma val ATTRIBUTE_UNUSED,
2735                        bfd_vma addend ATTRIBUTE_UNUSED,
2736                        bfd_vma *relocation ATTRIBUTE_UNUSED,
2737                        bfd_byte *contents ATTRIBUTE_UNUSED)
2738 {
2739   (*_bfd_error_handler)
2740     (_("%s: unsupported relocation type 0x%02x"),
2741      bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
2742   bfd_set_error (bfd_error_bad_value);
2743   return FALSE;
2744 }
2745
2746 bfd_boolean
2747 xcoff_reloc_type_pos (bfd *input_bfd ATTRIBUTE_UNUSED,
2748                       asection *input_section ATTRIBUTE_UNUSED,
2749                       bfd *output_bfd ATTRIBUTE_UNUSED,
2750                       struct internal_reloc *rel ATTRIBUTE_UNUSED,
2751                       struct internal_syment *sym ATTRIBUTE_UNUSED,
2752                       struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2753                       bfd_vma val,
2754                       bfd_vma addend,
2755                       bfd_vma *relocation,
2756                       bfd_byte *contents ATTRIBUTE_UNUSED)
2757 {
2758   *relocation = val + addend;
2759   return TRUE;
2760 }
2761
2762 bfd_boolean
2763 xcoff_reloc_type_neg (bfd *input_bfd ATTRIBUTE_UNUSED,
2764                       asection *input_section ATTRIBUTE_UNUSED,
2765                       bfd *output_bfd ATTRIBUTE_UNUSED,
2766                       struct internal_reloc *rel ATTRIBUTE_UNUSED,
2767                       struct internal_syment *sym ATTRIBUTE_UNUSED,
2768                       struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2769                       bfd_vma val,
2770                       bfd_vma addend,
2771                       bfd_vma *relocation,
2772                       bfd_byte *contents ATTRIBUTE_UNUSED)
2773 {
2774   *relocation = addend - val;
2775   return TRUE;
2776 }
2777
2778 bfd_boolean
2779 xcoff_reloc_type_rel (bfd *input_bfd ATTRIBUTE_UNUSED,
2780                       asection *input_section,
2781                       bfd *output_bfd ATTRIBUTE_UNUSED,
2782                       struct internal_reloc *rel ATTRIBUTE_UNUSED,
2783                       struct internal_syment *sym ATTRIBUTE_UNUSED,
2784                       struct reloc_howto_struct *howto,
2785                       bfd_vma val,
2786                       bfd_vma addend,
2787                       bfd_vma *relocation,
2788                       bfd_byte *contents ATTRIBUTE_UNUSED)
2789 {
2790   howto->pc_relative = TRUE;
2791
2792   /* A PC relative reloc includes the section address.  */
2793   addend += input_section->vma;
2794
2795   *relocation = val + addend;
2796   *relocation -= (input_section->output_section->vma
2797                   + input_section->output_offset);
2798   return TRUE;
2799 }
2800
2801 bfd_boolean
2802 xcoff_reloc_type_toc (bfd *input_bfd,
2803                       asection *input_section ATTRIBUTE_UNUSED,
2804                       bfd *output_bfd,
2805                       struct internal_reloc *rel,
2806                       struct internal_syment *sym,
2807                       struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2808                       bfd_vma val,
2809                       bfd_vma addend ATTRIBUTE_UNUSED,
2810                       bfd_vma *relocation,
2811                       bfd_byte *contents ATTRIBUTE_UNUSED)
2812 {
2813   struct xcoff_link_hash_entry *h;
2814
2815   if (0 > rel->r_symndx)
2816     return FALSE;
2817
2818   h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
2819
2820   if (h != NULL && h->smclas != XMC_TD)
2821     {
2822       if (h->toc_section == NULL)
2823         {
2824           (*_bfd_error_handler)
2825             (_("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry"),
2826              bfd_get_filename (input_bfd), rel->r_vaddr,
2827              h->root.root.string);
2828           bfd_set_error (bfd_error_bad_value);
2829           return FALSE;
2830         }
2831
2832       BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
2833       val = (h->toc_section->output_section->vma
2834               + h->toc_section->output_offset);
2835     }
2836
2837   *relocation = ((val - xcoff_data (output_bfd)->toc)
2838                  - (sym->n_value - xcoff_data (input_bfd)->toc));
2839   return TRUE;
2840 }
2841
2842 bfd_boolean
2843 xcoff_reloc_type_ba (bfd *input_bfd ATTRIBUTE_UNUSED,
2844                      asection *input_section ATTRIBUTE_UNUSED,
2845                      bfd *output_bfd ATTRIBUTE_UNUSED,
2846                      struct internal_reloc *rel ATTRIBUTE_UNUSED,
2847                      struct internal_syment *sym ATTRIBUTE_UNUSED,
2848                      struct reloc_howto_struct *howto,
2849                      bfd_vma val,
2850                      bfd_vma addend,
2851                      bfd_vma *relocation,
2852                      bfd_byte *contents ATTRIBUTE_UNUSED)
2853 {
2854   howto->src_mask &= ~3;
2855   howto->dst_mask = howto->src_mask;
2856
2857   *relocation = val + addend;
2858
2859   return TRUE;
2860 }
2861
2862 static bfd_boolean
2863 xcoff_reloc_type_br (bfd *input_bfd,
2864                      asection *input_section,
2865                      bfd *output_bfd ATTRIBUTE_UNUSED,
2866                      struct internal_reloc *rel,
2867                      struct internal_syment *sym ATTRIBUTE_UNUSED,
2868                      struct reloc_howto_struct *howto,
2869                      bfd_vma val,
2870                      bfd_vma addend,
2871                      bfd_vma *relocation,
2872                      bfd_byte *contents)
2873 {
2874   struct xcoff_link_hash_entry *h;
2875   bfd_vma section_offset;
2876
2877   if (0 > rel->r_symndx)
2878     return FALSE;
2879
2880   h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
2881   section_offset = rel->r_vaddr - input_section->vma;
2882
2883   /* If we see an R_BR or R_RBR reloc which is jumping to global
2884      linkage code, and it is followed by an appropriate cror nop
2885      instruction, we replace the cror with lwz r2,20(r1).  This
2886      restores the TOC after the glink code.  Contrariwise, if the
2887      call is followed by a lwz r2,20(r1), but the call is not
2888      going to global linkage code, we can replace the load with a
2889      cror.  */
2890   if (NULL != h
2891       && (bfd_link_hash_defined == h->root.type
2892           || bfd_link_hash_defweak == h->root.type)
2893       && section_offset + 8 <= input_section->size)
2894     {
2895       bfd_byte *pnext;
2896       unsigned long next;
2897
2898       pnext = contents + section_offset + 4;
2899       next = bfd_get_32 (input_bfd, pnext);
2900
2901       /* The _ptrgl function is magic.  It is used by the AIX
2902          compiler to call a function through a pointer.  */
2903       if (h->smclas == XMC_GL || strcmp (h->root.root.string, "._ptrgl") == 0)
2904         {
2905           if (next == 0x4def7b82                        /* cror 15,15,15 */
2906               || next == 0x4ffffb82                     /* cror 31,31,31 */
2907               || next == 0x60000000)                    /* ori r0,r0,0 */
2908             bfd_put_32 (input_bfd, 0x80410014, pnext);  /* lwz r2,20(r1) */
2909
2910         }
2911       else
2912         {
2913           if (next == 0x80410014)                       /* lwz r2,20(r1) */
2914             bfd_put_32 (input_bfd, 0x60000000, pnext);  /* ori r0,r0,0 */
2915         }
2916     }
2917   else if (NULL != h && bfd_link_hash_undefined == h->root.type)
2918     {
2919       /* Normally, this relocation is against a defined symbol.  In the
2920          case where this is a partial link and the output section offset
2921          is greater than 2^25, the linker will return an invalid error
2922          message that the relocation has been truncated.  Yes it has been
2923          truncated but no it not important.  For this case, disable the
2924          overflow checking. */
2925
2926       howto->complain_on_overflow = complain_overflow_dont;
2927     }
2928
2929   /* The original PC-relative relocation is biased by -r_vaddr, so adding
2930      the value below will give the absolute target address.  */
2931   *relocation = val + addend + rel->r_vaddr;
2932
2933   howto->src_mask &= ~3;
2934   howto->dst_mask = howto->src_mask;
2935
2936   if (h != NULL
2937       && (h->root.type == bfd_link_hash_defined
2938           || h->root.type == bfd_link_hash_defweak)
2939       && bfd_is_abs_section (h->root.u.def.section)
2940       && section_offset + 4 <= input_section->size)
2941     {
2942       bfd_byte *ptr;
2943       bfd_vma insn;
2944
2945       /* Turn the relative branch into an absolute one by setting the
2946          AA bit.  */
2947       ptr = contents + section_offset;
2948       insn = bfd_get_32 (input_bfd, ptr);
2949       insn |= 2;
2950       bfd_put_32 (input_bfd, insn, ptr);
2951
2952       /* Make the howto absolute too.  */
2953       howto->pc_relative = FALSE;
2954       howto->complain_on_overflow = complain_overflow_bitfield;
2955     }
2956   else
2957     {
2958       /* Use a PC-relative howto and subtract the instruction's address
2959          from the target address we calculated above.  */
2960       howto->pc_relative = TRUE;
2961       *relocation -= (input_section->output_section->vma
2962                       + input_section->output_offset
2963                       + section_offset);
2964     }
2965   return TRUE;
2966 }
2967
2968 bfd_boolean
2969 xcoff_reloc_type_crel (bfd *input_bfd ATTRIBUTE_UNUSED,
2970                        asection *input_section,
2971                        bfd *output_bfd ATTRIBUTE_UNUSED,
2972                        struct internal_reloc *rel ATTRIBUTE_UNUSED,
2973                        struct internal_syment *sym ATTRIBUTE_UNUSED,
2974                        struct reloc_howto_struct *howto,
2975                        bfd_vma val ATTRIBUTE_UNUSED,
2976                        bfd_vma addend,
2977                        bfd_vma *relocation,
2978                        bfd_byte *contents ATTRIBUTE_UNUSED)
2979 {
2980   howto->pc_relative = TRUE;
2981   howto->src_mask &= ~3;
2982   howto->dst_mask = howto->src_mask;
2983
2984   /* A PC relative reloc includes the section address.  */
2985   addend += input_section->vma;
2986
2987   *relocation = val + addend;
2988   *relocation -= (input_section->output_section->vma
2989                   + input_section->output_offset);
2990   return TRUE;
2991 }
2992
2993 static bfd_boolean
2994 xcoff_complain_overflow_dont_func (bfd *input_bfd ATTRIBUTE_UNUSED,
2995                                    bfd_vma val ATTRIBUTE_UNUSED,
2996                                    bfd_vma relocation ATTRIBUTE_UNUSED,
2997                                    struct reloc_howto_struct *
2998                                       howto ATTRIBUTE_UNUSED)
2999 {
3000   return FALSE;
3001 }
3002
3003 static bfd_boolean
3004 xcoff_complain_overflow_bitfield_func (bfd *input_bfd,
3005                                        bfd_vma val,
3006                                        bfd_vma relocation,
3007                                        struct reloc_howto_struct *howto)
3008 {
3009   bfd_vma fieldmask, signmask, ss;
3010   bfd_vma a, b, sum;
3011
3012   /* Get the values to be added together.  For signed and unsigned
3013      relocations, we assume that all values should be truncated to
3014      the size of an address.  For bitfields, all the bits matter.
3015      See also bfd_check_overflow.  */
3016   fieldmask = N_ONES (howto->bitsize);
3017   a = relocation;
3018   b = val & howto->src_mask;
3019
3020   /* Much like unsigned, except no trimming with addrmask.  In
3021      addition, the sum overflows if there is a carry out of
3022      the bfd_vma, i.e., the sum is less than either input
3023      operand.  */
3024   a >>= howto->rightshift;
3025   b >>= howto->bitpos;
3026
3027   /* Bitfields are sometimes used for signed numbers; for
3028      example, a 13-bit field sometimes represents values in
3029      0..8191 and sometimes represents values in -4096..4095.
3030      If the field is signed and a is -4095 (0x1001) and b is
3031      -1 (0x1fff), the sum is -4096 (0x1000), but (0x1001 +
3032      0x1fff is 0x3000).  It's not clear how to handle this
3033      everywhere, since there is not way to know how many bits
3034      are significant in the relocation, but the original code
3035      assumed that it was fully sign extended, and we will keep
3036      that assumption.  */
3037   signmask = (fieldmask >> 1) + 1;
3038
3039   if ((a & ~ fieldmask) != 0)
3040     {
3041       /* Some bits out of the field are set.  This might not
3042          be a problem: if this is a signed bitfield, it is OK
3043          iff all the high bits are set, including the sign
3044          bit.  We'll try setting all but the most significant
3045          bit in the original relocation value: if this is all
3046          ones, we are OK, assuming a signed bitfield.  */
3047       ss = (signmask << howto->rightshift) - 1;
3048       if ((ss | relocation) != ~ (bfd_vma) 0)
3049         return TRUE;
3050       a &= fieldmask;
3051     }
3052
3053   /* We just assume (b & ~ fieldmask) == 0.  */
3054
3055   /* We explicitly permit wrap around if this relocation
3056      covers the high bit of an address.  The Linux kernel
3057      relies on it, and it is the only way to write assembler
3058      code which can run when loaded at a location 0x80000000
3059      away from the location at which it is linked.  */
3060   if (howto->bitsize + howto->rightshift
3061       == bfd_arch_bits_per_address (input_bfd))
3062     return FALSE;
3063
3064   sum = a + b;
3065   if (sum < a || (sum & ~ fieldmask) != 0)
3066     {
3067       /* There was a carry out, or the field overflow.  Test
3068          for signed operands again.  Here is the overflow test
3069          is as for complain_overflow_signed.  */
3070       if (((~ (a ^ b)) & (a ^ sum)) & signmask)
3071         return TRUE;
3072     }
3073
3074   return FALSE;
3075 }
3076
3077 static bfd_boolean
3078 xcoff_complain_overflow_signed_func (bfd *input_bfd,
3079                                      bfd_vma val,
3080                                      bfd_vma relocation,
3081                                      struct reloc_howto_struct *howto)
3082 {
3083   bfd_vma addrmask, fieldmask, signmask, ss;
3084   bfd_vma a, b, sum;
3085
3086   /* Get the values to be added together.  For signed and unsigned
3087      relocations, we assume that all values should be truncated to
3088      the size of an address.  For bitfields, all the bits matter.
3089      See also bfd_check_overflow.  */
3090   fieldmask = N_ONES (howto->bitsize);
3091   addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3092   a = relocation;
3093   b = val & howto->src_mask;
3094
3095   a = (a & addrmask) >> howto->rightshift;
3096
3097   /* If any sign bits are set, all sign bits must be set.
3098      That is, A must be a valid negative address after
3099      shifting.  */
3100   signmask = ~ (fieldmask >> 1);
3101   ss = a & signmask;
3102   if (ss != 0 && ss != ((addrmask >> howto->rightshift) & signmask))
3103     return TRUE;
3104
3105   /* We only need this next bit of code if the sign bit of B
3106      is below the sign bit of A.  This would only happen if
3107      SRC_MASK had fewer bits than BITSIZE.  Note that if
3108      SRC_MASK has more bits than BITSIZE, we can get into
3109      trouble; we would need to verify that B is in range, as
3110      we do for A above.  */
3111   signmask = ((~ howto->src_mask) >> 1) & howto->src_mask;
3112   if ((b & signmask) != 0)
3113     {
3114       /* Set all the bits above the sign bit.  */
3115       b -= signmask <<= 1;
3116     }
3117
3118   b = (b & addrmask) >> howto->bitpos;
3119
3120   /* Now we can do the addition.  */
3121   sum = a + b;
3122
3123   /* See if the result has the correct sign.  Bits above the
3124      sign bit are junk now; ignore them.  If the sum is
3125      positive, make sure we did not have all negative inputs;
3126      if the sum is negative, make sure we did not have all
3127      positive inputs.  The test below looks only at the sign
3128      bits, and it really just
3129      SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
3130   */
3131   signmask = (fieldmask >> 1) + 1;
3132   if (((~ (a ^ b)) & (a ^ sum)) & signmask)
3133     return TRUE;
3134
3135   return FALSE;
3136 }
3137
3138 static bfd_boolean
3139 xcoff_complain_overflow_unsigned_func (bfd *input_bfd,
3140                                        bfd_vma val,
3141                                        bfd_vma relocation,
3142                                        struct reloc_howto_struct *howto)
3143 {
3144   bfd_vma addrmask, fieldmask;
3145   bfd_vma a, b, sum;
3146
3147   /* Get the values to be added together.  For signed and unsigned
3148      relocations, we assume that all values should be truncated to
3149      the size of an address.  For bitfields, all the bits matter.
3150      See also bfd_check_overflow.  */
3151   fieldmask = N_ONES (howto->bitsize);
3152   addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3153   a = relocation;
3154   b = val & howto->src_mask;
3155
3156   /* Checking for an unsigned overflow is relatively easy:
3157      trim the addresses and add, and trim the result as well.
3158      Overflow is normally indicated when the result does not
3159      fit in the field.  However, we also need to consider the
3160      case when, e.g., fieldmask is 0x7fffffff or smaller, an
3161      input is 0x80000000, and bfd_vma is only 32 bits; then we
3162      will get sum == 0, but there is an overflow, since the
3163      inputs did not fit in the field.  Instead of doing a
3164      separate test, we can check for this by or-ing in the
3165      operands when testing for the sum overflowing its final
3166      field.  */
3167   a = (a & addrmask) >> howto->rightshift;
3168   b = (b & addrmask) >> howto->bitpos;
3169   sum = (a + b) & addrmask;
3170   if ((a | b | sum) & ~ fieldmask)
3171     return TRUE;
3172
3173   return FALSE;
3174 }
3175
3176 /* This is the relocation function for the RS/6000/POWER/PowerPC.
3177    This is currently the only processor which uses XCOFF; I hope that
3178    will never change.
3179
3180    I took the relocation type definitions from two documents:
3181    the PowerPC AIX Version 4 Application Binary Interface, First
3182    Edition (April 1992), and the PowerOpen ABI, Big-Endian
3183    32-Bit Hardware Implementation (June 30, 1994).  Differences
3184    between the documents are noted below.
3185
3186    Unsupported r_type's
3187
3188    R_RTB:
3189    R_RRTBI:
3190    R_RRTBA:
3191
3192    These relocs are defined by the PowerPC ABI to be
3193    relative branches which use half of the difference
3194    between the symbol and the program counter.  I can't
3195    quite figure out when this is useful.  These relocs are
3196    not defined by the PowerOpen ABI.
3197
3198    Supported r_type's
3199
3200    R_POS:
3201    Simple positive relocation.
3202
3203    R_NEG:
3204    Simple negative relocation.
3205
3206    R_REL:
3207    Simple PC relative relocation.
3208
3209    R_TOC:
3210    TOC relative relocation.  The value in the instruction in
3211    the input file is the offset from the input file TOC to
3212    the desired location.  We want the offset from the final
3213    TOC to the desired location.  We have:
3214    isym = iTOC + in
3215    iinsn = in + o
3216    osym = oTOC + on
3217    oinsn = on + o
3218    so we must change insn by on - in.
3219
3220    R_GL:
3221    GL linkage relocation.  The value of this relocation
3222    is the address of the entry in the TOC section.
3223
3224    R_TCL:
3225    Local object TOC address.  I can't figure out the
3226    difference between this and case R_GL.
3227
3228    R_TRL:
3229    TOC relative relocation.  A TOC relative load instruction
3230    which may be changed to a load address instruction.
3231    FIXME: We don't currently implement this optimization.
3232
3233    R_TRLA:
3234    TOC relative relocation.  This is a TOC relative load
3235    address instruction which may be changed to a load
3236    instruction.  FIXME: I don't know if this is the correct
3237    implementation.
3238
3239    R_BA:
3240    Absolute branch.  We don't want to mess with the lower
3241    two bits of the instruction.
3242
3243    R_CAI:
3244    The PowerPC ABI defines this as an absolute call which
3245    may be modified to become a relative call.  The PowerOpen
3246    ABI does not define this relocation type.
3247
3248    R_RBA:
3249    Absolute branch which may be modified to become a
3250    relative branch.
3251
3252    R_RBAC:
3253    The PowerPC ABI defines this as an absolute branch to a
3254    fixed address which may be modified to an absolute branch
3255    to a symbol.  The PowerOpen ABI does not define this
3256    relocation type.
3257
3258    R_RBRC:
3259    The PowerPC ABI defines this as an absolute branch to a
3260    fixed address which may be modified to a relative branch.
3261    The PowerOpen ABI does not define this relocation type.
3262
3263    R_BR:
3264    Relative branch.  We don't want to mess with the lower
3265    two bits of the instruction.
3266
3267    R_CREL:
3268    The PowerPC ABI defines this as a relative call which may
3269    be modified to become an absolute call.  The PowerOpen
3270    ABI does not define this relocation type.
3271
3272    R_RBR:
3273    A relative branch which may be modified to become an
3274    absolute branch.
3275
3276    R_RL:
3277    The PowerPC AIX ABI describes this as a load which may be
3278    changed to a load address.  The PowerOpen ABI says this
3279    is the same as case R_POS.
3280
3281    R_RLA:
3282    The PowerPC AIX ABI describes this as a load address
3283    which may be changed to a load.  The PowerOpen ABI says
3284    this is the same as R_POS.
3285 */
3286
3287 bfd_boolean
3288 xcoff_ppc_relocate_section (bfd *output_bfd,
3289                             struct bfd_link_info *info,
3290                             bfd *input_bfd,
3291                             asection *input_section,
3292                             bfd_byte *contents,
3293                             struct internal_reloc *relocs,
3294                             struct internal_syment *syms,
3295                             asection **sections)
3296 {
3297   struct internal_reloc *rel;
3298   struct internal_reloc *relend;
3299
3300   rel = relocs;
3301   relend = rel + input_section->reloc_count;
3302   for (; rel < relend; rel++)
3303     {
3304       long symndx;
3305       struct xcoff_link_hash_entry *h;
3306       struct internal_syment *sym;
3307       bfd_vma addend;
3308       bfd_vma val;
3309       struct reloc_howto_struct howto;
3310       bfd_vma relocation;
3311       bfd_vma value_to_relocate;
3312       bfd_vma address;
3313       bfd_byte *location;
3314
3315       /* Relocation type R_REF is a special relocation type which is
3316          merely used to prevent garbage collection from occurring for
3317          the csect including the symbol which it references.  */
3318       if (rel->r_type == R_REF)
3319         continue;
3320
3321       /* howto */
3322       howto.type = rel->r_type;
3323       howto.rightshift = 0;
3324       howto.bitsize = (rel->r_size & 0x1f) + 1;
3325       howto.size = howto.bitsize > 16 ? 2 : 1;
3326       howto.pc_relative = FALSE;
3327       howto.bitpos = 0;
3328       howto.complain_on_overflow = (rel->r_size & 0x80
3329                                     ? complain_overflow_signed
3330                                     : complain_overflow_bitfield);
3331       howto.special_function = NULL;
3332       howto.name = "internal";
3333       howto.partial_inplace = TRUE;
3334       howto.src_mask = howto.dst_mask = N_ONES (howto.bitsize);
3335       howto.pcrel_offset = FALSE;
3336
3337       /* symbol */
3338       val = 0;
3339       addend = 0;
3340       h = NULL;
3341       sym = NULL;
3342       symndx = rel->r_symndx;
3343
3344       if (-1 != symndx)
3345         {
3346           asection *sec;
3347
3348           h = obj_xcoff_sym_hashes (input_bfd)[symndx];
3349           sym = syms + symndx;
3350           addend = - sym->n_value;
3351
3352           if (NULL == h)
3353             {
3354               sec = sections[symndx];
3355               /* Hack to make sure we use the right TOC anchor value
3356                  if this reloc is against the TOC anchor.  */
3357               if (sec->name[3] == '0'
3358                   && strcmp (sec->name, ".tc0") == 0)
3359                 val = xcoff_data (output_bfd)->toc;
3360               else
3361                 val = (sec->output_section->vma
3362                        + sec->output_offset
3363                        + sym->n_value
3364                        - sec->vma);
3365             }
3366           else
3367             {
3368               if (info->unresolved_syms_in_objects != RM_IGNORE
3369                   && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3370                 {
3371                   if (! ((*info->callbacks->undefined_symbol)
3372                          (info, h->root.root.string,
3373                           input_bfd, input_section,
3374                           rel->r_vaddr - input_section->vma,
3375                           (info->unresolved_syms_in_objects
3376                            == RM_GENERATE_ERROR))))
3377                     return FALSE;
3378                 }
3379               if (h->root.type == bfd_link_hash_defined
3380                   || h->root.type == bfd_link_hash_defweak)
3381                 {
3382                   sec = h->root.u.def.section;
3383                   val = (h->root.u.def.value
3384                          + sec->output_section->vma
3385                          + sec->output_offset);
3386                 }
3387               else if (h->root.type == bfd_link_hash_common)
3388                 {
3389                   sec = h->root.u.c.p->section;
3390                   val = (sec->output_section->vma
3391                          + sec->output_offset);
3392
3393                 }
3394               else
3395                 {
3396                   BFD_ASSERT (info->relocatable
3397                               || (info->static_link
3398                                   && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3399                               || (h->flags & XCOFF_DEF_DYNAMIC) != 0
3400                               || (h->flags & XCOFF_IMPORT) != 0);
3401                 }
3402             }
3403         }
3404
3405       if (rel->r_type >= XCOFF_MAX_CALCULATE_RELOCATION
3406           || !((*xcoff_calculate_relocation[rel->r_type])
3407                (input_bfd, input_section, output_bfd, rel, sym, &howto, val,
3408                 addend, &relocation, contents)))
3409         return FALSE;
3410
3411       /* address */
3412       address = rel->r_vaddr - input_section->vma;
3413       location = contents + address;
3414
3415       if (address > input_section->size)
3416         abort ();
3417
3418       /* Get the value we are going to relocate.  */
3419       if (1 == howto.size)
3420         value_to_relocate = bfd_get_16 (input_bfd, location);
3421       else
3422         value_to_relocate = bfd_get_32 (input_bfd, location);
3423
3424       /* overflow.
3425
3426          FIXME: We may drop bits during the addition
3427          which we don't check for.  We must either check at every single
3428          operation, which would be tedious, or we must do the computations
3429          in a type larger than bfd_vma, which would be inefficient.  */
3430
3431       if ((unsigned int) howto.complain_on_overflow
3432           >= XCOFF_MAX_COMPLAIN_OVERFLOW)
3433         abort ();
3434
3435       if (((*xcoff_complain_overflow[howto.complain_on_overflow])
3436            (input_bfd, value_to_relocate, relocation, &howto)))
3437         {
3438           const char *name;
3439           char buf[SYMNMLEN + 1];
3440           char reloc_type_name[10];
3441
3442           if (symndx == -1)
3443             {
3444               name = "*ABS*";
3445             }
3446           else if (h != NULL)
3447             {
3448               name = NULL;
3449             }
3450           else
3451             {
3452               name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3453               if (name == NULL)
3454                 name = "UNKNOWN";
3455             }
3456           sprintf (reloc_type_name, "0x%02x", rel->r_type);
3457
3458           if (! ((*info->callbacks->reloc_overflow)
3459                  (info, (h ? &h->root : NULL), name, reloc_type_name,
3460                   (bfd_vma) 0, input_bfd, input_section,
3461                   rel->r_vaddr - input_section->vma)))
3462             return FALSE;
3463         }
3464
3465       /* Add RELOCATION to the right bits of VALUE_TO_RELOCATE.  */
3466       value_to_relocate = ((value_to_relocate & ~howto.dst_mask)
3467                            | (((value_to_relocate & howto.src_mask)
3468                                + relocation) & howto.dst_mask));
3469
3470       /* Put the value back in the object file.  */
3471       if (1 == howto.size)
3472         bfd_put_16 (input_bfd, value_to_relocate, location);
3473       else
3474         bfd_put_32 (input_bfd, value_to_relocate, location);
3475     }
3476
3477   return TRUE;
3478 }
3479
3480 static bfd_boolean
3481 _bfd_xcoff_put_ldsymbol_name (bfd *abfd ATTRIBUTE_UNUSED,
3482                               struct xcoff_loader_info *ldinfo,
3483                               struct internal_ldsym *ldsym,
3484                               const char *name)
3485 {
3486   size_t len;
3487   len = strlen (name);
3488
3489   if (len <= SYMNMLEN)
3490     strncpy (ldsym->_l._l_name, name, SYMNMLEN);
3491   else
3492     {
3493       if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
3494         {
3495           bfd_size_type newalc;
3496           char *newstrings;
3497
3498           newalc = ldinfo->string_alc * 2;
3499           if (newalc == 0)
3500             newalc = 32;
3501           while (ldinfo->string_size + len + 3 > newalc)
3502             newalc *= 2;
3503
3504           newstrings = bfd_realloc (ldinfo->strings, newalc);
3505           if (newstrings == NULL)
3506             {
3507               ldinfo->failed = TRUE;
3508               return FALSE;
3509             }
3510           ldinfo->string_alc = newalc;
3511           ldinfo->strings = newstrings;
3512         }
3513
3514       bfd_put_16 (ldinfo->output_bfd, (bfd_vma) (len + 1),
3515                   ldinfo->strings + ldinfo->string_size);
3516       strcpy (ldinfo->strings + ldinfo->string_size + 2, name);
3517       ldsym->_l._l_l._l_zeroes = 0;
3518       ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
3519       ldinfo->string_size += len + 3;
3520     }
3521
3522   return TRUE;
3523 }
3524
3525 static bfd_boolean
3526 _bfd_xcoff_put_symbol_name (bfd *abfd, struct bfd_strtab_hash *strtab,
3527                             struct internal_syment *sym,
3528                             const char *name)
3529 {
3530   if (strlen (name) <= SYMNMLEN)
3531     {
3532       strncpy (sym->_n._n_name, name, SYMNMLEN);
3533     }
3534   else
3535     {
3536       bfd_boolean hash;
3537       bfd_size_type indx;
3538
3539       hash = TRUE;
3540       if ((abfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
3541         hash = FALSE;
3542       indx = _bfd_stringtab_add (strtab, name, hash, FALSE);
3543       if (indx == (bfd_size_type) -1)
3544         return FALSE;
3545       sym->_n._n_n._n_zeroes = 0;
3546       sym->_n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3547     }
3548   return TRUE;
3549 }
3550
3551 static asection *
3552 xcoff_create_csect_from_smclas (bfd *abfd,
3553                                 union internal_auxent *aux,
3554                                 const char *symbol_name)
3555 {
3556   asection *return_value = NULL;
3557
3558   /* .sv64 = x_smclas == 17
3559      This is an invalid csect for 32 bit apps.  */
3560   static const char * const names[] =
3561     {
3562       ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo", /* 0 - 7 */
3563       ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0", /* 8 - 15 */
3564       ".td", NULL, ".sv3264", NULL, ".tl", ".ul", ".te"
3565     };
3566   
3567   if ((aux->x_csect.x_smclas < ARRAY_SIZE (names))
3568       && (NULL != names[aux->x_csect.x_smclas]))
3569     {
3570       return_value = bfd_make_section_anyway
3571         (abfd, names[aux->x_csect.x_smclas]);
3572     }
3573   else
3574     {
3575       (*_bfd_error_handler)
3576         (_("%B: symbol `%s' has unrecognized smclas %d"),
3577          abfd, symbol_name, aux->x_csect.x_smclas);
3578       bfd_set_error (bfd_error_bad_value);
3579     }
3580
3581   return return_value;
3582 }
3583
3584 static bfd_boolean
3585 xcoff_is_lineno_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
3586 {
3587   if (0xffff <= value)
3588     return TRUE;
3589
3590   return FALSE;
3591 }
3592
3593 static bfd_boolean
3594 xcoff_is_reloc_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
3595 {
3596   if (0xffff <= value)
3597     return TRUE;
3598
3599   return FALSE;
3600 }
3601
3602 static bfd_vma
3603 xcoff_loader_symbol_offset (bfd *abfd,
3604                             struct internal_ldhdr *ldhdr ATTRIBUTE_UNUSED)
3605 {
3606   return bfd_xcoff_ldhdrsz (abfd);
3607 }
3608
3609 static bfd_vma
3610 xcoff_loader_reloc_offset (bfd *abfd, struct internal_ldhdr *ldhdr)
3611 {
3612   return bfd_xcoff_ldhdrsz (abfd) + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (abfd);
3613 }
3614
3615 static bfd_boolean
3616 xcoff_generate_rtinit  (bfd *abfd, const char *init, const char *fini,
3617                         bfd_boolean rtld)
3618 {
3619   bfd_byte filehdr_ext[FILHSZ];
3620   bfd_byte scnhdr_ext[SCNHSZ];
3621   bfd_byte syment_ext[SYMESZ * 10];
3622   bfd_byte reloc_ext[RELSZ * 3];
3623   bfd_byte *data_buffer;
3624   bfd_size_type data_buffer_size;
3625   bfd_byte *string_table = NULL, *st_tmp = NULL;
3626   bfd_size_type string_table_size;
3627   bfd_vma val;
3628   size_t initsz, finisz;
3629   struct internal_filehdr filehdr;
3630   struct internal_scnhdr scnhdr;
3631   struct internal_syment syment;
3632   union internal_auxent auxent;
3633   struct internal_reloc reloc;
3634
3635   char *data_name = ".data";
3636   char *rtinit_name = "__rtinit";
3637   char *rtld_name = "__rtld";
3638
3639   if (! bfd_xcoff_rtinit_size (abfd))
3640     return FALSE;
3641
3642   initsz = (init == NULL ? 0 : 1 + strlen (init));
3643   finisz = (fini == NULL ? 0 : 1 + strlen (fini));
3644
3645   /* file header */
3646   memset (filehdr_ext, 0, FILHSZ);
3647   memset (&filehdr, 0, sizeof (struct internal_filehdr));
3648   filehdr.f_magic = bfd_xcoff_magic_number (abfd);
3649   filehdr.f_nscns = 1;
3650   filehdr.f_timdat = 0;
3651   filehdr.f_nsyms = 0;  /* at least 6, no more than 10 */
3652   filehdr.f_symptr = 0; /* set below */
3653   filehdr.f_opthdr = 0;
3654   filehdr.f_flags = 0;
3655
3656   /* section header */
3657   memset (scnhdr_ext, 0, SCNHSZ);
3658   memset (&scnhdr, 0, sizeof (struct internal_scnhdr));
3659   memcpy (scnhdr.s_name, data_name, strlen (data_name));
3660   scnhdr.s_paddr = 0;
3661   scnhdr.s_vaddr = 0;
3662   scnhdr.s_size = 0;    /* set below */
3663   scnhdr.s_scnptr = FILHSZ + SCNHSZ;
3664   scnhdr.s_relptr = 0;  /* set below */
3665   scnhdr.s_lnnoptr = 0;
3666   scnhdr.s_nreloc = 0;  /* either 1 or 2 */
3667   scnhdr.s_nlnno = 0;
3668   scnhdr.s_flags = STYP_DATA;
3669
3670   /* .data
3671      0x0000           0x00000000 : rtl
3672      0x0004           0x00000010 : offset to init, or 0
3673      0x0008           0x00000028 : offset to fini, or 0
3674      0x000C           0x0000000C : size of descriptor
3675      0x0010           0x00000000 : init, needs a reloc
3676      0x0014           0x00000040 : offset to init name
3677      0x0018           0x00000000 : flags, padded to a word
3678      0x001C           0x00000000 : empty init
3679      0x0020           0x00000000 :
3680      0x0024           0x00000000 :
3681      0x0028           0x00000000 : fini, needs a reloc
3682      0x002C           0x00000??? : offset to fini name
3683      0x0030           0x00000000 : flags, padded to a word
3684      0x0034           0x00000000 : empty fini
3685      0x0038           0x00000000 :
3686      0x003C           0x00000000 :
3687      0x0040           init name
3688      0x0040 + initsz  fini name */
3689
3690   data_buffer_size = 0x0040 + initsz + finisz;
3691   data_buffer_size = (data_buffer_size + 7) &~ (bfd_size_type) 7;
3692   data_buffer = NULL;
3693   data_buffer = (bfd_byte *) bfd_zmalloc (data_buffer_size);
3694   if (data_buffer == NULL)
3695     return FALSE;
3696
3697   if (initsz)
3698     {
3699       val = 0x10;
3700       bfd_h_put_32 (abfd, val, &data_buffer[0x04]);
3701       val = 0x40;
3702       bfd_h_put_32 (abfd, val, &data_buffer[0x14]);
3703       memcpy (&data_buffer[val], init, initsz);
3704     }
3705
3706   if (finisz)
3707     {
3708       val = 0x28;
3709       bfd_h_put_32 (abfd, val, &data_buffer[0x08]);
3710       val = 0x40 + initsz;
3711       bfd_h_put_32 (abfd, val, &data_buffer[0x2C]);
3712       memcpy (&data_buffer[val], fini, finisz);
3713     }
3714
3715   val = 0x0C;
3716   bfd_h_put_32 (abfd, val, &data_buffer[0x0C]);
3717
3718   scnhdr.s_size = data_buffer_size;
3719
3720   /* string table */
3721   string_table_size = 0;
3722   if (initsz > 9)
3723     string_table_size += initsz;
3724   if (finisz > 9)
3725     string_table_size += finisz;
3726   if (string_table_size)
3727     {
3728       string_table_size += 4;
3729       string_table = (bfd_byte *) bfd_zmalloc (string_table_size);
3730       if (string_table == NULL)
3731         return FALSE;
3732
3733       val = string_table_size;
3734       bfd_h_put_32 (abfd, val, &string_table[0]);
3735       st_tmp = string_table + 4;
3736     }
3737
3738   /* symbols
3739      0. .data csect
3740      2. __rtinit
3741      4. init function
3742      6. fini function
3743      8. __rtld  */
3744   memset (syment_ext, 0, 10 * SYMESZ);
3745   memset (reloc_ext, 0, 3 * RELSZ);
3746
3747   /* .data csect */
3748   memset (&syment, 0, sizeof (struct internal_syment));
3749   memset (&auxent, 0, sizeof (union internal_auxent));
3750   memcpy (syment._n._n_name, data_name, strlen (data_name));
3751   syment.n_scnum = 1;
3752   syment.n_sclass = C_HIDEXT;
3753   syment.n_numaux = 1;
3754   auxent.x_csect.x_scnlen.l = data_buffer_size;
3755   auxent.x_csect.x_smtyp = 3 << 3 | XTY_SD;
3756   auxent.x_csect.x_smclas = XMC_RW;
3757   bfd_coff_swap_sym_out (abfd, &syment,
3758                          &syment_ext[filehdr.f_nsyms * SYMESZ]);
3759   bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3760                          syment.n_numaux,
3761                          &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3762   filehdr.f_nsyms += 2;
3763
3764   /* __rtinit */
3765   memset (&syment, 0, sizeof (struct internal_syment));
3766   memset (&auxent, 0, sizeof (union internal_auxent));
3767   memcpy (syment._n._n_name, rtinit_name, strlen (rtinit_name));
3768   syment.n_scnum = 1;
3769   syment.n_sclass = C_EXT;
3770   syment.n_numaux = 1;
3771   auxent.x_csect.x_smtyp = XTY_LD;
3772   auxent.x_csect.x_smclas = XMC_RW;
3773   bfd_coff_swap_sym_out (abfd, &syment,
3774                          &syment_ext[filehdr.f_nsyms * SYMESZ]);
3775   bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3776                          syment.n_numaux,
3777                          &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3778   filehdr.f_nsyms += 2;
3779
3780   /* init */
3781   if (initsz)
3782     {
3783       memset (&syment, 0, sizeof (struct internal_syment));
3784       memset (&auxent, 0, sizeof (union internal_auxent));
3785
3786       if (initsz > 9)
3787         {
3788           syment._n._n_n._n_offset = st_tmp - string_table;
3789           memcpy (st_tmp, init, initsz);
3790           st_tmp += initsz;
3791         }
3792       else
3793         memcpy (syment._n._n_name, init, initsz - 1);
3794
3795       syment.n_sclass = C_EXT;
3796       syment.n_numaux = 1;
3797       bfd_coff_swap_sym_out (abfd, &syment,
3798                              &syment_ext[filehdr.f_nsyms * SYMESZ]);
3799       bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3800                              syment.n_numaux,
3801                              &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3802
3803       /* reloc */
3804       memset (&reloc, 0, sizeof (struct internal_reloc));
3805       reloc.r_vaddr = 0x0010;
3806       reloc.r_symndx = filehdr.f_nsyms;
3807       reloc.r_type = R_POS;
3808       reloc.r_size = 31;
3809       bfd_coff_swap_reloc_out (abfd, &reloc, &reloc_ext[0]);
3810
3811       filehdr.f_nsyms += 2;
3812       scnhdr.s_nreloc += 1;
3813     }
3814
3815   /* fini */
3816   if (finisz)
3817     {
3818       memset (&syment, 0, sizeof (struct internal_syment));
3819       memset (&auxent, 0, sizeof (union internal_auxent));
3820
3821       if (finisz > 9)
3822         {
3823           syment._n._n_n._n_offset = st_tmp - string_table;
3824           memcpy (st_tmp, fini, finisz);
3825           st_tmp += finisz;
3826         }
3827       else
3828         memcpy (syment._n._n_name, fini, finisz - 1);
3829
3830       syment.n_sclass = C_EXT;
3831       syment.n_numaux = 1;
3832       bfd_coff_swap_sym_out (abfd, &syment,
3833                              &syment_ext[filehdr.f_nsyms * SYMESZ]);
3834       bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3835                              syment.n_numaux,
3836                              &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3837
3838       /* reloc */
3839       memset (&reloc, 0, sizeof (struct internal_reloc));
3840       reloc.r_vaddr = 0x0028;
3841       reloc.r_symndx = filehdr.f_nsyms;
3842       reloc.r_type = R_POS;
3843       reloc.r_size = 31;
3844       bfd_coff_swap_reloc_out (abfd, &reloc,
3845                                &reloc_ext[scnhdr.s_nreloc * RELSZ]);
3846
3847       filehdr.f_nsyms += 2;
3848       scnhdr.s_nreloc += 1;
3849     }
3850
3851   if (rtld)
3852     {
3853       memset (&syment, 0, sizeof (struct internal_syment));
3854       memset (&auxent, 0, sizeof (union internal_auxent));
3855       memcpy (syment._n._n_name, rtld_name, strlen (rtld_name));
3856       syment.n_sclass = C_EXT;
3857       syment.n_numaux = 1;
3858       bfd_coff_swap_sym_out (abfd, &syment,
3859                              &syment_ext[filehdr.f_nsyms * SYMESZ]);
3860       bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3861                              syment.n_numaux,
3862                              &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3863
3864       /* reloc */
3865       memset (&reloc, 0, sizeof (struct internal_reloc));
3866       reloc.r_vaddr = 0x0000;
3867       reloc.r_symndx = filehdr.f_nsyms;
3868       reloc.r_type = R_POS;
3869       reloc.r_size = 31;
3870       bfd_coff_swap_reloc_out (abfd, &reloc,
3871                                &reloc_ext[scnhdr.s_nreloc * RELSZ]);
3872
3873       filehdr.f_nsyms += 2;
3874       scnhdr.s_nreloc += 1;
3875     }
3876
3877   scnhdr.s_relptr = scnhdr.s_scnptr + data_buffer_size;
3878   filehdr.f_symptr = scnhdr.s_relptr + scnhdr.s_nreloc * RELSZ;
3879
3880   bfd_coff_swap_filehdr_out (abfd, &filehdr, filehdr_ext);
3881   bfd_bwrite (filehdr_ext, FILHSZ, abfd);
3882   bfd_coff_swap_scnhdr_out (abfd, &scnhdr, scnhdr_ext);
3883   bfd_bwrite (scnhdr_ext, SCNHSZ, abfd);
3884   bfd_bwrite (data_buffer, data_buffer_size, abfd);
3885   bfd_bwrite (reloc_ext, scnhdr.s_nreloc * RELSZ, abfd);
3886   bfd_bwrite (syment_ext, filehdr.f_nsyms * SYMESZ, abfd);
3887   bfd_bwrite (string_table, string_table_size, abfd);
3888
3889   free (data_buffer);
3890   data_buffer = NULL;
3891
3892   return TRUE;
3893 }
3894
3895
3896 static reloc_howto_type xcoff_dynamic_reloc =
3897 HOWTO (0,                       /* type */
3898        0,                       /* rightshift */
3899        2,                       /* size (0 = byte, 1 = short, 2 = long) */
3900        32,                      /* bitsize */
3901        FALSE,                   /* pc_relative */
3902        0,                       /* bitpos */
3903        complain_overflow_bitfield, /* complain_on_overflow */
3904        0,                       /* special_function */
3905        "R_POS",                 /* name */
3906        TRUE,                    /* partial_inplace */
3907        0xffffffff,              /* src_mask */
3908        0xffffffff,              /* dst_mask */
3909        FALSE);                  /* pcrel_offset */
3910
3911 /*  glink
3912
3913    The first word of global linkage code must be modified by filling in
3914    the correct TOC offset.  */
3915
3916 static unsigned long xcoff_glink_code[9] =
3917   {
3918     0x81820000, /* lwz r12,0(r2) */
3919     0x90410014, /* stw r2,20(r1) */
3920     0x800c0000, /* lwz r0,0(r12) */
3921     0x804c0004, /* lwz r2,4(r12) */
3922     0x7c0903a6, /* mtctr r0 */
3923     0x4e800420, /* bctr */
3924     0x00000000, /* start of traceback table */
3925     0x000c8000, /* traceback table */
3926     0x00000000, /* traceback table */
3927   };
3928
3929 /* Table to convert DWARF flags to section names.  */
3930
3931 const struct xcoff_dwsect_name xcoff_dwsect_names[] = {
3932   { SSUBTYP_DWINFO,  ".dwinfo",   TRUE },
3933   { SSUBTYP_DWLINE,  ".dwline",   TRUE },
3934   { SSUBTYP_DWPBNMS, ".dwpbnms",  TRUE },
3935   { SSUBTYP_DWPBTYP, ".dwpbtyp",  TRUE },
3936   { SSUBTYP_DWARNGE, ".dwarnge",  TRUE },
3937   { SSUBTYP_DWABREV, ".dwabrev",  FALSE },
3938   { SSUBTYP_DWSTR,   ".dwstr",    TRUE },
3939   { SSUBTYP_DWRNGES, ".dwrnges",  TRUE }
3940 };
3941
3942 static const struct xcoff_backend_data_rec bfd_xcoff_backend_data =
3943   {
3944     { /* COFF backend, defined in libcoff.h.  */
3945       _bfd_xcoff_swap_aux_in,
3946       _bfd_xcoff_swap_sym_in,
3947       coff_swap_lineno_in,
3948       _bfd_xcoff_swap_aux_out,
3949       _bfd_xcoff_swap_sym_out,
3950       coff_swap_lineno_out,
3951       xcoff_swap_reloc_out,
3952       coff_swap_filehdr_out,
3953       coff_swap_aouthdr_out,
3954       coff_swap_scnhdr_out,
3955       FILHSZ,
3956       AOUTSZ,
3957       SCNHSZ,
3958       SYMESZ,
3959       AUXESZ,
3960       RELSZ,
3961       LINESZ,
3962       FILNMLEN,
3963       TRUE,                     /* _bfd_coff_long_filenames */
3964       XCOFF_NO_LONG_SECTION_NAMES,  /* _bfd_coff_long_section_names */
3965       3,                        /* _bfd_coff_default_section_alignment_power */
3966       FALSE,                    /* _bfd_coff_force_symnames_in_strings */
3967       2,                        /* _bfd_coff_debug_string_prefix_length */
3968       coff_swap_filehdr_in,
3969       coff_swap_aouthdr_in,
3970       coff_swap_scnhdr_in,
3971       xcoff_swap_reloc_in,
3972       coff_bad_format_hook,
3973       coff_set_arch_mach_hook,
3974       coff_mkobject_hook,
3975       styp_to_sec_flags,
3976       coff_set_alignment_hook,
3977       coff_slurp_symbol_table,
3978       symname_in_debug_hook,
3979       coff_pointerize_aux_hook,
3980       coff_print_aux,
3981       dummy_reloc16_extra_cases,
3982       dummy_reloc16_estimate,
3983       NULL,                     /* bfd_coff_sym_is_global */
3984       coff_compute_section_file_positions,
3985       NULL,                     /* _bfd_coff_start_final_link */
3986       xcoff_ppc_relocate_section,
3987       coff_rtype_to_howto,
3988       NULL,                     /* _bfd_coff_adjust_symndx */
3989       _bfd_generic_link_add_one_symbol,
3990       coff_link_output_has_begun,
3991       coff_final_link_postscript,
3992       NULL                      /* print_pdata.  */
3993     },
3994
3995     0x01DF,                     /* magic number */
3996     bfd_arch_rs6000,
3997     bfd_mach_rs6k,
3998
3999     /* Function pointers to xcoff specific swap routines.  */
4000     xcoff_swap_ldhdr_in,
4001     xcoff_swap_ldhdr_out,
4002     xcoff_swap_ldsym_in,
4003     xcoff_swap_ldsym_out,
4004     xcoff_swap_ldrel_in,
4005     xcoff_swap_ldrel_out,
4006
4007     /* Sizes.  */
4008     LDHDRSZ,
4009     LDSYMSZ,
4010     LDRELSZ,
4011     12,                         /* _xcoff_function_descriptor_size */
4012     SMALL_AOUTSZ,
4013
4014     /* Versions.  */
4015     1,                          /* _xcoff_ldhdr_version */
4016
4017     _bfd_xcoff_put_symbol_name,
4018     _bfd_xcoff_put_ldsymbol_name,
4019     &xcoff_dynamic_reloc,
4020     xcoff_create_csect_from_smclas,
4021
4022     /* Lineno and reloc count overflow.  */
4023     xcoff_is_lineno_count_overflow,
4024     xcoff_is_reloc_count_overflow,
4025
4026     xcoff_loader_symbol_offset,
4027     xcoff_loader_reloc_offset,
4028
4029     /* glink.  */
4030     &xcoff_glink_code[0],
4031     36,                         /* _xcoff_glink_size */
4032
4033     /* rtinit */
4034     64,                         /* _xcoff_rtinit_size */
4035     xcoff_generate_rtinit,
4036   };
4037
4038 /* The transfer vector that leads the outside world to all of the above.  */
4039 const bfd_target rs6000coff_vec =
4040   {
4041     "aixcoff-rs6000",
4042     bfd_target_xcoff_flavour,
4043     BFD_ENDIAN_BIG,             /* data byte order is big */
4044     BFD_ENDIAN_BIG,             /* header byte order is big */
4045
4046     (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
4047      | HAS_SYMS | HAS_LOCALS | WP_TEXT),
4048
4049     SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
4050     0,                          /* leading char */
4051     '/',                        /* ar_pad_char */
4052     15,                         /* ar_max_namelen */
4053     0,                          /* match priority.  */
4054
4055     /* data */
4056     bfd_getb64,
4057     bfd_getb_signed_64,
4058     bfd_putb64,
4059     bfd_getb32,
4060     bfd_getb_signed_32,
4061     bfd_putb32,
4062     bfd_getb16,
4063     bfd_getb_signed_16,
4064     bfd_putb16,
4065
4066     /* hdrs */
4067     bfd_getb64,
4068     bfd_getb_signed_64,
4069     bfd_putb64,
4070     bfd_getb32,
4071     bfd_getb_signed_32,
4072     bfd_putb32,
4073     bfd_getb16,
4074     bfd_getb_signed_16,
4075     bfd_putb16,
4076
4077     { /* bfd_check_format */
4078       _bfd_dummy_target,
4079       coff_object_p,
4080       _bfd_xcoff_archive_p,
4081       CORE_FILE_P
4082     },
4083
4084     { /* bfd_set_format */
4085       bfd_false,
4086       coff_mkobject,
4087       _bfd_generic_mkarchive,
4088       bfd_false
4089     },
4090
4091     {/* bfd_write_contents */
4092       bfd_false,
4093       coff_write_object_contents,
4094       _bfd_xcoff_write_archive_contents,
4095       bfd_false
4096     },
4097
4098     /* Generic */
4099     _bfd_archive_close_and_cleanup,
4100     bfd_true,
4101     coff_new_section_hook,
4102     _bfd_generic_get_section_contents,
4103     _bfd_generic_get_section_contents_in_window,
4104
4105     /* Copy */
4106     _bfd_xcoff_copy_private_bfd_data,
4107     _bfd_generic_bfd_merge_private_bfd_data,
4108     _bfd_generic_init_private_section_data,
4109     _bfd_generic_bfd_copy_private_section_data,
4110     _bfd_generic_bfd_copy_private_symbol_data,
4111     _bfd_generic_bfd_copy_private_header_data,
4112     _bfd_generic_bfd_set_private_flags,
4113     _bfd_generic_bfd_print_private_bfd_data,
4114
4115     /* Core */
4116     BFD_JUMP_TABLE_CORE (coff),
4117
4118     /* Archive */
4119     _bfd_xcoff_slurp_armap,
4120     _bfd_noarchive_slurp_extended_name_table,
4121     _bfd_noarchive_construct_extended_name_table,
4122     bfd_dont_truncate_arname,
4123     _bfd_xcoff_write_armap,
4124     _bfd_xcoff_read_ar_hdr,
4125     _bfd_generic_write_ar_hdr,
4126     _bfd_xcoff_openr_next_archived_file,
4127     _bfd_generic_get_elt_at_index,
4128     _bfd_xcoff_stat_arch_elt,
4129     bfd_true,
4130
4131     /* Symbols */
4132     coff_get_symtab_upper_bound,
4133     coff_canonicalize_symtab,
4134     coff_make_empty_symbol,
4135     coff_print_symbol,
4136     coff_get_symbol_info,
4137     _bfd_xcoff_is_local_label_name,
4138     coff_bfd_is_target_special_symbol,
4139     coff_get_lineno,
4140     xcoff_find_nearest_line,
4141     xcoff_find_nearest_line_discriminator,
4142     _bfd_generic_find_line,
4143     coff_find_inliner_info,
4144     coff_bfd_make_debug_symbol,
4145     _bfd_generic_read_minisymbols,
4146     _bfd_generic_minisymbol_to_symbol,
4147
4148     /* Reloc */
4149     coff_get_reloc_upper_bound,
4150     coff_canonicalize_reloc,
4151     _bfd_xcoff_reloc_type_lookup,
4152     _bfd_xcoff_reloc_name_lookup,
4153
4154     /* Write */
4155     coff_set_arch_mach,
4156     coff_set_section_contents,
4157
4158     /* Link */
4159     _bfd_xcoff_sizeof_headers,
4160     bfd_generic_get_relocated_section_contents,
4161     bfd_generic_relax_section,
4162     _bfd_xcoff_bfd_link_hash_table_create,
4163     _bfd_generic_link_hash_table_free,
4164     _bfd_xcoff_bfd_link_add_symbols,
4165     _bfd_generic_link_just_syms,
4166     _bfd_generic_copy_link_hash_symbol_type,
4167     _bfd_xcoff_bfd_final_link,
4168     _bfd_generic_link_split_section,
4169     bfd_generic_gc_sections,
4170     bfd_generic_lookup_section_flags,
4171     bfd_generic_merge_sections,
4172     bfd_generic_is_group_section,
4173     bfd_generic_discard_group,
4174     _bfd_generic_section_already_linked,
4175     _bfd_xcoff_define_common_symbol,
4176
4177     /* Dynamic */
4178     _bfd_xcoff_get_dynamic_symtab_upper_bound,
4179     _bfd_xcoff_canonicalize_dynamic_symtab,
4180     _bfd_nodynamic_get_synthetic_symtab,
4181     _bfd_xcoff_get_dynamic_reloc_upper_bound,
4182     _bfd_xcoff_canonicalize_dynamic_reloc,
4183
4184     /* Opposite endian version, none exists */
4185     NULL,
4186
4187     & bfd_xcoff_backend_data,
4188   };
4189
4190 /* xcoff-powermac target
4191    Old target.
4192    Only difference between this target and the rs6000 target is the
4193    the default architecture and machine type used in coffcode.h
4194
4195    PowerPC Macs use the same magic numbers as RS/6000
4196    (because that's how they were bootstrapped originally),
4197    but they are always PowerPC architecture.  */
4198 static const struct xcoff_backend_data_rec bfd_pmac_xcoff_backend_data =
4199   {
4200     { /* COFF backend, defined in libcoff.h.  */
4201       _bfd_xcoff_swap_aux_in,
4202       _bfd_xcoff_swap_sym_in,
4203       coff_swap_lineno_in,
4204       _bfd_xcoff_swap_aux_out,
4205       _bfd_xcoff_swap_sym_out,
4206       coff_swap_lineno_out,
4207       xcoff_swap_reloc_out,
4208       coff_swap_filehdr_out,
4209       coff_swap_aouthdr_out,
4210       coff_swap_scnhdr_out,
4211       FILHSZ,
4212       AOUTSZ,
4213       SCNHSZ,
4214       SYMESZ,
4215       AUXESZ,
4216       RELSZ,
4217       LINESZ,
4218       FILNMLEN,
4219       TRUE,                     /* _bfd_coff_long_filenames */
4220       XCOFF_NO_LONG_SECTION_NAMES,  /* _bfd_coff_long_section_names */
4221       3,                        /* _bfd_coff_default_section_alignment_power */
4222       FALSE,                    /* _bfd_coff_force_symnames_in_strings */
4223       2,                        /* _bfd_coff_debug_string_prefix_length */
4224       coff_swap_filehdr_in,
4225       coff_swap_aouthdr_in,
4226       coff_swap_scnhdr_in,
4227       xcoff_swap_reloc_in,
4228       coff_bad_format_hook,
4229       coff_set_arch_mach_hook,
4230       coff_mkobject_hook,
4231       styp_to_sec_flags,
4232       coff_set_alignment_hook,
4233       coff_slurp_symbol_table,
4234       symname_in_debug_hook,
4235       coff_pointerize_aux_hook,
4236       coff_print_aux,
4237       dummy_reloc16_extra_cases,
4238       dummy_reloc16_estimate,
4239       NULL,                     /* bfd_coff_sym_is_global */
4240       coff_compute_section_file_positions,
4241       NULL,                     /* _bfd_coff_start_final_link */
4242       xcoff_ppc_relocate_section,
4243       coff_rtype_to_howto,
4244       NULL,                     /* _bfd_coff_adjust_symndx */
4245       _bfd_generic_link_add_one_symbol,
4246       coff_link_output_has_begun,
4247       coff_final_link_postscript,
4248       NULL                      /* print_pdata.  */
4249     },
4250
4251     0x01DF,                     /* magic number */
4252     bfd_arch_powerpc,
4253     bfd_mach_ppc,
4254
4255     /* Function pointers to xcoff specific swap routines.  */
4256     xcoff_swap_ldhdr_in,
4257     xcoff_swap_ldhdr_out,
4258     xcoff_swap_ldsym_in,
4259     xcoff_swap_ldsym_out,
4260     xcoff_swap_ldrel_in,
4261     xcoff_swap_ldrel_out,
4262
4263     /* Sizes.  */
4264     LDHDRSZ,
4265     LDSYMSZ,
4266     LDRELSZ,
4267     12,                         /* _xcoff_function_descriptor_size */
4268     SMALL_AOUTSZ,
4269
4270     /* Versions.  */
4271     1,                          /* _xcoff_ldhdr_version */
4272
4273     _bfd_xcoff_put_symbol_name,
4274     _bfd_xcoff_put_ldsymbol_name,
4275     &xcoff_dynamic_reloc,
4276     xcoff_create_csect_from_smclas,
4277
4278     /* Lineno and reloc count overflow.  */
4279     xcoff_is_lineno_count_overflow,
4280     xcoff_is_reloc_count_overflow,
4281
4282     xcoff_loader_symbol_offset,
4283     xcoff_loader_reloc_offset,
4284
4285     /* glink.  */
4286     &xcoff_glink_code[0],
4287     36,                         /* _xcoff_glink_size */
4288
4289     /* rtinit */
4290     0,                          /* _xcoff_rtinit_size */
4291     xcoff_generate_rtinit,
4292   };
4293
4294 /* The transfer vector that leads the outside world to all of the above.  */
4295 const bfd_target pmac_xcoff_vec =
4296   {
4297     "xcoff-powermac",
4298     bfd_target_xcoff_flavour,
4299     BFD_ENDIAN_BIG,             /* data byte order is big */
4300     BFD_ENDIAN_BIG,             /* header byte order is big */
4301
4302     (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
4303      | HAS_SYMS | HAS_LOCALS | WP_TEXT),
4304
4305     SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
4306     0,                          /* leading char */
4307     '/',                        /* ar_pad_char */
4308     15,                         /* ar_max_namelen */
4309     0,                          /* match priority.  */
4310
4311     /* data */
4312     bfd_getb64,
4313     bfd_getb_signed_64,
4314     bfd_putb64,
4315     bfd_getb32,
4316     bfd_getb_signed_32,
4317     bfd_putb32,
4318     bfd_getb16,
4319     bfd_getb_signed_16,
4320     bfd_putb16,
4321
4322     /* hdrs */
4323     bfd_getb64,
4324     bfd_getb_signed_64,
4325     bfd_putb64,
4326     bfd_getb32,
4327     bfd_getb_signed_32,
4328     bfd_putb32,
4329     bfd_getb16,
4330     bfd_getb_signed_16,
4331     bfd_putb16,
4332
4333     { /* bfd_check_format */
4334       _bfd_dummy_target,
4335       coff_object_p,
4336       _bfd_xcoff_archive_p,
4337       CORE_FILE_P
4338     },
4339
4340     { /* bfd_set_format */
4341       bfd_false,
4342       coff_mkobject,
4343       _bfd_generic_mkarchive,
4344       bfd_false
4345     },
4346
4347     {/* bfd_write_contents */
4348       bfd_false,
4349       coff_write_object_contents,
4350       _bfd_xcoff_write_archive_contents,
4351       bfd_false
4352     },
4353
4354     /* Generic */
4355     _bfd_archive_close_and_cleanup,
4356     bfd_true,
4357     coff_new_section_hook,
4358     _bfd_generic_get_section_contents,
4359     _bfd_generic_get_section_contents_in_window,
4360
4361     /* Copy */
4362     _bfd_xcoff_copy_private_bfd_data,
4363     _bfd_generic_bfd_merge_private_bfd_data,
4364     _bfd_generic_init_private_section_data,
4365     _bfd_generic_bfd_copy_private_section_data,
4366     _bfd_generic_bfd_copy_private_symbol_data,
4367     _bfd_generic_bfd_copy_private_header_data,
4368     _bfd_generic_bfd_set_private_flags,
4369     _bfd_generic_bfd_print_private_bfd_data,
4370
4371     /* Core */
4372     BFD_JUMP_TABLE_CORE (coff),
4373
4374     /* Archive */
4375     _bfd_xcoff_slurp_armap,
4376     _bfd_noarchive_slurp_extended_name_table,
4377     _bfd_noarchive_construct_extended_name_table,
4378     bfd_dont_truncate_arname,
4379     _bfd_xcoff_write_armap,
4380     _bfd_xcoff_read_ar_hdr,
4381     _bfd_generic_write_ar_hdr,
4382     _bfd_xcoff_openr_next_archived_file,
4383     _bfd_generic_get_elt_at_index,
4384     _bfd_xcoff_stat_arch_elt,
4385     bfd_true,
4386
4387     /* Symbols */
4388     coff_get_symtab_upper_bound,
4389     coff_canonicalize_symtab,
4390     coff_make_empty_symbol,
4391     coff_print_symbol,
4392     coff_get_symbol_info,
4393     _bfd_xcoff_is_local_label_name,
4394     coff_bfd_is_target_special_symbol,
4395     coff_get_lineno,
4396     xcoff_find_nearest_line,
4397     _bfd_generic_find_nearest_line_discriminator,
4398     _bfd_generic_find_line,
4399     coff_find_inliner_info,
4400     coff_bfd_make_debug_symbol,
4401     _bfd_generic_read_minisymbols,
4402     _bfd_generic_minisymbol_to_symbol,
4403
4404     /* Reloc */
4405     coff_get_reloc_upper_bound,
4406     coff_canonicalize_reloc,
4407     _bfd_xcoff_reloc_type_lookup,
4408     _bfd_xcoff_reloc_name_lookup,
4409
4410     /* Write */
4411     coff_set_arch_mach,
4412     coff_set_section_contents,
4413
4414     /* Link */
4415     _bfd_xcoff_sizeof_headers,
4416     bfd_generic_get_relocated_section_contents,
4417     bfd_generic_relax_section,
4418     _bfd_xcoff_bfd_link_hash_table_create,
4419     _bfd_generic_link_hash_table_free,
4420     _bfd_xcoff_bfd_link_add_symbols,
4421     _bfd_generic_link_just_syms,
4422     _bfd_generic_copy_link_hash_symbol_type,
4423     _bfd_xcoff_bfd_final_link,
4424     _bfd_generic_link_split_section,
4425     bfd_generic_gc_sections,
4426     bfd_generic_lookup_section_flags,
4427     bfd_generic_merge_sections,
4428     bfd_generic_is_group_section,
4429     bfd_generic_discard_group,
4430     _bfd_generic_section_already_linked,
4431     _bfd_xcoff_define_common_symbol,
4432
4433     /* Dynamic */
4434     _bfd_xcoff_get_dynamic_symtab_upper_bound,
4435     _bfd_xcoff_canonicalize_dynamic_symtab,
4436     _bfd_nodynamic_get_synthetic_symtab,
4437     _bfd_xcoff_get_dynamic_reloc_upper_bound,
4438     _bfd_xcoff_canonicalize_dynamic_reloc,
4439
4440     /* Opposite endian version, none exists */
4441     NULL,
4442
4443     & bfd_pmac_xcoff_backend_data,
4444   };