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