1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010, 2011, 2012 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"
39 static bfd_reloc_status_type ppc64_elf_ha_reloc
40 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
41 static bfd_reloc_status_type ppc64_elf_branch_reloc
42 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
44 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
45 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
46 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
47 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
48 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
49 static bfd_reloc_status_type ppc64_elf_toc_reloc
50 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
51 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
52 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
53 static bfd_reloc_status_type ppc64_elf_toc64_reloc
54 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
55 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
56 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
57 static bfd_vma opd_entry_value
58 (asection *, bfd_vma, asection **, bfd_vma *);
60 #define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
61 #define TARGET_LITTLE_NAME "elf64-powerpcle"
62 #define TARGET_BIG_SYM bfd_elf64_powerpc_vec
63 #define TARGET_BIG_NAME "elf64-powerpc"
64 #define ELF_ARCH bfd_arch_powerpc
65 #define ELF_TARGET_ID PPC64_ELF_DATA
66 #define ELF_MACHINE_CODE EM_PPC64
67 #define ELF_MAXPAGESIZE 0x10000
68 #define ELF_COMMONPAGESIZE 0x1000
69 #define elf_info_to_howto ppc64_elf_info_to_howto
71 #define elf_backend_want_got_sym 0
72 #define elf_backend_want_plt_sym 0
73 #define elf_backend_plt_alignment 3
74 #define elf_backend_plt_not_loaded 1
75 #define elf_backend_got_header_size 8
76 #define elf_backend_can_gc_sections 1
77 #define elf_backend_can_refcount 1
78 #define elf_backend_rela_normal 1
79 #define elf_backend_default_execstack 0
81 #define bfd_elf64_mkobject ppc64_elf_mkobject
82 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
83 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
84 #define bfd_elf64_bfd_merge_private_bfd_data _bfd_generic_verify_endian_match
85 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
86 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
87 #define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
88 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
89 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
91 #define elf_backend_object_p ppc64_elf_object_p
92 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
93 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
94 #define elf_backend_write_core_note ppc64_elf_write_core_note
95 #define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
96 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
97 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
98 #define elf_backend_check_directives ppc64_elf_process_dot_syms
99 #define elf_backend_as_needed_cleanup ppc64_elf_as_needed_cleanup
100 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
101 #define elf_backend_check_relocs ppc64_elf_check_relocs
102 #define elf_backend_gc_keep ppc64_elf_gc_keep
103 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
104 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
105 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
106 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
107 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
108 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
109 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
110 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
111 #define elf_backend_action_discarded ppc64_elf_action_discarded
112 #define elf_backend_relocate_section ppc64_elf_relocate_section
113 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
114 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
115 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
116 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
117 #define elf_backend_special_sections ppc64_elf_special_sections
118 #define elf_backend_post_process_headers _bfd_elf_set_osabi
120 /* The name of the dynamic interpreter. This is put in the .interp
122 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
124 /* The size in bytes of an entry in the procedure linkage table. */
125 #define PLT_ENTRY_SIZE 24
127 /* The initial size of the plt reserved for the dynamic linker. */
128 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
130 /* TOC base pointers offset from start of TOC. */
131 #define TOC_BASE_OFF 0x8000
133 /* Offset of tp and dtp pointers from start of TLS block. */
134 #define TP_OFFSET 0x7000
135 #define DTP_OFFSET 0x8000
137 /* .plt call stub instructions. The normal stub is like this, but
138 sometimes the .plt entry crosses a 64k boundary and we need to
139 insert an addi to adjust r12. */
140 #define PLT_CALL_STUB_SIZE (7*4)
141 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
142 #define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
143 #define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
144 #define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
145 #define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
146 /* ld %r11,xxx+16@l(%r12) */
147 #define BCTR 0x4e800420 /* bctr */
150 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,off@ha */
151 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
152 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
153 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
155 #define XOR_R11_R11_R11 0x7d6b5a78 /* xor %r11,%r11,%r11 */
156 #define ADD_R12_R12_R11 0x7d8c5a14 /* add %r12,%r12,%r11 */
157 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
158 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
159 #define BNECTR 0x4ca20420 /* bnectr+ */
160 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
162 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
163 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
165 #define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
167 /* glink call stub instructions. We enter with the index in R0. */
168 #define GLINK_CALL_STUB_SIZE (16*4)
172 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
173 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
175 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
176 #define LD_R2_M16R11 0xe84bfff0 /* ld %2,(0b-1b)(%11) */
177 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
178 #define ADD_R12_R2_R11 0x7d825a14 /* add %12,%2,%11 */
186 #define NOP 0x60000000
188 /* Some other nops. */
189 #define CROR_151515 0x4def7b82
190 #define CROR_313131 0x4ffffb82
192 /* .glink entries for the first 32k functions are two instructions. */
193 #define LI_R0_0 0x38000000 /* li %r0,0 */
194 #define B_DOT 0x48000000 /* b . */
196 /* After that, we need two instructions to load the index, followed by
198 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
199 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
201 /* Instructions used by the save and restore reg functions. */
202 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
203 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
204 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
205 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
206 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
207 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
208 #define LI_R12_0 0x39800000 /* li %r12,0 */
209 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
210 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
211 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
212 #define BLR 0x4e800020 /* blr */
214 /* Since .opd is an array of descriptors and each entry will end up
215 with identical R_PPC64_RELATIVE relocs, there is really no need to
216 propagate .opd relocs; The dynamic linker should be taught to
217 relocate .opd without reloc entries. */
218 #ifndef NO_OPD_RELOCS
219 #define NO_OPD_RELOCS 0
222 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
224 /* Relocation HOWTO's. */
225 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
227 static reloc_howto_type ppc64_elf_howto_raw[] = {
228 /* This reloc does nothing. */
229 HOWTO (R_PPC64_NONE, /* type */
231 2, /* size (0 = byte, 1 = short, 2 = long) */
233 FALSE, /* pc_relative */
235 complain_overflow_dont, /* complain_on_overflow */
236 bfd_elf_generic_reloc, /* special_function */
237 "R_PPC64_NONE", /* name */
238 FALSE, /* partial_inplace */
241 FALSE), /* pcrel_offset */
243 /* A standard 32 bit relocation. */
244 HOWTO (R_PPC64_ADDR32, /* type */
246 2, /* size (0 = byte, 1 = short, 2 = long) */
248 FALSE, /* pc_relative */
250 complain_overflow_bitfield, /* complain_on_overflow */
251 bfd_elf_generic_reloc, /* special_function */
252 "R_PPC64_ADDR32", /* name */
253 FALSE, /* partial_inplace */
255 0xffffffff, /* dst_mask */
256 FALSE), /* pcrel_offset */
258 /* An absolute 26 bit branch; the lower two bits must be zero.
259 FIXME: we don't check that, we just clear them. */
260 HOWTO (R_PPC64_ADDR24, /* type */
262 2, /* size (0 = byte, 1 = short, 2 = long) */
264 FALSE, /* pc_relative */
266 complain_overflow_bitfield, /* complain_on_overflow */
267 bfd_elf_generic_reloc, /* special_function */
268 "R_PPC64_ADDR24", /* name */
269 FALSE, /* partial_inplace */
271 0x03fffffc, /* dst_mask */
272 FALSE), /* pcrel_offset */
274 /* A standard 16 bit relocation. */
275 HOWTO (R_PPC64_ADDR16, /* type */
277 1, /* size (0 = byte, 1 = short, 2 = long) */
279 FALSE, /* pc_relative */
281 complain_overflow_bitfield, /* complain_on_overflow */
282 bfd_elf_generic_reloc, /* special_function */
283 "R_PPC64_ADDR16", /* name */
284 FALSE, /* partial_inplace */
286 0xffff, /* dst_mask */
287 FALSE), /* pcrel_offset */
289 /* A 16 bit relocation without overflow. */
290 HOWTO (R_PPC64_ADDR16_LO, /* type */
292 1, /* size (0 = byte, 1 = short, 2 = long) */
294 FALSE, /* pc_relative */
296 complain_overflow_dont,/* complain_on_overflow */
297 bfd_elf_generic_reloc, /* special_function */
298 "R_PPC64_ADDR16_LO", /* name */
299 FALSE, /* partial_inplace */
301 0xffff, /* dst_mask */
302 FALSE), /* pcrel_offset */
304 /* Bits 16-31 of an address. */
305 HOWTO (R_PPC64_ADDR16_HI, /* type */
307 1, /* size (0 = byte, 1 = short, 2 = long) */
309 FALSE, /* pc_relative */
311 complain_overflow_dont, /* complain_on_overflow */
312 bfd_elf_generic_reloc, /* special_function */
313 "R_PPC64_ADDR16_HI", /* name */
314 FALSE, /* partial_inplace */
316 0xffff, /* dst_mask */
317 FALSE), /* pcrel_offset */
319 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
320 bits, treated as a signed number, is negative. */
321 HOWTO (R_PPC64_ADDR16_HA, /* type */
323 1, /* size (0 = byte, 1 = short, 2 = long) */
325 FALSE, /* pc_relative */
327 complain_overflow_dont, /* complain_on_overflow */
328 ppc64_elf_ha_reloc, /* special_function */
329 "R_PPC64_ADDR16_HA", /* name */
330 FALSE, /* partial_inplace */
332 0xffff, /* dst_mask */
333 FALSE), /* pcrel_offset */
335 /* An absolute 16 bit branch; the lower two bits must be zero.
336 FIXME: we don't check that, we just clear them. */
337 HOWTO (R_PPC64_ADDR14, /* type */
339 2, /* size (0 = byte, 1 = short, 2 = long) */
341 FALSE, /* pc_relative */
343 complain_overflow_bitfield, /* complain_on_overflow */
344 ppc64_elf_branch_reloc, /* special_function */
345 "R_PPC64_ADDR14", /* name */
346 FALSE, /* partial_inplace */
348 0x0000fffc, /* dst_mask */
349 FALSE), /* pcrel_offset */
351 /* An absolute 16 bit branch, for which bit 10 should be set to
352 indicate that the branch is expected to be taken. The lower two
353 bits must be zero. */
354 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
356 2, /* size (0 = byte, 1 = short, 2 = long) */
358 FALSE, /* pc_relative */
360 complain_overflow_bitfield, /* complain_on_overflow */
361 ppc64_elf_brtaken_reloc, /* special_function */
362 "R_PPC64_ADDR14_BRTAKEN",/* name */
363 FALSE, /* partial_inplace */
365 0x0000fffc, /* dst_mask */
366 FALSE), /* pcrel_offset */
368 /* An absolute 16 bit branch, for which bit 10 should be set to
369 indicate that the branch is not expected to be taken. The lower
370 two bits must be zero. */
371 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
373 2, /* size (0 = byte, 1 = short, 2 = long) */
375 FALSE, /* pc_relative */
377 complain_overflow_bitfield, /* complain_on_overflow */
378 ppc64_elf_brtaken_reloc, /* special_function */
379 "R_PPC64_ADDR14_BRNTAKEN",/* name */
380 FALSE, /* partial_inplace */
382 0x0000fffc, /* dst_mask */
383 FALSE), /* pcrel_offset */
385 /* A relative 26 bit branch; the lower two bits must be zero. */
386 HOWTO (R_PPC64_REL24, /* type */
388 2, /* size (0 = byte, 1 = short, 2 = long) */
390 TRUE, /* pc_relative */
392 complain_overflow_signed, /* complain_on_overflow */
393 ppc64_elf_branch_reloc, /* special_function */
394 "R_PPC64_REL24", /* name */
395 FALSE, /* partial_inplace */
397 0x03fffffc, /* dst_mask */
398 TRUE), /* pcrel_offset */
400 /* A relative 16 bit branch; the lower two bits must be zero. */
401 HOWTO (R_PPC64_REL14, /* type */
403 2, /* size (0 = byte, 1 = short, 2 = long) */
405 TRUE, /* pc_relative */
407 complain_overflow_signed, /* complain_on_overflow */
408 ppc64_elf_branch_reloc, /* special_function */
409 "R_PPC64_REL14", /* name */
410 FALSE, /* partial_inplace */
412 0x0000fffc, /* dst_mask */
413 TRUE), /* pcrel_offset */
415 /* A relative 16 bit branch. Bit 10 should be set to indicate that
416 the branch is expected to be taken. The lower two bits must be
418 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
420 2, /* size (0 = byte, 1 = short, 2 = long) */
422 TRUE, /* pc_relative */
424 complain_overflow_signed, /* complain_on_overflow */
425 ppc64_elf_brtaken_reloc, /* special_function */
426 "R_PPC64_REL14_BRTAKEN", /* name */
427 FALSE, /* partial_inplace */
429 0x0000fffc, /* dst_mask */
430 TRUE), /* pcrel_offset */
432 /* A relative 16 bit branch. Bit 10 should be set to indicate that
433 the branch is not expected to be taken. The lower two bits must
435 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
437 2, /* size (0 = byte, 1 = short, 2 = long) */
439 TRUE, /* pc_relative */
441 complain_overflow_signed, /* complain_on_overflow */
442 ppc64_elf_brtaken_reloc, /* special_function */
443 "R_PPC64_REL14_BRNTAKEN",/* name */
444 FALSE, /* partial_inplace */
446 0x0000fffc, /* dst_mask */
447 TRUE), /* pcrel_offset */
449 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
451 HOWTO (R_PPC64_GOT16, /* type */
453 1, /* size (0 = byte, 1 = short, 2 = long) */
455 FALSE, /* pc_relative */
457 complain_overflow_signed, /* complain_on_overflow */
458 ppc64_elf_unhandled_reloc, /* special_function */
459 "R_PPC64_GOT16", /* name */
460 FALSE, /* partial_inplace */
462 0xffff, /* dst_mask */
463 FALSE), /* pcrel_offset */
465 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
467 HOWTO (R_PPC64_GOT16_LO, /* type */
469 1, /* size (0 = byte, 1 = short, 2 = long) */
471 FALSE, /* pc_relative */
473 complain_overflow_dont, /* complain_on_overflow */
474 ppc64_elf_unhandled_reloc, /* special_function */
475 "R_PPC64_GOT16_LO", /* name */
476 FALSE, /* partial_inplace */
478 0xffff, /* dst_mask */
479 FALSE), /* pcrel_offset */
481 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
483 HOWTO (R_PPC64_GOT16_HI, /* type */
485 1, /* size (0 = byte, 1 = short, 2 = long) */
487 FALSE, /* pc_relative */
489 complain_overflow_dont,/* complain_on_overflow */
490 ppc64_elf_unhandled_reloc, /* special_function */
491 "R_PPC64_GOT16_HI", /* name */
492 FALSE, /* partial_inplace */
494 0xffff, /* dst_mask */
495 FALSE), /* pcrel_offset */
497 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
499 HOWTO (R_PPC64_GOT16_HA, /* type */
501 1, /* size (0 = byte, 1 = short, 2 = long) */
503 FALSE, /* pc_relative */
505 complain_overflow_dont,/* complain_on_overflow */
506 ppc64_elf_unhandled_reloc, /* special_function */
507 "R_PPC64_GOT16_HA", /* name */
508 FALSE, /* partial_inplace */
510 0xffff, /* dst_mask */
511 FALSE), /* pcrel_offset */
513 /* This is used only by the dynamic linker. The symbol should exist
514 both in the object being run and in some shared library. The
515 dynamic linker copies the data addressed by the symbol from the
516 shared library into the object, because the object being
517 run has to have the data at some particular address. */
518 HOWTO (R_PPC64_COPY, /* type */
520 0, /* this one is variable size */
522 FALSE, /* pc_relative */
524 complain_overflow_dont, /* complain_on_overflow */
525 ppc64_elf_unhandled_reloc, /* special_function */
526 "R_PPC64_COPY", /* name */
527 FALSE, /* partial_inplace */
530 FALSE), /* pcrel_offset */
532 /* Like R_PPC64_ADDR64, but used when setting global offset table
534 HOWTO (R_PPC64_GLOB_DAT, /* type */
536 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
538 FALSE, /* pc_relative */
540 complain_overflow_dont, /* complain_on_overflow */
541 ppc64_elf_unhandled_reloc, /* special_function */
542 "R_PPC64_GLOB_DAT", /* name */
543 FALSE, /* partial_inplace */
545 ONES (64), /* dst_mask */
546 FALSE), /* pcrel_offset */
548 /* Created by the link editor. Marks a procedure linkage table
549 entry for a symbol. */
550 HOWTO (R_PPC64_JMP_SLOT, /* type */
552 0, /* size (0 = byte, 1 = short, 2 = long) */
554 FALSE, /* pc_relative */
556 complain_overflow_dont, /* complain_on_overflow */
557 ppc64_elf_unhandled_reloc, /* special_function */
558 "R_PPC64_JMP_SLOT", /* name */
559 FALSE, /* partial_inplace */
562 FALSE), /* pcrel_offset */
564 /* Used only by the dynamic linker. When the object is run, this
565 doubleword64 is set to the load address of the object, plus the
567 HOWTO (R_PPC64_RELATIVE, /* type */
569 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
571 FALSE, /* pc_relative */
573 complain_overflow_dont, /* complain_on_overflow */
574 bfd_elf_generic_reloc, /* special_function */
575 "R_PPC64_RELATIVE", /* name */
576 FALSE, /* partial_inplace */
578 ONES (64), /* dst_mask */
579 FALSE), /* pcrel_offset */
581 /* Like R_PPC64_ADDR32, but may be unaligned. */
582 HOWTO (R_PPC64_UADDR32, /* type */
584 2, /* size (0 = byte, 1 = short, 2 = long) */
586 FALSE, /* pc_relative */
588 complain_overflow_bitfield, /* complain_on_overflow */
589 bfd_elf_generic_reloc, /* special_function */
590 "R_PPC64_UADDR32", /* name */
591 FALSE, /* partial_inplace */
593 0xffffffff, /* dst_mask */
594 FALSE), /* pcrel_offset */
596 /* Like R_PPC64_ADDR16, but may be unaligned. */
597 HOWTO (R_PPC64_UADDR16, /* type */
599 1, /* size (0 = byte, 1 = short, 2 = long) */
601 FALSE, /* pc_relative */
603 complain_overflow_bitfield, /* complain_on_overflow */
604 bfd_elf_generic_reloc, /* special_function */
605 "R_PPC64_UADDR16", /* name */
606 FALSE, /* partial_inplace */
608 0xffff, /* dst_mask */
609 FALSE), /* pcrel_offset */
611 /* 32-bit PC relative. */
612 HOWTO (R_PPC64_REL32, /* type */
614 2, /* size (0 = byte, 1 = short, 2 = long) */
616 TRUE, /* pc_relative */
618 /* FIXME: Verify. Was complain_overflow_bitfield. */
619 complain_overflow_signed, /* complain_on_overflow */
620 bfd_elf_generic_reloc, /* special_function */
621 "R_PPC64_REL32", /* name */
622 FALSE, /* partial_inplace */
624 0xffffffff, /* dst_mask */
625 TRUE), /* pcrel_offset */
627 /* 32-bit relocation to the symbol's procedure linkage table. */
628 HOWTO (R_PPC64_PLT32, /* type */
630 2, /* size (0 = byte, 1 = short, 2 = long) */
632 FALSE, /* pc_relative */
634 complain_overflow_bitfield, /* complain_on_overflow */
635 ppc64_elf_unhandled_reloc, /* special_function */
636 "R_PPC64_PLT32", /* name */
637 FALSE, /* partial_inplace */
639 0xffffffff, /* dst_mask */
640 FALSE), /* pcrel_offset */
642 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
643 FIXME: R_PPC64_PLTREL32 not supported. */
644 HOWTO (R_PPC64_PLTREL32, /* type */
646 2, /* size (0 = byte, 1 = short, 2 = long) */
648 TRUE, /* pc_relative */
650 complain_overflow_signed, /* complain_on_overflow */
651 bfd_elf_generic_reloc, /* special_function */
652 "R_PPC64_PLTREL32", /* name */
653 FALSE, /* partial_inplace */
655 0xffffffff, /* dst_mask */
656 TRUE), /* pcrel_offset */
658 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
660 HOWTO (R_PPC64_PLT16_LO, /* type */
662 1, /* size (0 = byte, 1 = short, 2 = long) */
664 FALSE, /* pc_relative */
666 complain_overflow_dont, /* complain_on_overflow */
667 ppc64_elf_unhandled_reloc, /* special_function */
668 "R_PPC64_PLT16_LO", /* name */
669 FALSE, /* partial_inplace */
671 0xffff, /* dst_mask */
672 FALSE), /* pcrel_offset */
674 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
676 HOWTO (R_PPC64_PLT16_HI, /* type */
678 1, /* size (0 = byte, 1 = short, 2 = long) */
680 FALSE, /* pc_relative */
682 complain_overflow_dont, /* complain_on_overflow */
683 ppc64_elf_unhandled_reloc, /* special_function */
684 "R_PPC64_PLT16_HI", /* name */
685 FALSE, /* partial_inplace */
687 0xffff, /* dst_mask */
688 FALSE), /* pcrel_offset */
690 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
692 HOWTO (R_PPC64_PLT16_HA, /* type */
694 1, /* size (0 = byte, 1 = short, 2 = long) */
696 FALSE, /* pc_relative */
698 complain_overflow_dont, /* complain_on_overflow */
699 ppc64_elf_unhandled_reloc, /* special_function */
700 "R_PPC64_PLT16_HA", /* name */
701 FALSE, /* partial_inplace */
703 0xffff, /* dst_mask */
704 FALSE), /* pcrel_offset */
706 /* 16-bit section relative relocation. */
707 HOWTO (R_PPC64_SECTOFF, /* type */
709 1, /* size (0 = byte, 1 = short, 2 = long) */
711 FALSE, /* pc_relative */
713 complain_overflow_bitfield, /* complain_on_overflow */
714 ppc64_elf_sectoff_reloc, /* special_function */
715 "R_PPC64_SECTOFF", /* name */
716 FALSE, /* partial_inplace */
718 0xffff, /* dst_mask */
719 FALSE), /* pcrel_offset */
721 /* Like R_PPC64_SECTOFF, but no overflow warning. */
722 HOWTO (R_PPC64_SECTOFF_LO, /* type */
724 1, /* size (0 = byte, 1 = short, 2 = long) */
726 FALSE, /* pc_relative */
728 complain_overflow_dont, /* complain_on_overflow */
729 ppc64_elf_sectoff_reloc, /* special_function */
730 "R_PPC64_SECTOFF_LO", /* name */
731 FALSE, /* partial_inplace */
733 0xffff, /* dst_mask */
734 FALSE), /* pcrel_offset */
736 /* 16-bit upper half section relative relocation. */
737 HOWTO (R_PPC64_SECTOFF_HI, /* type */
739 1, /* size (0 = byte, 1 = short, 2 = long) */
741 FALSE, /* pc_relative */
743 complain_overflow_dont, /* complain_on_overflow */
744 ppc64_elf_sectoff_reloc, /* special_function */
745 "R_PPC64_SECTOFF_HI", /* name */
746 FALSE, /* partial_inplace */
748 0xffff, /* dst_mask */
749 FALSE), /* pcrel_offset */
751 /* 16-bit upper half adjusted section relative relocation. */
752 HOWTO (R_PPC64_SECTOFF_HA, /* type */
754 1, /* size (0 = byte, 1 = short, 2 = long) */
756 FALSE, /* pc_relative */
758 complain_overflow_dont, /* complain_on_overflow */
759 ppc64_elf_sectoff_ha_reloc, /* special_function */
760 "R_PPC64_SECTOFF_HA", /* name */
761 FALSE, /* partial_inplace */
763 0xffff, /* dst_mask */
764 FALSE), /* pcrel_offset */
766 /* Like R_PPC64_REL24 without touching the two least significant bits. */
767 HOWTO (R_PPC64_REL30, /* type */
769 2, /* size (0 = byte, 1 = short, 2 = long) */
771 TRUE, /* pc_relative */
773 complain_overflow_dont, /* complain_on_overflow */
774 bfd_elf_generic_reloc, /* special_function */
775 "R_PPC64_REL30", /* name */
776 FALSE, /* partial_inplace */
778 0xfffffffc, /* dst_mask */
779 TRUE), /* pcrel_offset */
781 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
783 /* A standard 64-bit relocation. */
784 HOWTO (R_PPC64_ADDR64, /* type */
786 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
788 FALSE, /* pc_relative */
790 complain_overflow_dont, /* complain_on_overflow */
791 bfd_elf_generic_reloc, /* special_function */
792 "R_PPC64_ADDR64", /* name */
793 FALSE, /* partial_inplace */
795 ONES (64), /* dst_mask */
796 FALSE), /* pcrel_offset */
798 /* The bits 32-47 of an address. */
799 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
801 1, /* size (0 = byte, 1 = short, 2 = long) */
803 FALSE, /* pc_relative */
805 complain_overflow_dont, /* complain_on_overflow */
806 bfd_elf_generic_reloc, /* special_function */
807 "R_PPC64_ADDR16_HIGHER", /* name */
808 FALSE, /* partial_inplace */
810 0xffff, /* dst_mask */
811 FALSE), /* pcrel_offset */
813 /* The bits 32-47 of an address, plus 1 if the contents of the low
814 16 bits, treated as a signed number, is negative. */
815 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
817 1, /* size (0 = byte, 1 = short, 2 = long) */
819 FALSE, /* pc_relative */
821 complain_overflow_dont, /* complain_on_overflow */
822 ppc64_elf_ha_reloc, /* special_function */
823 "R_PPC64_ADDR16_HIGHERA", /* name */
824 FALSE, /* partial_inplace */
826 0xffff, /* dst_mask */
827 FALSE), /* pcrel_offset */
829 /* The bits 48-63 of an address. */
830 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
832 1, /* size (0 = byte, 1 = short, 2 = long) */
834 FALSE, /* pc_relative */
836 complain_overflow_dont, /* complain_on_overflow */
837 bfd_elf_generic_reloc, /* special_function */
838 "R_PPC64_ADDR16_HIGHEST", /* name */
839 FALSE, /* partial_inplace */
841 0xffff, /* dst_mask */
842 FALSE), /* pcrel_offset */
844 /* The bits 48-63 of an address, plus 1 if the contents of the low
845 16 bits, treated as a signed number, is negative. */
846 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
848 1, /* size (0 = byte, 1 = short, 2 = long) */
850 FALSE, /* pc_relative */
852 complain_overflow_dont, /* complain_on_overflow */
853 ppc64_elf_ha_reloc, /* special_function */
854 "R_PPC64_ADDR16_HIGHESTA", /* name */
855 FALSE, /* partial_inplace */
857 0xffff, /* dst_mask */
858 FALSE), /* pcrel_offset */
860 /* Like ADDR64, but may be unaligned. */
861 HOWTO (R_PPC64_UADDR64, /* type */
863 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
865 FALSE, /* pc_relative */
867 complain_overflow_dont, /* complain_on_overflow */
868 bfd_elf_generic_reloc, /* special_function */
869 "R_PPC64_UADDR64", /* name */
870 FALSE, /* partial_inplace */
872 ONES (64), /* dst_mask */
873 FALSE), /* pcrel_offset */
875 /* 64-bit relative relocation. */
876 HOWTO (R_PPC64_REL64, /* type */
878 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
880 TRUE, /* pc_relative */
882 complain_overflow_dont, /* complain_on_overflow */
883 bfd_elf_generic_reloc, /* special_function */
884 "R_PPC64_REL64", /* name */
885 FALSE, /* partial_inplace */
887 ONES (64), /* dst_mask */
888 TRUE), /* pcrel_offset */
890 /* 64-bit relocation to the symbol's procedure linkage table. */
891 HOWTO (R_PPC64_PLT64, /* type */
893 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
895 FALSE, /* pc_relative */
897 complain_overflow_dont, /* complain_on_overflow */
898 ppc64_elf_unhandled_reloc, /* special_function */
899 "R_PPC64_PLT64", /* name */
900 FALSE, /* partial_inplace */
902 ONES (64), /* dst_mask */
903 FALSE), /* pcrel_offset */
905 /* 64-bit PC relative relocation to the symbol's procedure linkage
907 /* FIXME: R_PPC64_PLTREL64 not supported. */
908 HOWTO (R_PPC64_PLTREL64, /* type */
910 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
912 TRUE, /* pc_relative */
914 complain_overflow_dont, /* complain_on_overflow */
915 ppc64_elf_unhandled_reloc, /* special_function */
916 "R_PPC64_PLTREL64", /* name */
917 FALSE, /* partial_inplace */
919 ONES (64), /* dst_mask */
920 TRUE), /* pcrel_offset */
922 /* 16 bit TOC-relative relocation. */
924 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
925 HOWTO (R_PPC64_TOC16, /* type */
927 1, /* size (0 = byte, 1 = short, 2 = long) */
929 FALSE, /* pc_relative */
931 complain_overflow_signed, /* complain_on_overflow */
932 ppc64_elf_toc_reloc, /* special_function */
933 "R_PPC64_TOC16", /* name */
934 FALSE, /* partial_inplace */
936 0xffff, /* dst_mask */
937 FALSE), /* pcrel_offset */
939 /* 16 bit TOC-relative relocation without overflow. */
941 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
942 HOWTO (R_PPC64_TOC16_LO, /* type */
944 1, /* size (0 = byte, 1 = short, 2 = long) */
946 FALSE, /* pc_relative */
948 complain_overflow_dont, /* complain_on_overflow */
949 ppc64_elf_toc_reloc, /* special_function */
950 "R_PPC64_TOC16_LO", /* name */
951 FALSE, /* partial_inplace */
953 0xffff, /* dst_mask */
954 FALSE), /* pcrel_offset */
956 /* 16 bit TOC-relative relocation, high 16 bits. */
958 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
959 HOWTO (R_PPC64_TOC16_HI, /* type */
961 1, /* size (0 = byte, 1 = short, 2 = long) */
963 FALSE, /* pc_relative */
965 complain_overflow_dont, /* complain_on_overflow */
966 ppc64_elf_toc_reloc, /* special_function */
967 "R_PPC64_TOC16_HI", /* name */
968 FALSE, /* partial_inplace */
970 0xffff, /* dst_mask */
971 FALSE), /* pcrel_offset */
973 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
974 contents of the low 16 bits, treated as a signed number, is
977 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
978 HOWTO (R_PPC64_TOC16_HA, /* type */
980 1, /* size (0 = byte, 1 = short, 2 = long) */
982 FALSE, /* pc_relative */
984 complain_overflow_dont, /* complain_on_overflow */
985 ppc64_elf_toc_ha_reloc, /* special_function */
986 "R_PPC64_TOC16_HA", /* name */
987 FALSE, /* partial_inplace */
989 0xffff, /* dst_mask */
990 FALSE), /* pcrel_offset */
992 /* 64-bit relocation; insert value of TOC base (.TOC.). */
994 /* R_PPC64_TOC 51 doubleword64 .TOC. */
995 HOWTO (R_PPC64_TOC, /* type */
997 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
999 FALSE, /* pc_relative */
1001 complain_overflow_bitfield, /* complain_on_overflow */
1002 ppc64_elf_toc64_reloc, /* special_function */
1003 "R_PPC64_TOC", /* name */
1004 FALSE, /* partial_inplace */
1006 ONES (64), /* dst_mask */
1007 FALSE), /* pcrel_offset */
1009 /* Like R_PPC64_GOT16, but also informs the link editor that the
1010 value to relocate may (!) refer to a PLT entry which the link
1011 editor (a) may replace with the symbol value. If the link editor
1012 is unable to fully resolve the symbol, it may (b) create a PLT
1013 entry and store the address to the new PLT entry in the GOT.
1014 This permits lazy resolution of function symbols at run time.
1015 The link editor may also skip all of this and just (c) emit a
1016 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1017 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1018 HOWTO (R_PPC64_PLTGOT16, /* type */
1020 1, /* size (0 = byte, 1 = short, 2 = long) */
1022 FALSE, /* pc_relative */
1024 complain_overflow_signed, /* complain_on_overflow */
1025 ppc64_elf_unhandled_reloc, /* special_function */
1026 "R_PPC64_PLTGOT16", /* name */
1027 FALSE, /* partial_inplace */
1029 0xffff, /* dst_mask */
1030 FALSE), /* pcrel_offset */
1032 /* Like R_PPC64_PLTGOT16, but without overflow. */
1033 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1034 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1036 1, /* size (0 = byte, 1 = short, 2 = long) */
1038 FALSE, /* pc_relative */
1040 complain_overflow_dont, /* complain_on_overflow */
1041 ppc64_elf_unhandled_reloc, /* special_function */
1042 "R_PPC64_PLTGOT16_LO", /* name */
1043 FALSE, /* partial_inplace */
1045 0xffff, /* dst_mask */
1046 FALSE), /* pcrel_offset */
1048 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1049 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1050 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1051 16, /* rightshift */
1052 1, /* size (0 = byte, 1 = short, 2 = long) */
1054 FALSE, /* pc_relative */
1056 complain_overflow_dont, /* complain_on_overflow */
1057 ppc64_elf_unhandled_reloc, /* special_function */
1058 "R_PPC64_PLTGOT16_HI", /* name */
1059 FALSE, /* partial_inplace */
1061 0xffff, /* dst_mask */
1062 FALSE), /* pcrel_offset */
1064 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1065 1 if the contents of the low 16 bits, treated as a signed number,
1067 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1068 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1069 16, /* rightshift */
1070 1, /* size (0 = byte, 1 = short, 2 = long) */
1072 FALSE, /* pc_relative */
1074 complain_overflow_dont,/* complain_on_overflow */
1075 ppc64_elf_unhandled_reloc, /* special_function */
1076 "R_PPC64_PLTGOT16_HA", /* name */
1077 FALSE, /* partial_inplace */
1079 0xffff, /* dst_mask */
1080 FALSE), /* pcrel_offset */
1082 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1083 HOWTO (R_PPC64_ADDR16_DS, /* type */
1085 1, /* size (0 = byte, 1 = short, 2 = long) */
1087 FALSE, /* pc_relative */
1089 complain_overflow_bitfield, /* complain_on_overflow */
1090 bfd_elf_generic_reloc, /* special_function */
1091 "R_PPC64_ADDR16_DS", /* name */
1092 FALSE, /* partial_inplace */
1094 0xfffc, /* dst_mask */
1095 FALSE), /* pcrel_offset */
1097 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1098 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1100 1, /* size (0 = byte, 1 = short, 2 = long) */
1102 FALSE, /* pc_relative */
1104 complain_overflow_dont,/* complain_on_overflow */
1105 bfd_elf_generic_reloc, /* special_function */
1106 "R_PPC64_ADDR16_LO_DS",/* name */
1107 FALSE, /* partial_inplace */
1109 0xfffc, /* dst_mask */
1110 FALSE), /* pcrel_offset */
1112 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1113 HOWTO (R_PPC64_GOT16_DS, /* type */
1115 1, /* size (0 = byte, 1 = short, 2 = long) */
1117 FALSE, /* pc_relative */
1119 complain_overflow_signed, /* complain_on_overflow */
1120 ppc64_elf_unhandled_reloc, /* special_function */
1121 "R_PPC64_GOT16_DS", /* name */
1122 FALSE, /* partial_inplace */
1124 0xfffc, /* dst_mask */
1125 FALSE), /* pcrel_offset */
1127 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1128 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1130 1, /* size (0 = byte, 1 = short, 2 = long) */
1132 FALSE, /* pc_relative */
1134 complain_overflow_dont, /* complain_on_overflow */
1135 ppc64_elf_unhandled_reloc, /* special_function */
1136 "R_PPC64_GOT16_LO_DS", /* name */
1137 FALSE, /* partial_inplace */
1139 0xfffc, /* dst_mask */
1140 FALSE), /* pcrel_offset */
1142 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1143 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1145 1, /* size (0 = byte, 1 = short, 2 = long) */
1147 FALSE, /* pc_relative */
1149 complain_overflow_dont, /* complain_on_overflow */
1150 ppc64_elf_unhandled_reloc, /* special_function */
1151 "R_PPC64_PLT16_LO_DS", /* name */
1152 FALSE, /* partial_inplace */
1154 0xfffc, /* dst_mask */
1155 FALSE), /* pcrel_offset */
1157 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1158 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1160 1, /* size (0 = byte, 1 = short, 2 = long) */
1162 FALSE, /* pc_relative */
1164 complain_overflow_bitfield, /* complain_on_overflow */
1165 ppc64_elf_sectoff_reloc, /* special_function */
1166 "R_PPC64_SECTOFF_DS", /* name */
1167 FALSE, /* partial_inplace */
1169 0xfffc, /* dst_mask */
1170 FALSE), /* pcrel_offset */
1172 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1173 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1175 1, /* size (0 = byte, 1 = short, 2 = long) */
1177 FALSE, /* pc_relative */
1179 complain_overflow_dont, /* complain_on_overflow */
1180 ppc64_elf_sectoff_reloc, /* special_function */
1181 "R_PPC64_SECTOFF_LO_DS",/* name */
1182 FALSE, /* partial_inplace */
1184 0xfffc, /* dst_mask */
1185 FALSE), /* pcrel_offset */
1187 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1188 HOWTO (R_PPC64_TOC16_DS, /* type */
1190 1, /* size (0 = byte, 1 = short, 2 = long) */
1192 FALSE, /* pc_relative */
1194 complain_overflow_signed, /* complain_on_overflow */
1195 ppc64_elf_toc_reloc, /* special_function */
1196 "R_PPC64_TOC16_DS", /* name */
1197 FALSE, /* partial_inplace */
1199 0xfffc, /* dst_mask */
1200 FALSE), /* pcrel_offset */
1202 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1203 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1205 1, /* size (0 = byte, 1 = short, 2 = long) */
1207 FALSE, /* pc_relative */
1209 complain_overflow_dont, /* complain_on_overflow */
1210 ppc64_elf_toc_reloc, /* special_function */
1211 "R_PPC64_TOC16_LO_DS", /* name */
1212 FALSE, /* partial_inplace */
1214 0xfffc, /* dst_mask */
1215 FALSE), /* pcrel_offset */
1217 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1218 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1219 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1221 1, /* size (0 = byte, 1 = short, 2 = long) */
1223 FALSE, /* pc_relative */
1225 complain_overflow_signed, /* complain_on_overflow */
1226 ppc64_elf_unhandled_reloc, /* special_function */
1227 "R_PPC64_PLTGOT16_DS", /* name */
1228 FALSE, /* partial_inplace */
1230 0xfffc, /* dst_mask */
1231 FALSE), /* pcrel_offset */
1233 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1234 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1235 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1237 1, /* size (0 = byte, 1 = short, 2 = long) */
1239 FALSE, /* pc_relative */
1241 complain_overflow_dont, /* complain_on_overflow */
1242 ppc64_elf_unhandled_reloc, /* special_function */
1243 "R_PPC64_PLTGOT16_LO_DS",/* name */
1244 FALSE, /* partial_inplace */
1246 0xfffc, /* dst_mask */
1247 FALSE), /* pcrel_offset */
1249 /* Marker relocs for TLS. */
1252 2, /* size (0 = byte, 1 = short, 2 = long) */
1254 FALSE, /* pc_relative */
1256 complain_overflow_dont, /* complain_on_overflow */
1257 bfd_elf_generic_reloc, /* special_function */
1258 "R_PPC64_TLS", /* name */
1259 FALSE, /* partial_inplace */
1262 FALSE), /* pcrel_offset */
1264 HOWTO (R_PPC64_TLSGD,
1266 2, /* size (0 = byte, 1 = short, 2 = long) */
1268 FALSE, /* pc_relative */
1270 complain_overflow_dont, /* complain_on_overflow */
1271 bfd_elf_generic_reloc, /* special_function */
1272 "R_PPC64_TLSGD", /* name */
1273 FALSE, /* partial_inplace */
1276 FALSE), /* pcrel_offset */
1278 HOWTO (R_PPC64_TLSLD,
1280 2, /* size (0 = byte, 1 = short, 2 = long) */
1282 FALSE, /* pc_relative */
1284 complain_overflow_dont, /* complain_on_overflow */
1285 bfd_elf_generic_reloc, /* special_function */
1286 "R_PPC64_TLSLD", /* name */
1287 FALSE, /* partial_inplace */
1290 FALSE), /* pcrel_offset */
1292 HOWTO (R_PPC64_TOCSAVE,
1294 2, /* size (0 = byte, 1 = short, 2 = long) */
1296 FALSE, /* pc_relative */
1298 complain_overflow_dont, /* complain_on_overflow */
1299 bfd_elf_generic_reloc, /* special_function */
1300 "R_PPC64_TOCSAVE", /* name */
1301 FALSE, /* partial_inplace */
1304 FALSE), /* pcrel_offset */
1306 /* Computes the load module index of the load module that contains the
1307 definition of its TLS sym. */
1308 HOWTO (R_PPC64_DTPMOD64,
1310 4, /* size (0 = byte, 1 = short, 2 = long) */
1312 FALSE, /* pc_relative */
1314 complain_overflow_dont, /* complain_on_overflow */
1315 ppc64_elf_unhandled_reloc, /* special_function */
1316 "R_PPC64_DTPMOD64", /* name */
1317 FALSE, /* partial_inplace */
1319 ONES (64), /* dst_mask */
1320 FALSE), /* pcrel_offset */
1322 /* Computes a dtv-relative displacement, the difference between the value
1323 of sym+add and the base address of the thread-local storage block that
1324 contains the definition of sym, minus 0x8000. */
1325 HOWTO (R_PPC64_DTPREL64,
1327 4, /* size (0 = byte, 1 = short, 2 = long) */
1329 FALSE, /* pc_relative */
1331 complain_overflow_dont, /* complain_on_overflow */
1332 ppc64_elf_unhandled_reloc, /* special_function */
1333 "R_PPC64_DTPREL64", /* name */
1334 FALSE, /* partial_inplace */
1336 ONES (64), /* dst_mask */
1337 FALSE), /* pcrel_offset */
1339 /* A 16 bit dtprel reloc. */
1340 HOWTO (R_PPC64_DTPREL16,
1342 1, /* size (0 = byte, 1 = short, 2 = long) */
1344 FALSE, /* pc_relative */
1346 complain_overflow_signed, /* complain_on_overflow */
1347 ppc64_elf_unhandled_reloc, /* special_function */
1348 "R_PPC64_DTPREL16", /* name */
1349 FALSE, /* partial_inplace */
1351 0xffff, /* dst_mask */
1352 FALSE), /* pcrel_offset */
1354 /* Like DTPREL16, but no overflow. */
1355 HOWTO (R_PPC64_DTPREL16_LO,
1357 1, /* size (0 = byte, 1 = short, 2 = long) */
1359 FALSE, /* pc_relative */
1361 complain_overflow_dont, /* complain_on_overflow */
1362 ppc64_elf_unhandled_reloc, /* special_function */
1363 "R_PPC64_DTPREL16_LO", /* name */
1364 FALSE, /* partial_inplace */
1366 0xffff, /* dst_mask */
1367 FALSE), /* pcrel_offset */
1369 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1370 HOWTO (R_PPC64_DTPREL16_HI,
1371 16, /* rightshift */
1372 1, /* size (0 = byte, 1 = short, 2 = long) */
1374 FALSE, /* pc_relative */
1376 complain_overflow_dont, /* complain_on_overflow */
1377 ppc64_elf_unhandled_reloc, /* special_function */
1378 "R_PPC64_DTPREL16_HI", /* name */
1379 FALSE, /* partial_inplace */
1381 0xffff, /* dst_mask */
1382 FALSE), /* pcrel_offset */
1384 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1385 HOWTO (R_PPC64_DTPREL16_HA,
1386 16, /* rightshift */
1387 1, /* size (0 = byte, 1 = short, 2 = long) */
1389 FALSE, /* pc_relative */
1391 complain_overflow_dont, /* complain_on_overflow */
1392 ppc64_elf_unhandled_reloc, /* special_function */
1393 "R_PPC64_DTPREL16_HA", /* name */
1394 FALSE, /* partial_inplace */
1396 0xffff, /* dst_mask */
1397 FALSE), /* pcrel_offset */
1399 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1400 HOWTO (R_PPC64_DTPREL16_HIGHER,
1401 32, /* rightshift */
1402 1, /* size (0 = byte, 1 = short, 2 = long) */
1404 FALSE, /* pc_relative */
1406 complain_overflow_dont, /* complain_on_overflow */
1407 ppc64_elf_unhandled_reloc, /* special_function */
1408 "R_PPC64_DTPREL16_HIGHER", /* name */
1409 FALSE, /* partial_inplace */
1411 0xffff, /* dst_mask */
1412 FALSE), /* pcrel_offset */
1414 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1415 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1416 32, /* rightshift */
1417 1, /* size (0 = byte, 1 = short, 2 = long) */
1419 FALSE, /* pc_relative */
1421 complain_overflow_dont, /* complain_on_overflow */
1422 ppc64_elf_unhandled_reloc, /* special_function */
1423 "R_PPC64_DTPREL16_HIGHERA", /* name */
1424 FALSE, /* partial_inplace */
1426 0xffff, /* dst_mask */
1427 FALSE), /* pcrel_offset */
1429 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1430 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1431 48, /* rightshift */
1432 1, /* size (0 = byte, 1 = short, 2 = long) */
1434 FALSE, /* pc_relative */
1436 complain_overflow_dont, /* complain_on_overflow */
1437 ppc64_elf_unhandled_reloc, /* special_function */
1438 "R_PPC64_DTPREL16_HIGHEST", /* name */
1439 FALSE, /* partial_inplace */
1441 0xffff, /* dst_mask */
1442 FALSE), /* pcrel_offset */
1444 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1445 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1446 48, /* rightshift */
1447 1, /* size (0 = byte, 1 = short, 2 = long) */
1449 FALSE, /* pc_relative */
1451 complain_overflow_dont, /* complain_on_overflow */
1452 ppc64_elf_unhandled_reloc, /* special_function */
1453 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1454 FALSE, /* partial_inplace */
1456 0xffff, /* dst_mask */
1457 FALSE), /* pcrel_offset */
1459 /* Like DTPREL16, but for insns with a DS field. */
1460 HOWTO (R_PPC64_DTPREL16_DS,
1462 1, /* size (0 = byte, 1 = short, 2 = long) */
1464 FALSE, /* pc_relative */
1466 complain_overflow_signed, /* complain_on_overflow */
1467 ppc64_elf_unhandled_reloc, /* special_function */
1468 "R_PPC64_DTPREL16_DS", /* name */
1469 FALSE, /* partial_inplace */
1471 0xfffc, /* dst_mask */
1472 FALSE), /* pcrel_offset */
1474 /* Like DTPREL16_DS, but no overflow. */
1475 HOWTO (R_PPC64_DTPREL16_LO_DS,
1477 1, /* size (0 = byte, 1 = short, 2 = long) */
1479 FALSE, /* pc_relative */
1481 complain_overflow_dont, /* complain_on_overflow */
1482 ppc64_elf_unhandled_reloc, /* special_function */
1483 "R_PPC64_DTPREL16_LO_DS", /* name */
1484 FALSE, /* partial_inplace */
1486 0xfffc, /* dst_mask */
1487 FALSE), /* pcrel_offset */
1489 /* Computes a tp-relative displacement, the difference between the value of
1490 sym+add and the value of the thread pointer (r13). */
1491 HOWTO (R_PPC64_TPREL64,
1493 4, /* size (0 = byte, 1 = short, 2 = long) */
1495 FALSE, /* pc_relative */
1497 complain_overflow_dont, /* complain_on_overflow */
1498 ppc64_elf_unhandled_reloc, /* special_function */
1499 "R_PPC64_TPREL64", /* name */
1500 FALSE, /* partial_inplace */
1502 ONES (64), /* dst_mask */
1503 FALSE), /* pcrel_offset */
1505 /* A 16 bit tprel reloc. */
1506 HOWTO (R_PPC64_TPREL16,
1508 1, /* size (0 = byte, 1 = short, 2 = long) */
1510 FALSE, /* pc_relative */
1512 complain_overflow_signed, /* complain_on_overflow */
1513 ppc64_elf_unhandled_reloc, /* special_function */
1514 "R_PPC64_TPREL16", /* name */
1515 FALSE, /* partial_inplace */
1517 0xffff, /* dst_mask */
1518 FALSE), /* pcrel_offset */
1520 /* Like TPREL16, but no overflow. */
1521 HOWTO (R_PPC64_TPREL16_LO,
1523 1, /* size (0 = byte, 1 = short, 2 = long) */
1525 FALSE, /* pc_relative */
1527 complain_overflow_dont, /* complain_on_overflow */
1528 ppc64_elf_unhandled_reloc, /* special_function */
1529 "R_PPC64_TPREL16_LO", /* name */
1530 FALSE, /* partial_inplace */
1532 0xffff, /* dst_mask */
1533 FALSE), /* pcrel_offset */
1535 /* Like TPREL16_LO, but next higher group of 16 bits. */
1536 HOWTO (R_PPC64_TPREL16_HI,
1537 16, /* rightshift */
1538 1, /* size (0 = byte, 1 = short, 2 = long) */
1540 FALSE, /* pc_relative */
1542 complain_overflow_dont, /* complain_on_overflow */
1543 ppc64_elf_unhandled_reloc, /* special_function */
1544 "R_PPC64_TPREL16_HI", /* name */
1545 FALSE, /* partial_inplace */
1547 0xffff, /* dst_mask */
1548 FALSE), /* pcrel_offset */
1550 /* Like TPREL16_HI, but adjust for low 16 bits. */
1551 HOWTO (R_PPC64_TPREL16_HA,
1552 16, /* rightshift */
1553 1, /* size (0 = byte, 1 = short, 2 = long) */
1555 FALSE, /* pc_relative */
1557 complain_overflow_dont, /* complain_on_overflow */
1558 ppc64_elf_unhandled_reloc, /* special_function */
1559 "R_PPC64_TPREL16_HA", /* name */
1560 FALSE, /* partial_inplace */
1562 0xffff, /* dst_mask */
1563 FALSE), /* pcrel_offset */
1565 /* Like TPREL16_HI, but next higher group of 16 bits. */
1566 HOWTO (R_PPC64_TPREL16_HIGHER,
1567 32, /* rightshift */
1568 1, /* size (0 = byte, 1 = short, 2 = long) */
1570 FALSE, /* pc_relative */
1572 complain_overflow_dont, /* complain_on_overflow */
1573 ppc64_elf_unhandled_reloc, /* special_function */
1574 "R_PPC64_TPREL16_HIGHER", /* name */
1575 FALSE, /* partial_inplace */
1577 0xffff, /* dst_mask */
1578 FALSE), /* pcrel_offset */
1580 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1581 HOWTO (R_PPC64_TPREL16_HIGHERA,
1582 32, /* rightshift */
1583 1, /* size (0 = byte, 1 = short, 2 = long) */
1585 FALSE, /* pc_relative */
1587 complain_overflow_dont, /* complain_on_overflow */
1588 ppc64_elf_unhandled_reloc, /* special_function */
1589 "R_PPC64_TPREL16_HIGHERA", /* name */
1590 FALSE, /* partial_inplace */
1592 0xffff, /* dst_mask */
1593 FALSE), /* pcrel_offset */
1595 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1596 HOWTO (R_PPC64_TPREL16_HIGHEST,
1597 48, /* rightshift */
1598 1, /* size (0 = byte, 1 = short, 2 = long) */
1600 FALSE, /* pc_relative */
1602 complain_overflow_dont, /* complain_on_overflow */
1603 ppc64_elf_unhandled_reloc, /* special_function */
1604 "R_PPC64_TPREL16_HIGHEST", /* name */
1605 FALSE, /* partial_inplace */
1607 0xffff, /* dst_mask */
1608 FALSE), /* pcrel_offset */
1610 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1611 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1612 48, /* rightshift */
1613 1, /* size (0 = byte, 1 = short, 2 = long) */
1615 FALSE, /* pc_relative */
1617 complain_overflow_dont, /* complain_on_overflow */
1618 ppc64_elf_unhandled_reloc, /* special_function */
1619 "R_PPC64_TPREL16_HIGHESTA", /* name */
1620 FALSE, /* partial_inplace */
1622 0xffff, /* dst_mask */
1623 FALSE), /* pcrel_offset */
1625 /* Like TPREL16, but for insns with a DS field. */
1626 HOWTO (R_PPC64_TPREL16_DS,
1628 1, /* size (0 = byte, 1 = short, 2 = long) */
1630 FALSE, /* pc_relative */
1632 complain_overflow_signed, /* complain_on_overflow */
1633 ppc64_elf_unhandled_reloc, /* special_function */
1634 "R_PPC64_TPREL16_DS", /* name */
1635 FALSE, /* partial_inplace */
1637 0xfffc, /* dst_mask */
1638 FALSE), /* pcrel_offset */
1640 /* Like TPREL16_DS, but no overflow. */
1641 HOWTO (R_PPC64_TPREL16_LO_DS,
1643 1, /* size (0 = byte, 1 = short, 2 = long) */
1645 FALSE, /* pc_relative */
1647 complain_overflow_dont, /* complain_on_overflow */
1648 ppc64_elf_unhandled_reloc, /* special_function */
1649 "R_PPC64_TPREL16_LO_DS", /* name */
1650 FALSE, /* partial_inplace */
1652 0xfffc, /* dst_mask */
1653 FALSE), /* pcrel_offset */
1655 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1656 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1657 to the first entry relative to the TOC base (r2). */
1658 HOWTO (R_PPC64_GOT_TLSGD16,
1660 1, /* size (0 = byte, 1 = short, 2 = long) */
1662 FALSE, /* pc_relative */
1664 complain_overflow_signed, /* complain_on_overflow */
1665 ppc64_elf_unhandled_reloc, /* special_function */
1666 "R_PPC64_GOT_TLSGD16", /* name */
1667 FALSE, /* partial_inplace */
1669 0xffff, /* dst_mask */
1670 FALSE), /* pcrel_offset */
1672 /* Like GOT_TLSGD16, but no overflow. */
1673 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1675 1, /* size (0 = byte, 1 = short, 2 = long) */
1677 FALSE, /* pc_relative */
1679 complain_overflow_dont, /* complain_on_overflow */
1680 ppc64_elf_unhandled_reloc, /* special_function */
1681 "R_PPC64_GOT_TLSGD16_LO", /* name */
1682 FALSE, /* partial_inplace */
1684 0xffff, /* dst_mask */
1685 FALSE), /* pcrel_offset */
1687 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1688 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1689 16, /* rightshift */
1690 1, /* size (0 = byte, 1 = short, 2 = long) */
1692 FALSE, /* pc_relative */
1694 complain_overflow_dont, /* complain_on_overflow */
1695 ppc64_elf_unhandled_reloc, /* special_function */
1696 "R_PPC64_GOT_TLSGD16_HI", /* name */
1697 FALSE, /* partial_inplace */
1699 0xffff, /* dst_mask */
1700 FALSE), /* pcrel_offset */
1702 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1703 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1704 16, /* rightshift */
1705 1, /* size (0 = byte, 1 = short, 2 = long) */
1707 FALSE, /* pc_relative */
1709 complain_overflow_dont, /* complain_on_overflow */
1710 ppc64_elf_unhandled_reloc, /* special_function */
1711 "R_PPC64_GOT_TLSGD16_HA", /* name */
1712 FALSE, /* partial_inplace */
1714 0xffff, /* dst_mask */
1715 FALSE), /* pcrel_offset */
1717 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1718 with values (sym+add)@dtpmod and zero, and computes the offset to the
1719 first entry relative to the TOC base (r2). */
1720 HOWTO (R_PPC64_GOT_TLSLD16,
1722 1, /* size (0 = byte, 1 = short, 2 = long) */
1724 FALSE, /* pc_relative */
1726 complain_overflow_signed, /* complain_on_overflow */
1727 ppc64_elf_unhandled_reloc, /* special_function */
1728 "R_PPC64_GOT_TLSLD16", /* name */
1729 FALSE, /* partial_inplace */
1731 0xffff, /* dst_mask */
1732 FALSE), /* pcrel_offset */
1734 /* Like GOT_TLSLD16, but no overflow. */
1735 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1737 1, /* size (0 = byte, 1 = short, 2 = long) */
1739 FALSE, /* pc_relative */
1741 complain_overflow_dont, /* complain_on_overflow */
1742 ppc64_elf_unhandled_reloc, /* special_function */
1743 "R_PPC64_GOT_TLSLD16_LO", /* name */
1744 FALSE, /* partial_inplace */
1746 0xffff, /* dst_mask */
1747 FALSE), /* pcrel_offset */
1749 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1750 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1751 16, /* rightshift */
1752 1, /* size (0 = byte, 1 = short, 2 = long) */
1754 FALSE, /* pc_relative */
1756 complain_overflow_dont, /* complain_on_overflow */
1757 ppc64_elf_unhandled_reloc, /* special_function */
1758 "R_PPC64_GOT_TLSLD16_HI", /* name */
1759 FALSE, /* partial_inplace */
1761 0xffff, /* dst_mask */
1762 FALSE), /* pcrel_offset */
1764 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1765 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1766 16, /* rightshift */
1767 1, /* size (0 = byte, 1 = short, 2 = long) */
1769 FALSE, /* pc_relative */
1771 complain_overflow_dont, /* complain_on_overflow */
1772 ppc64_elf_unhandled_reloc, /* special_function */
1773 "R_PPC64_GOT_TLSLD16_HA", /* name */
1774 FALSE, /* partial_inplace */
1776 0xffff, /* dst_mask */
1777 FALSE), /* pcrel_offset */
1779 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1780 the offset to the entry relative to the TOC base (r2). */
1781 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1783 1, /* size (0 = byte, 1 = short, 2 = long) */
1785 FALSE, /* pc_relative */
1787 complain_overflow_signed, /* complain_on_overflow */
1788 ppc64_elf_unhandled_reloc, /* special_function */
1789 "R_PPC64_GOT_DTPREL16_DS", /* name */
1790 FALSE, /* partial_inplace */
1792 0xfffc, /* dst_mask */
1793 FALSE), /* pcrel_offset */
1795 /* Like GOT_DTPREL16_DS, but no overflow. */
1796 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1798 1, /* size (0 = byte, 1 = short, 2 = long) */
1800 FALSE, /* pc_relative */
1802 complain_overflow_dont, /* complain_on_overflow */
1803 ppc64_elf_unhandled_reloc, /* special_function */
1804 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1805 FALSE, /* partial_inplace */
1807 0xfffc, /* dst_mask */
1808 FALSE), /* pcrel_offset */
1810 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1811 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1812 16, /* rightshift */
1813 1, /* size (0 = byte, 1 = short, 2 = long) */
1815 FALSE, /* pc_relative */
1817 complain_overflow_dont, /* complain_on_overflow */
1818 ppc64_elf_unhandled_reloc, /* special_function */
1819 "R_PPC64_GOT_DTPREL16_HI", /* name */
1820 FALSE, /* partial_inplace */
1822 0xffff, /* dst_mask */
1823 FALSE), /* pcrel_offset */
1825 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1826 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1827 16, /* rightshift */
1828 1, /* size (0 = byte, 1 = short, 2 = long) */
1830 FALSE, /* pc_relative */
1832 complain_overflow_dont, /* complain_on_overflow */
1833 ppc64_elf_unhandled_reloc, /* special_function */
1834 "R_PPC64_GOT_DTPREL16_HA", /* name */
1835 FALSE, /* partial_inplace */
1837 0xffff, /* dst_mask */
1838 FALSE), /* pcrel_offset */
1840 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1841 offset to the entry relative to the TOC base (r2). */
1842 HOWTO (R_PPC64_GOT_TPREL16_DS,
1844 1, /* size (0 = byte, 1 = short, 2 = long) */
1846 FALSE, /* pc_relative */
1848 complain_overflow_signed, /* complain_on_overflow */
1849 ppc64_elf_unhandled_reloc, /* special_function */
1850 "R_PPC64_GOT_TPREL16_DS", /* name */
1851 FALSE, /* partial_inplace */
1853 0xfffc, /* dst_mask */
1854 FALSE), /* pcrel_offset */
1856 /* Like GOT_TPREL16_DS, but no overflow. */
1857 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1859 1, /* size (0 = byte, 1 = short, 2 = long) */
1861 FALSE, /* pc_relative */
1863 complain_overflow_dont, /* complain_on_overflow */
1864 ppc64_elf_unhandled_reloc, /* special_function */
1865 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1866 FALSE, /* partial_inplace */
1868 0xfffc, /* dst_mask */
1869 FALSE), /* pcrel_offset */
1871 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1872 HOWTO (R_PPC64_GOT_TPREL16_HI,
1873 16, /* rightshift */
1874 1, /* size (0 = byte, 1 = short, 2 = long) */
1876 FALSE, /* pc_relative */
1878 complain_overflow_dont, /* complain_on_overflow */
1879 ppc64_elf_unhandled_reloc, /* special_function */
1880 "R_PPC64_GOT_TPREL16_HI", /* name */
1881 FALSE, /* partial_inplace */
1883 0xffff, /* dst_mask */
1884 FALSE), /* pcrel_offset */
1886 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1887 HOWTO (R_PPC64_GOT_TPREL16_HA,
1888 16, /* rightshift */
1889 1, /* size (0 = byte, 1 = short, 2 = long) */
1891 FALSE, /* pc_relative */
1893 complain_overflow_dont, /* complain_on_overflow */
1894 ppc64_elf_unhandled_reloc, /* special_function */
1895 "R_PPC64_GOT_TPREL16_HA", /* name */
1896 FALSE, /* partial_inplace */
1898 0xffff, /* dst_mask */
1899 FALSE), /* pcrel_offset */
1901 HOWTO (R_PPC64_JMP_IREL, /* type */
1903 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1905 FALSE, /* pc_relative */
1907 complain_overflow_dont, /* complain_on_overflow */
1908 ppc64_elf_unhandled_reloc, /* special_function */
1909 "R_PPC64_JMP_IREL", /* name */
1910 FALSE, /* partial_inplace */
1913 FALSE), /* pcrel_offset */
1915 HOWTO (R_PPC64_IRELATIVE, /* type */
1917 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1919 FALSE, /* pc_relative */
1921 complain_overflow_dont, /* complain_on_overflow */
1922 bfd_elf_generic_reloc, /* special_function */
1923 "R_PPC64_IRELATIVE", /* name */
1924 FALSE, /* partial_inplace */
1926 ONES (64), /* dst_mask */
1927 FALSE), /* pcrel_offset */
1929 /* A 16 bit relative relocation. */
1930 HOWTO (R_PPC64_REL16, /* type */
1932 1, /* size (0 = byte, 1 = short, 2 = long) */
1934 TRUE, /* pc_relative */
1936 complain_overflow_bitfield, /* complain_on_overflow */
1937 bfd_elf_generic_reloc, /* special_function */
1938 "R_PPC64_REL16", /* name */
1939 FALSE, /* partial_inplace */
1941 0xffff, /* dst_mask */
1942 TRUE), /* pcrel_offset */
1944 /* A 16 bit relative relocation without overflow. */
1945 HOWTO (R_PPC64_REL16_LO, /* type */
1947 1, /* size (0 = byte, 1 = short, 2 = long) */
1949 TRUE, /* pc_relative */
1951 complain_overflow_dont,/* complain_on_overflow */
1952 bfd_elf_generic_reloc, /* special_function */
1953 "R_PPC64_REL16_LO", /* name */
1954 FALSE, /* partial_inplace */
1956 0xffff, /* dst_mask */
1957 TRUE), /* pcrel_offset */
1959 /* The high order 16 bits of a relative address. */
1960 HOWTO (R_PPC64_REL16_HI, /* type */
1961 16, /* rightshift */
1962 1, /* size (0 = byte, 1 = short, 2 = long) */
1964 TRUE, /* pc_relative */
1966 complain_overflow_dont, /* complain_on_overflow */
1967 bfd_elf_generic_reloc, /* special_function */
1968 "R_PPC64_REL16_HI", /* name */
1969 FALSE, /* partial_inplace */
1971 0xffff, /* dst_mask */
1972 TRUE), /* pcrel_offset */
1974 /* The high order 16 bits of a relative address, plus 1 if the contents of
1975 the low 16 bits, treated as a signed number, is negative. */
1976 HOWTO (R_PPC64_REL16_HA, /* type */
1977 16, /* rightshift */
1978 1, /* size (0 = byte, 1 = short, 2 = long) */
1980 TRUE, /* pc_relative */
1982 complain_overflow_dont, /* complain_on_overflow */
1983 ppc64_elf_ha_reloc, /* special_function */
1984 "R_PPC64_REL16_HA", /* name */
1985 FALSE, /* partial_inplace */
1987 0xffff, /* dst_mask */
1988 TRUE), /* pcrel_offset */
1990 /* GNU extension to record C++ vtable hierarchy. */
1991 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1993 0, /* size (0 = byte, 1 = short, 2 = long) */
1995 FALSE, /* pc_relative */
1997 complain_overflow_dont, /* complain_on_overflow */
1998 NULL, /* special_function */
1999 "R_PPC64_GNU_VTINHERIT", /* name */
2000 FALSE, /* partial_inplace */
2003 FALSE), /* pcrel_offset */
2005 /* GNU extension to record C++ vtable member usage. */
2006 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2008 0, /* size (0 = byte, 1 = short, 2 = long) */
2010 FALSE, /* pc_relative */
2012 complain_overflow_dont, /* complain_on_overflow */
2013 NULL, /* special_function */
2014 "R_PPC64_GNU_VTENTRY", /* name */
2015 FALSE, /* partial_inplace */
2018 FALSE), /* pcrel_offset */
2022 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2026 ppc_howto_init (void)
2028 unsigned int i, type;
2031 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2034 type = ppc64_elf_howto_raw[i].type;
2035 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2036 / sizeof (ppc64_elf_howto_table[0])));
2037 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2041 static reloc_howto_type *
2042 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2043 bfd_reloc_code_real_type code)
2045 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2047 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2048 /* Initialize howto table if needed. */
2056 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2058 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2060 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2062 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2064 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2066 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2068 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2070 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2072 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2074 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2076 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2078 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2080 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2082 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2084 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2086 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2088 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2090 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2092 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2094 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2096 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2098 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2100 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2102 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2104 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2106 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2108 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2110 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2112 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2114 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2116 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2118 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2120 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2122 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2124 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2126 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2128 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2130 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2132 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2134 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2136 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2138 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2140 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2142 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2144 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2146 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2148 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2150 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2152 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2154 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2156 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2158 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2160 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2162 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2164 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2166 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2168 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2170 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2172 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2174 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2176 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2178 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2180 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2182 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2184 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2186 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2188 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2190 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2192 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2194 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2196 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2198 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2200 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2202 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2204 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2206 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2208 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2210 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2212 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2214 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2216 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2218 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2220 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2222 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2224 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2226 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2228 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2230 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2232 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2234 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2236 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2238 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2240 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2242 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2244 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2246 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2248 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2250 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2252 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2254 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2256 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2258 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2260 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2262 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2264 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2266 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2268 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2272 return ppc64_elf_howto_table[r];
2275 static reloc_howto_type *
2276 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2282 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2284 if (ppc64_elf_howto_raw[i].name != NULL
2285 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2286 return &ppc64_elf_howto_raw[i];
2291 /* Set the howto pointer for a PowerPC ELF reloc. */
2294 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2295 Elf_Internal_Rela *dst)
2299 /* Initialize howto table if needed. */
2300 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2303 type = ELF64_R_TYPE (dst->r_info);
2304 if (type >= (sizeof (ppc64_elf_howto_table)
2305 / sizeof (ppc64_elf_howto_table[0])))
2307 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2309 type = R_PPC64_NONE;
2311 cache_ptr->howto = ppc64_elf_howto_table[type];
2314 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2316 static bfd_reloc_status_type
2317 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2318 void *data, asection *input_section,
2319 bfd *output_bfd, char **error_message)
2321 /* If this is a relocatable link (output_bfd test tells us), just
2322 call the generic function. Any adjustment will be done at final
2324 if (output_bfd != NULL)
2325 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2326 input_section, output_bfd, error_message);
2328 /* Adjust the addend for sign extension of the low 16 bits.
2329 We won't actually be using the low 16 bits, so trashing them
2331 reloc_entry->addend += 0x8000;
2332 return bfd_reloc_continue;
2335 static bfd_reloc_status_type
2336 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2337 void *data, asection *input_section,
2338 bfd *output_bfd, char **error_message)
2340 if (output_bfd != NULL)
2341 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2342 input_section, output_bfd, error_message);
2344 if (strcmp (symbol->section->name, ".opd") == 0
2345 && (symbol->section->owner->flags & DYNAMIC) == 0)
2347 bfd_vma dest = opd_entry_value (symbol->section,
2348 symbol->value + reloc_entry->addend,
2350 if (dest != (bfd_vma) -1)
2351 reloc_entry->addend = dest - (symbol->value
2352 + symbol->section->output_section->vma
2353 + symbol->section->output_offset);
2355 return bfd_reloc_continue;
2358 static bfd_reloc_status_type
2359 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2360 void *data, asection *input_section,
2361 bfd *output_bfd, char **error_message)
2364 enum elf_ppc64_reloc_type r_type;
2365 bfd_size_type octets;
2366 /* Assume 'at' branch hints. */
2367 bfd_boolean is_isa_v2 = TRUE;
2369 /* If this is a relocatable link (output_bfd test tells us), just
2370 call the generic function. Any adjustment will be done at final
2372 if (output_bfd != NULL)
2373 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2374 input_section, output_bfd, error_message);
2376 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2377 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2378 insn &= ~(0x01 << 21);
2379 r_type = reloc_entry->howto->type;
2380 if (r_type == R_PPC64_ADDR14_BRTAKEN
2381 || r_type == R_PPC64_REL14_BRTAKEN)
2382 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2386 /* Set 'a' bit. This is 0b00010 in BO field for branch
2387 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2388 for branch on CTR insns (BO == 1a00t or 1a01t). */
2389 if ((insn & (0x14 << 21)) == (0x04 << 21))
2391 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2401 if (!bfd_is_com_section (symbol->section))
2402 target = symbol->value;
2403 target += symbol->section->output_section->vma;
2404 target += symbol->section->output_offset;
2405 target += reloc_entry->addend;
2407 from = (reloc_entry->address
2408 + input_section->output_offset
2409 + input_section->output_section->vma);
2411 /* Invert 'y' bit if not the default. */
2412 if ((bfd_signed_vma) (target - from) < 0)
2415 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2417 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2418 input_section, output_bfd, error_message);
2421 static bfd_reloc_status_type
2422 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2423 void *data, asection *input_section,
2424 bfd *output_bfd, char **error_message)
2426 /* If this is a relocatable link (output_bfd test tells us), just
2427 call the generic function. Any adjustment will be done at final
2429 if (output_bfd != NULL)
2430 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2431 input_section, output_bfd, error_message);
2433 /* Subtract the symbol section base address. */
2434 reloc_entry->addend -= symbol->section->output_section->vma;
2435 return bfd_reloc_continue;
2438 static bfd_reloc_status_type
2439 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2440 void *data, asection *input_section,
2441 bfd *output_bfd, char **error_message)
2443 /* If this is a relocatable link (output_bfd test tells us), just
2444 call the generic function. Any adjustment will be done at final
2446 if (output_bfd != NULL)
2447 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2448 input_section, output_bfd, error_message);
2450 /* Subtract the symbol section base address. */
2451 reloc_entry->addend -= symbol->section->output_section->vma;
2453 /* Adjust the addend for sign extension of the low 16 bits. */
2454 reloc_entry->addend += 0x8000;
2455 return bfd_reloc_continue;
2458 static bfd_reloc_status_type
2459 ppc64_elf_toc_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;
2478 return bfd_reloc_continue;
2481 static bfd_reloc_status_type
2482 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2483 void *data, asection *input_section,
2484 bfd *output_bfd, char **error_message)
2488 /* If this is a relocatable link (output_bfd test tells us), just
2489 call the generic function. Any adjustment will be done at final
2491 if (output_bfd != NULL)
2492 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2493 input_section, output_bfd, error_message);
2495 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2497 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2499 /* Subtract the TOC base address. */
2500 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2502 /* Adjust the addend for sign extension of the low 16 bits. */
2503 reloc_entry->addend += 0x8000;
2504 return bfd_reloc_continue;
2507 static bfd_reloc_status_type
2508 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2509 void *data, asection *input_section,
2510 bfd *output_bfd, char **error_message)
2513 bfd_size_type octets;
2515 /* If this is a relocatable link (output_bfd test tells us), just
2516 call the generic function. Any adjustment will be done at final
2518 if (output_bfd != NULL)
2519 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2520 input_section, output_bfd, error_message);
2522 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2524 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2526 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2527 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2528 return bfd_reloc_ok;
2531 static bfd_reloc_status_type
2532 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2533 void *data, asection *input_section,
2534 bfd *output_bfd, char **error_message)
2536 /* If this is a relocatable link (output_bfd test tells us), just
2537 call the generic function. Any adjustment will be done at final
2539 if (output_bfd != NULL)
2540 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2541 input_section, output_bfd, error_message);
2543 if (error_message != NULL)
2545 static char buf[60];
2546 sprintf (buf, "generic linker can't handle %s",
2547 reloc_entry->howto->name);
2548 *error_message = buf;
2550 return bfd_reloc_dangerous;
2553 /* Track GOT entries needed for a given symbol. We might need more
2554 than one got entry per symbol. */
2557 struct got_entry *next;
2559 /* The symbol addend that we'll be placing in the GOT. */
2562 /* Unlike other ELF targets, we use separate GOT entries for the same
2563 symbol referenced from different input files. This is to support
2564 automatic multiple TOC/GOT sections, where the TOC base can vary
2565 from one input file to another. After partitioning into TOC groups
2566 we merge entries within the group.
2568 Point to the BFD owning this GOT entry. */
2571 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2572 TLS_TPREL or TLS_DTPREL for tls entries. */
2573 unsigned char tls_type;
2575 /* Non-zero if got.ent points to real entry. */
2576 unsigned char is_indirect;
2578 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2581 bfd_signed_vma refcount;
2583 struct got_entry *ent;
2587 /* The same for PLT. */
2590 struct plt_entry *next;
2596 bfd_signed_vma refcount;
2601 struct ppc64_elf_obj_tdata
2603 struct elf_obj_tdata elf;
2605 /* Shortcuts to dynamic linker sections. */
2609 /* Used during garbage collection. We attach global symbols defined
2610 on removed .opd entries to this section so that the sym is removed. */
2611 asection *deleted_section;
2613 /* TLS local dynamic got entry handling. Support for multiple GOT
2614 sections means we potentially need one of these for each input bfd. */
2615 struct got_entry tlsld_got;
2617 /* A copy of relocs before they are modified for --emit-relocs. */
2618 Elf_Internal_Rela *opd_relocs;
2620 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2621 the reloc to be in the range -32768 to 32767. */
2622 unsigned int has_small_toc_reloc : 1;
2624 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2625 instruction not one we handle. */
2626 unsigned int unexpected_toc_insn : 1;
2629 #define ppc64_elf_tdata(bfd) \
2630 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2632 #define ppc64_tlsld_got(bfd) \
2633 (&ppc64_elf_tdata (bfd)->tlsld_got)
2635 #define is_ppc64_elf(bfd) \
2636 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2637 && elf_object_id (bfd) == PPC64_ELF_DATA)
2639 /* Override the generic function because we store some extras. */
2642 ppc64_elf_mkobject (bfd *abfd)
2644 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2648 /* Fix bad default arch selected for a 64 bit input bfd when the
2649 default is 32 bit. */
2652 ppc64_elf_object_p (bfd *abfd)
2654 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2656 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2658 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2660 /* Relies on arch after 32 bit default being 64 bit default. */
2661 abfd->arch_info = abfd->arch_info->next;
2662 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2668 /* Support for core dump NOTE sections. */
2671 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2673 size_t offset, size;
2675 if (note->descsz != 504)
2679 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2682 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 32);
2688 /* Make a ".reg/999" section. */
2689 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2690 size, note->descpos + offset);
2694 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2696 if (note->descsz != 136)
2699 elf_tdata (abfd)->core_pid
2700 = bfd_get_32 (abfd, note->descdata + 24);
2701 elf_tdata (abfd)->core_program
2702 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2703 elf_tdata (abfd)->core_command
2704 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2710 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2723 va_start (ap, note_type);
2724 memset (data, 0, sizeof (data));
2725 strncpy (data + 40, va_arg (ap, const char *), 16);
2726 strncpy (data + 56, va_arg (ap, const char *), 80);
2728 return elfcore_write_note (abfd, buf, bufsiz,
2729 "CORE", note_type, data, sizeof (data));
2740 va_start (ap, note_type);
2741 memset (data, 0, 112);
2742 pid = va_arg (ap, long);
2743 bfd_put_32 (abfd, pid, data + 32);
2744 cursig = va_arg (ap, int);
2745 bfd_put_16 (abfd, cursig, data + 12);
2746 greg = va_arg (ap, const void *);
2747 memcpy (data + 112, greg, 384);
2748 memset (data + 496, 0, 8);
2750 return elfcore_write_note (abfd, buf, bufsiz,
2751 "CORE", note_type, data, sizeof (data));
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 /* SEC_LOAD may not be set if SEC is from a separate debug
3321 if ((sec->flags & SEC_ALLOC) == 0)
3323 if ((sec->flags & SEC_CODE) != 0)
3326 s->flags |= BSF_SYNTHETIC;
3327 s->value = ent - s->section->vma;
3330 len = strlen (syms[i]->name);
3331 memcpy (names, syms[i]->name, len + 1);
3333 /* Have udata.p point back to the original symbol this
3334 synthetic symbol was derived from. */
3335 s->udata.p = syms[i];
3341 if (glink != NULL && relplt != NULL)
3345 /* Add a symbol for the main glink trampoline. */
3346 memset (s, 0, sizeof *s);
3348 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3350 s->value = resolv_vma - glink->vma;
3352 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3353 names += sizeof ("__glink_PLTresolve");
3358 /* FIXME: It would be very much nicer to put sym@plt on the
3359 stub rather than on the glink branch table entry. The
3360 objdump disassembler would then use a sensible symbol
3361 name on plt calls. The difficulty in doing so is
3362 a) finding the stubs, and,
3363 b) matching stubs against plt entries, and,
3364 c) there can be multiple stubs for a given plt entry.
3366 Solving (a) could be done by code scanning, but older
3367 ppc64 binaries used different stubs to current code.
3368 (b) is the tricky one since you need to known the toc
3369 pointer for at least one function that uses a pic stub to
3370 be able to calculate the plt address referenced.
3371 (c) means gdb would need to set multiple breakpoints (or
3372 find the glink branch itself) when setting breakpoints
3373 for pending shared library loads. */
3374 p = relplt->relocation;
3375 for (i = 0; i < plt_count; i++, p++)
3379 *s = **p->sym_ptr_ptr;
3380 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3381 we are defining a symbol, ensure one of them is set. */
3382 if ((s->flags & BSF_LOCAL) == 0)
3383 s->flags |= BSF_GLOBAL;
3384 s->flags |= BSF_SYNTHETIC;
3386 s->value = glink_vma - glink->vma;
3389 len = strlen ((*p->sym_ptr_ptr)->name);
3390 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3394 memcpy (names, "+0x", sizeof ("+0x") - 1);
3395 names += sizeof ("+0x") - 1;
3396 bfd_sprintf_vma (abfd, names, p->addend);
3397 names += strlen (names);
3399 memcpy (names, "@plt", sizeof ("@plt"));
3400 names += sizeof ("@plt");
3415 /* The following functions are specific to the ELF linker, while
3416 functions above are used generally. Those named ppc64_elf_* are
3417 called by the main ELF linker code. They appear in this file more
3418 or less in the order in which they are called. eg.
3419 ppc64_elf_check_relocs is called early in the link process,
3420 ppc64_elf_finish_dynamic_sections is one of the last functions
3423 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3424 functions have both a function code symbol and a function descriptor
3425 symbol. A call to foo in a relocatable object file looks like:
3432 The function definition in another object file might be:
3436 . .quad .TOC.@tocbase
3442 When the linker resolves the call during a static link, the branch
3443 unsurprisingly just goes to .foo and the .opd information is unused.
3444 If the function definition is in a shared library, things are a little
3445 different: The call goes via a plt call stub, the opd information gets
3446 copied to the plt, and the linker patches the nop.
3454 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
3455 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
3456 . std 2,40(1) # this is the general idea
3464 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3466 The "reloc ()" notation is supposed to indicate that the linker emits
3467 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3470 What are the difficulties here? Well, firstly, the relocations
3471 examined by the linker in check_relocs are against the function code
3472 sym .foo, while the dynamic relocation in the plt is emitted against
3473 the function descriptor symbol, foo. Somewhere along the line, we need
3474 to carefully copy dynamic link information from one symbol to the other.
3475 Secondly, the generic part of the elf linker will make .foo a dynamic
3476 symbol as is normal for most other backends. We need foo dynamic
3477 instead, at least for an application final link. However, when
3478 creating a shared library containing foo, we need to have both symbols
3479 dynamic so that references to .foo are satisfied during the early
3480 stages of linking. Otherwise the linker might decide to pull in a
3481 definition from some other object, eg. a static library.
3483 Update: As of August 2004, we support a new convention. Function
3484 calls may use the function descriptor symbol, ie. "bl foo". This
3485 behaves exactly as "bl .foo". */
3487 /* Of those relocs that might be copied as dynamic relocs, this function
3488 selects those that must be copied when linking a shared library,
3489 even when the symbol is local. */
3492 must_be_dyn_reloc (struct bfd_link_info *info,
3493 enum elf_ppc64_reloc_type r_type)
3505 case R_PPC64_TPREL16:
3506 case R_PPC64_TPREL16_LO:
3507 case R_PPC64_TPREL16_HI:
3508 case R_PPC64_TPREL16_HA:
3509 case R_PPC64_TPREL16_DS:
3510 case R_PPC64_TPREL16_LO_DS:
3511 case R_PPC64_TPREL16_HIGHER:
3512 case R_PPC64_TPREL16_HIGHERA:
3513 case R_PPC64_TPREL16_HIGHEST:
3514 case R_PPC64_TPREL16_HIGHESTA:
3515 case R_PPC64_TPREL64:
3516 return !info->executable;
3520 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3521 copying dynamic variables from a shared lib into an app's dynbss
3522 section, and instead use a dynamic relocation to point into the
3523 shared lib. With code that gcc generates, it's vital that this be
3524 enabled; In the PowerPC64 ABI, the address of a function is actually
3525 the address of a function descriptor, which resides in the .opd
3526 section. gcc uses the descriptor directly rather than going via the
3527 GOT as some other ABI's do, which means that initialized function
3528 pointers must reference the descriptor. Thus, a function pointer
3529 initialized to the address of a function in a shared library will
3530 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3531 redefines the function descriptor symbol to point to the copy. This
3532 presents a problem as a plt entry for that function is also
3533 initialized from the function descriptor symbol and the copy reloc
3534 may not be initialized first. */
3535 #define ELIMINATE_COPY_RELOCS 1
3537 /* Section name for stubs is the associated section name plus this
3539 #define STUB_SUFFIX ".stub"
3542 ppc_stub_long_branch:
3543 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3544 destination, but a 24 bit branch in a stub section will reach.
3547 ppc_stub_plt_branch:
3548 Similar to the above, but a 24 bit branch in the stub section won't
3549 reach its destination.
3550 . addis %r12,%r2,xxx@toc@ha
3551 . ld %r11,xxx@toc@l(%r12)
3556 Used to call a function in a shared library. If it so happens that
3557 the plt entry referenced crosses a 64k boundary, then an extra
3558 "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
3559 . addis %r12,%r2,xxx@toc@ha
3561 . ld %r11,xxx+0@toc@l(%r12)
3563 . ld %r2,xxx+8@toc@l(%r12)
3564 . ld %r11,xxx+16@toc@l(%r12)
3567 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3568 code to adjust the value and save r2 to support multiple toc sections.
3569 A ppc_stub_long_branch with an r2 offset looks like:
3571 . addis %r2,%r2,off@ha
3572 . addi %r2,%r2,off@l
3575 A ppc_stub_plt_branch with an r2 offset looks like:
3577 . addis %r12,%r2,xxx@toc@ha
3578 . ld %r11,xxx@toc@l(%r12)
3579 . addis %r2,%r2,off@ha
3580 . addi %r2,%r2,off@l
3584 In cases where the "addis" instruction would add zero, the "addis" is
3585 omitted and following instructions modified slightly in some cases.
3588 enum ppc_stub_type {
3590 ppc_stub_long_branch,
3591 ppc_stub_long_branch_r2off,
3592 ppc_stub_plt_branch,
3593 ppc_stub_plt_branch_r2off,
3595 ppc_stub_plt_call_r2save
3598 struct ppc_stub_hash_entry {
3600 /* Base hash table entry structure. */
3601 struct bfd_hash_entry root;
3603 enum ppc_stub_type stub_type;
3605 /* The stub section. */
3608 /* Offset within stub_sec of the beginning of this stub. */
3609 bfd_vma stub_offset;
3611 /* Given the symbol's value and its section we can determine its final
3612 value when building the stubs (so the stub knows where to jump. */
3613 bfd_vma target_value;
3614 asection *target_section;
3616 /* The symbol table entry, if any, that this was derived from. */
3617 struct ppc_link_hash_entry *h;
3618 struct plt_entry *plt_ent;
3620 /* And the reloc addend that this was derived from. */
3623 /* Where this stub is being called from, or, in the case of combined
3624 stub sections, the first input section in the group. */
3628 struct ppc_branch_hash_entry {
3630 /* Base hash table entry structure. */
3631 struct bfd_hash_entry root;
3633 /* Offset within branch lookup table. */
3634 unsigned int offset;
3636 /* Generation marker. */
3640 struct ppc_link_hash_entry
3642 struct elf_link_hash_entry elf;
3645 /* A pointer to the most recently used stub hash entry against this
3647 struct ppc_stub_hash_entry *stub_cache;
3649 /* A pointer to the next symbol starting with a '.' */
3650 struct ppc_link_hash_entry *next_dot_sym;
3653 /* Track dynamic relocs copied for this symbol. */
3654 struct elf_dyn_relocs *dyn_relocs;
3656 /* Link between function code and descriptor symbols. */
3657 struct ppc_link_hash_entry *oh;
3659 /* Flag function code and descriptor symbols. */
3660 unsigned int is_func:1;
3661 unsigned int is_func_descriptor:1;
3662 unsigned int fake:1;
3664 /* Whether global opd/toc sym has been adjusted or not.
3665 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3666 should be set for all globals defined in any opd/toc section. */
3667 unsigned int adjust_done:1;
3669 /* Set if we twiddled this symbol to weak at some stage. */
3670 unsigned int was_undefined:1;
3672 /* Contexts in which symbol is used in the GOT (or TOC).
3673 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3674 corresponding relocs are encountered during check_relocs.
3675 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3676 indicate the corresponding GOT entry type is not needed.
3677 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3678 a TPREL one. We use a separate flag rather than setting TPREL
3679 just for convenience in distinguishing the two cases. */
3680 #define TLS_GD 1 /* GD reloc. */
3681 #define TLS_LD 2 /* LD reloc. */
3682 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
3683 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3684 #define TLS_TLS 16 /* Any TLS reloc. */
3685 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3686 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
3687 #define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
3688 unsigned char tls_mask;
3691 /* ppc64 ELF linker hash table. */
3693 struct ppc_link_hash_table
3695 struct elf_link_hash_table elf;
3697 /* The stub hash table. */
3698 struct bfd_hash_table stub_hash_table;
3700 /* Another hash table for plt_branch stubs. */
3701 struct bfd_hash_table branch_hash_table;
3703 /* Hash table for function prologue tocsave. */
3704 htab_t tocsave_htab;
3706 /* Linker stub bfd. */
3709 /* Linker call-backs. */
3710 asection * (*add_stub_section) (const char *, asection *);
3711 void (*layout_sections_again) (void);
3713 /* Array to keep track of which stub sections have been created, and
3714 information on stub grouping. */
3716 /* This is the section to which stubs in the group will be attached. */
3718 /* The stub section. */
3720 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3724 /* Temp used when calculating TOC pointers. */
3727 asection *toc_first_sec;
3729 /* Highest input section id. */
3732 /* Highest output section index. */
3735 /* Used when adding symbols. */
3736 struct ppc_link_hash_entry *dot_syms;
3738 /* List of input sections for each output section. */
3739 asection **input_list;
3741 /* Short-cuts to get to dynamic linker sections. */
3753 asection *glink_eh_frame;
3755 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3756 struct ppc_link_hash_entry *tls_get_addr;
3757 struct ppc_link_hash_entry *tls_get_addr_fd;
3759 /* The size of reliplt used by got entry relocs. */
3760 bfd_size_type got_reli_size;
3763 unsigned long stub_count[ppc_stub_plt_call_r2save];
3765 /* Number of stubs against global syms. */
3766 unsigned long stub_globals;
3768 /* Alignment of PLT call stubs. */
3769 unsigned int plt_stub_align:4;
3771 /* Set if PLT call stubs should load r11. */
3772 unsigned int plt_static_chain:1;
3774 /* Set if PLT call stubs need a read-read barrier. */
3775 unsigned int plt_thread_safe:1;
3777 /* Set if we should emit symbols for stubs. */
3778 unsigned int emit_stub_syms:1;
3780 /* Set if __tls_get_addr optimization should not be done. */
3781 unsigned int no_tls_get_addr_opt:1;
3783 /* Support for multiple toc sections. */
3784 unsigned int do_multi_toc:1;
3785 unsigned int multi_toc_needed:1;
3786 unsigned int second_toc_pass:1;
3787 unsigned int do_toc_opt:1;
3790 unsigned int stub_error:1;
3792 /* Temp used by ppc64_elf_process_dot_syms. */
3793 unsigned int twiddled_syms:1;
3795 /* Incremented every time we size stubs. */
3796 unsigned int stub_iteration;
3798 /* Small local sym cache. */
3799 struct sym_cache sym_cache;
3802 /* Rename some of the generic section flags to better document how they
3805 /* Nonzero if this section has TLS related relocations. */
3806 #define has_tls_reloc sec_flg0
3808 /* Nonzero if this section has a call to __tls_get_addr. */
3809 #define has_tls_get_addr_call sec_flg1
3811 /* Nonzero if this section has any toc or got relocs. */
3812 #define has_toc_reloc sec_flg2
3814 /* Nonzero if this section has a call to another section that uses
3816 #define makes_toc_func_call sec_flg3
3818 /* Recursion protection when determining above flag. */
3819 #define call_check_in_progress sec_flg4
3820 #define call_check_done sec_flg5
3822 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3824 #define ppc_hash_table(p) \
3825 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3826 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3828 #define ppc_stub_hash_lookup(table, string, create, copy) \
3829 ((struct ppc_stub_hash_entry *) \
3830 bfd_hash_lookup ((table), (string), (create), (copy)))
3832 #define ppc_branch_hash_lookup(table, string, create, copy) \
3833 ((struct ppc_branch_hash_entry *) \
3834 bfd_hash_lookup ((table), (string), (create), (copy)))
3836 /* Create an entry in the stub hash table. */
3838 static struct bfd_hash_entry *
3839 stub_hash_newfunc (struct bfd_hash_entry *entry,
3840 struct bfd_hash_table *table,
3843 /* Allocate the structure if it has not already been allocated by a
3847 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3852 /* Call the allocation method of the superclass. */
3853 entry = bfd_hash_newfunc (entry, table, string);
3856 struct ppc_stub_hash_entry *eh;
3858 /* Initialize the local fields. */
3859 eh = (struct ppc_stub_hash_entry *) entry;
3860 eh->stub_type = ppc_stub_none;
3861 eh->stub_sec = NULL;
3862 eh->stub_offset = 0;
3863 eh->target_value = 0;
3864 eh->target_section = NULL;
3872 /* Create an entry in the branch hash table. */
3874 static struct bfd_hash_entry *
3875 branch_hash_newfunc (struct bfd_hash_entry *entry,
3876 struct bfd_hash_table *table,
3879 /* Allocate the structure if it has not already been allocated by a
3883 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3888 /* Call the allocation method of the superclass. */
3889 entry = bfd_hash_newfunc (entry, table, string);
3892 struct ppc_branch_hash_entry *eh;
3894 /* Initialize the local fields. */
3895 eh = (struct ppc_branch_hash_entry *) entry;
3903 /* Create an entry in a ppc64 ELF linker hash table. */
3905 static struct bfd_hash_entry *
3906 link_hash_newfunc (struct bfd_hash_entry *entry,
3907 struct bfd_hash_table *table,
3910 /* Allocate the structure if it has not already been allocated by a
3914 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3919 /* Call the allocation method of the superclass. */
3920 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3923 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3925 memset (&eh->u.stub_cache, 0,
3926 (sizeof (struct ppc_link_hash_entry)
3927 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3929 /* When making function calls, old ABI code references function entry
3930 points (dot symbols), while new ABI code references the function
3931 descriptor symbol. We need to make any combination of reference and
3932 definition work together, without breaking archive linking.
3934 For a defined function "foo" and an undefined call to "bar":
3935 An old object defines "foo" and ".foo", references ".bar" (possibly
3937 A new object defines "foo" and references "bar".
3939 A new object thus has no problem with its undefined symbols being
3940 satisfied by definitions in an old object. On the other hand, the
3941 old object won't have ".bar" satisfied by a new object.
3943 Keep a list of newly added dot-symbols. */
3945 if (string[0] == '.')
3947 struct ppc_link_hash_table *htab;
3949 htab = (struct ppc_link_hash_table *) table;
3950 eh->u.next_dot_sym = htab->dot_syms;
3951 htab->dot_syms = eh;
3958 struct tocsave_entry {
3964 tocsave_htab_hash (const void *p)
3966 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3967 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
3971 tocsave_htab_eq (const void *p1, const void *p2)
3973 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3974 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3975 return e1->sec == e2->sec && e1->offset == e2->offset;
3978 /* Create a ppc64 ELF linker hash table. */
3980 static struct bfd_link_hash_table *
3981 ppc64_elf_link_hash_table_create (bfd *abfd)
3983 struct ppc_link_hash_table *htab;
3984 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3986 htab = bfd_zmalloc (amt);
3990 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3991 sizeof (struct ppc_link_hash_entry),
3998 /* Init the stub hash table too. */
3999 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4000 sizeof (struct ppc_stub_hash_entry)))
4003 /* And the branch hash table. */
4004 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4005 sizeof (struct ppc_branch_hash_entry)))
4008 htab->tocsave_htab = htab_try_create (1024,
4012 if (htab->tocsave_htab == NULL)
4015 /* Initializing two fields of the union is just cosmetic. We really
4016 only care about glist, but when compiled on a 32-bit host the
4017 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4018 debugger inspection of these fields look nicer. */
4019 htab->elf.init_got_refcount.refcount = 0;
4020 htab->elf.init_got_refcount.glist = NULL;
4021 htab->elf.init_plt_refcount.refcount = 0;
4022 htab->elf.init_plt_refcount.glist = NULL;
4023 htab->elf.init_got_offset.offset = 0;
4024 htab->elf.init_got_offset.glist = NULL;
4025 htab->elf.init_plt_offset.offset = 0;
4026 htab->elf.init_plt_offset.glist = NULL;
4028 return &htab->elf.root;
4031 /* Free the derived linker hash table. */
4034 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
4036 struct ppc_link_hash_table *htab = (struct ppc_link_hash_table *) hash;
4038 bfd_hash_table_free (&htab->stub_hash_table);
4039 bfd_hash_table_free (&htab->branch_hash_table);
4040 if (htab->tocsave_htab)
4041 htab_delete (htab->tocsave_htab);
4042 _bfd_generic_link_hash_table_free (hash);
4045 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4048 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4050 struct ppc_link_hash_table *htab;
4052 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4054 /* Always hook our dynamic sections into the first bfd, which is the
4055 linker created stub bfd. This ensures that the GOT header is at
4056 the start of the output TOC section. */
4057 htab = ppc_hash_table (info);
4060 htab->stub_bfd = abfd;
4061 htab->elf.dynobj = abfd;
4064 /* Build a name for an entry in the stub hash table. */
4067 ppc_stub_name (const asection *input_section,
4068 const asection *sym_sec,
4069 const struct ppc_link_hash_entry *h,
4070 const Elf_Internal_Rela *rel)
4075 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4076 offsets from a sym as a branch target? In fact, we could
4077 probably assume the addend is always zero. */
4078 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4082 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4083 stub_name = bfd_malloc (len);
4084 if (stub_name == NULL)
4087 sprintf (stub_name, "%08x.%s+%x",
4088 input_section->id & 0xffffffff,
4089 h->elf.root.root.string,
4090 (int) rel->r_addend & 0xffffffff);
4094 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4095 stub_name = bfd_malloc (len);
4096 if (stub_name == NULL)
4099 sprintf (stub_name, "%08x.%x:%x+%x",
4100 input_section->id & 0xffffffff,
4101 sym_sec->id & 0xffffffff,
4102 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4103 (int) rel->r_addend & 0xffffffff);
4105 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4106 stub_name[len - 2] = 0;
4110 /* Look up an entry in the stub hash. Stub entries are cached because
4111 creating the stub name takes a bit of time. */
4113 static struct ppc_stub_hash_entry *
4114 ppc_get_stub_entry (const asection *input_section,
4115 const asection *sym_sec,
4116 struct ppc_link_hash_entry *h,
4117 const Elf_Internal_Rela *rel,
4118 struct ppc_link_hash_table *htab)
4120 struct ppc_stub_hash_entry *stub_entry;
4121 const asection *id_sec;
4123 /* If this input section is part of a group of sections sharing one
4124 stub section, then use the id of the first section in the group.
4125 Stub names need to include a section id, as there may well be
4126 more than one stub used to reach say, printf, and we need to
4127 distinguish between them. */
4128 id_sec = htab->stub_group[input_section->id].link_sec;
4130 if (h != NULL && h->u.stub_cache != NULL
4131 && h->u.stub_cache->h == h
4132 && h->u.stub_cache->id_sec == id_sec)
4134 stub_entry = h->u.stub_cache;
4140 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4141 if (stub_name == NULL)
4144 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4145 stub_name, FALSE, FALSE);
4147 h->u.stub_cache = stub_entry;
4155 /* Add a new stub entry to the stub hash. Not all fields of the new
4156 stub entry are initialised. */
4158 static struct ppc_stub_hash_entry *
4159 ppc_add_stub (const char *stub_name,
4161 struct bfd_link_info *info)
4163 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4166 struct ppc_stub_hash_entry *stub_entry;
4168 link_sec = htab->stub_group[section->id].link_sec;
4169 stub_sec = htab->stub_group[section->id].stub_sec;
4170 if (stub_sec == NULL)
4172 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4173 if (stub_sec == NULL)
4179 namelen = strlen (link_sec->name);
4180 len = namelen + sizeof (STUB_SUFFIX);
4181 s_name = bfd_alloc (htab->stub_bfd, len);
4185 memcpy (s_name, link_sec->name, namelen);
4186 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4187 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4188 if (stub_sec == NULL)
4190 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4192 htab->stub_group[section->id].stub_sec = stub_sec;
4195 /* Enter this entry into the linker stub hash table. */
4196 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4198 if (stub_entry == NULL)
4200 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4201 section->owner, stub_name);
4205 stub_entry->stub_sec = stub_sec;
4206 stub_entry->stub_offset = 0;
4207 stub_entry->id_sec = link_sec;
4211 /* Create sections for linker generated code. */
4214 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4216 struct ppc_link_hash_table *htab;
4219 htab = ppc_hash_table (info);
4223 /* Create .sfpr for code to save and restore fp regs. */
4224 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4225 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4226 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4228 if (htab->sfpr == NULL
4229 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4232 /* Create .glink for lazy dynamic linking support. */
4233 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4235 if (htab->glink == NULL
4236 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4239 if (!info->no_ld_generated_unwind_info)
4241 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4242 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4243 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4246 if (htab->glink_eh_frame == NULL
4247 || !bfd_set_section_alignment (abfd, htab->glink_eh_frame, 2))
4251 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4252 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4253 if (htab->iplt == NULL
4254 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4257 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4258 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4259 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4262 if (htab->reliplt == NULL
4263 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4266 /* Create branch lookup table for plt_branch stubs. */
4267 flags = (SEC_ALLOC | SEC_LOAD
4268 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4269 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4271 if (htab->brlt == NULL
4272 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4278 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4279 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4280 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4283 if (htab->relbrlt == NULL
4284 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4290 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4291 not already done. */
4294 create_got_section (bfd *abfd, struct bfd_link_info *info)
4296 asection *got, *relgot;
4298 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4300 if (!is_ppc64_elf (abfd))
4307 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4310 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
4315 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4316 | SEC_LINKER_CREATED);
4318 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4320 || !bfd_set_section_alignment (abfd, got, 3))
4323 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4324 flags | SEC_READONLY);
4326 || ! bfd_set_section_alignment (abfd, relgot, 3))
4329 ppc64_elf_tdata (abfd)->got = got;
4330 ppc64_elf_tdata (abfd)->relgot = relgot;
4334 /* Create the dynamic sections, and set up shortcuts. */
4337 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4339 struct ppc_link_hash_table *htab;
4341 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4344 htab = ppc_hash_table (info);
4349 htab->got = bfd_get_section_by_name (dynobj, ".got");
4350 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
4351 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
4352 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
4354 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
4356 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4357 || (!info->shared && !htab->relbss))
4363 /* Follow indirect and warning symbol links. */
4365 static inline struct bfd_link_hash_entry *
4366 follow_link (struct bfd_link_hash_entry *h)
4368 while (h->type == bfd_link_hash_indirect
4369 || h->type == bfd_link_hash_warning)
4374 static inline struct elf_link_hash_entry *
4375 elf_follow_link (struct elf_link_hash_entry *h)
4377 return (struct elf_link_hash_entry *) follow_link (&h->root);
4380 static inline struct ppc_link_hash_entry *
4381 ppc_follow_link (struct ppc_link_hash_entry *h)
4383 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4386 /* Merge PLT info on FROM with that on TO. */
4389 move_plt_plist (struct ppc_link_hash_entry *from,
4390 struct ppc_link_hash_entry *to)
4392 if (from->elf.plt.plist != NULL)
4394 if (to->elf.plt.plist != NULL)
4396 struct plt_entry **entp;
4397 struct plt_entry *ent;
4399 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4401 struct plt_entry *dent;
4403 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4404 if (dent->addend == ent->addend)
4406 dent->plt.refcount += ent->plt.refcount;
4413 *entp = to->elf.plt.plist;
4416 to->elf.plt.plist = from->elf.plt.plist;
4417 from->elf.plt.plist = NULL;
4421 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4424 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4425 struct elf_link_hash_entry *dir,
4426 struct elf_link_hash_entry *ind)
4428 struct ppc_link_hash_entry *edir, *eind;
4430 edir = (struct ppc_link_hash_entry *) dir;
4431 eind = (struct ppc_link_hash_entry *) ind;
4433 edir->is_func |= eind->is_func;
4434 edir->is_func_descriptor |= eind->is_func_descriptor;
4435 edir->tls_mask |= eind->tls_mask;
4436 if (eind->oh != NULL)
4437 edir->oh = ppc_follow_link (eind->oh);
4439 /* If called to transfer flags for a weakdef during processing
4440 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4441 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4442 if (!(ELIMINATE_COPY_RELOCS
4443 && eind->elf.root.type != bfd_link_hash_indirect
4444 && edir->elf.dynamic_adjusted))
4445 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4447 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4448 edir->elf.ref_regular |= eind->elf.ref_regular;
4449 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4450 edir->elf.needs_plt |= eind->elf.needs_plt;
4452 /* Copy over any dynamic relocs we may have on the indirect sym. */
4453 if (eind->dyn_relocs != NULL)
4455 if (edir->dyn_relocs != NULL)
4457 struct elf_dyn_relocs **pp;
4458 struct elf_dyn_relocs *p;
4460 /* Add reloc counts against the indirect sym to the direct sym
4461 list. Merge any entries against the same section. */
4462 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4464 struct elf_dyn_relocs *q;
4466 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4467 if (q->sec == p->sec)
4469 q->pc_count += p->pc_count;
4470 q->count += p->count;
4477 *pp = edir->dyn_relocs;
4480 edir->dyn_relocs = eind->dyn_relocs;
4481 eind->dyn_relocs = NULL;
4484 /* If we were called to copy over info for a weak sym, that's all.
4485 You might think dyn_relocs need not be copied over; After all,
4486 both syms will be dynamic or both non-dynamic so we're just
4487 moving reloc accounting around. However, ELIMINATE_COPY_RELOCS
4488 code in ppc64_elf_adjust_dynamic_symbol needs to check for
4489 dyn_relocs in read-only sections, and it does so on what is the
4491 if (eind->elf.root.type != bfd_link_hash_indirect)
4494 /* Copy over got entries that we may have already seen to the
4495 symbol which just became indirect. */
4496 if (eind->elf.got.glist != NULL)
4498 if (edir->elf.got.glist != NULL)
4500 struct got_entry **entp;
4501 struct got_entry *ent;
4503 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4505 struct got_entry *dent;
4507 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4508 if (dent->addend == ent->addend
4509 && dent->owner == ent->owner
4510 && dent->tls_type == ent->tls_type)
4512 dent->got.refcount += ent->got.refcount;
4519 *entp = edir->elf.got.glist;
4522 edir->elf.got.glist = eind->elf.got.glist;
4523 eind->elf.got.glist = NULL;
4526 /* And plt entries. */
4527 move_plt_plist (eind, edir);
4529 if (eind->elf.dynindx != -1)
4531 if (edir->elf.dynindx != -1)
4532 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4533 edir->elf.dynstr_index);
4534 edir->elf.dynindx = eind->elf.dynindx;
4535 edir->elf.dynstr_index = eind->elf.dynstr_index;
4536 eind->elf.dynindx = -1;
4537 eind->elf.dynstr_index = 0;
4541 /* Find the function descriptor hash entry from the given function code
4542 hash entry FH. Link the entries via their OH fields. */
4544 static struct ppc_link_hash_entry *
4545 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4547 struct ppc_link_hash_entry *fdh = fh->oh;
4551 const char *fd_name = fh->elf.root.root.string + 1;
4553 fdh = (struct ppc_link_hash_entry *)
4554 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4558 fdh->is_func_descriptor = 1;
4564 return ppc_follow_link (fdh);
4567 /* Make a fake function descriptor sym for the code sym FH. */
4569 static struct ppc_link_hash_entry *
4570 make_fdh (struct bfd_link_info *info,
4571 struct ppc_link_hash_entry *fh)
4575 struct bfd_link_hash_entry *bh;
4576 struct ppc_link_hash_entry *fdh;
4578 abfd = fh->elf.root.u.undef.abfd;
4579 newsym = bfd_make_empty_symbol (abfd);
4580 newsym->name = fh->elf.root.root.string + 1;
4581 newsym->section = bfd_und_section_ptr;
4583 newsym->flags = BSF_WEAK;
4586 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4587 newsym->flags, newsym->section,
4588 newsym->value, NULL, FALSE, FALSE,
4592 fdh = (struct ppc_link_hash_entry *) bh;
4593 fdh->elf.non_elf = 0;
4595 fdh->is_func_descriptor = 1;
4602 /* Fix function descriptor symbols defined in .opd sections to be
4606 ppc64_elf_add_symbol_hook (bfd *ibfd,
4607 struct bfd_link_info *info,
4608 Elf_Internal_Sym *isym,
4609 const char **name ATTRIBUTE_UNUSED,
4610 flagword *flags ATTRIBUTE_UNUSED,
4612 bfd_vma *value ATTRIBUTE_UNUSED)
4614 if ((ibfd->flags & DYNAMIC) == 0
4615 && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4616 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4618 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4620 if ((ibfd->flags & DYNAMIC) == 0)
4621 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4623 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4625 else if (*sec != NULL
4626 && strcmp ((*sec)->name, ".opd") == 0)
4627 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4632 /* This function makes an old ABI object reference to ".bar" cause the
4633 inclusion of a new ABI object archive that defines "bar".
4634 NAME is a symbol defined in an archive. Return a symbol in the hash
4635 table that might be satisfied by the archive symbols. */
4637 static struct elf_link_hash_entry *
4638 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4639 struct bfd_link_info *info,
4642 struct elf_link_hash_entry *h;
4646 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4648 /* Don't return this sym if it is a fake function descriptor
4649 created by add_symbol_adjust. */
4650 && !(h->root.type == bfd_link_hash_undefweak
4651 && ((struct ppc_link_hash_entry *) h)->fake))
4657 len = strlen (name);
4658 dot_name = bfd_alloc (abfd, len + 2);
4659 if (dot_name == NULL)
4660 return (struct elf_link_hash_entry *) 0 - 1;
4662 memcpy (dot_name + 1, name, len + 1);
4663 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4664 bfd_release (abfd, dot_name);
4668 /* This function satisfies all old ABI object references to ".bar" if a
4669 new ABI object defines "bar". Well, at least, undefined dot symbols
4670 are made weak. This stops later archive searches from including an
4671 object if we already have a function descriptor definition. It also
4672 prevents the linker complaining about undefined symbols.
4673 We also check and correct mismatched symbol visibility here. The
4674 most restrictive visibility of the function descriptor and the
4675 function entry symbol is used. */
4678 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4680 struct ppc_link_hash_table *htab;
4681 struct ppc_link_hash_entry *fdh;
4683 if (eh->elf.root.type == bfd_link_hash_indirect)
4686 if (eh->elf.root.type == bfd_link_hash_warning)
4687 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4689 if (eh->elf.root.root.string[0] != '.')
4692 htab = ppc_hash_table (info);
4696 fdh = lookup_fdh (eh, htab);
4699 if (!info->relocatable
4700 && (eh->elf.root.type == bfd_link_hash_undefined
4701 || eh->elf.root.type == bfd_link_hash_undefweak)
4702 && eh->elf.ref_regular)
4704 /* Make an undefweak function descriptor sym, which is enough to
4705 pull in an --as-needed shared lib, but won't cause link
4706 errors. Archives are handled elsewhere. */
4707 fdh = make_fdh (info, eh);
4710 fdh->elf.ref_regular = 1;
4715 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4716 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4717 if (entry_vis < descr_vis)
4718 fdh->elf.other += entry_vis - descr_vis;
4719 else if (entry_vis > descr_vis)
4720 eh->elf.other += descr_vis - entry_vis;
4722 if ((fdh->elf.root.type == bfd_link_hash_defined
4723 || fdh->elf.root.type == bfd_link_hash_defweak)
4724 && eh->elf.root.type == bfd_link_hash_undefined)
4726 eh->elf.root.type = bfd_link_hash_undefweak;
4727 eh->was_undefined = 1;
4728 htab->twiddled_syms = 1;
4735 /* Process list of dot-symbols we made in link_hash_newfunc. */
4738 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
4740 struct ppc_link_hash_table *htab;
4741 struct ppc_link_hash_entry **p, *eh;
4743 if (!is_ppc64_elf (info->output_bfd))
4745 htab = ppc_hash_table (info);
4749 if (is_ppc64_elf (ibfd))
4751 p = &htab->dot_syms;
4752 while ((eh = *p) != NULL)
4755 if (!add_symbol_adjust (eh, info))
4757 p = &eh->u.next_dot_sym;
4761 /* Clear the list for non-ppc64 input files. */
4762 p = &htab->dot_syms;
4763 while ((eh = *p) != NULL)
4766 p = &eh->u.next_dot_sym;
4769 /* We need to fix the undefs list for any syms we have twiddled to
4771 if (htab->twiddled_syms)
4773 bfd_link_repair_undef_list (&htab->elf.root);
4774 htab->twiddled_syms = 0;
4779 /* Undo hash table changes when an --as-needed input file is determined
4780 not to be needed. */
4783 ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4784 struct bfd_link_info *info)
4786 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4791 htab->dot_syms = NULL;
4795 /* If --just-symbols against a final linked binary, then assume we need
4796 toc adjusting stubs when calling functions defined there. */
4799 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4801 if ((sec->flags & SEC_CODE) != 0
4802 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4803 && is_ppc64_elf (sec->owner))
4805 asection *got = bfd_get_section_by_name (sec->owner, ".got");
4807 && got->size >= elf_backend_got_header_size
4808 && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4809 sec->has_toc_reloc = 1;
4811 _bfd_elf_link_just_syms (sec, info);
4814 static struct plt_entry **
4815 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4816 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4818 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4819 struct plt_entry **local_plt;
4820 unsigned char *local_got_tls_masks;
4822 if (local_got_ents == NULL)
4824 bfd_size_type size = symtab_hdr->sh_info;
4826 size *= (sizeof (*local_got_ents)
4827 + sizeof (*local_plt)
4828 + sizeof (*local_got_tls_masks));
4829 local_got_ents = bfd_zalloc (abfd, size);
4830 if (local_got_ents == NULL)
4832 elf_local_got_ents (abfd) = local_got_ents;
4835 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
4837 struct got_entry *ent;
4839 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4840 if (ent->addend == r_addend
4841 && ent->owner == abfd
4842 && ent->tls_type == tls_type)
4846 bfd_size_type amt = sizeof (*ent);
4847 ent = bfd_alloc (abfd, amt);
4850 ent->next = local_got_ents[r_symndx];
4851 ent->addend = r_addend;
4853 ent->tls_type = tls_type;
4854 ent->is_indirect = FALSE;
4855 ent->got.refcount = 0;
4856 local_got_ents[r_symndx] = ent;
4858 ent->got.refcount += 1;
4861 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4862 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4863 local_got_tls_masks[r_symndx] |= tls_type;
4865 return local_plt + r_symndx;
4869 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4871 struct plt_entry *ent;
4873 for (ent = *plist; ent != NULL; ent = ent->next)
4874 if (ent->addend == addend)
4878 bfd_size_type amt = sizeof (*ent);
4879 ent = bfd_alloc (abfd, amt);
4883 ent->addend = addend;
4884 ent->plt.refcount = 0;
4887 ent->plt.refcount += 1;
4892 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4894 return (r_type == R_PPC64_REL24
4895 || r_type == R_PPC64_REL14
4896 || r_type == R_PPC64_REL14_BRTAKEN
4897 || r_type == R_PPC64_REL14_BRNTAKEN
4898 || r_type == R_PPC64_ADDR24
4899 || r_type == R_PPC64_ADDR14
4900 || r_type == R_PPC64_ADDR14_BRTAKEN
4901 || r_type == R_PPC64_ADDR14_BRNTAKEN);
4904 /* Look through the relocs for a section during the first phase, and
4905 calculate needed space in the global offset table, procedure
4906 linkage table, and dynamic reloc sections. */
4909 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4910 asection *sec, const Elf_Internal_Rela *relocs)
4912 struct ppc_link_hash_table *htab;
4913 Elf_Internal_Shdr *symtab_hdr;
4914 struct elf_link_hash_entry **sym_hashes;
4915 const Elf_Internal_Rela *rel;
4916 const Elf_Internal_Rela *rel_end;
4918 asection **opd_sym_map;
4919 struct elf_link_hash_entry *tga, *dottga;
4921 if (info->relocatable)
4924 /* Don't do anything special with non-loaded, non-alloced sections.
4925 In particular, any relocs in such sections should not affect GOT
4926 and PLT reference counting (ie. we don't allow them to create GOT
4927 or PLT entries), there's no possibility or desire to optimize TLS
4928 relocs, and there's not much point in propagating relocs to shared
4929 libs that the dynamic linker won't relocate. */
4930 if ((sec->flags & SEC_ALLOC) == 0)
4933 BFD_ASSERT (is_ppc64_elf (abfd));
4935 htab = ppc_hash_table (info);
4939 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4940 FALSE, FALSE, TRUE);
4941 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4942 FALSE, FALSE, TRUE);
4943 symtab_hdr = &elf_symtab_hdr (abfd);
4944 sym_hashes = elf_sym_hashes (abfd);
4947 if (strcmp (sec->name, ".opd") == 0)
4949 /* Garbage collection needs some extra help with .opd sections.
4950 We don't want to necessarily keep everything referenced by
4951 relocs in .opd, as that would keep all functions. Instead,
4952 if we reference an .opd symbol (a function descriptor), we
4953 want to keep the function code symbol's section. This is
4954 easy for global symbols, but for local syms we need to keep
4955 information about the associated function section. */
4958 amt = sec->size * sizeof (*opd_sym_map) / 8;
4959 opd_sym_map = bfd_zalloc (abfd, amt);
4960 if (opd_sym_map == NULL)
4962 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
4963 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4964 ppc64_elf_section_data (sec)->sec_type = sec_opd;
4967 if (htab->sfpr == NULL
4968 && !create_linkage_sections (htab->elf.dynobj, info))
4971 rel_end = relocs + sec->reloc_count;
4972 for (rel = relocs; rel < rel_end; rel++)
4974 unsigned long r_symndx;
4975 struct elf_link_hash_entry *h;
4976 enum elf_ppc64_reloc_type r_type;
4978 struct _ppc64_elf_section_data *ppc64_sec;
4979 struct plt_entry **ifunc;
4981 r_symndx = ELF64_R_SYM (rel->r_info);
4982 if (r_symndx < symtab_hdr->sh_info)
4986 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4987 h = elf_follow_link (h);
4994 if (h->type == STT_GNU_IFUNC)
4997 ifunc = &h->plt.plist;
5002 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5007 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5009 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5010 rel->r_addend, PLT_IFUNC);
5015 r_type = ELF64_R_TYPE (rel->r_info);
5016 if (is_branch_reloc (r_type))
5018 if (h != NULL && (h == tga || h == dottga))
5021 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5022 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5023 /* We have a new-style __tls_get_addr call with a marker
5027 /* Mark this section as having an old-style call. */
5028 sec->has_tls_get_addr_call = 1;
5031 /* STT_GNU_IFUNC symbols must have a PLT entry. */
5033 && !update_plt_info (abfd, ifunc, rel->r_addend))
5041 /* These special tls relocs tie a call to __tls_get_addr with
5042 its parameter symbol. */
5045 case R_PPC64_GOT_TLSLD16:
5046 case R_PPC64_GOT_TLSLD16_LO:
5047 case R_PPC64_GOT_TLSLD16_HI:
5048 case R_PPC64_GOT_TLSLD16_HA:
5049 tls_type = TLS_TLS | TLS_LD;
5052 case R_PPC64_GOT_TLSGD16:
5053 case R_PPC64_GOT_TLSGD16_LO:
5054 case R_PPC64_GOT_TLSGD16_HI:
5055 case R_PPC64_GOT_TLSGD16_HA:
5056 tls_type = TLS_TLS | TLS_GD;
5059 case R_PPC64_GOT_TPREL16_DS:
5060 case R_PPC64_GOT_TPREL16_LO_DS:
5061 case R_PPC64_GOT_TPREL16_HI:
5062 case R_PPC64_GOT_TPREL16_HA:
5063 if (!info->executable)
5064 info->flags |= DF_STATIC_TLS;
5065 tls_type = TLS_TLS | TLS_TPREL;
5068 case R_PPC64_GOT_DTPREL16_DS:
5069 case R_PPC64_GOT_DTPREL16_LO_DS:
5070 case R_PPC64_GOT_DTPREL16_HI:
5071 case R_PPC64_GOT_DTPREL16_HA:
5072 tls_type = TLS_TLS | TLS_DTPREL;
5074 sec->has_tls_reloc = 1;
5078 case R_PPC64_GOT16_DS:
5079 case R_PPC64_GOT16_HA:
5080 case R_PPC64_GOT16_HI:
5081 case R_PPC64_GOT16_LO:
5082 case R_PPC64_GOT16_LO_DS:
5083 /* This symbol requires a global offset table entry. */
5084 sec->has_toc_reloc = 1;
5085 if (r_type == R_PPC64_GOT_TLSLD16
5086 || r_type == R_PPC64_GOT_TLSGD16
5087 || r_type == R_PPC64_GOT_TPREL16_DS
5088 || r_type == R_PPC64_GOT_DTPREL16_DS
5089 || r_type == R_PPC64_GOT16
5090 || r_type == R_PPC64_GOT16_DS)
5092 htab->do_multi_toc = 1;
5093 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5096 if (ppc64_elf_tdata (abfd)->got == NULL
5097 && !create_got_section (abfd, info))
5102 struct ppc_link_hash_entry *eh;
5103 struct got_entry *ent;
5105 eh = (struct ppc_link_hash_entry *) h;
5106 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5107 if (ent->addend == rel->r_addend
5108 && ent->owner == abfd
5109 && ent->tls_type == tls_type)
5113 bfd_size_type amt = sizeof (*ent);
5114 ent = bfd_alloc (abfd, amt);
5117 ent->next = eh->elf.got.glist;
5118 ent->addend = rel->r_addend;
5120 ent->tls_type = tls_type;
5121 ent->is_indirect = FALSE;
5122 ent->got.refcount = 0;
5123 eh->elf.got.glist = ent;
5125 ent->got.refcount += 1;
5126 eh->tls_mask |= tls_type;
5129 /* This is a global offset table entry for a local symbol. */
5130 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5131 rel->r_addend, tls_type))
5135 case R_PPC64_PLT16_HA:
5136 case R_PPC64_PLT16_HI:
5137 case R_PPC64_PLT16_LO:
5140 /* This symbol requires a procedure linkage table entry. We
5141 actually build the entry in adjust_dynamic_symbol,
5142 because this might be a case of linking PIC code without
5143 linking in any dynamic objects, in which case we don't
5144 need to generate a procedure linkage table after all. */
5147 /* It does not make sense to have a procedure linkage
5148 table entry for a local symbol. */
5149 bfd_set_error (bfd_error_bad_value);
5154 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5157 if (h->root.root.string[0] == '.'
5158 && h->root.root.string[1] != '\0')
5159 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5163 /* The following relocations don't need to propagate the
5164 relocation if linking a shared object since they are
5165 section relative. */
5166 case R_PPC64_SECTOFF:
5167 case R_PPC64_SECTOFF_LO:
5168 case R_PPC64_SECTOFF_HI:
5169 case R_PPC64_SECTOFF_HA:
5170 case R_PPC64_SECTOFF_DS:
5171 case R_PPC64_SECTOFF_LO_DS:
5172 case R_PPC64_DTPREL16:
5173 case R_PPC64_DTPREL16_LO:
5174 case R_PPC64_DTPREL16_HI:
5175 case R_PPC64_DTPREL16_HA:
5176 case R_PPC64_DTPREL16_DS:
5177 case R_PPC64_DTPREL16_LO_DS:
5178 case R_PPC64_DTPREL16_HIGHER:
5179 case R_PPC64_DTPREL16_HIGHERA:
5180 case R_PPC64_DTPREL16_HIGHEST:
5181 case R_PPC64_DTPREL16_HIGHESTA:
5186 case R_PPC64_REL16_LO:
5187 case R_PPC64_REL16_HI:
5188 case R_PPC64_REL16_HA:
5192 case R_PPC64_TOC16_DS:
5193 htab->do_multi_toc = 1;
5194 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5195 case R_PPC64_TOC16_LO:
5196 case R_PPC64_TOC16_HI:
5197 case R_PPC64_TOC16_HA:
5198 case R_PPC64_TOC16_LO_DS:
5199 sec->has_toc_reloc = 1;
5202 /* This relocation describes the C++ object vtable hierarchy.
5203 Reconstruct it for later use during GC. */
5204 case R_PPC64_GNU_VTINHERIT:
5205 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5209 /* This relocation describes which C++ vtable entries are actually
5210 used. Record for later use during GC. */
5211 case R_PPC64_GNU_VTENTRY:
5212 BFD_ASSERT (h != NULL);
5214 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5219 case R_PPC64_REL14_BRTAKEN:
5220 case R_PPC64_REL14_BRNTAKEN:
5222 asection *dest = NULL;
5224 /* Heuristic: If jumping outside our section, chances are
5225 we are going to need a stub. */
5228 /* If the sym is weak it may be overridden later, so
5229 don't assume we know where a weak sym lives. */
5230 if (h->root.type == bfd_link_hash_defined)
5231 dest = h->root.u.def.section;
5235 Elf_Internal_Sym *isym;
5237 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5242 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5246 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5251 if (h != NULL && ifunc == NULL)
5253 /* We may need a .plt entry if the function this reloc
5254 refers to is in a shared lib. */
5255 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5258 if (h->root.root.string[0] == '.'
5259 && h->root.root.string[1] != '\0')
5260 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5261 if (h == tga || h == dottga)
5262 sec->has_tls_reloc = 1;
5266 case R_PPC64_TPREL64:
5267 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5268 if (!info->executable)
5269 info->flags |= DF_STATIC_TLS;
5272 case R_PPC64_DTPMOD64:
5273 if (rel + 1 < rel_end
5274 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5275 && rel[1].r_offset == rel->r_offset + 8)
5276 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5278 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5281 case R_PPC64_DTPREL64:
5282 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5284 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5285 && rel[-1].r_offset == rel->r_offset - 8)
5286 /* This is the second reloc of a dtpmod, dtprel pair.
5287 Don't mark with TLS_DTPREL. */
5291 sec->has_tls_reloc = 1;
5294 struct ppc_link_hash_entry *eh;
5295 eh = (struct ppc_link_hash_entry *) h;
5296 eh->tls_mask |= tls_type;
5299 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5300 rel->r_addend, tls_type))
5303 ppc64_sec = ppc64_elf_section_data (sec);
5304 if (ppc64_sec->sec_type != sec_toc)
5308 /* One extra to simplify get_tls_mask. */
5309 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5310 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5311 if (ppc64_sec->u.toc.symndx == NULL)
5313 amt = sec->size * sizeof (bfd_vma) / 8;
5314 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5315 if (ppc64_sec->u.toc.add == NULL)
5317 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5318 ppc64_sec->sec_type = sec_toc;
5320 BFD_ASSERT (rel->r_offset % 8 == 0);
5321 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5322 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5324 /* Mark the second slot of a GD or LD entry.
5325 -1 to indicate GD and -2 to indicate LD. */
5326 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5327 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5328 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5329 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5332 case R_PPC64_TPREL16:
5333 case R_PPC64_TPREL16_LO:
5334 case R_PPC64_TPREL16_HI:
5335 case R_PPC64_TPREL16_HA:
5336 case R_PPC64_TPREL16_DS:
5337 case R_PPC64_TPREL16_LO_DS:
5338 case R_PPC64_TPREL16_HIGHER:
5339 case R_PPC64_TPREL16_HIGHERA:
5340 case R_PPC64_TPREL16_HIGHEST:
5341 case R_PPC64_TPREL16_HIGHESTA:
5344 if (!info->executable)
5345 info->flags |= DF_STATIC_TLS;
5350 case R_PPC64_ADDR64:
5351 if (opd_sym_map != NULL
5352 && rel + 1 < rel_end
5353 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5357 if (h->root.root.string[0] == '.'
5358 && h->root.root.string[1] != 0
5359 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5362 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5367 Elf_Internal_Sym *isym;
5369 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5374 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5375 if (s != NULL && s != sec)
5376 opd_sym_map[rel->r_offset / 8] = s;
5384 case R_PPC64_ADDR14:
5385 case R_PPC64_ADDR14_BRNTAKEN:
5386 case R_PPC64_ADDR14_BRTAKEN:
5387 case R_PPC64_ADDR16:
5388 case R_PPC64_ADDR16_DS:
5389 case R_PPC64_ADDR16_HA:
5390 case R_PPC64_ADDR16_HI:
5391 case R_PPC64_ADDR16_HIGHER:
5392 case R_PPC64_ADDR16_HIGHERA:
5393 case R_PPC64_ADDR16_HIGHEST:
5394 case R_PPC64_ADDR16_HIGHESTA:
5395 case R_PPC64_ADDR16_LO:
5396 case R_PPC64_ADDR16_LO_DS:
5397 case R_PPC64_ADDR24:
5398 case R_PPC64_ADDR32:
5399 case R_PPC64_UADDR16:
5400 case R_PPC64_UADDR32:
5401 case R_PPC64_UADDR64:
5403 if (h != NULL && !info->shared)
5404 /* We may need a copy reloc. */
5407 /* Don't propagate .opd relocs. */
5408 if (NO_OPD_RELOCS && opd_sym_map != NULL)
5411 /* If we are creating a shared library, and this is a reloc
5412 against a global symbol, or a non PC relative reloc
5413 against a local symbol, then we need to copy the reloc
5414 into the shared library. However, if we are linking with
5415 -Bsymbolic, we do not need to copy a reloc against a
5416 global symbol which is defined in an object we are
5417 including in the link (i.e., DEF_REGULAR is set). At
5418 this point we have not seen all the input files, so it is
5419 possible that DEF_REGULAR is not set now but will be set
5420 later (it is never cleared). In case of a weak definition,
5421 DEF_REGULAR may be cleared later by a strong definition in
5422 a shared library. We account for that possibility below by
5423 storing information in the dyn_relocs field of the hash
5424 table entry. A similar situation occurs when creating
5425 shared libraries and symbol visibility changes render the
5428 If on the other hand, we are creating an executable, we
5429 may need to keep relocations for symbols satisfied by a
5430 dynamic library if we manage to avoid copy relocs for the
5434 && (must_be_dyn_reloc (info, r_type)
5436 && (! info->symbolic
5437 || h->root.type == bfd_link_hash_defweak
5438 || !h->def_regular))))
5439 || (ELIMINATE_COPY_RELOCS
5442 && (h->root.type == bfd_link_hash_defweak
5443 || !h->def_regular))
5447 struct elf_dyn_relocs *p;
5448 struct elf_dyn_relocs **head;
5450 /* We must copy these reloc types into the output file.
5451 Create a reloc section in dynobj and make room for
5455 sreloc = _bfd_elf_make_dynamic_reloc_section
5456 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5462 /* If this is a global symbol, we count the number of
5463 relocations we need for this symbol. */
5466 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5470 /* Track dynamic relocs needed for local syms too.
5471 We really need local syms available to do this
5475 Elf_Internal_Sym *isym;
5477 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5482 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5486 vpp = &elf_section_data (s)->local_dynrel;
5487 head = (struct elf_dyn_relocs **) vpp;
5491 if (p == NULL || p->sec != sec)
5493 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5504 if (!must_be_dyn_reloc (info, r_type))
5517 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5518 of the code entry point, and its section. */
5521 opd_entry_value (asection *opd_sec,
5523 asection **code_sec,
5526 bfd *opd_bfd = opd_sec->owner;
5527 Elf_Internal_Rela *relocs;
5528 Elf_Internal_Rela *lo, *hi, *look;
5531 /* No relocs implies we are linking a --just-symbols object. */
5532 if (opd_sec->reloc_count == 0)
5536 if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
5537 return (bfd_vma) -1;
5539 val = bfd_get_64 (opd_bfd, buf);
5540 if (code_sec != NULL)
5542 asection *sec, *likely = NULL;
5543 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5545 && (sec->flags & SEC_LOAD) != 0
5546 && (sec->flags & SEC_ALLOC) != 0)
5551 if (code_off != NULL)
5552 *code_off = val - likely->vma;
5558 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5560 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
5562 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5564 /* Go find the opd reloc at the sym address. */
5566 BFD_ASSERT (lo != NULL);
5567 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5571 look = lo + (hi - lo) / 2;
5572 if (look->r_offset < offset)
5574 else if (look->r_offset > offset)
5578 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5580 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5581 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5583 unsigned long symndx = ELF64_R_SYM (look->r_info);
5586 if (symndx < symtab_hdr->sh_info)
5588 Elf_Internal_Sym *sym;
5590 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5593 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5594 symtab_hdr->sh_info,
5595 0, NULL, NULL, NULL);
5598 symtab_hdr->contents = (bfd_byte *) sym;
5602 val = sym->st_value;
5603 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5604 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5608 struct elf_link_hash_entry **sym_hashes;
5609 struct elf_link_hash_entry *rh;
5611 sym_hashes = elf_sym_hashes (opd_bfd);
5612 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5613 rh = elf_follow_link (rh);
5614 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5615 || rh->root.type == bfd_link_hash_defweak);
5616 val = rh->root.u.def.value;
5617 sec = rh->root.u.def.section;
5619 val += look->r_addend;
5620 if (code_off != NULL)
5622 if (code_sec != NULL)
5624 if (sec != NULL && sec->output_section != NULL)
5625 val += sec->output_section->vma + sec->output_offset;
5634 /* Return true if symbol is defined in a regular object file. */
5637 is_static_defined (struct elf_link_hash_entry *h)
5639 return ((h->root.type == bfd_link_hash_defined
5640 || h->root.type == bfd_link_hash_defweak)
5641 && h->root.u.def.section != NULL
5642 && h->root.u.def.section->output_section != NULL);
5645 /* If FDH is a function descriptor symbol, return the associated code
5646 entry symbol if it is defined. Return NULL otherwise. */
5648 static struct ppc_link_hash_entry *
5649 defined_code_entry (struct ppc_link_hash_entry *fdh)
5651 if (fdh->is_func_descriptor)
5653 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5654 if (fh->elf.root.type == bfd_link_hash_defined
5655 || fh->elf.root.type == bfd_link_hash_defweak)
5661 /* If FH is a function code entry symbol, return the associated
5662 function descriptor symbol if it is defined. Return NULL otherwise. */
5664 static struct ppc_link_hash_entry *
5665 defined_func_desc (struct ppc_link_hash_entry *fh)
5668 && fh->oh->is_func_descriptor)
5670 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5671 if (fdh->elf.root.type == bfd_link_hash_defined
5672 || fdh->elf.root.type == bfd_link_hash_defweak)
5678 /* Mark all our entry sym sections, both opd and code section. */
5681 ppc64_elf_gc_keep (struct bfd_link_info *info)
5683 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5684 struct bfd_sym_chain *sym;
5689 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5691 struct ppc_link_hash_entry *eh, *fh;
5694 eh = (struct ppc_link_hash_entry *)
5695 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5698 if (eh->elf.root.type != bfd_link_hash_defined
5699 && eh->elf.root.type != bfd_link_hash_defweak)
5702 fh = defined_code_entry (eh);
5705 sec = fh->elf.root.u.def.section;
5706 sec->flags |= SEC_KEEP;
5708 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5709 && opd_entry_value (eh->elf.root.u.def.section,
5710 eh->elf.root.u.def.value,
5711 &sec, NULL) != (bfd_vma) -1)
5712 sec->flags |= SEC_KEEP;
5714 sec = eh->elf.root.u.def.section;
5715 sec->flags |= SEC_KEEP;
5719 /* Mark sections containing dynamically referenced symbols. When
5720 building shared libraries, we must assume that any visible symbol is
5724 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5726 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5727 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5728 struct ppc_link_hash_entry *fdh;
5730 /* Dynamic linking info is on the func descriptor sym. */
5731 fdh = defined_func_desc (eh);
5735 if ((eh->elf.root.type == bfd_link_hash_defined
5736 || eh->elf.root.type == bfd_link_hash_defweak)
5737 && (eh->elf.ref_dynamic
5738 || (!info->executable
5739 && eh->elf.def_regular
5740 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5741 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5742 && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
5743 || !bfd_hide_sym_by_version (info->version_info,
5744 eh->elf.root.root.string)))))
5747 struct ppc_link_hash_entry *fh;
5749 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5751 /* Function descriptor syms cause the associated
5752 function code sym section to be marked. */
5753 fh = defined_code_entry (eh);
5756 code_sec = fh->elf.root.u.def.section;
5757 code_sec->flags |= SEC_KEEP;
5759 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5760 && opd_entry_value (eh->elf.root.u.def.section,
5761 eh->elf.root.u.def.value,
5762 &code_sec, NULL) != (bfd_vma) -1)
5763 code_sec->flags |= SEC_KEEP;
5769 /* Return the section that should be marked against GC for a given
5773 ppc64_elf_gc_mark_hook (asection *sec,
5774 struct bfd_link_info *info,
5775 Elf_Internal_Rela *rel,
5776 struct elf_link_hash_entry *h,
5777 Elf_Internal_Sym *sym)
5781 /* Syms return NULL if we're marking .opd, so we avoid marking all
5782 function sections, as all functions are referenced in .opd. */
5784 if (get_opd_info (sec) != NULL)
5789 enum elf_ppc64_reloc_type r_type;
5790 struct ppc_link_hash_entry *eh, *fh, *fdh;
5792 r_type = ELF64_R_TYPE (rel->r_info);
5795 case R_PPC64_GNU_VTINHERIT:
5796 case R_PPC64_GNU_VTENTRY:
5800 switch (h->root.type)
5802 case bfd_link_hash_defined:
5803 case bfd_link_hash_defweak:
5804 eh = (struct ppc_link_hash_entry *) h;
5805 fdh = defined_func_desc (eh);
5809 /* Function descriptor syms cause the associated
5810 function code sym section to be marked. */
5811 fh = defined_code_entry (eh);
5814 /* They also mark their opd section. */
5815 eh->elf.root.u.def.section->gc_mark = 1;
5817 rsec = fh->elf.root.u.def.section;
5819 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5820 && opd_entry_value (eh->elf.root.u.def.section,
5821 eh->elf.root.u.def.value,
5822 &rsec, NULL) != (bfd_vma) -1)
5823 eh->elf.root.u.def.section->gc_mark = 1;
5825 rsec = h->root.u.def.section;
5828 case bfd_link_hash_common:
5829 rsec = h->root.u.c.p->section;
5833 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5839 struct _opd_sec_data *opd;
5841 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5842 opd = get_opd_info (rsec);
5843 if (opd != NULL && opd->func_sec != NULL)
5847 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
5854 /* Update the .got, .plt. and dynamic reloc reference counts for the
5855 section being removed. */
5858 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5859 asection *sec, const Elf_Internal_Rela *relocs)
5861 struct ppc_link_hash_table *htab;
5862 Elf_Internal_Shdr *symtab_hdr;
5863 struct elf_link_hash_entry **sym_hashes;
5864 struct got_entry **local_got_ents;
5865 const Elf_Internal_Rela *rel, *relend;
5867 if (info->relocatable)
5870 if ((sec->flags & SEC_ALLOC) == 0)
5873 elf_section_data (sec)->local_dynrel = NULL;
5875 htab = ppc_hash_table (info);
5879 symtab_hdr = &elf_symtab_hdr (abfd);
5880 sym_hashes = elf_sym_hashes (abfd);
5881 local_got_ents = elf_local_got_ents (abfd);
5883 relend = relocs + sec->reloc_count;
5884 for (rel = relocs; rel < relend; rel++)
5886 unsigned long r_symndx;
5887 enum elf_ppc64_reloc_type r_type;
5888 struct elf_link_hash_entry *h = NULL;
5889 unsigned char tls_type = 0;
5891 r_symndx = ELF64_R_SYM (rel->r_info);
5892 r_type = ELF64_R_TYPE (rel->r_info);
5893 if (r_symndx >= symtab_hdr->sh_info)
5895 struct ppc_link_hash_entry *eh;
5896 struct elf_dyn_relocs **pp;
5897 struct elf_dyn_relocs *p;
5899 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5900 h = elf_follow_link (h);
5901 eh = (struct ppc_link_hash_entry *) h;
5903 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5906 /* Everything must go for SEC. */
5912 if (is_branch_reloc (r_type))
5914 struct plt_entry **ifunc = NULL;
5917 if (h->type == STT_GNU_IFUNC)
5918 ifunc = &h->plt.plist;
5920 else if (local_got_ents != NULL)
5922 struct plt_entry **local_plt = (struct plt_entry **)
5923 (local_got_ents + symtab_hdr->sh_info);
5924 unsigned char *local_got_tls_masks = (unsigned char *)
5925 (local_plt + symtab_hdr->sh_info);
5926 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
5927 ifunc = local_plt + r_symndx;
5931 struct plt_entry *ent;
5933 for (ent = *ifunc; ent != NULL; ent = ent->next)
5934 if (ent->addend == rel->r_addend)
5938 if (ent->plt.refcount > 0)
5939 ent->plt.refcount -= 1;
5946 case R_PPC64_GOT_TLSLD16:
5947 case R_PPC64_GOT_TLSLD16_LO:
5948 case R_PPC64_GOT_TLSLD16_HI:
5949 case R_PPC64_GOT_TLSLD16_HA:
5950 tls_type = TLS_TLS | TLS_LD;
5953 case R_PPC64_GOT_TLSGD16:
5954 case R_PPC64_GOT_TLSGD16_LO:
5955 case R_PPC64_GOT_TLSGD16_HI:
5956 case R_PPC64_GOT_TLSGD16_HA:
5957 tls_type = TLS_TLS | TLS_GD;
5960 case R_PPC64_GOT_TPREL16_DS:
5961 case R_PPC64_GOT_TPREL16_LO_DS:
5962 case R_PPC64_GOT_TPREL16_HI:
5963 case R_PPC64_GOT_TPREL16_HA:
5964 tls_type = TLS_TLS | TLS_TPREL;
5967 case R_PPC64_GOT_DTPREL16_DS:
5968 case R_PPC64_GOT_DTPREL16_LO_DS:
5969 case R_PPC64_GOT_DTPREL16_HI:
5970 case R_PPC64_GOT_DTPREL16_HA:
5971 tls_type = TLS_TLS | TLS_DTPREL;
5975 case R_PPC64_GOT16_DS:
5976 case R_PPC64_GOT16_HA:
5977 case R_PPC64_GOT16_HI:
5978 case R_PPC64_GOT16_LO:
5979 case R_PPC64_GOT16_LO_DS:
5982 struct got_entry *ent;
5987 ent = local_got_ents[r_symndx];
5989 for (; ent != NULL; ent = ent->next)
5990 if (ent->addend == rel->r_addend
5991 && ent->owner == abfd
5992 && ent->tls_type == tls_type)
5996 if (ent->got.refcount > 0)
5997 ent->got.refcount -= 1;
6001 case R_PPC64_PLT16_HA:
6002 case R_PPC64_PLT16_HI:
6003 case R_PPC64_PLT16_LO:
6007 case R_PPC64_REL14_BRNTAKEN:
6008 case R_PPC64_REL14_BRTAKEN:
6012 struct plt_entry *ent;
6014 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6015 if (ent->addend == rel->r_addend)
6017 if (ent != NULL && ent->plt.refcount > 0)
6018 ent->plt.refcount -= 1;
6029 /* The maximum size of .sfpr. */
6030 #define SFPR_MAX (218*4)
6032 struct sfpr_def_parms
6034 const char name[12];
6035 unsigned char lo, hi;
6036 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6037 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6040 /* Auto-generate _save*, _rest* functions in .sfpr. */
6043 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6045 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6047 size_t len = strlen (parm->name);
6048 bfd_boolean writing = FALSE;
6054 memcpy (sym, parm->name, len);
6057 for (i = parm->lo; i <= parm->hi; i++)
6059 struct elf_link_hash_entry *h;
6061 sym[len + 0] = i / 10 + '0';
6062 sym[len + 1] = i % 10 + '0';
6063 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6067 h->root.type = bfd_link_hash_defined;
6068 h->root.u.def.section = htab->sfpr;
6069 h->root.u.def.value = htab->sfpr->size;
6072 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6074 if (htab->sfpr->contents == NULL)
6076 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6077 if (htab->sfpr->contents == NULL)
6083 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6085 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6087 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6088 htab->sfpr->size = p - htab->sfpr->contents;
6096 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6098 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6103 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6105 p = savegpr0 (abfd, p, r);
6106 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6108 bfd_put_32 (abfd, BLR, p);
6113 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6115 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6120 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6122 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6124 p = restgpr0 (abfd, p, r);
6125 bfd_put_32 (abfd, MTLR_R0, p);
6129 p = restgpr0 (abfd, p, 30);
6130 p = restgpr0 (abfd, p, 31);
6132 bfd_put_32 (abfd, BLR, p);
6137 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6139 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6144 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6146 p = savegpr1 (abfd, p, r);
6147 bfd_put_32 (abfd, BLR, p);
6152 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6154 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6159 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6161 p = restgpr1 (abfd, p, r);
6162 bfd_put_32 (abfd, BLR, p);
6167 savefpr (bfd *abfd, bfd_byte *p, int r)
6169 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6174 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6176 p = savefpr (abfd, p, r);
6177 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6179 bfd_put_32 (abfd, BLR, p);
6184 restfpr (bfd *abfd, bfd_byte *p, int r)
6186 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6191 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6193 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6195 p = restfpr (abfd, p, r);
6196 bfd_put_32 (abfd, MTLR_R0, p);
6200 p = restfpr (abfd, p, 30);
6201 p = restfpr (abfd, p, 31);
6203 bfd_put_32 (abfd, BLR, p);
6208 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6210 p = savefpr (abfd, p, r);
6211 bfd_put_32 (abfd, BLR, p);
6216 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6218 p = restfpr (abfd, p, r);
6219 bfd_put_32 (abfd, BLR, p);
6224 savevr (bfd *abfd, bfd_byte *p, int r)
6226 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6228 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6233 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6235 p = savevr (abfd, p, r);
6236 bfd_put_32 (abfd, BLR, p);
6241 restvr (bfd *abfd, bfd_byte *p, int r)
6243 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6245 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6250 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6252 p = restvr (abfd, p, r);
6253 bfd_put_32 (abfd, BLR, p);
6257 /* Called via elf_link_hash_traverse to transfer dynamic linking
6258 information on function code symbol entries to their corresponding
6259 function descriptor symbol entries. */
6262 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6264 struct bfd_link_info *info;
6265 struct ppc_link_hash_table *htab;
6266 struct plt_entry *ent;
6267 struct ppc_link_hash_entry *fh;
6268 struct ppc_link_hash_entry *fdh;
6269 bfd_boolean force_local;
6271 fh = (struct ppc_link_hash_entry *) h;
6272 if (fh->elf.root.type == bfd_link_hash_indirect)
6276 htab = ppc_hash_table (info);
6280 /* Resolve undefined references to dot-symbols as the value
6281 in the function descriptor, if we have one in a regular object.
6282 This is to satisfy cases like ".quad .foo". Calls to functions
6283 in dynamic objects are handled elsewhere. */
6284 if (fh->elf.root.type == bfd_link_hash_undefweak
6285 && fh->was_undefined
6286 && (fdh = defined_func_desc (fh)) != NULL
6287 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6288 && opd_entry_value (fdh->elf.root.u.def.section,
6289 fdh->elf.root.u.def.value,
6290 &fh->elf.root.u.def.section,
6291 &fh->elf.root.u.def.value) != (bfd_vma) -1)
6293 fh->elf.root.type = fdh->elf.root.type;
6294 fh->elf.forced_local = 1;
6295 fh->elf.def_regular = fdh->elf.def_regular;
6296 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6299 /* If this is a function code symbol, transfer dynamic linking
6300 information to the function descriptor symbol. */
6304 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6305 if (ent->plt.refcount > 0)
6308 || fh->elf.root.root.string[0] != '.'
6309 || fh->elf.root.root.string[1] == '\0')
6312 /* Find the corresponding function descriptor symbol. Create it
6313 as undefined if necessary. */
6315 fdh = lookup_fdh (fh, htab);
6317 && !info->executable
6318 && (fh->elf.root.type == bfd_link_hash_undefined
6319 || fh->elf.root.type == bfd_link_hash_undefweak))
6321 fdh = make_fdh (info, fh);
6326 /* Fake function descriptors are made undefweak. If the function
6327 code symbol is strong undefined, make the fake sym the same.
6328 If the function code symbol is defined, then force the fake
6329 descriptor local; We can't support overriding of symbols in a
6330 shared library on a fake descriptor. */
6334 && fdh->elf.root.type == bfd_link_hash_undefweak)
6336 if (fh->elf.root.type == bfd_link_hash_undefined)
6338 fdh->elf.root.type = bfd_link_hash_undefined;
6339 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6341 else if (fh->elf.root.type == bfd_link_hash_defined
6342 || fh->elf.root.type == bfd_link_hash_defweak)
6344 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6349 && !fdh->elf.forced_local
6350 && (!info->executable
6351 || fdh->elf.def_dynamic
6352 || fdh->elf.ref_dynamic
6353 || (fdh->elf.root.type == bfd_link_hash_undefweak
6354 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6356 if (fdh->elf.dynindx == -1)
6357 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6359 fdh->elf.ref_regular |= fh->elf.ref_regular;
6360 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6361 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6362 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6363 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6365 move_plt_plist (fh, fdh);
6366 fdh->elf.needs_plt = 1;
6368 fdh->is_func_descriptor = 1;
6373 /* Now that the info is on the function descriptor, clear the
6374 function code sym info. Any function code syms for which we
6375 don't have a definition in a regular file, we force local.
6376 This prevents a shared library from exporting syms that have
6377 been imported from another library. Function code syms that
6378 are really in the library we must leave global to prevent the
6379 linker dragging in a definition from a static library. */
6380 force_local = (!fh->elf.def_regular
6382 || !fdh->elf.def_regular
6383 || fdh->elf.forced_local);
6384 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6389 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6390 this hook to a) provide some gcc support functions, and b) transfer
6391 dynamic linking information gathered so far on function code symbol
6392 entries, to their corresponding function descriptor symbol entries. */
6395 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6396 struct bfd_link_info *info)
6398 struct ppc_link_hash_table *htab;
6400 const struct sfpr_def_parms funcs[] =
6402 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6403 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6404 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6405 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6406 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6407 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6408 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6409 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6410 { "._savef", 14, 31, savefpr, savefpr1_tail },
6411 { "._restf", 14, 31, restfpr, restfpr1_tail },
6412 { "_savevr_", 20, 31, savevr, savevr_tail },
6413 { "_restvr_", 20, 31, restvr, restvr_tail }
6416 htab = ppc_hash_table (info);
6420 if (htab->sfpr == NULL)
6421 /* We don't have any relocs. */
6424 /* Provide any missing _save* and _rest* functions. */
6425 htab->sfpr->size = 0;
6426 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6427 if (!sfpr_define (info, &funcs[i]))
6430 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6432 if (htab->sfpr->size == 0)
6433 htab->sfpr->flags |= SEC_EXCLUDE;
6438 /* Adjust a symbol defined by a dynamic object and referenced by a
6439 regular object. The current definition is in some section of the
6440 dynamic object, but we're not including those sections. We have to
6441 change the definition to something the rest of the link can
6445 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6446 struct elf_link_hash_entry *h)
6448 struct ppc_link_hash_table *htab;
6451 htab = ppc_hash_table (info);
6455 /* Deal with function syms. */
6456 if (h->type == STT_FUNC
6457 || h->type == STT_GNU_IFUNC
6460 /* Clear procedure linkage table information for any symbol that
6461 won't need a .plt entry. */
6462 struct plt_entry *ent;
6463 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6464 if (ent->plt.refcount > 0)
6467 || (h->type != STT_GNU_IFUNC
6468 && (SYMBOL_CALLS_LOCAL (info, h)
6469 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6470 && h->root.type == bfd_link_hash_undefweak))))
6472 h->plt.plist = NULL;
6477 h->plt.plist = NULL;
6479 /* If this is a weak symbol, and there is a real definition, the
6480 processor independent code will have arranged for us to see the
6481 real definition first, and we can just use the same value. */
6482 if (h->u.weakdef != NULL)
6484 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6485 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6486 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6487 h->root.u.def.value = h->u.weakdef->root.u.def.value;
6488 if (ELIMINATE_COPY_RELOCS)
6489 h->non_got_ref = h->u.weakdef->non_got_ref;
6493 /* If we are creating a shared library, we must presume that the
6494 only references to the symbol are via the global offset table.
6495 For such cases we need not do anything here; the relocations will
6496 be handled correctly by relocate_section. */
6500 /* If there are no references to this symbol that do not use the
6501 GOT, we don't need to generate a copy reloc. */
6502 if (!h->non_got_ref)
6505 /* Don't generate a copy reloc for symbols defined in the executable. */
6506 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6509 if (ELIMINATE_COPY_RELOCS)
6511 struct ppc_link_hash_entry * eh;
6512 struct elf_dyn_relocs *p;
6514 eh = (struct ppc_link_hash_entry *) h;
6515 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6517 s = p->sec->output_section;
6518 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6522 /* If we didn't find any dynamic relocs in read-only sections, then
6523 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6531 if (h->plt.plist != NULL)
6533 /* We should never get here, but unfortunately there are versions
6534 of gcc out there that improperly (for this ABI) put initialized
6535 function pointers, vtable refs and suchlike in read-only
6536 sections. Allow them to proceed, but warn that this might
6537 break at runtime. */
6538 info->callbacks->einfo
6539 (_("%P: copy reloc against `%s' requires lazy plt linking; "
6540 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6541 h->root.root.string);
6544 /* This is a reference to a symbol defined by a dynamic object which
6545 is not a function. */
6549 info->callbacks->einfo (_("%P: dynamic variable `%s' is zero size\n"),
6550 h->root.root.string);
6554 /* We must allocate the symbol in our .dynbss section, which will
6555 become part of the .bss section of the executable. There will be
6556 an entry for this symbol in the .dynsym section. The dynamic
6557 object will contain position independent code, so all references
6558 from the dynamic object to this symbol will go through the global
6559 offset table. The dynamic linker will use the .dynsym entry to
6560 determine the address it must put in the global offset table, so
6561 both the dynamic object and the regular object will refer to the
6562 same memory location for the variable. */
6564 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6565 to copy the initial value out of the dynamic object and into the
6566 runtime process image. We need to remember the offset into the
6567 .rela.bss section we are going to use. */
6568 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6570 htab->relbss->size += sizeof (Elf64_External_Rela);
6576 return _bfd_elf_adjust_dynamic_copy (h, s);
6579 /* If given a function descriptor symbol, hide both the function code
6580 sym and the descriptor. */
6582 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6583 struct elf_link_hash_entry *h,
6584 bfd_boolean force_local)
6586 struct ppc_link_hash_entry *eh;
6587 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6589 eh = (struct ppc_link_hash_entry *) h;
6590 if (eh->is_func_descriptor)
6592 struct ppc_link_hash_entry *fh = eh->oh;
6597 struct ppc_link_hash_table *htab;
6600 /* We aren't supposed to use alloca in BFD because on
6601 systems which do not have alloca the version in libiberty
6602 calls xmalloc, which might cause the program to crash
6603 when it runs out of memory. This function doesn't have a
6604 return status, so there's no way to gracefully return an
6605 error. So cheat. We know that string[-1] can be safely
6606 accessed; It's either a string in an ELF string table,
6607 or allocated in an objalloc structure. */
6609 p = eh->elf.root.root.string - 1;
6612 htab = ppc_hash_table (info);
6616 fh = (struct ppc_link_hash_entry *)
6617 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6620 /* Unfortunately, if it so happens that the string we were
6621 looking for was allocated immediately before this string,
6622 then we overwrote the string terminator. That's the only
6623 reason the lookup should fail. */
6626 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6627 while (q >= eh->elf.root.root.string && *q == *p)
6629 if (q < eh->elf.root.root.string && *p == '.')
6630 fh = (struct ppc_link_hash_entry *)
6631 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6640 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6645 get_sym_h (struct elf_link_hash_entry **hp,
6646 Elf_Internal_Sym **symp,
6648 unsigned char **tls_maskp,
6649 Elf_Internal_Sym **locsymsp,
6650 unsigned long r_symndx,
6653 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6655 if (r_symndx >= symtab_hdr->sh_info)
6657 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6658 struct elf_link_hash_entry *h;
6660 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6661 h = elf_follow_link (h);
6669 if (symsecp != NULL)
6671 asection *symsec = NULL;
6672 if (h->root.type == bfd_link_hash_defined
6673 || h->root.type == bfd_link_hash_defweak)
6674 symsec = h->root.u.def.section;
6678 if (tls_maskp != NULL)
6680 struct ppc_link_hash_entry *eh;
6682 eh = (struct ppc_link_hash_entry *) h;
6683 *tls_maskp = &eh->tls_mask;
6688 Elf_Internal_Sym *sym;
6689 Elf_Internal_Sym *locsyms = *locsymsp;
6691 if (locsyms == NULL)
6693 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6694 if (locsyms == NULL)
6695 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6696 symtab_hdr->sh_info,
6697 0, NULL, NULL, NULL);
6698 if (locsyms == NULL)
6700 *locsymsp = locsyms;
6702 sym = locsyms + r_symndx;
6710 if (symsecp != NULL)
6711 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6713 if (tls_maskp != NULL)
6715 struct got_entry **lgot_ents;
6716 unsigned char *tls_mask;
6719 lgot_ents = elf_local_got_ents (ibfd);
6720 if (lgot_ents != NULL)
6722 struct plt_entry **local_plt = (struct plt_entry **)
6723 (lgot_ents + symtab_hdr->sh_info);
6724 unsigned char *lgot_masks = (unsigned char *)
6725 (local_plt + symtab_hdr->sh_info);
6726 tls_mask = &lgot_masks[r_symndx];
6728 *tls_maskp = tls_mask;
6734 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6735 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6736 type suitable for optimization, and 1 otherwise. */
6739 get_tls_mask (unsigned char **tls_maskp,
6740 unsigned long *toc_symndx,
6741 bfd_vma *toc_addend,
6742 Elf_Internal_Sym **locsymsp,
6743 const Elf_Internal_Rela *rel,
6746 unsigned long r_symndx;
6748 struct elf_link_hash_entry *h;
6749 Elf_Internal_Sym *sym;
6753 r_symndx = ELF64_R_SYM (rel->r_info);
6754 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6757 if ((*tls_maskp != NULL && **tls_maskp != 0)
6759 || ppc64_elf_section_data (sec) == NULL
6760 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6763 /* Look inside a TOC section too. */
6766 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6767 off = h->root.u.def.value;
6770 off = sym->st_value;
6771 off += rel->r_addend;
6772 BFD_ASSERT (off % 8 == 0);
6773 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6774 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6775 if (toc_symndx != NULL)
6776 *toc_symndx = r_symndx;
6777 if (toc_addend != NULL)
6778 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6779 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6781 if ((h == NULL || is_static_defined (h))
6782 && (next_r == -1 || next_r == -2))
6787 /* Find (or create) an entry in the tocsave hash table. */
6789 static struct tocsave_entry *
6790 tocsave_find (struct ppc_link_hash_table *htab,
6791 enum insert_option insert,
6792 Elf_Internal_Sym **local_syms,
6793 const Elf_Internal_Rela *irela,
6796 unsigned long r_indx;
6797 struct elf_link_hash_entry *h;
6798 Elf_Internal_Sym *sym;
6799 struct tocsave_entry ent, *p;
6801 struct tocsave_entry **slot;
6803 r_indx = ELF64_R_SYM (irela->r_info);
6804 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6806 if (ent.sec == NULL || ent.sec->output_section == NULL)
6808 (*_bfd_error_handler)
6809 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
6814 ent.offset = h->root.u.def.value;
6816 ent.offset = sym->st_value;
6817 ent.offset += irela->r_addend;
6819 hash = tocsave_htab_hash (&ent);
6820 slot = ((struct tocsave_entry **)
6821 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6827 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6836 /* Adjust all global syms defined in opd sections. In gcc generated
6837 code for the old ABI, these will already have been done. */
6840 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6842 struct ppc_link_hash_entry *eh;
6844 struct _opd_sec_data *opd;
6846 if (h->root.type == bfd_link_hash_indirect)
6849 if (h->root.type != bfd_link_hash_defined
6850 && h->root.type != bfd_link_hash_defweak)
6853 eh = (struct ppc_link_hash_entry *) h;
6854 if (eh->adjust_done)
6857 sym_sec = eh->elf.root.u.def.section;
6858 opd = get_opd_info (sym_sec);
6859 if (opd != NULL && opd->adjust != NULL)
6861 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
6864 /* This entry has been deleted. */
6865 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6868 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6869 if (elf_discarded_section (dsec))
6871 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6875 eh->elf.root.u.def.value = 0;
6876 eh->elf.root.u.def.section = dsec;
6879 eh->elf.root.u.def.value += adjust;
6880 eh->adjust_done = 1;
6885 /* Handles decrementing dynamic reloc counts for the reloc specified by
6886 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6887 have already been determined. */
6890 dec_dynrel_count (bfd_vma r_info,
6892 struct bfd_link_info *info,
6893 Elf_Internal_Sym **local_syms,
6894 struct elf_link_hash_entry *h,
6897 enum elf_ppc64_reloc_type r_type;
6898 struct elf_dyn_relocs *p;
6899 struct elf_dyn_relocs **pp;
6901 /* Can this reloc be dynamic? This switch, and later tests here
6902 should be kept in sync with the code in check_relocs. */
6903 r_type = ELF64_R_TYPE (r_info);
6909 case R_PPC64_TPREL16:
6910 case R_PPC64_TPREL16_LO:
6911 case R_PPC64_TPREL16_HI:
6912 case R_PPC64_TPREL16_HA:
6913 case R_PPC64_TPREL16_DS:
6914 case R_PPC64_TPREL16_LO_DS:
6915 case R_PPC64_TPREL16_HIGHER:
6916 case R_PPC64_TPREL16_HIGHERA:
6917 case R_PPC64_TPREL16_HIGHEST:
6918 case R_PPC64_TPREL16_HIGHESTA:
6922 case R_PPC64_TPREL64:
6923 case R_PPC64_DTPMOD64:
6924 case R_PPC64_DTPREL64:
6925 case R_PPC64_ADDR64:
6929 case R_PPC64_ADDR14:
6930 case R_PPC64_ADDR14_BRNTAKEN:
6931 case R_PPC64_ADDR14_BRTAKEN:
6932 case R_PPC64_ADDR16:
6933 case R_PPC64_ADDR16_DS:
6934 case R_PPC64_ADDR16_HA:
6935 case R_PPC64_ADDR16_HI:
6936 case R_PPC64_ADDR16_HIGHER:
6937 case R_PPC64_ADDR16_HIGHERA:
6938 case R_PPC64_ADDR16_HIGHEST:
6939 case R_PPC64_ADDR16_HIGHESTA:
6940 case R_PPC64_ADDR16_LO:
6941 case R_PPC64_ADDR16_LO_DS:
6942 case R_PPC64_ADDR24:
6943 case R_PPC64_ADDR32:
6944 case R_PPC64_UADDR16:
6945 case R_PPC64_UADDR32:
6946 case R_PPC64_UADDR64:
6951 if (local_syms != NULL)
6953 unsigned long r_symndx;
6954 Elf_Internal_Sym *sym;
6955 bfd *ibfd = sec->owner;
6957 r_symndx = ELF64_R_SYM (r_info);
6958 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6963 && (must_be_dyn_reloc (info, r_type)
6966 || h->root.type == bfd_link_hash_defweak
6967 || !h->def_regular))))
6968 || (ELIMINATE_COPY_RELOCS
6971 && (h->root.type == bfd_link_hash_defweak
6972 || !h->def_regular)))
6978 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6981 if (sym_sec != NULL)
6983 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6984 pp = (struct elf_dyn_relocs **) vpp;
6988 void *vpp = &elf_section_data (sec)->local_dynrel;
6989 pp = (struct elf_dyn_relocs **) vpp;
6992 /* elf_gc_sweep may have already removed all dyn relocs associated
6993 with local syms for a given section. Don't report a dynreloc
6999 while ((p = *pp) != NULL)
7003 if (!must_be_dyn_reloc (info, r_type))
7013 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7015 bfd_set_error (bfd_error_bad_value);
7019 /* Remove unused Official Procedure Descriptor entries. Currently we
7020 only remove those associated with functions in discarded link-once
7021 sections, or weakly defined functions that have been overridden. It
7022 would be possible to remove many more entries for statically linked
7026 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
7029 bfd_boolean some_edited = FALSE;
7030 asection *need_pad = NULL;
7032 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7035 Elf_Internal_Rela *relstart, *rel, *relend;
7036 Elf_Internal_Shdr *symtab_hdr;
7037 Elf_Internal_Sym *local_syms;
7039 struct _opd_sec_data *opd;
7040 bfd_boolean need_edit, add_aux_fields;
7041 bfd_size_type cnt_16b = 0;
7043 if (!is_ppc64_elf (ibfd))
7046 sec = bfd_get_section_by_name (ibfd, ".opd");
7047 if (sec == NULL || sec->size == 0)
7050 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
7053 if (sec->output_section == bfd_abs_section_ptr)
7056 /* Look through the section relocs. */
7057 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7061 symtab_hdr = &elf_symtab_hdr (ibfd);
7063 /* Read the relocations. */
7064 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7066 if (relstart == NULL)
7069 /* First run through the relocs to check they are sane, and to
7070 determine whether we need to edit this opd section. */
7074 relend = relstart + sec->reloc_count;
7075 for (rel = relstart; rel < relend; )
7077 enum elf_ppc64_reloc_type r_type;
7078 unsigned long r_symndx;
7080 struct elf_link_hash_entry *h;
7081 Elf_Internal_Sym *sym;
7083 /* .opd contains a regular array of 16 or 24 byte entries. We're
7084 only interested in the reloc pointing to a function entry
7086 if (rel->r_offset != offset
7087 || rel + 1 >= relend
7088 || (rel + 1)->r_offset != offset + 8)
7090 /* If someone messes with .opd alignment then after a
7091 "ld -r" we might have padding in the middle of .opd.
7092 Also, there's nothing to prevent someone putting
7093 something silly in .opd with the assembler. No .opd
7094 optimization for them! */
7096 (*_bfd_error_handler)
7097 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7102 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7103 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7105 (*_bfd_error_handler)
7106 (_("%B: unexpected reloc type %u in .opd section"),
7112 r_symndx = ELF64_R_SYM (rel->r_info);
7113 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7117 if (sym_sec == NULL || sym_sec->owner == NULL)
7119 const char *sym_name;
7121 sym_name = h->root.root.string;
7123 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7126 (*_bfd_error_handler)
7127 (_("%B: undefined sym `%s' in .opd section"),
7133 /* opd entries are always for functions defined in the
7134 current input bfd. If the symbol isn't defined in the
7135 input bfd, then we won't be using the function in this
7136 bfd; It must be defined in a linkonce section in another
7137 bfd, or is weak. It's also possible that we are
7138 discarding the function due to a linker script /DISCARD/,
7139 which we test for via the output_section. */
7140 if (sym_sec->owner != ibfd
7141 || sym_sec->output_section == bfd_abs_section_ptr)
7146 || (rel + 1 == relend && rel->r_offset == offset + 16))
7148 if (sec->size == offset + 24)
7153 if (rel == relend && sec->size == offset + 16)
7161 if (rel->r_offset == offset + 24)
7163 else if (rel->r_offset != offset + 16)
7165 else if (rel + 1 < relend
7166 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7167 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7172 else if (rel + 2 < relend
7173 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7174 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7183 add_aux_fields = non_overlapping && cnt_16b > 0;
7185 if (need_edit || add_aux_fields)
7187 Elf_Internal_Rela *write_rel;
7188 Elf_Internal_Shdr *rel_hdr;
7189 bfd_byte *rptr, *wptr;
7190 bfd_byte *new_contents;
7195 new_contents = NULL;
7196 amt = sec->size * sizeof (long) / 8;
7197 opd = &ppc64_elf_section_data (sec)->u.opd;
7198 opd->adjust = bfd_zalloc (sec->owner, amt);
7199 if (opd->adjust == NULL)
7201 ppc64_elf_section_data (sec)->sec_type = sec_opd;
7203 /* This seems a waste of time as input .opd sections are all
7204 zeros as generated by gcc, but I suppose there's no reason
7205 this will always be so. We might start putting something in
7206 the third word of .opd entries. */
7207 if ((sec->flags & SEC_IN_MEMORY) == 0)
7210 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7215 if (local_syms != NULL
7216 && symtab_hdr->contents != (unsigned char *) local_syms)
7218 if (elf_section_data (sec)->relocs != relstart)
7222 sec->contents = loc;
7223 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7226 elf_section_data (sec)->relocs = relstart;
7228 new_contents = sec->contents;
7231 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7232 if (new_contents == NULL)
7236 wptr = new_contents;
7237 rptr = sec->contents;
7239 write_rel = relstart;
7243 for (rel = relstart; rel < relend; rel++)
7245 unsigned long r_symndx;
7247 struct elf_link_hash_entry *h;
7248 Elf_Internal_Sym *sym;
7250 r_symndx = ELF64_R_SYM (rel->r_info);
7251 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7255 if (rel->r_offset == offset)
7257 struct ppc_link_hash_entry *fdh = NULL;
7259 /* See if the .opd entry is full 24 byte or
7260 16 byte (with fd_aux entry overlapped with next
7263 if ((rel + 2 == relend && sec->size == offset + 16)
7264 || (rel + 3 < relend
7265 && rel[2].r_offset == offset + 16
7266 && rel[3].r_offset == offset + 24
7267 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7268 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7272 && h->root.root.string[0] == '.')
7274 struct ppc_link_hash_table *htab;
7276 htab = ppc_hash_table (info);
7278 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7281 && fdh->elf.root.type != bfd_link_hash_defined
7282 && fdh->elf.root.type != bfd_link_hash_defweak)
7286 skip = (sym_sec->owner != ibfd
7287 || sym_sec->output_section == bfd_abs_section_ptr);
7290 if (fdh != NULL && sym_sec->owner == ibfd)
7292 /* Arrange for the function descriptor sym
7294 fdh->elf.root.u.def.value = 0;
7295 fdh->elf.root.u.def.section = sym_sec;
7297 opd->adjust[rel->r_offset / 8] = -1;
7301 /* We'll be keeping this opd entry. */
7305 /* Redefine the function descriptor symbol to
7306 this location in the opd section. It is
7307 necessary to update the value here rather
7308 than using an array of adjustments as we do
7309 for local symbols, because various places
7310 in the generic ELF code use the value
7311 stored in u.def.value. */
7312 fdh->elf.root.u.def.value = wptr - new_contents;
7313 fdh->adjust_done = 1;
7316 /* Local syms are a bit tricky. We could
7317 tweak them as they can be cached, but
7318 we'd need to look through the local syms
7319 for the function descriptor sym which we
7320 don't have at the moment. So keep an
7321 array of adjustments. */
7322 opd->adjust[rel->r_offset / 8]
7323 = (wptr - new_contents) - (rptr - sec->contents);
7326 memcpy (wptr, rptr, opd_ent_size);
7327 wptr += opd_ent_size;
7328 if (add_aux_fields && opd_ent_size == 16)
7330 memset (wptr, '\0', 8);
7334 rptr += opd_ent_size;
7335 offset += opd_ent_size;
7341 && !info->relocatable
7342 && !dec_dynrel_count (rel->r_info, sec, info,
7348 /* We need to adjust any reloc offsets to point to the
7349 new opd entries. While we're at it, we may as well
7350 remove redundant relocs. */
7351 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
7352 if (write_rel != rel)
7353 memcpy (write_rel, rel, sizeof (*rel));
7358 sec->size = wptr - new_contents;
7359 sec->reloc_count = write_rel - relstart;
7362 free (sec->contents);
7363 sec->contents = new_contents;
7366 /* Fudge the header size too, as this is used later in
7367 elf_bfd_final_link if we are emitting relocs. */
7368 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7369 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7372 else if (elf_section_data (sec)->relocs != relstart)
7375 if (local_syms != NULL
7376 && symtab_hdr->contents != (unsigned char *) local_syms)
7378 if (!info->keep_memory)
7381 symtab_hdr->contents = (unsigned char *) local_syms;
7386 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7388 /* If we are doing a final link and the last .opd entry is just 16 byte
7389 long, add a 8 byte padding after it. */
7390 if (need_pad != NULL && !info->relocatable)
7394 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7396 BFD_ASSERT (need_pad->size > 0);
7398 p = bfd_malloc (need_pad->size + 8);
7402 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7403 p, 0, need_pad->size))
7406 need_pad->contents = p;
7407 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7411 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7415 need_pad->contents = p;
7418 memset (need_pad->contents + need_pad->size, 0, 8);
7419 need_pad->size += 8;
7425 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7428 ppc64_elf_tls_setup (struct bfd_link_info *info,
7429 int no_tls_get_addr_opt,
7432 struct ppc_link_hash_table *htab;
7434 htab = ppc_hash_table (info);
7439 htab->do_multi_toc = 0;
7440 else if (!htab->do_multi_toc)
7443 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7444 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7445 FALSE, FALSE, TRUE));
7446 /* Move dynamic linking info to the function descriptor sym. */
7447 if (htab->tls_get_addr != NULL)
7448 func_desc_adjust (&htab->tls_get_addr->elf, info);
7449 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7450 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7451 FALSE, FALSE, TRUE));
7452 if (!no_tls_get_addr_opt)
7454 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7456 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7457 FALSE, FALSE, TRUE);
7459 func_desc_adjust (opt, info);
7460 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7461 FALSE, FALSE, TRUE);
7463 && (opt_fd->root.type == bfd_link_hash_defined
7464 || opt_fd->root.type == bfd_link_hash_defweak))
7466 /* If glibc supports an optimized __tls_get_addr call stub,
7467 signalled by the presence of __tls_get_addr_opt, and we'll
7468 be calling __tls_get_addr via a plt call stub, then
7469 make __tls_get_addr point to __tls_get_addr_opt. */
7470 tga_fd = &htab->tls_get_addr_fd->elf;
7471 if (htab->elf.dynamic_sections_created
7473 && (tga_fd->type == STT_FUNC
7474 || tga_fd->needs_plt)
7475 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7476 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7477 && tga_fd->root.type == bfd_link_hash_undefweak)))
7479 struct plt_entry *ent;
7481 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7482 if (ent->plt.refcount > 0)
7486 tga_fd->root.type = bfd_link_hash_indirect;
7487 tga_fd->root.u.i.link = &opt_fd->root;
7488 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7489 if (opt_fd->dynindx != -1)
7491 /* Use __tls_get_addr_opt in dynamic relocations. */
7492 opt_fd->dynindx = -1;
7493 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7494 opt_fd->dynstr_index);
7495 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7498 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7499 tga = &htab->tls_get_addr->elf;
7500 if (opt != NULL && tga != NULL)
7502 tga->root.type = bfd_link_hash_indirect;
7503 tga->root.u.i.link = &opt->root;
7504 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7505 _bfd_elf_link_hash_hide_symbol (info, opt,
7507 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7509 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7510 htab->tls_get_addr_fd->is_func_descriptor = 1;
7511 if (htab->tls_get_addr != NULL)
7513 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7514 htab->tls_get_addr->is_func = 1;
7520 no_tls_get_addr_opt = TRUE;
7522 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
7523 return _bfd_elf_tls_setup (info->output_bfd, info);
7526 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7530 branch_reloc_hash_match (const bfd *ibfd,
7531 const Elf_Internal_Rela *rel,
7532 const struct ppc_link_hash_entry *hash1,
7533 const struct ppc_link_hash_entry *hash2)
7535 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7536 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7537 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7539 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7541 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7542 struct elf_link_hash_entry *h;
7544 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7545 h = elf_follow_link (h);
7546 if (h == &hash1->elf || h == &hash2->elf)
7552 /* Run through all the TLS relocs looking for optimization
7553 opportunities. The linker has been hacked (see ppc64elf.em) to do
7554 a preliminary section layout so that we know the TLS segment
7555 offsets. We can't optimize earlier because some optimizations need
7556 to know the tp offset, and we need to optimize before allocating
7557 dynamic relocations. */
7560 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7564 struct ppc_link_hash_table *htab;
7565 unsigned char *toc_ref;
7568 if (info->relocatable || !info->executable)
7571 htab = ppc_hash_table (info);
7575 /* Make two passes over the relocs. On the first pass, mark toc
7576 entries involved with tls relocs, and check that tls relocs
7577 involved in setting up a tls_get_addr call are indeed followed by
7578 such a call. If they are not, we can't do any tls optimization.
7579 On the second pass twiddle tls_mask flags to notify
7580 relocate_section that optimization can be done, and adjust got
7581 and plt refcounts. */
7583 for (pass = 0; pass < 2; ++pass)
7584 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7586 Elf_Internal_Sym *locsyms = NULL;
7587 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7589 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7590 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7592 Elf_Internal_Rela *relstart, *rel, *relend;
7593 bfd_boolean found_tls_get_addr_arg = 0;
7595 /* Read the relocations. */
7596 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7598 if (relstart == NULL)
7601 relend = relstart + sec->reloc_count;
7602 for (rel = relstart; rel < relend; rel++)
7604 enum elf_ppc64_reloc_type r_type;
7605 unsigned long r_symndx;
7606 struct elf_link_hash_entry *h;
7607 Elf_Internal_Sym *sym;
7609 unsigned char *tls_mask;
7610 unsigned char tls_set, tls_clear, tls_type = 0;
7612 bfd_boolean ok_tprel, is_local;
7613 long toc_ref_index = 0;
7614 int expecting_tls_get_addr = 0;
7615 bfd_boolean ret = FALSE;
7617 r_symndx = ELF64_R_SYM (rel->r_info);
7618 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7622 if (elf_section_data (sec)->relocs != relstart)
7624 if (toc_ref != NULL)
7627 && (elf_symtab_hdr (ibfd).contents
7628 != (unsigned char *) locsyms))
7635 if (h->root.type == bfd_link_hash_defined
7636 || h->root.type == bfd_link_hash_defweak)
7637 value = h->root.u.def.value;
7638 else if (h->root.type == bfd_link_hash_undefweak)
7642 found_tls_get_addr_arg = 0;
7647 /* Symbols referenced by TLS relocs must be of type
7648 STT_TLS. So no need for .opd local sym adjust. */
7649 value = sym->st_value;
7658 && h->root.type == bfd_link_hash_undefweak)
7662 value += sym_sec->output_offset;
7663 value += sym_sec->output_section->vma;
7664 value -= htab->elf.tls_sec->vma;
7665 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7666 < (bfd_vma) 1 << 32);
7670 r_type = ELF64_R_TYPE (rel->r_info);
7671 /* If this section has old-style __tls_get_addr calls
7672 without marker relocs, then check that each
7673 __tls_get_addr call reloc is preceded by a reloc
7674 that conceivably belongs to the __tls_get_addr arg
7675 setup insn. If we don't find matching arg setup
7676 relocs, don't do any tls optimization. */
7678 && sec->has_tls_get_addr_call
7680 && (h == &htab->tls_get_addr->elf
7681 || h == &htab->tls_get_addr_fd->elf)
7682 && !found_tls_get_addr_arg
7683 && is_branch_reloc (r_type))
7685 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7686 "TLS optimization disabled\n"),
7687 ibfd, sec, rel->r_offset);
7692 found_tls_get_addr_arg = 0;
7695 case R_PPC64_GOT_TLSLD16:
7696 case R_PPC64_GOT_TLSLD16_LO:
7697 expecting_tls_get_addr = 1;
7698 found_tls_get_addr_arg = 1;
7701 case R_PPC64_GOT_TLSLD16_HI:
7702 case R_PPC64_GOT_TLSLD16_HA:
7703 /* These relocs should never be against a symbol
7704 defined in a shared lib. Leave them alone if
7705 that turns out to be the case. */
7712 tls_type = TLS_TLS | TLS_LD;
7715 case R_PPC64_GOT_TLSGD16:
7716 case R_PPC64_GOT_TLSGD16_LO:
7717 expecting_tls_get_addr = 1;
7718 found_tls_get_addr_arg = 1;
7721 case R_PPC64_GOT_TLSGD16_HI:
7722 case R_PPC64_GOT_TLSGD16_HA:
7728 tls_set = TLS_TLS | TLS_TPRELGD;
7730 tls_type = TLS_TLS | TLS_GD;
7733 case R_PPC64_GOT_TPREL16_DS:
7734 case R_PPC64_GOT_TPREL16_LO_DS:
7735 case R_PPC64_GOT_TPREL16_HI:
7736 case R_PPC64_GOT_TPREL16_HA:
7741 tls_clear = TLS_TPREL;
7742 tls_type = TLS_TLS | TLS_TPREL;
7749 found_tls_get_addr_arg = 1;
7754 case R_PPC64_TOC16_LO:
7755 if (sym_sec == NULL || sym_sec != toc)
7758 /* Mark this toc entry as referenced by a TLS
7759 code sequence. We can do that now in the
7760 case of R_PPC64_TLS, and after checking for
7761 tls_get_addr for the TOC16 relocs. */
7762 if (toc_ref == NULL)
7763 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
7764 if (toc_ref == NULL)
7768 value = h->root.u.def.value;
7770 value = sym->st_value;
7771 value += rel->r_addend;
7772 BFD_ASSERT (value < toc->size && value % 8 == 0);
7773 toc_ref_index = (value + toc->output_offset) / 8;
7774 if (r_type == R_PPC64_TLS
7775 || r_type == R_PPC64_TLSGD
7776 || r_type == R_PPC64_TLSLD)
7778 toc_ref[toc_ref_index] = 1;
7782 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7787 expecting_tls_get_addr = 2;
7790 case R_PPC64_TPREL64:
7794 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7799 tls_set = TLS_EXPLICIT;
7800 tls_clear = TLS_TPREL;
7805 case R_PPC64_DTPMOD64:
7809 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7811 if (rel + 1 < relend
7813 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7814 && rel[1].r_offset == rel->r_offset + 8)
7818 tls_set = TLS_EXPLICIT | TLS_GD;
7821 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7830 tls_set = TLS_EXPLICIT;
7841 if (!expecting_tls_get_addr
7842 || !sec->has_tls_get_addr_call)
7845 if (rel + 1 < relend
7846 && branch_reloc_hash_match (ibfd, rel + 1,
7848 htab->tls_get_addr_fd))
7850 if (expecting_tls_get_addr == 2)
7852 /* Check for toc tls entries. */
7853 unsigned char *toc_tls;
7856 retval = get_tls_mask (&toc_tls, NULL, NULL,
7861 if (toc_tls != NULL)
7863 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
7864 found_tls_get_addr_arg = 1;
7866 toc_ref[toc_ref_index] = 1;
7872 if (expecting_tls_get_addr != 1)
7875 /* Uh oh, we didn't find the expected call. We
7876 could just mark this symbol to exclude it
7877 from tls optimization but it's safer to skip
7878 the entire optimization. */
7879 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
7880 "TLS optimization disabled\n"),
7881 ibfd, sec, rel->r_offset);
7886 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
7888 struct plt_entry *ent;
7889 for (ent = htab->tls_get_addr->elf.plt.plist;
7892 if (ent->addend == 0)
7894 if (ent->plt.refcount > 0)
7896 ent->plt.refcount -= 1;
7897 expecting_tls_get_addr = 0;
7903 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
7905 struct plt_entry *ent;
7906 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7909 if (ent->addend == 0)
7911 if (ent->plt.refcount > 0)
7912 ent->plt.refcount -= 1;
7920 if ((tls_set & TLS_EXPLICIT) == 0)
7922 struct got_entry *ent;
7924 /* Adjust got entry for this reloc. */
7928 ent = elf_local_got_ents (ibfd)[r_symndx];
7930 for (; ent != NULL; ent = ent->next)
7931 if (ent->addend == rel->r_addend
7932 && ent->owner == ibfd
7933 && ent->tls_type == tls_type)
7940 /* We managed to get rid of a got entry. */
7941 if (ent->got.refcount > 0)
7942 ent->got.refcount -= 1;
7947 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7948 we'll lose one or two dyn relocs. */
7949 if (!dec_dynrel_count (rel->r_info, sec, info,
7953 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7955 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7961 *tls_mask |= tls_set;
7962 *tls_mask &= ~tls_clear;
7965 if (elf_section_data (sec)->relocs != relstart)
7970 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7972 if (!info->keep_memory)
7975 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7979 if (toc_ref != NULL)
7984 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7985 the values of any global symbols in a toc section that has been
7986 edited. Globals in toc sections should be a rarity, so this function
7987 sets a flag if any are found in toc sections other than the one just
7988 edited, so that futher hash table traversals can be avoided. */
7990 struct adjust_toc_info
7993 unsigned long *skip;
7994 bfd_boolean global_toc_syms;
7997 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8000 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8002 struct ppc_link_hash_entry *eh;
8003 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8006 if (h->root.type != bfd_link_hash_defined
8007 && h->root.type != bfd_link_hash_defweak)
8010 eh = (struct ppc_link_hash_entry *) h;
8011 if (eh->adjust_done)
8014 if (eh->elf.root.u.def.section == toc_inf->toc)
8016 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8017 i = toc_inf->toc->rawsize >> 3;
8019 i = eh->elf.root.u.def.value >> 3;
8021 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8023 (*_bfd_error_handler)
8024 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8027 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8028 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8031 eh->elf.root.u.def.value -= toc_inf->skip[i];
8032 eh->adjust_done = 1;
8034 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8035 toc_inf->global_toc_syms = TRUE;
8040 /* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc. */
8043 ok_lo_toc_insn (unsigned int insn)
8045 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8046 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8047 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8048 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8049 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8050 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8051 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8052 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8053 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8054 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8055 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8056 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8057 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8058 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8059 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8061 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8062 && ((insn & 3) == 0 || (insn & 3) == 3))
8063 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8066 /* Examine all relocs referencing .toc sections in order to remove
8067 unused .toc entries. */
8070 ppc64_elf_edit_toc (struct bfd_link_info *info)
8073 struct adjust_toc_info toc_inf;
8074 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8076 htab->do_toc_opt = 1;
8077 toc_inf.global_toc_syms = TRUE;
8078 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8080 asection *toc, *sec;
8081 Elf_Internal_Shdr *symtab_hdr;
8082 Elf_Internal_Sym *local_syms;
8083 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8084 unsigned long *skip, *drop;
8085 unsigned char *used;
8086 unsigned char *keep, last, some_unused;
8088 if (!is_ppc64_elf (ibfd))
8091 toc = bfd_get_section_by_name (ibfd, ".toc");
8094 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
8095 || elf_discarded_section (toc))
8100 symtab_hdr = &elf_symtab_hdr (ibfd);
8102 /* Look at sections dropped from the final link. */
8105 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8107 if (sec->reloc_count == 0
8108 || !elf_discarded_section (sec)
8109 || get_opd_info (sec)
8110 || (sec->flags & SEC_ALLOC) == 0
8111 || (sec->flags & SEC_DEBUGGING) != 0)
8114 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8115 if (relstart == NULL)
8118 /* Run through the relocs to see which toc entries might be
8120 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8122 enum elf_ppc64_reloc_type r_type;
8123 unsigned long r_symndx;
8125 struct elf_link_hash_entry *h;
8126 Elf_Internal_Sym *sym;
8129 r_type = ELF64_R_TYPE (rel->r_info);
8136 case R_PPC64_TOC16_LO:
8137 case R_PPC64_TOC16_HI:
8138 case R_PPC64_TOC16_HA:
8139 case R_PPC64_TOC16_DS:
8140 case R_PPC64_TOC16_LO_DS:
8144 r_symndx = ELF64_R_SYM (rel->r_info);
8145 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8153 val = h->root.u.def.value;
8155 val = sym->st_value;
8156 val += rel->r_addend;
8158 if (val >= toc->size)
8161 /* Anything in the toc ought to be aligned to 8 bytes.
8162 If not, don't mark as unused. */
8168 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8173 skip[val >> 3] = ref_from_discarded;
8176 if (elf_section_data (sec)->relocs != relstart)
8180 /* For largetoc loads of address constants, we can convert
8181 . addis rx,2,addr@got@ha
8182 . ld ry,addr@got@l(rx)
8184 . addis rx,2,addr@toc@ha
8185 . addi ry,rx,addr@toc@l
8186 when addr is within 2G of the toc pointer. This then means
8187 that the word storing "addr" in the toc is no longer needed. */
8189 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8190 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8191 && toc->reloc_count != 0)
8193 /* Read toc relocs. */
8194 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8196 if (toc_relocs == NULL)
8199 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8201 enum elf_ppc64_reloc_type r_type;
8202 unsigned long r_symndx;
8204 struct elf_link_hash_entry *h;
8205 Elf_Internal_Sym *sym;
8208 r_type = ELF64_R_TYPE (rel->r_info);
8209 if (r_type != R_PPC64_ADDR64)
8212 r_symndx = ELF64_R_SYM (rel->r_info);
8213 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8218 || elf_discarded_section (sym_sec))
8221 if (!SYMBOL_CALLS_LOCAL (info, h))
8226 if (h->type == STT_GNU_IFUNC)
8228 val = h->root.u.def.value;
8232 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8234 val = sym->st_value;
8236 val += rel->r_addend;
8237 val += sym_sec->output_section->vma + sym_sec->output_offset;
8239 /* We don't yet know the exact toc pointer value, but we
8240 know it will be somewhere in the toc section. Don't
8241 optimize if the difference from any possible toc
8242 pointer is outside [ff..f80008000, 7fff7fff]. */
8243 addr = toc->output_section->vma + TOC_BASE_OFF;
8244 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8247 addr = toc->output_section->vma + toc->output_section->rawsize;
8248 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8253 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8258 skip[rel->r_offset >> 3]
8259 |= can_optimize | ((rel - toc_relocs) << 2);
8266 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8270 if (local_syms != NULL
8271 && symtab_hdr->contents != (unsigned char *) local_syms)
8275 && elf_section_data (sec)->relocs != relstart)
8277 if (toc_relocs != NULL
8278 && elf_section_data (toc)->relocs != toc_relocs)
8285 /* Now check all kept sections that might reference the toc.
8286 Check the toc itself last. */
8287 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8290 sec = (sec == toc ? NULL
8291 : sec->next == NULL ? toc
8292 : sec->next == toc && toc->next ? toc->next
8297 if (sec->reloc_count == 0
8298 || elf_discarded_section (sec)
8299 || get_opd_info (sec)
8300 || (sec->flags & SEC_ALLOC) == 0
8301 || (sec->flags & SEC_DEBUGGING) != 0)
8304 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8306 if (relstart == NULL)
8309 /* Mark toc entries referenced as used. */
8312 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8314 enum elf_ppc64_reloc_type r_type;
8315 unsigned long r_symndx;
8317 struct elf_link_hash_entry *h;
8318 Elf_Internal_Sym *sym;
8320 enum {no_check, check_lo, check_ha} insn_check;
8322 r_type = ELF64_R_TYPE (rel->r_info);
8326 insn_check = no_check;
8329 case R_PPC64_GOT_TLSLD16_HA:
8330 case R_PPC64_GOT_TLSGD16_HA:
8331 case R_PPC64_GOT_TPREL16_HA:
8332 case R_PPC64_GOT_DTPREL16_HA:
8333 case R_PPC64_GOT16_HA:
8334 case R_PPC64_TOC16_HA:
8335 insn_check = check_ha;
8338 case R_PPC64_GOT_TLSLD16_LO:
8339 case R_PPC64_GOT_TLSGD16_LO:
8340 case R_PPC64_GOT_TPREL16_LO_DS:
8341 case R_PPC64_GOT_DTPREL16_LO_DS:
8342 case R_PPC64_GOT16_LO:
8343 case R_PPC64_GOT16_LO_DS:
8344 case R_PPC64_TOC16_LO:
8345 case R_PPC64_TOC16_LO_DS:
8346 insn_check = check_lo;
8350 if (insn_check != no_check)
8352 bfd_vma off = rel->r_offset & ~3;
8353 unsigned char buf[4];
8356 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8361 insn = bfd_get_32 (ibfd, buf);
8362 if (insn_check == check_lo
8363 ? !ok_lo_toc_insn (insn)
8364 : ((insn & ((0x3f << 26) | 0x1f << 16))
8365 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8369 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8370 sprintf (str, "%#08x", insn);
8371 info->callbacks->einfo
8372 (_("%P: %H: toc optimization is not supported for"
8373 " %s instruction.\n"),
8374 ibfd, sec, rel->r_offset & ~3, str);
8381 case R_PPC64_TOC16_LO:
8382 case R_PPC64_TOC16_HI:
8383 case R_PPC64_TOC16_HA:
8384 case R_PPC64_TOC16_DS:
8385 case R_PPC64_TOC16_LO_DS:
8386 /* In case we're taking addresses of toc entries. */
8387 case R_PPC64_ADDR64:
8394 r_symndx = ELF64_R_SYM (rel->r_info);
8395 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8406 val = h->root.u.def.value;
8408 val = sym->st_value;
8409 val += rel->r_addend;
8411 if (val >= toc->size)
8414 if ((skip[val >> 3] & can_optimize) != 0)
8421 case R_PPC64_TOC16_HA:
8424 case R_PPC64_TOC16_LO_DS:
8425 off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
8426 if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
8431 if ((opc & (0x3f << 2)) == (58u << 2))
8436 /* Wrong sort of reloc, or not a ld. We may
8437 as well clear ref_from_discarded too. */
8442 /* For the toc section, we only mark as used if
8443 this entry itself isn't unused. */
8446 && (used[rel->r_offset >> 3]
8447 || !(skip[rel->r_offset >> 3] & ref_from_discarded)))
8448 /* Do all the relocs again, to catch reference
8456 if (elf_section_data (sec)->relocs != relstart)
8460 /* Merge the used and skip arrays. Assume that TOC
8461 doublewords not appearing as either used or unused belong
8462 to to an entry more than one doubleword in size. */
8463 for (drop = skip, keep = used, last = 0, some_unused = 0;
8464 drop < skip + (toc->size + 7) / 8;
8469 *drop &= ~ref_from_discarded;
8470 if ((*drop & can_optimize) != 0)
8474 else if ((*drop & ref_from_discarded) != 0)
8477 last = ref_from_discarded;
8487 bfd_byte *contents, *src;
8489 Elf_Internal_Sym *sym;
8490 bfd_boolean local_toc_syms = FALSE;
8492 /* Shuffle the toc contents, and at the same time convert the
8493 skip array from booleans into offsets. */
8494 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8497 elf_section_data (toc)->this_hdr.contents = contents;
8499 for (src = contents, off = 0, drop = skip;
8500 src < contents + toc->size;
8503 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8508 memcpy (src - off, src, 8);
8512 toc->rawsize = toc->size;
8513 toc->size = src - contents - off;
8515 /* Adjust addends for relocs against the toc section sym,
8516 and optimize any accesses we can. */
8517 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8519 if (sec->reloc_count == 0
8520 || elf_discarded_section (sec))
8523 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8525 if (relstart == NULL)
8528 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8530 enum elf_ppc64_reloc_type r_type;
8531 unsigned long r_symndx;
8533 struct elf_link_hash_entry *h;
8536 r_type = ELF64_R_TYPE (rel->r_info);
8543 case R_PPC64_TOC16_LO:
8544 case R_PPC64_TOC16_HI:
8545 case R_PPC64_TOC16_HA:
8546 case R_PPC64_TOC16_DS:
8547 case R_PPC64_TOC16_LO_DS:
8548 case R_PPC64_ADDR64:
8552 r_symndx = ELF64_R_SYM (rel->r_info);
8553 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8561 val = h->root.u.def.value;
8564 val = sym->st_value;
8566 local_toc_syms = TRUE;
8569 val += rel->r_addend;
8571 if (val > toc->rawsize)
8573 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8575 else if ((skip[val >> 3] & can_optimize) != 0)
8577 Elf_Internal_Rela *tocrel
8578 = toc_relocs + (skip[val >> 3] >> 2);
8579 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8583 case R_PPC64_TOC16_HA:
8584 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8587 case R_PPC64_TOC16_LO_DS:
8588 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8592 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8594 info->callbacks->einfo
8595 (_("%P: %H: %s relocation references "
8596 "optimized away TOC entry\n"),
8597 ibfd, sec, rel->r_offset,
8598 ppc64_elf_howto_table[r_type]->name);
8599 bfd_set_error (bfd_error_bad_value);
8602 rel->r_addend = tocrel->r_addend;
8603 elf_section_data (sec)->relocs = relstart;
8607 if (h != NULL || sym->st_value != 0)
8610 rel->r_addend -= skip[val >> 3];
8611 elf_section_data (sec)->relocs = relstart;
8614 if (elf_section_data (sec)->relocs != relstart)
8618 /* We shouldn't have local or global symbols defined in the TOC,
8619 but handle them anyway. */
8620 if (local_syms != NULL)
8621 for (sym = local_syms;
8622 sym < local_syms + symtab_hdr->sh_info;
8624 if (sym->st_value != 0
8625 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8629 if (sym->st_value > toc->rawsize)
8630 i = toc->rawsize >> 3;
8632 i = sym->st_value >> 3;
8634 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8637 (*_bfd_error_handler)
8638 (_("%s defined on removed toc entry"),
8639 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8642 while ((skip[i] & (ref_from_discarded | can_optimize)));
8643 sym->st_value = (bfd_vma) i << 3;
8646 sym->st_value -= skip[i];
8647 symtab_hdr->contents = (unsigned char *) local_syms;
8650 /* Adjust any global syms defined in this toc input section. */
8651 if (toc_inf.global_toc_syms)
8654 toc_inf.skip = skip;
8655 toc_inf.global_toc_syms = FALSE;
8656 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8660 if (toc->reloc_count != 0)
8662 Elf_Internal_Shdr *rel_hdr;
8663 Elf_Internal_Rela *wrel;
8666 /* Remove unused toc relocs, and adjust those we keep. */
8667 if (toc_relocs == NULL)
8668 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8670 if (toc_relocs == NULL)
8674 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8675 if ((skip[rel->r_offset >> 3]
8676 & (ref_from_discarded | can_optimize)) == 0)
8678 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8679 wrel->r_info = rel->r_info;
8680 wrel->r_addend = rel->r_addend;
8683 else if (!dec_dynrel_count (rel->r_info, toc, info,
8684 &local_syms, NULL, NULL))
8687 elf_section_data (toc)->relocs = toc_relocs;
8688 toc->reloc_count = wrel - toc_relocs;
8689 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8690 sz = rel_hdr->sh_entsize;
8691 rel_hdr->sh_size = toc->reloc_count * sz;
8694 else if (toc_relocs != NULL
8695 && elf_section_data (toc)->relocs != toc_relocs)
8698 if (local_syms != NULL
8699 && symtab_hdr->contents != (unsigned char *) local_syms)
8701 if (!info->keep_memory)
8704 symtab_hdr->contents = (unsigned char *) local_syms;
8712 /* Return true iff input section I references the TOC using
8713 instructions limited to +/-32k offsets. */
8716 ppc64_elf_has_small_toc_reloc (asection *i)
8718 return (is_ppc64_elf (i->owner)
8719 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8722 /* Allocate space for one GOT entry. */
8725 allocate_got (struct elf_link_hash_entry *h,
8726 struct bfd_link_info *info,
8727 struct got_entry *gent)
8729 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8731 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8732 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8734 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8735 ? 2 : 1) * sizeof (Elf64_External_Rela);
8736 asection *got = ppc64_elf_tdata (gent->owner)->got;
8738 gent->got.offset = got->size;
8739 got->size += entsize;
8741 dyn = htab->elf.dynamic_sections_created;
8743 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8744 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8745 || h->root.type != bfd_link_hash_undefweak))
8747 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8748 relgot->size += rentsize;
8750 else if (h->type == STT_GNU_IFUNC)
8752 asection *relgot = htab->reliplt;
8753 relgot->size += rentsize;
8754 htab->got_reli_size += rentsize;
8758 /* This function merges got entries in the same toc group. */
8761 merge_got_entries (struct got_entry **pent)
8763 struct got_entry *ent, *ent2;
8765 for (ent = *pent; ent != NULL; ent = ent->next)
8766 if (!ent->is_indirect)
8767 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8768 if (!ent2->is_indirect
8769 && ent2->addend == ent->addend
8770 && ent2->tls_type == ent->tls_type
8771 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8773 ent2->is_indirect = TRUE;
8774 ent2->got.ent = ent;
8778 /* Allocate space in .plt, .got and associated reloc sections for
8782 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8784 struct bfd_link_info *info;
8785 struct ppc_link_hash_table *htab;
8787 struct ppc_link_hash_entry *eh;
8788 struct elf_dyn_relocs *p;
8789 struct got_entry **pgent, *gent;
8791 if (h->root.type == bfd_link_hash_indirect)
8794 info = (struct bfd_link_info *) inf;
8795 htab = ppc_hash_table (info);
8799 if ((htab->elf.dynamic_sections_created
8801 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
8802 || h->type == STT_GNU_IFUNC)
8804 struct plt_entry *pent;
8805 bfd_boolean doneone = FALSE;
8806 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8807 if (pent->plt.refcount > 0)
8809 if (!htab->elf.dynamic_sections_created
8810 || h->dynindx == -1)
8813 pent->plt.offset = s->size;
8814 s->size += PLT_ENTRY_SIZE;
8819 /* If this is the first .plt entry, make room for the special
8823 s->size += PLT_INITIAL_ENTRY_SIZE;
8825 pent->plt.offset = s->size;
8827 /* Make room for this entry. */
8828 s->size += PLT_ENTRY_SIZE;
8830 /* Make room for the .glink code. */
8833 s->size += GLINK_CALL_STUB_SIZE;
8834 /* We need bigger stubs past index 32767. */
8835 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
8839 /* We also need to make an entry in the .rela.plt section. */
8842 s->size += sizeof (Elf64_External_Rela);
8846 pent->plt.offset = (bfd_vma) -1;
8849 h->plt.plist = NULL;
8855 h->plt.plist = NULL;
8859 eh = (struct ppc_link_hash_entry *) h;
8860 /* Run through the TLS GD got entries first if we're changing them
8862 if ((eh->tls_mask & TLS_TPRELGD) != 0)
8863 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8864 if (gent->got.refcount > 0
8865 && (gent->tls_type & TLS_GD) != 0)
8867 /* This was a GD entry that has been converted to TPREL. If
8868 there happens to be a TPREL entry we can use that one. */
8869 struct got_entry *ent;
8870 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8871 if (ent->got.refcount > 0
8872 && (ent->tls_type & TLS_TPREL) != 0
8873 && ent->addend == gent->addend
8874 && ent->owner == gent->owner)
8876 gent->got.refcount = 0;
8880 /* If not, then we'll be using our own TPREL entry. */
8881 if (gent->got.refcount != 0)
8882 gent->tls_type = TLS_TLS | TLS_TPREL;
8885 /* Remove any list entry that won't generate a word in the GOT before
8886 we call merge_got_entries. Otherwise we risk merging to empty
8888 pgent = &h->got.glist;
8889 while ((gent = *pgent) != NULL)
8890 if (gent->got.refcount > 0)
8892 if ((gent->tls_type & TLS_LD) != 0
8895 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8896 *pgent = gent->next;
8899 pgent = &gent->next;
8902 *pgent = gent->next;
8904 if (!htab->do_multi_toc)
8905 merge_got_entries (&h->got.glist);
8907 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8908 if (!gent->is_indirect)
8910 /* Make sure this symbol is output as a dynamic symbol.
8911 Undefined weak syms won't yet be marked as dynamic,
8912 nor will all TLS symbols. */
8913 if (h->dynindx == -1
8915 && h->type != STT_GNU_IFUNC
8916 && htab->elf.dynamic_sections_created)
8918 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8922 if (!is_ppc64_elf (gent->owner))
8925 allocate_got (h, info, gent);
8928 if (eh->dyn_relocs == NULL
8929 || (!htab->elf.dynamic_sections_created
8930 && h->type != STT_GNU_IFUNC))
8933 /* In the shared -Bsymbolic case, discard space allocated for
8934 dynamic pc-relative relocs against symbols which turn out to be
8935 defined in regular objects. For the normal shared case, discard
8936 space for relocs that have become local due to symbol visibility
8941 /* Relocs that use pc_count are those that appear on a call insn,
8942 or certain REL relocs (see must_be_dyn_reloc) that can be
8943 generated via assembly. We want calls to protected symbols to
8944 resolve directly to the function rather than going via the plt.
8945 If people want function pointer comparisons to work as expected
8946 then they should avoid writing weird assembly. */
8947 if (SYMBOL_CALLS_LOCAL (info, h))
8949 struct elf_dyn_relocs **pp;
8951 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
8953 p->count -= p->pc_count;
8962 /* Also discard relocs on undefined weak syms with non-default
8964 if (eh->dyn_relocs != NULL
8965 && h->root.type == bfd_link_hash_undefweak)
8967 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8968 eh->dyn_relocs = NULL;
8970 /* Make sure this symbol is output as a dynamic symbol.
8971 Undefined weak syms won't yet be marked as dynamic. */
8972 else if (h->dynindx == -1
8973 && !h->forced_local)
8975 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8980 else if (h->type == STT_GNU_IFUNC)
8982 if (!h->non_got_ref)
8983 eh->dyn_relocs = NULL;
8985 else if (ELIMINATE_COPY_RELOCS)
8987 /* For the non-shared case, discard space for relocs against
8988 symbols which turn out to need copy relocs or are not
8994 /* Make sure this symbol is output as a dynamic symbol.
8995 Undefined weak syms won't yet be marked as dynamic. */
8996 if (h->dynindx == -1
8997 && !h->forced_local)
8999 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9003 /* If that succeeded, we know we'll be keeping all the
9005 if (h->dynindx != -1)
9009 eh->dyn_relocs = NULL;
9014 /* Finally, allocate space. */
9015 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9017 asection *sreloc = elf_section_data (p->sec)->sreloc;
9018 if (!htab->elf.dynamic_sections_created)
9019 sreloc = htab->reliplt;
9020 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9026 /* Find any dynamic relocs that apply to read-only sections. */
9029 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9031 struct ppc_link_hash_entry *eh;
9032 struct elf_dyn_relocs *p;
9034 eh = (struct ppc_link_hash_entry *) h;
9035 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9037 asection *s = p->sec->output_section;
9039 if (s != NULL && (s->flags & SEC_READONLY) != 0)
9041 struct bfd_link_info *info = inf;
9043 info->flags |= DF_TEXTREL;
9045 /* Not an error, just cut short the traversal. */
9052 /* Set the sizes of the dynamic sections. */
9055 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
9056 struct bfd_link_info *info)
9058 struct ppc_link_hash_table *htab;
9063 struct got_entry *first_tlsld;
9065 htab = ppc_hash_table (info);
9069 dynobj = htab->elf.dynobj;
9073 if (htab->elf.dynamic_sections_created)
9075 /* Set the contents of the .interp section to the interpreter. */
9076 if (info->executable)
9078 s = bfd_get_section_by_name (dynobj, ".interp");
9081 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9082 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9086 /* Set up .got offsets for local syms, and space for local dynamic
9088 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9090 struct got_entry **lgot_ents;
9091 struct got_entry **end_lgot_ents;
9092 struct plt_entry **local_plt;
9093 struct plt_entry **end_local_plt;
9094 unsigned char *lgot_masks;
9095 bfd_size_type locsymcount;
9096 Elf_Internal_Shdr *symtab_hdr;
9099 if (!is_ppc64_elf (ibfd))
9102 for (s = ibfd->sections; s != NULL; s = s->next)
9104 struct elf_dyn_relocs *p;
9106 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9108 if (!bfd_is_abs_section (p->sec)
9109 && bfd_is_abs_section (p->sec->output_section))
9111 /* Input section has been discarded, either because
9112 it is a copy of a linkonce section or due to
9113 linker script /DISCARD/, so we'll be discarding
9116 else if (p->count != 0)
9118 srel = elf_section_data (p->sec)->sreloc;
9119 if (!htab->elf.dynamic_sections_created)
9120 srel = htab->reliplt;
9121 srel->size += p->count * sizeof (Elf64_External_Rela);
9122 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9123 info->flags |= DF_TEXTREL;
9128 lgot_ents = elf_local_got_ents (ibfd);
9132 symtab_hdr = &elf_symtab_hdr (ibfd);
9133 locsymcount = symtab_hdr->sh_info;
9134 end_lgot_ents = lgot_ents + locsymcount;
9135 local_plt = (struct plt_entry **) end_lgot_ents;
9136 end_local_plt = local_plt + locsymcount;
9137 lgot_masks = (unsigned char *) end_local_plt;
9138 s = ppc64_elf_tdata (ibfd)->got;
9139 srel = ppc64_elf_tdata (ibfd)->relgot;
9140 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9142 struct got_entry **pent, *ent;
9145 while ((ent = *pent) != NULL)
9146 if (ent->got.refcount > 0)
9148 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9150 ppc64_tlsld_got (ibfd)->got.refcount += 1;
9155 unsigned int num = 1;
9156 ent->got.offset = s->size;
9157 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9161 srel->size += num * sizeof (Elf64_External_Rela);
9162 else if ((*lgot_masks & PLT_IFUNC) != 0)
9165 += num * sizeof (Elf64_External_Rela);
9167 += num * sizeof (Elf64_External_Rela);
9176 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
9177 for (; local_plt < end_local_plt; ++local_plt)
9179 struct plt_entry *ent;
9181 for (ent = *local_plt; ent != NULL; ent = ent->next)
9182 if (ent->plt.refcount > 0)
9185 ent->plt.offset = s->size;
9186 s->size += PLT_ENTRY_SIZE;
9188 htab->reliplt->size += sizeof (Elf64_External_Rela);
9191 ent->plt.offset = (bfd_vma) -1;
9195 /* Allocate global sym .plt and .got entries, and space for global
9196 sym dynamic relocs. */
9197 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9200 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9202 struct got_entry *ent;
9204 if (!is_ppc64_elf (ibfd))
9207 ent = ppc64_tlsld_got (ibfd);
9208 if (ent->got.refcount > 0)
9210 if (!htab->do_multi_toc && first_tlsld != NULL)
9212 ent->is_indirect = TRUE;
9213 ent->got.ent = first_tlsld;
9217 if (first_tlsld == NULL)
9219 s = ppc64_elf_tdata (ibfd)->got;
9220 ent->got.offset = s->size;
9225 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9226 srel->size += sizeof (Elf64_External_Rela);
9231 ent->got.offset = (bfd_vma) -1;
9234 /* We now have determined the sizes of the various dynamic sections.
9235 Allocate memory for them. */
9237 for (s = dynobj->sections; s != NULL; s = s->next)
9239 if ((s->flags & SEC_LINKER_CREATED) == 0)
9242 if (s == htab->brlt || s == htab->relbrlt)
9243 /* These haven't been allocated yet; don't strip. */
9245 else if (s == htab->got
9249 || s == htab->dynbss)
9251 /* Strip this section if we don't need it; see the
9254 else if (s == htab->glink_eh_frame)
9256 if (!bfd_is_abs_section (s->output_section))
9257 /* Not sized yet. */
9260 else if (CONST_STRNEQ (s->name, ".rela"))
9264 if (s != htab->relplt)
9267 /* We use the reloc_count field as a counter if we need
9268 to copy relocs into the output file. */
9274 /* It's not one of our sections, so don't allocate space. */
9280 /* If we don't need this section, strip it from the
9281 output file. This is mostly to handle .rela.bss and
9282 .rela.plt. We must create both sections in
9283 create_dynamic_sections, because they must be created
9284 before the linker maps input sections to output
9285 sections. The linker does that before
9286 adjust_dynamic_symbol is called, and it is that
9287 function which decides whether anything needs to go
9288 into these sections. */
9289 s->flags |= SEC_EXCLUDE;
9293 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9296 /* Allocate memory for the section contents. We use bfd_zalloc
9297 here in case unused entries are not reclaimed before the
9298 section's contents are written out. This should not happen,
9299 but this way if it does we get a R_PPC64_NONE reloc in .rela
9300 sections instead of garbage.
9301 We also rely on the section contents being zero when writing
9303 s->contents = bfd_zalloc (dynobj, s->size);
9304 if (s->contents == NULL)
9308 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9310 if (!is_ppc64_elf (ibfd))
9313 s = ppc64_elf_tdata (ibfd)->got;
9314 if (s != NULL && s != htab->got)
9317 s->flags |= SEC_EXCLUDE;
9320 s->contents = bfd_zalloc (ibfd, s->size);
9321 if (s->contents == NULL)
9325 s = ppc64_elf_tdata (ibfd)->relgot;
9329 s->flags |= SEC_EXCLUDE;
9332 s->contents = bfd_zalloc (ibfd, s->size);
9333 if (s->contents == NULL)
9341 if (htab->elf.dynamic_sections_created)
9343 /* Add some entries to the .dynamic section. We fill in the
9344 values later, in ppc64_elf_finish_dynamic_sections, but we
9345 must add the entries now so that we get the correct size for
9346 the .dynamic section. The DT_DEBUG entry is filled in by the
9347 dynamic linker and used by the debugger. */
9348 #define add_dynamic_entry(TAG, VAL) \
9349 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9351 if (info->executable)
9353 if (!add_dynamic_entry (DT_DEBUG, 0))
9357 if (htab->plt != NULL && htab->plt->size != 0)
9359 if (!add_dynamic_entry (DT_PLTGOT, 0)
9360 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9361 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9362 || !add_dynamic_entry (DT_JMPREL, 0)
9363 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9369 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9370 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9374 if (!htab->no_tls_get_addr_opt
9375 && htab->tls_get_addr_fd != NULL
9376 && htab->tls_get_addr_fd->elf.plt.plist != NULL
9377 && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9382 if (!add_dynamic_entry (DT_RELA, 0)
9383 || !add_dynamic_entry (DT_RELASZ, 0)
9384 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9387 /* If any dynamic relocs apply to a read-only section,
9388 then we need a DT_TEXTREL entry. */
9389 if ((info->flags & DF_TEXTREL) == 0)
9390 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
9392 if ((info->flags & DF_TEXTREL) != 0)
9394 if (!add_dynamic_entry (DT_TEXTREL, 0))
9399 #undef add_dynamic_entry
9404 /* Determine the type of stub needed, if any, for a call. */
9406 static inline enum ppc_stub_type
9407 ppc_type_of_stub (asection *input_sec,
9408 const Elf_Internal_Rela *rel,
9409 struct ppc_link_hash_entry **hash,
9410 struct plt_entry **plt_ent,
9411 bfd_vma destination)
9413 struct ppc_link_hash_entry *h = *hash;
9415 bfd_vma branch_offset;
9416 bfd_vma max_branch_offset;
9417 enum elf_ppc64_reloc_type r_type;
9421 struct plt_entry *ent;
9422 struct ppc_link_hash_entry *fdh = h;
9424 && h->oh->is_func_descriptor)
9426 fdh = ppc_follow_link (h->oh);
9430 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9431 if (ent->addend == rel->r_addend
9432 && ent->plt.offset != (bfd_vma) -1)
9435 return ppc_stub_plt_call;
9438 /* Here, we know we don't have a plt entry. If we don't have a
9439 either a defined function descriptor or a defined entry symbol
9440 in a regular object file, then it is pointless trying to make
9441 any other type of stub. */
9442 if (!is_static_defined (&fdh->elf)
9443 && !is_static_defined (&h->elf))
9444 return ppc_stub_none;
9446 else if (elf_local_got_ents (input_sec->owner) != NULL)
9448 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9449 struct plt_entry **local_plt = (struct plt_entry **)
9450 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9451 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9453 if (local_plt[r_symndx] != NULL)
9455 struct plt_entry *ent;
9457 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9458 if (ent->addend == rel->r_addend
9459 && ent->plt.offset != (bfd_vma) -1)
9462 return ppc_stub_plt_call;
9467 /* Determine where the call point is. */
9468 location = (input_sec->output_offset
9469 + input_sec->output_section->vma
9472 branch_offset = destination - location;
9473 r_type = ELF64_R_TYPE (rel->r_info);
9475 /* Determine if a long branch stub is needed. */
9476 max_branch_offset = 1 << 25;
9477 if (r_type != R_PPC64_REL24)
9478 max_branch_offset = 1 << 15;
9480 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
9481 /* We need a stub. Figure out whether a long_branch or plt_branch
9483 return ppc_stub_long_branch;
9485 return ppc_stub_none;
9488 /* With power7 weakly ordered memory model, it is possible for ld.so
9489 to update a plt entry in one thread and have another thread see a
9490 stale zero toc entry. To avoid this we need some sort of acquire
9491 barrier in the call stub. One solution is to make the load of the
9492 toc word seem to appear to depend on the load of the function entry
9493 word. Another solution is to test for r2 being zero, and branch to
9494 the appropriate glink entry if so.
9496 . fake dep barrier compare
9497 . ld 11,xxx(2) ld 11,xxx(2)
9499 . xor 11,11,11 ld 2,xxx+8(2)
9500 . add 2,2,11 cmpldi 2,0
9501 . ld 2,xxx+8(2) bnectr+
9502 . bctr b <glink_entry>
9504 The solution involving the compare turns out to be faster, so
9505 that's what we use unless the branch won't reach. */
9507 #define ALWAYS_USE_FAKE_DEP 0
9508 #define ALWAYS_EMIT_R2SAVE 0
9510 #define PPC_LO(v) ((v) & 0xffff)
9511 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9512 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9514 static inline unsigned int
9515 plt_stub_size (struct ppc_link_hash_table *htab,
9516 struct ppc_stub_hash_entry *stub_entry,
9519 unsigned size = PLT_CALL_STUB_SIZE;
9521 if (!(ALWAYS_EMIT_R2SAVE
9522 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
9524 if (!htab->plt_static_chain)
9526 if (htab->plt_thread_safe)
9528 if (PPC_HA (off) == 0)
9530 if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
9532 if (stub_entry->h != NULL
9533 && (stub_entry->h == htab->tls_get_addr_fd
9534 || stub_entry->h == htab->tls_get_addr)
9535 && !htab->no_tls_get_addr_opt)
9540 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
9541 then return the padding needed to do so. */
9542 static inline unsigned int
9543 plt_stub_pad (struct ppc_link_hash_table *htab,
9544 struct ppc_stub_hash_entry *stub_entry,
9547 int stub_align = 1 << htab->plt_stub_align;
9548 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
9549 bfd_vma stub_off = stub_entry->stub_sec->size;
9551 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
9552 > (stub_size & -stub_align))
9553 return stub_align - (stub_off & (stub_align - 1));
9557 /* Build a .plt call stub. */
9559 static inline bfd_byte *
9560 build_plt_stub (struct ppc_link_hash_table *htab,
9561 struct ppc_stub_hash_entry *stub_entry,
9562 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9564 bfd *obfd = htab->stub_bfd;
9565 bfd_boolean plt_static_chain = htab->plt_static_chain;
9566 bfd_boolean plt_thread_safe = htab->plt_thread_safe;
9567 bfd_boolean use_fake_dep = plt_thread_safe;
9568 bfd_vma cmp_branch_off = 0;
9570 if (!ALWAYS_USE_FAKE_DEP
9572 && !(stub_entry->h != NULL
9573 && (stub_entry->h == htab->tls_get_addr_fd
9574 || stub_entry->h == htab->tls_get_addr)
9575 && !htab->no_tls_get_addr_opt))
9577 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
9578 bfd_vma pltindex = (pltoff - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE;
9579 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
9582 if (pltindex > 32767)
9583 glinkoff += (pltindex - 32767) * 4;
9585 + htab->glink->output_offset
9586 + htab->glink->output_section->vma);
9587 from = (p - stub_entry->stub_sec->contents
9588 + 4 * (ALWAYS_EMIT_R2SAVE
9589 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9590 + 4 * (PPC_HA (offset) != 0)
9591 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
9593 + 4 * (plt_static_chain != 0)
9595 + stub_entry->stub_sec->output_offset
9596 + stub_entry->stub_sec->output_section->vma);
9597 cmp_branch_off = to - from;
9598 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
9601 if (PPC_HA (offset) != 0)
9605 if (ALWAYS_EMIT_R2SAVE
9606 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9608 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9609 r[1].r_offset = r[0].r_offset + 4;
9610 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9611 r[1].r_addend = r[0].r_addend;
9612 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9614 r[2].r_offset = r[1].r_offset + 4;
9615 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9616 r[2].r_addend = r[0].r_addend;
9620 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
9621 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9622 r[2].r_addend = r[0].r_addend + 8;
9623 if (plt_static_chain)
9625 r[3].r_offset = r[2].r_offset + 4;
9626 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9627 r[3].r_addend = r[0].r_addend + 16;
9631 if (ALWAYS_EMIT_R2SAVE
9632 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9633 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9634 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
9635 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
9636 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9638 bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p), p += 4;
9641 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9644 bfd_put_32 (obfd, XOR_R11_R11_R11, p), p += 4;
9645 bfd_put_32 (obfd, ADD_R12_R12_R11, p), p += 4;
9647 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p), p += 4;
9648 if (plt_static_chain)
9649 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
9655 if (ALWAYS_EMIT_R2SAVE
9656 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9658 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9659 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9661 r[1].r_offset = r[0].r_offset + 4;
9662 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9663 r[1].r_addend = r[0].r_addend;
9667 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
9668 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9669 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
9670 if (plt_static_chain)
9672 r[2].r_offset = r[1].r_offset + 4;
9673 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9674 r[2].r_addend = r[0].r_addend + 8;
9678 if (ALWAYS_EMIT_R2SAVE
9679 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9680 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9681 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p), p += 4;
9682 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9684 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
9687 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9690 bfd_put_32 (obfd, XOR_R11_R11_R11, p), p += 4;
9691 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
9693 if (plt_static_chain)
9694 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
9695 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
9697 if (plt_thread_safe && !use_fake_dep)
9699 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
9700 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
9701 bfd_put_32 (obfd, B_DOT + cmp_branch_off, p), p += 4;
9704 bfd_put_32 (obfd, BCTR, p), p += 4;
9708 /* Build a special .plt call stub for __tls_get_addr. */
9710 #define LD_R11_0R3 0xe9630000
9711 #define LD_R12_0R3 0xe9830000
9712 #define MR_R0_R3 0x7c601b78
9713 #define CMPDI_R11_0 0x2c2b0000
9714 #define ADD_R3_R12_R13 0x7c6c6a14
9715 #define BEQLR 0x4d820020
9716 #define MR_R3_R0 0x7c030378
9717 #define MFLR_R11 0x7d6802a6
9718 #define STD_R11_0R1 0xf9610000
9719 #define BCTRL 0x4e800421
9720 #define LD_R11_0R1 0xe9610000
9721 #define LD_R2_0R1 0xe8410000
9722 #define MTLR_R11 0x7d6803a6
9724 static inline bfd_byte *
9725 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
9726 struct ppc_stub_hash_entry *stub_entry,
9727 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9729 bfd *obfd = htab->stub_bfd;
9731 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
9732 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
9733 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
9734 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
9735 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
9736 bfd_put_32 (obfd, BEQLR, p), p += 4;
9737 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9738 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
9739 bfd_put_32 (obfd, STD_R11_0R1 + 32, p), p += 4;
9742 r[0].r_offset += 9 * 4;
9743 p = build_plt_stub (htab, stub_entry, p, offset, r);
9744 bfd_put_32 (obfd, BCTRL, p - 4);
9746 bfd_put_32 (obfd, LD_R11_0R1 + 32, p), p += 4;
9747 bfd_put_32 (obfd, LD_R2_0R1 + 40, p), p += 4;
9748 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
9749 bfd_put_32 (obfd, BLR, p), p += 4;
9754 static Elf_Internal_Rela *
9755 get_relocs (asection *sec, int count)
9757 Elf_Internal_Rela *relocs;
9758 struct bfd_elf_section_data *elfsec_data;
9760 elfsec_data = elf_section_data (sec);
9761 relocs = elfsec_data->relocs;
9764 bfd_size_type relsize;
9765 relsize = sec->reloc_count * sizeof (*relocs);
9766 relocs = bfd_alloc (sec->owner, relsize);
9769 elfsec_data->relocs = relocs;
9770 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9771 sizeof (Elf_Internal_Shdr));
9772 if (elfsec_data->rela.hdr == NULL)
9774 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9775 * sizeof (Elf64_External_Rela));
9776 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
9777 sec->reloc_count = 0;
9779 relocs += sec->reloc_count;
9780 sec->reloc_count += count;
9785 get_r2off (struct bfd_link_info *info,
9786 struct ppc_stub_hash_entry *stub_entry)
9788 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9789 bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
9793 /* Support linking -R objects. Get the toc pointer from the
9796 asection *opd = stub_entry->h->elf.root.u.def.section;
9797 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
9799 if (strcmp (opd->name, ".opd") != 0
9800 || opd->reloc_count != 0)
9802 info->callbacks->einfo (_("%P: cannot find opd entry toc for %s\n"),
9803 stub_entry->h->elf.root.root.string);
9804 bfd_set_error (bfd_error_bad_value);
9807 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
9809 r2off = bfd_get_64 (opd->owner, buf);
9810 r2off -= elf_gp (info->output_bfd);
9812 r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
9817 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
9819 struct ppc_stub_hash_entry *stub_entry;
9820 struct ppc_branch_hash_entry *br_entry;
9821 struct bfd_link_info *info;
9822 struct ppc_link_hash_table *htab;
9827 Elf_Internal_Rela *r;
9830 /* Massage our args to the form they really have. */
9831 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
9834 htab = ppc_hash_table (info);
9838 /* Make a note of the offset within the stubs for this entry. */
9839 stub_entry->stub_offset = stub_entry->stub_sec->size;
9840 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
9842 htab->stub_count[stub_entry->stub_type - 1] += 1;
9843 switch (stub_entry->stub_type)
9845 case ppc_stub_long_branch:
9846 case ppc_stub_long_branch_r2off:
9847 /* Branches are relative. This is where we are going to. */
9848 off = dest = (stub_entry->target_value
9849 + stub_entry->target_section->output_offset
9850 + stub_entry->target_section->output_section->vma);
9852 /* And this is where we are coming from. */
9853 off -= (stub_entry->stub_offset
9854 + stub_entry->stub_sec->output_offset
9855 + stub_entry->stub_sec->output_section->vma);
9858 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9860 bfd_vma r2off = get_r2off (info, stub_entry);
9864 htab->stub_error = TRUE;
9867 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
9870 if (PPC_HA (r2off) != 0)
9873 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9876 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
9880 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
9882 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9884 info->callbacks->einfo (_("%P: long branch stub `%s' offset overflow\n"),
9885 stub_entry->root.string);
9886 htab->stub_error = TRUE;
9890 if (info->emitrelocations)
9892 r = get_relocs (stub_entry->stub_sec, 1);
9895 r->r_offset = loc - stub_entry->stub_sec->contents;
9896 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
9898 if (stub_entry->h != NULL)
9900 struct elf_link_hash_entry **hashes;
9901 unsigned long symndx;
9902 struct ppc_link_hash_entry *h;
9904 hashes = elf_sym_hashes (htab->stub_bfd);
9907 bfd_size_type hsize;
9909 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
9910 hashes = bfd_zalloc (htab->stub_bfd, hsize);
9913 elf_sym_hashes (htab->stub_bfd) = hashes;
9914 htab->stub_globals = 1;
9916 symndx = htab->stub_globals++;
9918 hashes[symndx] = &h->elf;
9919 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
9920 if (h->oh != NULL && h->oh->is_func)
9921 h = ppc_follow_link (h->oh);
9922 if (h->elf.root.u.def.section != stub_entry->target_section)
9923 /* H is an opd symbol. The addend must be zero. */
9927 off = (h->elf.root.u.def.value
9928 + h->elf.root.u.def.section->output_offset
9929 + h->elf.root.u.def.section->output_section->vma);
9936 case ppc_stub_plt_branch:
9937 case ppc_stub_plt_branch_r2off:
9938 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9939 stub_entry->root.string + 9,
9941 if (br_entry == NULL)
9943 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
9944 stub_entry->root.string);
9945 htab->stub_error = TRUE;
9949 dest = (stub_entry->target_value
9950 + stub_entry->target_section->output_offset
9951 + stub_entry->target_section->output_section->vma);
9953 bfd_put_64 (htab->brlt->owner, dest,
9954 htab->brlt->contents + br_entry->offset);
9956 if (br_entry->iter == htab->stub_iteration)
9960 if (htab->relbrlt != NULL)
9962 /* Create a reloc for the branch lookup table entry. */
9963 Elf_Internal_Rela rela;
9966 rela.r_offset = (br_entry->offset
9967 + htab->brlt->output_offset
9968 + htab->brlt->output_section->vma);
9969 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9970 rela.r_addend = dest;
9972 rl = htab->relbrlt->contents;
9973 rl += (htab->relbrlt->reloc_count++
9974 * sizeof (Elf64_External_Rela));
9975 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
9977 else if (info->emitrelocations)
9979 r = get_relocs (htab->brlt, 1);
9982 /* brlt, being SEC_LINKER_CREATED does not go through the
9983 normal reloc processing. Symbols and offsets are not
9984 translated from input file to output file form, so
9985 set up the offset per the output file. */
9986 r->r_offset = (br_entry->offset
9987 + htab->brlt->output_offset
9988 + htab->brlt->output_section->vma);
9989 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9994 dest = (br_entry->offset
9995 + htab->brlt->output_offset
9996 + htab->brlt->output_section->vma);
9999 - elf_gp (htab->brlt->output_section->owner)
10000 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10002 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10004 info->callbacks->einfo
10005 (_("%P: linkage table error against `%s'\n"),
10006 stub_entry->root.string);
10007 bfd_set_error (bfd_error_bad_value);
10008 htab->stub_error = TRUE;
10012 if (info->emitrelocations)
10014 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
10017 r[0].r_offset = loc - stub_entry->stub_sec->contents;
10018 if (bfd_big_endian (info->output_bfd))
10019 r[0].r_offset += 2;
10020 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10021 r[0].r_offset += 4;
10022 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10023 r[0].r_addend = dest;
10024 if (PPC_HA (off) != 0)
10026 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10027 r[1].r_offset = r[0].r_offset + 4;
10028 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10029 r[1].r_addend = r[0].r_addend;
10033 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10035 if (PPC_HA (off) != 0)
10038 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
10040 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
10045 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
10050 bfd_vma r2off = get_r2off (info, stub_entry);
10054 htab->stub_error = TRUE;
10058 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
10061 if (PPC_HA (off) != 0)
10064 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
10066 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
10071 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
10075 if (PPC_HA (r2off) != 0)
10078 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10081 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
10084 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
10086 bfd_put_32 (htab->stub_bfd, BCTR, loc);
10089 case ppc_stub_plt_call:
10090 case ppc_stub_plt_call_r2save:
10091 if (stub_entry->h != NULL
10092 && stub_entry->h->is_func_descriptor
10093 && stub_entry->h->oh != NULL)
10095 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10097 /* If the old-ABI "dot-symbol" is undefined make it weak so
10098 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
10099 FIXME: We used to define the symbol on one of the call
10100 stubs instead, which is why we test symbol section id
10101 against htab->top_id in various places. Likely all
10102 these checks could now disappear. */
10103 if (fh->elf.root.type == bfd_link_hash_undefined)
10104 fh->elf.root.type = bfd_link_hash_undefweak;
10105 /* Stop undo_symbol_twiddle changing it back to undefined. */
10106 fh->was_undefined = 0;
10109 /* Now build the stub. */
10110 dest = stub_entry->plt_ent->plt.offset & ~1;
10111 if (dest >= (bfd_vma) -2)
10115 if (!htab->elf.dynamic_sections_created
10116 || stub_entry->h == NULL
10117 || stub_entry->h->elf.dynindx == -1)
10120 dest += plt->output_offset + plt->output_section->vma;
10122 if (stub_entry->h == NULL
10123 && (stub_entry->plt_ent->plt.offset & 1) == 0)
10125 Elf_Internal_Rela rela;
10128 rela.r_offset = dest;
10129 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
10130 rela.r_addend = (stub_entry->target_value
10131 + stub_entry->target_section->output_offset
10132 + stub_entry->target_section->output_section->vma);
10134 rl = (htab->reliplt->contents
10135 + (htab->reliplt->reloc_count++
10136 * sizeof (Elf64_External_Rela)));
10137 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
10138 stub_entry->plt_ent->plt.offset |= 1;
10142 - elf_gp (plt->output_section->owner)
10143 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10145 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10147 info->callbacks->einfo
10148 (_("%P: linkage table error against `%s'\n"),
10149 stub_entry->h != NULL
10150 ? stub_entry->h->elf.root.root.string
10152 bfd_set_error (bfd_error_bad_value);
10153 htab->stub_error = TRUE;
10157 if (htab->plt_stub_align != 0)
10159 unsigned pad = plt_stub_pad (htab, stub_entry, off);
10161 stub_entry->stub_sec->size += pad;
10162 stub_entry->stub_offset = stub_entry->stub_sec->size;
10167 if (info->emitrelocations)
10169 r = get_relocs (stub_entry->stub_sec,
10171 + (PPC_HA (off) != 0)
10172 + (htab->plt_static_chain
10173 && PPC_HA (off + 16) == PPC_HA (off))));
10176 r[0].r_offset = loc - stub_entry->stub_sec->contents;
10177 if (bfd_big_endian (info->output_bfd))
10178 r[0].r_offset += 2;
10179 r[0].r_addend = dest;
10181 if (stub_entry->h != NULL
10182 && (stub_entry->h == htab->tls_get_addr_fd
10183 || stub_entry->h == htab->tls_get_addr)
10184 && !htab->no_tls_get_addr_opt)
10185 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
10187 p = build_plt_stub (htab, stub_entry, loc, off, r);
10196 stub_entry->stub_sec->size += size;
10198 if (htab->emit_stub_syms)
10200 struct elf_link_hash_entry *h;
10203 const char *const stub_str[] = { "long_branch",
10204 "long_branch_r2off",
10206 "plt_branch_r2off",
10210 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10211 len2 = strlen (stub_entry->root.string);
10212 name = bfd_malloc (len1 + len2 + 2);
10215 memcpy (name, stub_entry->root.string, 9);
10216 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10217 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10218 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
10221 if (h->root.type == bfd_link_hash_new)
10223 h->root.type = bfd_link_hash_defined;
10224 h->root.u.def.section = stub_entry->stub_sec;
10225 h->root.u.def.value = stub_entry->stub_offset;
10226 h->ref_regular = 1;
10227 h->def_regular = 1;
10228 h->ref_regular_nonweak = 1;
10229 h->forced_local = 1;
10237 /* As above, but don't actually build the stub. Just bump offset so
10238 we know stub section sizes, and select plt_branch stubs where
10239 long_branch stubs won't do. */
10242 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10244 struct ppc_stub_hash_entry *stub_entry;
10245 struct bfd_link_info *info;
10246 struct ppc_link_hash_table *htab;
10250 /* Massage our args to the form they really have. */
10251 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10254 htab = ppc_hash_table (info);
10258 if (stub_entry->stub_type == ppc_stub_plt_call
10259 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10262 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
10263 if (off >= (bfd_vma) -2)
10266 if (!htab->elf.dynamic_sections_created
10267 || stub_entry->h == NULL
10268 || stub_entry->h->elf.dynindx == -1)
10270 off += (plt->output_offset
10271 + plt->output_section->vma
10272 - elf_gp (plt->output_section->owner)
10273 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10275 size = plt_stub_size (htab, stub_entry, off);
10276 if (htab->plt_stub_align)
10277 size += plt_stub_pad (htab, stub_entry, off);
10278 if (info->emitrelocations)
10280 stub_entry->stub_sec->reloc_count
10282 + (PPC_HA (off) != 0)
10283 + (htab->plt_static_chain
10284 && PPC_HA (off + 16) == PPC_HA (off)));
10285 stub_entry->stub_sec->flags |= SEC_RELOC;
10290 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10294 off = (stub_entry->target_value
10295 + stub_entry->target_section->output_offset
10296 + stub_entry->target_section->output_section->vma);
10297 off -= (stub_entry->stub_sec->size
10298 + stub_entry->stub_sec->output_offset
10299 + stub_entry->stub_sec->output_section->vma);
10301 /* Reset the stub type from the plt variant in case we now
10302 can reach with a shorter stub. */
10303 if (stub_entry->stub_type >= ppc_stub_plt_branch)
10304 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10307 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10309 r2off = get_r2off (info, stub_entry);
10312 htab->stub_error = TRUE;
10316 if (PPC_HA (r2off) != 0)
10321 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
10322 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10324 struct ppc_branch_hash_entry *br_entry;
10326 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10327 stub_entry->root.string + 9,
10329 if (br_entry == NULL)
10331 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
10332 stub_entry->root.string);
10333 htab->stub_error = TRUE;
10337 if (br_entry->iter != htab->stub_iteration)
10339 br_entry->iter = htab->stub_iteration;
10340 br_entry->offset = htab->brlt->size;
10341 htab->brlt->size += 8;
10343 if (htab->relbrlt != NULL)
10344 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10345 else if (info->emitrelocations)
10347 htab->brlt->reloc_count += 1;
10348 htab->brlt->flags |= SEC_RELOC;
10352 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
10353 off = (br_entry->offset
10354 + htab->brlt->output_offset
10355 + htab->brlt->output_section->vma
10356 - elf_gp (htab->brlt->output_section->owner)
10357 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10359 if (info->emitrelocations)
10361 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10362 stub_entry->stub_sec->flags |= SEC_RELOC;
10365 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10368 if (PPC_HA (off) != 0)
10374 if (PPC_HA (off) != 0)
10377 if (PPC_HA (r2off) != 0)
10381 else if (info->emitrelocations)
10383 stub_entry->stub_sec->reloc_count += 1;
10384 stub_entry->stub_sec->flags |= SEC_RELOC;
10388 stub_entry->stub_sec->size += size;
10392 /* Set up various things so that we can make a list of input sections
10393 for each output section included in the link. Returns -1 on error,
10394 0 when no stubs will be needed, and 1 on success. */
10397 ppc64_elf_setup_section_lists
10398 (struct bfd_link_info *info,
10399 asection *(*add_stub_section) (const char *, asection *),
10400 void (*layout_sections_again) (void))
10403 int top_id, top_index, id;
10405 asection **input_list;
10407 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10411 /* Stash our params away. */
10412 htab->add_stub_section = add_stub_section;
10413 htab->layout_sections_again = layout_sections_again;
10415 if (htab->brlt == NULL)
10418 /* Find the top input section id. */
10419 for (input_bfd = info->input_bfds, top_id = 3;
10421 input_bfd = input_bfd->link_next)
10423 for (section = input_bfd->sections;
10425 section = section->next)
10427 if (top_id < section->id)
10428 top_id = section->id;
10432 htab->top_id = top_id;
10433 amt = sizeof (struct map_stub) * (top_id + 1);
10434 htab->stub_group = bfd_zmalloc (amt);
10435 if (htab->stub_group == NULL)
10438 /* Set toc_off for com, und, abs and ind sections. */
10439 for (id = 0; id < 3; id++)
10440 htab->stub_group[id].toc_off = TOC_BASE_OFF;
10442 /* We can't use output_bfd->section_count here to find the top output
10443 section index as some sections may have been removed, and
10444 strip_excluded_output_sections doesn't renumber the indices. */
10445 for (section = info->output_bfd->sections, top_index = 0;
10447 section = section->next)
10449 if (top_index < section->index)
10450 top_index = section->index;
10453 htab->top_index = top_index;
10454 amt = sizeof (asection *) * (top_index + 1);
10455 input_list = bfd_zmalloc (amt);
10456 htab->input_list = input_list;
10457 if (input_list == NULL)
10463 /* Set up for first pass at multitoc partitioning. */
10466 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10468 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10470 elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
10471 htab->toc_curr = elf_gp (info->output_bfd);
10472 htab->toc_bfd = NULL;
10473 htab->toc_first_sec = NULL;
10476 /* The linker repeatedly calls this function for each TOC input section
10477 and linker generated GOT section. Group input bfds such that the toc
10478 within a group is less than 64k in size. */
10481 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10483 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10484 bfd_vma addr, off, limit;
10489 if (!htab->second_toc_pass)
10491 /* Keep track of the first .toc or .got section for this input bfd. */
10492 if (htab->toc_bfd != isec->owner)
10494 htab->toc_bfd = isec->owner;
10495 htab->toc_first_sec = isec;
10498 addr = isec->output_offset + isec->output_section->vma;
10499 off = addr - htab->toc_curr;
10500 limit = 0x80008000;
10501 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10503 if (off + isec->size > limit)
10505 addr = (htab->toc_first_sec->output_offset
10506 + htab->toc_first_sec->output_section->vma);
10507 htab->toc_curr = addr;
10510 /* toc_curr is the base address of this toc group. Set elf_gp
10511 for the input section to be the offset relative to the
10512 output toc base plus 0x8000. Making the input elf_gp an
10513 offset allows us to move the toc as a whole without
10514 recalculating input elf_gp. */
10515 off = htab->toc_curr - elf_gp (isec->output_section->owner);
10516 off += TOC_BASE_OFF;
10518 /* Die if someone uses a linker script that doesn't keep input
10519 file .toc and .got together. */
10520 if (elf_gp (isec->owner) != 0
10521 && elf_gp (isec->owner) != off)
10524 elf_gp (isec->owner) = off;
10528 /* During the second pass toc_first_sec points to the start of
10529 a toc group, and toc_curr is used to track the old elf_gp.
10530 We use toc_bfd to ensure we only look at each bfd once. */
10531 if (htab->toc_bfd == isec->owner)
10533 htab->toc_bfd = isec->owner;
10535 if (htab->toc_first_sec == NULL
10536 || htab->toc_curr != elf_gp (isec->owner))
10538 htab->toc_curr = elf_gp (isec->owner);
10539 htab->toc_first_sec = isec;
10541 addr = (htab->toc_first_sec->output_offset
10542 + htab->toc_first_sec->output_section->vma);
10543 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10544 elf_gp (isec->owner) = off;
10549 /* Called via elf_link_hash_traverse to merge GOT entries for global
10553 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10555 if (h->root.type == bfd_link_hash_indirect)
10558 merge_got_entries (&h->got.glist);
10563 /* Called via elf_link_hash_traverse to allocate GOT entries for global
10567 reallocate_got (struct elf_link_hash_entry *h, void *inf)
10569 struct got_entry *gent;
10571 if (h->root.type == bfd_link_hash_indirect)
10574 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10575 if (!gent->is_indirect)
10576 allocate_got (h, (struct bfd_link_info *) inf, gent);
10580 /* Called on the first multitoc pass after the last call to
10581 ppc64_elf_next_toc_section. This function removes duplicate GOT
10585 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
10587 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10588 struct bfd *ibfd, *ibfd2;
10589 bfd_boolean done_something;
10591 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
10593 if (!htab->do_multi_toc)
10596 /* Merge global sym got entries within a toc group. */
10597 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10599 /* And tlsld_got. */
10600 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10602 struct got_entry *ent, *ent2;
10604 if (!is_ppc64_elf (ibfd))
10607 ent = ppc64_tlsld_got (ibfd);
10608 if (!ent->is_indirect
10609 && ent->got.offset != (bfd_vma) -1)
10611 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
10613 if (!is_ppc64_elf (ibfd2))
10616 ent2 = ppc64_tlsld_got (ibfd2);
10617 if (!ent2->is_indirect
10618 && ent2->got.offset != (bfd_vma) -1
10619 && elf_gp (ibfd2) == elf_gp (ibfd))
10621 ent2->is_indirect = TRUE;
10622 ent2->got.ent = ent;
10628 /* Zap sizes of got sections. */
10629 htab->reliplt->rawsize = htab->reliplt->size;
10630 htab->reliplt->size -= htab->got_reli_size;
10631 htab->got_reli_size = 0;
10633 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10635 asection *got, *relgot;
10637 if (!is_ppc64_elf (ibfd))
10640 got = ppc64_elf_tdata (ibfd)->got;
10643 got->rawsize = got->size;
10645 relgot = ppc64_elf_tdata (ibfd)->relgot;
10646 relgot->rawsize = relgot->size;
10651 /* Now reallocate the got, local syms first. We don't need to
10652 allocate section contents again since we never increase size. */
10653 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10655 struct got_entry **lgot_ents;
10656 struct got_entry **end_lgot_ents;
10657 struct plt_entry **local_plt;
10658 struct plt_entry **end_local_plt;
10659 unsigned char *lgot_masks;
10660 bfd_size_type locsymcount;
10661 Elf_Internal_Shdr *symtab_hdr;
10662 asection *s, *srel;
10664 if (!is_ppc64_elf (ibfd))
10667 lgot_ents = elf_local_got_ents (ibfd);
10671 symtab_hdr = &elf_symtab_hdr (ibfd);
10672 locsymcount = symtab_hdr->sh_info;
10673 end_lgot_ents = lgot_ents + locsymcount;
10674 local_plt = (struct plt_entry **) end_lgot_ents;
10675 end_local_plt = local_plt + locsymcount;
10676 lgot_masks = (unsigned char *) end_local_plt;
10677 s = ppc64_elf_tdata (ibfd)->got;
10678 srel = ppc64_elf_tdata (ibfd)->relgot;
10679 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10681 struct got_entry *ent;
10683 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
10685 unsigned int num = 1;
10686 ent->got.offset = s->size;
10687 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10689 s->size += num * 8;
10691 srel->size += num * sizeof (Elf64_External_Rela);
10692 else if ((*lgot_masks & PLT_IFUNC) != 0)
10694 htab->reliplt->size
10695 += num * sizeof (Elf64_External_Rela);
10696 htab->got_reli_size
10697 += num * sizeof (Elf64_External_Rela);
10703 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
10705 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10707 struct got_entry *ent;
10709 if (!is_ppc64_elf (ibfd))
10712 ent = ppc64_tlsld_got (ibfd);
10713 if (!ent->is_indirect
10714 && ent->got.offset != (bfd_vma) -1)
10716 asection *s = ppc64_elf_tdata (ibfd)->got;
10717 ent->got.offset = s->size;
10721 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10722 srel->size += sizeof (Elf64_External_Rela);
10727 done_something = htab->reliplt->rawsize != htab->reliplt->size;
10728 if (!done_something)
10729 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10733 if (!is_ppc64_elf (ibfd))
10736 got = ppc64_elf_tdata (ibfd)->got;
10739 done_something = got->rawsize != got->size;
10740 if (done_something)
10745 if (done_something)
10746 (*htab->layout_sections_again) ();
10748 /* Set up for second pass over toc sections to recalculate elf_gp
10749 on input sections. */
10750 htab->toc_bfd = NULL;
10751 htab->toc_first_sec = NULL;
10752 htab->second_toc_pass = TRUE;
10753 return done_something;
10756 /* Called after second pass of multitoc partitioning. */
10759 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
10761 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10763 /* After the second pass, toc_curr tracks the TOC offset used
10764 for code sections below in ppc64_elf_next_input_section. */
10765 htab->toc_curr = TOC_BASE_OFF;
10768 /* No toc references were found in ISEC. If the code in ISEC makes no
10769 calls, then there's no need to use toc adjusting stubs when branching
10770 into ISEC. Actually, indirect calls from ISEC are OK as they will
10771 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
10772 needed, and 2 if a cyclical call-graph was found but no other reason
10773 for a stub was detected. If called from the top level, a return of
10774 2 means the same as a return of 0. */
10777 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
10781 /* Mark this section as checked. */
10782 isec->call_check_done = 1;
10784 /* We know none of our code bearing sections will need toc stubs. */
10785 if ((isec->flags & SEC_LINKER_CREATED) != 0)
10788 if (isec->size == 0)
10791 if (isec->output_section == NULL)
10795 if (isec->reloc_count != 0)
10797 Elf_Internal_Rela *relstart, *rel;
10798 Elf_Internal_Sym *local_syms;
10799 struct ppc_link_hash_table *htab;
10801 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
10802 info->keep_memory);
10803 if (relstart == NULL)
10806 /* Look for branches to outside of this section. */
10808 htab = ppc_hash_table (info);
10812 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
10814 enum elf_ppc64_reloc_type r_type;
10815 unsigned long r_symndx;
10816 struct elf_link_hash_entry *h;
10817 struct ppc_link_hash_entry *eh;
10818 Elf_Internal_Sym *sym;
10820 struct _opd_sec_data *opd;
10824 r_type = ELF64_R_TYPE (rel->r_info);
10825 if (r_type != R_PPC64_REL24
10826 && r_type != R_PPC64_REL14
10827 && r_type != R_PPC64_REL14_BRTAKEN
10828 && r_type != R_PPC64_REL14_BRNTAKEN)
10831 r_symndx = ELF64_R_SYM (rel->r_info);
10832 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
10839 /* Calls to dynamic lib functions go through a plt call stub
10841 eh = (struct ppc_link_hash_entry *) h;
10843 && (eh->elf.plt.plist != NULL
10845 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
10851 if (sym_sec == NULL)
10852 /* Ignore other undefined symbols. */
10855 /* Assume branches to other sections not included in the
10856 link need stubs too, to cover -R and absolute syms. */
10857 if (sym_sec->output_section == NULL)
10864 sym_value = sym->st_value;
10867 if (h->root.type != bfd_link_hash_defined
10868 && h->root.type != bfd_link_hash_defweak)
10870 sym_value = h->root.u.def.value;
10872 sym_value += rel->r_addend;
10874 /* If this branch reloc uses an opd sym, find the code section. */
10875 opd = get_opd_info (sym_sec);
10878 if (h == NULL && opd->adjust != NULL)
10882 adjust = opd->adjust[sym->st_value / 8];
10884 /* Assume deleted functions won't ever be called. */
10886 sym_value += adjust;
10889 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
10890 if (dest == (bfd_vma) -1)
10895 + sym_sec->output_offset
10896 + sym_sec->output_section->vma);
10898 /* Ignore branch to self. */
10899 if (sym_sec == isec)
10902 /* If the called function uses the toc, we need a stub. */
10903 if (sym_sec->has_toc_reloc
10904 || sym_sec->makes_toc_func_call)
10910 /* Assume any branch that needs a long branch stub might in fact
10911 need a plt_branch stub. A plt_branch stub uses r2. */
10912 else if (dest - (isec->output_offset
10913 + isec->output_section->vma
10914 + rel->r_offset) + (1 << 25) >= (2 << 25))
10920 /* If calling back to a section in the process of being
10921 tested, we can't say for sure that no toc adjusting stubs
10922 are needed, so don't return zero. */
10923 else if (sym_sec->call_check_in_progress)
10926 /* Branches to another section that itself doesn't have any TOC
10927 references are OK. Recursively call ourselves to check. */
10928 else if (!sym_sec->call_check_done)
10932 /* Mark current section as indeterminate, so that other
10933 sections that call back to current won't be marked as
10935 isec->call_check_in_progress = 1;
10936 recur = toc_adjusting_stub_needed (info, sym_sec);
10937 isec->call_check_in_progress = 0;
10948 if (local_syms != NULL
10949 && (elf_symtab_hdr (isec->owner).contents
10950 != (unsigned char *) local_syms))
10952 if (elf_section_data (isec)->relocs != relstart)
10957 && isec->map_head.s != NULL
10958 && (strcmp (isec->output_section->name, ".init") == 0
10959 || strcmp (isec->output_section->name, ".fini") == 0))
10961 if (isec->map_head.s->has_toc_reloc
10962 || isec->map_head.s->makes_toc_func_call)
10964 else if (!isec->map_head.s->call_check_done)
10967 isec->call_check_in_progress = 1;
10968 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
10969 isec->call_check_in_progress = 0;
10976 isec->makes_toc_func_call = 1;
10981 /* The linker repeatedly calls this function for each input section,
10982 in the order that input sections are linked into output sections.
10983 Build lists of input sections to determine groupings between which
10984 we may insert linker stubs. */
10987 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
10989 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10994 if ((isec->output_section->flags & SEC_CODE) != 0
10995 && isec->output_section->index <= htab->top_index)
10997 asection **list = htab->input_list + isec->output_section->index;
10998 /* Steal the link_sec pointer for our list. */
10999 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
11000 /* This happens to make the list in reverse order,
11001 which is what we want. */
11002 PREV_SEC (isec) = *list;
11006 if (htab->multi_toc_needed)
11008 /* If a code section has a function that uses the TOC then we need
11009 to use the right TOC (obviously). Also, make sure that .opd gets
11010 the correct TOC value for R_PPC64_TOC relocs that don't have or
11011 can't find their function symbol (shouldn't ever happen now).
11012 Also specially treat .fixup for the linux kernel. .fixup
11013 contains branches, but only back to the function that hit an
11015 if (isec->has_toc_reloc
11016 || (isec->flags & SEC_CODE) == 0
11017 || strcmp (isec->name, ".fixup") == 0)
11019 if (elf_gp (isec->owner) != 0)
11020 htab->toc_curr = elf_gp (isec->owner);
11024 if (!isec->call_check_done
11025 && toc_adjusting_stub_needed (info, isec) < 0)
11027 /* If we make a local call from this section, ie. a branch
11028 without a following nop, then we have no place to put a
11029 toc restoring insn. We must use the same toc group as
11031 Testing makes_toc_func_call actually tests for *any*
11032 calls to functions that need a good toc pointer. A more
11033 precise test would be better, as this one will set
11034 incorrect values for pasted .init/.fini fragments.
11035 (Fixed later in check_pasted_section.) */
11036 if (isec->makes_toc_func_call
11037 && elf_gp (isec->owner) != 0)
11038 htab->toc_curr = elf_gp (isec->owner);
11042 /* Functions that don't use the TOC can belong in any TOC group.
11043 Use the last TOC base. */
11044 htab->stub_group[isec->id].toc_off = htab->toc_curr;
11048 /* Check that all .init and .fini sections use the same toc, if they
11049 have toc relocs. */
11052 check_pasted_section (struct bfd_link_info *info, const char *name)
11054 asection *o = bfd_get_section_by_name (info->output_bfd, name);
11058 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11059 bfd_vma toc_off = 0;
11062 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11063 if (i->has_toc_reloc)
11066 toc_off = htab->stub_group[i->id].toc_off;
11067 else if (toc_off != htab->stub_group[i->id].toc_off)
11072 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11073 if (i->makes_toc_func_call)
11075 toc_off = htab->stub_group[i->id].toc_off;
11079 /* Make sure the whole pasted function uses the same toc offset. */
11081 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11082 htab->stub_group[i->id].toc_off = toc_off;
11088 ppc64_elf_check_init_fini (struct bfd_link_info *info)
11090 return (check_pasted_section (info, ".init")
11091 & check_pasted_section (info, ".fini"));
11094 /* See whether we can group stub sections together. Grouping stub
11095 sections may result in fewer stubs. More importantly, we need to
11096 put all .init* and .fini* stubs at the beginning of the .init or
11097 .fini output sections respectively, because glibc splits the
11098 _init and _fini functions into multiple parts. Putting a stub in
11099 the middle of a function is not a good idea. */
11102 group_sections (struct ppc_link_hash_table *htab,
11103 bfd_size_type stub_group_size,
11104 bfd_boolean stubs_always_before_branch)
11107 bfd_size_type stub14_group_size;
11108 bfd_boolean suppress_size_errors;
11110 suppress_size_errors = FALSE;
11111 stub14_group_size = stub_group_size;
11112 if (stub_group_size == 1)
11114 /* Default values. */
11115 if (stubs_always_before_branch)
11117 stub_group_size = 0x1e00000;
11118 stub14_group_size = 0x7800;
11122 stub_group_size = 0x1c00000;
11123 stub14_group_size = 0x7000;
11125 suppress_size_errors = TRUE;
11128 list = htab->input_list + htab->top_index;
11131 asection *tail = *list;
11132 while (tail != NULL)
11136 bfd_size_type total;
11137 bfd_boolean big_sec;
11141 total = tail->size;
11142 big_sec = total > (ppc64_elf_section_data (tail) != NULL
11143 && ppc64_elf_section_data (tail)->has_14bit_branch
11144 ? stub14_group_size : stub_group_size);
11145 if (big_sec && !suppress_size_errors)
11146 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
11147 tail->owner, tail);
11148 curr_toc = htab->stub_group[tail->id].toc_off;
11150 while ((prev = PREV_SEC (curr)) != NULL
11151 && ((total += curr->output_offset - prev->output_offset)
11152 < (ppc64_elf_section_data (prev) != NULL
11153 && ppc64_elf_section_data (prev)->has_14bit_branch
11154 ? stub14_group_size : stub_group_size))
11155 && htab->stub_group[prev->id].toc_off == curr_toc)
11158 /* OK, the size from the start of CURR to the end is less
11159 than stub_group_size and thus can be handled by one stub
11160 section. (or the tail section is itself larger than
11161 stub_group_size, in which case we may be toast.) We
11162 should really be keeping track of the total size of stubs
11163 added here, as stubs contribute to the final output
11164 section size. That's a little tricky, and this way will
11165 only break if stubs added make the total size more than
11166 2^25, ie. for the default stub_group_size, if stubs total
11167 more than 2097152 bytes, or nearly 75000 plt call stubs. */
11170 prev = PREV_SEC (tail);
11171 /* Set up this stub group. */
11172 htab->stub_group[tail->id].link_sec = curr;
11174 while (tail != curr && (tail = prev) != NULL);
11176 /* But wait, there's more! Input sections up to stub_group_size
11177 bytes before the stub section can be handled by it too.
11178 Don't do this if we have a really large section after the
11179 stubs, as adding more stubs increases the chance that
11180 branches may not reach into the stub section. */
11181 if (!stubs_always_before_branch && !big_sec)
11184 while (prev != NULL
11185 && ((total += tail->output_offset - prev->output_offset)
11186 < (ppc64_elf_section_data (prev) != NULL
11187 && ppc64_elf_section_data (prev)->has_14bit_branch
11188 ? stub14_group_size : stub_group_size))
11189 && htab->stub_group[prev->id].toc_off == curr_toc)
11192 prev = PREV_SEC (tail);
11193 htab->stub_group[tail->id].link_sec = curr;
11199 while (list-- != htab->input_list);
11200 free (htab->input_list);
11204 static const unsigned char glink_eh_frame_cie[] =
11206 0, 0, 0, 16, /* length. */
11207 0, 0, 0, 0, /* id. */
11208 1, /* CIE version. */
11209 'z', 'R', 0, /* Augmentation string. */
11210 4, /* Code alignment. */
11211 0x78, /* Data alignment. */
11213 1, /* Augmentation size. */
11214 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
11215 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
11218 /* Stripping output sections is normally done before dynamic section
11219 symbols have been allocated. This function is called later, and
11220 handles cases like htab->brlt which is mapped to its own output
11224 maybe_strip_output (struct bfd_link_info *info, asection *isec)
11226 if (isec->size == 0
11227 && isec->output_section->size == 0
11228 && !bfd_section_removed_from_list (info->output_bfd,
11229 isec->output_section)
11230 && elf_section_data (isec->output_section)->dynindx == 0)
11232 isec->output_section->flags |= SEC_EXCLUDE;
11233 bfd_section_list_remove (info->output_bfd, isec->output_section);
11234 info->output_bfd->section_count--;
11238 /* Determine and set the size of the stub section for a final link.
11240 The basic idea here is to examine all the relocations looking for
11241 PC-relative calls to a target that is unreachable with a "bl"
11245 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
11246 bfd_boolean plt_static_chain, int plt_thread_safe,
11247 int plt_stub_align)
11249 bfd_size_type stub_group_size;
11250 bfd_boolean stubs_always_before_branch;
11251 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11256 htab->plt_static_chain = plt_static_chain;
11257 htab->plt_stub_align = plt_stub_align;
11258 if (plt_thread_safe == -1)
11260 const char *const thread_starter[] =
11264 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11266 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11267 "mq_notify", "create_timer",
11271 "GOMP_parallel_start",
11272 "GOMP_parallel_loop_static_start",
11273 "GOMP_parallel_loop_dynamic_start",
11274 "GOMP_parallel_loop_guided_start",
11275 "GOMP_parallel_loop_runtime_start",
11276 "GOMP_parallel_sections_start",
11280 for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
11282 struct elf_link_hash_entry *h;
11283 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11284 FALSE, FALSE, TRUE);
11285 plt_thread_safe = h != NULL && h->ref_regular;
11286 if (plt_thread_safe)
11290 htab->plt_thread_safe = plt_thread_safe;
11291 stubs_always_before_branch = group_size < 0;
11292 if (group_size < 0)
11293 stub_group_size = -group_size;
11295 stub_group_size = group_size;
11297 group_sections (htab, stub_group_size, stubs_always_before_branch);
11302 unsigned int bfd_indx;
11303 asection *stub_sec;
11305 htab->stub_iteration += 1;
11307 for (input_bfd = info->input_bfds, bfd_indx = 0;
11309 input_bfd = input_bfd->link_next, bfd_indx++)
11311 Elf_Internal_Shdr *symtab_hdr;
11313 Elf_Internal_Sym *local_syms = NULL;
11315 if (!is_ppc64_elf (input_bfd))
11318 /* We'll need the symbol table in a second. */
11319 symtab_hdr = &elf_symtab_hdr (input_bfd);
11320 if (symtab_hdr->sh_info == 0)
11323 /* Walk over each section attached to the input bfd. */
11324 for (section = input_bfd->sections;
11326 section = section->next)
11328 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
11330 /* If there aren't any relocs, then there's nothing more
11332 if ((section->flags & SEC_RELOC) == 0
11333 || (section->flags & SEC_ALLOC) == 0
11334 || (section->flags & SEC_LOAD) == 0
11335 || (section->flags & SEC_CODE) == 0
11336 || section->reloc_count == 0)
11339 /* If this section is a link-once section that will be
11340 discarded, then don't create any stubs. */
11341 if (section->output_section == NULL
11342 || section->output_section->owner != info->output_bfd)
11345 /* Get the relocs. */
11347 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
11348 info->keep_memory);
11349 if (internal_relocs == NULL)
11350 goto error_ret_free_local;
11352 /* Now examine each relocation. */
11353 irela = internal_relocs;
11354 irelaend = irela + section->reloc_count;
11355 for (; irela < irelaend; irela++)
11357 enum elf_ppc64_reloc_type r_type;
11358 unsigned int r_indx;
11359 enum ppc_stub_type stub_type;
11360 struct ppc_stub_hash_entry *stub_entry;
11361 asection *sym_sec, *code_sec;
11362 bfd_vma sym_value, code_value;
11363 bfd_vma destination;
11364 bfd_boolean ok_dest;
11365 struct ppc_link_hash_entry *hash;
11366 struct ppc_link_hash_entry *fdh;
11367 struct elf_link_hash_entry *h;
11368 Elf_Internal_Sym *sym;
11370 const asection *id_sec;
11371 struct _opd_sec_data *opd;
11372 struct plt_entry *plt_ent;
11374 r_type = ELF64_R_TYPE (irela->r_info);
11375 r_indx = ELF64_R_SYM (irela->r_info);
11377 if (r_type >= R_PPC64_max)
11379 bfd_set_error (bfd_error_bad_value);
11380 goto error_ret_free_internal;
11383 /* Only look for stubs on branch instructions. */
11384 if (r_type != R_PPC64_REL24
11385 && r_type != R_PPC64_REL14
11386 && r_type != R_PPC64_REL14_BRTAKEN
11387 && r_type != R_PPC64_REL14_BRNTAKEN)
11390 /* Now determine the call target, its name, value,
11392 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11393 r_indx, input_bfd))
11394 goto error_ret_free_internal;
11395 hash = (struct ppc_link_hash_entry *) h;
11402 sym_value = sym->st_value;
11405 else if (hash->elf.root.type == bfd_link_hash_defined
11406 || hash->elf.root.type == bfd_link_hash_defweak)
11408 sym_value = hash->elf.root.u.def.value;
11409 if (sym_sec->output_section != NULL)
11412 else if (hash->elf.root.type == bfd_link_hash_undefweak
11413 || hash->elf.root.type == bfd_link_hash_undefined)
11415 /* Recognise an old ABI func code entry sym, and
11416 use the func descriptor sym instead if it is
11418 if (hash->elf.root.root.string[0] == '.'
11419 && (fdh = lookup_fdh (hash, htab)) != NULL)
11421 if (fdh->elf.root.type == bfd_link_hash_defined
11422 || fdh->elf.root.type == bfd_link_hash_defweak)
11424 sym_sec = fdh->elf.root.u.def.section;
11425 sym_value = fdh->elf.root.u.def.value;
11426 if (sym_sec->output_section != NULL)
11435 bfd_set_error (bfd_error_bad_value);
11436 goto error_ret_free_internal;
11442 sym_value += irela->r_addend;
11443 destination = (sym_value
11444 + sym_sec->output_offset
11445 + sym_sec->output_section->vma);
11448 code_sec = sym_sec;
11449 code_value = sym_value;
11450 opd = get_opd_info (sym_sec);
11455 if (hash == NULL && opd->adjust != NULL)
11457 long adjust = opd->adjust[sym_value / 8];
11460 code_value += adjust;
11461 sym_value += adjust;
11463 dest = opd_entry_value (sym_sec, sym_value,
11464 &code_sec, &code_value);
11465 if (dest != (bfd_vma) -1)
11467 destination = dest;
11470 /* Fixup old ABI sym to point at code
11472 hash->elf.root.type = bfd_link_hash_defweak;
11473 hash->elf.root.u.def.section = code_sec;
11474 hash->elf.root.u.def.value = code_value;
11479 /* Determine what (if any) linker stub is needed. */
11481 stub_type = ppc_type_of_stub (section, irela, &hash,
11482 &plt_ent, destination);
11484 if (stub_type != ppc_stub_plt_call)
11486 /* Check whether we need a TOC adjusting stub.
11487 Since the linker pastes together pieces from
11488 different object files when creating the
11489 _init and _fini functions, it may be that a
11490 call to what looks like a local sym is in
11491 fact a call needing a TOC adjustment. */
11492 if (code_sec != NULL
11493 && code_sec->output_section != NULL
11494 && (htab->stub_group[code_sec->id].toc_off
11495 != htab->stub_group[section->id].toc_off)
11496 && (code_sec->has_toc_reloc
11497 || code_sec->makes_toc_func_call))
11498 stub_type = ppc_stub_long_branch_r2off;
11501 if (stub_type == ppc_stub_none)
11504 /* __tls_get_addr calls might be eliminated. */
11505 if (stub_type != ppc_stub_plt_call
11507 && (hash == htab->tls_get_addr
11508 || hash == htab->tls_get_addr_fd)
11509 && section->has_tls_reloc
11510 && irela != internal_relocs)
11512 /* Get tls info. */
11513 unsigned char *tls_mask;
11515 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
11516 irela - 1, input_bfd))
11517 goto error_ret_free_internal;
11518 if (*tls_mask != 0)
11522 if (stub_type == ppc_stub_plt_call
11523 && irela + 1 < irelaend
11524 && irela[1].r_offset == irela->r_offset + 4
11525 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
11527 if (!tocsave_find (htab, INSERT,
11528 &local_syms, irela + 1, input_bfd))
11529 goto error_ret_free_internal;
11531 else if (stub_type == ppc_stub_plt_call)
11532 stub_type = ppc_stub_plt_call_r2save;
11534 /* Support for grouping stub sections. */
11535 id_sec = htab->stub_group[section->id].link_sec;
11537 /* Get the name of this stub. */
11538 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11540 goto error_ret_free_internal;
11542 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
11543 stub_name, FALSE, FALSE);
11544 if (stub_entry != NULL)
11546 /* The proper stub has already been created. */
11548 if (stub_type == ppc_stub_plt_call_r2save)
11549 stub_entry->stub_type = stub_type;
11553 stub_entry = ppc_add_stub (stub_name, section, info);
11554 if (stub_entry == NULL)
11557 error_ret_free_internal:
11558 if (elf_section_data (section)->relocs == NULL)
11559 free (internal_relocs);
11560 error_ret_free_local:
11561 if (local_syms != NULL
11562 && (symtab_hdr->contents
11563 != (unsigned char *) local_syms))
11568 stub_entry->stub_type = stub_type;
11569 if (stub_type != ppc_stub_plt_call
11570 && stub_type != ppc_stub_plt_call_r2save)
11572 stub_entry->target_value = code_value;
11573 stub_entry->target_section = code_sec;
11577 stub_entry->target_value = sym_value;
11578 stub_entry->target_section = sym_sec;
11580 stub_entry->h = hash;
11581 stub_entry->plt_ent = plt_ent;
11582 stub_entry->addend = irela->r_addend;
11584 if (stub_entry->h != NULL)
11585 htab->stub_globals += 1;
11588 /* We're done with the internal relocs, free them. */
11589 if (elf_section_data (section)->relocs != internal_relocs)
11590 free (internal_relocs);
11593 if (local_syms != NULL
11594 && symtab_hdr->contents != (unsigned char *) local_syms)
11596 if (!info->keep_memory)
11599 symtab_hdr->contents = (unsigned char *) local_syms;
11603 /* We may have added some stubs. Find out the new size of the
11605 for (stub_sec = htab->stub_bfd->sections;
11607 stub_sec = stub_sec->next)
11608 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11610 stub_sec->rawsize = stub_sec->size;
11611 stub_sec->size = 0;
11612 stub_sec->reloc_count = 0;
11613 stub_sec->flags &= ~SEC_RELOC;
11616 htab->brlt->size = 0;
11617 htab->brlt->reloc_count = 0;
11618 htab->brlt->flags &= ~SEC_RELOC;
11619 if (htab->relbrlt != NULL)
11620 htab->relbrlt->size = 0;
11622 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
11624 if (info->emitrelocations
11625 && htab->glink != NULL && htab->glink->size != 0)
11627 htab->glink->reloc_count = 1;
11628 htab->glink->flags |= SEC_RELOC;
11631 if (htab->glink_eh_frame != NULL
11632 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
11633 && (htab->glink_eh_frame->flags & SEC_EXCLUDE) == 0)
11635 bfd_size_type size = 0;
11637 for (stub_sec = htab->stub_bfd->sections;
11639 stub_sec = stub_sec->next)
11640 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11642 if (htab->glink != NULL && htab->glink->size != 0)
11645 size += sizeof (glink_eh_frame_cie);
11646 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
11647 htab->glink_eh_frame->size = size;
11650 if (htab->plt_stub_align != 0)
11651 for (stub_sec = htab->stub_bfd->sections;
11653 stub_sec = stub_sec->next)
11654 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11655 stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
11656 & (-1 << htab->plt_stub_align));
11658 for (stub_sec = htab->stub_bfd->sections;
11660 stub_sec = stub_sec->next)
11661 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11662 && stub_sec->rawsize != stub_sec->size)
11665 /* Exit from this loop when no stubs have been added, and no stubs
11666 have changed size. */
11667 if (stub_sec == NULL
11668 && (htab->glink_eh_frame == NULL
11669 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
11672 /* Ask the linker to do its stuff. */
11673 (*htab->layout_sections_again) ();
11676 maybe_strip_output (info, htab->brlt);
11677 if (htab->glink_eh_frame != NULL)
11678 maybe_strip_output (info, htab->glink_eh_frame);
11683 /* Called after we have determined section placement. If sections
11684 move, we'll be called again. Provide a value for TOCstart. */
11687 ppc64_elf_toc (bfd *obfd)
11692 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
11693 order. The TOC starts where the first of these sections starts. */
11694 s = bfd_get_section_by_name (obfd, ".got");
11695 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11696 s = bfd_get_section_by_name (obfd, ".toc");
11697 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11698 s = bfd_get_section_by_name (obfd, ".tocbss");
11699 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11700 s = bfd_get_section_by_name (obfd, ".plt");
11701 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11703 /* This may happen for
11704 o references to TOC base (SYM@toc / TOC[tc0]) without a
11706 o bad linker script
11707 o --gc-sections and empty TOC sections
11709 FIXME: Warn user? */
11711 /* Look for a likely section. We probably won't even be
11713 for (s = obfd->sections; s != NULL; s = s->next)
11714 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
11716 == (SEC_ALLOC | SEC_SMALL_DATA))
11719 for (s = obfd->sections; s != NULL; s = s->next)
11720 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
11721 == (SEC_ALLOC | SEC_SMALL_DATA))
11724 for (s = obfd->sections; s != NULL; s = s->next)
11725 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
11729 for (s = obfd->sections; s != NULL; s = s->next)
11730 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
11736 TOCstart = s->output_section->vma + s->output_offset;
11741 /* Build all the stubs associated with the current output file.
11742 The stubs are kept in a hash table attached to the main linker
11743 hash table. This function is called via gldelf64ppc_finish. */
11746 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
11747 struct bfd_link_info *info,
11750 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11751 asection *stub_sec;
11753 int stub_sec_count = 0;
11758 htab->emit_stub_syms = emit_stub_syms;
11760 /* Allocate memory to hold the linker stubs. */
11761 for (stub_sec = htab->stub_bfd->sections;
11763 stub_sec = stub_sec->next)
11764 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11765 && stub_sec->size != 0)
11767 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
11768 if (stub_sec->contents == NULL)
11770 /* We want to check that built size is the same as calculated
11771 size. rawsize is a convenient location to use. */
11772 stub_sec->rawsize = stub_sec->size;
11773 stub_sec->size = 0;
11776 if (htab->glink != NULL && htab->glink->size != 0)
11781 /* Build the .glink plt call stub. */
11782 if (htab->emit_stub_syms)
11784 struct elf_link_hash_entry *h;
11785 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
11786 TRUE, FALSE, FALSE);
11789 if (h->root.type == bfd_link_hash_new)
11791 h->root.type = bfd_link_hash_defined;
11792 h->root.u.def.section = htab->glink;
11793 h->root.u.def.value = 8;
11794 h->ref_regular = 1;
11795 h->def_regular = 1;
11796 h->ref_regular_nonweak = 1;
11797 h->forced_local = 1;
11801 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
11802 if (info->emitrelocations)
11804 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
11807 r->r_offset = (htab->glink->output_offset
11808 + htab->glink->output_section->vma);
11809 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
11810 r->r_addend = plt0;
11812 p = htab->glink->contents;
11813 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
11814 bfd_put_64 (htab->glink->owner, plt0, p);
11816 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
11818 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
11820 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
11822 bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
11824 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
11826 bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
11828 bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
11830 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
11832 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
11834 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
11836 bfd_put_32 (htab->glink->owner, BCTR, p);
11838 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
11840 bfd_put_32 (htab->glink->owner, NOP, p);
11844 /* Build the .glink lazy link call stubs. */
11846 while (p < htab->glink->contents + htab->glink->size)
11850 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
11855 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
11857 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
11860 bfd_put_32 (htab->glink->owner,
11861 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
11865 htab->glink->rawsize = p - htab->glink->contents;
11868 if (htab->brlt->size != 0)
11870 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
11872 if (htab->brlt->contents == NULL)
11875 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
11877 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
11878 htab->relbrlt->size);
11879 if (htab->relbrlt->contents == NULL)
11883 if (htab->glink_eh_frame != NULL
11884 && htab->glink_eh_frame->size != 0)
11888 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
11891 htab->glink_eh_frame->contents = p;
11893 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
11895 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
11896 /* CIE length (rewrite in case little-endian). */
11897 bfd_put_32 (htab->elf.dynobj, sizeof (glink_eh_frame_cie) - 4, p);
11898 p += sizeof (glink_eh_frame_cie);
11900 for (stub_sec = htab->stub_bfd->sections;
11902 stub_sec = stub_sec->next)
11903 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11906 bfd_put_32 (htab->elf.dynobj, 16, p);
11909 val = p - htab->glink_eh_frame->contents;
11910 bfd_put_32 (htab->elf.dynobj, val, p);
11912 /* Offset to stub section. */
11913 val = (stub_sec->output_section->vma
11914 + stub_sec->output_offset);
11915 val -= (htab->glink_eh_frame->output_section->vma
11916 + htab->glink_eh_frame->output_offset);
11917 val -= p - htab->glink_eh_frame->contents;
11918 if (val + 0x80000000 > 0xffffffff)
11920 info->callbacks->einfo
11921 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
11925 bfd_put_32 (htab->elf.dynobj, val, p);
11927 /* stub section size. */
11928 bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
11930 /* Augmentation. */
11935 if (htab->glink != NULL && htab->glink->size != 0)
11938 bfd_put_32 (htab->elf.dynobj, 20, p);
11941 val = p - htab->glink_eh_frame->contents;
11942 bfd_put_32 (htab->elf.dynobj, val, p);
11944 /* Offset to .glink. */
11945 val = (htab->glink->output_section->vma
11946 + htab->glink->output_offset
11948 val -= (htab->glink_eh_frame->output_section->vma
11949 + htab->glink_eh_frame->output_offset);
11950 val -= p - htab->glink_eh_frame->contents;
11951 if (val + 0x80000000 > 0xffffffff)
11953 info->callbacks->einfo
11954 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
11955 htab->glink->name);
11958 bfd_put_32 (htab->elf.dynobj, val, p);
11961 bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
11963 /* Augmentation. */
11966 *p++ = DW_CFA_advance_loc + 1;
11967 *p++ = DW_CFA_register;
11970 *p++ = DW_CFA_advance_loc + 4;
11971 *p++ = DW_CFA_restore_extended;
11974 htab->glink_eh_frame->size = p - htab->glink_eh_frame->contents;
11977 /* Build the stubs as directed by the stub hash table. */
11978 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
11980 if (htab->relbrlt != NULL)
11981 htab->relbrlt->reloc_count = 0;
11983 if (htab->plt_stub_align != 0)
11984 for (stub_sec = htab->stub_bfd->sections;
11986 stub_sec = stub_sec->next)
11987 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11988 stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
11989 & (-1 << htab->plt_stub_align));
11991 for (stub_sec = htab->stub_bfd->sections;
11993 stub_sec = stub_sec->next)
11994 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11996 stub_sec_count += 1;
11997 if (stub_sec->rawsize != stub_sec->size)
12001 if (stub_sec != NULL
12002 || htab->glink->rawsize != htab->glink->size
12003 || (htab->glink_eh_frame != NULL
12004 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
12006 htab->stub_error = TRUE;
12007 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
12010 if (htab->stub_error)
12015 *stats = bfd_malloc (500);
12016 if (*stats == NULL)
12019 sprintf (*stats, _("linker stubs in %u group%s\n"
12021 " toc adjust %lu\n"
12022 " long branch %lu\n"
12023 " long toc adj %lu\n"
12025 " plt call toc %lu"),
12027 stub_sec_count == 1 ? "" : "s",
12028 htab->stub_count[ppc_stub_long_branch - 1],
12029 htab->stub_count[ppc_stub_long_branch_r2off - 1],
12030 htab->stub_count[ppc_stub_plt_branch - 1],
12031 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
12032 htab->stub_count[ppc_stub_plt_call - 1],
12033 htab->stub_count[ppc_stub_plt_call_r2save - 1]);
12038 /* This function undoes the changes made by add_symbol_adjust. */
12041 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12043 struct ppc_link_hash_entry *eh;
12045 if (h->root.type == bfd_link_hash_indirect)
12048 eh = (struct ppc_link_hash_entry *) h;
12049 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
12052 eh->elf.root.type = bfd_link_hash_undefined;
12057 ppc64_elf_restore_symbols (struct bfd_link_info *info)
12059 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12062 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
12065 /* What to do when ld finds relocations against symbols defined in
12066 discarded sections. */
12068 static unsigned int
12069 ppc64_elf_action_discarded (asection *sec)
12071 if (strcmp (".opd", sec->name) == 0)
12074 if (strcmp (".toc", sec->name) == 0)
12077 if (strcmp (".toc1", sec->name) == 0)
12080 return _bfd_elf_default_action_discarded (sec);
12083 /* The RELOCATE_SECTION function is called by the ELF backend linker
12084 to handle the relocations for a section.
12086 The relocs are always passed as Rela structures; if the section
12087 actually uses Rel structures, the r_addend field will always be
12090 This function is responsible for adjust the section contents as
12091 necessary, and (if using Rela relocs and generating a
12092 relocatable output file) adjusting the reloc addend as
12095 This function does not have to worry about setting the reloc
12096 address or the reloc symbol index.
12098 LOCAL_SYMS is a pointer to the swapped in local symbols.
12100 LOCAL_SECTIONS is an array giving the section in the input file
12101 corresponding to the st_shndx field of each local symbol.
12103 The global hash table entry for the global symbols can be found
12104 via elf_sym_hashes (input_bfd).
12106 When generating relocatable output, this function must handle
12107 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
12108 going to be the section symbol corresponding to the output
12109 section, which means that the addend must be adjusted
12113 ppc64_elf_relocate_section (bfd *output_bfd,
12114 struct bfd_link_info *info,
12116 asection *input_section,
12117 bfd_byte *contents,
12118 Elf_Internal_Rela *relocs,
12119 Elf_Internal_Sym *local_syms,
12120 asection **local_sections)
12122 struct ppc_link_hash_table *htab;
12123 Elf_Internal_Shdr *symtab_hdr;
12124 struct elf_link_hash_entry **sym_hashes;
12125 Elf_Internal_Rela *rel;
12126 Elf_Internal_Rela *relend;
12127 Elf_Internal_Rela outrel;
12129 struct got_entry **local_got_ents;
12131 bfd_boolean ret = TRUE;
12132 bfd_boolean is_opd;
12133 /* Assume 'at' branch hints. */
12134 bfd_boolean is_isa_v2 = TRUE;
12135 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
12137 /* Initialize howto table if needed. */
12138 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
12141 htab = ppc_hash_table (info);
12145 /* Don't relocate stub sections. */
12146 if (input_section->owner == htab->stub_bfd)
12149 BFD_ASSERT (is_ppc64_elf (input_bfd));
12151 local_got_ents = elf_local_got_ents (input_bfd);
12152 TOCstart = elf_gp (output_bfd);
12153 symtab_hdr = &elf_symtab_hdr (input_bfd);
12154 sym_hashes = elf_sym_hashes (input_bfd);
12155 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
12158 relend = relocs + input_section->reloc_count;
12159 for (; rel < relend; rel++)
12161 enum elf_ppc64_reloc_type r_type;
12162 bfd_vma addend, orig_addend;
12163 bfd_reloc_status_type r;
12164 Elf_Internal_Sym *sym;
12166 struct elf_link_hash_entry *h_elf;
12167 struct ppc_link_hash_entry *h;
12168 struct ppc_link_hash_entry *fdh;
12169 const char *sym_name;
12170 unsigned long r_symndx, toc_symndx;
12171 bfd_vma toc_addend;
12172 unsigned char tls_mask, tls_gd, tls_type;
12173 unsigned char sym_type;
12174 bfd_vma relocation;
12175 bfd_boolean unresolved_reloc;
12176 bfd_boolean warned;
12179 struct ppc_stub_hash_entry *stub_entry;
12180 bfd_vma max_br_offset;
12183 r_type = ELF64_R_TYPE (rel->r_info);
12184 r_symndx = ELF64_R_SYM (rel->r_info);
12186 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
12187 symbol of the previous ADDR64 reloc. The symbol gives us the
12188 proper TOC base to use. */
12189 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
12191 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
12193 r_symndx = ELF64_R_SYM (rel[-1].r_info);
12199 unresolved_reloc = FALSE;
12201 orig_addend = rel->r_addend;
12203 if (r_symndx < symtab_hdr->sh_info)
12205 /* It's a local symbol. */
12206 struct _opd_sec_data *opd;
12208 sym = local_syms + r_symndx;
12209 sec = local_sections[r_symndx];
12210 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
12211 sym_type = ELF64_ST_TYPE (sym->st_info);
12212 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
12213 opd = get_opd_info (sec);
12214 if (opd != NULL && opd->adjust != NULL)
12216 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
12221 /* If this is a relocation against the opd section sym
12222 and we have edited .opd, adjust the reloc addend so
12223 that ld -r and ld --emit-relocs output is correct.
12224 If it is a reloc against some other .opd symbol,
12225 then the symbol value will be adjusted later. */
12226 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
12227 rel->r_addend += adjust;
12229 relocation += adjust;
12235 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
12236 r_symndx, symtab_hdr, sym_hashes,
12237 h_elf, sec, relocation,
12238 unresolved_reloc, warned);
12239 sym_name = h_elf->root.root.string;
12240 sym_type = h_elf->type;
12242 h = (struct ppc_link_hash_entry *) h_elf;
12244 if (sec != NULL && elf_discarded_section (sec))
12245 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
12247 ppc64_elf_howto_table[r_type],
12250 if (info->relocatable)
12253 /* TLS optimizations. Replace instruction sequences and relocs
12254 based on information we collected in tls_optimize. We edit
12255 RELOCS so that --emit-relocs will output something sensible
12256 for the final instruction stream. */
12261 tls_mask = h->tls_mask;
12262 else if (local_got_ents != NULL)
12264 struct plt_entry **local_plt = (struct plt_entry **)
12265 (local_got_ents + symtab_hdr->sh_info);
12266 unsigned char *lgot_masks = (unsigned char *)
12267 (local_plt + symtab_hdr->sh_info);
12268 tls_mask = lgot_masks[r_symndx];
12271 && (r_type == R_PPC64_TLS
12272 || r_type == R_PPC64_TLSGD
12273 || r_type == R_PPC64_TLSLD))
12275 /* Check for toc tls entries. */
12276 unsigned char *toc_tls;
12278 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12279 &local_syms, rel, input_bfd))
12283 tls_mask = *toc_tls;
12286 /* Check that tls relocs are used with tls syms, and non-tls
12287 relocs are used with non-tls syms. */
12288 if (r_symndx != STN_UNDEF
12289 && r_type != R_PPC64_NONE
12291 || h->elf.root.type == bfd_link_hash_defined
12292 || h->elf.root.type == bfd_link_hash_defweak)
12293 && (IS_PPC64_TLS_RELOC (r_type)
12294 != (sym_type == STT_TLS
12295 || (sym_type == STT_SECTION
12296 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
12299 && (r_type == R_PPC64_TLS
12300 || r_type == R_PPC64_TLSGD
12301 || r_type == R_PPC64_TLSLD))
12302 /* R_PPC64_TLS is OK against a symbol in the TOC. */
12305 info->callbacks->einfo
12306 (!IS_PPC64_TLS_RELOC (r_type)
12307 ? _("%P: %H: %s used with TLS symbol %s\n")
12308 : _("%P: %H: %s used with non-TLS symbol %s\n"),
12309 input_bfd, input_section, rel->r_offset,
12310 ppc64_elf_howto_table[r_type]->name,
12314 /* Ensure reloc mapping code below stays sane. */
12315 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
12316 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
12317 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
12318 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
12319 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
12320 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
12321 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
12322 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
12323 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
12324 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
12332 case R_PPC64_LO_DS_OPT:
12333 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12334 if ((insn & (0x3f << 26)) != 58u << 26)
12336 insn += (14u << 26) - (58u << 26);
12337 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12338 r_type = R_PPC64_TOC16_LO;
12339 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12342 case R_PPC64_TOC16:
12343 case R_PPC64_TOC16_LO:
12344 case R_PPC64_TOC16_DS:
12345 case R_PPC64_TOC16_LO_DS:
12347 /* Check for toc tls entries. */
12348 unsigned char *toc_tls;
12351 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12352 &local_syms, rel, input_bfd);
12358 tls_mask = *toc_tls;
12359 if (r_type == R_PPC64_TOC16_DS
12360 || r_type == R_PPC64_TOC16_LO_DS)
12363 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
12368 /* If we found a GD reloc pair, then we might be
12369 doing a GD->IE transition. */
12372 tls_gd = TLS_TPRELGD;
12373 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12376 else if (retval == 3)
12378 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12386 case R_PPC64_GOT_TPREL16_HI:
12387 case R_PPC64_GOT_TPREL16_HA:
12389 && (tls_mask & TLS_TPREL) == 0)
12391 rel->r_offset -= d_offset;
12392 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12393 r_type = R_PPC64_NONE;
12394 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12398 case R_PPC64_GOT_TPREL16_DS:
12399 case R_PPC64_GOT_TPREL16_LO_DS:
12401 && (tls_mask & TLS_TPREL) == 0)
12404 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12406 insn |= 0x3c0d0000; /* addis 0,13,0 */
12407 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12408 r_type = R_PPC64_TPREL16_HA;
12409 if (toc_symndx != 0)
12411 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
12412 rel->r_addend = toc_addend;
12413 /* We changed the symbol. Start over in order to
12414 get h, sym, sec etc. right. */
12419 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12425 && (tls_mask & TLS_TPREL) == 0)
12427 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
12428 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
12431 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
12432 /* Was PPC64_TLS which sits on insn boundary, now
12433 PPC64_TPREL16_LO which is at low-order half-word. */
12434 rel->r_offset += d_offset;
12435 r_type = R_PPC64_TPREL16_LO;
12436 if (toc_symndx != 0)
12438 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
12439 rel->r_addend = toc_addend;
12440 /* We changed the symbol. Start over in order to
12441 get h, sym, sec etc. right. */
12446 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12450 case R_PPC64_GOT_TLSGD16_HI:
12451 case R_PPC64_GOT_TLSGD16_HA:
12452 tls_gd = TLS_TPRELGD;
12453 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12457 case R_PPC64_GOT_TLSLD16_HI:
12458 case R_PPC64_GOT_TLSLD16_HA:
12459 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12462 if ((tls_mask & tls_gd) != 0)
12463 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12464 + R_PPC64_GOT_TPREL16_DS);
12467 rel->r_offset -= d_offset;
12468 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12469 r_type = R_PPC64_NONE;
12471 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12475 case R_PPC64_GOT_TLSGD16:
12476 case R_PPC64_GOT_TLSGD16_LO:
12477 tls_gd = TLS_TPRELGD;
12478 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12482 case R_PPC64_GOT_TLSLD16:
12483 case R_PPC64_GOT_TLSLD16_LO:
12484 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12486 unsigned int insn1, insn2, insn3;
12490 offset = (bfd_vma) -1;
12491 /* If not using the newer R_PPC64_TLSGD/LD to mark
12492 __tls_get_addr calls, we must trust that the call
12493 stays with its arg setup insns, ie. that the next
12494 reloc is the __tls_get_addr call associated with
12495 the current reloc. Edit both insns. */
12496 if (input_section->has_tls_get_addr_call
12497 && rel + 1 < relend
12498 && branch_reloc_hash_match (input_bfd, rel + 1,
12499 htab->tls_get_addr,
12500 htab->tls_get_addr_fd))
12501 offset = rel[1].r_offset;
12502 if ((tls_mask & tls_gd) != 0)
12505 insn1 = bfd_get_32 (output_bfd,
12506 contents + rel->r_offset - d_offset);
12507 insn1 &= (1 << 26) - (1 << 2);
12508 insn1 |= 58 << 26; /* ld */
12509 insn2 = 0x7c636a14; /* add 3,3,13 */
12510 if (offset != (bfd_vma) -1)
12511 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12512 if ((tls_mask & TLS_EXPLICIT) == 0)
12513 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12514 + R_PPC64_GOT_TPREL16_DS);
12516 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
12517 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12522 insn1 = 0x3c6d0000; /* addis 3,13,0 */
12523 insn2 = 0x38630000; /* addi 3,3,0 */
12526 /* Was an LD reloc. */
12528 sec = local_sections[toc_symndx];
12530 r_symndx < symtab_hdr->sh_info;
12532 if (local_sections[r_symndx] == sec)
12534 if (r_symndx >= symtab_hdr->sh_info)
12535 r_symndx = STN_UNDEF;
12536 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12537 if (r_symndx != STN_UNDEF)
12538 rel->r_addend -= (local_syms[r_symndx].st_value
12539 + sec->output_offset
12540 + sec->output_section->vma);
12542 else if (toc_symndx != 0)
12544 r_symndx = toc_symndx;
12545 rel->r_addend = toc_addend;
12547 r_type = R_PPC64_TPREL16_HA;
12548 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12549 if (offset != (bfd_vma) -1)
12551 rel[1].r_info = ELF64_R_INFO (r_symndx,
12552 R_PPC64_TPREL16_LO);
12553 rel[1].r_offset = offset + d_offset;
12554 rel[1].r_addend = rel->r_addend;
12557 bfd_put_32 (output_bfd, insn1,
12558 contents + rel->r_offset - d_offset);
12559 if (offset != (bfd_vma) -1)
12561 insn3 = bfd_get_32 (output_bfd,
12562 contents + offset + 4);
12564 || insn3 == CROR_151515 || insn3 == CROR_313131)
12566 rel[1].r_offset += 4;
12567 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12570 bfd_put_32 (output_bfd, insn2, contents + offset);
12572 if ((tls_mask & tls_gd) == 0
12573 && (tls_gd == 0 || toc_symndx != 0))
12575 /* We changed the symbol. Start over in order
12576 to get h, sym, sec etc. right. */
12583 case R_PPC64_TLSGD:
12584 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12586 unsigned int insn2, insn3;
12587 bfd_vma offset = rel->r_offset;
12589 if ((tls_mask & TLS_TPRELGD) != 0)
12592 r_type = R_PPC64_NONE;
12593 insn2 = 0x7c636a14; /* add 3,3,13 */
12598 if (toc_symndx != 0)
12600 r_symndx = toc_symndx;
12601 rel->r_addend = toc_addend;
12603 r_type = R_PPC64_TPREL16_LO;
12604 rel->r_offset = offset + d_offset;
12605 insn2 = 0x38630000; /* addi 3,3,0 */
12607 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12608 /* Zap the reloc on the _tls_get_addr call too. */
12609 BFD_ASSERT (offset == rel[1].r_offset);
12610 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12611 insn3 = bfd_get_32 (output_bfd,
12612 contents + offset + 4);
12614 || insn3 == CROR_151515 || insn3 == CROR_313131)
12616 rel->r_offset += 4;
12617 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12620 bfd_put_32 (output_bfd, insn2, contents + offset);
12621 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
12629 case R_PPC64_TLSLD:
12630 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12632 unsigned int insn2, insn3;
12633 bfd_vma offset = rel->r_offset;
12636 sec = local_sections[toc_symndx];
12638 r_symndx < symtab_hdr->sh_info;
12640 if (local_sections[r_symndx] == sec)
12642 if (r_symndx >= symtab_hdr->sh_info)
12643 r_symndx = STN_UNDEF;
12644 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12645 if (r_symndx != STN_UNDEF)
12646 rel->r_addend -= (local_syms[r_symndx].st_value
12647 + sec->output_offset
12648 + sec->output_section->vma);
12650 r_type = R_PPC64_TPREL16_LO;
12651 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12652 rel->r_offset = offset + d_offset;
12653 /* Zap the reloc on the _tls_get_addr call too. */
12654 BFD_ASSERT (offset == rel[1].r_offset);
12655 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12656 insn2 = 0x38630000; /* addi 3,3,0 */
12657 insn3 = bfd_get_32 (output_bfd,
12658 contents + offset + 4);
12660 || insn3 == CROR_151515 || insn3 == CROR_313131)
12662 rel->r_offset += 4;
12663 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12666 bfd_put_32 (output_bfd, insn2, contents + offset);
12672 case R_PPC64_DTPMOD64:
12673 if (rel + 1 < relend
12674 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
12675 && rel[1].r_offset == rel->r_offset + 8)
12677 if ((tls_mask & TLS_GD) == 0)
12679 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
12680 if ((tls_mask & TLS_TPRELGD) != 0)
12681 r_type = R_PPC64_TPREL64;
12684 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12685 r_type = R_PPC64_NONE;
12687 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12692 if ((tls_mask & TLS_LD) == 0)
12694 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12695 r_type = R_PPC64_NONE;
12696 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12701 case R_PPC64_TPREL64:
12702 if ((tls_mask & TLS_TPREL) == 0)
12704 r_type = R_PPC64_NONE;
12705 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12710 /* Handle other relocations that tweak non-addend part of insn. */
12712 max_br_offset = 1 << 25;
12713 addend = rel->r_addend;
12719 case R_PPC64_TOCSAVE:
12720 if (relocation + addend == (rel->r_offset
12721 + input_section->output_offset
12722 + input_section->output_section->vma)
12723 && tocsave_find (htab, NO_INSERT,
12724 &local_syms, rel, input_bfd))
12726 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
12728 || insn == CROR_151515 || insn == CROR_313131)
12729 bfd_put_32 (input_bfd, STD_R2_40R1,
12730 contents + rel->r_offset);
12734 /* Branch taken prediction relocations. */
12735 case R_PPC64_ADDR14_BRTAKEN:
12736 case R_PPC64_REL14_BRTAKEN:
12737 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
12740 /* Branch not taken prediction relocations. */
12741 case R_PPC64_ADDR14_BRNTAKEN:
12742 case R_PPC64_REL14_BRNTAKEN:
12743 insn |= bfd_get_32 (output_bfd,
12744 contents + rel->r_offset) & ~(0x01 << 21);
12747 case R_PPC64_REL14:
12748 max_br_offset = 1 << 15;
12751 case R_PPC64_REL24:
12752 /* Calls to functions with a different TOC, such as calls to
12753 shared objects, need to alter the TOC pointer. This is
12754 done using a linkage stub. A REL24 branching to these
12755 linkage stubs needs to be followed by a nop, as the nop
12756 will be replaced with an instruction to restore the TOC
12761 && h->oh->is_func_descriptor)
12762 fdh = ppc_follow_link (h->oh);
12763 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
12764 if (stub_entry != NULL
12765 && (stub_entry->stub_type == ppc_stub_plt_call
12766 || stub_entry->stub_type == ppc_stub_plt_call_r2save
12767 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
12768 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
12770 bfd_boolean can_plt_call = FALSE;
12772 if (rel->r_offset + 8 <= input_section->size)
12775 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
12777 || nop == CROR_151515 || nop == CROR_313131)
12780 && (h == htab->tls_get_addr_fd
12781 || h == htab->tls_get_addr)
12782 && !htab->no_tls_get_addr_opt)
12784 /* Special stub used, leave nop alone. */
12787 bfd_put_32 (input_bfd, LD_R2_40R1,
12788 contents + rel->r_offset + 4);
12789 can_plt_call = TRUE;
12795 if (stub_entry->stub_type == ppc_stub_plt_call
12796 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
12798 /* If this is a plain branch rather than a branch
12799 and link, don't require a nop. However, don't
12800 allow tail calls in a shared library as they
12801 will result in r2 being corrupted. */
12803 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
12804 if (info->executable && (br & 1) == 0)
12805 can_plt_call = TRUE;
12810 && strcmp (h->elf.root.root.string,
12811 ".__libc_start_main") == 0)
12813 /* Allow crt1 branch to go via a toc adjusting stub. */
12814 can_plt_call = TRUE;
12818 if (strcmp (input_section->output_section->name,
12820 || strcmp (input_section->output_section->name,
12822 info->callbacks->einfo
12823 (_("%P: %H: automatic multiple TOCs "
12824 "not supported using your crt files; "
12825 "recompile with -mminimal-toc or upgrade gcc\n"),
12826 input_bfd, input_section, rel->r_offset);
12828 info->callbacks->einfo
12829 (_("%P: %H: sibling call optimization to `%s' "
12830 "does not allow automatic multiple TOCs; "
12831 "recompile with -mminimal-toc or "
12832 "-fno-optimize-sibling-calls, "
12833 "or make `%s' extern\n"),
12834 input_bfd, input_section, rel->r_offset,
12837 bfd_set_error (bfd_error_bad_value);
12843 && (stub_entry->stub_type == ppc_stub_plt_call
12844 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
12845 unresolved_reloc = FALSE;
12848 if ((stub_entry == NULL
12849 || stub_entry->stub_type == ppc_stub_long_branch
12850 || stub_entry->stub_type == ppc_stub_plt_branch)
12851 && get_opd_info (sec) != NULL)
12853 /* The branch destination is the value of the opd entry. */
12854 bfd_vma off = (relocation + addend
12855 - sec->output_section->vma
12856 - sec->output_offset);
12857 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
12858 if (dest != (bfd_vma) -1)
12865 /* If the branch is out of reach we ought to have a long
12867 from = (rel->r_offset
12868 + input_section->output_offset
12869 + input_section->output_section->vma);
12871 if (stub_entry != NULL
12872 && (stub_entry->stub_type == ppc_stub_long_branch
12873 || stub_entry->stub_type == ppc_stub_plt_branch)
12874 && (r_type == R_PPC64_ADDR14_BRTAKEN
12875 || r_type == R_PPC64_ADDR14_BRNTAKEN
12876 || (relocation + addend - from + max_br_offset
12877 < 2 * max_br_offset)))
12878 /* Don't use the stub if this branch is in range. */
12881 if (stub_entry != NULL)
12883 /* Munge up the value and addend so that we call the stub
12884 rather than the procedure directly. */
12885 relocation = (stub_entry->stub_offset
12886 + stub_entry->stub_sec->output_offset
12887 + stub_entry->stub_sec->output_section->vma);
12890 if ((stub_entry->stub_type == ppc_stub_plt_call
12891 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
12892 && (ALWAYS_EMIT_R2SAVE
12893 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
12894 && rel + 1 < relend
12895 && rel[1].r_offset == rel->r_offset + 4
12896 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
12904 /* Set 'a' bit. This is 0b00010 in BO field for branch
12905 on CR(BI) insns (BO == 001at or 011at), and 0b01000
12906 for branch on CTR insns (BO == 1a00t or 1a01t). */
12907 if ((insn & (0x14 << 21)) == (0x04 << 21))
12908 insn |= 0x02 << 21;
12909 else if ((insn & (0x14 << 21)) == (0x10 << 21))
12910 insn |= 0x08 << 21;
12916 /* Invert 'y' bit if not the default. */
12917 if ((bfd_signed_vma) (relocation + addend - from) < 0)
12918 insn ^= 0x01 << 21;
12921 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
12924 /* NOP out calls to undefined weak functions.
12925 We can thus call a weak function without first
12926 checking whether the function is defined. */
12928 && h->elf.root.type == bfd_link_hash_undefweak
12929 && h->elf.dynindx == -1
12930 && r_type == R_PPC64_REL24
12934 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12940 /* Set `addend'. */
12945 info->callbacks->einfo
12946 (_("%P: %B: unknown relocation type %d for symbol %s\n"),
12947 input_bfd, (int) r_type, sym_name);
12949 bfd_set_error (bfd_error_bad_value);
12955 case R_PPC64_TLSGD:
12956 case R_PPC64_TLSLD:
12957 case R_PPC64_TOCSAVE:
12958 case R_PPC64_GNU_VTINHERIT:
12959 case R_PPC64_GNU_VTENTRY:
12962 /* GOT16 relocations. Like an ADDR16 using the symbol's
12963 address in the GOT as relocation value instead of the
12964 symbol's value itself. Also, create a GOT entry for the
12965 symbol and put the symbol value there. */
12966 case R_PPC64_GOT_TLSGD16:
12967 case R_PPC64_GOT_TLSGD16_LO:
12968 case R_PPC64_GOT_TLSGD16_HI:
12969 case R_PPC64_GOT_TLSGD16_HA:
12970 tls_type = TLS_TLS | TLS_GD;
12973 case R_PPC64_GOT_TLSLD16:
12974 case R_PPC64_GOT_TLSLD16_LO:
12975 case R_PPC64_GOT_TLSLD16_HI:
12976 case R_PPC64_GOT_TLSLD16_HA:
12977 tls_type = TLS_TLS | TLS_LD;
12980 case R_PPC64_GOT_TPREL16_DS:
12981 case R_PPC64_GOT_TPREL16_LO_DS:
12982 case R_PPC64_GOT_TPREL16_HI:
12983 case R_PPC64_GOT_TPREL16_HA:
12984 tls_type = TLS_TLS | TLS_TPREL;
12987 case R_PPC64_GOT_DTPREL16_DS:
12988 case R_PPC64_GOT_DTPREL16_LO_DS:
12989 case R_PPC64_GOT_DTPREL16_HI:
12990 case R_PPC64_GOT_DTPREL16_HA:
12991 tls_type = TLS_TLS | TLS_DTPREL;
12994 case R_PPC64_GOT16:
12995 case R_PPC64_GOT16_LO:
12996 case R_PPC64_GOT16_HI:
12997 case R_PPC64_GOT16_HA:
12998 case R_PPC64_GOT16_DS:
12999 case R_PPC64_GOT16_LO_DS:
13002 /* Relocation is to the entry for this symbol in the global
13007 unsigned long indx = 0;
13008 struct got_entry *ent;
13010 if (tls_type == (TLS_TLS | TLS_LD)
13012 || !h->elf.def_dynamic))
13013 ent = ppc64_tlsld_got (input_bfd);
13019 bfd_boolean dyn = htab->elf.dynamic_sections_created;
13020 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
13023 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
13024 /* This is actually a static link, or it is a
13025 -Bsymbolic link and the symbol is defined
13026 locally, or the symbol was forced to be local
13027 because of a version file. */
13031 indx = h->elf.dynindx;
13032 unresolved_reloc = FALSE;
13034 ent = h->elf.got.glist;
13038 if (local_got_ents == NULL)
13040 ent = local_got_ents[r_symndx];
13043 for (; ent != NULL; ent = ent->next)
13044 if (ent->addend == orig_addend
13045 && ent->owner == input_bfd
13046 && ent->tls_type == tls_type)
13052 if (ent->is_indirect)
13053 ent = ent->got.ent;
13054 offp = &ent->got.offset;
13055 got = ppc64_elf_tdata (ent->owner)->got;
13059 /* The offset must always be a multiple of 8. We use the
13060 least significant bit to record whether we have already
13061 processed this entry. */
13063 if ((off & 1) != 0)
13067 /* Generate relocs for the dynamic linker, except in
13068 the case of TLSLD where we'll use one entry per
13076 ? h->elf.type == STT_GNU_IFUNC
13077 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
13078 if ((info->shared || indx != 0)
13080 || (tls_type == (TLS_TLS | TLS_LD)
13081 && !h->elf.def_dynamic)
13082 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13083 || h->elf.root.type != bfd_link_hash_undefweak))
13084 relgot = ppc64_elf_tdata (ent->owner)->relgot;
13086 relgot = htab->reliplt;
13087 if (relgot != NULL)
13089 outrel.r_offset = (got->output_section->vma
13090 + got->output_offset
13092 outrel.r_addend = addend;
13093 if (tls_type & (TLS_LD | TLS_GD))
13095 outrel.r_addend = 0;
13096 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
13097 if (tls_type == (TLS_TLS | TLS_GD))
13099 loc = relgot->contents;
13100 loc += (relgot->reloc_count++
13101 * sizeof (Elf64_External_Rela));
13102 bfd_elf64_swap_reloca_out (output_bfd,
13104 outrel.r_offset += 8;
13105 outrel.r_addend = addend;
13107 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
13110 else if (tls_type == (TLS_TLS | TLS_DTPREL))
13111 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
13112 else if (tls_type == (TLS_TLS | TLS_TPREL))
13113 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
13114 else if (indx != 0)
13115 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
13119 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13121 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13123 /* Write the .got section contents for the sake
13125 loc = got->contents + off;
13126 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
13130 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
13132 outrel.r_addend += relocation;
13133 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
13134 outrel.r_addend -= htab->elf.tls_sec->vma;
13136 loc = relgot->contents;
13137 loc += (relgot->reloc_count++
13138 * sizeof (Elf64_External_Rela));
13139 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13142 /* Init the .got section contents here if we're not
13143 emitting a reloc. */
13146 relocation += addend;
13147 if (tls_type == (TLS_TLS | TLS_LD))
13149 else if (tls_type != 0)
13151 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
13152 if (tls_type == (TLS_TLS | TLS_TPREL))
13153 relocation += DTP_OFFSET - TP_OFFSET;
13155 if (tls_type == (TLS_TLS | TLS_GD))
13157 bfd_put_64 (output_bfd, relocation,
13158 got->contents + off + 8);
13163 bfd_put_64 (output_bfd, relocation,
13164 got->contents + off);
13168 if (off >= (bfd_vma) -2)
13171 relocation = got->output_section->vma + got->output_offset + off;
13172 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
13176 case R_PPC64_PLT16_HA:
13177 case R_PPC64_PLT16_HI:
13178 case R_PPC64_PLT16_LO:
13179 case R_PPC64_PLT32:
13180 case R_PPC64_PLT64:
13181 /* Relocation is to the entry for this symbol in the
13182 procedure linkage table. */
13184 /* Resolve a PLT reloc against a local symbol directly,
13185 without using the procedure linkage table. */
13189 /* It's possible that we didn't make a PLT entry for this
13190 symbol. This happens when statically linking PIC code,
13191 or when using -Bsymbolic. Go find a match if there is a
13193 if (htab->plt != NULL)
13195 struct plt_entry *ent;
13196 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
13197 if (ent->addend == orig_addend
13198 && ent->plt.offset != (bfd_vma) -1)
13200 relocation = (htab->plt->output_section->vma
13201 + htab->plt->output_offset
13202 + ent->plt.offset);
13203 unresolved_reloc = FALSE;
13209 /* Relocation value is TOC base. */
13210 relocation = TOCstart;
13211 if (r_symndx == STN_UNDEF)
13212 relocation += htab->stub_group[input_section->id].toc_off;
13213 else if (unresolved_reloc)
13215 else if (sec != NULL && sec->id <= htab->top_id)
13216 relocation += htab->stub_group[sec->id].toc_off;
13218 unresolved_reloc = TRUE;
13221 /* TOC16 relocs. We want the offset relative to the TOC base,
13222 which is the address of the start of the TOC plus 0x8000.
13223 The TOC consists of sections .got, .toc, .tocbss, and .plt,
13225 case R_PPC64_TOC16:
13226 case R_PPC64_TOC16_LO:
13227 case R_PPC64_TOC16_HI:
13228 case R_PPC64_TOC16_DS:
13229 case R_PPC64_TOC16_LO_DS:
13230 case R_PPC64_TOC16_HA:
13231 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
13234 /* Relocate against the beginning of the section. */
13235 case R_PPC64_SECTOFF:
13236 case R_PPC64_SECTOFF_LO:
13237 case R_PPC64_SECTOFF_HI:
13238 case R_PPC64_SECTOFF_DS:
13239 case R_PPC64_SECTOFF_LO_DS:
13240 case R_PPC64_SECTOFF_HA:
13242 addend -= sec->output_section->vma;
13245 case R_PPC64_REL16:
13246 case R_PPC64_REL16_LO:
13247 case R_PPC64_REL16_HI:
13248 case R_PPC64_REL16_HA:
13251 case R_PPC64_REL14:
13252 case R_PPC64_REL14_BRNTAKEN:
13253 case R_PPC64_REL14_BRTAKEN:
13254 case R_PPC64_REL24:
13257 case R_PPC64_TPREL16:
13258 case R_PPC64_TPREL16_LO:
13259 case R_PPC64_TPREL16_HI:
13260 case R_PPC64_TPREL16_HA:
13261 case R_PPC64_TPREL16_DS:
13262 case R_PPC64_TPREL16_LO_DS:
13263 case R_PPC64_TPREL16_HIGHER:
13264 case R_PPC64_TPREL16_HIGHERA:
13265 case R_PPC64_TPREL16_HIGHEST:
13266 case R_PPC64_TPREL16_HIGHESTA:
13268 && h->elf.root.type == bfd_link_hash_undefweak
13269 && h->elf.dynindx == -1)
13271 /* Make this relocation against an undefined weak symbol
13272 resolve to zero. This is really just a tweak, since
13273 code using weak externs ought to check that they are
13274 defined before using them. */
13275 bfd_byte *p = contents + rel->r_offset - d_offset;
13277 insn = bfd_get_32 (output_bfd, p);
13278 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
13280 bfd_put_32 (output_bfd, insn, p);
13283 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13285 /* The TPREL16 relocs shouldn't really be used in shared
13286 libs as they will result in DT_TEXTREL being set, but
13287 support them anyway. */
13291 case R_PPC64_DTPREL16:
13292 case R_PPC64_DTPREL16_LO:
13293 case R_PPC64_DTPREL16_HI:
13294 case R_PPC64_DTPREL16_HA:
13295 case R_PPC64_DTPREL16_DS:
13296 case R_PPC64_DTPREL16_LO_DS:
13297 case R_PPC64_DTPREL16_HIGHER:
13298 case R_PPC64_DTPREL16_HIGHERA:
13299 case R_PPC64_DTPREL16_HIGHEST:
13300 case R_PPC64_DTPREL16_HIGHESTA:
13301 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13304 case R_PPC64_DTPMOD64:
13309 case R_PPC64_TPREL64:
13310 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13313 case R_PPC64_DTPREL64:
13314 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13317 /* Relocations that may need to be propagated if this is a
13319 case R_PPC64_REL30:
13320 case R_PPC64_REL32:
13321 case R_PPC64_REL64:
13322 case R_PPC64_ADDR14:
13323 case R_PPC64_ADDR14_BRNTAKEN:
13324 case R_PPC64_ADDR14_BRTAKEN:
13325 case R_PPC64_ADDR16:
13326 case R_PPC64_ADDR16_DS:
13327 case R_PPC64_ADDR16_HA:
13328 case R_PPC64_ADDR16_HI:
13329 case R_PPC64_ADDR16_HIGHER:
13330 case R_PPC64_ADDR16_HIGHERA:
13331 case R_PPC64_ADDR16_HIGHEST:
13332 case R_PPC64_ADDR16_HIGHESTA:
13333 case R_PPC64_ADDR16_LO:
13334 case R_PPC64_ADDR16_LO_DS:
13335 case R_PPC64_ADDR24:
13336 case R_PPC64_ADDR32:
13337 case R_PPC64_ADDR64:
13338 case R_PPC64_UADDR16:
13339 case R_PPC64_UADDR32:
13340 case R_PPC64_UADDR64:
13342 if ((input_section->flags & SEC_ALLOC) == 0)
13345 if (NO_OPD_RELOCS && is_opd)
13350 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13351 || h->elf.root.type != bfd_link_hash_undefweak)
13352 && (must_be_dyn_reloc (info, r_type)
13353 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
13354 || (ELIMINATE_COPY_RELOCS
13357 && h->elf.dynindx != -1
13358 && !h->elf.non_got_ref
13359 && !h->elf.def_regular)
13362 ? h->elf.type == STT_GNU_IFUNC
13363 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
13365 bfd_boolean skip, relocate;
13369 /* When generating a dynamic object, these relocations
13370 are copied into the output file to be resolved at run
13376 out_off = _bfd_elf_section_offset (output_bfd, info,
13377 input_section, rel->r_offset);
13378 if (out_off == (bfd_vma) -1)
13380 else if (out_off == (bfd_vma) -2)
13381 skip = TRUE, relocate = TRUE;
13382 out_off += (input_section->output_section->vma
13383 + input_section->output_offset);
13384 outrel.r_offset = out_off;
13385 outrel.r_addend = rel->r_addend;
13387 /* Optimize unaligned reloc use. */
13388 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
13389 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
13390 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
13391 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
13392 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
13393 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
13394 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
13395 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
13396 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
13399 memset (&outrel, 0, sizeof outrel);
13400 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
13402 && r_type != R_PPC64_TOC)
13403 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
13406 /* This symbol is local, or marked to become local,
13407 or this is an opd section reloc which must point
13408 at a local function. */
13409 outrel.r_addend += relocation;
13410 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13412 if (is_opd && h != NULL)
13414 /* Lie about opd entries. This case occurs
13415 when building shared libraries and we
13416 reference a function in another shared
13417 lib. The same thing happens for a weak
13418 definition in an application that's
13419 overridden by a strong definition in a
13420 shared lib. (I believe this is a generic
13421 bug in binutils handling of weak syms.)
13422 In these cases we won't use the opd
13423 entry in this lib. */
13424 unresolved_reloc = FALSE;
13427 && r_type == R_PPC64_ADDR64
13429 ? h->elf.type == STT_GNU_IFUNC
13430 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
13431 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13434 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13436 /* We need to relocate .opd contents for ld.so.
13437 Prelink also wants simple and consistent rules
13438 for relocs. This make all RELATIVE relocs have
13439 *r_offset equal to r_addend. */
13448 ? h->elf.type == STT_GNU_IFUNC
13449 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13451 info->callbacks->einfo
13452 (_("%P: %H: relocation %s for indirect "
13453 "function %s unsupported\n"),
13454 input_bfd, input_section, rel->r_offset,
13455 ppc64_elf_howto_table[r_type]->name,
13459 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
13461 else if (sec == NULL || sec->owner == NULL)
13463 bfd_set_error (bfd_error_bad_value);
13470 osec = sec->output_section;
13471 indx = elf_section_data (osec)->dynindx;
13475 if ((osec->flags & SEC_READONLY) == 0
13476 && htab->elf.data_index_section != NULL)
13477 osec = htab->elf.data_index_section;
13479 osec = htab->elf.text_index_section;
13480 indx = elf_section_data (osec)->dynindx;
13482 BFD_ASSERT (indx != 0);
13484 /* We are turning this relocation into one
13485 against a section symbol, so subtract out
13486 the output section's address but not the
13487 offset of the input section in the output
13489 outrel.r_addend -= osec->vma;
13492 outrel.r_info = ELF64_R_INFO (indx, r_type);
13496 sreloc = elf_section_data (input_section)->sreloc;
13497 if (!htab->elf.dynamic_sections_created)
13498 sreloc = htab->reliplt;
13499 if (sreloc == NULL)
13502 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
13505 loc = sreloc->contents;
13506 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
13507 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13509 /* If this reloc is against an external symbol, it will
13510 be computed at runtime, so there's no need to do
13511 anything now. However, for the sake of prelink ensure
13512 that the section contents are a known value. */
13515 unresolved_reloc = FALSE;
13516 /* The value chosen here is quite arbitrary as ld.so
13517 ignores section contents except for the special
13518 case of .opd where the contents might be accessed
13519 before relocation. Choose zero, as that won't
13520 cause reloc overflow. */
13523 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
13524 to improve backward compatibility with older
13526 if (r_type == R_PPC64_ADDR64)
13527 addend = outrel.r_addend;
13528 /* Adjust pc_relative relocs to have zero in *r_offset. */
13529 else if (ppc64_elf_howto_table[r_type]->pc_relative)
13530 addend = (input_section->output_section->vma
13531 + input_section->output_offset
13538 case R_PPC64_GLOB_DAT:
13539 case R_PPC64_JMP_SLOT:
13540 case R_PPC64_JMP_IREL:
13541 case R_PPC64_RELATIVE:
13542 /* We shouldn't ever see these dynamic relocs in relocatable
13544 /* Fall through. */
13546 case R_PPC64_PLTGOT16:
13547 case R_PPC64_PLTGOT16_DS:
13548 case R_PPC64_PLTGOT16_HA:
13549 case R_PPC64_PLTGOT16_HI:
13550 case R_PPC64_PLTGOT16_LO:
13551 case R_PPC64_PLTGOT16_LO_DS:
13552 case R_PPC64_PLTREL32:
13553 case R_PPC64_PLTREL64:
13554 /* These ones haven't been implemented yet. */
13556 info->callbacks->einfo
13557 (_("%P: %B: relocation %s is not supported for symbol %s\n"),
13559 ppc64_elf_howto_table[r_type]->name, sym_name);
13561 bfd_set_error (bfd_error_invalid_operation);
13566 /* Multi-instruction sequences that access the TOC can be
13567 optimized, eg. addis ra,r2,0; addi rb,ra,x;
13568 to nop; addi rb,r2,x; */
13574 case R_PPC64_GOT_TLSLD16_HI:
13575 case R_PPC64_GOT_TLSGD16_HI:
13576 case R_PPC64_GOT_TPREL16_HI:
13577 case R_PPC64_GOT_DTPREL16_HI:
13578 case R_PPC64_GOT16_HI:
13579 case R_PPC64_TOC16_HI:
13580 /* These relocs would only be useful if building up an
13581 offset to later add to r2, perhaps in an indexed
13582 addressing mode instruction. Don't try to optimize.
13583 Unfortunately, the possibility of someone building up an
13584 offset like this or even with the HA relocs, means that
13585 we need to check the high insn when optimizing the low
13589 case R_PPC64_GOT_TLSLD16_HA:
13590 case R_PPC64_GOT_TLSGD16_HA:
13591 case R_PPC64_GOT_TPREL16_HA:
13592 case R_PPC64_GOT_DTPREL16_HA:
13593 case R_PPC64_GOT16_HA:
13594 case R_PPC64_TOC16_HA:
13595 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
13596 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
13598 bfd_byte *p = contents + (rel->r_offset & ~3);
13599 bfd_put_32 (input_bfd, NOP, p);
13603 case R_PPC64_GOT_TLSLD16_LO:
13604 case R_PPC64_GOT_TLSGD16_LO:
13605 case R_PPC64_GOT_TPREL16_LO_DS:
13606 case R_PPC64_GOT_DTPREL16_LO_DS:
13607 case R_PPC64_GOT16_LO:
13608 case R_PPC64_GOT16_LO_DS:
13609 case R_PPC64_TOC16_LO:
13610 case R_PPC64_TOC16_LO_DS:
13611 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
13612 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
13614 bfd_byte *p = contents + (rel->r_offset & ~3);
13615 insn = bfd_get_32 (input_bfd, p);
13616 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
13618 /* Transform addic to addi when we change reg. */
13619 insn &= ~((0x3f << 26) | (0x1f << 16));
13620 insn |= (14u << 26) | (2 << 16);
13624 insn &= ~(0x1f << 16);
13627 bfd_put_32 (input_bfd, insn, p);
13632 /* Do any further special processing. */
13638 case R_PPC64_ADDR16_HA:
13639 case R_PPC64_REL16_HA:
13640 case R_PPC64_ADDR16_HIGHERA:
13641 case R_PPC64_ADDR16_HIGHESTA:
13642 case R_PPC64_TOC16_HA:
13643 case R_PPC64_SECTOFF_HA:
13644 case R_PPC64_TPREL16_HA:
13645 case R_PPC64_DTPREL16_HA:
13646 case R_PPC64_TPREL16_HIGHER:
13647 case R_PPC64_TPREL16_HIGHERA:
13648 case R_PPC64_TPREL16_HIGHEST:
13649 case R_PPC64_TPREL16_HIGHESTA:
13650 case R_PPC64_DTPREL16_HIGHER:
13651 case R_PPC64_DTPREL16_HIGHERA:
13652 case R_PPC64_DTPREL16_HIGHEST:
13653 case R_PPC64_DTPREL16_HIGHESTA:
13654 /* It's just possible that this symbol is a weak symbol
13655 that's not actually defined anywhere. In that case,
13656 'sec' would be NULL, and we should leave the symbol
13657 alone (it will be set to zero elsewhere in the link). */
13662 case R_PPC64_GOT16_HA:
13663 case R_PPC64_PLTGOT16_HA:
13664 case R_PPC64_PLT16_HA:
13665 case R_PPC64_GOT_TLSGD16_HA:
13666 case R_PPC64_GOT_TLSLD16_HA:
13667 case R_PPC64_GOT_TPREL16_HA:
13668 case R_PPC64_GOT_DTPREL16_HA:
13669 /* Add 0x10000 if sign bit in 0:15 is set.
13670 Bits 0:15 are not used. */
13674 case R_PPC64_ADDR16_DS:
13675 case R_PPC64_ADDR16_LO_DS:
13676 case R_PPC64_GOT16_DS:
13677 case R_PPC64_GOT16_LO_DS:
13678 case R_PPC64_PLT16_LO_DS:
13679 case R_PPC64_SECTOFF_DS:
13680 case R_PPC64_SECTOFF_LO_DS:
13681 case R_PPC64_TOC16_DS:
13682 case R_PPC64_TOC16_LO_DS:
13683 case R_PPC64_PLTGOT16_DS:
13684 case R_PPC64_PLTGOT16_LO_DS:
13685 case R_PPC64_GOT_TPREL16_DS:
13686 case R_PPC64_GOT_TPREL16_LO_DS:
13687 case R_PPC64_GOT_DTPREL16_DS:
13688 case R_PPC64_GOT_DTPREL16_LO_DS:
13689 case R_PPC64_TPREL16_DS:
13690 case R_PPC64_TPREL16_LO_DS:
13691 case R_PPC64_DTPREL16_DS:
13692 case R_PPC64_DTPREL16_LO_DS:
13693 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
13695 /* If this reloc is against an lq insn, then the value must be
13696 a multiple of 16. This is somewhat of a hack, but the
13697 "correct" way to do this by defining _DQ forms of all the
13698 _DS relocs bloats all reloc switches in this file. It
13699 doesn't seem to make much sense to use any of these relocs
13700 in data, so testing the insn should be safe. */
13701 if ((insn & (0x3f << 26)) == (56u << 26))
13703 if (((relocation + addend) & mask) != 0)
13705 info->callbacks->einfo
13706 (_("%P: %H: error: %s not a multiple of %u\n"),
13707 input_bfd, input_section, rel->r_offset,
13708 ppc64_elf_howto_table[r_type]->name,
13710 bfd_set_error (bfd_error_bad_value);
13717 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13718 because such sections are not SEC_ALLOC and thus ld.so will
13719 not process them. */
13720 if (unresolved_reloc
13721 && !((input_section->flags & SEC_DEBUGGING) != 0
13722 && h->elf.def_dynamic)
13723 && _bfd_elf_section_offset (output_bfd, info, input_section,
13724 rel->r_offset) != (bfd_vma) -1)
13726 info->callbacks->einfo
13727 (_("%P: %H: unresolvable %s relocation against symbol `%s'\n"),
13728 input_bfd, input_section, rel->r_offset,
13729 ppc64_elf_howto_table[(int) r_type]->name,
13730 h->elf.root.root.string);
13734 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
13742 if (r != bfd_reloc_ok)
13744 if (sym_name == NULL)
13745 sym_name = "(null)";
13746 if (r == bfd_reloc_overflow)
13751 && h->elf.root.type == bfd_link_hash_undefweak
13752 && ppc64_elf_howto_table[r_type]->pc_relative)
13754 /* Assume this is a call protected by other code that
13755 detects the symbol is undefined. If this is the case,
13756 we can safely ignore the overflow. If not, the
13757 program is hosed anyway, and a little warning isn't
13763 if (!((*info->callbacks->reloc_overflow)
13764 (info, (h ? &h->elf.root : NULL), sym_name,
13765 ppc64_elf_howto_table[r_type]->name,
13766 orig_addend, input_bfd, input_section, rel->r_offset)))
13771 info->callbacks->einfo
13772 (_("%P: %H: %s reloc against `%s': error %d\n"),
13773 input_bfd, input_section, rel->r_offset,
13774 ppc64_elf_howto_table[r_type]->name,
13782 /* If we're emitting relocations, then shortly after this function
13783 returns, reloc offsets and addends for this section will be
13784 adjusted. Worse, reloc symbol indices will be for the output
13785 file rather than the input. Save a copy of the relocs for
13786 opd_entry_value. */
13787 if (is_opd && (info->emitrelocations || info->relocatable))
13790 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
13791 rel = bfd_alloc (input_bfd, amt);
13792 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
13793 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
13796 memcpy (rel, relocs, amt);
13801 /* Adjust the value of any local symbols in opd sections. */
13804 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
13805 const char *name ATTRIBUTE_UNUSED,
13806 Elf_Internal_Sym *elfsym,
13807 asection *input_sec,
13808 struct elf_link_hash_entry *h)
13810 struct _opd_sec_data *opd;
13817 opd = get_opd_info (input_sec);
13818 if (opd == NULL || opd->adjust == NULL)
13821 value = elfsym->st_value - input_sec->output_offset;
13822 if (!info->relocatable)
13823 value -= input_sec->output_section->vma;
13825 adjust = opd->adjust[value / 8];
13829 elfsym->st_value += adjust;
13833 /* Finish up dynamic symbol handling. We set the contents of various
13834 dynamic sections here. */
13837 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
13838 struct bfd_link_info *info,
13839 struct elf_link_hash_entry *h,
13840 Elf_Internal_Sym *sym)
13842 struct ppc_link_hash_table *htab;
13843 struct plt_entry *ent;
13844 Elf_Internal_Rela rela;
13847 htab = ppc_hash_table (info);
13851 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13852 if (ent->plt.offset != (bfd_vma) -1)
13854 /* This symbol has an entry in the procedure linkage
13855 table. Set it up. */
13856 if (!htab->elf.dynamic_sections_created
13857 || h->dynindx == -1)
13859 BFD_ASSERT (h->type == STT_GNU_IFUNC
13861 && (h->root.type == bfd_link_hash_defined
13862 || h->root.type == bfd_link_hash_defweak));
13863 rela.r_offset = (htab->iplt->output_section->vma
13864 + htab->iplt->output_offset
13865 + ent->plt.offset);
13866 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13867 rela.r_addend = (h->root.u.def.value
13868 + h->root.u.def.section->output_offset
13869 + h->root.u.def.section->output_section->vma
13871 loc = (htab->reliplt->contents
13872 + (htab->reliplt->reloc_count++
13873 * sizeof (Elf64_External_Rela)));
13877 rela.r_offset = (htab->plt->output_section->vma
13878 + htab->plt->output_offset
13879 + ent->plt.offset);
13880 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13881 rela.r_addend = ent->addend;
13882 loc = (htab->relplt->contents
13883 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
13884 / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
13886 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13891 /* This symbol needs a copy reloc. Set it up. */
13893 if (h->dynindx == -1
13894 || (h->root.type != bfd_link_hash_defined
13895 && h->root.type != bfd_link_hash_defweak)
13896 || htab->relbss == NULL)
13899 rela.r_offset = (h->root.u.def.value
13900 + h->root.u.def.section->output_section->vma
13901 + h->root.u.def.section->output_offset);
13902 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
13904 loc = htab->relbss->contents;
13905 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
13906 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13909 /* Mark some specially defined symbols as absolute. */
13910 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
13911 sym->st_shndx = SHN_ABS;
13916 /* Used to decide how to sort relocs in an optimal manner for the
13917 dynamic linker, before writing them out. */
13919 static enum elf_reloc_type_class
13920 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
13922 enum elf_ppc64_reloc_type r_type;
13924 r_type = ELF64_R_TYPE (rela->r_info);
13927 case R_PPC64_RELATIVE:
13928 return reloc_class_relative;
13929 case R_PPC64_JMP_SLOT:
13930 return reloc_class_plt;
13932 return reloc_class_copy;
13934 return reloc_class_normal;
13938 /* Finish up the dynamic sections. */
13941 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
13942 struct bfd_link_info *info)
13944 struct ppc_link_hash_table *htab;
13948 htab = ppc_hash_table (info);
13952 dynobj = htab->elf.dynobj;
13953 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
13955 if (htab->elf.dynamic_sections_created)
13957 Elf64_External_Dyn *dyncon, *dynconend;
13959 if (sdyn == NULL || htab->got == NULL)
13962 dyncon = (Elf64_External_Dyn *) sdyn->contents;
13963 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
13964 for (; dyncon < dynconend; dyncon++)
13966 Elf_Internal_Dyn dyn;
13969 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
13976 case DT_PPC64_GLINK:
13978 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13979 /* We stupidly defined DT_PPC64_GLINK to be the start
13980 of glink rather than the first entry point, which is
13981 what ld.so needs, and now have a bigger stub to
13982 support automatic multiple TOCs. */
13983 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
13987 s = bfd_get_section_by_name (output_bfd, ".opd");
13990 dyn.d_un.d_ptr = s->vma;
13993 case DT_PPC64_OPDSZ:
13994 s = bfd_get_section_by_name (output_bfd, ".opd");
13997 dyn.d_un.d_val = s->size;
14002 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14007 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14011 dyn.d_un.d_val = htab->relplt->size;
14015 /* Don't count procedure linkage table relocs in the
14016 overall reloc count. */
14020 dyn.d_un.d_val -= s->size;
14024 /* We may not be using the standard ELF linker script.
14025 If .rela.plt is the first .rela section, we adjust
14026 DT_RELA to not include it. */
14030 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
14032 dyn.d_un.d_ptr += s->size;
14036 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
14040 if (htab->got != NULL && htab->got->size != 0)
14042 /* Fill in the first entry in the global offset table.
14043 We use it to hold the link-time TOCbase. */
14044 bfd_put_64 (output_bfd,
14045 elf_gp (output_bfd) + TOC_BASE_OFF,
14046 htab->got->contents);
14048 /* Set .got entry size. */
14049 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
14052 if (htab->plt != NULL && htab->plt->size != 0)
14054 /* Set .plt entry size. */
14055 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
14059 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
14060 brlt ourselves if emitrelocations. */
14061 if (htab->brlt != NULL
14062 && htab->brlt->reloc_count != 0
14063 && !_bfd_elf_link_output_relocs (output_bfd,
14065 elf_section_data (htab->brlt)->rela.hdr,
14066 elf_section_data (htab->brlt)->relocs,
14070 if (htab->glink != NULL
14071 && htab->glink->reloc_count != 0
14072 && !_bfd_elf_link_output_relocs (output_bfd,
14074 elf_section_data (htab->glink)->rela.hdr,
14075 elf_section_data (htab->glink)->relocs,
14080 if (htab->glink_eh_frame != NULL
14081 && htab->glink_eh_frame->sec_info_type == ELF_INFO_TYPE_EH_FRAME
14082 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
14083 htab->glink_eh_frame,
14084 htab->glink_eh_frame->contents))
14087 /* We need to handle writing out multiple GOT sections ourselves,
14088 since we didn't add them to DYNOBJ. We know dynobj is the first
14090 while ((dynobj = dynobj->link_next) != NULL)
14094 if (!is_ppc64_elf (dynobj))
14097 s = ppc64_elf_tdata (dynobj)->got;
14100 && s->output_section != bfd_abs_section_ptr
14101 && !bfd_set_section_contents (output_bfd, s->output_section,
14102 s->contents, s->output_offset,
14105 s = ppc64_elf_tdata (dynobj)->relgot;
14108 && s->output_section != bfd_abs_section_ptr
14109 && !bfd_set_section_contents (output_bfd, s->output_section,
14110 s->contents, s->output_offset,
14118 #include "elf64-target.h"
14120 /* FreeBSD support */
14122 #undef TARGET_LITTLE_SYM
14123 #undef TARGET_LITTLE_NAME
14125 #undef TARGET_BIG_SYM
14126 #define TARGET_BIG_SYM bfd_elf64_powerpc_freebsd_vec
14127 #undef TARGET_BIG_NAME
14128 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
14131 #define ELF_OSABI ELFOSABI_FREEBSD
14134 #define elf64_bed elf64_powerpc_fbsd_bed
14136 #include "elf64-target.h"