OSDN Git Service

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