1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010, 2011 Free Software Foundation, Inc.
4 Written by Linus Nordberg, Swox AB <info@swox.com>,
5 based on elf32-ppc.c by Ian Lance Taylor.
6 Largely rewritten by Alan Modra.
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License along
21 with this program; if not, write to the Free Software Foundation, Inc.,
22 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
25 /* The 64-bit PowerPC ELF ABI may be found at
26 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
27 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
35 #include "elf/ppc64.h"
36 #include "elf64-ppc.h"
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_vma opd_entry_value
57 (asection *, bfd_vma, asection **, bfd_vma *);
59 #define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
60 #define TARGET_LITTLE_NAME "elf64-powerpcle"
61 #define TARGET_BIG_SYM bfd_elf64_powerpc_vec
62 #define TARGET_BIG_NAME "elf64-powerpc"
63 #define ELF_ARCH bfd_arch_powerpc
64 #define ELF_TARGET_ID PPC64_ELF_DATA
65 #define ELF_MACHINE_CODE EM_PPC64
66 #define ELF_MAXPAGESIZE 0x10000
67 #define ELF_COMMONPAGESIZE 0x1000
68 #define elf_info_to_howto ppc64_elf_info_to_howto
70 #define elf_backend_want_got_sym 0
71 #define elf_backend_want_plt_sym 0
72 #define elf_backend_plt_alignment 3
73 #define elf_backend_plt_not_loaded 1
74 #define elf_backend_got_header_size 8
75 #define elf_backend_can_gc_sections 1
76 #define elf_backend_can_refcount 1
77 #define elf_backend_rela_normal 1
78 #define elf_backend_default_execstack 0
80 #define bfd_elf64_mkobject ppc64_elf_mkobject
81 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
82 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
83 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
84 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
85 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
86 #define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
87 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
89 #define elf_backend_object_p ppc64_elf_object_p
90 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
91 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
92 #define elf_backend_write_core_note ppc64_elf_write_core_note
93 #define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
94 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
95 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
96 #define elf_backend_check_directives ppc64_elf_process_dot_syms
97 #define elf_backend_as_needed_cleanup ppc64_elf_as_needed_cleanup
98 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
99 #define elf_backend_check_relocs ppc64_elf_check_relocs
100 #define elf_backend_gc_keep ppc64_elf_gc_keep
101 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
102 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
103 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
104 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
105 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
106 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
107 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
108 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
109 #define elf_backend_action_discarded ppc64_elf_action_discarded
110 #define elf_backend_relocate_section ppc64_elf_relocate_section
111 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
112 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
113 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
114 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
115 #define elf_backend_special_sections ppc64_elf_special_sections
116 #define elf_backend_post_process_headers _bfd_elf_set_osabi
118 /* The name of the dynamic interpreter. This is put in the .interp
120 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
122 /* The size in bytes of an entry in the procedure linkage table. */
123 #define PLT_ENTRY_SIZE 24
125 /* The initial size of the plt reserved for the dynamic linker. */
126 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
128 /* TOC base pointers offset from start of TOC. */
129 #define TOC_BASE_OFF 0x8000
131 /* Offset of tp and dtp pointers from start of TLS block. */
132 #define TP_OFFSET 0x7000
133 #define DTP_OFFSET 0x8000
135 /* .plt call stub instructions. The normal stub is like this, but
136 sometimes the .plt entry crosses a 64k boundary and we need to
137 insert an addi to adjust r12. */
138 #define PLT_CALL_STUB_SIZE (7*4)
139 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
140 #define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
141 #define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
142 #define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
143 #define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
144 /* ld %r11,xxx+16@l(%r12) */
145 #define BCTR 0x4e800420 /* bctr */
148 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,off@ha */
149 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
150 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
151 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
153 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
154 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
156 #define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
158 /* glink call stub instructions. We enter with the index in R0. */
159 #define GLINK_CALL_STUB_SIZE (16*4)
163 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
164 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
166 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
167 #define LD_R2_M16R11 0xe84bfff0 /* ld %2,(0b-1b)(%11) */
168 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
169 #define ADD_R12_R2_R11 0x7d825a14 /* add %12,%2,%11 */
177 #define NOP 0x60000000
179 /* Some other nops. */
180 #define CROR_151515 0x4def7b82
181 #define CROR_313131 0x4ffffb82
183 /* .glink entries for the first 32k functions are two instructions. */
184 #define LI_R0_0 0x38000000 /* li %r0,0 */
185 #define B_DOT 0x48000000 /* b . */
187 /* After that, we need two instructions to load the index, followed by
189 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
190 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
192 /* Instructions used by the save and restore reg functions. */
193 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
194 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
195 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
196 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
197 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
198 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
199 #define LI_R12_0 0x39800000 /* li %r12,0 */
200 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
201 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
202 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
203 #define BLR 0x4e800020 /* blr */
205 /* Since .opd is an array of descriptors and each entry will end up
206 with identical R_PPC64_RELATIVE relocs, there is really no need to
207 propagate .opd relocs; The dynamic linker should be taught to
208 relocate .opd without reloc entries. */
209 #ifndef NO_OPD_RELOCS
210 #define NO_OPD_RELOCS 0
213 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
215 /* Relocation HOWTO's. */
216 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
218 static reloc_howto_type ppc64_elf_howto_raw[] = {
219 /* This reloc does nothing. */
220 HOWTO (R_PPC64_NONE, /* type */
222 2, /* size (0 = byte, 1 = short, 2 = long) */
224 FALSE, /* pc_relative */
226 complain_overflow_dont, /* complain_on_overflow */
227 bfd_elf_generic_reloc, /* special_function */
228 "R_PPC64_NONE", /* name */
229 FALSE, /* partial_inplace */
232 FALSE), /* pcrel_offset */
234 /* A standard 32 bit relocation. */
235 HOWTO (R_PPC64_ADDR32, /* type */
237 2, /* size (0 = byte, 1 = short, 2 = long) */
239 FALSE, /* pc_relative */
241 complain_overflow_bitfield, /* complain_on_overflow */
242 bfd_elf_generic_reloc, /* special_function */
243 "R_PPC64_ADDR32", /* name */
244 FALSE, /* partial_inplace */
246 0xffffffff, /* dst_mask */
247 FALSE), /* pcrel_offset */
249 /* An absolute 26 bit branch; the lower two bits must be zero.
250 FIXME: we don't check that, we just clear them. */
251 HOWTO (R_PPC64_ADDR24, /* type */
253 2, /* size (0 = byte, 1 = short, 2 = long) */
255 FALSE, /* pc_relative */
257 complain_overflow_bitfield, /* complain_on_overflow */
258 bfd_elf_generic_reloc, /* special_function */
259 "R_PPC64_ADDR24", /* name */
260 FALSE, /* partial_inplace */
262 0x03fffffc, /* dst_mask */
263 FALSE), /* pcrel_offset */
265 /* A standard 16 bit relocation. */
266 HOWTO (R_PPC64_ADDR16, /* type */
268 1, /* size (0 = byte, 1 = short, 2 = long) */
270 FALSE, /* pc_relative */
272 complain_overflow_bitfield, /* complain_on_overflow */
273 bfd_elf_generic_reloc, /* special_function */
274 "R_PPC64_ADDR16", /* name */
275 FALSE, /* partial_inplace */
277 0xffff, /* dst_mask */
278 FALSE), /* pcrel_offset */
280 /* A 16 bit relocation without overflow. */
281 HOWTO (R_PPC64_ADDR16_LO, /* type */
283 1, /* size (0 = byte, 1 = short, 2 = long) */
285 FALSE, /* pc_relative */
287 complain_overflow_dont,/* complain_on_overflow */
288 bfd_elf_generic_reloc, /* special_function */
289 "R_PPC64_ADDR16_LO", /* name */
290 FALSE, /* partial_inplace */
292 0xffff, /* dst_mask */
293 FALSE), /* pcrel_offset */
295 /* Bits 16-31 of an address. */
296 HOWTO (R_PPC64_ADDR16_HI, /* type */
298 1, /* size (0 = byte, 1 = short, 2 = long) */
300 FALSE, /* pc_relative */
302 complain_overflow_dont, /* complain_on_overflow */
303 bfd_elf_generic_reloc, /* special_function */
304 "R_PPC64_ADDR16_HI", /* name */
305 FALSE, /* partial_inplace */
307 0xffff, /* dst_mask */
308 FALSE), /* pcrel_offset */
310 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
311 bits, treated as a signed number, is negative. */
312 HOWTO (R_PPC64_ADDR16_HA, /* type */
314 1, /* size (0 = byte, 1 = short, 2 = long) */
316 FALSE, /* pc_relative */
318 complain_overflow_dont, /* complain_on_overflow */
319 ppc64_elf_ha_reloc, /* special_function */
320 "R_PPC64_ADDR16_HA", /* name */
321 FALSE, /* partial_inplace */
323 0xffff, /* dst_mask */
324 FALSE), /* pcrel_offset */
326 /* An absolute 16 bit branch; the lower two bits must be zero.
327 FIXME: we don't check that, we just clear them. */
328 HOWTO (R_PPC64_ADDR14, /* type */
330 2, /* size (0 = byte, 1 = short, 2 = long) */
332 FALSE, /* pc_relative */
334 complain_overflow_bitfield, /* complain_on_overflow */
335 ppc64_elf_branch_reloc, /* special_function */
336 "R_PPC64_ADDR14", /* name */
337 FALSE, /* partial_inplace */
339 0x0000fffc, /* dst_mask */
340 FALSE), /* pcrel_offset */
342 /* An absolute 16 bit branch, for which bit 10 should be set to
343 indicate that the branch is expected to be taken. The lower two
344 bits must be zero. */
345 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
347 2, /* size (0 = byte, 1 = short, 2 = long) */
349 FALSE, /* pc_relative */
351 complain_overflow_bitfield, /* complain_on_overflow */
352 ppc64_elf_brtaken_reloc, /* special_function */
353 "R_PPC64_ADDR14_BRTAKEN",/* name */
354 FALSE, /* partial_inplace */
356 0x0000fffc, /* dst_mask */
357 FALSE), /* pcrel_offset */
359 /* An absolute 16 bit branch, for which bit 10 should be set to
360 indicate that the branch is not expected to be taken. The lower
361 two bits must be zero. */
362 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
364 2, /* size (0 = byte, 1 = short, 2 = long) */
366 FALSE, /* pc_relative */
368 complain_overflow_bitfield, /* complain_on_overflow */
369 ppc64_elf_brtaken_reloc, /* special_function */
370 "R_PPC64_ADDR14_BRNTAKEN",/* name */
371 FALSE, /* partial_inplace */
373 0x0000fffc, /* dst_mask */
374 FALSE), /* pcrel_offset */
376 /* A relative 26 bit branch; the lower two bits must be zero. */
377 HOWTO (R_PPC64_REL24, /* type */
379 2, /* size (0 = byte, 1 = short, 2 = long) */
381 TRUE, /* pc_relative */
383 complain_overflow_signed, /* complain_on_overflow */
384 ppc64_elf_branch_reloc, /* special_function */
385 "R_PPC64_REL24", /* name */
386 FALSE, /* partial_inplace */
388 0x03fffffc, /* dst_mask */
389 TRUE), /* pcrel_offset */
391 /* A relative 16 bit branch; the lower two bits must be zero. */
392 HOWTO (R_PPC64_REL14, /* type */
394 2, /* size (0 = byte, 1 = short, 2 = long) */
396 TRUE, /* pc_relative */
398 complain_overflow_signed, /* complain_on_overflow */
399 ppc64_elf_branch_reloc, /* special_function */
400 "R_PPC64_REL14", /* name */
401 FALSE, /* partial_inplace */
403 0x0000fffc, /* dst_mask */
404 TRUE), /* pcrel_offset */
406 /* A relative 16 bit branch. Bit 10 should be set to indicate that
407 the branch is expected to be taken. The lower two bits must be
409 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
411 2, /* size (0 = byte, 1 = short, 2 = long) */
413 TRUE, /* pc_relative */
415 complain_overflow_signed, /* complain_on_overflow */
416 ppc64_elf_brtaken_reloc, /* special_function */
417 "R_PPC64_REL14_BRTAKEN", /* name */
418 FALSE, /* partial_inplace */
420 0x0000fffc, /* dst_mask */
421 TRUE), /* pcrel_offset */
423 /* A relative 16 bit branch. Bit 10 should be set to indicate that
424 the branch is not expected to be taken. The lower two bits must
426 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
428 2, /* size (0 = byte, 1 = short, 2 = long) */
430 TRUE, /* pc_relative */
432 complain_overflow_signed, /* complain_on_overflow */
433 ppc64_elf_brtaken_reloc, /* special_function */
434 "R_PPC64_REL14_BRNTAKEN",/* name */
435 FALSE, /* partial_inplace */
437 0x0000fffc, /* dst_mask */
438 TRUE), /* pcrel_offset */
440 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
442 HOWTO (R_PPC64_GOT16, /* type */
444 1, /* size (0 = byte, 1 = short, 2 = long) */
446 FALSE, /* pc_relative */
448 complain_overflow_signed, /* complain_on_overflow */
449 ppc64_elf_unhandled_reloc, /* special_function */
450 "R_PPC64_GOT16", /* name */
451 FALSE, /* partial_inplace */
453 0xffff, /* dst_mask */
454 FALSE), /* pcrel_offset */
456 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
458 HOWTO (R_PPC64_GOT16_LO, /* type */
460 1, /* size (0 = byte, 1 = short, 2 = long) */
462 FALSE, /* pc_relative */
464 complain_overflow_dont, /* complain_on_overflow */
465 ppc64_elf_unhandled_reloc, /* special_function */
466 "R_PPC64_GOT16_LO", /* name */
467 FALSE, /* partial_inplace */
469 0xffff, /* dst_mask */
470 FALSE), /* pcrel_offset */
472 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
474 HOWTO (R_PPC64_GOT16_HI, /* type */
476 1, /* size (0 = byte, 1 = short, 2 = long) */
478 FALSE, /* pc_relative */
480 complain_overflow_dont,/* complain_on_overflow */
481 ppc64_elf_unhandled_reloc, /* special_function */
482 "R_PPC64_GOT16_HI", /* name */
483 FALSE, /* partial_inplace */
485 0xffff, /* dst_mask */
486 FALSE), /* pcrel_offset */
488 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
490 HOWTO (R_PPC64_GOT16_HA, /* type */
492 1, /* size (0 = byte, 1 = short, 2 = long) */
494 FALSE, /* pc_relative */
496 complain_overflow_dont,/* complain_on_overflow */
497 ppc64_elf_unhandled_reloc, /* special_function */
498 "R_PPC64_GOT16_HA", /* name */
499 FALSE, /* partial_inplace */
501 0xffff, /* dst_mask */
502 FALSE), /* pcrel_offset */
504 /* This is used only by the dynamic linker. The symbol should exist
505 both in the object being run and in some shared library. The
506 dynamic linker copies the data addressed by the symbol from the
507 shared library into the object, because the object being
508 run has to have the data at some particular address. */
509 HOWTO (R_PPC64_COPY, /* type */
511 0, /* this one is variable size */
513 FALSE, /* pc_relative */
515 complain_overflow_dont, /* complain_on_overflow */
516 ppc64_elf_unhandled_reloc, /* special_function */
517 "R_PPC64_COPY", /* name */
518 FALSE, /* partial_inplace */
521 FALSE), /* pcrel_offset */
523 /* Like R_PPC64_ADDR64, but used when setting global offset table
525 HOWTO (R_PPC64_GLOB_DAT, /* type */
527 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
529 FALSE, /* pc_relative */
531 complain_overflow_dont, /* complain_on_overflow */
532 ppc64_elf_unhandled_reloc, /* special_function */
533 "R_PPC64_GLOB_DAT", /* name */
534 FALSE, /* partial_inplace */
536 ONES (64), /* dst_mask */
537 FALSE), /* pcrel_offset */
539 /* Created by the link editor. Marks a procedure linkage table
540 entry for a symbol. */
541 HOWTO (R_PPC64_JMP_SLOT, /* type */
543 0, /* size (0 = byte, 1 = short, 2 = long) */
545 FALSE, /* pc_relative */
547 complain_overflow_dont, /* complain_on_overflow */
548 ppc64_elf_unhandled_reloc, /* special_function */
549 "R_PPC64_JMP_SLOT", /* name */
550 FALSE, /* partial_inplace */
553 FALSE), /* pcrel_offset */
555 /* Used only by the dynamic linker. When the object is run, this
556 doubleword64 is set to the load address of the object, plus the
558 HOWTO (R_PPC64_RELATIVE, /* type */
560 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
562 FALSE, /* pc_relative */
564 complain_overflow_dont, /* complain_on_overflow */
565 bfd_elf_generic_reloc, /* special_function */
566 "R_PPC64_RELATIVE", /* name */
567 FALSE, /* partial_inplace */
569 ONES (64), /* dst_mask */
570 FALSE), /* pcrel_offset */
572 /* Like R_PPC64_ADDR32, but may be unaligned. */
573 HOWTO (R_PPC64_UADDR32, /* type */
575 2, /* size (0 = byte, 1 = short, 2 = long) */
577 FALSE, /* pc_relative */
579 complain_overflow_bitfield, /* complain_on_overflow */
580 bfd_elf_generic_reloc, /* special_function */
581 "R_PPC64_UADDR32", /* name */
582 FALSE, /* partial_inplace */
584 0xffffffff, /* dst_mask */
585 FALSE), /* pcrel_offset */
587 /* Like R_PPC64_ADDR16, but may be unaligned. */
588 HOWTO (R_PPC64_UADDR16, /* type */
590 1, /* size (0 = byte, 1 = short, 2 = long) */
592 FALSE, /* pc_relative */
594 complain_overflow_bitfield, /* complain_on_overflow */
595 bfd_elf_generic_reloc, /* special_function */
596 "R_PPC64_UADDR16", /* name */
597 FALSE, /* partial_inplace */
599 0xffff, /* dst_mask */
600 FALSE), /* pcrel_offset */
602 /* 32-bit PC relative. */
603 HOWTO (R_PPC64_REL32, /* type */
605 2, /* size (0 = byte, 1 = short, 2 = long) */
607 TRUE, /* pc_relative */
609 /* FIXME: Verify. Was complain_overflow_bitfield. */
610 complain_overflow_signed, /* complain_on_overflow */
611 bfd_elf_generic_reloc, /* special_function */
612 "R_PPC64_REL32", /* name */
613 FALSE, /* partial_inplace */
615 0xffffffff, /* dst_mask */
616 TRUE), /* pcrel_offset */
618 /* 32-bit relocation to the symbol's procedure linkage table. */
619 HOWTO (R_PPC64_PLT32, /* type */
621 2, /* size (0 = byte, 1 = short, 2 = long) */
623 FALSE, /* pc_relative */
625 complain_overflow_bitfield, /* complain_on_overflow */
626 ppc64_elf_unhandled_reloc, /* special_function */
627 "R_PPC64_PLT32", /* name */
628 FALSE, /* partial_inplace */
630 0xffffffff, /* dst_mask */
631 FALSE), /* pcrel_offset */
633 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
634 FIXME: R_PPC64_PLTREL32 not supported. */
635 HOWTO (R_PPC64_PLTREL32, /* type */
637 2, /* size (0 = byte, 1 = short, 2 = long) */
639 TRUE, /* pc_relative */
641 complain_overflow_signed, /* complain_on_overflow */
642 bfd_elf_generic_reloc, /* special_function */
643 "R_PPC64_PLTREL32", /* name */
644 FALSE, /* partial_inplace */
646 0xffffffff, /* dst_mask */
647 TRUE), /* pcrel_offset */
649 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
651 HOWTO (R_PPC64_PLT16_LO, /* type */
653 1, /* size (0 = byte, 1 = short, 2 = long) */
655 FALSE, /* pc_relative */
657 complain_overflow_dont, /* complain_on_overflow */
658 ppc64_elf_unhandled_reloc, /* special_function */
659 "R_PPC64_PLT16_LO", /* name */
660 FALSE, /* partial_inplace */
662 0xffff, /* dst_mask */
663 FALSE), /* pcrel_offset */
665 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
667 HOWTO (R_PPC64_PLT16_HI, /* type */
669 1, /* size (0 = byte, 1 = short, 2 = long) */
671 FALSE, /* pc_relative */
673 complain_overflow_dont, /* complain_on_overflow */
674 ppc64_elf_unhandled_reloc, /* special_function */
675 "R_PPC64_PLT16_HI", /* name */
676 FALSE, /* partial_inplace */
678 0xffff, /* dst_mask */
679 FALSE), /* pcrel_offset */
681 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
683 HOWTO (R_PPC64_PLT16_HA, /* type */
685 1, /* size (0 = byte, 1 = short, 2 = long) */
687 FALSE, /* pc_relative */
689 complain_overflow_dont, /* complain_on_overflow */
690 ppc64_elf_unhandled_reloc, /* special_function */
691 "R_PPC64_PLT16_HA", /* name */
692 FALSE, /* partial_inplace */
694 0xffff, /* dst_mask */
695 FALSE), /* pcrel_offset */
697 /* 16-bit section relative relocation. */
698 HOWTO (R_PPC64_SECTOFF, /* type */
700 1, /* size (0 = byte, 1 = short, 2 = long) */
702 FALSE, /* pc_relative */
704 complain_overflow_bitfield, /* complain_on_overflow */
705 ppc64_elf_sectoff_reloc, /* special_function */
706 "R_PPC64_SECTOFF", /* name */
707 FALSE, /* partial_inplace */
709 0xffff, /* dst_mask */
710 FALSE), /* pcrel_offset */
712 /* Like R_PPC64_SECTOFF, but no overflow warning. */
713 HOWTO (R_PPC64_SECTOFF_LO, /* type */
715 1, /* size (0 = byte, 1 = short, 2 = long) */
717 FALSE, /* pc_relative */
719 complain_overflow_dont, /* complain_on_overflow */
720 ppc64_elf_sectoff_reloc, /* special_function */
721 "R_PPC64_SECTOFF_LO", /* name */
722 FALSE, /* partial_inplace */
724 0xffff, /* dst_mask */
725 FALSE), /* pcrel_offset */
727 /* 16-bit upper half section relative relocation. */
728 HOWTO (R_PPC64_SECTOFF_HI, /* type */
730 1, /* size (0 = byte, 1 = short, 2 = long) */
732 FALSE, /* pc_relative */
734 complain_overflow_dont, /* complain_on_overflow */
735 ppc64_elf_sectoff_reloc, /* special_function */
736 "R_PPC64_SECTOFF_HI", /* name */
737 FALSE, /* partial_inplace */
739 0xffff, /* dst_mask */
740 FALSE), /* pcrel_offset */
742 /* 16-bit upper half adjusted section relative relocation. */
743 HOWTO (R_PPC64_SECTOFF_HA, /* type */
745 1, /* size (0 = byte, 1 = short, 2 = long) */
747 FALSE, /* pc_relative */
749 complain_overflow_dont, /* complain_on_overflow */
750 ppc64_elf_sectoff_ha_reloc, /* special_function */
751 "R_PPC64_SECTOFF_HA", /* name */
752 FALSE, /* partial_inplace */
754 0xffff, /* dst_mask */
755 FALSE), /* pcrel_offset */
757 /* Like R_PPC64_REL24 without touching the two least significant bits. */
758 HOWTO (R_PPC64_REL30, /* type */
760 2, /* size (0 = byte, 1 = short, 2 = long) */
762 TRUE, /* pc_relative */
764 complain_overflow_dont, /* complain_on_overflow */
765 bfd_elf_generic_reloc, /* special_function */
766 "R_PPC64_REL30", /* name */
767 FALSE, /* partial_inplace */
769 0xfffffffc, /* dst_mask */
770 TRUE), /* pcrel_offset */
772 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
774 /* A standard 64-bit relocation. */
775 HOWTO (R_PPC64_ADDR64, /* type */
777 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
779 FALSE, /* pc_relative */
781 complain_overflow_dont, /* complain_on_overflow */
782 bfd_elf_generic_reloc, /* special_function */
783 "R_PPC64_ADDR64", /* name */
784 FALSE, /* partial_inplace */
786 ONES (64), /* dst_mask */
787 FALSE), /* pcrel_offset */
789 /* The bits 32-47 of an address. */
790 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
792 1, /* size (0 = byte, 1 = short, 2 = long) */
794 FALSE, /* pc_relative */
796 complain_overflow_dont, /* complain_on_overflow */
797 bfd_elf_generic_reloc, /* special_function */
798 "R_PPC64_ADDR16_HIGHER", /* name */
799 FALSE, /* partial_inplace */
801 0xffff, /* dst_mask */
802 FALSE), /* pcrel_offset */
804 /* The bits 32-47 of an address, plus 1 if the contents of the low
805 16 bits, treated as a signed number, is negative. */
806 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
808 1, /* size (0 = byte, 1 = short, 2 = long) */
810 FALSE, /* pc_relative */
812 complain_overflow_dont, /* complain_on_overflow */
813 ppc64_elf_ha_reloc, /* special_function */
814 "R_PPC64_ADDR16_HIGHERA", /* name */
815 FALSE, /* partial_inplace */
817 0xffff, /* dst_mask */
818 FALSE), /* pcrel_offset */
820 /* The bits 48-63 of an address. */
821 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
823 1, /* size (0 = byte, 1 = short, 2 = long) */
825 FALSE, /* pc_relative */
827 complain_overflow_dont, /* complain_on_overflow */
828 bfd_elf_generic_reloc, /* special_function */
829 "R_PPC64_ADDR16_HIGHEST", /* name */
830 FALSE, /* partial_inplace */
832 0xffff, /* dst_mask */
833 FALSE), /* pcrel_offset */
835 /* The bits 48-63 of an address, plus 1 if the contents of the low
836 16 bits, treated as a signed number, is negative. */
837 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
839 1, /* size (0 = byte, 1 = short, 2 = long) */
841 FALSE, /* pc_relative */
843 complain_overflow_dont, /* complain_on_overflow */
844 ppc64_elf_ha_reloc, /* special_function */
845 "R_PPC64_ADDR16_HIGHESTA", /* name */
846 FALSE, /* partial_inplace */
848 0xffff, /* dst_mask */
849 FALSE), /* pcrel_offset */
851 /* Like ADDR64, but may be unaligned. */
852 HOWTO (R_PPC64_UADDR64, /* type */
854 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
856 FALSE, /* pc_relative */
858 complain_overflow_dont, /* complain_on_overflow */
859 bfd_elf_generic_reloc, /* special_function */
860 "R_PPC64_UADDR64", /* name */
861 FALSE, /* partial_inplace */
863 ONES (64), /* dst_mask */
864 FALSE), /* pcrel_offset */
866 /* 64-bit relative relocation. */
867 HOWTO (R_PPC64_REL64, /* type */
869 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
871 TRUE, /* pc_relative */
873 complain_overflow_dont, /* complain_on_overflow */
874 bfd_elf_generic_reloc, /* special_function */
875 "R_PPC64_REL64", /* name */
876 FALSE, /* partial_inplace */
878 ONES (64), /* dst_mask */
879 TRUE), /* pcrel_offset */
881 /* 64-bit relocation to the symbol's procedure linkage table. */
882 HOWTO (R_PPC64_PLT64, /* type */
884 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
886 FALSE, /* pc_relative */
888 complain_overflow_dont, /* complain_on_overflow */
889 ppc64_elf_unhandled_reloc, /* special_function */
890 "R_PPC64_PLT64", /* name */
891 FALSE, /* partial_inplace */
893 ONES (64), /* dst_mask */
894 FALSE), /* pcrel_offset */
896 /* 64-bit PC relative relocation to the symbol's procedure linkage
898 /* FIXME: R_PPC64_PLTREL64 not supported. */
899 HOWTO (R_PPC64_PLTREL64, /* type */
901 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
903 TRUE, /* pc_relative */
905 complain_overflow_dont, /* complain_on_overflow */
906 ppc64_elf_unhandled_reloc, /* special_function */
907 "R_PPC64_PLTREL64", /* name */
908 FALSE, /* partial_inplace */
910 ONES (64), /* dst_mask */
911 TRUE), /* pcrel_offset */
913 /* 16 bit TOC-relative relocation. */
915 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
916 HOWTO (R_PPC64_TOC16, /* type */
918 1, /* size (0 = byte, 1 = short, 2 = long) */
920 FALSE, /* pc_relative */
922 complain_overflow_signed, /* complain_on_overflow */
923 ppc64_elf_toc_reloc, /* special_function */
924 "R_PPC64_TOC16", /* name */
925 FALSE, /* partial_inplace */
927 0xffff, /* dst_mask */
928 FALSE), /* pcrel_offset */
930 /* 16 bit TOC-relative relocation without overflow. */
932 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
933 HOWTO (R_PPC64_TOC16_LO, /* type */
935 1, /* size (0 = byte, 1 = short, 2 = long) */
937 FALSE, /* pc_relative */
939 complain_overflow_dont, /* complain_on_overflow */
940 ppc64_elf_toc_reloc, /* special_function */
941 "R_PPC64_TOC16_LO", /* name */
942 FALSE, /* partial_inplace */
944 0xffff, /* dst_mask */
945 FALSE), /* pcrel_offset */
947 /* 16 bit TOC-relative relocation, high 16 bits. */
949 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
950 HOWTO (R_PPC64_TOC16_HI, /* type */
952 1, /* size (0 = byte, 1 = short, 2 = long) */
954 FALSE, /* pc_relative */
956 complain_overflow_dont, /* complain_on_overflow */
957 ppc64_elf_toc_reloc, /* special_function */
958 "R_PPC64_TOC16_HI", /* name */
959 FALSE, /* partial_inplace */
961 0xffff, /* dst_mask */
962 FALSE), /* pcrel_offset */
964 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
965 contents of the low 16 bits, treated as a signed number, is
968 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
969 HOWTO (R_PPC64_TOC16_HA, /* type */
971 1, /* size (0 = byte, 1 = short, 2 = long) */
973 FALSE, /* pc_relative */
975 complain_overflow_dont, /* complain_on_overflow */
976 ppc64_elf_toc_ha_reloc, /* special_function */
977 "R_PPC64_TOC16_HA", /* name */
978 FALSE, /* partial_inplace */
980 0xffff, /* dst_mask */
981 FALSE), /* pcrel_offset */
983 /* 64-bit relocation; insert value of TOC base (.TOC.). */
985 /* R_PPC64_TOC 51 doubleword64 .TOC. */
986 HOWTO (R_PPC64_TOC, /* type */
988 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
990 FALSE, /* pc_relative */
992 complain_overflow_bitfield, /* complain_on_overflow */
993 ppc64_elf_toc64_reloc, /* special_function */
994 "R_PPC64_TOC", /* name */
995 FALSE, /* partial_inplace */
997 ONES (64), /* dst_mask */
998 FALSE), /* pcrel_offset */
1000 /* Like R_PPC64_GOT16, but also informs the link editor that the
1001 value to relocate may (!) refer to a PLT entry which the link
1002 editor (a) may replace with the symbol value. If the link editor
1003 is unable to fully resolve the symbol, it may (b) create a PLT
1004 entry and store the address to the new PLT entry in the GOT.
1005 This permits lazy resolution of function symbols at run time.
1006 The link editor may also skip all of this and just (c) emit a
1007 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1008 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1009 HOWTO (R_PPC64_PLTGOT16, /* type */
1011 1, /* size (0 = byte, 1 = short, 2 = long) */
1013 FALSE, /* pc_relative */
1015 complain_overflow_signed, /* complain_on_overflow */
1016 ppc64_elf_unhandled_reloc, /* special_function */
1017 "R_PPC64_PLTGOT16", /* name */
1018 FALSE, /* partial_inplace */
1020 0xffff, /* dst_mask */
1021 FALSE), /* pcrel_offset */
1023 /* Like R_PPC64_PLTGOT16, but without overflow. */
1024 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1025 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1027 1, /* size (0 = byte, 1 = short, 2 = long) */
1029 FALSE, /* pc_relative */
1031 complain_overflow_dont, /* complain_on_overflow */
1032 ppc64_elf_unhandled_reloc, /* special_function */
1033 "R_PPC64_PLTGOT16_LO", /* name */
1034 FALSE, /* partial_inplace */
1036 0xffff, /* dst_mask */
1037 FALSE), /* pcrel_offset */
1039 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1040 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1041 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1042 16, /* rightshift */
1043 1, /* size (0 = byte, 1 = short, 2 = long) */
1045 FALSE, /* pc_relative */
1047 complain_overflow_dont, /* complain_on_overflow */
1048 ppc64_elf_unhandled_reloc, /* special_function */
1049 "R_PPC64_PLTGOT16_HI", /* name */
1050 FALSE, /* partial_inplace */
1052 0xffff, /* dst_mask */
1053 FALSE), /* pcrel_offset */
1055 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1056 1 if the contents of the low 16 bits, treated as a signed number,
1058 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1059 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1060 16, /* rightshift */
1061 1, /* size (0 = byte, 1 = short, 2 = long) */
1063 FALSE, /* pc_relative */
1065 complain_overflow_dont,/* complain_on_overflow */
1066 ppc64_elf_unhandled_reloc, /* special_function */
1067 "R_PPC64_PLTGOT16_HA", /* name */
1068 FALSE, /* partial_inplace */
1070 0xffff, /* dst_mask */
1071 FALSE), /* pcrel_offset */
1073 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1074 HOWTO (R_PPC64_ADDR16_DS, /* type */
1076 1, /* size (0 = byte, 1 = short, 2 = long) */
1078 FALSE, /* pc_relative */
1080 complain_overflow_bitfield, /* complain_on_overflow */
1081 bfd_elf_generic_reloc, /* special_function */
1082 "R_PPC64_ADDR16_DS", /* name */
1083 FALSE, /* partial_inplace */
1085 0xfffc, /* dst_mask */
1086 FALSE), /* pcrel_offset */
1088 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1089 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1091 1, /* size (0 = byte, 1 = short, 2 = long) */
1093 FALSE, /* pc_relative */
1095 complain_overflow_dont,/* complain_on_overflow */
1096 bfd_elf_generic_reloc, /* special_function */
1097 "R_PPC64_ADDR16_LO_DS",/* name */
1098 FALSE, /* partial_inplace */
1100 0xfffc, /* dst_mask */
1101 FALSE), /* pcrel_offset */
1103 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1104 HOWTO (R_PPC64_GOT16_DS, /* type */
1106 1, /* size (0 = byte, 1 = short, 2 = long) */
1108 FALSE, /* pc_relative */
1110 complain_overflow_signed, /* complain_on_overflow */
1111 ppc64_elf_unhandled_reloc, /* special_function */
1112 "R_PPC64_GOT16_DS", /* name */
1113 FALSE, /* partial_inplace */
1115 0xfffc, /* dst_mask */
1116 FALSE), /* pcrel_offset */
1118 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1119 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1121 1, /* size (0 = byte, 1 = short, 2 = long) */
1123 FALSE, /* pc_relative */
1125 complain_overflow_dont, /* complain_on_overflow */
1126 ppc64_elf_unhandled_reloc, /* special_function */
1127 "R_PPC64_GOT16_LO_DS", /* name */
1128 FALSE, /* partial_inplace */
1130 0xfffc, /* dst_mask */
1131 FALSE), /* pcrel_offset */
1133 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1134 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1136 1, /* size (0 = byte, 1 = short, 2 = long) */
1138 FALSE, /* pc_relative */
1140 complain_overflow_dont, /* complain_on_overflow */
1141 ppc64_elf_unhandled_reloc, /* special_function */
1142 "R_PPC64_PLT16_LO_DS", /* name */
1143 FALSE, /* partial_inplace */
1145 0xfffc, /* dst_mask */
1146 FALSE), /* pcrel_offset */
1148 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1149 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1151 1, /* size (0 = byte, 1 = short, 2 = long) */
1153 FALSE, /* pc_relative */
1155 complain_overflow_bitfield, /* complain_on_overflow */
1156 ppc64_elf_sectoff_reloc, /* special_function */
1157 "R_PPC64_SECTOFF_DS", /* name */
1158 FALSE, /* partial_inplace */
1160 0xfffc, /* dst_mask */
1161 FALSE), /* pcrel_offset */
1163 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1164 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1166 1, /* size (0 = byte, 1 = short, 2 = long) */
1168 FALSE, /* pc_relative */
1170 complain_overflow_dont, /* complain_on_overflow */
1171 ppc64_elf_sectoff_reloc, /* special_function */
1172 "R_PPC64_SECTOFF_LO_DS",/* name */
1173 FALSE, /* partial_inplace */
1175 0xfffc, /* dst_mask */
1176 FALSE), /* pcrel_offset */
1178 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1179 HOWTO (R_PPC64_TOC16_DS, /* type */
1181 1, /* size (0 = byte, 1 = short, 2 = long) */
1183 FALSE, /* pc_relative */
1185 complain_overflow_signed, /* complain_on_overflow */
1186 ppc64_elf_toc_reloc, /* special_function */
1187 "R_PPC64_TOC16_DS", /* name */
1188 FALSE, /* partial_inplace */
1190 0xfffc, /* dst_mask */
1191 FALSE), /* pcrel_offset */
1193 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1194 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1196 1, /* size (0 = byte, 1 = short, 2 = long) */
1198 FALSE, /* pc_relative */
1200 complain_overflow_dont, /* complain_on_overflow */
1201 ppc64_elf_toc_reloc, /* special_function */
1202 "R_PPC64_TOC16_LO_DS", /* name */
1203 FALSE, /* partial_inplace */
1205 0xfffc, /* dst_mask */
1206 FALSE), /* pcrel_offset */
1208 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1209 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1210 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1212 1, /* size (0 = byte, 1 = short, 2 = long) */
1214 FALSE, /* pc_relative */
1216 complain_overflow_signed, /* complain_on_overflow */
1217 ppc64_elf_unhandled_reloc, /* special_function */
1218 "R_PPC64_PLTGOT16_DS", /* name */
1219 FALSE, /* partial_inplace */
1221 0xfffc, /* dst_mask */
1222 FALSE), /* pcrel_offset */
1224 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1225 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1226 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1228 1, /* size (0 = byte, 1 = short, 2 = long) */
1230 FALSE, /* pc_relative */
1232 complain_overflow_dont, /* complain_on_overflow */
1233 ppc64_elf_unhandled_reloc, /* special_function */
1234 "R_PPC64_PLTGOT16_LO_DS",/* name */
1235 FALSE, /* partial_inplace */
1237 0xfffc, /* dst_mask */
1238 FALSE), /* pcrel_offset */
1240 /* Marker relocs for TLS. */
1243 2, /* size (0 = byte, 1 = short, 2 = long) */
1245 FALSE, /* pc_relative */
1247 complain_overflow_dont, /* complain_on_overflow */
1248 bfd_elf_generic_reloc, /* special_function */
1249 "R_PPC64_TLS", /* name */
1250 FALSE, /* partial_inplace */
1253 FALSE), /* pcrel_offset */
1255 HOWTO (R_PPC64_TLSGD,
1257 2, /* size (0 = byte, 1 = short, 2 = long) */
1259 FALSE, /* pc_relative */
1261 complain_overflow_dont, /* complain_on_overflow */
1262 bfd_elf_generic_reloc, /* special_function */
1263 "R_PPC64_TLSGD", /* name */
1264 FALSE, /* partial_inplace */
1267 FALSE), /* pcrel_offset */
1269 HOWTO (R_PPC64_TLSLD,
1271 2, /* size (0 = byte, 1 = short, 2 = long) */
1273 FALSE, /* pc_relative */
1275 complain_overflow_dont, /* complain_on_overflow */
1276 bfd_elf_generic_reloc, /* special_function */
1277 "R_PPC64_TLSLD", /* name */
1278 FALSE, /* partial_inplace */
1281 FALSE), /* pcrel_offset */
1283 /* Computes the load module index of the load module that contains the
1284 definition of its TLS sym. */
1285 HOWTO (R_PPC64_DTPMOD64,
1287 4, /* size (0 = byte, 1 = short, 2 = long) */
1289 FALSE, /* pc_relative */
1291 complain_overflow_dont, /* complain_on_overflow */
1292 ppc64_elf_unhandled_reloc, /* special_function */
1293 "R_PPC64_DTPMOD64", /* name */
1294 FALSE, /* partial_inplace */
1296 ONES (64), /* dst_mask */
1297 FALSE), /* pcrel_offset */
1299 /* Computes a dtv-relative displacement, the difference between the value
1300 of sym+add and the base address of the thread-local storage block that
1301 contains the definition of sym, minus 0x8000. */
1302 HOWTO (R_PPC64_DTPREL64,
1304 4, /* size (0 = byte, 1 = short, 2 = long) */
1306 FALSE, /* pc_relative */
1308 complain_overflow_dont, /* complain_on_overflow */
1309 ppc64_elf_unhandled_reloc, /* special_function */
1310 "R_PPC64_DTPREL64", /* name */
1311 FALSE, /* partial_inplace */
1313 ONES (64), /* dst_mask */
1314 FALSE), /* pcrel_offset */
1316 /* A 16 bit dtprel reloc. */
1317 HOWTO (R_PPC64_DTPREL16,
1319 1, /* size (0 = byte, 1 = short, 2 = long) */
1321 FALSE, /* pc_relative */
1323 complain_overflow_signed, /* complain_on_overflow */
1324 ppc64_elf_unhandled_reloc, /* special_function */
1325 "R_PPC64_DTPREL16", /* name */
1326 FALSE, /* partial_inplace */
1328 0xffff, /* dst_mask */
1329 FALSE), /* pcrel_offset */
1331 /* Like DTPREL16, but no overflow. */
1332 HOWTO (R_PPC64_DTPREL16_LO,
1334 1, /* size (0 = byte, 1 = short, 2 = long) */
1336 FALSE, /* pc_relative */
1338 complain_overflow_dont, /* complain_on_overflow */
1339 ppc64_elf_unhandled_reloc, /* special_function */
1340 "R_PPC64_DTPREL16_LO", /* name */
1341 FALSE, /* partial_inplace */
1343 0xffff, /* dst_mask */
1344 FALSE), /* pcrel_offset */
1346 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1347 HOWTO (R_PPC64_DTPREL16_HI,
1348 16, /* rightshift */
1349 1, /* size (0 = byte, 1 = short, 2 = long) */
1351 FALSE, /* pc_relative */
1353 complain_overflow_dont, /* complain_on_overflow */
1354 ppc64_elf_unhandled_reloc, /* special_function */
1355 "R_PPC64_DTPREL16_HI", /* name */
1356 FALSE, /* partial_inplace */
1358 0xffff, /* dst_mask */
1359 FALSE), /* pcrel_offset */
1361 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1362 HOWTO (R_PPC64_DTPREL16_HA,
1363 16, /* rightshift */
1364 1, /* size (0 = byte, 1 = short, 2 = long) */
1366 FALSE, /* pc_relative */
1368 complain_overflow_dont, /* complain_on_overflow */
1369 ppc64_elf_unhandled_reloc, /* special_function */
1370 "R_PPC64_DTPREL16_HA", /* name */
1371 FALSE, /* partial_inplace */
1373 0xffff, /* dst_mask */
1374 FALSE), /* pcrel_offset */
1376 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1377 HOWTO (R_PPC64_DTPREL16_HIGHER,
1378 32, /* rightshift */
1379 1, /* size (0 = byte, 1 = short, 2 = long) */
1381 FALSE, /* pc_relative */
1383 complain_overflow_dont, /* complain_on_overflow */
1384 ppc64_elf_unhandled_reloc, /* special_function */
1385 "R_PPC64_DTPREL16_HIGHER", /* name */
1386 FALSE, /* partial_inplace */
1388 0xffff, /* dst_mask */
1389 FALSE), /* pcrel_offset */
1391 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1392 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1393 32, /* rightshift */
1394 1, /* size (0 = byte, 1 = short, 2 = long) */
1396 FALSE, /* pc_relative */
1398 complain_overflow_dont, /* complain_on_overflow */
1399 ppc64_elf_unhandled_reloc, /* special_function */
1400 "R_PPC64_DTPREL16_HIGHERA", /* name */
1401 FALSE, /* partial_inplace */
1403 0xffff, /* dst_mask */
1404 FALSE), /* pcrel_offset */
1406 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1407 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1408 48, /* rightshift */
1409 1, /* size (0 = byte, 1 = short, 2 = long) */
1411 FALSE, /* pc_relative */
1413 complain_overflow_dont, /* complain_on_overflow */
1414 ppc64_elf_unhandled_reloc, /* special_function */
1415 "R_PPC64_DTPREL16_HIGHEST", /* name */
1416 FALSE, /* partial_inplace */
1418 0xffff, /* dst_mask */
1419 FALSE), /* pcrel_offset */
1421 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1422 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1423 48, /* rightshift */
1424 1, /* size (0 = byte, 1 = short, 2 = long) */
1426 FALSE, /* pc_relative */
1428 complain_overflow_dont, /* complain_on_overflow */
1429 ppc64_elf_unhandled_reloc, /* special_function */
1430 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1431 FALSE, /* partial_inplace */
1433 0xffff, /* dst_mask */
1434 FALSE), /* pcrel_offset */
1436 /* Like DTPREL16, but for insns with a DS field. */
1437 HOWTO (R_PPC64_DTPREL16_DS,
1439 1, /* size (0 = byte, 1 = short, 2 = long) */
1441 FALSE, /* pc_relative */
1443 complain_overflow_signed, /* complain_on_overflow */
1444 ppc64_elf_unhandled_reloc, /* special_function */
1445 "R_PPC64_DTPREL16_DS", /* name */
1446 FALSE, /* partial_inplace */
1448 0xfffc, /* dst_mask */
1449 FALSE), /* pcrel_offset */
1451 /* Like DTPREL16_DS, but no overflow. */
1452 HOWTO (R_PPC64_DTPREL16_LO_DS,
1454 1, /* size (0 = byte, 1 = short, 2 = long) */
1456 FALSE, /* pc_relative */
1458 complain_overflow_dont, /* complain_on_overflow */
1459 ppc64_elf_unhandled_reloc, /* special_function */
1460 "R_PPC64_DTPREL16_LO_DS", /* name */
1461 FALSE, /* partial_inplace */
1463 0xfffc, /* dst_mask */
1464 FALSE), /* pcrel_offset */
1466 /* Computes a tp-relative displacement, the difference between the value of
1467 sym+add and the value of the thread pointer (r13). */
1468 HOWTO (R_PPC64_TPREL64,
1470 4, /* size (0 = byte, 1 = short, 2 = long) */
1472 FALSE, /* pc_relative */
1474 complain_overflow_dont, /* complain_on_overflow */
1475 ppc64_elf_unhandled_reloc, /* special_function */
1476 "R_PPC64_TPREL64", /* name */
1477 FALSE, /* partial_inplace */
1479 ONES (64), /* dst_mask */
1480 FALSE), /* pcrel_offset */
1482 /* A 16 bit tprel reloc. */
1483 HOWTO (R_PPC64_TPREL16,
1485 1, /* size (0 = byte, 1 = short, 2 = long) */
1487 FALSE, /* pc_relative */
1489 complain_overflow_signed, /* complain_on_overflow */
1490 ppc64_elf_unhandled_reloc, /* special_function */
1491 "R_PPC64_TPREL16", /* name */
1492 FALSE, /* partial_inplace */
1494 0xffff, /* dst_mask */
1495 FALSE), /* pcrel_offset */
1497 /* Like TPREL16, but no overflow. */
1498 HOWTO (R_PPC64_TPREL16_LO,
1500 1, /* size (0 = byte, 1 = short, 2 = long) */
1502 FALSE, /* pc_relative */
1504 complain_overflow_dont, /* complain_on_overflow */
1505 ppc64_elf_unhandled_reloc, /* special_function */
1506 "R_PPC64_TPREL16_LO", /* name */
1507 FALSE, /* partial_inplace */
1509 0xffff, /* dst_mask */
1510 FALSE), /* pcrel_offset */
1512 /* Like TPREL16_LO, but next higher group of 16 bits. */
1513 HOWTO (R_PPC64_TPREL16_HI,
1514 16, /* rightshift */
1515 1, /* size (0 = byte, 1 = short, 2 = long) */
1517 FALSE, /* pc_relative */
1519 complain_overflow_dont, /* complain_on_overflow */
1520 ppc64_elf_unhandled_reloc, /* special_function */
1521 "R_PPC64_TPREL16_HI", /* name */
1522 FALSE, /* partial_inplace */
1524 0xffff, /* dst_mask */
1525 FALSE), /* pcrel_offset */
1527 /* Like TPREL16_HI, but adjust for low 16 bits. */
1528 HOWTO (R_PPC64_TPREL16_HA,
1529 16, /* rightshift */
1530 1, /* size (0 = byte, 1 = short, 2 = long) */
1532 FALSE, /* pc_relative */
1534 complain_overflow_dont, /* complain_on_overflow */
1535 ppc64_elf_unhandled_reloc, /* special_function */
1536 "R_PPC64_TPREL16_HA", /* name */
1537 FALSE, /* partial_inplace */
1539 0xffff, /* dst_mask */
1540 FALSE), /* pcrel_offset */
1542 /* Like TPREL16_HI, but next higher group of 16 bits. */
1543 HOWTO (R_PPC64_TPREL16_HIGHER,
1544 32, /* rightshift */
1545 1, /* size (0 = byte, 1 = short, 2 = long) */
1547 FALSE, /* pc_relative */
1549 complain_overflow_dont, /* complain_on_overflow */
1550 ppc64_elf_unhandled_reloc, /* special_function */
1551 "R_PPC64_TPREL16_HIGHER", /* name */
1552 FALSE, /* partial_inplace */
1554 0xffff, /* dst_mask */
1555 FALSE), /* pcrel_offset */
1557 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1558 HOWTO (R_PPC64_TPREL16_HIGHERA,
1559 32, /* rightshift */
1560 1, /* size (0 = byte, 1 = short, 2 = long) */
1562 FALSE, /* pc_relative */
1564 complain_overflow_dont, /* complain_on_overflow */
1565 ppc64_elf_unhandled_reloc, /* special_function */
1566 "R_PPC64_TPREL16_HIGHERA", /* name */
1567 FALSE, /* partial_inplace */
1569 0xffff, /* dst_mask */
1570 FALSE), /* pcrel_offset */
1572 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1573 HOWTO (R_PPC64_TPREL16_HIGHEST,
1574 48, /* rightshift */
1575 1, /* size (0 = byte, 1 = short, 2 = long) */
1577 FALSE, /* pc_relative */
1579 complain_overflow_dont, /* complain_on_overflow */
1580 ppc64_elf_unhandled_reloc, /* special_function */
1581 "R_PPC64_TPREL16_HIGHEST", /* name */
1582 FALSE, /* partial_inplace */
1584 0xffff, /* dst_mask */
1585 FALSE), /* pcrel_offset */
1587 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1588 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1589 48, /* rightshift */
1590 1, /* size (0 = byte, 1 = short, 2 = long) */
1592 FALSE, /* pc_relative */
1594 complain_overflow_dont, /* complain_on_overflow */
1595 ppc64_elf_unhandled_reloc, /* special_function */
1596 "R_PPC64_TPREL16_HIGHESTA", /* name */
1597 FALSE, /* partial_inplace */
1599 0xffff, /* dst_mask */
1600 FALSE), /* pcrel_offset */
1602 /* Like TPREL16, but for insns with a DS field. */
1603 HOWTO (R_PPC64_TPREL16_DS,
1605 1, /* size (0 = byte, 1 = short, 2 = long) */
1607 FALSE, /* pc_relative */
1609 complain_overflow_signed, /* complain_on_overflow */
1610 ppc64_elf_unhandled_reloc, /* special_function */
1611 "R_PPC64_TPREL16_DS", /* name */
1612 FALSE, /* partial_inplace */
1614 0xfffc, /* dst_mask */
1615 FALSE), /* pcrel_offset */
1617 /* Like TPREL16_DS, but no overflow. */
1618 HOWTO (R_PPC64_TPREL16_LO_DS,
1620 1, /* size (0 = byte, 1 = short, 2 = long) */
1622 FALSE, /* pc_relative */
1624 complain_overflow_dont, /* complain_on_overflow */
1625 ppc64_elf_unhandled_reloc, /* special_function */
1626 "R_PPC64_TPREL16_LO_DS", /* name */
1627 FALSE, /* partial_inplace */
1629 0xfffc, /* dst_mask */
1630 FALSE), /* pcrel_offset */
1632 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1633 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1634 to the first entry relative to the TOC base (r2). */
1635 HOWTO (R_PPC64_GOT_TLSGD16,
1637 1, /* size (0 = byte, 1 = short, 2 = long) */
1639 FALSE, /* pc_relative */
1641 complain_overflow_signed, /* complain_on_overflow */
1642 ppc64_elf_unhandled_reloc, /* special_function */
1643 "R_PPC64_GOT_TLSGD16", /* name */
1644 FALSE, /* partial_inplace */
1646 0xffff, /* dst_mask */
1647 FALSE), /* pcrel_offset */
1649 /* Like GOT_TLSGD16, but no overflow. */
1650 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1652 1, /* size (0 = byte, 1 = short, 2 = long) */
1654 FALSE, /* pc_relative */
1656 complain_overflow_dont, /* complain_on_overflow */
1657 ppc64_elf_unhandled_reloc, /* special_function */
1658 "R_PPC64_GOT_TLSGD16_LO", /* name */
1659 FALSE, /* partial_inplace */
1661 0xffff, /* dst_mask */
1662 FALSE), /* pcrel_offset */
1664 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1665 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1666 16, /* rightshift */
1667 1, /* size (0 = byte, 1 = short, 2 = long) */
1669 FALSE, /* pc_relative */
1671 complain_overflow_dont, /* complain_on_overflow */
1672 ppc64_elf_unhandled_reloc, /* special_function */
1673 "R_PPC64_GOT_TLSGD16_HI", /* name */
1674 FALSE, /* partial_inplace */
1676 0xffff, /* dst_mask */
1677 FALSE), /* pcrel_offset */
1679 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1680 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1681 16, /* rightshift */
1682 1, /* size (0 = byte, 1 = short, 2 = long) */
1684 FALSE, /* pc_relative */
1686 complain_overflow_dont, /* complain_on_overflow */
1687 ppc64_elf_unhandled_reloc, /* special_function */
1688 "R_PPC64_GOT_TLSGD16_HA", /* name */
1689 FALSE, /* partial_inplace */
1691 0xffff, /* dst_mask */
1692 FALSE), /* pcrel_offset */
1694 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1695 with values (sym+add)@dtpmod and zero, and computes the offset to the
1696 first entry relative to the TOC base (r2). */
1697 HOWTO (R_PPC64_GOT_TLSLD16,
1699 1, /* size (0 = byte, 1 = short, 2 = long) */
1701 FALSE, /* pc_relative */
1703 complain_overflow_signed, /* complain_on_overflow */
1704 ppc64_elf_unhandled_reloc, /* special_function */
1705 "R_PPC64_GOT_TLSLD16", /* name */
1706 FALSE, /* partial_inplace */
1708 0xffff, /* dst_mask */
1709 FALSE), /* pcrel_offset */
1711 /* Like GOT_TLSLD16, but no overflow. */
1712 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1714 1, /* size (0 = byte, 1 = short, 2 = long) */
1716 FALSE, /* pc_relative */
1718 complain_overflow_dont, /* complain_on_overflow */
1719 ppc64_elf_unhandled_reloc, /* special_function */
1720 "R_PPC64_GOT_TLSLD16_LO", /* name */
1721 FALSE, /* partial_inplace */
1723 0xffff, /* dst_mask */
1724 FALSE), /* pcrel_offset */
1726 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1727 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1728 16, /* rightshift */
1729 1, /* size (0 = byte, 1 = short, 2 = long) */
1731 FALSE, /* pc_relative */
1733 complain_overflow_dont, /* complain_on_overflow */
1734 ppc64_elf_unhandled_reloc, /* special_function */
1735 "R_PPC64_GOT_TLSLD16_HI", /* name */
1736 FALSE, /* partial_inplace */
1738 0xffff, /* dst_mask */
1739 FALSE), /* pcrel_offset */
1741 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1742 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1743 16, /* rightshift */
1744 1, /* size (0 = byte, 1 = short, 2 = long) */
1746 FALSE, /* pc_relative */
1748 complain_overflow_dont, /* complain_on_overflow */
1749 ppc64_elf_unhandled_reloc, /* special_function */
1750 "R_PPC64_GOT_TLSLD16_HA", /* name */
1751 FALSE, /* partial_inplace */
1753 0xffff, /* dst_mask */
1754 FALSE), /* pcrel_offset */
1756 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1757 the offset to the entry relative to the TOC base (r2). */
1758 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1760 1, /* size (0 = byte, 1 = short, 2 = long) */
1762 FALSE, /* pc_relative */
1764 complain_overflow_signed, /* complain_on_overflow */
1765 ppc64_elf_unhandled_reloc, /* special_function */
1766 "R_PPC64_GOT_DTPREL16_DS", /* name */
1767 FALSE, /* partial_inplace */
1769 0xfffc, /* dst_mask */
1770 FALSE), /* pcrel_offset */
1772 /* Like GOT_DTPREL16_DS, but no overflow. */
1773 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1775 1, /* size (0 = byte, 1 = short, 2 = long) */
1777 FALSE, /* pc_relative */
1779 complain_overflow_dont, /* complain_on_overflow */
1780 ppc64_elf_unhandled_reloc, /* special_function */
1781 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1782 FALSE, /* partial_inplace */
1784 0xfffc, /* dst_mask */
1785 FALSE), /* pcrel_offset */
1787 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1788 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1789 16, /* rightshift */
1790 1, /* size (0 = byte, 1 = short, 2 = long) */
1792 FALSE, /* pc_relative */
1794 complain_overflow_dont, /* complain_on_overflow */
1795 ppc64_elf_unhandled_reloc, /* special_function */
1796 "R_PPC64_GOT_DTPREL16_HI", /* name */
1797 FALSE, /* partial_inplace */
1799 0xffff, /* dst_mask */
1800 FALSE), /* pcrel_offset */
1802 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1803 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1804 16, /* rightshift */
1805 1, /* size (0 = byte, 1 = short, 2 = long) */
1807 FALSE, /* pc_relative */
1809 complain_overflow_dont, /* complain_on_overflow */
1810 ppc64_elf_unhandled_reloc, /* special_function */
1811 "R_PPC64_GOT_DTPREL16_HA", /* name */
1812 FALSE, /* partial_inplace */
1814 0xffff, /* dst_mask */
1815 FALSE), /* pcrel_offset */
1817 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1818 offset to the entry relative to the TOC base (r2). */
1819 HOWTO (R_PPC64_GOT_TPREL16_DS,
1821 1, /* size (0 = byte, 1 = short, 2 = long) */
1823 FALSE, /* pc_relative */
1825 complain_overflow_signed, /* complain_on_overflow */
1826 ppc64_elf_unhandled_reloc, /* special_function */
1827 "R_PPC64_GOT_TPREL16_DS", /* name */
1828 FALSE, /* partial_inplace */
1830 0xfffc, /* dst_mask */
1831 FALSE), /* pcrel_offset */
1833 /* Like GOT_TPREL16_DS, but no overflow. */
1834 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1836 1, /* size (0 = byte, 1 = short, 2 = long) */
1838 FALSE, /* pc_relative */
1840 complain_overflow_dont, /* complain_on_overflow */
1841 ppc64_elf_unhandled_reloc, /* special_function */
1842 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1843 FALSE, /* partial_inplace */
1845 0xfffc, /* dst_mask */
1846 FALSE), /* pcrel_offset */
1848 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1849 HOWTO (R_PPC64_GOT_TPREL16_HI,
1850 16, /* rightshift */
1851 1, /* size (0 = byte, 1 = short, 2 = long) */
1853 FALSE, /* pc_relative */
1855 complain_overflow_dont, /* complain_on_overflow */
1856 ppc64_elf_unhandled_reloc, /* special_function */
1857 "R_PPC64_GOT_TPREL16_HI", /* name */
1858 FALSE, /* partial_inplace */
1860 0xffff, /* dst_mask */
1861 FALSE), /* pcrel_offset */
1863 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1864 HOWTO (R_PPC64_GOT_TPREL16_HA,
1865 16, /* rightshift */
1866 1, /* size (0 = byte, 1 = short, 2 = long) */
1868 FALSE, /* pc_relative */
1870 complain_overflow_dont, /* complain_on_overflow */
1871 ppc64_elf_unhandled_reloc, /* special_function */
1872 "R_PPC64_GOT_TPREL16_HA", /* name */
1873 FALSE, /* partial_inplace */
1875 0xffff, /* dst_mask */
1876 FALSE), /* pcrel_offset */
1878 HOWTO (R_PPC64_JMP_IREL, /* type */
1880 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1882 FALSE, /* pc_relative */
1884 complain_overflow_dont, /* complain_on_overflow */
1885 ppc64_elf_unhandled_reloc, /* special_function */
1886 "R_PPC64_JMP_IREL", /* name */
1887 FALSE, /* partial_inplace */
1890 FALSE), /* pcrel_offset */
1892 HOWTO (R_PPC64_IRELATIVE, /* type */
1894 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1896 FALSE, /* pc_relative */
1898 complain_overflow_dont, /* complain_on_overflow */
1899 bfd_elf_generic_reloc, /* special_function */
1900 "R_PPC64_IRELATIVE", /* name */
1901 FALSE, /* partial_inplace */
1903 ONES (64), /* dst_mask */
1904 FALSE), /* pcrel_offset */
1906 /* A 16 bit relative relocation. */
1907 HOWTO (R_PPC64_REL16, /* type */
1909 1, /* size (0 = byte, 1 = short, 2 = long) */
1911 TRUE, /* pc_relative */
1913 complain_overflow_bitfield, /* complain_on_overflow */
1914 bfd_elf_generic_reloc, /* special_function */
1915 "R_PPC64_REL16", /* name */
1916 FALSE, /* partial_inplace */
1918 0xffff, /* dst_mask */
1919 TRUE), /* pcrel_offset */
1921 /* A 16 bit relative relocation without overflow. */
1922 HOWTO (R_PPC64_REL16_LO, /* type */
1924 1, /* size (0 = byte, 1 = short, 2 = long) */
1926 TRUE, /* pc_relative */
1928 complain_overflow_dont,/* complain_on_overflow */
1929 bfd_elf_generic_reloc, /* special_function */
1930 "R_PPC64_REL16_LO", /* name */
1931 FALSE, /* partial_inplace */
1933 0xffff, /* dst_mask */
1934 TRUE), /* pcrel_offset */
1936 /* The high order 16 bits of a relative address. */
1937 HOWTO (R_PPC64_REL16_HI, /* type */
1938 16, /* rightshift */
1939 1, /* size (0 = byte, 1 = short, 2 = long) */
1941 TRUE, /* pc_relative */
1943 complain_overflow_dont, /* complain_on_overflow */
1944 bfd_elf_generic_reloc, /* special_function */
1945 "R_PPC64_REL16_HI", /* name */
1946 FALSE, /* partial_inplace */
1948 0xffff, /* dst_mask */
1949 TRUE), /* pcrel_offset */
1951 /* The high order 16 bits of a relative address, plus 1 if the contents of
1952 the low 16 bits, treated as a signed number, is negative. */
1953 HOWTO (R_PPC64_REL16_HA, /* type */
1954 16, /* rightshift */
1955 1, /* size (0 = byte, 1 = short, 2 = long) */
1957 TRUE, /* pc_relative */
1959 complain_overflow_dont, /* complain_on_overflow */
1960 ppc64_elf_ha_reloc, /* special_function */
1961 "R_PPC64_REL16_HA", /* name */
1962 FALSE, /* partial_inplace */
1964 0xffff, /* dst_mask */
1965 TRUE), /* pcrel_offset */
1967 /* GNU extension to record C++ vtable hierarchy. */
1968 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1970 0, /* size (0 = byte, 1 = short, 2 = long) */
1972 FALSE, /* pc_relative */
1974 complain_overflow_dont, /* complain_on_overflow */
1975 NULL, /* special_function */
1976 "R_PPC64_GNU_VTINHERIT", /* name */
1977 FALSE, /* partial_inplace */
1980 FALSE), /* pcrel_offset */
1982 /* GNU extension to record C++ vtable member usage. */
1983 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1985 0, /* size (0 = byte, 1 = short, 2 = long) */
1987 FALSE, /* pc_relative */
1989 complain_overflow_dont, /* complain_on_overflow */
1990 NULL, /* special_function */
1991 "R_PPC64_GNU_VTENTRY", /* name */
1992 FALSE, /* partial_inplace */
1995 FALSE), /* pcrel_offset */
1999 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2003 ppc_howto_init (void)
2005 unsigned int i, type;
2008 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2011 type = ppc64_elf_howto_raw[i].type;
2012 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2013 / sizeof (ppc64_elf_howto_table[0])));
2014 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2018 static reloc_howto_type *
2019 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2020 bfd_reloc_code_real_type code)
2022 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2024 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2025 /* Initialize howto table if needed. */
2033 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2035 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2037 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2039 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2041 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2043 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2045 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2047 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2049 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2051 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2053 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2055 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2057 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2059 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2061 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2063 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2065 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2067 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2069 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2071 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2073 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2075 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2077 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2079 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2081 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2083 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2085 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2087 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2089 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2091 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2093 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2095 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2097 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2099 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2101 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2103 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2105 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2107 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2109 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2111 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2113 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2115 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2117 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2119 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2121 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2123 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2125 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2127 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2129 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2131 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2133 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2135 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2137 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2139 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2141 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2143 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2145 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2147 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2149 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2151 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2153 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2155 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2157 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2159 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2161 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2163 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2165 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2167 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2169 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2171 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2173 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2175 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2177 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2179 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2181 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2183 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2185 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2187 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2189 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2191 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2193 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2195 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2197 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2199 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2201 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2203 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2205 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2207 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2209 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2211 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2213 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2215 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2217 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2219 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2221 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2223 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2225 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2227 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2229 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2231 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2233 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2235 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2237 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2239 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2241 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2243 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2245 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2249 return ppc64_elf_howto_table[r];
2252 static reloc_howto_type *
2253 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2259 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2261 if (ppc64_elf_howto_raw[i].name != NULL
2262 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2263 return &ppc64_elf_howto_raw[i];
2268 /* Set the howto pointer for a PowerPC ELF reloc. */
2271 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2272 Elf_Internal_Rela *dst)
2276 /* Initialize howto table if needed. */
2277 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2280 type = ELF64_R_TYPE (dst->r_info);
2281 if (type >= (sizeof (ppc64_elf_howto_table)
2282 / sizeof (ppc64_elf_howto_table[0])))
2284 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2286 type = R_PPC64_NONE;
2288 cache_ptr->howto = ppc64_elf_howto_table[type];
2291 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2293 static bfd_reloc_status_type
2294 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2295 void *data, asection *input_section,
2296 bfd *output_bfd, char **error_message)
2298 /* If this is a relocatable link (output_bfd test tells us), just
2299 call the generic function. Any adjustment will be done at final
2301 if (output_bfd != NULL)
2302 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2303 input_section, output_bfd, error_message);
2305 /* Adjust the addend for sign extension of the low 16 bits.
2306 We won't actually be using the low 16 bits, so trashing them
2308 reloc_entry->addend += 0x8000;
2309 return bfd_reloc_continue;
2312 static bfd_reloc_status_type
2313 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2314 void *data, asection *input_section,
2315 bfd *output_bfd, char **error_message)
2317 if (output_bfd != NULL)
2318 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2319 input_section, output_bfd, error_message);
2321 if (strcmp (symbol->section->name, ".opd") == 0
2322 && (symbol->section->owner->flags & DYNAMIC) == 0)
2324 bfd_vma dest = opd_entry_value (symbol->section,
2325 symbol->value + reloc_entry->addend,
2327 if (dest != (bfd_vma) -1)
2328 reloc_entry->addend = dest - (symbol->value
2329 + symbol->section->output_section->vma
2330 + symbol->section->output_offset);
2332 return bfd_reloc_continue;
2335 static bfd_reloc_status_type
2336 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2337 void *data, asection *input_section,
2338 bfd *output_bfd, char **error_message)
2341 enum elf_ppc64_reloc_type r_type;
2342 bfd_size_type octets;
2343 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
2344 bfd_boolean is_power4 = FALSE;
2346 /* If this is a relocatable link (output_bfd test tells us), just
2347 call the generic function. Any adjustment will be done at final
2349 if (output_bfd != NULL)
2350 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2351 input_section, output_bfd, error_message);
2353 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2354 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2355 insn &= ~(0x01 << 21);
2356 r_type = reloc_entry->howto->type;
2357 if (r_type == R_PPC64_ADDR14_BRTAKEN
2358 || r_type == R_PPC64_REL14_BRTAKEN)
2359 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2363 /* Set 'a' bit. This is 0b00010 in BO field for branch
2364 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2365 for branch on CTR insns (BO == 1a00t or 1a01t). */
2366 if ((insn & (0x14 << 21)) == (0x04 << 21))
2368 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2378 if (!bfd_is_com_section (symbol->section))
2379 target = symbol->value;
2380 target += symbol->section->output_section->vma;
2381 target += symbol->section->output_offset;
2382 target += reloc_entry->addend;
2384 from = (reloc_entry->address
2385 + input_section->output_offset
2386 + input_section->output_section->vma);
2388 /* Invert 'y' bit if not the default. */
2389 if ((bfd_signed_vma) (target - from) < 0)
2392 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2394 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2395 input_section, output_bfd, error_message);
2398 static bfd_reloc_status_type
2399 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2400 void *data, asection *input_section,
2401 bfd *output_bfd, char **error_message)
2403 /* If this is a relocatable link (output_bfd test tells us), just
2404 call the generic function. Any adjustment will be done at final
2406 if (output_bfd != NULL)
2407 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2408 input_section, output_bfd, error_message);
2410 /* Subtract the symbol section base address. */
2411 reloc_entry->addend -= symbol->section->output_section->vma;
2412 return bfd_reloc_continue;
2415 static bfd_reloc_status_type
2416 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2417 void *data, asection *input_section,
2418 bfd *output_bfd, char **error_message)
2420 /* If this is a relocatable link (output_bfd test tells us), just
2421 call the generic function. Any adjustment will be done at final
2423 if (output_bfd != NULL)
2424 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2425 input_section, output_bfd, error_message);
2427 /* Subtract the symbol section base address. */
2428 reloc_entry->addend -= symbol->section->output_section->vma;
2430 /* Adjust the addend for sign extension of the low 16 bits. */
2431 reloc_entry->addend += 0x8000;
2432 return bfd_reloc_continue;
2435 static bfd_reloc_status_type
2436 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2437 void *data, asection *input_section,
2438 bfd *output_bfd, char **error_message)
2442 /* If this is a relocatable link (output_bfd test tells us), just
2443 call the generic function. Any adjustment will be done at final
2445 if (output_bfd != NULL)
2446 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2447 input_section, output_bfd, error_message);
2449 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2451 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2453 /* Subtract the TOC base address. */
2454 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2455 return bfd_reloc_continue;
2458 static bfd_reloc_status_type
2459 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2460 void *data, asection *input_section,
2461 bfd *output_bfd, char **error_message)
2465 /* If this is a relocatable link (output_bfd test tells us), just
2466 call the generic function. Any adjustment will be done at final
2468 if (output_bfd != NULL)
2469 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2470 input_section, output_bfd, error_message);
2472 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2474 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2476 /* Subtract the TOC base address. */
2477 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2479 /* Adjust the addend for sign extension of the low 16 bits. */
2480 reloc_entry->addend += 0x8000;
2481 return bfd_reloc_continue;
2484 static bfd_reloc_status_type
2485 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2486 void *data, asection *input_section,
2487 bfd *output_bfd, char **error_message)
2490 bfd_size_type octets;
2492 /* If this is a relocatable link (output_bfd test tells us), just
2493 call the generic function. Any adjustment will be done at final
2495 if (output_bfd != NULL)
2496 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2497 input_section, output_bfd, error_message);
2499 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2501 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2503 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2504 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2505 return bfd_reloc_ok;
2508 static bfd_reloc_status_type
2509 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2510 void *data, asection *input_section,
2511 bfd *output_bfd, char **error_message)
2513 /* If this is a relocatable link (output_bfd test tells us), just
2514 call the generic function. Any adjustment will be done at final
2516 if (output_bfd != NULL)
2517 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2518 input_section, output_bfd, error_message);
2520 if (error_message != NULL)
2522 static char buf[60];
2523 sprintf (buf, "generic linker can't handle %s",
2524 reloc_entry->howto->name);
2525 *error_message = buf;
2527 return bfd_reloc_dangerous;
2530 /* Track GOT entries needed for a given symbol. We might need more
2531 than one got entry per symbol. */
2534 struct got_entry *next;
2536 /* The symbol addend that we'll be placing in the GOT. */
2539 /* Unlike other ELF targets, we use separate GOT entries for the same
2540 symbol referenced from different input files. This is to support
2541 automatic multiple TOC/GOT sections, where the TOC base can vary
2542 from one input file to another. After partitioning into TOC groups
2543 we merge entries within the group.
2545 Point to the BFD owning this GOT entry. */
2548 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2549 TLS_TPREL or TLS_DTPREL for tls entries. */
2550 unsigned char tls_type;
2552 /* Non-zero if got.ent points to real entry. */
2553 unsigned char is_indirect;
2555 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2558 bfd_signed_vma refcount;
2560 struct got_entry *ent;
2564 /* The same for PLT. */
2567 struct plt_entry *next;
2573 bfd_signed_vma refcount;
2578 struct ppc64_elf_obj_tdata
2580 struct elf_obj_tdata elf;
2582 /* Shortcuts to dynamic linker sections. */
2586 /* Used during garbage collection. We attach global symbols defined
2587 on removed .opd entries to this section so that the sym is removed. */
2588 asection *deleted_section;
2590 /* TLS local dynamic got entry handling. Support for multiple GOT
2591 sections means we potentially need one of these for each input bfd. */
2592 struct got_entry tlsld_got;
2594 /* A copy of relocs before they are modified for --emit-relocs. */
2595 Elf_Internal_Rela *opd_relocs;
2597 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2598 the reloc to be in the range -32768 to 32767. */
2599 unsigned int has_small_toc_reloc;
2602 #define ppc64_elf_tdata(bfd) \
2603 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2605 #define ppc64_tlsld_got(bfd) \
2606 (&ppc64_elf_tdata (bfd)->tlsld_got)
2608 #define is_ppc64_elf(bfd) \
2609 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2610 && elf_object_id (bfd) == PPC64_ELF_DATA)
2612 /* Override the generic function because we store some extras. */
2615 ppc64_elf_mkobject (bfd *abfd)
2617 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2621 /* Fix bad default arch selected for a 64 bit input bfd when the
2622 default is 32 bit. */
2625 ppc64_elf_object_p (bfd *abfd)
2627 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2629 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2631 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2633 /* Relies on arch after 32 bit default being 64 bit default. */
2634 abfd->arch_info = abfd->arch_info->next;
2635 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2641 /* Support for core dump NOTE sections. */
2644 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2646 size_t offset, size;
2648 if (note->descsz != 504)
2652 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2655 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 32);
2661 /* Make a ".reg/999" section. */
2662 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2663 size, note->descpos + offset);
2667 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2669 if (note->descsz != 136)
2672 elf_tdata (abfd)->core_program
2673 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2674 elf_tdata (abfd)->core_command
2675 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2681 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2694 va_start (ap, note_type);
2695 memset (data, 0, 40);
2696 strncpy (data + 40, va_arg (ap, const char *), 16);
2697 strncpy (data + 56, va_arg (ap, const char *), 80);
2699 return elfcore_write_note (abfd, buf, bufsiz,
2700 "CORE", note_type, data, sizeof (data));
2711 va_start (ap, note_type);
2712 memset (data, 0, 112);
2713 pid = va_arg (ap, long);
2714 bfd_put_32 (abfd, pid, data + 32);
2715 cursig = va_arg (ap, int);
2716 bfd_put_16 (abfd, cursig, data + 12);
2717 greg = va_arg (ap, const void *);
2718 memcpy (data + 112, greg, 384);
2719 memset (data + 496, 0, 8);
2721 return elfcore_write_note (abfd, buf, bufsiz,
2722 "CORE", note_type, data, sizeof (data));
2727 /* Merge backend specific data from an object file to the output
2728 object file when linking. */
2731 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
2733 /* Check if we have the same endianess. */
2734 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2735 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
2736 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2740 if (bfd_big_endian (ibfd))
2741 msg = _("%B: compiled for a big endian system "
2742 "and target is little endian");
2744 msg = _("%B: compiled for a little endian system "
2745 "and target is big endian");
2747 (*_bfd_error_handler) (msg, ibfd);
2749 bfd_set_error (bfd_error_wrong_format);
2756 /* Add extra PPC sections. */
2758 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2760 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2761 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2762 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2763 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2764 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2765 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2766 { NULL, 0, 0, 0, 0 }
2769 enum _ppc64_sec_type {
2775 struct _ppc64_elf_section_data
2777 struct bfd_elf_section_data elf;
2781 /* An array with one entry for each opd function descriptor. */
2782 struct _opd_sec_data
2784 /* Points to the function code section for local opd entries. */
2785 asection **func_sec;
2787 /* After editing .opd, adjust references to opd local syms. */
2791 /* An array for toc sections, indexed by offset/8. */
2792 struct _toc_sec_data
2794 /* Specifies the relocation symbol index used at a given toc offset. */
2797 /* And the relocation addend. */
2802 enum _ppc64_sec_type sec_type:2;
2804 /* Flag set when small branches are detected. Used to
2805 select suitable defaults for the stub group size. */
2806 unsigned int has_14bit_branch:1;
2809 #define ppc64_elf_section_data(sec) \
2810 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2813 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2815 if (!sec->used_by_bfd)
2817 struct _ppc64_elf_section_data *sdata;
2818 bfd_size_type amt = sizeof (*sdata);
2820 sdata = bfd_zalloc (abfd, amt);
2823 sec->used_by_bfd = sdata;
2826 return _bfd_elf_new_section_hook (abfd, sec);
2829 static struct _opd_sec_data *
2830 get_opd_info (asection * sec)
2833 && ppc64_elf_section_data (sec) != NULL
2834 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2835 return &ppc64_elf_section_data (sec)->u.opd;
2839 /* Parameters for the qsort hook. */
2840 static bfd_boolean synthetic_relocatable;
2842 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2845 compare_symbols (const void *ap, const void *bp)
2847 const asymbol *a = * (const asymbol **) ap;
2848 const asymbol *b = * (const asymbol **) bp;
2850 /* Section symbols first. */
2851 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2853 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2856 /* then .opd symbols. */
2857 if (strcmp (a->section->name, ".opd") == 0
2858 && strcmp (b->section->name, ".opd") != 0)
2860 if (strcmp (a->section->name, ".opd") != 0
2861 && strcmp (b->section->name, ".opd") == 0)
2864 /* then other code symbols. */
2865 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2866 == (SEC_CODE | SEC_ALLOC)
2867 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2868 != (SEC_CODE | SEC_ALLOC))
2871 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2872 != (SEC_CODE | SEC_ALLOC)
2873 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2874 == (SEC_CODE | SEC_ALLOC))
2877 if (synthetic_relocatable)
2879 if (a->section->id < b->section->id)
2882 if (a->section->id > b->section->id)
2886 if (a->value + a->section->vma < b->value + b->section->vma)
2889 if (a->value + a->section->vma > b->value + b->section->vma)
2892 /* For syms with the same value, prefer strong dynamic global function
2893 syms over other syms. */
2894 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2897 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2900 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2903 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2906 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2909 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2912 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2915 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2921 /* Search SYMS for a symbol of the given VALUE. */
2924 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
2932 mid = (lo + hi) >> 1;
2933 if (syms[mid]->value + syms[mid]->section->vma < value)
2935 else if (syms[mid]->value + syms[mid]->section->vma > value)
2945 mid = (lo + hi) >> 1;
2946 if (syms[mid]->section->id < id)
2948 else if (syms[mid]->section->id > id)
2950 else if (syms[mid]->value < value)
2952 else if (syms[mid]->value > value)
2962 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2964 bfd_vma vma = *(bfd_vma *) ptr;
2965 return ((section->flags & SEC_ALLOC) != 0
2966 && section->vma <= vma
2967 && vma < section->vma + section->size);
2970 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2971 entry syms. Also generate @plt symbols for the glink branch table. */
2974 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2975 long static_count, asymbol **static_syms,
2976 long dyn_count, asymbol **dyn_syms,
2983 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2985 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2990 opd = bfd_get_section_by_name (abfd, ".opd");
2994 symcount = static_count;
2996 symcount += dyn_count;
3000 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3004 if (!relocatable && static_count != 0 && dyn_count != 0)
3006 /* Use both symbol tables. */
3007 memcpy (syms, static_syms, static_count * sizeof (*syms));
3008 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3010 else if (!relocatable && static_count == 0)
3011 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3013 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3015 synthetic_relocatable = relocatable;
3016 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3018 if (!relocatable && symcount > 1)
3021 /* Trim duplicate syms, since we may have merged the normal and
3022 dynamic symbols. Actually, we only care about syms that have
3023 different values, so trim any with the same value. */
3024 for (i = 1, j = 1; i < symcount; ++i)
3025 if (syms[i - 1]->value + syms[i - 1]->section->vma
3026 != syms[i]->value + syms[i]->section->vma)
3027 syms[j++] = syms[i];
3032 if (strcmp (syms[i]->section->name, ".opd") == 0)
3036 for (; i < symcount; ++i)
3037 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3038 != (SEC_CODE | SEC_ALLOC))
3039 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3043 for (; i < symcount; ++i)
3044 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3048 for (; i < symcount; ++i)
3049 if (strcmp (syms[i]->section->name, ".opd") != 0)
3053 for (; i < symcount; ++i)
3054 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3055 != (SEC_CODE | SEC_ALLOC))
3063 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3068 if (opdsymend == secsymend)
3071 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3072 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3076 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3083 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3087 while (r < opd->relocation + relcount
3088 && r->address < syms[i]->value + opd->vma)
3091 if (r == opd->relocation + relcount)
3094 if (r->address != syms[i]->value + opd->vma)
3097 if (r->howto->type != R_PPC64_ADDR64)
3100 sym = *r->sym_ptr_ptr;
3101 if (!sym_exists_at (syms, opdsymend, symcount,
3102 sym->section->id, sym->value + r->addend))
3105 size += sizeof (asymbol);
3106 size += strlen (syms[i]->name) + 2;
3110 s = *ret = bfd_malloc (size);
3117 names = (char *) (s + count);
3119 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3123 while (r < opd->relocation + relcount
3124 && r->address < syms[i]->value + opd->vma)
3127 if (r == opd->relocation + relcount)
3130 if (r->address != syms[i]->value + opd->vma)
3133 if (r->howto->type != R_PPC64_ADDR64)
3136 sym = *r->sym_ptr_ptr;
3137 if (!sym_exists_at (syms, opdsymend, symcount,
3138 sym->section->id, sym->value + r->addend))
3143 s->flags |= BSF_SYNTHETIC;
3144 s->section = sym->section;
3145 s->value = sym->value + r->addend;
3148 len = strlen (syms[i]->name);
3149 memcpy (names, syms[i]->name, len + 1);
3151 /* Have udata.p point back to the original symbol this
3152 synthetic symbol was derived from. */
3153 s->udata.p = syms[i];
3160 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3164 bfd_vma glink_vma = 0, resolv_vma = 0;
3165 asection *dynamic, *glink = NULL, *relplt = NULL;
3168 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
3172 free_contents_and_exit:
3180 for (i = secsymend; i < opdsymend; ++i)
3184 /* Ignore bogus symbols. */
3185 if (syms[i]->value > opd->size - 8)
3188 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3189 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3192 size += sizeof (asymbol);
3193 size += strlen (syms[i]->name) + 2;
3197 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3199 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3201 bfd_byte *dynbuf, *extdyn, *extdynend;
3203 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3205 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3206 goto free_contents_and_exit;
3208 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3209 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3212 extdynend = extdyn + dynamic->size;
3213 for (; extdyn < extdynend; extdyn += extdynsize)
3215 Elf_Internal_Dyn dyn;
3216 (*swap_dyn_in) (abfd, extdyn, &dyn);
3218 if (dyn.d_tag == DT_NULL)
3221 if (dyn.d_tag == DT_PPC64_GLINK)
3223 /* The first glink stub starts at offset 32; see comment in
3224 ppc64_elf_finish_dynamic_sections. */
3225 glink_vma = dyn.d_un.d_val + 32;
3226 /* The .glink section usually does not survive the final
3227 link; search for the section (usually .text) where the
3228 glink stubs now reside. */
3229 glink = bfd_sections_find_if (abfd, section_covers_vma,
3240 /* Determine __glink trampoline by reading the relative branch
3241 from the first glink stub. */
3243 if (bfd_get_section_contents (abfd, glink, buf,
3244 glink_vma + 4 - glink->vma, 4))
3246 unsigned int insn = bfd_get_32 (abfd, buf);
3248 if ((insn & ~0x3fffffc) == 0)
3249 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3253 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3255 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3258 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3259 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3260 goto free_contents_and_exit;
3262 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3263 size += plt_count * sizeof (asymbol);
3265 p = relplt->relocation;
3266 for (i = 0; i < plt_count; i++, p++)
3268 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3270 size += sizeof ("+0x") - 1 + 16;
3275 s = *ret = bfd_malloc (size);
3277 goto free_contents_and_exit;
3279 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3281 for (i = secsymend; i < opdsymend; ++i)
3285 if (syms[i]->value > opd->size - 8)
3288 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3289 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3293 asection *sec = abfd->sections;
3300 long mid = (lo + hi) >> 1;
3301 if (syms[mid]->section->vma < ent)
3303 else if (syms[mid]->section->vma > ent)
3307 sec = syms[mid]->section;
3312 if (lo >= hi && lo > codesecsym)
3313 sec = syms[lo - 1]->section;
3315 for (; sec != NULL; sec = sec->next)
3319 if ((sec->flags & SEC_ALLOC) == 0
3320 || (sec->flags & SEC_LOAD) == 0)
3322 if ((sec->flags & SEC_CODE) != 0)
3325 s->flags |= BSF_SYNTHETIC;
3326 s->value = ent - s->section->vma;
3329 len = strlen (syms[i]->name);
3330 memcpy (names, syms[i]->name, len + 1);
3332 /* Have udata.p point back to the original symbol this
3333 synthetic symbol was derived from. */
3334 s->udata.p = syms[i];
3340 if (glink != NULL && relplt != NULL)
3344 /* Add a symbol for the main glink trampoline. */
3345 memset (s, 0, sizeof *s);
3347 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3349 s->value = resolv_vma - glink->vma;
3351 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3352 names += sizeof ("__glink_PLTresolve");
3357 /* FIXME: It would be very much nicer to put sym@plt on the
3358 stub rather than on the glink branch table entry. The
3359 objdump disassembler would then use a sensible symbol
3360 name on plt calls. The difficulty in doing so is
3361 a) finding the stubs, and,
3362 b) matching stubs against plt entries, and,
3363 c) there can be multiple stubs for a given plt entry.
3365 Solving (a) could be done by code scanning, but older
3366 ppc64 binaries used different stubs to current code.
3367 (b) is the tricky one since you need to known the toc
3368 pointer for at least one function that uses a pic stub to
3369 be able to calculate the plt address referenced.
3370 (c) means gdb would need to set multiple breakpoints (or
3371 find the glink branch itself) when setting breakpoints
3372 for pending shared library loads. */
3373 p = relplt->relocation;
3374 for (i = 0; i < plt_count; i++, p++)
3378 *s = **p->sym_ptr_ptr;
3379 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3380 we are defining a symbol, ensure one of them is set. */
3381 if ((s->flags & BSF_LOCAL) == 0)
3382 s->flags |= BSF_GLOBAL;
3383 s->flags |= BSF_SYNTHETIC;
3385 s->value = glink_vma - glink->vma;
3388 len = strlen ((*p->sym_ptr_ptr)->name);
3389 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3393 memcpy (names, "+0x", sizeof ("+0x") - 1);
3394 names += sizeof ("+0x") - 1;
3395 bfd_sprintf_vma (abfd, names, p->addend);
3396 names += strlen (names);
3398 memcpy (names, "@plt", sizeof ("@plt"));
3399 names += sizeof ("@plt");
3414 /* The following functions are specific to the ELF linker, while
3415 functions above are used generally. Those named ppc64_elf_* are
3416 called by the main ELF linker code. They appear in this file more
3417 or less in the order in which they are called. eg.
3418 ppc64_elf_check_relocs is called early in the link process,
3419 ppc64_elf_finish_dynamic_sections is one of the last functions
3422 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3423 functions have both a function code symbol and a function descriptor
3424 symbol. A call to foo in a relocatable object file looks like:
3431 The function definition in another object file might be:
3435 . .quad .TOC.@tocbase
3441 When the linker resolves the call during a static link, the branch
3442 unsurprisingly just goes to .foo and the .opd information is unused.
3443 If the function definition is in a shared library, things are a little
3444 different: The call goes via a plt call stub, the opd information gets
3445 copied to the plt, and the linker patches the nop.
3453 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
3454 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
3455 . std 2,40(1) # this is the general idea
3463 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3465 The "reloc ()" notation is supposed to indicate that the linker emits
3466 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3469 What are the difficulties here? Well, firstly, the relocations
3470 examined by the linker in check_relocs are against the function code
3471 sym .foo, while the dynamic relocation in the plt is emitted against
3472 the function descriptor symbol, foo. Somewhere along the line, we need
3473 to carefully copy dynamic link information from one symbol to the other.
3474 Secondly, the generic part of the elf linker will make .foo a dynamic
3475 symbol as is normal for most other backends. We need foo dynamic
3476 instead, at least for an application final link. However, when
3477 creating a shared library containing foo, we need to have both symbols
3478 dynamic so that references to .foo are satisfied during the early
3479 stages of linking. Otherwise the linker might decide to pull in a
3480 definition from some other object, eg. a static library.
3482 Update: As of August 2004, we support a new convention. Function
3483 calls may use the function descriptor symbol, ie. "bl foo". This
3484 behaves exactly as "bl .foo". */
3486 /* The linker needs to keep track of the number of relocs that it
3487 decides to copy as dynamic relocs in check_relocs for each symbol.
3488 This is so that it can later discard them if they are found to be
3489 unnecessary. We store the information in a field extending the
3490 regular ELF linker hash table. */
3492 struct ppc_dyn_relocs
3494 struct ppc_dyn_relocs *next;
3496 /* The input section of the reloc. */
3499 /* Total number of relocs copied for the input section. */
3500 bfd_size_type count;
3502 /* Number of pc-relative relocs copied for the input section. */
3503 bfd_size_type pc_count;
3506 /* Of those relocs that might be copied as dynamic relocs, this function
3507 selects those that must be copied when linking a shared library,
3508 even when the symbol is local. */
3511 must_be_dyn_reloc (struct bfd_link_info *info,
3512 enum elf_ppc64_reloc_type r_type)
3524 case R_PPC64_TPREL16:
3525 case R_PPC64_TPREL16_LO:
3526 case R_PPC64_TPREL16_HI:
3527 case R_PPC64_TPREL16_HA:
3528 case R_PPC64_TPREL16_DS:
3529 case R_PPC64_TPREL16_LO_DS:
3530 case R_PPC64_TPREL16_HIGHER:
3531 case R_PPC64_TPREL16_HIGHERA:
3532 case R_PPC64_TPREL16_HIGHEST:
3533 case R_PPC64_TPREL16_HIGHESTA:
3534 case R_PPC64_TPREL64:
3535 return !info->executable;
3539 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3540 copying dynamic variables from a shared lib into an app's dynbss
3541 section, and instead use a dynamic relocation to point into the
3542 shared lib. With code that gcc generates, it's vital that this be
3543 enabled; In the PowerPC64 ABI, the address of a function is actually
3544 the address of a function descriptor, which resides in the .opd
3545 section. gcc uses the descriptor directly rather than going via the
3546 GOT as some other ABI's do, which means that initialized function
3547 pointers must reference the descriptor. Thus, a function pointer
3548 initialized to the address of a function in a shared library will
3549 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3550 redefines the function descriptor symbol to point to the copy. This
3551 presents a problem as a plt entry for that function is also
3552 initialized from the function descriptor symbol and the copy reloc
3553 may not be initialized first. */
3554 #define ELIMINATE_COPY_RELOCS 1
3556 /* Section name for stubs is the associated section name plus this
3558 #define STUB_SUFFIX ".stub"
3561 ppc_stub_long_branch:
3562 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3563 destination, but a 24 bit branch in a stub section will reach.
3566 ppc_stub_plt_branch:
3567 Similar to the above, but a 24 bit branch in the stub section won't
3568 reach its destination.
3569 . addis %r12,%r2,xxx@toc@ha
3570 . ld %r11,xxx@toc@l(%r12)
3575 Used to call a function in a shared library. If it so happens that
3576 the plt entry referenced crosses a 64k boundary, then an extra
3577 "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
3578 . addis %r12,%r2,xxx@toc@ha
3580 . ld %r11,xxx+0@toc@l(%r12)
3582 . ld %r2,xxx+8@toc@l(%r12)
3583 . ld %r11,xxx+16@toc@l(%r12)
3586 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3587 code to adjust the value and save r2 to support multiple toc sections.
3588 A ppc_stub_long_branch with an r2 offset looks like:
3590 . addis %r2,%r2,off@ha
3591 . addi %r2,%r2,off@l
3594 A ppc_stub_plt_branch with an r2 offset looks like:
3596 . addis %r12,%r2,xxx@toc@ha
3597 . ld %r11,xxx@toc@l(%r12)
3598 . addis %r2,%r2,off@ha
3599 . addi %r2,%r2,off@l
3603 In cases where the "addis" instruction would add zero, the "addis" is
3604 omitted and following instructions modified slightly in some cases.
3607 enum ppc_stub_type {
3609 ppc_stub_long_branch,
3610 ppc_stub_long_branch_r2off,
3611 ppc_stub_plt_branch,
3612 ppc_stub_plt_branch_r2off,
3616 struct ppc_stub_hash_entry {
3618 /* Base hash table entry structure. */
3619 struct bfd_hash_entry root;
3621 enum ppc_stub_type stub_type;
3623 /* The stub section. */
3626 /* Offset within stub_sec of the beginning of this stub. */
3627 bfd_vma stub_offset;
3629 /* Given the symbol's value and its section we can determine its final
3630 value when building the stubs (so the stub knows where to jump. */
3631 bfd_vma target_value;
3632 asection *target_section;
3634 /* The symbol table entry, if any, that this was derived from. */
3635 struct ppc_link_hash_entry *h;
3636 struct plt_entry *plt_ent;
3638 /* And the reloc addend that this was derived from. */
3641 /* Where this stub is being called from, or, in the case of combined
3642 stub sections, the first input section in the group. */
3646 struct ppc_branch_hash_entry {
3648 /* Base hash table entry structure. */
3649 struct bfd_hash_entry root;
3651 /* Offset within branch lookup table. */
3652 unsigned int offset;
3654 /* Generation marker. */
3658 struct ppc_link_hash_entry
3660 struct elf_link_hash_entry elf;
3663 /* A pointer to the most recently used stub hash entry against this
3665 struct ppc_stub_hash_entry *stub_cache;
3667 /* A pointer to the next symbol starting with a '.' */
3668 struct ppc_link_hash_entry *next_dot_sym;
3671 /* Track dynamic relocs copied for this symbol. */
3672 struct ppc_dyn_relocs *dyn_relocs;
3674 /* Link between function code and descriptor symbols. */
3675 struct ppc_link_hash_entry *oh;
3677 /* Flag function code and descriptor symbols. */
3678 unsigned int is_func:1;
3679 unsigned int is_func_descriptor:1;
3680 unsigned int fake:1;
3682 /* Whether global opd/toc sym has been adjusted or not.
3683 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3684 should be set for all globals defined in any opd/toc section. */
3685 unsigned int adjust_done:1;
3687 /* Set if we twiddled this symbol to weak at some stage. */
3688 unsigned int was_undefined:1;
3690 /* Contexts in which symbol is used in the GOT (or TOC).
3691 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3692 corresponding relocs are encountered during check_relocs.
3693 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3694 indicate the corresponding GOT entry type is not needed.
3695 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3696 a TPREL one. We use a separate flag rather than setting TPREL
3697 just for convenience in distinguishing the two cases. */
3698 #define TLS_GD 1 /* GD reloc. */
3699 #define TLS_LD 2 /* LD reloc. */
3700 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
3701 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3702 #define TLS_TLS 16 /* Any TLS reloc. */
3703 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3704 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
3705 #define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
3706 unsigned char tls_mask;
3709 /* ppc64 ELF linker hash table. */
3711 struct ppc_link_hash_table
3713 struct elf_link_hash_table elf;
3715 /* The stub hash table. */
3716 struct bfd_hash_table stub_hash_table;
3718 /* Another hash table for plt_branch stubs. */
3719 struct bfd_hash_table branch_hash_table;
3721 /* Linker stub bfd. */
3724 /* Linker call-backs. */
3725 asection * (*add_stub_section) (const char *, asection *);
3726 void (*layout_sections_again) (void);
3728 /* Array to keep track of which stub sections have been created, and
3729 information on stub grouping. */
3731 /* This is the section to which stubs in the group will be attached. */
3733 /* The stub section. */
3735 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3739 /* Temp used when calculating TOC pointers. */
3742 asection *toc_first_sec;
3744 /* Highest input section id. */
3747 /* Highest output section index. */
3750 /* Used when adding symbols. */
3751 struct ppc_link_hash_entry *dot_syms;
3753 /* List of input sections for each output section. */
3754 asection **input_list;
3756 /* Short-cuts to get to dynamic linker sections. */
3769 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3770 struct ppc_link_hash_entry *tls_get_addr;
3771 struct ppc_link_hash_entry *tls_get_addr_fd;
3773 /* The size of reliplt used by got entry relocs. */
3774 bfd_size_type got_reli_size;
3777 unsigned long stub_count[ppc_stub_plt_call];
3779 /* Number of stubs against global syms. */
3780 unsigned long stub_globals;
3782 /* Set if we should emit symbols for stubs. */
3783 unsigned int emit_stub_syms:1;
3785 /* Set if __tls_get_addr optimization should not be done. */
3786 unsigned int no_tls_get_addr_opt:1;
3788 /* Support for multiple toc sections. */
3789 unsigned int do_multi_toc:1;
3790 unsigned int multi_toc_needed:1;
3791 unsigned int second_toc_pass:1;
3792 unsigned int do_toc_opt:1;
3795 unsigned int stub_error:1;
3797 /* Temp used by ppc64_elf_process_dot_syms. */
3798 unsigned int twiddled_syms:1;
3800 /* Incremented every time we size stubs. */
3801 unsigned int stub_iteration;
3803 /* Small local sym cache. */
3804 struct sym_cache sym_cache;
3807 /* Rename some of the generic section flags to better document how they
3810 /* Nonzero if this section has TLS related relocations. */
3811 #define has_tls_reloc sec_flg0
3813 /* Nonzero if this section has a call to __tls_get_addr. */
3814 #define has_tls_get_addr_call sec_flg1
3816 /* Nonzero if this section has any toc or got relocs. */
3817 #define has_toc_reloc sec_flg2
3819 /* Nonzero if this section has a call to another section that uses
3821 #define makes_toc_func_call sec_flg3
3823 /* Recursion protection when determining above flag. */
3824 #define call_check_in_progress sec_flg4
3825 #define call_check_done sec_flg5
3827 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3829 #define ppc_hash_table(p) \
3830 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3831 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3833 #define ppc_stub_hash_lookup(table, string, create, copy) \
3834 ((struct ppc_stub_hash_entry *) \
3835 bfd_hash_lookup ((table), (string), (create), (copy)))
3837 #define ppc_branch_hash_lookup(table, string, create, copy) \
3838 ((struct ppc_branch_hash_entry *) \
3839 bfd_hash_lookup ((table), (string), (create), (copy)))
3841 /* Create an entry in the stub hash table. */
3843 static struct bfd_hash_entry *
3844 stub_hash_newfunc (struct bfd_hash_entry *entry,
3845 struct bfd_hash_table *table,
3848 /* Allocate the structure if it has not already been allocated by a
3852 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3857 /* Call the allocation method of the superclass. */
3858 entry = bfd_hash_newfunc (entry, table, string);
3861 struct ppc_stub_hash_entry *eh;
3863 /* Initialize the local fields. */
3864 eh = (struct ppc_stub_hash_entry *) entry;
3865 eh->stub_type = ppc_stub_none;
3866 eh->stub_sec = NULL;
3867 eh->stub_offset = 0;
3868 eh->target_value = 0;
3869 eh->target_section = NULL;
3877 /* Create an entry in the branch hash table. */
3879 static struct bfd_hash_entry *
3880 branch_hash_newfunc (struct bfd_hash_entry *entry,
3881 struct bfd_hash_table *table,
3884 /* Allocate the structure if it has not already been allocated by a
3888 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3893 /* Call the allocation method of the superclass. */
3894 entry = bfd_hash_newfunc (entry, table, string);
3897 struct ppc_branch_hash_entry *eh;
3899 /* Initialize the local fields. */
3900 eh = (struct ppc_branch_hash_entry *) entry;
3908 /* Create an entry in a ppc64 ELF linker hash table. */
3910 static struct bfd_hash_entry *
3911 link_hash_newfunc (struct bfd_hash_entry *entry,
3912 struct bfd_hash_table *table,
3915 /* Allocate the structure if it has not already been allocated by a
3919 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3924 /* Call the allocation method of the superclass. */
3925 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3928 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3930 memset (&eh->u.stub_cache, 0,
3931 (sizeof (struct ppc_link_hash_entry)
3932 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3934 /* When making function calls, old ABI code references function entry
3935 points (dot symbols), while new ABI code references the function
3936 descriptor symbol. We need to make any combination of reference and
3937 definition work together, without breaking archive linking.
3939 For a defined function "foo" and an undefined call to "bar":
3940 An old object defines "foo" and ".foo", references ".bar" (possibly
3942 A new object defines "foo" and references "bar".
3944 A new object thus has no problem with its undefined symbols being
3945 satisfied by definitions in an old object. On the other hand, the
3946 old object won't have ".bar" satisfied by a new object.
3948 Keep a list of newly added dot-symbols. */
3950 if (string[0] == '.')
3952 struct ppc_link_hash_table *htab;
3954 htab = (struct ppc_link_hash_table *) table;
3955 eh->u.next_dot_sym = htab->dot_syms;
3956 htab->dot_syms = eh;
3963 /* Create a ppc64 ELF linker hash table. */
3965 static struct bfd_link_hash_table *
3966 ppc64_elf_link_hash_table_create (bfd *abfd)
3968 struct ppc_link_hash_table *htab;
3969 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3971 htab = bfd_zmalloc (amt);
3975 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3976 sizeof (struct ppc_link_hash_entry),
3983 /* Init the stub hash table too. */
3984 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3985 sizeof (struct ppc_stub_hash_entry)))
3988 /* And the branch hash table. */
3989 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3990 sizeof (struct ppc_branch_hash_entry)))
3993 /* Initializing two fields of the union is just cosmetic. We really
3994 only care about glist, but when compiled on a 32-bit host the
3995 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3996 debugger inspection of these fields look nicer. */
3997 htab->elf.init_got_refcount.refcount = 0;
3998 htab->elf.init_got_refcount.glist = NULL;
3999 htab->elf.init_plt_refcount.refcount = 0;
4000 htab->elf.init_plt_refcount.glist = NULL;
4001 htab->elf.init_got_offset.offset = 0;
4002 htab->elf.init_got_offset.glist = NULL;
4003 htab->elf.init_plt_offset.offset = 0;
4004 htab->elf.init_plt_offset.glist = NULL;
4006 return &htab->elf.root;
4009 /* Free the derived linker hash table. */
4012 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
4014 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
4016 bfd_hash_table_free (&ret->stub_hash_table);
4017 bfd_hash_table_free (&ret->branch_hash_table);
4018 _bfd_generic_link_hash_table_free (hash);
4021 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4024 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4026 struct ppc_link_hash_table *htab;
4028 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4030 /* Always hook our dynamic sections into the first bfd, which is the
4031 linker created stub bfd. This ensures that the GOT header is at
4032 the start of the output TOC section. */
4033 htab = ppc_hash_table (info);
4036 htab->stub_bfd = abfd;
4037 htab->elf.dynobj = abfd;
4040 /* Build a name for an entry in the stub hash table. */
4043 ppc_stub_name (const asection *input_section,
4044 const asection *sym_sec,
4045 const struct ppc_link_hash_entry *h,
4046 const Elf_Internal_Rela *rel)
4051 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4052 offsets from a sym as a branch target? In fact, we could
4053 probably assume the addend is always zero. */
4054 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4058 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4059 stub_name = bfd_malloc (len);
4060 if (stub_name == NULL)
4063 sprintf (stub_name, "%08x.%s+%x",
4064 input_section->id & 0xffffffff,
4065 h->elf.root.root.string,
4066 (int) rel->r_addend & 0xffffffff);
4070 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4071 stub_name = bfd_malloc (len);
4072 if (stub_name == NULL)
4075 sprintf (stub_name, "%08x.%x:%x+%x",
4076 input_section->id & 0xffffffff,
4077 sym_sec->id & 0xffffffff,
4078 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4079 (int) rel->r_addend & 0xffffffff);
4081 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4082 stub_name[len - 2] = 0;
4086 /* Look up an entry in the stub hash. Stub entries are cached because
4087 creating the stub name takes a bit of time. */
4089 static struct ppc_stub_hash_entry *
4090 ppc_get_stub_entry (const asection *input_section,
4091 const asection *sym_sec,
4092 struct ppc_link_hash_entry *h,
4093 const Elf_Internal_Rela *rel,
4094 struct ppc_link_hash_table *htab)
4096 struct ppc_stub_hash_entry *stub_entry;
4097 const asection *id_sec;
4099 /* If this input section is part of a group of sections sharing one
4100 stub section, then use the id of the first section in the group.
4101 Stub names need to include a section id, as there may well be
4102 more than one stub used to reach say, printf, and we need to
4103 distinguish between them. */
4104 id_sec = htab->stub_group[input_section->id].link_sec;
4106 if (h != NULL && h->u.stub_cache != NULL
4107 && h->u.stub_cache->h == h
4108 && h->u.stub_cache->id_sec == id_sec)
4110 stub_entry = h->u.stub_cache;
4116 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4117 if (stub_name == NULL)
4120 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4121 stub_name, FALSE, FALSE);
4123 h->u.stub_cache = stub_entry;
4131 /* Add a new stub entry to the stub hash. Not all fields of the new
4132 stub entry are initialised. */
4134 static struct ppc_stub_hash_entry *
4135 ppc_add_stub (const char *stub_name,
4137 struct ppc_link_hash_table *htab)
4141 struct ppc_stub_hash_entry *stub_entry;
4143 link_sec = htab->stub_group[section->id].link_sec;
4144 stub_sec = htab->stub_group[section->id].stub_sec;
4145 if (stub_sec == NULL)
4147 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4148 if (stub_sec == NULL)
4154 namelen = strlen (link_sec->name);
4155 len = namelen + sizeof (STUB_SUFFIX);
4156 s_name = bfd_alloc (htab->stub_bfd, len);
4160 memcpy (s_name, link_sec->name, namelen);
4161 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4162 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4163 if (stub_sec == NULL)
4165 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4167 htab->stub_group[section->id].stub_sec = stub_sec;
4170 /* Enter this entry into the linker stub hash table. */
4171 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4173 if (stub_entry == NULL)
4175 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
4176 section->owner, stub_name);
4180 stub_entry->stub_sec = stub_sec;
4181 stub_entry->stub_offset = 0;
4182 stub_entry->id_sec = link_sec;
4186 /* Create sections for linker generated code. */
4189 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4191 struct ppc_link_hash_table *htab;
4194 htab = ppc_hash_table (info);
4198 /* Create .sfpr for code to save and restore fp regs. */
4199 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4200 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4201 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4203 if (htab->sfpr == NULL
4204 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4207 /* Create .glink for lazy dynamic linking support. */
4208 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4210 if (htab->glink == NULL
4211 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4214 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4215 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4216 if (htab->iplt == NULL
4217 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4220 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4221 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4222 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4225 if (htab->reliplt == NULL
4226 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4229 /* Create branch lookup table for plt_branch stubs. */
4230 flags = (SEC_ALLOC | SEC_LOAD
4231 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4232 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4234 if (htab->brlt == NULL
4235 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4241 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4242 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4243 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4246 if (htab->relbrlt == NULL
4247 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4253 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4254 not already done. */
4257 create_got_section (bfd *abfd, struct bfd_link_info *info)
4259 asection *got, *relgot;
4261 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4263 if (!is_ppc64_elf (abfd))
4270 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4273 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
4278 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4279 | SEC_LINKER_CREATED);
4281 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4283 || !bfd_set_section_alignment (abfd, got, 3))
4286 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4287 flags | SEC_READONLY);
4289 || ! bfd_set_section_alignment (abfd, relgot, 3))
4292 ppc64_elf_tdata (abfd)->got = got;
4293 ppc64_elf_tdata (abfd)->relgot = relgot;
4297 /* Create the dynamic sections, and set up shortcuts. */
4300 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4302 struct ppc_link_hash_table *htab;
4304 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4307 htab = ppc_hash_table (info);
4312 htab->got = bfd_get_section_by_name (dynobj, ".got");
4313 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
4314 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
4315 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
4317 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
4319 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4320 || (!info->shared && !htab->relbss))
4326 /* Follow indirect and warning symbol links. */
4328 static inline struct bfd_link_hash_entry *
4329 follow_link (struct bfd_link_hash_entry *h)
4331 while (h->type == bfd_link_hash_indirect
4332 || h->type == bfd_link_hash_warning)
4337 static inline struct elf_link_hash_entry *
4338 elf_follow_link (struct elf_link_hash_entry *h)
4340 return (struct elf_link_hash_entry *) follow_link (&h->root);
4343 static inline struct ppc_link_hash_entry *
4344 ppc_follow_link (struct ppc_link_hash_entry *h)
4346 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4349 /* Merge PLT info on FROM with that on TO. */
4352 move_plt_plist (struct ppc_link_hash_entry *from,
4353 struct ppc_link_hash_entry *to)
4355 if (from->elf.plt.plist != NULL)
4357 if (to->elf.plt.plist != NULL)
4359 struct plt_entry **entp;
4360 struct plt_entry *ent;
4362 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4364 struct plt_entry *dent;
4366 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4367 if (dent->addend == ent->addend)
4369 dent->plt.refcount += ent->plt.refcount;
4376 *entp = to->elf.plt.plist;
4379 to->elf.plt.plist = from->elf.plt.plist;
4380 from->elf.plt.plist = NULL;
4384 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4387 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4388 struct elf_link_hash_entry *dir,
4389 struct elf_link_hash_entry *ind)
4391 struct ppc_link_hash_entry *edir, *eind;
4393 edir = (struct ppc_link_hash_entry *) dir;
4394 eind = (struct ppc_link_hash_entry *) ind;
4396 /* Copy over any dynamic relocs we may have on the indirect sym. */
4397 if (eind->dyn_relocs != NULL)
4399 if (edir->dyn_relocs != NULL)
4401 struct ppc_dyn_relocs **pp;
4402 struct ppc_dyn_relocs *p;
4404 /* Add reloc counts against the indirect sym to the direct sym
4405 list. Merge any entries against the same section. */
4406 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4408 struct ppc_dyn_relocs *q;
4410 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4411 if (q->sec == p->sec)
4413 q->pc_count += p->pc_count;
4414 q->count += p->count;
4421 *pp = edir->dyn_relocs;
4424 edir->dyn_relocs = eind->dyn_relocs;
4425 eind->dyn_relocs = NULL;
4428 edir->is_func |= eind->is_func;
4429 edir->is_func_descriptor |= eind->is_func_descriptor;
4430 edir->tls_mask |= eind->tls_mask;
4431 if (eind->oh != NULL)
4432 edir->oh = ppc_follow_link (eind->oh);
4434 /* If called to transfer flags for a weakdef during processing
4435 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4436 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4437 if (!(ELIMINATE_COPY_RELOCS
4438 && eind->elf.root.type != bfd_link_hash_indirect
4439 && edir->elf.dynamic_adjusted))
4440 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4442 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4443 edir->elf.ref_regular |= eind->elf.ref_regular;
4444 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4445 edir->elf.needs_plt |= eind->elf.needs_plt;
4447 /* If we were called to copy over info for a weak sym, that's all. */
4448 if (eind->elf.root.type != bfd_link_hash_indirect)
4451 /* Copy over got entries that we may have already seen to the
4452 symbol which just became indirect. */
4453 if (eind->elf.got.glist != NULL)
4455 if (edir->elf.got.glist != NULL)
4457 struct got_entry **entp;
4458 struct got_entry *ent;
4460 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4462 struct got_entry *dent;
4464 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4465 if (dent->addend == ent->addend
4466 && dent->owner == ent->owner
4467 && dent->tls_type == ent->tls_type)
4469 dent->got.refcount += ent->got.refcount;
4476 *entp = edir->elf.got.glist;
4479 edir->elf.got.glist = eind->elf.got.glist;
4480 eind->elf.got.glist = NULL;
4483 /* And plt entries. */
4484 move_plt_plist (eind, edir);
4486 if (eind->elf.dynindx != -1)
4488 if (edir->elf.dynindx != -1)
4489 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4490 edir->elf.dynstr_index);
4491 edir->elf.dynindx = eind->elf.dynindx;
4492 edir->elf.dynstr_index = eind->elf.dynstr_index;
4493 eind->elf.dynindx = -1;
4494 eind->elf.dynstr_index = 0;
4498 /* Find the function descriptor hash entry from the given function code
4499 hash entry FH. Link the entries via their OH fields. */
4501 static struct ppc_link_hash_entry *
4502 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4504 struct ppc_link_hash_entry *fdh = fh->oh;
4508 const char *fd_name = fh->elf.root.root.string + 1;
4510 fdh = (struct ppc_link_hash_entry *)
4511 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4515 fdh->is_func_descriptor = 1;
4521 return ppc_follow_link (fdh);
4524 /* Make a fake function descriptor sym for the code sym FH. */
4526 static struct ppc_link_hash_entry *
4527 make_fdh (struct bfd_link_info *info,
4528 struct ppc_link_hash_entry *fh)
4532 struct bfd_link_hash_entry *bh;
4533 struct ppc_link_hash_entry *fdh;
4535 abfd = fh->elf.root.u.undef.abfd;
4536 newsym = bfd_make_empty_symbol (abfd);
4537 newsym->name = fh->elf.root.root.string + 1;
4538 newsym->section = bfd_und_section_ptr;
4540 newsym->flags = BSF_WEAK;
4543 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4544 newsym->flags, newsym->section,
4545 newsym->value, NULL, FALSE, FALSE,
4549 fdh = (struct ppc_link_hash_entry *) bh;
4550 fdh->elf.non_elf = 0;
4552 fdh->is_func_descriptor = 1;
4559 /* Fix function descriptor symbols defined in .opd sections to be
4563 ppc64_elf_add_symbol_hook (bfd *ibfd,
4564 struct bfd_link_info *info,
4565 Elf_Internal_Sym *isym,
4566 const char **name ATTRIBUTE_UNUSED,
4567 flagword *flags ATTRIBUTE_UNUSED,
4569 bfd_vma *value ATTRIBUTE_UNUSED)
4571 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4573 if ((ibfd->flags & DYNAMIC) == 0)
4574 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4576 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4578 else if (*sec != NULL
4579 && strcmp ((*sec)->name, ".opd") == 0)
4580 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4585 /* This function makes an old ABI object reference to ".bar" cause the
4586 inclusion of a new ABI object archive that defines "bar".
4587 NAME is a symbol defined in an archive. Return a symbol in the hash
4588 table that might be satisfied by the archive symbols. */
4590 static struct elf_link_hash_entry *
4591 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4592 struct bfd_link_info *info,
4595 struct elf_link_hash_entry *h;
4599 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4601 /* Don't return this sym if it is a fake function descriptor
4602 created by add_symbol_adjust. */
4603 && !(h->root.type == bfd_link_hash_undefweak
4604 && ((struct ppc_link_hash_entry *) h)->fake))
4610 len = strlen (name);
4611 dot_name = bfd_alloc (abfd, len + 2);
4612 if (dot_name == NULL)
4613 return (struct elf_link_hash_entry *) 0 - 1;
4615 memcpy (dot_name + 1, name, len + 1);
4616 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4617 bfd_release (abfd, dot_name);
4621 /* This function satisfies all old ABI object references to ".bar" if a
4622 new ABI object defines "bar". Well, at least, undefined dot symbols
4623 are made weak. This stops later archive searches from including an
4624 object if we already have a function descriptor definition. It also
4625 prevents the linker complaining about undefined symbols.
4626 We also check and correct mismatched symbol visibility here. The
4627 most restrictive visibility of the function descriptor and the
4628 function entry symbol is used. */
4631 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4633 struct ppc_link_hash_table *htab;
4634 struct ppc_link_hash_entry *fdh;
4636 if (eh->elf.root.type == bfd_link_hash_indirect)
4639 if (eh->elf.root.type == bfd_link_hash_warning)
4640 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4642 if (eh->elf.root.root.string[0] != '.')
4645 htab = ppc_hash_table (info);
4649 fdh = lookup_fdh (eh, htab);
4652 if (!info->relocatable
4653 && (eh->elf.root.type == bfd_link_hash_undefined
4654 || eh->elf.root.type == bfd_link_hash_undefweak)
4655 && eh->elf.ref_regular)
4657 /* Make an undefweak function descriptor sym, which is enough to
4658 pull in an --as-needed shared lib, but won't cause link
4659 errors. Archives are handled elsewhere. */
4660 fdh = make_fdh (info, eh);
4663 fdh->elf.ref_regular = 1;
4668 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4669 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4670 if (entry_vis < descr_vis)
4671 fdh->elf.other += entry_vis - descr_vis;
4672 else if (entry_vis > descr_vis)
4673 eh->elf.other += descr_vis - entry_vis;
4675 if ((fdh->elf.root.type == bfd_link_hash_defined
4676 || fdh->elf.root.type == bfd_link_hash_defweak)
4677 && eh->elf.root.type == bfd_link_hash_undefined)
4679 eh->elf.root.type = bfd_link_hash_undefweak;
4680 eh->was_undefined = 1;
4681 htab->twiddled_syms = 1;
4688 /* Process list of dot-symbols we made in link_hash_newfunc. */
4691 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
4693 struct ppc_link_hash_table *htab;
4694 struct ppc_link_hash_entry **p, *eh;
4696 if (!is_ppc64_elf (info->output_bfd))
4698 htab = ppc_hash_table (info);
4702 if (is_ppc64_elf (ibfd))
4704 p = &htab->dot_syms;
4705 while ((eh = *p) != NULL)
4708 if (!add_symbol_adjust (eh, info))
4710 p = &eh->u.next_dot_sym;
4714 /* Clear the list for non-ppc64 input files. */
4715 p = &htab->dot_syms;
4716 while ((eh = *p) != NULL)
4719 p = &eh->u.next_dot_sym;
4722 /* We need to fix the undefs list for any syms we have twiddled to
4724 if (htab->twiddled_syms)
4726 bfd_link_repair_undef_list (&htab->elf.root);
4727 htab->twiddled_syms = 0;
4732 /* Undo hash table changes when an --as-needed input file is determined
4733 not to be needed. */
4736 ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4737 struct bfd_link_info *info)
4739 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4744 htab->dot_syms = NULL;
4748 static struct plt_entry **
4749 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4750 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4752 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4753 struct plt_entry **local_plt;
4754 unsigned char *local_got_tls_masks;
4756 if (local_got_ents == NULL)
4758 bfd_size_type size = symtab_hdr->sh_info;
4760 size *= (sizeof (*local_got_ents)
4761 + sizeof (*local_plt)
4762 + sizeof (*local_got_tls_masks));
4763 local_got_ents = bfd_zalloc (abfd, size);
4764 if (local_got_ents == NULL)
4766 elf_local_got_ents (abfd) = local_got_ents;
4769 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
4771 struct got_entry *ent;
4773 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4774 if (ent->addend == r_addend
4775 && ent->owner == abfd
4776 && ent->tls_type == tls_type)
4780 bfd_size_type amt = sizeof (*ent);
4781 ent = bfd_alloc (abfd, amt);
4784 ent->next = local_got_ents[r_symndx];
4785 ent->addend = r_addend;
4787 ent->tls_type = tls_type;
4788 ent->is_indirect = FALSE;
4789 ent->got.refcount = 0;
4790 local_got_ents[r_symndx] = ent;
4792 ent->got.refcount += 1;
4795 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4796 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4797 local_got_tls_masks[r_symndx] |= tls_type;
4799 return local_plt + r_symndx;
4803 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4805 struct plt_entry *ent;
4807 for (ent = *plist; ent != NULL; ent = ent->next)
4808 if (ent->addend == addend)
4812 bfd_size_type amt = sizeof (*ent);
4813 ent = bfd_alloc (abfd, amt);
4817 ent->addend = addend;
4818 ent->plt.refcount = 0;
4821 ent->plt.refcount += 1;
4826 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4828 return (r_type == R_PPC64_REL24
4829 || r_type == R_PPC64_REL14
4830 || r_type == R_PPC64_REL14_BRTAKEN
4831 || r_type == R_PPC64_REL14_BRNTAKEN
4832 || r_type == R_PPC64_ADDR24
4833 || r_type == R_PPC64_ADDR14
4834 || r_type == R_PPC64_ADDR14_BRTAKEN
4835 || r_type == R_PPC64_ADDR14_BRNTAKEN);
4838 /* Look through the relocs for a section during the first phase, and
4839 calculate needed space in the global offset table, procedure
4840 linkage table, and dynamic reloc sections. */
4843 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4844 asection *sec, const Elf_Internal_Rela *relocs)
4846 struct ppc_link_hash_table *htab;
4847 Elf_Internal_Shdr *symtab_hdr;
4848 struct elf_link_hash_entry **sym_hashes;
4849 const Elf_Internal_Rela *rel;
4850 const Elf_Internal_Rela *rel_end;
4852 asection **opd_sym_map;
4853 struct elf_link_hash_entry *tga, *dottga;
4855 if (info->relocatable)
4858 /* Don't do anything special with non-loaded, non-alloced sections.
4859 In particular, any relocs in such sections should not affect GOT
4860 and PLT reference counting (ie. we don't allow them to create GOT
4861 or PLT entries), there's no possibility or desire to optimize TLS
4862 relocs, and there's not much point in propagating relocs to shared
4863 libs that the dynamic linker won't relocate. */
4864 if ((sec->flags & SEC_ALLOC) == 0)
4867 BFD_ASSERT (is_ppc64_elf (abfd));
4869 htab = ppc_hash_table (info);
4873 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4874 FALSE, FALSE, TRUE);
4875 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4876 FALSE, FALSE, TRUE);
4877 symtab_hdr = &elf_symtab_hdr (abfd);
4878 sym_hashes = elf_sym_hashes (abfd);
4881 if (strcmp (sec->name, ".opd") == 0)
4883 /* Garbage collection needs some extra help with .opd sections.
4884 We don't want to necessarily keep everything referenced by
4885 relocs in .opd, as that would keep all functions. Instead,
4886 if we reference an .opd symbol (a function descriptor), we
4887 want to keep the function code symbol's section. This is
4888 easy for global symbols, but for local syms we need to keep
4889 information about the associated function section. */
4892 amt = sec->size * sizeof (*opd_sym_map) / 8;
4893 opd_sym_map = bfd_zalloc (abfd, amt);
4894 if (opd_sym_map == NULL)
4896 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
4897 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4898 ppc64_elf_section_data (sec)->sec_type = sec_opd;
4901 if (htab->sfpr == NULL
4902 && !create_linkage_sections (htab->elf.dynobj, info))
4905 rel_end = relocs + sec->reloc_count;
4906 for (rel = relocs; rel < rel_end; rel++)
4908 unsigned long r_symndx;
4909 struct elf_link_hash_entry *h;
4910 enum elf_ppc64_reloc_type r_type;
4912 struct _ppc64_elf_section_data *ppc64_sec;
4913 struct plt_entry **ifunc;
4915 r_symndx = ELF64_R_SYM (rel->r_info);
4916 if (r_symndx < symtab_hdr->sh_info)
4920 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4921 h = elf_follow_link (h);
4928 if (h->type == STT_GNU_IFUNC)
4931 ifunc = &h->plt.plist;
4936 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4941 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4943 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4944 rel->r_addend, PLT_IFUNC);
4949 r_type = ELF64_R_TYPE (rel->r_info);
4950 if (is_branch_reloc (r_type))
4952 if (h != NULL && (h == tga || h == dottga))
4955 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4956 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4957 /* We have a new-style __tls_get_addr call with a marker
4961 /* Mark this section as having an old-style call. */
4962 sec->has_tls_get_addr_call = 1;
4965 /* STT_GNU_IFUNC symbols must have a PLT entry. */
4967 && !update_plt_info (abfd, ifunc, rel->r_addend))
4975 /* These special tls relocs tie a call to __tls_get_addr with
4976 its parameter symbol. */
4979 case R_PPC64_GOT_TLSLD16:
4980 case R_PPC64_GOT_TLSLD16_LO:
4981 case R_PPC64_GOT_TLSLD16_HI:
4982 case R_PPC64_GOT_TLSLD16_HA:
4983 tls_type = TLS_TLS | TLS_LD;
4986 case R_PPC64_GOT_TLSGD16:
4987 case R_PPC64_GOT_TLSGD16_LO:
4988 case R_PPC64_GOT_TLSGD16_HI:
4989 case R_PPC64_GOT_TLSGD16_HA:
4990 tls_type = TLS_TLS | TLS_GD;
4993 case R_PPC64_GOT_TPREL16_DS:
4994 case R_PPC64_GOT_TPREL16_LO_DS:
4995 case R_PPC64_GOT_TPREL16_HI:
4996 case R_PPC64_GOT_TPREL16_HA:
4997 if (!info->executable)
4998 info->flags |= DF_STATIC_TLS;
4999 tls_type = TLS_TLS | TLS_TPREL;
5002 case R_PPC64_GOT_DTPREL16_DS:
5003 case R_PPC64_GOT_DTPREL16_LO_DS:
5004 case R_PPC64_GOT_DTPREL16_HI:
5005 case R_PPC64_GOT_DTPREL16_HA:
5006 tls_type = TLS_TLS | TLS_DTPREL;
5008 sec->has_tls_reloc = 1;
5012 case R_PPC64_GOT16_DS:
5013 case R_PPC64_GOT16_HA:
5014 case R_PPC64_GOT16_HI:
5015 case R_PPC64_GOT16_LO:
5016 case R_PPC64_GOT16_LO_DS:
5017 /* This symbol requires a global offset table entry. */
5018 sec->has_toc_reloc = 1;
5019 if (r_type == R_PPC64_GOT_TLSLD16
5020 || r_type == R_PPC64_GOT_TLSGD16
5021 || r_type == R_PPC64_GOT_TPREL16_DS
5022 || r_type == R_PPC64_GOT_DTPREL16_DS
5023 || r_type == R_PPC64_GOT16
5024 || r_type == R_PPC64_GOT16_DS)
5026 htab->do_multi_toc = 1;
5027 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5030 if (ppc64_elf_tdata (abfd)->got == NULL
5031 && !create_got_section (abfd, info))
5036 struct ppc_link_hash_entry *eh;
5037 struct got_entry *ent;
5039 eh = (struct ppc_link_hash_entry *) h;
5040 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5041 if (ent->addend == rel->r_addend
5042 && ent->owner == abfd
5043 && ent->tls_type == tls_type)
5047 bfd_size_type amt = sizeof (*ent);
5048 ent = bfd_alloc (abfd, amt);
5051 ent->next = eh->elf.got.glist;
5052 ent->addend = rel->r_addend;
5054 ent->tls_type = tls_type;
5055 ent->is_indirect = FALSE;
5056 ent->got.refcount = 0;
5057 eh->elf.got.glist = ent;
5059 ent->got.refcount += 1;
5060 eh->tls_mask |= tls_type;
5063 /* This is a global offset table entry for a local symbol. */
5064 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5065 rel->r_addend, tls_type))
5069 case R_PPC64_PLT16_HA:
5070 case R_PPC64_PLT16_HI:
5071 case R_PPC64_PLT16_LO:
5074 /* This symbol requires a procedure linkage table entry. We
5075 actually build the entry in adjust_dynamic_symbol,
5076 because this might be a case of linking PIC code without
5077 linking in any dynamic objects, in which case we don't
5078 need to generate a procedure linkage table after all. */
5081 /* It does not make sense to have a procedure linkage
5082 table entry for a local symbol. */
5083 bfd_set_error (bfd_error_bad_value);
5088 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5091 if (h->root.root.string[0] == '.'
5092 && h->root.root.string[1] != '\0')
5093 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5097 /* The following relocations don't need to propagate the
5098 relocation if linking a shared object since they are
5099 section relative. */
5100 case R_PPC64_SECTOFF:
5101 case R_PPC64_SECTOFF_LO:
5102 case R_PPC64_SECTOFF_HI:
5103 case R_PPC64_SECTOFF_HA:
5104 case R_PPC64_SECTOFF_DS:
5105 case R_PPC64_SECTOFF_LO_DS:
5106 case R_PPC64_DTPREL16:
5107 case R_PPC64_DTPREL16_LO:
5108 case R_PPC64_DTPREL16_HI:
5109 case R_PPC64_DTPREL16_HA:
5110 case R_PPC64_DTPREL16_DS:
5111 case R_PPC64_DTPREL16_LO_DS:
5112 case R_PPC64_DTPREL16_HIGHER:
5113 case R_PPC64_DTPREL16_HIGHERA:
5114 case R_PPC64_DTPREL16_HIGHEST:
5115 case R_PPC64_DTPREL16_HIGHESTA:
5120 case R_PPC64_REL16_LO:
5121 case R_PPC64_REL16_HI:
5122 case R_PPC64_REL16_HA:
5126 case R_PPC64_TOC16_DS:
5127 htab->do_multi_toc = 1;
5128 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5129 case R_PPC64_TOC16_LO:
5130 case R_PPC64_TOC16_HI:
5131 case R_PPC64_TOC16_HA:
5132 case R_PPC64_TOC16_LO_DS:
5133 sec->has_toc_reloc = 1;
5136 /* This relocation describes the C++ object vtable hierarchy.
5137 Reconstruct it for later use during GC. */
5138 case R_PPC64_GNU_VTINHERIT:
5139 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5143 /* This relocation describes which C++ vtable entries are actually
5144 used. Record for later use during GC. */
5145 case R_PPC64_GNU_VTENTRY:
5146 BFD_ASSERT (h != NULL);
5148 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5153 case R_PPC64_REL14_BRTAKEN:
5154 case R_PPC64_REL14_BRNTAKEN:
5156 asection *dest = NULL;
5158 /* Heuristic: If jumping outside our section, chances are
5159 we are going to need a stub. */
5162 /* If the sym is weak it may be overridden later, so
5163 don't assume we know where a weak sym lives. */
5164 if (h->root.type == bfd_link_hash_defined)
5165 dest = h->root.u.def.section;
5169 Elf_Internal_Sym *isym;
5171 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5176 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5180 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5185 if (h != NULL && ifunc == NULL)
5187 /* We may need a .plt entry if the function this reloc
5188 refers to is in a shared lib. */
5189 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5192 if (h->root.root.string[0] == '.'
5193 && h->root.root.string[1] != '\0')
5194 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5195 if (h == tga || h == dottga)
5196 sec->has_tls_reloc = 1;
5200 case R_PPC64_TPREL64:
5201 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5202 if (!info->executable)
5203 info->flags |= DF_STATIC_TLS;
5206 case R_PPC64_DTPMOD64:
5207 if (rel + 1 < rel_end
5208 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5209 && rel[1].r_offset == rel->r_offset + 8)
5210 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5212 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5215 case R_PPC64_DTPREL64:
5216 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5218 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5219 && rel[-1].r_offset == rel->r_offset - 8)
5220 /* This is the second reloc of a dtpmod, dtprel pair.
5221 Don't mark with TLS_DTPREL. */
5225 sec->has_tls_reloc = 1;
5228 struct ppc_link_hash_entry *eh;
5229 eh = (struct ppc_link_hash_entry *) h;
5230 eh->tls_mask |= tls_type;
5233 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5234 rel->r_addend, tls_type))
5237 ppc64_sec = ppc64_elf_section_data (sec);
5238 if (ppc64_sec->sec_type != sec_toc)
5242 /* One extra to simplify get_tls_mask. */
5243 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5244 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5245 if (ppc64_sec->u.toc.symndx == NULL)
5247 amt = sec->size * sizeof (bfd_vma) / 8;
5248 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5249 if (ppc64_sec->u.toc.add == NULL)
5251 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5252 ppc64_sec->sec_type = sec_toc;
5254 BFD_ASSERT (rel->r_offset % 8 == 0);
5255 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5256 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5258 /* Mark the second slot of a GD or LD entry.
5259 -1 to indicate GD and -2 to indicate LD. */
5260 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5261 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5262 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5263 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5266 case R_PPC64_TPREL16:
5267 case R_PPC64_TPREL16_LO:
5268 case R_PPC64_TPREL16_HI:
5269 case R_PPC64_TPREL16_HA:
5270 case R_PPC64_TPREL16_DS:
5271 case R_PPC64_TPREL16_LO_DS:
5272 case R_PPC64_TPREL16_HIGHER:
5273 case R_PPC64_TPREL16_HIGHERA:
5274 case R_PPC64_TPREL16_HIGHEST:
5275 case R_PPC64_TPREL16_HIGHESTA:
5278 if (!info->executable)
5279 info->flags |= DF_STATIC_TLS;
5284 case R_PPC64_ADDR64:
5285 if (opd_sym_map != NULL
5286 && rel + 1 < rel_end
5287 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5291 if (h->root.root.string[0] == '.'
5292 && h->root.root.string[1] != 0
5293 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5296 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5301 Elf_Internal_Sym *isym;
5303 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5308 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5309 if (s != NULL && s != sec)
5310 opd_sym_map[rel->r_offset / 8] = s;
5318 case R_PPC64_ADDR14:
5319 case R_PPC64_ADDR14_BRNTAKEN:
5320 case R_PPC64_ADDR14_BRTAKEN:
5321 case R_PPC64_ADDR16:
5322 case R_PPC64_ADDR16_DS:
5323 case R_PPC64_ADDR16_HA:
5324 case R_PPC64_ADDR16_HI:
5325 case R_PPC64_ADDR16_HIGHER:
5326 case R_PPC64_ADDR16_HIGHERA:
5327 case R_PPC64_ADDR16_HIGHEST:
5328 case R_PPC64_ADDR16_HIGHESTA:
5329 case R_PPC64_ADDR16_LO:
5330 case R_PPC64_ADDR16_LO_DS:
5331 case R_PPC64_ADDR24:
5332 case R_PPC64_ADDR32:
5333 case R_PPC64_UADDR16:
5334 case R_PPC64_UADDR32:
5335 case R_PPC64_UADDR64:
5337 if (h != NULL && !info->shared)
5338 /* We may need a copy reloc. */
5341 /* Don't propagate .opd relocs. */
5342 if (NO_OPD_RELOCS && opd_sym_map != NULL)
5345 /* If we are creating a shared library, and this is a reloc
5346 against a global symbol, or a non PC relative reloc
5347 against a local symbol, then we need to copy the reloc
5348 into the shared library. However, if we are linking with
5349 -Bsymbolic, we do not need to copy a reloc against a
5350 global symbol which is defined in an object we are
5351 including in the link (i.e., DEF_REGULAR is set). At
5352 this point we have not seen all the input files, so it is
5353 possible that DEF_REGULAR is not set now but will be set
5354 later (it is never cleared). In case of a weak definition,
5355 DEF_REGULAR may be cleared later by a strong definition in
5356 a shared library. We account for that possibility below by
5357 storing information in the dyn_relocs field of the hash
5358 table entry. A similar situation occurs when creating
5359 shared libraries and symbol visibility changes render the
5362 If on the other hand, we are creating an executable, we
5363 may need to keep relocations for symbols satisfied by a
5364 dynamic library if we manage to avoid copy relocs for the
5368 && (must_be_dyn_reloc (info, r_type)
5370 && (! info->symbolic
5371 || h->root.type == bfd_link_hash_defweak
5372 || !h->def_regular))))
5373 || (ELIMINATE_COPY_RELOCS
5376 && (h->root.type == bfd_link_hash_defweak
5377 || !h->def_regular))
5381 struct ppc_dyn_relocs *p;
5382 struct ppc_dyn_relocs **head;
5384 /* We must copy these reloc types into the output file.
5385 Create a reloc section in dynobj and make room for
5389 sreloc = _bfd_elf_make_dynamic_reloc_section
5390 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5396 /* If this is a global symbol, we count the number of
5397 relocations we need for this symbol. */
5400 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5404 /* Track dynamic relocs needed for local syms too.
5405 We really need local syms available to do this
5409 Elf_Internal_Sym *isym;
5411 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5416 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5420 vpp = &elf_section_data (s)->local_dynrel;
5421 head = (struct ppc_dyn_relocs **) vpp;
5425 if (p == NULL || p->sec != sec)
5427 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5438 if (!must_be_dyn_reloc (info, r_type))
5451 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5452 of the code entry point, and its section. */
5455 opd_entry_value (asection *opd_sec,
5457 asection **code_sec,
5460 bfd *opd_bfd = opd_sec->owner;
5461 Elf_Internal_Rela *relocs;
5462 Elf_Internal_Rela *lo, *hi, *look;
5465 /* No relocs implies we are linking a --just-symbols object. */
5466 if (opd_sec->reloc_count == 0)
5468 if (!bfd_get_section_contents (opd_bfd, opd_sec, &val, offset, 8))
5469 return (bfd_vma) -1;
5471 if (code_sec != NULL)
5473 asection *sec, *likely = NULL;
5474 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5476 && (sec->flags & SEC_LOAD) != 0
5477 && (sec->flags & SEC_ALLOC) != 0)
5482 if (code_off != NULL)
5483 *code_off = val - likely->vma;
5489 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5491 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
5493 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5495 /* Go find the opd reloc at the sym address. */
5497 BFD_ASSERT (lo != NULL);
5498 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5502 look = lo + (hi - lo) / 2;
5503 if (look->r_offset < offset)
5505 else if (look->r_offset > offset)
5509 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5511 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5512 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5514 unsigned long symndx = ELF64_R_SYM (look->r_info);
5517 if (symndx < symtab_hdr->sh_info)
5519 Elf_Internal_Sym *sym;
5521 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5524 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5525 symtab_hdr->sh_info,
5526 0, NULL, NULL, NULL);
5529 symtab_hdr->contents = (bfd_byte *) sym;
5533 val = sym->st_value;
5534 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5535 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5539 struct elf_link_hash_entry **sym_hashes;
5540 struct elf_link_hash_entry *rh;
5542 sym_hashes = elf_sym_hashes (opd_bfd);
5543 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5544 rh = elf_follow_link (rh);
5545 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5546 || rh->root.type == bfd_link_hash_defweak);
5547 val = rh->root.u.def.value;
5548 sec = rh->root.u.def.section;
5550 val += look->r_addend;
5551 if (code_off != NULL)
5553 if (code_sec != NULL)
5555 if (sec != NULL && sec->output_section != NULL)
5556 val += sec->output_section->vma + sec->output_offset;
5565 /* Return true if symbol is defined in a regular object file. */
5568 is_static_defined (struct elf_link_hash_entry *h)
5570 return ((h->root.type == bfd_link_hash_defined
5571 || h->root.type == bfd_link_hash_defweak)
5572 && h->root.u.def.section != NULL
5573 && h->root.u.def.section->output_section != NULL);
5576 /* If FDH is a function descriptor symbol, return the associated code
5577 entry symbol if it is defined. Return NULL otherwise. */
5579 static struct ppc_link_hash_entry *
5580 defined_code_entry (struct ppc_link_hash_entry *fdh)
5582 if (fdh->is_func_descriptor)
5584 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5585 if (fh->elf.root.type == bfd_link_hash_defined
5586 || fh->elf.root.type == bfd_link_hash_defweak)
5592 /* If FH is a function code entry symbol, return the associated
5593 function descriptor symbol if it is defined. Return NULL otherwise. */
5595 static struct ppc_link_hash_entry *
5596 defined_func_desc (struct ppc_link_hash_entry *fh)
5599 && fh->oh->is_func_descriptor)
5601 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5602 if (fdh->elf.root.type == bfd_link_hash_defined
5603 || fdh->elf.root.type == bfd_link_hash_defweak)
5609 /* Mark all our entry sym sections, both opd and code section. */
5612 ppc64_elf_gc_keep (struct bfd_link_info *info)
5614 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5615 struct bfd_sym_chain *sym;
5620 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5622 struct ppc_link_hash_entry *eh, *fh;
5625 eh = (struct ppc_link_hash_entry *)
5626 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5629 if (eh->elf.root.type != bfd_link_hash_defined
5630 && eh->elf.root.type != bfd_link_hash_defweak)
5633 fh = defined_code_entry (eh);
5636 sec = fh->elf.root.u.def.section;
5637 sec->flags |= SEC_KEEP;
5639 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5640 && opd_entry_value (eh->elf.root.u.def.section,
5641 eh->elf.root.u.def.value,
5642 &sec, NULL) != (bfd_vma) -1)
5643 sec->flags |= SEC_KEEP;
5645 sec = eh->elf.root.u.def.section;
5646 sec->flags |= SEC_KEEP;
5650 /* Mark sections containing dynamically referenced symbols. When
5651 building shared libraries, we must assume that any visible symbol is
5655 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5657 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5658 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5659 struct ppc_link_hash_entry *fdh;
5661 if (eh->elf.root.type == bfd_link_hash_warning)
5662 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5664 /* Dynamic linking info is on the func descriptor sym. */
5665 fdh = defined_func_desc (eh);
5669 if ((eh->elf.root.type == bfd_link_hash_defined
5670 || eh->elf.root.type == bfd_link_hash_defweak)
5671 && (eh->elf.ref_dynamic
5672 || (!info->executable
5673 && eh->elf.def_regular
5674 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5675 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
5678 struct ppc_link_hash_entry *fh;
5680 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5682 /* Function descriptor syms cause the associated
5683 function code sym section to be marked. */
5684 fh = defined_code_entry (eh);
5687 code_sec = fh->elf.root.u.def.section;
5688 code_sec->flags |= SEC_KEEP;
5690 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5691 && opd_entry_value (eh->elf.root.u.def.section,
5692 eh->elf.root.u.def.value,
5693 &code_sec, NULL) != (bfd_vma) -1)
5694 code_sec->flags |= SEC_KEEP;
5700 /* Return the section that should be marked against GC for a given
5704 ppc64_elf_gc_mark_hook (asection *sec,
5705 struct bfd_link_info *info,
5706 Elf_Internal_Rela *rel,
5707 struct elf_link_hash_entry *h,
5708 Elf_Internal_Sym *sym)
5712 /* Syms return NULL if we're marking .opd, so we avoid marking all
5713 function sections, as all functions are referenced in .opd. */
5715 if (get_opd_info (sec) != NULL)
5720 enum elf_ppc64_reloc_type r_type;
5721 struct ppc_link_hash_entry *eh, *fh, *fdh;
5723 r_type = ELF64_R_TYPE (rel->r_info);
5726 case R_PPC64_GNU_VTINHERIT:
5727 case R_PPC64_GNU_VTENTRY:
5731 switch (h->root.type)
5733 case bfd_link_hash_defined:
5734 case bfd_link_hash_defweak:
5735 eh = (struct ppc_link_hash_entry *) h;
5736 fdh = defined_func_desc (eh);
5740 /* Function descriptor syms cause the associated
5741 function code sym section to be marked. */
5742 fh = defined_code_entry (eh);
5745 /* They also mark their opd section. */
5746 eh->elf.root.u.def.section->gc_mark = 1;
5748 rsec = fh->elf.root.u.def.section;
5750 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5751 && opd_entry_value (eh->elf.root.u.def.section,
5752 eh->elf.root.u.def.value,
5753 &rsec, NULL) != (bfd_vma) -1)
5754 eh->elf.root.u.def.section->gc_mark = 1;
5756 rsec = h->root.u.def.section;
5759 case bfd_link_hash_common:
5760 rsec = h->root.u.c.p->section;
5764 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5770 struct _opd_sec_data *opd;
5772 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5773 opd = get_opd_info (rsec);
5774 if (opd != NULL && opd->func_sec != NULL)
5778 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
5785 /* Update the .got, .plt. and dynamic reloc reference counts for the
5786 section being removed. */
5789 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5790 asection *sec, const Elf_Internal_Rela *relocs)
5792 struct ppc_link_hash_table *htab;
5793 Elf_Internal_Shdr *symtab_hdr;
5794 struct elf_link_hash_entry **sym_hashes;
5795 struct got_entry **local_got_ents;
5796 const Elf_Internal_Rela *rel, *relend;
5798 if (info->relocatable)
5801 if ((sec->flags & SEC_ALLOC) == 0)
5804 elf_section_data (sec)->local_dynrel = NULL;
5806 htab = ppc_hash_table (info);
5810 symtab_hdr = &elf_symtab_hdr (abfd);
5811 sym_hashes = elf_sym_hashes (abfd);
5812 local_got_ents = elf_local_got_ents (abfd);
5814 relend = relocs + sec->reloc_count;
5815 for (rel = relocs; rel < relend; rel++)
5817 unsigned long r_symndx;
5818 enum elf_ppc64_reloc_type r_type;
5819 struct elf_link_hash_entry *h = NULL;
5820 unsigned char tls_type = 0;
5822 r_symndx = ELF64_R_SYM (rel->r_info);
5823 r_type = ELF64_R_TYPE (rel->r_info);
5824 if (r_symndx >= symtab_hdr->sh_info)
5826 struct ppc_link_hash_entry *eh;
5827 struct ppc_dyn_relocs **pp;
5828 struct ppc_dyn_relocs *p;
5830 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5831 h = elf_follow_link (h);
5832 eh = (struct ppc_link_hash_entry *) h;
5834 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5837 /* Everything must go for SEC. */
5843 if (is_branch_reloc (r_type))
5845 struct plt_entry **ifunc = NULL;
5848 if (h->type == STT_GNU_IFUNC)
5849 ifunc = &h->plt.plist;
5851 else if (local_got_ents != NULL)
5853 struct plt_entry **local_plt = (struct plt_entry **)
5854 (local_got_ents + symtab_hdr->sh_info);
5855 unsigned char *local_got_tls_masks = (unsigned char *)
5856 (local_plt + symtab_hdr->sh_info);
5857 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
5858 ifunc = local_plt + r_symndx;
5862 struct plt_entry *ent;
5864 for (ent = *ifunc; ent != NULL; ent = ent->next)
5865 if (ent->addend == rel->r_addend)
5869 if (ent->plt.refcount > 0)
5870 ent->plt.refcount -= 1;
5877 case R_PPC64_GOT_TLSLD16:
5878 case R_PPC64_GOT_TLSLD16_LO:
5879 case R_PPC64_GOT_TLSLD16_HI:
5880 case R_PPC64_GOT_TLSLD16_HA:
5881 tls_type = TLS_TLS | TLS_LD;
5884 case R_PPC64_GOT_TLSGD16:
5885 case R_PPC64_GOT_TLSGD16_LO:
5886 case R_PPC64_GOT_TLSGD16_HI:
5887 case R_PPC64_GOT_TLSGD16_HA:
5888 tls_type = TLS_TLS | TLS_GD;
5891 case R_PPC64_GOT_TPREL16_DS:
5892 case R_PPC64_GOT_TPREL16_LO_DS:
5893 case R_PPC64_GOT_TPREL16_HI:
5894 case R_PPC64_GOT_TPREL16_HA:
5895 tls_type = TLS_TLS | TLS_TPREL;
5898 case R_PPC64_GOT_DTPREL16_DS:
5899 case R_PPC64_GOT_DTPREL16_LO_DS:
5900 case R_PPC64_GOT_DTPREL16_HI:
5901 case R_PPC64_GOT_DTPREL16_HA:
5902 tls_type = TLS_TLS | TLS_DTPREL;
5906 case R_PPC64_GOT16_DS:
5907 case R_PPC64_GOT16_HA:
5908 case R_PPC64_GOT16_HI:
5909 case R_PPC64_GOT16_LO:
5910 case R_PPC64_GOT16_LO_DS:
5913 struct got_entry *ent;
5918 ent = local_got_ents[r_symndx];
5920 for (; ent != NULL; ent = ent->next)
5921 if (ent->addend == rel->r_addend
5922 && ent->owner == abfd
5923 && ent->tls_type == tls_type)
5927 if (ent->got.refcount > 0)
5928 ent->got.refcount -= 1;
5932 case R_PPC64_PLT16_HA:
5933 case R_PPC64_PLT16_HI:
5934 case R_PPC64_PLT16_LO:
5938 case R_PPC64_REL14_BRNTAKEN:
5939 case R_PPC64_REL14_BRTAKEN:
5943 struct plt_entry *ent;
5945 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5946 if (ent->addend == rel->r_addend)
5948 if (ent != NULL && ent->plt.refcount > 0)
5949 ent->plt.refcount -= 1;
5960 /* The maximum size of .sfpr. */
5961 #define SFPR_MAX (218*4)
5963 struct sfpr_def_parms
5965 const char name[12];
5966 unsigned char lo, hi;
5967 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5968 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5971 /* Auto-generate _save*, _rest* functions in .sfpr. */
5974 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5976 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5978 size_t len = strlen (parm->name);
5979 bfd_boolean writing = FALSE;
5985 memcpy (sym, parm->name, len);
5988 for (i = parm->lo; i <= parm->hi; i++)
5990 struct elf_link_hash_entry *h;
5992 sym[len + 0] = i / 10 + '0';
5993 sym[len + 1] = i % 10 + '0';
5994 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
5998 h->root.type = bfd_link_hash_defined;
5999 h->root.u.def.section = htab->sfpr;
6000 h->root.u.def.value = htab->sfpr->size;
6003 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6005 if (htab->sfpr->contents == NULL)
6007 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6008 if (htab->sfpr->contents == NULL)
6014 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6016 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6018 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6019 htab->sfpr->size = p - htab->sfpr->contents;
6027 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6029 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6034 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6036 p = savegpr0 (abfd, p, r);
6037 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6039 bfd_put_32 (abfd, BLR, p);
6044 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6046 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6051 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6053 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6055 p = restgpr0 (abfd, p, r);
6056 bfd_put_32 (abfd, MTLR_R0, p);
6060 p = restgpr0 (abfd, p, 30);
6061 p = restgpr0 (abfd, p, 31);
6063 bfd_put_32 (abfd, BLR, p);
6068 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6070 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6075 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6077 p = savegpr1 (abfd, p, r);
6078 bfd_put_32 (abfd, BLR, p);
6083 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6085 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6090 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6092 p = restgpr1 (abfd, p, r);
6093 bfd_put_32 (abfd, BLR, p);
6098 savefpr (bfd *abfd, bfd_byte *p, int r)
6100 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6105 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6107 p = savefpr (abfd, p, r);
6108 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6110 bfd_put_32 (abfd, BLR, p);
6115 restfpr (bfd *abfd, bfd_byte *p, int r)
6117 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6122 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6124 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6126 p = restfpr (abfd, p, r);
6127 bfd_put_32 (abfd, MTLR_R0, p);
6131 p = restfpr (abfd, p, 30);
6132 p = restfpr (abfd, p, 31);
6134 bfd_put_32 (abfd, BLR, p);
6139 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6141 p = savefpr (abfd, p, r);
6142 bfd_put_32 (abfd, BLR, p);
6147 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6149 p = restfpr (abfd, p, r);
6150 bfd_put_32 (abfd, BLR, p);
6155 savevr (bfd *abfd, bfd_byte *p, int r)
6157 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6159 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6164 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6166 p = savevr (abfd, p, r);
6167 bfd_put_32 (abfd, BLR, p);
6172 restvr (bfd *abfd, bfd_byte *p, int r)
6174 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6176 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6181 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6183 p = restvr (abfd, p, r);
6184 bfd_put_32 (abfd, BLR, p);
6188 /* Called via elf_link_hash_traverse to transfer dynamic linking
6189 information on function code symbol entries to their corresponding
6190 function descriptor symbol entries. */
6193 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6195 struct bfd_link_info *info;
6196 struct ppc_link_hash_table *htab;
6197 struct plt_entry *ent;
6198 struct ppc_link_hash_entry *fh;
6199 struct ppc_link_hash_entry *fdh;
6200 bfd_boolean force_local;
6202 fh = (struct ppc_link_hash_entry *) h;
6203 if (fh->elf.root.type == bfd_link_hash_indirect)
6206 if (fh->elf.root.type == bfd_link_hash_warning)
6207 fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
6210 htab = ppc_hash_table (info);
6214 /* Resolve undefined references to dot-symbols as the value
6215 in the function descriptor, if we have one in a regular object.
6216 This is to satisfy cases like ".quad .foo". Calls to functions
6217 in dynamic objects are handled elsewhere. */
6218 if (fh->elf.root.type == bfd_link_hash_undefweak
6219 && fh->was_undefined
6220 && (fdh = defined_func_desc (fh)) != NULL
6221 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6222 && opd_entry_value (fdh->elf.root.u.def.section,
6223 fdh->elf.root.u.def.value,
6224 &fh->elf.root.u.def.section,
6225 &fh->elf.root.u.def.value) != (bfd_vma) -1)
6227 fh->elf.root.type = fdh->elf.root.type;
6228 fh->elf.forced_local = 1;
6229 fh->elf.def_regular = fdh->elf.def_regular;
6230 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6233 /* If this is a function code symbol, transfer dynamic linking
6234 information to the function descriptor symbol. */
6238 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6239 if (ent->plt.refcount > 0)
6242 || fh->elf.root.root.string[0] != '.'
6243 || fh->elf.root.root.string[1] == '\0')
6246 /* Find the corresponding function descriptor symbol. Create it
6247 as undefined if necessary. */
6249 fdh = lookup_fdh (fh, htab);
6251 && !info->executable
6252 && (fh->elf.root.type == bfd_link_hash_undefined
6253 || fh->elf.root.type == bfd_link_hash_undefweak))
6255 fdh = make_fdh (info, fh);
6260 /* Fake function descriptors are made undefweak. If the function
6261 code symbol is strong undefined, make the fake sym the same.
6262 If the function code symbol is defined, then force the fake
6263 descriptor local; We can't support overriding of symbols in a
6264 shared library on a fake descriptor. */
6268 && fdh->elf.root.type == bfd_link_hash_undefweak)
6270 if (fh->elf.root.type == bfd_link_hash_undefined)
6272 fdh->elf.root.type = bfd_link_hash_undefined;
6273 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6275 else if (fh->elf.root.type == bfd_link_hash_defined
6276 || fh->elf.root.type == bfd_link_hash_defweak)
6278 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6283 && !fdh->elf.forced_local
6284 && (!info->executable
6285 || fdh->elf.def_dynamic
6286 || fdh->elf.ref_dynamic
6287 || (fdh->elf.root.type == bfd_link_hash_undefweak
6288 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6290 if (fdh->elf.dynindx == -1)
6291 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6293 fdh->elf.ref_regular |= fh->elf.ref_regular;
6294 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6295 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6296 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6297 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6299 move_plt_plist (fh, fdh);
6300 fdh->elf.needs_plt = 1;
6302 fdh->is_func_descriptor = 1;
6307 /* Now that the info is on the function descriptor, clear the
6308 function code sym info. Any function code syms for which we
6309 don't have a definition in a regular file, we force local.
6310 This prevents a shared library from exporting syms that have
6311 been imported from another library. Function code syms that
6312 are really in the library we must leave global to prevent the
6313 linker dragging in a definition from a static library. */
6314 force_local = (!fh->elf.def_regular
6316 || !fdh->elf.def_regular
6317 || fdh->elf.forced_local);
6318 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6323 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6324 this hook to a) provide some gcc support functions, and b) transfer
6325 dynamic linking information gathered so far on function code symbol
6326 entries, to their corresponding function descriptor symbol entries. */
6329 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6330 struct bfd_link_info *info)
6332 struct ppc_link_hash_table *htab;
6334 const struct sfpr_def_parms funcs[] =
6336 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6337 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6338 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6339 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6340 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6341 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6342 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6343 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6344 { "._savef", 14, 31, savefpr, savefpr1_tail },
6345 { "._restf", 14, 31, restfpr, restfpr1_tail },
6346 { "_savevr_", 20, 31, savevr, savevr_tail },
6347 { "_restvr_", 20, 31, restvr, restvr_tail }
6350 htab = ppc_hash_table (info);
6354 if (htab->sfpr == NULL)
6355 /* We don't have any relocs. */
6358 /* Provide any missing _save* and _rest* functions. */
6359 htab->sfpr->size = 0;
6360 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6361 if (!sfpr_define (info, &funcs[i]))
6364 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6366 if (htab->sfpr->size == 0)
6367 htab->sfpr->flags |= SEC_EXCLUDE;
6372 /* Adjust a symbol defined by a dynamic object and referenced by a
6373 regular object. The current definition is in some section of the
6374 dynamic object, but we're not including those sections. We have to
6375 change the definition to something the rest of the link can
6379 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6380 struct elf_link_hash_entry *h)
6382 struct ppc_link_hash_table *htab;
6385 htab = ppc_hash_table (info);
6389 /* Deal with function syms. */
6390 if (h->type == STT_FUNC
6391 || h->type == STT_GNU_IFUNC
6394 /* Clear procedure linkage table information for any symbol that
6395 won't need a .plt entry. */
6396 struct plt_entry *ent;
6397 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6398 if (ent->plt.refcount > 0)
6401 || (h->type != STT_GNU_IFUNC
6402 && (SYMBOL_CALLS_LOCAL (info, h)
6403 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6404 && h->root.type == bfd_link_hash_undefweak))))
6406 h->plt.plist = NULL;
6411 h->plt.plist = NULL;
6413 /* If this is a weak symbol, and there is a real definition, the
6414 processor independent code will have arranged for us to see the
6415 real definition first, and we can just use the same value. */
6416 if (h->u.weakdef != NULL)
6418 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6419 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6420 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6421 h->root.u.def.value = h->u.weakdef->root.u.def.value;
6422 if (ELIMINATE_COPY_RELOCS)
6423 h->non_got_ref = h->u.weakdef->non_got_ref;
6427 /* If we are creating a shared library, we must presume that the
6428 only references to the symbol are via the global offset table.
6429 For such cases we need not do anything here; the relocations will
6430 be handled correctly by relocate_section. */
6434 /* If there are no references to this symbol that do not use the
6435 GOT, we don't need to generate a copy reloc. */
6436 if (!h->non_got_ref)
6439 /* Don't generate a copy reloc for symbols defined in the executable. */
6440 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6443 if (ELIMINATE_COPY_RELOCS)
6445 struct ppc_link_hash_entry * eh;
6446 struct ppc_dyn_relocs *p;
6448 eh = (struct ppc_link_hash_entry *) h;
6449 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6451 s = p->sec->output_section;
6452 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6456 /* If we didn't find any dynamic relocs in read-only sections, then
6457 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6465 if (h->plt.plist != NULL)
6467 /* We should never get here, but unfortunately there are versions
6468 of gcc out there that improperly (for this ABI) put initialized
6469 function pointers, vtable refs and suchlike in read-only
6470 sections. Allow them to proceed, but warn that this might
6471 break at runtime. */
6472 (*_bfd_error_handler)
6473 (_("copy reloc against `%s' requires lazy plt linking; "
6474 "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
6475 h->root.root.string);
6478 /* This is a reference to a symbol defined by a dynamic object which
6479 is not a function. */
6483 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
6484 h->root.root.string);
6488 /* We must allocate the symbol in our .dynbss section, which will
6489 become part of the .bss section of the executable. There will be
6490 an entry for this symbol in the .dynsym section. The dynamic
6491 object will contain position independent code, so all references
6492 from the dynamic object to this symbol will go through the global
6493 offset table. The dynamic linker will use the .dynsym entry to
6494 determine the address it must put in the global offset table, so
6495 both the dynamic object and the regular object will refer to the
6496 same memory location for the variable. */
6498 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6499 to copy the initial value out of the dynamic object and into the
6500 runtime process image. We need to remember the offset into the
6501 .rela.bss section we are going to use. */
6502 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6504 htab->relbss->size += sizeof (Elf64_External_Rela);
6510 return _bfd_elf_adjust_dynamic_copy (h, s);
6513 /* If given a function descriptor symbol, hide both the function code
6514 sym and the descriptor. */
6516 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6517 struct elf_link_hash_entry *h,
6518 bfd_boolean force_local)
6520 struct ppc_link_hash_entry *eh;
6521 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6523 eh = (struct ppc_link_hash_entry *) h;
6524 if (eh->is_func_descriptor)
6526 struct ppc_link_hash_entry *fh = eh->oh;
6531 struct ppc_link_hash_table *htab;
6534 /* We aren't supposed to use alloca in BFD because on
6535 systems which do not have alloca the version in libiberty
6536 calls xmalloc, which might cause the program to crash
6537 when it runs out of memory. This function doesn't have a
6538 return status, so there's no way to gracefully return an
6539 error. So cheat. We know that string[-1] can be safely
6540 accessed; It's either a string in an ELF string table,
6541 or allocated in an objalloc structure. */
6543 p = eh->elf.root.root.string - 1;
6546 htab = ppc_hash_table (info);
6550 fh = (struct ppc_link_hash_entry *)
6551 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6554 /* Unfortunately, if it so happens that the string we were
6555 looking for was allocated immediately before this string,
6556 then we overwrote the string terminator. That's the only
6557 reason the lookup should fail. */
6560 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6561 while (q >= eh->elf.root.root.string && *q == *p)
6563 if (q < eh->elf.root.root.string && *p == '.')
6564 fh = (struct ppc_link_hash_entry *)
6565 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6574 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6579 get_sym_h (struct elf_link_hash_entry **hp,
6580 Elf_Internal_Sym **symp,
6582 unsigned char **tls_maskp,
6583 Elf_Internal_Sym **locsymsp,
6584 unsigned long r_symndx,
6587 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6589 if (r_symndx >= symtab_hdr->sh_info)
6591 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6592 struct elf_link_hash_entry *h;
6594 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6595 h = elf_follow_link (h);
6603 if (symsecp != NULL)
6605 asection *symsec = NULL;
6606 if (h->root.type == bfd_link_hash_defined
6607 || h->root.type == bfd_link_hash_defweak)
6608 symsec = h->root.u.def.section;
6612 if (tls_maskp != NULL)
6614 struct ppc_link_hash_entry *eh;
6616 eh = (struct ppc_link_hash_entry *) h;
6617 *tls_maskp = &eh->tls_mask;
6622 Elf_Internal_Sym *sym;
6623 Elf_Internal_Sym *locsyms = *locsymsp;
6625 if (locsyms == NULL)
6627 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6628 if (locsyms == NULL)
6629 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6630 symtab_hdr->sh_info,
6631 0, NULL, NULL, NULL);
6632 if (locsyms == NULL)
6634 *locsymsp = locsyms;
6636 sym = locsyms + r_symndx;
6644 if (symsecp != NULL)
6645 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6647 if (tls_maskp != NULL)
6649 struct got_entry **lgot_ents;
6650 unsigned char *tls_mask;
6653 lgot_ents = elf_local_got_ents (ibfd);
6654 if (lgot_ents != NULL)
6656 struct plt_entry **local_plt = (struct plt_entry **)
6657 (lgot_ents + symtab_hdr->sh_info);
6658 unsigned char *lgot_masks = (unsigned char *)
6659 (local_plt + symtab_hdr->sh_info);
6660 tls_mask = &lgot_masks[r_symndx];
6662 *tls_maskp = tls_mask;
6668 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6669 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6670 type suitable for optimization, and 1 otherwise. */
6673 get_tls_mask (unsigned char **tls_maskp,
6674 unsigned long *toc_symndx,
6675 bfd_vma *toc_addend,
6676 Elf_Internal_Sym **locsymsp,
6677 const Elf_Internal_Rela *rel,
6680 unsigned long r_symndx;
6682 struct elf_link_hash_entry *h;
6683 Elf_Internal_Sym *sym;
6687 r_symndx = ELF64_R_SYM (rel->r_info);
6688 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6691 if ((*tls_maskp != NULL && **tls_maskp != 0)
6693 || ppc64_elf_section_data (sec) == NULL
6694 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6697 /* Look inside a TOC section too. */
6700 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6701 off = h->root.u.def.value;
6704 off = sym->st_value;
6705 off += rel->r_addend;
6706 BFD_ASSERT (off % 8 == 0);
6707 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6708 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6709 if (toc_symndx != NULL)
6710 *toc_symndx = r_symndx;
6711 if (toc_addend != NULL)
6712 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6713 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6715 if ((h == NULL || is_static_defined (h))
6716 && (next_r == -1 || next_r == -2))
6721 /* Adjust all global syms defined in opd sections. In gcc generated
6722 code for the old ABI, these will already have been done. */
6725 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6727 struct ppc_link_hash_entry *eh;
6729 struct _opd_sec_data *opd;
6731 if (h->root.type == bfd_link_hash_indirect)
6734 if (h->root.type == bfd_link_hash_warning)
6735 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6737 if (h->root.type != bfd_link_hash_defined
6738 && h->root.type != bfd_link_hash_defweak)
6741 eh = (struct ppc_link_hash_entry *) h;
6742 if (eh->adjust_done)
6745 sym_sec = eh->elf.root.u.def.section;
6746 opd = get_opd_info (sym_sec);
6747 if (opd != NULL && opd->adjust != NULL)
6749 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
6752 /* This entry has been deleted. */
6753 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6756 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6757 if (elf_discarded_section (dsec))
6759 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6763 eh->elf.root.u.def.value = 0;
6764 eh->elf.root.u.def.section = dsec;
6767 eh->elf.root.u.def.value += adjust;
6768 eh->adjust_done = 1;
6773 /* Handles decrementing dynamic reloc counts for the reloc specified by
6774 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6775 have already been determined. */
6778 dec_dynrel_count (bfd_vma r_info,
6780 struct bfd_link_info *info,
6781 Elf_Internal_Sym **local_syms,
6782 struct elf_link_hash_entry *h,
6785 enum elf_ppc64_reloc_type r_type;
6786 struct ppc_dyn_relocs *p;
6787 struct ppc_dyn_relocs **pp;
6789 /* Can this reloc be dynamic? This switch, and later tests here
6790 should be kept in sync with the code in check_relocs. */
6791 r_type = ELF64_R_TYPE (r_info);
6797 case R_PPC64_TPREL16:
6798 case R_PPC64_TPREL16_LO:
6799 case R_PPC64_TPREL16_HI:
6800 case R_PPC64_TPREL16_HA:
6801 case R_PPC64_TPREL16_DS:
6802 case R_PPC64_TPREL16_LO_DS:
6803 case R_PPC64_TPREL16_HIGHER:
6804 case R_PPC64_TPREL16_HIGHERA:
6805 case R_PPC64_TPREL16_HIGHEST:
6806 case R_PPC64_TPREL16_HIGHESTA:
6810 case R_PPC64_TPREL64:
6811 case R_PPC64_DTPMOD64:
6812 case R_PPC64_DTPREL64:
6813 case R_PPC64_ADDR64:
6817 case R_PPC64_ADDR14:
6818 case R_PPC64_ADDR14_BRNTAKEN:
6819 case R_PPC64_ADDR14_BRTAKEN:
6820 case R_PPC64_ADDR16:
6821 case R_PPC64_ADDR16_DS:
6822 case R_PPC64_ADDR16_HA:
6823 case R_PPC64_ADDR16_HI:
6824 case R_PPC64_ADDR16_HIGHER:
6825 case R_PPC64_ADDR16_HIGHERA:
6826 case R_PPC64_ADDR16_HIGHEST:
6827 case R_PPC64_ADDR16_HIGHESTA:
6828 case R_PPC64_ADDR16_LO:
6829 case R_PPC64_ADDR16_LO_DS:
6830 case R_PPC64_ADDR24:
6831 case R_PPC64_ADDR32:
6832 case R_PPC64_UADDR16:
6833 case R_PPC64_UADDR32:
6834 case R_PPC64_UADDR64:
6839 if (local_syms != NULL)
6841 unsigned long r_symndx;
6842 Elf_Internal_Sym *sym;
6843 bfd *ibfd = sec->owner;
6845 r_symndx = ELF64_R_SYM (r_info);
6846 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6851 && (must_be_dyn_reloc (info, r_type)
6854 || h->root.type == bfd_link_hash_defweak
6855 || !h->def_regular))))
6856 || (ELIMINATE_COPY_RELOCS
6859 && (h->root.type == bfd_link_hash_defweak
6860 || !h->def_regular)))
6866 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6869 if (sym_sec != NULL)
6871 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6872 pp = (struct ppc_dyn_relocs **) vpp;
6876 void *vpp = &elf_section_data (sec)->local_dynrel;
6877 pp = (struct ppc_dyn_relocs **) vpp;
6880 /* elf_gc_sweep may have already removed all dyn relocs associated
6881 with local syms for a given section. Don't report a dynreloc
6887 while ((p = *pp) != NULL)
6891 if (!must_be_dyn_reloc (info, r_type))
6901 (*_bfd_error_handler) (_("dynreloc miscount for %B, section %A"),
6903 bfd_set_error (bfd_error_bad_value);
6907 /* Remove unused Official Procedure Descriptor entries. Currently we
6908 only remove those associated with functions in discarded link-once
6909 sections, or weakly defined functions that have been overridden. It
6910 would be possible to remove many more entries for statically linked
6914 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
6917 bfd_boolean some_edited = FALSE;
6918 asection *need_pad = NULL;
6920 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6923 Elf_Internal_Rela *relstart, *rel, *relend;
6924 Elf_Internal_Shdr *symtab_hdr;
6925 Elf_Internal_Sym *local_syms;
6927 struct _opd_sec_data *opd;
6928 bfd_boolean need_edit, add_aux_fields;
6929 bfd_size_type cnt_16b = 0;
6931 if (!is_ppc64_elf (ibfd))
6934 sec = bfd_get_section_by_name (ibfd, ".opd");
6935 if (sec == NULL || sec->size == 0)
6938 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6941 if (sec->output_section == bfd_abs_section_ptr)
6944 /* Look through the section relocs. */
6945 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6949 symtab_hdr = &elf_symtab_hdr (ibfd);
6951 /* Read the relocations. */
6952 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6954 if (relstart == NULL)
6957 /* First run through the relocs to check they are sane, and to
6958 determine whether we need to edit this opd section. */
6962 relend = relstart + sec->reloc_count;
6963 for (rel = relstart; rel < relend; )
6965 enum elf_ppc64_reloc_type r_type;
6966 unsigned long r_symndx;
6968 struct elf_link_hash_entry *h;
6969 Elf_Internal_Sym *sym;
6971 /* .opd contains a regular array of 16 or 24 byte entries. We're
6972 only interested in the reloc pointing to a function entry
6974 if (rel->r_offset != offset
6975 || rel + 1 >= relend
6976 || (rel + 1)->r_offset != offset + 8)
6978 /* If someone messes with .opd alignment then after a
6979 "ld -r" we might have padding in the middle of .opd.
6980 Also, there's nothing to prevent someone putting
6981 something silly in .opd with the assembler. No .opd
6982 optimization for them! */
6984 (*_bfd_error_handler)
6985 (_("%B: .opd is not a regular array of opd entries"), ibfd);
6990 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6991 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6993 (*_bfd_error_handler)
6994 (_("%B: unexpected reloc type %u in .opd section"),
7000 r_symndx = ELF64_R_SYM (rel->r_info);
7001 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7005 if (sym_sec == NULL || sym_sec->owner == NULL)
7007 const char *sym_name;
7009 sym_name = h->root.root.string;
7011 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7014 (*_bfd_error_handler)
7015 (_("%B: undefined sym `%s' in .opd section"),
7021 /* opd entries are always for functions defined in the
7022 current input bfd. If the symbol isn't defined in the
7023 input bfd, then we won't be using the function in this
7024 bfd; It must be defined in a linkonce section in another
7025 bfd, or is weak. It's also possible that we are
7026 discarding the function due to a linker script /DISCARD/,
7027 which we test for via the output_section. */
7028 if (sym_sec->owner != ibfd
7029 || sym_sec->output_section == bfd_abs_section_ptr)
7034 || (rel + 1 == relend && rel->r_offset == offset + 16))
7036 if (sec->size == offset + 24)
7041 if (rel == relend && sec->size == offset + 16)
7049 if (rel->r_offset == offset + 24)
7051 else if (rel->r_offset != offset + 16)
7053 else if (rel + 1 < relend
7054 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7055 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7060 else if (rel + 2 < relend
7061 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7062 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7071 add_aux_fields = non_overlapping && cnt_16b > 0;
7073 if (need_edit || add_aux_fields)
7075 Elf_Internal_Rela *write_rel;
7076 Elf_Internal_Shdr *rel_hdr;
7077 bfd_byte *rptr, *wptr;
7078 bfd_byte *new_contents;
7083 new_contents = NULL;
7084 amt = sec->size * sizeof (long) / 8;
7085 opd = &ppc64_elf_section_data (sec)->u.opd;
7086 opd->adjust = bfd_zalloc (sec->owner, amt);
7087 if (opd->adjust == NULL)
7089 ppc64_elf_section_data (sec)->sec_type = sec_opd;
7091 /* This seems a waste of time as input .opd sections are all
7092 zeros as generated by gcc, but I suppose there's no reason
7093 this will always be so. We might start putting something in
7094 the third word of .opd entries. */
7095 if ((sec->flags & SEC_IN_MEMORY) == 0)
7098 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7103 if (local_syms != NULL
7104 && symtab_hdr->contents != (unsigned char *) local_syms)
7106 if (elf_section_data (sec)->relocs != relstart)
7110 sec->contents = loc;
7111 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7114 elf_section_data (sec)->relocs = relstart;
7116 new_contents = sec->contents;
7119 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7120 if (new_contents == NULL)
7124 wptr = new_contents;
7125 rptr = sec->contents;
7127 write_rel = relstart;
7131 for (rel = relstart; rel < relend; rel++)
7133 unsigned long r_symndx;
7135 struct elf_link_hash_entry *h;
7136 Elf_Internal_Sym *sym;
7138 r_symndx = ELF64_R_SYM (rel->r_info);
7139 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7143 if (rel->r_offset == offset)
7145 struct ppc_link_hash_entry *fdh = NULL;
7147 /* See if the .opd entry is full 24 byte or
7148 16 byte (with fd_aux entry overlapped with next
7151 if ((rel + 2 == relend && sec->size == offset + 16)
7152 || (rel + 3 < relend
7153 && rel[2].r_offset == offset + 16
7154 && rel[3].r_offset == offset + 24
7155 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7156 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7160 && h->root.root.string[0] == '.')
7162 struct ppc_link_hash_table *htab;
7164 htab = ppc_hash_table (info);
7166 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7169 && fdh->elf.root.type != bfd_link_hash_defined
7170 && fdh->elf.root.type != bfd_link_hash_defweak)
7174 skip = (sym_sec->owner != ibfd
7175 || sym_sec->output_section == bfd_abs_section_ptr);
7178 if (fdh != NULL && sym_sec->owner == ibfd)
7180 /* Arrange for the function descriptor sym
7182 fdh->elf.root.u.def.value = 0;
7183 fdh->elf.root.u.def.section = sym_sec;
7185 opd->adjust[rel->r_offset / 8] = -1;
7189 /* We'll be keeping this opd entry. */
7193 /* Redefine the function descriptor symbol to
7194 this location in the opd section. It is
7195 necessary to update the value here rather
7196 than using an array of adjustments as we do
7197 for local symbols, because various places
7198 in the generic ELF code use the value
7199 stored in u.def.value. */
7200 fdh->elf.root.u.def.value = wptr - new_contents;
7201 fdh->adjust_done = 1;
7204 /* Local syms are a bit tricky. We could
7205 tweak them as they can be cached, but
7206 we'd need to look through the local syms
7207 for the function descriptor sym which we
7208 don't have at the moment. So keep an
7209 array of adjustments. */
7210 opd->adjust[rel->r_offset / 8]
7211 = (wptr - new_contents) - (rptr - sec->contents);
7214 memcpy (wptr, rptr, opd_ent_size);
7215 wptr += opd_ent_size;
7216 if (add_aux_fields && opd_ent_size == 16)
7218 memset (wptr, '\0', 8);
7222 rptr += opd_ent_size;
7223 offset += opd_ent_size;
7229 && !info->relocatable
7230 && !dec_dynrel_count (rel->r_info, sec, info,
7236 /* We need to adjust any reloc offsets to point to the
7237 new opd entries. While we're at it, we may as well
7238 remove redundant relocs. */
7239 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
7240 if (write_rel != rel)
7241 memcpy (write_rel, rel, sizeof (*rel));
7246 sec->size = wptr - new_contents;
7247 sec->reloc_count = write_rel - relstart;
7250 free (sec->contents);
7251 sec->contents = new_contents;
7254 /* Fudge the header size too, as this is used later in
7255 elf_bfd_final_link if we are emitting relocs. */
7256 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7257 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7260 else if (elf_section_data (sec)->relocs != relstart)
7263 if (local_syms != NULL
7264 && symtab_hdr->contents != (unsigned char *) local_syms)
7266 if (!info->keep_memory)
7269 symtab_hdr->contents = (unsigned char *) local_syms;
7274 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7276 /* If we are doing a final link and the last .opd entry is just 16 byte
7277 long, add a 8 byte padding after it. */
7278 if (need_pad != NULL && !info->relocatable)
7282 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7284 BFD_ASSERT (need_pad->size > 0);
7286 p = bfd_malloc (need_pad->size + 8);
7290 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7291 p, 0, need_pad->size))
7294 need_pad->contents = p;
7295 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7299 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7303 need_pad->contents = p;
7306 memset (need_pad->contents + need_pad->size, 0, 8);
7307 need_pad->size += 8;
7313 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7316 ppc64_elf_tls_setup (struct bfd_link_info *info,
7317 int no_tls_get_addr_opt,
7320 struct ppc_link_hash_table *htab;
7322 htab = ppc_hash_table (info);
7327 htab->do_multi_toc = 0;
7328 else if (!htab->do_multi_toc)
7331 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7332 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7333 FALSE, FALSE, TRUE));
7334 /* Move dynamic linking info to the function descriptor sym. */
7335 if (htab->tls_get_addr != NULL)
7336 func_desc_adjust (&htab->tls_get_addr->elf, info);
7337 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7338 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7339 FALSE, FALSE, TRUE));
7340 if (!no_tls_get_addr_opt)
7342 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7344 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7345 FALSE, FALSE, TRUE);
7347 func_desc_adjust (opt, info);
7348 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7349 FALSE, FALSE, TRUE);
7351 && (opt_fd->root.type == bfd_link_hash_defined
7352 || opt_fd->root.type == bfd_link_hash_defweak))
7354 /* If glibc supports an optimized __tls_get_addr call stub,
7355 signalled by the presence of __tls_get_addr_opt, and we'll
7356 be calling __tls_get_addr via a plt call stub, then
7357 make __tls_get_addr point to __tls_get_addr_opt. */
7358 tga_fd = &htab->tls_get_addr_fd->elf;
7359 if (htab->elf.dynamic_sections_created
7361 && (tga_fd->type == STT_FUNC
7362 || tga_fd->needs_plt)
7363 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7364 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7365 && tga_fd->root.type == bfd_link_hash_undefweak)))
7367 struct plt_entry *ent;
7369 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7370 if (ent->plt.refcount > 0)
7374 tga_fd->root.type = bfd_link_hash_indirect;
7375 tga_fd->root.u.i.link = &opt_fd->root;
7376 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7377 if (opt_fd->dynindx != -1)
7379 /* Use __tls_get_addr_opt in dynamic relocations. */
7380 opt_fd->dynindx = -1;
7381 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7382 opt_fd->dynstr_index);
7383 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7386 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7387 tga = &htab->tls_get_addr->elf;
7388 if (opt != NULL && tga != NULL)
7390 tga->root.type = bfd_link_hash_indirect;
7391 tga->root.u.i.link = &opt->root;
7392 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7393 _bfd_elf_link_hash_hide_symbol (info, opt,
7395 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7397 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7398 htab->tls_get_addr_fd->is_func_descriptor = 1;
7399 if (htab->tls_get_addr != NULL)
7401 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7402 htab->tls_get_addr->is_func = 1;
7408 no_tls_get_addr_opt = TRUE;
7410 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
7411 return _bfd_elf_tls_setup (info->output_bfd, info);
7414 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7418 branch_reloc_hash_match (const bfd *ibfd,
7419 const Elf_Internal_Rela *rel,
7420 const struct ppc_link_hash_entry *hash1,
7421 const struct ppc_link_hash_entry *hash2)
7423 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7424 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7425 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7427 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7429 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7430 struct elf_link_hash_entry *h;
7432 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7433 h = elf_follow_link (h);
7434 if (h == &hash1->elf || h == &hash2->elf)
7440 /* Run through all the TLS relocs looking for optimization
7441 opportunities. The linker has been hacked (see ppc64elf.em) to do
7442 a preliminary section layout so that we know the TLS segment
7443 offsets. We can't optimize earlier because some optimizations need
7444 to know the tp offset, and we need to optimize before allocating
7445 dynamic relocations. */
7448 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7452 struct ppc_link_hash_table *htab;
7455 if (info->relocatable || !info->executable)
7458 htab = ppc_hash_table (info);
7462 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7464 Elf_Internal_Sym *locsyms = NULL;
7465 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7466 unsigned char *toc_ref = NULL;
7468 /* Look at all the sections for this file. Make two passes over
7469 the relocs. On the first pass, mark toc entries involved
7470 with tls relocs, and check that tls relocs involved in
7471 setting up a tls_get_addr call are indeed followed by such a
7472 call. If they are not, exclude them from the optimizations
7473 done on the second pass. */
7474 for (pass = 0; pass < 2; ++pass)
7475 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7476 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7478 Elf_Internal_Rela *relstart, *rel, *relend;
7480 /* Read the relocations. */
7481 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7483 if (relstart == NULL)
7486 relend = relstart + sec->reloc_count;
7487 for (rel = relstart; rel < relend; rel++)
7489 enum elf_ppc64_reloc_type r_type;
7490 unsigned long r_symndx;
7491 struct elf_link_hash_entry *h;
7492 Elf_Internal_Sym *sym;
7494 unsigned char *tls_mask;
7495 unsigned char tls_set, tls_clear, tls_type = 0;
7497 bfd_boolean ok_tprel, is_local;
7498 long toc_ref_index = 0;
7499 int expecting_tls_get_addr = 0;
7501 r_symndx = ELF64_R_SYM (rel->r_info);
7502 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7506 if (elf_section_data (sec)->relocs != relstart)
7508 if (toc_ref != NULL)
7511 && (elf_symtab_hdr (ibfd).contents
7512 != (unsigned char *) locsyms))
7519 if (h->root.type == bfd_link_hash_defined
7520 || h->root.type == bfd_link_hash_defweak)
7521 value = h->root.u.def.value;
7522 else if (h->root.type == bfd_link_hash_undefweak)
7528 /* Symbols referenced by TLS relocs must be of type
7529 STT_TLS. So no need for .opd local sym adjust. */
7530 value = sym->st_value;
7539 && h->root.type == bfd_link_hash_undefweak)
7543 value += sym_sec->output_offset;
7544 value += sym_sec->output_section->vma;
7545 value -= htab->elf.tls_sec->vma;
7546 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7547 < (bfd_vma) 1 << 32);
7551 r_type = ELF64_R_TYPE (rel->r_info);
7554 case R_PPC64_GOT_TLSLD16:
7555 case R_PPC64_GOT_TLSLD16_LO:
7556 expecting_tls_get_addr = 1;
7559 case R_PPC64_GOT_TLSLD16_HI:
7560 case R_PPC64_GOT_TLSLD16_HA:
7561 /* These relocs should never be against a symbol
7562 defined in a shared lib. Leave them alone if
7563 that turns out to be the case. */
7570 tls_type = TLS_TLS | TLS_LD;
7573 case R_PPC64_GOT_TLSGD16:
7574 case R_PPC64_GOT_TLSGD16_LO:
7575 expecting_tls_get_addr = 1;
7578 case R_PPC64_GOT_TLSGD16_HI:
7579 case R_PPC64_GOT_TLSGD16_HA:
7585 tls_set = TLS_TLS | TLS_TPRELGD;
7587 tls_type = TLS_TLS | TLS_GD;
7590 case R_PPC64_GOT_TPREL16_DS:
7591 case R_PPC64_GOT_TPREL16_LO_DS:
7592 case R_PPC64_GOT_TPREL16_HI:
7593 case R_PPC64_GOT_TPREL16_HA:
7598 tls_clear = TLS_TPREL;
7599 tls_type = TLS_TLS | TLS_TPREL;
7605 case R_PPC64_TOC16_LO:
7609 if (sym_sec == NULL || sym_sec != toc)
7612 /* Mark this toc entry as referenced by a TLS
7613 code sequence. We can do that now in the
7614 case of R_PPC64_TLS, and after checking for
7615 tls_get_addr for the TOC16 relocs. */
7616 if (toc_ref == NULL)
7618 toc_ref = bfd_zmalloc (toc->size / 8);
7619 if (toc_ref == NULL)
7623 value = h->root.u.def.value;
7625 value = sym->st_value;
7626 value += rel->r_addend;
7627 BFD_ASSERT (value < toc->size && value % 8 == 0);
7628 toc_ref_index = value / 8;
7629 if (r_type == R_PPC64_TLS
7630 || r_type == R_PPC64_TLSGD
7631 || r_type == R_PPC64_TLSLD)
7633 toc_ref[toc_ref_index] = 1;
7637 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7642 expecting_tls_get_addr = 2;
7645 case R_PPC64_TPREL64:
7649 || !toc_ref[rel->r_offset / 8])
7654 tls_set = TLS_EXPLICIT;
7655 tls_clear = TLS_TPREL;
7660 case R_PPC64_DTPMOD64:
7664 || !toc_ref[rel->r_offset / 8])
7666 if (rel + 1 < relend
7668 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7669 && rel[1].r_offset == rel->r_offset + 8)
7673 tls_set = TLS_EXPLICIT | TLS_GD;
7676 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7685 tls_set = TLS_EXPLICIT;
7696 if (!expecting_tls_get_addr
7697 || !sec->has_tls_get_addr_call)
7700 if (rel + 1 < relend
7701 && branch_reloc_hash_match (ibfd, rel + 1,
7703 htab->tls_get_addr_fd))
7705 if (expecting_tls_get_addr == 2)
7707 /* Check for toc tls entries. */
7708 unsigned char *toc_tls;
7711 retval = get_tls_mask (&toc_tls, NULL, NULL,
7716 if (retval > 1 && toc_tls != NULL)
7717 toc_ref[toc_ref_index] = 1;
7722 if (expecting_tls_get_addr != 1)
7725 /* Uh oh, we didn't find the expected call. We
7726 could just mark this symbol to exclude it
7727 from tls optimization but it's safer to skip
7728 the entire section. */
7729 sec->has_tls_reloc = 0;
7733 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
7735 struct plt_entry *ent;
7736 for (ent = htab->tls_get_addr->elf.plt.plist;
7739 if (ent->addend == 0)
7741 if (ent->plt.refcount > 0)
7743 ent->plt.refcount -= 1;
7744 expecting_tls_get_addr = 0;
7750 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
7752 struct plt_entry *ent;
7753 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7756 if (ent->addend == 0)
7758 if (ent->plt.refcount > 0)
7759 ent->plt.refcount -= 1;
7767 if ((tls_set & TLS_EXPLICIT) == 0)
7769 struct got_entry *ent;
7771 /* Adjust got entry for this reloc. */
7775 ent = elf_local_got_ents (ibfd)[r_symndx];
7777 for (; ent != NULL; ent = ent->next)
7778 if (ent->addend == rel->r_addend
7779 && ent->owner == ibfd
7780 && ent->tls_type == tls_type)
7787 /* We managed to get rid of a got entry. */
7788 if (ent->got.refcount > 0)
7789 ent->got.refcount -= 1;
7794 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7795 we'll lose one or two dyn relocs. */
7796 if (!dec_dynrel_count (rel->r_info, sec, info,
7800 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7802 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7808 *tls_mask |= tls_set;
7809 *tls_mask &= ~tls_clear;
7812 if (elf_section_data (sec)->relocs != relstart)
7816 if (toc_ref != NULL)
7820 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7822 if (!info->keep_memory)
7825 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7831 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7832 the values of any global symbols in a toc section that has been
7833 edited. Globals in toc sections should be a rarity, so this function
7834 sets a flag if any are found in toc sections other than the one just
7835 edited, so that futher hash table traversals can be avoided. */
7837 struct adjust_toc_info
7840 unsigned long *skip;
7841 bfd_boolean global_toc_syms;
7844 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7847 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7849 struct ppc_link_hash_entry *eh;
7850 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7853 if (h->root.type == bfd_link_hash_indirect)
7856 if (h->root.type == bfd_link_hash_warning)
7857 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7859 if (h->root.type != bfd_link_hash_defined
7860 && h->root.type != bfd_link_hash_defweak)
7863 eh = (struct ppc_link_hash_entry *) h;
7864 if (eh->adjust_done)
7867 if (eh->elf.root.u.def.section == toc_inf->toc)
7869 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7870 i = toc_inf->toc->rawsize >> 3;
7872 i = eh->elf.root.u.def.value >> 3;
7874 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
7876 (*_bfd_error_handler)
7877 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7880 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
7881 eh->elf.root.u.def.value = (bfd_vma) i << 3;
7884 eh->elf.root.u.def.value -= toc_inf->skip[i];
7885 eh->adjust_done = 1;
7887 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7888 toc_inf->global_toc_syms = TRUE;
7893 /* Examine all relocs referencing .toc sections in order to remove
7894 unused .toc entries. */
7897 ppc64_elf_edit_toc (struct bfd_link_info *info)
7900 struct adjust_toc_info toc_inf;
7901 struct ppc_link_hash_table *htab = ppc_hash_table (info);
7903 htab->do_toc_opt = 1;
7904 toc_inf.global_toc_syms = TRUE;
7905 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7907 asection *toc, *sec;
7908 Elf_Internal_Shdr *symtab_hdr;
7909 Elf_Internal_Sym *local_syms;
7910 Elf_Internal_Rela *relstart, *rel;
7911 unsigned long *skip, *drop;
7912 unsigned char *used;
7913 unsigned char *keep, last, some_unused;
7915 if (!is_ppc64_elf (ibfd))
7918 toc = bfd_get_section_by_name (ibfd, ".toc");
7921 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7922 || elf_discarded_section (toc))
7926 symtab_hdr = &elf_symtab_hdr (ibfd);
7928 /* Look at sections dropped from the final link. */
7931 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7933 if (sec->reloc_count == 0
7934 || !elf_discarded_section (sec)
7935 || get_opd_info (sec)
7936 || (sec->flags & SEC_ALLOC) == 0
7937 || (sec->flags & SEC_DEBUGGING) != 0)
7940 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7941 if (relstart == NULL)
7944 /* Run through the relocs to see which toc entries might be
7946 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7948 enum elf_ppc64_reloc_type r_type;
7949 unsigned long r_symndx;
7951 struct elf_link_hash_entry *h;
7952 Elf_Internal_Sym *sym;
7955 r_type = ELF64_R_TYPE (rel->r_info);
7962 case R_PPC64_TOC16_LO:
7963 case R_PPC64_TOC16_HI:
7964 case R_PPC64_TOC16_HA:
7965 case R_PPC64_TOC16_DS:
7966 case R_PPC64_TOC16_LO_DS:
7970 r_symndx = ELF64_R_SYM (rel->r_info);
7971 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7979 val = h->root.u.def.value;
7981 val = sym->st_value;
7982 val += rel->r_addend;
7984 if (val >= toc->size)
7987 /* Anything in the toc ought to be aligned to 8 bytes.
7988 If not, don't mark as unused. */
7994 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
7999 skip[val >> 3] = ref_from_discarded;
8002 if (elf_section_data (sec)->relocs != relstart)
8006 /* For largetoc loads of address constants, we can convert
8007 . addis rx,2,addr@got@ha
8008 . ld ry,addr@got@l(rx)
8010 . addis rx,2,addr@toc@ha
8011 . addi ry,rx,addr@toc@l
8012 when addr is within 2G of the toc pointer. This then means
8013 that the word storing "addr" in the toc is no longer needed. */
8015 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8016 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8017 && toc->reloc_count != 0)
8019 /* Read toc relocs. */
8020 relstart = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8022 if (relstart == NULL)
8025 for (rel = relstart; rel < relstart + toc->reloc_count; ++rel)
8027 enum elf_ppc64_reloc_type r_type;
8028 unsigned long r_symndx;
8030 struct elf_link_hash_entry *h;
8031 Elf_Internal_Sym *sym;
8034 r_type = ELF64_R_TYPE (rel->r_info);
8035 if (r_type != R_PPC64_ADDR64)
8038 r_symndx = ELF64_R_SYM (rel->r_info);
8039 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8043 if (!SYMBOL_CALLS_LOCAL (info, h))
8048 if (h->type == STT_GNU_IFUNC)
8050 val = h->root.u.def.value;
8054 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8056 val = sym->st_value;
8058 val += rel->r_addend;
8059 val += sym_sec->output_section->vma + sym_sec->output_offset;
8061 /* We don't yet know the exact toc pointer value, but we
8062 know it will be somewhere in the toc section. Don't
8063 optimize if the difference from any possible toc
8064 pointer is outside [ff..f80008000, 7fff7fff]. */
8065 addr = toc->output_section->vma + TOC_BASE_OFF;
8066 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8069 addr = toc->output_section->vma + toc->output_section->rawsize;
8070 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8075 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8080 skip[rel->r_offset >> 3]
8081 |= can_optimize | ((rel - relstart) << 2);
8084 if (elf_section_data (toc)->relocs != relstart)
8091 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8095 if (local_syms != NULL
8096 && symtab_hdr->contents != (unsigned char *) local_syms)
8100 && elf_section_data (sec)->relocs != relstart)
8107 /* Now check all kept sections that might reference the toc.
8108 Check the toc itself last. */
8109 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8112 sec = (sec == toc ? NULL
8113 : sec->next == NULL ? toc
8114 : sec->next == toc && toc->next ? toc->next
8119 if (sec->reloc_count == 0
8120 || elf_discarded_section (sec)
8121 || get_opd_info (sec)
8122 || (sec->flags & SEC_ALLOC) == 0
8123 || (sec->flags & SEC_DEBUGGING) != 0)
8126 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8128 if (relstart == NULL)
8131 /* Mark toc entries referenced as used. */
8134 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8136 enum elf_ppc64_reloc_type r_type;
8137 unsigned long r_symndx;
8139 struct elf_link_hash_entry *h;
8140 Elf_Internal_Sym *sym;
8143 r_type = ELF64_R_TYPE (rel->r_info);
8147 case R_PPC64_TOC16_LO:
8148 case R_PPC64_TOC16_HI:
8149 case R_PPC64_TOC16_HA:
8150 case R_PPC64_TOC16_DS:
8151 case R_PPC64_TOC16_LO_DS:
8152 /* In case we're taking addresses of toc entries. */
8153 case R_PPC64_ADDR64:
8160 r_symndx = ELF64_R_SYM (rel->r_info);
8161 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8172 val = h->root.u.def.value;
8174 val = sym->st_value;
8175 val += rel->r_addend;
8177 if (val >= toc->size)
8180 if ((skip[val >> 3] & can_optimize) != 0)
8187 case R_PPC64_TOC16_HA:
8190 case R_PPC64_TOC16_LO_DS:
8191 off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
8192 if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
8194 if ((opc & (0x3f << 2)) == (58u << 2))
8199 /* Wrong sort of reloc, or not a ld. We may
8200 as well clear ref_from_discarded too. */
8205 /* For the toc section, we only mark as used if
8206 this entry itself isn't unused. */
8209 && (used[rel->r_offset >> 3]
8210 || !(skip[rel->r_offset >> 3] & ref_from_discarded)))
8211 /* Do all the relocs again, to catch reference
8219 if (elf_section_data (sec)->relocs != relstart)
8223 /* Merge the used and skip arrays. Assume that TOC
8224 doublewords not appearing as either used or unused belong
8225 to to an entry more than one doubleword in size. */
8226 for (drop = skip, keep = used, last = 0, some_unused = 0;
8227 drop < skip + (toc->size + 7) / 8;
8232 *drop &= ~ref_from_discarded;
8233 if ((*drop & can_optimize) != 0)
8240 last = ref_from_discarded;
8250 bfd_byte *contents, *src;
8252 Elf_Internal_Sym *sym;
8253 bfd_boolean local_toc_syms = FALSE;
8255 /* Shuffle the toc contents, and at the same time convert the
8256 skip array from booleans into offsets. */
8257 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8260 elf_section_data (toc)->this_hdr.contents = contents;
8262 for (src = contents, off = 0, drop = skip;
8263 src < contents + toc->size;
8266 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8271 memcpy (src - off, src, 8);
8275 toc->rawsize = toc->size;
8276 toc->size = src - contents - off;
8278 /* Adjust addends for relocs against the toc section sym,
8279 and optimize any accesses we can. */
8280 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8282 if (sec->reloc_count == 0
8283 || elf_discarded_section (sec))
8286 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8288 if (relstart == NULL)
8291 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8293 enum elf_ppc64_reloc_type r_type;
8294 unsigned long r_symndx;
8296 struct elf_link_hash_entry *h;
8299 r_type = ELF64_R_TYPE (rel->r_info);
8306 case R_PPC64_TOC16_LO:
8307 case R_PPC64_TOC16_HI:
8308 case R_PPC64_TOC16_HA:
8309 case R_PPC64_TOC16_DS:
8310 case R_PPC64_TOC16_LO_DS:
8311 case R_PPC64_ADDR64:
8315 r_symndx = ELF64_R_SYM (rel->r_info);
8316 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8324 val = h->root.u.def.value;
8327 val = sym->st_value;
8329 local_toc_syms = TRUE;
8332 val += rel->r_addend;
8334 if (val > toc->rawsize)
8336 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8338 else if ((skip[val >> 3] & can_optimize) != 0)
8340 Elf_Internal_Rela *tocrel
8341 = elf_section_data (toc)->relocs + (skip[val >> 3] >> 2);
8342 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8346 case R_PPC64_TOC16_HA:
8347 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8350 case R_PPC64_TOC16_LO_DS:
8351 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8357 rel->r_addend = tocrel->r_addend;
8358 elf_section_data (sec)->relocs = relstart;
8362 if (h != NULL || sym->st_value != 0)
8365 rel->r_addend -= skip[val >> 3];
8366 elf_section_data (sec)->relocs = relstart;
8369 if (elf_section_data (sec)->relocs != relstart)
8373 /* We shouldn't have local or global symbols defined in the TOC,
8374 but handle them anyway. */
8375 if (local_syms != NULL)
8376 for (sym = local_syms;
8377 sym < local_syms + symtab_hdr->sh_info;
8379 if (sym->st_value != 0
8380 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8384 if (sym->st_value > toc->rawsize)
8385 i = toc->rawsize >> 3;
8387 i = sym->st_value >> 3;
8389 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8392 (*_bfd_error_handler)
8393 (_("%s defined on removed toc entry"),
8394 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8397 while ((skip[i] & (ref_from_discarded | can_optimize)));
8398 sym->st_value = (bfd_vma) i << 3;
8401 sym->st_value -= skip[i];
8402 symtab_hdr->contents = (unsigned char *) local_syms;
8405 /* Adjust any global syms defined in this toc input section. */
8406 if (toc_inf.global_toc_syms)
8409 toc_inf.skip = skip;
8410 toc_inf.global_toc_syms = FALSE;
8411 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8415 if (toc->reloc_count != 0)
8417 Elf_Internal_Shdr *rel_hdr;
8418 Elf_Internal_Rela *wrel;
8421 /* Read toc relocs. */
8422 relstart = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8424 if (relstart == NULL)
8427 /* Remove unused toc relocs, and adjust those we keep. */
8429 for (rel = relstart; rel < relstart + toc->reloc_count; ++rel)
8430 if ((skip[rel->r_offset >> 3]
8431 & (ref_from_discarded | can_optimize)) == 0)
8433 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8434 wrel->r_info = rel->r_info;
8435 wrel->r_addend = rel->r_addend;
8438 else if (!dec_dynrel_count (rel->r_info, toc, info,
8439 &local_syms, NULL, NULL))
8442 toc->reloc_count = wrel - relstart;
8443 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8444 sz = rel_hdr->sh_entsize;
8445 rel_hdr->sh_size = toc->reloc_count * sz;
8449 if (local_syms != NULL
8450 && symtab_hdr->contents != (unsigned char *) local_syms)
8452 if (!info->keep_memory)
8455 symtab_hdr->contents = (unsigned char *) local_syms;
8463 /* Return true iff input section I references the TOC using
8464 instructions limited to +/-32k offsets. */
8467 ppc64_elf_has_small_toc_reloc (asection *i)
8469 return (is_ppc64_elf (i->owner)
8470 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8473 /* Allocate space for one GOT entry. */
8476 allocate_got (struct elf_link_hash_entry *h,
8477 struct bfd_link_info *info,
8478 struct got_entry *gent)
8480 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8482 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8483 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8485 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8486 ? 2 : 1) * sizeof (Elf64_External_Rela);
8487 asection *got = ppc64_elf_tdata (gent->owner)->got;
8489 gent->got.offset = got->size;
8490 got->size += entsize;
8492 dyn = htab->elf.dynamic_sections_created;
8494 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8495 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8496 || h->root.type != bfd_link_hash_undefweak))
8498 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8499 relgot->size += rentsize;
8501 else if (h->type == STT_GNU_IFUNC)
8503 asection *relgot = htab->reliplt;
8504 relgot->size += rentsize;
8505 htab->got_reli_size += rentsize;
8509 /* This function merges got entries in the same toc group. */
8512 merge_got_entries (struct got_entry **pent)
8514 struct got_entry *ent, *ent2;
8516 for (ent = *pent; ent != NULL; ent = ent->next)
8517 if (!ent->is_indirect)
8518 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8519 if (!ent2->is_indirect
8520 && ent2->addend == ent->addend
8521 && ent2->tls_type == ent->tls_type
8522 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8524 ent2->is_indirect = TRUE;
8525 ent2->got.ent = ent;
8529 /* Allocate space in .plt, .got and associated reloc sections for
8533 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8535 struct bfd_link_info *info;
8536 struct ppc_link_hash_table *htab;
8538 struct ppc_link_hash_entry *eh;
8539 struct ppc_dyn_relocs *p;
8540 struct got_entry **pgent, *gent;
8542 if (h->root.type == bfd_link_hash_indirect)
8545 if (h->root.type == bfd_link_hash_warning)
8546 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8548 info = (struct bfd_link_info *) inf;
8549 htab = ppc_hash_table (info);
8553 if ((htab->elf.dynamic_sections_created
8555 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
8556 || h->type == STT_GNU_IFUNC)
8558 struct plt_entry *pent;
8559 bfd_boolean doneone = FALSE;
8560 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8561 if (pent->plt.refcount > 0)
8563 if (!htab->elf.dynamic_sections_created
8564 || h->dynindx == -1)
8567 pent->plt.offset = s->size;
8568 s->size += PLT_ENTRY_SIZE;
8573 /* If this is the first .plt entry, make room for the special
8577 s->size += PLT_INITIAL_ENTRY_SIZE;
8579 pent->plt.offset = s->size;
8581 /* Make room for this entry. */
8582 s->size += PLT_ENTRY_SIZE;
8584 /* Make room for the .glink code. */
8587 s->size += GLINK_CALL_STUB_SIZE;
8588 /* We need bigger stubs past index 32767. */
8589 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
8593 /* We also need to make an entry in the .rela.plt section. */
8596 s->size += sizeof (Elf64_External_Rela);
8600 pent->plt.offset = (bfd_vma) -1;
8603 h->plt.plist = NULL;
8609 h->plt.plist = NULL;
8613 eh = (struct ppc_link_hash_entry *) h;
8614 /* Run through the TLS GD got entries first if we're changing them
8616 if ((eh->tls_mask & TLS_TPRELGD) != 0)
8617 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8618 if (gent->got.refcount > 0
8619 && (gent->tls_type & TLS_GD) != 0)
8621 /* This was a GD entry that has been converted to TPREL. If
8622 there happens to be a TPREL entry we can use that one. */
8623 struct got_entry *ent;
8624 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8625 if (ent->got.refcount > 0
8626 && (ent->tls_type & TLS_TPREL) != 0
8627 && ent->addend == gent->addend
8628 && ent->owner == gent->owner)
8630 gent->got.refcount = 0;
8634 /* If not, then we'll be using our own TPREL entry. */
8635 if (gent->got.refcount != 0)
8636 gent->tls_type = TLS_TLS | TLS_TPREL;
8639 /* Remove any list entry that won't generate a word in the GOT before
8640 we call merge_got_entries. Otherwise we risk merging to empty
8642 pgent = &h->got.glist;
8643 while ((gent = *pgent) != NULL)
8644 if (gent->got.refcount > 0)
8646 if ((gent->tls_type & TLS_LD) != 0
8649 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8650 *pgent = gent->next;
8653 pgent = &gent->next;
8656 *pgent = gent->next;
8658 if (!htab->do_multi_toc)
8659 merge_got_entries (&h->got.glist);
8661 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8662 if (!gent->is_indirect)
8664 /* Make sure this symbol is output as a dynamic symbol.
8665 Undefined weak syms won't yet be marked as dynamic,
8666 nor will all TLS symbols. */
8667 if (h->dynindx == -1
8669 && h->type != STT_GNU_IFUNC
8670 && htab->elf.dynamic_sections_created)
8672 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8676 if (!is_ppc64_elf (gent->owner))
8679 allocate_got (h, info, gent);
8682 if (eh->dyn_relocs == NULL
8683 || (!htab->elf.dynamic_sections_created
8684 && h->type != STT_GNU_IFUNC))
8687 /* In the shared -Bsymbolic case, discard space allocated for
8688 dynamic pc-relative relocs against symbols which turn out to be
8689 defined in regular objects. For the normal shared case, discard
8690 space for relocs that have become local due to symbol visibility
8695 /* Relocs that use pc_count are those that appear on a call insn,
8696 or certain REL relocs (see must_be_dyn_reloc) that can be
8697 generated via assembly. We want calls to protected symbols to
8698 resolve directly to the function rather than going via the plt.
8699 If people want function pointer comparisons to work as expected
8700 then they should avoid writing weird assembly. */
8701 if (SYMBOL_CALLS_LOCAL (info, h))
8703 struct ppc_dyn_relocs **pp;
8705 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
8707 p->count -= p->pc_count;
8716 /* Also discard relocs on undefined weak syms with non-default
8718 if (eh->dyn_relocs != NULL
8719 && h->root.type == bfd_link_hash_undefweak)
8721 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8722 eh->dyn_relocs = NULL;
8724 /* Make sure this symbol is output as a dynamic symbol.
8725 Undefined weak syms won't yet be marked as dynamic. */
8726 else if (h->dynindx == -1
8727 && !h->forced_local)
8729 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8734 else if (h->type == STT_GNU_IFUNC)
8736 if (!h->non_got_ref)
8737 eh->dyn_relocs = NULL;
8739 else if (ELIMINATE_COPY_RELOCS)
8741 /* For the non-shared case, discard space for relocs against
8742 symbols which turn out to need copy relocs or are not
8748 /* Make sure this symbol is output as a dynamic symbol.
8749 Undefined weak syms won't yet be marked as dynamic. */
8750 if (h->dynindx == -1
8751 && !h->forced_local)
8753 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8757 /* If that succeeded, we know we'll be keeping all the
8759 if (h->dynindx != -1)
8763 eh->dyn_relocs = NULL;
8768 /* Finally, allocate space. */
8769 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8771 asection *sreloc = elf_section_data (p->sec)->sreloc;
8772 if (!htab->elf.dynamic_sections_created)
8773 sreloc = htab->reliplt;
8774 sreloc->size += p->count * sizeof (Elf64_External_Rela);
8780 /* Find any dynamic relocs that apply to read-only sections. */
8783 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8785 struct ppc_link_hash_entry *eh;
8786 struct ppc_dyn_relocs *p;
8788 if (h->root.type == bfd_link_hash_warning)
8789 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8791 eh = (struct ppc_link_hash_entry *) h;
8792 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8794 asection *s = p->sec->output_section;
8796 if (s != NULL && (s->flags & SEC_READONLY) != 0)
8798 struct bfd_link_info *info = inf;
8800 info->flags |= DF_TEXTREL;
8802 /* Not an error, just cut short the traversal. */
8809 /* Set the sizes of the dynamic sections. */
8812 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
8813 struct bfd_link_info *info)
8815 struct ppc_link_hash_table *htab;
8820 struct got_entry *first_tlsld;
8822 htab = ppc_hash_table (info);
8826 dynobj = htab->elf.dynobj;
8830 if (htab->elf.dynamic_sections_created)
8832 /* Set the contents of the .interp section to the interpreter. */
8833 if (info->executable)
8835 s = bfd_get_section_by_name (dynobj, ".interp");
8838 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
8839 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
8843 /* Set up .got offsets for local syms, and space for local dynamic
8845 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8847 struct got_entry **lgot_ents;
8848 struct got_entry **end_lgot_ents;
8849 struct plt_entry **local_plt;
8850 struct plt_entry **end_local_plt;
8851 unsigned char *lgot_masks;
8852 bfd_size_type locsymcount;
8853 Elf_Internal_Shdr *symtab_hdr;
8856 if (!is_ppc64_elf (ibfd))
8859 for (s = ibfd->sections; s != NULL; s = s->next)
8861 struct ppc_dyn_relocs *p;
8863 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
8865 if (!bfd_is_abs_section (p->sec)
8866 && bfd_is_abs_section (p->sec->output_section))
8868 /* Input section has been discarded, either because
8869 it is a copy of a linkonce section or due to
8870 linker script /DISCARD/, so we'll be discarding
8873 else if (p->count != 0)
8875 srel = elf_section_data (p->sec)->sreloc;
8876 if (!htab->elf.dynamic_sections_created)
8877 srel = htab->reliplt;
8878 srel->size += p->count * sizeof (Elf64_External_Rela);
8879 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
8880 info->flags |= DF_TEXTREL;
8885 lgot_ents = elf_local_got_ents (ibfd);
8889 symtab_hdr = &elf_symtab_hdr (ibfd);
8890 locsymcount = symtab_hdr->sh_info;
8891 end_lgot_ents = lgot_ents + locsymcount;
8892 local_plt = (struct plt_entry **) end_lgot_ents;
8893 end_local_plt = local_plt + locsymcount;
8894 lgot_masks = (unsigned char *) end_local_plt;
8895 s = ppc64_elf_tdata (ibfd)->got;
8896 srel = ppc64_elf_tdata (ibfd)->relgot;
8897 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
8899 struct got_entry **pent, *ent;
8902 while ((ent = *pent) != NULL)
8903 if (ent->got.refcount > 0)
8905 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
8907 ppc64_tlsld_got (ibfd)->got.refcount += 1;
8912 unsigned int num = 1;
8913 ent->got.offset = s->size;
8914 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
8918 srel->size += num * sizeof (Elf64_External_Rela);
8919 else if ((*lgot_masks & PLT_IFUNC) != 0)
8922 += num * sizeof (Elf64_External_Rela);
8924 += num * sizeof (Elf64_External_Rela);
8933 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
8934 for (; local_plt < end_local_plt; ++local_plt)
8936 struct plt_entry *ent;
8938 for (ent = *local_plt; ent != NULL; ent = ent->next)
8939 if (ent->plt.refcount > 0)
8942 ent->plt.offset = s->size;
8943 s->size += PLT_ENTRY_SIZE;
8945 htab->reliplt->size += sizeof (Elf64_External_Rela);
8948 ent->plt.offset = (bfd_vma) -1;
8952 /* Allocate global sym .plt and .got entries, and space for global
8953 sym dynamic relocs. */
8954 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
8957 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8959 struct got_entry *ent;
8961 if (!is_ppc64_elf (ibfd))
8964 ent = ppc64_tlsld_got (ibfd);
8965 if (ent->got.refcount > 0)
8967 if (!htab->do_multi_toc && first_tlsld != NULL)
8969 ent->is_indirect = TRUE;
8970 ent->got.ent = first_tlsld;
8974 if (first_tlsld == NULL)
8976 s = ppc64_elf_tdata (ibfd)->got;
8977 ent->got.offset = s->size;
8982 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
8983 srel->size += sizeof (Elf64_External_Rela);
8988 ent->got.offset = (bfd_vma) -1;
8991 /* We now have determined the sizes of the various dynamic sections.
8992 Allocate memory for them. */
8994 for (s = dynobj->sections; s != NULL; s = s->next)
8996 if ((s->flags & SEC_LINKER_CREATED) == 0)
8999 if (s == htab->brlt || s == htab->relbrlt)
9000 /* These haven't been allocated yet; don't strip. */
9002 else if (s == htab->got
9006 || s == htab->dynbss)
9008 /* Strip this section if we don't need it; see the
9011 else if (CONST_STRNEQ (s->name, ".rela"))
9015 if (s != htab->relplt)
9018 /* We use the reloc_count field as a counter if we need
9019 to copy relocs into the output file. */
9025 /* It's not one of our sections, so don't allocate space. */
9031 /* If we don't need this section, strip it from the
9032 output file. This is mostly to handle .rela.bss and
9033 .rela.plt. We must create both sections in
9034 create_dynamic_sections, because they must be created
9035 before the linker maps input sections to output
9036 sections. The linker does that before
9037 adjust_dynamic_symbol is called, and it is that
9038 function which decides whether anything needs to go
9039 into these sections. */
9040 s->flags |= SEC_EXCLUDE;
9044 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9047 /* Allocate memory for the section contents. We use bfd_zalloc
9048 here in case unused entries are not reclaimed before the
9049 section's contents are written out. This should not happen,
9050 but this way if it does we get a R_PPC64_NONE reloc in .rela
9051 sections instead of garbage.
9052 We also rely on the section contents being zero when writing
9054 s->contents = bfd_zalloc (dynobj, s->size);
9055 if (s->contents == NULL)
9059 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9061 if (!is_ppc64_elf (ibfd))
9064 s = ppc64_elf_tdata (ibfd)->got;
9065 if (s != NULL && s != htab->got)
9068 s->flags |= SEC_EXCLUDE;
9071 s->contents = bfd_zalloc (ibfd, s->size);
9072 if (s->contents == NULL)
9076 s = ppc64_elf_tdata (ibfd)->relgot;
9080 s->flags |= SEC_EXCLUDE;
9083 s->contents = bfd_zalloc (ibfd, s->size);
9084 if (s->contents == NULL)
9092 if (htab->elf.dynamic_sections_created)
9094 /* Add some entries to the .dynamic section. We fill in the
9095 values later, in ppc64_elf_finish_dynamic_sections, but we
9096 must add the entries now so that we get the correct size for
9097 the .dynamic section. The DT_DEBUG entry is filled in by the
9098 dynamic linker and used by the debugger. */
9099 #define add_dynamic_entry(TAG, VAL) \
9100 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9102 if (info->executable)
9104 if (!add_dynamic_entry (DT_DEBUG, 0))
9108 if (htab->plt != NULL && htab->plt->size != 0)
9110 if (!add_dynamic_entry (DT_PLTGOT, 0)
9111 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9112 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9113 || !add_dynamic_entry (DT_JMPREL, 0)
9114 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9120 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9121 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9125 if (!htab->no_tls_get_addr_opt
9126 && htab->tls_get_addr_fd != NULL
9127 && htab->tls_get_addr_fd->elf.plt.plist != NULL
9128 && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9133 if (!add_dynamic_entry (DT_RELA, 0)
9134 || !add_dynamic_entry (DT_RELASZ, 0)
9135 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9138 /* If any dynamic relocs apply to a read-only section,
9139 then we need a DT_TEXTREL entry. */
9140 if ((info->flags & DF_TEXTREL) == 0)
9141 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
9143 if ((info->flags & DF_TEXTREL) != 0)
9145 if (!add_dynamic_entry (DT_TEXTREL, 0))
9150 #undef add_dynamic_entry
9155 /* Determine the type of stub needed, if any, for a call. */
9157 static inline enum ppc_stub_type
9158 ppc_type_of_stub (asection *input_sec,
9159 const Elf_Internal_Rela *rel,
9160 struct ppc_link_hash_entry **hash,
9161 struct plt_entry **plt_ent,
9162 bfd_vma destination)
9164 struct ppc_link_hash_entry *h = *hash;
9166 bfd_vma branch_offset;
9167 bfd_vma max_branch_offset;
9168 enum elf_ppc64_reloc_type r_type;
9172 struct plt_entry *ent;
9173 struct ppc_link_hash_entry *fdh = h;
9175 && h->oh->is_func_descriptor)
9177 fdh = ppc_follow_link (h->oh);
9181 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9182 if (ent->addend == rel->r_addend
9183 && ent->plt.offset != (bfd_vma) -1)
9186 return ppc_stub_plt_call;
9189 /* Here, we know we don't have a plt entry. If we don't have a
9190 either a defined function descriptor or a defined entry symbol
9191 in a regular object file, then it is pointless trying to make
9192 any other type of stub. */
9193 if (!is_static_defined (&fdh->elf)
9194 && !is_static_defined (&h->elf))
9195 return ppc_stub_none;
9197 else if (elf_local_got_ents (input_sec->owner) != NULL)
9199 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9200 struct plt_entry **local_plt = (struct plt_entry **)
9201 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9202 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9204 if (local_plt[r_symndx] != NULL)
9206 struct plt_entry *ent;
9208 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9209 if (ent->addend == rel->r_addend
9210 && ent->plt.offset != (bfd_vma) -1)
9213 return ppc_stub_plt_call;
9218 /* Determine where the call point is. */
9219 location = (input_sec->output_offset
9220 + input_sec->output_section->vma
9223 branch_offset = destination - location;
9224 r_type = ELF64_R_TYPE (rel->r_info);
9226 /* Determine if a long branch stub is needed. */
9227 max_branch_offset = 1 << 25;
9228 if (r_type != R_PPC64_REL24)
9229 max_branch_offset = 1 << 15;
9231 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
9232 /* We need a stub. Figure out whether a long_branch or plt_branch
9234 return ppc_stub_long_branch;
9236 return ppc_stub_none;
9239 /* Build a .plt call stub. */
9241 static inline bfd_byte *
9242 build_plt_stub (bfd *obfd, bfd_byte *p, int offset, Elf_Internal_Rela *r)
9244 #define PPC_LO(v) ((v) & 0xffff)
9245 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9246 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9248 if (PPC_HA (offset) != 0)
9252 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9253 r[1].r_offset = r[0].r_offset + 8;
9254 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9255 r[1].r_addend = r[0].r_addend;
9256 if (PPC_HA (offset + 16) != PPC_HA (offset))
9258 r[2].r_offset = r[1].r_offset + 4;
9259 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9260 r[2].r_addend = r[0].r_addend;
9264 r[2].r_offset = r[1].r_offset + 8;
9265 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9266 r[2].r_addend = r[0].r_addend + 8;
9267 r[3].r_offset = r[2].r_offset + 4;
9268 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9269 r[3].r_addend = r[0].r_addend + 16;
9272 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
9273 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9274 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
9275 if (PPC_HA (offset + 16) != PPC_HA (offset))
9277 bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p), p += 4;
9280 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9281 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p), p += 4;
9282 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
9283 bfd_put_32 (obfd, BCTR, p), p += 4;
9290 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9291 if (PPC_HA (offset + 16) != PPC_HA (offset))
9293 r[1].r_offset = r[0].r_offset + 4;
9294 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9295 r[1].r_addend = r[0].r_addend;
9299 r[1].r_offset = r[0].r_offset + 8;
9300 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9301 r[1].r_addend = r[0].r_addend + 16;
9302 r[2].r_offset = r[1].r_offset + 4;
9303 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9304 r[2].r_addend = r[0].r_addend + 8;
9307 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9308 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p), p += 4;
9309 if (PPC_HA (offset + 16) != PPC_HA (offset))
9311 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
9314 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9315 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
9316 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
9317 bfd_put_32 (obfd, BCTR, p), p += 4;
9322 /* Build a special .plt call stub for __tls_get_addr. */
9324 #define LD_R11_0R3 0xe9630000
9325 #define LD_R12_0R3 0xe9830000
9326 #define MR_R0_R3 0x7c601b78
9327 #define CMPDI_R11_0 0x2c2b0000
9328 #define ADD_R3_R12_R13 0x7c6c6a14
9329 #define BEQLR 0x4d820020
9330 #define MR_R3_R0 0x7c030378
9331 #define MFLR_R11 0x7d6802a6
9332 #define STD_R11_0R1 0xf9610000
9333 #define BCTRL 0x4e800421
9334 #define LD_R11_0R1 0xe9610000
9335 #define LD_R2_0R1 0xe8410000
9336 #define MTLR_R11 0x7d6803a6
9338 static inline bfd_byte *
9339 build_tls_get_addr_stub (bfd *obfd, bfd_byte *p, int offset,
9340 Elf_Internal_Rela *r)
9342 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
9343 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
9344 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
9345 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
9346 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
9347 bfd_put_32 (obfd, BEQLR, p), p += 4;
9348 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9349 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
9350 bfd_put_32 (obfd, STD_R11_0R1 + 32, p), p += 4;
9353 r[0].r_offset += 9 * 4;
9354 p = build_plt_stub (obfd, p, offset, r);
9355 bfd_put_32 (obfd, BCTRL, p - 4);
9357 bfd_put_32 (obfd, LD_R11_0R1 + 32, p), p += 4;
9358 bfd_put_32 (obfd, LD_R2_0R1 + 40, p), p += 4;
9359 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
9360 bfd_put_32 (obfd, BLR, p), p += 4;
9365 static Elf_Internal_Rela *
9366 get_relocs (asection *sec, int count)
9368 Elf_Internal_Rela *relocs;
9369 struct bfd_elf_section_data *elfsec_data;
9371 elfsec_data = elf_section_data (sec);
9372 relocs = elfsec_data->relocs;
9375 bfd_size_type relsize;
9376 relsize = sec->reloc_count * sizeof (*relocs);
9377 relocs = bfd_alloc (sec->owner, relsize);
9380 elfsec_data->relocs = relocs;
9381 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9382 sizeof (Elf_Internal_Shdr));
9383 if (elfsec_data->rela.hdr == NULL)
9385 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9386 * sizeof (Elf64_External_Rela));
9387 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
9388 sec->reloc_count = 0;
9390 relocs += sec->reloc_count;
9391 sec->reloc_count += count;
9396 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
9398 struct ppc_stub_hash_entry *stub_entry;
9399 struct ppc_branch_hash_entry *br_entry;
9400 struct bfd_link_info *info;
9401 struct ppc_link_hash_table *htab;
9406 Elf_Internal_Rela *r;
9409 /* Massage our args to the form they really have. */
9410 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
9413 htab = ppc_hash_table (info);
9417 /* Make a note of the offset within the stubs for this entry. */
9418 stub_entry->stub_offset = stub_entry->stub_sec->size;
9419 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
9421 htab->stub_count[stub_entry->stub_type - 1] += 1;
9422 switch (stub_entry->stub_type)
9424 case ppc_stub_long_branch:
9425 case ppc_stub_long_branch_r2off:
9426 /* Branches are relative. This is where we are going to. */
9427 off = dest = (stub_entry->target_value
9428 + stub_entry->target_section->output_offset
9429 + stub_entry->target_section->output_section->vma);
9431 /* And this is where we are coming from. */
9432 off -= (stub_entry->stub_offset
9433 + stub_entry->stub_sec->output_offset
9434 + stub_entry->stub_sec->output_section->vma);
9437 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9441 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
9442 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9443 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
9446 if (PPC_HA (r2off) != 0)
9449 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9452 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
9456 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
9458 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9460 (*_bfd_error_handler) (_("long branch stub `%s' offset overflow"),
9461 stub_entry->root.string);
9462 htab->stub_error = TRUE;
9466 if (info->emitrelocations)
9468 r = get_relocs (stub_entry->stub_sec, 1);
9471 r->r_offset = loc - stub_entry->stub_sec->contents;
9472 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
9474 if (stub_entry->h != NULL)
9476 struct elf_link_hash_entry **hashes;
9477 unsigned long symndx;
9478 struct ppc_link_hash_entry *h;
9480 hashes = elf_sym_hashes (htab->stub_bfd);
9483 bfd_size_type hsize;
9485 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
9486 hashes = bfd_zalloc (htab->stub_bfd, hsize);
9489 elf_sym_hashes (htab->stub_bfd) = hashes;
9490 htab->stub_globals = 1;
9492 symndx = htab->stub_globals++;
9494 hashes[symndx] = &h->elf;
9495 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
9496 if (h->oh != NULL && h->oh->is_func)
9497 h = ppc_follow_link (h->oh);
9498 if (h->elf.root.u.def.section != stub_entry->target_section)
9499 /* H is an opd symbol. The addend must be zero. */
9503 off = (h->elf.root.u.def.value
9504 + h->elf.root.u.def.section->output_offset
9505 + h->elf.root.u.def.section->output_section->vma);
9512 case ppc_stub_plt_branch:
9513 case ppc_stub_plt_branch_r2off:
9514 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9515 stub_entry->root.string + 9,
9517 if (br_entry == NULL)
9519 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
9520 stub_entry->root.string);
9521 htab->stub_error = TRUE;
9525 dest = (stub_entry->target_value
9526 + stub_entry->target_section->output_offset
9527 + stub_entry->target_section->output_section->vma);
9529 bfd_put_64 (htab->brlt->owner, dest,
9530 htab->brlt->contents + br_entry->offset);
9532 if (br_entry->iter == htab->stub_iteration)
9536 if (htab->relbrlt != NULL)
9538 /* Create a reloc for the branch lookup table entry. */
9539 Elf_Internal_Rela rela;
9542 rela.r_offset = (br_entry->offset
9543 + htab->brlt->output_offset
9544 + htab->brlt->output_section->vma);
9545 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9546 rela.r_addend = dest;
9548 rl = htab->relbrlt->contents;
9549 rl += (htab->relbrlt->reloc_count++
9550 * sizeof (Elf64_External_Rela));
9551 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
9553 else if (info->emitrelocations)
9555 r = get_relocs (htab->brlt, 1);
9558 /* brlt, being SEC_LINKER_CREATED does not go through the
9559 normal reloc processing. Symbols and offsets are not
9560 translated from input file to output file form, so
9561 set up the offset per the output file. */
9562 r->r_offset = (br_entry->offset
9563 + htab->brlt->output_offset
9564 + htab->brlt->output_section->vma);
9565 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9570 dest = (br_entry->offset
9571 + htab->brlt->output_offset
9572 + htab->brlt->output_section->vma);
9575 - elf_gp (htab->brlt->output_section->owner)
9576 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9578 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
9580 (*_bfd_error_handler)
9581 (_("linkage table error against `%s'"),
9582 stub_entry->root.string);
9583 bfd_set_error (bfd_error_bad_value);
9584 htab->stub_error = TRUE;
9588 if (info->emitrelocations)
9590 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
9593 r[0].r_offset = loc - stub_entry->stub_sec->contents;
9594 if (bfd_big_endian (info->output_bfd))
9596 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
9598 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9599 r[0].r_addend = dest;
9600 if (PPC_HA (off) != 0)
9602 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9603 r[1].r_offset = r[0].r_offset + 4;
9604 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9605 r[1].r_addend = r[0].r_addend;
9609 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9611 if (PPC_HA (off) != 0)
9614 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
9616 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
9621 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
9628 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
9629 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9630 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
9633 if (PPC_HA (off) != 0)
9636 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
9638 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
9643 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
9647 if (PPC_HA (r2off) != 0)
9650 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9653 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
9656 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
9658 bfd_put_32 (htab->stub_bfd, BCTR, loc);
9661 case ppc_stub_plt_call:
9662 if (stub_entry->h != NULL
9663 && stub_entry->h->is_func_descriptor
9664 && stub_entry->h->oh != NULL)
9666 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
9668 /* If the old-ABI "dot-symbol" is undefined make it weak so
9669 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
9670 FIXME: We used to define the symbol on one of the call
9671 stubs instead, which is why we test symbol section id
9672 against htab->top_id in various places. Likely all
9673 these checks could now disappear. */
9674 if (fh->elf.root.type == bfd_link_hash_undefined)
9675 fh->elf.root.type = bfd_link_hash_undefweak;
9678 /* Now build the stub. */
9679 dest = stub_entry->plt_ent->plt.offset & ~1;
9680 if (dest >= (bfd_vma) -2)
9684 if (!htab->elf.dynamic_sections_created
9685 || stub_entry->h == NULL
9686 || stub_entry->h->elf.dynindx == -1)
9689 dest += plt->output_offset + plt->output_section->vma;
9691 if (stub_entry->h == NULL
9692 && (stub_entry->plt_ent->plt.offset & 1) == 0)
9694 Elf_Internal_Rela rela;
9697 rela.r_offset = dest;
9698 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
9699 rela.r_addend = (stub_entry->target_value
9700 + stub_entry->target_section->output_offset
9701 + stub_entry->target_section->output_section->vma);
9703 rl = (htab->reliplt->contents
9704 + (htab->reliplt->reloc_count++
9705 * sizeof (Elf64_External_Rela)));
9706 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
9707 stub_entry->plt_ent->plt.offset |= 1;
9711 - elf_gp (plt->output_section->owner)
9712 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9714 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
9716 (*_bfd_error_handler)
9717 (_("linkage table error against `%s'"),
9718 stub_entry->h != NULL
9719 ? stub_entry->h->elf.root.root.string
9721 bfd_set_error (bfd_error_bad_value);
9722 htab->stub_error = TRUE;
9727 if (info->emitrelocations)
9729 r = get_relocs (stub_entry->stub_sec,
9730 (2 + (PPC_HA (off) != 0)
9731 + (PPC_HA (off + 16) == PPC_HA (off))));
9734 r[0].r_offset = loc - stub_entry->stub_sec->contents;
9735 if (bfd_big_endian (info->output_bfd))
9737 r[0].r_addend = dest;
9739 if (stub_entry->h != NULL
9740 && (stub_entry->h == htab->tls_get_addr_fd
9741 || stub_entry->h == htab->tls_get_addr)
9742 && !htab->no_tls_get_addr_opt)
9743 p = build_tls_get_addr_stub (htab->stub_bfd, loc, off, r);
9745 p = build_plt_stub (htab->stub_bfd, loc, off, r);
9754 stub_entry->stub_sec->size += size;
9756 if (htab->emit_stub_syms)
9758 struct elf_link_hash_entry *h;
9761 const char *const stub_str[] = { "long_branch",
9762 "long_branch_r2off",
9767 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
9768 len2 = strlen (stub_entry->root.string);
9769 name = bfd_malloc (len1 + len2 + 2);
9772 memcpy (name, stub_entry->root.string, 9);
9773 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
9774 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
9775 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
9778 if (h->root.type == bfd_link_hash_new)
9780 h->root.type = bfd_link_hash_defined;
9781 h->root.u.def.section = stub_entry->stub_sec;
9782 h->root.u.def.value = stub_entry->stub_offset;
9785 h->ref_regular_nonweak = 1;
9786 h->forced_local = 1;
9794 /* As above, but don't actually build the stub. Just bump offset so
9795 we know stub section sizes, and select plt_branch stubs where
9796 long_branch stubs won't do. */
9799 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
9801 struct ppc_stub_hash_entry *stub_entry;
9802 struct bfd_link_info *info;
9803 struct ppc_link_hash_table *htab;
9807 /* Massage our args to the form they really have. */
9808 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
9811 htab = ppc_hash_table (info);
9815 if (stub_entry->stub_type == ppc_stub_plt_call)
9818 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
9819 if (off >= (bfd_vma) -2)
9822 if (!htab->elf.dynamic_sections_created
9823 || stub_entry->h == NULL
9824 || stub_entry->h->elf.dynindx == -1)
9826 off += (plt->output_offset
9827 + plt->output_section->vma
9828 - elf_gp (plt->output_section->owner)
9829 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9831 size = PLT_CALL_STUB_SIZE;
9832 if (PPC_HA (off) == 0)
9834 if (PPC_HA (off + 16) != PPC_HA (off))
9836 if (stub_entry->h != NULL
9837 && (stub_entry->h == htab->tls_get_addr_fd
9838 || stub_entry->h == htab->tls_get_addr)
9839 && !htab->no_tls_get_addr_opt)
9841 if (info->emitrelocations)
9843 stub_entry->stub_sec->reloc_count
9844 += 2 + (PPC_HA (off) != 0) + (PPC_HA (off + 16) == PPC_HA (off));
9845 stub_entry->stub_sec->flags |= SEC_RELOC;
9850 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
9854 off = (stub_entry->target_value
9855 + stub_entry->target_section->output_offset
9856 + stub_entry->target_section->output_section->vma);
9857 off -= (stub_entry->stub_sec->size
9858 + stub_entry->stub_sec->output_offset
9859 + stub_entry->stub_sec->output_section->vma);
9861 /* Reset the stub type from the plt variant in case we now
9862 can reach with a shorter stub. */
9863 if (stub_entry->stub_type >= ppc_stub_plt_branch)
9864 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
9867 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9869 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
9870 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9872 if (PPC_HA (r2off) != 0)
9877 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
9878 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9880 struct ppc_branch_hash_entry *br_entry;
9882 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9883 stub_entry->root.string + 9,
9885 if (br_entry == NULL)
9887 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
9888 stub_entry->root.string);
9889 htab->stub_error = TRUE;
9893 if (br_entry->iter != htab->stub_iteration)
9895 br_entry->iter = htab->stub_iteration;
9896 br_entry->offset = htab->brlt->size;
9897 htab->brlt->size += 8;
9899 if (htab->relbrlt != NULL)
9900 htab->relbrlt->size += sizeof (Elf64_External_Rela);
9901 else if (info->emitrelocations)
9903 htab->brlt->reloc_count += 1;
9904 htab->brlt->flags |= SEC_RELOC;
9908 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
9909 off = (br_entry->offset
9910 + htab->brlt->output_offset
9911 + htab->brlt->output_section->vma
9912 - elf_gp (htab->brlt->output_section->owner)
9913 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9915 if (info->emitrelocations)
9917 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
9918 stub_entry->stub_sec->flags |= SEC_RELOC;
9921 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9924 if (PPC_HA (off) != 0)
9930 if (PPC_HA (off) != 0)
9933 if (PPC_HA (r2off) != 0)
9937 else if (info->emitrelocations)
9939 stub_entry->stub_sec->reloc_count += 1;
9940 stub_entry->stub_sec->flags |= SEC_RELOC;
9944 stub_entry->stub_sec->size += size;
9948 /* Set up various things so that we can make a list of input sections
9949 for each output section included in the link. Returns -1 on error,
9950 0 when no stubs will be needed, and 1 on success. */
9953 ppc64_elf_setup_section_lists
9954 (struct bfd_link_info *info,
9955 asection *(*add_stub_section) (const char *, asection *),
9956 void (*layout_sections_again) (void))
9959 int top_id, top_index, id;
9961 asection **input_list;
9963 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9967 /* Stash our params away. */
9968 htab->add_stub_section = add_stub_section;
9969 htab->layout_sections_again = layout_sections_again;
9971 if (htab->brlt == NULL)
9974 /* Find the top input section id. */
9975 for (input_bfd = info->input_bfds, top_id = 3;
9977 input_bfd = input_bfd->link_next)
9979 for (section = input_bfd->sections;
9981 section = section->next)
9983 if (top_id < section->id)
9984 top_id = section->id;
9988 htab->top_id = top_id;
9989 amt = sizeof (struct map_stub) * (top_id + 1);
9990 htab->stub_group = bfd_zmalloc (amt);
9991 if (htab->stub_group == NULL)
9994 /* Set toc_off for com, und, abs and ind sections. */
9995 for (id = 0; id < 3; id++)
9996 htab->stub_group[id].toc_off = TOC_BASE_OFF;
9998 /* We can't use output_bfd->section_count here to find the top output
9999 section index as some sections may have been removed, and
10000 strip_excluded_output_sections doesn't renumber the indices. */
10001 for (section = info->output_bfd->sections, top_index = 0;
10003 section = section->next)
10005 if (top_index < section->index)
10006 top_index = section->index;
10009 htab->top_index = top_index;
10010 amt = sizeof (asection *) * (top_index + 1);
10011 input_list = bfd_zmalloc (amt);
10012 htab->input_list = input_list;
10013 if (input_list == NULL)
10019 /* Set up for first pass at multitoc partitioning. */
10022 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10024 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10026 elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
10027 htab->toc_curr = elf_gp (info->output_bfd);
10028 htab->toc_bfd = NULL;
10029 htab->toc_first_sec = NULL;
10032 /* The linker repeatedly calls this function for each TOC input section
10033 and linker generated GOT section. Group input bfds such that the toc
10034 within a group is less than 64k in size. */
10037 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10039 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10040 bfd_vma addr, off, limit;
10045 if (!htab->second_toc_pass)
10047 /* Keep track of the first .toc or .got section for this input bfd. */
10048 if (htab->toc_bfd != isec->owner)
10050 htab->toc_bfd = isec->owner;
10051 htab->toc_first_sec = isec;
10054 addr = isec->output_offset + isec->output_section->vma;
10055 off = addr - htab->toc_curr;
10056 limit = 0x80008000;
10057 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10059 if (off + isec->size > limit)
10061 addr = (htab->toc_first_sec->output_offset
10062 + htab->toc_first_sec->output_section->vma);
10063 htab->toc_curr = addr;
10066 /* toc_curr is the base address of this toc group. Set elf_gp
10067 for the input section to be the offset relative to the
10068 output toc base plus 0x8000. Making the input elf_gp an
10069 offset allows us to move the toc as a whole without
10070 recalculating input elf_gp. */
10071 off = htab->toc_curr - elf_gp (isec->output_section->owner);
10072 off += TOC_BASE_OFF;
10074 /* Die if someone uses a linker script that doesn't keep input
10075 file .toc and .got together. */
10076 if (elf_gp (isec->owner) != 0
10077 && elf_gp (isec->owner) != off)
10080 elf_gp (isec->owner) = off;
10084 /* During the second pass toc_first_sec points to the start of
10085 a toc group, and toc_curr is used to track the old elf_gp.
10086 We use toc_bfd to ensure we only look at each bfd once. */
10087 if (htab->toc_bfd == isec->owner)
10089 htab->toc_bfd = isec->owner;
10091 if (htab->toc_first_sec == NULL
10092 || htab->toc_curr != elf_gp (isec->owner))
10094 htab->toc_curr = elf_gp (isec->owner);
10095 htab->toc_first_sec = isec;
10097 addr = (htab->toc_first_sec->output_offset
10098 + htab->toc_first_sec->output_section->vma);
10099 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10100 elf_gp (isec->owner) = off;
10105 /* Called via elf_link_hash_traverse to merge GOT entries for global
10109 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10111 if (h->root.type == bfd_link_hash_indirect)
10114 if (h->root.type == bfd_link_hash_warning)
10115 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10117 merge_got_entries (&h->got.glist);
10122 /* Called via elf_link_hash_traverse to allocate GOT entries for global
10126 reallocate_got (struct elf_link_hash_entry *h, void *inf)
10128 struct got_entry *gent;
10130 if (h->root.type == bfd_link_hash_indirect)
10133 if (h->root.type == bfd_link_hash_warning)
10134 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10136 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10137 if (!gent->is_indirect)
10138 allocate_got (h, (struct bfd_link_info *) inf, gent);
10142 /* Called on the first multitoc pass after the last call to
10143 ppc64_elf_next_toc_section. This function removes duplicate GOT
10147 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
10149 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10150 struct bfd *ibfd, *ibfd2;
10151 bfd_boolean done_something;
10153 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
10155 if (!htab->do_multi_toc)
10158 /* Merge global sym got entries within a toc group. */
10159 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10161 /* And tlsld_got. */
10162 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10164 struct got_entry *ent, *ent2;
10166 if (!is_ppc64_elf (ibfd))
10169 ent = ppc64_tlsld_got (ibfd);
10170 if (!ent->is_indirect
10171 && ent->got.offset != (bfd_vma) -1)
10173 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
10175 if (!is_ppc64_elf (ibfd2))
10178 ent2 = ppc64_tlsld_got (ibfd2);
10179 if (!ent2->is_indirect
10180 && ent2->got.offset != (bfd_vma) -1
10181 && elf_gp (ibfd2) == elf_gp (ibfd))
10183 ent2->is_indirect = TRUE;
10184 ent2->got.ent = ent;
10190 /* Zap sizes of got sections. */
10191 htab->reliplt->rawsize = htab->reliplt->size;
10192 htab->reliplt->size -= htab->got_reli_size;
10193 htab->got_reli_size = 0;
10195 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10197 asection *got, *relgot;
10199 if (!is_ppc64_elf (ibfd))
10202 got = ppc64_elf_tdata (ibfd)->got;
10205 got->rawsize = got->size;
10207 relgot = ppc64_elf_tdata (ibfd)->relgot;
10208 relgot->rawsize = relgot->size;
10213 /* Now reallocate the got, local syms first. We don't need to
10214 allocate section contents again since we never increase size. */
10215 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10217 struct got_entry **lgot_ents;
10218 struct got_entry **end_lgot_ents;
10219 struct plt_entry **local_plt;
10220 struct plt_entry **end_local_plt;
10221 unsigned char *lgot_masks;
10222 bfd_size_type locsymcount;
10223 Elf_Internal_Shdr *symtab_hdr;
10224 asection *s, *srel;
10226 if (!is_ppc64_elf (ibfd))
10229 lgot_ents = elf_local_got_ents (ibfd);
10233 symtab_hdr = &elf_symtab_hdr (ibfd);
10234 locsymcount = symtab_hdr->sh_info;
10235 end_lgot_ents = lgot_ents + locsymcount;
10236 local_plt = (struct plt_entry **) end_lgot_ents;
10237 end_local_plt = local_plt + locsymcount;
10238 lgot_masks = (unsigned char *) end_local_plt;
10239 s = ppc64_elf_tdata (ibfd)->got;
10240 srel = ppc64_elf_tdata (ibfd)->relgot;
10241 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10243 struct got_entry *ent;
10245 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
10247 unsigned int num = 1;
10248 ent->got.offset = s->size;
10249 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10251 s->size += num * 8;
10253 srel->size += num * sizeof (Elf64_External_Rela);
10254 else if ((*lgot_masks & PLT_IFUNC) != 0)
10256 htab->reliplt->size
10257 += num * sizeof (Elf64_External_Rela);
10258 htab->got_reli_size
10259 += num * sizeof (Elf64_External_Rela);
10265 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
10267 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10269 struct got_entry *ent;
10271 if (!is_ppc64_elf (ibfd))
10274 ent = ppc64_tlsld_got (ibfd);
10275 if (!ent->is_indirect
10276 && ent->got.offset != (bfd_vma) -1)
10278 asection *s = ppc64_elf_tdata (ibfd)->got;
10279 ent->got.offset = s->size;
10283 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10284 srel->size += sizeof (Elf64_External_Rela);
10289 done_something = htab->reliplt->rawsize != htab->reliplt->size;
10290 if (!done_something)
10291 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10295 if (!is_ppc64_elf (ibfd))
10298 got = ppc64_elf_tdata (ibfd)->got;
10301 done_something = got->rawsize != got->size;
10302 if (done_something)
10307 if (done_something)
10308 (*htab->layout_sections_again) ();
10310 /* Set up for second pass over toc sections to recalculate elf_gp
10311 on input sections. */
10312 htab->toc_bfd = NULL;
10313 htab->toc_first_sec = NULL;
10314 htab->second_toc_pass = TRUE;
10315 return done_something;
10318 /* Called after second pass of multitoc partitioning. */
10321 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
10323 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10325 /* After the second pass, toc_curr tracks the TOC offset used
10326 for code sections below in ppc64_elf_next_input_section. */
10327 htab->toc_curr = TOC_BASE_OFF;
10330 /* No toc references were found in ISEC. If the code in ISEC makes no
10331 calls, then there's no need to use toc adjusting stubs when branching
10332 into ISEC. Actually, indirect calls from ISEC are OK as they will
10333 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
10334 needed, and 2 if a cyclical call-graph was found but no other reason
10335 for a stub was detected. If called from the top level, a return of
10336 2 means the same as a return of 0. */
10339 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
10343 /* Mark this section as checked. */
10344 isec->call_check_done = 1;
10346 /* We know none of our code bearing sections will need toc stubs. */
10347 if ((isec->flags & SEC_LINKER_CREATED) != 0)
10350 if (isec->size == 0)
10353 if (isec->output_section == NULL)
10357 if (isec->reloc_count != 0)
10359 Elf_Internal_Rela *relstart, *rel;
10360 Elf_Internal_Sym *local_syms;
10361 struct ppc_link_hash_table *htab;
10363 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
10364 info->keep_memory);
10365 if (relstart == NULL)
10368 /* Look for branches to outside of this section. */
10370 htab = ppc_hash_table (info);
10374 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
10376 enum elf_ppc64_reloc_type r_type;
10377 unsigned long r_symndx;
10378 struct elf_link_hash_entry *h;
10379 struct ppc_link_hash_entry *eh;
10380 Elf_Internal_Sym *sym;
10382 struct _opd_sec_data *opd;
10386 r_type = ELF64_R_TYPE (rel->r_info);
10387 if (r_type != R_PPC64_REL24
10388 && r_type != R_PPC64_REL14
10389 && r_type != R_PPC64_REL14_BRTAKEN
10390 && r_type != R_PPC64_REL14_BRNTAKEN)
10393 r_symndx = ELF64_R_SYM (rel->r_info);
10394 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
10401 /* Calls to dynamic lib functions go through a plt call stub
10403 eh = (struct ppc_link_hash_entry *) h;
10405 && (eh->elf.plt.plist != NULL
10407 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
10413 if (sym_sec == NULL)
10414 /* Ignore other undefined symbols. */
10417 /* Assume branches to other sections not included in the
10418 link need stubs too, to cover -R and absolute syms. */
10419 if (sym_sec->output_section == NULL)
10426 sym_value = sym->st_value;
10429 if (h->root.type != bfd_link_hash_defined
10430 && h->root.type != bfd_link_hash_defweak)
10432 sym_value = h->root.u.def.value;
10434 sym_value += rel->r_addend;
10436 /* If this branch reloc uses an opd sym, find the code section. */
10437 opd = get_opd_info (sym_sec);
10440 if (h == NULL && opd->adjust != NULL)
10444 adjust = opd->adjust[sym->st_value / 8];
10446 /* Assume deleted functions won't ever be called. */
10448 sym_value += adjust;
10451 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
10452 if (dest == (bfd_vma) -1)
10457 + sym_sec->output_offset
10458 + sym_sec->output_section->vma);
10460 /* Ignore branch to self. */
10461 if (sym_sec == isec)
10464 /* If the called function uses the toc, we need a stub. */
10465 if (sym_sec->has_toc_reloc
10466 || sym_sec->makes_toc_func_call)
10472 /* Assume any branch that needs a long branch stub might in fact
10473 need a plt_branch stub. A plt_branch stub uses r2. */
10474 else if (dest - (isec->output_offset
10475 + isec->output_section->vma
10476 + rel->r_offset) + (1 << 25) >= (2 << 25))
10482 /* If calling back to a section in the process of being
10483 tested, we can't say for sure that no toc adjusting stubs
10484 are needed, so don't return zero. */
10485 else if (sym_sec->call_check_in_progress)
10488 /* Branches to another section that itself doesn't have any TOC
10489 references are OK. Recursively call ourselves to check. */
10490 else if (!sym_sec->call_check_done)
10494 /* Mark current section as indeterminate, so that other
10495 sections that call back to current won't be marked as
10497 isec->call_check_in_progress = 1;
10498 recur = toc_adjusting_stub_needed (info, sym_sec);
10499 isec->call_check_in_progress = 0;
10510 if (local_syms != NULL
10511 && (elf_symtab_hdr (isec->owner).contents
10512 != (unsigned char *) local_syms))
10514 if (elf_section_data (isec)->relocs != relstart)
10519 && isec->map_head.s != NULL
10520 && (strcmp (isec->output_section->name, ".init") == 0
10521 || strcmp (isec->output_section->name, ".fini") == 0))
10523 if (isec->map_head.s->has_toc_reloc
10524 || isec->map_head.s->makes_toc_func_call)
10526 else if (!isec->map_head.s->call_check_done)
10529 isec->call_check_in_progress = 1;
10530 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
10531 isec->call_check_in_progress = 0;
10538 isec->makes_toc_func_call = 1;
10543 /* The linker repeatedly calls this function for each input section,
10544 in the order that input sections are linked into output sections.
10545 Build lists of input sections to determine groupings between which
10546 we may insert linker stubs. */
10549 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
10551 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10556 if ((isec->output_section->flags & SEC_CODE) != 0
10557 && isec->output_section->index <= htab->top_index)
10559 asection **list = htab->input_list + isec->output_section->index;
10560 /* Steal the link_sec pointer for our list. */
10561 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
10562 /* This happens to make the list in reverse order,
10563 which is what we want. */
10564 PREV_SEC (isec) = *list;
10568 if (htab->multi_toc_needed)
10570 /* If a code section has a function that uses the TOC then we need
10571 to use the right TOC (obviously). Also, make sure that .opd gets
10572 the correct TOC value for R_PPC64_TOC relocs that don't have or
10573 can't find their function symbol (shouldn't ever happen now).
10574 Also specially treat .fixup for the linux kernel. .fixup
10575 contains branches, but only back to the function that hit an
10577 if (isec->has_toc_reloc
10578 || (isec->flags & SEC_CODE) == 0
10579 || strcmp (isec->name, ".fixup") == 0)
10581 if (elf_gp (isec->owner) != 0)
10582 htab->toc_curr = elf_gp (isec->owner);
10586 if (!isec->call_check_done
10587 && toc_adjusting_stub_needed (info, isec) < 0)
10589 /* If we make a local call from this section, ie. a branch
10590 without a following nop, then we have no place to put a
10591 toc restoring insn. We must use the same toc group as
10593 Testing makes_toc_func_call actually tests for *any*
10594 calls to functions that need a good toc pointer. A more
10595 precise test would be better, as this one will set
10596 incorrect values for pasted .init/.fini fragments.
10597 (Fixed later in check_pasted_section.) */
10598 if (isec->makes_toc_func_call
10599 && elf_gp (isec->owner) != 0)
10600 htab->toc_curr = elf_gp (isec->owner);
10604 /* Functions that don't use the TOC can belong in any TOC group.
10605 Use the last TOC base. */
10606 htab->stub_group[isec->id].toc_off = htab->toc_curr;
10610 /* Check that all .init and .fini sections use the same toc, if they
10611 have toc relocs. */
10614 check_pasted_section (struct bfd_link_info *info, const char *name)
10616 asection *o = bfd_get_section_by_name (info->output_bfd, name);
10620 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10621 bfd_vma toc_off = 0;
10624 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10625 if (i->has_toc_reloc)
10628 toc_off = htab->stub_group[i->id].toc_off;
10629 else if (toc_off != htab->stub_group[i->id].toc_off)
10634 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10635 if (i->makes_toc_func_call)
10637 toc_off = htab->stub_group[i->id].toc_off;
10641 /* Make sure the whole pasted function uses the same toc offset. */
10643 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10644 htab->stub_group[i->id].toc_off = toc_off;
10650 ppc64_elf_check_init_fini (struct bfd_link_info *info)
10652 return (check_pasted_section (info, ".init")
10653 & check_pasted_section (info, ".fini"));
10656 /* See whether we can group stub sections together. Grouping stub
10657 sections may result in fewer stubs. More importantly, we need to
10658 put all .init* and .fini* stubs at the beginning of the .init or
10659 .fini output sections respectively, because glibc splits the
10660 _init and _fini functions into multiple parts. Putting a stub in
10661 the middle of a function is not a good idea. */
10664 group_sections (struct ppc_link_hash_table *htab,
10665 bfd_size_type stub_group_size,
10666 bfd_boolean stubs_always_before_branch)
10669 bfd_size_type stub14_group_size;
10670 bfd_boolean suppress_size_errors;
10672 suppress_size_errors = FALSE;
10673 stub14_group_size = stub_group_size;
10674 if (stub_group_size == 1)
10676 /* Default values. */
10677 if (stubs_always_before_branch)
10679 stub_group_size = 0x1e00000;
10680 stub14_group_size = 0x7800;
10684 stub_group_size = 0x1c00000;
10685 stub14_group_size = 0x7000;
10687 suppress_size_errors = TRUE;
10690 list = htab->input_list + htab->top_index;
10693 asection *tail = *list;
10694 while (tail != NULL)
10698 bfd_size_type total;
10699 bfd_boolean big_sec;
10703 total = tail->size;
10704 big_sec = total > (ppc64_elf_section_data (tail) != NULL
10705 && ppc64_elf_section_data (tail)->has_14bit_branch
10706 ? stub14_group_size : stub_group_size);
10707 if (big_sec && !suppress_size_errors)
10708 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
10709 tail->owner, tail);
10710 curr_toc = htab->stub_group[tail->id].toc_off;
10712 while ((prev = PREV_SEC (curr)) != NULL
10713 && ((total += curr->output_offset - prev->output_offset)
10714 < (ppc64_elf_section_data (prev) != NULL
10715 && ppc64_elf_section_data (prev)->has_14bit_branch
10716 ? stub14_group_size : stub_group_size))
10717 && htab->stub_group[prev->id].toc_off == curr_toc)
10720 /* OK, the size from the start of CURR to the end is less
10721 than stub_group_size and thus can be handled by one stub
10722 section. (or the tail section is itself larger than
10723 stub_group_size, in which case we may be toast.) We
10724 should really be keeping track of the total size of stubs
10725 added here, as stubs contribute to the final output
10726 section size. That's a little tricky, and this way will
10727 only break if stubs added make the total size more than
10728 2^25, ie. for the default stub_group_size, if stubs total
10729 more than 2097152 bytes, or nearly 75000 plt call stubs. */
10732 prev = PREV_SEC (tail);
10733 /* Set up this stub group. */
10734 htab->stub_group[tail->id].link_sec = curr;
10736 while (tail != curr && (tail = prev) != NULL);
10738 /* But wait, there's more! Input sections up to stub_group_size
10739 bytes before the stub section can be handled by it too.
10740 Don't do this if we have a really large section after the
10741 stubs, as adding more stubs increases the chance that
10742 branches may not reach into the stub section. */
10743 if (!stubs_always_before_branch && !big_sec)
10746 while (prev != NULL
10747 && ((total += tail->output_offset - prev->output_offset)
10748 < (ppc64_elf_section_data (prev) != NULL
10749 && ppc64_elf_section_data (prev)->has_14bit_branch
10750 ? stub14_group_size : stub_group_size))
10751 && htab->stub_group[prev->id].toc_off == curr_toc)
10754 prev = PREV_SEC (tail);
10755 htab->stub_group[tail->id].link_sec = curr;
10761 while (list-- != htab->input_list);
10762 free (htab->input_list);
10766 /* Determine and set the size of the stub section for a final link.
10768 The basic idea here is to examine all the relocations looking for
10769 PC-relative calls to a target that is unreachable with a "bl"
10773 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size)
10775 bfd_size_type stub_group_size;
10776 bfd_boolean stubs_always_before_branch;
10777 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10782 stubs_always_before_branch = group_size < 0;
10783 if (group_size < 0)
10784 stub_group_size = -group_size;
10786 stub_group_size = group_size;
10788 group_sections (htab, stub_group_size, stubs_always_before_branch);
10793 unsigned int bfd_indx;
10794 asection *stub_sec;
10796 htab->stub_iteration += 1;
10798 for (input_bfd = info->input_bfds, bfd_indx = 0;
10800 input_bfd = input_bfd->link_next, bfd_indx++)
10802 Elf_Internal_Shdr *symtab_hdr;
10804 Elf_Internal_Sym *local_syms = NULL;
10806 if (!is_ppc64_elf (input_bfd))
10809 /* We'll need the symbol table in a second. */
10810 symtab_hdr = &elf_symtab_hdr (input_bfd);
10811 if (symtab_hdr->sh_info == 0)
10814 /* Walk over each section attached to the input bfd. */
10815 for (section = input_bfd->sections;
10817 section = section->next)
10819 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
10821 /* If there aren't any relocs, then there's nothing more
10823 if ((section->flags & SEC_RELOC) == 0
10824 || (section->flags & SEC_ALLOC) == 0
10825 || (section->flags & SEC_LOAD) == 0
10826 || (section->flags & SEC_CODE) == 0
10827 || section->reloc_count == 0)
10830 /* If this section is a link-once section that will be
10831 discarded, then don't create any stubs. */
10832 if (section->output_section == NULL
10833 || section->output_section->owner != info->output_bfd)
10836 /* Get the relocs. */
10838 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
10839 info->keep_memory);
10840 if (internal_relocs == NULL)
10841 goto error_ret_free_local;
10843 /* Now examine each relocation. */
10844 irela = internal_relocs;
10845 irelaend = irela + section->reloc_count;
10846 for (; irela < irelaend; irela++)
10848 enum elf_ppc64_reloc_type r_type;
10849 unsigned int r_indx;
10850 enum ppc_stub_type stub_type;
10851 struct ppc_stub_hash_entry *stub_entry;
10852 asection *sym_sec, *code_sec;
10853 bfd_vma sym_value, code_value;
10854 bfd_vma destination;
10855 bfd_boolean ok_dest;
10856 struct ppc_link_hash_entry *hash;
10857 struct ppc_link_hash_entry *fdh;
10858 struct elf_link_hash_entry *h;
10859 Elf_Internal_Sym *sym;
10861 const asection *id_sec;
10862 struct _opd_sec_data *opd;
10863 struct plt_entry *plt_ent;
10865 r_type = ELF64_R_TYPE (irela->r_info);
10866 r_indx = ELF64_R_SYM (irela->r_info);
10868 if (r_type >= R_PPC64_max)
10870 bfd_set_error (bfd_error_bad_value);
10871 goto error_ret_free_internal;
10874 /* Only look for stubs on branch instructions. */
10875 if (r_type != R_PPC64_REL24
10876 && r_type != R_PPC64_REL14
10877 && r_type != R_PPC64_REL14_BRTAKEN
10878 && r_type != R_PPC64_REL14_BRNTAKEN)
10881 /* Now determine the call target, its name, value,
10883 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
10884 r_indx, input_bfd))
10885 goto error_ret_free_internal;
10886 hash = (struct ppc_link_hash_entry *) h;
10893 sym_value = sym->st_value;
10896 else if (hash->elf.root.type == bfd_link_hash_defined
10897 || hash->elf.root.type == bfd_link_hash_defweak)
10899 sym_value = hash->elf.root.u.def.value;
10900 if (sym_sec->output_section != NULL)
10903 else if (hash->elf.root.type == bfd_link_hash_undefweak
10904 || hash->elf.root.type == bfd_link_hash_undefined)
10906 /* Recognise an old ABI func code entry sym, and
10907 use the func descriptor sym instead if it is
10909 if (hash->elf.root.root.string[0] == '.'
10910 && (fdh = lookup_fdh (hash, htab)) != NULL)
10912 if (fdh->elf.root.type == bfd_link_hash_defined
10913 || fdh->elf.root.type == bfd_link_hash_defweak)
10915 sym_sec = fdh->elf.root.u.def.section;
10916 sym_value = fdh->elf.root.u.def.value;
10917 if (sym_sec->output_section != NULL)
10926 bfd_set_error (bfd_error_bad_value);
10927 goto error_ret_free_internal;
10933 sym_value += irela->r_addend;
10934 destination = (sym_value
10935 + sym_sec->output_offset
10936 + sym_sec->output_section->vma);
10939 code_sec = sym_sec;
10940 code_value = sym_value;
10941 opd = get_opd_info (sym_sec);
10946 if (hash == NULL && opd->adjust != NULL)
10948 long adjust = opd->adjust[sym_value / 8];
10951 code_value += adjust;
10952 sym_value += adjust;
10954 dest = opd_entry_value (sym_sec, sym_value,
10955 &code_sec, &code_value);
10956 if (dest != (bfd_vma) -1)
10958 destination = dest;
10961 /* Fixup old ABI sym to point at code
10963 hash->elf.root.type = bfd_link_hash_defweak;
10964 hash->elf.root.u.def.section = code_sec;
10965 hash->elf.root.u.def.value = code_value;
10970 /* Determine what (if any) linker stub is needed. */
10972 stub_type = ppc_type_of_stub (section, irela, &hash,
10973 &plt_ent, destination);
10975 if (stub_type != ppc_stub_plt_call)
10977 /* Check whether we need a TOC adjusting stub.
10978 Since the linker pastes together pieces from
10979 different object files when creating the
10980 _init and _fini functions, it may be that a
10981 call to what looks like a local sym is in
10982 fact a call needing a TOC adjustment. */
10983 if (code_sec != NULL
10984 && code_sec->output_section != NULL
10985 && (htab->stub_group[code_sec->id].toc_off
10986 != htab->stub_group[section->id].toc_off)
10987 && (code_sec->has_toc_reloc
10988 || code_sec->makes_toc_func_call))
10989 stub_type = ppc_stub_long_branch_r2off;
10992 if (stub_type == ppc_stub_none)
10995 /* __tls_get_addr calls might be eliminated. */
10996 if (stub_type != ppc_stub_plt_call
10998 && (hash == htab->tls_get_addr
10999 || hash == htab->tls_get_addr_fd)
11000 && section->has_tls_reloc
11001 && irela != internal_relocs)
11003 /* Get tls info. */
11004 unsigned char *tls_mask;
11006 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
11007 irela - 1, input_bfd))
11008 goto error_ret_free_internal;
11009 if (*tls_mask != 0)
11013 /* Support for grouping stub sections. */
11014 id_sec = htab->stub_group[section->id].link_sec;
11016 /* Get the name of this stub. */
11017 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11019 goto error_ret_free_internal;
11021 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
11022 stub_name, FALSE, FALSE);
11023 if (stub_entry != NULL)
11025 /* The proper stub has already been created. */
11030 stub_entry = ppc_add_stub (stub_name, section, htab);
11031 if (stub_entry == NULL)
11034 error_ret_free_internal:
11035 if (elf_section_data (section)->relocs == NULL)
11036 free (internal_relocs);
11037 error_ret_free_local:
11038 if (local_syms != NULL
11039 && (symtab_hdr->contents
11040 != (unsigned char *) local_syms))
11045 stub_entry->stub_type = stub_type;
11046 if (stub_type != ppc_stub_plt_call)
11048 stub_entry->target_value = code_value;
11049 stub_entry->target_section = code_sec;
11053 stub_entry->target_value = sym_value;
11054 stub_entry->target_section = sym_sec;
11056 stub_entry->h = hash;
11057 stub_entry->plt_ent = plt_ent;
11058 stub_entry->addend = irela->r_addend;
11060 if (stub_entry->h != NULL)
11061 htab->stub_globals += 1;
11064 /* We're done with the internal relocs, free them. */
11065 if (elf_section_data (section)->relocs != internal_relocs)
11066 free (internal_relocs);
11069 if (local_syms != NULL
11070 && symtab_hdr->contents != (unsigned char *) local_syms)
11072 if (!info->keep_memory)
11075 symtab_hdr->contents = (unsigned char *) local_syms;
11079 /* We may have added some stubs. Find out the new size of the
11081 for (stub_sec = htab->stub_bfd->sections;
11083 stub_sec = stub_sec->next)
11084 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11086 stub_sec->rawsize = stub_sec->size;
11087 stub_sec->size = 0;
11088 stub_sec->reloc_count = 0;
11089 stub_sec->flags &= ~SEC_RELOC;
11092 htab->brlt->size = 0;
11093 htab->brlt->reloc_count = 0;
11094 htab->brlt->flags &= ~SEC_RELOC;
11095 if (htab->relbrlt != NULL)
11096 htab->relbrlt->size = 0;
11098 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
11100 if (info->emitrelocations
11101 && htab->glink != NULL && htab->glink->size != 0)
11103 htab->glink->reloc_count = 1;
11104 htab->glink->flags |= SEC_RELOC;
11107 for (stub_sec = htab->stub_bfd->sections;
11109 stub_sec = stub_sec->next)
11110 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11111 && stub_sec->rawsize != stub_sec->size)
11114 /* Exit from this loop when no stubs have been added, and no stubs
11115 have changed size. */
11116 if (stub_sec == NULL)
11119 /* Ask the linker to do its stuff. */
11120 (*htab->layout_sections_again) ();
11123 /* It would be nice to strip htab->brlt from the output if the
11124 section is empty, but it's too late. If we strip sections here,
11125 the dynamic symbol table is corrupted since the section symbol
11126 for the stripped section isn't written. */
11131 /* Called after we have determined section placement. If sections
11132 move, we'll be called again. Provide a value for TOCstart. */
11135 ppc64_elf_toc (bfd *obfd)
11140 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
11141 order. The TOC starts where the first of these sections starts. */
11142 s = bfd_get_section_by_name (obfd, ".got");
11143 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11144 s = bfd_get_section_by_name (obfd, ".toc");
11145 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11146 s = bfd_get_section_by_name (obfd, ".tocbss");
11147 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11148 s = bfd_get_section_by_name (obfd, ".plt");
11149 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11151 /* This may happen for
11152 o references to TOC base (SYM@toc / TOC[tc0]) without a
11154 o bad linker script
11155 o --gc-sections and empty TOC sections
11157 FIXME: Warn user? */
11159 /* Look for a likely section. We probably won't even be
11161 for (s = obfd->sections; s != NULL; s = s->next)
11162 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
11164 == (SEC_ALLOC | SEC_SMALL_DATA))
11167 for (s = obfd->sections; s != NULL; s = s->next)
11168 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
11169 == (SEC_ALLOC | SEC_SMALL_DATA))
11172 for (s = obfd->sections; s != NULL; s = s->next)
11173 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
11177 for (s = obfd->sections; s != NULL; s = s->next)
11178 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
11184 TOCstart = s->output_section->vma + s->output_offset;
11189 /* Build all the stubs associated with the current output file.
11190 The stubs are kept in a hash table attached to the main linker
11191 hash table. This function is called via gldelf64ppc_finish. */
11194 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
11195 struct bfd_link_info *info,
11198 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11199 asection *stub_sec;
11201 int stub_sec_count = 0;
11206 htab->emit_stub_syms = emit_stub_syms;
11208 /* Allocate memory to hold the linker stubs. */
11209 for (stub_sec = htab->stub_bfd->sections;
11211 stub_sec = stub_sec->next)
11212 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11213 && stub_sec->size != 0)
11215 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
11216 if (stub_sec->contents == NULL)
11218 /* We want to check that built size is the same as calculated
11219 size. rawsize is a convenient location to use. */
11220 stub_sec->rawsize = stub_sec->size;
11221 stub_sec->size = 0;
11224 if (htab->glink != NULL && htab->glink->size != 0)
11229 /* Build the .glink plt call stub. */
11230 if (htab->emit_stub_syms)
11232 struct elf_link_hash_entry *h;
11233 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
11234 TRUE, FALSE, FALSE);
11237 if (h->root.type == bfd_link_hash_new)
11239 h->root.type = bfd_link_hash_defined;
11240 h->root.u.def.section = htab->glink;
11241 h->root.u.def.value = 8;
11242 h->ref_regular = 1;
11243 h->def_regular = 1;
11244 h->ref_regular_nonweak = 1;
11245 h->forced_local = 1;
11249 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
11250 if (info->emitrelocations)
11252 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
11255 r->r_offset = (htab->glink->output_offset
11256 + htab->glink->output_section->vma);
11257 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
11258 r->r_addend = plt0;
11260 p = htab->glink->contents;
11261 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
11262 bfd_put_64 (htab->glink->owner, plt0, p);
11264 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
11266 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
11268 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
11270 bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
11272 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
11274 bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
11276 bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
11278 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
11280 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
11282 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
11284 bfd_put_32 (htab->glink->owner, BCTR, p);
11286 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
11288 bfd_put_32 (htab->glink->owner, NOP, p);
11292 /* Build the .glink lazy link call stubs. */
11294 while (p < htab->glink->contents + htab->glink->size)
11298 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
11303 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
11305 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
11308 bfd_put_32 (htab->glink->owner,
11309 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
11313 htab->glink->rawsize = p - htab->glink->contents;
11316 if (htab->brlt->size != 0)
11318 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
11320 if (htab->brlt->contents == NULL)
11323 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
11325 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
11326 htab->relbrlt->size);
11327 if (htab->relbrlt->contents == NULL)
11331 /* Build the stubs as directed by the stub hash table. */
11332 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
11334 if (htab->relbrlt != NULL)
11335 htab->relbrlt->reloc_count = 0;
11337 for (stub_sec = htab->stub_bfd->sections;
11339 stub_sec = stub_sec->next)
11340 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11342 stub_sec_count += 1;
11343 if (stub_sec->rawsize != stub_sec->size)
11347 if (stub_sec != NULL
11348 || htab->glink->rawsize != htab->glink->size)
11350 htab->stub_error = TRUE;
11351 (*_bfd_error_handler) (_("stubs don't match calculated size"));
11354 if (htab->stub_error)
11359 *stats = bfd_malloc (500);
11360 if (*stats == NULL)
11363 sprintf (*stats, _("linker stubs in %u group%s\n"
11365 " toc adjust %lu\n"
11366 " long branch %lu\n"
11367 " long toc adj %lu\n"
11370 stub_sec_count == 1 ? "" : "s",
11371 htab->stub_count[ppc_stub_long_branch - 1],
11372 htab->stub_count[ppc_stub_long_branch_r2off - 1],
11373 htab->stub_count[ppc_stub_plt_branch - 1],
11374 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
11375 htab->stub_count[ppc_stub_plt_call - 1]);
11380 /* This function undoes the changes made by add_symbol_adjust. */
11383 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11385 struct ppc_link_hash_entry *eh;
11387 if (h->root.type == bfd_link_hash_indirect)
11390 if (h->root.type == bfd_link_hash_warning)
11391 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11393 eh = (struct ppc_link_hash_entry *) h;
11394 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
11397 eh->elf.root.type = bfd_link_hash_undefined;
11402 ppc64_elf_restore_symbols (struct bfd_link_info *info)
11404 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11407 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
11410 /* What to do when ld finds relocations against symbols defined in
11411 discarded sections. */
11413 static unsigned int
11414 ppc64_elf_action_discarded (asection *sec)
11416 if (strcmp (".opd", sec->name) == 0)
11419 if (strcmp (".toc", sec->name) == 0)
11422 if (strcmp (".toc1", sec->name) == 0)
11425 return _bfd_elf_default_action_discarded (sec);
11428 /* REL points to a low-part reloc on a largetoc instruction sequence.
11429 Find the matching high-part reloc instruction and verify that it
11430 is addis REG,x,imm. If so, set *REG to x and return a pointer to
11431 the high-part reloc. */
11433 static const Elf_Internal_Rela *
11434 ha_reloc_match (const Elf_Internal_Rela *relocs,
11435 const Elf_Internal_Rela *rel,
11437 bfd_boolean match_addend,
11438 const bfd *input_bfd,
11439 const bfd_byte *contents)
11441 enum elf_ppc64_reloc_type r_type, r_type_ha;
11442 bfd_vma r_info_ha, r_addend;
11444 r_type = ELF64_R_TYPE (rel->r_info);
11447 case R_PPC64_GOT_TLSLD16_LO:
11448 case R_PPC64_GOT_TLSGD16_LO:
11449 case R_PPC64_GOT_TPREL16_LO_DS:
11450 case R_PPC64_GOT_DTPREL16_LO_DS:
11451 case R_PPC64_GOT16_LO:
11452 case R_PPC64_TOC16_LO:
11453 r_type_ha = r_type + 2;
11455 case R_PPC64_GOT16_LO_DS:
11456 r_type_ha = R_PPC64_GOT16_HA;
11458 case R_PPC64_TOC16_LO_DS:
11459 r_type_ha = R_PPC64_TOC16_HA;
11464 r_info_ha = ELF64_R_INFO (ELF64_R_SYM (rel->r_info), r_type_ha);
11465 r_addend = rel->r_addend;
11467 while (--rel >= relocs)
11468 if (rel->r_info == r_info_ha
11470 || rel->r_addend == r_addend))
11472 const bfd_byte *p = contents + (rel->r_offset & ~3);
11473 unsigned int insn = bfd_get_32 (input_bfd, p);
11474 if ((insn & (0x3f << 26)) == (15u << 26) /* addis rt,x,imm */
11475 && (insn & (0x1f << 21)) == (*reg << 21))
11477 *reg = (insn >> 16) & 0x1f;
11485 /* The RELOCATE_SECTION function is called by the ELF backend linker
11486 to handle the relocations for a section.
11488 The relocs are always passed as Rela structures; if the section
11489 actually uses Rel structures, the r_addend field will always be
11492 This function is responsible for adjust the section contents as
11493 necessary, and (if using Rela relocs and generating a
11494 relocatable output file) adjusting the reloc addend as
11497 This function does not have to worry about setting the reloc
11498 address or the reloc symbol index.
11500 LOCAL_SYMS is a pointer to the swapped in local symbols.
11502 LOCAL_SECTIONS is an array giving the section in the input file
11503 corresponding to the st_shndx field of each local symbol.
11505 The global hash table entry for the global symbols can be found
11506 via elf_sym_hashes (input_bfd).
11508 When generating relocatable output, this function must handle
11509 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
11510 going to be the section symbol corresponding to the output
11511 section, which means that the addend must be adjusted
11515 ppc64_elf_relocate_section (bfd *output_bfd,
11516 struct bfd_link_info *info,
11518 asection *input_section,
11519 bfd_byte *contents,
11520 Elf_Internal_Rela *relocs,
11521 Elf_Internal_Sym *local_syms,
11522 asection **local_sections)
11524 struct ppc_link_hash_table *htab;
11525 Elf_Internal_Shdr *symtab_hdr;
11526 struct elf_link_hash_entry **sym_hashes;
11527 Elf_Internal_Rela *rel;
11528 Elf_Internal_Rela *relend;
11529 Elf_Internal_Rela outrel;
11531 struct got_entry **local_got_ents;
11532 unsigned char *ha_opt;
11534 bfd_boolean no_ha_opt;
11535 bfd_boolean ret = TRUE;
11536 bfd_boolean is_opd;
11537 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
11538 bfd_boolean is_power4 = FALSE;
11539 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
11541 /* Initialize howto table if needed. */
11542 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
11545 htab = ppc_hash_table (info);
11549 /* Don't relocate stub sections. */
11550 if (input_section->owner == htab->stub_bfd)
11553 BFD_ASSERT (is_ppc64_elf (input_bfd));
11555 local_got_ents = elf_local_got_ents (input_bfd);
11556 TOCstart = elf_gp (output_bfd);
11557 symtab_hdr = &elf_symtab_hdr (input_bfd);
11558 sym_hashes = elf_sym_hashes (input_bfd);
11559 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
11564 relend = relocs + input_section->reloc_count;
11565 for (; rel < relend; rel++)
11567 enum elf_ppc64_reloc_type r_type;
11568 bfd_vma addend, orig_addend;
11569 bfd_reloc_status_type r;
11570 Elf_Internal_Sym *sym;
11572 struct elf_link_hash_entry *h_elf;
11573 struct ppc_link_hash_entry *h;
11574 struct ppc_link_hash_entry *fdh;
11575 const char *sym_name;
11576 unsigned long r_symndx, toc_symndx;
11577 bfd_vma toc_addend;
11578 unsigned char tls_mask, tls_gd, tls_type;
11579 unsigned char sym_type;
11580 bfd_vma relocation;
11581 bfd_boolean unresolved_reloc;
11582 bfd_boolean warned;
11585 struct ppc_stub_hash_entry *stub_entry;
11586 bfd_vma max_br_offset;
11589 r_type = ELF64_R_TYPE (rel->r_info);
11590 r_symndx = ELF64_R_SYM (rel->r_info);
11592 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
11593 symbol of the previous ADDR64 reloc. The symbol gives us the
11594 proper TOC base to use. */
11595 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
11597 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
11599 r_symndx = ELF64_R_SYM (rel[-1].r_info);
11605 unresolved_reloc = FALSE;
11607 orig_addend = rel->r_addend;
11609 if (r_symndx < symtab_hdr->sh_info)
11611 /* It's a local symbol. */
11612 struct _opd_sec_data *opd;
11614 sym = local_syms + r_symndx;
11615 sec = local_sections[r_symndx];
11616 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
11617 sym_type = ELF64_ST_TYPE (sym->st_info);
11618 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
11619 opd = get_opd_info (sec);
11620 if (opd != NULL && opd->adjust != NULL)
11622 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
11627 /* If this is a relocation against the opd section sym
11628 and we have edited .opd, adjust the reloc addend so
11629 that ld -r and ld --emit-relocs output is correct.
11630 If it is a reloc against some other .opd symbol,
11631 then the symbol value will be adjusted later. */
11632 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
11633 rel->r_addend += adjust;
11635 relocation += adjust;
11641 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
11642 r_symndx, symtab_hdr, sym_hashes,
11643 h_elf, sec, relocation,
11644 unresolved_reloc, warned);
11645 sym_name = h_elf->root.root.string;
11646 sym_type = h_elf->type;
11648 h = (struct ppc_link_hash_entry *) h_elf;
11650 if (sec != NULL && elf_discarded_section (sec))
11651 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
11653 ppc64_elf_howto_table[r_type],
11656 if (info->relocatable)
11659 /* TLS optimizations. Replace instruction sequences and relocs
11660 based on information we collected in tls_optimize. We edit
11661 RELOCS so that --emit-relocs will output something sensible
11662 for the final instruction stream. */
11667 tls_mask = h->tls_mask;
11668 else if (local_got_ents != NULL)
11670 struct plt_entry **local_plt = (struct plt_entry **)
11671 (local_got_ents + symtab_hdr->sh_info);
11672 unsigned char *lgot_masks = (unsigned char *)
11673 (local_plt + symtab_hdr->sh_info);
11674 tls_mask = lgot_masks[r_symndx];
11677 && (r_type == R_PPC64_TLS
11678 || r_type == R_PPC64_TLSGD
11679 || r_type == R_PPC64_TLSLD))
11681 /* Check for toc tls entries. */
11682 unsigned char *toc_tls;
11684 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11685 &local_syms, rel, input_bfd))
11689 tls_mask = *toc_tls;
11692 /* Check that tls relocs are used with tls syms, and non-tls
11693 relocs are used with non-tls syms. */
11694 if (r_symndx != STN_UNDEF
11695 && r_type != R_PPC64_NONE
11697 || h->elf.root.type == bfd_link_hash_defined
11698 || h->elf.root.type == bfd_link_hash_defweak)
11699 && (IS_PPC64_TLS_RELOC (r_type)
11700 != (sym_type == STT_TLS
11701 || (sym_type == STT_SECTION
11702 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
11705 && (r_type == R_PPC64_TLS
11706 || r_type == R_PPC64_TLSGD
11707 || r_type == R_PPC64_TLSLD))
11708 /* R_PPC64_TLS is OK against a symbol in the TOC. */
11711 (*_bfd_error_handler)
11712 (!IS_PPC64_TLS_RELOC (r_type)
11713 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
11714 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s"),
11717 (long) rel->r_offset,
11718 ppc64_elf_howto_table[r_type]->name,
11722 /* Ensure reloc mapping code below stays sane. */
11723 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
11724 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
11725 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
11726 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
11727 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
11728 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
11729 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
11730 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
11731 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
11732 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
11740 case R_PPC64_LO_DS_OPT:
11741 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
11742 if ((insn & (0x3f << 26)) != 58u << 26)
11744 insn += (14u << 26) - (58u << 26);
11745 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
11746 r_type = R_PPC64_TOC16_LO;
11747 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11750 case R_PPC64_TOC16:
11751 case R_PPC64_TOC16_LO:
11752 case R_PPC64_TOC16_DS:
11753 case R_PPC64_TOC16_LO_DS:
11755 /* Check for toc tls entries. */
11756 unsigned char *toc_tls;
11759 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11760 &local_syms, rel, input_bfd);
11766 tls_mask = *toc_tls;
11767 if (r_type == R_PPC64_TOC16_DS
11768 || r_type == R_PPC64_TOC16_LO_DS)
11771 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
11776 /* If we found a GD reloc pair, then we might be
11777 doing a GD->IE transition. */
11780 tls_gd = TLS_TPRELGD;
11781 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11784 else if (retval == 3)
11786 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11794 case R_PPC64_GOT_TPREL16_HI:
11795 case R_PPC64_GOT_TPREL16_HA:
11797 && (tls_mask & TLS_TPREL) == 0)
11799 rel->r_offset -= d_offset;
11800 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
11801 r_type = R_PPC64_NONE;
11802 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11806 case R_PPC64_GOT_TPREL16_DS:
11807 case R_PPC64_GOT_TPREL16_LO_DS:
11809 && (tls_mask & TLS_TPREL) == 0)
11812 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
11814 insn |= 0x3c0d0000; /* addis 0,13,0 */
11815 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
11816 r_type = R_PPC64_TPREL16_HA;
11817 if (toc_symndx != 0)
11819 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
11820 rel->r_addend = toc_addend;
11821 /* We changed the symbol. Start over in order to
11822 get h, sym, sec etc. right. */
11827 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11833 && (tls_mask & TLS_TPREL) == 0)
11835 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
11836 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
11839 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
11840 /* Was PPC64_TLS which sits on insn boundary, now
11841 PPC64_TPREL16_LO which is at low-order half-word. */
11842 rel->r_offset += d_offset;
11843 r_type = R_PPC64_TPREL16_LO;
11844 if (toc_symndx != 0)
11846 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
11847 rel->r_addend = toc_addend;
11848 /* We changed the symbol. Start over in order to
11849 get h, sym, sec etc. right. */
11854 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11858 case R_PPC64_GOT_TLSGD16_HI:
11859 case R_PPC64_GOT_TLSGD16_HA:
11860 tls_gd = TLS_TPRELGD;
11861 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11865 case R_PPC64_GOT_TLSLD16_HI:
11866 case R_PPC64_GOT_TLSLD16_HA:
11867 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11870 if ((tls_mask & tls_gd) != 0)
11871 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
11872 + R_PPC64_GOT_TPREL16_DS);
11875 rel->r_offset -= d_offset;
11876 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
11877 r_type = R_PPC64_NONE;
11879 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11883 case R_PPC64_GOT_TLSGD16:
11884 case R_PPC64_GOT_TLSGD16_LO:
11885 tls_gd = TLS_TPRELGD;
11886 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11890 case R_PPC64_GOT_TLSLD16:
11891 case R_PPC64_GOT_TLSLD16_LO:
11892 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11894 unsigned int insn1, insn2, insn3;
11898 offset = (bfd_vma) -1;
11899 /* If not using the newer R_PPC64_TLSGD/LD to mark
11900 __tls_get_addr calls, we must trust that the call
11901 stays with its arg setup insns, ie. that the next
11902 reloc is the __tls_get_addr call associated with
11903 the current reloc. Edit both insns. */
11904 if (input_section->has_tls_get_addr_call
11905 && rel + 1 < relend
11906 && branch_reloc_hash_match (input_bfd, rel + 1,
11907 htab->tls_get_addr,
11908 htab->tls_get_addr_fd))
11909 offset = rel[1].r_offset;
11910 if ((tls_mask & tls_gd) != 0)
11913 insn1 = bfd_get_32 (output_bfd,
11914 contents + rel->r_offset - d_offset);
11915 insn1 &= (1 << 26) - (1 << 2);
11916 insn1 |= 58 << 26; /* ld */
11917 insn2 = 0x7c636a14; /* add 3,3,13 */
11918 if (offset != (bfd_vma) -1)
11919 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
11920 if ((tls_mask & TLS_EXPLICIT) == 0)
11921 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
11922 + R_PPC64_GOT_TPREL16_DS);
11924 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
11925 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11930 insn1 = 0x3c6d0000; /* addis 3,13,0 */
11931 insn2 = 0x38630000; /* addi 3,3,0 */
11934 /* Was an LD reloc. */
11936 sec = local_sections[toc_symndx];
11938 r_symndx < symtab_hdr->sh_info;
11940 if (local_sections[r_symndx] == sec)
11942 if (r_symndx >= symtab_hdr->sh_info)
11943 r_symndx = STN_UNDEF;
11944 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
11945 if (r_symndx != STN_UNDEF)
11946 rel->r_addend -= (local_syms[r_symndx].st_value
11947 + sec->output_offset
11948 + sec->output_section->vma);
11950 else if (toc_symndx != 0)
11952 r_symndx = toc_symndx;
11953 rel->r_addend = toc_addend;
11955 r_type = R_PPC64_TPREL16_HA;
11956 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11957 if (offset != (bfd_vma) -1)
11959 rel[1].r_info = ELF64_R_INFO (r_symndx,
11960 R_PPC64_TPREL16_LO);
11961 rel[1].r_offset = offset + d_offset;
11962 rel[1].r_addend = rel->r_addend;
11965 bfd_put_32 (output_bfd, insn1,
11966 contents + rel->r_offset - d_offset);
11967 if (offset != (bfd_vma) -1)
11969 insn3 = bfd_get_32 (output_bfd,
11970 contents + offset + 4);
11972 || insn3 == CROR_151515 || insn3 == CROR_313131)
11974 rel[1].r_offset += 4;
11975 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
11978 bfd_put_32 (output_bfd, insn2, contents + offset);
11980 if ((tls_mask & tls_gd) == 0
11981 && (tls_gd == 0 || toc_symndx != 0))
11983 /* We changed the symbol. Start over in order
11984 to get h, sym, sec etc. right. */
11991 case R_PPC64_TLSGD:
11992 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11994 unsigned int insn2, insn3;
11995 bfd_vma offset = rel->r_offset;
11997 if ((tls_mask & TLS_TPRELGD) != 0)
12000 r_type = R_PPC64_NONE;
12001 insn2 = 0x7c636a14; /* add 3,3,13 */
12006 if (toc_symndx != 0)
12008 r_symndx = toc_symndx;
12009 rel->r_addend = toc_addend;
12011 r_type = R_PPC64_TPREL16_LO;
12012 rel->r_offset = offset + d_offset;
12013 insn2 = 0x38630000; /* addi 3,3,0 */
12015 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12016 /* Zap the reloc on the _tls_get_addr call too. */
12017 BFD_ASSERT (offset == rel[1].r_offset);
12018 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12019 insn3 = bfd_get_32 (output_bfd,
12020 contents + offset + 4);
12022 || insn3 == CROR_151515 || insn3 == CROR_313131)
12024 rel->r_offset += 4;
12025 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12028 bfd_put_32 (output_bfd, insn2, contents + offset);
12029 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
12037 case R_PPC64_TLSLD:
12038 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12040 unsigned int insn2, insn3;
12041 bfd_vma offset = rel->r_offset;
12044 sec = local_sections[toc_symndx];
12046 r_symndx < symtab_hdr->sh_info;
12048 if (local_sections[r_symndx] == sec)
12050 if (r_symndx >= symtab_hdr->sh_info)
12051 r_symndx = STN_UNDEF;
12052 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12053 if (r_symndx != STN_UNDEF)
12054 rel->r_addend -= (local_syms[r_symndx].st_value
12055 + sec->output_offset
12056 + sec->output_section->vma);
12058 r_type = R_PPC64_TPREL16_LO;
12059 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12060 rel->r_offset = offset + d_offset;
12061 /* Zap the reloc on the _tls_get_addr call too. */
12062 BFD_ASSERT (offset == rel[1].r_offset);
12063 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12064 insn2 = 0x38630000; /* addi 3,3,0 */
12065 insn3 = bfd_get_32 (output_bfd,
12066 contents + offset + 4);
12068 || insn3 == CROR_151515 || insn3 == CROR_313131)
12070 rel->r_offset += 4;
12071 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12074 bfd_put_32 (output_bfd, insn2, contents + offset);
12080 case R_PPC64_DTPMOD64:
12081 if (rel + 1 < relend
12082 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
12083 && rel[1].r_offset == rel->r_offset + 8)
12085 if ((tls_mask & TLS_GD) == 0)
12087 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
12088 if ((tls_mask & TLS_TPRELGD) != 0)
12089 r_type = R_PPC64_TPREL64;
12092 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12093 r_type = R_PPC64_NONE;
12095 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12100 if ((tls_mask & TLS_LD) == 0)
12102 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12103 r_type = R_PPC64_NONE;
12104 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12109 case R_PPC64_TPREL64:
12110 if ((tls_mask & TLS_TPREL) == 0)
12112 r_type = R_PPC64_NONE;
12113 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12118 /* Handle other relocations that tweak non-addend part of insn. */
12120 max_br_offset = 1 << 25;
12121 addend = rel->r_addend;
12127 /* Branch taken prediction relocations. */
12128 case R_PPC64_ADDR14_BRTAKEN:
12129 case R_PPC64_REL14_BRTAKEN:
12130 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
12133 /* Branch not taken prediction relocations. */
12134 case R_PPC64_ADDR14_BRNTAKEN:
12135 case R_PPC64_REL14_BRNTAKEN:
12136 insn |= bfd_get_32 (output_bfd,
12137 contents + rel->r_offset) & ~(0x01 << 21);
12140 case R_PPC64_REL14:
12141 max_br_offset = 1 << 15;
12144 case R_PPC64_REL24:
12145 /* Calls to functions with a different TOC, such as calls to
12146 shared objects, need to alter the TOC pointer. This is
12147 done using a linkage stub. A REL24 branching to these
12148 linkage stubs needs to be followed by a nop, as the nop
12149 will be replaced with an instruction to restore the TOC
12154 && h->oh->is_func_descriptor)
12155 fdh = ppc_follow_link (h->oh);
12156 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
12157 if (stub_entry != NULL
12158 && (stub_entry->stub_type == ppc_stub_plt_call
12159 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
12160 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
12162 bfd_boolean can_plt_call = FALSE;
12164 if (rel->r_offset + 8 <= input_section->size)
12167 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
12169 || nop == CROR_151515 || nop == CROR_313131)
12172 && (h == htab->tls_get_addr_fd
12173 || h == htab->tls_get_addr)
12174 && !htab->no_tls_get_addr_opt)
12176 /* Special stub used, leave nop alone. */
12179 bfd_put_32 (input_bfd, LD_R2_40R1,
12180 contents + rel->r_offset + 4);
12181 can_plt_call = TRUE;
12187 if (stub_entry->stub_type == ppc_stub_plt_call)
12189 /* If this is a plain branch rather than a branch
12190 and link, don't require a nop. However, don't
12191 allow tail calls in a shared library as they
12192 will result in r2 being corrupted. */
12194 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
12195 if (info->executable && (br & 1) == 0)
12196 can_plt_call = TRUE;
12201 && strcmp (h->elf.root.root.string,
12202 ".__libc_start_main") == 0)
12204 /* Allow crt1 branch to go via a toc adjusting stub. */
12205 can_plt_call = TRUE;
12209 if (strcmp (input_section->output_section->name,
12211 || strcmp (input_section->output_section->name,
12213 (*_bfd_error_handler)
12214 (_("%B(%A+0x%lx): automatic multiple TOCs "
12215 "not supported using your crt files; "
12216 "recompile with -mminimal-toc or upgrade gcc"),
12219 (long) rel->r_offset);
12221 (*_bfd_error_handler)
12222 (_("%B(%A+0x%lx): sibling call optimization to `%s' "
12223 "does not allow automatic multiple TOCs; "
12224 "recompile with -mminimal-toc or "
12225 "-fno-optimize-sibling-calls, "
12226 "or make `%s' extern"),
12229 (long) rel->r_offset,
12232 bfd_set_error (bfd_error_bad_value);
12238 && stub_entry->stub_type == ppc_stub_plt_call)
12239 unresolved_reloc = FALSE;
12242 if ((stub_entry == NULL
12243 || stub_entry->stub_type == ppc_stub_long_branch
12244 || stub_entry->stub_type == ppc_stub_plt_branch)
12245 && get_opd_info (sec) != NULL)
12247 /* The branch destination is the value of the opd entry. */
12248 bfd_vma off = (relocation + addend
12249 - sec->output_section->vma
12250 - sec->output_offset);
12251 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
12252 if (dest != (bfd_vma) -1)
12259 /* If the branch is out of reach we ought to have a long
12261 from = (rel->r_offset
12262 + input_section->output_offset
12263 + input_section->output_section->vma);
12265 if (stub_entry != NULL
12266 && (stub_entry->stub_type == ppc_stub_long_branch
12267 || stub_entry->stub_type == ppc_stub_plt_branch)
12268 && (r_type == R_PPC64_ADDR14_BRTAKEN
12269 || r_type == R_PPC64_ADDR14_BRNTAKEN
12270 || (relocation + addend - from + max_br_offset
12271 < 2 * max_br_offset)))
12272 /* Don't use the stub if this branch is in range. */
12275 if (stub_entry != NULL)
12277 /* Munge up the value and addend so that we call the stub
12278 rather than the procedure directly. */
12279 relocation = (stub_entry->stub_offset
12280 + stub_entry->stub_sec->output_offset
12281 + stub_entry->stub_sec->output_section->vma);
12289 /* Set 'a' bit. This is 0b00010 in BO field for branch
12290 on CR(BI) insns (BO == 001at or 011at), and 0b01000
12291 for branch on CTR insns (BO == 1a00t or 1a01t). */
12292 if ((insn & (0x14 << 21)) == (0x04 << 21))
12293 insn |= 0x02 << 21;
12294 else if ((insn & (0x14 << 21)) == (0x10 << 21))
12295 insn |= 0x08 << 21;
12301 /* Invert 'y' bit if not the default. */
12302 if ((bfd_signed_vma) (relocation + addend - from) < 0)
12303 insn ^= 0x01 << 21;
12306 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
12309 /* NOP out calls to undefined weak functions.
12310 We can thus call a weak function without first
12311 checking whether the function is defined. */
12313 && h->elf.root.type == bfd_link_hash_undefweak
12314 && h->elf.dynindx == -1
12315 && r_type == R_PPC64_REL24
12319 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12325 /* Set `addend'. */
12330 (*_bfd_error_handler)
12331 (_("%B: unknown relocation type %d for symbol %s"),
12332 input_bfd, (int) r_type, sym_name);
12334 bfd_set_error (bfd_error_bad_value);
12340 case R_PPC64_TLSGD:
12341 case R_PPC64_TLSLD:
12342 case R_PPC64_GNU_VTINHERIT:
12343 case R_PPC64_GNU_VTENTRY:
12346 /* GOT16 relocations. Like an ADDR16 using the symbol's
12347 address in the GOT as relocation value instead of the
12348 symbol's value itself. Also, create a GOT entry for the
12349 symbol and put the symbol value there. */
12350 case R_PPC64_GOT_TLSGD16:
12351 case R_PPC64_GOT_TLSGD16_LO:
12352 case R_PPC64_GOT_TLSGD16_HI:
12353 case R_PPC64_GOT_TLSGD16_HA:
12354 tls_type = TLS_TLS | TLS_GD;
12357 case R_PPC64_GOT_TLSLD16:
12358 case R_PPC64_GOT_TLSLD16_LO:
12359 case R_PPC64_GOT_TLSLD16_HI:
12360 case R_PPC64_GOT_TLSLD16_HA:
12361 tls_type = TLS_TLS | TLS_LD;
12364 case R_PPC64_GOT_TPREL16_DS:
12365 case R_PPC64_GOT_TPREL16_LO_DS:
12366 case R_PPC64_GOT_TPREL16_HI:
12367 case R_PPC64_GOT_TPREL16_HA:
12368 tls_type = TLS_TLS | TLS_TPREL;
12371 case R_PPC64_GOT_DTPREL16_DS:
12372 case R_PPC64_GOT_DTPREL16_LO_DS:
12373 case R_PPC64_GOT_DTPREL16_HI:
12374 case R_PPC64_GOT_DTPREL16_HA:
12375 tls_type = TLS_TLS | TLS_DTPREL;
12378 case R_PPC64_GOT16:
12379 case R_PPC64_GOT16_LO:
12380 case R_PPC64_GOT16_HI:
12381 case R_PPC64_GOT16_HA:
12382 case R_PPC64_GOT16_DS:
12383 case R_PPC64_GOT16_LO_DS:
12386 /* Relocation is to the entry for this symbol in the global
12391 unsigned long indx = 0;
12392 struct got_entry *ent;
12394 if (tls_type == (TLS_TLS | TLS_LD)
12396 || !h->elf.def_dynamic))
12397 ent = ppc64_tlsld_got (input_bfd);
12403 bfd_boolean dyn = htab->elf.dynamic_sections_created;
12404 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
12407 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
12408 /* This is actually a static link, or it is a
12409 -Bsymbolic link and the symbol is defined
12410 locally, or the symbol was forced to be local
12411 because of a version file. */
12415 indx = h->elf.dynindx;
12416 unresolved_reloc = FALSE;
12418 ent = h->elf.got.glist;
12422 if (local_got_ents == NULL)
12424 ent = local_got_ents[r_symndx];
12427 for (; ent != NULL; ent = ent->next)
12428 if (ent->addend == orig_addend
12429 && ent->owner == input_bfd
12430 && ent->tls_type == tls_type)
12436 if (ent->is_indirect)
12437 ent = ent->got.ent;
12438 offp = &ent->got.offset;
12439 got = ppc64_elf_tdata (ent->owner)->got;
12443 /* The offset must always be a multiple of 8. We use the
12444 least significant bit to record whether we have already
12445 processed this entry. */
12447 if ((off & 1) != 0)
12451 /* Generate relocs for the dynamic linker, except in
12452 the case of TLSLD where we'll use one entry per
12460 ? h->elf.type == STT_GNU_IFUNC
12461 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
12462 if ((info->shared || indx != 0)
12464 || (tls_type == (TLS_TLS | TLS_LD)
12465 && !h->elf.def_dynamic)
12466 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12467 || h->elf.root.type != bfd_link_hash_undefweak))
12468 relgot = ppc64_elf_tdata (ent->owner)->relgot;
12470 relgot = htab->reliplt;
12471 if (relgot != NULL)
12473 outrel.r_offset = (got->output_section->vma
12474 + got->output_offset
12476 outrel.r_addend = addend;
12477 if (tls_type & (TLS_LD | TLS_GD))
12479 outrel.r_addend = 0;
12480 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
12481 if (tls_type == (TLS_TLS | TLS_GD))
12483 loc = relgot->contents;
12484 loc += (relgot->reloc_count++
12485 * sizeof (Elf64_External_Rela));
12486 bfd_elf64_swap_reloca_out (output_bfd,
12488 outrel.r_offset += 8;
12489 outrel.r_addend = addend;
12491 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
12494 else if (tls_type == (TLS_TLS | TLS_DTPREL))
12495 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
12496 else if (tls_type == (TLS_TLS | TLS_TPREL))
12497 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
12498 else if (indx != 0)
12499 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
12503 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12505 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12507 /* Write the .got section contents for the sake
12509 loc = got->contents + off;
12510 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
12514 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
12516 outrel.r_addend += relocation;
12517 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
12518 outrel.r_addend -= htab->elf.tls_sec->vma;
12520 loc = relgot->contents;
12521 loc += (relgot->reloc_count++
12522 * sizeof (Elf64_External_Rela));
12523 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12526 /* Init the .got section contents here if we're not
12527 emitting a reloc. */
12530 relocation += addend;
12531 if (tls_type == (TLS_TLS | TLS_LD))
12533 else if (tls_type != 0)
12535 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
12536 if (tls_type == (TLS_TLS | TLS_TPREL))
12537 relocation += DTP_OFFSET - TP_OFFSET;
12539 if (tls_type == (TLS_TLS | TLS_GD))
12541 bfd_put_64 (output_bfd, relocation,
12542 got->contents + off + 8);
12547 bfd_put_64 (output_bfd, relocation,
12548 got->contents + off);
12552 if (off >= (bfd_vma) -2)
12555 relocation = got->output_section->vma + got->output_offset + off;
12556 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
12560 case R_PPC64_PLT16_HA:
12561 case R_PPC64_PLT16_HI:
12562 case R_PPC64_PLT16_LO:
12563 case R_PPC64_PLT32:
12564 case R_PPC64_PLT64:
12565 /* Relocation is to the entry for this symbol in the
12566 procedure linkage table. */
12568 /* Resolve a PLT reloc against a local symbol directly,
12569 without using the procedure linkage table. */
12573 /* It's possible that we didn't make a PLT entry for this
12574 symbol. This happens when statically linking PIC code,
12575 or when using -Bsymbolic. Go find a match if there is a
12577 if (htab->plt != NULL)
12579 struct plt_entry *ent;
12580 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
12581 if (ent->addend == orig_addend
12582 && ent->plt.offset != (bfd_vma) -1)
12584 relocation = (htab->plt->output_section->vma
12585 + htab->plt->output_offset
12586 + ent->plt.offset);
12587 unresolved_reloc = FALSE;
12593 /* Relocation value is TOC base. */
12594 relocation = TOCstart;
12595 if (r_symndx == STN_UNDEF)
12596 relocation += htab->stub_group[input_section->id].toc_off;
12597 else if (unresolved_reloc)
12599 else if (sec != NULL && sec->id <= htab->top_id)
12600 relocation += htab->stub_group[sec->id].toc_off;
12602 unresolved_reloc = TRUE;
12605 /* TOC16 relocs. We want the offset relative to the TOC base,
12606 which is the address of the start of the TOC plus 0x8000.
12607 The TOC consists of sections .got, .toc, .tocbss, and .plt,
12609 case R_PPC64_TOC16:
12610 case R_PPC64_TOC16_LO:
12611 case R_PPC64_TOC16_HI:
12612 case R_PPC64_TOC16_DS:
12613 case R_PPC64_TOC16_LO_DS:
12614 case R_PPC64_TOC16_HA:
12615 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
12618 /* Relocate against the beginning of the section. */
12619 case R_PPC64_SECTOFF:
12620 case R_PPC64_SECTOFF_LO:
12621 case R_PPC64_SECTOFF_HI:
12622 case R_PPC64_SECTOFF_DS:
12623 case R_PPC64_SECTOFF_LO_DS:
12624 case R_PPC64_SECTOFF_HA:
12626 addend -= sec->output_section->vma;
12629 case R_PPC64_REL16:
12630 case R_PPC64_REL16_LO:
12631 case R_PPC64_REL16_HI:
12632 case R_PPC64_REL16_HA:
12635 case R_PPC64_REL14:
12636 case R_PPC64_REL14_BRNTAKEN:
12637 case R_PPC64_REL14_BRTAKEN:
12638 case R_PPC64_REL24:
12641 case R_PPC64_TPREL16:
12642 case R_PPC64_TPREL16_LO:
12643 case R_PPC64_TPREL16_HI:
12644 case R_PPC64_TPREL16_HA:
12645 case R_PPC64_TPREL16_DS:
12646 case R_PPC64_TPREL16_LO_DS:
12647 case R_PPC64_TPREL16_HIGHER:
12648 case R_PPC64_TPREL16_HIGHERA:
12649 case R_PPC64_TPREL16_HIGHEST:
12650 case R_PPC64_TPREL16_HIGHESTA:
12652 && h->elf.root.type == bfd_link_hash_undefweak
12653 && h->elf.dynindx == -1)
12655 /* Make this relocation against an undefined weak symbol
12656 resolve to zero. This is really just a tweak, since
12657 code using weak externs ought to check that they are
12658 defined before using them. */
12659 bfd_byte *p = contents + rel->r_offset - d_offset;
12661 insn = bfd_get_32 (output_bfd, p);
12662 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
12664 bfd_put_32 (output_bfd, insn, p);
12667 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
12669 /* The TPREL16 relocs shouldn't really be used in shared
12670 libs as they will result in DT_TEXTREL being set, but
12671 support them anyway. */
12675 case R_PPC64_DTPREL16:
12676 case R_PPC64_DTPREL16_LO:
12677 case R_PPC64_DTPREL16_HI:
12678 case R_PPC64_DTPREL16_HA:
12679 case R_PPC64_DTPREL16_DS:
12680 case R_PPC64_DTPREL16_LO_DS:
12681 case R_PPC64_DTPREL16_HIGHER:
12682 case R_PPC64_DTPREL16_HIGHERA:
12683 case R_PPC64_DTPREL16_HIGHEST:
12684 case R_PPC64_DTPREL16_HIGHESTA:
12685 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
12688 case R_PPC64_DTPMOD64:
12693 case R_PPC64_TPREL64:
12694 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
12697 case R_PPC64_DTPREL64:
12698 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
12701 /* Relocations that may need to be propagated if this is a
12703 case R_PPC64_REL30:
12704 case R_PPC64_REL32:
12705 case R_PPC64_REL64:
12706 case R_PPC64_ADDR14:
12707 case R_PPC64_ADDR14_BRNTAKEN:
12708 case R_PPC64_ADDR14_BRTAKEN:
12709 case R_PPC64_ADDR16:
12710 case R_PPC64_ADDR16_DS:
12711 case R_PPC64_ADDR16_HA:
12712 case R_PPC64_ADDR16_HI:
12713 case R_PPC64_ADDR16_HIGHER:
12714 case R_PPC64_ADDR16_HIGHERA:
12715 case R_PPC64_ADDR16_HIGHEST:
12716 case R_PPC64_ADDR16_HIGHESTA:
12717 case R_PPC64_ADDR16_LO:
12718 case R_PPC64_ADDR16_LO_DS:
12719 case R_PPC64_ADDR24:
12720 case R_PPC64_ADDR32:
12721 case R_PPC64_ADDR64:
12722 case R_PPC64_UADDR16:
12723 case R_PPC64_UADDR32:
12724 case R_PPC64_UADDR64:
12726 if ((input_section->flags & SEC_ALLOC) == 0)
12729 if (NO_OPD_RELOCS && is_opd)
12734 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12735 || h->elf.root.type != bfd_link_hash_undefweak)
12736 && (must_be_dyn_reloc (info, r_type)
12737 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
12738 || (ELIMINATE_COPY_RELOCS
12741 && h->elf.dynindx != -1
12742 && !h->elf.non_got_ref
12743 && !h->elf.def_regular)
12746 ? h->elf.type == STT_GNU_IFUNC
12747 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
12749 bfd_boolean skip, relocate;
12753 /* When generating a dynamic object, these relocations
12754 are copied into the output file to be resolved at run
12760 out_off = _bfd_elf_section_offset (output_bfd, info,
12761 input_section, rel->r_offset);
12762 if (out_off == (bfd_vma) -1)
12764 else if (out_off == (bfd_vma) -2)
12765 skip = TRUE, relocate = TRUE;
12766 out_off += (input_section->output_section->vma
12767 + input_section->output_offset);
12768 outrel.r_offset = out_off;
12769 outrel.r_addend = rel->r_addend;
12771 /* Optimize unaligned reloc use. */
12772 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
12773 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
12774 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
12775 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
12776 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
12777 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
12778 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
12779 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
12780 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
12783 memset (&outrel, 0, sizeof outrel);
12784 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
12786 && r_type != R_PPC64_TOC)
12787 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
12790 /* This symbol is local, or marked to become local,
12791 or this is an opd section reloc which must point
12792 at a local function. */
12793 outrel.r_addend += relocation;
12794 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
12796 if (is_opd && h != NULL)
12798 /* Lie about opd entries. This case occurs
12799 when building shared libraries and we
12800 reference a function in another shared
12801 lib. The same thing happens for a weak
12802 definition in an application that's
12803 overridden by a strong definition in a
12804 shared lib. (I believe this is a generic
12805 bug in binutils handling of weak syms.)
12806 In these cases we won't use the opd
12807 entry in this lib. */
12808 unresolved_reloc = FALSE;
12811 && r_type == R_PPC64_ADDR64
12813 ? h->elf.type == STT_GNU_IFUNC
12814 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
12815 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12818 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12820 /* We need to relocate .opd contents for ld.so.
12821 Prelink also wants simple and consistent rules
12822 for relocs. This make all RELATIVE relocs have
12823 *r_offset equal to r_addend. */
12832 ? h->elf.type == STT_GNU_IFUNC
12833 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12835 (*_bfd_error_handler)
12836 (_("%B(%A+0x%lx): relocation %s for indirect "
12837 "function %s unsupported"),
12840 (long) rel->r_offset,
12841 ppc64_elf_howto_table[r_type]->name,
12845 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
12847 else if (sec == NULL || sec->owner == NULL)
12849 bfd_set_error (bfd_error_bad_value);
12856 osec = sec->output_section;
12857 indx = elf_section_data (osec)->dynindx;
12861 if ((osec->flags & SEC_READONLY) == 0
12862 && htab->elf.data_index_section != NULL)
12863 osec = htab->elf.data_index_section;
12865 osec = htab->elf.text_index_section;
12866 indx = elf_section_data (osec)->dynindx;
12868 BFD_ASSERT (indx != 0);
12870 /* We are turning this relocation into one
12871 against a section symbol, so subtract out
12872 the output section's address but not the
12873 offset of the input section in the output
12875 outrel.r_addend -= osec->vma;
12878 outrel.r_info = ELF64_R_INFO (indx, r_type);
12882 sreloc = elf_section_data (input_section)->sreloc;
12883 if (!htab->elf.dynamic_sections_created)
12884 sreloc = htab->reliplt;
12885 if (sreloc == NULL)
12888 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
12891 loc = sreloc->contents;
12892 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
12893 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12895 /* If this reloc is against an external symbol, it will
12896 be computed at runtime, so there's no need to do
12897 anything now. However, for the sake of prelink ensure
12898 that the section contents are a known value. */
12901 unresolved_reloc = FALSE;
12902 /* The value chosen here is quite arbitrary as ld.so
12903 ignores section contents except for the special
12904 case of .opd where the contents might be accessed
12905 before relocation. Choose zero, as that won't
12906 cause reloc overflow. */
12909 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
12910 to improve backward compatibility with older
12912 if (r_type == R_PPC64_ADDR64)
12913 addend = outrel.r_addend;
12914 /* Adjust pc_relative relocs to have zero in *r_offset. */
12915 else if (ppc64_elf_howto_table[r_type]->pc_relative)
12916 addend = (input_section->output_section->vma
12917 + input_section->output_offset
12924 case R_PPC64_GLOB_DAT:
12925 case R_PPC64_JMP_SLOT:
12926 case R_PPC64_JMP_IREL:
12927 case R_PPC64_RELATIVE:
12928 /* We shouldn't ever see these dynamic relocs in relocatable
12930 /* Fall through. */
12932 case R_PPC64_PLTGOT16:
12933 case R_PPC64_PLTGOT16_DS:
12934 case R_PPC64_PLTGOT16_HA:
12935 case R_PPC64_PLTGOT16_HI:
12936 case R_PPC64_PLTGOT16_LO:
12937 case R_PPC64_PLTGOT16_LO_DS:
12938 case R_PPC64_PLTREL32:
12939 case R_PPC64_PLTREL64:
12940 /* These ones haven't been implemented yet. */
12942 (*_bfd_error_handler)
12943 (_("%B: relocation %s is not supported for symbol %s."),
12945 ppc64_elf_howto_table[r_type]->name, sym_name);
12947 bfd_set_error (bfd_error_invalid_operation);
12952 /* Multi-instruction sequences that access the TOC can be
12953 optimized, eg. addis ra,r2,0; addi rb,ra,x;
12954 to nop; addi rb,r2,x; */
12960 case R_PPC64_GOT_TLSLD16_HI:
12961 case R_PPC64_GOT_TLSGD16_HI:
12962 case R_PPC64_GOT_TPREL16_HI:
12963 case R_PPC64_GOT_DTPREL16_HI:
12964 case R_PPC64_GOT16_HI:
12965 case R_PPC64_TOC16_HI:
12966 /* These relocs would only be useful if building up an
12967 offset to later add to r2, perhaps in an indexed
12968 addressing mode instruction. Don't try to optimize.
12969 Unfortunately, the possibility of someone building up an
12970 offset like this or even with the HA relocs, means that
12971 we need to check the high insn when optimizing the low
12975 case R_PPC64_GOT_TLSLD16_HA:
12976 case R_PPC64_GOT_TLSGD16_HA:
12977 case R_PPC64_GOT_TPREL16_HA:
12978 case R_PPC64_GOT_DTPREL16_HA:
12979 case R_PPC64_GOT16_HA:
12980 case R_PPC64_TOC16_HA:
12981 /* nop is done later. */
12984 case R_PPC64_GOT_TLSLD16_LO:
12985 case R_PPC64_GOT_TLSGD16_LO:
12986 case R_PPC64_GOT_TPREL16_LO_DS:
12987 case R_PPC64_GOT_DTPREL16_LO_DS:
12988 case R_PPC64_GOT16_LO:
12989 case R_PPC64_GOT16_LO_DS:
12990 case R_PPC64_TOC16_LO:
12991 case R_PPC64_TOC16_LO_DS:
12992 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000)
12994 bfd_byte *p = contents + (rel->r_offset & ~3);
12995 insn = bfd_get_32 (input_bfd, p);
12996 if ((insn & (0x3f << 26)) == 14u << 26 /* addi */
12997 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
12998 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
12999 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
13000 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
13001 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
13002 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
13003 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
13004 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
13005 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
13006 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
13007 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
13008 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
13009 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
13010 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
13011 && (insn & 3) != 1)
13012 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
13013 && ((insn & 3) == 0 || (insn & 3) == 3)))
13015 unsigned int reg = (insn >> 16) & 0x1f;
13016 const Elf_Internal_Rela *ha;
13017 bfd_boolean match_addend;
13019 match_addend = (sym != NULL
13020 && ELF_ST_TYPE (sym->st_info) == STT_SECTION);
13021 ha = ha_reloc_match (relocs, rel, ®, match_addend,
13022 input_bfd, contents);
13025 insn &= ~(0x1f << 16);
13027 bfd_put_32 (input_bfd, insn, p);
13028 if (ha_opt == NULL)
13030 ha_opt = bfd_zmalloc (input_section->reloc_count);
13031 if (ha_opt == NULL)
13034 ha_opt[ha - relocs] = 1;
13037 /* If we don't find a matching high part insn,
13038 something is fishy. Refuse to nop any high
13039 part insn in this section. */
13046 /* Do any further special processing. */
13052 case R_PPC64_ADDR16_HA:
13053 case R_PPC64_REL16_HA:
13054 case R_PPC64_ADDR16_HIGHERA:
13055 case R_PPC64_ADDR16_HIGHESTA:
13056 case R_PPC64_TOC16_HA:
13057 case R_PPC64_SECTOFF_HA:
13058 case R_PPC64_TPREL16_HA:
13059 case R_PPC64_DTPREL16_HA:
13060 case R_PPC64_TPREL16_HIGHER:
13061 case R_PPC64_TPREL16_HIGHERA:
13062 case R_PPC64_TPREL16_HIGHEST:
13063 case R_PPC64_TPREL16_HIGHESTA:
13064 case R_PPC64_DTPREL16_HIGHER:
13065 case R_PPC64_DTPREL16_HIGHERA:
13066 case R_PPC64_DTPREL16_HIGHEST:
13067 case R_PPC64_DTPREL16_HIGHESTA:
13068 /* It's just possible that this symbol is a weak symbol
13069 that's not actually defined anywhere. In that case,
13070 'sec' would be NULL, and we should leave the symbol
13071 alone (it will be set to zero elsewhere in the link). */
13076 case R_PPC64_GOT16_HA:
13077 case R_PPC64_PLTGOT16_HA:
13078 case R_PPC64_PLT16_HA:
13079 case R_PPC64_GOT_TLSGD16_HA:
13080 case R_PPC64_GOT_TLSLD16_HA:
13081 case R_PPC64_GOT_TPREL16_HA:
13082 case R_PPC64_GOT_DTPREL16_HA:
13083 /* Add 0x10000 if sign bit in 0:15 is set.
13084 Bits 0:15 are not used. */
13088 case R_PPC64_ADDR16_DS:
13089 case R_PPC64_ADDR16_LO_DS:
13090 case R_PPC64_GOT16_DS:
13091 case R_PPC64_GOT16_LO_DS:
13092 case R_PPC64_PLT16_LO_DS:
13093 case R_PPC64_SECTOFF_DS:
13094 case R_PPC64_SECTOFF_LO_DS:
13095 case R_PPC64_TOC16_DS:
13096 case R_PPC64_TOC16_LO_DS:
13097 case R_PPC64_PLTGOT16_DS:
13098 case R_PPC64_PLTGOT16_LO_DS:
13099 case R_PPC64_GOT_TPREL16_DS:
13100 case R_PPC64_GOT_TPREL16_LO_DS:
13101 case R_PPC64_GOT_DTPREL16_DS:
13102 case R_PPC64_GOT_DTPREL16_LO_DS:
13103 case R_PPC64_TPREL16_DS:
13104 case R_PPC64_TPREL16_LO_DS:
13105 case R_PPC64_DTPREL16_DS:
13106 case R_PPC64_DTPREL16_LO_DS:
13107 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
13109 /* If this reloc is against an lq insn, then the value must be
13110 a multiple of 16. This is somewhat of a hack, but the
13111 "correct" way to do this by defining _DQ forms of all the
13112 _DS relocs bloats all reloc switches in this file. It
13113 doesn't seem to make much sense to use any of these relocs
13114 in data, so testing the insn should be safe. */
13115 if ((insn & (0x3f << 26)) == (56u << 26))
13117 if (((relocation + addend) & mask) != 0)
13119 (*_bfd_error_handler)
13120 (_("%B: error: relocation %s not a multiple of %d"),
13122 ppc64_elf_howto_table[r_type]->name,
13124 bfd_set_error (bfd_error_bad_value);
13131 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13132 because such sections are not SEC_ALLOC and thus ld.so will
13133 not process them. */
13134 if (unresolved_reloc
13135 && !((input_section->flags & SEC_DEBUGGING) != 0
13136 && h->elf.def_dynamic))
13138 (*_bfd_error_handler)
13139 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
13142 (long) rel->r_offset,
13143 ppc64_elf_howto_table[(int) r_type]->name,
13144 h->elf.root.root.string);
13148 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
13156 if (r != bfd_reloc_ok)
13158 if (sym_name == NULL)
13159 sym_name = "(null)";
13160 if (r == bfd_reloc_overflow)
13165 && h->elf.root.type == bfd_link_hash_undefweak
13166 && ppc64_elf_howto_table[r_type]->pc_relative)
13168 /* Assume this is a call protected by other code that
13169 detects the symbol is undefined. If this is the case,
13170 we can safely ignore the overflow. If not, the
13171 program is hosed anyway, and a little warning isn't
13177 if (!((*info->callbacks->reloc_overflow)
13178 (info, (h ? &h->elf.root : NULL), sym_name,
13179 ppc64_elf_howto_table[r_type]->name,
13180 orig_addend, input_bfd, input_section, rel->r_offset)))
13185 (*_bfd_error_handler)
13186 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
13189 (long) rel->r_offset,
13190 ppc64_elf_howto_table[r_type]->name,
13198 if (ha_opt != NULL)
13202 unsigned char *opt = ha_opt;
13204 relend = relocs + input_section->reloc_count;
13205 for (; rel < relend; opt++, rel++)
13208 bfd_byte *p = contents + (rel->r_offset & ~3);
13209 bfd_put_32 (input_bfd, NOP, p);
13215 /* If we're emitting relocations, then shortly after this function
13216 returns, reloc offsets and addends for this section will be
13217 adjusted. Worse, reloc symbol indices will be for the output
13218 file rather than the input. Save a copy of the relocs for
13219 opd_entry_value. */
13220 if (is_opd && (info->emitrelocations || info->relocatable))
13223 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
13224 rel = bfd_alloc (input_bfd, amt);
13225 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
13226 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
13229 memcpy (rel, relocs, amt);
13234 /* Adjust the value of any local symbols in opd sections. */
13237 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
13238 const char *name ATTRIBUTE_UNUSED,
13239 Elf_Internal_Sym *elfsym,
13240 asection *input_sec,
13241 struct elf_link_hash_entry *h)
13243 struct _opd_sec_data *opd;
13250 opd = get_opd_info (input_sec);
13251 if (opd == NULL || opd->adjust == NULL)
13254 value = elfsym->st_value - input_sec->output_offset;
13255 if (!info->relocatable)
13256 value -= input_sec->output_section->vma;
13258 adjust = opd->adjust[value / 8];
13262 elfsym->st_value += adjust;
13266 /* Finish up dynamic symbol handling. We set the contents of various
13267 dynamic sections here. */
13270 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
13271 struct bfd_link_info *info,
13272 struct elf_link_hash_entry *h,
13273 Elf_Internal_Sym *sym)
13275 struct ppc_link_hash_table *htab;
13276 struct plt_entry *ent;
13277 Elf_Internal_Rela rela;
13280 htab = ppc_hash_table (info);
13284 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13285 if (ent->plt.offset != (bfd_vma) -1)
13287 /* This symbol has an entry in the procedure linkage
13288 table. Set it up. */
13289 if (!htab->elf.dynamic_sections_created
13290 || h->dynindx == -1)
13292 BFD_ASSERT (h->type == STT_GNU_IFUNC
13294 && (h->root.type == bfd_link_hash_defined
13295 || h->root.type == bfd_link_hash_defweak));
13296 rela.r_offset = (htab->iplt->output_section->vma
13297 + htab->iplt->output_offset
13298 + ent->plt.offset);
13299 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13300 rela.r_addend = (h->root.u.def.value
13301 + h->root.u.def.section->output_offset
13302 + h->root.u.def.section->output_section->vma
13304 loc = (htab->reliplt->contents
13305 + (htab->reliplt->reloc_count++
13306 * sizeof (Elf64_External_Rela)));
13310 rela.r_offset = (htab->plt->output_section->vma
13311 + htab->plt->output_offset
13312 + ent->plt.offset);
13313 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13314 rela.r_addend = ent->addend;
13315 loc = (htab->relplt->contents
13316 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
13317 / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
13319 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13324 /* This symbol needs a copy reloc. Set it up. */
13326 if (h->dynindx == -1
13327 || (h->root.type != bfd_link_hash_defined
13328 && h->root.type != bfd_link_hash_defweak)
13329 || htab->relbss == NULL)
13332 rela.r_offset = (h->root.u.def.value
13333 + h->root.u.def.section->output_section->vma
13334 + h->root.u.def.section->output_offset);
13335 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
13337 loc = htab->relbss->contents;
13338 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
13339 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13342 /* Mark some specially defined symbols as absolute. */
13343 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
13344 sym->st_shndx = SHN_ABS;
13349 /* Used to decide how to sort relocs in an optimal manner for the
13350 dynamic linker, before writing them out. */
13352 static enum elf_reloc_type_class
13353 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
13355 enum elf_ppc64_reloc_type r_type;
13357 r_type = ELF64_R_TYPE (rela->r_info);
13360 case R_PPC64_RELATIVE:
13361 return reloc_class_relative;
13362 case R_PPC64_JMP_SLOT:
13363 return reloc_class_plt;
13365 return reloc_class_copy;
13367 return reloc_class_normal;
13371 /* Finish up the dynamic sections. */
13374 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
13375 struct bfd_link_info *info)
13377 struct ppc_link_hash_table *htab;
13381 htab = ppc_hash_table (info);
13385 dynobj = htab->elf.dynobj;
13386 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
13388 if (htab->elf.dynamic_sections_created)
13390 Elf64_External_Dyn *dyncon, *dynconend;
13392 if (sdyn == NULL || htab->got == NULL)
13395 dyncon = (Elf64_External_Dyn *) sdyn->contents;
13396 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
13397 for (; dyncon < dynconend; dyncon++)
13399 Elf_Internal_Dyn dyn;
13402 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
13409 case DT_PPC64_GLINK:
13411 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13412 /* We stupidly defined DT_PPC64_GLINK to be the start
13413 of glink rather than the first entry point, which is
13414 what ld.so needs, and now have a bigger stub to
13415 support automatic multiple TOCs. */
13416 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
13420 s = bfd_get_section_by_name (output_bfd, ".opd");
13423 dyn.d_un.d_ptr = s->vma;
13426 case DT_PPC64_OPDSZ:
13427 s = bfd_get_section_by_name (output_bfd, ".opd");
13430 dyn.d_un.d_val = s->size;
13435 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13440 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13444 dyn.d_un.d_val = htab->relplt->size;
13448 /* Don't count procedure linkage table relocs in the
13449 overall reloc count. */
13453 dyn.d_un.d_val -= s->size;
13457 /* We may not be using the standard ELF linker script.
13458 If .rela.plt is the first .rela section, we adjust
13459 DT_RELA to not include it. */
13463 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
13465 dyn.d_un.d_ptr += s->size;
13469 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
13473 if (htab->got != NULL && htab->got->size != 0)
13475 /* Fill in the first entry in the global offset table.
13476 We use it to hold the link-time TOCbase. */
13477 bfd_put_64 (output_bfd,
13478 elf_gp (output_bfd) + TOC_BASE_OFF,
13479 htab->got->contents);
13481 /* Set .got entry size. */
13482 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
13485 if (htab->plt != NULL && htab->plt->size != 0)
13487 /* Set .plt entry size. */
13488 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
13492 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
13493 brlt ourselves if emitrelocations. */
13494 if (htab->brlt != NULL
13495 && htab->brlt->reloc_count != 0
13496 && !_bfd_elf_link_output_relocs (output_bfd,
13498 elf_section_data (htab->brlt)->rela.hdr,
13499 elf_section_data (htab->brlt)->relocs,
13503 if (htab->glink != NULL
13504 && htab->glink->reloc_count != 0
13505 && !_bfd_elf_link_output_relocs (output_bfd,
13507 elf_section_data (htab->glink)->rela.hdr,
13508 elf_section_data (htab->glink)->relocs,
13512 /* We need to handle writing out multiple GOT sections ourselves,
13513 since we didn't add them to DYNOBJ. We know dynobj is the first
13515 while ((dynobj = dynobj->link_next) != NULL)
13519 if (!is_ppc64_elf (dynobj))
13522 s = ppc64_elf_tdata (dynobj)->got;
13525 && s->output_section != bfd_abs_section_ptr
13526 && !bfd_set_section_contents (output_bfd, s->output_section,
13527 s->contents, s->output_offset,
13530 s = ppc64_elf_tdata (dynobj)->relgot;
13533 && s->output_section != bfd_abs_section_ptr
13534 && !bfd_set_section_contents (output_bfd, s->output_section,
13535 s->contents, s->output_offset,
13543 #include "elf64-target.h"