Automatic date update in version.in
[external/binutils.git] / bfd / coff64-rs6000.c
1 /* BFD back-end for IBM RS/6000 "XCOFF64" files.
2    Copyright (C) 2000-2018 Free Software Foundation, Inc.
3    Written Clinton Popetz.
4    Contributed by Cygnus Support.
5
6    This file is part of BFD, the Binary File Descriptor library.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21    MA 02110-1301, USA.  */
22
23 #include "sysdep.h"
24 #include "bfd.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
86   (bfd *, void *, void *);
87 static unsigned int _bfd_xcoff64_swap_lineno_out
88   (bfd *, void *, void *);
89 static bfd_boolean _bfd_xcoff64_put_symbol_name
90   (struct bfd_link_info *, struct bfd_strtab_hash *,
91    struct internal_syment *, const char *);
92 static bfd_boolean _bfd_xcoff64_put_ldsymbol_name
93   (bfd *, struct xcoff_loader_info *, struct internal_ldsym *, const char *);
94 static void _bfd_xcoff64_swap_sym_in
95   (bfd *, void *, void *);
96 static unsigned int _bfd_xcoff64_swap_sym_out
97   (bfd *, void *, void *);
98 static void _bfd_xcoff64_swap_aux_in
99   (bfd *, void *, int, int, int, int, void *);
100 static unsigned int _bfd_xcoff64_swap_aux_out
101   (bfd *, void *, int, int, int, int, void *);
102 static void xcoff64_swap_reloc_in
103   (bfd *, void *, void *);
104 static unsigned int xcoff64_swap_reloc_out
105   (bfd *, void *, void *);
106 extern bfd_boolean _bfd_xcoff_mkobject
107   (bfd *);
108 extern bfd_boolean _bfd_xcoff_copy_private_bfd_data
109   (bfd *, bfd *);
110 extern bfd_boolean _bfd_xcoff_is_local_label_name
111   (bfd *, const char *);
112 extern void xcoff64_rtype2howto
113   (arelent *, struct internal_reloc *);
114 extern reloc_howto_type * xcoff64_reloc_type_lookup
115   (bfd *, bfd_reloc_code_real_type);
116 extern bfd_boolean _bfd_xcoff_slurp_armap
117   (bfd *);
118 extern void *_bfd_xcoff_read_ar_hdr
119   (bfd *);
120 extern bfd *_bfd_xcoff_openr_next_archived_file
121   (bfd *, bfd *);
122 extern int _bfd_xcoff_stat_arch_elt
123   (bfd *, struct stat *);
124 extern bfd_boolean _bfd_xcoff_write_armap
125   (bfd *, unsigned int, struct orl *, unsigned int, int);
126 extern bfd_boolean _bfd_xcoff_write_archive_contents
127   (bfd *);
128 extern int _bfd_xcoff_sizeof_headers
129   (bfd *, struct bfd_link_info *);
130 extern void _bfd_xcoff_swap_sym_in
131   (bfd *, void *, void *);
132 extern unsigned int _bfd_xcoff_swap_sym_out
133   (bfd *, void *, void *);
134 extern void _bfd_xcoff_swap_aux_in
135   (bfd *, void *, int, int, int, int, void *);
136 extern unsigned int _bfd_xcoff_swap_aux_out
137   (bfd *, void *, int, int, int, int, void *);
138 static void xcoff64_swap_ldhdr_in
139   (bfd *, const void *, struct internal_ldhdr *);
140 static void xcoff64_swap_ldhdr_out
141   (bfd *, const struct internal_ldhdr *, void *d);
142 static void xcoff64_swap_ldsym_in
143   (bfd *, const void *, struct internal_ldsym *);
144 static void xcoff64_swap_ldsym_out
145   (bfd *, const struct internal_ldsym *, void *d);
146 static void xcoff64_swap_ldrel_in
147   (bfd *, const void *, struct internal_ldrel *);
148 static void xcoff64_swap_ldrel_out
149   (bfd *, const struct internal_ldrel *, void *d);
150 static bfd_boolean xcoff64_write_object_contents
151   (bfd *);
152 static bfd_boolean xcoff64_ppc_relocate_section
153   (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
154    struct internal_reloc *, struct internal_syment *,
155    asection **);
156 static bfd_boolean xcoff64_slurp_armap
157   (bfd *);
158 static const bfd_target *xcoff64_archive_p
159   (bfd *);
160 static bfd *xcoff64_openr_next_archived_file
161   (bfd *, bfd *);
162 static int xcoff64_sizeof_headers
163   (bfd *, struct bfd_link_info *);
164 static asection *xcoff64_create_csect_from_smclas
165   (bfd *, union internal_auxent *, const char *);
166 static bfd_boolean xcoff64_is_lineno_count_overflow
167   (bfd *, bfd_vma);
168 static bfd_boolean xcoff64_is_reloc_count_overflow
169   (bfd *, bfd_vma);
170 static bfd_vma xcoff64_loader_symbol_offset
171   (bfd *, struct internal_ldhdr *);
172 static bfd_vma xcoff64_loader_reloc_offset
173   (bfd *, struct internal_ldhdr *);
174 static bfd_boolean xcoff64_generate_rtinit
175   (bfd *, const char *, const char *, bfd_boolean);
176 static bfd_boolean xcoff64_bad_format_hook
177   (bfd *, void *);
178
179 /* Relocation functions */
180 static bfd_boolean xcoff64_reloc_type_br
181   (XCOFF_RELOC_FUNCTION_ARGS);
182
183 bfd_boolean (*xcoff64_calculate_relocation[XCOFF_MAX_CALCULATE_RELOCATION])
184   (XCOFF_RELOC_FUNCTION_ARGS) =
185 {
186   xcoff_reloc_type_pos,  /* R_POS   (0x00) */
187   xcoff_reloc_type_neg,  /* R_NEG   (0x01) */
188   xcoff_reloc_type_rel,  /* R_REL   (0x02) */
189   xcoff_reloc_type_toc,  /* R_TOC   (0x03) */
190   xcoff_reloc_type_fail, /* R_RTB   (0x04) */
191   xcoff_reloc_type_toc,  /* R_GL    (0x05) */
192   xcoff_reloc_type_toc,  /* R_TCL   (0x06) */
193   xcoff_reloc_type_fail, /*         (0x07) */
194   xcoff_reloc_type_ba,   /* R_BA    (0x08) */
195   xcoff_reloc_type_fail, /*         (0x09) */
196   xcoff64_reloc_type_br, /* R_BR    (0x0a) */
197   xcoff_reloc_type_fail, /*         (0x0b) */
198   xcoff_reloc_type_pos,  /* R_RL    (0x0c) */
199   xcoff_reloc_type_pos,  /* R_RLA   (0x0d) */
200   xcoff_reloc_type_fail, /*         (0x0e) */
201   xcoff_reloc_type_noop, /* R_REF   (0x0f) */
202   xcoff_reloc_type_fail, /*         (0x10) */
203   xcoff_reloc_type_fail, /*         (0x11) */
204   xcoff_reloc_type_toc,  /* R_TRL   (0x12) */
205   xcoff_reloc_type_toc,  /* R_TRLA  (0x13) */
206   xcoff_reloc_type_fail, /* R_RRTBI (0x14) */
207   xcoff_reloc_type_fail, /* R_RRTBA (0x15) */
208   xcoff_reloc_type_ba,   /* R_CAI   (0x16) */
209   xcoff_reloc_type_crel, /* R_CREL  (0x17) */
210   xcoff_reloc_type_ba,   /* R_RBA   (0x18) */
211   xcoff_reloc_type_ba,   /* R_RBAC  (0x19) */
212   xcoff64_reloc_type_br, /* R_RBR   (0x1a) */
213   xcoff_reloc_type_ba,   /* R_RBRC  (0x1b) */
214 };
215
216 /* coffcode.h needs these to be defined.  */
217 /* Internalcoff.h and coffcode.h modify themselves based on these flags.  */
218 #define XCOFF64
219 #define RS6000COFF_C 1
220
221 #define SELECT_RELOC(internal, howto)                                   \
222   {                                                                     \
223     internal.r_type = howto->type;                                      \
224     internal.r_size =                                                   \
225       ((howto->complain_on_overflow == complain_overflow_signed         \
226         ? 0x80                                                          \
227         : 0)                                                            \
228        | (howto->bitsize - 1));                                         \
229   }
230
231 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
232 #define COFF_LONG_FILENAMES
233 #define NO_COFF_SYMBOLS
234 #define RTYPE2HOWTO(cache_ptr, dst) xcoff64_rtype2howto (cache_ptr, dst)
235 #define coff_mkobject _bfd_xcoff_mkobject
236 #define coff_bfd_copy_private_bfd_data _bfd_xcoff_copy_private_bfd_data
237 #define coff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
238 #define coff_bfd_reloc_type_lookup xcoff64_reloc_type_lookup
239 #define coff_bfd_reloc_name_lookup xcoff64_reloc_name_lookup
240 #ifdef AIX_CORE
241 extern const bfd_target * rs6000coff_core_p
242   (bfd *abfd);
243 extern bfd_boolean rs6000coff_core_file_matches_executable_p
244   (bfd *cbfd, bfd *ebfd);
245 extern char *rs6000coff_core_file_failing_command
246   (bfd *abfd);
247 extern int rs6000coff_core_file_failing_signal
248   (bfd *abfd);
249 #define CORE_FILE_P rs6000coff_core_p
250 #define coff_core_file_failing_command \
251   rs6000coff_core_file_failing_command
252 #define coff_core_file_failing_signal \
253   rs6000coff_core_file_failing_signal
254 #define coff_core_file_matches_executable_p \
255   rs6000coff_core_file_matches_executable_p
256 #define coff_core_file_pid \
257   _bfd_nocore_core_file_pid
258 #else
259 #define CORE_FILE_P _bfd_dummy_target
260 #define coff_core_file_failing_command \
261   _bfd_nocore_core_file_failing_command
262 #define coff_core_file_failing_signal \
263   _bfd_nocore_core_file_failing_signal
264 #define coff_core_file_matches_executable_p \
265   _bfd_nocore_core_file_matches_executable_p
266 #define coff_core_file_pid \
267   _bfd_nocore_core_file_pid
268 #endif
269 #define coff_SWAP_sym_in _bfd_xcoff64_swap_sym_in
270 #define coff_SWAP_sym_out _bfd_xcoff64_swap_sym_out
271 #define coff_SWAP_aux_in _bfd_xcoff64_swap_aux_in
272 #define coff_SWAP_aux_out _bfd_xcoff64_swap_aux_out
273 #define coff_swap_reloc_in xcoff64_swap_reloc_in
274 #define coff_swap_reloc_out xcoff64_swap_reloc_out
275 #define NO_COFF_RELOCS
276
277 #ifndef bfd_pe_print_pdata
278 #define bfd_pe_print_pdata      NULL
279 #endif
280
281 #include <stdint.h>
282 #include "coffcode.h"
283
284 /* For XCOFF64, the effective width of symndx changes depending on
285    whether we are the first entry.  Sigh.  */
286 static void
287 _bfd_xcoff64_swap_lineno_in (bfd *abfd, void *ext1, void *in1)
288 {
289   LINENO *ext = (LINENO *) ext1;
290   struct internal_lineno *in = (struct internal_lineno *) in1;
291
292   in->l_lnno = H_GET_32 (abfd, (ext->l_lnno));
293   if (in->l_lnno == 0)
294     in->l_addr.l_symndx = H_GET_32 (abfd, ext->l_addr.l_symndx);
295   else
296     in->l_addr.l_paddr = H_GET_64 (abfd, ext->l_addr.l_paddr);
297 }
298
299 static unsigned int
300 _bfd_xcoff64_swap_lineno_out (bfd *abfd, void *inp, void *outp)
301 {
302   struct internal_lineno *in = (struct internal_lineno *) inp;
303   struct external_lineno *ext = (struct external_lineno *) outp;
304
305   H_PUT_32 (abfd, in->l_addr.l_symndx, ext->l_addr.l_symndx);
306   H_PUT_32 (abfd, in->l_lnno, (ext->l_lnno));
307
308   if (in->l_lnno == 0)
309     H_PUT_32 (abfd, in->l_addr.l_symndx, ext->l_addr.l_symndx);
310   else
311     H_PUT_64 (abfd, in->l_addr.l_paddr, ext->l_addr.l_paddr);
312
313   return bfd_coff_linesz (abfd);
314 }
315
316 static void
317 _bfd_xcoff64_swap_sym_in (bfd *abfd, void *ext1, void *in1)
318 {
319   struct external_syment *ext = (struct external_syment *) ext1;
320   struct internal_syment *in = (struct internal_syment *) in1;
321
322   in->_n._n_n._n_zeroes = 0;
323   in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e_offset);
324   in->n_value = H_GET_64 (abfd, ext->e_value);
325   in->n_scnum = (short) H_GET_16 (abfd, ext->e_scnum);
326   in->n_type = H_GET_16 (abfd, ext->e_type);
327   in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
328   in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
329 }
330
331 static unsigned int
332 _bfd_xcoff64_swap_sym_out (bfd *abfd, void *inp, void *extp)
333 {
334   struct internal_syment *in = (struct internal_syment *) inp;
335   struct external_syment *ext = (struct external_syment *) extp;
336
337   H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e_offset);
338   H_PUT_64 (abfd, in->n_value, ext->e_value);
339   H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
340   H_PUT_16 (abfd, in->n_type, ext->e_type);
341   H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
342   H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
343   return bfd_coff_symesz (abfd);
344 }
345
346 static void
347 _bfd_xcoff64_swap_aux_in (bfd *abfd, void *ext1, int type, int in_class,
348                           int indx, int numaux, void *in1)
349 {
350   union external_auxent *ext = (union external_auxent *) ext1;
351   union internal_auxent *in = (union internal_auxent *) in1;
352
353   switch (in_class)
354     {
355     case C_FILE:
356       if (ext->x_file.x_n.x_n.x_zeroes[0] == 0)
357         {
358           in->x_file.x_n.x_zeroes = 0;
359           in->x_file.x_n.x_offset =
360             H_GET_32 (abfd, ext->x_file.x_n.x_n.x_offset);
361         }
362       else
363         {
364           memcpy (in->x_file.x_fname, ext->x_file.x_n.x_fname, FILNMLEN);
365         }
366       goto end;
367
368       /* RS/6000 "csect" auxents */
369     case C_EXT:
370     case C_AIX_WEAKEXT:
371     case C_HIDEXT:
372       if (indx + 1 == numaux)
373         {
374           bfd_signed_vma h = 0;
375           bfd_vma l = 0;
376
377           h = H_GET_S32 (abfd, ext->x_csect.x_scnlen_hi);
378           l = H_GET_32 (abfd, ext->x_csect.x_scnlen_lo);
379
380           in->x_csect.x_scnlen.l = h << 32 | (l & 0xffffffff);
381
382           in->x_csect.x_parmhash = H_GET_32 (abfd, ext->x_csect.x_parmhash);
383           in->x_csect.x_snhash = H_GET_16 (abfd, ext->x_csect.x_snhash);
384           /* We don't have to hack bitfields in x_smtyp because it's
385              defined by shifts-and-ands, which are equivalent on all
386              byte orders.  */
387           in->x_csect.x_smtyp = H_GET_8 (abfd, ext->x_csect.x_smtyp);
388           in->x_csect.x_smclas = H_GET_8 (abfd, ext->x_csect.x_smclas);
389           goto end;
390         }
391       break;
392
393     case C_STAT:
394     case C_LEAFSTAT:
395     case C_HIDDEN:
396       if (type == T_NULL)
397         {
398           /* PE defines some extra fields; we zero them out for
399              safety.  */
400           in->x_scn.x_checksum = 0;
401           in->x_scn.x_associated = 0;
402           in->x_scn.x_comdat = 0;
403
404           goto end;
405         }
406       break;
407     }
408
409   if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
410       || ISTAG (in_class))
411     {
412       in->x_sym.x_fcnary.x_fcn.x_lnnoptr
413         = H_GET_64 (abfd, ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
414       in->x_sym.x_fcnary.x_fcn.x_endndx.l
415         = H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_endndx);
416     }
417   if (ISFCN (type))
418     {
419       in->x_sym.x_misc.x_fsize
420         = H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_fsize);
421     }
422   else
423     {
424       in->x_sym.x_misc.x_lnsz.x_lnno
425         = H_GET_32 (abfd, ext->x_sym.x_fcnary.x_lnsz.x_lnno);
426       in->x_sym.x_misc.x_lnsz.x_size
427         = H_GET_16 (abfd, ext->x_sym.x_fcnary.x_lnsz.x_size);
428     }
429
430  end: ;
431 }
432
433 static unsigned int
434 _bfd_xcoff64_swap_aux_out (bfd *abfd, void *inp, int type, int in_class,
435                            int indx ATTRIBUTE_UNUSED,
436                            int numaux ATTRIBUTE_UNUSED,
437                            void *extp)
438 {
439   union internal_auxent *in = (union internal_auxent *) inp;
440   union external_auxent *ext = (union external_auxent *) extp;
441
442   memset (ext, 0, bfd_coff_auxesz (abfd));
443   switch (in_class)
444     {
445     case C_FILE:
446       if (in->x_file.x_n.x_zeroes == 0)
447         {
448           H_PUT_32 (abfd, 0, ext->x_file.x_n.x_n.x_zeroes);
449           H_PUT_32 (abfd, in->x_file.x_n.x_offset,
450                     ext->x_file.x_n.x_n.x_offset);
451         }
452       else
453         {
454           memcpy (ext->x_file.x_n.x_fname, in->x_file.x_fname, FILNMLEN);
455         }
456       H_PUT_8 (abfd, _AUX_FILE, ext->x_auxtype.x_auxtype);
457       goto end;
458
459       /* RS/6000 "csect" auxents */
460     case C_EXT:
461     case C_AIX_WEAKEXT:
462     case C_HIDEXT:
463       if (indx + 1 == numaux)
464         {
465           bfd_vma temp;
466
467           temp = in->x_csect.x_scnlen.l & 0xffffffff;
468           H_PUT_32 (abfd, temp, ext->x_csect.x_scnlen_lo);
469           temp = in->x_csect.x_scnlen.l >> 32;
470           H_PUT_32 (abfd, temp, ext->x_csect.x_scnlen_hi);
471           H_PUT_32 (abfd, in->x_csect.x_parmhash, ext->x_csect.x_parmhash);
472           H_PUT_16 (abfd, in->x_csect.x_snhash, ext->x_csect.x_snhash);
473           /* We don't have to hack bitfields in x_smtyp because it's
474              defined by shifts-and-ands, which are equivalent on all
475              byte orders.  */
476           H_PUT_8 (abfd, in->x_csect.x_smtyp, ext->x_csect.x_smtyp);
477           H_PUT_8 (abfd, in->x_csect.x_smclas, ext->x_csect.x_smclas);
478           H_PUT_8 (abfd, _AUX_CSECT, ext->x_auxtype.x_auxtype);
479           goto end;
480         }
481       break;
482
483     case C_STAT:
484     case C_LEAFSTAT:
485     case C_HIDDEN:
486       if (type == T_NULL)
487         {
488           goto end;
489         }
490       break;
491     }
492
493   if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
494       || ISTAG (in_class))
495     {
496       H_PUT_64 (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr,
497                ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
498       H_PUT_8 (abfd, _AUX_FCN,
499                ext->x_auxtype.x_auxtype);
500       H_PUT_32 (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l,
501                ext->x_sym.x_fcnary.x_fcn.x_endndx);
502     }
503   if (ISFCN (type))
504     {
505       H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize,
506                ext->x_sym.x_fcnary.x_fcn.x_fsize);
507     }
508   else
509     {
510       H_PUT_32 (abfd, in->x_sym.x_misc.x_lnsz.x_lnno,
511                ext->x_sym.x_fcnary.x_lnsz.x_lnno);
512       H_PUT_16 (abfd, in->x_sym.x_misc.x_lnsz.x_size,
513                ext->x_sym.x_fcnary.x_lnsz.x_size);
514     }
515
516  end:
517
518   return bfd_coff_auxesz (abfd);
519 }
520
521 static bfd_boolean
522 _bfd_xcoff64_put_symbol_name (struct bfd_link_info *info,
523                               struct bfd_strtab_hash *strtab,
524                               struct internal_syment *sym,
525                               const char *name)
526 {
527   bfd_boolean hash;
528   bfd_size_type indx;
529
530   hash = !info->traditional_format;
531   indx = _bfd_stringtab_add (strtab, name, hash, FALSE);
532
533   if (indx == (bfd_size_type) -1)
534     return FALSE;
535
536   sym->_n._n_n._n_zeroes = 0;
537   sym->_n._n_n._n_offset = STRING_SIZE_SIZE + indx;
538
539   return TRUE;
540 }
541
542 static bfd_boolean
543 _bfd_xcoff64_put_ldsymbol_name (bfd *abfd ATTRIBUTE_UNUSED,
544                                 struct xcoff_loader_info *ldinfo,
545                                 struct internal_ldsym *ldsym,
546                                 const char *name)
547 {
548   size_t len;
549   len = strlen (name);
550
551   if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
552     {
553       bfd_size_type newalc;
554       char *newstrings;
555
556       newalc = ldinfo->string_alc * 2;
557       if (newalc == 0)
558         newalc = 32;
559       while (ldinfo->string_size + len + 3 > newalc)
560         newalc *= 2;
561
562       newstrings = bfd_realloc (ldinfo->strings, newalc);
563       if (newstrings == NULL)
564         {
565           ldinfo->failed = TRUE;
566           return FALSE;
567         }
568       ldinfo->string_alc = newalc;
569       ldinfo->strings = newstrings;
570     }
571
572   bfd_put_16 (ldinfo->output_bfd, (bfd_vma) (len + 1),
573               ldinfo->strings + ldinfo->string_size);
574   strcpy (ldinfo->strings + ldinfo->string_size + 2, name);
575   ldsym->_l._l_l._l_zeroes = 0;
576   ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
577   ldinfo->string_size += len + 3;
578
579   return TRUE;
580 }
581
582 /* Routines to swap information in the XCOFF .loader section.  If we
583    ever need to write an XCOFF loader, this stuff will need to be
584    moved to another file shared by the linker (which XCOFF calls the
585    ``binder'') and the loader.  */
586
587 /* Swap in the ldhdr structure.  */
588
589 static void
590 xcoff64_swap_ldhdr_in (bfd *abfd,
591                        const void *s,
592                        struct internal_ldhdr *dst)
593 {
594   const struct external_ldhdr *src = (const struct external_ldhdr *) s;
595
596   dst->l_version = bfd_get_32 (abfd, src->l_version);
597   dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
598   dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
599   dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
600   dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
601   dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
602   dst->l_impoff = bfd_get_64 (abfd, src->l_impoff);
603   dst->l_stoff = bfd_get_64 (abfd, src->l_stoff);
604   dst->l_symoff = bfd_get_64 (abfd, src->l_symoff);
605   dst->l_rldoff = bfd_get_64 (abfd, src->l_rldoff);
606 }
607
608 /* Swap out the ldhdr structure.  */
609
610 static void
611 xcoff64_swap_ldhdr_out (bfd *abfd, const struct internal_ldhdr *src, void *d)
612 {
613   struct external_ldhdr *dst = (struct external_ldhdr *) d;
614
615   bfd_put_32 (abfd, (bfd_vma) src->l_version, dst->l_version);
616   bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
617   bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
618   bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
619   bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
620   bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
621   bfd_put_64 (abfd, src->l_impoff, dst->l_impoff);
622   bfd_put_64 (abfd, src->l_stoff, dst->l_stoff);
623   bfd_put_64 (abfd, src->l_symoff, dst->l_symoff);
624   bfd_put_64 (abfd, src->l_rldoff, dst->l_rldoff);
625 }
626
627 /* Swap in the ldsym structure.  */
628
629 static void
630 xcoff64_swap_ldsym_in (bfd *abfd, const void *s, struct internal_ldsym *dst)
631 {
632   const struct external_ldsym *src = (const struct external_ldsym *) s;
633   /* XCOFF64 does not use l_zeroes like XCOFF32
634      Set the internal l_zeroes to 0 so the common 32/64 code uses l_value
635      as an offset into the loader symbol table.  */
636   dst->_l._l_l._l_zeroes = 0;
637   dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->l_offset);
638   dst->l_value = bfd_get_64 (abfd, src->l_value);
639   dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
640   dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
641   dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
642   dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
643   dst->l_parm = bfd_get_32 (abfd, src->l_parm);
644 }
645
646 /* Swap out the ldsym structure.  */
647
648 static void
649 xcoff64_swap_ldsym_out (bfd *abfd, const struct internal_ldsym *src, void *d)
650 {
651   struct external_ldsym *dst = (struct external_ldsym *) d;
652
653   bfd_put_64 (abfd, src->l_value, dst->l_value);
654   bfd_put_32 (abfd, (bfd_vma) src->_l._l_l._l_offset, dst->l_offset);
655   bfd_put_16 (abfd, (bfd_vma) src->l_scnum, dst->l_scnum);
656   bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
657   bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
658   bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
659   bfd_put_32 (abfd, src->l_parm, dst->l_parm);
660 }
661
662 static void
663 xcoff64_swap_reloc_in (bfd *abfd, void *s, void *d)
664 {
665   struct external_reloc *src = (struct external_reloc *) s;
666   struct internal_reloc *dst = (struct internal_reloc *) d;
667
668   memset (dst, 0, sizeof (struct internal_reloc));
669
670   dst->r_vaddr = bfd_get_64 (abfd, src->r_vaddr);
671   dst->r_symndx = bfd_get_32 (abfd, src->r_symndx);
672   dst->r_size = bfd_get_8 (abfd, src->r_size);
673   dst->r_type = bfd_get_8 (abfd, src->r_type);
674 }
675
676 static unsigned int
677 xcoff64_swap_reloc_out (bfd *abfd, void *s, void *d)
678 {
679   struct internal_reloc *src = (struct internal_reloc *) s;
680   struct external_reloc *dst = (struct external_reloc *) d;
681
682   bfd_put_64 (abfd, src->r_vaddr, dst->r_vaddr);
683   bfd_put_32 (abfd, src->r_symndx, dst->r_symndx);
684   bfd_put_8 (abfd, src->r_type, dst->r_type);
685   bfd_put_8 (abfd, src->r_size, dst->r_size);
686
687   return bfd_coff_relsz (abfd);
688 }
689
690 /* Swap in the ldrel structure.  */
691
692 static void
693 xcoff64_swap_ldrel_in (bfd *abfd, const void *s, struct internal_ldrel *dst)
694 {
695   const struct external_ldrel *src = (const struct external_ldrel *) s;
696
697   dst->l_vaddr = bfd_get_64 (abfd, src->l_vaddr);
698   dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
699   dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
700   dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
701 }
702
703 /* Swap out the ldrel structure.  */
704
705 static void
706 xcoff64_swap_ldrel_out (bfd *abfd, const struct internal_ldrel *src, void *d)
707 {
708   struct external_ldrel *dst = (struct external_ldrel *) d;
709
710   bfd_put_64 (abfd, src->l_vaddr, dst->l_vaddr);
711   bfd_put_16 (abfd, (bfd_vma) src->l_rtype, dst->l_rtype);
712   bfd_put_16 (abfd, (bfd_vma) src->l_rsecnm, dst->l_rsecnm);
713   bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
714 }
715
716 static bfd_boolean
717 xcoff64_write_object_contents (bfd *abfd)
718 {
719   asection *current;
720   bfd_boolean hasrelocs = FALSE;
721   bfd_boolean haslinno = FALSE;
722   file_ptr scn_base;
723   file_ptr reloc_base;
724   file_ptr lineno_base;
725   file_ptr sym_base;
726   unsigned long reloc_size = 0;
727   unsigned long lnno_size = 0;
728   asection *text_sec = NULL;
729   asection *data_sec = NULL;
730   asection *bss_sec = NULL;
731   struct internal_filehdr internal_f;
732   struct internal_aouthdr internal_a;
733
734   bfd_set_error (bfd_error_system_call);
735
736   if (! abfd->output_has_begun)
737     {
738       if (! bfd_coff_compute_section_file_positions (abfd))
739         return FALSE;
740     }
741
742   /* Work out the size of the reloc and linno areas.  */
743   reloc_base = obj_relocbase (abfd);
744
745   for (current = abfd->sections; current != NULL; current = current->next)
746     reloc_size += current->reloc_count * bfd_coff_relsz (abfd);
747
748   lineno_base = reloc_base + reloc_size;
749
750   /* Make a pass through the symbol table to count line number entries and
751      put them into the correct asections.  */
752   lnno_size = coff_count_linenumbers (abfd) * bfd_coff_linesz (abfd);
753
754   sym_base = lineno_base + lnno_size;
755
756   /* Indicate in each section->line_filepos its actual file address.  */
757   for (current = abfd->sections; current != NULL; current =  current->next)
758     {
759       if (current->lineno_count)
760         {
761           current->line_filepos = lineno_base;
762           current->moving_line_filepos = lineno_base;
763           lineno_base += current->lineno_count * bfd_coff_linesz (abfd);
764         }
765       else
766         {
767           current->line_filepos = 0;
768         }
769
770       if (current->reloc_count)
771         {
772           current->rel_filepos = reloc_base;
773           reloc_base += current->reloc_count * bfd_coff_relsz (abfd);
774         }
775       else
776         {
777           current->rel_filepos = 0;
778         }
779     }
780
781   if ((abfd->flags & EXEC_P) != 0)
782     {
783       scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
784       internal_f.f_opthdr = bfd_coff_aoutsz (abfd);
785     }
786   else
787     {
788       scn_base = bfd_coff_filhsz (abfd);
789       internal_f.f_opthdr = 0;
790     }
791
792   internal_f.f_nscns = 0;
793
794   if (bfd_seek (abfd, scn_base, SEEK_SET) != 0)
795     return FALSE;
796
797   for (current = abfd->sections; current != NULL; current = current->next)
798     {
799       struct internal_scnhdr section;
800       struct external_scnhdr buff;
801       bfd_size_type amount;
802
803       internal_f.f_nscns++;
804
805       strncpy (section.s_name, current->name, SCNNMLEN);
806
807       section.s_vaddr = current->vma;
808       section.s_paddr = current->lma;
809       section.s_size =  current->size;
810
811       /* If this section has no size or is unloadable then the scnptr
812          will be 0 too.  */
813       if (current->size == 0
814           || (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
815         {
816           section.s_scnptr = 0;
817         }
818       else
819         {
820           section.s_scnptr = current->filepos;
821         }
822
823       section.s_relptr = current->rel_filepos;
824       section.s_lnnoptr = current->line_filepos;
825       section.s_nreloc = current->reloc_count;
826
827       section.s_nlnno = current->lineno_count;
828       if (current->reloc_count != 0)
829         hasrelocs = TRUE;
830       if (current->lineno_count != 0)
831         haslinno = TRUE;
832
833       section.s_flags = sec_to_styp_flags (current->name, current->flags);
834
835       if (!strcmp (current->name, _TEXT))
836         {
837           text_sec = current;
838         }
839       else if (!strcmp (current->name, _DATA))
840         {
841           data_sec = current;
842         }
843       else if (!strcmp (current->name, _BSS))
844         {
845           bss_sec = current;
846         }
847
848       amount = bfd_coff_scnhsz (abfd);
849       if (bfd_coff_swap_scnhdr_out (abfd, &section, &buff) == 0
850           || bfd_bwrite (&buff, amount, abfd) != amount)
851         return FALSE;
852     }
853
854   internal_f.f_timdat = 0;
855
856   internal_f.f_flags = 0;
857
858   if (!hasrelocs)
859     internal_f.f_flags |= F_RELFLG;
860   if (!haslinno)
861     internal_f.f_flags |= F_LNNO;
862   if (abfd->flags & EXEC_P)
863     internal_f.f_flags |= F_EXEC;
864
865   /* FIXME: this is wrong for PPC_PE!  */
866   if (bfd_little_endian (abfd))
867     internal_f.f_flags |= F_AR32WR;
868   else
869     internal_f.f_flags |= F_AR32W;
870
871   if ((abfd->flags & DYNAMIC) != 0)
872     internal_f.f_flags |= F_SHROBJ;
873   if (bfd_get_section_by_name (abfd, _LOADER) != NULL)
874     internal_f.f_flags |= F_DYNLOAD;
875
876   memset (&internal_a, 0, sizeof internal_a);
877
878   internal_f.f_magic = bfd_xcoff_magic_number (abfd);
879   internal_a.magic = (abfd->flags & D_PAGED
880                       ? RS6K_AOUTHDR_ZMAGIC
881                       : (abfd->flags & WP_TEXT
882                          ? RS6K_AOUTHDR_NMAGIC
883                          : RS6K_AOUTHDR_OMAGIC));
884
885   /* FIXME: Does anybody ever set this to another value?  */
886   internal_a.vstamp = 0;
887
888   /* Now should write relocs, strings, syms.  */
889   obj_sym_filepos (abfd) = sym_base;
890
891   internal_f.f_symptr = 0;
892   internal_f.f_nsyms = 0;
893
894   /* If bfd_get_symcount (abfd) != 0, then we are not using the COFF
895      backend linker, and obj_raw_syment_count is not valid until after
896      coff_write_symbols is called.  */
897   if (bfd_get_symcount (abfd) != 0)
898     {
899       int firstundef;
900
901       if (!coff_renumber_symbols (abfd, &firstundef))
902         return FALSE;
903       coff_mangle_symbols (abfd);
904       if (! coff_write_symbols (abfd))
905         return FALSE;
906       if (! coff_write_linenumbers (abfd))
907         return FALSE;
908       if (! coff_write_relocs (abfd, firstundef))
909         return FALSE;
910
911       internal_f.f_symptr = sym_base;
912       internal_f.f_nsyms = bfd_get_symcount (abfd);
913     }
914   else if (obj_raw_syment_count (abfd) != 0)
915     {
916       internal_f.f_symptr = sym_base;
917
918       /* AIX appears to require that F_RELFLG not be set if there are
919          local symbols but no relocations.  */
920       internal_f.f_flags &=~ F_RELFLG;
921     }
922   else
923     {
924       internal_f.f_flags |= F_LSYMS;
925     }
926
927   if (text_sec)
928     {
929       internal_a.tsize = text_sec->size;
930       internal_a.text_start = internal_a.tsize ? text_sec->vma : 0;
931     }
932
933   if (data_sec)
934     {
935       internal_a.dsize = data_sec->size;
936       internal_a.data_start = internal_a.dsize ? data_sec->vma : 0;
937     }
938
939   if (bss_sec)
940     {
941       internal_a.bsize = bss_sec->size;
942       if (internal_a.bsize && bss_sec->vma < internal_a.data_start)
943         internal_a.data_start = bss_sec->vma;
944     }
945
946   internal_a.entry = bfd_get_start_address (abfd);
947   internal_f.f_nsyms = obj_raw_syment_count (abfd);
948
949   if (xcoff_data (abfd)->full_aouthdr)
950     {
951       bfd_vma toc;
952       asection *loader_sec;
953
954       internal_a.vstamp = 1;
955
956       internal_a.o_snentry = xcoff_data (abfd)->snentry;
957       if (internal_a.o_snentry == 0)
958         internal_a.entry = (bfd_vma) -1;
959
960       if (text_sec != NULL)
961         {
962           internal_a.o_sntext = text_sec->target_index;
963           internal_a.o_algntext = bfd_get_section_alignment (abfd, text_sec);
964         }
965       else
966         {
967           internal_a.o_sntext = 0;
968           internal_a.o_algntext = 0;
969         }
970
971       if (data_sec != NULL)
972         {
973           internal_a.o_sndata = data_sec->target_index;
974           internal_a.o_algndata = bfd_get_section_alignment (abfd, data_sec);
975         }
976       else
977         {
978           internal_a.o_sndata = 0;
979           internal_a.o_algndata = 0;
980         }
981
982       loader_sec = bfd_get_section_by_name (abfd, ".loader");
983       if (loader_sec != NULL)
984         internal_a.o_snloader = loader_sec->target_index;
985       else
986         internal_a.o_snloader = 0;
987       if (bss_sec != NULL)
988         internal_a.o_snbss = bss_sec->target_index;
989       else
990         internal_a.o_snbss = 0;
991
992       toc = xcoff_data (abfd)->toc;
993       internal_a.o_toc = toc;
994       internal_a.o_sntoc = xcoff_data (abfd)->sntoc;
995
996       internal_a.o_modtype = xcoff_data (abfd)->modtype;
997       if (xcoff_data (abfd)->cputype != -1)
998         internal_a.o_cputype = xcoff_data (abfd)->cputype;
999       else
1000         {
1001           switch (bfd_get_arch (abfd))
1002             {
1003             case bfd_arch_rs6000:
1004               internal_a.o_cputype = 4;
1005               break;
1006             case bfd_arch_powerpc:
1007               if (bfd_get_mach (abfd) == bfd_mach_ppc)
1008                 internal_a.o_cputype = 3;
1009               else if (bfd_get_mach (abfd) == bfd_mach_ppc_620)
1010                 internal_a.o_cputype = 2;
1011               else
1012                 internal_a.o_cputype = 1;
1013               break;
1014             default:
1015               abort ();
1016             }
1017         }
1018       internal_a.o_maxstack = xcoff_data (abfd)->maxstack;
1019       internal_a.o_maxdata = xcoff_data (abfd)->maxdata;
1020     }
1021
1022   if (bfd_seek (abfd, (file_ptr) 0, 0) != 0)
1023     return FALSE;
1024
1025   {
1026     char * buff;
1027     bfd_size_type amount = bfd_coff_filhsz (abfd);
1028
1029     buff = bfd_malloc (amount);
1030     if (buff == NULL)
1031       return FALSE;
1032
1033     bfd_coff_swap_filehdr_out (abfd, &internal_f, buff);
1034     amount = bfd_bwrite (buff, amount, abfd);
1035
1036     free (buff);
1037
1038     if (amount != bfd_coff_filhsz (abfd))
1039       return FALSE;
1040   }
1041
1042   if (abfd->flags & EXEC_P)
1043     {
1044       char * buff;
1045       bfd_size_type amount = bfd_coff_aoutsz (abfd);
1046
1047       buff = bfd_malloc (amount);
1048       if (buff == NULL)
1049         return FALSE;
1050
1051       bfd_coff_swap_aouthdr_out (abfd, &internal_a, buff);
1052       amount = bfd_bwrite (buff, amount, abfd);
1053
1054       free (buff);
1055
1056       if (amount != bfd_coff_aoutsz (abfd))
1057         return FALSE;
1058     }
1059
1060   return TRUE;
1061 }
1062
1063 static bfd_boolean
1064 xcoff64_reloc_type_br (bfd *input_bfd,
1065                        asection *input_section,
1066                        bfd *output_bfd ATTRIBUTE_UNUSED,
1067                        struct internal_reloc *rel,
1068                        struct internal_syment *sym ATTRIBUTE_UNUSED,
1069                        struct reloc_howto_struct *howto,
1070                        bfd_vma val,
1071                        bfd_vma addend,
1072                        bfd_vma *relocation,
1073                        bfd_byte *contents)
1074 {
1075   struct xcoff_link_hash_entry *h;
1076   bfd_vma section_offset;
1077
1078   if (0 > rel->r_symndx)
1079     return FALSE;
1080
1081   h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
1082   section_offset = rel->r_vaddr - input_section->vma;
1083
1084   /* If we see an R_BR or R_RBR reloc which is jumping to global
1085      linkage code, and it is followed by an appropriate cror nop
1086      instruction, we replace the cror with ld r2,40(r1).  This
1087      restores the TOC after the glink code.  Contrariwise, if the
1088      call is followed by a ld r2,40(r1), but the call is not
1089      going to global linkage code, we can replace the load with a
1090      cror.  */
1091   if (NULL != h
1092       && (bfd_link_hash_defined == h->root.type
1093           || bfd_link_hash_defweak == h->root.type)
1094       && section_offset + 8 <= input_section->size)
1095     {
1096       bfd_byte *pnext;
1097       unsigned long next;
1098
1099       pnext = contents + section_offset + 4;
1100       next = bfd_get_32 (input_bfd, pnext);
1101
1102       /* The _ptrgl function is magic.  It is used by the AIX compiler to call
1103          a function through a pointer.  */
1104       if (h->smclas == XMC_GL || strcmp (h->root.root.string, "._ptrgl") == 0)
1105         {
1106           if (next == 0x4def7b82                        /* cror 15,15,15  */
1107               || next == 0x4ffffb82                     /* cror 31,31,31  */
1108               || next == 0x60000000)                    /* ori  r0,r0,0   */
1109             bfd_put_32 (input_bfd, 0xe8410028, pnext);  /* ld   r2,40(r1) */
1110         }
1111       else
1112         {
1113           if (next == 0xe8410028)                       /* ld r2,40(r1)   */
1114             bfd_put_32 (input_bfd, 0x60000000, pnext);  /* ori r0,r0,0    */
1115         }
1116     }
1117   else if (NULL != h && bfd_link_hash_undefined == h->root.type)
1118     {
1119       /* Normally, this relocation is against a defined symbol.  In the
1120          case where this is a partial link and the output section offset
1121          is greater than 2^25, the linker will return an invalid error
1122          message that the relocation has been truncated.  Yes it has been
1123          truncated but no it not important.  For this case, disable the
1124          overflow checking. */
1125       howto->complain_on_overflow = complain_overflow_dont;
1126     }
1127
1128   /* The original PC-relative relocation is biased by -r_vaddr, so adding
1129      the value below will give the absolute target address.  */
1130   *relocation = val + addend + rel->r_vaddr;
1131
1132   howto->src_mask &= ~3;
1133   howto->dst_mask = howto->src_mask;
1134
1135   if (h != NULL
1136       && (h->root.type == bfd_link_hash_defined
1137           || h->root.type == bfd_link_hash_defweak)
1138       && bfd_is_abs_section (h->root.u.def.section)
1139       && section_offset + 4 <= input_section->size)
1140     {
1141       bfd_byte *ptr;
1142       bfd_vma insn;
1143
1144       /* Turn the relative branch into an absolute one by setting the
1145          AA bit.  */
1146       ptr = contents + section_offset;
1147       insn = bfd_get_32 (input_bfd, ptr);
1148       insn |= 2;
1149       bfd_put_32 (input_bfd, insn, ptr);
1150
1151       /* Make the howto absolute too.  */
1152       howto->pc_relative = FALSE;
1153       howto->complain_on_overflow = complain_overflow_bitfield;
1154     }
1155   else
1156     {
1157       /* Use a PC-relative howto and subtract the instruction's address
1158          from the target address we calculated above.  */
1159       howto->pc_relative = TRUE;
1160       *relocation -= (input_section->output_section->vma
1161                       + input_section->output_offset
1162                       + section_offset);
1163     }
1164   return TRUE;
1165 }
1166
1167 /* This is the relocation function for the PowerPC64.
1168    See xcoff_ppc_relocation_section for more information. */
1169
1170 bfd_boolean
1171 xcoff64_ppc_relocate_section (bfd *output_bfd,
1172                               struct bfd_link_info *info,
1173                               bfd *input_bfd,
1174                               asection *input_section,
1175                               bfd_byte *contents,
1176                               struct internal_reloc *relocs,
1177                               struct internal_syment *syms,
1178                               asection **sections)
1179 {
1180   struct internal_reloc *rel;
1181   struct internal_reloc *relend;
1182
1183   rel = relocs;
1184   relend = rel + input_section->reloc_count;
1185   for (; rel < relend; rel++)
1186     {
1187       long symndx;
1188       struct xcoff_link_hash_entry *h;
1189       struct internal_syment *sym;
1190       bfd_vma addend;
1191       bfd_vma val;
1192       struct reloc_howto_struct howto;
1193       bfd_vma relocation;
1194       bfd_vma value_to_relocate;
1195       bfd_vma address;
1196       bfd_byte *location;
1197
1198       /* Relocation type R_REF is a special relocation type which is
1199          merely used to prevent garbage collection from occurring for
1200          the csect including the symbol which it references.  */
1201       if (rel->r_type == R_REF)
1202         continue;
1203
1204       /* howto */
1205       howto.type = rel->r_type;
1206       howto.rightshift = 0;
1207       howto.bitsize = (rel->r_size & 0x3f) + 1;
1208       howto.size = howto.bitsize > 16 ? (howto.bitsize > 32 ? 4 : 2) : 1;
1209       howto.pc_relative = FALSE;
1210       howto.bitpos = 0;
1211       howto.complain_on_overflow = (rel->r_size & 0x80
1212                                     ? complain_overflow_signed
1213                                     : complain_overflow_bitfield);
1214       howto.special_function = NULL;
1215       howto.name = "internal";
1216       howto.partial_inplace = TRUE;
1217       howto.src_mask = howto.dst_mask = N_ONES (howto.bitsize);
1218       howto.pcrel_offset = FALSE;
1219
1220       /* symbol */
1221       val = 0;
1222       addend = 0;
1223       h = NULL;
1224       sym = NULL;
1225       symndx = rel->r_symndx;
1226
1227       if (-1 != symndx)
1228         {
1229           asection *sec;
1230
1231           h = obj_xcoff_sym_hashes (input_bfd)[symndx];
1232           sym = syms + symndx;
1233           addend = - sym->n_value;
1234
1235           if (NULL == h)
1236             {
1237               sec = sections[symndx];
1238               /* Hack to make sure we use the right TOC anchor value
1239                  if this reloc is against the TOC anchor.  */
1240               if (sec->name[3] == '0'
1241                   && strcmp (sec->name, ".tc0") == 0)
1242                 val = xcoff_data (output_bfd)->toc;
1243               else
1244                 val = (sec->output_section->vma
1245                        + sec->output_offset
1246                        + sym->n_value
1247                        - sec->vma);
1248             }
1249           else
1250             {
1251               if (info->unresolved_syms_in_objects != RM_IGNORE
1252                   && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
1253                 (*info->callbacks->undefined_symbol)
1254                   (info, h->root.root.string, input_bfd, input_section,
1255                    rel->r_vaddr - input_section->vma,
1256                    info->unresolved_syms_in_objects == RM_GENERATE_ERROR);
1257
1258               if (h->root.type == bfd_link_hash_defined
1259                   || h->root.type == bfd_link_hash_defweak)
1260                 {
1261                   sec = h->root.u.def.section;
1262                   val = (h->root.u.def.value
1263                          + sec->output_section->vma
1264                          + sec->output_offset);
1265                 }
1266               else if (h->root.type == bfd_link_hash_common)
1267                 {
1268                   sec = h->root.u.c.p->section;
1269                   val = (sec->output_section->vma
1270                          + sec->output_offset);
1271                 }
1272               else
1273                 {
1274                   BFD_ASSERT (bfd_link_relocatable (info)
1275                               || (h->flags & XCOFF_DEF_DYNAMIC) != 0
1276                               || (h->flags & XCOFF_IMPORT) != 0);
1277                 }
1278             }
1279         }
1280
1281       if (rel->r_type >= XCOFF_MAX_CALCULATE_RELOCATION
1282           || !((*xcoff64_calculate_relocation[rel->r_type])
1283               (input_bfd, input_section, output_bfd, rel, sym, &howto, val,
1284                addend, &relocation, contents)))
1285         return FALSE;
1286
1287       /* address */
1288       address = rel->r_vaddr - input_section->vma;
1289       location = contents + address;
1290
1291       if (address > input_section->size)
1292         abort ();
1293
1294       /* Get the value we are going to relocate.  */
1295       if (1 == howto.size)
1296         value_to_relocate = bfd_get_16 (input_bfd, location);
1297       else if (2 == howto.size)
1298         value_to_relocate = bfd_get_32 (input_bfd, location);
1299       else
1300         value_to_relocate = bfd_get_64 (input_bfd, location);
1301
1302       /* overflow.
1303
1304          FIXME: We may drop bits during the addition
1305          which we don't check for.  We must either check at every single
1306          operation, which would be tedious, or we must do the computations
1307          in a type larger than bfd_vma, which would be inefficient.  */
1308
1309       if ((unsigned int) howto.complain_on_overflow
1310           >= XCOFF_MAX_COMPLAIN_OVERFLOW)
1311         abort ();
1312
1313       if (((*xcoff_complain_overflow[howto.complain_on_overflow])
1314            (input_bfd, value_to_relocate, relocation, &howto)))
1315         {
1316           const char *name;
1317           char buf[SYMNMLEN + 1];
1318           char reloc_type_name[10];
1319
1320           if (symndx == -1)
1321             {
1322               name = "*ABS*";
1323             }
1324           else if (h != NULL)
1325             {
1326               name = NULL;
1327             }
1328           else
1329             {
1330               name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
1331               if (name == NULL)
1332                 name = "UNKNOWN";
1333             }
1334           sprintf (reloc_type_name, "0x%02x", rel->r_type);
1335
1336           (*info->callbacks->reloc_overflow)
1337             (info, (h ? &h->root : NULL), name, reloc_type_name,
1338              (bfd_vma) 0, input_bfd, input_section,
1339              rel->r_vaddr - input_section->vma);
1340         }
1341
1342       /* Add RELOCATION to the right bits of VALUE_TO_RELOCATE.  */
1343       value_to_relocate = ((value_to_relocate & ~howto.dst_mask)
1344                            | (((value_to_relocate & howto.src_mask)
1345                                + relocation) & howto.dst_mask));
1346
1347       /* Put the value back in the object file.  */
1348       if (1 == howto.size)
1349         bfd_put_16 (input_bfd, value_to_relocate, location);
1350       else if (2 == howto.size)
1351         bfd_put_32 (input_bfd, value_to_relocate, location);
1352       else
1353         bfd_put_64 (input_bfd, value_to_relocate, location);
1354
1355     }
1356   return TRUE;
1357 }
1358
1359 \f
1360 /* The XCOFF reloc table.  Actually, XCOFF relocations specify the
1361    bitsize and whether they are signed or not, along with a
1362    conventional type.  This table is for the types, which are used for
1363    different algorithms for putting in the reloc.  Many of these
1364    relocs need special_function entries, which I have not written.  */
1365
1366 reloc_howto_type xcoff64_howto_table[] =
1367 {
1368   /* 0x00: Standard 64 bit relocation.  */
1369   HOWTO (R_POS,                 /* type */
1370          0,                     /* rightshift */
1371          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1372          64,                    /* bitsize */
1373          FALSE,                 /* pc_relative */
1374          0,                     /* bitpos */
1375          complain_overflow_bitfield, /* complain_on_overflow */
1376          0,                     /* special_function */
1377          "R_POS_64",            /* name */
1378          TRUE,                  /* partial_inplace */
1379          MINUS_ONE,             /* src_mask */
1380          MINUS_ONE,             /* dst_mask */
1381          FALSE),                /* pcrel_offset */
1382
1383   /* 0x01: 64 bit relocation, but store negative value.  */
1384   HOWTO (R_NEG,                 /* type */
1385          0,                     /* rightshift */
1386          -4,                    /* size (0 = byte, 1 = short, 2 = long) */
1387          64,                    /* bitsize */
1388          FALSE,                 /* pc_relative */
1389          0,                     /* bitpos */
1390          complain_overflow_bitfield, /* complain_on_overflow */
1391          0,                     /* special_function */
1392          "R_NEG",               /* name */
1393          TRUE,                  /* partial_inplace */
1394          MINUS_ONE,             /* src_mask */
1395          MINUS_ONE,             /* dst_mask */
1396          FALSE),                /* pcrel_offset */
1397
1398   /* 0x02: 32 bit PC relative relocation.  */
1399   HOWTO (R_REL,                 /* type */
1400          0,                     /* rightshift */
1401          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1402          32,                    /* bitsize */
1403          TRUE,                  /* pc_relative */
1404          0,                     /* bitpos */
1405          complain_overflow_signed, /* complain_on_overflow */
1406          0,                     /* special_function */
1407          "R_REL",               /* name */
1408          TRUE,                  /* partial_inplace */
1409          0xffffffff,            /* src_mask */
1410          0xffffffff,            /* dst_mask */
1411          FALSE),                /* pcrel_offset */
1412
1413   /* 0x03: 16 bit TOC relative relocation.  */
1414   HOWTO (R_TOC,                 /* type */
1415          0,                     /* rightshift */
1416          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1417          16,                    /* bitsize */
1418          FALSE,                 /* pc_relative */
1419          0,                     /* bitpos */
1420          complain_overflow_bitfield, /* complain_on_overflow */
1421          0,                     /* special_function */
1422          "R_TOC",               /* name */
1423          TRUE,                  /* partial_inplace */
1424          0xffff,                /* src_mask */
1425          0xffff,                /* dst_mask */
1426          FALSE),                /* pcrel_offset */
1427
1428   /* 0x04: I don't really know what this is.    */
1429   HOWTO (R_RTB,                 /* type */
1430          1,                     /* rightshift */
1431          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1432          32,                    /* bitsize */
1433          FALSE,                 /* pc_relative */
1434          0,                     /* bitpos */
1435          complain_overflow_bitfield, /* complain_on_overflow */
1436          0,                     /* special_function */
1437          "R_RTB",               /* name */
1438          TRUE,                  /* partial_inplace */
1439          0xffffffff,            /* src_mask */
1440          0xffffffff,            /* dst_mask */
1441          FALSE),                /* pcrel_offset */
1442
1443   /* 0x05: External TOC relative symbol.  */
1444   HOWTO (R_GL,                  /* type */
1445          0,                     /* rightshift */
1446          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1447          16,                    /* bitsize */
1448          FALSE,                 /* pc_relative */
1449          0,                     /* bitpos */
1450          complain_overflow_bitfield, /* complain_on_overflow */
1451          0,                     /* special_function */
1452          "R_GL",                /* name */
1453          TRUE,                  /* partial_inplace */
1454          0xffff,                /* src_mask */
1455          0xffff,                /* dst_mask */
1456          FALSE),                /* pcrel_offset */
1457
1458   /* 0x06: Local TOC relative symbol.    */
1459   HOWTO (R_TCL,                 /* type */
1460          0,                     /* rightshift */
1461          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1462          16,                    /* bitsize */
1463          FALSE,                 /* pc_relative */
1464          0,                     /* bitpos */
1465          complain_overflow_bitfield, /* complain_on_overflow */
1466          0,                     /* special_function */
1467          "R_TCL",               /* name */
1468          TRUE,                  /* partial_inplace */
1469          0xffff,                /* src_mask */
1470          0xffff,                /* dst_mask */
1471          FALSE),                /* pcrel_offset */
1472
1473   EMPTY_HOWTO (7),
1474
1475   /* 0x08: Non modifiable absolute branch.  */
1476   HOWTO (R_BA,                  /* type */
1477          0,                     /* rightshift */
1478          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1479          26,                    /* bitsize */
1480          FALSE,                 /* pc_relative */
1481          0,                     /* bitpos */
1482          complain_overflow_bitfield, /* complain_on_overflow */
1483          0,                     /* special_function */
1484          "R_BA_26",             /* name */
1485          TRUE,                  /* partial_inplace */
1486          0x03fffffc,            /* src_mask */
1487          0x03fffffc,            /* dst_mask */
1488          FALSE),                /* pcrel_offset */
1489
1490   EMPTY_HOWTO (9),
1491
1492   /* 0x0a: Non modifiable relative branch.  */
1493   HOWTO (R_BR,                  /* type */
1494          0,                     /* rightshift */
1495          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1496          26,                    /* bitsize */
1497          TRUE,                  /* pc_relative */
1498          0,                     /* bitpos */
1499          complain_overflow_signed, /* complain_on_overflow */
1500          0,                     /* special_function */
1501          "R_BR",                /* name */
1502          TRUE,                  /* partial_inplace */
1503          0x03fffffc,            /* src_mask */
1504          0x03fffffc,            /* dst_mask */
1505          FALSE),                /* pcrel_offset */
1506
1507   EMPTY_HOWTO (0xb),
1508
1509   /* 0x0c: Indirect load.  */
1510   HOWTO (R_RL,                  /* type */
1511          0,                     /* rightshift */
1512          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1513          16,                    /* bitsize */
1514          FALSE,                 /* pc_relative */
1515          0,                     /* bitpos */
1516          complain_overflow_bitfield, /* complain_on_overflow */
1517          0,                     /* special_function */
1518          "R_RL",                /* name */
1519          TRUE,                  /* partial_inplace */
1520          0xffff,                /* src_mask */
1521          0xffff,                /* dst_mask */
1522          FALSE),                /* pcrel_offset */
1523
1524   /* 0x0d: Load address.  */
1525   HOWTO (R_RLA,                 /* type */
1526          0,                     /* rightshift */
1527          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1528          16,                    /* bitsize */
1529          FALSE,                 /* pc_relative */
1530          0,                     /* bitpos */
1531          complain_overflow_bitfield, /* complain_on_overflow */
1532          0,                     /* special_function */
1533          "R_RLA",               /* name */
1534          TRUE,                  /* partial_inplace */
1535          0xffff,                /* src_mask */
1536          0xffff,                /* dst_mask */
1537          FALSE),                /* pcrel_offset */
1538
1539   EMPTY_HOWTO (0xe),
1540
1541   /* 0x0f: Non-relocating reference.  Bitsize is 1 so that r_rsize is 0.  */
1542   HOWTO (R_REF,                 /* type */
1543          0,                     /* rightshift */
1544          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1545          1,                     /* bitsize */
1546          FALSE,                 /* pc_relative */
1547          0,                     /* bitpos */
1548          complain_overflow_dont, /* complain_on_overflow */
1549          0,                     /* special_function */
1550          "R_REF",               /* name */
1551          FALSE,                 /* partial_inplace */
1552          0,                     /* src_mask */
1553          0,                     /* dst_mask */
1554          FALSE),                /* pcrel_offset */
1555
1556   EMPTY_HOWTO (0x10),
1557   EMPTY_HOWTO (0x11),
1558
1559   /* 0x12: TOC relative indirect load.  */
1560   HOWTO (R_TRL,                 /* type */
1561          0,                     /* rightshift */
1562          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1563          16,                    /* bitsize */
1564          FALSE,                 /* pc_relative */
1565          0,                     /* bitpos */
1566          complain_overflow_bitfield, /* complain_on_overflow */
1567          0,                     /* special_function */
1568          "R_TRL",               /* name */
1569          TRUE,                  /* partial_inplace */
1570          0xffff,                /* src_mask */
1571          0xffff,                /* dst_mask */
1572          FALSE),                /* pcrel_offset */
1573
1574   /* 0x13: TOC relative load address.    */
1575   HOWTO (R_TRLA,                /* type */
1576          0,                     /* rightshift */
1577          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1578          16,                    /* bitsize */
1579          FALSE,                 /* pc_relative */
1580          0,                     /* bitpos */
1581          complain_overflow_bitfield, /* complain_on_overflow */
1582          0,                     /* special_function */
1583          "R_TRLA",              /* name */
1584          TRUE,                  /* partial_inplace */
1585          0xffff,                /* src_mask */
1586          0xffff,                /* dst_mask */
1587          FALSE),                /* pcrel_offset */
1588
1589   /* 0x14: Modifiable relative branch.  */
1590   HOWTO (R_RRTBI,               /* type */
1591          1,                     /* rightshift */
1592          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1593          32,                    /* bitsize */
1594          FALSE,                 /* pc_relative */
1595          0,                     /* bitpos */
1596          complain_overflow_bitfield, /* complain_on_overflow */
1597          0,                     /* special_function */
1598          "R_RRTBI",             /* name */
1599          TRUE,                  /* partial_inplace */
1600          0xffffffff,            /* src_mask */
1601          0xffffffff,            /* dst_mask */
1602          FALSE),                /* pcrel_offset */
1603
1604   /* 0x15: Modifiable absolute branch.  */
1605   HOWTO (R_RRTBA,               /* type */
1606          1,                     /* rightshift */
1607          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1608          32,                    /* bitsize */
1609          FALSE,                 /* pc_relative */
1610          0,                     /* bitpos */
1611          complain_overflow_bitfield, /* complain_on_overflow */
1612          0,                     /* special_function */
1613          "R_RRTBA",             /* name */
1614          TRUE,                  /* partial_inplace */
1615          0xffffffff,            /* src_mask */
1616          0xffffffff,            /* dst_mask */
1617          FALSE),                /* pcrel_offset */
1618
1619   /* 0x16: Modifiable call absolute indirect.    */
1620   HOWTO (R_CAI,                 /* type */
1621          0,                     /* rightshift */
1622          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1623          16,                    /* bitsize */
1624          FALSE,                 /* pc_relative */
1625          0,                     /* bitpos */
1626          complain_overflow_bitfield, /* complain_on_overflow */
1627          0,                     /* special_function */
1628          "R_CAI",               /* name */
1629          TRUE,                  /* partial_inplace */
1630          0xffff,                /* src_mask */
1631          0xffff,                /* dst_mask */
1632          FALSE),                /* pcrel_offset */
1633
1634   /* 0x17: Modifiable call relative.    */
1635   HOWTO (R_CREL,                /* type */
1636          0,                     /* rightshift */
1637          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1638          16,                    /* bitsize */
1639          FALSE,                 /* pc_relative */
1640          0,                     /* bitpos */
1641          complain_overflow_bitfield, /* complain_on_overflow */
1642          0,                     /* special_function */
1643          "R_CREL",              /* name */
1644          TRUE,                  /* partial_inplace */
1645          0xffff,                /* src_mask */
1646          0xffff,                /* dst_mask */
1647          FALSE),                /* pcrel_offset */
1648
1649   /* 0x18: Modifiable branch absolute.  */
1650   HOWTO (R_RBA,                 /* type */
1651          0,                     /* rightshift */
1652          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1653          26,                    /* bitsize */
1654          FALSE,                 /* pc_relative */
1655          0,                     /* bitpos */
1656          complain_overflow_bitfield, /* complain_on_overflow */
1657          0,                     /* special_function */
1658          "R_RBA",               /* name */
1659          TRUE,                  /* partial_inplace */
1660          0x03fffffc,            /* src_mask */
1661          0x03fffffc,            /* dst_mask */
1662          FALSE),                /* pcrel_offset */
1663
1664   /* 0x19: Modifiable branch absolute.  */
1665   HOWTO (R_RBAC,                /* type */
1666          0,                     /* rightshift */
1667          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1668          32,                    /* bitsize */
1669          FALSE,                 /* pc_relative */
1670          0,                     /* bitpos */
1671          complain_overflow_bitfield, /* complain_on_overflow */
1672          0,                     /* special_function */
1673          "R_RBAC",              /* name */
1674          TRUE,                  /* partial_inplace */
1675          0xffffffff,            /* src_mask */
1676          0xffffffff,            /* dst_mask */
1677          FALSE),                /* pcrel_offset */
1678
1679   /* 0x1a: Modifiable branch relative.  */
1680   HOWTO (R_RBR,                 /* type */
1681          0,                     /* rightshift */
1682          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1683          26,                    /* bitsize */
1684          FALSE,                 /* pc_relative */
1685          0,                     /* bitpos */
1686          complain_overflow_signed, /* complain_on_overflow */
1687          0,                     /* special_function */
1688          "R_RBR_26",            /* name */
1689          TRUE,                  /* partial_inplace */
1690          0x03fffffc,            /* src_mask */
1691          0x03fffffc,            /* dst_mask */
1692          FALSE),                /* pcrel_offset */
1693
1694   /* 0x1b: Modifiable branch absolute.  */
1695   HOWTO (R_RBRC,                /* type */
1696          0,                     /* rightshift */
1697          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1698          16,                    /* bitsize */
1699          FALSE,                 /* pc_relative */
1700          0,                     /* bitpos */
1701          complain_overflow_bitfield, /* complain_on_overflow */
1702          0,                     /* special_function */
1703          "R_RBRC",              /* name */
1704          TRUE,                  /* partial_inplace */
1705          0xffff,                /* src_mask */
1706          0xffff,                /* dst_mask */
1707          FALSE),                /* pcrel_offset */
1708
1709   /* 0x1c: Standard 32 bit relocation.  */
1710   HOWTO (R_POS,                 /* type */
1711          0,                     /* rightshift */
1712          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1713          32,                    /* bitsize */
1714          FALSE,                 /* pc_relative */
1715          0,                     /* bitpos */
1716          complain_overflow_bitfield, /* complain_on_overflow */
1717          0,                     /* special_function */
1718          "R_POS_32",            /* name */
1719          TRUE,                  /* partial_inplace */
1720          0xffffffff,            /* src_mask */
1721          0xffffffff,            /* dst_mask */
1722          FALSE),                /* pcrel_offset */
1723
1724   /* 0x1d: 16 bit Non modifiable absolute branch.  */
1725   HOWTO (R_BA,                  /* type */
1726          0,                     /* rightshift */
1727          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1728          16,                    /* bitsize */
1729          FALSE,                 /* pc_relative */
1730          0,                     /* bitpos */
1731          complain_overflow_bitfield, /* complain_on_overflow */
1732          0,                     /* special_function */
1733          "R_BA_16",             /* name */
1734          TRUE,                  /* partial_inplace */
1735          0xfffc,                /* src_mask */
1736          0xfffc,                /* dst_mask */
1737          FALSE),                /* pcrel_offset */
1738
1739   /* 0x1e: Modifiable branch relative.  */
1740   HOWTO (R_RBR,                 /* type */
1741          0,                     /* rightshift */
1742          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1743          16,                    /* bitsize */
1744          TRUE,                  /* pc_relative */
1745          0,                     /* bitpos */
1746          complain_overflow_signed, /* complain_on_overflow */
1747          0,                     /* special_function */
1748          "R_RBR_16",            /* name */
1749          TRUE,                  /* partial_inplace */
1750          0xfffc,                /* src_mask */
1751          0xfffc,                /* dst_mask */
1752          FALSE),                /* pcrel_offset */
1753
1754   /* 0x1f: Modifiable branch absolute.  */
1755   HOWTO (R_RBA,                 /* type */
1756          0,                     /* rightshift */
1757          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1758          16,                    /* bitsize */
1759          FALSE,                 /* pc_relative */
1760          0,                     /* bitpos */
1761          complain_overflow_bitfield, /* complain_on_overflow */
1762          0,                     /* special_function */
1763          "R_RBA_16",            /* name */
1764          TRUE,                  /* partial_inplace */
1765          0xffff,                /* src_mask */
1766          0xffff,                /* dst_mask */
1767          FALSE),                /* pcrel_offset */
1768
1769 };
1770
1771 void
1772 xcoff64_rtype2howto (arelent *relent, struct internal_reloc *internal)
1773 {
1774   if (internal->r_type > R_RBRC)
1775     abort ();
1776
1777   /* Default howto layout works most of the time */
1778   relent->howto = &xcoff64_howto_table[internal->r_type];
1779
1780   /* Special case some 16 bit reloc */
1781   if (15 == (internal->r_size & 0x3f))
1782     {
1783       if (R_BA == internal->r_type)
1784         relent->howto = &xcoff64_howto_table[0x1d];
1785       else if (R_RBR == internal->r_type)
1786         relent->howto = &xcoff64_howto_table[0x1e];
1787       else if (R_RBA == internal->r_type)
1788         relent->howto = &xcoff64_howto_table[0x1f];
1789     }
1790   /* Special case 32 bit */
1791   else if (31 == (internal->r_size & 0x3f))
1792     {
1793       if (R_POS == internal->r_type)
1794         relent->howto = &xcoff64_howto_table[0x1c];
1795     }
1796
1797   /* The r_size field of an XCOFF reloc encodes the bitsize of the
1798      relocation, as well as indicating whether it is signed or not.
1799      Doublecheck that the relocation information gathered from the
1800      type matches this information.  The bitsize is not significant
1801      for R_REF relocs.  */
1802   if (relent->howto->dst_mask != 0
1803       && (relent->howto->bitsize
1804           != ((unsigned int) internal->r_size & 0x3f) + 1))
1805     abort ();
1806 }
1807
1808 reloc_howto_type *
1809 xcoff64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1810                            bfd_reloc_code_real_type code)
1811 {
1812   switch (code)
1813     {
1814     case BFD_RELOC_PPC_B26:
1815       return &xcoff64_howto_table[0xa];
1816     case BFD_RELOC_PPC_BA16:
1817       return &xcoff64_howto_table[0x1d];
1818     case BFD_RELOC_PPC_BA26:
1819       return &xcoff64_howto_table[8];
1820     case BFD_RELOC_PPC_TOC16:
1821       return &xcoff64_howto_table[3];
1822     case BFD_RELOC_16:
1823       /* Note that this relocation is only internally used by gas.  */
1824       return &xcoff64_howto_table[0xc];
1825     case BFD_RELOC_PPC_B16:
1826       return &xcoff64_howto_table[0x1e];
1827     case BFD_RELOC_32:
1828     case BFD_RELOC_CTOR:
1829       return &xcoff64_howto_table[0x1c];
1830     case BFD_RELOC_64:
1831       return &xcoff64_howto_table[0];
1832     case BFD_RELOC_NONE:
1833       return &xcoff64_howto_table[0xf];
1834     default:
1835       return NULL;
1836     }
1837 }
1838
1839 static reloc_howto_type *
1840 xcoff64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1841                            const char *r_name)
1842 {
1843   unsigned int i;
1844
1845   for (i = 0;
1846        i < sizeof (xcoff64_howto_table) / sizeof (xcoff64_howto_table[0]);
1847        i++)
1848     if (xcoff64_howto_table[i].name != NULL
1849         && strcasecmp (xcoff64_howto_table[i].name, r_name) == 0)
1850       return &xcoff64_howto_table[i];
1851
1852   return NULL;
1853 }
1854
1855 /* PR 21786:  The PE/COFF standard does not require NUL termination for any of
1856    the ASCII fields in the archive headers.  So in order to be able to extract
1857    numerical values we provide our own versions of strtol and strtoll which
1858    take a maximum length as an additional parameter.  Also - just to save space,
1859    we omit the endptr return parameter, since we know that it is never used.  */
1860
1861 static long
1862 _bfd_strntol (const char * nptr, int base, unsigned int maxlen)
1863 {
1864   char buf[24]; /* Should be enough.  */
1865
1866   BFD_ASSERT (maxlen < (sizeof (buf) - 1));
1867
1868   memcpy (buf, nptr, maxlen);
1869   buf[maxlen] = 0;
1870   return strtol (buf, NULL, base);
1871 }
1872
1873 static long long
1874 _bfd_strntoll (const char * nptr, int base, unsigned int maxlen)
1875 {
1876   char buf[32]; /* Should be enough.  */
1877
1878   BFD_ASSERT (maxlen < (sizeof (buf) - 1));
1879
1880   memcpy (buf, nptr, maxlen);
1881   buf[maxlen] = 0;
1882   return strtoll (buf, NULL, base);
1883 }
1884
1885 /* Macro to read an ASCII value stored in an archive header field.  */
1886 #define GET_VALUE_IN_FIELD(VAR, FIELD)            \
1887   do                                              \
1888     {                                             \
1889       (VAR) = sizeof (VAR) > sizeof (long)        \
1890         ? _bfd_strntoll (FIELD, 10, sizeof FIELD) \
1891         : _bfd_strntol (FIELD, 10, sizeof FIELD); \
1892     }                                             \
1893   while (0)
1894
1895 /* Read in the armap of an XCOFF archive.  */
1896
1897 static bfd_boolean
1898 xcoff64_slurp_armap (bfd *abfd)
1899 {
1900   file_ptr off;
1901   size_t namlen;
1902   bfd_size_type sz, amt;
1903   bfd_byte *contents, *cend;
1904   bfd_vma c, i;
1905   carsym *arsym;
1906   bfd_byte *p;
1907   file_ptr pos;
1908
1909   /* This is for the new format.  */
1910   struct xcoff_ar_hdr_big hdr;
1911
1912   if (xcoff_ardata (abfd) == NULL)
1913     {
1914       bfd_has_map (abfd) = FALSE;
1915       return TRUE;
1916     }
1917
1918   off = bfd_scan_vma (xcoff_ardata_big (abfd)->symoff64,
1919                       (const char **) NULL, 10);
1920   if (off == 0)
1921     {
1922       bfd_has_map (abfd) = FALSE;
1923       return TRUE;
1924     }
1925
1926   if (bfd_seek (abfd, off, SEEK_SET) != 0)
1927     return FALSE;
1928
1929   /* The symbol table starts with a normal archive header.  */
1930   if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR_BIG, abfd)
1931       != SIZEOF_AR_HDR_BIG)
1932     return FALSE;
1933
1934   /* Skip the name (normally empty).  */
1935   GET_VALUE_IN_FIELD (namlen, hdr.namlen);
1936   pos = ((namlen + 1) & ~(size_t) 1) + SXCOFFARFMAG;
1937   if (bfd_seek (abfd, pos, SEEK_CUR) != 0)
1938     return FALSE;
1939
1940   sz = bfd_scan_vma (hdr.size, (const char **) NULL, 10);
1941
1942   /* Read in the entire symbol table.  */
1943   contents = (bfd_byte *) bfd_alloc (abfd, sz);
1944   if (contents == NULL)
1945     return FALSE;
1946   if (bfd_bread (contents, sz, abfd) != sz)
1947     return FALSE;
1948
1949   /* The symbol table starts with an eight byte count.  */
1950   c = H_GET_64 (abfd, contents);
1951
1952   if (c * 8 >= sz)
1953     {
1954       bfd_set_error (bfd_error_bad_value);
1955       return FALSE;
1956     }
1957   amt = c;
1958   amt *= sizeof (carsym);
1959   bfd_ardata (abfd)->symdefs = (carsym *) bfd_alloc (abfd, amt);
1960   if (bfd_ardata (abfd)->symdefs == NULL)
1961     return FALSE;
1962
1963   /* After the count comes a list of eight byte file offsets.  */
1964   for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 8;
1965        i < c;
1966        ++i, ++arsym, p += 8)
1967     arsym->file_offset = H_GET_64 (abfd, p);
1968
1969   /* After the file offsets come null terminated symbol names.  */
1970   cend = contents + sz;
1971   for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
1972        i < c;
1973        ++i, ++arsym, p += strlen ((char *) p) + 1)
1974     {
1975       if (p >= cend)
1976         {
1977           bfd_set_error (bfd_error_bad_value);
1978           return FALSE;
1979         }
1980       arsym->name = (char *) p;
1981     }
1982
1983   bfd_ardata (abfd)->symdef_count = c;
1984   bfd_has_map (abfd) = TRUE;
1985
1986   return TRUE;
1987 }
1988
1989
1990 /* See if this is an NEW XCOFF archive.  */
1991
1992 static const bfd_target *
1993 xcoff64_archive_p (bfd *abfd)
1994 {
1995   struct artdata *tdata_hold;
1996   char magic[SXCOFFARMAG];
1997   /* This is the new format.  */
1998   struct xcoff_ar_file_hdr_big hdr;
1999   bfd_size_type amt = SXCOFFARMAG;
2000
2001   if (bfd_bread (magic, amt, abfd) != amt)
2002     {
2003       if (bfd_get_error () != bfd_error_system_call)
2004         bfd_set_error (bfd_error_wrong_format);
2005       return NULL;
2006     }
2007
2008   if (strncmp (magic, XCOFFARMAGBIG, SXCOFFARMAG) != 0)
2009     {
2010       bfd_set_error (bfd_error_wrong_format);
2011       return NULL;
2012     }
2013
2014   /* Copy over the magic string.  */
2015   memcpy (hdr.magic, magic, SXCOFFARMAG);
2016
2017   /* Now read the rest of the file header.  */
2018   amt = SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG;
2019   if (bfd_bread (&hdr.memoff, amt, abfd) != amt)
2020     {
2021       if (bfd_get_error () != bfd_error_system_call)
2022         bfd_set_error (bfd_error_wrong_format);
2023       return NULL;
2024     }
2025
2026   tdata_hold = bfd_ardata (abfd);
2027
2028   amt = sizeof (struct artdata);
2029   bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt);
2030   if (bfd_ardata (abfd) == (struct artdata *) NULL)
2031     goto error_ret_restore;
2032
2033   /* Already cleared by bfd_zalloc above.
2034      bfd_ardata (abfd)->cache = NULL;
2035      bfd_ardata (abfd)->archive_head = NULL;
2036      bfd_ardata (abfd)->symdefs = NULL;
2037      bfd_ardata (abfd)->extended_names = NULL;
2038      bfd_ardata (abfd)->extended_names_size = 0;  */
2039   bfd_ardata (abfd)->first_file_filepos = bfd_scan_vma (hdr.firstmemoff,
2040                                                         (const char **) NULL,
2041                                                         10);
2042
2043   amt = SIZEOF_AR_FILE_HDR_BIG;
2044   bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
2045   if (bfd_ardata (abfd)->tdata == NULL)
2046     goto error_ret;
2047
2048   memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR_BIG);
2049
2050   if (! xcoff64_slurp_armap (abfd))
2051     {
2052     error_ret:
2053       bfd_release (abfd, bfd_ardata (abfd));
2054     error_ret_restore:
2055       bfd_ardata (abfd) = tdata_hold;
2056       return NULL;
2057     }
2058
2059   return abfd->xvec;
2060 }
2061
2062
2063 /* Open the next element in an XCOFF archive.  */
2064
2065 static bfd *
2066 xcoff64_openr_next_archived_file (bfd *archive, bfd *last_file)
2067 {
2068   bfd_vma filestart;
2069
2070   if ((xcoff_ardata (archive) == NULL)
2071       || ! xcoff_big_format_p (archive))
2072     {
2073       bfd_set_error (bfd_error_invalid_operation);
2074       return NULL;
2075     }
2076
2077   if (last_file == NULL)
2078     {
2079       filestart = bfd_ardata (archive)->first_file_filepos;
2080     }
2081   else
2082     {
2083       filestart = bfd_scan_vma (arch_xhdr_big (last_file)->nextoff,
2084                                 (const char **) NULL, 10);
2085     }
2086
2087   if (filestart == 0
2088       || filestart == bfd_scan_vma (xcoff_ardata_big (archive)->memoff,
2089                                     (const char **) NULL, 10)
2090       || filestart == bfd_scan_vma (xcoff_ardata_big (archive)->symoff,
2091                                     (const char **) NULL, 10))
2092     {
2093       bfd_set_error (bfd_error_no_more_archived_files);
2094       return NULL;
2095     }
2096
2097   return _bfd_get_elt_at_filepos (archive, (file_ptr) filestart);
2098 }
2099
2100 /* We can't use the usual coff_sizeof_headers routine, because AIX
2101    always uses an a.out header.  */
2102
2103 static int
2104 xcoff64_sizeof_headers (bfd *abfd,
2105                         struct bfd_link_info *info ATTRIBUTE_UNUSED)
2106 {
2107   int size;
2108
2109   size = bfd_coff_filhsz (abfd);
2110
2111   /* Don't think the small aout header can be used since some of the
2112      old elements have been reordered past the end of the old coff
2113      small aout size.  */
2114
2115   if (xcoff_data (abfd)->full_aouthdr)
2116     size += bfd_coff_aoutsz (abfd);
2117
2118   size += abfd->section_count * bfd_coff_scnhsz (abfd);
2119   return size;
2120 }
2121
2122 static asection *
2123 xcoff64_create_csect_from_smclas (bfd *abfd, union internal_auxent *aux,
2124                                   const char *symbol_name)
2125 {
2126   asection *return_value = NULL;
2127
2128   /* Changes from 32 :
2129      .sv == 8, is only for 32 bit programs
2130      .ti == 12 and .tb == 13 are now reserved.  */
2131   static const char *names[19] =
2132   {
2133     ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
2134     NULL, ".bs", ".ds", ".uc", NULL,  NULL,  NULL,  ".tc0",
2135     ".td", ".sv64", ".sv3264"
2136   };
2137
2138   if ((19 >= aux->x_csect.x_smclas)
2139       && (NULL != names[aux->x_csect.x_smclas]))
2140     {
2141
2142       return_value = bfd_make_section_anyway
2143         (abfd, names[aux->x_csect.x_smclas]);
2144
2145     }
2146   else
2147     {
2148       _bfd_error_handler
2149         /* xgettext: c-format */
2150         (_("%pB: symbol `%s' has unrecognized smclas %d"),
2151          abfd, symbol_name, aux->x_csect.x_smclas);
2152       bfd_set_error (bfd_error_bad_value);
2153     }
2154
2155   return return_value;
2156 }
2157
2158 static bfd_boolean
2159 xcoff64_is_lineno_count_overflow (bfd *abfd ATTRIBUTE_UNUSED,
2160                                   bfd_vma value ATTRIBUTE_UNUSED)
2161 {
2162   return FALSE;
2163 }
2164
2165 static bfd_boolean
2166 xcoff64_is_reloc_count_overflow (bfd *abfd ATTRIBUTE_UNUSED,
2167                                  bfd_vma value ATTRIBUTE_UNUSED)
2168 {
2169   return FALSE;
2170 }
2171
2172 static bfd_vma
2173 xcoff64_loader_symbol_offset (bfd *abfd ATTRIBUTE_UNUSED,
2174                               struct internal_ldhdr *ldhdr)
2175 {
2176   return (ldhdr->l_symoff);
2177 }
2178
2179 static bfd_vma
2180 xcoff64_loader_reloc_offset (bfd *abfd ATTRIBUTE_UNUSED,
2181                              struct internal_ldhdr *ldhdr)
2182 {
2183   return (ldhdr->l_rldoff);
2184 }
2185
2186 static bfd_boolean
2187 xcoff64_bad_format_hook (bfd * abfd, void *filehdr)
2188 {
2189   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
2190
2191   /* Check flavor first.  */
2192   if (bfd_get_flavour (abfd) != bfd_target_xcoff_flavour)
2193     return FALSE;
2194
2195   if (bfd_xcoff_magic_number (abfd) != internal_f->f_magic)
2196     return FALSE;
2197
2198   return TRUE;
2199 }
2200
2201 static bfd_boolean
2202 xcoff64_generate_rtinit (bfd *abfd, const char *init, const char *fini,
2203                          bfd_boolean rtld)
2204 {
2205   bfd_byte filehdr_ext[FILHSZ];
2206   bfd_byte scnhdr_ext[SCNHSZ * 3];
2207   bfd_byte syment_ext[SYMESZ * 10];
2208   bfd_byte reloc_ext[RELSZ * 3];
2209   bfd_byte *data_buffer;
2210   bfd_size_type data_buffer_size;
2211   bfd_byte *string_table, *st_tmp;
2212   bfd_size_type string_table_size;
2213   bfd_vma val;
2214   size_t initsz, finisz;
2215   struct internal_filehdr filehdr;
2216   struct internal_scnhdr text_scnhdr;
2217   struct internal_scnhdr data_scnhdr;
2218   struct internal_scnhdr bss_scnhdr;
2219   struct internal_syment syment;
2220   union internal_auxent auxent;
2221   struct internal_reloc reloc;
2222
2223   char *text_name = ".text";
2224   char *data_name = ".data";
2225   char *bss_name = ".bss";
2226   char *rtinit_name = "__rtinit";
2227   char *rtld_name = "__rtld";
2228
2229   if (! bfd_xcoff_rtinit_size (abfd))
2230     return FALSE;
2231
2232   initsz = (init == NULL ? 0 : 1 + strlen (init));
2233   finisz = (fini == NULL ? 0 : 1 + strlen (fini));
2234
2235   /* File header.  */
2236   memset (filehdr_ext, 0, FILHSZ);
2237   memset (&filehdr, 0, sizeof (struct internal_filehdr));
2238   filehdr.f_magic = bfd_xcoff_magic_number (abfd);
2239   filehdr.f_nscns = 3;
2240   filehdr.f_timdat = 0;
2241   filehdr.f_nsyms = 0;  /* at least 6, no more than 8 */
2242   filehdr.f_symptr = 0; /* set below */
2243   filehdr.f_opthdr = 0;
2244   filehdr.f_flags = 0;
2245
2246   /* Section headers.  */
2247   memset (scnhdr_ext, 0, 3 * SCNHSZ);
2248
2249   /* Text.  */
2250   memset (&text_scnhdr, 0, sizeof (struct internal_scnhdr));
2251   memcpy (text_scnhdr.s_name, text_name, strlen (text_name));
2252   text_scnhdr.s_paddr = 0;
2253   text_scnhdr.s_vaddr = 0;
2254   text_scnhdr.s_size = 0;
2255   text_scnhdr.s_scnptr = 0;
2256   text_scnhdr.s_relptr = 0;
2257   text_scnhdr.s_lnnoptr = 0;
2258   text_scnhdr.s_nreloc = 0;
2259   text_scnhdr.s_nlnno = 0;
2260   text_scnhdr.s_flags = STYP_TEXT;
2261
2262   /* Data.  */
2263   memset (&data_scnhdr, 0, sizeof (struct internal_scnhdr));
2264   memcpy (data_scnhdr.s_name, data_name, strlen (data_name));
2265   data_scnhdr.s_paddr = 0;
2266   data_scnhdr.s_vaddr = 0;
2267   data_scnhdr.s_size = 0;    /* set below */
2268   data_scnhdr.s_scnptr = FILHSZ + 3 * SCNHSZ;
2269   data_scnhdr.s_relptr = 0;  /* set below */
2270   data_scnhdr.s_lnnoptr = 0;
2271   data_scnhdr.s_nreloc = 0;  /* either 1 or 2 */
2272   data_scnhdr.s_nlnno = 0;
2273   data_scnhdr.s_flags = STYP_DATA;
2274
2275   /* Bss.  */
2276   memset (&bss_scnhdr, 0, sizeof (struct internal_scnhdr));
2277   memcpy (bss_scnhdr.s_name, bss_name, strlen (bss_name));
2278   bss_scnhdr.s_paddr = 0; /* set below */
2279   bss_scnhdr.s_vaddr = 0; /* set below */
2280   bss_scnhdr.s_size = 0;  /* set below */
2281   bss_scnhdr.s_scnptr = 0;
2282   bss_scnhdr.s_relptr = 0;
2283   bss_scnhdr.s_lnnoptr = 0;
2284   bss_scnhdr.s_nreloc = 0;
2285   bss_scnhdr.s_nlnno = 0;
2286   bss_scnhdr.s_flags = STYP_BSS;
2287
2288   /* .data
2289      0x0000           0x00000000 : rtl
2290      0x0004           0x00000000 :
2291      0x0008           0x00000018 : offset to init, or 0
2292      0x000C           0x00000038 : offset to fini, or 0
2293      0x0010           0x00000010 : size of descriptor
2294      0x0014           0x00000000 : pad
2295      0x0018           0x00000000 : init, needs a reloc
2296      0x001C           0x00000000 :
2297      0x0020           0x00000058 : offset to init name
2298      0x0024           0x00000000 : flags, padded to a word
2299      0x0028           0x00000000 : empty init
2300      0x002C           0x00000000 :
2301      0x0030           0x00000000 :
2302      0x0034           0x00000000 :
2303      0x0038           0x00000000 : fini, needs a reloc
2304      0x003C           0x00000000 :
2305      0x0040           0x00000??? : offset to fini name
2306      0x0044           0x00000000 : flags, padded to a word
2307      0x0048           0x00000000 : empty fini
2308      0x004C           0x00000000 :
2309      0x0050           0x00000000 :
2310      0x0054           0x00000000 :
2311      0x0058           init name
2312      0x0058 + initsz  fini name */
2313
2314   data_buffer_size = 0x0058 + initsz + finisz;
2315   data_buffer_size = (data_buffer_size + 7) &~ (bfd_size_type) 7;
2316   data_buffer = NULL;
2317   data_buffer = (bfd_byte *) bfd_zmalloc (data_buffer_size);
2318   if (data_buffer == NULL)
2319     return FALSE;
2320
2321   if (initsz)
2322     {
2323       val = 0x18;
2324       bfd_put_32 (abfd, val, &data_buffer[0x08]);
2325       val = 0x58;
2326       bfd_put_32 (abfd, val, &data_buffer[0x20]);
2327       memcpy (&data_buffer[val], init, initsz);
2328     }
2329
2330   if (finisz)
2331     {
2332       val = 0x38;
2333       bfd_put_32 (abfd, val, &data_buffer[0x0C]);
2334       val = 0x58 + initsz;
2335       bfd_put_32 (abfd, val, &data_buffer[0x40]);
2336       memcpy (&data_buffer[val], fini, finisz);
2337     }
2338
2339   val = 0x10;
2340   bfd_put_32 (abfd, val, &data_buffer[0x10]);
2341   data_scnhdr.s_size = data_buffer_size;
2342   bss_scnhdr.s_paddr = bss_scnhdr.s_vaddr = data_scnhdr.s_size;
2343
2344   /* String table.  */
2345   string_table_size = 4;
2346   string_table_size += strlen (data_name) + 1;
2347   string_table_size += strlen (rtinit_name) + 1;
2348   string_table_size += initsz;
2349   string_table_size += finisz;
2350   if (rtld)
2351     string_table_size += strlen (rtld_name) + 1;
2352
2353   string_table = (bfd_byte *) bfd_zmalloc (string_table_size);
2354   if (string_table == NULL)
2355     return FALSE;
2356
2357   val = string_table_size;
2358   bfd_put_32 (abfd, val, &string_table[0]);
2359   st_tmp = string_table + 4;
2360
2361   /* symbols
2362      0. .data csect
2363      2. __rtinit
2364      4. init function
2365      6. fini function
2366      8. __rtld  */
2367   memset (syment_ext, 0, 10 * SYMESZ);
2368   memset (reloc_ext, 0, 3 * RELSZ);
2369
2370   /* .data csect */
2371   memset (&syment, 0, sizeof (struct internal_syment));
2372   memset (&auxent, 0, sizeof (union internal_auxent));
2373
2374   syment._n._n_n._n_offset = st_tmp - string_table;
2375   memcpy (st_tmp, data_name, strlen (data_name));
2376   st_tmp += strlen (data_name) + 1;
2377
2378   syment.n_scnum = 2;
2379   syment.n_sclass = C_HIDEXT;
2380   syment.n_numaux = 1;
2381   auxent.x_csect.x_scnlen.l = data_buffer_size;
2382   auxent.x_csect.x_smtyp = 3 << 3 | XTY_SD;
2383   auxent.x_csect.x_smclas = XMC_RW;
2384   bfd_coff_swap_sym_out (abfd, &syment,
2385                          &syment_ext[filehdr.f_nsyms * SYMESZ]);
2386   bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
2387                          syment.n_numaux,
2388                          &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
2389   filehdr.f_nsyms += 2;
2390
2391   /* __rtinit */
2392   memset (&syment, 0, sizeof (struct internal_syment));
2393   memset (&auxent, 0, sizeof (union internal_auxent));
2394   syment._n._n_n._n_offset = st_tmp - string_table;
2395   memcpy (st_tmp, rtinit_name, strlen (rtinit_name));
2396   st_tmp += strlen (rtinit_name) + 1;
2397
2398   syment.n_scnum = 2;
2399   syment.n_sclass = C_EXT;
2400   syment.n_numaux = 1;
2401   auxent.x_csect.x_smtyp = XTY_LD;
2402   auxent.x_csect.x_smclas = XMC_RW;
2403   bfd_coff_swap_sym_out (abfd, &syment,
2404                          &syment_ext[filehdr.f_nsyms * SYMESZ]);
2405   bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
2406                          syment.n_numaux,
2407                          &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
2408   filehdr.f_nsyms += 2;
2409
2410   /* Init.  */
2411   if (initsz)
2412     {
2413       memset (&syment, 0, sizeof (struct internal_syment));
2414       memset (&auxent, 0, sizeof (union internal_auxent));
2415
2416       syment._n._n_n._n_offset = st_tmp - string_table;
2417       memcpy (st_tmp, init, initsz);
2418       st_tmp += initsz;
2419
2420       syment.n_sclass = C_EXT;
2421       syment.n_numaux = 1;
2422       bfd_coff_swap_sym_out (abfd, &syment,
2423                              &syment_ext[filehdr.f_nsyms * SYMESZ]);
2424       bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
2425                              syment.n_numaux,
2426                              &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
2427       /* Reloc.  */
2428       memset (&reloc, 0, sizeof (struct internal_reloc));
2429       reloc.r_vaddr = 0x0018;
2430       reloc.r_symndx = filehdr.f_nsyms;
2431       reloc.r_type = R_POS;
2432       reloc.r_size = 63;
2433       bfd_coff_swap_reloc_out (abfd, &reloc, &reloc_ext[0]);
2434
2435       filehdr.f_nsyms += 2;
2436       data_scnhdr.s_nreloc += 1;
2437     }
2438
2439   /* Finit.  */
2440   if (finisz)
2441     {
2442       memset (&syment, 0, sizeof (struct internal_syment));
2443       memset (&auxent, 0, sizeof (union internal_auxent));
2444
2445       syment._n._n_n._n_offset = st_tmp - string_table;
2446       memcpy (st_tmp, fini, finisz);
2447       st_tmp += finisz;
2448
2449       syment.n_sclass = C_EXT;
2450       syment.n_numaux = 1;
2451       bfd_coff_swap_sym_out (abfd, &syment,
2452                              &syment_ext[filehdr.f_nsyms * SYMESZ]);
2453       bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
2454                              syment.n_numaux,
2455                              &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
2456
2457       /* Reloc.  */
2458       memset (&reloc, 0, sizeof (struct internal_reloc));
2459       reloc.r_vaddr = 0x0038;
2460       reloc.r_symndx = filehdr.f_nsyms;
2461       reloc.r_type = R_POS;
2462       reloc.r_size = 63;
2463       bfd_coff_swap_reloc_out (abfd, &reloc,
2464                                &reloc_ext[data_scnhdr.s_nreloc * RELSZ]);
2465
2466       filehdr.f_nsyms += 2;
2467       data_scnhdr.s_nreloc += 1;
2468     }
2469
2470   if (rtld)
2471     {
2472       memset (&syment, 0, sizeof (struct internal_syment));
2473       memset (&auxent, 0, sizeof (union internal_auxent));
2474
2475       syment._n._n_n._n_offset = st_tmp - string_table;
2476       memcpy (st_tmp, rtld_name, strlen (rtld_name));
2477       st_tmp += strlen (rtld_name) + 1;
2478
2479       syment.n_sclass = C_EXT;
2480       syment.n_numaux = 1;
2481       bfd_coff_swap_sym_out (abfd, &syment,
2482                              &syment_ext[filehdr.f_nsyms * SYMESZ]);
2483       bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
2484                              syment.n_numaux,
2485                              &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
2486
2487       /* Reloc.  */
2488       memset (&reloc, 0, sizeof (struct internal_reloc));
2489       reloc.r_vaddr = 0x0000;
2490       reloc.r_symndx = filehdr.f_nsyms;
2491       reloc.r_type = R_POS;
2492       reloc.r_size = 63;
2493       bfd_coff_swap_reloc_out (abfd, &reloc,
2494                                &reloc_ext[data_scnhdr.s_nreloc * RELSZ]);
2495
2496       filehdr.f_nsyms += 2;
2497       data_scnhdr.s_nreloc += 1;
2498
2499       bss_scnhdr.s_size = 0;
2500     }
2501
2502   data_scnhdr.s_relptr = data_scnhdr.s_scnptr + data_buffer_size;
2503   filehdr.f_symptr = data_scnhdr.s_relptr + data_scnhdr.s_nreloc * RELSZ;
2504
2505   bfd_coff_swap_filehdr_out (abfd, &filehdr, filehdr_ext);
2506   bfd_bwrite (filehdr_ext, FILHSZ, abfd);
2507   bfd_coff_swap_scnhdr_out (abfd, &text_scnhdr, &scnhdr_ext[SCNHSZ * 0]);
2508   bfd_coff_swap_scnhdr_out (abfd, &data_scnhdr, &scnhdr_ext[SCNHSZ * 1]);
2509   bfd_coff_swap_scnhdr_out (abfd, &bss_scnhdr, &scnhdr_ext[SCNHSZ * 2]);
2510   bfd_bwrite (scnhdr_ext, 3 * SCNHSZ, abfd);
2511   bfd_bwrite (data_buffer, data_buffer_size, abfd);
2512   bfd_bwrite (reloc_ext, data_scnhdr.s_nreloc * RELSZ, abfd);
2513   bfd_bwrite (syment_ext, filehdr.f_nsyms * SYMESZ, abfd);
2514   bfd_bwrite (string_table, string_table_size, abfd);
2515
2516   free (data_buffer);
2517   data_buffer = NULL;
2518
2519   return TRUE;
2520 }
2521
2522 /* The typical dynamic reloc.  */
2523
2524 static reloc_howto_type xcoff64_dynamic_reloc =
2525 HOWTO (0,                       /* type */
2526        0,                       /* rightshift */
2527        4,                       /* size (0 = byte, 1 = short, 2 = long) */
2528        64,                      /* bitsize */
2529        FALSE,                   /* pc_relative */
2530        0,                       /* bitpos */
2531        complain_overflow_bitfield, /* complain_on_overflow */
2532        0,                       /* special_function */
2533        "R_POS",                 /* name */
2534        TRUE,                    /* partial_inplace */
2535        MINUS_ONE,               /* src_mask */
2536        MINUS_ONE,               /* dst_mask */
2537        FALSE);                  /* pcrel_offset */
2538
2539 static unsigned long xcoff64_glink_code[10] =
2540 {
2541   0xe9820000,   /* ld r12,0(r2) */
2542   0xf8410028,   /* std r2,40(r1) */
2543   0xe80c0000,   /* ld r0,0(r12) */
2544   0xe84c0008,   /* ld r0,8(r12) */
2545   0x7c0903a6,   /* mtctr r0 */
2546   0x4e800420,   /* bctr */
2547   0x00000000,   /* start of traceback table */
2548   0x000ca000,   /* traceback table */
2549   0x00000000,   /* traceback table */
2550   0x00000018,   /* ??? */
2551 };
2552
2553 static const struct xcoff_backend_data_rec bfd_xcoff_backend_data =
2554   {
2555     { /* COFF backend, defined in libcoff.h.  */
2556       _bfd_xcoff64_swap_aux_in,
2557       _bfd_xcoff64_swap_sym_in,
2558       _bfd_xcoff64_swap_lineno_in,
2559       _bfd_xcoff64_swap_aux_out,
2560       _bfd_xcoff64_swap_sym_out,
2561       _bfd_xcoff64_swap_lineno_out,
2562       xcoff64_swap_reloc_out,
2563       coff_swap_filehdr_out,
2564       coff_swap_aouthdr_out,
2565       coff_swap_scnhdr_out,
2566       FILHSZ,
2567       AOUTSZ,
2568       SCNHSZ,
2569       SYMESZ,
2570       AUXESZ,
2571       RELSZ,
2572       LINESZ,
2573       FILNMLEN,
2574       TRUE,                     /* _bfd_coff_long_filenames */
2575       XCOFF_NO_LONG_SECTION_NAMES,  /* _bfd_coff_long_section_names */
2576       3,                        /* _bfd_coff_default_section_alignment_power */
2577       TRUE,                     /* _bfd_coff_force_symnames_in_strings */
2578       4,                        /* _bfd_coff_debug_string_prefix_length */
2579       32768,                    /* _bfd_coff_max_nscns */
2580       coff_swap_filehdr_in,
2581       coff_swap_aouthdr_in,
2582       coff_swap_scnhdr_in,
2583       xcoff64_swap_reloc_in,
2584       xcoff64_bad_format_hook,
2585       coff_set_arch_mach_hook,
2586       coff_mkobject_hook,
2587       styp_to_sec_flags,
2588       coff_set_alignment_hook,
2589       coff_slurp_symbol_table,
2590       symname_in_debug_hook,
2591       coff_pointerize_aux_hook,
2592       coff_print_aux,
2593       dummy_reloc16_extra_cases,
2594       dummy_reloc16_estimate,
2595       NULL,                     /* bfd_coff_symbol_classification */
2596       coff_compute_section_file_positions,
2597       NULL,                     /* _bfd_coff_start_final_link */
2598       xcoff64_ppc_relocate_section,
2599       coff_rtype_to_howto,
2600       NULL,                     /* _bfd_coff_adjust_symndx */
2601       _bfd_generic_link_add_one_symbol,
2602       coff_link_output_has_begun,
2603       coff_final_link_postscript,
2604       NULL                      /* print_pdata.  */
2605     },
2606
2607     0x01EF,                     /* magic number */
2608     bfd_arch_powerpc,
2609     bfd_mach_ppc_620,
2610
2611     /* Function pointers to xcoff specific swap routines.  */
2612     xcoff64_swap_ldhdr_in,
2613     xcoff64_swap_ldhdr_out,
2614     xcoff64_swap_ldsym_in,
2615     xcoff64_swap_ldsym_out,
2616     xcoff64_swap_ldrel_in,
2617     xcoff64_swap_ldrel_out,
2618
2619     /* Sizes.  */
2620     LDHDRSZ,
2621     LDSYMSZ,
2622     LDRELSZ,
2623     24,                         /* _xcoff_function_descriptor_size */
2624     0,                          /* _xcoff_small_aout_header_size */
2625
2626     /* Versions.  */
2627     2,                          /* _xcoff_ldhdr_version */
2628
2629     _bfd_xcoff64_put_symbol_name,
2630     _bfd_xcoff64_put_ldsymbol_name,
2631     &xcoff64_dynamic_reloc,
2632     xcoff64_create_csect_from_smclas,
2633
2634     /* Lineno and reloc count overflow.  */
2635     xcoff64_is_lineno_count_overflow,
2636     xcoff64_is_reloc_count_overflow,
2637
2638     xcoff64_loader_symbol_offset,
2639     xcoff64_loader_reloc_offset,
2640
2641     /* glink.  */
2642     &xcoff64_glink_code[0],
2643     40,                         /* _xcoff_glink_size */
2644
2645     /* rtinit.  */
2646     88,                         /* _xcoff_rtinit_size */
2647     xcoff64_generate_rtinit,
2648   };
2649
2650 /* The transfer vector that leads the outside world to all of the above.  */
2651 const bfd_target rs6000_xcoff64_vec =
2652   {
2653     "aixcoff64-rs6000",
2654     bfd_target_xcoff_flavour,
2655     BFD_ENDIAN_BIG,             /* data byte order is big */
2656     BFD_ENDIAN_BIG,             /* header byte order is big */
2657
2658     (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
2659      | HAS_SYMS | HAS_LOCALS | WP_TEXT),
2660
2661     SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
2662     0,                          /* leading char */
2663     '/',                        /* ar_pad_char */
2664     15,                         /* ar_max_namelen */
2665     0,                          /* match priority.  */
2666
2667     /* data */
2668     bfd_getb64,
2669     bfd_getb_signed_64,
2670     bfd_putb64,
2671     bfd_getb32,
2672     bfd_getb_signed_32,
2673     bfd_putb32,
2674     bfd_getb16,
2675     bfd_getb_signed_16,
2676     bfd_putb16,
2677
2678     /* hdrs */
2679     bfd_getb64,
2680     bfd_getb_signed_64,
2681     bfd_putb64,
2682     bfd_getb32,
2683     bfd_getb_signed_32,
2684     bfd_putb32,
2685     bfd_getb16,
2686     bfd_getb_signed_16,
2687     bfd_putb16,
2688
2689     { /* bfd_check_format */
2690       _bfd_dummy_target,
2691       coff_object_p,
2692       xcoff64_archive_p,
2693       CORE_FILE_P
2694     },
2695
2696     { /* bfd_set_format */
2697       _bfd_bool_bfd_false_error,
2698       coff_mkobject,
2699       _bfd_generic_mkarchive,
2700       _bfd_bool_bfd_false_error
2701     },
2702
2703     {/* bfd_write_contents */
2704       _bfd_bool_bfd_false_error,
2705       xcoff64_write_object_contents,
2706       _bfd_xcoff_write_archive_contents,
2707       _bfd_bool_bfd_false_error
2708     },
2709
2710     /* Generic */
2711     _bfd_archive_close_and_cleanup,
2712     _bfd_bool_bfd_true,
2713     coff_new_section_hook,
2714     _bfd_generic_get_section_contents,
2715     _bfd_generic_get_section_contents_in_window,
2716
2717     /* Copy */
2718     _bfd_xcoff_copy_private_bfd_data,
2719     _bfd_generic_bfd_merge_private_bfd_data,
2720     _bfd_generic_init_private_section_data,
2721     _bfd_generic_bfd_copy_private_section_data,
2722     _bfd_generic_bfd_copy_private_symbol_data,
2723     _bfd_generic_bfd_copy_private_header_data,
2724     _bfd_generic_bfd_set_private_flags,
2725     _bfd_generic_bfd_print_private_bfd_data,
2726
2727     /* Core */
2728     BFD_JUMP_TABLE_CORE (coff),
2729
2730     /* Archive */
2731     xcoff64_slurp_armap,
2732     _bfd_noarchive_slurp_extended_name_table,
2733     _bfd_noarchive_construct_extended_name_table,
2734     bfd_dont_truncate_arname,
2735     _bfd_xcoff_write_armap,
2736     _bfd_xcoff_read_ar_hdr,
2737     _bfd_generic_write_ar_hdr,
2738     xcoff64_openr_next_archived_file,
2739     _bfd_generic_get_elt_at_index,
2740     _bfd_xcoff_stat_arch_elt,
2741     _bfd_bool_bfd_true,
2742
2743     /* Symbols */
2744     coff_get_symtab_upper_bound,
2745     coff_canonicalize_symtab,
2746     coff_make_empty_symbol,
2747     coff_print_symbol,
2748     coff_get_symbol_info,
2749     coff_get_symbol_version_string,
2750     _bfd_xcoff_is_local_label_name,
2751     coff_bfd_is_target_special_symbol,
2752     coff_get_lineno,
2753     coff_find_nearest_line,
2754     coff_find_line,
2755     coff_find_inliner_info,
2756     coff_bfd_make_debug_symbol,
2757     _bfd_generic_read_minisymbols,
2758     _bfd_generic_minisymbol_to_symbol,
2759
2760     /* Reloc */
2761     coff_get_reloc_upper_bound,
2762     coff_canonicalize_reloc,
2763     _bfd_generic_set_reloc,
2764     xcoff64_reloc_type_lookup,
2765     xcoff64_reloc_name_lookup,
2766
2767     /* Write */
2768     coff_set_arch_mach,
2769     coff_set_section_contents,
2770
2771     /* Link */
2772     xcoff64_sizeof_headers,
2773     bfd_generic_get_relocated_section_contents,
2774     bfd_generic_relax_section,
2775     _bfd_xcoff_bfd_link_hash_table_create,
2776     _bfd_xcoff_bfd_link_add_symbols,
2777     _bfd_generic_link_just_syms,
2778     _bfd_generic_copy_link_hash_symbol_type,
2779     _bfd_xcoff_bfd_final_link,
2780     _bfd_generic_link_split_section,
2781     _bfd_generic_link_check_relocs,
2782     bfd_generic_gc_sections,
2783     bfd_generic_lookup_section_flags,
2784     bfd_generic_merge_sections,
2785     bfd_generic_is_group_section,
2786     bfd_generic_discard_group,
2787     _bfd_generic_section_already_linked,
2788     _bfd_xcoff_define_common_symbol,
2789     bfd_generic_define_start_stop,
2790
2791     /* Dynamic */
2792     _bfd_xcoff_get_dynamic_symtab_upper_bound,
2793     _bfd_xcoff_canonicalize_dynamic_symtab,
2794     _bfd_nodynamic_get_synthetic_symtab,
2795     _bfd_xcoff_get_dynamic_reloc_upper_bound,
2796     _bfd_xcoff_canonicalize_dynamic_reloc,
2797
2798     /* Opposite endian version, none exists */
2799     NULL,
2800
2801     &bfd_xcoff_backend_data,
2802   };
2803
2804 extern const bfd_target *xcoff64_core_p
2805   (bfd *);
2806 extern bfd_boolean xcoff64_core_file_matches_executable_p
2807   (bfd *, bfd *);
2808 extern char *xcoff64_core_file_failing_command
2809   (bfd *);
2810 extern int xcoff64_core_file_failing_signal
2811   (bfd *);
2812 #define xcoff64_core_file_pid _bfd_nocore_core_file_pid
2813
2814 /* AIX 5 */
2815 static const struct xcoff_backend_data_rec bfd_xcoff_aix5_backend_data =
2816   {
2817     { /* COFF backend, defined in libcoff.h.  */
2818       _bfd_xcoff64_swap_aux_in,
2819       _bfd_xcoff64_swap_sym_in,
2820       _bfd_xcoff64_swap_lineno_in,
2821       _bfd_xcoff64_swap_aux_out,
2822       _bfd_xcoff64_swap_sym_out,
2823       _bfd_xcoff64_swap_lineno_out,
2824       xcoff64_swap_reloc_out,
2825       coff_swap_filehdr_out,
2826       coff_swap_aouthdr_out,
2827       coff_swap_scnhdr_out,
2828       FILHSZ,
2829       AOUTSZ,
2830       SCNHSZ,
2831       SYMESZ,
2832       AUXESZ,
2833       RELSZ,
2834       LINESZ,
2835       FILNMLEN,
2836       TRUE,                     /* _bfd_coff_long_filenames */
2837       XCOFF_NO_LONG_SECTION_NAMES,  /* _bfd_coff_long_section_names */
2838       3,                        /* _bfd_coff_default_section_alignment_power */
2839       TRUE,                     /* _bfd_coff_force_symnames_in_strings */
2840       4,                        /* _bfd_coff_debug_string_prefix_length */
2841       32768,                    /* _bfd_coff_max_nscns */
2842       coff_swap_filehdr_in,
2843       coff_swap_aouthdr_in,
2844       coff_swap_scnhdr_in,
2845       xcoff64_swap_reloc_in,
2846       xcoff64_bad_format_hook,
2847       coff_set_arch_mach_hook,
2848       coff_mkobject_hook,
2849       styp_to_sec_flags,
2850       coff_set_alignment_hook,
2851       coff_slurp_symbol_table,
2852       symname_in_debug_hook,
2853       coff_pointerize_aux_hook,
2854       coff_print_aux,
2855       dummy_reloc16_extra_cases,
2856       dummy_reloc16_estimate,
2857       NULL,                     /* bfd_coff_sym_is_global */
2858       coff_compute_section_file_positions,
2859       NULL,                     /* _bfd_coff_start_final_link */
2860       xcoff64_ppc_relocate_section,
2861       coff_rtype_to_howto,
2862       NULL,                     /* _bfd_coff_adjust_symndx */
2863       _bfd_generic_link_add_one_symbol,
2864       coff_link_output_has_begun,
2865       coff_final_link_postscript,
2866       NULL                      /* print_pdata.  */
2867     },
2868
2869     U64_TOCMAGIC,               /* magic number */
2870     bfd_arch_powerpc,
2871     bfd_mach_ppc_620,
2872
2873     /* Function pointers to xcoff specific swap routines.  */
2874     xcoff64_swap_ldhdr_in,
2875     xcoff64_swap_ldhdr_out,
2876     xcoff64_swap_ldsym_in,
2877     xcoff64_swap_ldsym_out,
2878     xcoff64_swap_ldrel_in,
2879     xcoff64_swap_ldrel_out,
2880
2881     /* Sizes.  */
2882     LDHDRSZ,
2883     LDSYMSZ,
2884     LDRELSZ,
2885     24,                         /* _xcoff_function_descriptor_size */
2886     0,                          /* _xcoff_small_aout_header_size */
2887     /* Versions.  */
2888     2,                          /* _xcoff_ldhdr_version */
2889
2890     _bfd_xcoff64_put_symbol_name,
2891     _bfd_xcoff64_put_ldsymbol_name,
2892     &xcoff64_dynamic_reloc,
2893     xcoff64_create_csect_from_smclas,
2894
2895     /* Lineno and reloc count overflow.  */
2896     xcoff64_is_lineno_count_overflow,
2897     xcoff64_is_reloc_count_overflow,
2898
2899     xcoff64_loader_symbol_offset,
2900     xcoff64_loader_reloc_offset,
2901
2902     /* glink.  */
2903     &xcoff64_glink_code[0],
2904     40,                         /* _xcoff_glink_size */
2905
2906     /* rtinit.  */
2907     88,                         /* _xcoff_rtinit_size */
2908     xcoff64_generate_rtinit,
2909   };
2910
2911 /* The transfer vector that leads the outside world to all of the above.  */
2912 const bfd_target rs6000_xcoff64_aix_vec =
2913   {
2914     "aix5coff64-rs6000",
2915     bfd_target_xcoff_flavour,
2916     BFD_ENDIAN_BIG,             /* data byte order is big */
2917     BFD_ENDIAN_BIG,             /* header byte order is big */
2918
2919     (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
2920      | HAS_SYMS | HAS_LOCALS | WP_TEXT),
2921
2922     SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
2923     0,                          /* leading char */
2924     '/',                        /* ar_pad_char */
2925     15,                         /* ar_max_namelen */
2926     0,                          /* match priority.  */
2927
2928     /* data */
2929     bfd_getb64,
2930     bfd_getb_signed_64,
2931     bfd_putb64,
2932     bfd_getb32,
2933     bfd_getb_signed_32,
2934     bfd_putb32,
2935     bfd_getb16,
2936     bfd_getb_signed_16,
2937     bfd_putb16,
2938
2939     /* hdrs */
2940     bfd_getb64,
2941     bfd_getb_signed_64,
2942     bfd_putb64,
2943     bfd_getb32,
2944     bfd_getb_signed_32,
2945     bfd_putb32,
2946     bfd_getb16,
2947     bfd_getb_signed_16,
2948     bfd_putb16,
2949
2950     { /* bfd_check_format */
2951       _bfd_dummy_target,
2952       coff_object_p,
2953       xcoff64_archive_p,
2954       xcoff64_core_p
2955     },
2956
2957     { /* bfd_set_format */
2958       _bfd_bool_bfd_false_error,
2959       coff_mkobject,
2960       _bfd_generic_mkarchive,
2961       _bfd_bool_bfd_false_error
2962     },
2963
2964     {/* bfd_write_contents */
2965       _bfd_bool_bfd_false_error,
2966       xcoff64_write_object_contents,
2967       _bfd_xcoff_write_archive_contents,
2968       _bfd_bool_bfd_false_error
2969     },
2970
2971     /* Generic */
2972     _bfd_archive_close_and_cleanup,
2973     _bfd_bool_bfd_true,
2974     coff_new_section_hook,
2975     _bfd_generic_get_section_contents,
2976     _bfd_generic_get_section_contents_in_window,
2977
2978     /* Copy */
2979     _bfd_xcoff_copy_private_bfd_data,
2980     _bfd_generic_bfd_merge_private_bfd_data,
2981     _bfd_generic_init_private_section_data,
2982     _bfd_generic_bfd_copy_private_section_data,
2983     _bfd_generic_bfd_copy_private_symbol_data,
2984     _bfd_generic_bfd_copy_private_header_data,
2985     _bfd_generic_bfd_set_private_flags,
2986     _bfd_generic_bfd_print_private_bfd_data,
2987
2988     /* Core */
2989     BFD_JUMP_TABLE_CORE (xcoff64),
2990
2991     /* Archive */
2992     xcoff64_slurp_armap,
2993     _bfd_noarchive_slurp_extended_name_table,
2994     _bfd_noarchive_construct_extended_name_table,
2995     bfd_dont_truncate_arname,
2996     _bfd_xcoff_write_armap,
2997     _bfd_xcoff_read_ar_hdr,
2998     _bfd_generic_write_ar_hdr,
2999     xcoff64_openr_next_archived_file,
3000     _bfd_generic_get_elt_at_index,
3001     _bfd_xcoff_stat_arch_elt,
3002     _bfd_bool_bfd_true,
3003
3004     /* Symbols */
3005     coff_get_symtab_upper_bound,
3006     coff_canonicalize_symtab,
3007     coff_make_empty_symbol,
3008     coff_print_symbol,
3009     coff_get_symbol_info,
3010     coff_get_symbol_version_string,
3011     _bfd_xcoff_is_local_label_name,
3012     coff_bfd_is_target_special_symbol,
3013     coff_get_lineno,
3014     coff_find_nearest_line,
3015     coff_find_line,
3016     coff_find_inliner_info,
3017     coff_bfd_make_debug_symbol,
3018     _bfd_generic_read_minisymbols,
3019     _bfd_generic_minisymbol_to_symbol,
3020
3021     /* Reloc */
3022     coff_get_reloc_upper_bound,
3023     coff_canonicalize_reloc,
3024     _bfd_generic_set_reloc,
3025     xcoff64_reloc_type_lookup,
3026     xcoff64_reloc_name_lookup,
3027
3028     /* Write */
3029     coff_set_arch_mach,
3030     coff_set_section_contents,
3031
3032     /* Link */
3033     xcoff64_sizeof_headers,
3034     bfd_generic_get_relocated_section_contents,
3035     bfd_generic_relax_section,
3036     _bfd_xcoff_bfd_link_hash_table_create,
3037     _bfd_xcoff_bfd_link_add_symbols,
3038     _bfd_generic_link_just_syms,
3039     _bfd_generic_copy_link_hash_symbol_type,
3040     _bfd_xcoff_bfd_final_link,
3041     _bfd_generic_link_split_section,
3042     _bfd_generic_link_check_relocs,
3043     bfd_generic_gc_sections,
3044     bfd_generic_lookup_section_flags,
3045     bfd_generic_merge_sections,
3046     bfd_generic_is_group_section,
3047     bfd_generic_discard_group,
3048     _bfd_generic_section_already_linked,
3049     _bfd_xcoff_define_common_symbol,
3050     bfd_generic_define_start_stop,
3051
3052     /* Dynamic */
3053     _bfd_xcoff_get_dynamic_symtab_upper_bound,
3054     _bfd_xcoff_canonicalize_dynamic_symtab,
3055     _bfd_nodynamic_get_synthetic_symtab,
3056     _bfd_xcoff_get_dynamic_reloc_upper_bound,
3057     _bfd_xcoff_canonicalize_dynamic_reloc,
3058
3059     /* Opposite endian version, none exists.  */
3060     NULL,
3061
3062     & bfd_xcoff_aix5_backend_data,
3063   };