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