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