OSDN Git Service

Support for xcoff64
[pf3gnuchains/pf3gnuchains3x.git] / bfd / coff-rs6000.c
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
14 This file is part of BFD, the Binary File Descriptor library.
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
29
30 #include "bfd.h"
31 #include "sysdep.h"
32 #include "bfdlink.h"
33 #include "libbfd.h"
34 #include "coff/internal.h"
35 #include "coff/xcoff.h"
36 #include "coff/rs6000.h"
37 #include "libcoff.h"
38 #include "libxcoff.h"
39
40 extern const bfd_target * rs6000coff_core_p ();
41 extern boolean rs6000coff_core_file_matches_executable_p ();
42 extern char *rs6000coff_core_file_failing_command PARAMS ((bfd *abfd));
43 extern int rs6000coff_core_file_failing_signal PARAMS ((bfd *abfd));
44 extern boolean _bfd_xcoff_mkobject PARAMS ((bfd *));
45 extern boolean _bfd_xcoff_copy_private_bfd_data PARAMS ((bfd *, bfd *));
46 extern boolean _bfd_xcoff_is_local_label_name PARAMS ((bfd *, const char *));
47 extern reloc_howto_type *_bfd_xcoff_reloc_type_lookup
48   PARAMS ((bfd *, bfd_reloc_code_real_type));
49 extern boolean _bfd_xcoff_slurp_armap PARAMS ((bfd *));
50 extern const bfd_target *_bfd_xcoff_archive_p PARAMS ((bfd *));
51 extern PTR _bfd_xcoff_read_ar_hdr PARAMS ((bfd *));
52 extern bfd *_bfd_xcoff_openr_next_archived_file PARAMS ((bfd *, bfd *));
53 extern int _bfd_xcoff_generic_stat_arch_elt PARAMS ((bfd *, struct stat *));
54 extern boolean _bfd_xcoff_write_armap
55   PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int));
56 extern boolean _bfd_xcoff_write_archive_contents PARAMS ((bfd *));
57 extern int _bfd_xcoff_sizeof_headers PARAMS ((bfd *, boolean));
58 extern void _bfd_xcoff_swap_sym_in PARAMS ((bfd *, PTR, PTR));
59 extern unsigned int _bfd_xcoff_swap_sym_out PARAMS ((bfd *, PTR, PTR));
60 extern void _bfd_xcoff_swap_aux_in PARAMS ((bfd *, PTR, int, int, int, int, PTR));
61 extern unsigned int _bfd_xcoff_swap_aux_out PARAMS ((bfd *, PTR, int, int, int, int, PTR));
62
63 /* Forward declare _bfd_xcoff_rtype2howto for coffcode.h macro */
64 void _bfd_xcoff_rtype2howto PARAMS ((arelent *, struct internal_reloc *));
65
66 /* coffcode.h needs these to be defined */
67 #define RS6000COFF_C 1
68
69 #define SELECT_RELOC(internal, howto)                                   \
70   {                                                                     \
71     internal.r_type = howto->type;                                      \
72     internal.r_size =                                                   \
73       ((howto->complain_on_overflow == complain_overflow_signed         \
74         ? 0x80                                                          \
75         : 0)                                                            \
76        | (howto->bitsize - 1));                                         \
77   }
78
79 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
80 #define COFF_LONG_FILENAMES
81 #define NO_COFF_SYMBOLS
82 #define RTYPE2HOWTO(cache_ptr, dst) _bfd_xcoff_rtype2howto (cache_ptr, dst)
83 #define coff_mkobject _bfd_xcoff_mkobject 
84 #define coff_bfd_copy_private_bfd_data _bfd_xcoff_copy_private_bfd_data 
85 #define coff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name 
86 #define coff_bfd_reloc_type_lookup _bfd_xcoff_reloc_type_lookup 
87 #define CORE_FILE_P rs6000coff_core_p
88 #define coff_SWAP_sym_in _bfd_xcoff_swap_sym_in
89 #define coff_SWAP_sym_out _bfd_xcoff_swap_sym_out
90 #define coff_SWAP_aux_in _bfd_xcoff_swap_aux_in
91 #define coff_SWAP_aux_out _bfd_xcoff_swap_aux_out
92
93 #include "coffcode.h"
94
95 /* The main body of code is in coffcode.h.  */
96
97 static const char *normalize_filename PARAMS ((bfd *));
98
99 /* We use our own tdata type.  Its first field is the COFF tdata type,
100    so the COFF routines are compatible.  */
101
102 boolean
103 _bfd_xcoff_mkobject (abfd)
104      bfd *abfd;
105 {
106   coff_data_type *coff;
107
108   abfd->tdata.xcoff_obj_data =
109     ((struct xcoff_tdata *)
110      bfd_zalloc (abfd, sizeof (struct xcoff_tdata)));
111   if (abfd->tdata.xcoff_obj_data == NULL)
112     return false;
113   coff = coff_data (abfd);
114   coff->symbols = (coff_symbol_type *) NULL;
115   coff->conversion_table = (unsigned int *) NULL;
116   coff->raw_syments = (struct coff_ptr_struct *) NULL;
117   coff->relocbase = 0;
118
119   xcoff_data (abfd)->modtype = ('1' << 8) | 'L';
120
121   /* We set cputype to -1 to indicate that it has not been
122      initialized.  */
123   xcoff_data (abfd)->cputype = -1;
124
125   xcoff_data (abfd)->csects = NULL;
126   xcoff_data (abfd)->debug_indices = NULL;
127
128   /* text section alignment is different than the default */
129   /* xcoff_data (abfd)->text_align_power = 5; */
130
131   return true;
132 }
133
134 /* Copy XCOFF data from one BFD to another.  */
135
136 boolean
137 _bfd_xcoff_copy_private_bfd_data (ibfd, obfd)
138      bfd *ibfd;
139      bfd *obfd;
140 {
141   struct xcoff_tdata *ix, *ox;
142   asection *sec;
143
144   if (ibfd->xvec != obfd->xvec)
145     return true;
146   ix = xcoff_data (ibfd);
147   ox = xcoff_data (obfd);
148   ox->full_aouthdr = ix->full_aouthdr;
149   ox->toc = ix->toc;
150   if (ix->sntoc == 0)
151     ox->sntoc = 0;
152   else
153     {
154       sec = coff_section_from_bfd_index (ibfd, ix->sntoc);
155       if (sec == NULL)
156         ox->sntoc = 0;
157       else
158         ox->sntoc = sec->output_section->target_index;
159     }
160   if (ix->snentry == 0)
161     ox->snentry = 0;
162   else
163     {
164       sec = coff_section_from_bfd_index (ibfd, ix->snentry);
165       if (sec == NULL)
166         ox->snentry = 0;
167       else
168         ox->snentry = sec->output_section->target_index;
169     }
170   ox->text_align_power = ix->text_align_power;
171   ox->data_align_power = ix->data_align_power;
172   ox->modtype = ix->modtype;
173   ox->cputype = ix->cputype;
174   ox->maxdata = ix->maxdata;
175   ox->maxstack = ix->maxstack;
176   return true;
177 }
178
179 /* I don't think XCOFF really has a notion of local labels based on
180    name.  This will mean that ld -X doesn't actually strip anything.
181    The AIX native linker does not have a -X option, and it ignores the
182    -x option.  */
183
184 boolean
185 _bfd_xcoff_is_local_label_name (abfd, name)
186      bfd *abfd ATTRIBUTE_UNUSED;
187      const char *name ATTRIBUTE_UNUSED;
188 {
189   return false;
190 }
191 \f
192
193
194 void
195 _bfd_xcoff_swap_sym_in (abfd, ext1, in1)
196      bfd            *abfd;
197      PTR ext1;
198      PTR in1;
199 {
200   SYMENT *ext = (SYMENT *)ext1;
201   struct internal_syment      *in = (struct internal_syment *)in1;
202
203   if(ext->e.e_name[0] != 0) {
204     memcpy(in->_n._n_name, ext->e.e_name, SYMNMLEN);
205   } else {
206     in->_n._n_n._n_zeroes = 0;
207     in->_n._n_n._n_offset = 
208       bfd_h_get_32(abfd, (bfd_byte *) ext->e.e.e_offset);
209   }
210
211   in->n_value = bfd_h_get_32(abfd, (bfd_byte *) ext->e_value); 
212   in->n_scnum = bfd_h_get_16(abfd, (bfd_byte *) ext->e_scnum);
213   in->n_type = bfd_h_get_16(abfd, (bfd_byte *) ext->e_type);
214   in->n_sclass = bfd_h_get_8(abfd, ext->e_sclass);
215   in->n_numaux = bfd_h_get_8(abfd, ext->e_numaux);
216 }
217
218 unsigned int
219 _bfd_xcoff_swap_sym_out (abfd, inp, extp)
220      bfd       *abfd;
221      PTR        inp;
222      PTR        extp;
223 {
224   struct internal_syment *in = (struct internal_syment *)inp;
225   SYMENT *ext =(SYMENT *)extp;
226
227   if(in->_n._n_name[0] != 0) {
228     memcpy(ext->e.e_name, in->_n._n_name, SYMNMLEN);
229   } else {
230     bfd_h_put_32(abfd, 0, (bfd_byte *) ext->e.e.e_zeroes);
231     bfd_h_put_32(abfd, in->_n._n_n._n_offset, 
232                  (bfd_byte *)  ext->e.e.e_offset);
233   }
234
235   bfd_h_put_32(abfd,  in->n_value , (bfd_byte *) ext->e_value);
236   bfd_h_put_16(abfd,  in->n_scnum , (bfd_byte *) ext->e_scnum);
237   bfd_h_put_16(abfd,  in->n_type , (bfd_byte *) ext->e_type);
238   bfd_h_put_8(abfd,  in->n_sclass , ext->e_sclass);
239   bfd_h_put_8(abfd,  in->n_numaux , ext->e_numaux);
240   return bfd_coff_symesz (abfd);
241 }
242
243 #define PUTWORD bfd_h_put_32
244 #define PUTHALF bfd_h_put_16
245 #define PUTBYTE bfd_h_put_8
246 #define GETWORD bfd_h_get_32
247 #define GETHALF bfd_h_get_16
248 #define GETBYTE bfd_h_get_8
249
250 void
251 _bfd_xcoff_swap_aux_in (abfd, ext1, type, class, indx, numaux, in1)
252      bfd            *abfd;
253      PTR              ext1;
254      int             type;
255      int             class;
256      int              indx;
257      int              numaux;
258      PTR              in1;
259 {
260   AUXENT    *ext = (AUXENT *)ext1;
261   union internal_auxent *in = (union internal_auxent *)in1;
262
263   switch (class) {
264     case C_FILE:
265       if (ext->x_file.x_fname[0] == 0) {
266           in->x_file.x_n.x_zeroes = 0;
267           in->x_file.x_n.x_offset = 
268            bfd_h_get_32(abfd, (bfd_byte *) ext->x_file.x_n.x_offset);
269         } else {
270             if (numaux > 1)
271               {
272                 if (indx == 0)
273                   memcpy (in->x_file.x_fname, ext->x_file.x_fname,
274                           numaux * sizeof (AUXENT));
275               }
276             else
277               {
278                 memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
279               }
280           }
281       goto end;
282
283       /* RS/6000 "csect" auxents */
284     case C_EXT:
285     case C_HIDEXT:
286       if (indx + 1 == numaux)
287         {
288           in->x_csect.x_scnlen.l = 
289               bfd_h_get_32 (abfd, ext->x_csect.x_scnlen);
290           in->x_csect.x_parmhash = bfd_h_get_32 (abfd,
291                                                  ext->x_csect.x_parmhash);
292           in->x_csect.x_snhash   = bfd_h_get_16 (abfd, ext->x_csect.x_snhash);
293           /* We don't have to hack bitfields in x_smtyp because it's
294              defined by shifts-and-ands, which are equivalent on all
295              byte orders.  */
296           in->x_csect.x_smtyp    = bfd_h_get_8  (abfd, ext->x_csect.x_smtyp);
297           in->x_csect.x_smclas   = bfd_h_get_8  (abfd, ext->x_csect.x_smclas);
298           in->x_csect.x_stab     = bfd_h_get_32 (abfd, ext->x_csect.x_stab);
299           in->x_csect.x_snstab   = bfd_h_get_16 (abfd, ext->x_csect.x_snstab);
300           goto end;
301         }
302       break;
303
304     case C_STAT:
305     case C_LEAFSTAT:
306     case C_HIDDEN:
307       if (type == T_NULL) {
308           in->x_scn.x_scnlen = bfd_h_get_32(abfd, 
309                   (bfd_byte *) ext->x_scn.x_scnlen);
310           in->x_scn.x_nreloc = bfd_h_get_16(abfd, 
311                   (bfd_byte *) ext->x_scn.x_nreloc);
312           in->x_scn.x_nlinno = bfd_h_get_16(abfd, 
313                   (bfd_byte *) ext->x_scn.x_nlinno);
314           /* PE defines some extra fields; we zero them out for
315              safety.  */
316           in->x_scn.x_checksum = 0;
317           in->x_scn.x_associated = 0;
318           in->x_scn.x_comdat = 0;
319
320           goto end;
321         }
322       break;
323     }
324
325   in->x_sym.x_tagndx.l = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_tagndx);
326   in->x_sym.x_tvndx = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_tvndx);
327
328   if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
329     {
330       in->x_sym.x_fcnary.x_fcn.x_lnnoptr = bfd_h_get_32(abfd, (bfd_byte *)
331               ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
332       in->x_sym.x_fcnary.x_fcn.x_endndx.l = bfd_h_get_32(abfd, (bfd_byte *)
333               ext->x_sym.x_fcnary.x_fcn.x_endndx);
334     }
335   else
336     {
337       in->x_sym.x_fcnary.x_ary.x_dimen[0] =
338         bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
339       in->x_sym.x_fcnary.x_ary.x_dimen[1] =
340         bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
341       in->x_sym.x_fcnary.x_ary.x_dimen[2] =
342         bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
343       in->x_sym.x_fcnary.x_ary.x_dimen[3] =
344         bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
345     }
346   if (ISFCN(type)) {
347     in->x_sym.x_misc.x_fsize = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_misc.x_fsize);
348   }
349   else {
350     in->x_sym.x_misc.x_lnsz.x_lnno = bfd_h_get_16(abfd, (bfd_byte *)
351             ext->x_sym.x_misc.x_lnsz.x_lnno);
352     in->x_sym.x_misc.x_lnsz.x_size = bfd_h_get_16(abfd, (bfd_byte *)
353             ext->x_sym.x_misc.x_lnsz.x_size);
354   }
355
356 end: ;
357   /* the semicolon is because MSVC doesn't like labels at
358      end of block. */
359
360 }
361
362
363
364 unsigned int
365 _bfd_xcoff_swap_aux_out (abfd, inp, type, class, indx, numaux, extp)
366      bfd   *abfd;
367      PTR        inp;
368      int   type;
369      int   class;
370      int   indx ATTRIBUTE_UNUSED;
371      int   numaux ATTRIBUTE_UNUSED;
372      PTR        extp;
373 {
374   union internal_auxent *in = (union internal_auxent *)inp;
375   AUXENT *ext = (AUXENT *)extp;
376
377   memset((PTR)ext, 0, bfd_coff_auxesz (abfd));
378   switch (class)
379     {
380   case C_FILE:
381     if (in->x_file.x_fname[0] == 0)
382       {
383       PUTWORD(abfd, 0, (bfd_byte *) ext->x_file.x_n.x_zeroes);
384       PUTWORD(abfd,
385               in->x_file.x_n.x_offset,
386               (bfd_byte *) ext->x_file.x_n.x_offset);
387     }
388     else
389       {
390       memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
391       }
392     goto end;
393
394   /* RS/6000 "csect" auxents */
395   case C_EXT:
396   case C_HIDEXT:
397     if (indx + 1 == numaux)
398       {
399         PUTWORD (abfd, in->x_csect.x_scnlen.l,ext->x_csect.x_scnlen);
400         PUTWORD (abfd, in->x_csect.x_parmhash,  ext->x_csect.x_parmhash);
401         PUTHALF (abfd, in->x_csect.x_snhash,    ext->x_csect.x_snhash);
402         /* We don't have to hack bitfields in x_smtyp because it's
403            defined by shifts-and-ands, which are equivalent on all
404            byte orders.  */
405         PUTBYTE (abfd, in->x_csect.x_smtyp,     ext->x_csect.x_smtyp);
406         PUTBYTE (abfd, in->x_csect.x_smclas,    ext->x_csect.x_smclas);
407         PUTWORD (abfd, in->x_csect.x_stab,      ext->x_csect.x_stab);
408         PUTHALF (abfd, in->x_csect.x_snstab,    ext->x_csect.x_snstab);
409         goto end;
410       }
411     break;
412
413   case C_STAT:
414   case C_LEAFSTAT:
415   case C_HIDDEN:
416     if (type == T_NULL) {
417       bfd_h_put_32(abfd, in->x_scn.x_scnlen, (bfd_byte *) ext->x_scn.x_scnlen);
418       bfd_h_put_16(abfd, in->x_scn.x_nreloc, (bfd_byte *) ext->x_scn.x_nreloc);
419       bfd_h_put_16(abfd, in->x_scn.x_nlinno, (bfd_byte *) ext->x_scn.x_nlinno);
420       goto end;
421     }
422     break;
423   }
424
425   PUTWORD(abfd, in->x_sym.x_tagndx.l, (bfd_byte *) ext->x_sym.x_tagndx);
426   bfd_h_put_16 (abfd, in->x_sym.x_tvndx , (bfd_byte *) ext->x_sym.x_tvndx);
427
428   if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
429     {
430       bfd_h_put_32(abfd,  in->x_sym.x_fcnary.x_fcn.x_lnnoptr, 
431               (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
432       PUTWORD(abfd,  in->x_sym.x_fcnary.x_fcn.x_endndx.l, 
433               (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_endndx);
434     }
435   else
436     {
437       bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
438                     (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
439       bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
440                     (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
441       bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
442                     (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
443       bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
444                     (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
445     }
446
447   if (ISFCN (type))
448     PUTWORD (abfd, in->x_sym.x_misc.x_fsize,
449              (bfd_byte *)  ext->x_sym.x_misc.x_fsize);
450   else
451     {
452       bfd_h_put_16(abfd, in->x_sym.x_misc.x_lnsz.x_lnno, 
453               (bfd_byte *)ext->x_sym.x_misc.x_lnsz.x_lnno);
454       bfd_h_put_16(abfd, in->x_sym.x_misc.x_lnsz.x_size, 
455               (bfd_byte *)ext->x_sym.x_misc.x_lnsz.x_size);
456     }
457
458 end:
459   return bfd_coff_auxesz (abfd);
460 }
461
462
463 \f
464 /* The XCOFF reloc table.  Actually, XCOFF relocations specify the
465    bitsize and whether they are signed or not, along with a
466    conventional type.  This table is for the types, which are used for
467    different algorithms for putting in the reloc.  Many of these
468    relocs need special_function entries, which I have not written.  */
469
470
471 reloc_howto_type xcoff_howto_table[] =
472 {
473   /* Standard 32 bit relocation.  */
474   HOWTO (0,                     /* type */
475          0,                     /* rightshift */
476          2,                     /* size (0 = byte, 1 = short, 2 = long) */
477          32,                    /* bitsize */
478          false,                 /* pc_relative */
479          0,                     /* bitpos */
480          complain_overflow_bitfield, /* complain_on_overflow */
481          0,                     /* special_function */
482          "R_POS",               /* name */
483          true,                  /* partial_inplace */
484          0xffffffff,            /* src_mask */
485          0xffffffff,            /* dst_mask */
486          false),                /* pcrel_offset */
487
488   /* 32 bit relocation, but store negative value.  */
489   HOWTO (1,                     /* type */
490          0,                     /* rightshift */
491          -2,                    /* size (0 = byte, 1 = short, 2 = long) */
492          32,                    /* bitsize */
493          false,                 /* pc_relative */
494          0,                     /* bitpos */
495          complain_overflow_bitfield, /* complain_on_overflow */
496          0,                     /* special_function */
497          "R_NEG",               /* name */
498          true,                  /* partial_inplace */
499          0xffffffff,            /* src_mask */
500          0xffffffff,            /* dst_mask */
501          false),                /* pcrel_offset */
502
503   /* 32 bit PC relative relocation.  */
504   HOWTO (2,                     /* type */
505          0,                     /* rightshift */
506          2,                     /* size (0 = byte, 1 = short, 2 = long) */
507          32,                    /* bitsize */
508          true,                  /* pc_relative */
509          0,                     /* bitpos */
510          complain_overflow_signed, /* complain_on_overflow */
511          0,                     /* special_function */
512          "R_REL",               /* name */
513          true,                  /* partial_inplace */
514          0xffffffff,            /* src_mask */
515          0xffffffff,            /* dst_mask */
516          false),                /* pcrel_offset */
517
518   /* 16 bit TOC relative relocation.  */
519   HOWTO (3,                     /* type */
520          0,                     /* rightshift */
521          1,                     /* size (0 = byte, 1 = short, 2 = long) */
522          16,                    /* bitsize */
523          false,                 /* pc_relative */
524          0,                     /* bitpos */
525          complain_overflow_bitfield, /* complain_on_overflow */
526          0,                     /* special_function */
527          "R_TOC",               /* name */
528          true,                  /* partial_inplace */
529          0xffff,                /* src_mask */
530          0xffff,                /* dst_mask */
531          false),                /* pcrel_offset */
532
533   /* I don't really know what this is.  */
534   HOWTO (4,                     /* type */
535          1,                     /* rightshift */
536          2,                     /* size (0 = byte, 1 = short, 2 = long) */
537          32,                    /* bitsize */
538          false,                 /* pc_relative */
539          0,                     /* bitpos */
540          complain_overflow_bitfield, /* complain_on_overflow */
541          0,                     /* special_function */
542          "R_RTB",               /* name */
543          true,                  /* partial_inplace */
544          0xffffffff,            /* src_mask */
545          0xffffffff,            /* dst_mask */
546          false),                /* pcrel_offset */
547
548   /* External TOC relative symbol.  */
549   HOWTO (5,                     /* type */
550          0,                     /* rightshift */
551          2,                     /* size (0 = byte, 1 = short, 2 = long) */
552          16,                    /* bitsize */
553          false,                 /* pc_relative */
554          0,                     /* bitpos */
555          complain_overflow_bitfield, /* complain_on_overflow */
556          0,                     /* special_function */
557          "R_GL",                /* name */
558          true,                  /* partial_inplace */
559          0xffff,                /* src_mask */
560          0xffff,                /* dst_mask */
561          false),                /* pcrel_offset */
562
563   /* Local TOC relative symbol.  */
564   HOWTO (6,                     /* type */
565          0,                     /* rightshift */
566          2,                     /* size (0 = byte, 1 = short, 2 = long) */
567          16,                    /* bitsize */
568          false,                 /* pc_relative */
569          0,                     /* bitpos */
570          complain_overflow_bitfield, /* complain_on_overflow */
571          0,                     /* special_function */
572          "R_TCL",               /* name */
573          true,                  /* partial_inplace */
574          0xffff,                /* src_mask */
575          0xffff,                /* dst_mask */
576          false),                /* pcrel_offset */
577
578   EMPTY_HOWTO (7),
579
580   /* Non modifiable absolute branch.  */
581   HOWTO (8,                     /* type */
582          0,                     /* rightshift */
583          2,                     /* size (0 = byte, 1 = short, 2 = long) */
584          26,                    /* bitsize */
585          false,                 /* pc_relative */
586          0,                     /* bitpos */
587          complain_overflow_bitfield, /* complain_on_overflow */
588          0,                     /* special_function */
589          "R_BA",                /* name */
590          true,                  /* partial_inplace */
591          0x3fffffc,             /* src_mask */
592          0x3fffffc,             /* dst_mask */
593          false),                /* pcrel_offset */
594
595   EMPTY_HOWTO (9),
596
597   /* Non modifiable relative branch.  */
598   HOWTO (0xa,                   /* type */
599          0,                     /* rightshift */
600          2,                     /* size (0 = byte, 1 = short, 2 = long) */
601          26,                    /* bitsize */
602          true,                  /* pc_relative */
603          0,                     /* bitpos */
604          complain_overflow_signed, /* complain_on_overflow */
605          0,                     /* special_function */
606          "R_BR",                /* name */
607          true,                  /* partial_inplace */
608          0x3fffffc,             /* src_mask */
609          0x3fffffc,             /* dst_mask */
610          false),                /* pcrel_offset */
611
612   EMPTY_HOWTO (0xb),
613
614   /* Indirect load.  */
615   HOWTO (0xc,                   /* type */
616          0,                     /* rightshift */
617          2,                     /* size (0 = byte, 1 = short, 2 = long) */
618          16,                    /* bitsize */
619          false,                 /* pc_relative */
620          0,                     /* bitpos */
621          complain_overflow_bitfield, /* complain_on_overflow */
622          0,                     /* special_function */
623          "R_RL",                /* name */
624          true,                  /* partial_inplace */
625          0xffff,                /* src_mask */
626          0xffff,                /* dst_mask */
627          false),                /* pcrel_offset */
628
629   /* Load address.  */
630   HOWTO (0xd,                   /* type */
631          0,                     /* rightshift */
632          2,                     /* size (0 = byte, 1 = short, 2 = long) */
633          16,                    /* bitsize */
634          false,                 /* pc_relative */
635          0,                     /* bitpos */
636          complain_overflow_bitfield, /* complain_on_overflow */
637          0,                     /* special_function */
638          "R_RLA",               /* name */
639          true,                  /* partial_inplace */
640          0xffff,                /* src_mask */
641          0xffff,                /* dst_mask */
642          false),                /* pcrel_offset */
643
644   EMPTY_HOWTO (0xe),
645
646   /* Non-relocating reference.  */
647   HOWTO (0xf,                   /* type */
648          0,                     /* rightshift */
649          2,                     /* size (0 = byte, 1 = short, 2 = long) */
650          32,                    /* bitsize */
651          false,                 /* pc_relative */
652          0,                     /* bitpos */
653          complain_overflow_bitfield, /* complain_on_overflow */
654          0,                     /* special_function */
655          "R_REF",               /* name */
656          false,                 /* partial_inplace */
657          0,                     /* src_mask */
658          0,                     /* dst_mask */
659          false),                /* pcrel_offset */
660
661   EMPTY_HOWTO (0x10),
662   EMPTY_HOWTO (0x11),
663
664   /* TOC relative indirect load.  */
665   HOWTO (0x12,                  /* type */
666          0,                     /* rightshift */
667          2,                     /* size (0 = byte, 1 = short, 2 = long) */
668          16,                    /* bitsize */
669          false,                 /* pc_relative */
670          0,                     /* bitpos */
671          complain_overflow_bitfield, /* complain_on_overflow */
672          0,                     /* special_function */
673          "R_TRL",               /* name */
674          true,                  /* partial_inplace */
675          0xffff,                /* src_mask */
676          0xffff,                /* dst_mask */
677          false),                /* pcrel_offset */
678
679   /* TOC relative load address.  */
680   HOWTO (0x13,                  /* type */
681          0,                     /* rightshift */
682          2,                     /* size (0 = byte, 1 = short, 2 = long) */
683          16,                    /* bitsize */
684          false,                 /* pc_relative */
685          0,                     /* bitpos */
686          complain_overflow_bitfield, /* complain_on_overflow */
687          0,                     /* special_function */
688          "R_TRLA",              /* name */
689          true,                  /* partial_inplace */
690          0xffff,                /* src_mask */
691          0xffff,                /* dst_mask */
692          false),                /* pcrel_offset */
693
694   /* Modifiable relative branch.  */
695   HOWTO (0x14,                  /* type */
696          1,                     /* rightshift */
697          2,                     /* size (0 = byte, 1 = short, 2 = long) */
698          32,                    /* bitsize */
699          false,                 /* pc_relative */
700          0,                     /* bitpos */
701          complain_overflow_bitfield, /* complain_on_overflow */
702          0,                     /* special_function */
703          "R_RRTBI",             /* name */
704          true,                  /* partial_inplace */
705          0xffffffff,            /* src_mask */
706          0xffffffff,            /* dst_mask */
707          false),                /* pcrel_offset */
708
709   /* Modifiable absolute branch.  */
710   HOWTO (0x15,                  /* type */
711          1,                     /* rightshift */
712          2,                     /* size (0 = byte, 1 = short, 2 = long) */
713          32,                    /* bitsize */
714          false,                 /* pc_relative */
715          0,                     /* bitpos */
716          complain_overflow_bitfield, /* complain_on_overflow */
717          0,                     /* special_function */
718          "R_RRTBA",             /* name */
719          true,                  /* partial_inplace */
720          0xffffffff,            /* src_mask */
721          0xffffffff,            /* dst_mask */
722          false),                /* pcrel_offset */
723
724   /* Modifiable call absolute indirect.  */
725   HOWTO (0x16,                  /* type */
726          0,                     /* rightshift */
727          2,                     /* size (0 = byte, 1 = short, 2 = long) */
728          16,                    /* bitsize */
729          false,                 /* pc_relative */
730          0,                     /* bitpos */
731          complain_overflow_bitfield, /* complain_on_overflow */
732          0,                     /* special_function */
733          "R_CAI",               /* name */
734          true,                  /* partial_inplace */
735          0xffff,                /* src_mask */
736          0xffff,                /* dst_mask */
737          false),                /* pcrel_offset */
738
739   /* Modifiable call relative.  */
740   HOWTO (0x17,                  /* type */
741          0,                     /* rightshift */
742          2,                     /* size (0 = byte, 1 = short, 2 = long) */
743          16,                    /* bitsize */
744          false,                 /* pc_relative */
745          0,                     /* bitpos */
746          complain_overflow_bitfield, /* complain_on_overflow */
747          0,                     /* special_function */
748          "R_CREL",              /* name */
749          true,                  /* partial_inplace */
750          0xffff,                /* src_mask */
751          0xffff,                /* dst_mask */
752          false),                /* pcrel_offset */
753
754   /* Modifiable branch absolute.  */
755   HOWTO (0x18,                  /* type */
756          0,                     /* rightshift */
757          2,                     /* size (0 = byte, 1 = short, 2 = long) */
758          26,                    /* bitsize */
759          false,                 /* pc_relative */
760          0,                     /* bitpos */
761          complain_overflow_bitfield, /* complain_on_overflow */
762          0,                     /* special_function */
763          "R_RBA",               /* name */
764          true,                  /* partial_inplace */
765          0xffff,                /* src_mask */
766          0xffff,                /* dst_mask */
767          false),                /* pcrel_offset */
768
769   /* Modifiable branch absolute.  */
770   HOWTO (0x19,                  /* type */
771          0,                     /* rightshift */
772          2,                     /* size (0 = byte, 1 = short, 2 = long) */
773          32,                    /* bitsize */
774          false,                 /* pc_relative */
775          0,                     /* bitpos */
776          complain_overflow_bitfield, /* complain_on_overflow */
777          0,                     /* special_function */
778          "R_RBAC",              /* name */
779          true,                  /* partial_inplace */
780          0xffff,                /* src_mask */
781          0xffff,                /* dst_mask */
782          false),                /* pcrel_offset */
783
784   /* Modifiable branch relative.  */
785   HOWTO (0x1a,                  /* type */
786          0,                     /* rightshift */
787          2,                     /* size (0 = byte, 1 = short, 2 = long) */
788          26,                    /* bitsize */
789          false,                 /* pc_relative */
790          0,                     /* bitpos */
791          complain_overflow_signed, /* complain_on_overflow */
792          0,                     /* special_function */
793          "R_RBR",               /* name */
794          true,                  /* partial_inplace */
795          0xffff,                /* src_mask */
796          0xffff,                /* dst_mask */
797          false),                /* pcrel_offset */
798
799   /* Modifiable branch absolute.  */
800   HOWTO (0x1b,                  /* type */
801          0,                     /* rightshift */
802          2,                     /* size (0 = byte, 1 = short, 2 = long) */
803          16,                    /* bitsize */
804          false,                 /* pc_relative */
805          0,                     /* bitpos */
806          complain_overflow_bitfield, /* complain_on_overflow */
807          0,                     /* special_function */
808          "R_RBRC",              /* name */
809          true,                  /* partial_inplace */
810          0xffff,                /* src_mask */
811          0xffff,                /* dst_mask */
812          false),                /* pcrel_offset */
813
814   HOWTO (0,                     /* type */
815          0,                     /* rightshift */
816          4,                     /* size (0 = byte, 1 = short, 2 = long) */
817          64,                    /* bitsize */
818          false,                 /* pc_relative */
819          0,                     /* bitpos */
820          complain_overflow_bitfield, /* complain_on_overflow */
821          0,                     /* special_function */
822          "R_POS",               /* name */
823          true,                  /* partial_inplace */
824          MINUS_ONE,             /* src_mask */
825          MINUS_ONE,             /* dst_mask */
826          false)                 /* pcrel_offset */
827
828 };
829
830 void
831 _bfd_xcoff_rtype2howto (relent, internal)
832      arelent *relent;
833      struct internal_reloc *internal;
834 {
835   relent->howto = xcoff_howto_table + internal->r_type;
836
837   /* Check for relocs we don't know of.  */
838   if (internal->r_type
839       >= sizeof (xcoff_howto_table) / sizeof (xcoff_howto_table[0]))
840     abort ();
841   if (internal->r_type != relent->howto->type)
842     abort ();
843
844   /* The r_size field of an XCOFF reloc encodes the bitsize of the
845      relocation, as well as indicating whether it is signed or not.
846      Doublecheck that the relocation information gathered from the
847      type matches this information.  The bitsize is not significant
848      for R_REF relocs.  */
849   if (relent->howto->dst_mask != 0
850       && (relent->howto->bitsize 
851           != ((unsigned int) internal->r_size & 0x3f) + 1))
852     abort ();
853 #if 0
854   if ((internal->r_size & 0x80) != 0
855       ? (relent->howto->complain_on_overflow != complain_overflow_signed)
856       : (relent->howto->complain_on_overflow != complain_overflow_bitfield))
857     abort ();
858 #endif
859 }
860
861 reloc_howto_type *
862 _bfd_xcoff_reloc_type_lookup (abfd, code)
863      bfd *abfd ATTRIBUTE_UNUSED;
864      bfd_reloc_code_real_type code;
865 {
866   switch (code)
867     {
868     case BFD_RELOC_PPC_B26:
869       return &xcoff_howto_table[0xa];
870     case BFD_RELOC_PPC_BA26:
871       return &xcoff_howto_table[8];
872     case BFD_RELOC_PPC_TOC16:
873       return &xcoff_howto_table[3];
874     case BFD_RELOC_32:
875     case BFD_RELOC_CTOR:
876       return &xcoff_howto_table[0];
877     case BFD_RELOC_64:
878       return &xcoff_howto_table[0x1c];
879     default:
880       return NULL;
881     }
882 }
883
884 \f
885 /* XCOFF archive support.  The original version of this code was by
886    Damon A. Permezel.  It was enhanced to permit cross support, and
887    writing archive files, by Ian Lance Taylor, Cygnus Support.
888
889    XCOFF uses its own archive format.  Everything is hooked together
890    with file offset links, so it is possible to rapidly update an
891    archive in place.  Of course, we don't do that.  An XCOFF archive
892    has a real file header, not just an ARMAG string.  The structure of
893    the file header and of each archive header appear below.
894
895    An XCOFF archive also has a member table, which is a list of
896    elements in the archive (you can get that by looking through the
897    linked list, but you have to read a lot more of the file).  The
898    member table has a normal archive header with an empty name.  It is
899    normally (and perhaps must be) the second to last entry in the
900    archive.  The member table data is almost printable ASCII.  It
901    starts with a 12 character decimal string which is the number of
902    entries in the table.  For each entry it has a 12 character decimal
903    string which is the offset in the archive of that member.  These
904    entries are followed by a series of null terminated strings which
905    are the member names for each entry.
906
907    Finally, an XCOFF archive has a global symbol table, which is what
908    we call the armap.  The global symbol table has a normal archive
909    header with an empty name.  It is normally (and perhaps must be)
910    the last entry in the archive.  The contents start with a four byte
911    binary number which is the number of entries.  This is followed by
912    a that many four byte binary numbers; each is the file offset of an
913    entry in the archive.  These numbers are followed by a series of
914    null terminated strings, which are symbol names.
915
916    AIX 4.3 introduced a new archive format which can handle larger
917    files and also 32- and 64-bit objects in the same archive.  The
918    things said above remain true except that there is now more than
919    one global symbol table.  The one is used to index 32-bit objects,
920    the other for 64-bit objects.
921
922    The new archives (recognizable by the new ARMAG string) has larger
923    field lengths so that we cannot really share any code.  Also we have
924    to take care that we are not generating the new form of archives
925    on AIX 4.2 or earlier systems.  */
926
927 /* XCOFF archives use this as a magic string.  Note that both strings
928    have the same length.  */
929
930
931
932 /* Read in the armap of an XCOFF archive.  */
933
934 boolean
935 _bfd_xcoff_slurp_armap (abfd)
936      bfd *abfd;
937 {
938   file_ptr off;
939   size_t namlen;
940   bfd_size_type sz;
941   bfd_byte *contents, *cend;
942   bfd_vma c, i;
943   carsym *arsym;
944   bfd_byte *p;
945
946   if (xcoff_ardata (abfd) == NULL)
947     {
948       bfd_has_map (abfd) = false;
949       return true;
950     }
951
952   if (! xcoff_big_format_p (abfd))
953     {
954       /* This is for the old format.  */
955       struct xcoff_ar_hdr hdr;
956
957       off = strtol (xcoff_ardata (abfd)->symoff, (char **) NULL, 10);
958       if (off == 0)
959         {
960           bfd_has_map (abfd) = false;
961           return true;
962         }
963
964       if (bfd_seek (abfd, off, SEEK_SET) != 0)
965         return false;
966
967       /* The symbol table starts with a normal archive header.  */
968       if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR)
969         return false;
970
971       /* Skip the name (normally empty).  */
972       namlen = strtol (hdr.namlen, (char **) NULL, 10);
973       if (bfd_seek (abfd, ((namlen + 1) & ~1) + SXCOFFARFMAG, SEEK_CUR) != 0)
974         return false;
975
976       sz = strtol (hdr.size, (char **) NULL, 10);
977
978       /* Read in the entire symbol table.  */
979       contents = (bfd_byte *) bfd_alloc (abfd, sz);
980       if (contents == NULL)
981         return false;
982       if (bfd_read ((PTR) contents, 1, sz, abfd) != sz)
983         return false;
984
985       /* The symbol table starts with a four byte count.  */
986       c = bfd_h_get_32 (abfd, contents);
987       
988       if (c * 4 >= sz)
989         {
990           bfd_set_error (bfd_error_bad_value);
991           return false;
992         }
993       
994       bfd_ardata (abfd)->symdefs = ((carsym *)
995                                     bfd_alloc (abfd, c * sizeof (carsym)));
996       if (bfd_ardata (abfd)->symdefs == NULL)
997         return false;
998       
999       /* After the count comes a list of four byte file offsets.  */
1000       for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4;
1001            i < c;
1002            ++i, ++arsym, p += 4)
1003         arsym->file_offset = bfd_h_get_32 (abfd, p);
1004     }
1005   else
1006     {
1007       /* This is for the new format.  */
1008       struct xcoff_ar_hdr_big hdr;
1009
1010       off = strtol (xcoff_ardata_big (abfd)->symoff, (char **) NULL, 10);
1011       if (off == 0)
1012         {
1013           bfd_has_map (abfd) = false;
1014           return true;
1015         }
1016
1017       if (bfd_seek (abfd, off, SEEK_SET) != 0)
1018         return false;
1019
1020       /* The symbol table starts with a normal archive header.  */
1021       if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR_BIG, 1, abfd)
1022           != SIZEOF_AR_HDR_BIG)
1023         return false;
1024
1025       /* Skip the name (normally empty).  */
1026       namlen = strtol (hdr.namlen, (char **) NULL, 10);
1027       if (bfd_seek (abfd, ((namlen + 1) & ~1) + SXCOFFARFMAG, SEEK_CUR) != 0)
1028         return false;
1029
1030       /* XXX This actually has to be a call to strtoll (at least on 32-bit
1031          machines) since the field width is 20 and there numbers with more
1032          than 32 bits can be represented.  */
1033       sz = strtol (hdr.size, (char **) NULL, 10);
1034
1035       /* Read in the entire symbol table.  */
1036       contents = (bfd_byte *) bfd_alloc (abfd, sz);
1037       if (contents == NULL)
1038         return false;
1039       if (bfd_read ((PTR) contents, 1, sz, abfd) != sz)
1040         return false;
1041
1042       /* The symbol table starts with an eight byte count.  */
1043       c = bfd_h_get_64 (abfd, contents);
1044
1045       if (c * 8 >= sz)
1046         {
1047           bfd_set_error (bfd_error_bad_value);
1048           return false;
1049         }
1050       
1051       bfd_ardata (abfd)->symdefs = ((carsym *)
1052                                     bfd_alloc (abfd, c * sizeof (carsym)));
1053       if (bfd_ardata (abfd)->symdefs == NULL)
1054         return false;
1055       
1056       /* After the count comes a list of eight byte file offsets.  */
1057       for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 8;
1058            i < c;
1059            ++i, ++arsym, p += 8)
1060         arsym->file_offset = bfd_h_get_64 (abfd, p);
1061     }
1062
1063   /* After the file offsets come null terminated symbol names.  */
1064   cend = contents + sz;
1065   for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
1066        i < c;
1067        ++i, ++arsym, p += strlen ((char *) p) + 1)
1068     {
1069       if (p >= cend)
1070         {
1071           bfd_set_error (bfd_error_bad_value);
1072           return false;
1073         }
1074       arsym->name = (char *) p;
1075     }
1076
1077   bfd_ardata (abfd)->symdef_count = c;
1078   bfd_has_map (abfd) = true;
1079
1080   return true;
1081 }
1082
1083 /* See if this is an XCOFF archive.  */
1084
1085 const bfd_target *
1086 _bfd_xcoff_archive_p (abfd)
1087      bfd *abfd;
1088 {
1089   char magic[SXCOFFARMAG];
1090
1091   if (bfd_read ((PTR) magic, SXCOFFARMAG, 1, abfd) != SXCOFFARMAG)
1092     {
1093       if (bfd_get_error () != bfd_error_system_call)
1094         bfd_set_error (bfd_error_wrong_format);
1095       return NULL;
1096     }
1097
1098   if (strncmp (magic, XCOFFARMAG, SXCOFFARMAG) != 0
1099       && strncmp (magic, XCOFFARMAGBIG, SXCOFFARMAG) != 0)
1100     {
1101       bfd_set_error (bfd_error_wrong_format);
1102       return NULL;
1103     }
1104
1105   /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
1106      involves a cast, we can't do it as the left operand of
1107      assignment.  */
1108   abfd->tdata.aout_ar_data =
1109     (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
1110
1111   if (bfd_ardata (abfd) == (struct artdata *) NULL)
1112     return NULL;
1113
1114   bfd_ardata (abfd)->cache = NULL;
1115   bfd_ardata (abfd)->archive_head = NULL;
1116   bfd_ardata (abfd)->symdefs = NULL;
1117   bfd_ardata (abfd)->extended_names = NULL;
1118
1119   /* Now handle the two formats.  */
1120   if (magic[1] != 'b')
1121     {
1122       /* This is the old format.  */
1123       struct xcoff_ar_file_hdr hdr;
1124
1125       /* Copy over the magic string.  */
1126       memcpy (hdr.magic, magic, SXCOFFARMAG);
1127
1128       /* Now read the rest of the file header.  */
1129       if (bfd_read ((PTR) &hdr.memoff, SIZEOF_AR_FILE_HDR - SXCOFFARMAG, 1,
1130                     abfd) != SIZEOF_AR_FILE_HDR - SXCOFFARMAG)
1131         {
1132           if (bfd_get_error () != bfd_error_system_call)
1133             bfd_set_error (bfd_error_wrong_format);
1134           return NULL;
1135         }
1136
1137       bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff,
1138                                                       (char **) NULL, 10);
1139
1140       bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, SIZEOF_AR_FILE_HDR);
1141       if (bfd_ardata (abfd)->tdata == NULL)
1142         return NULL;
1143
1144       memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR);
1145     }
1146   else
1147     {
1148       /* This is the new format.  */
1149       struct xcoff_ar_file_hdr_big hdr;
1150
1151       /* Copy over the magic string.  */
1152       memcpy (hdr.magic, magic, SXCOFFARMAG);
1153
1154       /* Now read the rest of the file header.  */
1155       if (bfd_read ((PTR) &hdr.memoff, SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG, 1,
1156                     abfd) != SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG)
1157         {
1158           if (bfd_get_error () != bfd_error_system_call)
1159             bfd_set_error (bfd_error_wrong_format);
1160           return NULL;
1161         }
1162
1163       /* XXX This actually has to be a call to strtoll (at least on 32-bit
1164          machines) since the field width is 20 and there numbers with more
1165          than 32 bits can be represented.  */
1166       bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff,
1167                                                       (char **) NULL, 10);
1168
1169       bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, SIZEOF_AR_FILE_HDR_BIG);
1170       if (bfd_ardata (abfd)->tdata == NULL)
1171         return NULL;
1172
1173       memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR_BIG);
1174     }
1175
1176   if (! _bfd_xcoff_slurp_armap (abfd))
1177     {
1178       bfd_release (abfd, bfd_ardata (abfd));
1179       abfd->tdata.aout_ar_data = (struct artdata *) NULL;
1180       return NULL;
1181     }
1182
1183   return abfd->xvec;
1184 }
1185
1186 /* Read the archive header in an XCOFF archive.  */
1187
1188 PTR
1189 _bfd_xcoff_read_ar_hdr (abfd)
1190      bfd *abfd;
1191 {
1192   size_t namlen;
1193   struct areltdata *ret;
1194
1195   ret = (struct areltdata *) bfd_alloc (abfd, sizeof (struct areltdata));
1196   if (ret == NULL)
1197     return NULL;
1198
1199   if (! xcoff_big_format_p (abfd))
1200     {
1201       struct xcoff_ar_hdr hdr;
1202       struct xcoff_ar_hdr *hdrp;
1203
1204       if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR)
1205         {
1206           free (ret);
1207           return NULL;
1208         }
1209
1210       namlen = strtol (hdr.namlen, (char **) NULL, 10);
1211       hdrp = (struct xcoff_ar_hdr *) bfd_alloc (abfd,
1212                                                 SIZEOF_AR_HDR + namlen + 1);
1213       if (hdrp == NULL)
1214         {
1215           free (ret);
1216           return NULL;
1217         }
1218       memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
1219       if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR, 1, namlen, abfd) != namlen)
1220         {
1221           free (ret);
1222           return NULL;
1223         }
1224       ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
1225
1226       ret->arch_header = (char *) hdrp;
1227       ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
1228       ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
1229     }
1230   else
1231     {
1232       struct xcoff_ar_hdr_big hdr;
1233       struct xcoff_ar_hdr_big *hdrp;
1234
1235       if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR_BIG, 1, abfd)
1236           != SIZEOF_AR_HDR_BIG)
1237         {
1238           free (ret);
1239           return NULL;
1240         }
1241
1242       namlen = strtol (hdr.namlen, (char **) NULL, 10);
1243       hdrp = (struct xcoff_ar_hdr_big *) bfd_alloc (abfd,
1244                                                     SIZEOF_AR_HDR_BIG
1245                                                     + namlen + 1);
1246       if (hdrp == NULL)
1247         {
1248           free (ret);
1249           return NULL;
1250         }
1251       memcpy (hdrp, &hdr, SIZEOF_AR_HDR_BIG);
1252       if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR_BIG, 1, namlen, abfd) != namlen)
1253         {
1254           free (ret);
1255           return NULL;
1256         }
1257       ((char *) hdrp)[SIZEOF_AR_HDR_BIG + namlen] = '\0';
1258
1259       ret->arch_header = (char *) hdrp;
1260       /* XXX This actually has to be a call to strtoll (at least on 32-bit
1261          machines) since the field width is 20 and there numbers with more
1262          than 32 bits can be represented.  */
1263       ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
1264       ret->filename = (char *) hdrp + SIZEOF_AR_HDR_BIG;
1265     }
1266
1267   /* Skip over the XCOFFARFMAG at the end of the file name.  */
1268   if (bfd_seek (abfd, (namlen & 1) + SXCOFFARFMAG, SEEK_CUR) != 0)
1269     return NULL;
1270
1271   return (PTR) ret;
1272 }
1273
1274 /* Open the next element in an XCOFF archive.  */
1275
1276 bfd *
1277 _bfd_xcoff_openr_next_archived_file (archive, last_file)
1278      bfd *archive;
1279      bfd *last_file;
1280 {
1281   file_ptr filestart;
1282
1283   if (xcoff_ardata (archive) == NULL)
1284     {
1285       bfd_set_error (bfd_error_invalid_operation);
1286       return NULL;
1287     }
1288
1289   if (! xcoff_big_format_p (archive))
1290     {
1291       if (last_file == NULL)
1292         filestart = bfd_ardata (archive)->first_file_filepos;
1293       else
1294         filestart = strtol (arch_xhdr (last_file)->nextoff, (char **) NULL,
1295                             10);
1296
1297       if (filestart == 0
1298           || filestart == strtol (xcoff_ardata (archive)->memoff,
1299                                   (char **) NULL, 10)
1300           || filestart == strtol (xcoff_ardata (archive)->symoff,
1301                                   (char **) NULL, 10))
1302         {
1303           bfd_set_error (bfd_error_no_more_archived_files);
1304           return NULL;
1305         }
1306     }
1307   else
1308     {
1309       if (last_file == NULL)
1310         filestart = bfd_ardata (archive)->first_file_filepos;
1311       else
1312         /* XXX These actually have to be a calls to strtoll (at least
1313            on 32-bit machines) since the fields's width is 20 and
1314            there numbers with more than 32 bits can be represented.  */
1315         filestart = strtol (arch_xhdr_big (last_file)->nextoff, (char **) NULL,
1316                             10);
1317
1318       /* XXX These actually have to be calls to strtoll (at least on 32-bit
1319          machines) since the fields's width is 20 and there numbers with more
1320          than 32 bits can be represented.  */
1321       if (filestart == 0
1322           || filestart == strtol (xcoff_ardata_big (archive)->memoff,
1323                                   (char **) NULL, 10)
1324           || filestart == strtol (xcoff_ardata_big (archive)->symoff,
1325                                   (char **) NULL, 10))
1326         {
1327           bfd_set_error (bfd_error_no_more_archived_files);
1328           return NULL;
1329         }
1330     }
1331
1332   return _bfd_get_elt_at_filepos (archive, filestart);
1333 }
1334
1335 /* Stat an element in an XCOFF archive.  */
1336
1337 int
1338 _bfd_xcoff_generic_stat_arch_elt (abfd, s)
1339      bfd *abfd;
1340      struct stat *s;
1341 {
1342   if (abfd->arelt_data == NULL)
1343     {
1344       bfd_set_error (bfd_error_invalid_operation);
1345       return -1;
1346     }
1347
1348   if (! xcoff_big_format_p (abfd))
1349     {
1350       struct xcoff_ar_hdr *hdrp = arch_xhdr (abfd);
1351
1352       s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
1353       s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
1354       s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
1355       s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
1356       s->st_size = arch_eltdata (abfd)->parsed_size;
1357     }
1358   else
1359     {
1360       struct xcoff_ar_hdr_big *hdrp = arch_xhdr_big (abfd);
1361
1362       s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
1363       s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
1364       s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
1365       s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
1366       s->st_size = arch_eltdata (abfd)->parsed_size;
1367     }
1368
1369   return 0;
1370 }
1371
1372 /* Normalize a file name for inclusion in an archive.  */
1373
1374 static const char *
1375 normalize_filename (abfd)
1376      bfd *abfd;
1377 {
1378   const char *file;
1379   const char *filename;
1380
1381   file = bfd_get_filename (abfd);
1382   filename = strrchr (file, '/');
1383   if (filename != NULL)
1384     filename++;
1385   else
1386     filename = file;
1387   return filename;
1388 }
1389
1390 /* Write out an XCOFF armap.  */
1391
1392 /*ARGSUSED*/
1393 static boolean
1394 xcoff_write_armap_old (abfd, elength, map, orl_count, stridx)
1395      bfd *abfd;
1396      unsigned int elength ATTRIBUTE_UNUSED;
1397      struct orl *map;
1398      unsigned int orl_count;
1399      int stridx;
1400 {
1401   struct xcoff_ar_hdr hdr;
1402   char *p;
1403   unsigned char buf[4];
1404   bfd *sub;
1405   file_ptr fileoff;
1406   unsigned int i;
1407
1408   memset (&hdr, 0, sizeof hdr);
1409   sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
1410   sprintf (hdr.nextoff, "%d", 0);
1411   memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, 12);
1412   sprintf (hdr.date, "%d", 0);
1413   sprintf (hdr.uid, "%d", 0);
1414   sprintf (hdr.gid, "%d", 0);
1415   sprintf (hdr.mode, "%d", 0);
1416   sprintf (hdr.namlen, "%d", 0);
1417
1418   /* We need spaces, not null bytes, in the header.  */
1419   for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
1420     if (*p == '\0')
1421       *p = ' ';
1422
1423   if (bfd_write ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR
1424       || bfd_write (XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) != SXCOFFARFMAG)
1425     return false;
1426
1427   bfd_h_put_32 (abfd, orl_count, buf);
1428   if (bfd_write (buf, 1, 4, abfd) != 4)
1429     return false;
1430
1431   sub = abfd->archive_head;
1432   fileoff = SIZEOF_AR_FILE_HDR;
1433   i = 0;
1434   while (sub != NULL && i < orl_count)
1435     {
1436       size_t namlen;
1437
1438       while (((bfd *) (map[i]).pos) == sub)
1439         {
1440           bfd_h_put_32 (abfd, fileoff, buf);
1441           if (bfd_write (buf, 1, 4, abfd) != 4)
1442             return false;
1443           ++i;
1444         }
1445       namlen = strlen (normalize_filename (sub));
1446       namlen = (namlen + 1) &~ 1;
1447       fileoff += (SIZEOF_AR_HDR
1448                   + namlen
1449                   + SXCOFFARFMAG
1450                   + arelt_size (sub));
1451       fileoff = (fileoff + 1) &~ 1;
1452       sub = sub->next;
1453     }
1454
1455   for (i = 0; i < orl_count; i++)
1456     {
1457       const char *name;
1458       size_t namlen;
1459
1460       name = *map[i].name;
1461       namlen = strlen (name);
1462       if (bfd_write (name, 1, namlen + 1, abfd) != namlen + 1)
1463         return false;
1464     }
1465
1466   if ((stridx & 1) != 0)
1467     {
1468       char b;
1469
1470       b = '\0';
1471       if (bfd_write (&b, 1, 1, abfd) != 1)
1472         return false;
1473     }
1474
1475   return true;
1476 }
1477
1478 /* Write a single armap in the big format.  */
1479 static boolean
1480 xcoff_write_one_armap_big (abfd, map, orl_count, orl_ccount, stridx, bits64,
1481                            prevoff, nextoff)
1482      bfd *abfd;
1483      struct orl *map;
1484      unsigned int orl_count;
1485      unsigned int orl_ccount;
1486      unsigned int stridx;
1487      int bits64;
1488      const char *prevoff;
1489      char *nextoff;
1490 {
1491   struct xcoff_ar_hdr_big hdr;
1492   char *p;
1493   unsigned char buf[4];
1494   bfd *sub;
1495   file_ptr fileoff;
1496   const bfd_arch_info_type *arch_info;
1497   bfd *object_bfd;
1498   unsigned int i;
1499
1500   memset (&hdr, 0, sizeof hdr);
1501   /* XXX This call actually should use %lld (at least on 32-bit
1502      machines) since the fields's width is 20 and there numbers with
1503      more than 32 bits can be represented.  */
1504   sprintf (hdr.size, "%ld", (long) (4 + orl_ccount * 4 + stridx));
1505   if (bits64) {
1506     sprintf (hdr.nextoff, "%d", 0);
1507   } else {
1508           /* do explict cast to long to remove compiler warning */
1509           sprintf (hdr.nextoff, "%ld", (strtol (prevoff, (char **) NULL, 10)
1510                                                                    + (long) (4 + orl_ccount * 4 + stridx)));
1511   }
1512   
1513   memcpy (hdr.prevoff, prevoff, sizeof (hdr.prevoff));
1514   sprintf (hdr.date, "%d", 0);
1515   sprintf (hdr.uid, "%d", 0);
1516   sprintf (hdr.gid, "%d", 0);
1517   sprintf (hdr.mode, "%d", 0);
1518   sprintf (hdr.namlen, "%d", 0);
1519
1520   /* We need spaces, not null bytes, in the header.  */
1521   for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR_BIG; p++)
1522     if (*p == '\0')
1523       *p = ' ';
1524
1525   memcpy (nextoff, hdr.nextoff, sizeof (hdr.nextoff));
1526
1527   if (bfd_write ((PTR) &hdr, SIZEOF_AR_HDR_BIG, 1, abfd) != SIZEOF_AR_HDR_BIG
1528       || bfd_write (XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) != SXCOFFARFMAG)
1529     return false;
1530
1531   bfd_h_put_32 (abfd, orl_ccount, buf);
1532   if (bfd_write (buf, 1, 4, abfd) != 4)
1533     return false;
1534
1535   sub = abfd->archive_head;
1536   fileoff = SIZEOF_AR_FILE_HDR_BIG;
1537   i = 0;
1538   while (sub != NULL && i < orl_count)
1539     {
1540       size_t namlen;
1541
1542       if ((bfd_arch_bits_per_address ((bfd *) map[i].pos) == 64) == bits64)
1543         while (((bfd *) (map[i]).pos) == sub)
1544           {
1545             bfd_h_put_32 (abfd, fileoff, buf);
1546             if (bfd_write (buf, 1, 4, abfd) != 4)
1547               return false;
1548             i++;
1549           }
1550       else
1551         while (((bfd *) (map[i]).pos) == sub)
1552           i++;
1553
1554       namlen = strlen (normalize_filename (sub));
1555       namlen = (namlen + 1) &~ 1;
1556       fileoff += (SIZEOF_AR_HDR_BIG
1557                   + namlen
1558                   + SXCOFFARFMAG
1559                   + arelt_size (sub));
1560       fileoff = (fileoff + 1) &~ 1;
1561       sub = sub->next;
1562     }
1563
1564   object_bfd = NULL;
1565   for (i = 0; i < orl_count; i++)
1566     {
1567       const char *name;
1568       size_t namlen;
1569       bfd *ob = (bfd *)map[i].pos;
1570
1571       if (ob != object_bfd)
1572         arch_info = bfd_get_arch_info (ob);
1573       if ((arch_info->bits_per_address == 64) != bits64)
1574         continue;
1575
1576       name = *map[i].name;
1577       namlen = strlen (name);
1578       if (bfd_write (name, 1, namlen + 1, abfd) != namlen + 1)
1579         return false;
1580     }
1581
1582   if ((stridx & 1) != 0)
1583     {
1584       char b;
1585
1586       b = '\0';
1587       if (bfd_write (&b, 1, 1, abfd) != 1)
1588         return false;
1589     }
1590
1591   return true;
1592 }
1593
1594 /*ARGSUSED*/
1595 static boolean
1596 xcoff_write_armap_big (abfd, elength, map, orl_count, stridx)
1597      bfd *abfd;
1598      unsigned int elength ATTRIBUTE_UNUSED;
1599      struct orl *map;
1600      unsigned int orl_count;
1601      int stridx;
1602 {
1603   unsigned int i;
1604   unsigned int orl_count_32, orl_count_64;
1605   unsigned int stridx_32, stridx_64;
1606   const bfd_arch_info_type *arch_info;
1607   bfd *object_bfd;
1608
1609   /* First, we look through the symbols and work out which are
1610      from 32-bit objects and which from 64-bit ones.  */
1611   orl_count_32 = 0;
1612   orl_count_64 = 0;
1613   stridx_32 = 0;
1614   stridx_64 = 0;
1615   object_bfd = NULL;
1616   for (i = 0; i < orl_count; i++)
1617     {
1618       bfd *ob = (bfd *)map[i].pos;
1619       unsigned int len;
1620       if (ob != object_bfd)
1621         arch_info = bfd_get_arch_info (ob);
1622       len = strlen (*map[i].name) + 1;
1623       if (arch_info->bits_per_address == 64)
1624         {
1625           orl_count_64++;
1626           stridx_64 += len;
1627         }
1628       else
1629         {
1630           orl_count_32++;
1631           stridx_32 += len;
1632         }
1633       object_bfd = ob;
1634     }
1635   /* A quick sanity check... */
1636   BFD_ASSERT (orl_count_64 + orl_count_32 == orl_count);
1637   /* explicit cast to int for compiler */
1638   BFD_ASSERT ((int)(stridx_64 + stridx_32) == stridx);
1639
1640   /* Now write out each map.  */
1641   if (! xcoff_write_one_armap_big (abfd, map, orl_count, orl_count_32,
1642                                    stridx_32, false, 
1643                                    xcoff_ardata_big (abfd)->memoff,
1644                                    xcoff_ardata_big (abfd)->symoff))
1645     return false;
1646   if (! xcoff_write_one_armap_big (abfd, map, orl_count, orl_count_64,
1647                                    stridx_64, true,
1648                                    xcoff_ardata_big (abfd)->symoff,
1649                                    xcoff_ardata_big (abfd)->symoff64))
1650     return false;
1651     
1652   return true;
1653 }
1654
1655 /*ARGSUSED*/
1656 boolean
1657 _bfd_xcoff_write_armap (abfd, elength, map, orl_count, stridx)
1658      bfd *abfd;
1659      unsigned int elength ATTRIBUTE_UNUSED;
1660      struct orl *map;
1661      unsigned int orl_count;
1662      int stridx;
1663 {
1664   if (! xcoff_big_format_p (abfd))
1665     return xcoff_write_armap_old (abfd, elength, map, orl_count, stridx);
1666   else
1667     return xcoff_write_armap_big (abfd, elength, map, orl_count, stridx);
1668 }
1669
1670 /* Write out an XCOFF archive.  We always write an entire archive,
1671    rather than fussing with the freelist and so forth.  */
1672
1673 static boolean
1674 xcoff_write_archive_contents_old (abfd)
1675      bfd *abfd;
1676 {
1677   struct xcoff_ar_file_hdr fhdr;
1678   size_t count;
1679   size_t total_namlen;
1680   file_ptr *offsets;
1681   boolean makemap;
1682   boolean hasobjects;
1683   file_ptr prevoff, nextoff;
1684   bfd *sub;
1685   unsigned int i;
1686   struct xcoff_ar_hdr ahdr;
1687   bfd_size_type size;
1688   char *p;
1689   char decbuf[13];
1690
1691   memset (&fhdr, 0, sizeof fhdr);
1692   strncpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);
1693   sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR);
1694   sprintf (fhdr.freeoff, "%d", 0);
1695
1696   count = 0;
1697   total_namlen = 0;
1698   for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
1699     {
1700       ++count;
1701       total_namlen += strlen (normalize_filename (sub)) + 1;
1702     }
1703   offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
1704   if (offsets == NULL)
1705     return false;
1706
1707   if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
1708     return false;
1709
1710   makemap = bfd_has_map (abfd);
1711   hasobjects = false;
1712   prevoff = 0;
1713   nextoff = SIZEOF_AR_FILE_HDR;
1714   for (sub = abfd->archive_head, i = 0; sub != NULL; sub = sub->next, i++)
1715     {
1716       const char *name;
1717       size_t namlen;
1718       struct xcoff_ar_hdr *ahdrp;
1719       bfd_size_type remaining;
1720
1721       if (makemap && ! hasobjects)
1722         {
1723           if (bfd_check_format (sub, bfd_object))
1724             hasobjects = true;
1725         }
1726
1727       name = normalize_filename (sub);
1728       namlen = strlen (name);
1729
1730       if (sub->arelt_data != NULL)
1731         ahdrp = arch_xhdr (sub);
1732       else
1733         ahdrp = NULL;
1734
1735       if (ahdrp == NULL)
1736         {
1737           struct stat s;
1738
1739           memset (&ahdr, 0, sizeof ahdr);
1740           ahdrp = &ahdr;
1741           if (stat (bfd_get_filename (sub), &s) != 0)
1742             {
1743               bfd_set_error (bfd_error_system_call);
1744               return false;
1745             }
1746
1747           sprintf (ahdrp->size, "%ld", (long) s.st_size);
1748           sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
1749           sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
1750           sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
1751           sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
1752
1753           if (sub->arelt_data == NULL)
1754             {
1755               sub->arelt_data = bfd_alloc (sub, sizeof (struct areltdata));
1756               if (sub->arelt_data == NULL)
1757                 return false;
1758             }
1759
1760           arch_eltdata (sub)->parsed_size = s.st_size;
1761         }
1762
1763       sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
1764       sprintf (ahdrp->namlen, "%ld", (long) namlen);
1765
1766       /* If the length of the name is odd, we write out the null byte
1767          after the name as well.  */
1768       namlen = (namlen + 1) &~ 1;
1769
1770       remaining = arelt_size (sub);
1771       size = (SIZEOF_AR_HDR
1772               + namlen
1773               + SXCOFFARFMAG
1774               + remaining);
1775
1776       BFD_ASSERT (nextoff == bfd_tell (abfd));
1777
1778       offsets[i] = nextoff;
1779
1780       prevoff = nextoff;
1781       nextoff += size + (size & 1);
1782
1783       sprintf (ahdrp->nextoff, "%ld", (long) nextoff);
1784
1785       /* We need spaces, not null bytes, in the header.  */
1786       for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
1787         if (*p == '\0')
1788           *p = ' ';
1789
1790       if (bfd_write ((PTR) ahdrp, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
1791           || bfd_write ((PTR) name, 1, namlen, abfd) != namlen
1792           || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
1793               != SXCOFFARFMAG))
1794         return false;
1795
1796       if (bfd_seek (sub, (file_ptr) 0, SEEK_SET) != 0)
1797         return false;
1798       while (remaining != 0)
1799         {
1800           bfd_size_type amt;
1801           bfd_byte buffer[DEFAULT_BUFFERSIZE];
1802
1803           amt = sizeof buffer;
1804           if (amt > remaining)
1805             amt = remaining;
1806           if (bfd_read (buffer, 1, amt, sub) != amt
1807               || bfd_write (buffer, 1, amt, abfd) != amt)
1808             return false;
1809           remaining -= amt;
1810         }
1811
1812       if ((size & 1) != 0)
1813         {
1814           bfd_byte b;
1815
1816           b = '\0';
1817           if (bfd_write (&b, 1, 1, abfd) != 1)
1818             return false;
1819         }
1820     }
1821
1822   sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
1823
1824   /* Write out the member table.  */
1825
1826   BFD_ASSERT (nextoff == bfd_tell (abfd));
1827   sprintf (fhdr.memoff, "%ld", (long) nextoff);
1828
1829   memset (&ahdr, 0, sizeof ahdr);
1830   sprintf (ahdr.size, "%ld", (long) (12 + count * 12 + total_namlen));
1831   sprintf (ahdr.prevoff, "%ld", (long) prevoff);
1832   sprintf (ahdr.date, "%d", 0);
1833   sprintf (ahdr.uid, "%d", 0);
1834   sprintf (ahdr.gid, "%d", 0);
1835   sprintf (ahdr.mode, "%d", 0);
1836   sprintf (ahdr.namlen, "%d", 0);
1837
1838   size = (SIZEOF_AR_HDR
1839           + 12
1840           + count * 12
1841           + total_namlen
1842           + SXCOFFARFMAG);
1843
1844   prevoff = nextoff;
1845   nextoff += size + (size & 1);
1846
1847   if (makemap && hasobjects)
1848     sprintf (ahdr.nextoff, "%ld", (long) nextoff);
1849   else
1850     sprintf (ahdr.nextoff, "%d", 0);
1851
1852   /* We need spaces, not null bytes, in the header.  */
1853   for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
1854     if (*p == '\0')
1855       *p = ' ';
1856
1857   if (bfd_write ((PTR) &ahdr, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
1858       || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
1859           != SXCOFFARFMAG))
1860     return false;
1861
1862   sprintf (decbuf, "%-12ld", (long) count);
1863   if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
1864     return false;
1865   for (i = 0; i < count; i++)
1866     {
1867       sprintf (decbuf, "%-12ld", (long) offsets[i]);
1868       if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
1869         return false;
1870     }
1871   for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
1872     {
1873       const char *name;
1874       size_t namlen;
1875
1876       name = normalize_filename (sub);
1877       namlen = strlen (name);
1878       if (bfd_write ((PTR) name, 1, namlen + 1, abfd) != namlen + 1)
1879         return false;
1880     }
1881   if ((size & 1) != 0)
1882     {
1883       bfd_byte b;
1884
1885       b = '\0';
1886       if (bfd_write ((PTR) &b, 1, 1, abfd) != 1)
1887         return false;
1888     }
1889
1890   /* Write out the armap, if appropriate.  */
1891
1892   if (! makemap || ! hasobjects)
1893     sprintf (fhdr.symoff, "%d", 0);
1894   else
1895     {
1896       BFD_ASSERT (nextoff == bfd_tell (abfd));
1897       sprintf (fhdr.symoff, "%ld", (long) nextoff);
1898       bfd_ardata (abfd)->tdata = (PTR) &fhdr;
1899       if (! _bfd_compute_and_write_armap (abfd, 0))
1900         return false;
1901     }
1902
1903   /* Write out the archive file header.  */
1904
1905   /* We need spaces, not null bytes, in the header.  */
1906   for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR; p++)
1907     if (*p == '\0')
1908       *p = ' ';
1909
1910   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
1911       || (bfd_write ((PTR) &fhdr, SIZEOF_AR_FILE_HDR, 1, abfd) !=
1912           SIZEOF_AR_FILE_HDR))
1913     return false;
1914
1915   return true;
1916 }
1917
1918 static boolean
1919 xcoff_write_archive_contents_big (abfd)
1920      bfd *abfd;
1921 {
1922   struct xcoff_ar_file_hdr_big fhdr;
1923   size_t count;
1924   size_t total_namlen;
1925   file_ptr *offsets;
1926   boolean makemap;
1927   boolean hasobjects;
1928   file_ptr prevoff, nextoff;
1929   bfd *sub;
1930   unsigned int i;
1931   struct xcoff_ar_hdr_big ahdr;
1932   bfd_size_type size;
1933   char *p;
1934   char decbuf[13];
1935
1936   memset (&fhdr, 0, sizeof fhdr);
1937   strncpy (fhdr.magic, XCOFFARMAGBIG, SXCOFFARMAG);
1938   sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR_BIG);
1939   sprintf (fhdr.freeoff, "%d", 0);
1940
1941   count = 0;
1942   total_namlen = 0;
1943   for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
1944     {
1945       ++count;
1946       total_namlen += strlen (normalize_filename (sub)) + 1;
1947     }
1948   offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
1949   if (offsets == NULL)
1950     return false;
1951
1952   if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR_BIG, SEEK_SET) != 0)
1953     return false;
1954
1955   makemap = bfd_has_map (abfd);
1956   hasobjects = false;
1957   prevoff = 0;
1958   nextoff = SIZEOF_AR_FILE_HDR_BIG;
1959   for (sub = abfd->archive_head, i = 0; sub != NULL; sub = sub->next, i++)
1960     {
1961       const char *name;
1962       size_t namlen;
1963       struct xcoff_ar_hdr_big *ahdrp;
1964       bfd_size_type remaining;
1965
1966       if (makemap && ! hasobjects)
1967         {
1968           if (bfd_check_format (sub, bfd_object))
1969             hasobjects = true;
1970         }
1971
1972       name = normalize_filename (sub);
1973       namlen = strlen (name);
1974
1975       if (sub->arelt_data != NULL)
1976         ahdrp = arch_xhdr_big (sub);
1977       else
1978         ahdrp = NULL;
1979
1980       if (ahdrp == NULL)
1981         {
1982           struct stat s;
1983
1984           memset (&ahdr, 0, sizeof ahdr);
1985           ahdrp = &ahdr;
1986           /* XXX This should actually be a call to stat64 (at least on
1987              32-bit machines).  */
1988           if (stat (bfd_get_filename (sub), &s) != 0)
1989             {
1990               bfd_set_error (bfd_error_system_call);
1991               return false;
1992             }
1993
1994           /* XXX This call actually should use %lld (at least on 32-bit
1995              machines) since the fields's width is 20 and there numbers with
1996              more than 32 bits can be represented.  */
1997           sprintf (ahdrp->size, "%ld", (long) s.st_size);
1998           sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
1999           sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
2000           sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
2001           sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
2002
2003           if (sub->arelt_data == NULL)
2004             {
2005               sub->arelt_data = bfd_alloc (sub, sizeof (struct areltdata));
2006               if (sub->arelt_data == NULL)
2007                 return false;
2008             }
2009
2010           arch_eltdata (sub)->parsed_size = s.st_size;
2011         }
2012
2013       /* XXX These calls actually should use %lld (at least on 32-bit
2014          machines) since the fields's width is 20 and there numbers with
2015          more than 32 bits can be represented.  */
2016       sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
2017       sprintf (ahdrp->namlen, "%ld", (long) namlen);
2018
2019       /* If the length of the name is odd, we write out the null byte
2020          after the name as well.  */
2021       namlen = (namlen + 1) &~ 1;
2022
2023       remaining = arelt_size (sub);
2024       size = (SIZEOF_AR_HDR_BIG
2025               + namlen
2026               + SXCOFFARFMAG
2027               + remaining);
2028
2029       BFD_ASSERT (nextoff == bfd_tell (abfd));
2030
2031       offsets[i] = nextoff;
2032
2033       prevoff = nextoff;
2034       nextoff += size + (size & 1);
2035
2036       sprintf (ahdrp->nextoff, "%ld", (long) nextoff);
2037
2038       /* We need spaces, not null bytes, in the header.  */
2039       for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR_BIG; p++)
2040         if (*p == '\0')
2041           *p = ' ';
2042
2043       if (bfd_write ((PTR) ahdrp, 1, SIZEOF_AR_HDR_BIG, abfd)
2044           != SIZEOF_AR_HDR_BIG
2045           || bfd_write ((PTR) name, 1, namlen, abfd) != namlen
2046           || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
2047               != SXCOFFARFMAG))
2048         return false;
2049
2050       if (bfd_seek (sub, (file_ptr) 0, SEEK_SET) != 0)
2051         return false;
2052       while (remaining != 0)
2053         {
2054           bfd_size_type amt;
2055           bfd_byte buffer[DEFAULT_BUFFERSIZE];
2056
2057           amt = sizeof buffer;
2058           if (amt > remaining)
2059             amt = remaining;
2060           if (bfd_read (buffer, 1, amt, sub) != amt
2061               || bfd_write (buffer, 1, amt, abfd) != amt)
2062             return false;
2063           remaining -= amt;
2064         }
2065
2066       if ((size & 1) != 0)
2067         {
2068           bfd_byte b;
2069
2070           b = '\0';
2071           if (bfd_write (&b, 1, 1, abfd) != 1)
2072             return false;
2073         }
2074     }
2075
2076   /* XXX This call actually should use %lld (at least on 32-bit
2077      machines) since the fields's width is 20 and there numbers with
2078      more than 32 bits can be represented.  */
2079   sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
2080
2081   /* Write out the member table.  */
2082
2083   BFD_ASSERT (nextoff == bfd_tell (abfd));
2084   /* XXX This call actually should use %lld (at least on 32-bit
2085      machines) since the fields's width is 20 and there numbers with
2086      more than 32 bits can be represented.  */
2087   sprintf (fhdr.memoff, "%ld", (long) nextoff);
2088
2089   memset (&ahdr, 0, sizeof ahdr);
2090   /* XXX The next two calls actually should use %lld (at least on 32-bit
2091      machines) since the fields's width is 20 and there numbers with
2092      more than 32 bits can be represented.  */
2093   sprintf (ahdr.size, "%ld", (long) (12 + count * 12 + total_namlen));
2094   sprintf (ahdr.prevoff, "%ld", (long) prevoff);
2095   sprintf (ahdr.date, "%d", 0);
2096   sprintf (ahdr.uid, "%d", 0);
2097   sprintf (ahdr.gid, "%d", 0);
2098   sprintf (ahdr.mode, "%d", 0);
2099   sprintf (ahdr.namlen, "%d", 0);
2100
2101   size = (SIZEOF_AR_HDR_BIG
2102           + 12
2103           + count * 12
2104           + total_namlen
2105           + SXCOFFARFMAG);
2106
2107   prevoff = nextoff;
2108   nextoff += size + (size & 1);
2109
2110   if (makemap && hasobjects)
2111     /* XXX This call actually should use %lld (at least on 32-bit
2112        machines) since the fields's width is 20 and there numbers with
2113        more than 32 bits can be represented.  */
2114     sprintf (ahdr.nextoff, "%ld", (long) nextoff);
2115   else
2116     sprintf (ahdr.nextoff, "%d", 0);
2117
2118   /* We need spaces, not null bytes, in the header.  */
2119   for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR_BIG; p++)
2120     if (*p == '\0')
2121       *p = ' ';
2122
2123   if (bfd_write ((PTR) &ahdr, 1, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG
2124       || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
2125           != SXCOFFARFMAG))
2126     return false;
2127
2128   sprintf (decbuf, "%-12ld", (long) count);
2129   if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
2130     return false;
2131   for (i = 0; i < count; i++)
2132     {
2133       sprintf (decbuf, "%-12ld", (long) offsets[i]);
2134       if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
2135         return false;
2136     }
2137   for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
2138     {
2139       const char *name;
2140       size_t namlen;
2141
2142       name = normalize_filename (sub);
2143       namlen = strlen (name);
2144       if (bfd_write ((PTR) name, 1, namlen + 1, abfd) != namlen + 1)
2145         return false;
2146     }
2147   if ((size & 1) != 0)
2148     {
2149       bfd_byte b;
2150
2151       b = '\0';
2152       if (bfd_write ((PTR) &b, 1, 1, abfd) != 1)
2153         return false;
2154     }
2155
2156   /* Write out the armap, if appropriate.  */
2157
2158   if (! makemap || ! hasobjects)
2159     sprintf (fhdr.symoff, "%d", 0);
2160   else
2161     {
2162       BFD_ASSERT (nextoff == bfd_tell (abfd));
2163       /* XXX This call actually should use %lld (at least on 32-bit
2164          machines) since the fields's width is 20 and there numbers with
2165          more than 32 bits can be represented.  */
2166       bfd_ardata (abfd)->tdata = (PTR) &fhdr;
2167       if (! _bfd_compute_and_write_armap (abfd, 0))
2168         return false;
2169     }
2170
2171   /* Write out the archive file header.  */
2172
2173   /* We need spaces, not null bytes, in the header.  */
2174   for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR_BIG; p++)
2175     if (*p == '\0')
2176       *p = ' ';
2177
2178   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2179       || (bfd_write ((PTR) &fhdr, SIZEOF_AR_FILE_HDR_BIG, 1, abfd) !=
2180           SIZEOF_AR_FILE_HDR_BIG))
2181     return false;
2182
2183   return true;
2184 }
2185
2186 boolean
2187 _bfd_xcoff_write_archive_contents (abfd)
2188      bfd *abfd;
2189 {
2190   if (! xcoff_big_format_p (abfd))
2191     return xcoff_write_archive_contents_old (abfd);
2192   else
2193     return xcoff_write_archive_contents_big (abfd);
2194 }
2195 \f
2196 /* We can't use the usual coff_sizeof_headers routine, because AIX
2197    always uses an a.out header.  */
2198
2199 /*ARGSUSED*/
2200 int
2201 _bfd_xcoff_sizeof_headers (abfd, reloc)
2202      bfd *abfd;
2203      boolean reloc ATTRIBUTE_UNUSED;
2204 {
2205   int size;
2206
2207   size = FILHSZ;
2208   if (xcoff_data (abfd)->full_aouthdr)
2209     size += AOUTSZ;
2210   else
2211     size += SMALL_AOUTSZ;
2212   size += abfd->section_count * SCNHSZ;
2213   return size;
2214 }
2215 \f
2216 /* Routines to swap information in the XCOFF .loader section.  If we
2217    ever need to write an XCOFF loader, this stuff will need to be
2218    moved to another file shared by the linker (which XCOFF calls the
2219    ``binder'') and the loader.  */
2220
2221 /* Swap in the ldhdr structure.  */
2222
2223 static void
2224 xcoff_swap_ldhdr_in (abfd, src, dst)
2225      bfd *abfd;
2226      const struct external_ldhdr *src;
2227      struct internal_ldhdr *dst;
2228 {
2229   dst->l_version = bfd_get_32 (abfd, src->l_version);
2230   dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
2231   dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
2232   dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
2233   dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
2234   dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
2235   dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
2236   dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
2237 }
2238
2239 /* Swap out the ldhdr structure.  */
2240
2241 static void
2242 xcoff_swap_ldhdr_out (abfd, src, dst)
2243      bfd *abfd;
2244      const struct internal_ldhdr *src;
2245      struct external_ldhdr *dst;
2246 {
2247   bfd_put_32 (abfd, src->l_version, dst->l_version);
2248   bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
2249   bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
2250   bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
2251   bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
2252   bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
2253   bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
2254   bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
2255 }
2256
2257 /* Swap in the ldsym structure.  */
2258
2259 static void
2260 xcoff_swap_ldsym_in (abfd, src, dst)
2261      bfd *abfd;
2262      const struct external_ldsym *src;
2263      struct internal_ldsym *dst;
2264 {
2265   if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0) {
2266     memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2267   } else {
2268     dst->_l._l_l._l_zeroes = 0;
2269     dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
2270   }
2271   dst->l_value = bfd_get_32 (abfd, src->l_value);
2272   dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
2273   dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
2274   dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
2275   dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
2276   dst->l_parm = bfd_get_32 (abfd, src->l_parm);
2277 }
2278
2279 /* Swap out the ldsym structure.  */
2280
2281 static void
2282 xcoff_swap_ldsym_out (abfd, src, dst)
2283      bfd *abfd;
2284      const struct internal_ldsym *src;
2285      struct external_ldsym *dst;
2286 {
2287
2288   if (src->_l._l_l._l_zeroes != 0)
2289     memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2290   else
2291     {
2292       bfd_put_32 (abfd, 0, dst->_l._l_l._l_zeroes);
2293       bfd_put_32 (abfd, src->_l._l_l._l_offset, dst->_l._l_l._l_offset);
2294     }
2295   bfd_put_32 (abfd, src->l_value, dst->l_value);
2296   bfd_put_16 (abfd, src->l_scnum, dst->l_scnum);
2297   bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
2298   bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
2299   bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
2300   bfd_put_32 (abfd, src->l_parm, dst->l_parm);
2301 }
2302
2303 /* Swap in the ldrel structure.  */
2304
2305 static void
2306 xcoff_swap_ldrel_in (abfd, src, dst)
2307      bfd *abfd;
2308      const struct external_ldrel *src;
2309      struct internal_ldrel *dst;
2310 {
2311   dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
2312   dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
2313   dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
2314   dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
2315 }
2316
2317 /* Swap out the ldrel structure.  */
2318
2319 static void
2320 xcoff_swap_ldrel_out (abfd, src, dst)
2321      bfd *abfd;
2322      const struct internal_ldrel *src;
2323      struct external_ldrel *dst;
2324 {
2325   bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
2326   bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
2327   bfd_put_16 (abfd, src->l_rtype, dst->l_rtype);
2328   bfd_put_16 (abfd, src->l_rsecnm, dst->l_rsecnm);
2329 }
2330 \f
2331
2332
2333 /* This is the relocation function for the RS/6000/POWER/PowerPC.
2334    This is currently the only processor which uses XCOFF; I hope that
2335    will never change.  */
2336
2337 boolean
2338 xcoff_ppc_relocate_section (output_bfd, info, input_bfd,
2339                             input_section, contents, relocs, syms,
2340                             sections)
2341      bfd *output_bfd;
2342      struct bfd_link_info *info;
2343      bfd *input_bfd;
2344      asection *input_section;
2345      bfd_byte *contents;
2346      struct internal_reloc *relocs;
2347      struct internal_syment *syms;
2348      asection **sections;
2349 {
2350   struct internal_reloc *rel;
2351   struct internal_reloc *relend;
2352
2353   rel = relocs;
2354   relend = rel + input_section->reloc_count;
2355
2356   for (; rel < relend; rel++)
2357     {
2358       long symndx;
2359       struct xcoff_link_hash_entry *h;
2360       struct internal_syment *sym;
2361       bfd_vma addend;
2362       bfd_vma val;
2363       struct reloc_howto_struct howto;
2364       bfd_reloc_status_type rstat;
2365
2366       /* Relocation type R_REF is a special relocation type which is
2367          merely used to prevent garbage collection from occurring for
2368          the csect including the symbol which it references.  */
2369       if (rel->r_type == R_REF)
2370         continue;
2371
2372       symndx = rel->r_symndx;
2373
2374       if (symndx == -1)
2375         {
2376           h = NULL;
2377           sym = NULL;
2378           addend = 0;
2379         }
2380       else
2381         {    
2382           h = obj_xcoff_sym_hashes (input_bfd)[symndx];
2383           sym = syms + symndx;
2384           addend = - sym->n_value;
2385
2386         }
2387
2388       /* We build the howto information on the fly.  */
2389
2390       howto.type = rel->r_type;
2391       howto.rightshift = 0;
2392       howto.size = 2;
2393       howto.bitsize = (rel->r_size & 0x1f) + 1;
2394       howto.pc_relative = false;
2395       howto.bitpos = 0;
2396       if ((rel->r_size & 0x80) != 0)
2397         howto.complain_on_overflow = complain_overflow_signed;
2398       else
2399         howto.complain_on_overflow = complain_overflow_bitfield;
2400       howto.special_function = NULL;
2401       howto.name = "internal";
2402       howto.partial_inplace = true;
2403       if (howto.bitsize == 32)
2404         howto.src_mask = howto.dst_mask = 0xffffffff;
2405       else
2406         {
2407           howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
2408           if (howto.bitsize == 16)
2409             howto.size = 1;
2410         }
2411       howto.pcrel_offset = false;
2412
2413       val = 0;
2414
2415       if (h == NULL)
2416         {
2417           asection *sec;
2418
2419           if (symndx == -1)
2420             {
2421               sec = bfd_abs_section_ptr;
2422               val = 0;
2423             }
2424           else
2425             {
2426               sec = sections[symndx];
2427               /* Hack to make sure we use the right TOC anchor value
2428                  if this reloc is against the TOC anchor.  */
2429
2430               if (sec->name[3] == '0'
2431                           && strcmp (sec->name, ".tc0") == 0) {
2432                           
2433                           val = xcoff_data (output_bfd)->toc;
2434               } else {
2435                           
2436                           val = (sec->output_section->vma
2437                                          + sec->output_offset
2438                                          + sym->n_value
2439                                          - sec->vma);
2440                   }
2441                   
2442             }
2443         }
2444       else
2445         {
2446           if (h->root.type == bfd_link_hash_defined
2447               || h->root.type == bfd_link_hash_defweak)
2448             {
2449               asection *sec;
2450
2451               sec = h->root.u.def.section;
2452               val = (h->root.u.def.value
2453                      + sec->output_section->vma
2454                      + sec->output_offset);
2455             }
2456           else if (h->root.type == bfd_link_hash_common)
2457             {
2458               asection *sec;
2459
2460               sec = h->root.u.c.p->section;
2461               val = (sec->output_section->vma
2462                      + sec->output_offset);
2463             }
2464           else if ((h->flags & XCOFF_DEF_DYNAMIC) != 0
2465                    || (h->flags & XCOFF_IMPORT) != 0)
2466             {
2467               /* Every symbol in a shared object is defined somewhere.  */
2468               val = 0;
2469             }
2470           else if (! info->relocateable)
2471             {
2472               if (! ((*info->callbacks->undefined_symbol)
2473                      (info, h->root.root.string, input_bfd, input_section,
2474                       rel->r_vaddr - input_section->vma, true)))
2475                 return false;
2476
2477               /* Don't try to process the reloc.  It can't help, and
2478                  it may generate another error.  */
2479               continue;
2480             }
2481         }
2482
2483       /* I took the relocation type definitions from two documents:
2484          the PowerPC AIX Version 4 Application Binary Interface, First
2485          Edition (April 1992), and the PowerOpen ABI, Big-Endian
2486          32-Bit Hardware Implementation (June 30, 1994).  Differences
2487          between the documents are noted below.  */
2488
2489       switch (rel->r_type)
2490         {
2491         case R_RTB:
2492         case R_RRTBI:
2493         case R_RRTBA:
2494           /* These relocs are defined by the PowerPC ABI to be
2495              relative branches which use half of the difference
2496              between the symbol and the program counter.  I can't
2497              quite figure out when this is useful.  These relocs are
2498              not defined by the PowerOpen ABI.  */
2499         default:
2500           (*_bfd_error_handler)
2501             (_("%s: unsupported relocation type 0x%02x"),
2502              bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
2503           bfd_set_error (bfd_error_bad_value);
2504           return false;
2505         case R_POS:
2506           /* Simple positive relocation.  */
2507           break;
2508         case R_NEG:
2509           /* Simple negative relocation.  */
2510           val = - val;
2511           break;
2512         case R_REL:
2513           /* Simple PC relative relocation.  */
2514           howto.pc_relative = true;
2515           break;
2516         case R_TOC:
2517           /* TOC relative relocation.  The value in the instruction in
2518              the input file is the offset from the input file TOC to
2519              the desired location.  We want the offset from the final
2520              TOC to the desired location.  We have:
2521                  isym = iTOC + in
2522                  iinsn = in + o
2523                  osym = oTOC + on
2524                  oinsn = on + o
2525              so we must change insn by on - in.
2526              */
2527         case R_GL:
2528           /* Global linkage relocation.  The value of this relocation
2529              is the address of the entry in the TOC section.  */
2530         case R_TCL:
2531           /* Local object TOC address.  I can't figure out the
2532              difference between this and case R_GL.  */
2533         case R_TRL:
2534           /* TOC relative relocation.  A TOC relative load instruction
2535              which may be changed to a load address instruction.
2536              FIXME: We don't currently implement this optimization.  */
2537         case R_TRLA:
2538           /* TOC relative relocation.  This is a TOC relative load
2539              address instruction which may be changed to a load
2540              instruction.  FIXME: I don't know if this is the correct
2541              implementation.  */
2542           if (h != NULL && h->smclas != XMC_TD)
2543             {
2544               if (h->toc_section == NULL)
2545                 {
2546                   (*_bfd_error_handler)
2547                     (_("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry"),
2548                      bfd_get_filename (input_bfd), rel->r_vaddr,
2549                      h->root.root.string);
2550                   bfd_set_error (bfd_error_bad_value);
2551                   return false;
2552                 }
2553
2554               BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
2555               val = (h->toc_section->output_section->vma
2556                      + h->toc_section->output_offset);
2557             }
2558
2559           val = ((val - xcoff_data (output_bfd)->toc)
2560                  - (sym->n_value - xcoff_data (input_bfd)->toc));
2561           addend = 0;
2562           break;
2563         case R_BA:
2564           /* Absolute branch.  We don't want to mess with the lower
2565              two bits of the instruction.  */
2566         case R_CAI:
2567           /* The PowerPC ABI defines this as an absolute call which
2568              may be modified to become a relative call.  The PowerOpen
2569              ABI does not define this relocation type.  */
2570         case R_RBA:
2571           /* Absolute branch which may be modified to become a
2572              relative branch.  */
2573         case R_RBAC:
2574           /* The PowerPC ABI defines this as an absolute branch to a
2575              fixed address which may be modified to an absolute branch
2576              to a symbol.  The PowerOpen ABI does not define this
2577              relocation type.  */
2578         case R_RBRC:
2579           /* The PowerPC ABI defines this as an absolute branch to a
2580              fixed address which may be modified to a relative branch.
2581              The PowerOpen ABI does not define this relocation type.  */
2582           howto.src_mask &= ~3;
2583           howto.dst_mask = howto.src_mask;
2584           break;
2585         case R_BR:
2586           /* Relative branch.  We don't want to mess with the lower
2587              two bits of the instruction.  */
2588         case R_CREL:
2589           /* The PowerPC ABI defines this as a relative call which may
2590              be modified to become an absolute call.  The PowerOpen
2591              ABI does not define this relocation type.  */
2592         case R_RBR:
2593           /* A relative branch which may be modified to become an
2594              absolute branch.  FIXME: We don't implement this,
2595              although we should for symbols of storage mapping class
2596              XMC_XO.  */
2597           howto.pc_relative = true;
2598           howto.src_mask &= ~3;
2599           howto.dst_mask = howto.src_mask;
2600           break;
2601         case R_RL:
2602           /* The PowerPC AIX ABI describes this as a load which may be
2603              changed to a load address.  The PowerOpen ABI says this
2604              is the same as case R_POS.  */
2605           break;
2606         case R_RLA:
2607           /* The PowerPC AIX ABI describes this as a load address
2608              which may be changed to a load.  The PowerOpen ABI says
2609              this is the same as R_POS.  */
2610           break;
2611         }
2612
2613       /* If we see an R_BR or R_RBR reloc which is jumping to global
2614          linkage code, and it is followed by an appropriate cror nop
2615          instruction, we replace the cror with lwz r2,20(r1).  This
2616          restores the TOC after the glink code.  Contrariwise, if the
2617          call is followed by a lwz r2,20(r1), but the call is not
2618          going to global linkage code, we can replace the load with a
2619          cror.  */
2620       if ((rel->r_type == R_BR || rel->r_type == R_RBR)
2621           && h != NULL
2622           && h->root.type == bfd_link_hash_defined
2623           && (rel->r_vaddr - input_section->vma + 8
2624               <= input_section->_cooked_size))
2625         {
2626           bfd_byte *pnext;
2627           unsigned long next;
2628
2629           pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
2630           next = bfd_get_32 (input_bfd, pnext);
2631
2632           /* The _ptrgl function is magic.  It is used by the AIX
2633              compiler to call a function through a pointer.  */
2634           if (h->smclas == XMC_GL
2635               || strcmp (h->root.root.string, "._ptrgl") == 0)
2636             {
2637               if (next == 0x4def7b82            /* cror 15,15,15 */
2638                   || next == 0x4ffffb82         /* cror 31,31,31 */
2639                   || next == 0x60000000)        /* ori r0,r0,0 */
2640                 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */
2641             }
2642           else
2643             {
2644               if (next == 0x80410014)           /* lwz r1,20(r1) */
2645                 bfd_put_32 (input_bfd, 0x60000000, pnext); /* ori r0,r0,0 */
2646             }
2647         }
2648
2649       /* A PC relative reloc includes the section address.  */
2650       if (howto.pc_relative)
2651         addend += input_section->vma;
2652
2653       rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
2654                                         contents,
2655                                         rel->r_vaddr - input_section->vma,
2656                                         val, addend);
2657
2658       switch (rstat)
2659         {
2660         default:
2661           abort ();
2662         case bfd_reloc_ok:
2663           break;
2664         case bfd_reloc_overflow:
2665           {
2666             const char *name;
2667             char buf[SYMNMLEN + 1];
2668             char howto_name[10];
2669
2670             if (symndx == -1)
2671               name = "*ABS*";
2672             else if (h != NULL)
2673               name = h->root.root.string;
2674             else
2675               {
2676
2677                 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
2678
2679                 if (name == NULL)
2680                   return false;
2681               }
2682             sprintf (howto_name, "0x%02x", rel->r_type);
2683
2684             if (! ((*info->callbacks->reloc_overflow)
2685                    (info, name, howto_name, (bfd_vma) 0, input_bfd,
2686                     input_section, rel->r_vaddr - input_section->vma)))
2687               return false;
2688           }
2689         }
2690     }
2691
2692   return true;
2693 }
2694
2695 static boolean
2696 _bfd_xcoff_put_ldsymbol_name (abfd, ldinfo, ldsym, name)
2697      bfd *abfd ATTRIBUTE_UNUSED;
2698          struct xcoff_loader_info *ldinfo;
2699          struct internal_ldsym *ldsym;
2700          const char *name;
2701 {
2702   size_t len;
2703   len = strlen (name);
2704
2705   if (len <= SYMNMLEN)
2706     strncpy (ldsym->_l._l_name, name, SYMNMLEN);
2707   else
2708     {
2709       if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
2710         {
2711           size_t newalc;
2712           bfd_byte *newstrings;
2713
2714           newalc = ldinfo->string_alc * 2;
2715           if (newalc == 0)
2716             newalc = 32;
2717           while (ldinfo->string_size + len + 3 > newalc)
2718             newalc *= 2;
2719
2720           newstrings = ((bfd_byte *)
2721                         bfd_realloc ((PTR) ldinfo->strings, newalc));
2722           if (newstrings == NULL)
2723             {
2724               ldinfo->failed = true;
2725               return false;
2726             }
2727           ldinfo->string_alc = newalc;
2728           ldinfo->strings = newstrings;
2729         }
2730
2731       bfd_put_16 (ldinfo->output_bfd, len + 1,
2732                                   ldinfo->strings + ldinfo->string_size);
2733       strcpy (ldinfo->strings + ldinfo->string_size + 2, name);
2734       ldsym->_l._l_l._l_zeroes = 0;
2735       ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
2736       ldinfo->string_size += len + 3;
2737     }
2738
2739   return true;
2740 }
2741
2742 static boolean
2743 _bfd_xcoff_put_symbol_name (bfd *abfd, struct bfd_strtab_hash *strtab, 
2744                             struct internal_syment *sym,
2745                             const char *name) {
2746
2747   if (strlen (name) <= SYMNMLEN) {
2748     strncpy (sym->_n._n_name, name, SYMNMLEN);
2749   } else {
2750     boolean hash;
2751     bfd_size_type indx;
2752
2753     hash = true;
2754     if ((abfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
2755       hash = false;
2756     indx = _bfd_stringtab_add (strtab, name, hash, false);
2757     if (indx == (bfd_size_type) -1)
2758       return false;
2759     sym->_n._n_n._n_zeroes = 0;
2760     sym->_n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2761   }
2762   return true;
2763 }
2764
2765 static asection *
2766 xcoff_create_csect_from_smclas (abfd, aux, symbol_name) 
2767      bfd *abfd;
2768      union internal_auxent *aux;
2769      char *symbol_name;
2770 {
2771
2772   asection *return_value = NULL;
2773
2774   /*
2775    * .sv64 = x_smclas == 17
2776    * This is an invalid csect for 32 bit apps.
2777    */
2778   static const char *names[19] = {
2779     ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
2780     ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
2781     ".td", NULL, ".sv3264" 
2782   };
2783
2784   if ((19 >= aux->x_csect.x_smclas) &&
2785       (NULL != names[aux->x_csect.x_smclas])) {
2786
2787     return_value = bfd_make_section_anyway 
2788       (abfd, names[aux->x_csect.x_smclas]);
2789
2790
2791   } else {
2792     (*_bfd_error_handler)
2793       (_("%s: symbol `%s' has unrecognized smclas %d"),
2794        bfd_get_filename (abfd), symbol_name, aux->x_csect.x_smclas);
2795     bfd_set_error (bfd_error_bad_value);
2796   }
2797
2798   return return_value;
2799 }
2800
2801 boolean 
2802 xcoff_is_lineno_count_overflow (abfd, value)
2803     bfd *abfd ATTRIBUTE_UNUSED;
2804         bfd_vma value;
2805 {
2806   if (0xffff <= value) {
2807     return true;
2808   }
2809   return false;
2810 }
2811
2812 boolean 
2813 xcoff_is_reloc_count_overflow (abfd, value)
2814     bfd *abfd ATTRIBUTE_UNUSED;
2815         bfd_vma value;
2816 {
2817   if (0xffff <= value) {
2818     return true;
2819   }
2820   return false;
2821 }
2822
2823 bfd_vma
2824 xcoff_loader_symbol_offset (abfd, ldhdr)
2825     bfd *abfd;
2826         struct internal_ldhdr *ldhdr ATTRIBUTE_UNUSED;
2827 {
2828   return bfd_xcoff_ldhdrsz(abfd);
2829 }
2830
2831 bfd_vma
2832 xcoff_loader_reloc_offset (abfd, ldhdr)
2833     bfd *abfd;
2834         struct internal_ldhdr *ldhdr;
2835 {
2836   return bfd_xcoff_ldhdrsz(abfd) + 
2837     (ldhdr->l_nsyms * bfd_xcoff_ldsymsz(abfd));
2838 }
2839
2840
2841 static reloc_howto_type xcoff_dynamic_reloc =
2842 HOWTO (0,                       /* type */                                 
2843        0,                       /* rightshift */                           
2844        2,                       /* size (0 = byte, 1 = short, 2 = long) */ 
2845        32,                      /* bitsize */                   
2846        false,                   /* pc_relative */                          
2847        0,                       /* bitpos */                               
2848        complain_overflow_bitfield, /* complain_on_overflow */
2849        0,                       /* special_function */                     
2850        "R_POS",               /* name */                                 
2851        true,                    /* partial_inplace */                      
2852        0xffffffff,            /* src_mask */                             
2853        0xffffffff,            /* dst_mask */                             
2854        false);                /* pcrel_offset */
2855
2856 /* 
2857  * glink 
2858  *
2859  * The first word of global linkage code must be modified by filling in 
2860  * the correct TOC offset.  
2861  */
2862 static unsigned long xcoff_glink_code[9] =
2863 {
2864   0x81820000,   /* lwz r12,0(r2) */
2865   0x90410014,   /* stw r2,20(r1) */
2866   0x800c0000,   /* lwz r0,0(r12) */
2867   0x804c0004,   /* lwz r2,4(r12) */
2868   0x7c0903a6,   /* mtctr r0 */
2869   0x4e800420,   /* bctr */
2870   0x00000000,   /* start of traceback table */
2871   0x000c8000,   /* traceback table */
2872   0x00000000,   /* traceback table */
2873 };
2874
2875
2876 static const struct xcoff_backend_data_rec bfd_xcoff_backend_data = 
2877 {
2878   { /* COFF backend, defined in libcoff.h */
2879     _bfd_xcoff_swap_aux_in,           /* _bfd_coff_swap_aux_in */
2880     _bfd_xcoff_swap_sym_in,           /* _bfd_coff_swap_sym_in */ 
2881     coff_swap_lineno_in,              /* _bfd_coff_swap_lineno_in */
2882     _bfd_xcoff_swap_aux_out,          /* _bfd_swap_aux_out */
2883     _bfd_xcoff_swap_sym_out,          /* _bfd_swap_sym_out */
2884     coff_swap_lineno_out,             /* _bfd_swap_lineno_out */
2885     coff_swap_reloc_out,              /* _bfd_swap_reloc_out */
2886     coff_swap_filehdr_out,            /* _bfd_swap_filehdr_out */
2887     coff_swap_aouthdr_out,            /* _bfd_swap_aouthdr_out */
2888     coff_swap_scnhdr_out,             /* _bfd_swap_scnhdr_out */
2889     FILHSZ,                           /* _bfd_filhsz */
2890     AOUTSZ,                           /* _bfd_aoutsz */
2891     SCNHSZ,                           /* _bfd_scnhsz */
2892     SYMESZ,                           /* _bfd_symesz */
2893     AUXESZ,                           /* _bfd_auxesz */
2894     RELSZ,                            /* _bfd_relsz */
2895     LINESZ,                           /* _bfd_linesz */
2896         FILNMLEN,                         /* _bfd_filnmlen */
2897     true,                             /* _bfd_coff_long_filenames */
2898     false,                            /* _bfd_coff_long_section_names */
2899     (3),                        /* _bfd_coff_default_section_alignment_power */
2900     false,                            /* _bfd_coff_force_symnames_in_strings */
2901     2,                               /* _bfd_coff_debug_string_prefix_length */
2902     coff_swap_filehdr_in,             /* _bfd_coff_swap_filehdr_in */
2903     coff_swap_aouthdr_in,             /* _bfd_swap_aouthdr_in */
2904     coff_swap_scnhdr_in,              /* _bfd_swap_scnhdr_in */
2905     coff_swap_reloc_in,               /* _bfd_reloc_in */
2906     coff_bad_format_hook,             /* _bfd_bad_format_hook */
2907     coff_set_arch_mach_hook,          /* _bfd_set_arch_mach_hook */
2908     coff_mkobject_hook,               /* _bfd_mkobject_hook */
2909     styp_to_sec_flags,                /* _bfd_syp_to_sec_flags */
2910     coff_set_alignment_hook,          /* _bfd_set_alignment_hook */
2911     coff_slurp_symbol_table,     /* _bfd_coff_slurp_symbol_table */
2912     symname_in_debug_hook,            /* _coff_symname_in_debug_hook */
2913     coff_pointerize_aux_hook,         /* _bfd_coff_pointerize_aux_hook */
2914     coff_print_aux,                   /* bfd_coff_print_aux */
2915     dummy_reloc16_extra_cases,        /* _bfd_coff_reloc16_extra_cases */
2916     dummy_reloc16_estimate,           /* _bfd_coff_reloc16_estimate */
2917     NULL,                             /* bfd_coff_sym_is_global */
2918                                  /* _bfd_coff_compute_section_file_positions */
2919     coff_compute_section_file_positions,
2920     NULL ,                            /* _bfd_coff_start_final_link */
2921     xcoff_ppc_relocate_section,  /* _bfd_coff_relocate_section */
2922     coff_rtype_to_howto,              /* _bfd_coff_rtype_to_howto */
2923     NULL ,                            /* _bfd_coff_addust_symndx */
2924     _bfd_generic_link_add_one_symbol, /* _bfd_coff_add_one_symbol */
2925     coff_link_output_has_begun,       /* _bfd_coff_link_output_has_begun */
2926     coff_final_link_postscript        /* _bfd_coff_final_link_postscript */
2927   },
2928
2929   0x01DF,                             /* magic number */
2930   bfd_arch_rs6000,                    /* architecture */
2931   bfd_mach_rs6k,                      /* machine */
2932   
2933
2934   /* function pointers to xcoff specific swap routines */
2935   xcoff_swap_ldhdr_in,                /* _xcoff_swap_ldhdr_in */
2936   xcoff_swap_ldhdr_out,               /* _xcoff_swap_ldhdr_out */
2937   xcoff_swap_ldsym_in,                /* _xcoff_swap_ldsym_in */
2938   xcoff_swap_ldsym_out,               /* _xcoff_swap_ldsym_out */
2939   xcoff_swap_ldrel_in,                /* _xcoff_swap_ldrel_in */
2940   xcoff_swap_ldrel_out,               /* _xcoff_swap_ldrel_out */
2941
2942   /* sizes */
2943   LDHDRSZ,                            /* _xcoff_ldhdrsz */
2944   LDSYMSZ,                            /* _xcoff_ldsymsz */
2945   LDRELSZ,                            /* _xcoff_ldrelsz */
2946   12,                                 /* _xcoff_function_descriptor_size */
2947   SMALL_AOUTSZ,                       /* _xcoff_small_aout_header_size */
2948
2949   /* versions */
2950   1,                                    /* _xcoff_ldhdr_version */
2951
2952   /* xcoff vs xcoff64 putting symbol names */
2953   _bfd_xcoff_put_symbol_name,          /* _xcoff_put_symbol_name */
2954   _bfd_xcoff_put_ldsymbol_name,          /* _xcoff_put_ldsymbol_name */
2955
2956   &xcoff_dynamic_reloc,                  /* dynamic reloc howto */
2957
2958   xcoff_create_csect_from_smclas,      /* _xcoff_create_csect_from_smclas */
2959
2960   /* lineno and reloc count overflow */
2961   xcoff_is_lineno_count_overflow,
2962   xcoff_is_reloc_count_overflow,
2963
2964   xcoff_loader_symbol_offset,
2965   xcoff_loader_reloc_offset,
2966
2967   /* glink */
2968   &xcoff_glink_code[0],
2969   (36),           /* _xcoff_glink_size */
2970   
2971 };
2972
2973 /* The transfer vector that leads the outside world to all of the above. */
2974 const bfd_target rs6000coff_vec =
2975 {
2976   "aixcoff-rs6000",
2977   bfd_target_xcoff_flavour,
2978   BFD_ENDIAN_BIG,               /* data byte order is big */
2979   BFD_ENDIAN_BIG,               /* header byte order is big */
2980
2981   (HAS_RELOC | EXEC_P |         /* object flags */
2982    HAS_LINENO | HAS_DEBUG | DYNAMIC |
2983    HAS_SYMS | HAS_LOCALS | WP_TEXT),
2984
2985   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
2986   0,                            /* leading char */
2987   '/',                          /* ar_pad_char */
2988   15,                           /* ar_max_namelen??? FIXMEmgo */
2989
2990                       /* data */
2991   bfd_getb64,         /* bfd_getx64 */
2992   bfd_getb_signed_64, /* bfd_getx_signed_64 */
2993   bfd_putb64,         /* bfd_putx64 */
2994   bfd_getb32,         /* bfd_getx32 */
2995   bfd_getb_signed_32, /* bfd_getx_signed_32 */
2996   bfd_putb32,         /* bfd_putx32 */
2997   bfd_getb16,         /* bfd_getx16 */
2998   bfd_getb_signed_16, /* bfd_getx_signed_16 */
2999   bfd_putb16,         /* bfd_putx16 */ 
3000
3001                       /* hdrs */
3002   bfd_getb64,         /* bfd_h_getx64 */
3003   bfd_getb_signed_64, /* bfd_h_getx_signed_64 */
3004   bfd_putb64,         /* bfd_h_putx64 */
3005   bfd_getb32,         /* bfd_h_getx32 */
3006   bfd_getb_signed_32, /* bfd_h_getx_signed_32 */
3007   bfd_putb32,         /* bfd_h_putx32 */
3008   bfd_getb16,         /* bfd_h_getx16 */
3009   bfd_getb_signed_16, /* bfd_h_getx_signed_16 */
3010   bfd_putb16,         /* bfd_h_putx16 */
3011   
3012   { /* bfd_check_format */
3013     _bfd_dummy_target, 
3014     coff_object_p,      
3015     _bfd_xcoff_archive_p, 
3016     CORE_FILE_P
3017   },
3018   
3019   { /* bfd_set_format */
3020     bfd_false, 
3021     coff_mkobject,
3022     _bfd_generic_mkarchive, 
3023     bfd_false
3024   },
3025   
3026   {/* bfd_write_contents */
3027     bfd_false, 
3028     coff_write_object_contents,
3029     _bfd_xcoff_write_archive_contents, 
3030     bfd_false
3031   },
3032   
3033   /* Generic */
3034   bfd_true,                          /* _close_and_cleanup */
3035   bfd_true,                          /* _bfd_free_cached_info */
3036   coff_new_section_hook,             /* _new_section_hook */
3037   _bfd_generic_get_section_contents, /* _bfd_get_section_contents */
3038                                      /* _bfd_get_section_contents_in_window */
3039   _bfd_generic_get_section_contents_in_window, 
3040
3041   /* Copy */
3042   _bfd_xcoff_copy_private_bfd_data, /* _bfd_copy_private_bfd */
3043                                     /* _bfd_merge_private_bfd_data */ 
3044   ((boolean (*) (bfd *, bfd *)) bfd_true),
3045                                     /* _bfd_copy_pivate_section_data */
3046   ((boolean (*) (bfd *, asection *, bfd *, asection *)) bfd_true),
3047                                     /* _bfd_copy_private_symbol_data */
3048   ((boolean (*) (bfd *, asymbol *, bfd *, asymbol *)) bfd_true),
3049   ((boolean (*) (bfd *, flagword)) bfd_true), /* _bfd_set_private_flags */
3050   ((boolean (*) (bfd *, void * )) bfd_true),  /* _bfd_print_private_bfd_data */
3051
3052   /* Core */
3053   rs6000coff_core_file_failing_command,    /* _core_file_failing_command */
3054   rs6000coff_core_file_failing_signal,     /* _core_file_failing_signal */
3055                                           /* _core_file_matches_executable_p */
3056   rs6000coff_core_file_matches_executable_p, 
3057
3058   /* Archive */
3059   _bfd_xcoff_slurp_armap,                  /* _slurp_armap */
3060                                            /* XCOFF archives do not have 
3061                                               anything which corresponds to 
3062                                               an extended name table.  */
3063   bfd_false,                               /* _slurp_extended_name_table */
3064                                            /* _construct_extended_name_table */
3065   ((boolean (*) (bfd *, char **, bfd_size_type *, const char **)) bfd_false),
3066   bfd_dont_truncate_arname,                /* _truncate_arname */
3067   _bfd_xcoff_write_armap,                  /* _write_armap */
3068   _bfd_xcoff_read_ar_hdr,                  /* _read_ar_hdr */
3069   _bfd_xcoff_openr_next_archived_file,     /* _openr_next_archived_file */
3070   _bfd_generic_get_elt_at_index,           /* _get_elt_at_index */
3071   _bfd_xcoff_generic_stat_arch_elt,        /* _generic_dtat_arch_elt */
3072                                            /* XCOFF archives do not have 
3073                                               a timestamp.  */
3074   bfd_true,                                /* _update_armap_timestamp */
3075
3076   /* Symbols */
3077   coff_get_symtab_upper_bound,             /* _get_symtab_upper_bound */
3078   coff_get_symtab,                         /* _get_symtab */
3079   coff_make_empty_symbol,                  /* _make_empty_symbol */
3080   coff_print_symbol,                       /* _print_symbol */
3081   coff_get_symbol_info,                    /* _get_symbol_info */
3082   _bfd_xcoff_is_local_label_name,          /* _bfd_is_local_label_name */
3083   coff_get_lineno,                         /* _get_lineno */
3084   coff_find_nearest_line,                  /* _find_nearest_line */
3085   coff_bfd_make_debug_symbol,              /* _bfd_make_debug_symbol */
3086   _bfd_generic_read_minisymbols,           /* _read_minisymbols */
3087   _bfd_generic_minisymbol_to_symbol,       /* _minsymbol_to_symbol */
3088
3089   /* Reloc */
3090   coff_get_reloc_upper_bound,              /* _get_reloc_upper_bound */
3091   coff_canonicalize_reloc,                 /* _cononicalize_reloc */
3092   _bfd_xcoff_reloc_type_lookup,            /* _bfd_reloc_type_lookup */
3093
3094   /* Write */
3095   coff_set_arch_mach,                      /* _set_arch_mach */
3096   coff_set_section_contents,               /* _set_section_contents */
3097
3098   /* Link */
3099   _bfd_xcoff_sizeof_headers,               /* _sizeof_headers */
3100                                       /* _bfd_get_relocated_section_contents */
3101   bfd_generic_get_relocated_section_contents,
3102   bfd_generic_relax_section,               /* _bfd_relax_section */
3103   _bfd_xcoff_bfd_link_hash_table_create,   /* _bfd_link_hash_table_create */
3104   _bfd_xcoff_bfd_link_add_symbols,         /* _bfd_link_add_symbols */
3105   _bfd_xcoff_bfd_final_link,               /* _bfd_filnal_link */
3106   _bfd_generic_link_split_section,         /* _bfd_link_split_section */
3107   bfd_generic_gc_sections,                 /* _bfd_gc_sections */
3108   bfd_generic_merge_sections,               /* _bfd_merge_sections */
3109
3110   /* Dynamic */
3111                                           /* _get_dynamic_symtab_upper_bound */
3112   _bfd_xcoff_get_dynamic_symtab_upper_bound,  
3113   _bfd_xcoff_canonicalize_dynamic_symtab,  /* _cononicalize_dynamic_symtab */
3114   _bfd_xcoff_get_dynamic_reloc_upper_bound,/* _get_dynamic_reloc_upper_bound */
3115   _bfd_xcoff_canonicalize_dynamic_reloc,   /* _cononicalize_dynamic_reloc */
3116
3117   /* Opposite endian version, none exists */
3118   NULL,
3119   
3120   /* back end data */
3121   (void *) &bfd_xcoff_backend_data,
3122 };
3123
3124 /* 
3125  * xcoff-powermac target
3126  * Old target.
3127  * Only difference between this target and the rs6000 target is the 
3128  * the default architecture and machine type used in coffcode.h
3129  *
3130  * PowerPC Macs use the same magic numbers as RS/6000
3131  * (because that's how they were bootstrapped originally),
3132  * but they are always PowerPC architecture. 
3133  */
3134 static const struct xcoff_backend_data_rec bfd_pmac_xcoff_backend_data = 
3135 {
3136   { /* COFF backend, defined in libcoff.h */
3137     _bfd_xcoff_swap_aux_in,           /* _bfd_coff_swap_aux_in */
3138     _bfd_xcoff_swap_sym_in,           /* _bfd_coff_swap_sym_in */ 
3139     coff_swap_lineno_in,              /* _bfd_coff_swap_lineno_in */
3140     _bfd_xcoff_swap_aux_out,          /* _bfd_swap_aux_out */
3141     _bfd_xcoff_swap_sym_out,          /* _bfd_swap_sym_out */
3142     coff_swap_lineno_out,             /* _bfd_swap_lineno_out */
3143     coff_swap_reloc_out,              /* _bfd_swap_reloc_out */
3144     coff_swap_filehdr_out,            /* _bfd_swap_filehdr_out */
3145     coff_swap_aouthdr_out,            /* _bfd_swap_aouthdr_out */
3146     coff_swap_scnhdr_out,             /* _bfd_swap_scnhdr_out */
3147     FILHSZ,                           /* _bfd_filhsz */
3148     AOUTSZ,                           /* _bfd_aoutsz */
3149     SCNHSZ,                           /* _bfd_scnhsz */
3150     SYMESZ,                           /* _bfd_symesz */
3151     AUXESZ,                           /* _bfd_auxesz */
3152     RELSZ,                            /* _bfd_relsz */
3153     LINESZ,                           /* _bfd_linesz */
3154         FILNMLEN,                         /* _bfd_filnmlen */
3155     true,                             /* _bfd_coff_long_filenames */
3156     false,                            /* _bfd_coff_long_section_names */
3157     (3),                        /* _bfd_coff_default_section_alignment_power */
3158     false,                            /* _bfd_coff_force_symnames_in_strings */
3159     2,                               /* _bfd_coff_debug_string_prefix_length */
3160     coff_swap_filehdr_in,             /* _bfd_coff_swap_filehdr_in */
3161     coff_swap_aouthdr_in,             /* _bfd_swap_aouthdr_in */
3162     coff_swap_scnhdr_in,              /* _bfd_swap_scnhdr_in */
3163     coff_swap_reloc_in,               /* _bfd_reloc_in */
3164     coff_bad_format_hook,             /* _bfd_bad_format_hook */
3165     coff_set_arch_mach_hook,          /* _bfd_set_arch_mach_hook */
3166     coff_mkobject_hook,               /* _bfd_mkobject_hook */
3167     styp_to_sec_flags,                /* _bfd_syp_to_sec_flags */
3168     coff_set_alignment_hook,          /* _bfd_set_alignment_hook */
3169     coff_slurp_symbol_table,     /* _bfd_coff_slurp_symbol_table */
3170     symname_in_debug_hook,            /* _coff_symname_in_debug_hook */
3171     coff_pointerize_aux_hook,         /* _bfd_coff_pointerize_aux_hook */
3172     coff_print_aux,                   /* bfd_coff_print_aux */
3173     dummy_reloc16_extra_cases,        /* _bfd_coff_reloc16_extra_cases */
3174     dummy_reloc16_estimate,           /* _bfd_coff_reloc16_estimate */
3175     NULL,                             /* bfd_coff_sym_is_global */
3176                                  /* _bfd_coff_compute_section_file_positions */
3177     coff_compute_section_file_positions,
3178     NULL ,                            /* _bfd_coff_start_final_link */
3179     xcoff_ppc_relocate_section,  /* _bfd_coff_relocate_section */
3180     coff_rtype_to_howto,              /* _bfd_coff_rtype_to_howto */
3181     NULL ,                            /* _bfd_coff_addust_symndx */
3182     _bfd_generic_link_add_one_symbol, /* _bfd_coff_add_one_symbol */
3183     coff_link_output_has_begun,       /* _bfd_coff_link_output_has_begun */
3184     coff_final_link_postscript        /* _bfd_coff_final_link_postscript */
3185   },
3186
3187   0x01DF,                             /* magic number */
3188   bfd_arch_powerpc,                   /* architecture */
3189   bfd_mach_ppc,                       /* machine */
3190
3191   /* function pointers to xcoff specific swap routines */
3192   xcoff_swap_ldhdr_in,                /* _xcoff_swap_ldhdr_in */
3193   xcoff_swap_ldhdr_out,               /* _xcoff_swap_ldhdr_out */
3194   xcoff_swap_ldsym_in,                /* _xcoff_swap_ldsym_in */
3195   xcoff_swap_ldsym_out,               /* _xcoff_swap_ldsym_out */
3196   xcoff_swap_ldrel_in,                /* _xcoff_swap_ldrel_in */
3197   xcoff_swap_ldrel_out,               /* _xcoff_swap_ldrel_out */
3198
3199   /* sizes */
3200   LDHDRSZ,                            /* _xcoff_ldhdrsz */
3201   LDSYMSZ,                            /* _xcoff_ldsymsz */
3202   LDRELSZ,                            /* _xcoff_ldrelsz */
3203   12,                                 /* _xcoff_function_descriptor_size */
3204   SMALL_AOUTSZ,                       /* _xcoff_small_aout_header_size */
3205
3206   /* versions */
3207   1,                                    /* _xcoff_ldhdr_version */
3208
3209   /* xcoff vs xcoff64 putting symbol names */
3210   _bfd_xcoff_put_symbol_name,          /* _xcoff_put_symbol_name */
3211   _bfd_xcoff_put_ldsymbol_name,          /* _xcoff_put_ldsymbol_name */
3212
3213   &xcoff_dynamic_reloc,                  /* dynamic reloc howto */
3214
3215   xcoff_create_csect_from_smclas,      /* _xcoff_create_csect_from_smclas */
3216
3217   /* lineno and reloc count overflow */
3218   xcoff_is_lineno_count_overflow,
3219   xcoff_is_reloc_count_overflow,
3220
3221   xcoff_loader_symbol_offset,
3222   xcoff_loader_reloc_offset,
3223
3224   /* glink */
3225   &xcoff_glink_code[0],
3226   (36),           /* _xcoff_glink_size */
3227   
3228 };
3229
3230 /* The transfer vector that leads the outside world to all of the above. */
3231 const bfd_target pmac_xcoff_vec =
3232 {
3233   "xcoff-powermac",
3234   bfd_target_xcoff_flavour,
3235   BFD_ENDIAN_BIG,               /* data byte order is big */
3236   BFD_ENDIAN_BIG,               /* header byte order is big */
3237
3238   (HAS_RELOC | EXEC_P |         /* object flags */
3239    HAS_LINENO | HAS_DEBUG | DYNAMIC |
3240    HAS_SYMS | HAS_LOCALS | WP_TEXT),
3241
3242   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
3243   0,                            /* leading char */
3244   '/',                          /* ar_pad_char */
3245   15,                           /* ar_max_namelen??? FIXMEmgo */
3246
3247                       /* data */
3248   bfd_getb64,         /* bfd_getx64 */
3249   bfd_getb_signed_64, /* bfd_getx_signed_64 */
3250   bfd_putb64,         /* bfd_putx64 */
3251   bfd_getb32,         /* bfd_getx32 */
3252   bfd_getb_signed_32, /* bfd_getx_signed_32 */
3253   bfd_putb32,         /* bfd_putx32 */
3254   bfd_getb16,         /* bfd_getx16 */
3255   bfd_getb_signed_16, /* bfd_getx_signed_16 */
3256   bfd_putb16,         /* bfd_putx16 */ 
3257
3258                       /* hdrs */
3259   bfd_getb64,         /* bfd_h_getx64 */
3260   bfd_getb_signed_64, /* bfd_h_getx_signed_64 */
3261   bfd_putb64,         /* bfd_h_putx64 */
3262   bfd_getb32,         /* bfd_h_getx32 */
3263   bfd_getb_signed_32, /* bfd_h_getx_signed_32 */
3264   bfd_putb32,         /* bfd_h_putx32 */
3265   bfd_getb16,         /* bfd_h_getx16 */
3266   bfd_getb_signed_16, /* bfd_h_getx_signed_16 */
3267   bfd_putb16,         /* bfd_h_putx16 */
3268   
3269   { /* bfd_check_format */
3270     _bfd_dummy_target, 
3271     coff_object_p,      
3272     _bfd_xcoff_archive_p, 
3273     CORE_FILE_P
3274   },
3275   
3276   { /* bfd_set_format */
3277     bfd_false, 
3278     coff_mkobject,
3279     _bfd_generic_mkarchive, 
3280     bfd_false
3281   },
3282   
3283   {/* bfd_write_contents */
3284     bfd_false, 
3285     coff_write_object_contents,
3286     _bfd_xcoff_write_archive_contents, 
3287     bfd_false
3288   },
3289   
3290   /* Generic */
3291   bfd_true,                          /* _close_and_cleanup */
3292   bfd_true,                          /* _bfd_free_cached_info */
3293   coff_new_section_hook,             /* _new_section_hook */
3294   _bfd_generic_get_section_contents, /* _bfd_get_section_contents */
3295                                      /* _bfd_get_section_contents_in_window */
3296   _bfd_generic_get_section_contents_in_window, 
3297
3298   /* Copy */
3299   _bfd_xcoff_copy_private_bfd_data, /* _bfd_copy_private_bfd */
3300                                     /* _bfd_merge_private_bfd_data */ 
3301   ((boolean (*) (bfd *, bfd *)) bfd_true),
3302                                     /* _bfd_copy_pivate_section_data */
3303   ((boolean (*) (bfd *, asection *, bfd *, asection *)) bfd_true),
3304                                     /* _bfd_copy_private_symbol_data */
3305   ((boolean (*) (bfd *, asymbol *, bfd *, asymbol *)) bfd_true),
3306   ((boolean (*) (bfd *, flagword)) bfd_true), /* _bfd_set_private_flags */
3307   ((boolean (*) (bfd *, void * )) bfd_true),  /* _bfd_print_private_bfd_data */
3308
3309   /* Core */
3310   rs6000coff_core_file_failing_command,    /* _core_file_failing_command */
3311   rs6000coff_core_file_failing_signal,     /* _core_file_failing_signal */
3312                                           /* _core_file_matches_executable_p */
3313   rs6000coff_core_file_matches_executable_p, 
3314
3315   /* Archive */
3316   _bfd_xcoff_slurp_armap,                  /* _slurp_armap */
3317                                            /* XCOFF archives do not have 
3318                                               anything which corresponds to 
3319                                               an extended name table.  */
3320   bfd_false,                               /* _slurp_extended_name_table */
3321                                            /* _construct_extended_name_table */
3322   ((boolean (*) (bfd *, char **, bfd_size_type *, const char **)) bfd_false),
3323   bfd_dont_truncate_arname,                /* _truncate_arname */
3324   _bfd_xcoff_write_armap,                  /* _write_armap */
3325   _bfd_xcoff_read_ar_hdr,                  /* _read_ar_hdr */
3326   _bfd_xcoff_openr_next_archived_file,     /* _openr_next_archived_file */
3327   _bfd_generic_get_elt_at_index,           /* _get_elt_at_index */
3328   _bfd_xcoff_generic_stat_arch_elt,        /* _generic_dtat_arch_elt */
3329                                            /* XCOFF archives do not have 
3330                                               a timestamp.  */
3331   bfd_true,                                /* _update_armap_timestamp */
3332
3333   /* Symbols */
3334   coff_get_symtab_upper_bound,             /* _get_symtab_upper_bound */
3335   coff_get_symtab,                         /* _get_symtab */
3336   coff_make_empty_symbol,                  /* _make_empty_symbol */
3337   coff_print_symbol,                       /* _print_symbol */
3338   coff_get_symbol_info,                    /* _get_symbol_info */
3339   _bfd_xcoff_is_local_label_name,          /* _bfd_is_local_label_name */
3340   coff_get_lineno,                         /* _get_lineno */
3341   coff_find_nearest_line,                  /* _find_nearest_line */
3342   coff_bfd_make_debug_symbol,              /* _bfd_make_debug_symbol */
3343   _bfd_generic_read_minisymbols,           /* _read_minisymbols */
3344   _bfd_generic_minisymbol_to_symbol,       /* _minsymbol_to_symbol */
3345
3346   /* Reloc */
3347   coff_get_reloc_upper_bound,              /* _get_reloc_upper_bound */
3348   coff_canonicalize_reloc,                 /* _cononicalize_reloc */
3349   _bfd_xcoff_reloc_type_lookup,            /* _bfd_reloc_type_lookup */
3350
3351   /* Write */
3352   coff_set_arch_mach,                      /* _set_arch_mach */
3353   coff_set_section_contents,               /* _set_section_contents */
3354
3355   /* Link */
3356   _bfd_xcoff_sizeof_headers,               /* _sizeof_headers */
3357                                       /* _bfd_get_relocated_section_contents */
3358   bfd_generic_get_relocated_section_contents,
3359   bfd_generic_relax_section,               /* _bfd_relax_section */
3360   _bfd_xcoff_bfd_link_hash_table_create,   /* _bfd_link_hash_table_create */
3361   _bfd_xcoff_bfd_link_add_symbols,         /* _bfd_link_add_symbols */
3362   _bfd_xcoff_bfd_final_link,               /* _bfd_filnal_link */
3363   _bfd_generic_link_split_section,         /* _bfd_link_split_section */
3364   bfd_generic_gc_sections,                 /* _bfd_gc_sections */
3365   bfd_generic_merge_sections,               /* _bfd_merge_sections */
3366
3367   /* Dynamic */
3368                                           /* _get_dynamic_symtab_upper_bound */
3369   _bfd_xcoff_get_dynamic_symtab_upper_bound,  
3370   _bfd_xcoff_canonicalize_dynamic_symtab,  /* _cononicalize_dynamic_symtab */
3371   _bfd_xcoff_get_dynamic_reloc_upper_bound,/* _get_dynamic_reloc_upper_bound */
3372   _bfd_xcoff_canonicalize_dynamic_reloc,   /* _cononicalize_dynamic_reloc */
3373
3374   /* Opposite endian version, none exists */
3375   NULL,
3376   
3377   /* back end data */
3378   (void *) &bfd_pmac_xcoff_backend_data,
3379 };
3380