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