OSDN Git Service

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