OSDN Git Service

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