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