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