-binitfini emulation, put __rtinit symbol generation into linker.
[platform/upstream/binutils.git] / bfd / coff64-rs6000.c
1 /* BFD back-end for IBM RS/6000 "XCOFF64" files.
2    Copyright 2000, 2001
3    Free Software Foundation, Inc.
4    Written Clinton Popetz.
5    Contributed by Cygnus Support.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "coff/internal.h"
28 #include "coff/xcoff.h"
29 #include "coff/rs6k64.h"
30 #include "libcoff.h"
31 #include "libxcoff.h"
32
33 #define GET_FILEHDR_SYMPTR H_GET_64
34 #define PUT_FILEHDR_SYMPTR H_PUT_64
35 #define GET_AOUTHDR_DATA_START H_GET_64
36 #define PUT_AOUTHDR_DATA_START H_PUT_64
37 #define GET_AOUTHDR_TEXT_START H_GET_64
38 #define PUT_AOUTHDR_TEXT_START H_PUT_64
39 #define GET_AOUTHDR_TSIZE H_GET_64
40 #define PUT_AOUTHDR_TSIZE H_PUT_64
41 #define GET_AOUTHDR_DSIZE H_GET_64
42 #define PUT_AOUTHDR_DSIZE H_PUT_64
43 #define GET_AOUTHDR_BSIZE H_GET_64
44 #define PUT_AOUTHDR_BSIZE H_PUT_64
45 #define GET_AOUTHDR_ENTRY H_GET_64
46 #define PUT_AOUTHDR_ENTRY H_PUT_64
47 #define GET_SCNHDR_PADDR H_GET_64
48 #define PUT_SCNHDR_PADDR H_PUT_64
49 #define GET_SCNHDR_VADDR H_GET_64
50 #define PUT_SCNHDR_VADDR H_PUT_64
51 #define GET_SCNHDR_SIZE H_GET_64
52 #define PUT_SCNHDR_SIZE H_PUT_64
53 #define GET_SCNHDR_SCNPTR H_GET_64
54 #define PUT_SCNHDR_SCNPTR H_PUT_64
55 #define GET_SCNHDR_RELPTR H_GET_64
56 #define PUT_SCNHDR_RELPTR H_PUT_64
57 #define GET_SCNHDR_LNNOPTR H_GET_64
58 #define PUT_SCNHDR_LNNOPTR H_PUT_64
59 #define GET_SCNHDR_NRELOC H_GET_32
60 #define MAX_SCNHDR_NRELOC 0xffffffff
61 #define PUT_SCNHDR_NRELOC H_PUT_32
62 #define GET_SCNHDR_NLNNO H_GET_32
63 #define MAX_SCNHDR_NLNNO 0xffffffff
64 #define PUT_SCNHDR_NLNNO H_PUT_32
65 #define GET_RELOC_VADDR H_GET_64
66 #define PUT_RELOC_VADDR H_PUT_64
67
68 #define COFF_FORCE_SYMBOLS_IN_STRINGS
69 #define COFF_DEBUG_STRING_WIDE_PREFIX
70
71
72 #define COFF_ADJUST_SCNHDR_OUT_POST(ABFD, INT, EXT)                     \
73   do                                                                    \
74     {                                                                   \
75       memset (((SCNHDR *) EXT)->s_pad, 0,                               \
76               sizeof (((SCNHDR *) EXT)->s_pad));                        \
77     }                                                                   \
78   while (0)
79
80 #define NO_COFF_LINENOS
81
82 #define coff_SWAP_lineno_in _bfd_xcoff64_swap_lineno_in
83 #define coff_SWAP_lineno_out _bfd_xcoff64_swap_lineno_out
84
85 static void _bfd_xcoff64_swap_lineno_in PARAMS ((bfd *, PTR, PTR));
86 static unsigned int _bfd_xcoff64_swap_lineno_out PARAMS ((bfd *, PTR, PTR));
87 static boolean _bfd_xcoff64_put_ldsymbol_name
88   PARAMS ((bfd *, struct xcoff_loader_info *, struct internal_ldsym *,
89            const char *));
90 static void _bfd_xcoff64_swap_sym_in PARAMS ((bfd *, PTR, PTR));
91 static unsigned int _bfd_xcoff64_swap_sym_out PARAMS ((bfd *, PTR, PTR));
92 static void _bfd_xcoff64_swap_aux_in
93   PARAMS ((bfd *, PTR, int, int, int, int, PTR));
94 static unsigned int _bfd_xcoff64_swap_aux_out
95   PARAMS ((bfd *, PTR, int, int, int, int, PTR));
96 extern boolean _bfd_xcoff_mkobject PARAMS ((bfd *));
97 extern boolean _bfd_xcoff_copy_private_bfd_data PARAMS ((bfd *, bfd *));
98 extern boolean _bfd_xcoff_is_local_label_name PARAMS ((bfd *, const char *));
99 extern void xcoff64_rtype2howto
100   PARAMS ((arelent *, struct internal_reloc *));
101 extern reloc_howto_type * xcoff64_reloc_type_lookup
102   PARAMS ((bfd *, bfd_reloc_code_real_type));
103 extern boolean _bfd_xcoff_slurp_armap PARAMS ((bfd *));
104 extern PTR _bfd_xcoff_read_ar_hdr PARAMS ((bfd *));
105 extern bfd *_bfd_xcoff_openr_next_archived_file PARAMS ((bfd *, bfd *));
106 extern int _bfd_xcoff_generic_stat_arch_elt PARAMS ((bfd *, struct stat *));
107 extern boolean _bfd_xcoff_write_armap
108   PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int));
109 extern boolean _bfd_xcoff_write_archive_contents PARAMS ((bfd *));
110 extern int _bfd_xcoff_sizeof_headers PARAMS ((bfd *, boolean));
111 extern void _bfd_xcoff_swap_sym_in PARAMS ((bfd *, PTR, PTR));
112 extern unsigned int _bfd_xcoff_swap_sym_out PARAMS ((bfd *, PTR, PTR));
113 extern void _bfd_xcoff_swap_aux_in
114   PARAMS ((bfd *, PTR, int, int, int, int, PTR));
115 extern unsigned int _bfd_xcoff_swap_aux_out
116   PARAMS ((bfd *, PTR, int, int, int, int, PTR));
117 static void xcoff64_swap_ldhdr_in
118   PARAMS ((bfd *, const PTR, struct internal_ldhdr *));
119 static void xcoff64_swap_ldhdr_out
120   PARAMS ((bfd *, const struct internal_ldhdr *, PTR d));
121 static void xcoff64_swap_ldsym_in
122   PARAMS ((bfd *, const PTR, struct internal_ldsym *));
123 static void xcoff64_swap_ldsym_out
124   PARAMS ((bfd *, const struct internal_ldsym *, PTR d));
125 static void xcoff64_swap_ldrel_in
126   PARAMS ((bfd *, const PTR, struct internal_ldrel *));
127 static void xcoff64_swap_ldrel_out
128   PARAMS ((bfd *, const struct internal_ldrel *, PTR d));
129 static boolean xcoff64_write_object_contents PARAMS ((bfd *));
130 static boolean xcoff64_ppc_relocate_section
131   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
132          struct internal_reloc *, struct internal_syment *,
133          asection **sections));
134 static boolean xcoff64_slurp_armap PARAMS ((bfd *));
135 static const bfd_target *xcoff64_archive_p PARAMS ((bfd *));
136 static bfd *xcoff64_openr_next_archived_file PARAMS ((bfd *, bfd *));
137 static int xcoff64_sizeof_headers PARAMS ((bfd *, boolean));
138 static asection *xcoff64_create_csect_from_smclas
139   PARAMS ((bfd *, union internal_auxent *, const char *));
140 static boolean xcoff64_is_lineno_count_overflow PARAMS ((bfd *, bfd_vma));
141 static boolean xcoff64_is_reloc_count_overflow PARAMS ((bfd *, bfd_vma));
142 static bfd_vma xcoff64_loader_symbol_offset
143   PARAMS ((bfd *, struct internal_ldhdr *));
144 static bfd_vma xcoff64_loader_reloc_offset
145   PARAMS ((bfd *, struct internal_ldhdr *));
146
147 /* coffcode.h needs these to be defined */
148 /* Internalcoff.h and coffcode.h modify themselves based on these flags.  */
149 #define XCOFF64
150 #define RS6000COFF_C 1
151
152 #define SELECT_RELOC(internal, howto)                                   \
153   {                                                                     \
154     internal.r_type = howto->type;                                      \
155     internal.r_size =                                                   \
156       ((howto->complain_on_overflow == complain_overflow_signed         \
157         ? 0x80                                                          \
158         : 0)                                                            \
159        | (howto->bitsize - 1));                                         \
160   }
161
162 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
163 #define COFF_LONG_FILENAMES
164 #define NO_COFF_SYMBOLS
165 #define RTYPE2HOWTO(cache_ptr, dst) xcoff64_rtype2howto (cache_ptr, dst)
166 #define coff_mkobject _bfd_xcoff_mkobject
167 #define coff_bfd_copy_private_bfd_data _bfd_xcoff_copy_private_bfd_data
168 #define coff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
169 #define coff_bfd_reloc_type_lookup xcoff64_reloc_type_lookup
170 #ifdef AIX_CORE
171 extern const bfd_target * rs6000coff_core_p ();
172 extern boolean rs6000coff_core_file_matches_executable_p ();
173 extern char *rs6000coff_core_file_failing_command PARAMS ((bfd *abfd));
174 extern int rs6000coff_core_file_failing_signal PARAMS ((bfd *abfd));
175 #define CORE_FILE_P rs6000coff_core_p
176 #define coff_core_file_failing_command \
177   rs6000coff_core_file_failing_command
178 #define coff_core_file_failing_signal \
179   rs6000coff_core_file_failing_signal
180 #define coff_core_file_matches_executable_p \
181   rs6000coff_core_file_matches_executable_p
182 #else
183 #define CORE_FILE_P _bfd_dummy_target
184 #define coff_core_file_failing_command \
185   _bfd_nocore_core_file_failing_command
186 #define coff_core_file_failing_signal \
187   _bfd_nocore_core_file_failing_signal
188 #define coff_core_file_matches_executable_p \
189   _bfd_nocore_core_file_matches_executable_p
190 #endif
191 #define coff_SWAP_sym_in _bfd_xcoff64_swap_sym_in
192 #define coff_SWAP_sym_out _bfd_xcoff64_swap_sym_out
193 #define coff_SWAP_aux_in _bfd_xcoff64_swap_aux_in
194 #define coff_SWAP_aux_out _bfd_xcoff64_swap_aux_out
195
196
197 #include "coffcode.h"
198
199 /* For XCOFF64, the effective width of symndx changes depending on
200    whether we are the first entry.  Sigh.  */
201 static void
202 _bfd_xcoff64_swap_lineno_in (abfd, ext1, in1)
203      bfd *abfd;
204      PTR ext1;
205      PTR in1;
206 {
207   LINENO *ext = (LINENO *) ext1;
208   struct internal_lineno *in = (struct internal_lineno *) in1;
209
210   in->l_lnno = H_GET_32 (abfd, (ext->l_lnno));
211   if (in->l_lnno == 0)
212     in->l_addr.l_symndx = H_GET_32 (abfd, ext->l_addr.l_symndx);
213   else
214     in->l_addr.l_paddr = H_GET_64 (abfd, ext->l_addr.l_paddr);
215 }
216
217 static unsigned int
218 _bfd_xcoff64_swap_lineno_out (abfd, inp, outp)
219      bfd *abfd;
220      PTR inp;
221      PTR outp;
222 {
223   struct internal_lineno *in = (struct internal_lineno *) inp;
224   struct external_lineno *ext = (struct external_lineno *) outp;
225
226   H_PUT_32 (abfd, in->l_addr.l_symndx, ext->l_addr.l_symndx);
227   H_PUT_32 (abfd, in->l_lnno, (ext->l_lnno));
228
229   if (in->l_lnno == 0)
230     H_PUT_32 (abfd, in->l_addr.l_symndx, ext->l_addr.l_symndx);
231   else
232     H_PUT_64 (abfd, in->l_addr.l_paddr, ext->l_addr.l_paddr);
233
234   return bfd_coff_linesz (abfd);
235 }
236
237 static void
238 _bfd_xcoff64_swap_sym_in (abfd, ext1, in1)
239      bfd *abfd;
240      PTR ext1;
241      PTR in1;
242 {
243   struct external_syment *ext = (struct external_syment *) ext1;
244   struct internal_syment *in = (struct internal_syment *) in1;
245
246   in->_n._n_n._n_zeroes = 0;
247   in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e_offset);
248   in->n_value = H_GET_64 (abfd, ext->e_value);
249   in->n_scnum = H_GET_16 (abfd, ext->e_scnum);
250   in->n_type = H_GET_16 (abfd, ext->e_type);
251   in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
252   in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
253 }
254
255 static unsigned int
256 _bfd_xcoff64_swap_sym_out (abfd, inp, extp)
257      bfd *abfd;
258      PTR inp;
259      PTR extp;
260 {
261   struct internal_syment *in = (struct internal_syment *) inp;
262   struct external_syment *ext = (struct external_syment *) extp;
263
264   H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e_offset);
265   H_PUT_64 (abfd, in->n_value, ext->e_value);
266   H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
267   H_PUT_16 (abfd, in->n_type, ext->e_type);
268   H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
269   H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
270   return bfd_coff_symesz (abfd);
271 }
272
273 static void
274 _bfd_xcoff64_swap_aux_in (abfd, ext1, type, class, indx, numaux, in1)
275      bfd *abfd;
276      PTR ext1;
277      int type;
278      int class;
279      int indx;
280      int numaux;
281      PTR in1;
282 {
283   union external_auxent *ext = (union external_auxent *) ext1;
284   union internal_auxent *in = (union internal_auxent *) in1;
285
286   switch (class)
287     {
288     case C_FILE:
289       if (ext->x_file.x_n.x_zeroes == 0)
290         {
291           in->x_file.x_n.x_zeroes = 0;
292           in->x_file.x_n.x_offset = H_GET_32 (abfd, ext->x_file.x_n.x_offset);
293         }
294       else
295         {
296           memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
297         }
298       goto end;
299
300       /* RS/6000 "csect" auxents */
301     case C_EXT:
302     case C_HIDEXT:
303       if (indx + 1 == numaux)
304         {
305           bfd_signed_vma h = 0;
306           bfd_vma l = 0;
307
308           h = H_GET_S32 (abfd, ext->x_csect.x_scnlen_hi);
309           l = H_GET_32 (abfd, ext->x_csect.x_scnlen_lo);
310
311           in->x_csect.x_scnlen.l = h << 32 | (l & 0xffffffff);
312
313           in->x_csect.x_parmhash = H_GET_32 (abfd, ext->x_csect.x_parmhash);
314           in->x_csect.x_snhash = H_GET_16 (abfd, ext->x_csect.x_snhash);
315           /* We don't have to hack bitfields in x_smtyp because it's
316              defined by shifts-and-ands, which are equivalent on all
317              byte orders.  */
318           in->x_csect.x_smtyp = H_GET_8 (abfd, ext->x_csect.x_smtyp);
319           in->x_csect.x_smclas = H_GET_8 (abfd, ext->x_csect.x_smclas);
320           goto end;
321         }
322       break;
323
324     case C_STAT:
325     case C_LEAFSTAT:
326     case C_HIDDEN:
327       if (type == T_NULL)
328         {
329           /* PE defines some extra fields; we zero them out for
330              safety.  */
331           in->x_scn.x_checksum = 0;
332           in->x_scn.x_associated = 0;
333           in->x_scn.x_comdat = 0;
334
335           goto end;
336         }
337       break;
338     }
339
340   if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
341     {
342       in->x_sym.x_fcnary.x_fcn.x_lnnoptr
343         = H_GET_64 (abfd, ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
344       in->x_sym.x_fcnary.x_fcn.x_endndx.l
345         = H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_endndx);
346     }
347   if (ISFCN (type))
348     {
349       in->x_sym.x_misc.x_fsize
350         = H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_fsize);
351     }
352   else
353     {
354       in->x_sym.x_misc.x_lnsz.x_lnno
355         = H_GET_32 (abfd, ext->x_sym.x_fcnary.x_lnsz.x_lnno);
356       in->x_sym.x_misc.x_lnsz.x_size
357         = H_GET_16 (abfd, ext->x_sym.x_fcnary.x_lnsz.x_size);
358     }
359
360  end: ;
361 }
362
363 static unsigned int
364 _bfd_xcoff64_swap_aux_out (abfd, inp, type, class, indx, numaux, extp)
365      bfd *abfd;
366      PTR inp;
367      int type;
368      int class;
369      int indx ATTRIBUTE_UNUSED;
370      int numaux ATTRIBUTE_UNUSED;
371      PTR extp;
372 {
373   union internal_auxent *in = (union internal_auxent *) inp;
374   union external_auxent *ext = (union external_auxent *) extp;
375
376   memset ((PTR) ext, 0, bfd_coff_auxesz (abfd));
377   switch (class)
378     {
379     case C_FILE:
380       if (ext->x_file.x_n.x_zeroes == 0)
381         {
382           H_PUT_32 (abfd, 0, ext->x_file.x_n.x_zeroes);
383           H_PUT_32 (abfd, in->x_file.x_n.x_offset, ext->x_file.x_n.x_offset);
384         }
385       else
386         {
387           memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
388         }
389       H_PUT_8 (abfd, _AUX_FILE, ext->x_auxtype.x_auxtype);
390       goto end;
391
392       /* RS/6000 "csect" auxents */
393     case C_EXT:
394     case C_HIDEXT:
395       if (indx + 1 == numaux)
396         {
397           bfd_vma temp;
398
399           temp = in->x_csect.x_scnlen.l & 0xffffffff;
400           H_PUT_32 (abfd, temp, ext->x_csect.x_scnlen_lo);
401           temp = in->x_csect.x_scnlen.l >> 32;
402           H_PUT_32 (abfd, temp, ext->x_csect.x_scnlen_hi);
403           H_PUT_32 (abfd, in->x_csect.x_parmhash, ext->x_csect.x_parmhash);
404           H_PUT_16 (abfd, in->x_csect.x_snhash, ext->x_csect.x_snhash);
405           /* We don't have to hack bitfields in x_smtyp because it's
406              defined by shifts-and-ands, which are equivalent on all
407              byte orders.  */
408           H_PUT_8 (abfd, in->x_csect.x_smtyp, ext->x_csect.x_smtyp);
409           H_PUT_8 (abfd, in->x_csect.x_smclas, ext->x_csect.x_smclas);
410           H_PUT_8 (abfd, _AUX_CSECT, ext->x_auxtype.x_auxtype);
411           goto end;
412         }
413       break;
414
415     case C_STAT:
416     case C_LEAFSTAT:
417     case C_HIDDEN:
418       if (type == T_NULL)
419         {
420           goto end;
421         }
422       break;
423     }
424
425   if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
426     {
427       H_PUT_64 (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr,
428                ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
429       H_PUT_8 (abfd, _AUX_FCN,
430                ext->x_auxtype.x_auxtype);
431       H_PUT_32 (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l,
432                ext->x_sym.x_fcnary.x_fcn.x_endndx);
433     }
434   if (ISFCN (type))
435     {
436       H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize,
437                ext->x_sym.x_fcnary.x_fcn.x_fsize);
438     }
439   else
440     {
441       H_PUT_32 (abfd, in->x_sym.x_misc.x_lnsz.x_lnno,
442                ext->x_sym.x_fcnary.x_lnsz.x_lnno);
443       H_PUT_16 (abfd, in->x_sym.x_misc.x_lnsz.x_size,
444                ext->x_sym.x_fcnary.x_lnsz.x_size);
445     }
446
447  end:
448
449   return bfd_coff_auxesz (abfd);
450 }
451
452 static boolean
453 _bfd_xcoff64_put_symbol_name (bfd *abfd, struct bfd_strtab_hash *strtab,
454                               struct internal_syment *sym,
455                               const char *name) {
456   boolean hash;
457   bfd_size_type indx;
458
459   hash = true;
460
461   if ((abfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
462     hash = false;
463
464   indx = _bfd_stringtab_add (strtab, name, hash, false);
465
466   if (indx == (bfd_size_type) -1)
467     return false;
468
469   sym->_n._n_n._n_zeroes = 0;
470   sym->_n._n_n._n_offset = STRING_SIZE_SIZE + indx;
471
472   return true;
473 }
474
475 static boolean
476 _bfd_xcoff64_put_ldsymbol_name (abfd, ldinfo, ldsym, name)
477      bfd *abfd ATTRIBUTE_UNUSED;
478      struct xcoff_loader_info *ldinfo;
479      struct internal_ldsym *ldsym;
480      const char *name;
481 {
482
483   size_t len;
484   len = strlen (name);
485
486   if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
487     {
488       bfd_size_type newalc;
489       bfd_byte *newstrings;
490
491       newalc = ldinfo->string_alc * 2;
492       if (newalc == 0)
493         newalc = 32;
494       while (ldinfo->string_size + len + 3 > newalc)
495         newalc *= 2;
496
497       newstrings = ((bfd_byte *)
498                     bfd_realloc ((PTR) ldinfo->strings, newalc));
499       if (newstrings == NULL)
500         {
501           ldinfo->failed = true;
502           return false;
503         }
504       ldinfo->string_alc = newalc;
505       ldinfo->strings = newstrings;
506     }
507
508   bfd_put_16 (ldinfo->output_bfd, (bfd_vma) (len + 1),
509               ldinfo->strings + ldinfo->string_size);
510   strcpy (ldinfo->strings + ldinfo->string_size + 2, name);
511   ldsym->_l._l_l._l_zeroes = 0;
512   ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
513   ldinfo->string_size += len + 3;
514
515   return true;
516 }
517
518 /* Routines to swap information in the XCOFF .loader section.  If we
519    ever need to write an XCOFF loader, this stuff will need to be
520    moved to another file shared by the linker (which XCOFF calls the
521    ``binder'') and the loader.  */
522
523 /* Swap in the ldhdr structure.  */
524
525 static void
526 xcoff64_swap_ldhdr_in (abfd, s, dst)
527      bfd *abfd;
528      const PTR s;
529      struct internal_ldhdr *dst;
530 {
531   const struct external_ldhdr *src = (const struct external_ldhdr *) s;
532
533   dst->l_version = bfd_get_32 (abfd, src->l_version);
534   dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
535   dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
536   dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
537   dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
538   dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
539   dst->l_impoff = bfd_get_64 (abfd, src->l_impoff);
540   dst->l_stoff = bfd_get_64 (abfd, src->l_stoff);
541   dst->l_symoff = bfd_get_64 (abfd, src->l_symoff);
542   dst->l_rldoff = bfd_get_64 (abfd, src->l_rldoff);
543 }
544
545 /* Swap out the ldhdr structure.  */
546
547 static void
548 xcoff64_swap_ldhdr_out (abfd, src, d)
549      bfd *abfd;
550      const struct internal_ldhdr *src;
551      PTR d;
552 {
553   struct external_ldhdr *dst = (struct external_ldhdr *) d;
554
555   bfd_put_32 (abfd, (bfd_vma) src->l_version, dst->l_version);
556   bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
557   bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
558   bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
559   bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
560   bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
561   bfd_put_64 (abfd, src->l_impoff, dst->l_impoff);
562   bfd_put_64 (abfd, src->l_stoff, dst->l_stoff);
563   bfd_put_64 (abfd, src->l_symoff, dst->l_symoff);
564   bfd_put_64 (abfd, src->l_rldoff, dst->l_rldoff);
565 }
566
567 /* Swap in the ldsym structure.  */
568
569 static void
570 xcoff64_swap_ldsym_in (abfd, s, dst)
571      bfd *abfd;
572      const PTR s;
573      struct internal_ldsym *dst;
574 {
575   const struct external_ldsym *src = (const struct external_ldsym *) s;
576   /* XCOFF64 does not use l_zeroes like XCOFF32
577      Set the internal l_zeroes to 0 so the common 32/64 code uses l_value
578      as an offset into the loader symbol table.  */
579   dst->_l._l_l._l_zeroes = 0;
580   dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->l_offset);
581   dst->l_value = bfd_get_64 (abfd, src->l_value);
582   dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
583   dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
584   dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
585   dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
586   dst->l_parm = bfd_get_32 (abfd, src->l_parm);
587 }
588
589 /* Swap out the ldsym structure.  */
590
591 static void
592 xcoff64_swap_ldsym_out (abfd, src, d)
593      bfd *abfd;
594      const struct internal_ldsym *src;
595      PTR d;
596 {
597   struct external_ldsym *dst = (struct external_ldsym *) d;
598
599   bfd_put_64 (abfd, src->l_value, dst->l_value);
600   bfd_put_32 (abfd, (bfd_vma) src->_l._l_l._l_offset, dst->l_offset);
601   bfd_put_16 (abfd, (bfd_vma) src->l_scnum, dst->l_scnum);
602   bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
603   bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
604   bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
605   bfd_put_32 (abfd, src->l_parm, dst->l_parm);
606 }
607
608 /* Swap in the ldrel structure.  */
609
610 static void
611 xcoff64_swap_ldrel_in (abfd, s, dst)
612      bfd *abfd;
613      const PTR s;
614      struct internal_ldrel *dst;
615 {
616   const struct external_ldrel *src = (const struct external_ldrel *) s;
617
618   dst->l_vaddr = bfd_get_64 (abfd, src->l_vaddr);
619   dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
620   dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
621   dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
622 }
623
624 /* Swap out the ldrel structure.  */
625
626 static void
627 xcoff64_swap_ldrel_out (abfd, src, d)
628      bfd *abfd;
629      const struct internal_ldrel *src;
630      PTR d;
631 {
632   struct external_ldrel *dst = (struct external_ldrel *) d;
633
634   bfd_put_64 (abfd, src->l_vaddr, dst->l_vaddr);
635   bfd_put_16 (abfd, (bfd_vma) src->l_rtype, dst->l_rtype);
636   bfd_put_16 (abfd, (bfd_vma) src->l_rsecnm, dst->l_rsecnm);
637   bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
638 }
639
640 static boolean
641 xcoff64_write_object_contents (abfd)
642      bfd * abfd;
643 {
644   asection *current;
645   boolean hasrelocs = false;
646   boolean haslinno = false;
647   file_ptr scn_base;
648   file_ptr reloc_base;
649   file_ptr lineno_base;
650   file_ptr sym_base;
651   unsigned long reloc_size = 0;
652   unsigned long lnno_size = 0;
653   boolean long_section_names;
654   asection *text_sec = ((void *) 0);
655   asection *data_sec = ((void *) 0);
656   asection *bss_sec = ((void *) 0);
657   struct internal_filehdr internal_f;
658   struct internal_aouthdr internal_a;
659
660   bfd_set_error (bfd_error_system_call);
661
662   if (abfd->output_has_begun == false)
663     {
664       if (! bfd_coff_compute_section_file_positions (abfd))
665         return false;
666     }
667
668   /* Work out the size of the reloc and linno areas */
669   reloc_base = obj_relocbase (abfd);
670
671   for (current = abfd->sections; current != NULL; current = current->next)
672     reloc_size += current->reloc_count * bfd_coff_relsz (abfd);
673
674   lineno_base = reloc_base + reloc_size;
675
676   /* Make a pass through the symbol table to count line number entries and
677      put them into the correct asections */
678   lnno_size = coff_count_linenumbers (abfd) * bfd_coff_linesz (abfd);
679
680   sym_base = lineno_base + lnno_size;
681
682   /* Indicate in each section->line_filepos its actual file address */
683   for (current = abfd->sections; current != NULL; current =  current->next)
684     {
685       if (current->lineno_count)
686         {
687           current->line_filepos = lineno_base;
688           current->moving_line_filepos = lineno_base;
689           lineno_base += current->lineno_count * bfd_coff_linesz (abfd);
690         }
691       else
692         {
693           current->line_filepos = 0;
694         }
695
696       if (current->reloc_count)
697         {
698           current->rel_filepos = reloc_base;
699           reloc_base += current->reloc_count * bfd_coff_relsz (abfd);
700         }
701       else
702         {
703           current->rel_filepos = 0;
704         }
705     }
706
707   if ((abfd->flags & EXEC_P) != 0)
708     {
709       scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
710       internal_f.f_opthdr = bfd_coff_aoutsz (abfd);
711     }
712   else
713     {
714       scn_base = bfd_coff_filhsz (abfd);
715       internal_f.f_opthdr = 0;
716     }
717
718   internal_f.f_nscns = 0;
719
720   if (bfd_seek (abfd, scn_base, SEEK_SET) != 0)
721     return false;
722
723   long_section_names = false;
724   for (current = abfd->sections; current != NULL; current = current->next)
725     {
726       struct internal_scnhdr section;
727       struct external_scnhdr buff;
728       bfd_size_type amount;
729
730       internal_f.f_nscns++;
731
732       strncpy (section.s_name, current->name, SCNNMLEN);
733
734       section.s_vaddr = current->vma;
735       section.s_paddr = current->lma;
736       section.s_size =  current->_raw_size;
737
738       /* If this section has no size or is unloadable then the scnptr
739          will be 0 too.  */
740       if (current->_raw_size == 0
741           || (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
742         {
743           section.s_scnptr = 0;
744         }
745       else
746         {
747           section.s_scnptr = current->filepos;
748         }
749
750       section.s_relptr = current->rel_filepos;
751       section.s_lnnoptr = current->line_filepos;
752       section.s_nreloc = current->reloc_count;
753
754       section.s_nlnno = current->lineno_count;
755       if (current->reloc_count != 0)
756         hasrelocs = true;
757       if (current->lineno_count != 0)
758         haslinno = true;
759
760       section.s_flags = sec_to_styp_flags (current->name, current->flags);
761
762       if (!strcmp (current->name, _TEXT))
763         {
764           text_sec = current;
765         }
766       else if (!strcmp (current->name, _DATA))
767         {
768           data_sec = current;
769         }
770       else if (!strcmp (current->name, _BSS))
771         {
772           bss_sec = current;
773         }
774
775       amount = bfd_coff_scnhsz (abfd);
776       if (bfd_coff_swap_scnhdr_out (abfd, &section, &buff) == 0
777           || bfd_bwrite ((PTR) (&buff), amount, abfd) != amount)
778         return false;
779     }
780
781   internal_f.f_timdat = 0;
782
783   internal_f.f_flags = 0;
784
785   if (!hasrelocs)
786     internal_f.f_flags |= F_RELFLG;
787   if (!haslinno)
788     internal_f.f_flags |= F_LNNO;
789   if (abfd->flags & EXEC_P)
790     internal_f.f_flags |= F_EXEC;
791
792   /* FIXME: this is wrong for PPC_PE! */
793   if (bfd_little_endian (abfd))
794     internal_f.f_flags |= F_AR32WR;
795   else
796     internal_f.f_flags |= F_AR32W;
797
798   if ((abfd->flags & DYNAMIC) != 0)
799     internal_f.f_flags |= F_SHROBJ;
800   if (bfd_get_section_by_name (abfd, _LOADER) != NULL)
801     internal_f.f_flags |= F_DYNLOAD;
802
803   memset (&internal_a, 0, sizeof internal_a);
804
805
806   /* This can only be called from the xcoff64 backend so the magic #
807      must be for xcoff64.  */
808   internal_f.f_magic = 0757;
809
810   internal_a.magic = (abfd->flags & D_PAGED) ? RS6K_AOUTHDR_ZMAGIC :
811     (abfd->flags & WP_TEXT) ? RS6K_AOUTHDR_NMAGIC :
812     RS6K_AOUTHDR_OMAGIC;
813
814   /* FIXME: Does anybody ever set this to another value?  */
815   internal_a.vstamp = 0;
816
817   /* Now should write relocs, strings, syms */
818   obj_sym_filepos (abfd) = sym_base;
819
820   internal_f.f_symptr = 0;
821   internal_f.f_nsyms = 0;
822
823   /* If bfd_get_symcount (abfd) != 0, then we are not using the COFF
824      backend linker, and obj_raw_syment_count is not valid until after
825      coff_write_symbols is called.  */
826   if (bfd_get_symcount (abfd) != 0)
827     {
828       int firstundef;
829
830       if (!coff_renumber_symbols (abfd, &firstundef))
831         return false;
832       coff_mangle_symbols (abfd);
833       if (! coff_write_symbols (abfd))
834         return false;
835       if (! coff_write_linenumbers (abfd))
836         return false;
837       if (! coff_write_relocs (abfd, firstundef))
838         return false;
839
840       internal_f.f_symptr = sym_base;
841       internal_f.f_nsyms = bfd_get_symcount (abfd);
842     }
843   else if (obj_raw_syment_count (abfd) != 0)
844     {
845       internal_f.f_symptr = sym_base;
846
847       /* AIX appears to require that F_RELFLG not be set if there are
848          local symbols but no relocations.  */
849       internal_f.f_flags &=~ F_RELFLG;
850     }
851   else
852     {
853       internal_f.f_flags |= F_LSYMS;
854     }
855
856   if (text_sec)
857     {
858       internal_a.tsize = bfd_get_section_size_before_reloc (text_sec);
859       internal_a.text_start = internal_a.tsize ? text_sec->vma : 0;
860     }
861
862   if (data_sec)
863     {
864       internal_a.dsize = bfd_get_section_size_before_reloc (data_sec);
865       internal_a.data_start = internal_a.dsize ? data_sec->vma : 0;
866     }
867
868   if (bss_sec)
869     {
870       internal_a.bsize = bfd_get_section_size_before_reloc (bss_sec);
871       if (internal_a.bsize && bss_sec->vma < internal_a.data_start)
872         internal_a.data_start = bss_sec->vma;
873     }
874
875   internal_a.entry = bfd_get_start_address (abfd);
876   internal_f.f_nsyms = obj_raw_syment_count (abfd);
877
878   if (xcoff_data (abfd)->full_aouthdr)
879     {
880       bfd_vma toc;
881       asection *loader_sec;
882
883       internal_a.vstamp = 1;
884
885       internal_a.o_snentry = xcoff_data (abfd)->snentry;
886       if (internal_a.o_snentry == 0)
887         internal_a.entry = (bfd_vma) -1;
888
889       if (text_sec != NULL)
890         {
891           internal_a.o_sntext = text_sec->target_index;
892           internal_a.o_algntext = bfd_get_section_alignment (abfd, text_sec);
893         }
894       else
895         {
896           internal_a.o_sntext = 0;
897           internal_a.o_algntext = 0;
898         }
899
900       if (data_sec != NULL)
901         {
902           internal_a.o_sndata = data_sec->target_index;
903           internal_a.o_algndata = bfd_get_section_alignment (abfd, data_sec);
904         }
905       else
906         {
907           internal_a.o_sndata = 0;
908           internal_a.o_algndata = 0;
909         }
910
911       loader_sec = bfd_get_section_by_name (abfd, ".loader");
912       if (loader_sec != NULL)
913         internal_a.o_snloader = loader_sec->target_index;
914       else
915         internal_a.o_snloader = 0;
916       if (bss_sec != NULL)
917         internal_a.o_snbss = bss_sec->target_index;
918       else
919         internal_a.o_snbss = 0;
920
921       toc = xcoff_data (abfd)->toc;
922       internal_a.o_toc = toc;
923       internal_a.o_sntoc = xcoff_data (abfd)->sntoc;
924
925       internal_a.o_modtype = xcoff_data (abfd)->modtype;
926       if (xcoff_data (abfd)->cputype != -1)
927         internal_a.o_cputype = xcoff_data (abfd)->cputype;
928       else
929         {
930           switch (bfd_get_arch (abfd))
931             {
932             case bfd_arch_rs6000:
933               internal_a.o_cputype = 4;
934               break;
935             case bfd_arch_powerpc:
936               if (bfd_get_mach (abfd) == 0)
937                 internal_a.o_cputype = 3;
938               else
939                 internal_a.o_cputype = 1;
940               break;
941             default:
942               abort ();
943             }
944         }
945       internal_a.o_maxstack = xcoff_data (abfd)->maxstack;
946       internal_a.o_maxdata = xcoff_data (abfd)->maxdata;
947     }
948
949   if (bfd_seek (abfd, (file_ptr) 0, 0) != 0)
950     return false;
951
952   {
953     char * buff;
954     bfd_size_type amount = bfd_coff_filhsz (abfd);
955
956     buff = bfd_malloc (amount);
957     if (buff == NULL)
958       return false;
959
960     bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, (PTR) buff);
961     amount = bfd_bwrite ((PTR) buff, amount, abfd);
962
963     free (buff);
964
965     if (amount != bfd_coff_filhsz (abfd))
966       return false;
967   }
968
969   if (abfd->flags & EXEC_P)
970     {
971       char * buff;
972       bfd_size_type amount = bfd_coff_aoutsz (abfd);
973
974       buff = bfd_malloc (amount);
975       if (buff == NULL)
976         return false;
977
978       bfd_coff_swap_aouthdr_out (abfd, (PTR) & internal_a, (PTR) buff);
979       amount = bfd_bwrite ((PTR) buff, amount, abfd);
980
981       free (buff);
982
983       if (amount != bfd_coff_aoutsz (abfd))
984         return false;
985     }
986
987   return true;
988 }
989
990 /* This is the relocation function for the RS/6000/POWER/PowerPC.
991    This is currently the only processor which uses XCOFF; I hope that
992    will never change.  */
993
994 static boolean
995 xcoff64_ppc_relocate_section (output_bfd, info, input_bfd,
996                               input_section, contents, relocs, syms,
997                               sections)
998      bfd *output_bfd;
999      struct bfd_link_info *info;
1000      bfd *input_bfd;
1001      asection *input_section;
1002      bfd_byte *contents;
1003      struct internal_reloc *relocs;
1004      struct internal_syment *syms;
1005      asection **sections;
1006 {
1007   struct internal_reloc *rel;
1008   struct internal_reloc *relend;
1009
1010   rel = relocs;
1011   relend = rel + input_section->reloc_count;
1012   for (; rel < relend; rel++)
1013     {
1014       long symndx;
1015       struct xcoff_link_hash_entry *h;
1016       struct internal_syment *sym;
1017       bfd_vma addend;
1018       bfd_vma val;
1019       struct reloc_howto_struct howto;
1020       bfd_reloc_status_type rstat;
1021
1022       /* Relocation type R_REF is a special relocation type which is
1023          merely used to prevent garbage collection from occurring for
1024          the csect including the symbol which it references.  */
1025       if (rel->r_type == R_REF)
1026         continue;
1027
1028       symndx = rel->r_symndx;
1029
1030       if (symndx == -1)
1031         {
1032           h = NULL;
1033           sym = NULL;
1034           addend = 0;
1035         }
1036       else
1037         {
1038           h = obj_xcoff_sym_hashes (input_bfd)[symndx];
1039           sym = syms + symndx;
1040           addend = - sym->n_value;
1041         }
1042
1043       /* We build the howto information on the fly.  */
1044
1045       howto.type = rel->r_type;
1046       howto.rightshift = 0;
1047       howto.size = 4;
1048       howto.bitsize = (rel->r_size & 0x3f) + 1;
1049       howto.pc_relative = false;
1050       howto.bitpos = 0;
1051       if ((rel->r_size & 0x80) != 0)
1052         howto.complain_on_overflow = complain_overflow_signed;
1053       else
1054         howto.complain_on_overflow = complain_overflow_bitfield;
1055       howto.special_function = NULL;
1056       howto.name = "internal";
1057       howto.partial_inplace = true;
1058
1059       if (howto.bitsize == 64)
1060         {
1061           howto.src_mask = howto.dst_mask = MINUS_ONE;
1062         }
1063       else if (howto.bitsize == 32)
1064         {
1065           howto.src_mask = howto.dst_mask = 0xffffffff;
1066         }
1067       else
1068         {
1069           howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
1070           if (howto.bitsize == 16)
1071             howto.size = 1;
1072         }
1073       howto.pcrel_offset = false;
1074
1075       val = 0;
1076
1077       if (h == NULL)
1078         {
1079           asection *sec;
1080
1081           if (symndx == -1)
1082             {
1083               sec = bfd_abs_section_ptr;
1084               val = 0;
1085             }
1086           else
1087             {
1088               sec = sections[symndx];
1089               /* Hack to make sure we use the right TOC anchor value
1090                  if this reloc is against the TOC anchor.  */
1091               if (sec->name[3] == '0'
1092                   && strcmp (sec->name, ".tc0") == 0)
1093                 val = xcoff_data (output_bfd)->toc;
1094               else
1095                 val = (sec->output_section->vma
1096                        + sec->output_offset
1097                        + sym->n_value
1098                        - sec->vma);
1099             }
1100
1101         }
1102       else
1103         {
1104
1105           if (h->root.type == bfd_link_hash_defined
1106               || h->root.type == bfd_link_hash_defweak)
1107             {
1108               asection *sec;
1109
1110               sec = h->root.u.def.section;
1111               val = (h->root.u.def.value
1112                      + sec->output_section->vma
1113                      + sec->output_offset);
1114
1115             }
1116           else if (h->root.type == bfd_link_hash_common)
1117             {
1118               asection *sec;
1119
1120               sec = h->root.u.c.p->section;
1121               val = (sec->output_section->vma
1122                      + sec->output_offset);
1123             }
1124           else if ((h->flags & XCOFF_DEF_DYNAMIC) != 0
1125                    || (h->flags & XCOFF_IMPORT) != 0)
1126             {
1127               /* Every symbol in a shared object is defined somewhere.  */
1128               val = 0;
1129             }
1130           else if (! info->relocateable)
1131             {
1132               if (! ((*info->callbacks->undefined_symbol)
1133                      (info, h->root.root.string, input_bfd, input_section,
1134                       rel->r_vaddr - input_section->vma, true)))
1135                 return false;
1136
1137               /* Don't try to process the reloc.  It can't help, and
1138                  it may generate another error.  */
1139               continue;
1140             }
1141         }
1142
1143       /* I took the relocation type definitions from two documents:
1144          the PowerPC AIX Version 4 Application Binary Interface, First
1145          Edition (April 1992), and the PowerOpen ABI, Big-Endian
1146          32-Bit Hardware Implementation (June 30, 1994).  Differences
1147          between the documents are noted below.  */
1148
1149       switch (rel->r_type)
1150         {
1151         case R_RTB:
1152         case R_RRTBI:
1153         case R_RRTBA:
1154           /* These relocs are defined by the PowerPC ABI to be
1155              relative branches which use half of the difference
1156              between the symbol and the program counter.  I can't
1157              quite figure out when this is useful.  These relocs are
1158              not defined by the PowerOpen ABI.  */
1159         default:
1160           (*_bfd_error_handler)
1161             (_("%s: unsupported relocation type 0x%02x"),
1162              bfd_archive_filename (input_bfd), (unsigned int) rel->r_type);
1163           bfd_set_error (bfd_error_bad_value);
1164           return false;
1165         case R_POS:
1166           /* Simple positive relocation.  */
1167           break;
1168         case R_NEG:
1169           /* Simple negative relocation.  */
1170           val = - val;
1171           break;
1172         case R_REL:
1173           /* Simple PC relative relocation.  */
1174           howto.pc_relative = true;
1175           break;
1176         case R_TOC:
1177           /* TOC relative relocation.  The value in the instruction in
1178              the input file is the offset from the input file TOC to
1179              the desired location.  We want the offset from the final
1180              TOC to the desired location.  We have:
1181              isym = iTOC + in
1182              iinsn = in + o
1183              osym = oTOC + on
1184              oinsn = on + o
1185              so we must change insn by on - in.
1186           */
1187         case R_GL:
1188           /* Global linkage relocation.  The value of this relocation
1189              is the address of the entry in the TOC section.  */
1190         case R_TCL:
1191           /* Local object TOC address.  I can't figure out the
1192              difference between this and case R_GL.  */
1193         case R_TRL:
1194           /* TOC relative relocation.  A TOC relative load instruction
1195              which may be changed to a load address instruction.
1196              FIXME: We don't currently implement this optimization.  */
1197         case R_TRLA:
1198           /* TOC relative relocation.  This is a TOC relative load
1199              address instruction which may be changed to a load
1200              instruction.  FIXME: I don't know if this is the correct
1201              implementation.  */
1202           if (h != NULL && h->smclas != XMC_TD)
1203             {
1204               if (h->toc_section == NULL)
1205                 {
1206                   (*_bfd_error_handler)
1207                     (_("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry"),
1208                      bfd_archive_filename (input_bfd), rel->r_vaddr,
1209                      h->root.root.string);
1210                   bfd_set_error (bfd_error_bad_value);
1211                   return false;
1212                 }
1213
1214               BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
1215               val = (h->toc_section->output_section->vma
1216                      + h->toc_section->output_offset);
1217             }
1218
1219           val = ((val - xcoff_data (output_bfd)->toc)
1220                  - (sym->n_value - xcoff_data (input_bfd)->toc));
1221
1222           addend = 0;
1223           break;
1224         case R_BA:
1225           /* Absolute branch.  We don't want to mess with the lower
1226              two bits of the instruction.  */
1227         case R_CAI:
1228           /* The PowerPC ABI defines this as an absolute call which
1229              may be modified to become a relative call.  The PowerOpen
1230              ABI does not define this relocation type.  */
1231         case R_RBA:
1232           /* Absolute branch which may be modified to become a
1233              relative branch.  */
1234         case R_RBAC:
1235           /* The PowerPC ABI defines this as an absolute branch to a
1236              fixed address which may be modified to an absolute branch
1237              to a symbol.  The PowerOpen ABI does not define this
1238              relocation type.  */
1239         case R_RBRC:
1240           /* The PowerPC ABI defines this as an absolute branch to a
1241              fixed address which may be modified to a relative branch.
1242              The PowerOpen ABI does not define this relocation type.  */
1243           howto.src_mask &= ~3;
1244           howto.dst_mask = howto.src_mask;
1245           break;
1246         case R_BR:
1247           /* Relative branch.  We don't want to mess with the lower
1248              two bits of the instruction.  */
1249         case R_CREL:
1250           /* The PowerPC ABI defines this as a relative call which may
1251              be modified to become an absolute call.  The PowerOpen
1252              ABI does not define this relocation type.  */
1253         case R_RBR:
1254           /* A relative branch which may be modified to become an
1255              absolute branch.  FIXME: We don't implement this,
1256              although we should for symbols of storage mapping class
1257              XMC_XO.  */
1258           howto.pc_relative = true;
1259           howto.src_mask &= ~3;
1260           howto.dst_mask = howto.src_mask;
1261           howto.size = 2;
1262           howto.complain_on_overflow = complain_overflow_bitfield;
1263           break;
1264         case R_RL:
1265           /* The PowerPC AIX ABI describes this as a load which may be
1266              changed to a load address.  The PowerOpen ABI says this
1267              is the same as case R_POS.  */
1268           break;
1269         case R_RLA:
1270           /* The PowerPC AIX ABI describes this as a load address
1271              which may be changed to a load.  The PowerOpen ABI says
1272              this is the same as R_POS.  */
1273           break;
1274         }
1275
1276       /* If we see an R_BR or R_RBR reloc which is jumping to global
1277          linkage code, and it is followed by an appropriate cror nop
1278          instruction, we replace the cror with ld r2,40(r1).  This
1279          restores the TOC after the glink code.  Contrariwise, if the
1280          call is followed by a ld r2,40(r1), but the call is not
1281          going to global linkage code, we can replace the load with a
1282          cror.  */
1283       if ((rel->r_type == R_BR || rel->r_type == R_RBR)
1284           && h != NULL
1285           && h->root.type == bfd_link_hash_defined
1286           && (rel->r_vaddr - input_section->vma + 8
1287               <= input_section->_cooked_size))
1288         {
1289           bfd_byte *pnext;
1290           unsigned long next;
1291
1292           pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
1293           next = bfd_get_32 (input_bfd, pnext);
1294
1295
1296           /* The _ptrgl function is magic.  It is used by the AIX
1297            * compiler to call a function through a pointer.
1298            *
1299            * special case XMC_GL, global linkage
1300            */
1301           if (h->smclas == XMC_GL
1302               || strcmp (h->root.root.string, "._ptrgl") == 0)
1303             {
1304               if (next == 0x4def7b82 /* cror 15,15,15 */
1305                   || next == 0x4ffffb82 /* cror 31,31,31 */
1306                   || next == 0x60000000 /* ori r0,r0,0 */)
1307                 bfd_put_32 (input_bfd, (bfd_vma) 0xe8410028 /* ld r2,40(r1) */,
1308                             pnext);
1309             }
1310           else
1311             {
1312               if (next == 0xe8410028 /* ld r2,40(r1) */)
1313                 bfd_put_32 (input_bfd, (bfd_vma) 0x60000000 /* ori r0,r0,0 */,
1314                             pnext);
1315             }
1316         }
1317
1318       /* A PC relative reloc includes the section address.  */
1319       if (howto.pc_relative)
1320         addend += input_section->vma;
1321
1322       rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
1323                                         contents,
1324                                         rel->r_vaddr - input_section->vma,
1325                                         val, addend);
1326
1327       switch (rstat)
1328         {
1329         default:
1330           abort ();
1331         case bfd_reloc_ok:
1332           break;
1333         case bfd_reloc_overflow:
1334           {
1335             const char *name;
1336             char buf[SYMNMLEN + 1];
1337             char howto_name[10];
1338
1339             if (symndx == -1)
1340               name = "*ABS*";
1341             else if (h != NULL)
1342               name = h->root.root.string;
1343             else
1344               {
1345                 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
1346                 if (name == NULL)
1347                   return false;
1348               }
1349             sprintf (howto_name, "0x%02x", rel->r_type);
1350
1351             if (! ((*info->callbacks->reloc_overflow)
1352                    (info, name, howto_name, (bfd_vma) 0, input_bfd,
1353                     input_section, rel->r_vaddr - input_section->vma)))
1354               return false;
1355           }
1356         }
1357     }
1358
1359   return true;
1360 }
1361
1362
1363 \f
1364 /* The XCOFF reloc table.  Actually, XCOFF relocations specify the
1365    bitsize and whether they are signed or not, along with a
1366    conventional type.  This table is for the types, which are used for
1367    different algorithms for putting in the reloc.  Many of these
1368    relocs need special_function entries, which I have not written.  */
1369
1370
1371 reloc_howto_type xcoff64_howto_table[] =
1372 {
1373   /* Standard 64 bit relocation.  */
1374   HOWTO (0,                     /* type */
1375          0,                     /* rightshift */
1376          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1377          64,                    /* bitsize */
1378          false,                 /* pc_relative */
1379          0,                     /* bitpos */
1380          complain_overflow_bitfield, /* complain_on_overflow */
1381          0,                     /* special_function */
1382          "R_POS",               /* name */
1383          true,                  /* partial_inplace */
1384          MINUS_ONE,             /* src_mask */
1385          MINUS_ONE,             /* dst_mask */
1386          false),                /* pcrel_offset */
1387
1388   /* 64 bit relocation, but store negative value.  */
1389   HOWTO (1,                     /* type */
1390          0,                     /* rightshift */
1391          -4,                    /* size (0 = byte, 1 = short, 2 = long) */
1392          64,                    /* bitsize */
1393          false,                 /* pc_relative */
1394          0,                     /* bitpos */
1395          complain_overflow_bitfield, /* complain_on_overflow */
1396          0,                     /* special_function */
1397          "R_NEG",               /* name */
1398          true,                  /* partial_inplace */
1399          MINUS_ONE,             /* src_mask */
1400          MINUS_ONE,             /* dst_mask */
1401          false),                /* pcrel_offset */
1402
1403   /* 32 bit PC relative relocation.  */
1404   HOWTO (2,                     /* type */
1405          0,                     /* rightshift */
1406          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1407          32,                    /* bitsize */
1408          true,                  /* pc_relative */
1409          0,                     /* bitpos */
1410          complain_overflow_signed, /* complain_on_overflow */
1411          0,                     /* special_function */
1412          "R_REL",               /* name */
1413          true,                  /* partial_inplace */
1414          0xffffffff,            /* src_mask */
1415          0xffffffff,            /* dst_mask */
1416          false),                /* pcrel_offset */
1417
1418   /* 16 bit TOC relative relocation.  */
1419   HOWTO (3,                     /* type */
1420          0,                     /* rightshift */
1421          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1422          16,                    /* bitsize */
1423          false,                 /* pc_relative */
1424          0,                     /* bitpos */
1425          complain_overflow_bitfield, /* complain_on_overflow */
1426          0,                     /* special_function */
1427          "R_TOC",               /* name */
1428          true,                  /* partial_inplace */
1429          0xffff,                /* src_mask */
1430          0xffff,                /* dst_mask */
1431          false),                /* pcrel_offset */
1432
1433   /* I don't really know what this is.  */
1434   HOWTO (4,                     /* type */
1435          1,                     /* rightshift */
1436          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1437          32,                    /* bitsize */
1438          false,                 /* pc_relative */
1439          0,                     /* bitpos */
1440          complain_overflow_bitfield, /* complain_on_overflow */
1441          0,                     /* special_function */
1442          "R_RTB",               /* name */
1443          true,                  /* partial_inplace */
1444          0xffffffff,            /* src_mask */
1445          0xffffffff,            /* dst_mask */
1446          false),                /* pcrel_offset */
1447
1448   /* External TOC relative symbol.  */
1449   HOWTO (5,                     /* type */
1450          0,                     /* rightshift */
1451          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1452          16,                    /* bitsize */
1453          false,                 /* pc_relative */
1454          0,                     /* bitpos */
1455          complain_overflow_bitfield, /* complain_on_overflow */
1456          0,                     /* special_function */
1457          "R_GL",                /* name */
1458          true,                  /* partial_inplace */
1459          0xffff,                /* src_mask */
1460          0xffff,                /* dst_mask */
1461          false),                /* pcrel_offset */
1462
1463   /* Local TOC relative symbol.  */
1464   HOWTO (6,                     /* type */
1465          0,                     /* rightshift */
1466          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1467          16,                    /* bitsize */
1468          false,                 /* pc_relative */
1469          0,                     /* bitpos */
1470          complain_overflow_bitfield, /* complain_on_overflow */
1471          0,                     /* special_function */
1472          "R_TCL",               /* name */
1473          true,                  /* partial_inplace */
1474          0xffff,                /* src_mask */
1475          0xffff,                /* dst_mask */
1476          false),                /* pcrel_offset */
1477
1478   EMPTY_HOWTO (7),
1479
1480   /* Non modifiable absolute branch.  */
1481   HOWTO (8,                     /* type */
1482          0,                     /* rightshift */
1483          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1484          26,                    /* bitsize */
1485          false,                 /* pc_relative */
1486          0,                     /* bitpos */
1487          complain_overflow_bitfield, /* complain_on_overflow */
1488          0,                     /* special_function */
1489          "R_BA",                /* name */
1490          true,                  /* partial_inplace */
1491          0x3fffffc,             /* src_mask */
1492          0x3fffffc,             /* dst_mask */
1493          false),                /* pcrel_offset */
1494
1495   EMPTY_HOWTO (9),
1496
1497   /* Non modifiable relative branch.  */
1498   HOWTO (0xa,                   /* type */
1499          0,                     /* rightshift */
1500          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1501          26,                    /* bitsize */
1502          true,                  /* pc_relative */
1503          0,                     /* bitpos */
1504          complain_overflow_signed, /* complain_on_overflow */
1505          0,                     /* special_function */
1506          "R_BR",                /* name */
1507          true,                  /* partial_inplace */
1508          0x3fffffc,             /* src_mask */
1509          0x3fffffc,             /* dst_mask */
1510          false),                /* pcrel_offset */
1511
1512   EMPTY_HOWTO (0xb),
1513
1514   /* Indirect load.  */
1515   HOWTO (0xc,                   /* type */
1516          0,                     /* rightshift */
1517          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1518          16,                    /* bitsize */
1519          false,                 /* pc_relative */
1520          0,                     /* bitpos */
1521          complain_overflow_bitfield, /* complain_on_overflow */
1522          0,                     /* special_function */
1523          "R_RL",                /* name */
1524          true,                  /* partial_inplace */
1525          0xffff,                /* src_mask */
1526          0xffff,                /* dst_mask */
1527          false),                /* pcrel_offset */
1528
1529   /* Load address.  */
1530   HOWTO (0xd,                   /* type */
1531          0,                     /* rightshift */
1532          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1533          16,                    /* bitsize */
1534          false,                 /* pc_relative */
1535          0,                     /* bitpos */
1536          complain_overflow_bitfield, /* complain_on_overflow */
1537          0,                     /* special_function */
1538          "R_RLA",               /* name */
1539          true,                  /* partial_inplace */
1540          0xffff,                /* src_mask */
1541          0xffff,                /* dst_mask */
1542          false),                /* pcrel_offset */
1543
1544   EMPTY_HOWTO (0xe),
1545
1546   /* Non-relocating reference.  */
1547   HOWTO (0xf,                   /* type */
1548          0,                     /* rightshift */
1549          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1550          32,                    /* bitsize */
1551          false,                 /* pc_relative */
1552          0,                     /* bitpos */
1553          complain_overflow_bitfield, /* complain_on_overflow */
1554          0,                     /* special_function */
1555          "R_REF",               /* name */
1556          false,                 /* partial_inplace */
1557          0,                     /* src_mask */
1558          0,                     /* dst_mask */
1559          false),                /* pcrel_offset */
1560
1561   EMPTY_HOWTO (0x10),
1562   EMPTY_HOWTO (0x11),
1563
1564   /* TOC relative indirect load.  */
1565   HOWTO (0x12,                  /* type */
1566          0,                     /* rightshift */
1567          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1568          16,                    /* bitsize */
1569          false,                 /* pc_relative */
1570          0,                     /* bitpos */
1571          complain_overflow_bitfield, /* complain_on_overflow */
1572          0,                     /* special_function */
1573          "R_TRL",               /* name */
1574          true,                  /* partial_inplace */
1575          0xffff,                /* src_mask */
1576          0xffff,                /* dst_mask */
1577          false),                /* pcrel_offset */
1578
1579   /* TOC relative load address.  */
1580   HOWTO (0x13,                  /* type */
1581          0,                     /* rightshift */
1582          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1583          16,                    /* bitsize */
1584          false,                 /* pc_relative */
1585          0,                     /* bitpos */
1586          complain_overflow_bitfield, /* complain_on_overflow */
1587          0,                     /* special_function */
1588          "R_TRLA",              /* name */
1589          true,                  /* partial_inplace */
1590          0xffff,                /* src_mask */
1591          0xffff,                /* dst_mask */
1592          false),                /* pcrel_offset */
1593
1594   /* Modifiable relative branch.  */
1595   HOWTO (0x14,                  /* type */
1596          1,                     /* rightshift */
1597          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1598          32,                    /* bitsize */
1599          false,                 /* pc_relative */
1600          0,                     /* bitpos */
1601          complain_overflow_bitfield, /* complain_on_overflow */
1602          0,                     /* special_function */
1603          "R_RRTBI",             /* name */
1604          true,                  /* partial_inplace */
1605          0xffffffff,            /* src_mask */
1606          0xffffffff,            /* dst_mask */
1607          false),                /* pcrel_offset */
1608
1609   /* Modifiable absolute branch.  */
1610   HOWTO (0x15,                  /* type */
1611          1,                     /* rightshift */
1612          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1613          32,                    /* bitsize */
1614          false,                 /* pc_relative */
1615          0,                     /* bitpos */
1616          complain_overflow_bitfield, /* complain_on_overflow */
1617          0,                     /* special_function */
1618          "R_RRTBA",             /* name */
1619          true,                  /* partial_inplace */
1620          0xffffffff,            /* src_mask */
1621          0xffffffff,            /* dst_mask */
1622          false),                /* pcrel_offset */
1623
1624   /* Modifiable call absolute indirect.  */
1625   HOWTO (0x16,                  /* type */
1626          0,                     /* rightshift */
1627          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1628          16,                    /* bitsize */
1629          false,                 /* pc_relative */
1630          0,                     /* bitpos */
1631          complain_overflow_bitfield, /* complain_on_overflow */
1632          0,                     /* special_function */
1633          "R_CAI",               /* name */
1634          true,                  /* partial_inplace */
1635          0xffff,                /* src_mask */
1636          0xffff,                /* dst_mask */
1637          false),                /* pcrel_offset */
1638
1639   /* Modifiable call relative.  */
1640   HOWTO (0x17,                  /* type */
1641          0,                     /* rightshift */
1642          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1643          16,                    /* bitsize */
1644          false,                 /* pc_relative */
1645          0,                     /* bitpos */
1646          complain_overflow_bitfield, /* complain_on_overflow */
1647          0,                     /* special_function */
1648          "R_CREL",              /* name */
1649          true,                  /* partial_inplace */
1650          0xffff,                /* src_mask */
1651          0xffff,                /* dst_mask */
1652          false),                /* pcrel_offset */
1653
1654   /* Modifiable branch absolute.  */
1655   HOWTO (0x18,                  /* type */
1656          0,                     /* rightshift */
1657          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1658          26,                    /* bitsize */
1659          false,                 /* pc_relative */
1660          0,                     /* bitpos */
1661          complain_overflow_bitfield, /* complain_on_overflow */
1662          0,                     /* special_function */
1663          "R_RBA",               /* name */
1664          true,                  /* partial_inplace */
1665          0xffff,                /* src_mask */
1666          0xffff,                /* dst_mask */
1667          false),                /* pcrel_offset */
1668
1669   /* Modifiable branch absolute.  */
1670   HOWTO (0x19,                  /* type */
1671          0,                     /* rightshift */
1672          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1673          32,                    /* bitsize */
1674          false,                 /* pc_relative */
1675          0,                     /* bitpos */
1676          complain_overflow_bitfield, /* complain_on_overflow */
1677          0,                     /* special_function */
1678          "R_RBAC",              /* name */
1679          true,                  /* partial_inplace */
1680          0xffff,                /* src_mask */
1681          0xffff,                /* dst_mask */
1682          false),                /* pcrel_offset */
1683
1684   /* Modifiable branch relative.  */
1685   HOWTO (0x1a,                  /* type */
1686          0,                     /* rightshift */
1687          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1688          26,                    /* bitsize */
1689          false,                 /* pc_relative */
1690          0,                     /* bitpos */
1691          complain_overflow_signed, /* complain_on_overflow */
1692          0,                     /* special_function */
1693          "R_RBR",               /* name */
1694          true,                  /* partial_inplace */
1695          0xffff,                /* src_mask */
1696          0xffff,                /* dst_mask */
1697          false),                /* pcrel_offset */
1698
1699   /* Modifiable branch absolute.  */
1700   HOWTO (0x1b,                  /* type */
1701          0,                     /* rightshift */
1702          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1703          16,                    /* bitsize */
1704          false,                 /* pc_relative */
1705          0,                     /* bitpos */
1706          complain_overflow_bitfield, /* complain_on_overflow */
1707          0,                     /* special_function */
1708          "R_RBRC",              /* name */
1709          true,                  /* partial_inplace */
1710          0xffff,                /* src_mask */
1711          0xffff,                /* dst_mask */
1712          false),                /* pcrel_offset */
1713
1714   HOWTO (0,                     /* type */
1715          0,                     /* rightshift */
1716          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1717          64,                    /* bitsize */
1718          false,                 /* pc_relative */
1719          0,                     /* bitpos */
1720          complain_overflow_bitfield, /* complain_on_overflow */
1721          0,                     /* special_function */
1722          "R_POS",               /* name */
1723          true,                  /* partial_inplace */
1724          MINUS_ONE,             /* src_mask */
1725          MINUS_ONE,             /* dst_mask */
1726          false)                 /* pcrel_offset */
1727 };
1728
1729 void
1730 xcoff64_rtype2howto (relent, internal)
1731      arelent *relent;
1732      struct internal_reloc *internal;
1733 {
1734   relent->howto = xcoff64_howto_table + internal->r_type;
1735
1736   /* Check for relocs we don't know of.  */
1737   if (internal->r_type
1738       >= sizeof (xcoff64_howto_table) / sizeof (xcoff64_howto_table[0]))
1739     abort ();
1740   if (internal->r_type != relent->howto->type)
1741     abort ();
1742
1743   /* The r_size field of an XCOFF reloc encodes the bitsize of the
1744      relocation, as well as indicating whether it is signed or not.
1745      Doublecheck that the relocation information gathered from the
1746      type matches this information.  The bitsize is not significant
1747      for R_REF relocs.  */
1748   if (relent->howto->dst_mask != 0
1749       && (relent->howto->bitsize
1750           != ((unsigned int) internal->r_size & 0x3f) + 1))
1751     abort ();
1752 #if 0
1753   if ((internal->r_size & 0x80) != 0
1754       ? (relent->howto->complain_on_overflow != complain_overflow_signed)
1755       : (relent->howto->complain_on_overflow != complain_overflow_bitfield))
1756     abort ();
1757 #endif
1758 }
1759
1760 reloc_howto_type *
1761 xcoff64_reloc_type_lookup (abfd, code)
1762      bfd *abfd ATTRIBUTE_UNUSED;
1763      bfd_reloc_code_real_type code;
1764 {
1765   switch (code)
1766     {
1767     case BFD_RELOC_PPC_B26:
1768       return &xcoff64_howto_table[0xa];
1769     case BFD_RELOC_PPC_BA26:
1770       return &xcoff64_howto_table[8];
1771     case BFD_RELOC_PPC_TOC16:
1772       return &xcoff64_howto_table[3];
1773     case BFD_RELOC_32:
1774     case BFD_RELOC_CTOR:
1775       return &xcoff64_howto_table[0];
1776     case BFD_RELOC_64:
1777       return &xcoff64_howto_table[0x1c];
1778     default:
1779       return NULL;
1780     }
1781 }
1782
1783
1784
1785 /* Read in the armap of an XCOFF archive.  */
1786
1787 static boolean
1788 xcoff64_slurp_armap (abfd)
1789      bfd *abfd;
1790 {
1791   file_ptr off;
1792   size_t namlen;
1793   bfd_size_type sz, amt;
1794   bfd_byte *contents, *cend;
1795   bfd_vma c, i;
1796   carsym *arsym;
1797   bfd_byte *p;
1798   file_ptr pos;
1799
1800   /* This is for the new format.  */
1801   struct xcoff_ar_hdr_big hdr;
1802
1803   if (xcoff_ardata (abfd) == NULL)
1804     {
1805       bfd_has_map (abfd) = false;
1806       return true;
1807     }
1808
1809   off = strtol (xcoff_ardata_big (abfd)->symoff64, (char **) NULL, 10);
1810   if (off == 0)
1811     {
1812       bfd_has_map (abfd) = false;
1813       return true;
1814     }
1815
1816   if (bfd_seek (abfd, off, SEEK_SET) != 0)
1817     return false;
1818
1819   /* The symbol table starts with a normal archive header.  */
1820   if (bfd_bread ((PTR) &hdr, (bfd_size_type) SIZEOF_AR_HDR_BIG, abfd)
1821       != SIZEOF_AR_HDR_BIG)
1822     return false;
1823
1824   /* Skip the name (normally empty).  */
1825   namlen = strtol (hdr.namlen, (char **) NULL, 10);
1826   pos = ((namlen + 1) & ~(size_t) 1) + SXCOFFARFMAG;
1827   if (bfd_seek (abfd, pos, SEEK_CUR) != 0)
1828     return false;
1829
1830   /* XXX This actually has to be a call to strtoll (at least on 32-bit
1831      machines) since the field width is 20 and there numbers with more
1832      than 32 bits can be represented.  */
1833   sz = strtol (hdr.size, (char **) NULL, 10);
1834
1835   /* Read in the entire symbol table.  */
1836   contents = (bfd_byte *) bfd_alloc (abfd, sz);
1837   if (contents == NULL)
1838     return false;
1839   if (bfd_bread ((PTR) contents, sz, abfd) != sz)
1840     return false;
1841
1842   /* The symbol table starts with an eight byte count.  */
1843   c = H_GET_64 (abfd, contents);
1844
1845   if (c * 8 >= sz)
1846     {
1847       bfd_set_error (bfd_error_bad_value);
1848       return false;
1849     }
1850   amt = c;
1851   amt *= sizeof (carsym);
1852   bfd_ardata (abfd)->symdefs = (carsym *) bfd_alloc (abfd, amt);
1853   if (bfd_ardata (abfd)->symdefs == NULL)
1854     return false;
1855
1856   /* After the count comes a list of eight byte file offsets.  */
1857   for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 8;
1858        i < c;
1859        ++i, ++arsym, p += 8)
1860     arsym->file_offset = H_GET_64 (abfd, p);
1861
1862   /* After the file offsets come null terminated symbol names.  */
1863   cend = contents + sz;
1864   for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
1865        i < c;
1866        ++i, ++arsym, p += strlen ((char *) p) + 1)
1867     {
1868       if (p >= cend)
1869         {
1870           bfd_set_error (bfd_error_bad_value);
1871           return false;
1872         }
1873       arsym->name = (char *) p;
1874     }
1875
1876   bfd_ardata (abfd)->symdef_count = c;
1877   bfd_has_map (abfd) = true;
1878
1879   return true;
1880 }
1881
1882
1883 /* See if this is an NEW XCOFF archive.  */
1884
1885 static const bfd_target *
1886 xcoff64_archive_p (abfd)
1887      bfd *abfd;
1888 {
1889   char magic[SXCOFFARMAG];
1890   /* This is the new format.  */
1891   struct xcoff_ar_file_hdr_big hdr;
1892   bfd_size_type amt = SXCOFFARMAG;
1893
1894   if (bfd_bread ((PTR) magic, amt, abfd) != amt)
1895     {
1896       if (bfd_get_error () != bfd_error_system_call)
1897         bfd_set_error (bfd_error_wrong_format);
1898       return NULL;
1899     }
1900
1901   if (strncmp (magic, XCOFFARMAGBIG, SXCOFFARMAG) != 0)
1902     {
1903       bfd_set_error (bfd_error_wrong_format);
1904       return NULL;
1905     }
1906
1907   /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
1908      involves a cast, we can't do it as the left operand of
1909      assignment.  */
1910   amt = sizeof (struct artdata);
1911   abfd->tdata.aout_ar_data = (struct artdata *) bfd_zalloc (abfd, amt);
1912
1913   if (bfd_ardata (abfd) == (struct artdata *) NULL)
1914     return NULL;
1915
1916   bfd_ardata (abfd)->cache = NULL;
1917   bfd_ardata (abfd)->archive_head = NULL;
1918   bfd_ardata (abfd)->symdefs = NULL;
1919   bfd_ardata (abfd)->extended_names = NULL;
1920
1921   /* Copy over the magic string.  */
1922   memcpy (hdr.magic, magic, SXCOFFARMAG);
1923
1924   /* Now read the rest of the file header.  */
1925   if (bfd_bread ((PTR) &hdr.memoff,
1926                 (bfd_size_type) (SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG),
1927                 abfd) != SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG)
1928     {
1929       if (bfd_get_error () != bfd_error_system_call)
1930         bfd_set_error (bfd_error_wrong_format);
1931       return NULL;
1932     }
1933
1934   /* XXX This actually has to be a call to strtoll (at least on 32-bit
1935      machines) since the field width is 20 and there numbers with more
1936      than 32 bits can be represented.  */
1937   bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff,
1938                                                   (char **) NULL, 10);
1939
1940   amt = SIZEOF_AR_FILE_HDR_BIG;
1941   bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
1942   if (bfd_ardata (abfd)->tdata == NULL)
1943     return NULL;
1944
1945   memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR_BIG);
1946
1947   if (! xcoff64_slurp_armap (abfd))
1948     {
1949       bfd_release (abfd, bfd_ardata (abfd));
1950       abfd->tdata.aout_ar_data = (struct artdata *) NULL;
1951       return NULL;
1952     }
1953
1954   return abfd->xvec;
1955 }
1956
1957
1958 /* Open the next element in an XCOFF archive.  */
1959
1960 static bfd *
1961 xcoff64_openr_next_archived_file (archive, last_file)
1962      bfd *archive;
1963      bfd *last_file;
1964 {
1965   file_ptr filestart;
1966
1967   if ((xcoff_ardata (archive) == NULL)
1968       || ! xcoff_big_format_p (archive))
1969     {
1970       bfd_set_error (bfd_error_invalid_operation);
1971       return NULL;
1972     }
1973
1974   if (last_file == NULL)
1975     {
1976       filestart = bfd_ardata (archive)->first_file_filepos;
1977     }
1978   else
1979     {
1980       /* XXX These actually have to be a calls to strtoll (at least
1981          on 32-bit machines) since the fields's width is 20 and
1982          there numbers with more than 32 bits can be represented.  */
1983       filestart = strtol (arch_xhdr_big (last_file)->nextoff, (char **) NULL,
1984                           10);
1985     }
1986   /* XXX These actually have to be calls to strtoll (at least on 32-bit
1987      machines) since the fields's width is 20 and there numbers with more
1988      than 32 bits can be represented.  */
1989   if (filestart == 0
1990       || filestart == strtol (xcoff_ardata_big (archive)->memoff,
1991                               (char **) NULL, 10)
1992       || filestart == strtol (xcoff_ardata_big (archive)->symoff,
1993                               (char **) NULL, 10))
1994     {
1995       bfd_set_error (bfd_error_no_more_archived_files);
1996       return NULL;
1997     }
1998
1999   return _bfd_get_elt_at_filepos (archive, filestart);
2000 }
2001
2002 /* We can't use the usual coff_sizeof_headers routine, because AIX
2003    always uses an a.out header.  */
2004
2005 /*ARGSUSED*/
2006 static int
2007 xcoff64_sizeof_headers (abfd, reloc)
2008      bfd *abfd;
2009      boolean reloc ATTRIBUTE_UNUSED;
2010 {
2011   int size;
2012
2013   size = bfd_coff_filhsz (abfd);
2014
2015   /* Don't think the small aout header can be used since some of the
2016      old elements have been reordered past the end of the old coff
2017      small aout size.  */
2018
2019   if (xcoff_data (abfd)->full_aouthdr)
2020     size += bfd_coff_aoutsz (abfd);
2021
2022   size += abfd->section_count * bfd_coff_scnhsz (abfd);
2023   return size;
2024 }
2025
2026
2027
2028 static asection *
2029 xcoff64_create_csect_from_smclas (abfd, aux, symbol_name)
2030      bfd *abfd;
2031      union internal_auxent *aux;
2032      const char *symbol_name;
2033 {
2034   asection *return_value = NULL;
2035
2036   /* Changes from 32 :
2037      .sv == 8, is only for 32 bit programs
2038      .ti == 12 and .tb == 13 are now reserved.  */
2039   static const char *names[19] =
2040   {
2041     ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
2042     NULL, ".bs", ".ds", ".uc", NULL,  NULL,  NULL,  ".tc0",
2043     ".td", ".sv64", ".sv3264"
2044   };
2045
2046   if ((19 >= aux->x_csect.x_smclas)
2047       && (NULL != names[aux->x_csect.x_smclas]))
2048     {
2049
2050       return_value = bfd_make_section_anyway
2051         (abfd, names[aux->x_csect.x_smclas]);
2052
2053     }
2054   else
2055     {
2056       (*_bfd_error_handler)
2057         (_("%s: symbol `%s' has unrecognized smclas %d"),
2058          bfd_archive_filename (abfd), symbol_name, aux->x_csect.x_smclas);
2059       bfd_set_error (bfd_error_bad_value);
2060     }
2061
2062   return return_value;
2063 }
2064
2065 static boolean
2066 xcoff64_is_lineno_count_overflow (abfd, value)
2067      bfd *abfd ATTRIBUTE_UNUSED;
2068      bfd_vma value ATTRIBUTE_UNUSED;
2069 {
2070   return false;
2071 }
2072
2073 static boolean
2074 xcoff64_is_reloc_count_overflow (abfd, value)
2075      bfd *abfd ATTRIBUTE_UNUSED;
2076      bfd_vma value ATTRIBUTE_UNUSED;
2077 {
2078   return false;
2079 }
2080
2081 static bfd_vma
2082 xcoff64_loader_symbol_offset (abfd, ldhdr)
2083      bfd *abfd ATTRIBUTE_UNUSED;
2084      struct internal_ldhdr *ldhdr;
2085 {
2086   return (ldhdr->l_symoff);
2087 }
2088
2089 static bfd_vma
2090 xcoff64_loader_reloc_offset (abfd, ldhdr)
2091      bfd *abfd ATTRIBUTE_UNUSED;
2092      struct internal_ldhdr *ldhdr;
2093 {
2094   return (ldhdr->l_rldoff);
2095 }
2096
2097 static boolean 
2098 xcoff64_generate_rtinit  (abfd, init, fini)
2099      bfd *abfd;
2100      const char *init;
2101      const char *fini;
2102 {
2103   bfd_byte filehdr_ext[FILHSZ];
2104   bfd_byte scnhdr_ext[SCNHSZ];
2105   bfd_byte syment_ext[SYMESZ * 8];
2106   bfd_byte reloc_ext[RELSZ * 2];
2107   bfd_byte *data_buffer;
2108   bfd_size_type data_buffer_size;
2109   bfd_byte *string_table, *st_tmp;
2110   bfd_size_type string_table_size;
2111   bfd_vma val;
2112   size_t initsz, finisz;
2113   struct internal_filehdr filehdr;
2114   struct internal_scnhdr scnhdr;
2115   struct internal_syment syment;
2116   union internal_auxent auxent;
2117   struct internal_reloc reloc;
2118   
2119   char *data_name = ".data";
2120   char *rtinit_name = "__rtinit";
2121   
2122   if (! bfd_xcoff_rtinit_size (abfd) 
2123       || (init == NULL && fini == NULL))
2124     return false;
2125
2126   initsz = (init == NULL ? 0 : 1 + strlen (init));
2127   finisz = (fini == NULL ? 0 : 1 + strlen (fini));
2128
2129   /* file header */
2130   memset (filehdr_ext, 0, FILHSZ);
2131   memset (&filehdr, 0, sizeof (struct internal_filehdr));
2132   filehdr.f_magic = bfd_xcoff_magic_number (abfd);
2133   filehdr.f_nscns = 1; 
2134   filehdr.f_timdat = 0;
2135   filehdr.f_nsyms = 0;  /* at least 6, no more than 8 */
2136   filehdr.f_symptr = 0; /* set below */
2137   filehdr.f_opthdr = 0;
2138   filehdr.f_flags = 0;
2139
2140   /* section header */
2141   memset (scnhdr_ext, 0, SCNHSZ);
2142   memset (&scnhdr, 0, sizeof (struct internal_scnhdr));
2143   memcpy (scnhdr.s_name, data_name, strlen (data_name));
2144   scnhdr.s_paddr = 0;
2145   scnhdr.s_vaddr = 0;
2146   scnhdr.s_size = 0;    /* set below */
2147   scnhdr.s_scnptr = FILHSZ + SCNHSZ;
2148   scnhdr.s_relptr = 0;  /* set below */
2149   scnhdr.s_lnnoptr = 0;
2150   scnhdr.s_nreloc = 0;  /* either 1 or 2 */
2151   scnhdr.s_nlnno = 0;
2152   scnhdr.s_flags = STYP_DATA;
2153
2154   /* .data 
2155      0x0000           0x00000000 : rtl
2156      0x0004           0x00000000 :
2157      0x0008           0x00000018 : offset to init, or 0
2158      0x000C           0x00000038 : offset to fini, or 0
2159      0x0010           0x00000010 : size of descriptor 
2160      0x0014           0x00000000 : pad
2161      0x0018           0x00000000 : init, needs a reloc
2162      0x001C           0x00000000 :
2163      0x0020           0x00000058 : offset to init name
2164      0x0024           0x00000000 : flags, padded to a word
2165      0x0028           0x00000000 : empty init
2166      0x002C           0x00000000 :
2167      0x0030           0x00000000 : 
2168      0x0034           0x00000000 : 
2169      0x0038           0x00000000 : fini, needs a reloc
2170      0x003C           0x00000000 :
2171      0x0040           0x00000??? : offset to fini name
2172      0x0044           0x00000000 : flags, padded to a word
2173      0x0048           0x00000000 : empty fini
2174      0x004C           0x00000000 :
2175      0x0050           0x00000000 : 
2176      0x0054           0x00000000 : 
2177      0x0058           init name
2178      0x0058 + initsz  fini name */
2179
2180   data_buffer_size = 0x0058 + initsz + finisz;
2181   data_buffer_size += (data_buffer_size & 7) ? 8 - (data_buffer_size & 7) : 0;
2182   data_buffer = (bfd_byte *)bfd_malloc (data_buffer_size);
2183   memset (data_buffer, 0, data_buffer_size);
2184
2185   if (initsz) 
2186     {
2187       val = 0x18;
2188       bfd_put_32 (abfd, val, &data_buffer[0x08]);
2189       val = 0x58;
2190       bfd_put_32 (abfd, val, &data_buffer[0x20]);
2191       memcpy (&data_buffer[val], init, initsz);
2192     }
2193
2194   if (finisz) 
2195     {
2196       val = 0x38;
2197       bfd_put_32 (abfd, val, &data_buffer[0x0C]);
2198       val = 0x58 + initsz;
2199       bfd_put_32 (abfd, val, &data_buffer[0x40]);
2200       memcpy (&data_buffer[val], fini, finisz);
2201     }
2202
2203   val = 0x10;
2204   bfd_put_32 (abfd, val, &data_buffer[0x10]);
2205   scnhdr.s_size = data_buffer_size;
2206
2207   /* string table */
2208   string_table_size = 4;
2209   string_table_size += strlen (data_name) + 1;
2210   string_table_size += strlen (rtinit_name) + 1;
2211   string_table_size += initsz;
2212   string_table_size += finisz;
2213
2214   string_table = (bfd_byte *)bfd_malloc (string_table_size);
2215   memset (string_table, 0, string_table_size);
2216   val = string_table_size;
2217   bfd_put_32 (abfd, val, &string_table[0]);
2218   st_tmp = string_table + 4;
2219   
2220   /* symbols 
2221      0. .data csect
2222      2. __rtinit
2223      4. init function 
2224      6. fini function */
2225   memset (syment_ext, 0, 8 * SYMESZ);
2226   memset (reloc_ext, 0, 2 * RELSZ);
2227
2228   /* .data csect */
2229   memset (&syment, 0, sizeof (struct internal_syment));
2230   memset (&auxent, 0, sizeof (union internal_auxent));
2231
2232   syment._n._n_n._n_offset = st_tmp - string_table;
2233   memcpy (st_tmp, data_name, strlen (data_name));
2234   st_tmp += strlen (data_name) + 1;
2235
2236   syment.n_scnum = 1;
2237   syment.n_sclass = C_HIDEXT;
2238   syment.n_numaux = 1;
2239   auxent.x_csect.x_scnlen.l = data_buffer_size;
2240   auxent.x_csect.x_smtyp = 3 << 3 | XTY_SD;
2241   auxent.x_csect.x_smclas = XMC_RW;
2242   bfd_coff_swap_sym_out (abfd, &syment, 
2243                          &syment_ext[filehdr.f_nsyms * SYMESZ]);
2244   bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0, 
2245                          syment.n_numaux, 
2246                          &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
2247   filehdr.f_nsyms += 2;
2248
2249   /* __rtinit */
2250   memset (&syment, 0, sizeof (struct internal_syment));
2251   memset (&auxent, 0, sizeof (union internal_auxent));
2252   syment._n._n_n._n_offset = st_tmp - string_table;
2253   memcpy (st_tmp, rtinit_name, strlen (rtinit_name));
2254   st_tmp += strlen (rtinit_name) + 1;
2255   
2256   syment.n_scnum = 1;
2257   syment.n_sclass = C_EXT;
2258   syment.n_numaux = 1;
2259   auxent.x_csect.x_smtyp = XTY_LD;
2260   auxent.x_csect.x_smclas = XMC_RW;
2261   bfd_coff_swap_sym_out (abfd, &syment, 
2262                          &syment_ext[filehdr.f_nsyms * SYMESZ]);
2263   bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0, 
2264                          syment.n_numaux, 
2265                          &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
2266   filehdr.f_nsyms += 2;
2267
2268   /* init */
2269   if (initsz) 
2270     {
2271       memset (&syment, 0, sizeof (struct internal_syment));
2272       memset (&auxent, 0, sizeof (union internal_auxent));
2273
2274       syment._n._n_n._n_offset = st_tmp - string_table;
2275       memcpy (st_tmp, init, initsz);
2276       st_tmp += initsz;
2277
2278       syment.n_sclass = C_EXT;
2279       syment.n_numaux = 1;
2280       bfd_coff_swap_sym_out (abfd, &syment, 
2281                              &syment_ext[filehdr.f_nsyms * SYMESZ]);
2282       bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0, 
2283                              syment.n_numaux, 
2284                              &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
2285       /* reloc */
2286       memset (&reloc, 0, sizeof (struct internal_reloc));
2287       reloc.r_vaddr = 0x0018;
2288       reloc.r_symndx = filehdr.f_nsyms;
2289       reloc.r_type = R_POS;
2290       reloc.r_size = 63;
2291       bfd_coff_swap_reloc_out (abfd, &reloc, &reloc_ext[0]);
2292
2293       filehdr.f_nsyms += 2;
2294       scnhdr.s_nreloc += 1;
2295     }
2296
2297   /* finit */
2298   if (finisz) 
2299     {
2300       memset (&syment, 0, sizeof (struct internal_syment));
2301       memset (&auxent, 0, sizeof (union internal_auxent));
2302
2303       syment._n._n_n._n_offset = st_tmp - string_table;
2304       memcpy (st_tmp, fini, finisz);
2305       st_tmp += finisz;
2306
2307       syment.n_sclass = C_EXT;
2308       syment.n_numaux = 1;
2309       bfd_coff_swap_sym_out (abfd, &syment, 
2310                              &syment_ext[filehdr.f_nsyms * SYMESZ]);
2311       bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0, 
2312                              syment.n_numaux, 
2313                              &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
2314
2315       /* reloc */
2316       memset (&reloc, 0, sizeof (struct internal_reloc));
2317       reloc.r_vaddr = 0x0038;
2318       reloc.r_symndx = filehdr.f_nsyms;
2319       reloc.r_type = R_POS;
2320       reloc.r_size = 63;
2321       bfd_coff_swap_reloc_out (abfd, &reloc, 
2322                                &reloc_ext[scnhdr.s_nreloc * RELSZ]);
2323
2324       filehdr.f_nsyms += 2;
2325       scnhdr.s_nreloc += 1;
2326     }
2327
2328   scnhdr.s_relptr = scnhdr.s_scnptr + data_buffer_size;
2329   filehdr.f_symptr = scnhdr.s_relptr + scnhdr.s_nreloc * RELSZ;
2330
2331   bfd_coff_swap_filehdr_out (abfd, &filehdr, filehdr_ext);
2332   bfd_bwrite (filehdr_ext, FILHSZ, abfd);
2333   bfd_coff_swap_scnhdr_out (abfd, &scnhdr, scnhdr_ext);
2334   bfd_bwrite (scnhdr_ext, SCNHSZ, abfd);
2335   bfd_bwrite (data_buffer, data_buffer_size, abfd);
2336   bfd_bwrite (reloc_ext, scnhdr.s_nreloc * RELSZ, abfd);
2337   bfd_bwrite (syment_ext, filehdr.f_nsyms * SYMESZ, abfd);
2338   bfd_bwrite (string_table, string_table_size, abfd);
2339
2340   return true;
2341 }
2342
2343 /* The typical dynamic reloc.  */
2344
2345 static reloc_howto_type xcoff64_dynamic_reloc =
2346 HOWTO (0,                       /* type */
2347        0,                       /* rightshift */
2348        4,                       /* size (0 = byte, 1 = short, 2 = long) */
2349        64,                      /* bitsize */
2350        false,                   /* pc_relative */
2351        0,                       /* bitpos */
2352        complain_overflow_bitfield, /* complain_on_overflow */
2353        0,                       /* special_function */
2354        "R_POS",                 /* name */
2355        true,                    /* partial_inplace */
2356        MINUS_ONE,               /* src_mask */
2357        MINUS_ONE,               /* dst_mask */
2358        false);                  /* pcrel_offset */
2359
2360 static unsigned long xcoff64_glink_code[10] =
2361 {
2362   0xe9820000,   /* ld r12,0(r2) */
2363   0xf8410028,   /* std r2,40(r1) */
2364   0xe80c0000,   /* ld r0,0(r12) */
2365   0xe84c0008,   /* ld r0,8(r12) */
2366   0x7c0903a6,   /* mtctr r0 */
2367   0x4e800420,   /* bctr */
2368   0x00000000,   /* start of traceback table */
2369   0x000ca000,   /* traceback table */
2370   0x00000000,   /* traceback table */
2371   0x00000018,   /* ??? */
2372 };
2373
2374 static const struct xcoff_backend_data_rec bfd_xcoff_backend_data =
2375 {
2376   { /* COFF backend, defined in libcoff.h */
2377     _bfd_xcoff64_swap_aux_in,         /* _bfd_coff_swap_aux_in */
2378     _bfd_xcoff64_swap_sym_in,         /* _bfd_coff_swap_sym_in */
2379     _bfd_xcoff64_swap_lineno_in,      /* _bfd_coff_swap_lineno_in */
2380     _bfd_xcoff64_swap_aux_out,        /* _bfd_swap_aux_out */
2381     _bfd_xcoff64_swap_sym_out,        /* _bfd_swap_sym_out */
2382     _bfd_xcoff64_swap_lineno_out,     /* _bfd_swap_lineno_out */
2383     coff_swap_reloc_out,              /* _bfd_swap_reloc_out */
2384     coff_swap_filehdr_out,            /* _bfd_swap_filehdr_out */
2385     coff_swap_aouthdr_out,            /* _bfd_swap_aouthdr_out */
2386     coff_swap_scnhdr_out,             /* _bfd_swap_scnhdr_out */
2387     FILHSZ,                           /* _bfd_filhsz */
2388     AOUTSZ,                           /* _bfd_aoutsz */
2389     SCNHSZ,                           /* _bfd_scnhsz */
2390     SYMESZ,                           /* _bfd_symesz */
2391     AUXESZ,                           /* _bfd_auxesz */
2392     RELSZ,                            /* _bfd_relsz */
2393     LINESZ,                           /* _bfd_linesz */
2394     FILNMLEN,                         /* _bfd_filnmlen */
2395     true,                             /* _bfd_coff_long_filenames */
2396     false,                            /* _bfd_coff_long_section_names */
2397     (3),                        /* _bfd_coff_default_section_alignment_power */
2398     true,                            /* _bfd_coff_force_symnames_in_strings */
2399     4,                               /* _bfd_coff_debug_string_prefix_length */
2400     coff_swap_filehdr_in,             /* _bfd_coff_swap_filehdr_in */
2401     coff_swap_aouthdr_in,             /* _bfd_swap_aouthdr_in */
2402     coff_swap_scnhdr_in,              /* _bfd_swap_scnhdr_in */
2403     coff_swap_reloc_in,               /* _bfd_reloc_in */
2404     coff_bad_format_hook,             /* _bfd_bad_format_hook */
2405     coff_set_arch_mach_hook,          /* _bfd_set_arch_mach_hook */
2406     coff_mkobject_hook,               /* _bfd_mkobject_hook */
2407     styp_to_sec_flags,                /* _bfd_syp_to_sec_flags */
2408     coff_set_alignment_hook,          /* _bfd_set_alignment_hook */
2409     coff_slurp_symbol_table,     /* _bfd_coff_slurp_symbol_table */
2410     symname_in_debug_hook,            /* _coff_symname_in_debug_hook */
2411     coff_pointerize_aux_hook,         /* _bfd_coff_pointerize_aux_hook */
2412     coff_print_aux,                   /* bfd_coff_print_aux */
2413     dummy_reloc16_extra_cases,        /* _bfd_coff_reloc16_extra_cases */
2414     dummy_reloc16_estimate,           /* _bfd_coff_reloc16_estimate */
2415     NULL,                             /* bfd_coff_sym_is_global */
2416     /* _bfd_coff_compute_section_file_positions */
2417     coff_compute_section_file_positions,
2418     NULL ,                            /* _bfd_coff_start_final_link */
2419     xcoff64_ppc_relocate_section,  /* _bfd_coff_relocate_section */
2420     coff_rtype_to_howto,              /* _bfd_coff_rtype_to_howto */
2421     NULL ,                            /* _bfd_coff_addust_symndx */
2422     _bfd_generic_link_add_one_symbol, /* _bfd_coff_add_one_symbol */
2423     coff_link_output_has_begun,       /* _bfd_coff_link_output_has_begun */
2424     coff_final_link_postscript        /* _bfd_coff_final_link_postscript */
2425   },
2426
2427   0x01EF,                             /* magic number */
2428   bfd_arch_powerpc,                   /* architecture */
2429   bfd_mach_ppc_620,                   /* machine */
2430
2431   /* function pointers to xcoff specific swap routines */
2432   xcoff64_swap_ldhdr_in,                /* _xcoff_swap_ldhdr_in */
2433   xcoff64_swap_ldhdr_out,               /* _xcoff_swap_ldhdr_out */
2434   xcoff64_swap_ldsym_in,                /* _xcoff_swap_ldsym_in */
2435   xcoff64_swap_ldsym_out,               /* _xcoff_swap_ldsym_out */
2436   xcoff64_swap_ldrel_in,                /* _xcoff_swap_ldrel_in */
2437   xcoff64_swap_ldrel_out,               /* _xcoff_swap_ldrel_out */
2438
2439   /* sizes */
2440   LDHDRSZ,                              /* _xcoff_ldhdrsz */
2441   LDSYMSZ,                              /* _xcoff_ldsymsz */
2442   LDRELSZ,                              /* _xcoff_ldrelsz */
2443   24,                                   /* _xcoff_function_descriptor_size */
2444   0,                                    /* _xcoff_small_aout_header_size */
2445   /* versions */
2446   2,                                    /* _xcoff_ldhdr_version */
2447
2448   /* xcoff vs xcoff64 putting symbol names */
2449   _bfd_xcoff64_put_symbol_name,          /* _xcoff_put_symbol_name */
2450   _bfd_xcoff64_put_ldsymbol_name,        /* _xcoff_put_ldsymbol_name */
2451
2452   /* dynamic reloc howto */
2453   &xcoff64_dynamic_reloc,
2454
2455   xcoff64_create_csect_from_smclas,
2456
2457   /* lineno and reloc count overflow */
2458   xcoff64_is_lineno_count_overflow,
2459   xcoff64_is_reloc_count_overflow,
2460
2461   xcoff64_loader_symbol_offset,
2462   xcoff64_loader_reloc_offset,
2463
2464   /* glink */
2465   &xcoff64_glink_code[0],
2466   40,           /* _xcoff_glink_size */
2467
2468   /* rtinit */
2469   88,           /* _xcoff_rtinit_size */
2470   xcoff64_generate_rtinit,  /* _xcoff_generate_rtinit */
2471
2472 };
2473
2474 /* The transfer vector that leads the outside world to all of the above. */
2475 const bfd_target rs6000coff64_vec =
2476 {
2477   "aixcoff64-rs6000",
2478   bfd_target_xcoff_flavour,
2479   BFD_ENDIAN_BIG,               /* data byte order is big */
2480   BFD_ENDIAN_BIG,               /* header byte order is big */
2481
2482   (HAS_RELOC | EXEC_P |         /* object flags */
2483    HAS_LINENO | HAS_DEBUG | DYNAMIC |
2484    HAS_SYMS | HAS_LOCALS | WP_TEXT),
2485
2486   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
2487   0,                            /* leading char */
2488   '/',                          /* ar_pad_char */
2489   15,                           /* ar_max_namelen??? FIXMEmgo */
2490
2491   /* data */
2492   bfd_getb64,         /* bfd_getx64 */
2493   bfd_getb_signed_64, /* bfd_getx_signed_64 */
2494   bfd_putb64,         /* bfd_putx64 */
2495   bfd_getb32,         /* bfd_getx32 */
2496   bfd_getb_signed_32, /* bfd_getx_signed_32 */
2497   bfd_putb32,         /* bfd_putx32 */
2498   bfd_getb16,         /* bfd_getx16 */
2499   bfd_getb_signed_16, /* bfd_getx_signed_16 */
2500   bfd_putb16,         /* bfd_putx16 */
2501
2502   /* hdrs */
2503   bfd_getb64,         /* bfd_h_getx64 */
2504   bfd_getb_signed_64, /* bfd_h_getx_signed_64 */
2505   bfd_putb64,         /* bfd_h_putx64 */
2506   bfd_getb32,         /* bfd_h_getx32 */
2507   bfd_getb_signed_32, /* bfd_h_getx_signed_32 */
2508   bfd_putb32,         /* bfd_h_putx32 */
2509   bfd_getb16,         /* bfd_h_getx16 */
2510   bfd_getb_signed_16, /* bfd_h_getx_signed_16 */
2511   bfd_putb16,         /* bfd_h_putx16 */
2512
2513   { /* bfd_check_format */
2514     _bfd_dummy_target,
2515     coff_object_p,
2516     xcoff64_archive_p,
2517     CORE_FILE_P
2518   },
2519
2520   { /* bfd_set_format */
2521     bfd_false,
2522     coff_mkobject,
2523     _bfd_generic_mkarchive,
2524     bfd_false
2525   },
2526
2527   {/* bfd_write_contents */
2528     bfd_false,
2529     xcoff64_write_object_contents,
2530     _bfd_xcoff_write_archive_contents,
2531     bfd_false
2532   },
2533
2534   /* Generic */
2535   bfd_true,                          /* _close_and_cleanup */
2536   bfd_true,                          /* _bfd_free_cached_info */
2537   coff_new_section_hook,             /* _new_section_hook */
2538   _bfd_generic_get_section_contents, /* _bfd_get_section_contents */
2539   /* _bfd_get_section_contents_in_window */
2540   _bfd_generic_get_section_contents_in_window,
2541
2542   /* Copy */
2543   _bfd_xcoff_copy_private_bfd_data, /* _bfd_copy_private_bfd */
2544   /* _bfd_merge_private_bfd_data */
2545   ((boolean (*) (bfd *, bfd *)) bfd_true),
2546   /* _bfd_copy_pivate_section_data */
2547   ((boolean (*) (bfd *, asection *, bfd *, asection *)) bfd_true),
2548   /* _bfd_copy_private_symbol_data */
2549   ((boolean (*) (bfd *, asymbol *, bfd *, asymbol *)) bfd_true),
2550   ((boolean (*) (bfd *, flagword)) bfd_true), /* _bfd_set_private_flags */
2551   ((boolean (*) (bfd *, void * )) bfd_true),  /* _bfd_print_private_bfd_data */
2552
2553   /* Core */
2554   coff_core_file_failing_command,    /* _core_file_failing_command */
2555   coff_core_file_failing_signal,     /* _core_file_failing_signal */
2556   /* _core_file_matches_executable_p */
2557   coff_core_file_matches_executable_p,
2558
2559   /* Archive */
2560   xcoff64_slurp_armap,                  /* _slurp_armap */
2561   /* XCOFF archives do not have
2562      anything which corresponds to
2563      an extended name table.  */
2564   bfd_false,                               /* _slurp_extended_name_table */
2565   /* _construct_extended_name_table */
2566   ((boolean (*) (bfd *, char **, bfd_size_type *, const char **)) bfd_false),
2567   bfd_dont_truncate_arname,                /* _truncate_arname */
2568   _bfd_xcoff_write_armap,                  /* _write_armap */
2569   _bfd_xcoff_read_ar_hdr,                  /* _read_ar_hdr */
2570   xcoff64_openr_next_archived_file,     /* _openr_next_archived_file */
2571   _bfd_generic_get_elt_at_index,           /* _get_elt_at_index */
2572   _bfd_xcoff_generic_stat_arch_elt,        /* _generic_dtat_arch_elt */
2573   /* XCOFF archives do not have
2574      a timestamp.  */
2575   bfd_true,                                /* _update_armap_timestamp */
2576
2577   /* Symbols */
2578   coff_get_symtab_upper_bound,             /* _get_symtab_upper_bound */
2579   coff_get_symtab,                         /* _get_symtab */
2580   coff_make_empty_symbol,                  /* _make_empty_symbol */
2581   coff_print_symbol,                       /* _print_symbol */
2582   coff_get_symbol_info,                    /* _get_symbol_info */
2583   _bfd_xcoff_is_local_label_name,          /* _bfd_is_local_label_name */
2584   coff_get_lineno,                         /* _get_lineno */
2585   coff_find_nearest_line,                  /* _find_nearest_line */
2586   coff_bfd_make_debug_symbol,              /* _bfd_make_debug_symbol */
2587   _bfd_generic_read_minisymbols,           /* _read_minisymbols */
2588   _bfd_generic_minisymbol_to_symbol,       /* _minsymbol_to_symbol */
2589
2590   /* Reloc */
2591   coff_get_reloc_upper_bound,              /* _get_reloc_upper_bound */
2592   coff_canonicalize_reloc,           /* _cononicalize_reloc */
2593   xcoff64_reloc_type_lookup,               /* _bfd_reloc_type_lookup */
2594
2595   /* Write */
2596   coff_set_arch_mach,                      /* _set_arch_mach */
2597   coff_set_section_contents,               /* _set_section_contents */
2598
2599   /* Link */
2600   xcoff64_sizeof_headers,               /* _sizeof_headers */
2601   /* _bfd_get_relocated_section_contents */
2602   bfd_generic_get_relocated_section_contents,
2603   bfd_generic_relax_section,               /* _bfd_relax_section */
2604   _bfd_xcoff_bfd_link_hash_table_create,   /* _bfd_link_hash_table_create */
2605   _bfd_xcoff_bfd_link_add_symbols,         /* _bfd_link_add_symbols */
2606   _bfd_xcoff_bfd_final_link,               /* _bfd_filnal_link */
2607   _bfd_generic_link_split_section,         /* _bfd_link_split_section */
2608   bfd_generic_gc_sections,                 /* _bfd_gc_sections */
2609   bfd_generic_merge_sections,               /* _bfd_merge_sections */
2610
2611   /* Dynamic */
2612   /* _get_dynamic_symtab_upper_bound */
2613   _bfd_xcoff_get_dynamic_symtab_upper_bound,
2614   _bfd_xcoff_canonicalize_dynamic_symtab,  /* _cononicalize_dynamic_symtab */
2615   _bfd_xcoff_get_dynamic_reloc_upper_bound,/* _get_dynamic_reloc_upper_bound */
2616   _bfd_xcoff_canonicalize_dynamic_reloc,   /* _cononicalize_dynamic_reloc */
2617
2618   /* Opposite endian version, none exists */
2619   NULL,
2620
2621   /* back end data */
2622   (void *) &bfd_xcoff_backend_data,
2623 };