OSDN Git Service

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