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