OSDN Git Service

2001-01-23 Kazu Hirata <kazu@hxi.com>
[pf3gnuchains/pf3gnuchains3x.git] / bfd / coff-rs6000.c
1 /* BFD back-end for IBM RS/6000 "XCOFF" files.
2    Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
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 "libbfd.h"
32 #include "coff/internal.h"
33 #include "coff/rs6000.h"
34 #include "libcoff.h"
35 #define TARGET_NAME "aixcoff-rs6000"
36 #define TARGET_SYM rs6000coff_vec
37 #include "xcoff-target.h"
38
39 /* The main body of code is in coffcode.h.  */
40
41 static const char *normalize_filename PARAMS ((bfd *));
42
43 /* We use our own tdata type.  Its first field is the COFF tdata type,
44    so the COFF routines are compatible.  */
45
46 boolean
47 _bfd_xcoff_mkobject (abfd)
48      bfd *abfd;
49 {
50   coff_data_type *coff;
51
52   abfd->tdata.xcoff_obj_data =
53     ((struct xcoff_tdata *)
54      bfd_zalloc (abfd, sizeof (struct xcoff_tdata)));
55   if (abfd->tdata.xcoff_obj_data == NULL)
56     return false;
57   coff = coff_data (abfd);
58   coff->symbols = (coff_symbol_type *) NULL;
59   coff->conversion_table = (unsigned int *) NULL;
60   coff->raw_syments = (struct coff_ptr_struct *) NULL;
61   coff->relocbase = 0;
62
63   xcoff_data (abfd)->modtype = ('1' << 8) | 'L';
64
65   /* We set cputype to -1 to indicate that it has not been
66      initialized.  */
67   xcoff_data (abfd)->cputype = -1;
68
69   xcoff_data (abfd)->csects = NULL;
70   xcoff_data (abfd)->debug_indices = NULL;
71
72   return true;
73 }
74
75 /* Copy XCOFF data from one BFD to another.  */
76
77 boolean
78 _bfd_xcoff_copy_private_bfd_data (ibfd, obfd)
79      bfd *ibfd;
80      bfd *obfd;
81 {
82   struct xcoff_tdata *ix, *ox;
83   asection *sec;
84
85   if (ibfd->xvec != obfd->xvec)
86     return true;
87   ix = xcoff_data (ibfd);
88   ox = xcoff_data (obfd);
89   ox->full_aouthdr = ix->full_aouthdr;
90   ox->toc = ix->toc;
91   if (ix->sntoc == 0)
92     ox->sntoc = 0;
93   else
94     {
95       sec = coff_section_from_bfd_index (ibfd, ix->sntoc);
96       if (sec == NULL)
97         ox->sntoc = 0;
98       else
99         ox->sntoc = sec->output_section->target_index;
100     }
101   if (ix->snentry == 0)
102     ox->snentry = 0;
103   else
104     {
105       sec = coff_section_from_bfd_index (ibfd, ix->snentry);
106       if (sec == NULL)
107         ox->snentry = 0;
108       else
109         ox->snentry = sec->output_section->target_index;
110     }
111   ox->text_align_power = ix->text_align_power;
112   ox->data_align_power = ix->data_align_power;
113   ox->modtype = ix->modtype;
114   ox->cputype = ix->cputype;
115   ox->maxdata = ix->maxdata;
116   ox->maxstack = ix->maxstack;
117   return true;
118 }
119
120 /* I don't think XCOFF really has a notion of local labels based on
121    name.  This will mean that ld -X doesn't actually strip anything.
122    The AIX native linker does not have a -X option, and it ignores the
123    -x option.  */
124
125 boolean
126 _bfd_xcoff_is_local_label_name (abfd, name)
127      bfd *abfd ATTRIBUTE_UNUSED;
128      const char *name ATTRIBUTE_UNUSED;
129 {
130   return false;
131 }
132 \f
133 void
134 _bfd_xcoff_swap_sym_in (abfd, ext1, in1)
135      bfd            *abfd;
136      PTR ext1;
137      PTR in1;
138 {
139   SYMENT *ext = (SYMENT *)ext1;
140   struct internal_syment      *in = (struct internal_syment *)in1;
141
142   if (ext->e.e_name[0] != 0)
143     {
144       memcpy(in->_n._n_name, ext->e.e_name, SYMNMLEN);
145     }
146   else
147     {
148       in->_n._n_n._n_zeroes = 0;
149       in->_n._n_n._n_offset =
150           bfd_h_get_32(abfd, (bfd_byte *) ext->e.e.e_offset);
151     }
152
153   in->n_value = bfd_h_get_32(abfd, (bfd_byte *) ext->e_value);
154   in->n_scnum = bfd_h_get_16(abfd, (bfd_byte *) ext->e_scnum);
155   in->n_type = bfd_h_get_16(abfd, (bfd_byte *) ext->e_type);
156   in->n_sclass = bfd_h_get_8(abfd, ext->e_sclass);
157   in->n_numaux = bfd_h_get_8(abfd, ext->e_numaux);
158 }
159
160 unsigned int
161 _bfd_xcoff_swap_sym_out (abfd, inp, extp)
162      bfd       *abfd;
163      PTR        inp;
164      PTR        extp;
165 {
166   struct internal_syment *in = (struct internal_syment *)inp;
167   SYMENT *ext =(SYMENT *)extp;
168
169   if (in->_n._n_name[0] != 0)
170     {
171       memcpy(ext->e.e_name, in->_n._n_name, SYMNMLEN);
172     }
173   else
174     {
175       bfd_h_put_32(abfd, 0, (bfd_byte *) ext->e.e.e_zeroes);
176       bfd_h_put_32(abfd, in->_n._n_n._n_offset,
177               (bfd_byte *)  ext->e.e.e_offset);
178     }
179
180   bfd_h_put_32(abfd,  in->n_value , (bfd_byte *) ext->e_value);
181   bfd_h_put_16(abfd,  in->n_scnum , (bfd_byte *) ext->e_scnum);
182   bfd_h_put_16(abfd,  in->n_type , (bfd_byte *) ext->e_type);
183   bfd_h_put_8(abfd,  in->n_sclass , ext->e_sclass);
184   bfd_h_put_8(abfd,  in->n_numaux , ext->e_numaux);
185   return bfd_coff_symesz (abfd);
186 }
187
188 #define PUTWORD bfd_h_put_32
189 #define PUTHALF bfd_h_put_16
190 #define PUTBYTE bfd_h_put_8
191 #define GETWORD bfd_h_get_32
192 #define GETHALF bfd_h_get_16
193 #define GETBYTE bfd_h_get_8
194
195 void
196 _bfd_xcoff_swap_aux_in (abfd, ext1, type, class, indx, numaux, in1)
197      bfd            *abfd;
198      PTR              ext1;
199      int             type;
200      int             class;
201      int              indx;
202      int              numaux;
203      PTR              in1;
204 {
205   AUXENT    *ext = (AUXENT *)ext1;
206   union internal_auxent *in = (union internal_auxent *)in1;
207
208   switch (class) {
209     case C_FILE:
210       if (ext->x_file.x_fname[0] == 0) {
211           in->x_file.x_n.x_zeroes = 0;
212           in->x_file.x_n.x_offset =
213            bfd_h_get_32(abfd, (bfd_byte *) ext->x_file.x_n.x_offset);
214         } else {
215             if (numaux > 1)
216               {
217                 if (indx == 0)
218                   memcpy (in->x_file.x_fname, ext->x_file.x_fname,
219                           numaux * sizeof (AUXENT));
220               }
221             else
222               {
223                 memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
224               }
225           }
226       goto end;
227
228       /* RS/6000 "csect" auxents */
229     case C_EXT:
230     case C_HIDEXT:
231       if (indx + 1 == numaux)
232         {
233           in->x_csect.x_scnlen.l =
234               bfd_h_get_32 (abfd, ext->x_csect.x_scnlen);
235           in->x_csect.x_parmhash = bfd_h_get_32 (abfd,
236                                                  ext->x_csect.x_parmhash);
237           in->x_csect.x_snhash   = bfd_h_get_16 (abfd, ext->x_csect.x_snhash);
238           /* We don't have to hack bitfields in x_smtyp because it's
239              defined by shifts-and-ands, which are equivalent on all
240              byte orders.  */
241           in->x_csect.x_smtyp    = bfd_h_get_8  (abfd, ext->x_csect.x_smtyp);
242           in->x_csect.x_smclas   = bfd_h_get_8  (abfd, ext->x_csect.x_smclas);
243           in->x_csect.x_stab     = bfd_h_get_32 (abfd, ext->x_csect.x_stab);
244           in->x_csect.x_snstab   = bfd_h_get_16 (abfd, ext->x_csect.x_snstab);
245           goto end;
246         }
247       break;
248
249     case C_STAT:
250     case C_LEAFSTAT:
251     case C_HIDDEN:
252       if (type == T_NULL) {
253           in->x_scn.x_scnlen = bfd_h_get_32(abfd,
254                   (bfd_byte *) ext->x_scn.x_scnlen);
255           in->x_scn.x_nreloc = bfd_h_get_16(abfd,
256                   (bfd_byte *) ext->x_scn.x_nreloc);
257           in->x_scn.x_nlinno = bfd_h_get_16(abfd,
258                   (bfd_byte *) ext->x_scn.x_nlinno);
259           /* PE defines some extra fields; we zero them out for
260              safety.  */
261           in->x_scn.x_checksum = 0;
262           in->x_scn.x_associated = 0;
263           in->x_scn.x_comdat = 0;
264
265           goto end;
266         }
267       break;
268     }
269
270   in->x_sym.x_tagndx.l = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_tagndx);
271   in->x_sym.x_tvndx = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_tvndx);
272
273   if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
274     {
275       in->x_sym.x_fcnary.x_fcn.x_lnnoptr = bfd_h_get_32(abfd, (bfd_byte *)
276               ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
277       in->x_sym.x_fcnary.x_fcn.x_endndx.l = bfd_h_get_32(abfd, (bfd_byte *)
278               ext->x_sym.x_fcnary.x_fcn.x_endndx);
279     }
280   else
281     {
282       in->x_sym.x_fcnary.x_ary.x_dimen[0] =
283         bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
284       in->x_sym.x_fcnary.x_ary.x_dimen[1] =
285         bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
286       in->x_sym.x_fcnary.x_ary.x_dimen[2] =
287         bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
288       in->x_sym.x_fcnary.x_ary.x_dimen[3] =
289         bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
290     }
291   if (ISFCN(type)) {
292     in->x_sym.x_misc.x_fsize = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_misc.x_fsize);
293   }
294   else {
295     in->x_sym.x_misc.x_lnsz.x_lnno = bfd_h_get_16(abfd, (bfd_byte *)
296             ext->x_sym.x_misc.x_lnsz.x_lnno);
297     in->x_sym.x_misc.x_lnsz.x_size = bfd_h_get_16(abfd, (bfd_byte *)
298             ext->x_sym.x_misc.x_lnsz.x_size);
299   }
300
301 end: ;
302   /* the semicolon is because MSVC doesn't like labels at
303      end of block.  */
304
305 }
306
307 unsigned int
308 _bfd_xcoff_swap_aux_out (abfd, inp, type, class, indx, numaux, extp)
309      bfd   *abfd;
310      PTR        inp;
311      int   type;
312      int   class;
313      int   indx ATTRIBUTE_UNUSED;
314      int   numaux ATTRIBUTE_UNUSED;
315      PTR        extp;
316 {
317   union internal_auxent *in = (union internal_auxent *)inp;
318   AUXENT *ext = (AUXENT *)extp;
319
320   memset((PTR)ext, 0, bfd_coff_auxesz (abfd));
321   switch (class)
322     {
323   case C_FILE:
324     if (in->x_file.x_fname[0] == 0)
325       {
326       PUTWORD(abfd, 0, (bfd_byte *) ext->x_file.x_n.x_zeroes);
327       PUTWORD(abfd,
328               in->x_file.x_n.x_offset,
329               (bfd_byte *) ext->x_file.x_n.x_offset);
330     }
331     else
332       {
333       memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
334       }
335     goto end;
336
337   /* RS/6000 "csect" auxents */
338   case C_EXT:
339   case C_HIDEXT:
340     if (indx + 1 == numaux)
341       {
342         PUTWORD (abfd, in->x_csect.x_scnlen.l,ext->x_csect.x_scnlen);
343         PUTWORD (abfd, in->x_csect.x_parmhash,  ext->x_csect.x_parmhash);
344         PUTHALF (abfd, in->x_csect.x_snhash,    ext->x_csect.x_snhash);
345         /* We don't have to hack bitfields in x_smtyp because it's
346            defined by shifts-and-ands, which are equivalent on all
347            byte orders.  */
348         PUTBYTE (abfd, in->x_csect.x_smtyp,     ext->x_csect.x_smtyp);
349         PUTBYTE (abfd, in->x_csect.x_smclas,    ext->x_csect.x_smclas);
350         PUTWORD (abfd, in->x_csect.x_stab,      ext->x_csect.x_stab);
351         PUTHALF (abfd, in->x_csect.x_snstab,    ext->x_csect.x_snstab);
352         goto end;
353       }
354     break;
355
356   case C_STAT:
357   case C_LEAFSTAT:
358   case C_HIDDEN:
359     if (type == T_NULL) {
360       bfd_h_put_32(abfd, in->x_scn.x_scnlen, (bfd_byte *) ext->x_scn.x_scnlen);
361       bfd_h_put_16(abfd, in->x_scn.x_nreloc, (bfd_byte *) ext->x_scn.x_nreloc);
362       bfd_h_put_16(abfd, in->x_scn.x_nlinno, (bfd_byte *) ext->x_scn.x_nlinno);
363       goto end;
364     }
365     break;
366   }
367
368   PUTWORD(abfd, in->x_sym.x_tagndx.l, (bfd_byte *) ext->x_sym.x_tagndx);
369   bfd_h_put_16 (abfd, in->x_sym.x_tvndx , (bfd_byte *) ext->x_sym.x_tvndx);
370
371   if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
372     {
373       bfd_h_put_32(abfd,  in->x_sym.x_fcnary.x_fcn.x_lnnoptr,
374               (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
375       PUTWORD(abfd,  in->x_sym.x_fcnary.x_fcn.x_endndx.l,
376               (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_endndx);
377     }
378   else
379     {
380       bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
381                     (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
382       bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
383                     (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
384       bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
385                     (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
386       bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
387                     (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
388     }
389
390   if (ISFCN (type))
391     PUTWORD (abfd, in->x_sym.x_misc.x_fsize,
392              (bfd_byte *)  ext->x_sym.x_misc.x_fsize);
393   else
394     {
395       bfd_h_put_16(abfd, in->x_sym.x_misc.x_lnsz.x_lnno,
396               (bfd_byte *)ext->x_sym.x_misc.x_lnsz.x_lnno);
397       bfd_h_put_16(abfd, in->x_sym.x_misc.x_lnsz.x_size,
398               (bfd_byte *)ext->x_sym.x_misc.x_lnsz.x_size);
399     }
400
401 end:
402   return bfd_coff_auxesz (abfd);
403 }
404 \f
405 /* The XCOFF reloc table.  Actually, XCOFF relocations specify the
406    bitsize and whether they are signed or not, along with a
407    conventional type.  This table is for the types, which are used for
408    different algorithms for putting in the reloc.  Many of these
409    relocs need special_function entries, which I have not written.  */
410
411 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
412    from smaller values.  Start with zero, widen, *then* decrement.  */
413 #define MINUS_ONE       (((bfd_vma)0) - 1)
414
415 reloc_howto_type xcoff_howto_table[] =
416 {
417   /* Standard 32 bit relocation.  */
418   HOWTO (0,                     /* type */
419          0,                     /* rightshift */
420          2,                     /* size (0 = byte, 1 = short, 2 = long) */
421          32,                    /* bitsize */
422          false,                 /* pc_relative */
423          0,                     /* bitpos */
424          complain_overflow_bitfield, /* complain_on_overflow */
425          0,                     /* special_function */
426          "R_POS",               /* name */
427          true,                  /* partial_inplace */
428          0xffffffff,            /* src_mask */
429          0xffffffff,            /* dst_mask */
430          false),                /* pcrel_offset */
431
432   /* 32 bit relocation, but store negative value.  */
433   HOWTO (1,                     /* type */
434          0,                     /* rightshift */
435          -2,                    /* size (0 = byte, 1 = short, 2 = long) */
436          32,                    /* bitsize */
437          false,                 /* pc_relative */
438          0,                     /* bitpos */
439          complain_overflow_bitfield, /* complain_on_overflow */
440          0,                     /* special_function */
441          "R_NEG",               /* name */
442          true,                  /* partial_inplace */
443          0xffffffff,            /* src_mask */
444          0xffffffff,            /* dst_mask */
445          false),                /* pcrel_offset */
446
447   /* 32 bit PC relative relocation.  */
448   HOWTO (2,                     /* type */
449          0,                     /* rightshift */
450          2,                     /* size (0 = byte, 1 = short, 2 = long) */
451          32,                    /* bitsize */
452          true,                  /* pc_relative */
453          0,                     /* bitpos */
454          complain_overflow_signed, /* complain_on_overflow */
455          0,                     /* special_function */
456          "R_REL",               /* name */
457          true,                  /* partial_inplace */
458          0xffffffff,            /* src_mask */
459          0xffffffff,            /* dst_mask */
460          false),                /* pcrel_offset */
461
462   /* 16 bit TOC relative relocation.  */
463   HOWTO (3,                     /* type */
464          0,                     /* rightshift */
465          1,                     /* size (0 = byte, 1 = short, 2 = long) */
466          16,                    /* bitsize */
467          false,                 /* pc_relative */
468          0,                     /* bitpos */
469          complain_overflow_bitfield, /* complain_on_overflow */
470          0,                     /* special_function */
471          "R_TOC",               /* name */
472          true,                  /* partial_inplace */
473          0xffff,                /* src_mask */
474          0xffff,                /* dst_mask */
475          false),                /* pcrel_offset */
476
477   /* I don't really know what this is.  */
478   HOWTO (4,                     /* type */
479          1,                     /* rightshift */
480          2,                     /* size (0 = byte, 1 = short, 2 = long) */
481          32,                    /* bitsize */
482          false,                 /* pc_relative */
483          0,                     /* bitpos */
484          complain_overflow_bitfield, /* complain_on_overflow */
485          0,                     /* special_function */
486          "R_RTB",               /* name */
487          true,                  /* partial_inplace */
488          0xffffffff,            /* src_mask */
489          0xffffffff,            /* dst_mask */
490          false),                /* pcrel_offset */
491
492   /* External TOC relative symbol.  */
493   HOWTO (5,                     /* type */
494          0,                     /* rightshift */
495          2,                     /* size (0 = byte, 1 = short, 2 = long) */
496          16,                    /* bitsize */
497          false,                 /* pc_relative */
498          0,                     /* bitpos */
499          complain_overflow_bitfield, /* complain_on_overflow */
500          0,                     /* special_function */
501          "R_GL",                /* name */
502          true,                  /* partial_inplace */
503          0xffff,                /* src_mask */
504          0xffff,                /* dst_mask */
505          false),                /* pcrel_offset */
506
507   /* Local TOC relative symbol.  */
508   HOWTO (6,                     /* type */
509          0,                     /* rightshift */
510          2,                     /* size (0 = byte, 1 = short, 2 = long) */
511          16,                    /* bitsize */
512          false,                 /* pc_relative */
513          0,                     /* bitpos */
514          complain_overflow_bitfield, /* complain_on_overflow */
515          0,                     /* special_function */
516          "R_TCL",               /* name */
517          true,                  /* partial_inplace */
518          0xffff,                /* src_mask */
519          0xffff,                /* dst_mask */
520          false),                /* pcrel_offset */
521
522   EMPTY_HOWTO (7),
523
524   /* Non modifiable absolute branch.  */
525   HOWTO (8,                     /* type */
526          0,                     /* rightshift */
527          2,                     /* size (0 = byte, 1 = short, 2 = long) */
528          26,                    /* bitsize */
529          false,                 /* pc_relative */
530          0,                     /* bitpos */
531          complain_overflow_bitfield, /* complain_on_overflow */
532          0,                     /* special_function */
533          "R_BA",                /* name */
534          true,                  /* partial_inplace */
535          0x3fffffc,             /* src_mask */
536          0x3fffffc,             /* dst_mask */
537          false),                /* pcrel_offset */
538
539   EMPTY_HOWTO (9),
540
541   /* Non modifiable relative branch.  */
542   HOWTO (0xa,                   /* type */
543          0,                     /* rightshift */
544          2,                     /* size (0 = byte, 1 = short, 2 = long) */
545          26,                    /* bitsize */
546          true,                  /* pc_relative */
547          0,                     /* bitpos */
548          complain_overflow_signed, /* complain_on_overflow */
549          0,                     /* special_function */
550          "R_BR",                /* name */
551          true,                  /* partial_inplace */
552          0x3fffffc,             /* src_mask */
553          0x3fffffc,             /* dst_mask */
554          false),                /* pcrel_offset */
555
556   EMPTY_HOWTO (0xb),
557
558   /* Indirect load.  */
559   HOWTO (0xc,                   /* type */
560          0,                     /* rightshift */
561          2,                     /* size (0 = byte, 1 = short, 2 = long) */
562          16,                    /* bitsize */
563          false,                 /* pc_relative */
564          0,                     /* bitpos */
565          complain_overflow_bitfield, /* complain_on_overflow */
566          0,                     /* special_function */
567          "R_RL",                /* name */
568          true,                  /* partial_inplace */
569          0xffff,                /* src_mask */
570          0xffff,                /* dst_mask */
571          false),                /* pcrel_offset */
572
573   /* Load address.  */
574   HOWTO (0xd,                   /* type */
575          0,                     /* rightshift */
576          2,                     /* size (0 = byte, 1 = short, 2 = long) */
577          16,                    /* bitsize */
578          false,                 /* pc_relative */
579          0,                     /* bitpos */
580          complain_overflow_bitfield, /* complain_on_overflow */
581          0,                     /* special_function */
582          "R_RLA",               /* name */
583          true,                  /* partial_inplace */
584          0xffff,                /* src_mask */
585          0xffff,                /* dst_mask */
586          false),                /* pcrel_offset */
587
588   EMPTY_HOWTO (0xe),
589
590   /* Non-relocating reference.  */
591   HOWTO (0xf,                   /* type */
592          0,                     /* rightshift */
593          2,                     /* size (0 = byte, 1 = short, 2 = long) */
594          32,                    /* bitsize */
595          false,                 /* pc_relative */
596          0,                     /* bitpos */
597          complain_overflow_bitfield, /* complain_on_overflow */
598          0,                     /* special_function */
599          "R_REF",               /* name */
600          false,                 /* partial_inplace */
601          0,                     /* src_mask */
602          0,                     /* dst_mask */
603          false),                /* pcrel_offset */
604
605   EMPTY_HOWTO (0x10),
606   EMPTY_HOWTO (0x11),
607
608   /* TOC relative indirect load.  */
609   HOWTO (0x12,                  /* type */
610          0,                     /* rightshift */
611          2,                     /* size (0 = byte, 1 = short, 2 = long) */
612          16,                    /* bitsize */
613          false,                 /* pc_relative */
614          0,                     /* bitpos */
615          complain_overflow_bitfield, /* complain_on_overflow */
616          0,                     /* special_function */
617          "R_TRL",               /* name */
618          true,                  /* partial_inplace */
619          0xffff,                /* src_mask */
620          0xffff,                /* dst_mask */
621          false),                /* pcrel_offset */
622
623   /* TOC relative load address.  */
624   HOWTO (0x13,                  /* type */
625          0,                     /* rightshift */
626          2,                     /* size (0 = byte, 1 = short, 2 = long) */
627          16,                    /* bitsize */
628          false,                 /* pc_relative */
629          0,                     /* bitpos */
630          complain_overflow_bitfield, /* complain_on_overflow */
631          0,                     /* special_function */
632          "R_TRLA",              /* name */
633          true,                  /* partial_inplace */
634          0xffff,                /* src_mask */
635          0xffff,                /* dst_mask */
636          false),                /* pcrel_offset */
637
638   /* Modifiable relative branch.  */
639   HOWTO (0x14,                  /* type */
640          1,                     /* rightshift */
641          2,                     /* size (0 = byte, 1 = short, 2 = long) */
642          32,                    /* bitsize */
643          false,                 /* pc_relative */
644          0,                     /* bitpos */
645          complain_overflow_bitfield, /* complain_on_overflow */
646          0,                     /* special_function */
647          "R_RRTBI",             /* name */
648          true,                  /* partial_inplace */
649          0xffffffff,            /* src_mask */
650          0xffffffff,            /* dst_mask */
651          false),                /* pcrel_offset */
652
653   /* Modifiable absolute branch.  */
654   HOWTO (0x15,                  /* type */
655          1,                     /* rightshift */
656          2,                     /* size (0 = byte, 1 = short, 2 = long) */
657          32,                    /* bitsize */
658          false,                 /* pc_relative */
659          0,                     /* bitpos */
660          complain_overflow_bitfield, /* complain_on_overflow */
661          0,                     /* special_function */
662          "R_RRTBA",             /* name */
663          true,                  /* partial_inplace */
664          0xffffffff,            /* src_mask */
665          0xffffffff,            /* dst_mask */
666          false),                /* pcrel_offset */
667
668   /* Modifiable call absolute indirect.  */
669   HOWTO (0x16,                  /* type */
670          0,                     /* rightshift */
671          2,                     /* size (0 = byte, 1 = short, 2 = long) */
672          16,                    /* bitsize */
673          false,                 /* pc_relative */
674          0,                     /* bitpos */
675          complain_overflow_bitfield, /* complain_on_overflow */
676          0,                     /* special_function */
677          "R_CAI",               /* name */
678          true,                  /* partial_inplace */
679          0xffff,                /* src_mask */
680          0xffff,                /* dst_mask */
681          false),                /* pcrel_offset */
682
683   /* Modifiable call relative.  */
684   HOWTO (0x17,                  /* type */
685          0,                     /* rightshift */
686          2,                     /* size (0 = byte, 1 = short, 2 = long) */
687          16,                    /* bitsize */
688          false,                 /* pc_relative */
689          0,                     /* bitpos */
690          complain_overflow_bitfield, /* complain_on_overflow */
691          0,                     /* special_function */
692          "R_CREL",              /* name */
693          true,                  /* partial_inplace */
694          0xffff,                /* src_mask */
695          0xffff,                /* dst_mask */
696          false),                /* pcrel_offset */
697
698   /* Modifiable branch absolute.  */
699   HOWTO (0x18,                  /* type */
700          0,                     /* rightshift */
701          2,                     /* size (0 = byte, 1 = short, 2 = long) */
702          26,                    /* bitsize */
703          false,                 /* pc_relative */
704          0,                     /* bitpos */
705          complain_overflow_bitfield, /* complain_on_overflow */
706          0,                     /* special_function */
707          "R_RBA",               /* name */
708          true,                  /* partial_inplace */
709          0xffff,                /* src_mask */
710          0xffff,                /* dst_mask */
711          false),                /* pcrel_offset */
712
713   /* Modifiable branch absolute.  */
714   HOWTO (0x19,                  /* type */
715          0,                     /* rightshift */
716          2,                     /* size (0 = byte, 1 = short, 2 = long) */
717          32,                    /* bitsize */
718          false,                 /* pc_relative */
719          0,                     /* bitpos */
720          complain_overflow_bitfield, /* complain_on_overflow */
721          0,                     /* special_function */
722          "R_RBAC",              /* name */
723          true,                  /* partial_inplace */
724          0xffff,                /* src_mask */
725          0xffff,                /* dst_mask */
726          false),                /* pcrel_offset */
727
728   /* Modifiable branch relative.  */
729   HOWTO (0x1a,                  /* type */
730          0,                     /* rightshift */
731          2,                     /* size (0 = byte, 1 = short, 2 = long) */
732          26,                    /* bitsize */
733          false,                 /* pc_relative */
734          0,                     /* bitpos */
735          complain_overflow_signed, /* complain_on_overflow */
736          0,                     /* special_function */
737          "R_RBR",               /* name */
738          true,                  /* partial_inplace */
739          0xffff,                /* src_mask */
740          0xffff,                /* dst_mask */
741          false),                /* pcrel_offset */
742
743   /* Modifiable branch absolute.  */
744   HOWTO (0x1b,                  /* type */
745          0,                     /* rightshift */
746          2,                     /* size (0 = byte, 1 = short, 2 = long) */
747          16,                    /* bitsize */
748          false,                 /* pc_relative */
749          0,                     /* bitpos */
750          complain_overflow_bitfield, /* complain_on_overflow */
751          0,                     /* special_function */
752          "R_RBRC",              /* name */
753          true,                  /* partial_inplace */
754          0xffff,                /* src_mask */
755          0xffff,                /* dst_mask */
756          false),                /* pcrel_offset */
757   HOWTO (0,                     /* type */
758          0,                     /* rightshift */
759          4,                     /* size (0 = byte, 1 = short, 2 = long) */
760          64,                    /* bitsize */
761          false,                 /* pc_relative */
762          0,                     /* bitpos */
763          complain_overflow_bitfield, /* complain_on_overflow */
764          0,                     /* special_function */
765          "R_POS",               /* name */
766          true,                  /* partial_inplace */
767          MINUS_ONE,             /* src_mask */
768          MINUS_ONE,             /* dst_mask */
769          false)                 /* pcrel_offset */
770
771 };
772
773 /* These are the first two like the above but for 16-bit relocs.  */
774 static reloc_howto_type xcoff_howto_table_16[] =
775 {
776   /* Standard 16 bit relocation.  */
777   HOWTO (0,                     /* type */
778          0,                     /* rightshift */
779          2,                     /* size (0 = byte, 1 = short, 2 = long) */
780          16,                    /* bitsize */
781          false,                 /* pc_relative */
782          0,                     /* bitpos */
783          complain_overflow_bitfield, /* complain_on_overflow */
784          0,                     /* special_function */
785          "R_POS_16",            /* name */
786          true,                  /* partial_inplace */
787          0xffffffff,            /* src_mask */
788          0xffffffff,            /* dst_mask */
789          false),                /* pcrel_offset */
790
791   /* 16 bit relocation, but store negative value.  */
792   HOWTO (1,                     /* type */
793          0,                     /* rightshift */
794          -2,                    /* size (0 = byte, 1 = short, 2 = long) */
795          16,                    /* bitsize */
796          false,                 /* pc_relative */
797          0,                     /* bitpos */
798          complain_overflow_bitfield, /* complain_on_overflow */
799          0,                     /* special_function */
800          "R_NEG_16",            /* name */
801          true,                  /* partial_inplace */
802          0xffffffff,            /* src_mask */
803          0xffffffff,            /* dst_mask */
804          false),                /* pcrel_offset */
805
806   /* 16 bit PC relative relocation.  */
807   HOWTO (2,                     /* type */
808          0,                     /* rightshift */
809          2,                     /* size (0 = byte, 1 = short, 2 = long) */
810          32,                    /* bitsize */
811          true,                  /* pc_relative */
812          0,                     /* bitpos */
813          complain_overflow_signed, /* complain_on_overflow */
814          0,                     /* special_function */
815          "R_REL_16",            /* name */
816          true,                  /* partial_inplace */
817          0xffffffff,            /* src_mask */
818          0xffffffff,            /* dst_mask */
819          false)                /* pcrel_offset */
820   };
821
822 void
823 _bfd_xcoff_rtype2howto (relent, internal)
824      arelent *relent;
825      struct internal_reloc *internal;
826 {
827   relent->howto = xcoff_howto_table + internal->r_type;
828
829   if (relent->howto->bitsize != ((unsigned int) internal->r_size & 0x1f) + 1
830       && (internal->r_type
831           < sizeof (xcoff_howto_table_16)/sizeof (xcoff_howto_table_16[0])))
832     relent->howto = xcoff_howto_table_16 + internal->r_type;
833
834   /* The r_size field of an XCOFF reloc encodes the bitsize of the
835      relocation, as well as indicating whether it is signed or not.
836      Doublecheck that the relocation information gathered from the
837      type matches this information.  The bitsize is not significant
838      for R_REF relocs.  */
839   if (relent->howto->dst_mask != 0
840       && (relent->howto->bitsize
841           != ((unsigned int) internal->r_size & 0x3f) + 1))
842     abort ();
843 #if 0
844   if ((internal->r_size & 0x80) != 0
845       ? (relent->howto->complain_on_overflow != complain_overflow_signed)
846       : (relent->howto->complain_on_overflow != complain_overflow_bitfield))
847     abort ();
848 #endif
849 }
850
851 reloc_howto_type *
852 _bfd_xcoff_reloc_type_lookup (abfd, code)
853      bfd *abfd ATTRIBUTE_UNUSED;
854      bfd_reloc_code_real_type code;
855 {
856   switch (code)
857     {
858     case BFD_RELOC_PPC_B26:
859       return &xcoff_howto_table[0xa];
860     case BFD_RELOC_PPC_BA26:
861       return &xcoff_howto_table[8];
862     case BFD_RELOC_PPC_TOC16:
863       return &xcoff_howto_table[3];
864     case BFD_RELOC_32:
865     case BFD_RELOC_CTOR:
866       return &xcoff_howto_table[0];
867     case BFD_RELOC_64:
868       return &xcoff_howto_table[0x1c];
869     default:
870       return NULL;
871     }
872 }
873 \f
874 /* XCOFF archive support.  The original version of this code was by
875    Damon A. Permezel.  It was enhanced to permit cross support, and
876    writing archive files, by Ian Lance Taylor, Cygnus Support.
877
878    XCOFF uses its own archive format.  Everything is hooked together
879    with file offset links, so it is possible to rapidly update an
880    archive in place.  Of course, we don't do that.  An XCOFF archive
881    has a real file header, not just an ARMAG string.  The structure of
882    the file header and of each archive header appear below.
883
884    An XCOFF archive also has a member table, which is a list of
885    elements in the archive (you can get that by looking through the
886    linked list, but you have to read a lot more of the file).  The
887    member table has a normal archive header with an empty name.  It is
888    normally (and perhaps must be) the second to last entry in the
889    archive.  The member table data is almost printable ASCII.  It
890    starts with a 12 character decimal string which is the number of
891    entries in the table.  For each entry it has a 12 character decimal
892    string which is the offset in the archive of that member.  These
893    entries are followed by a series of null terminated strings which
894    are the member names for each entry.
895
896    Finally, an XCOFF archive has a global symbol table, which is what
897    we call the armap.  The global symbol table has a normal archive
898    header with an empty name.  It is normally (and perhaps must be)
899    the last entry in the archive.  The contents start with a four byte
900    binary number which is the number of entries.  This is followed by
901    a that many four byte binary numbers; each is the file offset of an
902    entry in the archive.  These numbers are followed by a series of
903    null terminated strings, which are symbol names.
904
905    AIX 4.3 introduced a new archive format which can handle larger
906    files and also 32- and 64-bit objects in the same archive.  The
907    things said above remain true except that there is now more than
908    one global symbol table.  The one is used to index 32-bit objects,
909    the other for 64-bit objects.
910
911    The new archives (recognizable by the new ARMAG string) has larger
912    field lengths so that we cannot really share any code.  Also we have
913    to take care that we are not generating the new form of archives
914    on AIX 4.2 or earlier systems.  */
915
916 /* XCOFF archives use this as a magic string.  Note that both strings
917    have the same length.  */
918
919 #define XCOFFARMAG    "<aiaff>\012"
920 #define XCOFFARMAGBIG "<bigaf>\012"
921 #define SXCOFFARMAG   8
922
923 /* This terminates an XCOFF archive member name.  */
924
925 #define XCOFFARFMAG "`\012"
926 #define SXCOFFARFMAG 2
927
928 /* XCOFF archives start with this (printable) structure.  */
929
930 struct xcoff_ar_file_hdr
931 {
932   /* Magic string.  */
933   char magic[SXCOFFARMAG];
934
935   /* Offset of the member table (decimal ASCII string).  */
936   char memoff[12];
937
938   /* Offset of the global symbol table (decimal ASCII string).  */
939   char symoff[12];
940
941   /* Offset of the first member in the archive (decimal ASCII string).  */
942   char firstmemoff[12];
943
944   /* Offset of the last member in the archive (decimal ASCII string).  */
945   char lastmemoff[12];
946
947   /* Offset of the first member on the free list (decimal ASCII
948      string).  */
949   char freeoff[12];
950 };
951
952 #define SIZEOF_AR_FILE_HDR (5 * 12 + SXCOFFARMAG)
953
954 /* This is the equivalent data structure for the big archive format.  */
955
956 struct xcoff_ar_file_hdr_big
957 {
958   /* Magic string.  */
959   char magic[SXCOFFARMAG];
960
961   /* Offset of the member table (decimal ASCII string).  */
962   char memoff[20];
963
964   /* Offset of the global symbol table for 32-bit objects (decimal ASCII
965      string).  */
966   char symoff[20];
967
968   /* Offset of the global symbol table for 64-bit objects (decimal ASCII
969      string).  */
970   char symoff64[20];
971
972   /* Offset of the first member in the archive (decimal ASCII string).  */
973   char firstmemoff[20];
974
975   /* Offset of the last member in the archive (decimal ASCII string).  */
976   char lastmemoff[20];
977
978   /* Offset of the first member on the free list (decimal ASCII
979      string).  */
980   char freeoff[20];
981 };
982
983 #define SIZEOF_AR_FILE_HDR_BIG (6 * 20 + SXCOFFARMAG)
984
985 /* Each XCOFF archive member starts with this (printable) structure.  */
986
987 struct xcoff_ar_hdr
988 {
989   /* File size not including the header (decimal ASCII string).  */
990   char size[12];
991
992   /* File offset of next archive member (decimal ASCII string).  */
993   char nextoff[12];
994
995   /* File offset of previous archive member (decimal ASCII string).  */
996   char prevoff[12];
997
998   /* File mtime (decimal ASCII string).  */
999   char date[12];
1000
1001   /* File UID (decimal ASCII string).  */
1002   char uid[12];
1003
1004   /* File GID (decimal ASCII string).  */
1005   char gid[12];
1006
1007   /* File mode (octal ASCII string).  */
1008   char mode[12];
1009
1010   /* Length of file name (decimal ASCII string).  */
1011   char namlen[4];
1012
1013   /* This structure is followed by the file name.  The length of the
1014      name is given in the namlen field.  If the length of the name is
1015      odd, the name is followed by a null byte.  The name and optional
1016      null byte are followed by XCOFFARFMAG, which is not included in
1017      namlen.  The contents of the archive member follow; the number of
1018      bytes is given in the size field.  */
1019 };
1020
1021 #define SIZEOF_AR_HDR (7 * 12 + 4)
1022
1023 /* The equivalent for the big archive format.  */
1024
1025 struct xcoff_ar_hdr_big
1026 {
1027   /* File size not including the header (decimal ASCII string).  */
1028   char size[20];
1029
1030   /* File offset of next archive member (decimal ASCII string).  */
1031   char nextoff[20];
1032
1033   /* File offset of previous archive member (decimal ASCII string).  */
1034   char prevoff[20];
1035
1036   /* File mtime (decimal ASCII string).  */
1037   char date[12];
1038
1039   /* File UID (decimal ASCII string).  */
1040   char uid[12];
1041
1042   /* File GID (decimal ASCII string).  */
1043   char gid[12];
1044
1045   /* File mode (octal ASCII string).  */
1046   char mode[12];
1047
1048   /* Length of file name (decimal ASCII string).  */
1049   char namlen[4];
1050
1051   /* This structure is followed by the file name.  The length of the
1052      name is given in the namlen field.  If the length of the name is
1053      odd, the name is followed by a null byte.  The name and optional
1054      null byte are followed by XCOFFARFMAG, which is not included in
1055      namlen.  The contents of the archive member follow; the number of
1056      bytes is given in the size field.  */
1057 };
1058
1059 #define SIZEOF_AR_HDR_BIG (3 * 20 + 4 * 12 + 4)
1060
1061 /* We often have to distinguish between the old and big file format.
1062    Make it a bit cleaner.  We can use `xcoff_ardata' here because the
1063    `hdr' member has the same size and position in both formats.  */
1064 #define xcoff_big_format_p(abfd) \
1065   (xcoff_ardata (abfd)->magic[1] == 'b')
1066
1067 /* We store a copy of the xcoff_ar_file_hdr in the tdata field of the
1068    artdata structure.  Similar for the big archive.  */
1069 #define xcoff_ardata(abfd) \
1070   ((struct xcoff_ar_file_hdr *) bfd_ardata (abfd)->tdata)
1071 #define xcoff_ardata_big(abfd) \
1072   ((struct xcoff_ar_file_hdr_big *) bfd_ardata (abfd)->tdata)
1073
1074 /* We store a copy of the xcoff_ar_hdr in the arelt_data field of an
1075    archive element.  Similar for the big archive.  */
1076 #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
1077 #define arch_xhdr(bfd) \
1078   ((struct xcoff_ar_hdr *) arch_eltdata (bfd)->arch_header)
1079 #define arch_xhdr_big(bfd) \
1080   ((struct xcoff_ar_hdr_big *) arch_eltdata (bfd)->arch_header)
1081
1082 /* Read in the armap of an XCOFF archive.  */
1083
1084 boolean
1085 _bfd_xcoff_slurp_armap (abfd)
1086      bfd *abfd;
1087 {
1088   file_ptr off;
1089   size_t namlen;
1090   bfd_size_type sz;
1091   bfd_byte *contents, *cend;
1092   bfd_vma c, i;
1093   carsym *arsym;
1094   bfd_byte *p;
1095
1096   if (xcoff_ardata (abfd) == NULL)
1097     {
1098       bfd_has_map (abfd) = false;
1099       return true;
1100     }
1101
1102   if (! xcoff_big_format_p (abfd))
1103     {
1104       /* This is for the old format.  */
1105       struct xcoff_ar_hdr hdr;
1106
1107       off = strtol (xcoff_ardata (abfd)->symoff, (char **) NULL, 10);
1108       if (off == 0)
1109         {
1110           bfd_has_map (abfd) = false;
1111           return true;
1112         }
1113
1114       if (bfd_seek (abfd, off, SEEK_SET) != 0)
1115         return false;
1116
1117       /* The symbol table starts with a normal archive header.  */
1118       if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR)
1119         return false;
1120
1121       /* Skip the name (normally empty).  */
1122       namlen = strtol (hdr.namlen, (char **) NULL, 10);
1123       if (bfd_seek (abfd, ((namlen + 1) & ~1) + SXCOFFARFMAG, SEEK_CUR) != 0)
1124         return false;
1125
1126       sz = strtol (hdr.size, (char **) NULL, 10);
1127
1128       /* Read in the entire symbol table.  */
1129       contents = (bfd_byte *) bfd_alloc (abfd, sz);
1130       if (contents == NULL)
1131         return false;
1132       if (bfd_read ((PTR) contents, 1, sz, abfd) != sz)
1133         return false;
1134
1135       /* The symbol table starts with a four byte count.  */
1136       c = bfd_h_get_32 (abfd, contents);
1137
1138       if (c * 4 >= sz)
1139         {
1140           bfd_set_error (bfd_error_bad_value);
1141           return false;
1142         }
1143
1144       bfd_ardata (abfd)->symdefs = ((carsym *)
1145                                     bfd_alloc (abfd, c * sizeof (carsym)));
1146       if (bfd_ardata (abfd)->symdefs == NULL)
1147         return false;
1148
1149       /* After the count comes a list of four byte file offsets.  */
1150       for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4;
1151            i < c;
1152            ++i, ++arsym, p += 4)
1153         arsym->file_offset = bfd_h_get_32 (abfd, p);
1154     }
1155   else
1156     {
1157       /* This is for the new format.  */
1158       struct xcoff_ar_hdr_big hdr;
1159
1160       off = strtol (xcoff_ardata_big (abfd)->symoff, (char **) NULL, 10);
1161       if (off == 0)
1162         {
1163           bfd_has_map (abfd) = false;
1164           return true;
1165         }
1166
1167       if (bfd_seek (abfd, off, SEEK_SET) != 0)
1168         return false;
1169
1170       /* The symbol table starts with a normal archive header.  */
1171       if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR_BIG, 1, abfd)
1172           != SIZEOF_AR_HDR_BIG)
1173         return false;
1174
1175       /* Skip the name (normally empty).  */
1176       namlen = strtol (hdr.namlen, (char **) NULL, 10);
1177       if (bfd_seek (abfd, ((namlen + 1) & ~1) + SXCOFFARFMAG, SEEK_CUR) != 0)
1178         return false;
1179
1180       /* XXX This actually has to be a call to strtoll (at least on 32-bit
1181          machines) since the field width is 20 and there numbers with more
1182          than 32 bits can be represented.  */
1183       sz = strtol (hdr.size, (char **) NULL, 10);
1184
1185       /* Read in the entire symbol table.  */
1186       contents = (bfd_byte *) bfd_alloc (abfd, sz);
1187       if (contents == NULL)
1188         return false;
1189       if (bfd_read ((PTR) contents, 1, sz, abfd) != sz)
1190         return false;
1191
1192       /* The symbol table starts with an eight byte count.  */
1193       c = bfd_h_get_64 (abfd, contents);
1194
1195       if (c * 8 >= sz)
1196         {
1197           bfd_set_error (bfd_error_bad_value);
1198           return false;
1199         }
1200
1201       bfd_ardata (abfd)->symdefs = ((carsym *)
1202                                     bfd_alloc (abfd, c * sizeof (carsym)));
1203       if (bfd_ardata (abfd)->symdefs == NULL)
1204         return false;
1205
1206       /* After the count comes a list of eight byte file offsets.  */
1207       for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 8;
1208            i < c;
1209            ++i, ++arsym, p += 8)
1210         arsym->file_offset = bfd_h_get_64 (abfd, p);
1211     }
1212
1213   /* After the file offsets come null terminated symbol names.  */
1214   cend = contents + sz;
1215   for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
1216        i < c;
1217        ++i, ++arsym, p += strlen ((char *) p) + 1)
1218     {
1219       if (p >= cend)
1220         {
1221           bfd_set_error (bfd_error_bad_value);
1222           return false;
1223         }
1224       arsym->name = (char *) p;
1225     }
1226
1227   bfd_ardata (abfd)->symdef_count = c;
1228   bfd_has_map (abfd) = true;
1229
1230   return true;
1231 }
1232
1233 /* See if this is an XCOFF archive.  */
1234
1235 const bfd_target *
1236 _bfd_xcoff_archive_p (abfd)
1237      bfd *abfd;
1238 {
1239   char magic[SXCOFFARMAG];
1240
1241   if (bfd_read ((PTR) magic, SXCOFFARMAG, 1, abfd) != SXCOFFARMAG)
1242     {
1243       if (bfd_get_error () != bfd_error_system_call)
1244         bfd_set_error (bfd_error_wrong_format);
1245       return NULL;
1246     }
1247
1248   if (strncmp (magic, XCOFFARMAG, SXCOFFARMAG) != 0
1249       && strncmp (magic, XCOFFARMAGBIG, SXCOFFARMAG) != 0)
1250     {
1251       bfd_set_error (bfd_error_wrong_format);
1252       return NULL;
1253     }
1254
1255   /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
1256      involves a cast, we can't do it as the left operand of
1257      assignment.  */
1258   abfd->tdata.aout_ar_data =
1259     (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
1260
1261   if (bfd_ardata (abfd) == (struct artdata *) NULL)
1262     return NULL;
1263
1264   bfd_ardata (abfd)->cache = NULL;
1265   bfd_ardata (abfd)->archive_head = NULL;
1266   bfd_ardata (abfd)->symdefs = NULL;
1267   bfd_ardata (abfd)->extended_names = NULL;
1268
1269   /* Now handle the two formats.  */
1270   if (magic[1] != 'b')
1271     {
1272       /* This is the old format.  */
1273       struct xcoff_ar_file_hdr hdr;
1274
1275       /* Copy over the magic string.  */
1276       memcpy (hdr.magic, magic, SXCOFFARMAG);
1277
1278       /* Now read the rest of the file header.  */
1279       if (bfd_read ((PTR) &hdr.memoff, SIZEOF_AR_FILE_HDR - SXCOFFARMAG, 1,
1280                     abfd) != SIZEOF_AR_FILE_HDR - SXCOFFARMAG)
1281         {
1282           if (bfd_get_error () != bfd_error_system_call)
1283             bfd_set_error (bfd_error_wrong_format);
1284           return NULL;
1285         }
1286
1287       bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff,
1288                                                       (char **) NULL, 10);
1289
1290       bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, SIZEOF_AR_FILE_HDR);
1291       if (bfd_ardata (abfd)->tdata == NULL)
1292         return NULL;
1293
1294       memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR);
1295     }
1296   else
1297     {
1298       /* This is the new format.  */
1299       struct xcoff_ar_file_hdr_big hdr;
1300
1301       /* Copy over the magic string.  */
1302       memcpy (hdr.magic, magic, SXCOFFARMAG);
1303
1304       /* Now read the rest of the file header.  */
1305       if (bfd_read ((PTR) &hdr.memoff, SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG, 1,
1306                     abfd) != SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG)
1307         {
1308           if (bfd_get_error () != bfd_error_system_call)
1309             bfd_set_error (bfd_error_wrong_format);
1310           return NULL;
1311         }
1312
1313       /* XXX This actually has to be a call to strtoll (at least on 32-bit
1314          machines) since the field width is 20 and there numbers with more
1315          than 32 bits can be represented.  */
1316       bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff,
1317                                                       (char **) NULL, 10);
1318
1319       bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, SIZEOF_AR_FILE_HDR_BIG);
1320       if (bfd_ardata (abfd)->tdata == NULL)
1321         return NULL;
1322
1323       memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR_BIG);
1324     }
1325
1326   if (! _bfd_xcoff_slurp_armap (abfd))
1327     {
1328       bfd_release (abfd, bfd_ardata (abfd));
1329       abfd->tdata.aout_ar_data = (struct artdata *) NULL;
1330       return NULL;
1331     }
1332
1333   return abfd->xvec;
1334 }
1335
1336 /* Read the archive header in an XCOFF archive.  */
1337
1338 PTR
1339 _bfd_xcoff_read_ar_hdr (abfd)
1340      bfd *abfd;
1341 {
1342   size_t namlen;
1343   struct areltdata *ret;
1344
1345   ret = (struct areltdata *) bfd_alloc (abfd, sizeof (struct areltdata));
1346   if (ret == NULL)
1347     return NULL;
1348
1349   if (! xcoff_big_format_p (abfd))
1350     {
1351       struct xcoff_ar_hdr hdr;
1352       struct xcoff_ar_hdr *hdrp;
1353
1354       if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR)
1355         {
1356           free (ret);
1357           return NULL;
1358         }
1359
1360       namlen = strtol (hdr.namlen, (char **) NULL, 10);
1361       hdrp = (struct xcoff_ar_hdr *) bfd_alloc (abfd,
1362                                                 SIZEOF_AR_HDR + namlen + 1);
1363       if (hdrp == NULL)
1364         {
1365           free (ret);
1366           return NULL;
1367         }
1368       memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
1369       if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR, 1, namlen, abfd) != namlen)
1370         {
1371           free (ret);
1372           return NULL;
1373         }
1374       ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
1375
1376       ret->arch_header = (char *) hdrp;
1377       ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
1378       ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
1379     }
1380   else
1381     {
1382       struct xcoff_ar_hdr_big hdr;
1383       struct xcoff_ar_hdr_big *hdrp;
1384
1385       if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR_BIG, 1, abfd)
1386           != SIZEOF_AR_HDR_BIG)
1387         {
1388           free (ret);
1389           return NULL;
1390         }
1391
1392       namlen = strtol (hdr.namlen, (char **) NULL, 10);
1393       hdrp = (struct xcoff_ar_hdr_big *) bfd_alloc (abfd,
1394                                                     SIZEOF_AR_HDR_BIG
1395                                                     + namlen + 1);
1396       if (hdrp == NULL)
1397         {
1398           free (ret);
1399           return NULL;
1400         }
1401       memcpy (hdrp, &hdr, SIZEOF_AR_HDR_BIG);
1402       if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR_BIG, 1, namlen, abfd) != namlen)
1403         {
1404           free (ret);
1405           return NULL;
1406         }
1407       ((char *) hdrp)[SIZEOF_AR_HDR_BIG + namlen] = '\0';
1408
1409       ret->arch_header = (char *) hdrp;
1410       /* XXX This actually has to be a call to strtoll (at least on 32-bit
1411          machines) since the field width is 20 and there numbers with more
1412          than 32 bits can be represented.  */
1413       ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
1414       ret->filename = (char *) hdrp + SIZEOF_AR_HDR_BIG;
1415     }
1416
1417   /* Skip over the XCOFFARFMAG at the end of the file name.  */
1418   if (bfd_seek (abfd, (namlen & 1) + SXCOFFARFMAG, SEEK_CUR) != 0)
1419     return NULL;
1420
1421   return (PTR) ret;
1422 }
1423
1424 /* Open the next element in an XCOFF archive.  */
1425
1426 bfd *
1427 _bfd_xcoff_openr_next_archived_file (archive, last_file)
1428      bfd *archive;
1429      bfd *last_file;
1430 {
1431   file_ptr filestart;
1432
1433   if (xcoff_ardata (archive) == NULL)
1434     {
1435       bfd_set_error (bfd_error_invalid_operation);
1436       return NULL;
1437     }
1438
1439   if (! xcoff_big_format_p (archive))
1440     {
1441       if (last_file == NULL)
1442         filestart = bfd_ardata (archive)->first_file_filepos;
1443       else
1444         filestart = strtol (arch_xhdr (last_file)->nextoff, (char **) NULL,
1445                             10);
1446
1447       if (filestart == 0
1448           || filestart == strtol (xcoff_ardata (archive)->memoff,
1449                                   (char **) NULL, 10)
1450           || filestart == strtol (xcoff_ardata (archive)->symoff,
1451                                   (char **) NULL, 10))
1452         {
1453           bfd_set_error (bfd_error_no_more_archived_files);
1454           return NULL;
1455         }
1456     }
1457   else
1458     {
1459       if (last_file == NULL)
1460         filestart = bfd_ardata (archive)->first_file_filepos;
1461       else
1462         /* XXX These actually have to be a calls to strtoll (at least
1463            on 32-bit machines) since the fields's width is 20 and
1464            there numbers with more than 32 bits can be represented.  */
1465         filestart = strtol (arch_xhdr_big (last_file)->nextoff, (char **) NULL,
1466                             10);
1467
1468       /* XXX These actually have to be calls to strtoll (at least on 32-bit
1469          machines) since the fields's width is 20 and there numbers with more
1470          than 32 bits can be represented.  */
1471       if (filestart == 0
1472           || filestart == strtol (xcoff_ardata_big (archive)->memoff,
1473                                   (char **) NULL, 10)
1474           || filestart == strtol (xcoff_ardata_big (archive)->symoff,
1475                                   (char **) NULL, 10))
1476         {
1477           bfd_set_error (bfd_error_no_more_archived_files);
1478           return NULL;
1479         }
1480     }
1481
1482   return _bfd_get_elt_at_filepos (archive, filestart);
1483 }
1484
1485 /* Stat an element in an XCOFF archive.  */
1486
1487 int
1488 _bfd_xcoff_generic_stat_arch_elt (abfd, s)
1489      bfd *abfd;
1490      struct stat *s;
1491 {
1492   if (abfd->arelt_data == NULL)
1493     {
1494       bfd_set_error (bfd_error_invalid_operation);
1495       return -1;
1496     }
1497
1498   if (! xcoff_big_format_p (abfd))
1499     {
1500       struct xcoff_ar_hdr *hdrp = arch_xhdr (abfd);
1501
1502       s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
1503       s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
1504       s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
1505       s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
1506       s->st_size = arch_eltdata (abfd)->parsed_size;
1507     }
1508   else
1509     {
1510       struct xcoff_ar_hdr_big *hdrp = arch_xhdr_big (abfd);
1511
1512       s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
1513       s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
1514       s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
1515       s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
1516       s->st_size = arch_eltdata (abfd)->parsed_size;
1517     }
1518
1519   return 0;
1520 }
1521
1522 /* Normalize a file name for inclusion in an archive.  */
1523
1524 static const char *
1525 normalize_filename (abfd)
1526      bfd *abfd;
1527 {
1528   const char *file;
1529   const char *filename;
1530
1531   file = bfd_get_filename (abfd);
1532   filename = strrchr (file, '/');
1533   if (filename != NULL)
1534     filename++;
1535   else
1536     filename = file;
1537   return filename;
1538 }
1539
1540 /* Write out an XCOFF armap.  */
1541
1542 static boolean
1543 xcoff_write_armap_old (abfd, elength, map, orl_count, stridx)
1544      bfd *abfd;
1545      unsigned int elength ATTRIBUTE_UNUSED;
1546      struct orl *map;
1547      unsigned int orl_count;
1548      int stridx;
1549 {
1550   struct xcoff_ar_hdr hdr;
1551   char *p;
1552   unsigned char buf[4];
1553   bfd *sub;
1554   file_ptr fileoff;
1555   unsigned int i;
1556
1557   memset (&hdr, 0, sizeof hdr);
1558   sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
1559   sprintf (hdr.nextoff, "%d", 0);
1560   memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, 12);
1561   sprintf (hdr.date, "%d", 0);
1562   sprintf (hdr.uid, "%d", 0);
1563   sprintf (hdr.gid, "%d", 0);
1564   sprintf (hdr.mode, "%d", 0);
1565   sprintf (hdr.namlen, "%d", 0);
1566
1567   /* We need spaces, not null bytes, in the header.  */
1568   for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
1569     if (*p == '\0')
1570       *p = ' ';
1571
1572   if (bfd_write ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR
1573       || bfd_write (XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) != SXCOFFARFMAG)
1574     return false;
1575
1576   bfd_h_put_32 (abfd, orl_count, buf);
1577   if (bfd_write (buf, 1, 4, abfd) != 4)
1578     return false;
1579
1580   sub = abfd->archive_head;
1581   fileoff = SIZEOF_AR_FILE_HDR;
1582   i = 0;
1583   while (sub != NULL && i < orl_count)
1584     {
1585       size_t namlen;
1586
1587       while (((bfd *) (map[i]).pos) == sub)
1588         {
1589           bfd_h_put_32 (abfd, fileoff, buf);
1590           if (bfd_write (buf, 1, 4, abfd) != 4)
1591             return false;
1592           ++i;
1593         }
1594       namlen = strlen (normalize_filename (sub));
1595       namlen = (namlen + 1) &~ 1;
1596       fileoff += (SIZEOF_AR_HDR
1597                   + namlen
1598                   + SXCOFFARFMAG
1599                   + arelt_size (sub));
1600       fileoff = (fileoff + 1) &~ 1;
1601       sub = sub->next;
1602     }
1603
1604   for (i = 0; i < orl_count; i++)
1605     {
1606       const char *name;
1607       size_t namlen;
1608
1609       name = *map[i].name;
1610       namlen = strlen (name);
1611       if (bfd_write (name, 1, namlen + 1, abfd) != namlen + 1)
1612         return false;
1613     }
1614
1615   if ((stridx & 1) != 0)
1616     {
1617       char b;
1618
1619       b = '\0';
1620       if (bfd_write (&b, 1, 1, abfd) != 1)
1621         return false;
1622     }
1623
1624   return true;
1625 }
1626
1627 /* Write a single armap in the big format.  */
1628 static boolean
1629 xcoff_write_one_armap_big (abfd, map, orl_count, orl_ccount, stridx, bits64,
1630                            prevoff, nextoff)
1631      bfd *abfd;
1632      struct orl *map;
1633      unsigned int orl_count;
1634      unsigned int orl_ccount;
1635      unsigned int stridx;
1636      int bits64;
1637      const char *prevoff;
1638      char *nextoff;
1639 {
1640   struct xcoff_ar_hdr_big hdr;
1641   char *p;
1642   unsigned char buf[4];
1643   const bfd_arch_info_type *arch_info = NULL;
1644   bfd *sub;
1645   file_ptr fileoff;
1646   bfd *object_bfd;
1647   unsigned int i;
1648
1649   memset (&hdr, 0, sizeof hdr);
1650   /* XXX This call actually should use %lld (at least on 32-bit
1651      machines) since the fields's width is 20 and there numbers with
1652      more than 32 bits can be represented.  */
1653   sprintf (hdr.size, "%ld", (long) (4 + orl_ccount * 4 + stridx));
1654   if (bits64)
1655     sprintf (hdr.nextoff, "%d", 0);
1656   else
1657     sprintf (hdr.nextoff, "%ld", (strtol (prevoff, (char **) NULL, 10)
1658                                  + 4 + orl_ccount * 4 + stridx));
1659   memcpy (hdr.prevoff, prevoff, sizeof (hdr.prevoff));
1660   sprintf (hdr.date, "%d", 0);
1661   sprintf (hdr.uid, "%d", 0);
1662   sprintf (hdr.gid, "%d", 0);
1663   sprintf (hdr.mode, "%d", 0);
1664   sprintf (hdr.namlen, "%d", 0);
1665
1666   /* We need spaces, not null bytes, in the header.  */
1667   for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR_BIG; p++)
1668     if (*p == '\0')
1669       *p = ' ';
1670
1671   memcpy (nextoff, hdr.nextoff, sizeof (hdr.nextoff));
1672
1673   if (bfd_write ((PTR) &hdr, SIZEOF_AR_HDR_BIG, 1, abfd) != SIZEOF_AR_HDR_BIG
1674       || bfd_write (XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) != SXCOFFARFMAG)
1675     return false;
1676
1677   bfd_h_put_32 (abfd, orl_ccount, buf);
1678   if (bfd_write (buf, 1, 4, abfd) != 4)
1679     return false;
1680
1681   sub = abfd->archive_head;
1682   fileoff = SIZEOF_AR_FILE_HDR_BIG;
1683   i = 0;
1684   while (sub != NULL && i < orl_count)
1685     {
1686       size_t namlen;
1687
1688       if ((bfd_arch_bits_per_address ((bfd *) map[i].pos) == 64) == bits64)
1689         while (((bfd *) (map[i]).pos) == sub)
1690           {
1691             bfd_h_put_32 (abfd, fileoff, buf);
1692             if (bfd_write (buf, 1, 4, abfd) != 4)
1693               return false;
1694             i++;
1695           }
1696       else
1697         while (((bfd *) (map[i]).pos) == sub)
1698           i++;
1699
1700       namlen = strlen (normalize_filename (sub));
1701       namlen = (namlen + 1) &~ 1;
1702       fileoff += (SIZEOF_AR_HDR_BIG
1703                   + namlen
1704                   + SXCOFFARFMAG
1705                   + arelt_size (sub));
1706       fileoff = (fileoff + 1) &~ 1;
1707       sub = sub->next;
1708     }
1709
1710   object_bfd = NULL;
1711   for (i = 0; i < orl_count; i++)
1712     {
1713       const char *name;
1714       size_t namlen;
1715       bfd *ob = (bfd *)map[i].pos;
1716
1717       if (ob != object_bfd)
1718         arch_info = bfd_get_arch_info (ob);
1719
1720       if (arch_info && (arch_info->bits_per_address == 64) != bits64)
1721         continue;
1722
1723       name = *map[i].name;
1724       namlen = strlen (name);
1725       if (bfd_write (name, 1, namlen + 1, abfd) != namlen + 1)
1726         return false;
1727     }
1728
1729   if ((stridx & 1) != 0)
1730     {
1731       char b;
1732
1733       b = '\0';
1734       if (bfd_write (&b, 1, 1, abfd) != 1)
1735         return false;
1736     }
1737
1738   return true;
1739 }
1740
1741 static boolean
1742 xcoff_write_armap_big (abfd, elength, map, orl_count, stridx)
1743      bfd *abfd;
1744      unsigned int elength ATTRIBUTE_UNUSED;
1745      struct orl *map;
1746      unsigned int orl_count;
1747      int stridx;
1748 {
1749   unsigned int i;
1750   unsigned int orl_count_32, orl_count_64;
1751   unsigned int stridx_32, stridx_64;
1752   const bfd_arch_info_type *arch_info = NULL;
1753   bfd *object_bfd;
1754
1755   /* First, we look through the symbols and work out which are
1756      from 32-bit objects and which from 64-bit ones.  */
1757   orl_count_32 = 0;
1758   orl_count_64 = 0;
1759   stridx_32 = 0;
1760   stridx_64 = 0;
1761   object_bfd = NULL;
1762   for (i = 0; i < orl_count; i++)
1763     {
1764       bfd *ob = (bfd *)map[i].pos;
1765       unsigned int len;
1766       if (ob != object_bfd)
1767         arch_info = bfd_get_arch_info (ob);
1768       len = strlen (*map[i].name) + 1;
1769       if (arch_info && arch_info->bits_per_address == 64)
1770         {
1771           orl_count_64++;
1772           stridx_64 += len;
1773         }
1774       else
1775         {
1776           orl_count_32++;
1777           stridx_32 += len;
1778         }
1779       object_bfd = ob;
1780     }
1781   /* A quick sanity check...  */
1782   BFD_ASSERT (orl_count_64 + orl_count_32 == orl_count);
1783   BFD_ASSERT (stridx_64 + stridx_32 == stridx);
1784
1785   /* Now write out each map.  */
1786   if (! xcoff_write_one_armap_big (abfd, map, orl_count, orl_count_32,
1787                                    stridx_32, false,
1788                                    xcoff_ardata_big (abfd)->memoff,
1789                                    xcoff_ardata_big (abfd)->symoff))
1790     return false;
1791   if (! xcoff_write_one_armap_big (abfd, map, orl_count, orl_count_64,
1792                                    stridx_64, true,
1793                                    xcoff_ardata_big (abfd)->symoff,
1794                                    xcoff_ardata_big (abfd)->symoff64))
1795     return false;
1796
1797   return true;
1798 }
1799
1800 boolean
1801 _bfd_xcoff_write_armap (abfd, elength, map, orl_count, stridx)
1802      bfd *abfd;
1803      unsigned int elength ATTRIBUTE_UNUSED;
1804      struct orl *map;
1805      unsigned int orl_count;
1806      int stridx;
1807 {
1808   if (! xcoff_big_format_p (abfd))
1809     return xcoff_write_armap_old (abfd, elength, map, orl_count, stridx);
1810   else
1811     return xcoff_write_armap_big (abfd, elength, map, orl_count, stridx);
1812 }
1813
1814 /* Write out an XCOFF archive.  We always write an entire archive,
1815    rather than fussing with the freelist and so forth.  */
1816
1817 static boolean
1818 xcoff_write_archive_contents_old (abfd)
1819      bfd *abfd;
1820 {
1821   struct xcoff_ar_file_hdr fhdr;
1822   size_t count;
1823   size_t total_namlen;
1824   file_ptr *offsets;
1825   boolean makemap;
1826   boolean hasobjects;
1827   file_ptr prevoff, nextoff;
1828   bfd *sub;
1829   unsigned int i;
1830   struct xcoff_ar_hdr ahdr;
1831   bfd_size_type size;
1832   char *p;
1833   char decbuf[13];
1834
1835   memset (&fhdr, 0, sizeof fhdr);
1836   strncpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);
1837   sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR);
1838   sprintf (fhdr.freeoff, "%d", 0);
1839
1840   count = 0;
1841   total_namlen = 0;
1842   for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
1843     {
1844       ++count;
1845       total_namlen += strlen (normalize_filename (sub)) + 1;
1846     }
1847   offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
1848   if (offsets == NULL)
1849     return false;
1850
1851   if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
1852     return false;
1853
1854   makemap = bfd_has_map (abfd);
1855   hasobjects = false;
1856   prevoff = 0;
1857   nextoff = SIZEOF_AR_FILE_HDR;
1858   for (sub = abfd->archive_head, i = 0; sub != NULL; sub = sub->next, i++)
1859     {
1860       const char *name;
1861       size_t namlen;
1862       struct xcoff_ar_hdr *ahdrp;
1863       bfd_size_type remaining;
1864
1865       if (makemap && ! hasobjects)
1866         {
1867           if (bfd_check_format (sub, bfd_object))
1868             hasobjects = true;
1869         }
1870
1871       name = normalize_filename (sub);
1872       namlen = strlen (name);
1873
1874       if (sub->arelt_data != NULL)
1875         ahdrp = arch_xhdr (sub);
1876       else
1877         ahdrp = NULL;
1878
1879       if (ahdrp == NULL)
1880         {
1881           struct stat s;
1882
1883           memset (&ahdr, 0, sizeof ahdr);
1884           ahdrp = &ahdr;
1885           if (stat (bfd_get_filename (sub), &s) != 0)
1886             {
1887               bfd_set_error (bfd_error_system_call);
1888               return false;
1889             }
1890
1891           sprintf (ahdrp->size, "%ld", (long) s.st_size);
1892           sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
1893           sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
1894           sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
1895           sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
1896
1897           if (sub->arelt_data == NULL)
1898             {
1899               sub->arelt_data = bfd_alloc (sub, sizeof (struct areltdata));
1900               if (sub->arelt_data == NULL)
1901                 return false;
1902             }
1903
1904           arch_eltdata (sub)->parsed_size = s.st_size;
1905         }
1906
1907       sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
1908       sprintf (ahdrp->namlen, "%ld", (long) namlen);
1909
1910       /* If the length of the name is odd, we write out the null byte
1911          after the name as well.  */
1912       namlen = (namlen + 1) &~ 1;
1913
1914       remaining = arelt_size (sub);
1915       size = (SIZEOF_AR_HDR
1916               + namlen
1917               + SXCOFFARFMAG
1918               + remaining);
1919
1920       BFD_ASSERT (nextoff == bfd_tell (abfd));
1921
1922       offsets[i] = nextoff;
1923
1924       prevoff = nextoff;
1925       nextoff += size + (size & 1);
1926
1927       sprintf (ahdrp->nextoff, "%ld", (long) nextoff);
1928
1929       /* We need spaces, not null bytes, in the header.  */
1930       for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
1931         if (*p == '\0')
1932           *p = ' ';
1933
1934       if (bfd_write ((PTR) ahdrp, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
1935           || bfd_write ((PTR) name, 1, namlen, abfd) != namlen
1936           || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
1937               != SXCOFFARFMAG))
1938         return false;
1939
1940       if (bfd_seek (sub, (file_ptr) 0, SEEK_SET) != 0)
1941         return false;
1942       while (remaining != 0)
1943         {
1944           bfd_size_type amt;
1945           bfd_byte buffer[DEFAULT_BUFFERSIZE];
1946
1947           amt = sizeof buffer;
1948           if (amt > remaining)
1949             amt = remaining;
1950           if (bfd_read (buffer, 1, amt, sub) != amt
1951               || bfd_write (buffer, 1, amt, abfd) != amt)
1952             return false;
1953           remaining -= amt;
1954         }
1955
1956       if ((size & 1) != 0)
1957         {
1958           bfd_byte b;
1959
1960           b = '\0';
1961           if (bfd_write (&b, 1, 1, abfd) != 1)
1962             return false;
1963         }
1964     }
1965
1966   sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
1967
1968   /* Write out the member table.  */
1969
1970   BFD_ASSERT (nextoff == bfd_tell (abfd));
1971   sprintf (fhdr.memoff, "%ld", (long) nextoff);
1972
1973   memset (&ahdr, 0, sizeof ahdr);
1974   sprintf (ahdr.size, "%ld", (long) (12 + count * 12 + total_namlen));
1975   sprintf (ahdr.prevoff, "%ld", (long) prevoff);
1976   sprintf (ahdr.date, "%d", 0);
1977   sprintf (ahdr.uid, "%d", 0);
1978   sprintf (ahdr.gid, "%d", 0);
1979   sprintf (ahdr.mode, "%d", 0);
1980   sprintf (ahdr.namlen, "%d", 0);
1981
1982   size = (SIZEOF_AR_HDR
1983           + 12
1984           + count * 12
1985           + total_namlen
1986           + SXCOFFARFMAG);
1987
1988   prevoff = nextoff;
1989   nextoff += size + (size & 1);
1990
1991   if (makemap && hasobjects)
1992     sprintf (ahdr.nextoff, "%ld", (long) nextoff);
1993   else
1994     sprintf (ahdr.nextoff, "%d", 0);
1995
1996   /* We need spaces, not null bytes, in the header.  */
1997   for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
1998     if (*p == '\0')
1999       *p = ' ';
2000
2001   if (bfd_write ((PTR) &ahdr, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
2002       || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
2003           != SXCOFFARFMAG))
2004     return false;
2005
2006   sprintf (decbuf, "%-12ld", (long) count);
2007   if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
2008     return false;
2009   for (i = 0; i < count; i++)
2010     {
2011       sprintf (decbuf, "%-12ld", (long) offsets[i]);
2012       if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
2013         return false;
2014     }
2015   for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
2016     {
2017       const char *name;
2018       size_t namlen;
2019
2020       name = normalize_filename (sub);
2021       namlen = strlen (name);
2022       if (bfd_write ((PTR) name, 1, namlen + 1, abfd) != namlen + 1)
2023         return false;
2024     }
2025   if ((size & 1) != 0)
2026     {
2027       bfd_byte b;
2028
2029       b = '\0';
2030       if (bfd_write ((PTR) &b, 1, 1, abfd) != 1)
2031         return false;
2032     }
2033
2034   /* Write out the armap, if appropriate.  */
2035
2036   if (! makemap || ! hasobjects)
2037     sprintf (fhdr.symoff, "%d", 0);
2038   else
2039     {
2040       BFD_ASSERT (nextoff == bfd_tell (abfd));
2041       sprintf (fhdr.symoff, "%ld", (long) nextoff);
2042       bfd_ardata (abfd)->tdata = (PTR) &fhdr;
2043       if (! _bfd_compute_and_write_armap (abfd, 0))
2044         return false;
2045     }
2046
2047   /* Write out the archive file header.  */
2048
2049   /* We need spaces, not null bytes, in the header.  */
2050   for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR; p++)
2051     if (*p == '\0')
2052       *p = ' ';
2053
2054   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2055       || (bfd_write ((PTR) &fhdr, SIZEOF_AR_FILE_HDR, 1, abfd) !=
2056           SIZEOF_AR_FILE_HDR))
2057     return false;
2058
2059   return true;
2060 }
2061
2062 static boolean
2063 xcoff_write_archive_contents_big (abfd)
2064      bfd *abfd;
2065 {
2066   struct xcoff_ar_file_hdr_big fhdr;
2067   size_t count;
2068   size_t total_namlen;
2069   file_ptr *offsets;
2070   boolean makemap;
2071   boolean hasobjects;
2072   file_ptr prevoff, nextoff;
2073   bfd *sub;
2074   unsigned int i;
2075   struct xcoff_ar_hdr_big ahdr;
2076   bfd_size_type size;
2077   char *p;
2078   char decbuf[13];
2079
2080   memset (&fhdr, 0, sizeof fhdr);
2081   strncpy (fhdr.magic, XCOFFARMAGBIG, SXCOFFARMAG);
2082   sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR_BIG);
2083   sprintf (fhdr.freeoff, "%d", 0);
2084
2085   count = 0;
2086   total_namlen = 0;
2087   for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
2088     {
2089       ++count;
2090       total_namlen += strlen (normalize_filename (sub)) + 1;
2091     }
2092   offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
2093   if (offsets == NULL)
2094     return false;
2095
2096   if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR_BIG, SEEK_SET) != 0)
2097     return false;
2098
2099   makemap = bfd_has_map (abfd);
2100   hasobjects = false;
2101   prevoff = 0;
2102   nextoff = SIZEOF_AR_FILE_HDR_BIG;
2103   for (sub = abfd->archive_head, i = 0; sub != NULL; sub = sub->next, i++)
2104     {
2105       const char *name;
2106       size_t namlen;
2107       struct xcoff_ar_hdr_big *ahdrp;
2108       bfd_size_type remaining;
2109
2110       if (makemap && ! hasobjects)
2111         {
2112           if (bfd_check_format (sub, bfd_object))
2113             hasobjects = true;
2114         }
2115
2116       name = normalize_filename (sub);
2117       namlen = strlen (name);
2118
2119       if (sub->arelt_data != NULL)
2120         ahdrp = arch_xhdr_big (sub);
2121       else
2122         ahdrp = NULL;
2123
2124       if (ahdrp == NULL)
2125         {
2126           struct stat s;
2127
2128           memset (&ahdr, 0, sizeof ahdr);
2129           ahdrp = &ahdr;
2130           /* XXX This should actually be a call to stat64 (at least on
2131              32-bit machines).  */
2132           if (stat (bfd_get_filename (sub), &s) != 0)
2133             {
2134               bfd_set_error (bfd_error_system_call);
2135               return false;
2136             }
2137
2138           /* XXX This call actually should use %lld (at least on 32-bit
2139              machines) since the fields's width is 20 and there numbers with
2140              more than 32 bits can be represented.  */
2141           sprintf (ahdrp->size, "%ld", (long) s.st_size);
2142           sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
2143           sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
2144           sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
2145           sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
2146
2147           if (sub->arelt_data == NULL)
2148             {
2149               sub->arelt_data = bfd_alloc (sub, sizeof (struct areltdata));
2150               if (sub->arelt_data == NULL)
2151                 return false;
2152             }
2153
2154           arch_eltdata (sub)->parsed_size = s.st_size;
2155         }
2156
2157       /* XXX These calls actually should use %lld (at least on 32-bit
2158          machines) since the fields's width is 20 and there numbers with
2159          more than 32 bits can be represented.  */
2160       sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
2161       sprintf (ahdrp->namlen, "%ld", (long) namlen);
2162
2163       /* If the length of the name is odd, we write out the null byte
2164          after the name as well.  */
2165       namlen = (namlen + 1) &~ 1;
2166
2167       remaining = arelt_size (sub);
2168       size = (SIZEOF_AR_HDR_BIG
2169               + namlen
2170               + SXCOFFARFMAG
2171               + remaining);
2172
2173       BFD_ASSERT (nextoff == bfd_tell (abfd));
2174
2175       offsets[i] = nextoff;
2176
2177       prevoff = nextoff;
2178       nextoff += size + (size & 1);
2179
2180       sprintf (ahdrp->nextoff, "%ld", (long) nextoff);
2181
2182       /* We need spaces, not null bytes, in the header.  */
2183       for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR_BIG; p++)
2184         if (*p == '\0')
2185           *p = ' ';
2186
2187       if (bfd_write ((PTR) ahdrp, 1, SIZEOF_AR_HDR_BIG, abfd)
2188           != SIZEOF_AR_HDR_BIG
2189           || bfd_write ((PTR) name, 1, namlen, abfd) != namlen
2190           || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
2191               != SXCOFFARFMAG))
2192         return false;
2193
2194       if (bfd_seek (sub, (file_ptr) 0, SEEK_SET) != 0)
2195         return false;
2196       while (remaining != 0)
2197         {
2198           bfd_size_type amt;
2199           bfd_byte buffer[DEFAULT_BUFFERSIZE];
2200
2201           amt = sizeof buffer;
2202           if (amt > remaining)
2203             amt = remaining;
2204           if (bfd_read (buffer, 1, amt, sub) != amt
2205               || bfd_write (buffer, 1, amt, abfd) != amt)
2206             return false;
2207           remaining -= amt;
2208         }
2209
2210       if ((size & 1) != 0)
2211         {
2212           bfd_byte b;
2213
2214           b = '\0';
2215           if (bfd_write (&b, 1, 1, abfd) != 1)
2216             return false;
2217         }
2218     }
2219
2220   /* XXX This call actually should use %lld (at least on 32-bit
2221      machines) since the fields's width is 20 and there numbers with
2222      more than 32 bits can be represented.  */
2223   sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
2224
2225   /* Write out the member table.  */
2226
2227   BFD_ASSERT (nextoff == bfd_tell (abfd));
2228   /* XXX This call actually should use %lld (at least on 32-bit
2229      machines) since the fields's width is 20 and there numbers with
2230      more than 32 bits can be represented.  */
2231   sprintf (fhdr.memoff, "%ld", (long) nextoff);
2232
2233   memset (&ahdr, 0, sizeof ahdr);
2234   /* XXX The next two calls actually should use %lld (at least on 32-bit
2235      machines) since the fields's width is 20 and there numbers with
2236      more than 32 bits can be represented.  */
2237   sprintf (ahdr.size, "%ld", (long) (12 + count * 12 + total_namlen));
2238   sprintf (ahdr.prevoff, "%ld", (long) prevoff);
2239   sprintf (ahdr.date, "%d", 0);
2240   sprintf (ahdr.uid, "%d", 0);
2241   sprintf (ahdr.gid, "%d", 0);
2242   sprintf (ahdr.mode, "%d", 0);
2243   sprintf (ahdr.namlen, "%d", 0);
2244
2245   size = (SIZEOF_AR_HDR_BIG
2246           + 12
2247           + count * 12
2248           + total_namlen
2249           + SXCOFFARFMAG);
2250
2251   prevoff = nextoff;
2252   nextoff += size + (size & 1);
2253
2254   if (makemap && hasobjects)
2255     /* XXX This call actually should use %lld (at least on 32-bit
2256        machines) since the fields's width is 20 and there numbers with
2257        more than 32 bits can be represented.  */
2258     sprintf (ahdr.nextoff, "%ld", (long) nextoff);
2259   else
2260     sprintf (ahdr.nextoff, "%d", 0);
2261
2262   /* We need spaces, not null bytes, in the header.  */
2263   for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR_BIG; p++)
2264     if (*p == '\0')
2265       *p = ' ';
2266
2267   if (bfd_write ((PTR) &ahdr, 1, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG
2268       || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
2269           != SXCOFFARFMAG))
2270     return false;
2271
2272   sprintf (decbuf, "%-12ld", (long) count);
2273   if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
2274     return false;
2275   for (i = 0; i < count; i++)
2276     {
2277       sprintf (decbuf, "%-12ld", (long) offsets[i]);
2278       if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
2279         return false;
2280     }
2281   for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
2282     {
2283       const char *name;
2284       size_t namlen;
2285
2286       name = normalize_filename (sub);
2287       namlen = strlen (name);
2288       if (bfd_write ((PTR) name, 1, namlen + 1, abfd) != namlen + 1)
2289         return false;
2290     }
2291   if ((size & 1) != 0)
2292     {
2293       bfd_byte b;
2294
2295       b = '\0';
2296       if (bfd_write ((PTR) &b, 1, 1, abfd) != 1)
2297         return false;
2298     }
2299
2300   /* Write out the armap, if appropriate.  */
2301
2302   if (! makemap || ! hasobjects)
2303     sprintf (fhdr.symoff, "%d", 0);
2304   else
2305     {
2306       BFD_ASSERT (nextoff == bfd_tell (abfd));
2307       /* XXX This call actually should use %lld (at least on 32-bit
2308          machines) since the fields's width is 20 and there numbers with
2309          more than 32 bits can be represented.  */
2310       bfd_ardata (abfd)->tdata = (PTR) &fhdr;
2311       if (! _bfd_compute_and_write_armap (abfd, 0))
2312         return false;
2313     }
2314
2315   /* Write out the archive file header.  */
2316
2317   /* We need spaces, not null bytes, in the header.  */
2318   for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR_BIG; p++)
2319     if (*p == '\0')
2320       *p = ' ';
2321
2322   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2323       || (bfd_write ((PTR) &fhdr, SIZEOF_AR_FILE_HDR_BIG, 1, abfd) !=
2324           SIZEOF_AR_FILE_HDR_BIG))
2325     return false;
2326
2327   return true;
2328 }
2329
2330 boolean
2331 _bfd_xcoff_write_archive_contents (abfd)
2332      bfd *abfd;
2333 {
2334   if (! xcoff_big_format_p (abfd))
2335     return xcoff_write_archive_contents_old (abfd);
2336   else
2337     return xcoff_write_archive_contents_big (abfd);
2338 }
2339 \f
2340 /* We can't use the usual coff_sizeof_headers routine, because AIX
2341    always uses an a.out header.  */
2342
2343 int
2344 _bfd_xcoff_sizeof_headers (abfd, reloc)
2345      bfd *abfd;
2346      boolean reloc ATTRIBUTE_UNUSED;
2347 {
2348   int size;
2349
2350   size = FILHSZ;
2351   if (xcoff_data (abfd)->full_aouthdr)
2352     size += AOUTSZ;
2353   else
2354     size += SMALL_AOUTSZ;
2355   size += abfd->section_count * SCNHSZ;
2356   return size;
2357 }