OSDN Git Service

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