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