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