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