OSDN Git Service

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