1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2017 Free Software Foundation, Inc.
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
5 Largely rewritten by Alan Modra.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
24 /* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
34 #include "elf/ppc64.h"
35 #include "elf64-ppc.h"
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_vma opd_entry_value
57 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
59 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
60 #define TARGET_LITTLE_NAME "elf64-powerpcle"
61 #define TARGET_BIG_SYM powerpc_elf64_vec
62 #define TARGET_BIG_NAME "elf64-powerpc"
63 #define ELF_ARCH bfd_arch_powerpc
64 #define ELF_TARGET_ID PPC64_ELF_DATA
65 #define ELF_MACHINE_CODE EM_PPC64
66 #define ELF_MAXPAGESIZE 0x10000
67 #define ELF_COMMONPAGESIZE 0x10000
68 #define elf_info_to_howto ppc64_elf_info_to_howto
70 #define elf_backend_want_got_sym 0
71 #define elf_backend_want_plt_sym 0
72 #define elf_backend_plt_alignment 3
73 #define elf_backend_plt_not_loaded 1
74 #define elf_backend_got_header_size 8
75 #define elf_backend_want_dynrelro 1
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_dtrel_excludes_plt 1
80 #define elf_backend_default_execstack 0
82 #define bfd_elf64_mkobject ppc64_elf_mkobject
83 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
84 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
85 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
86 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
87 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
88 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
89 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
90 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
91 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
93 #define elf_backend_object_p ppc64_elf_object_p
94 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
95 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
96 #define elf_backend_write_core_note ppc64_elf_write_core_note
97 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
98 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
99 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
100 #define elf_backend_check_directives ppc64_elf_before_check_relocs
101 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
102 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
103 #define elf_backend_check_relocs ppc64_elf_check_relocs
104 #define elf_backend_gc_keep ppc64_elf_gc_keep
105 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
106 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
107 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
108 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
109 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
110 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
111 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
112 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
113 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
114 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
115 #define elf_backend_action_discarded ppc64_elf_action_discarded
116 #define elf_backend_relocate_section ppc64_elf_relocate_section
117 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
118 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
119 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
120 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
121 #define elf_backend_special_sections ppc64_elf_special_sections
122 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
123 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
125 /* The name of the dynamic interpreter. This is put in the .interp
127 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
129 /* The size in bytes of an entry in the procedure linkage table. */
130 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
132 /* The initial size of the plt reserved for the dynamic linker. */
133 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
135 /* Offsets to some stack save slots. */
137 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
138 /* This one is dodgy. ELFv2 does not have a linker word, so use the
139 CR save slot. Used only by optimised __tls_get_addr call stub,
140 relying on __tls_get_addr_opt not saving CR.. */
141 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
143 /* TOC base pointers offset from start of TOC. */
144 #define TOC_BASE_OFF 0x8000
145 /* TOC base alignment. */
146 #define TOC_BASE_ALIGN 256
148 /* Offset of tp and dtp pointers from start of TLS block. */
149 #define TP_OFFSET 0x7000
150 #define DTP_OFFSET 0x8000
152 /* .plt call stub instructions. The normal stub is like this, but
153 sometimes the .plt entry crosses a 64k boundary and we need to
154 insert an addi to adjust r11. */
155 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
156 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
157 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
158 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
159 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
160 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
161 #define BCTR 0x4e800420 /* bctr */
163 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
164 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
165 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
167 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
168 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
169 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
170 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
171 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
172 #define BNECTR 0x4ca20420 /* bnectr+ */
173 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
175 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
176 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
177 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
179 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
180 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
181 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
183 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
184 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
185 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
186 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
187 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
189 /* glink call stub instructions. We enter with the index in R0. */
190 #define GLINK_CALL_STUB_SIZE (16*4)
194 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
195 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
197 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
198 /* ld %2,(0b-1b)(%11) */
199 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
200 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
206 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
207 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
208 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
209 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
210 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
213 #define NOP 0x60000000
215 /* Some other nops. */
216 #define CROR_151515 0x4def7b82
217 #define CROR_313131 0x4ffffb82
219 /* .glink entries for the first 32k functions are two instructions. */
220 #define LI_R0_0 0x38000000 /* li %r0,0 */
221 #define B_DOT 0x48000000 /* b . */
223 /* After that, we need two instructions to load the index, followed by
225 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
226 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
228 /* Instructions used by the save and restore reg functions. */
229 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
230 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
231 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
232 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
233 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
234 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
235 #define LI_R12_0 0x39800000 /* li %r12,0 */
236 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
237 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
238 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
239 #define BLR 0x4e800020 /* blr */
241 /* Since .opd is an array of descriptors and each entry will end up
242 with identical R_PPC64_RELATIVE relocs, there is really no need to
243 propagate .opd relocs; The dynamic linker should be taught to
244 relocate .opd without reloc entries. */
245 #ifndef NO_OPD_RELOCS
246 #define NO_OPD_RELOCS 0
250 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
254 abiversion (bfd *abfd)
256 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
260 set_abiversion (bfd *abfd, int ver)
262 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
263 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
266 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
268 /* Relocation HOWTO's. */
269 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
271 static reloc_howto_type ppc64_elf_howto_raw[] = {
272 /* This reloc does nothing. */
273 HOWTO (R_PPC64_NONE, /* type */
275 3, /* size (0 = byte, 1 = short, 2 = long) */
277 FALSE, /* pc_relative */
279 complain_overflow_dont, /* complain_on_overflow */
280 bfd_elf_generic_reloc, /* special_function */
281 "R_PPC64_NONE", /* name */
282 FALSE, /* partial_inplace */
285 FALSE), /* pcrel_offset */
287 /* A standard 32 bit relocation. */
288 HOWTO (R_PPC64_ADDR32, /* type */
290 2, /* size (0 = byte, 1 = short, 2 = long) */
292 FALSE, /* pc_relative */
294 complain_overflow_bitfield, /* complain_on_overflow */
295 bfd_elf_generic_reloc, /* special_function */
296 "R_PPC64_ADDR32", /* name */
297 FALSE, /* partial_inplace */
299 0xffffffff, /* dst_mask */
300 FALSE), /* pcrel_offset */
302 /* An absolute 26 bit branch; the lower two bits must be zero.
303 FIXME: we don't check that, we just clear them. */
304 HOWTO (R_PPC64_ADDR24, /* type */
306 2, /* size (0 = byte, 1 = short, 2 = long) */
308 FALSE, /* pc_relative */
310 complain_overflow_bitfield, /* complain_on_overflow */
311 bfd_elf_generic_reloc, /* special_function */
312 "R_PPC64_ADDR24", /* name */
313 FALSE, /* partial_inplace */
315 0x03fffffc, /* dst_mask */
316 FALSE), /* pcrel_offset */
318 /* A standard 16 bit relocation. */
319 HOWTO (R_PPC64_ADDR16, /* type */
321 1, /* size (0 = byte, 1 = short, 2 = long) */
323 FALSE, /* pc_relative */
325 complain_overflow_bitfield, /* complain_on_overflow */
326 bfd_elf_generic_reloc, /* special_function */
327 "R_PPC64_ADDR16", /* name */
328 FALSE, /* partial_inplace */
330 0xffff, /* dst_mask */
331 FALSE), /* pcrel_offset */
333 /* A 16 bit relocation without overflow. */
334 HOWTO (R_PPC64_ADDR16_LO, /* type */
336 1, /* size (0 = byte, 1 = short, 2 = long) */
338 FALSE, /* pc_relative */
340 complain_overflow_dont,/* complain_on_overflow */
341 bfd_elf_generic_reloc, /* special_function */
342 "R_PPC64_ADDR16_LO", /* name */
343 FALSE, /* partial_inplace */
345 0xffff, /* dst_mask */
346 FALSE), /* pcrel_offset */
348 /* Bits 16-31 of an address. */
349 HOWTO (R_PPC64_ADDR16_HI, /* type */
351 1, /* size (0 = byte, 1 = short, 2 = long) */
353 FALSE, /* pc_relative */
355 complain_overflow_signed, /* complain_on_overflow */
356 bfd_elf_generic_reloc, /* special_function */
357 "R_PPC64_ADDR16_HI", /* name */
358 FALSE, /* partial_inplace */
360 0xffff, /* dst_mask */
361 FALSE), /* pcrel_offset */
363 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
364 bits, treated as a signed number, is negative. */
365 HOWTO (R_PPC64_ADDR16_HA, /* type */
367 1, /* size (0 = byte, 1 = short, 2 = long) */
369 FALSE, /* pc_relative */
371 complain_overflow_signed, /* complain_on_overflow */
372 ppc64_elf_ha_reloc, /* special_function */
373 "R_PPC64_ADDR16_HA", /* name */
374 FALSE, /* partial_inplace */
376 0xffff, /* dst_mask */
377 FALSE), /* pcrel_offset */
379 /* An absolute 16 bit branch; the lower two bits must be zero.
380 FIXME: we don't check that, we just clear them. */
381 HOWTO (R_PPC64_ADDR14, /* type */
383 2, /* size (0 = byte, 1 = short, 2 = long) */
385 FALSE, /* pc_relative */
387 complain_overflow_signed, /* complain_on_overflow */
388 ppc64_elf_branch_reloc, /* special_function */
389 "R_PPC64_ADDR14", /* name */
390 FALSE, /* partial_inplace */
392 0x0000fffc, /* dst_mask */
393 FALSE), /* pcrel_offset */
395 /* An absolute 16 bit branch, for which bit 10 should be set to
396 indicate that the branch is expected to be taken. The lower two
397 bits must be zero. */
398 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
400 2, /* size (0 = byte, 1 = short, 2 = long) */
402 FALSE, /* pc_relative */
404 complain_overflow_signed, /* complain_on_overflow */
405 ppc64_elf_brtaken_reloc, /* special_function */
406 "R_PPC64_ADDR14_BRTAKEN",/* name */
407 FALSE, /* partial_inplace */
409 0x0000fffc, /* dst_mask */
410 FALSE), /* pcrel_offset */
412 /* An absolute 16 bit branch, for which bit 10 should be set to
413 indicate that the branch is not expected to be taken. The lower
414 two bits must be zero. */
415 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
417 2, /* size (0 = byte, 1 = short, 2 = long) */
419 FALSE, /* pc_relative */
421 complain_overflow_signed, /* complain_on_overflow */
422 ppc64_elf_brtaken_reloc, /* special_function */
423 "R_PPC64_ADDR14_BRNTAKEN",/* name */
424 FALSE, /* partial_inplace */
426 0x0000fffc, /* dst_mask */
427 FALSE), /* pcrel_offset */
429 /* A relative 26 bit branch; the lower two bits must be zero. */
430 HOWTO (R_PPC64_REL24, /* type */
432 2, /* size (0 = byte, 1 = short, 2 = long) */
434 TRUE, /* pc_relative */
436 complain_overflow_signed, /* complain_on_overflow */
437 ppc64_elf_branch_reloc, /* special_function */
438 "R_PPC64_REL24", /* name */
439 FALSE, /* partial_inplace */
441 0x03fffffc, /* dst_mask */
442 TRUE), /* pcrel_offset */
444 /* A relative 16 bit branch; the lower two bits must be zero. */
445 HOWTO (R_PPC64_REL14, /* type */
447 2, /* size (0 = byte, 1 = short, 2 = long) */
449 TRUE, /* pc_relative */
451 complain_overflow_signed, /* complain_on_overflow */
452 ppc64_elf_branch_reloc, /* special_function */
453 "R_PPC64_REL14", /* name */
454 FALSE, /* partial_inplace */
456 0x0000fffc, /* dst_mask */
457 TRUE), /* pcrel_offset */
459 /* A relative 16 bit branch. Bit 10 should be set to indicate that
460 the branch is expected to be taken. The lower two bits must be
462 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
464 2, /* size (0 = byte, 1 = short, 2 = long) */
466 TRUE, /* pc_relative */
468 complain_overflow_signed, /* complain_on_overflow */
469 ppc64_elf_brtaken_reloc, /* special_function */
470 "R_PPC64_REL14_BRTAKEN", /* name */
471 FALSE, /* partial_inplace */
473 0x0000fffc, /* dst_mask */
474 TRUE), /* pcrel_offset */
476 /* A relative 16 bit branch. Bit 10 should be set to indicate that
477 the branch is not expected to be taken. The lower two bits must
479 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
481 2, /* size (0 = byte, 1 = short, 2 = long) */
483 TRUE, /* pc_relative */
485 complain_overflow_signed, /* complain_on_overflow */
486 ppc64_elf_brtaken_reloc, /* special_function */
487 "R_PPC64_REL14_BRNTAKEN",/* name */
488 FALSE, /* partial_inplace */
490 0x0000fffc, /* dst_mask */
491 TRUE), /* pcrel_offset */
493 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
495 HOWTO (R_PPC64_GOT16, /* type */
497 1, /* size (0 = byte, 1 = short, 2 = long) */
499 FALSE, /* pc_relative */
501 complain_overflow_signed, /* complain_on_overflow */
502 ppc64_elf_unhandled_reloc, /* special_function */
503 "R_PPC64_GOT16", /* name */
504 FALSE, /* partial_inplace */
506 0xffff, /* dst_mask */
507 FALSE), /* pcrel_offset */
509 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
511 HOWTO (R_PPC64_GOT16_LO, /* type */
513 1, /* size (0 = byte, 1 = short, 2 = long) */
515 FALSE, /* pc_relative */
517 complain_overflow_dont, /* complain_on_overflow */
518 ppc64_elf_unhandled_reloc, /* special_function */
519 "R_PPC64_GOT16_LO", /* name */
520 FALSE, /* partial_inplace */
522 0xffff, /* dst_mask */
523 FALSE), /* pcrel_offset */
525 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
527 HOWTO (R_PPC64_GOT16_HI, /* type */
529 1, /* size (0 = byte, 1 = short, 2 = long) */
531 FALSE, /* pc_relative */
533 complain_overflow_signed,/* complain_on_overflow */
534 ppc64_elf_unhandled_reloc, /* special_function */
535 "R_PPC64_GOT16_HI", /* name */
536 FALSE, /* partial_inplace */
538 0xffff, /* dst_mask */
539 FALSE), /* pcrel_offset */
541 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
543 HOWTO (R_PPC64_GOT16_HA, /* type */
545 1, /* size (0 = byte, 1 = short, 2 = long) */
547 FALSE, /* pc_relative */
549 complain_overflow_signed,/* complain_on_overflow */
550 ppc64_elf_unhandled_reloc, /* special_function */
551 "R_PPC64_GOT16_HA", /* name */
552 FALSE, /* partial_inplace */
554 0xffff, /* dst_mask */
555 FALSE), /* pcrel_offset */
557 /* This is used only by the dynamic linker. The symbol should exist
558 both in the object being run and in some shared library. The
559 dynamic linker copies the data addressed by the symbol from the
560 shared library into the object, because the object being
561 run has to have the data at some particular address. */
562 HOWTO (R_PPC64_COPY, /* type */
564 0, /* this one is variable size */
566 FALSE, /* pc_relative */
568 complain_overflow_dont, /* complain_on_overflow */
569 ppc64_elf_unhandled_reloc, /* special_function */
570 "R_PPC64_COPY", /* name */
571 FALSE, /* partial_inplace */
574 FALSE), /* pcrel_offset */
576 /* Like R_PPC64_ADDR64, but used when setting global offset table
578 HOWTO (R_PPC64_GLOB_DAT, /* type */
580 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
582 FALSE, /* pc_relative */
584 complain_overflow_dont, /* complain_on_overflow */
585 ppc64_elf_unhandled_reloc, /* special_function */
586 "R_PPC64_GLOB_DAT", /* name */
587 FALSE, /* partial_inplace */
589 ONES (64), /* dst_mask */
590 FALSE), /* pcrel_offset */
592 /* Created by the link editor. Marks a procedure linkage table
593 entry for a symbol. */
594 HOWTO (R_PPC64_JMP_SLOT, /* type */
596 0, /* size (0 = byte, 1 = short, 2 = long) */
598 FALSE, /* pc_relative */
600 complain_overflow_dont, /* complain_on_overflow */
601 ppc64_elf_unhandled_reloc, /* special_function */
602 "R_PPC64_JMP_SLOT", /* name */
603 FALSE, /* partial_inplace */
606 FALSE), /* pcrel_offset */
608 /* Used only by the dynamic linker. When the object is run, this
609 doubleword64 is set to the load address of the object, plus the
611 HOWTO (R_PPC64_RELATIVE, /* type */
613 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
615 FALSE, /* pc_relative */
617 complain_overflow_dont, /* complain_on_overflow */
618 bfd_elf_generic_reloc, /* special_function */
619 "R_PPC64_RELATIVE", /* name */
620 FALSE, /* partial_inplace */
622 ONES (64), /* dst_mask */
623 FALSE), /* pcrel_offset */
625 /* Like R_PPC64_ADDR32, but may be unaligned. */
626 HOWTO (R_PPC64_UADDR32, /* type */
628 2, /* size (0 = byte, 1 = short, 2 = long) */
630 FALSE, /* pc_relative */
632 complain_overflow_bitfield, /* complain_on_overflow */
633 bfd_elf_generic_reloc, /* special_function */
634 "R_PPC64_UADDR32", /* name */
635 FALSE, /* partial_inplace */
637 0xffffffff, /* dst_mask */
638 FALSE), /* pcrel_offset */
640 /* Like R_PPC64_ADDR16, but may be unaligned. */
641 HOWTO (R_PPC64_UADDR16, /* type */
643 1, /* size (0 = byte, 1 = short, 2 = long) */
645 FALSE, /* pc_relative */
647 complain_overflow_bitfield, /* complain_on_overflow */
648 bfd_elf_generic_reloc, /* special_function */
649 "R_PPC64_UADDR16", /* name */
650 FALSE, /* partial_inplace */
652 0xffff, /* dst_mask */
653 FALSE), /* pcrel_offset */
655 /* 32-bit PC relative. */
656 HOWTO (R_PPC64_REL32, /* type */
658 2, /* size (0 = byte, 1 = short, 2 = long) */
660 TRUE, /* pc_relative */
662 complain_overflow_signed, /* complain_on_overflow */
663 bfd_elf_generic_reloc, /* special_function */
664 "R_PPC64_REL32", /* name */
665 FALSE, /* partial_inplace */
667 0xffffffff, /* dst_mask */
668 TRUE), /* pcrel_offset */
670 /* 32-bit relocation to the symbol's procedure linkage table. */
671 HOWTO (R_PPC64_PLT32, /* type */
673 2, /* size (0 = byte, 1 = short, 2 = long) */
675 FALSE, /* pc_relative */
677 complain_overflow_bitfield, /* complain_on_overflow */
678 ppc64_elf_unhandled_reloc, /* special_function */
679 "R_PPC64_PLT32", /* name */
680 FALSE, /* partial_inplace */
682 0xffffffff, /* dst_mask */
683 FALSE), /* pcrel_offset */
685 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
686 FIXME: R_PPC64_PLTREL32 not supported. */
687 HOWTO (R_PPC64_PLTREL32, /* type */
689 2, /* size (0 = byte, 1 = short, 2 = long) */
691 TRUE, /* pc_relative */
693 complain_overflow_signed, /* complain_on_overflow */
694 ppc64_elf_unhandled_reloc, /* special_function */
695 "R_PPC64_PLTREL32", /* name */
696 FALSE, /* partial_inplace */
698 0xffffffff, /* dst_mask */
699 TRUE), /* pcrel_offset */
701 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
703 HOWTO (R_PPC64_PLT16_LO, /* type */
705 1, /* size (0 = byte, 1 = short, 2 = long) */
707 FALSE, /* pc_relative */
709 complain_overflow_dont, /* complain_on_overflow */
710 ppc64_elf_unhandled_reloc, /* special_function */
711 "R_PPC64_PLT16_LO", /* name */
712 FALSE, /* partial_inplace */
714 0xffff, /* dst_mask */
715 FALSE), /* pcrel_offset */
717 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
719 HOWTO (R_PPC64_PLT16_HI, /* type */
721 1, /* size (0 = byte, 1 = short, 2 = long) */
723 FALSE, /* pc_relative */
725 complain_overflow_signed, /* complain_on_overflow */
726 ppc64_elf_unhandled_reloc, /* special_function */
727 "R_PPC64_PLT16_HI", /* name */
728 FALSE, /* partial_inplace */
730 0xffff, /* dst_mask */
731 FALSE), /* pcrel_offset */
733 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
735 HOWTO (R_PPC64_PLT16_HA, /* type */
737 1, /* size (0 = byte, 1 = short, 2 = long) */
739 FALSE, /* pc_relative */
741 complain_overflow_signed, /* complain_on_overflow */
742 ppc64_elf_unhandled_reloc, /* special_function */
743 "R_PPC64_PLT16_HA", /* name */
744 FALSE, /* partial_inplace */
746 0xffff, /* dst_mask */
747 FALSE), /* pcrel_offset */
749 /* 16-bit section relative relocation. */
750 HOWTO (R_PPC64_SECTOFF, /* type */
752 1, /* size (0 = byte, 1 = short, 2 = long) */
754 FALSE, /* pc_relative */
756 complain_overflow_signed, /* complain_on_overflow */
757 ppc64_elf_sectoff_reloc, /* special_function */
758 "R_PPC64_SECTOFF", /* name */
759 FALSE, /* partial_inplace */
761 0xffff, /* dst_mask */
762 FALSE), /* pcrel_offset */
764 /* Like R_PPC64_SECTOFF, but no overflow warning. */
765 HOWTO (R_PPC64_SECTOFF_LO, /* type */
767 1, /* size (0 = byte, 1 = short, 2 = long) */
769 FALSE, /* pc_relative */
771 complain_overflow_dont, /* complain_on_overflow */
772 ppc64_elf_sectoff_reloc, /* special_function */
773 "R_PPC64_SECTOFF_LO", /* name */
774 FALSE, /* partial_inplace */
776 0xffff, /* dst_mask */
777 FALSE), /* pcrel_offset */
779 /* 16-bit upper half section relative relocation. */
780 HOWTO (R_PPC64_SECTOFF_HI, /* type */
782 1, /* size (0 = byte, 1 = short, 2 = long) */
784 FALSE, /* pc_relative */
786 complain_overflow_signed, /* complain_on_overflow */
787 ppc64_elf_sectoff_reloc, /* special_function */
788 "R_PPC64_SECTOFF_HI", /* name */
789 FALSE, /* partial_inplace */
791 0xffff, /* dst_mask */
792 FALSE), /* pcrel_offset */
794 /* 16-bit upper half adjusted section relative relocation. */
795 HOWTO (R_PPC64_SECTOFF_HA, /* type */
797 1, /* size (0 = byte, 1 = short, 2 = long) */
799 FALSE, /* pc_relative */
801 complain_overflow_signed, /* complain_on_overflow */
802 ppc64_elf_sectoff_ha_reloc, /* special_function */
803 "R_PPC64_SECTOFF_HA", /* name */
804 FALSE, /* partial_inplace */
806 0xffff, /* dst_mask */
807 FALSE), /* pcrel_offset */
809 /* Like R_PPC64_REL24 without touching the two least significant bits. */
810 HOWTO (R_PPC64_REL30, /* type */
812 2, /* size (0 = byte, 1 = short, 2 = long) */
814 TRUE, /* pc_relative */
816 complain_overflow_dont, /* complain_on_overflow */
817 bfd_elf_generic_reloc, /* special_function */
818 "R_PPC64_REL30", /* name */
819 FALSE, /* partial_inplace */
821 0xfffffffc, /* dst_mask */
822 TRUE), /* pcrel_offset */
824 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
826 /* A standard 64-bit relocation. */
827 HOWTO (R_PPC64_ADDR64, /* type */
829 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
831 FALSE, /* pc_relative */
833 complain_overflow_dont, /* complain_on_overflow */
834 bfd_elf_generic_reloc, /* special_function */
835 "R_PPC64_ADDR64", /* name */
836 FALSE, /* partial_inplace */
838 ONES (64), /* dst_mask */
839 FALSE), /* pcrel_offset */
841 /* The bits 32-47 of an address. */
842 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
844 1, /* size (0 = byte, 1 = short, 2 = long) */
846 FALSE, /* pc_relative */
848 complain_overflow_dont, /* complain_on_overflow */
849 bfd_elf_generic_reloc, /* special_function */
850 "R_PPC64_ADDR16_HIGHER", /* name */
851 FALSE, /* partial_inplace */
853 0xffff, /* dst_mask */
854 FALSE), /* pcrel_offset */
856 /* The bits 32-47 of an address, plus 1 if the contents of the low
857 16 bits, treated as a signed number, is negative. */
858 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
860 1, /* size (0 = byte, 1 = short, 2 = long) */
862 FALSE, /* pc_relative */
864 complain_overflow_dont, /* complain_on_overflow */
865 ppc64_elf_ha_reloc, /* special_function */
866 "R_PPC64_ADDR16_HIGHERA", /* name */
867 FALSE, /* partial_inplace */
869 0xffff, /* dst_mask */
870 FALSE), /* pcrel_offset */
872 /* The bits 48-63 of an address. */
873 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
875 1, /* size (0 = byte, 1 = short, 2 = long) */
877 FALSE, /* pc_relative */
879 complain_overflow_dont, /* complain_on_overflow */
880 bfd_elf_generic_reloc, /* special_function */
881 "R_PPC64_ADDR16_HIGHEST", /* name */
882 FALSE, /* partial_inplace */
884 0xffff, /* dst_mask */
885 FALSE), /* pcrel_offset */
887 /* The bits 48-63 of an address, plus 1 if the contents of the low
888 16 bits, treated as a signed number, is negative. */
889 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
891 1, /* size (0 = byte, 1 = short, 2 = long) */
893 FALSE, /* pc_relative */
895 complain_overflow_dont, /* complain_on_overflow */
896 ppc64_elf_ha_reloc, /* special_function */
897 "R_PPC64_ADDR16_HIGHESTA", /* name */
898 FALSE, /* partial_inplace */
900 0xffff, /* dst_mask */
901 FALSE), /* pcrel_offset */
903 /* Like ADDR64, but may be unaligned. */
904 HOWTO (R_PPC64_UADDR64, /* type */
906 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
908 FALSE, /* pc_relative */
910 complain_overflow_dont, /* complain_on_overflow */
911 bfd_elf_generic_reloc, /* special_function */
912 "R_PPC64_UADDR64", /* name */
913 FALSE, /* partial_inplace */
915 ONES (64), /* dst_mask */
916 FALSE), /* pcrel_offset */
918 /* 64-bit relative relocation. */
919 HOWTO (R_PPC64_REL64, /* type */
921 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
923 TRUE, /* pc_relative */
925 complain_overflow_dont, /* complain_on_overflow */
926 bfd_elf_generic_reloc, /* special_function */
927 "R_PPC64_REL64", /* name */
928 FALSE, /* partial_inplace */
930 ONES (64), /* dst_mask */
931 TRUE), /* pcrel_offset */
933 /* 64-bit relocation to the symbol's procedure linkage table. */
934 HOWTO (R_PPC64_PLT64, /* type */
936 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
938 FALSE, /* pc_relative */
940 complain_overflow_dont, /* complain_on_overflow */
941 ppc64_elf_unhandled_reloc, /* special_function */
942 "R_PPC64_PLT64", /* name */
943 FALSE, /* partial_inplace */
945 ONES (64), /* dst_mask */
946 FALSE), /* pcrel_offset */
948 /* 64-bit PC relative relocation to the symbol's procedure linkage
950 /* FIXME: R_PPC64_PLTREL64 not supported. */
951 HOWTO (R_PPC64_PLTREL64, /* type */
953 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
955 TRUE, /* pc_relative */
957 complain_overflow_dont, /* complain_on_overflow */
958 ppc64_elf_unhandled_reloc, /* special_function */
959 "R_PPC64_PLTREL64", /* name */
960 FALSE, /* partial_inplace */
962 ONES (64), /* dst_mask */
963 TRUE), /* pcrel_offset */
965 /* 16 bit TOC-relative relocation. */
967 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
968 HOWTO (R_PPC64_TOC16, /* type */
970 1, /* size (0 = byte, 1 = short, 2 = long) */
972 FALSE, /* pc_relative */
974 complain_overflow_signed, /* complain_on_overflow */
975 ppc64_elf_toc_reloc, /* special_function */
976 "R_PPC64_TOC16", /* name */
977 FALSE, /* partial_inplace */
979 0xffff, /* dst_mask */
980 FALSE), /* pcrel_offset */
982 /* 16 bit TOC-relative relocation without overflow. */
984 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
985 HOWTO (R_PPC64_TOC16_LO, /* type */
987 1, /* size (0 = byte, 1 = short, 2 = long) */
989 FALSE, /* pc_relative */
991 complain_overflow_dont, /* complain_on_overflow */
992 ppc64_elf_toc_reloc, /* special_function */
993 "R_PPC64_TOC16_LO", /* name */
994 FALSE, /* partial_inplace */
996 0xffff, /* dst_mask */
997 FALSE), /* pcrel_offset */
999 /* 16 bit TOC-relative relocation, high 16 bits. */
1001 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
1002 HOWTO (R_PPC64_TOC16_HI, /* type */
1003 16, /* rightshift */
1004 1, /* size (0 = byte, 1 = short, 2 = long) */
1006 FALSE, /* pc_relative */
1008 complain_overflow_signed, /* complain_on_overflow */
1009 ppc64_elf_toc_reloc, /* special_function */
1010 "R_PPC64_TOC16_HI", /* name */
1011 FALSE, /* partial_inplace */
1013 0xffff, /* dst_mask */
1014 FALSE), /* pcrel_offset */
1016 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1017 contents of the low 16 bits, treated as a signed number, is
1020 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1021 HOWTO (R_PPC64_TOC16_HA, /* type */
1022 16, /* rightshift */
1023 1, /* size (0 = byte, 1 = short, 2 = long) */
1025 FALSE, /* pc_relative */
1027 complain_overflow_signed, /* complain_on_overflow */
1028 ppc64_elf_toc_ha_reloc, /* special_function */
1029 "R_PPC64_TOC16_HA", /* name */
1030 FALSE, /* partial_inplace */
1032 0xffff, /* dst_mask */
1033 FALSE), /* pcrel_offset */
1035 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1037 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1038 HOWTO (R_PPC64_TOC, /* type */
1040 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1042 FALSE, /* pc_relative */
1044 complain_overflow_dont, /* complain_on_overflow */
1045 ppc64_elf_toc64_reloc, /* special_function */
1046 "R_PPC64_TOC", /* name */
1047 FALSE, /* partial_inplace */
1049 ONES (64), /* dst_mask */
1050 FALSE), /* pcrel_offset */
1052 /* Like R_PPC64_GOT16, but also informs the link editor that the
1053 value to relocate may (!) refer to a PLT entry which the link
1054 editor (a) may replace with the symbol value. If the link editor
1055 is unable to fully resolve the symbol, it may (b) create a PLT
1056 entry and store the address to the new PLT entry in the GOT.
1057 This permits lazy resolution of function symbols at run time.
1058 The link editor may also skip all of this and just (c) emit a
1059 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1060 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1061 HOWTO (R_PPC64_PLTGOT16, /* type */
1063 1, /* size (0 = byte, 1 = short, 2 = long) */
1065 FALSE, /* pc_relative */
1067 complain_overflow_signed, /* complain_on_overflow */
1068 ppc64_elf_unhandled_reloc, /* special_function */
1069 "R_PPC64_PLTGOT16", /* name */
1070 FALSE, /* partial_inplace */
1072 0xffff, /* dst_mask */
1073 FALSE), /* pcrel_offset */
1075 /* Like R_PPC64_PLTGOT16, but without overflow. */
1076 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1077 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1079 1, /* size (0 = byte, 1 = short, 2 = long) */
1081 FALSE, /* pc_relative */
1083 complain_overflow_dont, /* complain_on_overflow */
1084 ppc64_elf_unhandled_reloc, /* special_function */
1085 "R_PPC64_PLTGOT16_LO", /* name */
1086 FALSE, /* partial_inplace */
1088 0xffff, /* dst_mask */
1089 FALSE), /* pcrel_offset */
1091 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1092 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1093 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1094 16, /* rightshift */
1095 1, /* size (0 = byte, 1 = short, 2 = long) */
1097 FALSE, /* pc_relative */
1099 complain_overflow_signed, /* complain_on_overflow */
1100 ppc64_elf_unhandled_reloc, /* special_function */
1101 "R_PPC64_PLTGOT16_HI", /* name */
1102 FALSE, /* partial_inplace */
1104 0xffff, /* dst_mask */
1105 FALSE), /* pcrel_offset */
1107 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1108 1 if the contents of the low 16 bits, treated as a signed number,
1110 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1111 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1112 16, /* rightshift */
1113 1, /* size (0 = byte, 1 = short, 2 = long) */
1115 FALSE, /* pc_relative */
1117 complain_overflow_signed, /* complain_on_overflow */
1118 ppc64_elf_unhandled_reloc, /* special_function */
1119 "R_PPC64_PLTGOT16_HA", /* name */
1120 FALSE, /* partial_inplace */
1122 0xffff, /* dst_mask */
1123 FALSE), /* pcrel_offset */
1125 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1126 HOWTO (R_PPC64_ADDR16_DS, /* type */
1128 1, /* size (0 = byte, 1 = short, 2 = long) */
1130 FALSE, /* pc_relative */
1132 complain_overflow_signed, /* complain_on_overflow */
1133 bfd_elf_generic_reloc, /* special_function */
1134 "R_PPC64_ADDR16_DS", /* name */
1135 FALSE, /* partial_inplace */
1137 0xfffc, /* dst_mask */
1138 FALSE), /* pcrel_offset */
1140 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1141 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1143 1, /* size (0 = byte, 1 = short, 2 = long) */
1145 FALSE, /* pc_relative */
1147 complain_overflow_dont,/* complain_on_overflow */
1148 bfd_elf_generic_reloc, /* special_function */
1149 "R_PPC64_ADDR16_LO_DS",/* name */
1150 FALSE, /* partial_inplace */
1152 0xfffc, /* dst_mask */
1153 FALSE), /* pcrel_offset */
1155 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1156 HOWTO (R_PPC64_GOT16_DS, /* type */
1158 1, /* size (0 = byte, 1 = short, 2 = long) */
1160 FALSE, /* pc_relative */
1162 complain_overflow_signed, /* complain_on_overflow */
1163 ppc64_elf_unhandled_reloc, /* special_function */
1164 "R_PPC64_GOT16_DS", /* name */
1165 FALSE, /* partial_inplace */
1167 0xfffc, /* dst_mask */
1168 FALSE), /* pcrel_offset */
1170 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1171 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1173 1, /* size (0 = byte, 1 = short, 2 = long) */
1175 FALSE, /* pc_relative */
1177 complain_overflow_dont, /* complain_on_overflow */
1178 ppc64_elf_unhandled_reloc, /* special_function */
1179 "R_PPC64_GOT16_LO_DS", /* name */
1180 FALSE, /* partial_inplace */
1182 0xfffc, /* dst_mask */
1183 FALSE), /* pcrel_offset */
1185 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1186 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1188 1, /* size (0 = byte, 1 = short, 2 = long) */
1190 FALSE, /* pc_relative */
1192 complain_overflow_dont, /* complain_on_overflow */
1193 ppc64_elf_unhandled_reloc, /* special_function */
1194 "R_PPC64_PLT16_LO_DS", /* name */
1195 FALSE, /* partial_inplace */
1197 0xfffc, /* dst_mask */
1198 FALSE), /* pcrel_offset */
1200 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1201 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1203 1, /* size (0 = byte, 1 = short, 2 = long) */
1205 FALSE, /* pc_relative */
1207 complain_overflow_signed, /* complain_on_overflow */
1208 ppc64_elf_sectoff_reloc, /* special_function */
1209 "R_PPC64_SECTOFF_DS", /* name */
1210 FALSE, /* partial_inplace */
1212 0xfffc, /* dst_mask */
1213 FALSE), /* pcrel_offset */
1215 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1216 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1218 1, /* size (0 = byte, 1 = short, 2 = long) */
1220 FALSE, /* pc_relative */
1222 complain_overflow_dont, /* complain_on_overflow */
1223 ppc64_elf_sectoff_reloc, /* special_function */
1224 "R_PPC64_SECTOFF_LO_DS",/* name */
1225 FALSE, /* partial_inplace */
1227 0xfffc, /* dst_mask */
1228 FALSE), /* pcrel_offset */
1230 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1231 HOWTO (R_PPC64_TOC16_DS, /* type */
1233 1, /* size (0 = byte, 1 = short, 2 = long) */
1235 FALSE, /* pc_relative */
1237 complain_overflow_signed, /* complain_on_overflow */
1238 ppc64_elf_toc_reloc, /* special_function */
1239 "R_PPC64_TOC16_DS", /* name */
1240 FALSE, /* partial_inplace */
1242 0xfffc, /* dst_mask */
1243 FALSE), /* pcrel_offset */
1245 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1246 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1248 1, /* size (0 = byte, 1 = short, 2 = long) */
1250 FALSE, /* pc_relative */
1252 complain_overflow_dont, /* complain_on_overflow */
1253 ppc64_elf_toc_reloc, /* special_function */
1254 "R_PPC64_TOC16_LO_DS", /* name */
1255 FALSE, /* partial_inplace */
1257 0xfffc, /* dst_mask */
1258 FALSE), /* pcrel_offset */
1260 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1261 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1262 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1264 1, /* size (0 = byte, 1 = short, 2 = long) */
1266 FALSE, /* pc_relative */
1268 complain_overflow_signed, /* complain_on_overflow */
1269 ppc64_elf_unhandled_reloc, /* special_function */
1270 "R_PPC64_PLTGOT16_DS", /* name */
1271 FALSE, /* partial_inplace */
1273 0xfffc, /* dst_mask */
1274 FALSE), /* pcrel_offset */
1276 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1277 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1278 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1280 1, /* size (0 = byte, 1 = short, 2 = long) */
1282 FALSE, /* pc_relative */
1284 complain_overflow_dont, /* complain_on_overflow */
1285 ppc64_elf_unhandled_reloc, /* special_function */
1286 "R_PPC64_PLTGOT16_LO_DS",/* name */
1287 FALSE, /* partial_inplace */
1289 0xfffc, /* dst_mask */
1290 FALSE), /* pcrel_offset */
1292 /* Marker relocs for TLS. */
1295 2, /* size (0 = byte, 1 = short, 2 = long) */
1297 FALSE, /* pc_relative */
1299 complain_overflow_dont, /* complain_on_overflow */
1300 bfd_elf_generic_reloc, /* special_function */
1301 "R_PPC64_TLS", /* name */
1302 FALSE, /* partial_inplace */
1305 FALSE), /* pcrel_offset */
1307 HOWTO (R_PPC64_TLSGD,
1309 2, /* size (0 = byte, 1 = short, 2 = long) */
1311 FALSE, /* pc_relative */
1313 complain_overflow_dont, /* complain_on_overflow */
1314 bfd_elf_generic_reloc, /* special_function */
1315 "R_PPC64_TLSGD", /* name */
1316 FALSE, /* partial_inplace */
1319 FALSE), /* pcrel_offset */
1321 HOWTO (R_PPC64_TLSLD,
1323 2, /* size (0 = byte, 1 = short, 2 = long) */
1325 FALSE, /* pc_relative */
1327 complain_overflow_dont, /* complain_on_overflow */
1328 bfd_elf_generic_reloc, /* special_function */
1329 "R_PPC64_TLSLD", /* name */
1330 FALSE, /* partial_inplace */
1333 FALSE), /* pcrel_offset */
1335 HOWTO (R_PPC64_TOCSAVE,
1337 2, /* size (0 = byte, 1 = short, 2 = long) */
1339 FALSE, /* pc_relative */
1341 complain_overflow_dont, /* complain_on_overflow */
1342 bfd_elf_generic_reloc, /* special_function */
1343 "R_PPC64_TOCSAVE", /* name */
1344 FALSE, /* partial_inplace */
1347 FALSE), /* pcrel_offset */
1349 /* Computes the load module index of the load module that contains the
1350 definition of its TLS sym. */
1351 HOWTO (R_PPC64_DTPMOD64,
1353 4, /* size (0 = byte, 1 = short, 2 = long) */
1355 FALSE, /* pc_relative */
1357 complain_overflow_dont, /* complain_on_overflow */
1358 ppc64_elf_unhandled_reloc, /* special_function */
1359 "R_PPC64_DTPMOD64", /* name */
1360 FALSE, /* partial_inplace */
1362 ONES (64), /* dst_mask */
1363 FALSE), /* pcrel_offset */
1365 /* Computes a dtv-relative displacement, the difference between the value
1366 of sym+add and the base address of the thread-local storage block that
1367 contains the definition of sym, minus 0x8000. */
1368 HOWTO (R_PPC64_DTPREL64,
1370 4, /* size (0 = byte, 1 = short, 2 = long) */
1372 FALSE, /* pc_relative */
1374 complain_overflow_dont, /* complain_on_overflow */
1375 ppc64_elf_unhandled_reloc, /* special_function */
1376 "R_PPC64_DTPREL64", /* name */
1377 FALSE, /* partial_inplace */
1379 ONES (64), /* dst_mask */
1380 FALSE), /* pcrel_offset */
1382 /* A 16 bit dtprel reloc. */
1383 HOWTO (R_PPC64_DTPREL16,
1385 1, /* size (0 = byte, 1 = short, 2 = long) */
1387 FALSE, /* pc_relative */
1389 complain_overflow_signed, /* complain_on_overflow */
1390 ppc64_elf_unhandled_reloc, /* special_function */
1391 "R_PPC64_DTPREL16", /* name */
1392 FALSE, /* partial_inplace */
1394 0xffff, /* dst_mask */
1395 FALSE), /* pcrel_offset */
1397 /* Like DTPREL16, but no overflow. */
1398 HOWTO (R_PPC64_DTPREL16_LO,
1400 1, /* size (0 = byte, 1 = short, 2 = long) */
1402 FALSE, /* pc_relative */
1404 complain_overflow_dont, /* complain_on_overflow */
1405 ppc64_elf_unhandled_reloc, /* special_function */
1406 "R_PPC64_DTPREL16_LO", /* name */
1407 FALSE, /* partial_inplace */
1409 0xffff, /* dst_mask */
1410 FALSE), /* pcrel_offset */
1412 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1413 HOWTO (R_PPC64_DTPREL16_HI,
1414 16, /* rightshift */
1415 1, /* size (0 = byte, 1 = short, 2 = long) */
1417 FALSE, /* pc_relative */
1419 complain_overflow_signed, /* complain_on_overflow */
1420 ppc64_elf_unhandled_reloc, /* special_function */
1421 "R_PPC64_DTPREL16_HI", /* name */
1422 FALSE, /* partial_inplace */
1424 0xffff, /* dst_mask */
1425 FALSE), /* pcrel_offset */
1427 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1428 HOWTO (R_PPC64_DTPREL16_HA,
1429 16, /* rightshift */
1430 1, /* size (0 = byte, 1 = short, 2 = long) */
1432 FALSE, /* pc_relative */
1434 complain_overflow_signed, /* complain_on_overflow */
1435 ppc64_elf_unhandled_reloc, /* special_function */
1436 "R_PPC64_DTPREL16_HA", /* name */
1437 FALSE, /* partial_inplace */
1439 0xffff, /* dst_mask */
1440 FALSE), /* pcrel_offset */
1442 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1443 HOWTO (R_PPC64_DTPREL16_HIGHER,
1444 32, /* rightshift */
1445 1, /* size (0 = byte, 1 = short, 2 = long) */
1447 FALSE, /* pc_relative */
1449 complain_overflow_dont, /* complain_on_overflow */
1450 ppc64_elf_unhandled_reloc, /* special_function */
1451 "R_PPC64_DTPREL16_HIGHER", /* name */
1452 FALSE, /* partial_inplace */
1454 0xffff, /* dst_mask */
1455 FALSE), /* pcrel_offset */
1457 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1458 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1459 32, /* rightshift */
1460 1, /* size (0 = byte, 1 = short, 2 = long) */
1462 FALSE, /* pc_relative */
1464 complain_overflow_dont, /* complain_on_overflow */
1465 ppc64_elf_unhandled_reloc, /* special_function */
1466 "R_PPC64_DTPREL16_HIGHERA", /* name */
1467 FALSE, /* partial_inplace */
1469 0xffff, /* dst_mask */
1470 FALSE), /* pcrel_offset */
1472 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1473 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1474 48, /* rightshift */
1475 1, /* size (0 = byte, 1 = short, 2 = long) */
1477 FALSE, /* pc_relative */
1479 complain_overflow_dont, /* complain_on_overflow */
1480 ppc64_elf_unhandled_reloc, /* special_function */
1481 "R_PPC64_DTPREL16_HIGHEST", /* name */
1482 FALSE, /* partial_inplace */
1484 0xffff, /* dst_mask */
1485 FALSE), /* pcrel_offset */
1487 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1488 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1489 48, /* rightshift */
1490 1, /* size (0 = byte, 1 = short, 2 = long) */
1492 FALSE, /* pc_relative */
1494 complain_overflow_dont, /* complain_on_overflow */
1495 ppc64_elf_unhandled_reloc, /* special_function */
1496 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1497 FALSE, /* partial_inplace */
1499 0xffff, /* dst_mask */
1500 FALSE), /* pcrel_offset */
1502 /* Like DTPREL16, but for insns with a DS field. */
1503 HOWTO (R_PPC64_DTPREL16_DS,
1505 1, /* size (0 = byte, 1 = short, 2 = long) */
1507 FALSE, /* pc_relative */
1509 complain_overflow_signed, /* complain_on_overflow */
1510 ppc64_elf_unhandled_reloc, /* special_function */
1511 "R_PPC64_DTPREL16_DS", /* name */
1512 FALSE, /* partial_inplace */
1514 0xfffc, /* dst_mask */
1515 FALSE), /* pcrel_offset */
1517 /* Like DTPREL16_DS, but no overflow. */
1518 HOWTO (R_PPC64_DTPREL16_LO_DS,
1520 1, /* size (0 = byte, 1 = short, 2 = long) */
1522 FALSE, /* pc_relative */
1524 complain_overflow_dont, /* complain_on_overflow */
1525 ppc64_elf_unhandled_reloc, /* special_function */
1526 "R_PPC64_DTPREL16_LO_DS", /* name */
1527 FALSE, /* partial_inplace */
1529 0xfffc, /* dst_mask */
1530 FALSE), /* pcrel_offset */
1532 /* Computes a tp-relative displacement, the difference between the value of
1533 sym+add and the value of the thread pointer (r13). */
1534 HOWTO (R_PPC64_TPREL64,
1536 4, /* size (0 = byte, 1 = short, 2 = long) */
1538 FALSE, /* pc_relative */
1540 complain_overflow_dont, /* complain_on_overflow */
1541 ppc64_elf_unhandled_reloc, /* special_function */
1542 "R_PPC64_TPREL64", /* name */
1543 FALSE, /* partial_inplace */
1545 ONES (64), /* dst_mask */
1546 FALSE), /* pcrel_offset */
1548 /* A 16 bit tprel reloc. */
1549 HOWTO (R_PPC64_TPREL16,
1551 1, /* size (0 = byte, 1 = short, 2 = long) */
1553 FALSE, /* pc_relative */
1555 complain_overflow_signed, /* complain_on_overflow */
1556 ppc64_elf_unhandled_reloc, /* special_function */
1557 "R_PPC64_TPREL16", /* name */
1558 FALSE, /* partial_inplace */
1560 0xffff, /* dst_mask */
1561 FALSE), /* pcrel_offset */
1563 /* Like TPREL16, but no overflow. */
1564 HOWTO (R_PPC64_TPREL16_LO,
1566 1, /* size (0 = byte, 1 = short, 2 = long) */
1568 FALSE, /* pc_relative */
1570 complain_overflow_dont, /* complain_on_overflow */
1571 ppc64_elf_unhandled_reloc, /* special_function */
1572 "R_PPC64_TPREL16_LO", /* name */
1573 FALSE, /* partial_inplace */
1575 0xffff, /* dst_mask */
1576 FALSE), /* pcrel_offset */
1578 /* Like TPREL16_LO, but next higher group of 16 bits. */
1579 HOWTO (R_PPC64_TPREL16_HI,
1580 16, /* rightshift */
1581 1, /* size (0 = byte, 1 = short, 2 = long) */
1583 FALSE, /* pc_relative */
1585 complain_overflow_signed, /* complain_on_overflow */
1586 ppc64_elf_unhandled_reloc, /* special_function */
1587 "R_PPC64_TPREL16_HI", /* name */
1588 FALSE, /* partial_inplace */
1590 0xffff, /* dst_mask */
1591 FALSE), /* pcrel_offset */
1593 /* Like TPREL16_HI, but adjust for low 16 bits. */
1594 HOWTO (R_PPC64_TPREL16_HA,
1595 16, /* rightshift */
1596 1, /* size (0 = byte, 1 = short, 2 = long) */
1598 FALSE, /* pc_relative */
1600 complain_overflow_signed, /* complain_on_overflow */
1601 ppc64_elf_unhandled_reloc, /* special_function */
1602 "R_PPC64_TPREL16_HA", /* name */
1603 FALSE, /* partial_inplace */
1605 0xffff, /* dst_mask */
1606 FALSE), /* pcrel_offset */
1608 /* Like TPREL16_HI, but next higher group of 16 bits. */
1609 HOWTO (R_PPC64_TPREL16_HIGHER,
1610 32, /* rightshift */
1611 1, /* size (0 = byte, 1 = short, 2 = long) */
1613 FALSE, /* pc_relative */
1615 complain_overflow_dont, /* complain_on_overflow */
1616 ppc64_elf_unhandled_reloc, /* special_function */
1617 "R_PPC64_TPREL16_HIGHER", /* name */
1618 FALSE, /* partial_inplace */
1620 0xffff, /* dst_mask */
1621 FALSE), /* pcrel_offset */
1623 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1624 HOWTO (R_PPC64_TPREL16_HIGHERA,
1625 32, /* rightshift */
1626 1, /* size (0 = byte, 1 = short, 2 = long) */
1628 FALSE, /* pc_relative */
1630 complain_overflow_dont, /* complain_on_overflow */
1631 ppc64_elf_unhandled_reloc, /* special_function */
1632 "R_PPC64_TPREL16_HIGHERA", /* name */
1633 FALSE, /* partial_inplace */
1635 0xffff, /* dst_mask */
1636 FALSE), /* pcrel_offset */
1638 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1639 HOWTO (R_PPC64_TPREL16_HIGHEST,
1640 48, /* rightshift */
1641 1, /* size (0 = byte, 1 = short, 2 = long) */
1643 FALSE, /* pc_relative */
1645 complain_overflow_dont, /* complain_on_overflow */
1646 ppc64_elf_unhandled_reloc, /* special_function */
1647 "R_PPC64_TPREL16_HIGHEST", /* name */
1648 FALSE, /* partial_inplace */
1650 0xffff, /* dst_mask */
1651 FALSE), /* pcrel_offset */
1653 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1654 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1655 48, /* rightshift */
1656 1, /* size (0 = byte, 1 = short, 2 = long) */
1658 FALSE, /* pc_relative */
1660 complain_overflow_dont, /* complain_on_overflow */
1661 ppc64_elf_unhandled_reloc, /* special_function */
1662 "R_PPC64_TPREL16_HIGHESTA", /* name */
1663 FALSE, /* partial_inplace */
1665 0xffff, /* dst_mask */
1666 FALSE), /* pcrel_offset */
1668 /* Like TPREL16, but for insns with a DS field. */
1669 HOWTO (R_PPC64_TPREL16_DS,
1671 1, /* size (0 = byte, 1 = short, 2 = long) */
1673 FALSE, /* pc_relative */
1675 complain_overflow_signed, /* complain_on_overflow */
1676 ppc64_elf_unhandled_reloc, /* special_function */
1677 "R_PPC64_TPREL16_DS", /* name */
1678 FALSE, /* partial_inplace */
1680 0xfffc, /* dst_mask */
1681 FALSE), /* pcrel_offset */
1683 /* Like TPREL16_DS, but no overflow. */
1684 HOWTO (R_PPC64_TPREL16_LO_DS,
1686 1, /* size (0 = byte, 1 = short, 2 = long) */
1688 FALSE, /* pc_relative */
1690 complain_overflow_dont, /* complain_on_overflow */
1691 ppc64_elf_unhandled_reloc, /* special_function */
1692 "R_PPC64_TPREL16_LO_DS", /* name */
1693 FALSE, /* partial_inplace */
1695 0xfffc, /* dst_mask */
1696 FALSE), /* pcrel_offset */
1698 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1699 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1700 to the first entry relative to the TOC base (r2). */
1701 HOWTO (R_PPC64_GOT_TLSGD16,
1703 1, /* size (0 = byte, 1 = short, 2 = long) */
1705 FALSE, /* pc_relative */
1707 complain_overflow_signed, /* complain_on_overflow */
1708 ppc64_elf_unhandled_reloc, /* special_function */
1709 "R_PPC64_GOT_TLSGD16", /* name */
1710 FALSE, /* partial_inplace */
1712 0xffff, /* dst_mask */
1713 FALSE), /* pcrel_offset */
1715 /* Like GOT_TLSGD16, but no overflow. */
1716 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1718 1, /* size (0 = byte, 1 = short, 2 = long) */
1720 FALSE, /* pc_relative */
1722 complain_overflow_dont, /* complain_on_overflow */
1723 ppc64_elf_unhandled_reloc, /* special_function */
1724 "R_PPC64_GOT_TLSGD16_LO", /* name */
1725 FALSE, /* partial_inplace */
1727 0xffff, /* dst_mask */
1728 FALSE), /* pcrel_offset */
1730 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1731 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1732 16, /* rightshift */
1733 1, /* size (0 = byte, 1 = short, 2 = long) */
1735 FALSE, /* pc_relative */
1737 complain_overflow_signed, /* complain_on_overflow */
1738 ppc64_elf_unhandled_reloc, /* special_function */
1739 "R_PPC64_GOT_TLSGD16_HI", /* name */
1740 FALSE, /* partial_inplace */
1742 0xffff, /* dst_mask */
1743 FALSE), /* pcrel_offset */
1745 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1746 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1747 16, /* rightshift */
1748 1, /* size (0 = byte, 1 = short, 2 = long) */
1750 FALSE, /* pc_relative */
1752 complain_overflow_signed, /* complain_on_overflow */
1753 ppc64_elf_unhandled_reloc, /* special_function */
1754 "R_PPC64_GOT_TLSGD16_HA", /* name */
1755 FALSE, /* partial_inplace */
1757 0xffff, /* dst_mask */
1758 FALSE), /* pcrel_offset */
1760 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1761 with values (sym+add)@dtpmod and zero, and computes the offset to the
1762 first entry relative to the TOC base (r2). */
1763 HOWTO (R_PPC64_GOT_TLSLD16,
1765 1, /* size (0 = byte, 1 = short, 2 = long) */
1767 FALSE, /* pc_relative */
1769 complain_overflow_signed, /* complain_on_overflow */
1770 ppc64_elf_unhandled_reloc, /* special_function */
1771 "R_PPC64_GOT_TLSLD16", /* name */
1772 FALSE, /* partial_inplace */
1774 0xffff, /* dst_mask */
1775 FALSE), /* pcrel_offset */
1777 /* Like GOT_TLSLD16, but no overflow. */
1778 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1780 1, /* size (0 = byte, 1 = short, 2 = long) */
1782 FALSE, /* pc_relative */
1784 complain_overflow_dont, /* complain_on_overflow */
1785 ppc64_elf_unhandled_reloc, /* special_function */
1786 "R_PPC64_GOT_TLSLD16_LO", /* name */
1787 FALSE, /* partial_inplace */
1789 0xffff, /* dst_mask */
1790 FALSE), /* pcrel_offset */
1792 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1793 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1794 16, /* rightshift */
1795 1, /* size (0 = byte, 1 = short, 2 = long) */
1797 FALSE, /* pc_relative */
1799 complain_overflow_signed, /* complain_on_overflow */
1800 ppc64_elf_unhandled_reloc, /* special_function */
1801 "R_PPC64_GOT_TLSLD16_HI", /* name */
1802 FALSE, /* partial_inplace */
1804 0xffff, /* dst_mask */
1805 FALSE), /* pcrel_offset */
1807 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1808 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1809 16, /* rightshift */
1810 1, /* size (0 = byte, 1 = short, 2 = long) */
1812 FALSE, /* pc_relative */
1814 complain_overflow_signed, /* complain_on_overflow */
1815 ppc64_elf_unhandled_reloc, /* special_function */
1816 "R_PPC64_GOT_TLSLD16_HA", /* name */
1817 FALSE, /* partial_inplace */
1819 0xffff, /* dst_mask */
1820 FALSE), /* pcrel_offset */
1822 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1823 the offset to the entry relative to the TOC base (r2). */
1824 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1826 1, /* size (0 = byte, 1 = short, 2 = long) */
1828 FALSE, /* pc_relative */
1830 complain_overflow_signed, /* complain_on_overflow */
1831 ppc64_elf_unhandled_reloc, /* special_function */
1832 "R_PPC64_GOT_DTPREL16_DS", /* name */
1833 FALSE, /* partial_inplace */
1835 0xfffc, /* dst_mask */
1836 FALSE), /* pcrel_offset */
1838 /* Like GOT_DTPREL16_DS, but no overflow. */
1839 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1841 1, /* size (0 = byte, 1 = short, 2 = long) */
1843 FALSE, /* pc_relative */
1845 complain_overflow_dont, /* complain_on_overflow */
1846 ppc64_elf_unhandled_reloc, /* special_function */
1847 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1848 FALSE, /* partial_inplace */
1850 0xfffc, /* dst_mask */
1851 FALSE), /* pcrel_offset */
1853 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1854 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1855 16, /* rightshift */
1856 1, /* size (0 = byte, 1 = short, 2 = long) */
1858 FALSE, /* pc_relative */
1860 complain_overflow_signed, /* complain_on_overflow */
1861 ppc64_elf_unhandled_reloc, /* special_function */
1862 "R_PPC64_GOT_DTPREL16_HI", /* name */
1863 FALSE, /* partial_inplace */
1865 0xffff, /* dst_mask */
1866 FALSE), /* pcrel_offset */
1868 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1869 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1870 16, /* rightshift */
1871 1, /* size (0 = byte, 1 = short, 2 = long) */
1873 FALSE, /* pc_relative */
1875 complain_overflow_signed, /* complain_on_overflow */
1876 ppc64_elf_unhandled_reloc, /* special_function */
1877 "R_PPC64_GOT_DTPREL16_HA", /* name */
1878 FALSE, /* partial_inplace */
1880 0xffff, /* dst_mask */
1881 FALSE), /* pcrel_offset */
1883 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1884 offset to the entry relative to the TOC base (r2). */
1885 HOWTO (R_PPC64_GOT_TPREL16_DS,
1887 1, /* size (0 = byte, 1 = short, 2 = long) */
1889 FALSE, /* pc_relative */
1891 complain_overflow_signed, /* complain_on_overflow */
1892 ppc64_elf_unhandled_reloc, /* special_function */
1893 "R_PPC64_GOT_TPREL16_DS", /* name */
1894 FALSE, /* partial_inplace */
1896 0xfffc, /* dst_mask */
1897 FALSE), /* pcrel_offset */
1899 /* Like GOT_TPREL16_DS, but no overflow. */
1900 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1902 1, /* size (0 = byte, 1 = short, 2 = long) */
1904 FALSE, /* pc_relative */
1906 complain_overflow_dont, /* complain_on_overflow */
1907 ppc64_elf_unhandled_reloc, /* special_function */
1908 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1909 FALSE, /* partial_inplace */
1911 0xfffc, /* dst_mask */
1912 FALSE), /* pcrel_offset */
1914 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1915 HOWTO (R_PPC64_GOT_TPREL16_HI,
1916 16, /* rightshift */
1917 1, /* size (0 = byte, 1 = short, 2 = long) */
1919 FALSE, /* pc_relative */
1921 complain_overflow_signed, /* complain_on_overflow */
1922 ppc64_elf_unhandled_reloc, /* special_function */
1923 "R_PPC64_GOT_TPREL16_HI", /* name */
1924 FALSE, /* partial_inplace */
1926 0xffff, /* dst_mask */
1927 FALSE), /* pcrel_offset */
1929 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1930 HOWTO (R_PPC64_GOT_TPREL16_HA,
1931 16, /* rightshift */
1932 1, /* size (0 = byte, 1 = short, 2 = long) */
1934 FALSE, /* pc_relative */
1936 complain_overflow_signed, /* complain_on_overflow */
1937 ppc64_elf_unhandled_reloc, /* special_function */
1938 "R_PPC64_GOT_TPREL16_HA", /* name */
1939 FALSE, /* partial_inplace */
1941 0xffff, /* dst_mask */
1942 FALSE), /* pcrel_offset */
1944 HOWTO (R_PPC64_JMP_IREL, /* type */
1946 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1948 FALSE, /* pc_relative */
1950 complain_overflow_dont, /* complain_on_overflow */
1951 ppc64_elf_unhandled_reloc, /* special_function */
1952 "R_PPC64_JMP_IREL", /* name */
1953 FALSE, /* partial_inplace */
1956 FALSE), /* pcrel_offset */
1958 HOWTO (R_PPC64_IRELATIVE, /* type */
1960 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1962 FALSE, /* pc_relative */
1964 complain_overflow_dont, /* complain_on_overflow */
1965 bfd_elf_generic_reloc, /* special_function */
1966 "R_PPC64_IRELATIVE", /* name */
1967 FALSE, /* partial_inplace */
1969 ONES (64), /* dst_mask */
1970 FALSE), /* pcrel_offset */
1972 /* A 16 bit relative relocation. */
1973 HOWTO (R_PPC64_REL16, /* type */
1975 1, /* size (0 = byte, 1 = short, 2 = long) */
1977 TRUE, /* pc_relative */
1979 complain_overflow_signed, /* complain_on_overflow */
1980 bfd_elf_generic_reloc, /* special_function */
1981 "R_PPC64_REL16", /* name */
1982 FALSE, /* partial_inplace */
1984 0xffff, /* dst_mask */
1985 TRUE), /* pcrel_offset */
1987 /* A 16 bit relative relocation without overflow. */
1988 HOWTO (R_PPC64_REL16_LO, /* type */
1990 1, /* size (0 = byte, 1 = short, 2 = long) */
1992 TRUE, /* pc_relative */
1994 complain_overflow_dont,/* complain_on_overflow */
1995 bfd_elf_generic_reloc, /* special_function */
1996 "R_PPC64_REL16_LO", /* name */
1997 FALSE, /* partial_inplace */
1999 0xffff, /* dst_mask */
2000 TRUE), /* pcrel_offset */
2002 /* The high order 16 bits of a relative address. */
2003 HOWTO (R_PPC64_REL16_HI, /* type */
2004 16, /* rightshift */
2005 1, /* size (0 = byte, 1 = short, 2 = long) */
2007 TRUE, /* pc_relative */
2009 complain_overflow_signed, /* complain_on_overflow */
2010 bfd_elf_generic_reloc, /* special_function */
2011 "R_PPC64_REL16_HI", /* name */
2012 FALSE, /* partial_inplace */
2014 0xffff, /* dst_mask */
2015 TRUE), /* pcrel_offset */
2017 /* The high order 16 bits of a relative address, plus 1 if the contents of
2018 the low 16 bits, treated as a signed number, is negative. */
2019 HOWTO (R_PPC64_REL16_HA, /* type */
2020 16, /* rightshift */
2021 1, /* size (0 = byte, 1 = short, 2 = long) */
2023 TRUE, /* pc_relative */
2025 complain_overflow_signed, /* complain_on_overflow */
2026 ppc64_elf_ha_reloc, /* special_function */
2027 "R_PPC64_REL16_HA", /* name */
2028 FALSE, /* partial_inplace */
2030 0xffff, /* dst_mask */
2031 TRUE), /* pcrel_offset */
2033 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2034 HOWTO (R_PPC64_REL16DX_HA, /* type */
2035 16, /* rightshift */
2036 2, /* size (0 = byte, 1 = short, 2 = long) */
2038 TRUE, /* pc_relative */
2040 complain_overflow_signed, /* complain_on_overflow */
2041 ppc64_elf_ha_reloc, /* special_function */
2042 "R_PPC64_REL16DX_HA", /* name */
2043 FALSE, /* partial_inplace */
2045 0x1fffc1, /* dst_mask */
2046 TRUE), /* pcrel_offset */
2048 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
2049 HOWTO (R_PPC64_16DX_HA, /* type */
2050 16, /* rightshift */
2051 2, /* size (0 = byte, 1 = short, 2 = long) */
2053 FALSE, /* pc_relative */
2055 complain_overflow_signed, /* complain_on_overflow */
2056 ppc64_elf_ha_reloc, /* special_function */
2057 "R_PPC64_16DX_HA", /* name */
2058 FALSE, /* partial_inplace */
2060 0x1fffc1, /* dst_mask */
2061 FALSE), /* pcrel_offset */
2063 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2064 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2065 16, /* rightshift */
2066 1, /* size (0 = byte, 1 = short, 2 = long) */
2068 FALSE, /* pc_relative */
2070 complain_overflow_dont, /* complain_on_overflow */
2071 bfd_elf_generic_reloc, /* special_function */
2072 "R_PPC64_ADDR16_HIGH", /* name */
2073 FALSE, /* partial_inplace */
2075 0xffff, /* dst_mask */
2076 FALSE), /* pcrel_offset */
2078 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2079 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2080 16, /* rightshift */
2081 1, /* size (0 = byte, 1 = short, 2 = long) */
2083 FALSE, /* pc_relative */
2085 complain_overflow_dont, /* complain_on_overflow */
2086 ppc64_elf_ha_reloc, /* special_function */
2087 "R_PPC64_ADDR16_HIGHA", /* name */
2088 FALSE, /* partial_inplace */
2090 0xffff, /* dst_mask */
2091 FALSE), /* pcrel_offset */
2093 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2094 HOWTO (R_PPC64_DTPREL16_HIGH,
2095 16, /* rightshift */
2096 1, /* size (0 = byte, 1 = short, 2 = long) */
2098 FALSE, /* pc_relative */
2100 complain_overflow_dont, /* complain_on_overflow */
2101 ppc64_elf_unhandled_reloc, /* special_function */
2102 "R_PPC64_DTPREL16_HIGH", /* name */
2103 FALSE, /* partial_inplace */
2105 0xffff, /* dst_mask */
2106 FALSE), /* pcrel_offset */
2108 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2109 HOWTO (R_PPC64_DTPREL16_HIGHA,
2110 16, /* rightshift */
2111 1, /* size (0 = byte, 1 = short, 2 = long) */
2113 FALSE, /* pc_relative */
2115 complain_overflow_dont, /* complain_on_overflow */
2116 ppc64_elf_unhandled_reloc, /* special_function */
2117 "R_PPC64_DTPREL16_HIGHA", /* name */
2118 FALSE, /* partial_inplace */
2120 0xffff, /* dst_mask */
2121 FALSE), /* pcrel_offset */
2123 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2124 HOWTO (R_PPC64_TPREL16_HIGH,
2125 16, /* rightshift */
2126 1, /* size (0 = byte, 1 = short, 2 = long) */
2128 FALSE, /* pc_relative */
2130 complain_overflow_dont, /* complain_on_overflow */
2131 ppc64_elf_unhandled_reloc, /* special_function */
2132 "R_PPC64_TPREL16_HIGH", /* name */
2133 FALSE, /* partial_inplace */
2135 0xffff, /* dst_mask */
2136 FALSE), /* pcrel_offset */
2138 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2139 HOWTO (R_PPC64_TPREL16_HIGHA,
2140 16, /* rightshift */
2141 1, /* size (0 = byte, 1 = short, 2 = long) */
2143 FALSE, /* pc_relative */
2145 complain_overflow_dont, /* complain_on_overflow */
2146 ppc64_elf_unhandled_reloc, /* special_function */
2147 "R_PPC64_TPREL16_HIGHA", /* name */
2148 FALSE, /* partial_inplace */
2150 0xffff, /* dst_mask */
2151 FALSE), /* pcrel_offset */
2153 /* Marker reloc on ELFv2 large-model function entry. */
2154 HOWTO (R_PPC64_ENTRY,
2156 2, /* size (0 = byte, 1 = short, 2 = long) */
2158 FALSE, /* pc_relative */
2160 complain_overflow_dont, /* complain_on_overflow */
2161 bfd_elf_generic_reloc, /* special_function */
2162 "R_PPC64_ENTRY", /* name */
2163 FALSE, /* partial_inplace */
2166 FALSE), /* pcrel_offset */
2168 /* Like ADDR64, but use local entry point of function. */
2169 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2171 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2173 FALSE, /* pc_relative */
2175 complain_overflow_dont, /* complain_on_overflow */
2176 bfd_elf_generic_reloc, /* special_function */
2177 "R_PPC64_ADDR64_LOCAL", /* name */
2178 FALSE, /* partial_inplace */
2180 ONES (64), /* dst_mask */
2181 FALSE), /* pcrel_offset */
2183 /* GNU extension to record C++ vtable hierarchy. */
2184 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2186 0, /* size (0 = byte, 1 = short, 2 = long) */
2188 FALSE, /* pc_relative */
2190 complain_overflow_dont, /* complain_on_overflow */
2191 NULL, /* special_function */
2192 "R_PPC64_GNU_VTINHERIT", /* name */
2193 FALSE, /* partial_inplace */
2196 FALSE), /* pcrel_offset */
2198 /* GNU extension to record C++ vtable member usage. */
2199 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2201 0, /* size (0 = byte, 1 = short, 2 = long) */
2203 FALSE, /* pc_relative */
2205 complain_overflow_dont, /* complain_on_overflow */
2206 NULL, /* special_function */
2207 "R_PPC64_GNU_VTENTRY", /* name */
2208 FALSE, /* partial_inplace */
2211 FALSE), /* pcrel_offset */
2215 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2219 ppc_howto_init (void)
2221 unsigned int i, type;
2223 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2225 type = ppc64_elf_howto_raw[i].type;
2226 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
2227 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2231 static reloc_howto_type *
2232 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2233 bfd_reloc_code_real_type code)
2235 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2237 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2238 /* Initialize howto table if needed. */
2246 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2248 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2250 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2252 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2254 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2256 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2258 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2260 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2262 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2264 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2266 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2268 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2270 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2272 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2274 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2276 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2278 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2280 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2282 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2284 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2286 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2288 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2290 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2292 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2294 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2296 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2298 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2300 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2302 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2304 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2306 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2308 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2310 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2312 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2314 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2316 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2318 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2320 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2322 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2324 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2326 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2328 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2330 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2332 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2334 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2336 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2338 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2340 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2342 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2344 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2346 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2348 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2350 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2352 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2354 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2356 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2358 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2360 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2362 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2364 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2366 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2368 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2370 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2372 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2374 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2376 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2378 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2380 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2382 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2384 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2386 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2388 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2390 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2392 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2394 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2396 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2398 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2400 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2402 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2404 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2406 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2408 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2410 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2412 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2414 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2416 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2418 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2420 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2422 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2424 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2426 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2428 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2430 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2432 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2434 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2436 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2438 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2440 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2442 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2444 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2446 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2448 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2450 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2452 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2454 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2456 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2458 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2460 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2462 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2464 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2466 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2468 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
2470 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2472 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2474 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2476 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2478 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2482 return ppc64_elf_howto_table[r];
2485 static reloc_howto_type *
2486 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2491 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2492 if (ppc64_elf_howto_raw[i].name != NULL
2493 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2494 return &ppc64_elf_howto_raw[i];
2499 /* Set the howto pointer for a PowerPC ELF reloc. */
2502 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
2503 Elf_Internal_Rela *dst)
2507 /* Initialize howto table if needed. */
2508 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2511 type = ELF64_R_TYPE (dst->r_info);
2512 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
2514 /* xgettext:c-format */
2515 _bfd_error_handler (_("%B: invalid relocation type %d"),
2517 type = R_PPC64_NONE;
2519 cache_ptr->howto = ppc64_elf_howto_table[type];
2522 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2524 static bfd_reloc_status_type
2525 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2526 void *data, asection *input_section,
2527 bfd *output_bfd, char **error_message)
2529 enum elf_ppc64_reloc_type r_type;
2531 bfd_size_type octets;
2534 /* If this is a relocatable link (output_bfd test tells us), just
2535 call the generic function. Any adjustment will be done at final
2537 if (output_bfd != NULL)
2538 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2539 input_section, output_bfd, error_message);
2541 /* Adjust the addend for sign extension of the low 16 bits.
2542 We won't actually be using the low 16 bits, so trashing them
2544 reloc_entry->addend += 0x8000;
2545 r_type = reloc_entry->howto->type;
2546 if (r_type != R_PPC64_REL16DX_HA)
2547 return bfd_reloc_continue;
2550 if (!bfd_is_com_section (symbol->section))
2551 value = symbol->value;
2552 value += (reloc_entry->addend
2553 + symbol->section->output_offset
2554 + symbol->section->output_section->vma);
2555 value -= (reloc_entry->address
2556 + input_section->output_offset
2557 + input_section->output_section->vma);
2558 value = (bfd_signed_vma) value >> 16;
2560 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2561 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2563 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2564 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2565 if (value + 0x8000 > 0xffff)
2566 return bfd_reloc_overflow;
2567 return bfd_reloc_ok;
2570 static bfd_reloc_status_type
2571 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2572 void *data, asection *input_section,
2573 bfd *output_bfd, char **error_message)
2575 if (output_bfd != NULL)
2576 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2577 input_section, output_bfd, error_message);
2579 if (strcmp (symbol->section->name, ".opd") == 0
2580 && (symbol->section->owner->flags & DYNAMIC) == 0)
2582 bfd_vma dest = opd_entry_value (symbol->section,
2583 symbol->value + reloc_entry->addend,
2585 if (dest != (bfd_vma) -1)
2586 reloc_entry->addend = dest - (symbol->value
2587 + symbol->section->output_section->vma
2588 + symbol->section->output_offset);
2592 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2594 if (symbol->section->owner != abfd
2595 && symbol->section->owner != NULL
2596 && abiversion (symbol->section->owner) >= 2)
2600 for (i = 0; i < symbol->section->owner->symcount; ++i)
2602 asymbol *symdef = symbol->section->owner->outsymbols[i];
2604 if (strcmp (symdef->name, symbol->name) == 0)
2606 elfsym = (elf_symbol_type *) symdef;
2612 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2614 return bfd_reloc_continue;
2617 static bfd_reloc_status_type
2618 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2619 void *data, asection *input_section,
2620 bfd *output_bfd, char **error_message)
2623 enum elf_ppc64_reloc_type r_type;
2624 bfd_size_type octets;
2625 /* Assume 'at' branch hints. */
2626 bfd_boolean is_isa_v2 = TRUE;
2628 /* If this is a relocatable link (output_bfd test tells us), just
2629 call the generic function. Any adjustment will be done at final
2631 if (output_bfd != NULL)
2632 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2633 input_section, output_bfd, error_message);
2635 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2636 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2637 insn &= ~(0x01 << 21);
2638 r_type = reloc_entry->howto->type;
2639 if (r_type == R_PPC64_ADDR14_BRTAKEN
2640 || r_type == R_PPC64_REL14_BRTAKEN)
2641 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2645 /* Set 'a' bit. This is 0b00010 in BO field for branch
2646 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2647 for branch on CTR insns (BO == 1a00t or 1a01t). */
2648 if ((insn & (0x14 << 21)) == (0x04 << 21))
2650 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2660 if (!bfd_is_com_section (symbol->section))
2661 target = symbol->value;
2662 target += symbol->section->output_section->vma;
2663 target += symbol->section->output_offset;
2664 target += reloc_entry->addend;
2666 from = (reloc_entry->address
2667 + input_section->output_offset
2668 + input_section->output_section->vma);
2670 /* Invert 'y' bit if not the default. */
2671 if ((bfd_signed_vma) (target - from) < 0)
2674 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2676 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2677 input_section, output_bfd, error_message);
2680 static bfd_reloc_status_type
2681 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2682 void *data, asection *input_section,
2683 bfd *output_bfd, char **error_message)
2685 /* If this is a relocatable link (output_bfd test tells us), just
2686 call the generic function. Any adjustment will be done at final
2688 if (output_bfd != NULL)
2689 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2690 input_section, output_bfd, error_message);
2692 /* Subtract the symbol section base address. */
2693 reloc_entry->addend -= symbol->section->output_section->vma;
2694 return bfd_reloc_continue;
2697 static bfd_reloc_status_type
2698 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2699 void *data, asection *input_section,
2700 bfd *output_bfd, char **error_message)
2702 /* If this is a relocatable link (output_bfd test tells us), just
2703 call the generic function. Any adjustment will be done at final
2705 if (output_bfd != NULL)
2706 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2707 input_section, output_bfd, error_message);
2709 /* Subtract the symbol section base address. */
2710 reloc_entry->addend -= symbol->section->output_section->vma;
2712 /* Adjust the addend for sign extension of the low 16 bits. */
2713 reloc_entry->addend += 0x8000;
2714 return bfd_reloc_continue;
2717 static bfd_reloc_status_type
2718 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2719 void *data, asection *input_section,
2720 bfd *output_bfd, char **error_message)
2724 /* If this is a relocatable link (output_bfd test tells us), just
2725 call the generic function. Any adjustment will be done at final
2727 if (output_bfd != NULL)
2728 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2729 input_section, output_bfd, error_message);
2731 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2733 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2735 /* Subtract the TOC base address. */
2736 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2737 return bfd_reloc_continue;
2740 static bfd_reloc_status_type
2741 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2742 void *data, asection *input_section,
2743 bfd *output_bfd, char **error_message)
2747 /* If this is a relocatable link (output_bfd test tells us), just
2748 call the generic function. Any adjustment will be done at final
2750 if (output_bfd != NULL)
2751 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2752 input_section, output_bfd, error_message);
2754 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2756 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2758 /* Subtract the TOC base address. */
2759 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2761 /* Adjust the addend for sign extension of the low 16 bits. */
2762 reloc_entry->addend += 0x8000;
2763 return bfd_reloc_continue;
2766 static bfd_reloc_status_type
2767 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2768 void *data, asection *input_section,
2769 bfd *output_bfd, char **error_message)
2772 bfd_size_type octets;
2774 /* If this is a relocatable link (output_bfd test tells us), just
2775 call the generic function. Any adjustment will be done at final
2777 if (output_bfd != NULL)
2778 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2779 input_section, output_bfd, error_message);
2781 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2783 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2785 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2786 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2787 return bfd_reloc_ok;
2790 static bfd_reloc_status_type
2791 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2792 void *data, asection *input_section,
2793 bfd *output_bfd, char **error_message)
2795 /* If this is a relocatable link (output_bfd test tells us), just
2796 call the generic function. Any adjustment will be done at final
2798 if (output_bfd != NULL)
2799 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2800 input_section, output_bfd, error_message);
2802 if (error_message != NULL)
2804 static char buf[60];
2805 sprintf (buf, "generic linker can't handle %s",
2806 reloc_entry->howto->name);
2807 *error_message = buf;
2809 return bfd_reloc_dangerous;
2812 /* Track GOT entries needed for a given symbol. We might need more
2813 than one got entry per symbol. */
2816 struct got_entry *next;
2818 /* The symbol addend that we'll be placing in the GOT. */
2821 /* Unlike other ELF targets, we use separate GOT entries for the same
2822 symbol referenced from different input files. This is to support
2823 automatic multiple TOC/GOT sections, where the TOC base can vary
2824 from one input file to another. After partitioning into TOC groups
2825 we merge entries within the group.
2827 Point to the BFD owning this GOT entry. */
2830 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2831 TLS_TPREL or TLS_DTPREL for tls entries. */
2832 unsigned char tls_type;
2834 /* Non-zero if got.ent points to real entry. */
2835 unsigned char is_indirect;
2837 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2840 bfd_signed_vma refcount;
2842 struct got_entry *ent;
2846 /* The same for PLT. */
2849 struct plt_entry *next;
2855 bfd_signed_vma refcount;
2860 struct ppc64_elf_obj_tdata
2862 struct elf_obj_tdata elf;
2864 /* Shortcuts to dynamic linker sections. */
2868 /* Used during garbage collection. We attach global symbols defined
2869 on removed .opd entries to this section so that the sym is removed. */
2870 asection *deleted_section;
2872 /* TLS local dynamic got entry handling. Support for multiple GOT
2873 sections means we potentially need one of these for each input bfd. */
2874 struct got_entry tlsld_got;
2877 /* A copy of relocs before they are modified for --emit-relocs. */
2878 Elf_Internal_Rela *relocs;
2880 /* Section contents. */
2884 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2885 the reloc to be in the range -32768 to 32767. */
2886 unsigned int has_small_toc_reloc : 1;
2888 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2889 instruction not one we handle. */
2890 unsigned int unexpected_toc_insn : 1;
2893 #define ppc64_elf_tdata(bfd) \
2894 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2896 #define ppc64_tlsld_got(bfd) \
2897 (&ppc64_elf_tdata (bfd)->tlsld_got)
2899 #define is_ppc64_elf(bfd) \
2900 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2901 && elf_object_id (bfd) == PPC64_ELF_DATA)
2903 /* Override the generic function because we store some extras. */
2906 ppc64_elf_mkobject (bfd *abfd)
2908 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2912 /* Fix bad default arch selected for a 64 bit input bfd when the
2913 default is 32 bit. Also select arch based on apuinfo. */
2916 ppc64_elf_object_p (bfd *abfd)
2918 if (!abfd->arch_info->the_default)
2921 if (abfd->arch_info->bits_per_word == 32)
2923 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2925 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2927 /* Relies on arch after 32 bit default being 64 bit default. */
2928 abfd->arch_info = abfd->arch_info->next;
2929 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2932 return _bfd_elf_ppc_set_arch (abfd);
2935 /* Support for core dump NOTE sections. */
2938 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2940 size_t offset, size;
2942 if (note->descsz != 504)
2946 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2949 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2955 /* Make a ".reg/999" section. */
2956 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2957 size, note->descpos + offset);
2961 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2963 if (note->descsz != 136)
2966 elf_tdata (abfd)->core->pid
2967 = bfd_get_32 (abfd, note->descdata + 24);
2968 elf_tdata (abfd)->core->program
2969 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2970 elf_tdata (abfd)->core->command
2971 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2977 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2990 va_start (ap, note_type);
2991 memset (data, 0, sizeof (data));
2992 strncpy (data + 40, va_arg (ap, const char *), 16);
2993 strncpy (data + 56, va_arg (ap, const char *), 80);
2995 return elfcore_write_note (abfd, buf, bufsiz,
2996 "CORE", note_type, data, sizeof (data));
3007 va_start (ap, note_type);
3008 memset (data, 0, 112);
3009 pid = va_arg (ap, long);
3010 bfd_put_32 (abfd, pid, data + 32);
3011 cursig = va_arg (ap, int);
3012 bfd_put_16 (abfd, cursig, data + 12);
3013 greg = va_arg (ap, const void *);
3014 memcpy (data + 112, greg, 384);
3015 memset (data + 496, 0, 8);
3017 return elfcore_write_note (abfd, buf, bufsiz,
3018 "CORE", note_type, data, sizeof (data));
3023 /* Add extra PPC sections. */
3025 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3027 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3028 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3029 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3030 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3031 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3032 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3033 { NULL, 0, 0, 0, 0 }
3036 enum _ppc64_sec_type {
3042 struct _ppc64_elf_section_data
3044 struct bfd_elf_section_data elf;
3048 /* An array with one entry for each opd function descriptor,
3049 and some spares since opd entries may be either 16 or 24 bytes. */
3050 #define OPD_NDX(OFF) ((OFF) >> 4)
3051 struct _opd_sec_data
3053 /* Points to the function code section for local opd entries. */
3054 asection **func_sec;
3056 /* After editing .opd, adjust references to opd local syms. */
3060 /* An array for toc sections, indexed by offset/8. */
3061 struct _toc_sec_data
3063 /* Specifies the relocation symbol index used at a given toc offset. */
3066 /* And the relocation addend. */
3071 enum _ppc64_sec_type sec_type:2;
3073 /* Flag set when small branches are detected. Used to
3074 select suitable defaults for the stub group size. */
3075 unsigned int has_14bit_branch:1;
3078 #define ppc64_elf_section_data(sec) \
3079 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3082 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3084 if (!sec->used_by_bfd)
3086 struct _ppc64_elf_section_data *sdata;
3087 bfd_size_type amt = sizeof (*sdata);
3089 sdata = bfd_zalloc (abfd, amt);
3092 sec->used_by_bfd = sdata;
3095 return _bfd_elf_new_section_hook (abfd, sec);
3098 static struct _opd_sec_data *
3099 get_opd_info (asection * sec)
3102 && ppc64_elf_section_data (sec) != NULL
3103 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3104 return &ppc64_elf_section_data (sec)->u.opd;
3108 /* Parameters for the qsort hook. */
3109 static bfd_boolean synthetic_relocatable;
3110 static asection *synthetic_opd;
3112 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
3115 compare_symbols (const void *ap, const void *bp)
3117 const asymbol *a = * (const asymbol **) ap;
3118 const asymbol *b = * (const asymbol **) bp;
3120 /* Section symbols first. */
3121 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3123 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3126 /* then .opd symbols. */
3127 if (synthetic_opd != NULL)
3129 if (strcmp (a->section->name, ".opd") == 0
3130 && strcmp (b->section->name, ".opd") != 0)
3132 if (strcmp (a->section->name, ".opd") != 0
3133 && strcmp (b->section->name, ".opd") == 0)
3137 /* then other code symbols. */
3138 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3139 == (SEC_CODE | SEC_ALLOC)
3140 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3141 != (SEC_CODE | SEC_ALLOC))
3144 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3145 != (SEC_CODE | SEC_ALLOC)
3146 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3147 == (SEC_CODE | SEC_ALLOC))
3150 if (synthetic_relocatable)
3152 if (a->section->id < b->section->id)
3155 if (a->section->id > b->section->id)
3159 if (a->value + a->section->vma < b->value + b->section->vma)
3162 if (a->value + a->section->vma > b->value + b->section->vma)
3165 /* For syms with the same value, prefer strong dynamic global function
3166 syms over other syms. */
3167 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3170 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3173 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3176 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3179 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3182 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3185 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3188 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3194 /* Search SYMS for a symbol of the given VALUE. */
3197 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3201 if (id == (unsigned) -1)
3205 mid = (lo + hi) >> 1;
3206 if (syms[mid]->value + syms[mid]->section->vma < value)
3208 else if (syms[mid]->value + syms[mid]->section->vma > value)
3218 mid = (lo + hi) >> 1;
3219 if (syms[mid]->section->id < id)
3221 else if (syms[mid]->section->id > id)
3223 else if (syms[mid]->value < value)
3225 else if (syms[mid]->value > value)
3235 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3237 bfd_vma vma = *(bfd_vma *) ptr;
3238 return ((section->flags & SEC_ALLOC) != 0
3239 && section->vma <= vma
3240 && vma < section->vma + section->size);
3243 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3244 entry syms. Also generate @plt symbols for the glink branch table.
3245 Returns count of synthetic symbols in RET or -1 on error. */
3248 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3249 long static_count, asymbol **static_syms,
3250 long dyn_count, asymbol **dyn_syms,
3257 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3258 asection *opd = NULL;
3259 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3261 int abi = abiversion (abfd);
3267 opd = bfd_get_section_by_name (abfd, ".opd");
3268 if (opd == NULL && abi == 1)
3272 symcount = static_count;
3274 symcount += dyn_count;
3278 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3282 if (!relocatable && static_count != 0 && dyn_count != 0)
3284 /* Use both symbol tables. */
3285 memcpy (syms, static_syms, static_count * sizeof (*syms));
3286 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3288 else if (!relocatable && static_count == 0)
3289 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3291 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3293 synthetic_relocatable = relocatable;
3294 synthetic_opd = opd;
3295 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3297 if (!relocatable && symcount > 1)
3300 /* Trim duplicate syms, since we may have merged the normal and
3301 dynamic symbols. Actually, we only care about syms that have
3302 different values, so trim any with the same value. */
3303 for (i = 1, j = 1; i < symcount; ++i)
3304 if (syms[i - 1]->value + syms[i - 1]->section->vma
3305 != syms[i]->value + syms[i]->section->vma)
3306 syms[j++] = syms[i];
3311 /* Note that here and in compare_symbols we can't compare opd and
3312 sym->section directly. With separate debug info files, the
3313 symbols will be extracted from the debug file while abfd passed
3314 to this function is the real binary. */
3315 if (opd != NULL && strcmp (syms[i]->section->name, ".opd") == 0)
3319 for (; i < symcount; ++i)
3320 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3321 != (SEC_CODE | SEC_ALLOC))
3322 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3326 for (; i < symcount; ++i)
3327 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3332 for (; i < symcount; ++i)
3333 if (strcmp (syms[i]->section->name, ".opd") != 0)
3337 for (; i < symcount; ++i)
3338 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3339 != (SEC_CODE | SEC_ALLOC))
3347 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3352 if (opdsymend == secsymend)
3355 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3356 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3360 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3367 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3371 while (r < opd->relocation + relcount
3372 && r->address < syms[i]->value + opd->vma)
3375 if (r == opd->relocation + relcount)
3378 if (r->address != syms[i]->value + opd->vma)
3381 if (r->howto->type != R_PPC64_ADDR64)
3384 sym = *r->sym_ptr_ptr;
3385 if (!sym_exists_at (syms, opdsymend, symcount,
3386 sym->section->id, sym->value + r->addend))
3389 size += sizeof (asymbol);
3390 size += strlen (syms[i]->name) + 2;
3396 s = *ret = bfd_malloc (size);
3403 names = (char *) (s + count);
3405 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3409 while (r < opd->relocation + relcount
3410 && r->address < syms[i]->value + opd->vma)
3413 if (r == opd->relocation + relcount)
3416 if (r->address != syms[i]->value + opd->vma)
3419 if (r->howto->type != R_PPC64_ADDR64)
3422 sym = *r->sym_ptr_ptr;
3423 if (!sym_exists_at (syms, opdsymend, symcount,
3424 sym->section->id, sym->value + r->addend))
3429 s->flags |= BSF_SYNTHETIC;
3430 s->section = sym->section;
3431 s->value = sym->value + r->addend;
3434 len = strlen (syms[i]->name);
3435 memcpy (names, syms[i]->name, len + 1);
3437 /* Have udata.p point back to the original symbol this
3438 synthetic symbol was derived from. */
3439 s->udata.p = syms[i];
3446 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3447 bfd_byte *contents = NULL;
3450 bfd_vma glink_vma = 0, resolv_vma = 0;
3451 asection *dynamic, *glink = NULL, *relplt = NULL;
3454 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3456 free_contents_and_exit_err:
3458 free_contents_and_exit:
3465 for (i = secsymend; i < opdsymend; ++i)
3469 /* Ignore bogus symbols. */
3470 if (syms[i]->value > opd->size - 8)
3473 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3474 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3477 size += sizeof (asymbol);
3478 size += strlen (syms[i]->name) + 2;
3482 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3484 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3486 bfd_byte *dynbuf, *extdyn, *extdynend;
3488 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3490 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3491 goto free_contents_and_exit_err;
3493 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3494 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3497 extdynend = extdyn + dynamic->size;
3498 for (; extdyn < extdynend; extdyn += extdynsize)
3500 Elf_Internal_Dyn dyn;
3501 (*swap_dyn_in) (abfd, extdyn, &dyn);
3503 if (dyn.d_tag == DT_NULL)
3506 if (dyn.d_tag == DT_PPC64_GLINK)
3508 /* The first glink stub starts at offset 32; see
3509 comment in ppc64_elf_finish_dynamic_sections. */
3510 glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
3511 /* The .glink section usually does not survive the final
3512 link; search for the section (usually .text) where the
3513 glink stubs now reside. */
3514 glink = bfd_sections_find_if (abfd, section_covers_vma,
3525 /* Determine __glink trampoline by reading the relative branch
3526 from the first glink stub. */
3528 unsigned int off = 0;
3530 while (bfd_get_section_contents (abfd, glink, buf,
3531 glink_vma + off - glink->vma, 4))
3533 unsigned int insn = bfd_get_32 (abfd, buf);
3535 if ((insn & ~0x3fffffc) == 0)
3537 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3546 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3548 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3551 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3552 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3553 goto free_contents_and_exit_err;
3555 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3556 size += plt_count * sizeof (asymbol);
3558 p = relplt->relocation;
3559 for (i = 0; i < plt_count; i++, p++)
3561 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3563 size += sizeof ("+0x") - 1 + 16;
3569 goto free_contents_and_exit;
3570 s = *ret = bfd_malloc (size);
3572 goto free_contents_and_exit_err;
3574 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3576 for (i = secsymend; i < opdsymend; ++i)
3580 if (syms[i]->value > opd->size - 8)
3583 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3584 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3588 asection *sec = abfd->sections;
3595 long mid = (lo + hi) >> 1;
3596 if (syms[mid]->section->vma < ent)
3598 else if (syms[mid]->section->vma > ent)
3602 sec = syms[mid]->section;
3607 if (lo >= hi && lo > codesecsym)
3608 sec = syms[lo - 1]->section;
3610 for (; sec != NULL; sec = sec->next)
3614 /* SEC_LOAD may not be set if SEC is from a separate debug
3616 if ((sec->flags & SEC_ALLOC) == 0)
3618 if ((sec->flags & SEC_CODE) != 0)
3621 s->flags |= BSF_SYNTHETIC;
3622 s->value = ent - s->section->vma;
3625 len = strlen (syms[i]->name);
3626 memcpy (names, syms[i]->name, len + 1);
3628 /* Have udata.p point back to the original symbol this
3629 synthetic symbol was derived from. */
3630 s->udata.p = syms[i];
3636 if (glink != NULL && relplt != NULL)
3640 /* Add a symbol for the main glink trampoline. */
3641 memset (s, 0, sizeof *s);
3643 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3645 s->value = resolv_vma - glink->vma;
3647 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3648 names += sizeof ("__glink_PLTresolve");
3653 /* FIXME: It would be very much nicer to put sym@plt on the
3654 stub rather than on the glink branch table entry. The
3655 objdump disassembler would then use a sensible symbol
3656 name on plt calls. The difficulty in doing so is
3657 a) finding the stubs, and,
3658 b) matching stubs against plt entries, and,
3659 c) there can be multiple stubs for a given plt entry.
3661 Solving (a) could be done by code scanning, but older
3662 ppc64 binaries used different stubs to current code.
3663 (b) is the tricky one since you need to known the toc
3664 pointer for at least one function that uses a pic stub to
3665 be able to calculate the plt address referenced.
3666 (c) means gdb would need to set multiple breakpoints (or
3667 find the glink branch itself) when setting breakpoints
3668 for pending shared library loads. */
3669 p = relplt->relocation;
3670 for (i = 0; i < plt_count; i++, p++)
3674 *s = **p->sym_ptr_ptr;
3675 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3676 we are defining a symbol, ensure one of them is set. */
3677 if ((s->flags & BSF_LOCAL) == 0)
3678 s->flags |= BSF_GLOBAL;
3679 s->flags |= BSF_SYNTHETIC;
3681 s->value = glink_vma - glink->vma;
3684 len = strlen ((*p->sym_ptr_ptr)->name);
3685 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3689 memcpy (names, "+0x", sizeof ("+0x") - 1);
3690 names += sizeof ("+0x") - 1;
3691 bfd_sprintf_vma (abfd, names, p->addend);
3692 names += strlen (names);
3694 memcpy (names, "@plt", sizeof ("@plt"));
3695 names += sizeof ("@plt");
3715 /* The following functions are specific to the ELF linker, while
3716 functions above are used generally. Those named ppc64_elf_* are
3717 called by the main ELF linker code. They appear in this file more
3718 or less in the order in which they are called. eg.
3719 ppc64_elf_check_relocs is called early in the link process,
3720 ppc64_elf_finish_dynamic_sections is one of the last functions
3723 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3724 functions have both a function code symbol and a function descriptor
3725 symbol. A call to foo in a relocatable object file looks like:
3732 The function definition in another object file might be:
3736 . .quad .TOC.@tocbase
3742 When the linker resolves the call during a static link, the branch
3743 unsurprisingly just goes to .foo and the .opd information is unused.
3744 If the function definition is in a shared library, things are a little
3745 different: The call goes via a plt call stub, the opd information gets
3746 copied to the plt, and the linker patches the nop.
3754 . std 2,40(1) # in practice, the call stub
3755 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3756 . addi 11,11,Lfoo@toc@l # this is the general idea
3764 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3766 The "reloc ()" notation is supposed to indicate that the linker emits
3767 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3770 What are the difficulties here? Well, firstly, the relocations
3771 examined by the linker in check_relocs are against the function code
3772 sym .foo, while the dynamic relocation in the plt is emitted against
3773 the function descriptor symbol, foo. Somewhere along the line, we need
3774 to carefully copy dynamic link information from one symbol to the other.
3775 Secondly, the generic part of the elf linker will make .foo a dynamic
3776 symbol as is normal for most other backends. We need foo dynamic
3777 instead, at least for an application final link. However, when
3778 creating a shared library containing foo, we need to have both symbols
3779 dynamic so that references to .foo are satisfied during the early
3780 stages of linking. Otherwise the linker might decide to pull in a
3781 definition from some other object, eg. a static library.
3783 Update: As of August 2004, we support a new convention. Function
3784 calls may use the function descriptor symbol, ie. "bl foo". This
3785 behaves exactly as "bl .foo". */
3787 /* Of those relocs that might be copied as dynamic relocs, this function
3788 selects those that must be copied when linking a shared library,
3789 even when the symbol is local. */
3792 must_be_dyn_reloc (struct bfd_link_info *info,
3793 enum elf_ppc64_reloc_type r_type)
3805 case R_PPC64_TPREL16:
3806 case R_PPC64_TPREL16_LO:
3807 case R_PPC64_TPREL16_HI:
3808 case R_PPC64_TPREL16_HA:
3809 case R_PPC64_TPREL16_DS:
3810 case R_PPC64_TPREL16_LO_DS:
3811 case R_PPC64_TPREL16_HIGH:
3812 case R_PPC64_TPREL16_HIGHA:
3813 case R_PPC64_TPREL16_HIGHER:
3814 case R_PPC64_TPREL16_HIGHERA:
3815 case R_PPC64_TPREL16_HIGHEST:
3816 case R_PPC64_TPREL16_HIGHESTA:
3817 case R_PPC64_TPREL64:
3818 return !bfd_link_executable (info);
3822 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3823 copying dynamic variables from a shared lib into an app's dynbss
3824 section, and instead use a dynamic relocation to point into the
3825 shared lib. With code that gcc generates, it's vital that this be
3826 enabled; In the PowerPC64 ABI, the address of a function is actually
3827 the address of a function descriptor, which resides in the .opd
3828 section. gcc uses the descriptor directly rather than going via the
3829 GOT as some other ABI's do, which means that initialized function
3830 pointers must reference the descriptor. Thus, a function pointer
3831 initialized to the address of a function in a shared library will
3832 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3833 redefines the function descriptor symbol to point to the copy. This
3834 presents a problem as a plt entry for that function is also
3835 initialized from the function descriptor symbol and the copy reloc
3836 may not be initialized first. */
3837 #define ELIMINATE_COPY_RELOCS 1
3839 /* Section name for stubs is the associated section name plus this
3841 #define STUB_SUFFIX ".stub"
3844 ppc_stub_long_branch:
3845 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3846 destination, but a 24 bit branch in a stub section will reach.
3849 ppc_stub_plt_branch:
3850 Similar to the above, but a 24 bit branch in the stub section won't
3851 reach its destination.
3852 . addis %r11,%r2,xxx@toc@ha
3853 . ld %r12,xxx@toc@l(%r11)
3858 Used to call a function in a shared library. If it so happens that
3859 the plt entry referenced crosses a 64k boundary, then an extra
3860 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3862 . addis %r11,%r2,xxx@toc@ha
3863 . ld %r12,xxx+0@toc@l(%r11)
3865 . ld %r2,xxx+8@toc@l(%r11)
3866 . ld %r11,xxx+16@toc@l(%r11)
3869 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3870 code to adjust the value and save r2 to support multiple toc sections.
3871 A ppc_stub_long_branch with an r2 offset looks like:
3873 . addis %r2,%r2,off@ha
3874 . addi %r2,%r2,off@l
3877 A ppc_stub_plt_branch with an r2 offset looks like:
3879 . addis %r11,%r2,xxx@toc@ha
3880 . ld %r12,xxx@toc@l(%r11)
3881 . addis %r2,%r2,off@ha
3882 . addi %r2,%r2,off@l
3886 In cases where the "addis" instruction would add zero, the "addis" is
3887 omitted and following instructions modified slightly in some cases.
3890 enum ppc_stub_type {
3892 ppc_stub_long_branch,
3893 ppc_stub_long_branch_r2off,
3894 ppc_stub_plt_branch,
3895 ppc_stub_plt_branch_r2off,
3897 ppc_stub_plt_call_r2save,
3898 ppc_stub_global_entry,
3902 /* Information on stub grouping. */
3905 /* The stub section. */
3907 /* This is the section to which stubs in the group will be attached. */
3910 struct map_stub *next;
3911 /* Whether to emit a copy of register save/restore functions in this
3916 struct ppc_stub_hash_entry {
3918 /* Base hash table entry structure. */
3919 struct bfd_hash_entry root;
3921 enum ppc_stub_type stub_type;
3923 /* Group information. */
3924 struct map_stub *group;
3926 /* Offset within stub_sec of the beginning of this stub. */
3927 bfd_vma stub_offset;
3929 /* Given the symbol's value and its section we can determine its final
3930 value when building the stubs (so the stub knows where to jump. */
3931 bfd_vma target_value;
3932 asection *target_section;
3934 /* The symbol table entry, if any, that this was derived from. */
3935 struct ppc_link_hash_entry *h;
3936 struct plt_entry *plt_ent;
3938 /* Symbol st_other. */
3939 unsigned char other;
3942 struct ppc_branch_hash_entry {
3944 /* Base hash table entry structure. */
3945 struct bfd_hash_entry root;
3947 /* Offset within branch lookup table. */
3948 unsigned int offset;
3950 /* Generation marker. */
3954 /* Used to track dynamic relocations for local symbols. */
3955 struct ppc_dyn_relocs
3957 struct ppc_dyn_relocs *next;
3959 /* The input section of the reloc. */
3962 /* Total number of relocs copied for the input section. */
3963 unsigned int count : 31;
3965 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3966 unsigned int ifunc : 1;
3969 struct ppc_link_hash_entry
3971 struct elf_link_hash_entry elf;
3974 /* A pointer to the most recently used stub hash entry against this
3976 struct ppc_stub_hash_entry *stub_cache;
3978 /* A pointer to the next symbol starting with a '.' */
3979 struct ppc_link_hash_entry *next_dot_sym;
3982 /* Track dynamic relocs copied for this symbol. */
3983 struct elf_dyn_relocs *dyn_relocs;
3985 /* Chain of aliases referring to a weakdef. */
3986 struct ppc_link_hash_entry *weakref;
3988 /* Link between function code and descriptor symbols. */
3989 struct ppc_link_hash_entry *oh;
3991 /* Flag function code and descriptor symbols. */
3992 unsigned int is_func:1;
3993 unsigned int is_func_descriptor:1;
3994 unsigned int fake:1;
3996 /* Whether global opd/toc sym has been adjusted or not.
3997 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3998 should be set for all globals defined in any opd/toc section. */
3999 unsigned int adjust_done:1;
4001 /* Set if this is an out-of-line register save/restore function,
4002 with non-standard calling convention. */
4003 unsigned int save_res:1;
4005 /* Contexts in which symbol is used in the GOT (or TOC).
4006 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
4007 corresponding relocs are encountered during check_relocs.
4008 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
4009 indicate the corresponding GOT entry type is not needed.
4010 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
4011 a TPREL one. We use a separate flag rather than setting TPREL
4012 just for convenience in distinguishing the two cases. */
4013 #define TLS_GD 1 /* GD reloc. */
4014 #define TLS_LD 2 /* LD reloc. */
4015 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
4016 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
4017 #define TLS_TLS 16 /* Any TLS reloc. */
4018 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
4019 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
4020 #define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
4021 unsigned char tls_mask;
4024 /* ppc64 ELF linker hash table. */
4026 struct ppc_link_hash_table
4028 struct elf_link_hash_table elf;
4030 /* The stub hash table. */
4031 struct bfd_hash_table stub_hash_table;
4033 /* Another hash table for plt_branch stubs. */
4034 struct bfd_hash_table branch_hash_table;
4036 /* Hash table for function prologue tocsave. */
4037 htab_t tocsave_htab;
4039 /* Various options and other info passed from the linker. */
4040 struct ppc64_elf_params *params;
4042 /* The size of sec_info below. */
4043 unsigned int sec_info_arr_size;
4045 /* Per-section array of extra section info. Done this way rather
4046 than as part of ppc64_elf_section_data so we have the info for
4047 non-ppc64 sections. */
4050 /* Along with elf_gp, specifies the TOC pointer used by this section. */
4055 /* The section group that this section belongs to. */
4056 struct map_stub *group;
4057 /* A temp section list pointer. */
4062 /* Linked list of groups. */
4063 struct map_stub *group;
4065 /* Temp used when calculating TOC pointers. */
4068 asection *toc_first_sec;
4070 /* Used when adding symbols. */
4071 struct ppc_link_hash_entry *dot_syms;
4073 /* Shortcuts to get to dynamic linker sections. */
4078 asection *glink_eh_frame;
4080 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4081 struct ppc_link_hash_entry *tls_get_addr;
4082 struct ppc_link_hash_entry *tls_get_addr_fd;
4084 /* The size of reliplt used by got entry relocs. */
4085 bfd_size_type got_reli_size;
4088 unsigned long stub_count[ppc_stub_global_entry];
4090 /* Number of stubs against global syms. */
4091 unsigned long stub_globals;
4093 /* Set if we're linking code with function descriptors. */
4094 unsigned int opd_abi:1;
4096 /* Support for multiple toc sections. */
4097 unsigned int do_multi_toc:1;
4098 unsigned int multi_toc_needed:1;
4099 unsigned int second_toc_pass:1;
4100 unsigned int do_toc_opt:1;
4103 unsigned int stub_error:1;
4105 /* Whether func_desc_adjust needs to be run over symbols. */
4106 unsigned int need_func_desc_adj:1;
4108 /* Whether there exist local gnu indirect function resolvers,
4109 referenced by dynamic relocations. */
4110 unsigned int local_ifunc_resolver:1;
4111 unsigned int maybe_local_ifunc_resolver:1;
4113 /* Incremented every time we size stubs. */
4114 unsigned int stub_iteration;
4116 /* Small local sym cache. */
4117 struct sym_cache sym_cache;
4120 /* Rename some of the generic section flags to better document how they
4123 /* Nonzero if this section has TLS related relocations. */
4124 #define has_tls_reloc sec_flg0
4126 /* Nonzero if this section has a call to __tls_get_addr. */
4127 #define has_tls_get_addr_call sec_flg1
4129 /* Nonzero if this section has any toc or got relocs. */
4130 #define has_toc_reloc sec_flg2
4132 /* Nonzero if this section has a call to another section that uses
4134 #define makes_toc_func_call sec_flg3
4136 /* Recursion protection when determining above flag. */
4137 #define call_check_in_progress sec_flg4
4138 #define call_check_done sec_flg5
4140 /* Get the ppc64 ELF linker hash table from a link_info structure. */
4142 #define ppc_hash_table(p) \
4143 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4144 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4146 #define ppc_stub_hash_lookup(table, string, create, copy) \
4147 ((struct ppc_stub_hash_entry *) \
4148 bfd_hash_lookup ((table), (string), (create), (copy)))
4150 #define ppc_branch_hash_lookup(table, string, create, copy) \
4151 ((struct ppc_branch_hash_entry *) \
4152 bfd_hash_lookup ((table), (string), (create), (copy)))
4154 /* Create an entry in the stub hash table. */
4156 static struct bfd_hash_entry *
4157 stub_hash_newfunc (struct bfd_hash_entry *entry,
4158 struct bfd_hash_table *table,
4161 /* Allocate the structure if it has not already been allocated by a
4165 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4170 /* Call the allocation method of the superclass. */
4171 entry = bfd_hash_newfunc (entry, table, string);
4174 struct ppc_stub_hash_entry *eh;
4176 /* Initialize the local fields. */
4177 eh = (struct ppc_stub_hash_entry *) entry;
4178 eh->stub_type = ppc_stub_none;
4180 eh->stub_offset = 0;
4181 eh->target_value = 0;
4182 eh->target_section = NULL;
4191 /* Create an entry in the branch hash table. */
4193 static struct bfd_hash_entry *
4194 branch_hash_newfunc (struct bfd_hash_entry *entry,
4195 struct bfd_hash_table *table,
4198 /* Allocate the structure if it has not already been allocated by a
4202 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4207 /* Call the allocation method of the superclass. */
4208 entry = bfd_hash_newfunc (entry, table, string);
4211 struct ppc_branch_hash_entry *eh;
4213 /* Initialize the local fields. */
4214 eh = (struct ppc_branch_hash_entry *) entry;
4222 /* Create an entry in a ppc64 ELF linker hash table. */
4224 static struct bfd_hash_entry *
4225 link_hash_newfunc (struct bfd_hash_entry *entry,
4226 struct bfd_hash_table *table,
4229 /* Allocate the structure if it has not already been allocated by a
4233 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4238 /* Call the allocation method of the superclass. */
4239 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4242 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4244 memset (&eh->u.stub_cache, 0,
4245 (sizeof (struct ppc_link_hash_entry)
4246 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4248 /* When making function calls, old ABI code references function entry
4249 points (dot symbols), while new ABI code references the function
4250 descriptor symbol. We need to make any combination of reference and
4251 definition work together, without breaking archive linking.
4253 For a defined function "foo" and an undefined call to "bar":
4254 An old object defines "foo" and ".foo", references ".bar" (possibly
4256 A new object defines "foo" and references "bar".
4258 A new object thus has no problem with its undefined symbols being
4259 satisfied by definitions in an old object. On the other hand, the
4260 old object won't have ".bar" satisfied by a new object.
4262 Keep a list of newly added dot-symbols. */
4264 if (string[0] == '.')
4266 struct ppc_link_hash_table *htab;
4268 htab = (struct ppc_link_hash_table *) table;
4269 eh->u.next_dot_sym = htab->dot_syms;
4270 htab->dot_syms = eh;
4277 struct tocsave_entry {
4283 tocsave_htab_hash (const void *p)
4285 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4286 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
4290 tocsave_htab_eq (const void *p1, const void *p2)
4292 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4293 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4294 return e1->sec == e2->sec && e1->offset == e2->offset;
4297 /* Destroy a ppc64 ELF linker hash table. */
4300 ppc64_elf_link_hash_table_free (bfd *obfd)
4302 struct ppc_link_hash_table *htab;
4304 htab = (struct ppc_link_hash_table *) obfd->link.hash;
4305 if (htab->tocsave_htab)
4306 htab_delete (htab->tocsave_htab);
4307 bfd_hash_table_free (&htab->branch_hash_table);
4308 bfd_hash_table_free (&htab->stub_hash_table);
4309 _bfd_elf_link_hash_table_free (obfd);
4312 /* Create a ppc64 ELF linker hash table. */
4314 static struct bfd_link_hash_table *
4315 ppc64_elf_link_hash_table_create (bfd *abfd)
4317 struct ppc_link_hash_table *htab;
4318 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4320 htab = bfd_zmalloc (amt);
4324 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4325 sizeof (struct ppc_link_hash_entry),
4332 /* Init the stub hash table too. */
4333 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4334 sizeof (struct ppc_stub_hash_entry)))
4336 _bfd_elf_link_hash_table_free (abfd);
4340 /* And the branch hash table. */
4341 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4342 sizeof (struct ppc_branch_hash_entry)))
4344 bfd_hash_table_free (&htab->stub_hash_table);
4345 _bfd_elf_link_hash_table_free (abfd);
4349 htab->tocsave_htab = htab_try_create (1024,
4353 if (htab->tocsave_htab == NULL)
4355 ppc64_elf_link_hash_table_free (abfd);
4358 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4360 /* Initializing two fields of the union is just cosmetic. We really
4361 only care about glist, but when compiled on a 32-bit host the
4362 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4363 debugger inspection of these fields look nicer. */
4364 htab->elf.init_got_refcount.refcount = 0;
4365 htab->elf.init_got_refcount.glist = NULL;
4366 htab->elf.init_plt_refcount.refcount = 0;
4367 htab->elf.init_plt_refcount.glist = NULL;
4368 htab->elf.init_got_offset.offset = 0;
4369 htab->elf.init_got_offset.glist = NULL;
4370 htab->elf.init_plt_offset.offset = 0;
4371 htab->elf.init_plt_offset.glist = NULL;
4373 return &htab->elf.root;
4376 /* Create sections for linker generated code. */
4379 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4381 struct ppc_link_hash_table *htab;
4384 htab = ppc_hash_table (info);
4386 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4387 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4388 if (htab->params->save_restore_funcs)
4390 /* Create .sfpr for code to save and restore fp regs. */
4391 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4393 if (htab->sfpr == NULL
4394 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4398 if (bfd_link_relocatable (info))
4401 /* Create .glink for lazy dynamic linking support. */
4402 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4404 if (htab->glink == NULL
4405 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4408 if (!info->no_ld_generated_unwind_info)
4410 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4411 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4412 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4415 if (htab->glink_eh_frame == NULL
4416 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4420 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4421 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4422 if (htab->elf.iplt == NULL
4423 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4426 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4427 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4429 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4430 if (htab->elf.irelplt == NULL
4431 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4434 /* Create branch lookup table for plt_branch stubs. */
4435 flags = (SEC_ALLOC | SEC_LOAD
4436 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4437 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4439 if (htab->brlt == NULL
4440 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4443 if (!bfd_link_pic (info))
4446 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4447 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4448 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4451 if (htab->relbrlt == NULL
4452 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4458 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4461 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4462 struct ppc64_elf_params *params)
4464 struct ppc_link_hash_table *htab;
4466 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4468 /* Always hook our dynamic sections into the first bfd, which is the
4469 linker created stub bfd. This ensures that the GOT header is at
4470 the start of the output TOC section. */
4471 htab = ppc_hash_table (info);
4472 htab->elf.dynobj = params->stub_bfd;
4473 htab->params = params;
4475 return create_linkage_sections (htab->elf.dynobj, info);
4478 /* Build a name for an entry in the stub hash table. */
4481 ppc_stub_name (const asection *input_section,
4482 const asection *sym_sec,
4483 const struct ppc_link_hash_entry *h,
4484 const Elf_Internal_Rela *rel)
4489 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4490 offsets from a sym as a branch target? In fact, we could
4491 probably assume the addend is always zero. */
4492 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4496 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4497 stub_name = bfd_malloc (len);
4498 if (stub_name == NULL)
4501 len = sprintf (stub_name, "%08x.%s+%x",
4502 input_section->id & 0xffffffff,
4503 h->elf.root.root.string,
4504 (int) rel->r_addend & 0xffffffff);
4508 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4509 stub_name = bfd_malloc (len);
4510 if (stub_name == NULL)
4513 len = sprintf (stub_name, "%08x.%x:%x+%x",
4514 input_section->id & 0xffffffff,
4515 sym_sec->id & 0xffffffff,
4516 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4517 (int) rel->r_addend & 0xffffffff);
4519 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4520 stub_name[len - 2] = 0;
4524 /* Look up an entry in the stub hash. Stub entries are cached because
4525 creating the stub name takes a bit of time. */
4527 static struct ppc_stub_hash_entry *
4528 ppc_get_stub_entry (const asection *input_section,
4529 const asection *sym_sec,
4530 struct ppc_link_hash_entry *h,
4531 const Elf_Internal_Rela *rel,
4532 struct ppc_link_hash_table *htab)
4534 struct ppc_stub_hash_entry *stub_entry;
4535 struct map_stub *group;
4537 /* If this input section is part of a group of sections sharing one
4538 stub section, then use the id of the first section in the group.
4539 Stub names need to include a section id, as there may well be
4540 more than one stub used to reach say, printf, and we need to
4541 distinguish between them. */
4542 group = htab->sec_info[input_section->id].u.group;
4546 if (h != NULL && h->u.stub_cache != NULL
4547 && h->u.stub_cache->h == h
4548 && h->u.stub_cache->group == group)
4550 stub_entry = h->u.stub_cache;
4556 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4557 if (stub_name == NULL)
4560 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4561 stub_name, FALSE, FALSE);
4563 h->u.stub_cache = stub_entry;
4571 /* Add a new stub entry to the stub hash. Not all fields of the new
4572 stub entry are initialised. */
4574 static struct ppc_stub_hash_entry *
4575 ppc_add_stub (const char *stub_name,
4577 struct bfd_link_info *info)
4579 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4580 struct map_stub *group;
4583 struct ppc_stub_hash_entry *stub_entry;
4585 group = htab->sec_info[section->id].u.group;
4586 link_sec = group->link_sec;
4587 stub_sec = group->stub_sec;
4588 if (stub_sec == NULL)
4594 namelen = strlen (link_sec->name);
4595 len = namelen + sizeof (STUB_SUFFIX);
4596 s_name = bfd_alloc (htab->params->stub_bfd, len);
4600 memcpy (s_name, link_sec->name, namelen);
4601 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4602 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4603 if (stub_sec == NULL)
4605 group->stub_sec = stub_sec;
4608 /* Enter this entry into the linker stub hash table. */
4609 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4611 if (stub_entry == NULL)
4613 /* xgettext:c-format */
4614 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4615 section->owner, stub_name);
4619 stub_entry->group = group;
4620 stub_entry->stub_offset = 0;
4624 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4625 not already done. */
4628 create_got_section (bfd *abfd, struct bfd_link_info *info)
4630 asection *got, *relgot;
4632 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4634 if (!is_ppc64_elf (abfd))
4640 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4643 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4644 | SEC_LINKER_CREATED);
4646 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4648 || !bfd_set_section_alignment (abfd, got, 3))
4651 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4652 flags | SEC_READONLY);
4654 || ! bfd_set_section_alignment (abfd, relgot, 3))
4657 ppc64_elf_tdata (abfd)->got = got;
4658 ppc64_elf_tdata (abfd)->relgot = relgot;
4662 /* Follow indirect and warning symbol links. */
4664 static inline struct bfd_link_hash_entry *
4665 follow_link (struct bfd_link_hash_entry *h)
4667 while (h->type == bfd_link_hash_indirect
4668 || h->type == bfd_link_hash_warning)
4673 static inline struct elf_link_hash_entry *
4674 elf_follow_link (struct elf_link_hash_entry *h)
4676 return (struct elf_link_hash_entry *) follow_link (&h->root);
4679 static inline struct ppc_link_hash_entry *
4680 ppc_follow_link (struct ppc_link_hash_entry *h)
4682 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4685 /* Merge PLT info on FROM with that on TO. */
4688 move_plt_plist (struct ppc_link_hash_entry *from,
4689 struct ppc_link_hash_entry *to)
4691 if (from->elf.plt.plist != NULL)
4693 if (to->elf.plt.plist != NULL)
4695 struct plt_entry **entp;
4696 struct plt_entry *ent;
4698 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4700 struct plt_entry *dent;
4702 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4703 if (dent->addend == ent->addend)
4705 dent->plt.refcount += ent->plt.refcount;
4712 *entp = to->elf.plt.plist;
4715 to->elf.plt.plist = from->elf.plt.plist;
4716 from->elf.plt.plist = NULL;
4720 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4723 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4724 struct elf_link_hash_entry *dir,
4725 struct elf_link_hash_entry *ind)
4727 struct ppc_link_hash_entry *edir, *eind;
4729 edir = (struct ppc_link_hash_entry *) dir;
4730 eind = (struct ppc_link_hash_entry *) ind;
4732 edir->is_func |= eind->is_func;
4733 edir->is_func_descriptor |= eind->is_func_descriptor;
4734 edir->tls_mask |= eind->tls_mask;
4735 if (eind->oh != NULL)
4736 edir->oh = ppc_follow_link (eind->oh);
4738 /* If called to transfer flags for a weakdef during processing
4739 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4740 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4741 if (!(ELIMINATE_COPY_RELOCS
4742 && eind->elf.root.type != bfd_link_hash_indirect
4743 && edir->elf.dynamic_adjusted))
4744 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4746 if (edir->elf.versioned != versioned_hidden)
4747 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4748 edir->elf.ref_regular |= eind->elf.ref_regular;
4749 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4750 edir->elf.needs_plt |= eind->elf.needs_plt;
4751 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4753 /* If we were called to copy over info for a weak sym, don't copy
4754 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4755 in order to simplify readonly_dynrelocs and save a field in the
4756 symbol hash entry, but that means dyn_relocs can't be used in any
4757 tests about a specific symbol, or affect other symbol flags which
4759 Chain weakdefs so we can get from the weakdef back to an alias.
4760 The list is circular so that we don't need to use u.weakdef as
4761 well as this list to look at all aliases. */
4762 if (eind->elf.root.type != bfd_link_hash_indirect)
4764 struct ppc_link_hash_entry *cur, *add, *next;
4769 cur = edir->weakref;
4774 /* We can be called twice for the same symbols.
4775 Don't make multiple loops. */
4779 } while (cur != edir);
4781 next = add->weakref;
4784 add->weakref = edir->weakref != NULL ? edir->weakref : edir;
4785 edir->weakref = add;
4788 } while (add != NULL && add != eind);
4792 /* Copy over any dynamic relocs we may have on the indirect sym. */
4793 if (eind->dyn_relocs != NULL)
4795 if (edir->dyn_relocs != NULL)
4797 struct elf_dyn_relocs **pp;
4798 struct elf_dyn_relocs *p;
4800 /* Add reloc counts against the indirect sym to the direct sym
4801 list. Merge any entries against the same section. */
4802 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4804 struct elf_dyn_relocs *q;
4806 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4807 if (q->sec == p->sec)
4809 q->pc_count += p->pc_count;
4810 q->count += p->count;
4817 *pp = edir->dyn_relocs;
4820 edir->dyn_relocs = eind->dyn_relocs;
4821 eind->dyn_relocs = NULL;
4824 /* Copy over got entries that we may have already seen to the
4825 symbol which just became indirect. */
4826 if (eind->elf.got.glist != NULL)
4828 if (edir->elf.got.glist != NULL)
4830 struct got_entry **entp;
4831 struct got_entry *ent;
4833 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4835 struct got_entry *dent;
4837 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4838 if (dent->addend == ent->addend
4839 && dent->owner == ent->owner
4840 && dent->tls_type == ent->tls_type)
4842 dent->got.refcount += ent->got.refcount;
4849 *entp = edir->elf.got.glist;
4852 edir->elf.got.glist = eind->elf.got.glist;
4853 eind->elf.got.glist = NULL;
4856 /* And plt entries. */
4857 move_plt_plist (eind, edir);
4859 if (eind->elf.dynindx != -1)
4861 if (edir->elf.dynindx != -1)
4862 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4863 edir->elf.dynstr_index);
4864 edir->elf.dynindx = eind->elf.dynindx;
4865 edir->elf.dynstr_index = eind->elf.dynstr_index;
4866 eind->elf.dynindx = -1;
4867 eind->elf.dynstr_index = 0;
4871 /* Find the function descriptor hash entry from the given function code
4872 hash entry FH. Link the entries via their OH fields. */
4874 static struct ppc_link_hash_entry *
4875 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4877 struct ppc_link_hash_entry *fdh = fh->oh;
4881 const char *fd_name = fh->elf.root.root.string + 1;
4883 fdh = (struct ppc_link_hash_entry *)
4884 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4888 fdh->is_func_descriptor = 1;
4894 fdh = ppc_follow_link (fdh);
4895 fdh->is_func_descriptor = 1;
4900 /* Make a fake function descriptor sym for the undefined code sym FH. */
4902 static struct ppc_link_hash_entry *
4903 make_fdh (struct bfd_link_info *info,
4904 struct ppc_link_hash_entry *fh)
4906 bfd *abfd = fh->elf.root.u.undef.abfd;
4907 struct bfd_link_hash_entry *bh = NULL;
4908 struct ppc_link_hash_entry *fdh;
4909 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4913 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4914 fh->elf.root.root.string + 1,
4915 flags, bfd_und_section_ptr, 0,
4916 NULL, FALSE, FALSE, &bh))
4919 fdh = (struct ppc_link_hash_entry *) bh;
4920 fdh->elf.non_elf = 0;
4922 fdh->is_func_descriptor = 1;
4929 /* Fix function descriptor symbols defined in .opd sections to be
4933 ppc64_elf_add_symbol_hook (bfd *ibfd,
4934 struct bfd_link_info *info,
4935 Elf_Internal_Sym *isym,
4937 flagword *flags ATTRIBUTE_UNUSED,
4941 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4942 && (ibfd->flags & DYNAMIC) == 0
4943 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4944 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
4947 && strcmp ((*sec)->name, ".opd") == 0)
4951 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4952 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4953 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4955 /* If the symbol is a function defined in .opd, and the function
4956 code is in a discarded group, let it appear to be undefined. */
4957 if (!bfd_link_relocatable (info)
4958 && (*sec)->reloc_count != 0
4959 && opd_entry_value (*sec, *value, &code_sec, NULL,
4960 FALSE) != (bfd_vma) -1
4961 && discarded_section (code_sec))
4963 *sec = bfd_und_section_ptr;
4964 isym->st_shndx = SHN_UNDEF;
4967 else if (*sec != NULL
4968 && strcmp ((*sec)->name, ".toc") == 0
4969 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4971 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4973 htab->params->object_in_toc = 1;
4976 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4978 if (abiversion (ibfd) == 0)
4979 set_abiversion (ibfd, 2);
4980 else if (abiversion (ibfd) == 1)
4982 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4983 " for ABI version 1\n"), name);
4984 bfd_set_error (bfd_error_bad_value);
4992 /* Merge non-visibility st_other attributes: local entry point. */
4995 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4996 const Elf_Internal_Sym *isym,
4997 bfd_boolean definition,
4998 bfd_boolean dynamic)
5000 if (definition && !dynamic)
5001 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5002 | ELF_ST_VISIBILITY (h->other));
5005 /* Hook called on merging a symbol. We use this to clear "fake" since
5006 we now have a real symbol. */
5009 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5010 const Elf_Internal_Sym *isym ATTRIBUTE_UNUSED,
5011 asection **psec ATTRIBUTE_UNUSED,
5012 bfd_boolean newdef ATTRIBUTE_UNUSED,
5013 bfd_boolean olddef ATTRIBUTE_UNUSED,
5014 bfd *oldbfd ATTRIBUTE_UNUSED,
5015 const asection *oldsec ATTRIBUTE_UNUSED)
5017 ((struct ppc_link_hash_entry *) h)->fake = 0;
5021 /* This function makes an old ABI object reference to ".bar" cause the
5022 inclusion of a new ABI object archive that defines "bar".
5023 NAME is a symbol defined in an archive. Return a symbol in the hash
5024 table that might be satisfied by the archive symbols. */
5026 static struct elf_link_hash_entry *
5027 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5028 struct bfd_link_info *info,
5031 struct elf_link_hash_entry *h;
5035 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5037 /* Don't return this sym if it is a fake function descriptor
5038 created by add_symbol_adjust. */
5039 && !((struct ppc_link_hash_entry *) h)->fake)
5045 len = strlen (name);
5046 dot_name = bfd_alloc (abfd, len + 2);
5047 if (dot_name == NULL)
5048 return (struct elf_link_hash_entry *) 0 - 1;
5050 memcpy (dot_name + 1, name, len + 1);
5051 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5052 bfd_release (abfd, dot_name);
5056 /* This function satisfies all old ABI object references to ".bar" if a
5057 new ABI object defines "bar". Well, at least, undefined dot symbols
5058 are made weak. This stops later archive searches from including an
5059 object if we already have a function descriptor definition. It also
5060 prevents the linker complaining about undefined symbols.
5061 We also check and correct mismatched symbol visibility here. The
5062 most restrictive visibility of the function descriptor and the
5063 function entry symbol is used. */
5066 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5068 struct ppc_link_hash_table *htab;
5069 struct ppc_link_hash_entry *fdh;
5071 if (eh->elf.root.type == bfd_link_hash_warning)
5072 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5074 if (eh->elf.root.type == bfd_link_hash_indirect)
5077 if (eh->elf.root.root.string[0] != '.')
5080 htab = ppc_hash_table (info);
5084 fdh = lookup_fdh (eh, htab);
5086 && !bfd_link_relocatable (info)
5087 && (eh->elf.root.type == bfd_link_hash_undefined
5088 || eh->elf.root.type == bfd_link_hash_undefweak)
5089 && eh->elf.ref_regular)
5091 /* Make an undefined function descriptor sym, in order to
5092 pull in an --as-needed shared lib. Archives are handled
5094 fdh = make_fdh (info, eh);
5101 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5102 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5104 /* Make both descriptor and entry symbol have the most
5105 constraining visibility of either symbol. */
5106 if (entry_vis < descr_vis)
5107 fdh->elf.other += entry_vis - descr_vis;
5108 else if (entry_vis > descr_vis)
5109 eh->elf.other += descr_vis - entry_vis;
5111 /* Propagate reference flags from entry symbol to function
5112 descriptor symbol. */
5113 fdh->elf.root.non_ir_ref |= eh->elf.root.non_ir_ref;
5114 fdh->elf.ref_regular |= eh->elf.ref_regular;
5115 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5117 if (!fdh->elf.forced_local
5118 && fdh->elf.dynindx == -1
5119 && fdh->elf.versioned != versioned_hidden
5120 && (bfd_link_dll (info)
5121 || fdh->elf.def_dynamic
5122 || fdh->elf.ref_dynamic)
5123 && (eh->elf.ref_regular
5124 || eh->elf.def_regular))
5126 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5134 /* Set up opd section info and abiversion for IBFD, and process list
5135 of dot-symbols we made in link_hash_newfunc. */
5138 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5140 struct ppc_link_hash_table *htab;
5141 struct ppc_link_hash_entry **p, *eh;
5142 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5144 if (opd != NULL && opd->size != 0)
5146 if (abiversion (ibfd) == 0)
5147 set_abiversion (ibfd, 1);
5148 else if (abiversion (ibfd) >= 2)
5150 /* xgettext:c-format */
5151 info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5153 ibfd, abiversion (ibfd));
5154 bfd_set_error (bfd_error_bad_value);
5158 if ((ibfd->flags & DYNAMIC) == 0
5159 && (opd->flags & SEC_RELOC) != 0
5160 && opd->reloc_count != 0
5161 && !bfd_is_abs_section (opd->output_section))
5163 /* Garbage collection needs some extra help with .opd sections.
5164 We don't want to necessarily keep everything referenced by
5165 relocs in .opd, as that would keep all functions. Instead,
5166 if we reference an .opd symbol (a function descriptor), we
5167 want to keep the function code symbol's section. This is
5168 easy for global symbols, but for local syms we need to keep
5169 information about the associated function section. */
5171 asection **opd_sym_map;
5173 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5174 opd_sym_map = bfd_zalloc (ibfd, amt);
5175 if (opd_sym_map == NULL)
5177 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5178 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5179 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5183 if (!is_ppc64_elf (info->output_bfd))
5185 htab = ppc_hash_table (info);
5189 /* For input files without an explicit abiversion in e_flags
5190 we should have flagged any with symbol st_other bits set
5191 as ELFv1 and above flagged those with .opd as ELFv2.
5192 Set the output abiversion if not yet set, and for any input
5193 still ambiguous, take its abiversion from the output.
5194 Differences in ABI are reported later. */
5195 if (abiversion (info->output_bfd) == 0)
5196 set_abiversion (info->output_bfd, abiversion (ibfd));
5197 else if (abiversion (ibfd) == 0)
5198 set_abiversion (ibfd, abiversion (info->output_bfd));
5200 p = &htab->dot_syms;
5201 while ((eh = *p) != NULL)
5204 if (&eh->elf == htab->elf.hgot)
5206 else if (htab->elf.hgot == NULL
5207 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5208 htab->elf.hgot = &eh->elf;
5209 else if (abiversion (ibfd) <= 1)
5211 htab->need_func_desc_adj = 1;
5212 if (!add_symbol_adjust (eh, info))
5215 p = &eh->u.next_dot_sym;
5220 /* Undo hash table changes when an --as-needed input file is determined
5221 not to be needed. */
5224 ppc64_elf_notice_as_needed (bfd *ibfd,
5225 struct bfd_link_info *info,
5226 enum notice_asneeded_action act)
5228 if (act == notice_not_needed)
5230 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5235 htab->dot_syms = NULL;
5237 return _bfd_elf_notice_as_needed (ibfd, info, act);
5240 /* If --just-symbols against a final linked binary, then assume we need
5241 toc adjusting stubs when calling functions defined there. */
5244 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5246 if ((sec->flags & SEC_CODE) != 0
5247 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5248 && is_ppc64_elf (sec->owner))
5250 if (abiversion (sec->owner) >= 2
5251 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5252 sec->has_toc_reloc = 1;
5254 _bfd_elf_link_just_syms (sec, info);
5257 static struct plt_entry **
5258 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5259 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5261 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5262 struct plt_entry **local_plt;
5263 unsigned char *local_got_tls_masks;
5265 if (local_got_ents == NULL)
5267 bfd_size_type size = symtab_hdr->sh_info;
5269 size *= (sizeof (*local_got_ents)
5270 + sizeof (*local_plt)
5271 + sizeof (*local_got_tls_masks));
5272 local_got_ents = bfd_zalloc (abfd, size);
5273 if (local_got_ents == NULL)
5275 elf_local_got_ents (abfd) = local_got_ents;
5278 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5280 struct got_entry *ent;
5282 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5283 if (ent->addend == r_addend
5284 && ent->owner == abfd
5285 && ent->tls_type == tls_type)
5289 bfd_size_type amt = sizeof (*ent);
5290 ent = bfd_alloc (abfd, amt);
5293 ent->next = local_got_ents[r_symndx];
5294 ent->addend = r_addend;
5296 ent->tls_type = tls_type;
5297 ent->is_indirect = FALSE;
5298 ent->got.refcount = 0;
5299 local_got_ents[r_symndx] = ent;
5301 ent->got.refcount += 1;
5304 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5305 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5306 local_got_tls_masks[r_symndx] |= tls_type;
5308 return local_plt + r_symndx;
5312 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5314 struct plt_entry *ent;
5316 for (ent = *plist; ent != NULL; ent = ent->next)
5317 if (ent->addend == addend)
5321 bfd_size_type amt = sizeof (*ent);
5322 ent = bfd_alloc (abfd, amt);
5326 ent->addend = addend;
5327 ent->plt.refcount = 0;
5330 ent->plt.refcount += 1;
5335 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5337 return (r_type == R_PPC64_REL24
5338 || r_type == R_PPC64_REL14
5339 || r_type == R_PPC64_REL14_BRTAKEN
5340 || r_type == R_PPC64_REL14_BRNTAKEN
5341 || r_type == R_PPC64_ADDR24
5342 || r_type == R_PPC64_ADDR14
5343 || r_type == R_PPC64_ADDR14_BRTAKEN
5344 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5347 /* Look through the relocs for a section during the first phase, and
5348 calculate needed space in the global offset table, procedure
5349 linkage table, and dynamic reloc sections. */
5352 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5353 asection *sec, const Elf_Internal_Rela *relocs)
5355 struct ppc_link_hash_table *htab;
5356 Elf_Internal_Shdr *symtab_hdr;
5357 struct elf_link_hash_entry **sym_hashes;
5358 const Elf_Internal_Rela *rel;
5359 const Elf_Internal_Rela *rel_end;
5361 asection **opd_sym_map;
5362 struct elf_link_hash_entry *tga, *dottga;
5364 if (bfd_link_relocatable (info))
5367 /* Don't do anything special with non-loaded, non-alloced sections.
5368 In particular, any relocs in such sections should not affect GOT
5369 and PLT reference counting (ie. we don't allow them to create GOT
5370 or PLT entries), there's no possibility or desire to optimize TLS
5371 relocs, and there's not much point in propagating relocs to shared
5372 libs that the dynamic linker won't relocate. */
5373 if ((sec->flags & SEC_ALLOC) == 0)
5376 BFD_ASSERT (is_ppc64_elf (abfd));
5378 htab = ppc_hash_table (info);
5382 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5383 FALSE, FALSE, TRUE);
5384 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5385 FALSE, FALSE, TRUE);
5386 symtab_hdr = &elf_symtab_hdr (abfd);
5387 sym_hashes = elf_sym_hashes (abfd);
5390 if (ppc64_elf_section_data (sec) != NULL
5391 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5392 opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5394 rel_end = relocs + sec->reloc_count;
5395 for (rel = relocs; rel < rel_end; rel++)
5397 unsigned long r_symndx;
5398 struct elf_link_hash_entry *h;
5399 enum elf_ppc64_reloc_type r_type;
5401 struct _ppc64_elf_section_data *ppc64_sec;
5402 struct plt_entry **ifunc, **plt_list;
5404 r_symndx = ELF64_R_SYM (rel->r_info);
5405 if (r_symndx < symtab_hdr->sh_info)
5409 struct ppc_link_hash_entry *eh;
5411 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5412 h = elf_follow_link (h);
5413 eh = (struct ppc_link_hash_entry *) h;
5415 /* PR15323, ref flags aren't set for references in the same
5417 h->root.non_ir_ref = 1;
5418 if (eh->is_func && eh->oh != NULL)
5419 eh->oh->elf.root.non_ir_ref = 1;
5421 if (h == htab->elf.hgot)
5422 sec->has_toc_reloc = 1;
5429 if (h->type == STT_GNU_IFUNC)
5432 ifunc = &h->plt.plist;
5437 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5442 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5444 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5445 rel->r_addend, PLT_IFUNC);
5451 r_type = ELF64_R_TYPE (rel->r_info);
5456 /* These special tls relocs tie a call to __tls_get_addr with
5457 its parameter symbol. */
5460 case R_PPC64_GOT_TLSLD16:
5461 case R_PPC64_GOT_TLSLD16_LO:
5462 case R_PPC64_GOT_TLSLD16_HI:
5463 case R_PPC64_GOT_TLSLD16_HA:
5464 tls_type = TLS_TLS | TLS_LD;
5467 case R_PPC64_GOT_TLSGD16:
5468 case R_PPC64_GOT_TLSGD16_LO:
5469 case R_PPC64_GOT_TLSGD16_HI:
5470 case R_PPC64_GOT_TLSGD16_HA:
5471 tls_type = TLS_TLS | TLS_GD;
5474 case R_PPC64_GOT_TPREL16_DS:
5475 case R_PPC64_GOT_TPREL16_LO_DS:
5476 case R_PPC64_GOT_TPREL16_HI:
5477 case R_PPC64_GOT_TPREL16_HA:
5478 if (bfd_link_pic (info))
5479 info->flags |= DF_STATIC_TLS;
5480 tls_type = TLS_TLS | TLS_TPREL;
5483 case R_PPC64_GOT_DTPREL16_DS:
5484 case R_PPC64_GOT_DTPREL16_LO_DS:
5485 case R_PPC64_GOT_DTPREL16_HI:
5486 case R_PPC64_GOT_DTPREL16_HA:
5487 tls_type = TLS_TLS | TLS_DTPREL;
5489 sec->has_tls_reloc = 1;
5493 case R_PPC64_GOT16_DS:
5494 case R_PPC64_GOT16_HA:
5495 case R_PPC64_GOT16_HI:
5496 case R_PPC64_GOT16_LO:
5497 case R_PPC64_GOT16_LO_DS:
5498 /* This symbol requires a global offset table entry. */
5499 sec->has_toc_reloc = 1;
5500 if (r_type == R_PPC64_GOT_TLSLD16
5501 || r_type == R_PPC64_GOT_TLSGD16
5502 || r_type == R_PPC64_GOT_TPREL16_DS
5503 || r_type == R_PPC64_GOT_DTPREL16_DS
5504 || r_type == R_PPC64_GOT16
5505 || r_type == R_PPC64_GOT16_DS)
5507 htab->do_multi_toc = 1;
5508 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5511 if (ppc64_elf_tdata (abfd)->got == NULL
5512 && !create_got_section (abfd, info))
5517 struct ppc_link_hash_entry *eh;
5518 struct got_entry *ent;
5520 eh = (struct ppc_link_hash_entry *) h;
5521 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5522 if (ent->addend == rel->r_addend
5523 && ent->owner == abfd
5524 && ent->tls_type == tls_type)
5528 bfd_size_type amt = sizeof (*ent);
5529 ent = bfd_alloc (abfd, amt);
5532 ent->next = eh->elf.got.glist;
5533 ent->addend = rel->r_addend;
5535 ent->tls_type = tls_type;
5536 ent->is_indirect = FALSE;
5537 ent->got.refcount = 0;
5538 eh->elf.got.glist = ent;
5540 ent->got.refcount += 1;
5541 eh->tls_mask |= tls_type;
5544 /* This is a global offset table entry for a local symbol. */
5545 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5546 rel->r_addend, tls_type))
5549 /* We may also need a plt entry if the symbol turns out to be
5551 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5553 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5558 case R_PPC64_PLT16_HA:
5559 case R_PPC64_PLT16_HI:
5560 case R_PPC64_PLT16_LO:
5563 /* This symbol requires a procedure linkage table entry. */
5568 if (h->root.root.string[0] == '.'
5569 && h->root.root.string[1] != '\0')
5570 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5571 plt_list = &h->plt.plist;
5573 if (plt_list == NULL)
5575 /* It does not make sense to have a procedure linkage
5576 table entry for a non-ifunc local symbol. */
5577 info->callbacks->einfo
5578 /* xgettext:c-format */
5579 (_("%H: %s reloc against local symbol\n"),
5580 abfd, sec, rel->r_offset,
5581 ppc64_elf_howto_table[r_type]->name);
5582 bfd_set_error (bfd_error_bad_value);
5585 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5589 /* The following relocations don't need to propagate the
5590 relocation if linking a shared object since they are
5591 section relative. */
5592 case R_PPC64_SECTOFF:
5593 case R_PPC64_SECTOFF_LO:
5594 case R_PPC64_SECTOFF_HI:
5595 case R_PPC64_SECTOFF_HA:
5596 case R_PPC64_SECTOFF_DS:
5597 case R_PPC64_SECTOFF_LO_DS:
5598 case R_PPC64_DTPREL16:
5599 case R_PPC64_DTPREL16_LO:
5600 case R_PPC64_DTPREL16_HI:
5601 case R_PPC64_DTPREL16_HA:
5602 case R_PPC64_DTPREL16_DS:
5603 case R_PPC64_DTPREL16_LO_DS:
5604 case R_PPC64_DTPREL16_HIGH:
5605 case R_PPC64_DTPREL16_HIGHA:
5606 case R_PPC64_DTPREL16_HIGHER:
5607 case R_PPC64_DTPREL16_HIGHERA:
5608 case R_PPC64_DTPREL16_HIGHEST:
5609 case R_PPC64_DTPREL16_HIGHESTA:
5614 case R_PPC64_REL16_LO:
5615 case R_PPC64_REL16_HI:
5616 case R_PPC64_REL16_HA:
5617 case R_PPC64_REL16DX_HA:
5620 /* Not supported as a dynamic relocation. */
5621 case R_PPC64_ADDR64_LOCAL:
5622 if (bfd_link_pic (info))
5624 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5626 /* xgettext:c-format */
5627 info->callbacks->einfo (_("%H: %s reloc unsupported "
5628 "in shared libraries and PIEs.\n"),
5629 abfd, sec, rel->r_offset,
5630 ppc64_elf_howto_table[r_type]->name);
5631 bfd_set_error (bfd_error_bad_value);
5637 case R_PPC64_TOC16_DS:
5638 htab->do_multi_toc = 1;
5639 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5641 case R_PPC64_TOC16_LO:
5642 case R_PPC64_TOC16_HI:
5643 case R_PPC64_TOC16_HA:
5644 case R_PPC64_TOC16_LO_DS:
5645 sec->has_toc_reloc = 1;
5652 /* This relocation describes the C++ object vtable hierarchy.
5653 Reconstruct it for later use during GC. */
5654 case R_PPC64_GNU_VTINHERIT:
5655 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5659 /* This relocation describes which C++ vtable entries are actually
5660 used. Record for later use during GC. */
5661 case R_PPC64_GNU_VTENTRY:
5662 BFD_ASSERT (h != NULL);
5664 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5669 case R_PPC64_REL14_BRTAKEN:
5670 case R_PPC64_REL14_BRNTAKEN:
5672 asection *dest = NULL;
5674 /* Heuristic: If jumping outside our section, chances are
5675 we are going to need a stub. */
5678 /* If the sym is weak it may be overridden later, so
5679 don't assume we know where a weak sym lives. */
5680 if (h->root.type == bfd_link_hash_defined)
5681 dest = h->root.u.def.section;
5685 Elf_Internal_Sym *isym;
5687 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5692 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5696 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5705 if (h->root.root.string[0] == '.'
5706 && h->root.root.string[1] != '\0')
5707 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5709 if (h == tga || h == dottga)
5711 sec->has_tls_reloc = 1;
5713 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5714 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5715 /* We have a new-style __tls_get_addr call with
5719 /* Mark this section as having an old-style call. */
5720 sec->has_tls_get_addr_call = 1;
5722 plt_list = &h->plt.plist;
5725 /* We may need a .plt entry if the function this reloc
5726 refers to is in a shared lib. */
5728 && !update_plt_info (abfd, plt_list, rel->r_addend))
5732 case R_PPC64_ADDR14:
5733 case R_PPC64_ADDR14_BRNTAKEN:
5734 case R_PPC64_ADDR14_BRTAKEN:
5735 case R_PPC64_ADDR24:
5738 case R_PPC64_TPREL64:
5739 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5740 if (bfd_link_pic (info))
5741 info->flags |= DF_STATIC_TLS;
5744 case R_PPC64_DTPMOD64:
5745 if (rel + 1 < rel_end
5746 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5747 && rel[1].r_offset == rel->r_offset + 8)
5748 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5750 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5753 case R_PPC64_DTPREL64:
5754 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5756 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5757 && rel[-1].r_offset == rel->r_offset - 8)
5758 /* This is the second reloc of a dtpmod, dtprel pair.
5759 Don't mark with TLS_DTPREL. */
5763 sec->has_tls_reloc = 1;
5766 struct ppc_link_hash_entry *eh;
5767 eh = (struct ppc_link_hash_entry *) h;
5768 eh->tls_mask |= tls_type;
5771 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5772 rel->r_addend, tls_type))
5775 ppc64_sec = ppc64_elf_section_data (sec);
5776 if (ppc64_sec->sec_type != sec_toc)
5780 /* One extra to simplify get_tls_mask. */
5781 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5782 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5783 if (ppc64_sec->u.toc.symndx == NULL)
5785 amt = sec->size * sizeof (bfd_vma) / 8;
5786 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5787 if (ppc64_sec->u.toc.add == NULL)
5789 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5790 ppc64_sec->sec_type = sec_toc;
5792 BFD_ASSERT (rel->r_offset % 8 == 0);
5793 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5794 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5796 /* Mark the second slot of a GD or LD entry.
5797 -1 to indicate GD and -2 to indicate LD. */
5798 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5799 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5800 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5801 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5804 case R_PPC64_TPREL16:
5805 case R_PPC64_TPREL16_LO:
5806 case R_PPC64_TPREL16_HI:
5807 case R_PPC64_TPREL16_HA:
5808 case R_PPC64_TPREL16_DS:
5809 case R_PPC64_TPREL16_LO_DS:
5810 case R_PPC64_TPREL16_HIGH:
5811 case R_PPC64_TPREL16_HIGHA:
5812 case R_PPC64_TPREL16_HIGHER:
5813 case R_PPC64_TPREL16_HIGHERA:
5814 case R_PPC64_TPREL16_HIGHEST:
5815 case R_PPC64_TPREL16_HIGHESTA:
5816 if (bfd_link_pic (info))
5818 info->flags |= DF_STATIC_TLS;
5823 case R_PPC64_ADDR64:
5824 if (opd_sym_map != NULL
5825 && rel + 1 < rel_end
5826 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5829 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5833 Elf_Internal_Sym *isym;
5835 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5840 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5841 if (s != NULL && s != sec)
5842 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5847 case R_PPC64_ADDR16:
5848 case R_PPC64_ADDR16_DS:
5849 case R_PPC64_ADDR16_HA:
5850 case R_PPC64_ADDR16_HI:
5851 case R_PPC64_ADDR16_HIGH:
5852 case R_PPC64_ADDR16_HIGHA:
5853 case R_PPC64_ADDR16_HIGHER:
5854 case R_PPC64_ADDR16_HIGHERA:
5855 case R_PPC64_ADDR16_HIGHEST:
5856 case R_PPC64_ADDR16_HIGHESTA:
5857 case R_PPC64_ADDR16_LO:
5858 case R_PPC64_ADDR16_LO_DS:
5859 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5860 && rel->r_addend == 0)
5862 /* We may need a .plt entry if this reloc refers to a
5863 function in a shared lib. */
5864 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5866 h->pointer_equality_needed = 1;
5873 case R_PPC64_ADDR32:
5874 case R_PPC64_UADDR16:
5875 case R_PPC64_UADDR32:
5876 case R_PPC64_UADDR64:
5878 if (h != NULL && !bfd_link_pic (info))
5879 /* We may need a copy reloc. */
5882 /* Don't propagate .opd relocs. */
5883 if (NO_OPD_RELOCS && opd_sym_map != NULL)
5886 /* If we are creating a shared library, and this is a reloc
5887 against a global symbol, or a non PC relative reloc
5888 against a local symbol, then we need to copy the reloc
5889 into the shared library. However, if we are linking with
5890 -Bsymbolic, we do not need to copy a reloc against a
5891 global symbol which is defined in an object we are
5892 including in the link (i.e., DEF_REGULAR is set). At
5893 this point we have not seen all the input files, so it is
5894 possible that DEF_REGULAR is not set now but will be set
5895 later (it is never cleared). In case of a weak definition,
5896 DEF_REGULAR may be cleared later by a strong definition in
5897 a shared library. We account for that possibility below by
5898 storing information in the dyn_relocs field of the hash
5899 table entry. A similar situation occurs when creating
5900 shared libraries and symbol visibility changes render the
5903 If on the other hand, we are creating an executable, we
5904 may need to keep relocations for symbols satisfied by a
5905 dynamic library if we manage to avoid copy relocs for the
5908 if ((bfd_link_pic (info)
5909 && (must_be_dyn_reloc (info, r_type)
5911 && (!SYMBOLIC_BIND (info, h)
5912 || h->root.type == bfd_link_hash_defweak
5913 || !h->def_regular))))
5914 || (ELIMINATE_COPY_RELOCS
5915 && !bfd_link_pic (info)
5917 && (h->root.type == bfd_link_hash_defweak
5918 || !h->def_regular))
5919 || (!bfd_link_pic (info)
5922 /* We must copy these reloc types into the output file.
5923 Create a reloc section in dynobj and make room for
5927 sreloc = _bfd_elf_make_dynamic_reloc_section
5928 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5934 /* If this is a global symbol, we count the number of
5935 relocations we need for this symbol. */
5938 struct elf_dyn_relocs *p;
5939 struct elf_dyn_relocs **head;
5941 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5943 if (p == NULL || p->sec != sec)
5945 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5955 if (!must_be_dyn_reloc (info, r_type))
5960 /* Track dynamic relocs needed for local syms too.
5961 We really need local syms available to do this
5963 struct ppc_dyn_relocs *p;
5964 struct ppc_dyn_relocs **head;
5965 bfd_boolean is_ifunc;
5968 Elf_Internal_Sym *isym;
5970 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5975 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5979 vpp = &elf_section_data (s)->local_dynrel;
5980 head = (struct ppc_dyn_relocs **) vpp;
5981 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5983 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5985 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5987 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5993 p->ifunc = is_ifunc;
6009 /* Merge backend specific data from an object file to the output
6010 object file when linking. */
6013 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6015 bfd *obfd = info->output_bfd;
6016 unsigned long iflags, oflags;
6018 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6021 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6024 if (!_bfd_generic_verify_endian_match (ibfd, info))
6027 iflags = elf_elfheader (ibfd)->e_flags;
6028 oflags = elf_elfheader (obfd)->e_flags;
6030 if (iflags & ~EF_PPC64_ABI)
6033 /* xgettext:c-format */
6034 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
6035 bfd_set_error (bfd_error_bad_value);
6038 else if (iflags != oflags && iflags != 0)
6041 /* xgettext:c-format */
6042 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
6043 ibfd, iflags, oflags);
6044 bfd_set_error (bfd_error_bad_value);
6048 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
6050 /* Merge Tag_compatibility attributes and any common GNU ones. */
6051 _bfd_elf_merge_object_attributes (ibfd, info);
6057 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6059 /* Print normal ELF private data. */
6060 _bfd_elf_print_private_bfd_data (abfd, ptr);
6062 if (elf_elfheader (abfd)->e_flags != 0)
6066 fprintf (file, _("private flags = 0x%lx:"),
6067 elf_elfheader (abfd)->e_flags);
6069 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6070 fprintf (file, _(" [abiv%ld]"),
6071 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6078 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
6079 of the code entry point, and its section, which must be in the same
6080 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
6083 opd_entry_value (asection *opd_sec,
6085 asection **code_sec,
6087 bfd_boolean in_code_sec)
6089 bfd *opd_bfd = opd_sec->owner;
6090 Elf_Internal_Rela *relocs;
6091 Elf_Internal_Rela *lo, *hi, *look;
6094 /* No relocs implies we are linking a --just-symbols object, or looking
6095 at a final linked executable with addr2line or somesuch. */
6096 if (opd_sec->reloc_count == 0)
6098 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6100 if (contents == NULL)
6102 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6103 return (bfd_vma) -1;
6104 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6107 /* PR 17512: file: 64b9dfbb. */
6108 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6109 return (bfd_vma) -1;
6111 val = bfd_get_64 (opd_bfd, contents + offset);
6112 if (code_sec != NULL)
6114 asection *sec, *likely = NULL;
6120 && val < sec->vma + sec->size)
6126 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6128 && (sec->flags & SEC_LOAD) != 0
6129 && (sec->flags & SEC_ALLOC) != 0)
6134 if (code_off != NULL)
6135 *code_off = val - likely->vma;
6141 BFD_ASSERT (is_ppc64_elf (opd_bfd));
6143 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6145 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6146 /* PR 17512: file: df8e1fd6. */
6148 return (bfd_vma) -1;
6150 /* Go find the opd reloc at the sym address. */
6152 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6156 look = lo + (hi - lo) / 2;
6157 if (look->r_offset < offset)
6159 else if (look->r_offset > offset)
6163 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6165 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6166 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6168 unsigned long symndx = ELF64_R_SYM (look->r_info);
6169 asection *sec = NULL;
6171 if (symndx >= symtab_hdr->sh_info
6172 && elf_sym_hashes (opd_bfd) != NULL)
6174 struct elf_link_hash_entry **sym_hashes;
6175 struct elf_link_hash_entry *rh;
6177 sym_hashes = elf_sym_hashes (opd_bfd);
6178 rh = sym_hashes[symndx - symtab_hdr->sh_info];
6181 rh = elf_follow_link (rh);
6182 if (rh->root.type != bfd_link_hash_defined
6183 && rh->root.type != bfd_link_hash_defweak)
6185 if (rh->root.u.def.section->owner == opd_bfd)
6187 val = rh->root.u.def.value;
6188 sec = rh->root.u.def.section;
6195 Elf_Internal_Sym *sym;
6197 if (symndx < symtab_hdr->sh_info)
6199 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6202 size_t symcnt = symtab_hdr->sh_info;
6203 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6208 symtab_hdr->contents = (bfd_byte *) sym;
6214 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6220 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6223 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6224 val = sym->st_value;
6227 val += look->r_addend;
6228 if (code_off != NULL)
6230 if (code_sec != NULL)
6232 if (in_code_sec && *code_sec != sec)
6237 if (sec->output_section != NULL)
6238 val += sec->output_section->vma + sec->output_offset;
6247 /* If the ELF symbol SYM might be a function in SEC, return the
6248 function size and set *CODE_OFF to the function's entry point,
6249 otherwise return zero. */
6251 static bfd_size_type
6252 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6257 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6258 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6262 if (!(sym->flags & BSF_SYNTHETIC))
6263 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6265 if (strcmp (sym->section->name, ".opd") == 0)
6267 struct _opd_sec_data *opd = get_opd_info (sym->section);
6268 bfd_vma symval = sym->value;
6271 && opd->adjust != NULL
6272 && elf_section_data (sym->section)->relocs != NULL)
6274 /* opd_entry_value will use cached relocs that have been
6275 adjusted, but with raw symbols. That means both local
6276 and global symbols need adjusting. */
6277 long adjust = opd->adjust[OPD_NDX (symval)];
6283 if (opd_entry_value (sym->section, symval,
6284 &sec, code_off, TRUE) == (bfd_vma) -1)
6286 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6287 symbol. This size has nothing to do with the code size of the
6288 function, which is what we're supposed to return, but the
6289 code size isn't available without looking up the dot-sym.
6290 However, doing that would be a waste of time particularly
6291 since elf_find_function will look at the dot-sym anyway.
6292 Now, elf_find_function will keep the largest size of any
6293 function sym found at the code address of interest, so return
6294 1 here to avoid it incorrectly caching a larger function size
6295 for a small function. This does mean we return the wrong
6296 size for a new-ABI function of size 24, but all that does is
6297 disable caching for such functions. */
6303 if (sym->section != sec)
6305 *code_off = sym->value;
6312 /* Return true if symbol is defined in a regular object file. */
6315 is_static_defined (struct elf_link_hash_entry *h)
6317 return ((h->root.type == bfd_link_hash_defined
6318 || h->root.type == bfd_link_hash_defweak)
6319 && h->root.u.def.section != NULL
6320 && h->root.u.def.section->output_section != NULL);
6323 /* If FDH is a function descriptor symbol, return the associated code
6324 entry symbol if it is defined. Return NULL otherwise. */
6326 static struct ppc_link_hash_entry *
6327 defined_code_entry (struct ppc_link_hash_entry *fdh)
6329 if (fdh->is_func_descriptor)
6331 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6332 if (fh->elf.root.type == bfd_link_hash_defined
6333 || fh->elf.root.type == bfd_link_hash_defweak)
6339 /* If FH is a function code entry symbol, return the associated
6340 function descriptor symbol if it is defined. Return NULL otherwise. */
6342 static struct ppc_link_hash_entry *
6343 defined_func_desc (struct ppc_link_hash_entry *fh)
6346 && fh->oh->is_func_descriptor)
6348 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6349 if (fdh->elf.root.type == bfd_link_hash_defined
6350 || fdh->elf.root.type == bfd_link_hash_defweak)
6356 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6358 /* Garbage collect sections, after first dealing with dot-symbols. */
6361 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6363 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6365 if (htab != NULL && htab->need_func_desc_adj)
6367 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6368 htab->need_func_desc_adj = 0;
6370 return bfd_elf_gc_sections (abfd, info);
6373 /* Mark all our entry sym sections, both opd and code section. */
6376 ppc64_elf_gc_keep (struct bfd_link_info *info)
6378 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6379 struct bfd_sym_chain *sym;
6384 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6386 struct ppc_link_hash_entry *eh, *fh;
6389 eh = (struct ppc_link_hash_entry *)
6390 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6393 if (eh->elf.root.type != bfd_link_hash_defined
6394 && eh->elf.root.type != bfd_link_hash_defweak)
6397 fh = defined_code_entry (eh);
6400 sec = fh->elf.root.u.def.section;
6401 sec->flags |= SEC_KEEP;
6403 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6404 && opd_entry_value (eh->elf.root.u.def.section,
6405 eh->elf.root.u.def.value,
6406 &sec, NULL, FALSE) != (bfd_vma) -1)
6407 sec->flags |= SEC_KEEP;
6409 sec = eh->elf.root.u.def.section;
6410 sec->flags |= SEC_KEEP;
6414 /* Mark sections containing dynamically referenced symbols. When
6415 building shared libraries, we must assume that any visible symbol is
6419 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6421 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6422 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6423 struct ppc_link_hash_entry *fdh;
6424 struct bfd_elf_dynamic_list *d = info->dynamic_list;
6426 /* Dynamic linking info is on the func descriptor sym. */
6427 fdh = defined_func_desc (eh);
6431 if ((eh->elf.root.type == bfd_link_hash_defined
6432 || eh->elf.root.type == bfd_link_hash_defweak)
6433 && (eh->elf.ref_dynamic
6434 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6435 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6436 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6437 && (!bfd_link_executable (info)
6438 || info->gc_keep_exported
6439 || info->export_dynamic
6442 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6443 && (eh->elf.versioned >= versioned
6444 || !bfd_hide_sym_by_version (info->version_info,
6445 eh->elf.root.root.string)))))
6448 struct ppc_link_hash_entry *fh;
6450 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6452 /* Function descriptor syms cause the associated
6453 function code sym section to be marked. */
6454 fh = defined_code_entry (eh);
6457 code_sec = fh->elf.root.u.def.section;
6458 code_sec->flags |= SEC_KEEP;
6460 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6461 && opd_entry_value (eh->elf.root.u.def.section,
6462 eh->elf.root.u.def.value,
6463 &code_sec, NULL, FALSE) != (bfd_vma) -1)
6464 code_sec->flags |= SEC_KEEP;
6470 /* Return the section that should be marked against GC for a given
6474 ppc64_elf_gc_mark_hook (asection *sec,
6475 struct bfd_link_info *info,
6476 Elf_Internal_Rela *rel,
6477 struct elf_link_hash_entry *h,
6478 Elf_Internal_Sym *sym)
6482 /* Syms return NULL if we're marking .opd, so we avoid marking all
6483 function sections, as all functions are referenced in .opd. */
6485 if (get_opd_info (sec) != NULL)
6490 enum elf_ppc64_reloc_type r_type;
6491 struct ppc_link_hash_entry *eh, *fh, *fdh;
6493 r_type = ELF64_R_TYPE (rel->r_info);
6496 case R_PPC64_GNU_VTINHERIT:
6497 case R_PPC64_GNU_VTENTRY:
6501 switch (h->root.type)
6503 case bfd_link_hash_defined:
6504 case bfd_link_hash_defweak:
6505 eh = (struct ppc_link_hash_entry *) h;
6506 fdh = defined_func_desc (eh);
6509 /* -mcall-aixdesc code references the dot-symbol on
6510 a call reloc. Mark the function descriptor too
6511 against garbage collection. */
6513 if (fdh->elf.u.weakdef != NULL)
6514 fdh->elf.u.weakdef->mark = 1;
6518 /* Function descriptor syms cause the associated
6519 function code sym section to be marked. */
6520 fh = defined_code_entry (eh);
6523 /* They also mark their opd section. */
6524 eh->elf.root.u.def.section->gc_mark = 1;
6526 rsec = fh->elf.root.u.def.section;
6528 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6529 && opd_entry_value (eh->elf.root.u.def.section,
6530 eh->elf.root.u.def.value,
6531 &rsec, NULL, FALSE) != (bfd_vma) -1)
6532 eh->elf.root.u.def.section->gc_mark = 1;
6534 rsec = h->root.u.def.section;
6537 case bfd_link_hash_common:
6538 rsec = h->root.u.c.p->section;
6542 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6548 struct _opd_sec_data *opd;
6550 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6551 opd = get_opd_info (rsec);
6552 if (opd != NULL && opd->func_sec != NULL)
6556 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6563 /* Update the .got, .plt. and dynamic reloc reference counts for the
6564 section being removed. */
6567 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6568 asection *sec, const Elf_Internal_Rela *relocs)
6570 struct ppc_link_hash_table *htab;
6571 Elf_Internal_Shdr *symtab_hdr;
6572 struct elf_link_hash_entry **sym_hashes;
6573 struct got_entry **local_got_ents;
6574 const Elf_Internal_Rela *rel, *relend;
6576 if (bfd_link_relocatable (info))
6579 if ((sec->flags & SEC_ALLOC) == 0)
6582 elf_section_data (sec)->local_dynrel = NULL;
6584 htab = ppc_hash_table (info);
6588 symtab_hdr = &elf_symtab_hdr (abfd);
6589 sym_hashes = elf_sym_hashes (abfd);
6590 local_got_ents = elf_local_got_ents (abfd);
6592 relend = relocs + sec->reloc_count;
6593 for (rel = relocs; rel < relend; rel++)
6595 unsigned long r_symndx;
6596 enum elf_ppc64_reloc_type r_type;
6597 struct elf_link_hash_entry *h = NULL;
6598 struct plt_entry **plt_list;
6599 unsigned char tls_type = 0;
6601 r_symndx = ELF64_R_SYM (rel->r_info);
6602 r_type = ELF64_R_TYPE (rel->r_info);
6603 if (r_symndx >= symtab_hdr->sh_info)
6605 struct ppc_link_hash_entry *eh;
6606 struct elf_dyn_relocs **pp;
6607 struct elf_dyn_relocs *p;
6609 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6610 h = elf_follow_link (h);
6611 eh = (struct ppc_link_hash_entry *) h;
6613 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6616 /* Everything must go for SEC. */
6624 case R_PPC64_GOT_TLSLD16:
6625 case R_PPC64_GOT_TLSLD16_LO:
6626 case R_PPC64_GOT_TLSLD16_HI:
6627 case R_PPC64_GOT_TLSLD16_HA:
6628 tls_type = TLS_TLS | TLS_LD;
6631 case R_PPC64_GOT_TLSGD16:
6632 case R_PPC64_GOT_TLSGD16_LO:
6633 case R_PPC64_GOT_TLSGD16_HI:
6634 case R_PPC64_GOT_TLSGD16_HA:
6635 tls_type = TLS_TLS | TLS_GD;
6638 case R_PPC64_GOT_TPREL16_DS:
6639 case R_PPC64_GOT_TPREL16_LO_DS:
6640 case R_PPC64_GOT_TPREL16_HI:
6641 case R_PPC64_GOT_TPREL16_HA:
6642 tls_type = TLS_TLS | TLS_TPREL;
6645 case R_PPC64_GOT_DTPREL16_DS:
6646 case R_PPC64_GOT_DTPREL16_LO_DS:
6647 case R_PPC64_GOT_DTPREL16_HI:
6648 case R_PPC64_GOT_DTPREL16_HA:
6649 tls_type = TLS_TLS | TLS_DTPREL;
6653 case R_PPC64_GOT16_DS:
6654 case R_PPC64_GOT16_HA:
6655 case R_PPC64_GOT16_HI:
6656 case R_PPC64_GOT16_LO:
6657 case R_PPC64_GOT16_LO_DS:
6660 struct got_entry *ent;
6665 ent = local_got_ents[r_symndx];
6667 for (; ent != NULL; ent = ent->next)
6668 if (ent->addend == rel->r_addend
6669 && ent->owner == abfd
6670 && ent->tls_type == tls_type)
6674 if (ent->got.refcount > 0)
6675 ent->got.refcount -= 1;
6679 case R_PPC64_PLT16_HA:
6680 case R_PPC64_PLT16_HI:
6681 case R_PPC64_PLT16_LO:
6685 case R_PPC64_REL14_BRNTAKEN:
6686 case R_PPC64_REL14_BRTAKEN:
6690 plt_list = &h->plt.plist;
6691 else if (local_got_ents != NULL)
6693 struct plt_entry **local_plt = (struct plt_entry **)
6694 (local_got_ents + symtab_hdr->sh_info);
6695 unsigned char *local_got_tls_masks = (unsigned char *)
6696 (local_plt + symtab_hdr->sh_info);
6697 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6698 plt_list = local_plt + r_symndx;
6702 struct plt_entry *ent;
6704 for (ent = *plt_list; ent != NULL; ent = ent->next)
6705 if (ent->addend == rel->r_addend)
6707 if (ent != NULL && ent->plt.refcount > 0)
6708 ent->plt.refcount -= 1;
6719 /* The maximum size of .sfpr. */
6720 #define SFPR_MAX (218*4)
6722 struct sfpr_def_parms
6724 const char name[12];
6725 unsigned char lo, hi;
6726 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6727 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6730 /* Auto-generate _save*, _rest* functions in .sfpr.
6731 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6735 sfpr_define (struct bfd_link_info *info,
6736 const struct sfpr_def_parms *parm,
6739 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6741 size_t len = strlen (parm->name);
6742 bfd_boolean writing = FALSE;
6748 memcpy (sym, parm->name, len);
6751 for (i = parm->lo; i <= parm->hi; i++)
6753 struct ppc_link_hash_entry *h;
6755 sym[len + 0] = i / 10 + '0';
6756 sym[len + 1] = i % 10 + '0';
6757 h = (struct ppc_link_hash_entry *)
6758 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6759 if (stub_sec != NULL)
6762 && h->elf.root.type == bfd_link_hash_defined
6763 && h->elf.root.u.def.section == htab->sfpr)
6765 struct elf_link_hash_entry *s;
6767 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6768 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6771 if (s->root.type == bfd_link_hash_new
6772 || (s->root.type = bfd_link_hash_defined
6773 && s->root.u.def.section == stub_sec))
6775 s->root.type = bfd_link_hash_defined;
6776 s->root.u.def.section = stub_sec;
6777 s->root.u.def.value = (stub_sec->size
6778 + h->elf.root.u.def.value);
6781 s->ref_regular_nonweak = 1;
6782 s->forced_local = 1;
6784 s->root.linker_def = 1;
6792 if (!h->elf.def_regular)
6794 h->elf.root.type = bfd_link_hash_defined;
6795 h->elf.root.u.def.section = htab->sfpr;
6796 h->elf.root.u.def.value = htab->sfpr->size;
6797 h->elf.type = STT_FUNC;
6798 h->elf.def_regular = 1;
6800 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6802 if (htab->sfpr->contents == NULL)
6804 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6805 if (htab->sfpr->contents == NULL)
6812 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6814 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6816 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6817 htab->sfpr->size = p - htab->sfpr->contents;
6825 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6827 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6832 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6834 p = savegpr0 (abfd, p, r);
6835 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6837 bfd_put_32 (abfd, BLR, p);
6842 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6844 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6849 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6851 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6853 p = restgpr0 (abfd, p, r);
6854 bfd_put_32 (abfd, MTLR_R0, p);
6858 p = restgpr0 (abfd, p, 30);
6859 p = restgpr0 (abfd, p, 31);
6861 bfd_put_32 (abfd, BLR, p);
6866 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6868 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6873 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6875 p = savegpr1 (abfd, p, r);
6876 bfd_put_32 (abfd, BLR, p);
6881 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6883 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6888 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6890 p = restgpr1 (abfd, p, r);
6891 bfd_put_32 (abfd, BLR, p);
6896 savefpr (bfd *abfd, bfd_byte *p, int r)
6898 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6903 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6905 p = savefpr (abfd, p, r);
6906 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6908 bfd_put_32 (abfd, BLR, p);
6913 restfpr (bfd *abfd, bfd_byte *p, int r)
6915 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6920 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6922 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6924 p = restfpr (abfd, p, r);
6925 bfd_put_32 (abfd, MTLR_R0, p);
6929 p = restfpr (abfd, p, 30);
6930 p = restfpr (abfd, p, 31);
6932 bfd_put_32 (abfd, BLR, p);
6937 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6939 p = savefpr (abfd, p, r);
6940 bfd_put_32 (abfd, BLR, p);
6945 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6947 p = restfpr (abfd, p, r);
6948 bfd_put_32 (abfd, BLR, p);
6953 savevr (bfd *abfd, bfd_byte *p, int r)
6955 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6957 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6962 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6964 p = savevr (abfd, p, r);
6965 bfd_put_32 (abfd, BLR, p);
6970 restvr (bfd *abfd, bfd_byte *p, int r)
6972 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6974 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6979 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6981 p = restvr (abfd, p, r);
6982 bfd_put_32 (abfd, BLR, p);
6986 /* Called via elf_link_hash_traverse to transfer dynamic linking
6987 information on function code symbol entries to their corresponding
6988 function descriptor symbol entries. */
6991 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6993 struct bfd_link_info *info;
6994 struct ppc_link_hash_table *htab;
6995 struct ppc_link_hash_entry *fh;
6996 struct ppc_link_hash_entry *fdh;
6997 bfd_boolean force_local;
6999 fh = (struct ppc_link_hash_entry *) h;
7000 if (fh->elf.root.type == bfd_link_hash_indirect)
7006 if (fh->elf.root.root.string[0] != '.'
7007 || fh->elf.root.root.string[1] == '\0')
7011 htab = ppc_hash_table (info);
7015 /* Find the corresponding function descriptor symbol. */
7016 fdh = lookup_fdh (fh, htab);
7018 /* Resolve undefined references to dot-symbols as the value
7019 in the function descriptor, if we have one in a regular object.
7020 This is to satisfy cases like ".quad .foo". Calls to functions
7021 in dynamic objects are handled elsewhere. */
7022 if ((fh->elf.root.type == bfd_link_hash_undefined
7023 || fh->elf.root.type == bfd_link_hash_undefweak)
7024 && (fdh->elf.root.type == bfd_link_hash_defined
7025 || fdh->elf.root.type == bfd_link_hash_defweak)
7026 && get_opd_info (fdh->elf.root.u.def.section) != NULL
7027 && opd_entry_value (fdh->elf.root.u.def.section,
7028 fdh->elf.root.u.def.value,
7029 &fh->elf.root.u.def.section,
7030 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
7032 fh->elf.root.type = fdh->elf.root.type;
7033 fh->elf.forced_local = 1;
7034 fh->elf.def_regular = fdh->elf.def_regular;
7035 fh->elf.def_dynamic = fdh->elf.def_dynamic;
7038 if (!fh->elf.dynamic)
7040 struct plt_entry *ent;
7042 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7043 if (ent->plt.refcount > 0)
7049 /* Create a descriptor as undefined if necessary. */
7051 && !bfd_link_executable (info)
7052 && (fh->elf.root.type == bfd_link_hash_undefined
7053 || fh->elf.root.type == bfd_link_hash_undefweak))
7055 fdh = make_fdh (info, fh);
7060 /* We can't support overriding of symbols on a fake descriptor. */
7063 && (fh->elf.root.type == bfd_link_hash_defined
7064 || fh->elf.root.type == bfd_link_hash_defweak))
7065 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
7067 /* Transfer dynamic linking information to the function descriptor. */
7070 fdh->elf.ref_regular |= fh->elf.ref_regular;
7071 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7072 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7073 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
7074 fdh->elf.dynamic |= fh->elf.dynamic;
7075 fdh->elf.needs_plt |= (fh->elf.needs_plt
7076 || fh->elf.type == STT_FUNC
7077 || fh->elf.type == STT_GNU_IFUNC);
7078 move_plt_plist (fh, fdh);
7080 if (!fdh->elf.forced_local
7081 && fh->elf.dynindx != -1)
7082 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7086 /* Now that the info is on the function descriptor, clear the
7087 function code sym info. Any function code syms for which we
7088 don't have a definition in a regular file, we force local.
7089 This prevents a shared library from exporting syms that have
7090 been imported from another library. Function code syms that
7091 are really in the library we must leave global to prevent the
7092 linker dragging in a definition from a static library. */
7093 force_local = (!fh->elf.def_regular
7095 || !fdh->elf.def_regular
7096 || fdh->elf.forced_local);
7097 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7102 static const struct sfpr_def_parms save_res_funcs[] =
7104 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7105 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7106 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7107 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7108 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7109 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7110 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7111 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7112 { "._savef", 14, 31, savefpr, savefpr1_tail },
7113 { "._restf", 14, 31, restfpr, restfpr1_tail },
7114 { "_savevr_", 20, 31, savevr, savevr_tail },
7115 { "_restvr_", 20, 31, restvr, restvr_tail }
7118 /* Called near the start of bfd_elf_size_dynamic_sections. We use
7119 this hook to a) provide some gcc support functions, and b) transfer
7120 dynamic linking information gathered so far on function code symbol
7121 entries, to their corresponding function descriptor symbol entries. */
7124 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7125 struct bfd_link_info *info)
7127 struct ppc_link_hash_table *htab;
7129 htab = ppc_hash_table (info);
7133 /* Provide any missing _save* and _rest* functions. */
7134 if (htab->sfpr != NULL)
7138 htab->sfpr->size = 0;
7139 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7140 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7142 if (htab->sfpr->size == 0)
7143 htab->sfpr->flags |= SEC_EXCLUDE;
7146 if (bfd_link_relocatable (info))
7149 if (htab->elf.hgot != NULL)
7151 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7152 /* Make .TOC. defined so as to prevent it being made dynamic.
7153 The wrong value here is fixed later in ppc64_elf_set_toc. */
7154 if (!htab->elf.hgot->def_regular
7155 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7157 htab->elf.hgot->root.type = bfd_link_hash_defined;
7158 htab->elf.hgot->root.u.def.value = 0;
7159 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7160 htab->elf.hgot->def_regular = 1;
7161 htab->elf.hgot->root.linker_def = 1;
7163 htab->elf.hgot->type = STT_OBJECT;
7164 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7168 if (htab->need_func_desc_adj)
7170 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7171 htab->need_func_desc_adj = 0;
7177 /* Return true if we have dynamic relocs against H that apply to
7178 read-only sections. */
7181 readonly_dynrelocs (struct elf_link_hash_entry *h)
7183 struct ppc_link_hash_entry *eh;
7184 struct elf_dyn_relocs *p;
7186 eh = (struct ppc_link_hash_entry *) h;
7187 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7189 asection *s = p->sec->output_section;
7191 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7197 /* Return true if we have dynamic relocs against H or any of its weak
7198 aliases, that apply to read-only sections. */
7201 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7203 struct ppc_link_hash_entry *eh;
7205 eh = (struct ppc_link_hash_entry *) h;
7208 if (readonly_dynrelocs (&eh->elf))
7211 } while (eh != NULL && &eh->elf != h);
7216 /* Return whether EH has pc-relative dynamic relocs. */
7219 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7221 struct elf_dyn_relocs *p;
7223 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7224 if (p->pc_count != 0)
7229 /* Return true if a global entry stub will be created for H. Valid
7230 for ELFv2 before plt entries have been allocated. */
7233 global_entry_stub (struct elf_link_hash_entry *h)
7235 struct plt_entry *pent;
7237 if (!h->pointer_equality_needed
7241 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7242 if (pent->plt.refcount > 0
7243 && pent->addend == 0)
7249 /* Adjust a symbol defined by a dynamic object and referenced by a
7250 regular object. The current definition is in some section of the
7251 dynamic object, but we're not including those sections. We have to
7252 change the definition to something the rest of the link can
7256 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7257 struct elf_link_hash_entry *h)
7259 struct ppc_link_hash_table *htab;
7262 htab = ppc_hash_table (info);
7266 /* Deal with function syms. */
7267 if (h->type == STT_FUNC
7268 || h->type == STT_GNU_IFUNC
7271 /* Clear procedure linkage table information for any symbol that
7272 won't need a .plt entry. */
7273 struct plt_entry *ent;
7274 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7275 if (ent->plt.refcount > 0)
7278 || (h->type != STT_GNU_IFUNC
7279 && (SYMBOL_CALLS_LOCAL (info, h)
7280 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7281 && h->root.type == bfd_link_hash_undefweak)))
7282 || ((struct ppc_link_hash_entry *) h)->save_res)
7284 h->plt.plist = NULL;
7286 h->pointer_equality_needed = 0;
7288 else if (abiversion (info->output_bfd) >= 2)
7290 /* Taking a function's address in a read/write section
7291 doesn't require us to define the function symbol in the
7292 executable on a global entry stub. A dynamic reloc can
7293 be used instead. The reason we prefer a few more dynamic
7294 relocs is that calling via a global entry stub costs a
7295 few more instructions, and pointer_equality_needed causes
7296 extra work in ld.so when resolving these symbols. */
7297 if (global_entry_stub (h)
7298 && !alias_readonly_dynrelocs (h))
7300 h->pointer_equality_needed = 0;
7301 /* After adjust_dynamic_symbol, non_got_ref set in
7302 the non-pic case means that dyn_relocs for this
7303 symbol should be discarded. */
7307 /* If making a plt entry, then we don't need copy relocs. */
7312 h->plt.plist = NULL;
7314 /* If this is a weak symbol, and there is a real definition, the
7315 processor independent code will have arranged for us to see the
7316 real definition first, and we can just use the same value. */
7317 if (h->u.weakdef != NULL)
7319 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7320 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7321 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7322 h->root.u.def.value = h->u.weakdef->root.u.def.value;
7323 if (ELIMINATE_COPY_RELOCS)
7324 h->non_got_ref = h->u.weakdef->non_got_ref;
7328 /* If we are creating a shared library, we must presume that the
7329 only references to the symbol are via the global offset table.
7330 For such cases we need not do anything here; the relocations will
7331 be handled correctly by relocate_section. */
7332 if (bfd_link_pic (info))
7335 /* If there are no references to this symbol that do not use the
7336 GOT, we don't need to generate a copy reloc. */
7337 if (!h->non_got_ref)
7340 /* Don't generate a copy reloc for symbols defined in the executable. */
7341 if (!h->def_dynamic || !h->ref_regular || h->def_regular
7343 /* If -z nocopyreloc was given, don't generate them either. */
7344 || info->nocopyreloc
7346 /* If we didn't find any dynamic relocs in read-only sections, then
7347 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
7348 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7350 /* Protected variables do not work with .dynbss. The copy in
7351 .dynbss won't be used by the shared library with the protected
7352 definition for the variable. Text relocations are preferable
7353 to an incorrect program. */
7354 || h->protected_def)
7360 if (h->plt.plist != NULL)
7362 /* We should never get here, but unfortunately there are versions
7363 of gcc out there that improperly (for this ABI) put initialized
7364 function pointers, vtable refs and suchlike in read-only
7365 sections. Allow them to proceed, but warn that this might
7366 break at runtime. */
7367 info->callbacks->einfo
7368 (_("%P: copy reloc against `%T' requires lazy plt linking; "
7369 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7370 h->root.root.string);
7373 /* This is a reference to a symbol defined by a dynamic object which
7374 is not a function. */
7376 /* We must allocate the symbol in our .dynbss section, which will
7377 become part of the .bss section of the executable. There will be
7378 an entry for this symbol in the .dynsym section. The dynamic
7379 object will contain position independent code, so all references
7380 from the dynamic object to this symbol will go through the global
7381 offset table. The dynamic linker will use the .dynsym entry to
7382 determine the address it must put in the global offset table, so
7383 both the dynamic object and the regular object will refer to the
7384 same memory location for the variable. */
7386 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7387 to copy the initial value out of the dynamic object and into the
7388 runtime process image. We need to remember the offset into the
7389 .rela.bss section we are going to use. */
7390 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7392 s = htab->elf.sdynrelro;
7393 srel = htab->elf.sreldynrelro;
7397 s = htab->elf.sdynbss;
7398 srel = htab->elf.srelbss;
7400 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7402 srel->size += sizeof (Elf64_External_Rela);
7406 return _bfd_elf_adjust_dynamic_copy (info, h, s);
7409 /* If given a function descriptor symbol, hide both the function code
7410 sym and the descriptor. */
7412 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7413 struct elf_link_hash_entry *h,
7414 bfd_boolean force_local)
7416 struct ppc_link_hash_entry *eh;
7417 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7419 eh = (struct ppc_link_hash_entry *) h;
7420 if (eh->is_func_descriptor)
7422 struct ppc_link_hash_entry *fh = eh->oh;
7427 struct elf_link_hash_table *htab = elf_hash_table (info);
7430 /* We aren't supposed to use alloca in BFD because on
7431 systems which do not have alloca the version in libiberty
7432 calls xmalloc, which might cause the program to crash
7433 when it runs out of memory. This function doesn't have a
7434 return status, so there's no way to gracefully return an
7435 error. So cheat. We know that string[-1] can be safely
7436 accessed; It's either a string in an ELF string table,
7437 or allocated in an objalloc structure. */
7439 p = eh->elf.root.root.string - 1;
7442 fh = (struct ppc_link_hash_entry *)
7443 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7446 /* Unfortunately, if it so happens that the string we were
7447 looking for was allocated immediately before this string,
7448 then we overwrote the string terminator. That's the only
7449 reason the lookup should fail. */
7452 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7453 while (q >= eh->elf.root.root.string && *q == *p)
7455 if (q < eh->elf.root.root.string && *p == '.')
7456 fh = (struct ppc_link_hash_entry *)
7457 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7466 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7471 get_sym_h (struct elf_link_hash_entry **hp,
7472 Elf_Internal_Sym **symp,
7474 unsigned char **tls_maskp,
7475 Elf_Internal_Sym **locsymsp,
7476 unsigned long r_symndx,
7479 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7481 if (r_symndx >= symtab_hdr->sh_info)
7483 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7484 struct elf_link_hash_entry *h;
7486 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7487 h = elf_follow_link (h);
7495 if (symsecp != NULL)
7497 asection *symsec = NULL;
7498 if (h->root.type == bfd_link_hash_defined
7499 || h->root.type == bfd_link_hash_defweak)
7500 symsec = h->root.u.def.section;
7504 if (tls_maskp != NULL)
7506 struct ppc_link_hash_entry *eh;
7508 eh = (struct ppc_link_hash_entry *) h;
7509 *tls_maskp = &eh->tls_mask;
7514 Elf_Internal_Sym *sym;
7515 Elf_Internal_Sym *locsyms = *locsymsp;
7517 if (locsyms == NULL)
7519 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7520 if (locsyms == NULL)
7521 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7522 symtab_hdr->sh_info,
7523 0, NULL, NULL, NULL);
7524 if (locsyms == NULL)
7526 *locsymsp = locsyms;
7528 sym = locsyms + r_symndx;
7536 if (symsecp != NULL)
7537 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7539 if (tls_maskp != NULL)
7541 struct got_entry **lgot_ents;
7542 unsigned char *tls_mask;
7545 lgot_ents = elf_local_got_ents (ibfd);
7546 if (lgot_ents != NULL)
7548 struct plt_entry **local_plt = (struct plt_entry **)
7549 (lgot_ents + symtab_hdr->sh_info);
7550 unsigned char *lgot_masks = (unsigned char *)
7551 (local_plt + symtab_hdr->sh_info);
7552 tls_mask = &lgot_masks[r_symndx];
7554 *tls_maskp = tls_mask;
7560 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7561 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7562 type suitable for optimization, and 1 otherwise. */
7565 get_tls_mask (unsigned char **tls_maskp,
7566 unsigned long *toc_symndx,
7567 bfd_vma *toc_addend,
7568 Elf_Internal_Sym **locsymsp,
7569 const Elf_Internal_Rela *rel,
7572 unsigned long r_symndx;
7574 struct elf_link_hash_entry *h;
7575 Elf_Internal_Sym *sym;
7579 r_symndx = ELF64_R_SYM (rel->r_info);
7580 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7583 if ((*tls_maskp != NULL && **tls_maskp != 0)
7585 || ppc64_elf_section_data (sec) == NULL
7586 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7589 /* Look inside a TOC section too. */
7592 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7593 off = h->root.u.def.value;
7596 off = sym->st_value;
7597 off += rel->r_addend;
7598 BFD_ASSERT (off % 8 == 0);
7599 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7600 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7601 if (toc_symndx != NULL)
7602 *toc_symndx = r_symndx;
7603 if (toc_addend != NULL)
7604 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7605 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7607 if ((h == NULL || is_static_defined (h))
7608 && (next_r == -1 || next_r == -2))
7613 /* Find (or create) an entry in the tocsave hash table. */
7615 static struct tocsave_entry *
7616 tocsave_find (struct ppc_link_hash_table *htab,
7617 enum insert_option insert,
7618 Elf_Internal_Sym **local_syms,
7619 const Elf_Internal_Rela *irela,
7622 unsigned long r_indx;
7623 struct elf_link_hash_entry *h;
7624 Elf_Internal_Sym *sym;
7625 struct tocsave_entry ent, *p;
7627 struct tocsave_entry **slot;
7629 r_indx = ELF64_R_SYM (irela->r_info);
7630 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7632 if (ent.sec == NULL || ent.sec->output_section == NULL)
7635 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7640 ent.offset = h->root.u.def.value;
7642 ent.offset = sym->st_value;
7643 ent.offset += irela->r_addend;
7645 hash = tocsave_htab_hash (&ent);
7646 slot = ((struct tocsave_entry **)
7647 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7653 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7662 /* Adjust all global syms defined in opd sections. In gcc generated
7663 code for the old ABI, these will already have been done. */
7666 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7668 struct ppc_link_hash_entry *eh;
7670 struct _opd_sec_data *opd;
7672 if (h->root.type == bfd_link_hash_indirect)
7675 if (h->root.type != bfd_link_hash_defined
7676 && h->root.type != bfd_link_hash_defweak)
7679 eh = (struct ppc_link_hash_entry *) h;
7680 if (eh->adjust_done)
7683 sym_sec = eh->elf.root.u.def.section;
7684 opd = get_opd_info (sym_sec);
7685 if (opd != NULL && opd->adjust != NULL)
7687 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7690 /* This entry has been deleted. */
7691 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7694 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7695 if (discarded_section (dsec))
7697 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7701 eh->elf.root.u.def.value = 0;
7702 eh->elf.root.u.def.section = dsec;
7705 eh->elf.root.u.def.value += adjust;
7706 eh->adjust_done = 1;
7711 /* Handles decrementing dynamic reloc counts for the reloc specified by
7712 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7713 have already been determined. */
7716 dec_dynrel_count (bfd_vma r_info,
7718 struct bfd_link_info *info,
7719 Elf_Internal_Sym **local_syms,
7720 struct elf_link_hash_entry *h,
7721 Elf_Internal_Sym *sym)
7723 enum elf_ppc64_reloc_type r_type;
7724 asection *sym_sec = NULL;
7726 /* Can this reloc be dynamic? This switch, and later tests here
7727 should be kept in sync with the code in check_relocs. */
7728 r_type = ELF64_R_TYPE (r_info);
7734 case R_PPC64_TPREL16:
7735 case R_PPC64_TPREL16_LO:
7736 case R_PPC64_TPREL16_HI:
7737 case R_PPC64_TPREL16_HA:
7738 case R_PPC64_TPREL16_DS:
7739 case R_PPC64_TPREL16_LO_DS:
7740 case R_PPC64_TPREL16_HIGH:
7741 case R_PPC64_TPREL16_HIGHA:
7742 case R_PPC64_TPREL16_HIGHER:
7743 case R_PPC64_TPREL16_HIGHERA:
7744 case R_PPC64_TPREL16_HIGHEST:
7745 case R_PPC64_TPREL16_HIGHESTA:
7746 if (!bfd_link_pic (info))
7749 case R_PPC64_TPREL64:
7750 case R_PPC64_DTPMOD64:
7751 case R_PPC64_DTPREL64:
7752 case R_PPC64_ADDR64:
7756 case R_PPC64_ADDR14:
7757 case R_PPC64_ADDR14_BRNTAKEN:
7758 case R_PPC64_ADDR14_BRTAKEN:
7759 case R_PPC64_ADDR16:
7760 case R_PPC64_ADDR16_DS:
7761 case R_PPC64_ADDR16_HA:
7762 case R_PPC64_ADDR16_HI:
7763 case R_PPC64_ADDR16_HIGH:
7764 case R_PPC64_ADDR16_HIGHA:
7765 case R_PPC64_ADDR16_HIGHER:
7766 case R_PPC64_ADDR16_HIGHERA:
7767 case R_PPC64_ADDR16_HIGHEST:
7768 case R_PPC64_ADDR16_HIGHESTA:
7769 case R_PPC64_ADDR16_LO:
7770 case R_PPC64_ADDR16_LO_DS:
7771 case R_PPC64_ADDR24:
7772 case R_PPC64_ADDR32:
7773 case R_PPC64_UADDR16:
7774 case R_PPC64_UADDR32:
7775 case R_PPC64_UADDR64:
7780 if (local_syms != NULL)
7782 unsigned long r_symndx;
7783 bfd *ibfd = sec->owner;
7785 r_symndx = ELF64_R_SYM (r_info);
7786 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7790 if ((bfd_link_pic (info)
7791 && (must_be_dyn_reloc (info, r_type)
7793 && (!SYMBOLIC_BIND (info, h)
7794 || h->root.type == bfd_link_hash_defweak
7795 || !h->def_regular))))
7796 || (ELIMINATE_COPY_RELOCS
7797 && !bfd_link_pic (info)
7799 && (h->root.type == bfd_link_hash_defweak
7800 || !h->def_regular)))
7807 struct elf_dyn_relocs *p;
7808 struct elf_dyn_relocs **pp;
7809 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7811 /* elf_gc_sweep may have already removed all dyn relocs associated
7812 with local syms for a given section. Also, symbol flags are
7813 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7814 report a dynreloc miscount. */
7815 if (*pp == NULL && info->gc_sections)
7818 while ((p = *pp) != NULL)
7822 if (!must_be_dyn_reloc (info, r_type))
7834 struct ppc_dyn_relocs *p;
7835 struct ppc_dyn_relocs **pp;
7837 bfd_boolean is_ifunc;
7839 if (local_syms == NULL)
7840 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7841 if (sym_sec == NULL)
7844 vpp = &elf_section_data (sym_sec)->local_dynrel;
7845 pp = (struct ppc_dyn_relocs **) vpp;
7847 if (*pp == NULL && info->gc_sections)
7850 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7851 while ((p = *pp) != NULL)
7853 if (p->sec == sec && p->ifunc == is_ifunc)
7864 /* xgettext:c-format */
7865 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7867 bfd_set_error (bfd_error_bad_value);
7871 /* Remove unused Official Procedure Descriptor entries. Currently we
7872 only remove those associated with functions in discarded link-once
7873 sections, or weakly defined functions that have been overridden. It
7874 would be possible to remove many more entries for statically linked
7878 ppc64_elf_edit_opd (struct bfd_link_info *info)
7881 bfd_boolean some_edited = FALSE;
7882 asection *need_pad = NULL;
7883 struct ppc_link_hash_table *htab;
7885 htab = ppc_hash_table (info);
7889 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7892 Elf_Internal_Rela *relstart, *rel, *relend;
7893 Elf_Internal_Shdr *symtab_hdr;
7894 Elf_Internal_Sym *local_syms;
7895 struct _opd_sec_data *opd;
7896 bfd_boolean need_edit, add_aux_fields, broken;
7897 bfd_size_type cnt_16b = 0;
7899 if (!is_ppc64_elf (ibfd))
7902 sec = bfd_get_section_by_name (ibfd, ".opd");
7903 if (sec == NULL || sec->size == 0)
7906 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7909 if (sec->output_section == bfd_abs_section_ptr)
7912 /* Look through the section relocs. */
7913 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7917 symtab_hdr = &elf_symtab_hdr (ibfd);
7919 /* Read the relocations. */
7920 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7922 if (relstart == NULL)
7925 /* First run through the relocs to check they are sane, and to
7926 determine whether we need to edit this opd section. */
7930 relend = relstart + sec->reloc_count;
7931 for (rel = relstart; rel < relend; )
7933 enum elf_ppc64_reloc_type r_type;
7934 unsigned long r_symndx;
7936 struct elf_link_hash_entry *h;
7937 Elf_Internal_Sym *sym;
7940 /* .opd contains an array of 16 or 24 byte entries. We're
7941 only interested in the reloc pointing to a function entry
7943 offset = rel->r_offset;
7944 if (rel + 1 == relend
7945 || rel[1].r_offset != offset + 8)
7947 /* If someone messes with .opd alignment then after a
7948 "ld -r" we might have padding in the middle of .opd.
7949 Also, there's nothing to prevent someone putting
7950 something silly in .opd with the assembler. No .opd
7951 optimization for them! */
7954 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7959 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7960 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7963 /* xgettext:c-format */
7964 (_("%B: unexpected reloc type %u in .opd section"),
7970 r_symndx = ELF64_R_SYM (rel->r_info);
7971 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7975 if (sym_sec == NULL || sym_sec->owner == NULL)
7977 const char *sym_name;
7979 sym_name = h->root.root.string;
7981 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7985 /* xgettext:c-format */
7986 (_("%B: undefined sym `%s' in .opd section"),
7992 /* opd entries are always for functions defined in the
7993 current input bfd. If the symbol isn't defined in the
7994 input bfd, then we won't be using the function in this
7995 bfd; It must be defined in a linkonce section in another
7996 bfd, or is weak. It's also possible that we are
7997 discarding the function due to a linker script /DISCARD/,
7998 which we test for via the output_section. */
7999 if (sym_sec->owner != ibfd
8000 || sym_sec->output_section == bfd_abs_section_ptr)
8004 if (rel + 1 == relend
8005 || (rel + 2 < relend
8006 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
8011 if (sec->size == offset + 24)
8016 if (sec->size == offset + 16)
8023 else if (rel + 1 < relend
8024 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8025 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8027 if (rel[0].r_offset == offset + 16)
8029 else if (rel[0].r_offset != offset + 24)
8036 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
8038 if (!broken && (need_edit || add_aux_fields))
8040 Elf_Internal_Rela *write_rel;
8041 Elf_Internal_Shdr *rel_hdr;
8042 bfd_byte *rptr, *wptr;
8043 bfd_byte *new_contents;
8046 new_contents = NULL;
8047 amt = OPD_NDX (sec->size) * sizeof (long);
8048 opd = &ppc64_elf_section_data (sec)->u.opd;
8049 opd->adjust = bfd_zalloc (sec->owner, amt);
8050 if (opd->adjust == NULL)
8052 ppc64_elf_section_data (sec)->sec_type = sec_opd;
8054 /* This seems a waste of time as input .opd sections are all
8055 zeros as generated by gcc, but I suppose there's no reason
8056 this will always be so. We might start putting something in
8057 the third word of .opd entries. */
8058 if ((sec->flags & SEC_IN_MEMORY) == 0)
8061 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
8066 if (local_syms != NULL
8067 && symtab_hdr->contents != (unsigned char *) local_syms)
8069 if (elf_section_data (sec)->relocs != relstart)
8073 sec->contents = loc;
8074 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8077 elf_section_data (sec)->relocs = relstart;
8079 new_contents = sec->contents;
8082 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8083 if (new_contents == NULL)
8087 wptr = new_contents;
8088 rptr = sec->contents;
8089 write_rel = relstart;
8090 for (rel = relstart; rel < relend; )
8092 unsigned long r_symndx;
8094 struct elf_link_hash_entry *h;
8095 struct ppc_link_hash_entry *fdh = NULL;
8096 Elf_Internal_Sym *sym;
8098 Elf_Internal_Rela *next_rel;
8101 r_symndx = ELF64_R_SYM (rel->r_info);
8102 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8107 if (next_rel + 1 == relend
8108 || (next_rel + 2 < relend
8109 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8112 /* See if the .opd entry is full 24 byte or
8113 16 byte (with fd_aux entry overlapped with next
8116 if (next_rel == relend)
8118 if (sec->size == rel->r_offset + 16)
8121 else if (next_rel->r_offset == rel->r_offset + 16)
8125 && h->root.root.string[0] == '.')
8127 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8130 fdh = ppc_follow_link (fdh);
8131 if (fdh->elf.root.type != bfd_link_hash_defined
8132 && fdh->elf.root.type != bfd_link_hash_defweak)
8137 skip = (sym_sec->owner != ibfd
8138 || sym_sec->output_section == bfd_abs_section_ptr);
8141 if (fdh != NULL && sym_sec->owner == ibfd)
8143 /* Arrange for the function descriptor sym
8145 fdh->elf.root.u.def.value = 0;
8146 fdh->elf.root.u.def.section = sym_sec;
8148 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8150 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8155 if (!dec_dynrel_count (rel->r_info, sec, info,
8159 if (++rel == next_rel)
8162 r_symndx = ELF64_R_SYM (rel->r_info);
8163 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8170 /* We'll be keeping this opd entry. */
8175 /* Redefine the function descriptor symbol to
8176 this location in the opd section. It is
8177 necessary to update the value here rather
8178 than using an array of adjustments as we do
8179 for local symbols, because various places
8180 in the generic ELF code use the value
8181 stored in u.def.value. */
8182 fdh->elf.root.u.def.value = wptr - new_contents;
8183 fdh->adjust_done = 1;
8186 /* Local syms are a bit tricky. We could
8187 tweak them as they can be cached, but
8188 we'd need to look through the local syms
8189 for the function descriptor sym which we
8190 don't have at the moment. So keep an
8191 array of adjustments. */
8192 adjust = (wptr - new_contents) - (rptr - sec->contents);
8193 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8196 memcpy (wptr, rptr, opd_ent_size);
8197 wptr += opd_ent_size;
8198 if (add_aux_fields && opd_ent_size == 16)
8200 memset (wptr, '\0', 8);
8204 /* We need to adjust any reloc offsets to point to the
8206 for ( ; rel != next_rel; ++rel)
8208 rel->r_offset += adjust;
8209 if (write_rel != rel)
8210 memcpy (write_rel, rel, sizeof (*rel));
8215 rptr += opd_ent_size;
8218 sec->size = wptr - new_contents;
8219 sec->reloc_count = write_rel - relstart;
8222 free (sec->contents);
8223 sec->contents = new_contents;
8226 /* Fudge the header size too, as this is used later in
8227 elf_bfd_final_link if we are emitting relocs. */
8228 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8229 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8232 else if (elf_section_data (sec)->relocs != relstart)
8235 if (local_syms != NULL
8236 && symtab_hdr->contents != (unsigned char *) local_syms)
8238 if (!info->keep_memory)
8241 symtab_hdr->contents = (unsigned char *) local_syms;
8246 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8248 /* If we are doing a final link and the last .opd entry is just 16 byte
8249 long, add a 8 byte padding after it. */
8250 if (need_pad != NULL && !bfd_link_relocatable (info))
8254 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8256 BFD_ASSERT (need_pad->size > 0);
8258 p = bfd_malloc (need_pad->size + 8);
8262 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8263 p, 0, need_pad->size))
8266 need_pad->contents = p;
8267 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8271 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8275 need_pad->contents = p;
8278 memset (need_pad->contents + need_pad->size, 0, 8);
8279 need_pad->size += 8;
8285 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
8288 ppc64_elf_tls_setup (struct bfd_link_info *info)
8290 struct ppc_link_hash_table *htab;
8292 htab = ppc_hash_table (info);
8296 if (abiversion (info->output_bfd) == 1)
8299 if (htab->params->no_multi_toc)
8300 htab->do_multi_toc = 0;
8301 else if (!htab->do_multi_toc)
8302 htab->params->no_multi_toc = 1;
8304 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8305 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8306 FALSE, FALSE, TRUE));
8307 /* Move dynamic linking info to the function descriptor sym. */
8308 if (htab->tls_get_addr != NULL)
8309 func_desc_adjust (&htab->tls_get_addr->elf, info);
8310 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8311 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8312 FALSE, FALSE, TRUE));
8313 if (htab->params->tls_get_addr_opt)
8315 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8317 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8318 FALSE, FALSE, TRUE);
8320 func_desc_adjust (opt, info);
8321 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8322 FALSE, FALSE, TRUE);
8324 && (opt_fd->root.type == bfd_link_hash_defined
8325 || opt_fd->root.type == bfd_link_hash_defweak))
8327 /* If glibc supports an optimized __tls_get_addr call stub,
8328 signalled by the presence of __tls_get_addr_opt, and we'll
8329 be calling __tls_get_addr via a plt call stub, then
8330 make __tls_get_addr point to __tls_get_addr_opt. */
8331 tga_fd = &htab->tls_get_addr_fd->elf;
8332 if (htab->elf.dynamic_sections_created
8334 && (tga_fd->type == STT_FUNC
8335 || tga_fd->needs_plt)
8336 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8337 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
8338 && tga_fd->root.type == bfd_link_hash_undefweak)))
8340 struct plt_entry *ent;
8342 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8343 if (ent->plt.refcount > 0)
8347 tga_fd->root.type = bfd_link_hash_indirect;
8348 tga_fd->root.u.i.link = &opt_fd->root;
8349 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8351 if (opt_fd->dynindx != -1)
8353 /* Use __tls_get_addr_opt in dynamic relocations. */
8354 opt_fd->dynindx = -1;
8355 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8356 opt_fd->dynstr_index);
8357 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8360 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8361 tga = &htab->tls_get_addr->elf;
8362 if (opt != NULL && tga != NULL)
8364 tga->root.type = bfd_link_hash_indirect;
8365 tga->root.u.i.link = &opt->root;
8366 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8368 _bfd_elf_link_hash_hide_symbol (info, opt,
8370 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8372 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8373 htab->tls_get_addr_fd->is_func_descriptor = 1;
8374 if (htab->tls_get_addr != NULL)
8376 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8377 htab->tls_get_addr->is_func = 1;
8382 else if (htab->params->tls_get_addr_opt < 0)
8383 htab->params->tls_get_addr_opt = 0;
8385 return _bfd_elf_tls_setup (info->output_bfd, info);
8388 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8392 branch_reloc_hash_match (const bfd *ibfd,
8393 const Elf_Internal_Rela *rel,
8394 const struct ppc_link_hash_entry *hash1,
8395 const struct ppc_link_hash_entry *hash2)
8397 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8398 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8399 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8401 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8403 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8404 struct elf_link_hash_entry *h;
8406 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8407 h = elf_follow_link (h);
8408 if (h == &hash1->elf || h == &hash2->elf)
8414 /* Run through all the TLS relocs looking for optimization
8415 opportunities. The linker has been hacked (see ppc64elf.em) to do
8416 a preliminary section layout so that we know the TLS segment
8417 offsets. We can't optimize earlier because some optimizations need
8418 to know the tp offset, and we need to optimize before allocating
8419 dynamic relocations. */
8422 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8426 struct ppc_link_hash_table *htab;
8427 unsigned char *toc_ref;
8430 if (!bfd_link_executable (info))
8433 htab = ppc_hash_table (info);
8437 /* Make two passes over the relocs. On the first pass, mark toc
8438 entries involved with tls relocs, and check that tls relocs
8439 involved in setting up a tls_get_addr call are indeed followed by
8440 such a call. If they are not, we can't do any tls optimization.
8441 On the second pass twiddle tls_mask flags to notify
8442 relocate_section that optimization can be done, and adjust got
8443 and plt refcounts. */
8445 for (pass = 0; pass < 2; ++pass)
8446 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8448 Elf_Internal_Sym *locsyms = NULL;
8449 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8451 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8452 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8454 Elf_Internal_Rela *relstart, *rel, *relend;
8455 bfd_boolean found_tls_get_addr_arg = 0;
8457 /* Read the relocations. */
8458 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8460 if (relstart == NULL)
8466 relend = relstart + sec->reloc_count;
8467 for (rel = relstart; rel < relend; rel++)
8469 enum elf_ppc64_reloc_type r_type;
8470 unsigned long r_symndx;
8471 struct elf_link_hash_entry *h;
8472 Elf_Internal_Sym *sym;
8474 unsigned char *tls_mask;
8475 unsigned char tls_set, tls_clear, tls_type = 0;
8477 bfd_boolean ok_tprel, is_local;
8478 long toc_ref_index = 0;
8479 int expecting_tls_get_addr = 0;
8480 bfd_boolean ret = FALSE;
8482 r_symndx = ELF64_R_SYM (rel->r_info);
8483 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8487 if (elf_section_data (sec)->relocs != relstart)
8489 if (toc_ref != NULL)
8492 && (elf_symtab_hdr (ibfd).contents
8493 != (unsigned char *) locsyms))
8500 if (h->root.type == bfd_link_hash_defined
8501 || h->root.type == bfd_link_hash_defweak)
8502 value = h->root.u.def.value;
8503 else if (h->root.type == bfd_link_hash_undefweak)
8507 found_tls_get_addr_arg = 0;
8512 /* Symbols referenced by TLS relocs must be of type
8513 STT_TLS. So no need for .opd local sym adjust. */
8514 value = sym->st_value;
8523 && h->root.type == bfd_link_hash_undefweak)
8525 else if (sym_sec != NULL
8526 && sym_sec->output_section != NULL)
8528 value += sym_sec->output_offset;
8529 value += sym_sec->output_section->vma;
8530 value -= htab->elf.tls_sec->vma;
8531 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8532 < (bfd_vma) 1 << 32);
8536 r_type = ELF64_R_TYPE (rel->r_info);
8537 /* If this section has old-style __tls_get_addr calls
8538 without marker relocs, then check that each
8539 __tls_get_addr call reloc is preceded by a reloc
8540 that conceivably belongs to the __tls_get_addr arg
8541 setup insn. If we don't find matching arg setup
8542 relocs, don't do any tls optimization. */
8544 && sec->has_tls_get_addr_call
8546 && (h == &htab->tls_get_addr->elf
8547 || h == &htab->tls_get_addr_fd->elf)
8548 && !found_tls_get_addr_arg
8549 && is_branch_reloc (r_type))
8551 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8552 "TLS optimization disabled\n"),
8553 ibfd, sec, rel->r_offset);
8558 found_tls_get_addr_arg = 0;
8561 case R_PPC64_GOT_TLSLD16:
8562 case R_PPC64_GOT_TLSLD16_LO:
8563 expecting_tls_get_addr = 1;
8564 found_tls_get_addr_arg = 1;
8567 case R_PPC64_GOT_TLSLD16_HI:
8568 case R_PPC64_GOT_TLSLD16_HA:
8569 /* These relocs should never be against a symbol
8570 defined in a shared lib. Leave them alone if
8571 that turns out to be the case. */
8578 tls_type = TLS_TLS | TLS_LD;
8581 case R_PPC64_GOT_TLSGD16:
8582 case R_PPC64_GOT_TLSGD16_LO:
8583 expecting_tls_get_addr = 1;
8584 found_tls_get_addr_arg = 1;
8587 case R_PPC64_GOT_TLSGD16_HI:
8588 case R_PPC64_GOT_TLSGD16_HA:
8594 tls_set = TLS_TLS | TLS_TPRELGD;
8596 tls_type = TLS_TLS | TLS_GD;
8599 case R_PPC64_GOT_TPREL16_DS:
8600 case R_PPC64_GOT_TPREL16_LO_DS:
8601 case R_PPC64_GOT_TPREL16_HI:
8602 case R_PPC64_GOT_TPREL16_HA:
8607 tls_clear = TLS_TPREL;
8608 tls_type = TLS_TLS | TLS_TPREL;
8615 found_tls_get_addr_arg = 1;
8620 case R_PPC64_TOC16_LO:
8621 if (sym_sec == NULL || sym_sec != toc)
8624 /* Mark this toc entry as referenced by a TLS
8625 code sequence. We can do that now in the
8626 case of R_PPC64_TLS, and after checking for
8627 tls_get_addr for the TOC16 relocs. */
8628 if (toc_ref == NULL)
8629 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8630 if (toc_ref == NULL)
8634 value = h->root.u.def.value;
8636 value = sym->st_value;
8637 value += rel->r_addend;
8640 BFD_ASSERT (value < toc->size
8641 && toc->output_offset % 8 == 0);
8642 toc_ref_index = (value + toc->output_offset) / 8;
8643 if (r_type == R_PPC64_TLS
8644 || r_type == R_PPC64_TLSGD
8645 || r_type == R_PPC64_TLSLD)
8647 toc_ref[toc_ref_index] = 1;
8651 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8656 expecting_tls_get_addr = 2;
8659 case R_PPC64_TPREL64:
8663 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8668 tls_set = TLS_EXPLICIT;
8669 tls_clear = TLS_TPREL;
8674 case R_PPC64_DTPMOD64:
8678 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8680 if (rel + 1 < relend
8682 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8683 && rel[1].r_offset == rel->r_offset + 8)
8687 tls_set = TLS_EXPLICIT | TLS_GD;
8690 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8699 tls_set = TLS_EXPLICIT;
8710 if (!expecting_tls_get_addr
8711 || !sec->has_tls_get_addr_call)
8714 if (rel + 1 < relend
8715 && branch_reloc_hash_match (ibfd, rel + 1,
8717 htab->tls_get_addr_fd))
8719 if (expecting_tls_get_addr == 2)
8721 /* Check for toc tls entries. */
8722 unsigned char *toc_tls;
8725 retval = get_tls_mask (&toc_tls, NULL, NULL,
8730 if (toc_tls != NULL)
8732 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8733 found_tls_get_addr_arg = 1;
8735 toc_ref[toc_ref_index] = 1;
8741 if (expecting_tls_get_addr != 1)
8744 /* Uh oh, we didn't find the expected call. We
8745 could just mark this symbol to exclude it
8746 from tls optimization but it's safer to skip
8747 the entire optimization. */
8748 /* xgettext:c-format */
8749 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8750 "TLS optimization disabled\n"),
8751 ibfd, sec, rel->r_offset);
8756 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8758 struct plt_entry *ent;
8759 for (ent = htab->tls_get_addr->elf.plt.plist;
8762 if (ent->addend == 0)
8764 if (ent->plt.refcount > 0)
8766 ent->plt.refcount -= 1;
8767 expecting_tls_get_addr = 0;
8773 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8775 struct plt_entry *ent;
8776 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8779 if (ent->addend == 0)
8781 if (ent->plt.refcount > 0)
8782 ent->plt.refcount -= 1;
8790 if ((tls_set & TLS_EXPLICIT) == 0)
8792 struct got_entry *ent;
8794 /* Adjust got entry for this reloc. */
8798 ent = elf_local_got_ents (ibfd)[r_symndx];
8800 for (; ent != NULL; ent = ent->next)
8801 if (ent->addend == rel->r_addend
8802 && ent->owner == ibfd
8803 && ent->tls_type == tls_type)
8810 /* We managed to get rid of a got entry. */
8811 if (ent->got.refcount > 0)
8812 ent->got.refcount -= 1;
8817 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8818 we'll lose one or two dyn relocs. */
8819 if (!dec_dynrel_count (rel->r_info, sec, info,
8823 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8825 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8831 *tls_mask |= tls_set;
8832 *tls_mask &= ~tls_clear;
8835 if (elf_section_data (sec)->relocs != relstart)
8840 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8842 if (!info->keep_memory)
8845 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8849 if (toc_ref != NULL)
8854 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8855 the values of any global symbols in a toc section that has been
8856 edited. Globals in toc sections should be a rarity, so this function
8857 sets a flag if any are found in toc sections other than the one just
8858 edited, so that futher hash table traversals can be avoided. */
8860 struct adjust_toc_info
8863 unsigned long *skip;
8864 bfd_boolean global_toc_syms;
8867 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8870 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8872 struct ppc_link_hash_entry *eh;
8873 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8876 if (h->root.type != bfd_link_hash_defined
8877 && h->root.type != bfd_link_hash_defweak)
8880 eh = (struct ppc_link_hash_entry *) h;
8881 if (eh->adjust_done)
8884 if (eh->elf.root.u.def.section == toc_inf->toc)
8886 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8887 i = toc_inf->toc->rawsize >> 3;
8889 i = eh->elf.root.u.def.value >> 3;
8891 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8894 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8897 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8898 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8901 eh->elf.root.u.def.value -= toc_inf->skip[i];
8902 eh->adjust_done = 1;
8904 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8905 toc_inf->global_toc_syms = TRUE;
8910 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8911 on a _LO variety toc/got reloc. */
8914 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8916 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8917 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8918 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8919 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8920 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8921 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8922 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8923 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8924 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8925 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8926 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8927 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8928 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8929 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8930 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8931 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8932 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8933 /* Exclude lfqu by testing reloc. If relocs are ever
8934 defined for the reduced D field in psq_lu then those
8935 will need testing too. */
8936 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8937 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8939 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8940 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8941 /* Exclude stfqu. psq_stu as above for psq_lu. */
8942 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8943 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8944 && (insn & 1) == 0));
8947 /* Examine all relocs referencing .toc sections in order to remove
8948 unused .toc entries. */
8951 ppc64_elf_edit_toc (struct bfd_link_info *info)
8954 struct adjust_toc_info toc_inf;
8955 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8957 htab->do_toc_opt = 1;
8958 toc_inf.global_toc_syms = TRUE;
8959 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8961 asection *toc, *sec;
8962 Elf_Internal_Shdr *symtab_hdr;
8963 Elf_Internal_Sym *local_syms;
8964 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8965 unsigned long *skip, *drop;
8966 unsigned char *used;
8967 unsigned char *keep, last, some_unused;
8969 if (!is_ppc64_elf (ibfd))
8972 toc = bfd_get_section_by_name (ibfd, ".toc");
8975 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8976 || discarded_section (toc))
8981 symtab_hdr = &elf_symtab_hdr (ibfd);
8983 /* Look at sections dropped from the final link. */
8986 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8988 if (sec->reloc_count == 0
8989 || !discarded_section (sec)
8990 || get_opd_info (sec)
8991 || (sec->flags & SEC_ALLOC) == 0
8992 || (sec->flags & SEC_DEBUGGING) != 0)
8995 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8996 if (relstart == NULL)
8999 /* Run through the relocs to see which toc entries might be
9001 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9003 enum elf_ppc64_reloc_type r_type;
9004 unsigned long r_symndx;
9006 struct elf_link_hash_entry *h;
9007 Elf_Internal_Sym *sym;
9010 r_type = ELF64_R_TYPE (rel->r_info);
9017 case R_PPC64_TOC16_LO:
9018 case R_PPC64_TOC16_HI:
9019 case R_PPC64_TOC16_HA:
9020 case R_PPC64_TOC16_DS:
9021 case R_PPC64_TOC16_LO_DS:
9025 r_symndx = ELF64_R_SYM (rel->r_info);
9026 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9034 val = h->root.u.def.value;
9036 val = sym->st_value;
9037 val += rel->r_addend;
9039 if (val >= toc->size)
9042 /* Anything in the toc ought to be aligned to 8 bytes.
9043 If not, don't mark as unused. */
9049 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9054 skip[val >> 3] = ref_from_discarded;
9057 if (elf_section_data (sec)->relocs != relstart)
9061 /* For largetoc loads of address constants, we can convert
9062 . addis rx,2,addr@got@ha
9063 . ld ry,addr@got@l(rx)
9065 . addis rx,2,addr@toc@ha
9066 . addi ry,rx,addr@toc@l
9067 when addr is within 2G of the toc pointer. This then means
9068 that the word storing "addr" in the toc is no longer needed. */
9070 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9071 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9072 && toc->reloc_count != 0)
9074 /* Read toc relocs. */
9075 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9077 if (toc_relocs == NULL)
9080 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9082 enum elf_ppc64_reloc_type r_type;
9083 unsigned long r_symndx;
9085 struct elf_link_hash_entry *h;
9086 Elf_Internal_Sym *sym;
9089 r_type = ELF64_R_TYPE (rel->r_info);
9090 if (r_type != R_PPC64_ADDR64)
9093 r_symndx = ELF64_R_SYM (rel->r_info);
9094 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9099 || sym_sec->output_section == NULL
9100 || discarded_section (sym_sec))
9103 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9108 if (h->type == STT_GNU_IFUNC)
9110 val = h->root.u.def.value;
9114 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9116 val = sym->st_value;
9118 val += rel->r_addend;
9119 val += sym_sec->output_section->vma + sym_sec->output_offset;
9121 /* We don't yet know the exact toc pointer value, but we
9122 know it will be somewhere in the toc section. Don't
9123 optimize if the difference from any possible toc
9124 pointer is outside [ff..f80008000, 7fff7fff]. */
9125 addr = toc->output_section->vma + TOC_BASE_OFF;
9126 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9129 addr = toc->output_section->vma + toc->output_section->rawsize;
9130 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9135 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9140 skip[rel->r_offset >> 3]
9141 |= can_optimize | ((rel - toc_relocs) << 2);
9148 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9152 if (local_syms != NULL
9153 && symtab_hdr->contents != (unsigned char *) local_syms)
9157 && elf_section_data (sec)->relocs != relstart)
9159 if (toc_relocs != NULL
9160 && elf_section_data (toc)->relocs != toc_relocs)
9167 /* Now check all kept sections that might reference the toc.
9168 Check the toc itself last. */
9169 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9172 sec = (sec == toc ? NULL
9173 : sec->next == NULL ? toc
9174 : sec->next == toc && toc->next ? toc->next
9179 if (sec->reloc_count == 0
9180 || discarded_section (sec)
9181 || get_opd_info (sec)
9182 || (sec->flags & SEC_ALLOC) == 0
9183 || (sec->flags & SEC_DEBUGGING) != 0)
9186 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9188 if (relstart == NULL)
9194 /* Mark toc entries referenced as used. */
9198 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9200 enum elf_ppc64_reloc_type r_type;
9201 unsigned long r_symndx;
9203 struct elf_link_hash_entry *h;
9204 Elf_Internal_Sym *sym;
9206 enum {no_check, check_lo, check_ha} insn_check;
9208 r_type = ELF64_R_TYPE (rel->r_info);
9212 insn_check = no_check;
9215 case R_PPC64_GOT_TLSLD16_HA:
9216 case R_PPC64_GOT_TLSGD16_HA:
9217 case R_PPC64_GOT_TPREL16_HA:
9218 case R_PPC64_GOT_DTPREL16_HA:
9219 case R_PPC64_GOT16_HA:
9220 case R_PPC64_TOC16_HA:
9221 insn_check = check_ha;
9224 case R_PPC64_GOT_TLSLD16_LO:
9225 case R_PPC64_GOT_TLSGD16_LO:
9226 case R_PPC64_GOT_TPREL16_LO_DS:
9227 case R_PPC64_GOT_DTPREL16_LO_DS:
9228 case R_PPC64_GOT16_LO:
9229 case R_PPC64_GOT16_LO_DS:
9230 case R_PPC64_TOC16_LO:
9231 case R_PPC64_TOC16_LO_DS:
9232 insn_check = check_lo;
9236 if (insn_check != no_check)
9238 bfd_vma off = rel->r_offset & ~3;
9239 unsigned char buf[4];
9242 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9247 insn = bfd_get_32 (ibfd, buf);
9248 if (insn_check == check_lo
9249 ? !ok_lo_toc_insn (insn, r_type)
9250 : ((insn & ((0x3f << 26) | 0x1f << 16))
9251 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9255 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9256 sprintf (str, "%#08x", insn);
9257 info->callbacks->einfo
9258 /* xgettext:c-format */
9259 (_("%H: toc optimization is not supported for"
9260 " %s instruction.\n"),
9261 ibfd, sec, rel->r_offset & ~3, str);
9268 case R_PPC64_TOC16_LO:
9269 case R_PPC64_TOC16_HI:
9270 case R_PPC64_TOC16_HA:
9271 case R_PPC64_TOC16_DS:
9272 case R_PPC64_TOC16_LO_DS:
9273 /* In case we're taking addresses of toc entries. */
9274 case R_PPC64_ADDR64:
9281 r_symndx = ELF64_R_SYM (rel->r_info);
9282 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9293 val = h->root.u.def.value;
9295 val = sym->st_value;
9296 val += rel->r_addend;
9298 if (val >= toc->size)
9301 if ((skip[val >> 3] & can_optimize) != 0)
9308 case R_PPC64_TOC16_HA:
9311 case R_PPC64_TOC16_LO_DS:
9312 off = rel->r_offset;
9313 off += (bfd_big_endian (ibfd) ? -2 : 3);
9314 if (!bfd_get_section_contents (ibfd, sec, &opc,
9320 if ((opc & (0x3f << 2)) == (58u << 2))
9325 /* Wrong sort of reloc, or not a ld. We may
9326 as well clear ref_from_discarded too. */
9333 /* For the toc section, we only mark as used if this
9334 entry itself isn't unused. */
9335 else if ((used[rel->r_offset >> 3]
9336 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9339 /* Do all the relocs again, to catch reference
9348 if (elf_section_data (sec)->relocs != relstart)
9352 /* Merge the used and skip arrays. Assume that TOC
9353 doublewords not appearing as either used or unused belong
9354 to to an entry more than one doubleword in size. */
9355 for (drop = skip, keep = used, last = 0, some_unused = 0;
9356 drop < skip + (toc->size + 7) / 8;
9361 *drop &= ~ref_from_discarded;
9362 if ((*drop & can_optimize) != 0)
9366 else if ((*drop & ref_from_discarded) != 0)
9369 last = ref_from_discarded;
9379 bfd_byte *contents, *src;
9381 Elf_Internal_Sym *sym;
9382 bfd_boolean local_toc_syms = FALSE;
9384 /* Shuffle the toc contents, and at the same time convert the
9385 skip array from booleans into offsets. */
9386 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9389 elf_section_data (toc)->this_hdr.contents = contents;
9391 for (src = contents, off = 0, drop = skip;
9392 src < contents + toc->size;
9395 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9400 memcpy (src - off, src, 8);
9404 toc->rawsize = toc->size;
9405 toc->size = src - contents - off;
9407 /* Adjust addends for relocs against the toc section sym,
9408 and optimize any accesses we can. */
9409 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9411 if (sec->reloc_count == 0
9412 || discarded_section (sec))
9415 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9417 if (relstart == NULL)
9420 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9422 enum elf_ppc64_reloc_type r_type;
9423 unsigned long r_symndx;
9425 struct elf_link_hash_entry *h;
9428 r_type = ELF64_R_TYPE (rel->r_info);
9435 case R_PPC64_TOC16_LO:
9436 case R_PPC64_TOC16_HI:
9437 case R_PPC64_TOC16_HA:
9438 case R_PPC64_TOC16_DS:
9439 case R_PPC64_TOC16_LO_DS:
9440 case R_PPC64_ADDR64:
9444 r_symndx = ELF64_R_SYM (rel->r_info);
9445 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9453 val = h->root.u.def.value;
9456 val = sym->st_value;
9458 local_toc_syms = TRUE;
9461 val += rel->r_addend;
9463 if (val > toc->rawsize)
9465 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9467 else if ((skip[val >> 3] & can_optimize) != 0)
9469 Elf_Internal_Rela *tocrel
9470 = toc_relocs + (skip[val >> 3] >> 2);
9471 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9475 case R_PPC64_TOC16_HA:
9476 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9479 case R_PPC64_TOC16_LO_DS:
9480 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9484 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9486 info->callbacks->einfo
9487 /* xgettext:c-format */
9488 (_("%H: %s references "
9489 "optimized away TOC entry\n"),
9490 ibfd, sec, rel->r_offset,
9491 ppc64_elf_howto_table[r_type]->name);
9492 bfd_set_error (bfd_error_bad_value);
9495 rel->r_addend = tocrel->r_addend;
9496 elf_section_data (sec)->relocs = relstart;
9500 if (h != NULL || sym->st_value != 0)
9503 rel->r_addend -= skip[val >> 3];
9504 elf_section_data (sec)->relocs = relstart;
9507 if (elf_section_data (sec)->relocs != relstart)
9511 /* We shouldn't have local or global symbols defined in the TOC,
9512 but handle them anyway. */
9513 if (local_syms != NULL)
9514 for (sym = local_syms;
9515 sym < local_syms + symtab_hdr->sh_info;
9517 if (sym->st_value != 0
9518 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9522 if (sym->st_value > toc->rawsize)
9523 i = toc->rawsize >> 3;
9525 i = sym->st_value >> 3;
9527 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9531 (_("%s defined on removed toc entry"),
9532 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9535 while ((skip[i] & (ref_from_discarded | can_optimize)));
9536 sym->st_value = (bfd_vma) i << 3;
9539 sym->st_value -= skip[i];
9540 symtab_hdr->contents = (unsigned char *) local_syms;
9543 /* Adjust any global syms defined in this toc input section. */
9544 if (toc_inf.global_toc_syms)
9547 toc_inf.skip = skip;
9548 toc_inf.global_toc_syms = FALSE;
9549 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9553 if (toc->reloc_count != 0)
9555 Elf_Internal_Shdr *rel_hdr;
9556 Elf_Internal_Rela *wrel;
9559 /* Remove unused toc relocs, and adjust those we keep. */
9560 if (toc_relocs == NULL)
9561 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9563 if (toc_relocs == NULL)
9567 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9568 if ((skip[rel->r_offset >> 3]
9569 & (ref_from_discarded | can_optimize)) == 0)
9571 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9572 wrel->r_info = rel->r_info;
9573 wrel->r_addend = rel->r_addend;
9576 else if (!dec_dynrel_count (rel->r_info, toc, info,
9577 &local_syms, NULL, NULL))
9580 elf_section_data (toc)->relocs = toc_relocs;
9581 toc->reloc_count = wrel - toc_relocs;
9582 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9583 sz = rel_hdr->sh_entsize;
9584 rel_hdr->sh_size = toc->reloc_count * sz;
9587 else if (toc_relocs != NULL
9588 && elf_section_data (toc)->relocs != toc_relocs)
9591 if (local_syms != NULL
9592 && symtab_hdr->contents != (unsigned char *) local_syms)
9594 if (!info->keep_memory)
9597 symtab_hdr->contents = (unsigned char *) local_syms;
9605 /* Return true iff input section I references the TOC using
9606 instructions limited to +/-32k offsets. */
9609 ppc64_elf_has_small_toc_reloc (asection *i)
9611 return (is_ppc64_elf (i->owner)
9612 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9615 /* Allocate space for one GOT entry. */
9618 allocate_got (struct elf_link_hash_entry *h,
9619 struct bfd_link_info *info,
9620 struct got_entry *gent)
9622 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9623 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9624 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9626 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9627 ? 2 : 1) * sizeof (Elf64_External_Rela);
9628 asection *got = ppc64_elf_tdata (gent->owner)->got;
9630 gent->got.offset = got->size;
9631 got->size += entsize;
9633 if (h->type == STT_GNU_IFUNC)
9635 htab->elf.irelplt->size += rentsize;
9636 htab->got_reli_size += rentsize;
9638 else if ((bfd_link_pic (info)
9639 || (htab->elf.dynamic_sections_created
9641 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9642 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9643 || h->root.type != bfd_link_hash_undefweak))
9645 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9646 relgot->size += rentsize;
9650 /* This function merges got entries in the same toc group. */
9653 merge_got_entries (struct got_entry **pent)
9655 struct got_entry *ent, *ent2;
9657 for (ent = *pent; ent != NULL; ent = ent->next)
9658 if (!ent->is_indirect)
9659 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9660 if (!ent2->is_indirect
9661 && ent2->addend == ent->addend
9662 && ent2->tls_type == ent->tls_type
9663 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9665 ent2->is_indirect = TRUE;
9666 ent2->got.ent = ent;
9670 /* If H is undefined weak, make it dynamic if that makes sense. */
9673 ensure_undefweak_dynamic (struct bfd_link_info *info,
9674 struct elf_link_hash_entry *h)
9676 struct elf_link_hash_table *htab = elf_hash_table (info);
9678 if (htab->dynamic_sections_created
9679 && h->root.type == bfd_link_hash_undefweak
9682 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9683 return bfd_elf_link_record_dynamic_symbol (info, h);
9687 /* Allocate space in .plt, .got and associated reloc sections for
9691 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9693 struct bfd_link_info *info;
9694 struct ppc_link_hash_table *htab;
9696 struct ppc_link_hash_entry *eh;
9697 struct got_entry **pgent, *gent;
9699 if (h->root.type == bfd_link_hash_indirect)
9702 info = (struct bfd_link_info *) inf;
9703 htab = ppc_hash_table (info);
9707 eh = (struct ppc_link_hash_entry *) h;
9708 /* Run through the TLS GD got entries first if we're changing them
9710 if ((eh->tls_mask & TLS_TPRELGD) != 0)
9711 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9712 if (gent->got.refcount > 0
9713 && (gent->tls_type & TLS_GD) != 0)
9715 /* This was a GD entry that has been converted to TPREL. If
9716 there happens to be a TPREL entry we can use that one. */
9717 struct got_entry *ent;
9718 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9719 if (ent->got.refcount > 0
9720 && (ent->tls_type & TLS_TPREL) != 0
9721 && ent->addend == gent->addend
9722 && ent->owner == gent->owner)
9724 gent->got.refcount = 0;
9728 /* If not, then we'll be using our own TPREL entry. */
9729 if (gent->got.refcount != 0)
9730 gent->tls_type = TLS_TLS | TLS_TPREL;
9733 /* Remove any list entry that won't generate a word in the GOT before
9734 we call merge_got_entries. Otherwise we risk merging to empty
9736 pgent = &h->got.glist;
9737 while ((gent = *pgent) != NULL)
9738 if (gent->got.refcount > 0)
9740 if ((gent->tls_type & TLS_LD) != 0
9743 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9744 *pgent = gent->next;
9747 pgent = &gent->next;
9750 *pgent = gent->next;
9752 if (!htab->do_multi_toc)
9753 merge_got_entries (&h->got.glist);
9755 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9756 if (!gent->is_indirect)
9758 /* Make sure this symbol is output as a dynamic symbol.
9759 Undefined weak syms won't yet be marked as dynamic. */
9760 if (!ensure_undefweak_dynamic (info, h))
9763 if (!is_ppc64_elf (gent->owner))
9766 allocate_got (h, info, gent);
9769 if (!htab->elf.dynamic_sections_created
9770 && h->type != STT_GNU_IFUNC)
9771 eh->dyn_relocs = NULL;
9773 if (eh->dyn_relocs != NULL)
9775 struct elf_dyn_relocs *p, **pp;
9777 /* In the shared -Bsymbolic case, discard space allocated for
9778 dynamic pc-relative relocs against symbols which turn out to
9779 be defined in regular objects. For the normal shared case,
9780 discard space for relocs that have become local due to symbol
9781 visibility changes. */
9783 if (bfd_link_pic (info))
9785 /* Relocs that use pc_count are those that appear on a call
9786 insn, or certain REL relocs (see must_be_dyn_reloc) that
9787 can be generated via assembly. We want calls to
9788 protected symbols to resolve directly to the function
9789 rather than going via the plt. If people want function
9790 pointer comparisons to work as expected then they should
9791 avoid writing weird assembly. */
9792 if (SYMBOL_CALLS_LOCAL (info, h))
9794 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9796 p->count -= p->pc_count;
9805 /* Also discard relocs on undefined weak syms with
9806 non-default visibility. */
9807 if (eh->dyn_relocs != NULL
9808 && h->root.type == bfd_link_hash_undefweak)
9810 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9811 eh->dyn_relocs = NULL;
9813 /* Make sure this symbol is output as a dynamic symbol.
9814 Undefined weak syms won't yet be marked as dynamic. */
9815 else if (!ensure_undefweak_dynamic (info, h))
9819 else if (h->type == STT_GNU_IFUNC)
9821 /* A plt entry is always created when making direct calls to
9822 an ifunc, even when building a static executable, but
9823 that doesn't cover all cases. We may have only an ifunc
9824 initialised function pointer for a given ifunc symbol.
9826 For ELFv2, dynamic relocations are not required when
9827 generating a global entry PLT stub. */
9828 if (abiversion (info->output_bfd) >= 2)
9830 if (global_entry_stub (h))
9831 eh->dyn_relocs = NULL;
9834 /* For ELFv1 we have function descriptors. Descriptors need
9835 to be treated like PLT entries and thus have dynamic
9836 relocations. One exception is when the function
9837 descriptor is copied into .dynbss (which should only
9838 happen with ancient versions of gcc). */
9839 else if (h->needs_copy)
9840 eh->dyn_relocs = NULL;
9842 else if (ELIMINATE_COPY_RELOCS)
9844 /* For the non-pic case, discard space for relocs against
9845 symbols which turn out to need copy relocs or are not
9850 /* Make sure this symbol is output as a dynamic symbol.
9851 Undefined weak syms won't yet be marked as dynamic. */
9852 if (!ensure_undefweak_dynamic (info, h))
9855 if (h->dynindx == -1)
9856 eh->dyn_relocs = NULL;
9859 eh->dyn_relocs = NULL;
9862 /* Finally, allocate space. */
9863 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9865 asection *sreloc = elf_section_data (p->sec)->sreloc;
9866 if (eh->elf.type == STT_GNU_IFUNC)
9867 sreloc = htab->elf.irelplt;
9868 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9872 if ((htab->elf.dynamic_sections_created
9873 && h->dynindx != -1)
9874 || h->type == STT_GNU_IFUNC)
9876 struct plt_entry *pent;
9877 bfd_boolean doneone = FALSE;
9878 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9879 if (pent->plt.refcount > 0)
9881 if (!htab->elf.dynamic_sections_created
9882 || h->dynindx == -1)
9885 pent->plt.offset = s->size;
9886 s->size += PLT_ENTRY_SIZE (htab);
9887 s = htab->elf.irelplt;
9891 /* If this is the first .plt entry, make room for the special
9895 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9897 pent->plt.offset = s->size;
9899 /* Make room for this entry. */
9900 s->size += PLT_ENTRY_SIZE (htab);
9902 /* Make room for the .glink code. */
9905 s->size += GLINK_CALL_STUB_SIZE;
9908 /* We need bigger stubs past index 32767. */
9909 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9916 /* We also need to make an entry in the .rela.plt section. */
9917 s = htab->elf.srelplt;
9919 s->size += sizeof (Elf64_External_Rela);
9923 pent->plt.offset = (bfd_vma) -1;
9926 h->plt.plist = NULL;
9932 h->plt.plist = NULL;
9939 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9940 to set up space for global entry stubs. These are put in glink,
9941 after the branch table. */
9944 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9946 struct bfd_link_info *info;
9947 struct ppc_link_hash_table *htab;
9948 struct plt_entry *pent;
9951 if (h->root.type == bfd_link_hash_indirect)
9954 if (!h->pointer_equality_needed)
9961 htab = ppc_hash_table (info);
9966 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9967 if (pent->plt.offset != (bfd_vma) -1
9968 && pent->addend == 0)
9970 /* For ELFv2, if this symbol is not defined in a regular file
9971 and we are not generating a shared library or pie, then we
9972 need to define the symbol in the executable on a call stub.
9973 This is to avoid text relocations. */
9974 s->size = (s->size + 15) & -16;
9975 h->root.type = bfd_link_hash_defined;
9976 h->root.u.def.section = s;
9977 h->root.u.def.value = s->size;
9984 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9985 read-only sections. */
9988 maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
9990 if (h->root.type == bfd_link_hash_indirect)
9993 if (readonly_dynrelocs (h))
9995 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
9997 /* Not an error, just cut short the traversal. */
10003 /* Set the sizes of the dynamic sections. */
10006 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10007 struct bfd_link_info *info)
10009 struct ppc_link_hash_table *htab;
10012 bfd_boolean relocs;
10014 struct got_entry *first_tlsld;
10016 htab = ppc_hash_table (info);
10020 dynobj = htab->elf.dynobj;
10021 if (dynobj == NULL)
10024 if (htab->elf.dynamic_sections_created)
10026 /* Set the contents of the .interp section to the interpreter. */
10027 if (bfd_link_executable (info) && !info->nointerp)
10029 s = bfd_get_linker_section (dynobj, ".interp");
10032 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10033 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10037 /* Set up .got offsets for local syms, and space for local dynamic
10039 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10041 struct got_entry **lgot_ents;
10042 struct got_entry **end_lgot_ents;
10043 struct plt_entry **local_plt;
10044 struct plt_entry **end_local_plt;
10045 unsigned char *lgot_masks;
10046 bfd_size_type locsymcount;
10047 Elf_Internal_Shdr *symtab_hdr;
10049 if (!is_ppc64_elf (ibfd))
10052 for (s = ibfd->sections; s != NULL; s = s->next)
10054 struct ppc_dyn_relocs *p;
10056 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10058 if (!bfd_is_abs_section (p->sec)
10059 && bfd_is_abs_section (p->sec->output_section))
10061 /* Input section has been discarded, either because
10062 it is a copy of a linkonce section or due to
10063 linker script /DISCARD/, so we'll be discarding
10066 else if (p->count != 0)
10068 asection *srel = elf_section_data (p->sec)->sreloc;
10070 srel = htab->elf.irelplt;
10071 srel->size += p->count * sizeof (Elf64_External_Rela);
10072 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10073 info->flags |= DF_TEXTREL;
10078 lgot_ents = elf_local_got_ents (ibfd);
10082 symtab_hdr = &elf_symtab_hdr (ibfd);
10083 locsymcount = symtab_hdr->sh_info;
10084 end_lgot_ents = lgot_ents + locsymcount;
10085 local_plt = (struct plt_entry **) end_lgot_ents;
10086 end_local_plt = local_plt + locsymcount;
10087 lgot_masks = (unsigned char *) end_local_plt;
10088 s = ppc64_elf_tdata (ibfd)->got;
10089 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10091 struct got_entry **pent, *ent;
10094 while ((ent = *pent) != NULL)
10095 if (ent->got.refcount > 0)
10097 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10099 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10104 unsigned int ent_size = 8;
10105 unsigned int rel_size = sizeof (Elf64_External_Rela);
10107 ent->got.offset = s->size;
10108 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10113 s->size += ent_size;
10114 if ((*lgot_masks & PLT_IFUNC) != 0)
10116 htab->elf.irelplt->size += rel_size;
10117 htab->got_reli_size += rel_size;
10119 else if (bfd_link_pic (info))
10121 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10122 srel->size += rel_size;
10131 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
10132 for (; local_plt < end_local_plt; ++local_plt)
10134 struct plt_entry *ent;
10136 for (ent = *local_plt; ent != NULL; ent = ent->next)
10137 if (ent->plt.refcount > 0)
10139 s = htab->elf.iplt;
10140 ent->plt.offset = s->size;
10141 s->size += PLT_ENTRY_SIZE (htab);
10143 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10146 ent->plt.offset = (bfd_vma) -1;
10150 /* Allocate global sym .plt and .got entries, and space for global
10151 sym dynamic relocs. */
10152 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10153 /* Stash the end of glink branch table. */
10154 if (htab->glink != NULL)
10155 htab->glink->rawsize = htab->glink->size;
10157 if (!htab->opd_abi && !bfd_link_pic (info))
10158 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10160 first_tlsld = NULL;
10161 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10163 struct got_entry *ent;
10165 if (!is_ppc64_elf (ibfd))
10168 ent = ppc64_tlsld_got (ibfd);
10169 if (ent->got.refcount > 0)
10171 if (!htab->do_multi_toc && first_tlsld != NULL)
10173 ent->is_indirect = TRUE;
10174 ent->got.ent = first_tlsld;
10178 if (first_tlsld == NULL)
10180 s = ppc64_elf_tdata (ibfd)->got;
10181 ent->got.offset = s->size;
10184 if (bfd_link_pic (info))
10186 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10187 srel->size += sizeof (Elf64_External_Rela);
10192 ent->got.offset = (bfd_vma) -1;
10195 /* We now have determined the sizes of the various dynamic sections.
10196 Allocate memory for them. */
10198 for (s = dynobj->sections; s != NULL; s = s->next)
10200 if ((s->flags & SEC_LINKER_CREATED) == 0)
10203 if (s == htab->brlt || s == htab->relbrlt)
10204 /* These haven't been allocated yet; don't strip. */
10206 else if (s == htab->elf.sgot
10207 || s == htab->elf.splt
10208 || s == htab->elf.iplt
10209 || s == htab->glink
10210 || s == htab->elf.sdynbss
10211 || s == htab->elf.sdynrelro)
10213 /* Strip this section if we don't need it; see the
10216 else if (s == htab->glink_eh_frame)
10218 if (!bfd_is_abs_section (s->output_section))
10219 /* Not sized yet. */
10222 else if (CONST_STRNEQ (s->name, ".rela"))
10226 if (s != htab->elf.srelplt)
10229 /* We use the reloc_count field as a counter if we need
10230 to copy relocs into the output file. */
10231 s->reloc_count = 0;
10236 /* It's not one of our sections, so don't allocate space. */
10242 /* If we don't need this section, strip it from the
10243 output file. This is mostly to handle .rela.bss and
10244 .rela.plt. We must create both sections in
10245 create_dynamic_sections, because they must be created
10246 before the linker maps input sections to output
10247 sections. The linker does that before
10248 adjust_dynamic_symbol is called, and it is that
10249 function which decides whether anything needs to go
10250 into these sections. */
10251 s->flags |= SEC_EXCLUDE;
10255 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10258 /* Allocate memory for the section contents. We use bfd_zalloc
10259 here in case unused entries are not reclaimed before the
10260 section's contents are written out. This should not happen,
10261 but this way if it does we get a R_PPC64_NONE reloc in .rela
10262 sections instead of garbage.
10263 We also rely on the section contents being zero when writing
10264 the GOT and .dynrelro. */
10265 s->contents = bfd_zalloc (dynobj, s->size);
10266 if (s->contents == NULL)
10270 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10272 if (!is_ppc64_elf (ibfd))
10275 s = ppc64_elf_tdata (ibfd)->got;
10276 if (s != NULL && s != htab->elf.sgot)
10279 s->flags |= SEC_EXCLUDE;
10282 s->contents = bfd_zalloc (ibfd, s->size);
10283 if (s->contents == NULL)
10287 s = ppc64_elf_tdata (ibfd)->relgot;
10291 s->flags |= SEC_EXCLUDE;
10294 s->contents = bfd_zalloc (ibfd, s->size);
10295 if (s->contents == NULL)
10298 s->reloc_count = 0;
10303 if (htab->elf.dynamic_sections_created)
10305 bfd_boolean tls_opt;
10307 /* Add some entries to the .dynamic section. We fill in the
10308 values later, in ppc64_elf_finish_dynamic_sections, but we
10309 must add the entries now so that we get the correct size for
10310 the .dynamic section. The DT_DEBUG entry is filled in by the
10311 dynamic linker and used by the debugger. */
10312 #define add_dynamic_entry(TAG, VAL) \
10313 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10315 if (bfd_link_executable (info))
10317 if (!add_dynamic_entry (DT_DEBUG, 0))
10321 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10323 if (!add_dynamic_entry (DT_PLTGOT, 0)
10324 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10325 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10326 || !add_dynamic_entry (DT_JMPREL, 0)
10327 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10331 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10333 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10334 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10338 tls_opt = (htab->params->tls_get_addr_opt
10339 && htab->tls_get_addr_fd != NULL
10340 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10341 if (tls_opt || !htab->opd_abi)
10343 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10349 if (!add_dynamic_entry (DT_RELA, 0)
10350 || !add_dynamic_entry (DT_RELASZ, 0)
10351 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10354 /* If any dynamic relocs apply to a read-only section,
10355 then we need a DT_TEXTREL entry. */
10356 if ((info->flags & DF_TEXTREL) == 0)
10357 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10359 if ((info->flags & DF_TEXTREL) != 0)
10361 if (!add_dynamic_entry (DT_TEXTREL, 0))
10366 #undef add_dynamic_entry
10371 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10374 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10376 if (h->plt.plist != NULL
10378 && !h->pointer_equality_needed)
10381 return _bfd_elf_hash_symbol (h);
10384 /* Determine the type of stub needed, if any, for a call. */
10386 static inline enum ppc_stub_type
10387 ppc_type_of_stub (asection *input_sec,
10388 const Elf_Internal_Rela *rel,
10389 struct ppc_link_hash_entry **hash,
10390 struct plt_entry **plt_ent,
10391 bfd_vma destination,
10392 unsigned long local_off)
10394 struct ppc_link_hash_entry *h = *hash;
10396 bfd_vma branch_offset;
10397 bfd_vma max_branch_offset;
10398 enum elf_ppc64_reloc_type r_type;
10402 struct plt_entry *ent;
10403 struct ppc_link_hash_entry *fdh = h;
10405 && h->oh->is_func_descriptor)
10407 fdh = ppc_follow_link (h->oh);
10411 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10412 if (ent->addend == rel->r_addend
10413 && ent->plt.offset != (bfd_vma) -1)
10416 return ppc_stub_plt_call;
10419 /* Here, we know we don't have a plt entry. If we don't have a
10420 either a defined function descriptor or a defined entry symbol
10421 in a regular object file, then it is pointless trying to make
10422 any other type of stub. */
10423 if (!is_static_defined (&fdh->elf)
10424 && !is_static_defined (&h->elf))
10425 return ppc_stub_none;
10427 else if (elf_local_got_ents (input_sec->owner) != NULL)
10429 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10430 struct plt_entry **local_plt = (struct plt_entry **)
10431 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10432 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10434 if (local_plt[r_symndx] != NULL)
10436 struct plt_entry *ent;
10438 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10439 if (ent->addend == rel->r_addend
10440 && ent->plt.offset != (bfd_vma) -1)
10443 return ppc_stub_plt_call;
10448 /* Determine where the call point is. */
10449 location = (input_sec->output_offset
10450 + input_sec->output_section->vma
10453 branch_offset = destination - location;
10454 r_type = ELF64_R_TYPE (rel->r_info);
10456 /* Determine if a long branch stub is needed. */
10457 max_branch_offset = 1 << 25;
10458 if (r_type != R_PPC64_REL24)
10459 max_branch_offset = 1 << 15;
10461 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10462 /* We need a stub. Figure out whether a long_branch or plt_branch
10463 is needed later. */
10464 return ppc_stub_long_branch;
10466 return ppc_stub_none;
10469 /* With power7 weakly ordered memory model, it is possible for ld.so
10470 to update a plt entry in one thread and have another thread see a
10471 stale zero toc entry. To avoid this we need some sort of acquire
10472 barrier in the call stub. One solution is to make the load of the
10473 toc word seem to appear to depend on the load of the function entry
10474 word. Another solution is to test for r2 being zero, and branch to
10475 the appropriate glink entry if so.
10477 . fake dep barrier compare
10478 . ld 12,xxx(2) ld 12,xxx(2)
10479 . mtctr 12 mtctr 12
10480 . xor 11,12,12 ld 2,xxx+8(2)
10481 . add 2,2,11 cmpldi 2,0
10482 . ld 2,xxx+8(2) bnectr+
10483 . bctr b <glink_entry>
10485 The solution involving the compare turns out to be faster, so
10486 that's what we use unless the branch won't reach. */
10488 #define ALWAYS_USE_FAKE_DEP 0
10489 #define ALWAYS_EMIT_R2SAVE 0
10491 #define PPC_LO(v) ((v) & 0xffff)
10492 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10493 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10495 static inline unsigned int
10496 plt_stub_size (struct ppc_link_hash_table *htab,
10497 struct ppc_stub_hash_entry *stub_entry,
10500 unsigned size = 12;
10502 if (ALWAYS_EMIT_R2SAVE
10503 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10505 if (PPC_HA (off) != 0)
10510 if (htab->params->plt_static_chain)
10512 if (htab->params->plt_thread_safe
10513 && htab->elf.dynamic_sections_created
10514 && stub_entry->h != NULL
10515 && stub_entry->h->elf.dynindx != -1)
10517 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10520 if (stub_entry->h != NULL
10521 && (stub_entry->h == htab->tls_get_addr_fd
10522 || stub_entry->h == htab->tls_get_addr)
10523 && htab->params->tls_get_addr_opt)
10528 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10529 then return the padding needed to do so. */
10530 static inline unsigned int
10531 plt_stub_pad (struct ppc_link_hash_table *htab,
10532 struct ppc_stub_hash_entry *stub_entry,
10535 int stub_align = 1 << htab->params->plt_stub_align;
10536 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10537 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10539 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10540 > ((stub_size - 1) & -stub_align))
10541 return stub_align - (stub_off & (stub_align - 1));
10545 /* Build a .plt call stub. */
10547 static inline bfd_byte *
10548 build_plt_stub (struct ppc_link_hash_table *htab,
10549 struct ppc_stub_hash_entry *stub_entry,
10550 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10552 bfd *obfd = htab->params->stub_bfd;
10553 bfd_boolean plt_load_toc = htab->opd_abi;
10554 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10555 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10556 && htab->elf.dynamic_sections_created
10557 && stub_entry->h != NULL
10558 && stub_entry->h->elf.dynindx != -1);
10559 bfd_boolean use_fake_dep = plt_thread_safe;
10560 bfd_vma cmp_branch_off = 0;
10562 if (!ALWAYS_USE_FAKE_DEP
10565 && !((stub_entry->h == htab->tls_get_addr_fd
10566 || stub_entry->h == htab->tls_get_addr)
10567 && htab->params->tls_get_addr_opt))
10569 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10570 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10571 / PLT_ENTRY_SIZE (htab));
10572 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10575 if (pltindex > 32768)
10576 glinkoff += (pltindex - 32768) * 4;
10578 + htab->glink->output_offset
10579 + htab->glink->output_section->vma);
10580 from = (p - stub_entry->group->stub_sec->contents
10581 + 4 * (ALWAYS_EMIT_R2SAVE
10582 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10583 + 4 * (PPC_HA (offset) != 0)
10584 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10585 != PPC_HA (offset))
10586 + 4 * (plt_static_chain != 0)
10588 + stub_entry->group->stub_sec->output_offset
10589 + stub_entry->group->stub_sec->output_section->vma);
10590 cmp_branch_off = to - from;
10591 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10594 if (PPC_HA (offset) != 0)
10598 if (ALWAYS_EMIT_R2SAVE
10599 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10600 r[0].r_offset += 4;
10601 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10602 r[1].r_offset = r[0].r_offset + 4;
10603 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10604 r[1].r_addend = r[0].r_addend;
10607 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10609 r[2].r_offset = r[1].r_offset + 4;
10610 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10611 r[2].r_addend = r[0].r_addend;
10615 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10616 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10617 r[2].r_addend = r[0].r_addend + 8;
10618 if (plt_static_chain)
10620 r[3].r_offset = r[2].r_offset + 4;
10621 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10622 r[3].r_addend = r[0].r_addend + 16;
10627 if (ALWAYS_EMIT_R2SAVE
10628 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10629 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10632 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10633 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10637 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10638 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10641 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10643 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10646 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10651 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10652 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10654 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10655 if (plt_static_chain)
10656 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10663 if (ALWAYS_EMIT_R2SAVE
10664 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10665 r[0].r_offset += 4;
10666 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10669 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10671 r[1].r_offset = r[0].r_offset + 4;
10672 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10673 r[1].r_addend = r[0].r_addend;
10677 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10678 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10679 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10680 if (plt_static_chain)
10682 r[2].r_offset = r[1].r_offset + 4;
10683 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10684 r[2].r_addend = r[0].r_addend + 8;
10689 if (ALWAYS_EMIT_R2SAVE
10690 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10691 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10692 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10694 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10696 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10699 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10704 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10705 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10707 if (plt_static_chain)
10708 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10709 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10712 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10714 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10715 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
10716 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10719 bfd_put_32 (obfd, BCTR, p), p += 4;
10723 /* Build a special .plt call stub for __tls_get_addr. */
10725 #define LD_R11_0R3 0xe9630000
10726 #define LD_R12_0R3 0xe9830000
10727 #define MR_R0_R3 0x7c601b78
10728 #define CMPDI_R11_0 0x2c2b0000
10729 #define ADD_R3_R12_R13 0x7c6c6a14
10730 #define BEQLR 0x4d820020
10731 #define MR_R3_R0 0x7c030378
10732 #define STD_R11_0R1 0xf9610000
10733 #define BCTRL 0x4e800421
10734 #define LD_R11_0R1 0xe9610000
10735 #define MTLR_R11 0x7d6803a6
10737 static inline bfd_byte *
10738 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10739 struct ppc_stub_hash_entry *stub_entry,
10740 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10742 bfd *obfd = htab->params->stub_bfd;
10744 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10745 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10746 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10747 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10748 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10749 bfd_put_32 (obfd, BEQLR, p), p += 4;
10750 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10751 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10752 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10755 r[0].r_offset += 9 * 4;
10756 p = build_plt_stub (htab, stub_entry, p, offset, r);
10757 bfd_put_32 (obfd, BCTRL, p - 4);
10759 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
10760 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10761 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10762 bfd_put_32 (obfd, BLR, p), p += 4;
10767 static Elf_Internal_Rela *
10768 get_relocs (asection *sec, int count)
10770 Elf_Internal_Rela *relocs;
10771 struct bfd_elf_section_data *elfsec_data;
10773 elfsec_data = elf_section_data (sec);
10774 relocs = elfsec_data->relocs;
10775 if (relocs == NULL)
10777 bfd_size_type relsize;
10778 relsize = sec->reloc_count * sizeof (*relocs);
10779 relocs = bfd_alloc (sec->owner, relsize);
10780 if (relocs == NULL)
10782 elfsec_data->relocs = relocs;
10783 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10784 sizeof (Elf_Internal_Shdr));
10785 if (elfsec_data->rela.hdr == NULL)
10787 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10788 * sizeof (Elf64_External_Rela));
10789 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10790 sec->reloc_count = 0;
10792 relocs += sec->reloc_count;
10793 sec->reloc_count += count;
10798 get_r2off (struct bfd_link_info *info,
10799 struct ppc_stub_hash_entry *stub_entry)
10801 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10802 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10806 /* Support linking -R objects. Get the toc pointer from the
10809 if (!htab->opd_abi)
10811 asection *opd = stub_entry->h->elf.root.u.def.section;
10812 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10814 if (strcmp (opd->name, ".opd") != 0
10815 || opd->reloc_count != 0)
10817 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10818 stub_entry->h->elf.root.root.string);
10819 bfd_set_error (bfd_error_bad_value);
10820 return (bfd_vma) -1;
10822 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10823 return (bfd_vma) -1;
10824 r2off = bfd_get_64 (opd->owner, buf);
10825 r2off -= elf_gp (info->output_bfd);
10827 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10832 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10834 struct ppc_stub_hash_entry *stub_entry;
10835 struct ppc_branch_hash_entry *br_entry;
10836 struct bfd_link_info *info;
10837 struct ppc_link_hash_table *htab;
10842 Elf_Internal_Rela *r;
10845 /* Massage our args to the form they really have. */
10846 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10849 htab = ppc_hash_table (info);
10853 /* Make a note of the offset within the stubs for this entry. */
10854 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10855 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10857 htab->stub_count[stub_entry->stub_type - 1] += 1;
10858 switch (stub_entry->stub_type)
10860 case ppc_stub_long_branch:
10861 case ppc_stub_long_branch_r2off:
10862 /* Branches are relative. This is where we are going to. */
10863 dest = (stub_entry->target_value
10864 + stub_entry->target_section->output_offset
10865 + stub_entry->target_section->output_section->vma);
10866 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10869 /* And this is where we are coming from. */
10870 off -= (stub_entry->stub_offset
10871 + stub_entry->group->stub_sec->output_offset
10872 + stub_entry->group->stub_sec->output_section->vma);
10875 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10877 bfd_vma r2off = get_r2off (info, stub_entry);
10879 if (r2off == (bfd_vma) -1)
10881 htab->stub_error = TRUE;
10884 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10887 if (PPC_HA (r2off) != 0)
10889 bfd_put_32 (htab->params->stub_bfd,
10890 ADDIS_R2_R2 | PPC_HA (r2off), loc);
10894 if (PPC_LO (r2off) != 0)
10896 bfd_put_32 (htab->params->stub_bfd,
10897 ADDI_R2_R2 | PPC_LO (r2off), loc);
10903 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
10905 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10907 info->callbacks->einfo
10908 (_("%P: long branch stub `%s' offset overflow\n"),
10909 stub_entry->root.string);
10910 htab->stub_error = TRUE;
10914 if (info->emitrelocations)
10916 r = get_relocs (stub_entry->group->stub_sec, 1);
10919 r->r_offset = loc - stub_entry->group->stub_sec->contents;
10920 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10921 r->r_addend = dest;
10922 if (stub_entry->h != NULL)
10924 struct elf_link_hash_entry **hashes;
10925 unsigned long symndx;
10926 struct ppc_link_hash_entry *h;
10928 hashes = elf_sym_hashes (htab->params->stub_bfd);
10929 if (hashes == NULL)
10931 bfd_size_type hsize;
10933 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10934 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10935 if (hashes == NULL)
10937 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10938 htab->stub_globals = 1;
10940 symndx = htab->stub_globals++;
10942 hashes[symndx] = &h->elf;
10943 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10944 if (h->oh != NULL && h->oh->is_func)
10945 h = ppc_follow_link (h->oh);
10946 if (h->elf.root.u.def.section != stub_entry->target_section)
10947 /* H is an opd symbol. The addend must be zero. */
10951 off = (h->elf.root.u.def.value
10952 + h->elf.root.u.def.section->output_offset
10953 + h->elf.root.u.def.section->output_section->vma);
10954 r->r_addend -= off;
10960 case ppc_stub_plt_branch:
10961 case ppc_stub_plt_branch_r2off:
10962 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10963 stub_entry->root.string + 9,
10965 if (br_entry == NULL)
10967 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10968 stub_entry->root.string);
10969 htab->stub_error = TRUE;
10973 dest = (stub_entry->target_value
10974 + stub_entry->target_section->output_offset
10975 + stub_entry->target_section->output_section->vma);
10976 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10977 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10979 bfd_put_64 (htab->brlt->owner, dest,
10980 htab->brlt->contents + br_entry->offset);
10982 if (br_entry->iter == htab->stub_iteration)
10984 br_entry->iter = 0;
10986 if (htab->relbrlt != NULL)
10988 /* Create a reloc for the branch lookup table entry. */
10989 Elf_Internal_Rela rela;
10992 rela.r_offset = (br_entry->offset
10993 + htab->brlt->output_offset
10994 + htab->brlt->output_section->vma);
10995 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10996 rela.r_addend = dest;
10998 rl = htab->relbrlt->contents;
10999 rl += (htab->relbrlt->reloc_count++
11000 * sizeof (Elf64_External_Rela));
11001 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11003 else if (info->emitrelocations)
11005 r = get_relocs (htab->brlt, 1);
11008 /* brlt, being SEC_LINKER_CREATED does not go through the
11009 normal reloc processing. Symbols and offsets are not
11010 translated from input file to output file form, so
11011 set up the offset per the output file. */
11012 r->r_offset = (br_entry->offset
11013 + htab->brlt->output_offset
11014 + htab->brlt->output_section->vma);
11015 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11016 r->r_addend = dest;
11020 dest = (br_entry->offset
11021 + htab->brlt->output_offset
11022 + htab->brlt->output_section->vma);
11025 - elf_gp (htab->brlt->output_section->owner)
11026 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11028 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11030 info->callbacks->einfo
11031 (_("%P: linkage table error against `%T'\n"),
11032 stub_entry->root.string);
11033 bfd_set_error (bfd_error_bad_value);
11034 htab->stub_error = TRUE;
11038 if (info->emitrelocations)
11040 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11043 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11044 if (bfd_big_endian (info->output_bfd))
11045 r[0].r_offset += 2;
11046 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11047 r[0].r_offset += 4;
11048 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11049 r[0].r_addend = dest;
11050 if (PPC_HA (off) != 0)
11052 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11053 r[1].r_offset = r[0].r_offset + 4;
11054 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11055 r[1].r_addend = r[0].r_addend;
11059 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11061 if (PPC_HA (off) != 0)
11064 bfd_put_32 (htab->params->stub_bfd,
11065 ADDIS_R12_R2 | PPC_HA (off), loc);
11067 bfd_put_32 (htab->params->stub_bfd,
11068 LD_R12_0R12 | PPC_LO (off), loc);
11073 bfd_put_32 (htab->params->stub_bfd,
11074 LD_R12_0R2 | PPC_LO (off), loc);
11079 bfd_vma r2off = get_r2off (info, stub_entry);
11081 if (r2off == (bfd_vma) -1)
11083 htab->stub_error = TRUE;
11087 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
11090 if (PPC_HA (off) != 0)
11093 bfd_put_32 (htab->params->stub_bfd,
11094 ADDIS_R12_R2 | PPC_HA (off), loc);
11096 bfd_put_32 (htab->params->stub_bfd,
11097 LD_R12_0R12 | PPC_LO (off), loc);
11100 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
11102 if (PPC_HA (r2off) != 0)
11106 bfd_put_32 (htab->params->stub_bfd,
11107 ADDIS_R2_R2 | PPC_HA (r2off), loc);
11109 if (PPC_LO (r2off) != 0)
11113 bfd_put_32 (htab->params->stub_bfd,
11114 ADDI_R2_R2 | PPC_LO (r2off), loc);
11118 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
11120 bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
11123 case ppc_stub_plt_call:
11124 case ppc_stub_plt_call_r2save:
11125 if (stub_entry->h != NULL
11126 && stub_entry->h->is_func_descriptor
11127 && stub_entry->h->oh != NULL)
11129 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11131 /* If the old-ABI "dot-symbol" is undefined make it weak so
11132 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11133 if (fh->elf.root.type == bfd_link_hash_undefined
11134 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11135 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11136 fh->elf.root.type = bfd_link_hash_undefweak;
11139 /* Now build the stub. */
11140 dest = stub_entry->plt_ent->plt.offset & ~1;
11141 if (dest >= (bfd_vma) -2)
11144 plt = htab->elf.splt;
11145 if (!htab->elf.dynamic_sections_created
11146 || stub_entry->h == NULL
11147 || stub_entry->h->elf.dynindx == -1)
11148 plt = htab->elf.iplt;
11150 dest += plt->output_offset + plt->output_section->vma;
11152 if (stub_entry->h == NULL
11153 && (stub_entry->plt_ent->plt.offset & 1) == 0)
11155 Elf_Internal_Rela rela;
11158 rela.r_offset = dest;
11160 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11162 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
11163 rela.r_addend = (stub_entry->target_value
11164 + stub_entry->target_section->output_offset
11165 + stub_entry->target_section->output_section->vma);
11167 rl = (htab->elf.irelplt->contents
11168 + (htab->elf.irelplt->reloc_count++
11169 * sizeof (Elf64_External_Rela)));
11170 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
11171 stub_entry->plt_ent->plt.offset |= 1;
11172 htab->local_ifunc_resolver = 1;
11176 - elf_gp (plt->output_section->owner)
11177 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11179 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11181 info->callbacks->einfo
11182 /* xgettext:c-format */
11183 (_("%P: linkage table error against `%T'\n"),
11184 stub_entry->h != NULL
11185 ? stub_entry->h->elf.root.root.string
11187 bfd_set_error (bfd_error_bad_value);
11188 htab->stub_error = TRUE;
11192 if (htab->params->plt_stub_align != 0)
11194 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11196 stub_entry->group->stub_sec->size += pad;
11197 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11202 if (info->emitrelocations)
11204 r = get_relocs (stub_entry->group->stub_sec,
11205 ((PPC_HA (off) != 0)
11207 ? 2 + (htab->params->plt_static_chain
11208 && PPC_HA (off + 16) == PPC_HA (off))
11212 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11213 if (bfd_big_endian (info->output_bfd))
11214 r[0].r_offset += 2;
11215 r[0].r_addend = dest;
11217 if (stub_entry->h != NULL
11218 && (stub_entry->h == htab->tls_get_addr_fd
11219 || stub_entry->h == htab->tls_get_addr)
11220 && htab->params->tls_get_addr_opt)
11221 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11223 p = build_plt_stub (htab, stub_entry, loc, off, r);
11227 case ppc_stub_save_res:
11235 stub_entry->group->stub_sec->size += size;
11237 if (htab->params->emit_stub_syms)
11239 struct elf_link_hash_entry *h;
11242 const char *const stub_str[] = { "long_branch",
11243 "long_branch_r2off",
11245 "plt_branch_r2off",
11249 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11250 len2 = strlen (stub_entry->root.string);
11251 name = bfd_malloc (len1 + len2 + 2);
11254 memcpy (name, stub_entry->root.string, 9);
11255 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11256 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11257 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11260 if (h->root.type == bfd_link_hash_new)
11262 h->root.type = bfd_link_hash_defined;
11263 h->root.u.def.section = stub_entry->group->stub_sec;
11264 h->root.u.def.value = stub_entry->stub_offset;
11265 h->ref_regular = 1;
11266 h->def_regular = 1;
11267 h->ref_regular_nonweak = 1;
11268 h->forced_local = 1;
11270 h->root.linker_def = 1;
11277 /* As above, but don't actually build the stub. Just bump offset so
11278 we know stub section sizes, and select plt_branch stubs where
11279 long_branch stubs won't do. */
11282 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11284 struct ppc_stub_hash_entry *stub_entry;
11285 struct bfd_link_info *info;
11286 struct ppc_link_hash_table *htab;
11290 /* Massage our args to the form they really have. */
11291 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11294 htab = ppc_hash_table (info);
11298 if (stub_entry->h != NULL
11299 && stub_entry->h->save_res
11300 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11301 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11303 /* Don't make stubs to out-of-line register save/restore
11304 functions. Instead, emit copies of the functions. */
11305 stub_entry->group->needs_save_res = 1;
11306 stub_entry->stub_type = ppc_stub_save_res;
11310 if (stub_entry->stub_type == ppc_stub_plt_call
11311 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11314 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11315 if (off >= (bfd_vma) -2)
11317 plt = htab->elf.splt;
11318 if (!htab->elf.dynamic_sections_created
11319 || stub_entry->h == NULL
11320 || stub_entry->h->elf.dynindx == -1)
11321 plt = htab->elf.iplt;
11322 off += (plt->output_offset
11323 + plt->output_section->vma
11324 - elf_gp (plt->output_section->owner)
11325 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11327 size = plt_stub_size (htab, stub_entry, off);
11328 if (htab->params->plt_stub_align)
11329 size += plt_stub_pad (htab, stub_entry, off);
11330 if (info->emitrelocations)
11332 stub_entry->group->stub_sec->reloc_count
11333 += ((PPC_HA (off) != 0)
11335 ? 2 + (htab->params->plt_static_chain
11336 && PPC_HA (off + 16) == PPC_HA (off))
11338 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11343 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11346 bfd_vma local_off = 0;
11348 off = (stub_entry->target_value
11349 + stub_entry->target_section->output_offset
11350 + stub_entry->target_section->output_section->vma);
11351 off -= (stub_entry->group->stub_sec->size
11352 + stub_entry->group->stub_sec->output_offset
11353 + stub_entry->group->stub_sec->output_section->vma);
11355 /* Reset the stub type from the plt variant in case we now
11356 can reach with a shorter stub. */
11357 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11358 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11361 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11363 r2off = get_r2off (info, stub_entry);
11364 if (r2off == (bfd_vma) -1)
11366 htab->stub_error = TRUE;
11370 if (PPC_HA (r2off) != 0)
11372 if (PPC_LO (r2off) != 0)
11377 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11379 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11380 Do the same for -R objects without function descriptors. */
11381 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11382 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11384 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11386 struct ppc_branch_hash_entry *br_entry;
11388 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11389 stub_entry->root.string + 9,
11391 if (br_entry == NULL)
11393 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
11394 stub_entry->root.string);
11395 htab->stub_error = TRUE;
11399 if (br_entry->iter != htab->stub_iteration)
11401 br_entry->iter = htab->stub_iteration;
11402 br_entry->offset = htab->brlt->size;
11403 htab->brlt->size += 8;
11405 if (htab->relbrlt != NULL)
11406 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11407 else if (info->emitrelocations)
11409 htab->brlt->reloc_count += 1;
11410 htab->brlt->flags |= SEC_RELOC;
11414 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11415 off = (br_entry->offset
11416 + htab->brlt->output_offset
11417 + htab->brlt->output_section->vma
11418 - elf_gp (htab->brlt->output_section->owner)
11419 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11421 if (info->emitrelocations)
11423 stub_entry->group->stub_sec->reloc_count
11424 += 1 + (PPC_HA (off) != 0);
11425 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11428 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11431 if (PPC_HA (off) != 0)
11437 if (PPC_HA (off) != 0)
11440 if (PPC_HA (r2off) != 0)
11442 if (PPC_LO (r2off) != 0)
11446 else if (info->emitrelocations)
11448 stub_entry->group->stub_sec->reloc_count += 1;
11449 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11453 stub_entry->group->stub_sec->size += size;
11457 /* Set up various things so that we can make a list of input sections
11458 for each output section included in the link. Returns -1 on error,
11459 0 when no stubs will be needed, and 1 on success. */
11462 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11466 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11471 htab->sec_info_arr_size = bfd_get_next_section_id ();
11472 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11473 htab->sec_info = bfd_zmalloc (amt);
11474 if (htab->sec_info == NULL)
11477 /* Set toc_off for com, und, abs and ind sections. */
11478 for (id = 0; id < 3; id++)
11479 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11484 /* Set up for first pass at multitoc partitioning. */
11487 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11489 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11491 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11492 htab->toc_bfd = NULL;
11493 htab->toc_first_sec = NULL;
11496 /* The linker repeatedly calls this function for each TOC input section
11497 and linker generated GOT section. Group input bfds such that the toc
11498 within a group is less than 64k in size. */
11501 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11503 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11504 bfd_vma addr, off, limit;
11509 if (!htab->second_toc_pass)
11511 /* Keep track of the first .toc or .got section for this input bfd. */
11512 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11516 htab->toc_bfd = isec->owner;
11517 htab->toc_first_sec = isec;
11520 addr = isec->output_offset + isec->output_section->vma;
11521 off = addr - htab->toc_curr;
11522 limit = 0x80008000;
11523 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11525 if (off + isec->size > limit)
11527 addr = (htab->toc_first_sec->output_offset
11528 + htab->toc_first_sec->output_section->vma);
11529 htab->toc_curr = addr;
11530 htab->toc_curr &= -TOC_BASE_ALIGN;
11533 /* toc_curr is the base address of this toc group. Set elf_gp
11534 for the input section to be the offset relative to the
11535 output toc base plus 0x8000. Making the input elf_gp an
11536 offset allows us to move the toc as a whole without
11537 recalculating input elf_gp. */
11538 off = htab->toc_curr - elf_gp (isec->output_section->owner);
11539 off += TOC_BASE_OFF;
11541 /* Die if someone uses a linker script that doesn't keep input
11542 file .toc and .got together. */
11544 && elf_gp (isec->owner) != 0
11545 && elf_gp (isec->owner) != off)
11548 elf_gp (isec->owner) = off;
11552 /* During the second pass toc_first_sec points to the start of
11553 a toc group, and toc_curr is used to track the old elf_gp.
11554 We use toc_bfd to ensure we only look at each bfd once. */
11555 if (htab->toc_bfd == isec->owner)
11557 htab->toc_bfd = isec->owner;
11559 if (htab->toc_first_sec == NULL
11560 || htab->toc_curr != elf_gp (isec->owner))
11562 htab->toc_curr = elf_gp (isec->owner);
11563 htab->toc_first_sec = isec;
11565 addr = (htab->toc_first_sec->output_offset
11566 + htab->toc_first_sec->output_section->vma);
11567 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11568 elf_gp (isec->owner) = off;
11573 /* Called via elf_link_hash_traverse to merge GOT entries for global
11577 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11579 if (h->root.type == bfd_link_hash_indirect)
11582 merge_got_entries (&h->got.glist);
11587 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11591 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11593 struct got_entry *gent;
11595 if (h->root.type == bfd_link_hash_indirect)
11598 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11599 if (!gent->is_indirect)
11600 allocate_got (h, (struct bfd_link_info *) inf, gent);
11604 /* Called on the first multitoc pass after the last call to
11605 ppc64_elf_next_toc_section. This function removes duplicate GOT
11609 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11611 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11612 struct bfd *ibfd, *ibfd2;
11613 bfd_boolean done_something;
11615 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11617 if (!htab->do_multi_toc)
11620 /* Merge global sym got entries within a toc group. */
11621 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11623 /* And tlsld_got. */
11624 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11626 struct got_entry *ent, *ent2;
11628 if (!is_ppc64_elf (ibfd))
11631 ent = ppc64_tlsld_got (ibfd);
11632 if (!ent->is_indirect
11633 && ent->got.offset != (bfd_vma) -1)
11635 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11637 if (!is_ppc64_elf (ibfd2))
11640 ent2 = ppc64_tlsld_got (ibfd2);
11641 if (!ent2->is_indirect
11642 && ent2->got.offset != (bfd_vma) -1
11643 && elf_gp (ibfd2) == elf_gp (ibfd))
11645 ent2->is_indirect = TRUE;
11646 ent2->got.ent = ent;
11652 /* Zap sizes of got sections. */
11653 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11654 htab->elf.irelplt->size -= htab->got_reli_size;
11655 htab->got_reli_size = 0;
11657 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11659 asection *got, *relgot;
11661 if (!is_ppc64_elf (ibfd))
11664 got = ppc64_elf_tdata (ibfd)->got;
11667 got->rawsize = got->size;
11669 relgot = ppc64_elf_tdata (ibfd)->relgot;
11670 relgot->rawsize = relgot->size;
11675 /* Now reallocate the got, local syms first. We don't need to
11676 allocate section contents again since we never increase size. */
11677 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11679 struct got_entry **lgot_ents;
11680 struct got_entry **end_lgot_ents;
11681 struct plt_entry **local_plt;
11682 struct plt_entry **end_local_plt;
11683 unsigned char *lgot_masks;
11684 bfd_size_type locsymcount;
11685 Elf_Internal_Shdr *symtab_hdr;
11688 if (!is_ppc64_elf (ibfd))
11691 lgot_ents = elf_local_got_ents (ibfd);
11695 symtab_hdr = &elf_symtab_hdr (ibfd);
11696 locsymcount = symtab_hdr->sh_info;
11697 end_lgot_ents = lgot_ents + locsymcount;
11698 local_plt = (struct plt_entry **) end_lgot_ents;
11699 end_local_plt = local_plt + locsymcount;
11700 lgot_masks = (unsigned char *) end_local_plt;
11701 s = ppc64_elf_tdata (ibfd)->got;
11702 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11704 struct got_entry *ent;
11706 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11708 unsigned int ent_size = 8;
11709 unsigned int rel_size = sizeof (Elf64_External_Rela);
11711 ent->got.offset = s->size;
11712 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11717 s->size += ent_size;
11718 if ((*lgot_masks & PLT_IFUNC) != 0)
11720 htab->elf.irelplt->size += rel_size;
11721 htab->got_reli_size += rel_size;
11723 else if (bfd_link_pic (info))
11725 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11726 srel->size += rel_size;
11732 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11734 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11736 struct got_entry *ent;
11738 if (!is_ppc64_elf (ibfd))
11741 ent = ppc64_tlsld_got (ibfd);
11742 if (!ent->is_indirect
11743 && ent->got.offset != (bfd_vma) -1)
11745 asection *s = ppc64_elf_tdata (ibfd)->got;
11746 ent->got.offset = s->size;
11748 if (bfd_link_pic (info))
11750 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11751 srel->size += sizeof (Elf64_External_Rela);
11756 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11757 if (!done_something)
11758 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11762 if (!is_ppc64_elf (ibfd))
11765 got = ppc64_elf_tdata (ibfd)->got;
11768 done_something = got->rawsize != got->size;
11769 if (done_something)
11774 if (done_something)
11775 (*htab->params->layout_sections_again) ();
11777 /* Set up for second pass over toc sections to recalculate elf_gp
11778 on input sections. */
11779 htab->toc_bfd = NULL;
11780 htab->toc_first_sec = NULL;
11781 htab->second_toc_pass = TRUE;
11782 return done_something;
11785 /* Called after second pass of multitoc partitioning. */
11788 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11790 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11792 /* After the second pass, toc_curr tracks the TOC offset used
11793 for code sections below in ppc64_elf_next_input_section. */
11794 htab->toc_curr = TOC_BASE_OFF;
11797 /* No toc references were found in ISEC. If the code in ISEC makes no
11798 calls, then there's no need to use toc adjusting stubs when branching
11799 into ISEC. Actually, indirect calls from ISEC are OK as they will
11800 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11801 needed, and 2 if a cyclical call-graph was found but no other reason
11802 for a stub was detected. If called from the top level, a return of
11803 2 means the same as a return of 0. */
11806 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11810 /* Mark this section as checked. */
11811 isec->call_check_done = 1;
11813 /* We know none of our code bearing sections will need toc stubs. */
11814 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11817 if (isec->size == 0)
11820 if (isec->output_section == NULL)
11824 if (isec->reloc_count != 0)
11826 Elf_Internal_Rela *relstart, *rel;
11827 Elf_Internal_Sym *local_syms;
11828 struct ppc_link_hash_table *htab;
11830 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11831 info->keep_memory);
11832 if (relstart == NULL)
11835 /* Look for branches to outside of this section. */
11837 htab = ppc_hash_table (info);
11841 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11843 enum elf_ppc64_reloc_type r_type;
11844 unsigned long r_symndx;
11845 struct elf_link_hash_entry *h;
11846 struct ppc_link_hash_entry *eh;
11847 Elf_Internal_Sym *sym;
11849 struct _opd_sec_data *opd;
11853 r_type = ELF64_R_TYPE (rel->r_info);
11854 if (r_type != R_PPC64_REL24
11855 && r_type != R_PPC64_REL14
11856 && r_type != R_PPC64_REL14_BRTAKEN
11857 && r_type != R_PPC64_REL14_BRNTAKEN)
11860 r_symndx = ELF64_R_SYM (rel->r_info);
11861 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11868 /* Calls to dynamic lib functions go through a plt call stub
11870 eh = (struct ppc_link_hash_entry *) h;
11872 && (eh->elf.plt.plist != NULL
11874 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11880 if (sym_sec == NULL)
11881 /* Ignore other undefined symbols. */
11884 /* Assume branches to other sections not included in the
11885 link need stubs too, to cover -R and absolute syms. */
11886 if (sym_sec->output_section == NULL)
11893 sym_value = sym->st_value;
11896 if (h->root.type != bfd_link_hash_defined
11897 && h->root.type != bfd_link_hash_defweak)
11899 sym_value = h->root.u.def.value;
11901 sym_value += rel->r_addend;
11903 /* If this branch reloc uses an opd sym, find the code section. */
11904 opd = get_opd_info (sym_sec);
11907 if (h == NULL && opd->adjust != NULL)
11911 adjust = opd->adjust[OPD_NDX (sym_value)];
11913 /* Assume deleted functions won't ever be called. */
11915 sym_value += adjust;
11918 dest = opd_entry_value (sym_sec, sym_value,
11919 &sym_sec, NULL, FALSE);
11920 if (dest == (bfd_vma) -1)
11925 + sym_sec->output_offset
11926 + sym_sec->output_section->vma);
11928 /* Ignore branch to self. */
11929 if (sym_sec == isec)
11932 /* If the called function uses the toc, we need a stub. */
11933 if (sym_sec->has_toc_reloc
11934 || sym_sec->makes_toc_func_call)
11940 /* Assume any branch that needs a long branch stub might in fact
11941 need a plt_branch stub. A plt_branch stub uses r2. */
11942 else if (dest - (isec->output_offset
11943 + isec->output_section->vma
11944 + rel->r_offset) + (1 << 25)
11945 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11953 /* If calling back to a section in the process of being
11954 tested, we can't say for sure that no toc adjusting stubs
11955 are needed, so don't return zero. */
11956 else if (sym_sec->call_check_in_progress)
11959 /* Branches to another section that itself doesn't have any TOC
11960 references are OK. Recursively call ourselves to check. */
11961 else if (!sym_sec->call_check_done)
11965 /* Mark current section as indeterminate, so that other
11966 sections that call back to current won't be marked as
11968 isec->call_check_in_progress = 1;
11969 recur = toc_adjusting_stub_needed (info, sym_sec);
11970 isec->call_check_in_progress = 0;
11981 if (local_syms != NULL
11982 && (elf_symtab_hdr (isec->owner).contents
11983 != (unsigned char *) local_syms))
11985 if (elf_section_data (isec)->relocs != relstart)
11990 && isec->map_head.s != NULL
11991 && (strcmp (isec->output_section->name, ".init") == 0
11992 || strcmp (isec->output_section->name, ".fini") == 0))
11994 if (isec->map_head.s->has_toc_reloc
11995 || isec->map_head.s->makes_toc_func_call)
11997 else if (!isec->map_head.s->call_check_done)
12000 isec->call_check_in_progress = 1;
12001 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12002 isec->call_check_in_progress = 0;
12009 isec->makes_toc_func_call = 1;
12014 /* The linker repeatedly calls this function for each input section,
12015 in the order that input sections are linked into output sections.
12016 Build lists of input sections to determine groupings between which
12017 we may insert linker stubs. */
12020 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12022 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12027 if ((isec->output_section->flags & SEC_CODE) != 0
12028 && isec->output_section->id < htab->sec_info_arr_size)
12030 /* This happens to make the list in reverse order,
12031 which is what we want. */
12032 htab->sec_info[isec->id].u.list
12033 = htab->sec_info[isec->output_section->id].u.list;
12034 htab->sec_info[isec->output_section->id].u.list = isec;
12037 if (htab->multi_toc_needed)
12039 /* Analyse sections that aren't already flagged as needing a
12040 valid toc pointer. Exclude .fixup for the linux kernel.
12041 .fixup contains branches, but only back to the function that
12042 hit an exception. */
12043 if (!(isec->has_toc_reloc
12044 || (isec->flags & SEC_CODE) == 0
12045 || strcmp (isec->name, ".fixup") == 0
12046 || isec->call_check_done))
12048 if (toc_adjusting_stub_needed (info, isec) < 0)
12051 /* Make all sections use the TOC assigned for this object file.
12052 This will be wrong for pasted sections; We fix that in
12053 check_pasted_section(). */
12054 if (elf_gp (isec->owner) != 0)
12055 htab->toc_curr = elf_gp (isec->owner);
12058 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12062 /* Check that all .init and .fini sections use the same toc, if they
12063 have toc relocs. */
12066 check_pasted_section (struct bfd_link_info *info, const char *name)
12068 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12072 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12073 bfd_vma toc_off = 0;
12076 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12077 if (i->has_toc_reloc)
12080 toc_off = htab->sec_info[i->id].toc_off;
12081 else if (toc_off != htab->sec_info[i->id].toc_off)
12086 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12087 if (i->makes_toc_func_call)
12089 toc_off = htab->sec_info[i->id].toc_off;
12093 /* Make sure the whole pasted function uses the same toc offset. */
12095 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12096 htab->sec_info[i->id].toc_off = toc_off;
12102 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12104 return (check_pasted_section (info, ".init")
12105 & check_pasted_section (info, ".fini"));
12108 /* See whether we can group stub sections together. Grouping stub
12109 sections may result in fewer stubs. More importantly, we need to
12110 put all .init* and .fini* stubs at the beginning of the .init or
12111 .fini output sections respectively, because glibc splits the
12112 _init and _fini functions into multiple parts. Putting a stub in
12113 the middle of a function is not a good idea. */
12116 group_sections (struct bfd_link_info *info,
12117 bfd_size_type stub_group_size,
12118 bfd_boolean stubs_always_before_branch)
12120 struct ppc_link_hash_table *htab;
12122 bfd_boolean suppress_size_errors;
12124 htab = ppc_hash_table (info);
12128 suppress_size_errors = FALSE;
12129 if (stub_group_size == 1)
12131 /* Default values. */
12132 if (stubs_always_before_branch)
12133 stub_group_size = 0x1e00000;
12135 stub_group_size = 0x1c00000;
12136 suppress_size_errors = TRUE;
12139 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12143 if (osec->id >= htab->sec_info_arr_size)
12146 tail = htab->sec_info[osec->id].u.list;
12147 while (tail != NULL)
12151 bfd_size_type total;
12152 bfd_boolean big_sec;
12154 struct map_stub *group;
12155 bfd_size_type group_size;
12158 total = tail->size;
12159 group_size = (ppc64_elf_section_data (tail) != NULL
12160 && ppc64_elf_section_data (tail)->has_14bit_branch
12161 ? stub_group_size >> 10 : stub_group_size);
12163 big_sec = total > group_size;
12164 if (big_sec && !suppress_size_errors)
12165 /* xgettext:c-format */
12166 _bfd_error_handler (_("%B section %A exceeds stub group size"),
12167 tail->owner, tail);
12168 curr_toc = htab->sec_info[tail->id].toc_off;
12170 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12171 && ((total += curr->output_offset - prev->output_offset)
12172 < (ppc64_elf_section_data (prev) != NULL
12173 && ppc64_elf_section_data (prev)->has_14bit_branch
12174 ? (group_size = stub_group_size >> 10) : group_size))
12175 && htab->sec_info[prev->id].toc_off == curr_toc)
12178 /* OK, the size from the start of CURR to the end is less
12179 than group_size and thus can be handled by one stub
12180 section. (or the tail section is itself larger than
12181 group_size, in which case we may be toast.) We should
12182 really be keeping track of the total size of stubs added
12183 here, as stubs contribute to the final output section
12184 size. That's a little tricky, and this way will only
12185 break if stubs added make the total size more than 2^25,
12186 ie. for the default stub_group_size, if stubs total more
12187 than 2097152 bytes, or nearly 75000 plt call stubs. */
12188 group = bfd_alloc (curr->owner, sizeof (*group));
12191 group->link_sec = curr;
12192 group->stub_sec = NULL;
12193 group->needs_save_res = 0;
12194 group->next = htab->group;
12195 htab->group = group;
12198 prev = htab->sec_info[tail->id].u.list;
12199 /* Set up this stub group. */
12200 htab->sec_info[tail->id].u.group = group;
12202 while (tail != curr && (tail = prev) != NULL);
12204 /* But wait, there's more! Input sections up to group_size
12205 bytes before the stub section can be handled by it too.
12206 Don't do this if we have a really large section after the
12207 stubs, as adding more stubs increases the chance that
12208 branches may not reach into the stub section. */
12209 if (!stubs_always_before_branch && !big_sec)
12212 while (prev != NULL
12213 && ((total += tail->output_offset - prev->output_offset)
12214 < (ppc64_elf_section_data (prev) != NULL
12215 && ppc64_elf_section_data (prev)->has_14bit_branch
12216 ? (group_size = stub_group_size >> 10) : group_size))
12217 && htab->sec_info[prev->id].toc_off == curr_toc)
12220 prev = htab->sec_info[tail->id].u.list;
12221 htab->sec_info[tail->id].u.group = group;
12230 static const unsigned char glink_eh_frame_cie[] =
12232 0, 0, 0, 16, /* length. */
12233 0, 0, 0, 0, /* id. */
12234 1, /* CIE version. */
12235 'z', 'R', 0, /* Augmentation string. */
12236 4, /* Code alignment. */
12237 0x78, /* Data alignment. */
12239 1, /* Augmentation size. */
12240 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12241 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
12244 /* Stripping output sections is normally done before dynamic section
12245 symbols have been allocated. This function is called later, and
12246 handles cases like htab->brlt which is mapped to its own output
12250 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12252 if (isec->size == 0
12253 && isec->output_section->size == 0
12254 && !(isec->output_section->flags & SEC_KEEP)
12255 && !bfd_section_removed_from_list (info->output_bfd,
12256 isec->output_section)
12257 && elf_section_data (isec->output_section)->dynindx == 0)
12259 isec->output_section->flags |= SEC_EXCLUDE;
12260 bfd_section_list_remove (info->output_bfd, isec->output_section);
12261 info->output_bfd->section_count--;
12265 /* Determine and set the size of the stub section for a final link.
12267 The basic idea here is to examine all the relocations looking for
12268 PC-relative calls to a target that is unreachable with a "bl"
12272 ppc64_elf_size_stubs (struct bfd_link_info *info)
12274 bfd_size_type stub_group_size;
12275 bfd_boolean stubs_always_before_branch;
12276 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12281 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12282 htab->params->plt_thread_safe = 1;
12283 if (!htab->opd_abi)
12284 htab->params->plt_thread_safe = 0;
12285 else if (htab->params->plt_thread_safe == -1)
12287 static const char *const thread_starter[] =
12291 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12293 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12294 "mq_notify", "create_timer",
12299 "GOMP_parallel_start",
12300 "GOMP_parallel_loop_static",
12301 "GOMP_parallel_loop_static_start",
12302 "GOMP_parallel_loop_dynamic",
12303 "GOMP_parallel_loop_dynamic_start",
12304 "GOMP_parallel_loop_guided",
12305 "GOMP_parallel_loop_guided_start",
12306 "GOMP_parallel_loop_runtime",
12307 "GOMP_parallel_loop_runtime_start",
12308 "GOMP_parallel_sections",
12309 "GOMP_parallel_sections_start",
12315 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12317 struct elf_link_hash_entry *h;
12318 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12319 FALSE, FALSE, TRUE);
12320 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12321 if (htab->params->plt_thread_safe)
12325 stubs_always_before_branch = htab->params->group_size < 0;
12326 if (htab->params->group_size < 0)
12327 stub_group_size = -htab->params->group_size;
12329 stub_group_size = htab->params->group_size;
12331 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12334 #define STUB_SHRINK_ITER 20
12335 /* Loop until no stubs added. After iteration 20 of this loop we may
12336 exit on a stub section shrinking. This is to break out of a
12337 pathological case where adding stubs on one iteration decreases
12338 section gaps (perhaps due to alignment), which then requires
12339 fewer or smaller stubs on the next iteration. */
12344 unsigned int bfd_indx;
12345 struct map_stub *group;
12346 asection *stub_sec;
12348 htab->stub_iteration += 1;
12350 for (input_bfd = info->input_bfds, bfd_indx = 0;
12352 input_bfd = input_bfd->link.next, bfd_indx++)
12354 Elf_Internal_Shdr *symtab_hdr;
12356 Elf_Internal_Sym *local_syms = NULL;
12358 if (!is_ppc64_elf (input_bfd))
12361 /* We'll need the symbol table in a second. */
12362 symtab_hdr = &elf_symtab_hdr (input_bfd);
12363 if (symtab_hdr->sh_info == 0)
12366 /* Walk over each section attached to the input bfd. */
12367 for (section = input_bfd->sections;
12369 section = section->next)
12371 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12373 /* If there aren't any relocs, then there's nothing more
12375 if ((section->flags & SEC_RELOC) == 0
12376 || (section->flags & SEC_ALLOC) == 0
12377 || (section->flags & SEC_LOAD) == 0
12378 || (section->flags & SEC_CODE) == 0
12379 || section->reloc_count == 0)
12382 /* If this section is a link-once section that will be
12383 discarded, then don't create any stubs. */
12384 if (section->output_section == NULL
12385 || section->output_section->owner != info->output_bfd)
12388 /* Get the relocs. */
12390 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12391 info->keep_memory);
12392 if (internal_relocs == NULL)
12393 goto error_ret_free_local;
12395 /* Now examine each relocation. */
12396 irela = internal_relocs;
12397 irelaend = irela + section->reloc_count;
12398 for (; irela < irelaend; irela++)
12400 enum elf_ppc64_reloc_type r_type;
12401 unsigned int r_indx;
12402 enum ppc_stub_type stub_type;
12403 struct ppc_stub_hash_entry *stub_entry;
12404 asection *sym_sec, *code_sec;
12405 bfd_vma sym_value, code_value;
12406 bfd_vma destination;
12407 unsigned long local_off;
12408 bfd_boolean ok_dest;
12409 struct ppc_link_hash_entry *hash;
12410 struct ppc_link_hash_entry *fdh;
12411 struct elf_link_hash_entry *h;
12412 Elf_Internal_Sym *sym;
12414 const asection *id_sec;
12415 struct _opd_sec_data *opd;
12416 struct plt_entry *plt_ent;
12418 r_type = ELF64_R_TYPE (irela->r_info);
12419 r_indx = ELF64_R_SYM (irela->r_info);
12421 if (r_type >= R_PPC64_max)
12423 bfd_set_error (bfd_error_bad_value);
12424 goto error_ret_free_internal;
12427 /* Only look for stubs on branch instructions. */
12428 if (r_type != R_PPC64_REL24
12429 && r_type != R_PPC64_REL14
12430 && r_type != R_PPC64_REL14_BRTAKEN
12431 && r_type != R_PPC64_REL14_BRNTAKEN)
12434 /* Now determine the call target, its name, value,
12436 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12437 r_indx, input_bfd))
12438 goto error_ret_free_internal;
12439 hash = (struct ppc_link_hash_entry *) h;
12446 sym_value = sym->st_value;
12447 if (sym_sec != NULL
12448 && sym_sec->output_section != NULL)
12451 else if (hash->elf.root.type == bfd_link_hash_defined
12452 || hash->elf.root.type == bfd_link_hash_defweak)
12454 sym_value = hash->elf.root.u.def.value;
12455 if (sym_sec->output_section != NULL)
12458 else if (hash->elf.root.type == bfd_link_hash_undefweak
12459 || hash->elf.root.type == bfd_link_hash_undefined)
12461 /* Recognise an old ABI func code entry sym, and
12462 use the func descriptor sym instead if it is
12464 if (hash->elf.root.root.string[0] == '.'
12465 && hash->oh != NULL)
12467 fdh = ppc_follow_link (hash->oh);
12468 if (fdh->elf.root.type == bfd_link_hash_defined
12469 || fdh->elf.root.type == bfd_link_hash_defweak)
12471 sym_sec = fdh->elf.root.u.def.section;
12472 sym_value = fdh->elf.root.u.def.value;
12473 if (sym_sec->output_section != NULL)
12482 bfd_set_error (bfd_error_bad_value);
12483 goto error_ret_free_internal;
12490 sym_value += irela->r_addend;
12491 destination = (sym_value
12492 + sym_sec->output_offset
12493 + sym_sec->output_section->vma);
12494 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12499 code_sec = sym_sec;
12500 code_value = sym_value;
12501 opd = get_opd_info (sym_sec);
12506 if (hash == NULL && opd->adjust != NULL)
12508 long adjust = opd->adjust[OPD_NDX (sym_value)];
12511 code_value += adjust;
12512 sym_value += adjust;
12514 dest = opd_entry_value (sym_sec, sym_value,
12515 &code_sec, &code_value, FALSE);
12516 if (dest != (bfd_vma) -1)
12518 destination = dest;
12521 /* Fixup old ABI sym to point at code
12523 hash->elf.root.type = bfd_link_hash_defweak;
12524 hash->elf.root.u.def.section = code_sec;
12525 hash->elf.root.u.def.value = code_value;
12530 /* Determine what (if any) linker stub is needed. */
12532 stub_type = ppc_type_of_stub (section, irela, &hash,
12533 &plt_ent, destination,
12536 if (stub_type != ppc_stub_plt_call)
12538 /* Check whether we need a TOC adjusting stub.
12539 Since the linker pastes together pieces from
12540 different object files when creating the
12541 _init and _fini functions, it may be that a
12542 call to what looks like a local sym is in
12543 fact a call needing a TOC adjustment. */
12544 if (code_sec != NULL
12545 && code_sec->output_section != NULL
12546 && (htab->sec_info[code_sec->id].toc_off
12547 != htab->sec_info[section->id].toc_off)
12548 && (code_sec->has_toc_reloc
12549 || code_sec->makes_toc_func_call))
12550 stub_type = ppc_stub_long_branch_r2off;
12553 if (stub_type == ppc_stub_none)
12556 /* __tls_get_addr calls might be eliminated. */
12557 if (stub_type != ppc_stub_plt_call
12559 && (hash == htab->tls_get_addr
12560 || hash == htab->tls_get_addr_fd)
12561 && section->has_tls_reloc
12562 && irela != internal_relocs)
12564 /* Get tls info. */
12565 unsigned char *tls_mask;
12567 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12568 irela - 1, input_bfd))
12569 goto error_ret_free_internal;
12570 if (*tls_mask != 0)
12574 if (stub_type == ppc_stub_plt_call
12575 && irela + 1 < irelaend
12576 && irela[1].r_offset == irela->r_offset + 4
12577 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12579 if (!tocsave_find (htab, INSERT,
12580 &local_syms, irela + 1, input_bfd))
12581 goto error_ret_free_internal;
12583 else if (stub_type == ppc_stub_plt_call)
12584 stub_type = ppc_stub_plt_call_r2save;
12586 /* Support for grouping stub sections. */
12587 id_sec = htab->sec_info[section->id].u.group->link_sec;
12589 /* Get the name of this stub. */
12590 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12592 goto error_ret_free_internal;
12594 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12595 stub_name, FALSE, FALSE);
12596 if (stub_entry != NULL)
12598 /* The proper stub has already been created. */
12600 if (stub_type == ppc_stub_plt_call_r2save)
12601 stub_entry->stub_type = stub_type;
12605 stub_entry = ppc_add_stub (stub_name, section, info);
12606 if (stub_entry == NULL)
12609 error_ret_free_internal:
12610 if (elf_section_data (section)->relocs == NULL)
12611 free (internal_relocs);
12612 error_ret_free_local:
12613 if (local_syms != NULL
12614 && (symtab_hdr->contents
12615 != (unsigned char *) local_syms))
12620 stub_entry->stub_type = stub_type;
12621 if (stub_type != ppc_stub_plt_call
12622 && stub_type != ppc_stub_plt_call_r2save)
12624 stub_entry->target_value = code_value;
12625 stub_entry->target_section = code_sec;
12629 stub_entry->target_value = sym_value;
12630 stub_entry->target_section = sym_sec;
12632 stub_entry->h = hash;
12633 stub_entry->plt_ent = plt_ent;
12634 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12636 if (stub_entry->h != NULL)
12637 htab->stub_globals += 1;
12640 /* We're done with the internal relocs, free them. */
12641 if (elf_section_data (section)->relocs != internal_relocs)
12642 free (internal_relocs);
12645 if (local_syms != NULL
12646 && symtab_hdr->contents != (unsigned char *) local_syms)
12648 if (!info->keep_memory)
12651 symtab_hdr->contents = (unsigned char *) local_syms;
12655 /* We may have added some stubs. Find out the new size of the
12657 for (stub_sec = htab->params->stub_bfd->sections;
12659 stub_sec = stub_sec->next)
12660 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12662 if (htab->stub_iteration <= STUB_SHRINK_ITER
12663 || stub_sec->rawsize < stub_sec->size)
12664 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12665 stub_sec->rawsize = stub_sec->size;
12666 stub_sec->size = 0;
12667 stub_sec->reloc_count = 0;
12668 stub_sec->flags &= ~SEC_RELOC;
12671 htab->brlt->size = 0;
12672 htab->brlt->reloc_count = 0;
12673 htab->brlt->flags &= ~SEC_RELOC;
12674 if (htab->relbrlt != NULL)
12675 htab->relbrlt->size = 0;
12677 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12679 for (group = htab->group; group != NULL; group = group->next)
12680 if (group->needs_save_res)
12681 group->stub_sec->size += htab->sfpr->size;
12683 if (info->emitrelocations
12684 && htab->glink != NULL && htab->glink->size != 0)
12686 htab->glink->reloc_count = 1;
12687 htab->glink->flags |= SEC_RELOC;
12690 if (htab->glink_eh_frame != NULL
12691 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12692 && htab->glink_eh_frame->output_section->size != 0)
12694 size_t size = 0, align = 4;
12696 for (stub_sec = htab->params->stub_bfd->sections;
12698 stub_sec = stub_sec->next)
12699 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12700 size += (17 + align - 1) & -align;
12701 if (htab->glink != NULL && htab->glink->size != 0)
12702 size += (24 + align - 1) & -align;
12704 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12705 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12706 size = (size + align - 1) & -align;
12707 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12708 htab->glink_eh_frame->size = size;
12711 if (htab->params->plt_stub_align != 0)
12712 for (stub_sec = htab->params->stub_bfd->sections;
12714 stub_sec = stub_sec->next)
12715 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12716 stub_sec->size = ((stub_sec->size
12717 + (1 << htab->params->plt_stub_align) - 1)
12718 & -(1 << htab->params->plt_stub_align));
12720 for (stub_sec = htab->params->stub_bfd->sections;
12722 stub_sec = stub_sec->next)
12723 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12724 && stub_sec->rawsize != stub_sec->size
12725 && (htab->stub_iteration <= STUB_SHRINK_ITER
12726 || stub_sec->rawsize < stub_sec->size))
12729 if (stub_sec == NULL
12730 && (htab->glink_eh_frame == NULL
12731 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12734 /* Ask the linker to do its stuff. */
12735 (*htab->params->layout_sections_again) ();
12738 if (htab->glink_eh_frame != NULL
12739 && htab->glink_eh_frame->size != 0)
12742 bfd_byte *p, *last_fde;
12743 size_t last_fde_len, size, align, pad;
12744 asection *stub_sec;
12746 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12749 htab->glink_eh_frame->contents = p;
12753 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12754 /* CIE length (rewrite in case little-endian). */
12755 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
12756 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12757 p += last_fde_len + 4;
12759 for (stub_sec = htab->params->stub_bfd->sections;
12761 stub_sec = stub_sec->next)
12762 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12765 last_fde_len = ((17 + align - 1) & -align) - 4;
12767 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12770 val = p - htab->glink_eh_frame->contents;
12771 bfd_put_32 (htab->elf.dynobj, val, p);
12773 /* Offset to stub section, written later. */
12775 /* stub section size. */
12776 bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
12778 /* Augmentation. */
12781 p += ((17 + align - 1) & -align) - 17;
12783 if (htab->glink != NULL && htab->glink->size != 0)
12786 last_fde_len = ((24 + align - 1) & -align) - 4;
12788 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12791 val = p - htab->glink_eh_frame->contents;
12792 bfd_put_32 (htab->elf.dynobj, val, p);
12794 /* Offset to .glink, written later. */
12797 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12799 /* Augmentation. */
12802 *p++ = DW_CFA_advance_loc + 1;
12803 *p++ = DW_CFA_register;
12805 *p++ = htab->opd_abi ? 12 : 0;
12806 *p++ = DW_CFA_advance_loc + 4;
12807 *p++ = DW_CFA_restore_extended;
12809 p += ((24 + align - 1) & -align) - 24;
12811 /* Subsume any padding into the last FDE if user .eh_frame
12812 sections are aligned more than glink_eh_frame. Otherwise any
12813 zero padding will be seen as a terminator. */
12814 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12815 size = p - htab->glink_eh_frame->contents;
12816 pad = ((size + align - 1) & -align) - size;
12817 htab->glink_eh_frame->size = size + pad;
12818 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12821 maybe_strip_output (info, htab->brlt);
12822 if (htab->glink_eh_frame != NULL)
12823 maybe_strip_output (info, htab->glink_eh_frame);
12828 /* Called after we have determined section placement. If sections
12829 move, we'll be called again. Provide a value for TOCstart. */
12832 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12835 bfd_vma TOCstart, adjust;
12839 struct elf_link_hash_entry *h;
12840 struct elf_link_hash_table *htab = elf_hash_table (info);
12842 if (is_elf_hash_table (htab)
12843 && htab->hgot != NULL)
12847 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12848 if (is_elf_hash_table (htab))
12852 && h->root.type == bfd_link_hash_defined
12853 && !h->root.linker_def
12854 && (!is_elf_hash_table (htab)
12855 || h->def_regular))
12857 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12858 + h->root.u.def.section->output_offset
12859 + h->root.u.def.section->output_section->vma);
12860 _bfd_set_gp_value (obfd, TOCstart);
12865 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12866 order. The TOC starts where the first of these sections starts. */
12867 s = bfd_get_section_by_name (obfd, ".got");
12868 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12869 s = bfd_get_section_by_name (obfd, ".toc");
12870 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12871 s = bfd_get_section_by_name (obfd, ".tocbss");
12872 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12873 s = bfd_get_section_by_name (obfd, ".plt");
12874 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12876 /* This may happen for
12877 o references to TOC base (SYM@toc / TOC[tc0]) without a
12879 o bad linker script
12880 o --gc-sections and empty TOC sections
12882 FIXME: Warn user? */
12884 /* Look for a likely section. We probably won't even be
12886 for (s = obfd->sections; s != NULL; s = s->next)
12887 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12889 == (SEC_ALLOC | SEC_SMALL_DATA))
12892 for (s = obfd->sections; s != NULL; s = s->next)
12893 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12894 == (SEC_ALLOC | SEC_SMALL_DATA))
12897 for (s = obfd->sections; s != NULL; s = s->next)
12898 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12902 for (s = obfd->sections; s != NULL; s = s->next)
12903 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12909 TOCstart = s->output_section->vma + s->output_offset;
12911 /* Force alignment. */
12912 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12913 TOCstart -= adjust;
12914 _bfd_set_gp_value (obfd, TOCstart);
12916 if (info != NULL && s != NULL)
12918 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12922 if (htab->elf.hgot != NULL)
12924 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
12925 htab->elf.hgot->root.u.def.section = s;
12930 struct bfd_link_hash_entry *bh = NULL;
12931 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
12932 s, TOC_BASE_OFF - adjust,
12933 NULL, FALSE, FALSE, &bh);
12939 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12940 write out any global entry stubs. */
12943 build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12945 struct bfd_link_info *info;
12946 struct ppc_link_hash_table *htab;
12947 struct plt_entry *pent;
12950 if (h->root.type == bfd_link_hash_indirect)
12953 if (!h->pointer_equality_needed)
12956 if (h->def_regular)
12960 htab = ppc_hash_table (info);
12965 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12966 if (pent->plt.offset != (bfd_vma) -1
12967 && pent->addend == 0)
12973 p = s->contents + h->root.u.def.value;
12974 plt = htab->elf.splt;
12975 if (!htab->elf.dynamic_sections_created
12976 || h->dynindx == -1)
12977 plt = htab->elf.iplt;
12978 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12979 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12981 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12983 info->callbacks->einfo
12984 (_("%P: linkage table error against `%T'\n"),
12985 h->root.root.string);
12986 bfd_set_error (bfd_error_bad_value);
12987 htab->stub_error = TRUE;
12990 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12991 if (htab->params->emit_stub_syms)
12993 size_t len = strlen (h->root.root.string);
12994 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12999 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13000 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13003 if (h->root.type == bfd_link_hash_new)
13005 h->root.type = bfd_link_hash_defined;
13006 h->root.u.def.section = s;
13007 h->root.u.def.value = p - s->contents;
13008 h->ref_regular = 1;
13009 h->def_regular = 1;
13010 h->ref_regular_nonweak = 1;
13011 h->forced_local = 1;
13013 h->root.linker_def = 1;
13017 if (PPC_HA (off) != 0)
13019 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13022 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13024 bfd_put_32 (s->owner, MTCTR_R12, p);
13026 bfd_put_32 (s->owner, BCTR, p);
13032 /* Build all the stubs associated with the current output file.
13033 The stubs are kept in a hash table attached to the main linker
13034 hash table. This function is called via gldelf64ppc_finish. */
13037 ppc64_elf_build_stubs (struct bfd_link_info *info,
13040 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13041 struct map_stub *group;
13042 asection *stub_sec;
13044 int stub_sec_count = 0;
13049 /* Allocate memory to hold the linker stubs. */
13050 for (stub_sec = htab->params->stub_bfd->sections;
13052 stub_sec = stub_sec->next)
13053 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
13054 && stub_sec->size != 0)
13056 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
13057 if (stub_sec->contents == NULL)
13059 stub_sec->size = 0;
13062 if (htab->glink != NULL && htab->glink->size != 0)
13067 /* Build the .glink plt call stub. */
13068 if (htab->params->emit_stub_syms)
13070 struct elf_link_hash_entry *h;
13071 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13072 TRUE, FALSE, FALSE);
13075 if (h->root.type == bfd_link_hash_new)
13077 h->root.type = bfd_link_hash_defined;
13078 h->root.u.def.section = htab->glink;
13079 h->root.u.def.value = 8;
13080 h->ref_regular = 1;
13081 h->def_regular = 1;
13082 h->ref_regular_nonweak = 1;
13083 h->forced_local = 1;
13085 h->root.linker_def = 1;
13088 plt0 = (htab->elf.splt->output_section->vma
13089 + htab->elf.splt->output_offset
13091 if (info->emitrelocations)
13093 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13096 r->r_offset = (htab->glink->output_offset
13097 + htab->glink->output_section->vma);
13098 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13099 r->r_addend = plt0;
13101 p = htab->glink->contents;
13102 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13103 bfd_put_64 (htab->glink->owner, plt0, p);
13107 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13109 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13111 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13113 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13115 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13117 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13119 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13121 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13123 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13125 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13130 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13132 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13134 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13136 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13138 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13140 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13142 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13144 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13146 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13148 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13150 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13152 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13155 bfd_put_32 (htab->glink->owner, BCTR, p);
13157 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
13159 bfd_put_32 (htab->glink->owner, NOP, p);
13163 /* Build the .glink lazy link call stubs. */
13165 while (p < htab->glink->contents + htab->glink->rawsize)
13171 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13176 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13178 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13183 bfd_put_32 (htab->glink->owner,
13184 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13189 /* Build .glink global entry stubs. */
13190 if (htab->glink->size > htab->glink->rawsize)
13191 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
13194 if (htab->brlt != NULL && htab->brlt->size != 0)
13196 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13198 if (htab->brlt->contents == NULL)
13201 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13203 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13204 htab->relbrlt->size);
13205 if (htab->relbrlt->contents == NULL)
13209 /* Build the stubs as directed by the stub hash table. */
13210 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13212 for (group = htab->group; group != NULL; group = group->next)
13213 if (group->needs_save_res)
13215 stub_sec = group->stub_sec;
13216 memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13218 if (htab->params->emit_stub_syms)
13222 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13223 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13226 stub_sec->size += htab->sfpr->size;
13229 if (htab->relbrlt != NULL)
13230 htab->relbrlt->reloc_count = 0;
13232 if (htab->params->plt_stub_align != 0)
13233 for (stub_sec = htab->params->stub_bfd->sections;
13235 stub_sec = stub_sec->next)
13236 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13237 stub_sec->size = ((stub_sec->size
13238 + (1 << htab->params->plt_stub_align) - 1)
13239 & -(1 << htab->params->plt_stub_align));
13241 for (stub_sec = htab->params->stub_bfd->sections;
13243 stub_sec = stub_sec->next)
13244 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13246 stub_sec_count += 1;
13247 if (stub_sec->rawsize != stub_sec->size
13248 && (htab->stub_iteration <= STUB_SHRINK_ITER
13249 || stub_sec->rawsize < stub_sec->size))
13253 /* Note that the glink_eh_frame check here is not only testing that
13254 the generated size matched the calculated size but also that
13255 bfd_elf_discard_info didn't make any changes to the section. */
13256 if (stub_sec != NULL
13257 || (htab->glink_eh_frame != NULL
13258 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
13260 htab->stub_error = TRUE;
13261 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
13264 if (htab->stub_error)
13269 *stats = bfd_malloc (500);
13270 if (*stats == NULL)
13273 sprintf (*stats, _("linker stubs in %u group%s\n"
13275 " toc adjust %lu\n"
13276 " long branch %lu\n"
13277 " long toc adj %lu\n"
13279 " plt call toc %lu\n"
13280 " global entry %lu"),
13282 stub_sec_count == 1 ? "" : "s",
13283 htab->stub_count[ppc_stub_long_branch - 1],
13284 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13285 htab->stub_count[ppc_stub_plt_branch - 1],
13286 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13287 htab->stub_count[ppc_stub_plt_call - 1],
13288 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13289 htab->stub_count[ppc_stub_global_entry - 1]);
13294 /* What to do when ld finds relocations against symbols defined in
13295 discarded sections. */
13297 static unsigned int
13298 ppc64_elf_action_discarded (asection *sec)
13300 if (strcmp (".opd", sec->name) == 0)
13303 if (strcmp (".toc", sec->name) == 0)
13306 if (strcmp (".toc1", sec->name) == 0)
13309 return _bfd_elf_default_action_discarded (sec);
13312 /* The RELOCATE_SECTION function is called by the ELF backend linker
13313 to handle the relocations for a section.
13315 The relocs are always passed as Rela structures; if the section
13316 actually uses Rel structures, the r_addend field will always be
13319 This function is responsible for adjust the section contents as
13320 necessary, and (if using Rela relocs and generating a
13321 relocatable output file) adjusting the reloc addend as
13324 This function does not have to worry about setting the reloc
13325 address or the reloc symbol index.
13327 LOCAL_SYMS is a pointer to the swapped in local symbols.
13329 LOCAL_SECTIONS is an array giving the section in the input file
13330 corresponding to the st_shndx field of each local symbol.
13332 The global hash table entry for the global symbols can be found
13333 via elf_sym_hashes (input_bfd).
13335 When generating relocatable output, this function must handle
13336 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13337 going to be the section symbol corresponding to the output
13338 section, which means that the addend must be adjusted
13342 ppc64_elf_relocate_section (bfd *output_bfd,
13343 struct bfd_link_info *info,
13345 asection *input_section,
13346 bfd_byte *contents,
13347 Elf_Internal_Rela *relocs,
13348 Elf_Internal_Sym *local_syms,
13349 asection **local_sections)
13351 struct ppc_link_hash_table *htab;
13352 Elf_Internal_Shdr *symtab_hdr;
13353 struct elf_link_hash_entry **sym_hashes;
13354 Elf_Internal_Rela *rel;
13355 Elf_Internal_Rela *wrel;
13356 Elf_Internal_Rela *relend;
13357 Elf_Internal_Rela outrel;
13359 struct got_entry **local_got_ents;
13361 bfd_boolean ret = TRUE;
13362 bfd_boolean is_opd;
13363 /* Assume 'at' branch hints. */
13364 bfd_boolean is_isa_v2 = TRUE;
13365 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13367 /* Initialize howto table if needed. */
13368 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13371 htab = ppc_hash_table (info);
13375 /* Don't relocate stub sections. */
13376 if (input_section->owner == htab->params->stub_bfd)
13379 BFD_ASSERT (is_ppc64_elf (input_bfd));
13381 local_got_ents = elf_local_got_ents (input_bfd);
13382 TOCstart = elf_gp (output_bfd);
13383 symtab_hdr = &elf_symtab_hdr (input_bfd);
13384 sym_hashes = elf_sym_hashes (input_bfd);
13385 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13387 rel = wrel = relocs;
13388 relend = relocs + input_section->reloc_count;
13389 for (; rel < relend; wrel++, rel++)
13391 enum elf_ppc64_reloc_type r_type;
13393 bfd_reloc_status_type r;
13394 Elf_Internal_Sym *sym;
13396 struct elf_link_hash_entry *h_elf;
13397 struct ppc_link_hash_entry *h;
13398 struct ppc_link_hash_entry *fdh;
13399 const char *sym_name;
13400 unsigned long r_symndx, toc_symndx;
13401 bfd_vma toc_addend;
13402 unsigned char tls_mask, tls_gd, tls_type;
13403 unsigned char sym_type;
13404 bfd_vma relocation;
13405 bfd_boolean unresolved_reloc;
13406 bfd_boolean warned;
13407 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13410 struct ppc_stub_hash_entry *stub_entry;
13411 bfd_vma max_br_offset;
13413 Elf_Internal_Rela orig_rel;
13414 reloc_howto_type *howto;
13415 struct reloc_howto_struct alt_howto;
13420 r_type = ELF64_R_TYPE (rel->r_info);
13421 r_symndx = ELF64_R_SYM (rel->r_info);
13423 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13424 symbol of the previous ADDR64 reloc. The symbol gives us the
13425 proper TOC base to use. */
13426 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13428 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13430 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13436 unresolved_reloc = FALSE;
13439 if (r_symndx < symtab_hdr->sh_info)
13441 /* It's a local symbol. */
13442 struct _opd_sec_data *opd;
13444 sym = local_syms + r_symndx;
13445 sec = local_sections[r_symndx];
13446 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13447 sym_type = ELF64_ST_TYPE (sym->st_info);
13448 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13449 opd = get_opd_info (sec);
13450 if (opd != NULL && opd->adjust != NULL)
13452 long adjust = opd->adjust[OPD_NDX (sym->st_value
13458 /* If this is a relocation against the opd section sym
13459 and we have edited .opd, adjust the reloc addend so
13460 that ld -r and ld --emit-relocs output is correct.
13461 If it is a reloc against some other .opd symbol,
13462 then the symbol value will be adjusted later. */
13463 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13464 rel->r_addend += adjust;
13466 relocation += adjust;
13472 bfd_boolean ignored;
13474 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13475 r_symndx, symtab_hdr, sym_hashes,
13476 h_elf, sec, relocation,
13477 unresolved_reloc, warned, ignored);
13478 sym_name = h_elf->root.root.string;
13479 sym_type = h_elf->type;
13481 && sec->owner == output_bfd
13482 && strcmp (sec->name, ".opd") == 0)
13484 /* This is a symbol defined in a linker script. All
13485 such are defined in output sections, even those
13486 defined by simple assignment from a symbol defined in
13487 an input section. Transfer the symbol to an
13488 appropriate input .opd section, so that a branch to
13489 this symbol will be mapped to the location specified
13490 by the opd entry. */
13491 struct bfd_link_order *lo;
13492 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13493 if (lo->type == bfd_indirect_link_order)
13495 asection *isec = lo->u.indirect.section;
13496 if (h_elf->root.u.def.value >= isec->output_offset
13497 && h_elf->root.u.def.value < (isec->output_offset
13500 h_elf->root.u.def.value -= isec->output_offset;
13501 h_elf->root.u.def.section = isec;
13508 h = (struct ppc_link_hash_entry *) h_elf;
13510 if (sec != NULL && discarded_section (sec))
13512 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13513 input_bfd, input_section,
13514 contents + rel->r_offset);
13515 wrel->r_offset = rel->r_offset;
13517 wrel->r_addend = 0;
13519 /* For ld -r, remove relocations in debug sections against
13520 sections defined in discarded sections. Not done for
13521 non-debug to preserve relocs in .eh_frame which the
13522 eh_frame editing code expects to be present. */
13523 if (bfd_link_relocatable (info)
13524 && (input_section->flags & SEC_DEBUGGING))
13530 if (bfd_link_relocatable (info))
13533 if (h != NULL && &h->elf == htab->elf.hgot)
13535 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13536 sec = bfd_abs_section_ptr;
13537 unresolved_reloc = FALSE;
13540 /* TLS optimizations. Replace instruction sequences and relocs
13541 based on information we collected in tls_optimize. We edit
13542 RELOCS so that --emit-relocs will output something sensible
13543 for the final instruction stream. */
13548 tls_mask = h->tls_mask;
13549 else if (local_got_ents != NULL)
13551 struct plt_entry **local_plt = (struct plt_entry **)
13552 (local_got_ents + symtab_hdr->sh_info);
13553 unsigned char *lgot_masks = (unsigned char *)
13554 (local_plt + symtab_hdr->sh_info);
13555 tls_mask = lgot_masks[r_symndx];
13558 && (r_type == R_PPC64_TLS
13559 || r_type == R_PPC64_TLSGD
13560 || r_type == R_PPC64_TLSLD))
13562 /* Check for toc tls entries. */
13563 unsigned char *toc_tls;
13565 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13566 &local_syms, rel, input_bfd))
13570 tls_mask = *toc_tls;
13573 /* Check that tls relocs are used with tls syms, and non-tls
13574 relocs are used with non-tls syms. */
13575 if (r_symndx != STN_UNDEF
13576 && r_type != R_PPC64_NONE
13578 || h->elf.root.type == bfd_link_hash_defined
13579 || h->elf.root.type == bfd_link_hash_defweak)
13580 && (IS_PPC64_TLS_RELOC (r_type)
13581 != (sym_type == STT_TLS
13582 || (sym_type == STT_SECTION
13583 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13586 && (r_type == R_PPC64_TLS
13587 || r_type == R_PPC64_TLSGD
13588 || r_type == R_PPC64_TLSLD))
13589 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13592 info->callbacks->einfo
13593 (!IS_PPC64_TLS_RELOC (r_type)
13594 /* xgettext:c-format */
13595 ? _("%H: %s used with TLS symbol `%T'\n")
13596 /* xgettext:c-format */
13597 : _("%H: %s used with non-TLS symbol `%T'\n"),
13598 input_bfd, input_section, rel->r_offset,
13599 ppc64_elf_howto_table[r_type]->name,
13603 /* Ensure reloc mapping code below stays sane. */
13604 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13605 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13606 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13607 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13608 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13609 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13610 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13611 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13612 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13613 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13621 case R_PPC64_LO_DS_OPT:
13622 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13623 if ((insn & (0x3f << 26)) != 58u << 26)
13625 insn += (14u << 26) - (58u << 26);
13626 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13627 r_type = R_PPC64_TOC16_LO;
13628 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13631 case R_PPC64_TOC16:
13632 case R_PPC64_TOC16_LO:
13633 case R_PPC64_TOC16_DS:
13634 case R_PPC64_TOC16_LO_DS:
13636 /* Check for toc tls entries. */
13637 unsigned char *toc_tls;
13640 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13641 &local_syms, rel, input_bfd);
13647 tls_mask = *toc_tls;
13648 if (r_type == R_PPC64_TOC16_DS
13649 || r_type == R_PPC64_TOC16_LO_DS)
13652 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13657 /* If we found a GD reloc pair, then we might be
13658 doing a GD->IE transition. */
13661 tls_gd = TLS_TPRELGD;
13662 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13665 else if (retval == 3)
13667 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13675 case R_PPC64_GOT_TPREL16_HI:
13676 case R_PPC64_GOT_TPREL16_HA:
13678 && (tls_mask & TLS_TPREL) == 0)
13680 rel->r_offset -= d_offset;
13681 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13682 r_type = R_PPC64_NONE;
13683 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13687 case R_PPC64_GOT_TPREL16_DS:
13688 case R_PPC64_GOT_TPREL16_LO_DS:
13690 && (tls_mask & TLS_TPREL) == 0)
13693 insn = bfd_get_32 (input_bfd,
13694 contents + rel->r_offset - d_offset);
13696 insn |= 0x3c0d0000; /* addis 0,13,0 */
13697 bfd_put_32 (input_bfd, insn,
13698 contents + rel->r_offset - d_offset);
13699 r_type = R_PPC64_TPREL16_HA;
13700 if (toc_symndx != 0)
13702 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13703 rel->r_addend = toc_addend;
13704 /* We changed the symbol. Start over in order to
13705 get h, sym, sec etc. right. */
13709 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13715 && (tls_mask & TLS_TPREL) == 0)
13717 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13718 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13721 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
13722 /* Was PPC64_TLS which sits on insn boundary, now
13723 PPC64_TPREL16_LO which is at low-order half-word. */
13724 rel->r_offset += d_offset;
13725 r_type = R_PPC64_TPREL16_LO;
13726 if (toc_symndx != 0)
13728 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13729 rel->r_addend = toc_addend;
13730 /* We changed the symbol. Start over in order to
13731 get h, sym, sec etc. right. */
13735 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13739 case R_PPC64_GOT_TLSGD16_HI:
13740 case R_PPC64_GOT_TLSGD16_HA:
13741 tls_gd = TLS_TPRELGD;
13742 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13746 case R_PPC64_GOT_TLSLD16_HI:
13747 case R_PPC64_GOT_TLSLD16_HA:
13748 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13751 if ((tls_mask & tls_gd) != 0)
13752 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13753 + R_PPC64_GOT_TPREL16_DS);
13756 rel->r_offset -= d_offset;
13757 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13758 r_type = R_PPC64_NONE;
13760 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13764 case R_PPC64_GOT_TLSGD16:
13765 case R_PPC64_GOT_TLSGD16_LO:
13766 tls_gd = TLS_TPRELGD;
13767 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13771 case R_PPC64_GOT_TLSLD16:
13772 case R_PPC64_GOT_TLSLD16_LO:
13773 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13775 unsigned int insn1, insn2, insn3;
13779 offset = (bfd_vma) -1;
13780 /* If not using the newer R_PPC64_TLSGD/LD to mark
13781 __tls_get_addr calls, we must trust that the call
13782 stays with its arg setup insns, ie. that the next
13783 reloc is the __tls_get_addr call associated with
13784 the current reloc. Edit both insns. */
13785 if (input_section->has_tls_get_addr_call
13786 && rel + 1 < relend
13787 && branch_reloc_hash_match (input_bfd, rel + 1,
13788 htab->tls_get_addr,
13789 htab->tls_get_addr_fd))
13790 offset = rel[1].r_offset;
13791 /* We read the low GOT_TLS (or TOC16) insn because we
13792 need to keep the destination reg. It may be
13793 something other than the usual r3, and moved to r3
13794 before the call by intervening code. */
13795 insn1 = bfd_get_32 (input_bfd,
13796 contents + rel->r_offset - d_offset);
13797 if ((tls_mask & tls_gd) != 0)
13800 insn1 &= (0x1f << 21) | (0x1f << 16);
13801 insn1 |= 58 << 26; /* ld */
13802 insn2 = 0x7c636a14; /* add 3,3,13 */
13803 if (offset != (bfd_vma) -1)
13804 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13805 if ((tls_mask & TLS_EXPLICIT) == 0)
13806 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13807 + R_PPC64_GOT_TPREL16_DS);
13809 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13810 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13815 insn1 &= 0x1f << 21;
13816 insn1 |= 0x3c0d0000; /* addis r,13,0 */
13817 insn2 = 0x38630000; /* addi 3,3,0 */
13820 /* Was an LD reloc. */
13822 sec = local_sections[toc_symndx];
13824 r_symndx < symtab_hdr->sh_info;
13826 if (local_sections[r_symndx] == sec)
13828 if (r_symndx >= symtab_hdr->sh_info)
13829 r_symndx = STN_UNDEF;
13830 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13831 if (r_symndx != STN_UNDEF)
13832 rel->r_addend -= (local_syms[r_symndx].st_value
13833 + sec->output_offset
13834 + sec->output_section->vma);
13836 else if (toc_symndx != 0)
13838 r_symndx = toc_symndx;
13839 rel->r_addend = toc_addend;
13841 r_type = R_PPC64_TPREL16_HA;
13842 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13843 if (offset != (bfd_vma) -1)
13845 rel[1].r_info = ELF64_R_INFO (r_symndx,
13846 R_PPC64_TPREL16_LO);
13847 rel[1].r_offset = offset + d_offset;
13848 rel[1].r_addend = rel->r_addend;
13851 bfd_put_32 (input_bfd, insn1,
13852 contents + rel->r_offset - d_offset);
13853 if (offset != (bfd_vma) -1)
13855 insn3 = bfd_get_32 (input_bfd,
13856 contents + offset + 4);
13858 || insn3 == CROR_151515 || insn3 == CROR_313131)
13860 rel[1].r_offset += 4;
13861 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
13864 bfd_put_32 (input_bfd, insn2, contents + offset);
13866 if ((tls_mask & tls_gd) == 0
13867 && (tls_gd == 0 || toc_symndx != 0))
13869 /* We changed the symbol. Start over in order
13870 to get h, sym, sec etc. right. */
13876 case R_PPC64_TLSGD:
13877 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13879 unsigned int insn2, insn3;
13880 bfd_vma offset = rel->r_offset;
13882 if ((tls_mask & TLS_TPRELGD) != 0)
13885 r_type = R_PPC64_NONE;
13886 insn2 = 0x7c636a14; /* add 3,3,13 */
13891 if (toc_symndx != 0)
13893 r_symndx = toc_symndx;
13894 rel->r_addend = toc_addend;
13896 r_type = R_PPC64_TPREL16_LO;
13897 rel->r_offset = offset + d_offset;
13898 insn2 = 0x38630000; /* addi 3,3,0 */
13900 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13901 /* Zap the reloc on the _tls_get_addr call too. */
13902 BFD_ASSERT (offset == rel[1].r_offset);
13903 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13904 insn3 = bfd_get_32 (input_bfd,
13905 contents + offset + 4);
13907 || insn3 == CROR_151515 || insn3 == CROR_313131)
13909 rel->r_offset += 4;
13910 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
13913 bfd_put_32 (input_bfd, insn2, contents + offset);
13914 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13919 case R_PPC64_TLSLD:
13920 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13922 unsigned int insn2, insn3;
13923 bfd_vma offset = rel->r_offset;
13926 sec = local_sections[toc_symndx];
13928 r_symndx < symtab_hdr->sh_info;
13930 if (local_sections[r_symndx] == sec)
13932 if (r_symndx >= symtab_hdr->sh_info)
13933 r_symndx = STN_UNDEF;
13934 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13935 if (r_symndx != STN_UNDEF)
13936 rel->r_addend -= (local_syms[r_symndx].st_value
13937 + sec->output_offset
13938 + sec->output_section->vma);
13940 r_type = R_PPC64_TPREL16_LO;
13941 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13942 rel->r_offset = offset + d_offset;
13943 /* Zap the reloc on the _tls_get_addr call too. */
13944 BFD_ASSERT (offset == rel[1].r_offset);
13945 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13946 insn2 = 0x38630000; /* addi 3,3,0 */
13947 insn3 = bfd_get_32 (input_bfd,
13948 contents + offset + 4);
13950 || insn3 == CROR_151515 || insn3 == CROR_313131)
13952 rel->r_offset += 4;
13953 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
13956 bfd_put_32 (input_bfd, insn2, contents + offset);
13961 case R_PPC64_DTPMOD64:
13962 if (rel + 1 < relend
13963 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13964 && rel[1].r_offset == rel->r_offset + 8)
13966 if ((tls_mask & TLS_GD) == 0)
13968 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13969 if ((tls_mask & TLS_TPRELGD) != 0)
13970 r_type = R_PPC64_TPREL64;
13973 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13974 r_type = R_PPC64_NONE;
13976 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13981 if ((tls_mask & TLS_LD) == 0)
13983 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13984 r_type = R_PPC64_NONE;
13985 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13990 case R_PPC64_TPREL64:
13991 if ((tls_mask & TLS_TPREL) == 0)
13993 r_type = R_PPC64_NONE;
13994 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13998 case R_PPC64_ENTRY:
13999 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14000 if (!bfd_link_pic (info)
14001 && !info->traditional_format
14002 && relocation + 0x80008000 <= 0xffffffff)
14004 unsigned int insn1, insn2;
14006 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14007 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14008 if ((insn1 & ~0xfffc) == LD_R2_0R12
14009 && insn2 == ADD_R2_R2_R12)
14011 bfd_put_32 (input_bfd,
14012 LIS_R2 + PPC_HA (relocation),
14013 contents + rel->r_offset);
14014 bfd_put_32 (input_bfd,
14015 ADDI_R2_R2 + PPC_LO (relocation),
14016 contents + rel->r_offset + 4);
14021 relocation -= (rel->r_offset
14022 + input_section->output_offset
14023 + input_section->output_section->vma);
14024 if (relocation + 0x80008000 <= 0xffffffff)
14026 unsigned int insn1, insn2;
14028 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14029 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14030 if ((insn1 & ~0xfffc) == LD_R2_0R12
14031 && insn2 == ADD_R2_R2_R12)
14033 bfd_put_32 (input_bfd,
14034 ADDIS_R2_R12 + PPC_HA (relocation),
14035 contents + rel->r_offset);
14036 bfd_put_32 (input_bfd,
14037 ADDI_R2_R2 + PPC_LO (relocation),
14038 contents + rel->r_offset + 4);
14044 case R_PPC64_REL16_HA:
14045 /* If we are generating a non-PIC executable, edit
14046 . 0: addis 2,12,.TOC.-0b@ha
14047 . addi 2,2,.TOC.-0b@l
14048 used by ELFv2 global entry points to set up r2, to
14051 if .TOC. is in range. */
14052 if (!bfd_link_pic (info)
14053 && !info->traditional_format
14055 && rel->r_addend == d_offset
14056 && h != NULL && &h->elf == htab->elf.hgot
14057 && rel + 1 < relend
14058 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14059 && rel[1].r_offset == rel->r_offset + 4
14060 && rel[1].r_addend == rel->r_addend + 4
14061 && relocation + 0x80008000 <= 0xffffffff)
14063 unsigned int insn1, insn2;
14064 bfd_vma offset = rel->r_offset - d_offset;
14065 insn1 = bfd_get_32 (input_bfd, contents + offset);
14066 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14067 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14068 && (insn2 & 0xffff0000) == ADDI_R2_R2)
14070 r_type = R_PPC64_ADDR16_HA;
14071 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14072 rel->r_addend -= d_offset;
14073 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14074 rel[1].r_addend -= d_offset + 4;
14075 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14081 /* Handle other relocations that tweak non-addend part of insn. */
14083 max_br_offset = 1 << 25;
14084 addend = rel->r_addend;
14085 reloc_dest = DEST_NORMAL;
14091 case R_PPC64_TOCSAVE:
14092 if (relocation + addend == (rel->r_offset
14093 + input_section->output_offset
14094 + input_section->output_section->vma)
14095 && tocsave_find (htab, NO_INSERT,
14096 &local_syms, rel, input_bfd))
14098 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14100 || insn == CROR_151515 || insn == CROR_313131)
14101 bfd_put_32 (input_bfd,
14102 STD_R2_0R1 + STK_TOC (htab),
14103 contents + rel->r_offset);
14107 /* Branch taken prediction relocations. */
14108 case R_PPC64_ADDR14_BRTAKEN:
14109 case R_PPC64_REL14_BRTAKEN:
14110 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
14111 /* Fall through. */
14113 /* Branch not taken prediction relocations. */
14114 case R_PPC64_ADDR14_BRNTAKEN:
14115 case R_PPC64_REL14_BRNTAKEN:
14116 insn |= bfd_get_32 (input_bfd,
14117 contents + rel->r_offset) & ~(0x01 << 21);
14118 /* Fall through. */
14120 case R_PPC64_REL14:
14121 max_br_offset = 1 << 15;
14122 /* Fall through. */
14124 case R_PPC64_REL24:
14125 /* Calls to functions with a different TOC, such as calls to
14126 shared objects, need to alter the TOC pointer. This is
14127 done using a linkage stub. A REL24 branching to these
14128 linkage stubs needs to be followed by a nop, as the nop
14129 will be replaced with an instruction to restore the TOC
14134 && h->oh->is_func_descriptor)
14135 fdh = ppc_follow_link (h->oh);
14136 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14138 if (stub_entry != NULL
14139 && (stub_entry->stub_type == ppc_stub_plt_call
14140 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14141 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14142 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
14144 bfd_boolean can_plt_call = FALSE;
14146 /* All of these stubs will modify r2, so there must be a
14147 branch and link followed by a nop. The nop is
14148 replaced by an insn to restore r2. */
14149 if (rel->r_offset + 8 <= input_section->size)
14153 br = bfd_get_32 (input_bfd,
14154 contents + rel->r_offset);
14159 nop = bfd_get_32 (input_bfd,
14160 contents + rel->r_offset + 4);
14162 || nop == CROR_151515 || nop == CROR_313131)
14165 && (h == htab->tls_get_addr_fd
14166 || h == htab->tls_get_addr)
14167 && htab->params->tls_get_addr_opt)
14169 /* Special stub used, leave nop alone. */
14172 bfd_put_32 (input_bfd,
14173 LD_R2_0R1 + STK_TOC (htab),
14174 contents + rel->r_offset + 4);
14175 can_plt_call = TRUE;
14180 if (!can_plt_call && h != NULL)
14182 const char *name = h->elf.root.root.string;
14187 if (strncmp (name, "__libc_start_main", 17) == 0
14188 && (name[17] == 0 || name[17] == '@'))
14190 /* Allow crt1 branch to go via a toc adjusting
14191 stub. Other calls that never return could do
14192 the same, if we could detect such. */
14193 can_plt_call = TRUE;
14199 /* g++ as of 20130507 emits self-calls without a
14200 following nop. This is arguably wrong since we
14201 have conflicting information. On the one hand a
14202 global symbol and on the other a local call
14203 sequence, but don't error for this special case.
14204 It isn't possible to cheaply verify we have
14205 exactly such a call. Allow all calls to the same
14207 asection *code_sec = sec;
14209 if (get_opd_info (sec) != NULL)
14211 bfd_vma off = (relocation + addend
14212 - sec->output_section->vma
14213 - sec->output_offset);
14215 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14217 if (code_sec == input_section)
14218 can_plt_call = TRUE;
14223 if (stub_entry->stub_type == ppc_stub_plt_call
14224 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14225 info->callbacks->einfo
14226 /* xgettext:c-format */
14227 (_("%H: call to `%T' lacks nop, can't restore toc; "
14228 "recompile with -fPIC\n"),
14229 input_bfd, input_section, rel->r_offset, sym_name);
14231 info->callbacks->einfo
14232 /* xgettext:c-format */
14233 (_("%H: call to `%T' lacks nop, can't restore toc; "
14234 "(-mcmodel=small toc adjust stub)\n"),
14235 input_bfd, input_section, rel->r_offset, sym_name);
14237 bfd_set_error (bfd_error_bad_value);
14242 && (stub_entry->stub_type == ppc_stub_plt_call
14243 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14244 unresolved_reloc = FALSE;
14247 if ((stub_entry == NULL
14248 || stub_entry->stub_type == ppc_stub_long_branch
14249 || stub_entry->stub_type == ppc_stub_plt_branch)
14250 && get_opd_info (sec) != NULL)
14252 /* The branch destination is the value of the opd entry. */
14253 bfd_vma off = (relocation + addend
14254 - sec->output_section->vma
14255 - sec->output_offset);
14256 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14257 if (dest != (bfd_vma) -1)
14261 reloc_dest = DEST_OPD;
14265 /* If the branch is out of reach we ought to have a long
14267 from = (rel->r_offset
14268 + input_section->output_offset
14269 + input_section->output_section->vma);
14271 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14275 if (stub_entry != NULL
14276 && (stub_entry->stub_type == ppc_stub_long_branch
14277 || stub_entry->stub_type == ppc_stub_plt_branch)
14278 && (r_type == R_PPC64_ADDR14_BRTAKEN
14279 || r_type == R_PPC64_ADDR14_BRNTAKEN
14280 || (relocation + addend - from + max_br_offset
14281 < 2 * max_br_offset)))
14282 /* Don't use the stub if this branch is in range. */
14285 if (stub_entry != NULL)
14287 /* Munge up the value and addend so that we call the stub
14288 rather than the procedure directly. */
14289 asection *stub_sec = stub_entry->group->stub_sec;
14291 if (stub_entry->stub_type == ppc_stub_save_res)
14292 relocation += (stub_sec->output_offset
14293 + stub_sec->output_section->vma
14294 + stub_sec->size - htab->sfpr->size
14295 - htab->sfpr->output_offset
14296 - htab->sfpr->output_section->vma);
14298 relocation = (stub_entry->stub_offset
14299 + stub_sec->output_offset
14300 + stub_sec->output_section->vma);
14302 reloc_dest = DEST_STUB;
14304 if ((stub_entry->stub_type == ppc_stub_plt_call
14305 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14306 && (ALWAYS_EMIT_R2SAVE
14307 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14308 && rel + 1 < relend
14309 && rel[1].r_offset == rel->r_offset + 4
14310 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14318 /* Set 'a' bit. This is 0b00010 in BO field for branch
14319 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14320 for branch on CTR insns (BO == 1a00t or 1a01t). */
14321 if ((insn & (0x14 << 21)) == (0x04 << 21))
14322 insn |= 0x02 << 21;
14323 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14324 insn |= 0x08 << 21;
14330 /* Invert 'y' bit if not the default. */
14331 if ((bfd_signed_vma) (relocation + addend - from) < 0)
14332 insn ^= 0x01 << 21;
14335 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14338 /* NOP out calls to undefined weak functions.
14339 We can thus call a weak function without first
14340 checking whether the function is defined. */
14342 && h->elf.root.type == bfd_link_hash_undefweak
14343 && h->elf.dynindx == -1
14344 && r_type == R_PPC64_REL24
14348 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14354 /* Set `addend'. */
14359 info->callbacks->einfo
14360 /* xgettext:c-format */
14361 (_("%P: %B: unknown relocation type %d for `%T'\n"),
14362 input_bfd, (int) r_type, sym_name);
14364 bfd_set_error (bfd_error_bad_value);
14370 case R_PPC64_TLSGD:
14371 case R_PPC64_TLSLD:
14372 case R_PPC64_TOCSAVE:
14373 case R_PPC64_GNU_VTINHERIT:
14374 case R_PPC64_GNU_VTENTRY:
14375 case R_PPC64_ENTRY:
14378 /* GOT16 relocations. Like an ADDR16 using the symbol's
14379 address in the GOT as relocation value instead of the
14380 symbol's value itself. Also, create a GOT entry for the
14381 symbol and put the symbol value there. */
14382 case R_PPC64_GOT_TLSGD16:
14383 case R_PPC64_GOT_TLSGD16_LO:
14384 case R_PPC64_GOT_TLSGD16_HI:
14385 case R_PPC64_GOT_TLSGD16_HA:
14386 tls_type = TLS_TLS | TLS_GD;
14389 case R_PPC64_GOT_TLSLD16:
14390 case R_PPC64_GOT_TLSLD16_LO:
14391 case R_PPC64_GOT_TLSLD16_HI:
14392 case R_PPC64_GOT_TLSLD16_HA:
14393 tls_type = TLS_TLS | TLS_LD;
14396 case R_PPC64_GOT_TPREL16_DS:
14397 case R_PPC64_GOT_TPREL16_LO_DS:
14398 case R_PPC64_GOT_TPREL16_HI:
14399 case R_PPC64_GOT_TPREL16_HA:
14400 tls_type = TLS_TLS | TLS_TPREL;
14403 case R_PPC64_GOT_DTPREL16_DS:
14404 case R_PPC64_GOT_DTPREL16_LO_DS:
14405 case R_PPC64_GOT_DTPREL16_HI:
14406 case R_PPC64_GOT_DTPREL16_HA:
14407 tls_type = TLS_TLS | TLS_DTPREL;
14410 case R_PPC64_GOT16:
14411 case R_PPC64_GOT16_LO:
14412 case R_PPC64_GOT16_HI:
14413 case R_PPC64_GOT16_HA:
14414 case R_PPC64_GOT16_DS:
14415 case R_PPC64_GOT16_LO_DS:
14418 /* Relocation is to the entry for this symbol in the global
14423 unsigned long indx = 0;
14424 struct got_entry *ent;
14426 if (tls_type == (TLS_TLS | TLS_LD)
14428 || !h->elf.def_dynamic))
14429 ent = ppc64_tlsld_got (input_bfd);
14434 if (!htab->elf.dynamic_sections_created
14435 || h->elf.dynindx == -1
14436 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14437 || (ELF_ST_VISIBILITY (h->elf.other) != STV_DEFAULT
14438 && h->elf.root.type == bfd_link_hash_undefweak))
14439 /* This is actually a static link, or it is a
14440 -Bsymbolic link and the symbol is defined
14441 locally, or the symbol was forced to be local
14442 because of a version file. */
14446 indx = h->elf.dynindx;
14447 unresolved_reloc = FALSE;
14449 ent = h->elf.got.glist;
14453 if (local_got_ents == NULL)
14455 ent = local_got_ents[r_symndx];
14458 for (; ent != NULL; ent = ent->next)
14459 if (ent->addend == orig_rel.r_addend
14460 && ent->owner == input_bfd
14461 && ent->tls_type == tls_type)
14467 if (ent->is_indirect)
14468 ent = ent->got.ent;
14469 offp = &ent->got.offset;
14470 got = ppc64_elf_tdata (ent->owner)->got;
14474 /* The offset must always be a multiple of 8. We use the
14475 least significant bit to record whether we have already
14476 processed this entry. */
14478 if ((off & 1) != 0)
14482 /* Generate relocs for the dynamic linker, except in
14483 the case of TLSLD where we'll use one entry per
14491 ? h->elf.type == STT_GNU_IFUNC
14492 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14495 relgot = htab->elf.irelplt;
14497 htab->local_ifunc_resolver = 1;
14498 else if (is_static_defined (&h->elf))
14499 htab->maybe_local_ifunc_resolver = 1;
14502 || (bfd_link_pic (info)
14504 || (ELF_ST_VISIBILITY (h->elf.other)
14506 || h->elf.root.type != bfd_link_hash_undefweak
14507 || (tls_type == (TLS_TLS | TLS_LD)
14508 && !h->elf.def_dynamic))))
14509 relgot = ppc64_elf_tdata (ent->owner)->relgot;
14510 if (relgot != NULL)
14512 outrel.r_offset = (got->output_section->vma
14513 + got->output_offset
14515 outrel.r_addend = addend;
14516 if (tls_type & (TLS_LD | TLS_GD))
14518 outrel.r_addend = 0;
14519 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14520 if (tls_type == (TLS_TLS | TLS_GD))
14522 loc = relgot->contents;
14523 loc += (relgot->reloc_count++
14524 * sizeof (Elf64_External_Rela));
14525 bfd_elf64_swap_reloca_out (output_bfd,
14527 outrel.r_offset += 8;
14528 outrel.r_addend = addend;
14530 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14533 else if (tls_type == (TLS_TLS | TLS_DTPREL))
14534 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14535 else if (tls_type == (TLS_TLS | TLS_TPREL))
14536 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14537 else if (indx != 0)
14538 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14542 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14544 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14546 /* Write the .got section contents for the sake
14548 loc = got->contents + off;
14549 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14553 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14555 outrel.r_addend += relocation;
14556 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14558 if (htab->elf.tls_sec == NULL)
14559 outrel.r_addend = 0;
14561 outrel.r_addend -= htab->elf.tls_sec->vma;
14564 loc = relgot->contents;
14565 loc += (relgot->reloc_count++
14566 * sizeof (Elf64_External_Rela));
14567 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14570 /* Init the .got section contents here if we're not
14571 emitting a reloc. */
14575 = (htab->params->tls_get_addr_opt
14576 && htab->tls_get_addr_fd != NULL
14577 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
14579 relocation += addend;
14582 if (htab->elf.tls_sec == NULL)
14586 if (tls_type & TLS_LD)
14589 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14590 if ((tls_type & TLS_TPREL)
14591 || (tlsopt && !(tls_type & TLS_DTPREL)))
14592 relocation += DTP_OFFSET - TP_OFFSET;
14595 if (tls_type & (TLS_GD | TLS_LD))
14597 bfd_put_64 (output_bfd, relocation,
14598 got->contents + off + 8);
14599 relocation = !tlsopt;
14602 bfd_put_64 (output_bfd, relocation,
14603 got->contents + off);
14607 if (off >= (bfd_vma) -2)
14610 relocation = got->output_section->vma + got->output_offset + off;
14611 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14615 case R_PPC64_PLT16_HA:
14616 case R_PPC64_PLT16_HI:
14617 case R_PPC64_PLT16_LO:
14618 case R_PPC64_PLT32:
14619 case R_PPC64_PLT64:
14620 /* Relocation is to the entry for this symbol in the
14621 procedure linkage table. */
14623 struct plt_entry **plt_list = NULL;
14625 plt_list = &h->elf.plt.plist;
14626 else if (local_got_ents != NULL)
14628 struct plt_entry **local_plt = (struct plt_entry **)
14629 (local_got_ents + symtab_hdr->sh_info);
14630 unsigned char *local_got_tls_masks = (unsigned char *)
14631 (local_plt + symtab_hdr->sh_info);
14632 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14633 plt_list = local_plt + r_symndx;
14637 struct plt_entry *ent;
14639 for (ent = *plt_list; ent != NULL; ent = ent->next)
14640 if (ent->plt.offset != (bfd_vma) -1
14641 && ent->addend == orig_rel.r_addend)
14645 plt = htab->elf.splt;
14646 if (!htab->elf.dynamic_sections_created
14648 || h->elf.dynindx == -1)
14649 plt = htab->elf.iplt;
14650 relocation = (plt->output_section->vma
14651 + plt->output_offset
14652 + ent->plt.offset);
14654 unresolved_reloc = FALSE;
14662 /* Relocation value is TOC base. */
14663 relocation = TOCstart;
14664 if (r_symndx == STN_UNDEF)
14665 relocation += htab->sec_info[input_section->id].toc_off;
14666 else if (unresolved_reloc)
14668 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14669 relocation += htab->sec_info[sec->id].toc_off;
14671 unresolved_reloc = TRUE;
14674 /* TOC16 relocs. We want the offset relative to the TOC base,
14675 which is the address of the start of the TOC plus 0x8000.
14676 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14678 case R_PPC64_TOC16:
14679 case R_PPC64_TOC16_LO:
14680 case R_PPC64_TOC16_HI:
14681 case R_PPC64_TOC16_DS:
14682 case R_PPC64_TOC16_LO_DS:
14683 case R_PPC64_TOC16_HA:
14684 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14687 /* Relocate against the beginning of the section. */
14688 case R_PPC64_SECTOFF:
14689 case R_PPC64_SECTOFF_LO:
14690 case R_PPC64_SECTOFF_HI:
14691 case R_PPC64_SECTOFF_DS:
14692 case R_PPC64_SECTOFF_LO_DS:
14693 case R_PPC64_SECTOFF_HA:
14695 addend -= sec->output_section->vma;
14698 case R_PPC64_REL16:
14699 case R_PPC64_REL16_LO:
14700 case R_PPC64_REL16_HI:
14701 case R_PPC64_REL16_HA:
14702 case R_PPC64_REL16DX_HA:
14705 case R_PPC64_REL14:
14706 case R_PPC64_REL14_BRNTAKEN:
14707 case R_PPC64_REL14_BRTAKEN:
14708 case R_PPC64_REL24:
14711 case R_PPC64_TPREL16:
14712 case R_PPC64_TPREL16_LO:
14713 case R_PPC64_TPREL16_HI:
14714 case R_PPC64_TPREL16_HA:
14715 case R_PPC64_TPREL16_DS:
14716 case R_PPC64_TPREL16_LO_DS:
14717 case R_PPC64_TPREL16_HIGH:
14718 case R_PPC64_TPREL16_HIGHA:
14719 case R_PPC64_TPREL16_HIGHER:
14720 case R_PPC64_TPREL16_HIGHERA:
14721 case R_PPC64_TPREL16_HIGHEST:
14722 case R_PPC64_TPREL16_HIGHESTA:
14724 && h->elf.root.type == bfd_link_hash_undefweak
14725 && h->elf.dynindx == -1)
14727 /* Make this relocation against an undefined weak symbol
14728 resolve to zero. This is really just a tweak, since
14729 code using weak externs ought to check that they are
14730 defined before using them. */
14731 bfd_byte *p = contents + rel->r_offset - d_offset;
14733 insn = bfd_get_32 (input_bfd, p);
14734 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14736 bfd_put_32 (input_bfd, insn, p);
14739 if (htab->elf.tls_sec != NULL)
14740 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14741 if (bfd_link_pic (info))
14742 /* The TPREL16 relocs shouldn't really be used in shared
14743 libs as they will result in DT_TEXTREL being set, but
14744 support them anyway. */
14748 case R_PPC64_DTPREL16:
14749 case R_PPC64_DTPREL16_LO:
14750 case R_PPC64_DTPREL16_HI:
14751 case R_PPC64_DTPREL16_HA:
14752 case R_PPC64_DTPREL16_DS:
14753 case R_PPC64_DTPREL16_LO_DS:
14754 case R_PPC64_DTPREL16_HIGH:
14755 case R_PPC64_DTPREL16_HIGHA:
14756 case R_PPC64_DTPREL16_HIGHER:
14757 case R_PPC64_DTPREL16_HIGHERA:
14758 case R_PPC64_DTPREL16_HIGHEST:
14759 case R_PPC64_DTPREL16_HIGHESTA:
14760 if (htab->elf.tls_sec != NULL)
14761 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14764 case R_PPC64_ADDR64_LOCAL:
14765 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14770 case R_PPC64_DTPMOD64:
14775 case R_PPC64_TPREL64:
14776 if (htab->elf.tls_sec != NULL)
14777 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14780 case R_PPC64_DTPREL64:
14781 if (htab->elf.tls_sec != NULL)
14782 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14783 /* Fall through. */
14785 /* Relocations that may need to be propagated if this is a
14787 case R_PPC64_REL30:
14788 case R_PPC64_REL32:
14789 case R_PPC64_REL64:
14790 case R_PPC64_ADDR14:
14791 case R_PPC64_ADDR14_BRNTAKEN:
14792 case R_PPC64_ADDR14_BRTAKEN:
14793 case R_PPC64_ADDR16:
14794 case R_PPC64_ADDR16_DS:
14795 case R_PPC64_ADDR16_HA:
14796 case R_PPC64_ADDR16_HI:
14797 case R_PPC64_ADDR16_HIGH:
14798 case R_PPC64_ADDR16_HIGHA:
14799 case R_PPC64_ADDR16_HIGHER:
14800 case R_PPC64_ADDR16_HIGHERA:
14801 case R_PPC64_ADDR16_HIGHEST:
14802 case R_PPC64_ADDR16_HIGHESTA:
14803 case R_PPC64_ADDR16_LO:
14804 case R_PPC64_ADDR16_LO_DS:
14805 case R_PPC64_ADDR24:
14806 case R_PPC64_ADDR32:
14807 case R_PPC64_ADDR64:
14808 case R_PPC64_UADDR16:
14809 case R_PPC64_UADDR32:
14810 case R_PPC64_UADDR64:
14812 if ((input_section->flags & SEC_ALLOC) == 0)
14815 if (NO_OPD_RELOCS && is_opd)
14818 if (bfd_link_pic (info)
14820 || h->dyn_relocs != NULL)
14821 && ((h != NULL && pc_dynrelocs (h))
14822 || must_be_dyn_reloc (info, r_type)))
14824 ? h->dyn_relocs != NULL
14825 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14827 bfd_boolean skip, relocate;
14832 /* When generating a dynamic object, these relocations
14833 are copied into the output file to be resolved at run
14839 out_off = _bfd_elf_section_offset (output_bfd, info,
14840 input_section, rel->r_offset);
14841 if (out_off == (bfd_vma) -1)
14843 else if (out_off == (bfd_vma) -2)
14844 skip = TRUE, relocate = TRUE;
14845 out_off += (input_section->output_section->vma
14846 + input_section->output_offset);
14847 outrel.r_offset = out_off;
14848 outrel.r_addend = rel->r_addend;
14850 /* Optimize unaligned reloc use. */
14851 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14852 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14853 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14854 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14855 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14856 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14857 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14858 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14859 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14862 memset (&outrel, 0, sizeof outrel);
14863 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14865 && r_type != R_PPC64_TOC)
14867 indx = h->elf.dynindx;
14868 BFD_ASSERT (indx != -1);
14869 outrel.r_info = ELF64_R_INFO (indx, r_type);
14873 /* This symbol is local, or marked to become local,
14874 or this is an opd section reloc which must point
14875 at a local function. */
14876 outrel.r_addend += relocation;
14877 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14879 if (is_opd && h != NULL)
14881 /* Lie about opd entries. This case occurs
14882 when building shared libraries and we
14883 reference a function in another shared
14884 lib. The same thing happens for a weak
14885 definition in an application that's
14886 overridden by a strong definition in a
14887 shared lib. (I believe this is a generic
14888 bug in binutils handling of weak syms.)
14889 In these cases we won't use the opd
14890 entry in this lib. */
14891 unresolved_reloc = FALSE;
14894 && r_type == R_PPC64_ADDR64
14896 ? h->elf.type == STT_GNU_IFUNC
14897 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14898 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14901 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14903 /* We need to relocate .opd contents for ld.so.
14904 Prelink also wants simple and consistent rules
14905 for relocs. This make all RELATIVE relocs have
14906 *r_offset equal to r_addend. */
14913 ? h->elf.type == STT_GNU_IFUNC
14914 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14916 info->callbacks->einfo
14917 /* xgettext:c-format */
14918 (_("%H: %s for indirect "
14919 "function `%T' unsupported\n"),
14920 input_bfd, input_section, rel->r_offset,
14921 ppc64_elf_howto_table[r_type]->name,
14925 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14927 else if (sec == NULL || sec->owner == NULL)
14929 bfd_set_error (bfd_error_bad_value);
14936 osec = sec->output_section;
14937 indx = elf_section_data (osec)->dynindx;
14941 if ((osec->flags & SEC_READONLY) == 0
14942 && htab->elf.data_index_section != NULL)
14943 osec = htab->elf.data_index_section;
14945 osec = htab->elf.text_index_section;
14946 indx = elf_section_data (osec)->dynindx;
14948 BFD_ASSERT (indx != 0);
14950 /* We are turning this relocation into one
14951 against a section symbol, so subtract out
14952 the output section's address but not the
14953 offset of the input section in the output
14955 outrel.r_addend -= osec->vma;
14958 outrel.r_info = ELF64_R_INFO (indx, r_type);
14962 sreloc = elf_section_data (input_section)->sreloc;
14964 ? h->elf.type == STT_GNU_IFUNC
14965 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14967 sreloc = htab->elf.irelplt;
14969 htab->local_ifunc_resolver = 1;
14970 else if (is_static_defined (&h->elf))
14971 htab->maybe_local_ifunc_resolver = 1;
14973 if (sreloc == NULL)
14976 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14979 loc = sreloc->contents;
14980 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14981 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14983 /* If this reloc is against an external symbol, it will
14984 be computed at runtime, so there's no need to do
14985 anything now. However, for the sake of prelink ensure
14986 that the section contents are a known value. */
14989 unresolved_reloc = FALSE;
14990 /* The value chosen here is quite arbitrary as ld.so
14991 ignores section contents except for the special
14992 case of .opd where the contents might be accessed
14993 before relocation. Choose zero, as that won't
14994 cause reloc overflow. */
14997 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14998 to improve backward compatibility with older
15000 if (r_type == R_PPC64_ADDR64)
15001 addend = outrel.r_addend;
15002 /* Adjust pc_relative relocs to have zero in *r_offset. */
15003 else if (ppc64_elf_howto_table[r_type]->pc_relative)
15004 addend = outrel.r_offset;
15007 else if (r_type == R_PPC64_DTPMOD64
15008 && htab->params->tls_get_addr_opt
15009 && htab->tls_get_addr_fd != NULL
15010 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
15012 /* Set up for __tls_get_addr_opt stub, when this entry
15013 does not have dynamic relocs. */
15015 /* Set up the next word for local dynamic. If it turns
15016 out to be global dynamic, the reloc will overwrite
15018 if (rel->r_offset + 16 <= input_section->size)
15019 bfd_put_64 (input_bfd, DTP_OFFSET - TP_OFFSET,
15020 contents + rel->r_offset + 8);
15022 else if (r_type == R_PPC64_DTPREL64
15023 && htab->params->tls_get_addr_opt
15024 && htab->tls_get_addr_fd != NULL
15025 && htab->tls_get_addr_fd->elf.plt.plist != NULL
15027 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
15028 && rel[-1].r_offset + 8 == rel->r_offset)
15030 /* __tls_get_addr_opt stub value. */
15031 addend += DTP_OFFSET - TP_OFFSET;
15036 case R_PPC64_GLOB_DAT:
15037 case R_PPC64_JMP_SLOT:
15038 case R_PPC64_JMP_IREL:
15039 case R_PPC64_RELATIVE:
15040 /* We shouldn't ever see these dynamic relocs in relocatable
15042 /* Fall through. */
15044 case R_PPC64_PLTGOT16:
15045 case R_PPC64_PLTGOT16_DS:
15046 case R_PPC64_PLTGOT16_HA:
15047 case R_PPC64_PLTGOT16_HI:
15048 case R_PPC64_PLTGOT16_LO:
15049 case R_PPC64_PLTGOT16_LO_DS:
15050 case R_PPC64_PLTREL32:
15051 case R_PPC64_PLTREL64:
15052 /* These ones haven't been implemented yet. */
15054 info->callbacks->einfo
15055 /* xgettext:c-format */
15056 (_("%P: %B: %s is not supported for `%T'\n"),
15058 ppc64_elf_howto_table[r_type]->name, sym_name);
15060 bfd_set_error (bfd_error_invalid_operation);
15065 /* Multi-instruction sequences that access the TOC can be
15066 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15067 to nop; addi rb,r2,x; */
15073 case R_PPC64_GOT_TLSLD16_HI:
15074 case R_PPC64_GOT_TLSGD16_HI:
15075 case R_PPC64_GOT_TPREL16_HI:
15076 case R_PPC64_GOT_DTPREL16_HI:
15077 case R_PPC64_GOT16_HI:
15078 case R_PPC64_TOC16_HI:
15079 /* These relocs would only be useful if building up an
15080 offset to later add to r2, perhaps in an indexed
15081 addressing mode instruction. Don't try to optimize.
15082 Unfortunately, the possibility of someone building up an
15083 offset like this or even with the HA relocs, means that
15084 we need to check the high insn when optimizing the low
15088 case R_PPC64_GOT_TLSLD16_HA:
15089 case R_PPC64_GOT_TLSGD16_HA:
15090 case R_PPC64_GOT_TPREL16_HA:
15091 case R_PPC64_GOT_DTPREL16_HA:
15092 case R_PPC64_GOT16_HA:
15093 case R_PPC64_TOC16_HA:
15094 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15095 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15097 bfd_byte *p = contents + (rel->r_offset & ~3);
15098 bfd_put_32 (input_bfd, NOP, p);
15102 case R_PPC64_GOT_TLSLD16_LO:
15103 case R_PPC64_GOT_TLSGD16_LO:
15104 case R_PPC64_GOT_TPREL16_LO_DS:
15105 case R_PPC64_GOT_DTPREL16_LO_DS:
15106 case R_PPC64_GOT16_LO:
15107 case R_PPC64_GOT16_LO_DS:
15108 case R_PPC64_TOC16_LO:
15109 case R_PPC64_TOC16_LO_DS:
15110 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15111 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15113 bfd_byte *p = contents + (rel->r_offset & ~3);
15114 insn = bfd_get_32 (input_bfd, p);
15115 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15117 /* Transform addic to addi when we change reg. */
15118 insn &= ~((0x3f << 26) | (0x1f << 16));
15119 insn |= (14u << 26) | (2 << 16);
15123 insn &= ~(0x1f << 16);
15126 bfd_put_32 (input_bfd, insn, p);
15131 /* Do any further special processing. */
15132 howto = ppc64_elf_howto_table[(int) r_type];
15138 case R_PPC64_REL16_HA:
15139 case R_PPC64_REL16DX_HA:
15140 case R_PPC64_ADDR16_HA:
15141 case R_PPC64_ADDR16_HIGHA:
15142 case R_PPC64_ADDR16_HIGHERA:
15143 case R_PPC64_ADDR16_HIGHESTA:
15144 case R_PPC64_TOC16_HA:
15145 case R_PPC64_SECTOFF_HA:
15146 case R_PPC64_TPREL16_HA:
15147 case R_PPC64_TPREL16_HIGHA:
15148 case R_PPC64_TPREL16_HIGHERA:
15149 case R_PPC64_TPREL16_HIGHESTA:
15150 case R_PPC64_DTPREL16_HA:
15151 case R_PPC64_DTPREL16_HIGHA:
15152 case R_PPC64_DTPREL16_HIGHERA:
15153 case R_PPC64_DTPREL16_HIGHESTA:
15154 /* It's just possible that this symbol is a weak symbol
15155 that's not actually defined anywhere. In that case,
15156 'sec' would be NULL, and we should leave the symbol
15157 alone (it will be set to zero elsewhere in the link). */
15160 /* Fall through. */
15162 case R_PPC64_GOT16_HA:
15163 case R_PPC64_PLTGOT16_HA:
15164 case R_PPC64_PLT16_HA:
15165 case R_PPC64_GOT_TLSGD16_HA:
15166 case R_PPC64_GOT_TLSLD16_HA:
15167 case R_PPC64_GOT_TPREL16_HA:
15168 case R_PPC64_GOT_DTPREL16_HA:
15169 /* Add 0x10000 if sign bit in 0:15 is set.
15170 Bits 0:15 are not used. */
15174 case R_PPC64_ADDR16_DS:
15175 case R_PPC64_ADDR16_LO_DS:
15176 case R_PPC64_GOT16_DS:
15177 case R_PPC64_GOT16_LO_DS:
15178 case R_PPC64_PLT16_LO_DS:
15179 case R_PPC64_SECTOFF_DS:
15180 case R_PPC64_SECTOFF_LO_DS:
15181 case R_PPC64_TOC16_DS:
15182 case R_PPC64_TOC16_LO_DS:
15183 case R_PPC64_PLTGOT16_DS:
15184 case R_PPC64_PLTGOT16_LO_DS:
15185 case R_PPC64_GOT_TPREL16_DS:
15186 case R_PPC64_GOT_TPREL16_LO_DS:
15187 case R_PPC64_GOT_DTPREL16_DS:
15188 case R_PPC64_GOT_DTPREL16_LO_DS:
15189 case R_PPC64_TPREL16_DS:
15190 case R_PPC64_TPREL16_LO_DS:
15191 case R_PPC64_DTPREL16_DS:
15192 case R_PPC64_DTPREL16_LO_DS:
15193 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15195 /* If this reloc is against an lq, lxv, or stxv insn, then
15196 the value must be a multiple of 16. This is somewhat of
15197 a hack, but the "correct" way to do this by defining _DQ
15198 forms of all the _DS relocs bloats all reloc switches in
15199 this file. It doesn't make much sense to use these
15200 relocs in data, so testing the insn should be safe. */
15201 if ((insn & (0x3f << 26)) == (56u << 26)
15202 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15204 relocation += addend;
15205 addend = insn & (mask ^ 3);
15206 if ((relocation & mask) != 0)
15208 relocation ^= relocation & mask;
15209 info->callbacks->einfo
15210 /* xgettext:c-format */
15211 (_("%H: error: %s not a multiple of %u\n"),
15212 input_bfd, input_section, rel->r_offset,
15215 bfd_set_error (bfd_error_bad_value);
15222 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15223 because such sections are not SEC_ALLOC and thus ld.so will
15224 not process them. */
15225 if (unresolved_reloc
15226 && !((input_section->flags & SEC_DEBUGGING) != 0
15227 && h->elf.def_dynamic)
15228 && _bfd_elf_section_offset (output_bfd, info, input_section,
15229 rel->r_offset) != (bfd_vma) -1)
15231 info->callbacks->einfo
15232 /* xgettext:c-format */
15233 (_("%H: unresolvable %s against `%T'\n"),
15234 input_bfd, input_section, rel->r_offset,
15236 h->elf.root.root.string);
15240 /* 16-bit fields in insns mostly have signed values, but a
15241 few insns have 16-bit unsigned values. Really, we should
15242 have different reloc types. */
15243 if (howto->complain_on_overflow != complain_overflow_dont
15244 && howto->dst_mask == 0xffff
15245 && (input_section->flags & SEC_CODE) != 0)
15247 enum complain_overflow complain = complain_overflow_signed;
15249 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15250 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15251 complain = complain_overflow_bitfield;
15252 else if (howto->rightshift == 0
15253 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15254 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15255 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15256 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15257 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15258 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15259 complain = complain_overflow_unsigned;
15260 if (howto->complain_on_overflow != complain)
15262 alt_howto = *howto;
15263 alt_howto.complain_on_overflow = complain;
15264 howto = &alt_howto;
15268 if (r_type == R_PPC64_REL16DX_HA)
15270 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15271 if (rel->r_offset + 4 > input_section->size)
15272 r = bfd_reloc_outofrange;
15275 relocation += addend;
15276 relocation -= (rel->r_offset
15277 + input_section->output_offset
15278 + input_section->output_section->vma);
15279 relocation = (bfd_signed_vma) relocation >> 16;
15280 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15282 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15283 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15285 if (relocation + 0x8000 > 0xffff)
15286 r = bfd_reloc_overflow;
15290 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15291 rel->r_offset, relocation, addend);
15293 if (r != bfd_reloc_ok)
15295 char *more_info = NULL;
15296 const char *reloc_name = howto->name;
15298 if (reloc_dest != DEST_NORMAL)
15300 more_info = bfd_malloc (strlen (reloc_name) + 8);
15301 if (more_info != NULL)
15303 strcpy (more_info, reloc_name);
15304 strcat (more_info, (reloc_dest == DEST_OPD
15305 ? " (OPD)" : " (stub)"));
15306 reloc_name = more_info;
15310 if (r == bfd_reloc_overflow)
15312 /* On code like "if (foo) foo();" don't report overflow
15313 on a branch to zero when foo is undefined. */
15315 && (reloc_dest == DEST_STUB
15317 && (h->elf.root.type == bfd_link_hash_undefweak
15318 || h->elf.root.type == bfd_link_hash_undefined)
15319 && is_branch_reloc (r_type))))
15320 info->callbacks->reloc_overflow (info, &h->elf.root,
15321 sym_name, reloc_name,
15323 input_bfd, input_section,
15328 info->callbacks->einfo
15329 /* xgettext:c-format */
15330 (_("%H: %s against `%T': error %d\n"),
15331 input_bfd, input_section, rel->r_offset,
15332 reloc_name, sym_name, (int) r);
15335 if (more_info != NULL)
15345 Elf_Internal_Shdr *rel_hdr;
15346 size_t deleted = rel - wrel;
15348 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15349 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15350 if (rel_hdr->sh_size == 0)
15352 /* It is too late to remove an empty reloc section. Leave
15354 ??? What is wrong with an empty section??? */
15355 rel_hdr->sh_size = rel_hdr->sh_entsize;
15358 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15359 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15360 input_section->reloc_count -= deleted;
15363 /* If we're emitting relocations, then shortly after this function
15364 returns, reloc offsets and addends for this section will be
15365 adjusted. Worse, reloc symbol indices will be for the output
15366 file rather than the input. Save a copy of the relocs for
15367 opd_entry_value. */
15368 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15371 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15372 rel = bfd_alloc (input_bfd, amt);
15373 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15374 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15377 memcpy (rel, relocs, amt);
15382 /* Adjust the value of any local symbols in opd sections. */
15385 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15386 const char *name ATTRIBUTE_UNUSED,
15387 Elf_Internal_Sym *elfsym,
15388 asection *input_sec,
15389 struct elf_link_hash_entry *h)
15391 struct _opd_sec_data *opd;
15398 opd = get_opd_info (input_sec);
15399 if (opd == NULL || opd->adjust == NULL)
15402 value = elfsym->st_value - input_sec->output_offset;
15403 if (!bfd_link_relocatable (info))
15404 value -= input_sec->output_section->vma;
15406 adjust = opd->adjust[OPD_NDX (value)];
15410 elfsym->st_value += adjust;
15414 /* Finish up dynamic symbol handling. We set the contents of various
15415 dynamic sections here. */
15418 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15419 struct bfd_link_info *info,
15420 struct elf_link_hash_entry *h,
15421 Elf_Internal_Sym *sym)
15423 struct ppc_link_hash_table *htab;
15424 struct plt_entry *ent;
15425 Elf_Internal_Rela rela;
15428 htab = ppc_hash_table (info);
15432 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15433 if (ent->plt.offset != (bfd_vma) -1)
15435 /* This symbol has an entry in the procedure linkage
15436 table. Set it up. */
15437 if (!htab->elf.dynamic_sections_created
15438 || h->dynindx == -1)
15440 BFD_ASSERT (h->type == STT_GNU_IFUNC
15442 && (h->root.type == bfd_link_hash_defined
15443 || h->root.type == bfd_link_hash_defweak));
15444 rela.r_offset = (htab->elf.iplt->output_section->vma
15445 + htab->elf.iplt->output_offset
15446 + ent->plt.offset);
15448 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15450 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15451 rela.r_addend = (h->root.u.def.value
15452 + h->root.u.def.section->output_offset
15453 + h->root.u.def.section->output_section->vma
15455 loc = (htab->elf.irelplt->contents
15456 + (htab->elf.irelplt->reloc_count++
15457 * sizeof (Elf64_External_Rela)));
15458 htab->local_ifunc_resolver = 1;
15462 rela.r_offset = (htab->elf.splt->output_section->vma
15463 + htab->elf.splt->output_offset
15464 + ent->plt.offset);
15465 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15466 rela.r_addend = ent->addend;
15467 loc = (htab->elf.srelplt->contents
15468 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15469 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
15470 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
15471 htab->maybe_local_ifunc_resolver = 1;
15473 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15475 if (!htab->opd_abi)
15477 if (!h->def_regular)
15479 /* Mark the symbol as undefined, rather than as
15480 defined in glink. Leave the value if there were
15481 any relocations where pointer equality matters
15482 (this is a clue for the dynamic linker, to make
15483 function pointer comparisons work between an
15484 application and shared library), otherwise set it
15486 sym->st_shndx = SHN_UNDEF;
15487 if (!h->pointer_equality_needed)
15489 else if (!h->ref_regular_nonweak)
15491 /* This breaks function pointer comparisons, but
15492 that is better than breaking tests for a NULL
15493 function pointer. */
15502 /* This symbol needs a copy reloc. Set it up. */
15505 if (h->dynindx == -1
15506 || (h->root.type != bfd_link_hash_defined
15507 && h->root.type != bfd_link_hash_defweak)
15508 || htab->elf.srelbss == NULL
15509 || htab->elf.sreldynrelro == NULL)
15512 rela.r_offset = (h->root.u.def.value
15513 + h->root.u.def.section->output_section->vma
15514 + h->root.u.def.section->output_offset);
15515 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15517 if (h->root.u.def.section == htab->elf.sdynrelro)
15518 srel = htab->elf.sreldynrelro;
15520 srel = htab->elf.srelbss;
15521 loc = srel->contents;
15522 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
15523 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15529 /* Used to decide how to sort relocs in an optimal manner for the
15530 dynamic linker, before writing them out. */
15532 static enum elf_reloc_type_class
15533 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15534 const asection *rel_sec,
15535 const Elf_Internal_Rela *rela)
15537 enum elf_ppc64_reloc_type r_type;
15538 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15540 if (rel_sec == htab->elf.irelplt)
15541 return reloc_class_ifunc;
15543 r_type = ELF64_R_TYPE (rela->r_info);
15546 case R_PPC64_RELATIVE:
15547 return reloc_class_relative;
15548 case R_PPC64_JMP_SLOT:
15549 return reloc_class_plt;
15551 return reloc_class_copy;
15553 return reloc_class_normal;
15557 /* Finish up the dynamic sections. */
15560 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15561 struct bfd_link_info *info)
15563 struct ppc_link_hash_table *htab;
15567 htab = ppc_hash_table (info);
15571 dynobj = htab->elf.dynobj;
15572 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15574 if (htab->elf.dynamic_sections_created)
15576 Elf64_External_Dyn *dyncon, *dynconend;
15578 if (sdyn == NULL || htab->elf.sgot == NULL)
15581 dyncon = (Elf64_External_Dyn *) sdyn->contents;
15582 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15583 for (; dyncon < dynconend; dyncon++)
15585 Elf_Internal_Dyn dyn;
15588 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15595 case DT_PPC64_GLINK:
15597 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15598 /* We stupidly defined DT_PPC64_GLINK to be the start
15599 of glink rather than the first entry point, which is
15600 what ld.so needs, and now have a bigger stub to
15601 support automatic multiple TOCs. */
15602 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
15606 s = bfd_get_section_by_name (output_bfd, ".opd");
15609 dyn.d_un.d_ptr = s->vma;
15613 if (htab->do_multi_toc && htab->multi_toc_needed)
15614 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15617 case DT_PPC64_OPDSZ:
15618 s = bfd_get_section_by_name (output_bfd, ".opd");
15621 dyn.d_un.d_val = s->size;
15625 s = htab->elf.splt;
15626 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15630 s = htab->elf.srelplt;
15631 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15635 dyn.d_un.d_val = htab->elf.srelplt->size;
15639 if (htab->local_ifunc_resolver)
15640 info->callbacks->einfo
15641 (_("%X%P: text relocations and GNU indirect "
15642 "functions will result in a segfault at runtime\n"));
15643 else if (htab->maybe_local_ifunc_resolver)
15644 info->callbacks->einfo
15645 (_("%P: warning: text relocations and GNU indirect "
15646 "functions may result in a segfault at runtime\n"));
15650 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15654 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15655 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
15657 /* Fill in the first entry in the global offset table.
15658 We use it to hold the link-time TOCbase. */
15659 bfd_put_64 (output_bfd,
15660 elf_gp (output_bfd) + TOC_BASE_OFF,
15661 htab->elf.sgot->contents);
15663 /* Set .got entry size. */
15664 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
15667 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15668 && htab->elf.splt->output_section != bfd_abs_section_ptr)
15670 /* Set .plt entry size. */
15671 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15672 = PLT_ENTRY_SIZE (htab);
15675 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15676 brlt ourselves if emitrelocations. */
15677 if (htab->brlt != NULL
15678 && htab->brlt->reloc_count != 0
15679 && !_bfd_elf_link_output_relocs (output_bfd,
15681 elf_section_data (htab->brlt)->rela.hdr,
15682 elf_section_data (htab->brlt)->relocs,
15686 if (htab->glink != NULL
15687 && htab->glink->reloc_count != 0
15688 && !_bfd_elf_link_output_relocs (output_bfd,
15690 elf_section_data (htab->glink)->rela.hdr,
15691 elf_section_data (htab->glink)->relocs,
15695 if (htab->glink_eh_frame != NULL
15696 && htab->glink_eh_frame->size != 0)
15700 asection *stub_sec;
15703 p = htab->glink_eh_frame->contents;
15704 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15705 for (stub_sec = htab->params->stub_bfd->sections;
15707 stub_sec = stub_sec->next)
15708 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
15714 /* Offset to stub section. */
15715 val = (stub_sec->output_section->vma
15716 + stub_sec->output_offset);
15717 val -= (htab->glink_eh_frame->output_section->vma
15718 + htab->glink_eh_frame->output_offset
15719 + (p - htab->glink_eh_frame->contents));
15720 if (val + 0x80000000 > 0xffffffff)
15722 info->callbacks->einfo
15723 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15727 bfd_put_32 (dynobj, val, p);
15729 /* stub section size. */
15731 /* Augmentation. */
15734 p += ((17 + align - 1) & -align) - 17;
15736 if (htab->glink != NULL && htab->glink->size != 0)
15742 /* Offset to .glink. */
15743 val = (htab->glink->output_section->vma
15744 + htab->glink->output_offset
15746 val -= (htab->glink_eh_frame->output_section->vma
15747 + htab->glink_eh_frame->output_offset
15748 + (p - htab->glink_eh_frame->contents));
15749 if (val + 0x80000000 > 0xffffffff)
15751 info->callbacks->einfo
15752 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15753 htab->glink->name);
15756 bfd_put_32 (dynobj, val, p);
15760 /* Augmentation. */
15764 p += ((24 + align - 1) & -align) - 24;
15767 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15768 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15769 htab->glink_eh_frame,
15770 htab->glink_eh_frame->contents))
15774 /* We need to handle writing out multiple GOT sections ourselves,
15775 since we didn't add them to DYNOBJ. We know dynobj is the first
15777 while ((dynobj = dynobj->link.next) != NULL)
15781 if (!is_ppc64_elf (dynobj))
15784 s = ppc64_elf_tdata (dynobj)->got;
15787 && s->output_section != bfd_abs_section_ptr
15788 && !bfd_set_section_contents (output_bfd, s->output_section,
15789 s->contents, s->output_offset,
15792 s = ppc64_elf_tdata (dynobj)->relgot;
15795 && s->output_section != bfd_abs_section_ptr
15796 && !bfd_set_section_contents (output_bfd, s->output_section,
15797 s->contents, s->output_offset,
15805 #include "elf64-target.h"
15807 /* FreeBSD support */
15809 #undef TARGET_LITTLE_SYM
15810 #undef TARGET_LITTLE_NAME
15812 #undef TARGET_BIG_SYM
15813 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
15814 #undef TARGET_BIG_NAME
15815 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15818 #define ELF_OSABI ELFOSABI_FREEBSD
15821 #define elf64_bed elf64_powerpc_fbsd_bed
15823 #include "elf64-target.h"