1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2019 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_prefix_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58 static bfd_vma opd_entry_value
59 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
61 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
62 #define TARGET_LITTLE_NAME "elf64-powerpcle"
63 #define TARGET_BIG_SYM powerpc_elf64_vec
64 #define TARGET_BIG_NAME "elf64-powerpc"
65 #define ELF_ARCH bfd_arch_powerpc
66 #define ELF_TARGET_ID PPC64_ELF_DATA
67 #define ELF_MACHINE_CODE EM_PPC64
68 #define ELF_MAXPAGESIZE 0x10000
69 #define ELF_COMMONPAGESIZE 0x1000
70 #define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
71 #define elf_info_to_howto ppc64_elf_info_to_howto
73 #define elf_backend_want_got_sym 0
74 #define elf_backend_want_plt_sym 0
75 #define elf_backend_plt_alignment 3
76 #define elf_backend_plt_not_loaded 1
77 #define elf_backend_got_header_size 8
78 #define elf_backend_want_dynrelro 1
79 #define elf_backend_can_gc_sections 1
80 #define elf_backend_can_refcount 1
81 #define elf_backend_rela_normal 1
82 #define elf_backend_dtrel_excludes_plt 1
83 #define elf_backend_default_execstack 0
85 #define bfd_elf64_mkobject ppc64_elf_mkobject
86 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
87 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
88 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
89 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
90 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
91 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
92 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
93 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
94 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
96 #define elf_backend_object_p ppc64_elf_object_p
97 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
98 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
99 #define elf_backend_write_core_note ppc64_elf_write_core_note
100 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
101 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
102 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
103 #define elf_backend_check_directives ppc64_elf_before_check_relocs
104 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
105 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
106 #define elf_backend_check_relocs ppc64_elf_check_relocs
107 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
108 #define elf_backend_gc_keep ppc64_elf_gc_keep
109 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
110 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
111 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
112 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
113 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
114 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
115 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
116 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
117 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
118 #define elf_backend_action_discarded ppc64_elf_action_discarded
119 #define elf_backend_relocate_section ppc64_elf_relocate_section
120 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
121 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
122 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
123 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
124 #define elf_backend_special_sections ppc64_elf_special_sections
125 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
126 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
127 #define elf_backend_get_reloc_section bfd_get_section_by_name
129 /* The name of the dynamic interpreter. This is put in the .interp
131 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
133 /* The size in bytes of an entry in the procedure linkage table. */
134 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
135 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
137 /* The initial size of the plt reserved for the dynamic linker. */
138 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
140 /* Offsets to some stack save slots. */
142 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
143 /* This one is dodgy. ELFv2 does not have a linker word, so use the
144 CR save slot. Used only by optimised __tls_get_addr call stub,
145 relying on __tls_get_addr_opt not saving CR.. */
146 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
148 /* TOC base pointers offset from start of TOC. */
149 #define TOC_BASE_OFF 0x8000
150 /* TOC base alignment. */
151 #define TOC_BASE_ALIGN 256
153 /* Offset of tp and dtp pointers from start of TLS block. */
154 #define TP_OFFSET 0x7000
155 #define DTP_OFFSET 0x8000
157 /* .plt call stub instructions. The normal stub is like this, but
158 sometimes the .plt entry crosses a 64k boundary and we need to
159 insert an addi to adjust r11. */
160 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
161 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
162 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
163 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
164 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
165 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
166 #define BCTR 0x4e800420 /* bctr */
168 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
169 #define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
170 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
171 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
172 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
174 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
175 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
176 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
177 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
178 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
179 #define BNECTR 0x4ca20420 /* bnectr+ */
180 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
182 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
183 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
184 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
186 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
187 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
188 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
190 #define LI_R11_0 0x39600000 /* li %r11,0 */
191 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
192 #define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
193 #define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
194 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
195 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
196 #define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
197 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
198 #define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
199 #define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
200 #define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
201 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
202 #define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
203 #define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
204 #define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
205 #define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
206 #define PADDI_R12_PC 0x0610000039800000ULL
207 #define PLD_R12_PC 0x04100000e5800000ULL
208 #define PNOP 0x0700000000000000ULL
210 /* __glink_PLTresolve stub instructions. We enter with the index in R0. */
211 #define GLINK_PLTRESOLVE_SIZE(htab) \
212 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
216 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
217 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
219 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
220 /* ld %2,(0b-1b)(%11) */
221 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
222 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
228 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
229 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
230 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
231 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
232 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
235 #define NOP 0x60000000
237 /* Some other nops. */
238 #define CROR_151515 0x4def7b82
239 #define CROR_313131 0x4ffffb82
241 /* .glink entries for the first 32k functions are two instructions. */
242 #define LI_R0_0 0x38000000 /* li %r0,0 */
243 #define B_DOT 0x48000000 /* b . */
245 /* After that, we need two instructions to load the index, followed by
247 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
248 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
250 /* Instructions used by the save and restore reg functions. */
251 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
252 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
253 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
254 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
255 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
256 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
257 #define LI_R12_0 0x39800000 /* li %r12,0 */
258 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
259 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
260 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
261 #define BLR 0x4e800020 /* blr */
263 /* Since .opd is an array of descriptors and each entry will end up
264 with identical R_PPC64_RELATIVE relocs, there is really no need to
265 propagate .opd relocs; The dynamic linker should be taught to
266 relocate .opd without reloc entries. */
267 #ifndef NO_OPD_RELOCS
268 #define NO_OPD_RELOCS 0
272 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
276 abiversion (bfd *abfd)
278 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
282 set_abiversion (bfd *abfd, int ver)
284 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
285 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
288 /* Relocation HOWTO's. */
289 /* Like other ELF RELA targets that don't apply multiple
290 field-altering relocations to the same localation, src_mask is
291 always zero and pcrel_offset is the same as pc_relative.
292 PowerPC can always use a zero bitpos, even when the field is not at
293 the LSB. For example, a REL24 could use rightshift=2, bisize=24
294 and bitpos=2 which matches the ABI description, or as we do here,
295 rightshift=0, bitsize=26 and bitpos=0. */
296 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
297 complain, special_func) \
298 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
299 complain_overflow_ ## complain, special_func, \
300 #type, FALSE, 0, mask, pc_relative)
302 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
304 static reloc_howto_type ppc64_elf_howto_raw[] =
306 /* This reloc does nothing. */
307 HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
308 bfd_elf_generic_reloc),
310 /* A standard 32 bit relocation. */
311 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
312 bfd_elf_generic_reloc),
314 /* An absolute 26 bit branch; the lower two bits must be zero.
315 FIXME: we don't check that, we just clear them. */
316 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
317 bfd_elf_generic_reloc),
319 /* A standard 16 bit relocation. */
320 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
321 bfd_elf_generic_reloc),
323 /* A 16 bit relocation without overflow. */
324 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
325 bfd_elf_generic_reloc),
327 /* Bits 16-31 of an address. */
328 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
329 bfd_elf_generic_reloc),
331 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
332 bits, treated as a signed number, is negative. */
333 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
336 /* An absolute 16 bit branch; the lower two bits must be zero.
337 FIXME: we don't check that, we just clear them. */
338 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
339 ppc64_elf_branch_reloc),
341 /* An absolute 16 bit branch, for which bit 10 should be set to
342 indicate that the branch is expected to be taken. The lower two
343 bits must be zero. */
344 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
345 ppc64_elf_brtaken_reloc),
347 /* An absolute 16 bit branch, for which bit 10 should be set to
348 indicate that the branch is not expected to be taken. The lower
349 two bits must be zero. */
350 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
351 ppc64_elf_brtaken_reloc),
353 /* A relative 26 bit branch; the lower two bits must be zero. */
354 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
355 ppc64_elf_branch_reloc),
357 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
358 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
359 ppc64_elf_branch_reloc),
361 /* A relative 16 bit branch; the lower two bits must be zero. */
362 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
363 ppc64_elf_branch_reloc),
365 /* A relative 16 bit branch. Bit 10 should be set to indicate that
366 the branch is expected to be taken. The lower two bits must be
368 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
369 ppc64_elf_brtaken_reloc),
371 /* A relative 16 bit branch. Bit 10 should be set to indicate that
372 the branch is not expected to be taken. The lower two bits must
374 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
375 ppc64_elf_brtaken_reloc),
377 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
379 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
380 ppc64_elf_unhandled_reloc),
382 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
384 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
385 ppc64_elf_unhandled_reloc),
387 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
389 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
390 ppc64_elf_unhandled_reloc),
392 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
394 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
395 ppc64_elf_unhandled_reloc),
397 /* This is used only by the dynamic linker. The symbol should exist
398 both in the object being run and in some shared library. The
399 dynamic linker copies the data addressed by the symbol from the
400 shared library into the object, because the object being
401 run has to have the data at some particular address. */
402 HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
403 ppc64_elf_unhandled_reloc),
405 /* Like R_PPC64_ADDR64, but used when setting global offset table
407 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
408 ppc64_elf_unhandled_reloc),
410 /* Created by the link editor. Marks a procedure linkage table
411 entry for a symbol. */
412 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
413 ppc64_elf_unhandled_reloc),
415 /* Used only by the dynamic linker. When the object is run, this
416 doubleword64 is set to the load address of the object, plus the
418 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
419 bfd_elf_generic_reloc),
421 /* Like R_PPC64_ADDR32, but may be unaligned. */
422 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
423 bfd_elf_generic_reloc),
425 /* Like R_PPC64_ADDR16, but may be unaligned. */
426 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
427 bfd_elf_generic_reloc),
429 /* 32-bit PC relative. */
430 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
431 bfd_elf_generic_reloc),
433 /* 32-bit relocation to the symbol's procedure linkage table. */
434 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
435 ppc64_elf_unhandled_reloc),
437 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
438 FIXME: R_PPC64_PLTREL32 not supported. */
439 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
440 ppc64_elf_unhandled_reloc),
442 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
444 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
445 ppc64_elf_unhandled_reloc),
447 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
449 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
450 ppc64_elf_unhandled_reloc),
452 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
454 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
455 ppc64_elf_unhandled_reloc),
457 /* 16-bit section relative relocation. */
458 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
459 ppc64_elf_sectoff_reloc),
461 /* Like R_PPC64_SECTOFF, but no overflow warning. */
462 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
463 ppc64_elf_sectoff_reloc),
465 /* 16-bit upper half section relative relocation. */
466 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
467 ppc64_elf_sectoff_reloc),
469 /* 16-bit upper half adjusted section relative relocation. */
470 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
471 ppc64_elf_sectoff_ha_reloc),
473 /* Like R_PPC64_REL24 without touching the two least significant bits. */
474 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
475 bfd_elf_generic_reloc),
477 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
479 /* A standard 64-bit relocation. */
480 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
481 bfd_elf_generic_reloc),
483 /* The bits 32-47 of an address. */
484 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
485 bfd_elf_generic_reloc),
487 /* The bits 32-47 of an address, plus 1 if the contents of the low
488 16 bits, treated as a signed number, is negative. */
489 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
492 /* The bits 48-63 of an address. */
493 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
494 bfd_elf_generic_reloc),
496 /* The bits 48-63 of an address, plus 1 if the contents of the low
497 16 bits, treated as a signed number, is negative. */
498 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
501 /* Like ADDR64, but may be unaligned. */
502 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
503 bfd_elf_generic_reloc),
505 /* 64-bit relative relocation. */
506 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
507 bfd_elf_generic_reloc),
509 /* 64-bit relocation to the symbol's procedure linkage table. */
510 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
511 ppc64_elf_unhandled_reloc),
513 /* 64-bit PC relative relocation to the symbol's procedure linkage
515 /* FIXME: R_PPC64_PLTREL64 not supported. */
516 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
517 ppc64_elf_unhandled_reloc),
519 /* 16 bit TOC-relative relocation. */
520 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
521 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
522 ppc64_elf_toc_reloc),
524 /* 16 bit TOC-relative relocation without overflow. */
525 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
526 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
527 ppc64_elf_toc_reloc),
529 /* 16 bit TOC-relative relocation, high 16 bits. */
530 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
531 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
532 ppc64_elf_toc_reloc),
534 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
535 contents of the low 16 bits, treated as a signed number, is
537 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
538 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
539 ppc64_elf_toc_ha_reloc),
541 /* 64-bit relocation; insert value of TOC base (.TOC.). */
542 /* R_PPC64_TOC 51 doubleword64 .TOC. */
543 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
544 ppc64_elf_toc64_reloc),
546 /* Like R_PPC64_GOT16, but also informs the link editor that the
547 value to relocate may (!) refer to a PLT entry which the link
548 editor (a) may replace with the symbol value. If the link editor
549 is unable to fully resolve the symbol, it may (b) create a PLT
550 entry and store the address to the new PLT entry in the GOT.
551 This permits lazy resolution of function symbols at run time.
552 The link editor may also skip all of this and just (c) emit a
553 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
554 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
555 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
556 ppc64_elf_unhandled_reloc),
558 /* Like R_PPC64_PLTGOT16, but without overflow. */
559 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
560 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
561 ppc64_elf_unhandled_reloc),
563 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
564 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
565 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
566 ppc64_elf_unhandled_reloc),
568 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
569 1 if the contents of the low 16 bits, treated as a signed number,
571 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
572 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
573 ppc64_elf_unhandled_reloc),
575 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
576 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
577 bfd_elf_generic_reloc),
579 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
580 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
581 bfd_elf_generic_reloc),
583 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
584 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
585 ppc64_elf_unhandled_reloc),
587 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
588 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
589 ppc64_elf_unhandled_reloc),
591 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
592 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
593 ppc64_elf_unhandled_reloc),
595 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
596 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
597 ppc64_elf_sectoff_reloc),
599 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
600 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
601 ppc64_elf_sectoff_reloc),
603 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
604 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
605 ppc64_elf_toc_reloc),
607 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
608 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
609 ppc64_elf_toc_reloc),
611 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
612 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
613 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
614 ppc64_elf_unhandled_reloc),
616 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
617 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
618 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
619 ppc64_elf_unhandled_reloc),
621 /* Marker relocs for TLS. */
622 HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
623 bfd_elf_generic_reloc),
625 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
626 bfd_elf_generic_reloc),
628 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
629 bfd_elf_generic_reloc),
631 /* Marker reloc for optimizing r2 save in prologue rather than on
632 each plt call stub. */
633 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
634 bfd_elf_generic_reloc),
636 /* Marker relocs on inline plt call instructions. */
637 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
638 bfd_elf_generic_reloc),
640 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
641 bfd_elf_generic_reloc),
643 /* Computes the load module index of the load module that contains the
644 definition of its TLS sym. */
645 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
646 ppc64_elf_unhandled_reloc),
648 /* Computes a dtv-relative displacement, the difference between the value
649 of sym+add and the base address of the thread-local storage block that
650 contains the definition of sym, minus 0x8000. */
651 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
652 ppc64_elf_unhandled_reloc),
654 /* A 16 bit dtprel reloc. */
655 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
656 ppc64_elf_unhandled_reloc),
658 /* Like DTPREL16, but no overflow. */
659 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
660 ppc64_elf_unhandled_reloc),
662 /* Like DTPREL16_LO, but next higher group of 16 bits. */
663 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
664 ppc64_elf_unhandled_reloc),
666 /* Like DTPREL16_HI, but adjust for low 16 bits. */
667 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
668 ppc64_elf_unhandled_reloc),
670 /* Like DTPREL16_HI, but next higher group of 16 bits. */
671 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
672 ppc64_elf_unhandled_reloc),
674 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
675 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
676 ppc64_elf_unhandled_reloc),
678 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
679 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
680 ppc64_elf_unhandled_reloc),
682 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
683 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
684 ppc64_elf_unhandled_reloc),
686 /* Like DTPREL16, but for insns with a DS field. */
687 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
688 ppc64_elf_unhandled_reloc),
690 /* Like DTPREL16_DS, but no overflow. */
691 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
692 ppc64_elf_unhandled_reloc),
694 /* Computes a tp-relative displacement, the difference between the value of
695 sym+add and the value of the thread pointer (r13). */
696 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
697 ppc64_elf_unhandled_reloc),
699 /* A 16 bit tprel reloc. */
700 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
701 ppc64_elf_unhandled_reloc),
703 /* Like TPREL16, but no overflow. */
704 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
705 ppc64_elf_unhandled_reloc),
707 /* Like TPREL16_LO, but next higher group of 16 bits. */
708 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
709 ppc64_elf_unhandled_reloc),
711 /* Like TPREL16_HI, but adjust for low 16 bits. */
712 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
713 ppc64_elf_unhandled_reloc),
715 /* Like TPREL16_HI, but next higher group of 16 bits. */
716 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
717 ppc64_elf_unhandled_reloc),
719 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
720 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
721 ppc64_elf_unhandled_reloc),
723 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
724 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
725 ppc64_elf_unhandled_reloc),
727 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
728 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
729 ppc64_elf_unhandled_reloc),
731 /* Like TPREL16, but for insns with a DS field. */
732 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
733 ppc64_elf_unhandled_reloc),
735 /* Like TPREL16_DS, but no overflow. */
736 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
737 ppc64_elf_unhandled_reloc),
739 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
740 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
741 to the first entry relative to the TOC base (r2). */
742 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
743 ppc64_elf_unhandled_reloc),
745 /* Like GOT_TLSGD16, but no overflow. */
746 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
747 ppc64_elf_unhandled_reloc),
749 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
750 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
751 ppc64_elf_unhandled_reloc),
753 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
754 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
755 ppc64_elf_unhandled_reloc),
757 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
758 with values (sym+add)@dtpmod and zero, and computes the offset to the
759 first entry relative to the TOC base (r2). */
760 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
761 ppc64_elf_unhandled_reloc),
763 /* Like GOT_TLSLD16, but no overflow. */
764 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
765 ppc64_elf_unhandled_reloc),
767 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
768 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
769 ppc64_elf_unhandled_reloc),
771 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
772 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
773 ppc64_elf_unhandled_reloc),
775 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
776 the offset to the entry relative to the TOC base (r2). */
777 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
778 ppc64_elf_unhandled_reloc),
780 /* Like GOT_DTPREL16_DS, but no overflow. */
781 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
782 ppc64_elf_unhandled_reloc),
784 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
785 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
786 ppc64_elf_unhandled_reloc),
788 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
789 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
790 ppc64_elf_unhandled_reloc),
792 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
793 offset to the entry relative to the TOC base (r2). */
794 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
795 ppc64_elf_unhandled_reloc),
797 /* Like GOT_TPREL16_DS, but no overflow. */
798 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
799 ppc64_elf_unhandled_reloc),
801 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
802 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
803 ppc64_elf_unhandled_reloc),
805 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
806 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
807 ppc64_elf_unhandled_reloc),
809 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
810 ppc64_elf_unhandled_reloc),
812 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
813 bfd_elf_generic_reloc),
815 /* A 16 bit relative relocation. */
816 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
817 bfd_elf_generic_reloc),
819 /* A 16 bit relative relocation without overflow. */
820 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
821 bfd_elf_generic_reloc),
823 /* The high order 16 bits of a relative address. */
824 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
825 bfd_elf_generic_reloc),
827 /* The high order 16 bits of a relative address, plus 1 if the contents of
828 the low 16 bits, treated as a signed number, is negative. */
829 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
832 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
833 bfd_elf_generic_reloc),
835 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
838 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
839 bfd_elf_generic_reloc),
841 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
844 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
845 bfd_elf_generic_reloc),
847 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
850 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
851 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
854 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
855 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
858 /* Like R_PPC64_ADDR16_HI, but no overflow. */
859 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
860 bfd_elf_generic_reloc),
862 /* Like R_PPC64_ADDR16_HA, but no overflow. */
863 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
866 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
867 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
868 ppc64_elf_unhandled_reloc),
870 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
871 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
872 ppc64_elf_unhandled_reloc),
874 /* Like R_PPC64_TPREL16_HI, but no overflow. */
875 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
876 ppc64_elf_unhandled_reloc),
878 /* Like R_PPC64_TPREL16_HA, but no overflow. */
879 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
880 ppc64_elf_unhandled_reloc),
882 /* Marker reloc on ELFv2 large-model function entry. */
883 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
884 bfd_elf_generic_reloc),
886 /* Like ADDR64, but use local entry point of function. */
887 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
888 bfd_elf_generic_reloc),
890 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, FALSE, dont,
891 bfd_elf_generic_reloc),
893 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, FALSE, dont,
894 bfd_elf_generic_reloc),
896 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, FALSE, dont,
897 bfd_elf_generic_reloc),
899 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
900 ppc64_elf_prefix_reloc),
902 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, dont,
903 ppc64_elf_prefix_reloc),
905 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
906 ppc64_elf_prefix_reloc),
908 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
909 ppc64_elf_prefix_reloc),
911 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
912 ppc64_elf_prefix_reloc),
914 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
915 ppc64_elf_unhandled_reloc),
917 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
918 ppc64_elf_unhandled_reloc),
920 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
921 ppc64_elf_unhandled_reloc),
923 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, FALSE, dont,
924 bfd_elf_generic_reloc),
926 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, FALSE, dont,
929 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, FALSE, dont,
930 bfd_elf_generic_reloc),
932 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, FALSE, dont,
935 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, TRUE, dont,
936 bfd_elf_generic_reloc),
938 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, TRUE, dont,
941 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, TRUE, dont,
942 bfd_elf_generic_reloc),
944 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, TRUE, dont,
947 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, FALSE, signed,
948 ppc64_elf_prefix_reloc),
950 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, TRUE, signed,
951 ppc64_elf_prefix_reloc),
953 /* GNU extension to record C++ vtable hierarchy. */
954 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
957 /* GNU extension to record C++ vtable member usage. */
958 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
963 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
967 ppc_howto_init (void)
969 unsigned int i, type;
971 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
973 type = ppc64_elf_howto_raw[i].type;
974 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
975 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
979 static reloc_howto_type *
980 ppc64_elf_reloc_type_lookup (bfd *abfd,
981 bfd_reloc_code_real_type code)
983 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
985 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
986 /* Initialize howto table if needed. */
992 /* xgettext:c-format */
993 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
995 bfd_set_error (bfd_error_bad_value);
998 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1000 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1002 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1004 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1006 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1008 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1010 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1012 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1014 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1016 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1018 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1020 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1022 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1024 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1026 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1028 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1030 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1032 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1034 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1036 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1038 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1040 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1042 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1044 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1046 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1048 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1050 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1052 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1054 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1056 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1058 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1060 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1062 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1064 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1066 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1068 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1070 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1072 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1074 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1076 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1078 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1080 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1082 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1084 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1086 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1088 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1090 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1092 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1094 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1096 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1098 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1100 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1102 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1104 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1106 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1108 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1110 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1112 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1114 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1116 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1118 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1120 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1122 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1124 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1126 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1128 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1130 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1132 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1134 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1136 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1138 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1140 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1142 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1144 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1146 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1148 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1150 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1152 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1154 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1156 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1158 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1160 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1162 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1164 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1166 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1168 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1170 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1172 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1174 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1176 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1178 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1180 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1182 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1184 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1186 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1188 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1190 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1192 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1194 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1196 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1198 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1200 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1202 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1204 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1206 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1208 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1210 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1212 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1214 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1216 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1218 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1220 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1222 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1224 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1226 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1228 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1230 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1232 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1234 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1236 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1238 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1240 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1242 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1244 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1246 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1248 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1250 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1252 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1254 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1256 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1258 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1260 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1262 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1264 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1266 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1268 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1270 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1272 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1274 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1276 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1278 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1282 return ppc64_elf_howto_table[r];
1285 static reloc_howto_type *
1286 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1291 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1292 if (ppc64_elf_howto_raw[i].name != NULL
1293 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1294 return &ppc64_elf_howto_raw[i];
1299 /* Set the howto pointer for a PowerPC ELF reloc. */
1302 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1303 Elf_Internal_Rela *dst)
1307 /* Initialize howto table if needed. */
1308 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1311 type = ELF64_R_TYPE (dst->r_info);
1312 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1314 /* xgettext:c-format */
1315 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1317 bfd_set_error (bfd_error_bad_value);
1320 cache_ptr->howto = ppc64_elf_howto_table[type];
1321 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1323 /* xgettext:c-format */
1324 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1326 bfd_set_error (bfd_error_bad_value);
1333 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1335 static bfd_reloc_status_type
1336 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1337 void *data, asection *input_section,
1338 bfd *output_bfd, char **error_message)
1340 enum elf_ppc64_reloc_type r_type;
1342 bfd_size_type octets;
1345 /* If this is a relocatable link (output_bfd test tells us), just
1346 call the generic function. Any adjustment will be done at final
1348 if (output_bfd != NULL)
1349 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1350 input_section, output_bfd, error_message);
1352 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1353 We won't actually be using the low bits, so trashing them
1355 r_type = reloc_entry->howto->type;
1356 if (r_type == R_PPC64_ADDR16_HIGHERA34
1357 || r_type == R_PPC64_ADDR16_HIGHESTA34
1358 || r_type == R_PPC64_REL16_HIGHERA34
1359 || r_type == R_PPC64_REL16_HIGHESTA34)
1360 reloc_entry->addend += 1ULL << 33;
1362 reloc_entry->addend += 1U << 15;
1363 if (r_type != R_PPC64_REL16DX_HA)
1364 return bfd_reloc_continue;
1367 if (!bfd_is_com_section (symbol->section))
1368 value = symbol->value;
1369 value += (reloc_entry->addend
1370 + symbol->section->output_offset
1371 + symbol->section->output_section->vma);
1372 value -= (reloc_entry->address
1373 + input_section->output_offset
1374 + input_section->output_section->vma);
1375 value = (bfd_signed_vma) value >> 16;
1377 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1378 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1380 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1381 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1382 if (value + 0x8000 > 0xffff)
1383 return bfd_reloc_overflow;
1384 return bfd_reloc_ok;
1387 static bfd_reloc_status_type
1388 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1389 void *data, asection *input_section,
1390 bfd *output_bfd, char **error_message)
1392 if (output_bfd != NULL)
1393 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1394 input_section, output_bfd, error_message);
1396 if (strcmp (symbol->section->name, ".opd") == 0
1397 && (symbol->section->owner->flags & DYNAMIC) == 0)
1399 bfd_vma dest = opd_entry_value (symbol->section,
1400 symbol->value + reloc_entry->addend,
1402 if (dest != (bfd_vma) -1)
1403 reloc_entry->addend = dest - (symbol->value
1404 + symbol->section->output_section->vma
1405 + symbol->section->output_offset);
1409 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1411 if (symbol->section->owner != abfd
1412 && symbol->section->owner != NULL
1413 && abiversion (symbol->section->owner) >= 2)
1417 for (i = 0; i < symbol->section->owner->symcount; ++i)
1419 asymbol *symdef = symbol->section->owner->outsymbols[i];
1421 if (strcmp (symdef->name, symbol->name) == 0)
1423 elfsym = (elf_symbol_type *) symdef;
1429 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1431 return bfd_reloc_continue;
1434 static bfd_reloc_status_type
1435 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1436 void *data, asection *input_section,
1437 bfd *output_bfd, char **error_message)
1440 enum elf_ppc64_reloc_type r_type;
1441 bfd_size_type octets;
1442 /* Assume 'at' branch hints. */
1443 bfd_boolean is_isa_v2 = TRUE;
1445 /* If this is a relocatable link (output_bfd test tells us), just
1446 call the generic function. Any adjustment will be done at final
1448 if (output_bfd != NULL)
1449 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1450 input_section, output_bfd, error_message);
1452 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1453 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1454 insn &= ~(0x01 << 21);
1455 r_type = reloc_entry->howto->type;
1456 if (r_type == R_PPC64_ADDR14_BRTAKEN
1457 || r_type == R_PPC64_REL14_BRTAKEN)
1458 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1462 /* Set 'a' bit. This is 0b00010 in BO field for branch
1463 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1464 for branch on CTR insns (BO == 1a00t or 1a01t). */
1465 if ((insn & (0x14 << 21)) == (0x04 << 21))
1467 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1477 if (!bfd_is_com_section (symbol->section))
1478 target = symbol->value;
1479 target += symbol->section->output_section->vma;
1480 target += symbol->section->output_offset;
1481 target += reloc_entry->addend;
1483 from = (reloc_entry->address
1484 + input_section->output_offset
1485 + input_section->output_section->vma);
1487 /* Invert 'y' bit if not the default. */
1488 if ((bfd_signed_vma) (target - from) < 0)
1491 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1493 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1494 input_section, output_bfd, error_message);
1497 static bfd_reloc_status_type
1498 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1499 void *data, asection *input_section,
1500 bfd *output_bfd, char **error_message)
1502 /* If this is a relocatable link (output_bfd test tells us), just
1503 call the generic function. Any adjustment will be done at final
1505 if (output_bfd != NULL)
1506 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1507 input_section, output_bfd, error_message);
1509 /* Subtract the symbol section base address. */
1510 reloc_entry->addend -= symbol->section->output_section->vma;
1511 return bfd_reloc_continue;
1514 static bfd_reloc_status_type
1515 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1516 void *data, asection *input_section,
1517 bfd *output_bfd, char **error_message)
1519 /* If this is a relocatable link (output_bfd test tells us), just
1520 call the generic function. Any adjustment will be done at final
1522 if (output_bfd != NULL)
1523 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1524 input_section, output_bfd, error_message);
1526 /* Subtract the symbol section base address. */
1527 reloc_entry->addend -= symbol->section->output_section->vma;
1529 /* Adjust the addend for sign extension of the low 16 bits. */
1530 reloc_entry->addend += 0x8000;
1531 return bfd_reloc_continue;
1534 static bfd_reloc_status_type
1535 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1536 void *data, asection *input_section,
1537 bfd *output_bfd, char **error_message)
1541 /* If this is a relocatable link (output_bfd test tells us), just
1542 call the generic function. Any adjustment will be done at final
1544 if (output_bfd != NULL)
1545 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1546 input_section, output_bfd, error_message);
1548 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1550 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1552 /* Subtract the TOC base address. */
1553 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1554 return bfd_reloc_continue;
1557 static bfd_reloc_status_type
1558 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1559 void *data, asection *input_section,
1560 bfd *output_bfd, char **error_message)
1564 /* If this is a relocatable link (output_bfd test tells us), just
1565 call the generic function. Any adjustment will be done at final
1567 if (output_bfd != NULL)
1568 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1569 input_section, output_bfd, error_message);
1571 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1573 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1575 /* Subtract the TOC base address. */
1576 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1578 /* Adjust the addend for sign extension of the low 16 bits. */
1579 reloc_entry->addend += 0x8000;
1580 return bfd_reloc_continue;
1583 static bfd_reloc_status_type
1584 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1585 void *data, asection *input_section,
1586 bfd *output_bfd, char **error_message)
1589 bfd_size_type octets;
1591 /* If this is a relocatable link (output_bfd test tells us), just
1592 call the generic function. Any adjustment will be done at final
1594 if (output_bfd != NULL)
1595 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1596 input_section, output_bfd, error_message);
1598 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1600 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1602 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1603 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1604 return bfd_reloc_ok;
1607 static bfd_reloc_status_type
1608 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1609 void *data, asection *input_section,
1610 bfd *output_bfd, char **error_message)
1615 if (output_bfd != NULL)
1616 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1617 input_section, output_bfd, error_message);
1619 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1621 insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
1623 targ = (symbol->section->output_section->vma
1624 + symbol->section->output_offset
1625 + reloc_entry->addend);
1626 if (!bfd_is_com_section (symbol->section))
1627 targ += symbol->value;
1628 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1630 if (reloc_entry->howto->pc_relative)
1632 bfd_vma from = (reloc_entry->address
1633 + input_section->output_offset
1634 + input_section->output_section->vma);
1637 targ >>= reloc_entry->howto->rightshift;
1638 insn &= ~reloc_entry->howto->dst_mask;
1639 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1640 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
1641 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
1642 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1643 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1644 >= 1ULL << reloc_entry->howto->bitsize))
1645 return bfd_reloc_overflow;
1646 return bfd_reloc_ok;
1649 static bfd_reloc_status_type
1650 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1651 void *data, asection *input_section,
1652 bfd *output_bfd, char **error_message)
1654 /* If this is a relocatable link (output_bfd test tells us), just
1655 call the generic function. Any adjustment will be done at final
1657 if (output_bfd != NULL)
1658 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1659 input_section, output_bfd, error_message);
1661 if (error_message != NULL)
1663 static char buf[60];
1664 sprintf (buf, "generic linker can't handle %s",
1665 reloc_entry->howto->name);
1666 *error_message = buf;
1668 return bfd_reloc_dangerous;
1671 /* Track GOT entries needed for a given symbol. We might need more
1672 than one got entry per symbol. */
1675 struct got_entry *next;
1677 /* The symbol addend that we'll be placing in the GOT. */
1680 /* Unlike other ELF targets, we use separate GOT entries for the same
1681 symbol referenced from different input files. This is to support
1682 automatic multiple TOC/GOT sections, where the TOC base can vary
1683 from one input file to another. After partitioning into TOC groups
1684 we merge entries within the group.
1686 Point to the BFD owning this GOT entry. */
1689 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1690 TLS_TPREL or TLS_DTPREL for tls entries. */
1691 unsigned char tls_type;
1693 /* Non-zero if got.ent points to real entry. */
1694 unsigned char is_indirect;
1696 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1699 bfd_signed_vma refcount;
1701 struct got_entry *ent;
1705 /* The same for PLT. */
1708 struct plt_entry *next;
1714 bfd_signed_vma refcount;
1719 struct ppc64_elf_obj_tdata
1721 struct elf_obj_tdata elf;
1723 /* Shortcuts to dynamic linker sections. */
1727 /* Used during garbage collection. We attach global symbols defined
1728 on removed .opd entries to this section so that the sym is removed. */
1729 asection *deleted_section;
1731 /* TLS local dynamic got entry handling. Support for multiple GOT
1732 sections means we potentially need one of these for each input bfd. */
1733 struct got_entry tlsld_got;
1737 /* A copy of relocs before they are modified for --emit-relocs. */
1738 Elf_Internal_Rela *relocs;
1740 /* Section contents. */
1744 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1745 the reloc to be in the range -32768 to 32767. */
1746 unsigned int has_small_toc_reloc : 1;
1748 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1749 instruction not one we handle. */
1750 unsigned int unexpected_toc_insn : 1;
1752 /* Set if got relocs that can be optimised are present in this file. */
1753 unsigned int has_gotrel : 1;
1756 #define ppc64_elf_tdata(bfd) \
1757 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1759 #define ppc64_tlsld_got(bfd) \
1760 (&ppc64_elf_tdata (bfd)->tlsld_got)
1762 #define is_ppc64_elf(bfd) \
1763 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1764 && elf_object_id (bfd) == PPC64_ELF_DATA)
1766 /* Override the generic function because we store some extras. */
1769 ppc64_elf_mkobject (bfd *abfd)
1771 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1775 /* Fix bad default arch selected for a 64 bit input bfd when the
1776 default is 32 bit. Also select arch based on apuinfo. */
1779 ppc64_elf_object_p (bfd *abfd)
1781 if (!abfd->arch_info->the_default)
1784 if (abfd->arch_info->bits_per_word == 32)
1786 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1788 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1790 /* Relies on arch after 32 bit default being 64 bit default. */
1791 abfd->arch_info = abfd->arch_info->next;
1792 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1795 return _bfd_elf_ppc_set_arch (abfd);
1798 /* Support for core dump NOTE sections. */
1801 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1803 size_t offset, size;
1805 if (note->descsz != 504)
1809 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1812 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1818 /* Make a ".reg/999" section. */
1819 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1820 size, note->descpos + offset);
1824 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1826 if (note->descsz != 136)
1829 elf_tdata (abfd)->core->pid
1830 = bfd_get_32 (abfd, note->descdata + 24);
1831 elf_tdata (abfd)->core->program
1832 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1833 elf_tdata (abfd)->core->command
1834 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1840 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1850 char data[136] ATTRIBUTE_NONSTRING;
1853 va_start (ap, note_type);
1854 memset (data, 0, sizeof (data));
1855 strncpy (data + 40, va_arg (ap, const char *), 16);
1856 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1858 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1859 -Wstringop-truncation:
1860 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1862 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1864 strncpy (data + 56, va_arg (ap, const char *), 80);
1865 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1869 return elfcore_write_note (abfd, buf, bufsiz,
1870 "CORE", note_type, data, sizeof (data));
1881 va_start (ap, note_type);
1882 memset (data, 0, 112);
1883 pid = va_arg (ap, long);
1884 bfd_put_32 (abfd, pid, data + 32);
1885 cursig = va_arg (ap, int);
1886 bfd_put_16 (abfd, cursig, data + 12);
1887 greg = va_arg (ap, const void *);
1888 memcpy (data + 112, greg, 384);
1889 memset (data + 496, 0, 8);
1891 return elfcore_write_note (abfd, buf, bufsiz,
1892 "CORE", note_type, data, sizeof (data));
1897 /* Add extra PPC sections. */
1899 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1901 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1902 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1903 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1904 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1905 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1906 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1907 { NULL, 0, 0, 0, 0 }
1910 enum _ppc64_sec_type {
1916 struct _ppc64_elf_section_data
1918 struct bfd_elf_section_data elf;
1922 /* An array with one entry for each opd function descriptor,
1923 and some spares since opd entries may be either 16 or 24 bytes. */
1924 #define OPD_NDX(OFF) ((OFF) >> 4)
1925 struct _opd_sec_data
1927 /* Points to the function code section for local opd entries. */
1928 asection **func_sec;
1930 /* After editing .opd, adjust references to opd local syms. */
1934 /* An array for toc sections, indexed by offset/8. */
1935 struct _toc_sec_data
1937 /* Specifies the relocation symbol index used at a given toc offset. */
1940 /* And the relocation addend. */
1945 enum _ppc64_sec_type sec_type:2;
1947 /* Flag set when small branches are detected. Used to
1948 select suitable defaults for the stub group size. */
1949 unsigned int has_14bit_branch:1;
1951 /* Flag set when PLTCALL relocs are detected. */
1952 unsigned int has_pltcall:1;
1954 /* Flag set when section has GOT relocations that can be optimised. */
1955 unsigned int has_gotrel:1;
1958 #define ppc64_elf_section_data(sec) \
1959 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
1962 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
1964 if (!sec->used_by_bfd)
1966 struct _ppc64_elf_section_data *sdata;
1967 bfd_size_type amt = sizeof (*sdata);
1969 sdata = bfd_zalloc (abfd, amt);
1972 sec->used_by_bfd = sdata;
1975 return _bfd_elf_new_section_hook (abfd, sec);
1978 static struct _opd_sec_data *
1979 get_opd_info (asection * sec)
1982 && ppc64_elf_section_data (sec) != NULL
1983 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
1984 return &ppc64_elf_section_data (sec)->u.opd;
1988 /* Parameters for the qsort hook. */
1989 static bfd_boolean synthetic_relocatable;
1990 static asection *synthetic_opd;
1992 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
1995 compare_symbols (const void *ap, const void *bp)
1997 const asymbol *a = *(const asymbol **) ap;
1998 const asymbol *b = *(const asymbol **) bp;
2000 /* Section symbols first. */
2001 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2003 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2006 /* then .opd symbols. */
2007 if (synthetic_opd != NULL)
2009 if (strcmp (a->section->name, ".opd") == 0
2010 && strcmp (b->section->name, ".opd") != 0)
2012 if (strcmp (a->section->name, ".opd") != 0
2013 && strcmp (b->section->name, ".opd") == 0)
2017 /* then other code symbols. */
2018 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2019 == (SEC_CODE | SEC_ALLOC))
2020 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2021 != (SEC_CODE | SEC_ALLOC)))
2024 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2025 != (SEC_CODE | SEC_ALLOC))
2026 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2027 == (SEC_CODE | SEC_ALLOC)))
2030 if (synthetic_relocatable)
2032 if (a->section->id < b->section->id)
2035 if (a->section->id > b->section->id)
2039 if (a->value + a->section->vma < b->value + b->section->vma)
2042 if (a->value + a->section->vma > b->value + b->section->vma)
2045 /* For syms with the same value, prefer strong dynamic global function
2046 syms over other syms. */
2047 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2050 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2053 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2056 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2059 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2062 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2065 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2068 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2074 /* Search SYMS for a symbol of the given VALUE. */
2077 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
2081 if (id == (unsigned) -1)
2085 mid = (lo + hi) >> 1;
2086 if (syms[mid]->value + syms[mid]->section->vma < value)
2088 else if (syms[mid]->value + syms[mid]->section->vma > value)
2098 mid = (lo + hi) >> 1;
2099 if (syms[mid]->section->id < id)
2101 else if (syms[mid]->section->id > id)
2103 else if (syms[mid]->value < value)
2105 else if (syms[mid]->value > value)
2115 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2117 bfd_vma vma = *(bfd_vma *) ptr;
2118 return ((section->flags & SEC_ALLOC) != 0
2119 && section->vma <= vma
2120 && vma < section->vma + section->size);
2123 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2124 entry syms. Also generate @plt symbols for the glink branch table.
2125 Returns count of synthetic symbols in RET or -1 on error. */
2128 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2129 long static_count, asymbol **static_syms,
2130 long dyn_count, asymbol **dyn_syms,
2136 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2137 asection *opd = NULL;
2138 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2140 int abi = abiversion (abfd);
2146 opd = bfd_get_section_by_name (abfd, ".opd");
2147 if (opd == NULL && abi == 1)
2159 symcount = static_count;
2161 symcount += dyn_count;
2165 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2169 if (!relocatable && static_count != 0 && dyn_count != 0)
2171 /* Use both symbol tables. */
2172 memcpy (syms, static_syms, static_count * sizeof (*syms));
2173 memcpy (syms + static_count, dyn_syms,
2174 (dyn_count + 1) * sizeof (*syms));
2176 else if (!relocatable && static_count == 0)
2177 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2179 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2181 /* Trim uninteresting symbols. Interesting symbols are section,
2182 function, and notype symbols. */
2183 for (i = 0, j = 0; i < symcount; ++i)
2184 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2185 | BSF_RELC | BSF_SRELC)) == 0)
2186 syms[j++] = syms[i];
2189 synthetic_relocatable = relocatable;
2190 synthetic_opd = opd;
2191 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2193 if (!relocatable && symcount > 1)
2195 /* Trim duplicate syms, since we may have merged the normal
2196 and dynamic symbols. Actually, we only care about syms
2197 that have different values, so trim any with the same
2198 value. Don't consider ifunc and ifunc resolver symbols
2199 duplicates however, because GDB wants to know whether a
2200 text symbol is an ifunc resolver. */
2201 for (i = 1, j = 1; i < symcount; ++i)
2203 const asymbol *s0 = syms[i - 1];
2204 const asymbol *s1 = syms[i];
2206 if ((s0->value + s0->section->vma
2207 != s1->value + s1->section->vma)
2208 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2209 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2210 syms[j++] = syms[i];
2216 /* Note that here and in compare_symbols we can't compare opd and
2217 sym->section directly. With separate debug info files, the
2218 symbols will be extracted from the debug file while abfd passed
2219 to this function is the real binary. */
2220 if (strcmp (syms[i]->section->name, ".opd") == 0)
2224 for (; i < symcount; ++i)
2225 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2226 | SEC_THREAD_LOCAL))
2227 != (SEC_CODE | SEC_ALLOC))
2228 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2232 for (; i < symcount; ++i)
2233 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2237 for (; i < symcount; ++i)
2238 if (strcmp (syms[i]->section->name, ".opd") != 0)
2242 for (; i < symcount; ++i)
2243 if (((syms[i]->section->flags
2244 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2245 != (SEC_CODE | SEC_ALLOC))
2253 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2258 if (opdsymend == secsymend)
2261 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2262 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2266 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2273 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2277 while (r < opd->relocation + relcount
2278 && r->address < syms[i]->value + opd->vma)
2281 if (r == opd->relocation + relcount)
2284 if (r->address != syms[i]->value + opd->vma)
2287 if (r->howto->type != R_PPC64_ADDR64)
2290 sym = *r->sym_ptr_ptr;
2291 if (!sym_exists_at (syms, opdsymend, symcount,
2292 sym->section->id, sym->value + r->addend))
2295 size += sizeof (asymbol);
2296 size += strlen (syms[i]->name) + 2;
2302 s = *ret = bfd_malloc (size);
2309 names = (char *) (s + count);
2311 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2315 while (r < opd->relocation + relcount
2316 && r->address < syms[i]->value + opd->vma)
2319 if (r == opd->relocation + relcount)
2322 if (r->address != syms[i]->value + opd->vma)
2325 if (r->howto->type != R_PPC64_ADDR64)
2328 sym = *r->sym_ptr_ptr;
2329 if (!sym_exists_at (syms, opdsymend, symcount,
2330 sym->section->id, sym->value + r->addend))
2335 s->flags |= BSF_SYNTHETIC;
2336 s->section = sym->section;
2337 s->value = sym->value + r->addend;
2340 len = strlen (syms[i]->name);
2341 memcpy (names, syms[i]->name, len + 1);
2343 /* Have udata.p point back to the original symbol this
2344 synthetic symbol was derived from. */
2345 s->udata.p = syms[i];
2352 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2353 bfd_byte *contents = NULL;
2355 size_t plt_count = 0;
2356 bfd_vma glink_vma = 0, resolv_vma = 0;
2357 asection *dynamic, *glink = NULL, *relplt = NULL;
2360 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2362 free_contents_and_exit_err:
2364 free_contents_and_exit:
2371 for (i = secsymend; i < opdsymend; ++i)
2375 /* Ignore bogus symbols. */
2376 if (syms[i]->value > opd->size - 8)
2379 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2380 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2383 size += sizeof (asymbol);
2384 size += strlen (syms[i]->name) + 2;
2388 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2390 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2392 bfd_byte *dynbuf, *extdyn, *extdynend;
2394 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2396 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2397 goto free_contents_and_exit_err;
2399 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2400 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2403 extdynend = extdyn + dynamic->size;
2404 for (; extdyn < extdynend; extdyn += extdynsize)
2406 Elf_Internal_Dyn dyn;
2407 (*swap_dyn_in) (abfd, extdyn, &dyn);
2409 if (dyn.d_tag == DT_NULL)
2412 if (dyn.d_tag == DT_PPC64_GLINK)
2414 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2415 See comment in ppc64_elf_finish_dynamic_sections. */
2416 glink_vma = dyn.d_un.d_val + 8 * 4;
2417 /* The .glink section usually does not survive the final
2418 link; search for the section (usually .text) where the
2419 glink stubs now reside. */
2420 glink = bfd_sections_find_if (abfd, section_covers_vma,
2431 /* Determine __glink trampoline by reading the relative branch
2432 from the first glink stub. */
2434 unsigned int off = 0;
2436 while (bfd_get_section_contents (abfd, glink, buf,
2437 glink_vma + off - glink->vma, 4))
2439 unsigned int insn = bfd_get_32 (abfd, buf);
2441 if ((insn & ~0x3fffffc) == 0)
2444 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2453 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2455 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2458 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2459 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
2460 goto free_contents_and_exit_err;
2462 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2463 size += plt_count * sizeof (asymbol);
2465 p = relplt->relocation;
2466 for (i = 0; i < plt_count; i++, p++)
2468 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2470 size += sizeof ("+0x") - 1 + 16;
2476 goto free_contents_and_exit;
2477 s = *ret = bfd_malloc (size);
2479 goto free_contents_and_exit_err;
2481 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2483 for (i = secsymend; i < opdsymend; ++i)
2487 if (syms[i]->value > opd->size - 8)
2490 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2491 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2495 asection *sec = abfd->sections;
2502 size_t mid = (lo + hi) >> 1;
2503 if (syms[mid]->section->vma < ent)
2505 else if (syms[mid]->section->vma > ent)
2509 sec = syms[mid]->section;
2514 if (lo >= hi && lo > codesecsym)
2515 sec = syms[lo - 1]->section;
2517 for (; sec != NULL; sec = sec->next)
2521 /* SEC_LOAD may not be set if SEC is from a separate debug
2523 if ((sec->flags & SEC_ALLOC) == 0)
2525 if ((sec->flags & SEC_CODE) != 0)
2528 s->flags |= BSF_SYNTHETIC;
2529 s->value = ent - s->section->vma;
2532 len = strlen (syms[i]->name);
2533 memcpy (names, syms[i]->name, len + 1);
2535 /* Have udata.p point back to the original symbol this
2536 synthetic symbol was derived from. */
2537 s->udata.p = syms[i];
2543 if (glink != NULL && relplt != NULL)
2547 /* Add a symbol for the main glink trampoline. */
2548 memset (s, 0, sizeof *s);
2550 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2552 s->value = resolv_vma - glink->vma;
2554 memcpy (names, "__glink_PLTresolve",
2555 sizeof ("__glink_PLTresolve"));
2556 names += sizeof ("__glink_PLTresolve");
2561 /* FIXME: It would be very much nicer to put sym@plt on the
2562 stub rather than on the glink branch table entry. The
2563 objdump disassembler would then use a sensible symbol
2564 name on plt calls. The difficulty in doing so is
2565 a) finding the stubs, and,
2566 b) matching stubs against plt entries, and,
2567 c) there can be multiple stubs for a given plt entry.
2569 Solving (a) could be done by code scanning, but older
2570 ppc64 binaries used different stubs to current code.
2571 (b) is the tricky one since you need to known the toc
2572 pointer for at least one function that uses a pic stub to
2573 be able to calculate the plt address referenced.
2574 (c) means gdb would need to set multiple breakpoints (or
2575 find the glink branch itself) when setting breakpoints
2576 for pending shared library loads. */
2577 p = relplt->relocation;
2578 for (i = 0; i < plt_count; i++, p++)
2582 *s = **p->sym_ptr_ptr;
2583 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2584 we are defining a symbol, ensure one of them is set. */
2585 if ((s->flags & BSF_LOCAL) == 0)
2586 s->flags |= BSF_GLOBAL;
2587 s->flags |= BSF_SYNTHETIC;
2589 s->value = glink_vma - glink->vma;
2592 len = strlen ((*p->sym_ptr_ptr)->name);
2593 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2597 memcpy (names, "+0x", sizeof ("+0x") - 1);
2598 names += sizeof ("+0x") - 1;
2599 bfd_sprintf_vma (abfd, names, p->addend);
2600 names += strlen (names);
2602 memcpy (names, "@plt", sizeof ("@plt"));
2603 names += sizeof ("@plt");
2623 /* The following functions are specific to the ELF linker, while
2624 functions above are used generally. Those named ppc64_elf_* are
2625 called by the main ELF linker code. They appear in this file more
2626 or less in the order in which they are called. eg.
2627 ppc64_elf_check_relocs is called early in the link process,
2628 ppc64_elf_finish_dynamic_sections is one of the last functions
2631 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2632 functions have both a function code symbol and a function descriptor
2633 symbol. A call to foo in a relocatable object file looks like:
2640 The function definition in another object file might be:
2644 . .quad .TOC.@tocbase
2650 When the linker resolves the call during a static link, the branch
2651 unsurprisingly just goes to .foo and the .opd information is unused.
2652 If the function definition is in a shared library, things are a little
2653 different: The call goes via a plt call stub, the opd information gets
2654 copied to the plt, and the linker patches the nop.
2662 . std 2,40(1) # in practice, the call stub
2663 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2664 . addi 11,11,Lfoo@toc@l # this is the general idea
2672 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2674 The "reloc ()" notation is supposed to indicate that the linker emits
2675 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2678 What are the difficulties here? Well, firstly, the relocations
2679 examined by the linker in check_relocs are against the function code
2680 sym .foo, while the dynamic relocation in the plt is emitted against
2681 the function descriptor symbol, foo. Somewhere along the line, we need
2682 to carefully copy dynamic link information from one symbol to the other.
2683 Secondly, the generic part of the elf linker will make .foo a dynamic
2684 symbol as is normal for most other backends. We need foo dynamic
2685 instead, at least for an application final link. However, when
2686 creating a shared library containing foo, we need to have both symbols
2687 dynamic so that references to .foo are satisfied during the early
2688 stages of linking. Otherwise the linker might decide to pull in a
2689 definition from some other object, eg. a static library.
2691 Update: As of August 2004, we support a new convention. Function
2692 calls may use the function descriptor symbol, ie. "bl foo". This
2693 behaves exactly as "bl .foo". */
2695 /* Of those relocs that might be copied as dynamic relocs, this
2696 function selects those that must be copied when linking a shared
2697 library or PIE, even when the symbol is local. */
2700 must_be_dyn_reloc (struct bfd_link_info *info,
2701 enum elf_ppc64_reloc_type r_type)
2706 /* Only relative relocs can be resolved when the object load
2707 address isn't fixed. DTPREL64 is excluded because the
2708 dynamic linker needs to differentiate global dynamic from
2709 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2717 case R_PPC64_TPREL16:
2718 case R_PPC64_TPREL16_LO:
2719 case R_PPC64_TPREL16_HI:
2720 case R_PPC64_TPREL16_HA:
2721 case R_PPC64_TPREL16_DS:
2722 case R_PPC64_TPREL16_LO_DS:
2723 case R_PPC64_TPREL16_HIGH:
2724 case R_PPC64_TPREL16_HIGHA:
2725 case R_PPC64_TPREL16_HIGHER:
2726 case R_PPC64_TPREL16_HIGHERA:
2727 case R_PPC64_TPREL16_HIGHEST:
2728 case R_PPC64_TPREL16_HIGHESTA:
2729 case R_PPC64_TPREL64:
2730 /* These relocations are relative but in a shared library the
2731 linker doesn't know the thread pointer base. */
2732 return bfd_link_dll (info);
2736 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2737 copying dynamic variables from a shared lib into an app's dynbss
2738 section, and instead use a dynamic relocation to point into the
2739 shared lib. With code that gcc generates, it's vital that this be
2740 enabled; In the PowerPC64 ABI, the address of a function is actually
2741 the address of a function descriptor, which resides in the .opd
2742 section. gcc uses the descriptor directly rather than going via the
2743 GOT as some other ABI's do, which means that initialized function
2744 pointers must reference the descriptor. Thus, a function pointer
2745 initialized to the address of a function in a shared library will
2746 either require a copy reloc, or a dynamic reloc. Using a copy reloc
2747 redefines the function descriptor symbol to point to the copy. This
2748 presents a problem as a plt entry for that function is also
2749 initialized from the function descriptor symbol and the copy reloc
2750 may not be initialized first. */
2751 #define ELIMINATE_COPY_RELOCS 1
2753 /* Section name for stubs is the associated section name plus this
2755 #define STUB_SUFFIX ".stub"
2758 ppc_stub_long_branch:
2759 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2760 destination, but a 24 bit branch in a stub section will reach.
2763 ppc_stub_plt_branch:
2764 Similar to the above, but a 24 bit branch in the stub section won't
2765 reach its destination.
2766 . addis %r11,%r2,xxx@toc@ha
2767 . ld %r12,xxx@toc@l(%r11)
2772 Used to call a function in a shared library. If it so happens that
2773 the plt entry referenced crosses a 64k boundary, then an extra
2774 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2775 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
2776 . addis %r11,%r2,xxx@toc@ha
2777 . ld %r12,xxx+0@toc@l(%r11)
2779 . ld %r2,xxx+8@toc@l(%r11)
2780 . ld %r11,xxx+16@toc@l(%r11)
2783 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2784 code to adjust the value and save r2 to support multiple toc sections.
2785 A ppc_stub_long_branch with an r2 offset looks like:
2787 . addis %r2,%r2,off@ha
2788 . addi %r2,%r2,off@l
2791 A ppc_stub_plt_branch with an r2 offset looks like:
2793 . addis %r11,%r2,xxx@toc@ha
2794 . ld %r12,xxx@toc@l(%r11)
2795 . addis %r2,%r2,off@ha
2796 . addi %r2,%r2,off@l
2800 All of the above stubs are shown as their ELFv1 variants. ELFv2
2801 variants exist too, simpler for plt calls since a new toc pointer
2802 and static chain are not loaded by the stub. In addition, ELFv2
2803 has some more complex stubs to handle calls marked with NOTOC
2804 relocs from functions where r2 is not a valid toc pointer. These
2805 come in two flavours, the ones shown below, and _both variants that
2806 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2807 one call is from a function where r2 is used as the toc pointer but
2808 needs a toc adjusting stub for small-model multi-toc, and another
2809 call is from a function where r2 is not valid.
2810 ppc_stub_long_branch_notoc:
2816 . addis %r12,%r11,dest-1b@ha
2817 . addi %r12,%r12,dest-1b@l
2820 ppc_stub_plt_branch_notoc:
2826 . lis %r12,xxx-1b@highest
2827 . ori %r12,%r12,xxx-1b@higher
2829 . oris %r12,%r12,xxx-1b@high
2830 . ori %r12,%r12,xxx-1b@l
2831 . add %r12,%r11,%r12
2835 ppc_stub_plt_call_notoc:
2841 . lis %r12,xxx-1b@highest
2842 . ori %r12,%r12,xxx-1b@higher
2844 . oris %r12,%r12,xxx-1b@high
2845 . ori %r12,%r12,xxx-1b@l
2846 . ldx %r12,%r11,%r12
2850 There are also ELFv1 powerxx variants of these stubs.
2851 ppc_stub_long_branch_notoc:
2852 . paddi %r12,dest@pcrel
2854 ppc_stub_plt_branch_notoc:
2855 . lis %r11,(dest-1f)@highesta34
2856 . ori %r11,%r11,(dest-1f)@highera34
2858 . 1: paddi %r12,dest@pcrel
2859 . add %r12,%r11,%r12
2862 ppc_stub_plt_call_notoc:
2863 . lis %r11,(xxx-1f)@highesta34
2864 . ori %r11,%r11,(xxx-1f)@highera34
2866 . 1: paddi %r12,xxx@pcrel
2867 . ldx %r12,%r11,%r12
2871 In cases where the high instructions would add zero, they are
2872 omitted and following instructions modified in some cases.
2873 For example, a powerxx ppc_stub_plt_call_notoc might simplify down
2875 . pld %r12,xxx@pcrel
2879 For a given stub group (a set of sections all using the same toc
2880 pointer value) there will be just one stub type used for any
2881 particular function symbol. For example, if printf is called from
2882 code with the tocsave optimization (ie. r2 saved in function
2883 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2884 and from other code without the tocsave optimization requiring a
2885 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2886 type will be created. Calls with the tocsave optimization will
2887 enter this stub after the instruction saving r2. A similar
2888 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2889 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2890 to call an external function like printf. If other calls to printf
2891 require a ppc_stub_plt_call linkage stub then a single
2892 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2893 call. If other calls to printf require a ppc_stub_plt_call_r2save
2894 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2895 be created and calls not requiring r2 to be saved will enter the
2896 stub after the r2 save instruction. There is an analogous
2897 hierarchy of long branch and plt branch stubs for local call
2903 ppc_stub_long_branch,
2904 ppc_stub_long_branch_r2off,
2905 ppc_stub_long_branch_notoc,
2906 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
2907 ppc_stub_plt_branch,
2908 ppc_stub_plt_branch_r2off,
2909 ppc_stub_plt_branch_notoc,
2910 ppc_stub_plt_branch_both,
2912 ppc_stub_plt_call_r2save,
2913 ppc_stub_plt_call_notoc,
2914 ppc_stub_plt_call_both,
2915 ppc_stub_global_entry,
2919 /* Information on stub grouping. */
2922 /* The stub section. */
2924 /* This is the section to which stubs in the group will be attached. */
2927 struct map_stub *next;
2928 /* Whether to emit a copy of register save/restore functions in this
2931 /* Current offset within stubs after the insn restoring lr in a
2932 _notoc or _both stub using bcl for pc-relative addressing, or
2933 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2934 unsigned int lr_restore;
2935 /* Accumulated size of EH info emitted to describe return address
2936 if stubs modify lr. Does not include 17 byte FDE header. */
2937 unsigned int eh_size;
2938 /* Offset in glink_eh_frame to the start of EH info for this group. */
2939 unsigned int eh_base;
2942 struct ppc_stub_hash_entry
2944 /* Base hash table entry structure. */
2945 struct bfd_hash_entry root;
2947 enum ppc_stub_type stub_type;
2949 /* Group information. */
2950 struct map_stub *group;
2952 /* Offset within stub_sec of the beginning of this stub. */
2953 bfd_vma stub_offset;
2955 /* Given the symbol's value and its section we can determine its final
2956 value when building the stubs (so the stub knows where to jump. */
2957 bfd_vma target_value;
2958 asection *target_section;
2960 /* The symbol table entry, if any, that this was derived from. */
2961 struct ppc_link_hash_entry *h;
2962 struct plt_entry *plt_ent;
2965 unsigned char symtype;
2967 /* Symbol st_other. */
2968 unsigned char other;
2971 struct ppc_branch_hash_entry
2973 /* Base hash table entry structure. */
2974 struct bfd_hash_entry root;
2976 /* Offset within branch lookup table. */
2977 unsigned int offset;
2979 /* Generation marker. */
2983 /* Used to track dynamic relocations for local symbols. */
2984 struct ppc_dyn_relocs
2986 struct ppc_dyn_relocs *next;
2988 /* The input section of the reloc. */
2991 /* Total number of relocs copied for the input section. */
2992 unsigned int count : 31;
2994 /* Whether this entry is for STT_GNU_IFUNC symbols. */
2995 unsigned int ifunc : 1;
2998 struct ppc_link_hash_entry
3000 struct elf_link_hash_entry elf;
3004 /* A pointer to the most recently used stub hash entry against this
3006 struct ppc_stub_hash_entry *stub_cache;
3008 /* A pointer to the next symbol starting with a '.' */
3009 struct ppc_link_hash_entry *next_dot_sym;
3012 /* Track dynamic relocs copied for this symbol. */
3013 struct elf_dyn_relocs *dyn_relocs;
3015 /* Link between function code and descriptor symbols. */
3016 struct ppc_link_hash_entry *oh;
3018 /* Flag function code and descriptor symbols. */
3019 unsigned int is_func:1;
3020 unsigned int is_func_descriptor:1;
3021 unsigned int fake:1;
3023 /* Whether global opd/toc sym has been adjusted or not.
3024 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3025 should be set for all globals defined in any opd/toc section. */
3026 unsigned int adjust_done:1;
3028 /* Set if this is an out-of-line register save/restore function,
3029 with non-standard calling convention. */
3030 unsigned int save_res:1;
3032 /* Set if a duplicate symbol with non-zero localentry is detected,
3033 even when the duplicate symbol does not provide a definition. */
3034 unsigned int non_zero_localentry:1;
3036 /* Contexts in which symbol is used in the GOT (or TOC).
3037 Bits are or'd into the mask as the corresponding relocs are
3038 encountered during check_relocs, with TLS_TLS being set when any
3039 of the other TLS bits are set. tls_optimize clears bits when
3040 optimizing to indicate the corresponding GOT entry type is not
3041 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3042 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
3043 separate flag rather than setting TPREL just for convenience in
3044 distinguishing the two cases.
3045 These flags are also kept for local symbols. */
3046 #define TLS_TLS 1 /* Any TLS reloc. */
3047 #define TLS_GD 2 /* GD reloc. */
3048 #define TLS_LD 4 /* LD reloc. */
3049 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
3050 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3051 #define TLS_MARK 32 /* __tls_get_addr call marked. */
3052 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
3053 #define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
3054 unsigned char tls_mask;
3056 /* The above field is also used to mark function symbols. In which
3057 case TLS_TLS will be 0. */
3058 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
3059 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
3060 #define NON_GOT 256 /* local symbol plt, not stored. */
3063 /* ppc64 ELF linker hash table. */
3065 struct ppc_link_hash_table
3067 struct elf_link_hash_table elf;
3069 /* The stub hash table. */
3070 struct bfd_hash_table stub_hash_table;
3072 /* Another hash table for plt_branch stubs. */
3073 struct bfd_hash_table branch_hash_table;
3075 /* Hash table for function prologue tocsave. */
3076 htab_t tocsave_htab;
3078 /* Various options and other info passed from the linker. */
3079 struct ppc64_elf_params *params;
3081 /* The size of sec_info below. */
3082 unsigned int sec_info_arr_size;
3084 /* Per-section array of extra section info. Done this way rather
3085 than as part of ppc64_elf_section_data so we have the info for
3086 non-ppc64 sections. */
3089 /* Along with elf_gp, specifies the TOC pointer used by this section. */
3094 /* The section group that this section belongs to. */
3095 struct map_stub *group;
3096 /* A temp section list pointer. */
3101 /* Linked list of groups. */
3102 struct map_stub *group;
3104 /* Temp used when calculating TOC pointers. */
3107 asection *toc_first_sec;
3109 /* Used when adding symbols. */
3110 struct ppc_link_hash_entry *dot_syms;
3112 /* Shortcuts to get to dynamic linker sections. */
3114 asection *global_entry;
3117 asection *relpltlocal;
3120 asection *glink_eh_frame;
3122 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3123 struct ppc_link_hash_entry *tls_get_addr;
3124 struct ppc_link_hash_entry *tls_get_addr_fd;
3126 /* The size of reliplt used by got entry relocs. */
3127 bfd_size_type got_reli_size;
3130 unsigned long stub_count[ppc_stub_global_entry];
3132 /* Number of stubs against global syms. */
3133 unsigned long stub_globals;
3135 /* Set if we're linking code with function descriptors. */
3136 unsigned int opd_abi:1;
3138 /* Support for multiple toc sections. */
3139 unsigned int do_multi_toc:1;
3140 unsigned int multi_toc_needed:1;
3141 unsigned int second_toc_pass:1;
3142 unsigned int do_toc_opt:1;
3144 /* Set if tls optimization is enabled. */
3145 unsigned int do_tls_opt:1;
3147 /* Set if inline plt calls should be converted to direct calls. */
3148 unsigned int can_convert_all_inline_plt:1;
3151 unsigned int stub_error:1;
3153 /* Whether func_desc_adjust needs to be run over symbols. */
3154 unsigned int need_func_desc_adj:1;
3156 /* Whether there exist local gnu indirect function resolvers,
3157 referenced by dynamic relocations. */
3158 unsigned int local_ifunc_resolver:1;
3159 unsigned int maybe_local_ifunc_resolver:1;
3161 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3162 unsigned int has_plt_localentry0:1;
3164 /* Whether calls are made via the PLT from NOTOC functions. */
3165 unsigned int notoc_plt:1;
3167 /* Whether to use powerxx instructions in linkage stubs. */
3168 unsigned int powerxx_stubs:1;
3170 /* Incremented every time we size stubs. */
3171 unsigned int stub_iteration;
3173 /* Small local sym cache. */
3174 struct sym_cache sym_cache;
3177 /* Rename some of the generic section flags to better document how they
3180 /* Nonzero if this section has TLS related relocations. */
3181 #define has_tls_reloc sec_flg0
3183 /* Nonzero if this section has an old-style call to __tls_get_addr. */
3184 #define has_tls_get_addr_call sec_flg1
3186 /* Nonzero if this section has any toc or got relocs. */
3187 #define has_toc_reloc sec_flg2
3189 /* Nonzero if this section has a call to another section that uses
3191 #define makes_toc_func_call sec_flg3
3193 /* Recursion protection when determining above flag. */
3194 #define call_check_in_progress sec_flg4
3195 #define call_check_done sec_flg5
3197 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3199 #define ppc_hash_table(p) \
3200 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3201 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3203 #define ppc_stub_hash_lookup(table, string, create, copy) \
3204 ((struct ppc_stub_hash_entry *) \
3205 bfd_hash_lookup ((table), (string), (create), (copy)))
3207 #define ppc_branch_hash_lookup(table, string, create, copy) \
3208 ((struct ppc_branch_hash_entry *) \
3209 bfd_hash_lookup ((table), (string), (create), (copy)))
3211 /* Create an entry in the stub hash table. */
3213 static struct bfd_hash_entry *
3214 stub_hash_newfunc (struct bfd_hash_entry *entry,
3215 struct bfd_hash_table *table,
3218 /* Allocate the structure if it has not already been allocated by a
3222 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3227 /* Call the allocation method of the superclass. */
3228 entry = bfd_hash_newfunc (entry, table, string);
3231 struct ppc_stub_hash_entry *eh;
3233 /* Initialize the local fields. */
3234 eh = (struct ppc_stub_hash_entry *) entry;
3235 eh->stub_type = ppc_stub_none;
3237 eh->stub_offset = 0;
3238 eh->target_value = 0;
3239 eh->target_section = NULL;
3248 /* Create an entry in the branch hash table. */
3250 static struct bfd_hash_entry *
3251 branch_hash_newfunc (struct bfd_hash_entry *entry,
3252 struct bfd_hash_table *table,
3255 /* Allocate the structure if it has not already been allocated by a
3259 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3264 /* Call the allocation method of the superclass. */
3265 entry = bfd_hash_newfunc (entry, table, string);
3268 struct ppc_branch_hash_entry *eh;
3270 /* Initialize the local fields. */
3271 eh = (struct ppc_branch_hash_entry *) entry;
3279 /* Create an entry in a ppc64 ELF linker hash table. */
3281 static struct bfd_hash_entry *
3282 link_hash_newfunc (struct bfd_hash_entry *entry,
3283 struct bfd_hash_table *table,
3286 /* Allocate the structure if it has not already been allocated by a
3290 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3295 /* Call the allocation method of the superclass. */
3296 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3299 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3301 memset (&eh->u.stub_cache, 0,
3302 (sizeof (struct ppc_link_hash_entry)
3303 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3305 /* When making function calls, old ABI code references function entry
3306 points (dot symbols), while new ABI code references the function
3307 descriptor symbol. We need to make any combination of reference and
3308 definition work together, without breaking archive linking.
3310 For a defined function "foo" and an undefined call to "bar":
3311 An old object defines "foo" and ".foo", references ".bar" (possibly
3313 A new object defines "foo" and references "bar".
3315 A new object thus has no problem with its undefined symbols being
3316 satisfied by definitions in an old object. On the other hand, the
3317 old object won't have ".bar" satisfied by a new object.
3319 Keep a list of newly added dot-symbols. */
3321 if (string[0] == '.')
3323 struct ppc_link_hash_table *htab;
3325 htab = (struct ppc_link_hash_table *) table;
3326 eh->u.next_dot_sym = htab->dot_syms;
3327 htab->dot_syms = eh;
3334 struct tocsave_entry
3341 tocsave_htab_hash (const void *p)
3343 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3344 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3348 tocsave_htab_eq (const void *p1, const void *p2)
3350 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3351 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3352 return e1->sec == e2->sec && e1->offset == e2->offset;
3355 /* Destroy a ppc64 ELF linker hash table. */
3358 ppc64_elf_link_hash_table_free (bfd *obfd)
3360 struct ppc_link_hash_table *htab;
3362 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3363 if (htab->tocsave_htab)
3364 htab_delete (htab->tocsave_htab);
3365 bfd_hash_table_free (&htab->branch_hash_table);
3366 bfd_hash_table_free (&htab->stub_hash_table);
3367 _bfd_elf_link_hash_table_free (obfd);
3370 /* Create a ppc64 ELF linker hash table. */
3372 static struct bfd_link_hash_table *
3373 ppc64_elf_link_hash_table_create (bfd *abfd)
3375 struct ppc_link_hash_table *htab;
3376 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3378 htab = bfd_zmalloc (amt);
3382 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3383 sizeof (struct ppc_link_hash_entry),
3390 /* Init the stub hash table too. */
3391 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3392 sizeof (struct ppc_stub_hash_entry)))
3394 _bfd_elf_link_hash_table_free (abfd);
3398 /* And the branch hash table. */
3399 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3400 sizeof (struct ppc_branch_hash_entry)))
3402 bfd_hash_table_free (&htab->stub_hash_table);
3403 _bfd_elf_link_hash_table_free (abfd);
3407 htab->tocsave_htab = htab_try_create (1024,
3411 if (htab->tocsave_htab == NULL)
3413 ppc64_elf_link_hash_table_free (abfd);
3416 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3418 /* Initializing two fields of the union is just cosmetic. We really
3419 only care about glist, but when compiled on a 32-bit host the
3420 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3421 debugger inspection of these fields look nicer. */
3422 htab->elf.init_got_refcount.refcount = 0;
3423 htab->elf.init_got_refcount.glist = NULL;
3424 htab->elf.init_plt_refcount.refcount = 0;
3425 htab->elf.init_plt_refcount.glist = NULL;
3426 htab->elf.init_got_offset.offset = 0;
3427 htab->elf.init_got_offset.glist = NULL;
3428 htab->elf.init_plt_offset.offset = 0;
3429 htab->elf.init_plt_offset.glist = NULL;
3431 return &htab->elf.root;
3434 /* Create sections for linker generated code. */
3437 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3439 struct ppc_link_hash_table *htab;
3442 htab = ppc_hash_table (info);
3444 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3445 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3446 if (htab->params->save_restore_funcs)
3448 /* Create .sfpr for code to save and restore fp regs. */
3449 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3451 if (htab->sfpr == NULL
3452 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3456 if (bfd_link_relocatable (info))
3459 /* Create .glink for lazy dynamic linking support. */
3460 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3462 if (htab->glink == NULL
3463 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
3466 /* The part of .glink used by global entry stubs, separate so that
3467 it can be aligned appropriately without affecting htab->glink. */
3468 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3470 if (htab->global_entry == NULL
3471 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
3474 if (!info->no_ld_generated_unwind_info)
3476 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3477 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3478 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3481 if (htab->glink_eh_frame == NULL
3482 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3486 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3487 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3488 if (htab->elf.iplt == NULL
3489 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
3492 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3493 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3495 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3496 if (htab->elf.irelplt == NULL
3497 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
3500 /* Create branch lookup table for plt_branch stubs. */
3501 flags = (SEC_ALLOC | SEC_LOAD
3502 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3503 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3505 if (htab->brlt == NULL
3506 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
3509 /* Local plt entries, put in .branch_lt but a separate section for
3511 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3513 if (htab->pltlocal == NULL
3514 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
3517 if (!bfd_link_pic (info))
3520 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3521 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3523 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3524 if (htab->relbrlt == NULL
3525 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3529 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3530 if (htab->relpltlocal == NULL
3531 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
3537 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3540 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3541 struct ppc64_elf_params *params)
3543 struct ppc_link_hash_table *htab;
3545 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3547 /* Always hook our dynamic sections into the first bfd, which is the
3548 linker created stub bfd. This ensures that the GOT header is at
3549 the start of the output TOC section. */
3550 htab = ppc_hash_table (info);
3551 htab->elf.dynobj = params->stub_bfd;
3552 htab->params = params;
3554 return create_linkage_sections (htab->elf.dynobj, info);
3557 /* Build a name for an entry in the stub hash table. */
3560 ppc_stub_name (const asection *input_section,
3561 const asection *sym_sec,
3562 const struct ppc_link_hash_entry *h,
3563 const Elf_Internal_Rela *rel)
3568 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3569 offsets from a sym as a branch target? In fact, we could
3570 probably assume the addend is always zero. */
3571 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3575 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3576 stub_name = bfd_malloc (len);
3577 if (stub_name == NULL)
3580 len = sprintf (stub_name, "%08x.%s+%x",
3581 input_section->id & 0xffffffff,
3582 h->elf.root.root.string,
3583 (int) rel->r_addend & 0xffffffff);
3587 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3588 stub_name = bfd_malloc (len);
3589 if (stub_name == NULL)
3592 len = sprintf (stub_name, "%08x.%x:%x+%x",
3593 input_section->id & 0xffffffff,
3594 sym_sec->id & 0xffffffff,
3595 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3596 (int) rel->r_addend & 0xffffffff);
3598 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3599 stub_name[len - 2] = 0;
3603 /* Look up an entry in the stub hash. Stub entries are cached because
3604 creating the stub name takes a bit of time. */
3606 static struct ppc_stub_hash_entry *
3607 ppc_get_stub_entry (const asection *input_section,
3608 const asection *sym_sec,
3609 struct ppc_link_hash_entry *h,
3610 const Elf_Internal_Rela *rel,
3611 struct ppc_link_hash_table *htab)
3613 struct ppc_stub_hash_entry *stub_entry;
3614 struct map_stub *group;
3616 /* If this input section is part of a group of sections sharing one
3617 stub section, then use the id of the first section in the group.
3618 Stub names need to include a section id, as there may well be
3619 more than one stub used to reach say, printf, and we need to
3620 distinguish between them. */
3621 group = htab->sec_info[input_section->id].u.group;
3625 if (h != NULL && h->u.stub_cache != NULL
3626 && h->u.stub_cache->h == h
3627 && h->u.stub_cache->group == group)
3629 stub_entry = h->u.stub_cache;
3635 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3636 if (stub_name == NULL)
3639 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3640 stub_name, FALSE, FALSE);
3642 h->u.stub_cache = stub_entry;
3650 /* Add a new stub entry to the stub hash. Not all fields of the new
3651 stub entry are initialised. */
3653 static struct ppc_stub_hash_entry *
3654 ppc_add_stub (const char *stub_name,
3656 struct bfd_link_info *info)
3658 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3659 struct map_stub *group;
3662 struct ppc_stub_hash_entry *stub_entry;
3664 group = htab->sec_info[section->id].u.group;
3665 link_sec = group->link_sec;
3666 stub_sec = group->stub_sec;
3667 if (stub_sec == NULL)
3673 namelen = strlen (link_sec->name);
3674 len = namelen + sizeof (STUB_SUFFIX);
3675 s_name = bfd_alloc (htab->params->stub_bfd, len);
3679 memcpy (s_name, link_sec->name, namelen);
3680 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3681 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3682 if (stub_sec == NULL)
3684 group->stub_sec = stub_sec;
3687 /* Enter this entry into the linker stub hash table. */
3688 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3690 if (stub_entry == NULL)
3692 /* xgettext:c-format */
3693 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3694 section->owner, stub_name);
3698 stub_entry->group = group;
3699 stub_entry->stub_offset = 0;
3703 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3704 not already done. */
3707 create_got_section (bfd *abfd, struct bfd_link_info *info)
3709 asection *got, *relgot;
3711 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3713 if (!is_ppc64_elf (abfd))
3719 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3722 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3723 | SEC_LINKER_CREATED);
3725 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3727 || !bfd_set_section_alignment (abfd, got, 3))
3730 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3731 flags | SEC_READONLY);
3733 || !bfd_set_section_alignment (abfd, relgot, 3))
3736 ppc64_elf_tdata (abfd)->got = got;
3737 ppc64_elf_tdata (abfd)->relgot = relgot;
3741 /* Follow indirect and warning symbol links. */
3743 static inline struct bfd_link_hash_entry *
3744 follow_link (struct bfd_link_hash_entry *h)
3746 while (h->type == bfd_link_hash_indirect
3747 || h->type == bfd_link_hash_warning)
3752 static inline struct elf_link_hash_entry *
3753 elf_follow_link (struct elf_link_hash_entry *h)
3755 return (struct elf_link_hash_entry *) follow_link (&h->root);
3758 static inline struct ppc_link_hash_entry *
3759 ppc_follow_link (struct ppc_link_hash_entry *h)
3761 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3764 /* Merge PLT info on FROM with that on TO. */
3767 move_plt_plist (struct ppc_link_hash_entry *from,
3768 struct ppc_link_hash_entry *to)
3770 if (from->elf.plt.plist != NULL)
3772 if (to->elf.plt.plist != NULL)
3774 struct plt_entry **entp;
3775 struct plt_entry *ent;
3777 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3779 struct plt_entry *dent;
3781 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3782 if (dent->addend == ent->addend)
3784 dent->plt.refcount += ent->plt.refcount;
3791 *entp = to->elf.plt.plist;
3794 to->elf.plt.plist = from->elf.plt.plist;
3795 from->elf.plt.plist = NULL;
3799 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3802 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3803 struct elf_link_hash_entry *dir,
3804 struct elf_link_hash_entry *ind)
3806 struct ppc_link_hash_entry *edir, *eind;
3808 edir = (struct ppc_link_hash_entry *) dir;
3809 eind = (struct ppc_link_hash_entry *) ind;
3811 edir->is_func |= eind->is_func;
3812 edir->is_func_descriptor |= eind->is_func_descriptor;
3813 edir->tls_mask |= eind->tls_mask;
3814 if (eind->oh != NULL)
3815 edir->oh = ppc_follow_link (eind->oh);
3817 if (edir->elf.versioned != versioned_hidden)
3818 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3819 edir->elf.ref_regular |= eind->elf.ref_regular;
3820 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3821 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3822 edir->elf.needs_plt |= eind->elf.needs_plt;
3823 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3825 /* If we were called to copy over info for a weak sym, don't copy
3826 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3827 in order to simplify readonly_dynrelocs and save a field in the
3828 symbol hash entry, but that means dyn_relocs can't be used in any
3829 tests about a specific symbol, or affect other symbol flags which
3831 if (eind->elf.root.type != bfd_link_hash_indirect)
3834 /* Copy over any dynamic relocs we may have on the indirect sym. */
3835 if (eind->dyn_relocs != NULL)
3837 if (edir->dyn_relocs != NULL)
3839 struct elf_dyn_relocs **pp;
3840 struct elf_dyn_relocs *p;
3842 /* Add reloc counts against the indirect sym to the direct sym
3843 list. Merge any entries against the same section. */
3844 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3846 struct elf_dyn_relocs *q;
3848 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3849 if (q->sec == p->sec)
3851 q->pc_count += p->pc_count;
3852 q->count += p->count;
3859 *pp = edir->dyn_relocs;
3862 edir->dyn_relocs = eind->dyn_relocs;
3863 eind->dyn_relocs = NULL;
3866 /* Copy over got entries that we may have already seen to the
3867 symbol which just became indirect. */
3868 if (eind->elf.got.glist != NULL)
3870 if (edir->elf.got.glist != NULL)
3872 struct got_entry **entp;
3873 struct got_entry *ent;
3875 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3877 struct got_entry *dent;
3879 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3880 if (dent->addend == ent->addend
3881 && dent->owner == ent->owner
3882 && dent->tls_type == ent->tls_type)
3884 dent->got.refcount += ent->got.refcount;
3891 *entp = edir->elf.got.glist;
3894 edir->elf.got.glist = eind->elf.got.glist;
3895 eind->elf.got.glist = NULL;
3898 /* And plt entries. */
3899 move_plt_plist (eind, edir);
3901 if (eind->elf.dynindx != -1)
3903 if (edir->elf.dynindx != -1)
3904 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3905 edir->elf.dynstr_index);
3906 edir->elf.dynindx = eind->elf.dynindx;
3907 edir->elf.dynstr_index = eind->elf.dynstr_index;
3908 eind->elf.dynindx = -1;
3909 eind->elf.dynstr_index = 0;
3913 /* Find the function descriptor hash entry from the given function code
3914 hash entry FH. Link the entries via their OH fields. */
3916 static struct ppc_link_hash_entry *
3917 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3919 struct ppc_link_hash_entry *fdh = fh->oh;
3923 const char *fd_name = fh->elf.root.root.string + 1;
3925 fdh = (struct ppc_link_hash_entry *)
3926 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
3930 fdh->is_func_descriptor = 1;
3936 fdh = ppc_follow_link (fdh);
3937 fdh->is_func_descriptor = 1;
3942 /* Make a fake function descriptor sym for the undefined code sym FH. */
3944 static struct ppc_link_hash_entry *
3945 make_fdh (struct bfd_link_info *info,
3946 struct ppc_link_hash_entry *fh)
3948 bfd *abfd = fh->elf.root.u.undef.abfd;
3949 struct bfd_link_hash_entry *bh = NULL;
3950 struct ppc_link_hash_entry *fdh;
3951 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3955 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3956 fh->elf.root.root.string + 1,
3957 flags, bfd_und_section_ptr, 0,
3958 NULL, FALSE, FALSE, &bh))
3961 fdh = (struct ppc_link_hash_entry *) bh;
3962 fdh->elf.non_elf = 0;
3964 fdh->is_func_descriptor = 1;
3971 /* Fix function descriptor symbols defined in .opd sections to be
3975 ppc64_elf_add_symbol_hook (bfd *ibfd,
3976 struct bfd_link_info *info,
3977 Elf_Internal_Sym *isym,
3979 flagword *flags ATTRIBUTE_UNUSED,
3984 && strcmp ((*sec)->name, ".opd") == 0)
3988 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3989 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
3990 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
3992 /* If the symbol is a function defined in .opd, and the function
3993 code is in a discarded group, let it appear to be undefined. */
3994 if (!bfd_link_relocatable (info)
3995 && (*sec)->reloc_count != 0
3996 && opd_entry_value (*sec, *value, &code_sec, NULL,
3997 FALSE) != (bfd_vma) -1
3998 && discarded_section (code_sec))
4000 *sec = bfd_und_section_ptr;
4001 isym->st_shndx = SHN_UNDEF;
4004 else if (*sec != NULL
4005 && strcmp ((*sec)->name, ".toc") == 0
4006 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4008 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4010 htab->params->object_in_toc = 1;
4013 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4015 if (abiversion (ibfd) == 0)
4016 set_abiversion (ibfd, 2);
4017 else if (abiversion (ibfd) == 1)
4019 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4020 " for ABI version 1"), *name);
4021 bfd_set_error (bfd_error_bad_value);
4029 /* Merge non-visibility st_other attributes: local entry point. */
4032 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4033 const Elf_Internal_Sym *isym,
4034 bfd_boolean definition,
4035 bfd_boolean dynamic)
4037 if (definition && (!dynamic || !h->def_regular))
4038 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4039 | ELF_ST_VISIBILITY (h->other));
4042 /* Hook called on merging a symbol. We use this to clear "fake" since
4043 we now have a real symbol. */
4046 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4047 const Elf_Internal_Sym *isym,
4048 asection **psec ATTRIBUTE_UNUSED,
4049 bfd_boolean newdef ATTRIBUTE_UNUSED,
4050 bfd_boolean olddef ATTRIBUTE_UNUSED,
4051 bfd *oldbfd ATTRIBUTE_UNUSED,
4052 const asection *oldsec ATTRIBUTE_UNUSED)
4054 ((struct ppc_link_hash_entry *) h)->fake = 0;
4055 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4056 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
4060 /* This function makes an old ABI object reference to ".bar" cause the
4061 inclusion of a new ABI object archive that defines "bar".
4062 NAME is a symbol defined in an archive. Return a symbol in the hash
4063 table that might be satisfied by the archive symbols. */
4065 static struct elf_link_hash_entry *
4066 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4067 struct bfd_link_info *info,
4070 struct elf_link_hash_entry *h;
4074 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4076 /* Don't return this sym if it is a fake function descriptor
4077 created by add_symbol_adjust. */
4078 && !((struct ppc_link_hash_entry *) h)->fake)
4084 len = strlen (name);
4085 dot_name = bfd_alloc (abfd, len + 2);
4086 if (dot_name == NULL)
4087 return (struct elf_link_hash_entry *) -1;
4089 memcpy (dot_name + 1, name, len + 1);
4090 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4091 bfd_release (abfd, dot_name);
4095 /* This function satisfies all old ABI object references to ".bar" if a
4096 new ABI object defines "bar". Well, at least, undefined dot symbols
4097 are made weak. This stops later archive searches from including an
4098 object if we already have a function descriptor definition. It also
4099 prevents the linker complaining about undefined symbols.
4100 We also check and correct mismatched symbol visibility here. The
4101 most restrictive visibility of the function descriptor and the
4102 function entry symbol is used. */
4105 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4107 struct ppc_link_hash_table *htab;
4108 struct ppc_link_hash_entry *fdh;
4110 if (eh->elf.root.type == bfd_link_hash_warning)
4111 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4113 if (eh->elf.root.type == bfd_link_hash_indirect)
4116 if (eh->elf.root.root.string[0] != '.')
4119 htab = ppc_hash_table (info);
4123 fdh = lookup_fdh (eh, htab);
4125 && !bfd_link_relocatable (info)
4126 && (eh->elf.root.type == bfd_link_hash_undefined
4127 || eh->elf.root.type == bfd_link_hash_undefweak)
4128 && eh->elf.ref_regular)
4130 /* Make an undefined function descriptor sym, in order to
4131 pull in an --as-needed shared lib. Archives are handled
4133 fdh = make_fdh (info, eh);
4140 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4141 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4143 /* Make both descriptor and entry symbol have the most
4144 constraining visibility of either symbol. */
4145 if (entry_vis < descr_vis)
4146 fdh->elf.other += entry_vis - descr_vis;
4147 else if (entry_vis > descr_vis)
4148 eh->elf.other += descr_vis - entry_vis;
4150 /* Propagate reference flags from entry symbol to function
4151 descriptor symbol. */
4152 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4153 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4154 fdh->elf.ref_regular |= eh->elf.ref_regular;
4155 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4157 if (!fdh->elf.forced_local
4158 && fdh->elf.dynindx == -1
4159 && fdh->elf.versioned != versioned_hidden
4160 && (bfd_link_dll (info)
4161 || fdh->elf.def_dynamic
4162 || fdh->elf.ref_dynamic)
4163 && (eh->elf.ref_regular
4164 || eh->elf.def_regular))
4166 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4174 /* Set up opd section info and abiversion for IBFD, and process list
4175 of dot-symbols we made in link_hash_newfunc. */
4178 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4180 struct ppc_link_hash_table *htab;
4181 struct ppc_link_hash_entry **p, *eh;
4182 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4184 if (opd != NULL && opd->size != 0)
4186 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4187 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4189 if (abiversion (ibfd) == 0)
4190 set_abiversion (ibfd, 1);
4191 else if (abiversion (ibfd) >= 2)
4193 /* xgettext:c-format */
4194 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4195 ibfd, abiversion (ibfd));
4196 bfd_set_error (bfd_error_bad_value);
4201 if (is_ppc64_elf (info->output_bfd))
4203 /* For input files without an explicit abiversion in e_flags
4204 we should have flagged any with symbol st_other bits set
4205 as ELFv1 and above flagged those with .opd as ELFv2.
4206 Set the output abiversion if not yet set, and for any input
4207 still ambiguous, take its abiversion from the output.
4208 Differences in ABI are reported later. */
4209 if (abiversion (info->output_bfd) == 0)
4210 set_abiversion (info->output_bfd, abiversion (ibfd));
4211 else if (abiversion (ibfd) == 0)
4212 set_abiversion (ibfd, abiversion (info->output_bfd));
4215 htab = ppc_hash_table (info);
4219 if (opd != NULL && opd->size != 0
4220 && (ibfd->flags & DYNAMIC) == 0
4221 && (opd->flags & SEC_RELOC) != 0
4222 && opd->reloc_count != 0
4223 && !bfd_is_abs_section (opd->output_section)
4224 && info->gc_sections)
4226 /* Garbage collection needs some extra help with .opd sections.
4227 We don't want to necessarily keep everything referenced by
4228 relocs in .opd, as that would keep all functions. Instead,
4229 if we reference an .opd symbol (a function descriptor), we
4230 want to keep the function code symbol's section. This is
4231 easy for global symbols, but for local syms we need to keep
4232 information about the associated function section. */
4234 asection **opd_sym_map;
4235 Elf_Internal_Shdr *symtab_hdr;
4236 Elf_Internal_Rela *relocs, *rel_end, *rel;
4238 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4239 opd_sym_map = bfd_zalloc (ibfd, amt);
4240 if (opd_sym_map == NULL)
4242 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4243 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4247 symtab_hdr = &elf_symtab_hdr (ibfd);
4248 rel_end = relocs + opd->reloc_count - 1;
4249 for (rel = relocs; rel < rel_end; rel++)
4251 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4252 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4254 if (r_type == R_PPC64_ADDR64
4255 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4256 && r_symndx < symtab_hdr->sh_info)
4258 Elf_Internal_Sym *isym;
4261 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4264 if (elf_section_data (opd)->relocs != relocs)
4269 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4270 if (s != NULL && s != opd)
4271 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4274 if (elf_section_data (opd)->relocs != relocs)
4278 p = &htab->dot_syms;
4279 while ((eh = *p) != NULL)
4282 if (&eh->elf == htab->elf.hgot)
4284 else if (htab->elf.hgot == NULL
4285 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4286 htab->elf.hgot = &eh->elf;
4287 else if (abiversion (ibfd) <= 1)
4289 htab->need_func_desc_adj = 1;
4290 if (!add_symbol_adjust (eh, info))
4293 p = &eh->u.next_dot_sym;
4298 /* Undo hash table changes when an --as-needed input file is determined
4299 not to be needed. */
4302 ppc64_elf_notice_as_needed (bfd *ibfd,
4303 struct bfd_link_info *info,
4304 enum notice_asneeded_action act)
4306 if (act == notice_not_needed)
4308 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4313 htab->dot_syms = NULL;
4315 return _bfd_elf_notice_as_needed (ibfd, info, act);
4318 /* If --just-symbols against a final linked binary, then assume we need
4319 toc adjusting stubs when calling functions defined there. */
4322 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4324 if ((sec->flags & SEC_CODE) != 0
4325 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4326 && is_ppc64_elf (sec->owner))
4328 if (abiversion (sec->owner) >= 2
4329 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4330 sec->has_toc_reloc = 1;
4332 _bfd_elf_link_just_syms (sec, info);
4335 static struct plt_entry **
4336 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4337 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4339 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4340 struct plt_entry **local_plt;
4341 unsigned char *local_got_tls_masks;
4343 if (local_got_ents == NULL)
4345 bfd_size_type size = symtab_hdr->sh_info;
4347 size *= (sizeof (*local_got_ents)
4348 + sizeof (*local_plt)
4349 + sizeof (*local_got_tls_masks));
4350 local_got_ents = bfd_zalloc (abfd, size);
4351 if (local_got_ents == NULL)
4353 elf_local_got_ents (abfd) = local_got_ents;
4356 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4358 struct got_entry *ent;
4360 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4361 if (ent->addend == r_addend
4362 && ent->owner == abfd
4363 && ent->tls_type == tls_type)
4367 bfd_size_type amt = sizeof (*ent);
4368 ent = bfd_alloc (abfd, amt);
4371 ent->next = local_got_ents[r_symndx];
4372 ent->addend = r_addend;
4374 ent->tls_type = tls_type;
4375 ent->is_indirect = FALSE;
4376 ent->got.refcount = 0;
4377 local_got_ents[r_symndx] = ent;
4379 ent->got.refcount += 1;
4382 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4383 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4384 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4386 return local_plt + r_symndx;
4390 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4392 struct plt_entry *ent;
4394 for (ent = *plist; ent != NULL; ent = ent->next)
4395 if (ent->addend == addend)
4399 bfd_size_type amt = sizeof (*ent);
4400 ent = bfd_alloc (abfd, amt);
4404 ent->addend = addend;
4405 ent->plt.refcount = 0;
4408 ent->plt.refcount += 1;
4413 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4415 return (r_type == R_PPC64_REL24
4416 || r_type == R_PPC64_REL24_NOTOC
4417 || r_type == R_PPC64_REL14
4418 || r_type == R_PPC64_REL14_BRTAKEN
4419 || r_type == R_PPC64_REL14_BRNTAKEN
4420 || r_type == R_PPC64_ADDR24
4421 || r_type == R_PPC64_ADDR14
4422 || r_type == R_PPC64_ADDR14_BRTAKEN
4423 || r_type == R_PPC64_ADDR14_BRNTAKEN
4424 || r_type == R_PPC64_PLTCALL
4425 || r_type == R_PPC64_PLTCALL_NOTOC);
4428 /* Relocs on inline plt call sequence insns prior to the call. */
4431 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4433 return (r_type == R_PPC64_PLT16_HA
4434 || r_type == R_PPC64_PLT16_HI
4435 || r_type == R_PPC64_PLT16_LO
4436 || r_type == R_PPC64_PLT16_LO_DS
4437 || r_type == R_PPC64_PLT_PCREL34
4438 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4439 || r_type == R_PPC64_PLTSEQ
4440 || r_type == R_PPC64_PLTSEQ_NOTOC);
4443 /* Look through the relocs for a section during the first phase, and
4444 calculate needed space in the global offset table, procedure
4445 linkage table, and dynamic reloc sections. */
4448 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4449 asection *sec, const Elf_Internal_Rela *relocs)
4451 struct ppc_link_hash_table *htab;
4452 Elf_Internal_Shdr *symtab_hdr;
4453 struct elf_link_hash_entry **sym_hashes;
4454 const Elf_Internal_Rela *rel;
4455 const Elf_Internal_Rela *rel_end;
4457 struct elf_link_hash_entry *tga, *dottga;
4460 if (bfd_link_relocatable (info))
4463 /* Don't do anything special with non-loaded, non-alloced sections.
4464 In particular, any relocs in such sections should not affect GOT
4465 and PLT reference counting (ie. we don't allow them to create GOT
4466 or PLT entries), there's no possibility or desire to optimize TLS
4467 relocs, and there's not much point in propagating relocs to shared
4468 libs that the dynamic linker won't relocate. */
4469 if ((sec->flags & SEC_ALLOC) == 0)
4472 BFD_ASSERT (is_ppc64_elf (abfd));
4474 htab = ppc_hash_table (info);
4478 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4479 FALSE, FALSE, TRUE);
4480 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4481 FALSE, FALSE, TRUE);
4482 symtab_hdr = &elf_symtab_hdr (abfd);
4483 sym_hashes = elf_sym_hashes (abfd);
4485 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4486 rel_end = relocs + sec->reloc_count;
4487 for (rel = relocs; rel < rel_end; rel++)
4489 unsigned long r_symndx;
4490 struct elf_link_hash_entry *h;
4491 enum elf_ppc64_reloc_type r_type;
4493 struct _ppc64_elf_section_data *ppc64_sec;
4494 struct plt_entry **ifunc, **plt_list;
4497 r_symndx = ELF64_R_SYM (rel->r_info);
4498 if (r_symndx < symtab_hdr->sh_info)
4502 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4503 h = elf_follow_link (h);
4505 if (h == htab->elf.hgot)
4506 sec->has_toc_reloc = 1;
4511 r_type = ELF64_R_TYPE (rel->r_info);
4515 case R_PPC64_D34_LO:
4516 case R_PPC64_D34_HI30:
4517 case R_PPC64_D34_HA30:
4519 htab->powerxx_stubs = 1;
4522 /* Somewhat foolishly, because the ABIs don't specifically
4523 allow it, ppc64 gas and ld support GOT and PLT relocs
4524 with non-zero addends where the addend results in
4525 sym+addend being stored in the GOT or PLT entry. This
4526 can't be supported for pcrel relocs because the addend is
4527 used to specify the pcrel offset. */
4528 sym_addend = rel->r_addend;
4531 case R_PPC64_PCREL34:
4532 case R_PPC64_GOT_PCREL34:
4533 case R_PPC64_PLT_PCREL34:
4534 case R_PPC64_PLT_PCREL34_NOTOC:
4535 case R_PPC64_PCREL28:
4536 htab->powerxx_stubs = 1;
4542 if (h->type == STT_GNU_IFUNC)
4545 ifunc = &h->plt.plist;
4550 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4555 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4557 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4559 NON_GOT | PLT_IFUNC);
4569 /* These special tls relocs tie a call to __tls_get_addr with
4570 its parameter symbol. */
4572 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4574 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4576 NON_GOT | TLS_TLS | TLS_MARK))
4578 sec->has_tls_reloc = 1;
4581 case R_PPC64_GOT_TLSLD16:
4582 case R_PPC64_GOT_TLSLD16_LO:
4583 case R_PPC64_GOT_TLSLD16_HI:
4584 case R_PPC64_GOT_TLSLD16_HA:
4585 tls_type = TLS_TLS | TLS_LD;
4588 case R_PPC64_GOT_TLSGD16:
4589 case R_PPC64_GOT_TLSGD16_LO:
4590 case R_PPC64_GOT_TLSGD16_HI:
4591 case R_PPC64_GOT_TLSGD16_HA:
4592 tls_type = TLS_TLS | TLS_GD;
4595 case R_PPC64_GOT_TPREL16_DS:
4596 case R_PPC64_GOT_TPREL16_LO_DS:
4597 case R_PPC64_GOT_TPREL16_HI:
4598 case R_PPC64_GOT_TPREL16_HA:
4599 if (bfd_link_dll (info))
4600 info->flags |= DF_STATIC_TLS;
4601 tls_type = TLS_TLS | TLS_TPREL;
4604 case R_PPC64_GOT_DTPREL16_DS:
4605 case R_PPC64_GOT_DTPREL16_LO_DS:
4606 case R_PPC64_GOT_DTPREL16_HI:
4607 case R_PPC64_GOT_DTPREL16_HA:
4608 tls_type = TLS_TLS | TLS_DTPREL;
4610 sec->has_tls_reloc = 1;
4613 case R_PPC64_GOT16_DS:
4614 case R_PPC64_GOT16_HA:
4615 case R_PPC64_GOT16_LO_DS:
4616 case R_PPC64_GOT_PCREL34:
4617 ppc64_elf_tdata (abfd)->has_gotrel = 1;
4618 ppc64_elf_section_data (sec)->has_gotrel = 1;
4622 case R_PPC64_GOT16_HI:
4623 case R_PPC64_GOT16_LO:
4625 /* This symbol requires a global offset table entry. */
4626 sec->has_toc_reloc = 1;
4627 if (r_type == R_PPC64_GOT_TLSLD16
4628 || r_type == R_PPC64_GOT_TLSGD16
4629 || r_type == R_PPC64_GOT_TPREL16_DS
4630 || r_type == R_PPC64_GOT_DTPREL16_DS
4631 || r_type == R_PPC64_GOT16
4632 || r_type == R_PPC64_GOT16_DS)
4634 htab->do_multi_toc = 1;
4635 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4638 if (ppc64_elf_tdata (abfd)->got == NULL
4639 && !create_got_section (abfd, info))
4644 struct ppc_link_hash_entry *eh;
4645 struct got_entry *ent;
4647 eh = (struct ppc_link_hash_entry *) h;
4648 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4649 if (ent->addend == sym_addend
4650 && ent->owner == abfd
4651 && ent->tls_type == tls_type)
4655 bfd_size_type amt = sizeof (*ent);
4656 ent = bfd_alloc (abfd, amt);
4659 ent->next = eh->elf.got.glist;
4660 ent->addend = sym_addend;
4662 ent->tls_type = tls_type;
4663 ent->is_indirect = FALSE;
4664 ent->got.refcount = 0;
4665 eh->elf.got.glist = ent;
4667 ent->got.refcount += 1;
4668 eh->tls_mask |= tls_type;
4671 /* This is a global offset table entry for a local symbol. */
4672 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4673 sym_addend, tls_type))
4676 /* We may also need a plt entry if the symbol turns out to be
4678 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
4680 if (!update_plt_info (abfd, &h->plt.plist, sym_addend))
4685 case R_PPC64_PLT16_HA:
4686 case R_PPC64_PLT16_HI:
4687 case R_PPC64_PLT16_LO:
4688 case R_PPC64_PLT16_LO_DS:
4689 case R_PPC64_PLT_PCREL34:
4690 case R_PPC64_PLT_PCREL34_NOTOC:
4693 /* This symbol requires a procedure linkage table entry. */
4698 if (h->root.root.string[0] == '.'
4699 && h->root.root.string[1] != '\0')
4700 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4701 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
4702 plt_list = &h->plt.plist;
4704 if (plt_list == NULL)
4705 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4707 NON_GOT | PLT_KEEP);
4708 if (!update_plt_info (abfd, plt_list, sym_addend))
4712 /* The following relocations don't need to propagate the
4713 relocation if linking a shared object since they are
4714 section relative. */
4715 case R_PPC64_SECTOFF:
4716 case R_PPC64_SECTOFF_LO:
4717 case R_PPC64_SECTOFF_HI:
4718 case R_PPC64_SECTOFF_HA:
4719 case R_PPC64_SECTOFF_DS:
4720 case R_PPC64_SECTOFF_LO_DS:
4721 case R_PPC64_DTPREL16:
4722 case R_PPC64_DTPREL16_LO:
4723 case R_PPC64_DTPREL16_HI:
4724 case R_PPC64_DTPREL16_HA:
4725 case R_PPC64_DTPREL16_DS:
4726 case R_PPC64_DTPREL16_LO_DS:
4727 case R_PPC64_DTPREL16_HIGH:
4728 case R_PPC64_DTPREL16_HIGHA:
4729 case R_PPC64_DTPREL16_HIGHER:
4730 case R_PPC64_DTPREL16_HIGHERA:
4731 case R_PPC64_DTPREL16_HIGHEST:
4732 case R_PPC64_DTPREL16_HIGHESTA:
4737 case R_PPC64_REL16_LO:
4738 case R_PPC64_REL16_HI:
4739 case R_PPC64_REL16_HA:
4740 case R_PPC64_REL16_HIGH:
4741 case R_PPC64_REL16_HIGHA:
4742 case R_PPC64_REL16_HIGHER:
4743 case R_PPC64_REL16_HIGHERA:
4744 case R_PPC64_REL16_HIGHEST:
4745 case R_PPC64_REL16_HIGHESTA:
4746 case R_PPC64_REL16_HIGHER34:
4747 case R_PPC64_REL16_HIGHERA34:
4748 case R_PPC64_REL16_HIGHEST34:
4749 case R_PPC64_REL16_HIGHESTA34:
4750 case R_PPC64_REL16DX_HA:
4753 /* Not supported as a dynamic relocation. */
4754 case R_PPC64_ADDR64_LOCAL:
4755 if (bfd_link_pic (info))
4757 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4759 /* xgettext:c-format */
4760 info->callbacks->einfo (_("%H: %s reloc unsupported "
4761 "in shared libraries and PIEs\n"),
4762 abfd, sec, rel->r_offset,
4763 ppc64_elf_howto_table[r_type]->name);
4764 bfd_set_error (bfd_error_bad_value);
4770 case R_PPC64_TOC16_DS:
4771 htab->do_multi_toc = 1;
4772 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4774 case R_PPC64_TOC16_LO:
4775 case R_PPC64_TOC16_HI:
4776 case R_PPC64_TOC16_HA:
4777 case R_PPC64_TOC16_LO_DS:
4778 sec->has_toc_reloc = 1;
4785 /* This relocation describes the C++ object vtable hierarchy.
4786 Reconstruct it for later use during GC. */
4787 case R_PPC64_GNU_VTINHERIT:
4788 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4792 /* This relocation describes which C++ vtable entries are actually
4793 used. Record for later use during GC. */
4794 case R_PPC64_GNU_VTENTRY:
4795 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4800 case R_PPC64_REL14_BRTAKEN:
4801 case R_PPC64_REL14_BRNTAKEN:
4803 asection *dest = NULL;
4805 /* Heuristic: If jumping outside our section, chances are
4806 we are going to need a stub. */
4809 /* If the sym is weak it may be overridden later, so
4810 don't assume we know where a weak sym lives. */
4811 if (h->root.type == bfd_link_hash_defined)
4812 dest = h->root.u.def.section;
4816 Elf_Internal_Sym *isym;
4818 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4823 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4827 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
4831 case R_PPC64_PLTCALL:
4832 case R_PPC64_PLTCALL_NOTOC:
4833 ppc64_elf_section_data (sec)->has_pltcall = 1;
4837 case R_PPC64_REL24_NOTOC:
4843 if (h->root.root.string[0] == '.'
4844 && h->root.root.string[1] != '\0')
4845 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4847 if (h == tga || h == dottga)
4849 sec->has_tls_reloc = 1;
4851 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4852 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4853 /* We have a new-style __tls_get_addr call with
4857 /* Mark this section as having an old-style call. */
4858 sec->has_tls_get_addr_call = 1;
4860 plt_list = &h->plt.plist;
4863 /* We may need a .plt entry if the function this reloc
4864 refers to is in a shared lib. */
4866 && !update_plt_info (abfd, plt_list, sym_addend))
4870 case R_PPC64_ADDR14:
4871 case R_PPC64_ADDR14_BRNTAKEN:
4872 case R_PPC64_ADDR14_BRTAKEN:
4873 case R_PPC64_ADDR24:
4876 case R_PPC64_TPREL64:
4877 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4878 if (bfd_link_dll (info))
4879 info->flags |= DF_STATIC_TLS;
4882 case R_PPC64_DTPMOD64:
4883 if (rel + 1 < rel_end
4884 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4885 && rel[1].r_offset == rel->r_offset + 8)
4886 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4888 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4891 case R_PPC64_DTPREL64:
4892 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4894 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4895 && rel[-1].r_offset == rel->r_offset - 8)
4896 /* This is the second reloc of a dtpmod, dtprel pair.
4897 Don't mark with TLS_DTPREL. */
4901 sec->has_tls_reloc = 1;
4904 struct ppc_link_hash_entry *eh;
4905 eh = (struct ppc_link_hash_entry *) h;
4906 eh->tls_mask |= tls_type;
4909 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4910 sym_addend, tls_type))
4913 ppc64_sec = ppc64_elf_section_data (sec);
4914 if (ppc64_sec->sec_type != sec_toc)
4918 /* One extra to simplify get_tls_mask. */
4919 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4920 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4921 if (ppc64_sec->u.toc.symndx == NULL)
4923 amt = sec->size * sizeof (bfd_vma) / 8;
4924 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4925 if (ppc64_sec->u.toc.add == NULL)
4927 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4928 ppc64_sec->sec_type = sec_toc;
4930 BFD_ASSERT (rel->r_offset % 8 == 0);
4931 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
4932 ppc64_sec->u.toc.add[rel->r_offset / 8] = sym_addend;
4934 /* Mark the second slot of a GD or LD entry.
4935 -1 to indicate GD and -2 to indicate LD. */
4936 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
4937 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
4938 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
4939 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
4942 case R_PPC64_TPREL16:
4943 case R_PPC64_TPREL16_LO:
4944 case R_PPC64_TPREL16_HI:
4945 case R_PPC64_TPREL16_HA:
4946 case R_PPC64_TPREL16_DS:
4947 case R_PPC64_TPREL16_LO_DS:
4948 case R_PPC64_TPREL16_HIGH:
4949 case R_PPC64_TPREL16_HIGHA:
4950 case R_PPC64_TPREL16_HIGHER:
4951 case R_PPC64_TPREL16_HIGHERA:
4952 case R_PPC64_TPREL16_HIGHEST:
4953 case R_PPC64_TPREL16_HIGHESTA:
4954 if (bfd_link_dll (info))
4955 info->flags |= DF_STATIC_TLS;
4958 case R_PPC64_ADDR64:
4960 && rel + 1 < rel_end
4961 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
4964 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4968 case R_PPC64_ADDR16:
4969 case R_PPC64_ADDR16_DS:
4970 case R_PPC64_ADDR16_HA:
4971 case R_PPC64_ADDR16_HI:
4972 case R_PPC64_ADDR16_HIGH:
4973 case R_PPC64_ADDR16_HIGHA:
4974 case R_PPC64_ADDR16_HIGHER:
4975 case R_PPC64_ADDR16_HIGHERA:
4976 case R_PPC64_ADDR16_HIGHEST:
4977 case R_PPC64_ADDR16_HIGHESTA:
4978 case R_PPC64_ADDR16_LO:
4979 case R_PPC64_ADDR16_LO_DS:
4981 case R_PPC64_D34_LO:
4982 case R_PPC64_D34_HI30:
4983 case R_PPC64_D34_HA30:
4984 case R_PPC64_ADDR16_HIGHER34:
4985 case R_PPC64_ADDR16_HIGHERA34:
4986 case R_PPC64_ADDR16_HIGHEST34:
4987 case R_PPC64_ADDR16_HIGHESTA34:
4989 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
4990 && rel->r_addend == 0)
4992 /* We may need a .plt entry if this reloc refers to a
4993 function in a shared lib. */
4994 if (!update_plt_info (abfd, &h->plt.plist, 0))
4996 h->pointer_equality_needed = 1;
5003 case R_PPC64_ADDR32:
5004 case R_PPC64_UADDR16:
5005 case R_PPC64_UADDR32:
5006 case R_PPC64_UADDR64:
5008 if (h != NULL && !bfd_link_pic (info))
5009 /* We may need a copy reloc. */
5012 /* Don't propagate .opd relocs. */
5013 if (NO_OPD_RELOCS && is_opd)
5016 /* If we are creating a shared library, and this is a reloc
5017 against a global symbol, or a non PC relative reloc
5018 against a local symbol, then we need to copy the reloc
5019 into the shared library. However, if we are linking with
5020 -Bsymbolic, we do not need to copy a reloc against a
5021 global symbol which is defined in an object we are
5022 including in the link (i.e., DEF_REGULAR is set). At
5023 this point we have not seen all the input files, so it is
5024 possible that DEF_REGULAR is not set now but will be set
5025 later (it is never cleared). In case of a weak definition,
5026 DEF_REGULAR may be cleared later by a strong definition in
5027 a shared library. We account for that possibility below by
5028 storing information in the dyn_relocs field of the hash
5029 table entry. A similar situation occurs when creating
5030 shared libraries and symbol visibility changes render the
5033 If on the other hand, we are creating an executable, we
5034 may need to keep relocations for symbols satisfied by a
5035 dynamic library if we manage to avoid copy relocs for the
5038 if ((bfd_link_pic (info)
5039 && (must_be_dyn_reloc (info, r_type)
5041 && (!SYMBOLIC_BIND (info, h)
5042 || h->root.type == bfd_link_hash_defweak
5043 || !h->def_regular))))
5044 || (ELIMINATE_COPY_RELOCS
5045 && !bfd_link_pic (info)
5047 && (h->root.type == bfd_link_hash_defweak
5048 || !h->def_regular))
5049 || (!bfd_link_pic (info)
5052 /* We must copy these reloc types into the output file.
5053 Create a reloc section in dynobj and make room for
5057 sreloc = _bfd_elf_make_dynamic_reloc_section
5058 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5064 /* If this is a global symbol, we count the number of
5065 relocations we need for this symbol. */
5068 struct elf_dyn_relocs *p;
5069 struct elf_dyn_relocs **head;
5071 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5073 if (p == NULL || p->sec != sec)
5075 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5085 if (!must_be_dyn_reloc (info, r_type))
5090 /* Track dynamic relocs needed for local syms too.
5091 We really need local syms available to do this
5093 struct ppc_dyn_relocs *p;
5094 struct ppc_dyn_relocs **head;
5095 bfd_boolean is_ifunc;
5098 Elf_Internal_Sym *isym;
5100 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5105 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5109 vpp = &elf_section_data (s)->local_dynrel;
5110 head = (struct ppc_dyn_relocs **) vpp;
5111 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5113 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5115 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5117 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5123 p->ifunc = is_ifunc;
5139 /* Merge backend specific data from an object file to the output
5140 object file when linking. */
5143 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5145 bfd *obfd = info->output_bfd;
5146 unsigned long iflags, oflags;
5148 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5151 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5154 if (!_bfd_generic_verify_endian_match (ibfd, info))
5157 iflags = elf_elfheader (ibfd)->e_flags;
5158 oflags = elf_elfheader (obfd)->e_flags;
5160 if (iflags & ~EF_PPC64_ABI)
5163 /* xgettext:c-format */
5164 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5165 bfd_set_error (bfd_error_bad_value);
5168 else if (iflags != oflags && iflags != 0)
5171 /* xgettext:c-format */
5172 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5173 ibfd, iflags, oflags);
5174 bfd_set_error (bfd_error_bad_value);
5178 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5181 /* Merge Tag_compatibility attributes and any common GNU ones. */
5182 return _bfd_elf_merge_object_attributes (ibfd, info);
5186 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5188 /* Print normal ELF private data. */
5189 _bfd_elf_print_private_bfd_data (abfd, ptr);
5191 if (elf_elfheader (abfd)->e_flags != 0)
5195 fprintf (file, _("private flags = 0x%lx:"),
5196 elf_elfheader (abfd)->e_flags);
5198 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5199 fprintf (file, _(" [abiv%ld]"),
5200 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5207 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5208 of the code entry point, and its section, which must be in the same
5209 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
5212 opd_entry_value (asection *opd_sec,
5214 asection **code_sec,
5216 bfd_boolean in_code_sec)
5218 bfd *opd_bfd = opd_sec->owner;
5219 Elf_Internal_Rela *relocs;
5220 Elf_Internal_Rela *lo, *hi, *look;
5223 /* No relocs implies we are linking a --just-symbols object, or looking
5224 at a final linked executable with addr2line or somesuch. */
5225 if (opd_sec->reloc_count == 0)
5227 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5229 if (contents == NULL)
5231 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5232 return (bfd_vma) -1;
5233 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5236 /* PR 17512: file: 64b9dfbb. */
5237 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5238 return (bfd_vma) -1;
5240 val = bfd_get_64 (opd_bfd, contents + offset);
5241 if (code_sec != NULL)
5243 asection *sec, *likely = NULL;
5249 && val < sec->vma + sec->size)
5255 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5257 && (sec->flags & SEC_LOAD) != 0
5258 && (sec->flags & SEC_ALLOC) != 0)
5263 if (code_off != NULL)
5264 *code_off = val - likely->vma;
5270 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5272 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5274 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5275 /* PR 17512: file: df8e1fd6. */
5277 return (bfd_vma) -1;
5279 /* Go find the opd reloc at the sym address. */
5281 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5285 look = lo + (hi - lo) / 2;
5286 if (look->r_offset < offset)
5288 else if (look->r_offset > offset)
5292 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5294 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5295 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5297 unsigned long symndx = ELF64_R_SYM (look->r_info);
5298 asection *sec = NULL;
5300 if (symndx >= symtab_hdr->sh_info
5301 && elf_sym_hashes (opd_bfd) != NULL)
5303 struct elf_link_hash_entry **sym_hashes;
5304 struct elf_link_hash_entry *rh;
5306 sym_hashes = elf_sym_hashes (opd_bfd);
5307 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5310 rh = elf_follow_link (rh);
5311 if (rh->root.type != bfd_link_hash_defined
5312 && rh->root.type != bfd_link_hash_defweak)
5314 if (rh->root.u.def.section->owner == opd_bfd)
5316 val = rh->root.u.def.value;
5317 sec = rh->root.u.def.section;
5324 Elf_Internal_Sym *sym;
5326 if (symndx < symtab_hdr->sh_info)
5328 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5331 size_t symcnt = symtab_hdr->sh_info;
5332 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5337 symtab_hdr->contents = (bfd_byte *) sym;
5343 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5349 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5352 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5353 val = sym->st_value;
5356 val += look->r_addend;
5357 if (code_off != NULL)
5359 if (code_sec != NULL)
5361 if (in_code_sec && *code_sec != sec)
5366 if (sec->output_section != NULL)
5367 val += sec->output_section->vma + sec->output_offset;
5376 /* If the ELF symbol SYM might be a function in SEC, return the
5377 function size and set *CODE_OFF to the function's entry point,
5378 otherwise return zero. */
5380 static bfd_size_type
5381 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5386 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5387 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5391 if (!(sym->flags & BSF_SYNTHETIC))
5392 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5394 if (strcmp (sym->section->name, ".opd") == 0)
5396 struct _opd_sec_data *opd = get_opd_info (sym->section);
5397 bfd_vma symval = sym->value;
5400 && opd->adjust != NULL
5401 && elf_section_data (sym->section)->relocs != NULL)
5403 /* opd_entry_value will use cached relocs that have been
5404 adjusted, but with raw symbols. That means both local
5405 and global symbols need adjusting. */
5406 long adjust = opd->adjust[OPD_NDX (symval)];
5412 if (opd_entry_value (sym->section, symval,
5413 &sec, code_off, TRUE) == (bfd_vma) -1)
5415 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5416 symbol. This size has nothing to do with the code size of the
5417 function, which is what we're supposed to return, but the
5418 code size isn't available without looking up the dot-sym.
5419 However, doing that would be a waste of time particularly
5420 since elf_find_function will look at the dot-sym anyway.
5421 Now, elf_find_function will keep the largest size of any
5422 function sym found at the code address of interest, so return
5423 1 here to avoid it incorrectly caching a larger function size
5424 for a small function. This does mean we return the wrong
5425 size for a new-ABI function of size 24, but all that does is
5426 disable caching for such functions. */
5432 if (sym->section != sec)
5434 *code_off = sym->value;
5441 /* Return true if symbol is a strong function defined in an ELFv2
5442 object with st_other localentry bits of zero, ie. its local entry
5443 point coincides with its global entry point. */
5446 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5449 && h->type == STT_FUNC
5450 && h->root.type == bfd_link_hash_defined
5451 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5452 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
5453 && is_ppc64_elf (h->root.u.def.section->owner)
5454 && abiversion (h->root.u.def.section->owner) >= 2);
5457 /* Return true if symbol is defined in a regular object file. */
5460 is_static_defined (struct elf_link_hash_entry *h)
5462 return ((h->root.type == bfd_link_hash_defined
5463 || h->root.type == bfd_link_hash_defweak)
5464 && h->root.u.def.section != NULL
5465 && h->root.u.def.section->output_section != NULL);
5468 /* If FDH is a function descriptor symbol, return the associated code
5469 entry symbol if it is defined. Return NULL otherwise. */
5471 static struct ppc_link_hash_entry *
5472 defined_code_entry (struct ppc_link_hash_entry *fdh)
5474 if (fdh->is_func_descriptor)
5476 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5477 if (fh->elf.root.type == bfd_link_hash_defined
5478 || fh->elf.root.type == bfd_link_hash_defweak)
5484 /* If FH is a function code entry symbol, return the associated
5485 function descriptor symbol if it is defined. Return NULL otherwise. */
5487 static struct ppc_link_hash_entry *
5488 defined_func_desc (struct ppc_link_hash_entry *fh)
5491 && fh->oh->is_func_descriptor)
5493 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5494 if (fdh->elf.root.type == bfd_link_hash_defined
5495 || fdh->elf.root.type == bfd_link_hash_defweak)
5501 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5503 /* Garbage collect sections, after first dealing with dot-symbols. */
5506 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5508 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5510 if (htab != NULL && htab->need_func_desc_adj)
5512 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5513 htab->need_func_desc_adj = 0;
5515 return bfd_elf_gc_sections (abfd, info);
5518 /* Mark all our entry sym sections, both opd and code section. */
5521 ppc64_elf_gc_keep (struct bfd_link_info *info)
5523 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5524 struct bfd_sym_chain *sym;
5529 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5531 struct ppc_link_hash_entry *eh, *fh;
5534 eh = (struct ppc_link_hash_entry *)
5535 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5538 if (eh->elf.root.type != bfd_link_hash_defined
5539 && eh->elf.root.type != bfd_link_hash_defweak)
5542 fh = defined_code_entry (eh);
5545 sec = fh->elf.root.u.def.section;
5546 sec->flags |= SEC_KEEP;
5548 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5549 && opd_entry_value (eh->elf.root.u.def.section,
5550 eh->elf.root.u.def.value,
5551 &sec, NULL, FALSE) != (bfd_vma) -1)
5552 sec->flags |= SEC_KEEP;
5554 sec = eh->elf.root.u.def.section;
5555 sec->flags |= SEC_KEEP;
5559 /* Mark sections containing dynamically referenced symbols. When
5560 building shared libraries, we must assume that any visible symbol is
5564 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5566 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5567 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5568 struct ppc_link_hash_entry *fdh;
5569 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5571 /* Dynamic linking info is on the func descriptor sym. */
5572 fdh = defined_func_desc (eh);
5576 if ((eh->elf.root.type == bfd_link_hash_defined
5577 || eh->elf.root.type == bfd_link_hash_defweak)
5578 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5579 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5580 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5581 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5582 && (!bfd_link_executable (info)
5583 || info->gc_keep_exported
5584 || info->export_dynamic
5587 && (*d->match) (&d->head, NULL,
5588 eh->elf.root.root.string)))
5589 && (eh->elf.versioned >= versioned
5590 || !bfd_hide_sym_by_version (info->version_info,
5591 eh->elf.root.root.string)))))
5594 struct ppc_link_hash_entry *fh;
5596 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5598 /* Function descriptor syms cause the associated
5599 function code sym section to be marked. */
5600 fh = defined_code_entry (eh);
5603 code_sec = fh->elf.root.u.def.section;
5604 code_sec->flags |= SEC_KEEP;
5606 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5607 && opd_entry_value (eh->elf.root.u.def.section,
5608 eh->elf.root.u.def.value,
5609 &code_sec, NULL, FALSE) != (bfd_vma) -1)
5610 code_sec->flags |= SEC_KEEP;
5616 /* Return the section that should be marked against GC for a given
5620 ppc64_elf_gc_mark_hook (asection *sec,
5621 struct bfd_link_info *info,
5622 Elf_Internal_Rela *rel,
5623 struct elf_link_hash_entry *h,
5624 Elf_Internal_Sym *sym)
5628 /* Syms return NULL if we're marking .opd, so we avoid marking all
5629 function sections, as all functions are referenced in .opd. */
5631 if (get_opd_info (sec) != NULL)
5636 enum elf_ppc64_reloc_type r_type;
5637 struct ppc_link_hash_entry *eh, *fh, *fdh;
5639 r_type = ELF64_R_TYPE (rel->r_info);
5642 case R_PPC64_GNU_VTINHERIT:
5643 case R_PPC64_GNU_VTENTRY:
5647 switch (h->root.type)
5649 case bfd_link_hash_defined:
5650 case bfd_link_hash_defweak:
5651 eh = (struct ppc_link_hash_entry *) h;
5652 fdh = defined_func_desc (eh);
5655 /* -mcall-aixdesc code references the dot-symbol on
5656 a call reloc. Mark the function descriptor too
5657 against garbage collection. */
5659 if (fdh->elf.is_weakalias)
5660 weakdef (&fdh->elf)->mark = 1;
5664 /* Function descriptor syms cause the associated
5665 function code sym section to be marked. */
5666 fh = defined_code_entry (eh);
5669 /* They also mark their opd section. */
5670 eh->elf.root.u.def.section->gc_mark = 1;
5672 rsec = fh->elf.root.u.def.section;
5674 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5675 && opd_entry_value (eh->elf.root.u.def.section,
5676 eh->elf.root.u.def.value,
5677 &rsec, NULL, FALSE) != (bfd_vma) -1)
5678 eh->elf.root.u.def.section->gc_mark = 1;
5680 rsec = h->root.u.def.section;
5683 case bfd_link_hash_common:
5684 rsec = h->root.u.c.p->section;
5688 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5694 struct _opd_sec_data *opd;
5696 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5697 opd = get_opd_info (rsec);
5698 if (opd != NULL && opd->func_sec != NULL)
5702 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
5709 /* The maximum size of .sfpr. */
5710 #define SFPR_MAX (218*4)
5712 struct sfpr_def_parms
5714 const char name[12];
5715 unsigned char lo, hi;
5716 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5717 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
5720 /* Auto-generate _save*, _rest* functions in .sfpr.
5721 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5725 sfpr_define (struct bfd_link_info *info,
5726 const struct sfpr_def_parms *parm,
5729 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5731 size_t len = strlen (parm->name);
5732 bfd_boolean writing = FALSE;
5738 memcpy (sym, parm->name, len);
5741 for (i = parm->lo; i <= parm->hi; i++)
5743 struct ppc_link_hash_entry *h;
5745 sym[len + 0] = i / 10 + '0';
5746 sym[len + 1] = i % 10 + '0';
5747 h = (struct ppc_link_hash_entry *)
5748 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
5749 if (stub_sec != NULL)
5752 && h->elf.root.type == bfd_link_hash_defined
5753 && h->elf.root.u.def.section == htab->sfpr)
5755 struct elf_link_hash_entry *s;
5757 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5758 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5761 if (s->root.type == bfd_link_hash_new
5762 || (s->root.type = bfd_link_hash_defined
5763 && s->root.u.def.section == stub_sec))
5765 s->root.type = bfd_link_hash_defined;
5766 s->root.u.def.section = stub_sec;
5767 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
5768 + h->elf.root.u.def.value);
5771 s->ref_regular_nonweak = 1;
5772 s->forced_local = 1;
5774 s->root.linker_def = 1;
5782 if (!h->elf.def_regular)
5784 h->elf.root.type = bfd_link_hash_defined;
5785 h->elf.root.u.def.section = htab->sfpr;
5786 h->elf.root.u.def.value = htab->sfpr->size;
5787 h->elf.type = STT_FUNC;
5788 h->elf.def_regular = 1;
5790 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5792 if (htab->sfpr->contents == NULL)
5794 htab->sfpr->contents
5795 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5796 if (htab->sfpr->contents == NULL)
5803 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5805 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5807 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5808 htab->sfpr->size = p - htab->sfpr->contents;
5816 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5818 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5823 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5825 p = savegpr0 (abfd, p, r);
5826 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5828 bfd_put_32 (abfd, BLR, p);
5833 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5835 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5840 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5842 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5844 p = restgpr0 (abfd, p, r);
5845 bfd_put_32 (abfd, MTLR_R0, p);
5849 p = restgpr0 (abfd, p, 30);
5850 p = restgpr0 (abfd, p, 31);
5852 bfd_put_32 (abfd, BLR, p);
5857 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5859 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5864 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5866 p = savegpr1 (abfd, p, r);
5867 bfd_put_32 (abfd, BLR, p);
5872 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5874 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5879 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5881 p = restgpr1 (abfd, p, r);
5882 bfd_put_32 (abfd, BLR, p);
5887 savefpr (bfd *abfd, bfd_byte *p, int r)
5889 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5894 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5896 p = savefpr (abfd, p, r);
5897 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5899 bfd_put_32 (abfd, BLR, p);
5904 restfpr (bfd *abfd, bfd_byte *p, int r)
5906 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5911 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5913 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5915 p = restfpr (abfd, p, r);
5916 bfd_put_32 (abfd, MTLR_R0, p);
5920 p = restfpr (abfd, p, 30);
5921 p = restfpr (abfd, p, 31);
5923 bfd_put_32 (abfd, BLR, p);
5928 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5930 p = savefpr (abfd, p, r);
5931 bfd_put_32 (abfd, BLR, p);
5936 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5938 p = restfpr (abfd, p, r);
5939 bfd_put_32 (abfd, BLR, p);
5944 savevr (bfd *abfd, bfd_byte *p, int r)
5946 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5948 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5953 savevr_tail (bfd *abfd, bfd_byte *p, int r)
5955 p = savevr (abfd, p, r);
5956 bfd_put_32 (abfd, BLR, p);
5961 restvr (bfd *abfd, bfd_byte *p, int r)
5963 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5965 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5970 restvr_tail (bfd *abfd, bfd_byte *p, int r)
5972 p = restvr (abfd, p, r);
5973 bfd_put_32 (abfd, BLR, p);
5977 /* Called via elf_link_hash_traverse to transfer dynamic linking
5978 information on function code symbol entries to their corresponding
5979 function descriptor symbol entries. */
5982 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5984 struct bfd_link_info *info;
5985 struct ppc_link_hash_table *htab;
5986 struct ppc_link_hash_entry *fh;
5987 struct ppc_link_hash_entry *fdh;
5988 bfd_boolean force_local;
5990 fh = (struct ppc_link_hash_entry *) h;
5991 if (fh->elf.root.type == bfd_link_hash_indirect)
5997 if (fh->elf.root.root.string[0] != '.'
5998 || fh->elf.root.root.string[1] == '\0')
6002 htab = ppc_hash_table (info);
6006 /* Find the corresponding function descriptor symbol. */
6007 fdh = lookup_fdh (fh, htab);
6009 /* Resolve undefined references to dot-symbols as the value
6010 in the function descriptor, if we have one in a regular object.
6011 This is to satisfy cases like ".quad .foo". Calls to functions
6012 in dynamic objects are handled elsewhere. */
6013 if ((fh->elf.root.type == bfd_link_hash_undefined
6014 || fh->elf.root.type == bfd_link_hash_undefweak)
6015 && (fdh->elf.root.type == bfd_link_hash_defined
6016 || fdh->elf.root.type == bfd_link_hash_defweak)
6017 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6018 && opd_entry_value (fdh->elf.root.u.def.section,
6019 fdh->elf.root.u.def.value,
6020 &fh->elf.root.u.def.section,
6021 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6023 fh->elf.root.type = fdh->elf.root.type;
6024 fh->elf.forced_local = 1;
6025 fh->elf.def_regular = fdh->elf.def_regular;
6026 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6029 if (!fh->elf.dynamic)
6031 struct plt_entry *ent;
6033 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6034 if (ent->plt.refcount > 0)
6040 /* Create a descriptor as undefined if necessary. */
6042 && !bfd_link_executable (info)
6043 && (fh->elf.root.type == bfd_link_hash_undefined
6044 || fh->elf.root.type == bfd_link_hash_undefweak))
6046 fdh = make_fdh (info, fh);
6051 /* We can't support overriding of symbols on a fake descriptor. */
6054 && (fh->elf.root.type == bfd_link_hash_defined
6055 || fh->elf.root.type == bfd_link_hash_defweak))
6056 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6058 /* Transfer dynamic linking information to the function descriptor. */
6061 fdh->elf.ref_regular |= fh->elf.ref_regular;
6062 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6063 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6064 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6065 fdh->elf.dynamic |= fh->elf.dynamic;
6066 fdh->elf.needs_plt |= (fh->elf.needs_plt
6067 || fh->elf.type == STT_FUNC
6068 || fh->elf.type == STT_GNU_IFUNC);
6069 move_plt_plist (fh, fdh);
6071 if (!fdh->elf.forced_local
6072 && fh->elf.dynindx != -1)
6073 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6077 /* Now that the info is on the function descriptor, clear the
6078 function code sym info. Any function code syms for which we
6079 don't have a definition in a regular file, we force local.
6080 This prevents a shared library from exporting syms that have
6081 been imported from another library. Function code syms that
6082 are really in the library we must leave global to prevent the
6083 linker dragging in a definition from a static library. */
6084 force_local = (!fh->elf.def_regular
6086 || !fdh->elf.def_regular
6087 || fdh->elf.forced_local);
6088 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6093 static const struct sfpr_def_parms save_res_funcs[] =
6095 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6096 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6097 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6098 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6099 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6100 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6101 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6102 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6103 { "._savef", 14, 31, savefpr, savefpr1_tail },
6104 { "._restf", 14, 31, restfpr, restfpr1_tail },
6105 { "_savevr_", 20, 31, savevr, savevr_tail },
6106 { "_restvr_", 20, 31, restvr, restvr_tail }
6109 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6110 this hook to a) provide some gcc support functions, and b) transfer
6111 dynamic linking information gathered so far on function code symbol
6112 entries, to their corresponding function descriptor symbol entries. */
6115 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6116 struct bfd_link_info *info)
6118 struct ppc_link_hash_table *htab;
6120 htab = ppc_hash_table (info);
6124 /* Provide any missing _save* and _rest* functions. */
6125 if (htab->sfpr != NULL)
6129 htab->sfpr->size = 0;
6130 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6131 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6133 if (htab->sfpr->size == 0)
6134 htab->sfpr->flags |= SEC_EXCLUDE;
6137 if (bfd_link_relocatable (info))
6140 if (htab->elf.hgot != NULL)
6142 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6143 /* Make .TOC. defined so as to prevent it being made dynamic.
6144 The wrong value here is fixed later in ppc64_elf_set_toc. */
6145 if (!htab->elf.hgot->def_regular
6146 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6148 htab->elf.hgot->root.type = bfd_link_hash_defined;
6149 htab->elf.hgot->root.u.def.value = 0;
6150 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6151 htab->elf.hgot->def_regular = 1;
6152 htab->elf.hgot->root.linker_def = 1;
6154 htab->elf.hgot->type = STT_OBJECT;
6155 htab->elf.hgot->other
6156 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6159 if (htab->need_func_desc_adj)
6161 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6162 htab->need_func_desc_adj = 0;
6168 /* Find dynamic relocs for H that apply to read-only sections. */
6171 readonly_dynrelocs (struct elf_link_hash_entry *h)
6173 struct ppc_link_hash_entry *eh;
6174 struct elf_dyn_relocs *p;
6176 eh = (struct ppc_link_hash_entry *) h;
6177 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6179 asection *s = p->sec->output_section;
6181 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6187 /* Return true if we have dynamic relocs against H or any of its weak
6188 aliases, that apply to read-only sections. Cannot be used after
6189 size_dynamic_sections. */
6192 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6194 struct ppc_link_hash_entry *eh;
6196 eh = (struct ppc_link_hash_entry *) h;
6199 if (readonly_dynrelocs (&eh->elf))
6201 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
6203 while (eh != NULL && &eh->elf != h);
6208 /* Return whether EH has pc-relative dynamic relocs. */
6211 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6213 struct elf_dyn_relocs *p;
6215 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6216 if (p->pc_count != 0)
6221 /* Return true if a global entry stub will be created for H. Valid
6222 for ELFv2 before plt entries have been allocated. */
6225 global_entry_stub (struct elf_link_hash_entry *h)
6227 struct plt_entry *pent;
6229 if (!h->pointer_equality_needed
6233 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6234 if (pent->plt.refcount > 0
6235 && pent->addend == 0)
6241 /* Adjust a symbol defined by a dynamic object and referenced by a
6242 regular object. The current definition is in some section of the
6243 dynamic object, but we're not including those sections. We have to
6244 change the definition to something the rest of the link can
6248 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6249 struct elf_link_hash_entry *h)
6251 struct ppc_link_hash_table *htab;
6254 htab = ppc_hash_table (info);
6258 /* Deal with function syms. */
6259 if (h->type == STT_FUNC
6260 || h->type == STT_GNU_IFUNC
6263 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6264 || SYMBOL_CALLS_LOCAL (info, h)
6265 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6266 /* Discard dyn_relocs when non-pic if we've decided that a
6267 function symbol is local and not an ifunc. We keep dynamic
6268 relocs for ifuncs when local rather than always emitting a
6269 plt call stub for them and defining the symbol on the call
6270 stub. We can't do that for ELFv1 anyway (a function symbol
6271 is defined on a descriptor, not code) and it can be faster at
6272 run-time due to not needing to bounce through a stub. The
6273 dyn_relocs for ifuncs will be applied even in a static
6275 if (!bfd_link_pic (info)
6276 && h->type != STT_GNU_IFUNC
6278 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6280 /* Clear procedure linkage table information for any symbol that
6281 won't need a .plt entry. */
6282 struct plt_entry *ent;
6283 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6284 if (ent->plt.refcount > 0)
6287 || (h->type != STT_GNU_IFUNC
6289 && (htab->can_convert_all_inline_plt
6290 || (((struct ppc_link_hash_entry *) h)->tls_mask
6291 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6293 h->plt.plist = NULL;
6295 h->pointer_equality_needed = 0;
6297 else if (abiversion (info->output_bfd) >= 2)
6299 /* Taking a function's address in a read/write section
6300 doesn't require us to define the function symbol in the
6301 executable on a global entry stub. A dynamic reloc can
6302 be used instead. The reason we prefer a few more dynamic
6303 relocs is that calling via a global entry stub costs a
6304 few more instructions, and pointer_equality_needed causes
6305 extra work in ld.so when resolving these symbols. */
6306 if (global_entry_stub (h))
6308 if (!readonly_dynrelocs (h))
6310 h->pointer_equality_needed = 0;
6311 /* If we haven't seen a branch reloc and the symbol
6312 isn't an ifunc then we don't need a plt entry. */
6314 h->plt.plist = NULL;
6316 else if (!bfd_link_pic (info))
6317 /* We are going to be defining the function symbol on the
6318 plt stub, so no dyn_relocs needed when non-pic. */
6319 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6322 /* ELFv2 function symbols can't have copy relocs. */
6325 else if (!h->needs_plt
6326 && !readonly_dynrelocs (h))
6328 /* If we haven't seen a branch reloc and the symbol isn't an
6329 ifunc then we don't need a plt entry. */
6330 h->plt.plist = NULL;
6331 h->pointer_equality_needed = 0;
6336 h->plt.plist = NULL;
6338 /* If this is a weak symbol, and there is a real definition, the
6339 processor independent code will have arranged for us to see the
6340 real definition first, and we can just use the same value. */
6341 if (h->is_weakalias)
6343 struct elf_link_hash_entry *def = weakdef (h);
6344 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6345 h->root.u.def.section = def->root.u.def.section;
6346 h->root.u.def.value = def->root.u.def.value;
6347 if (def->root.u.def.section == htab->elf.sdynbss
6348 || def->root.u.def.section == htab->elf.sdynrelro)
6349 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6353 /* If we are creating a shared library, we must presume that the
6354 only references to the symbol are via the global offset table.
6355 For such cases we need not do anything here; the relocations will
6356 be handled correctly by relocate_section. */
6357 if (bfd_link_pic (info))
6360 /* If there are no references to this symbol that do not use the
6361 GOT, we don't need to generate a copy reloc. */
6362 if (!h->non_got_ref)
6365 /* Don't generate a copy reloc for symbols defined in the executable. */
6366 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6368 /* If -z nocopyreloc was given, don't generate them either. */
6369 || info->nocopyreloc
6371 /* If we don't find any dynamic relocs in read-only sections, then
6372 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6373 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
6375 /* Protected variables do not work with .dynbss. The copy in
6376 .dynbss won't be used by the shared library with the protected
6377 definition for the variable. Text relocations are preferable
6378 to an incorrect program. */
6379 || h->protected_def)
6382 if (h->plt.plist != NULL)
6384 /* We should never get here, but unfortunately there are versions
6385 of gcc out there that improperly (for this ABI) put initialized
6386 function pointers, vtable refs and suchlike in read-only
6387 sections. Allow them to proceed, but warn that this might
6388 break at runtime. */
6389 info->callbacks->einfo
6390 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6391 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6392 h->root.root.string);
6395 /* This is a reference to a symbol defined by a dynamic object which
6396 is not a function. */
6398 /* We must allocate the symbol in our .dynbss section, which will
6399 become part of the .bss section of the executable. There will be
6400 an entry for this symbol in the .dynsym section. The dynamic
6401 object will contain position independent code, so all references
6402 from the dynamic object to this symbol will go through the global
6403 offset table. The dynamic linker will use the .dynsym entry to
6404 determine the address it must put in the global offset table, so
6405 both the dynamic object and the regular object will refer to the
6406 same memory location for the variable. */
6407 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6409 s = htab->elf.sdynrelro;
6410 srel = htab->elf.sreldynrelro;
6414 s = htab->elf.sdynbss;
6415 srel = htab->elf.srelbss;
6417 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6419 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6420 linker to copy the initial value out of the dynamic object
6421 and into the runtime process image. */
6422 srel->size += sizeof (Elf64_External_Rela);
6426 /* We no longer want dyn_relocs. */
6427 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6428 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6431 /* If given a function descriptor symbol, hide both the function code
6432 sym and the descriptor. */
6434 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6435 struct elf_link_hash_entry *h,
6436 bfd_boolean force_local)
6438 struct ppc_link_hash_entry *eh;
6439 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6441 if (ppc_hash_table (info) == NULL)
6444 eh = (struct ppc_link_hash_entry *) h;
6445 if (eh->is_func_descriptor)
6447 struct ppc_link_hash_entry *fh = eh->oh;
6452 struct elf_link_hash_table *htab = elf_hash_table (info);
6455 /* We aren't supposed to use alloca in BFD because on
6456 systems which do not have alloca the version in libiberty
6457 calls xmalloc, which might cause the program to crash
6458 when it runs out of memory. This function doesn't have a
6459 return status, so there's no way to gracefully return an
6460 error. So cheat. We know that string[-1] can be safely
6461 accessed; It's either a string in an ELF string table,
6462 or allocated in an objalloc structure. */
6464 p = eh->elf.root.root.string - 1;
6467 fh = (struct ppc_link_hash_entry *)
6468 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6471 /* Unfortunately, if it so happens that the string we were
6472 looking for was allocated immediately before this string,
6473 then we overwrote the string terminator. That's the only
6474 reason the lookup should fail. */
6477 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6478 while (q >= eh->elf.root.root.string && *q == *p)
6480 if (q < eh->elf.root.root.string && *p == '.')
6481 fh = (struct ppc_link_hash_entry *)
6482 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6491 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6496 get_sym_h (struct elf_link_hash_entry **hp,
6497 Elf_Internal_Sym **symp,
6499 unsigned char **tls_maskp,
6500 Elf_Internal_Sym **locsymsp,
6501 unsigned long r_symndx,
6504 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6506 if (r_symndx >= symtab_hdr->sh_info)
6508 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6509 struct elf_link_hash_entry *h;
6511 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6512 h = elf_follow_link (h);
6520 if (symsecp != NULL)
6522 asection *symsec = NULL;
6523 if (h->root.type == bfd_link_hash_defined
6524 || h->root.type == bfd_link_hash_defweak)
6525 symsec = h->root.u.def.section;
6529 if (tls_maskp != NULL)
6531 struct ppc_link_hash_entry *eh;
6533 eh = (struct ppc_link_hash_entry *) h;
6534 *tls_maskp = &eh->tls_mask;
6539 Elf_Internal_Sym *sym;
6540 Elf_Internal_Sym *locsyms = *locsymsp;
6542 if (locsyms == NULL)
6544 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6545 if (locsyms == NULL)
6546 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6547 symtab_hdr->sh_info,
6548 0, NULL, NULL, NULL);
6549 if (locsyms == NULL)
6551 *locsymsp = locsyms;
6553 sym = locsyms + r_symndx;
6561 if (symsecp != NULL)
6562 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6564 if (tls_maskp != NULL)
6566 struct got_entry **lgot_ents;
6567 unsigned char *tls_mask;
6570 lgot_ents = elf_local_got_ents (ibfd);
6571 if (lgot_ents != NULL)
6573 struct plt_entry **local_plt = (struct plt_entry **)
6574 (lgot_ents + symtab_hdr->sh_info);
6575 unsigned char *lgot_masks = (unsigned char *)
6576 (local_plt + symtab_hdr->sh_info);
6577 tls_mask = &lgot_masks[r_symndx];
6579 *tls_maskp = tls_mask;
6585 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6586 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6587 type suitable for optimization, and 1 otherwise. */
6590 get_tls_mask (unsigned char **tls_maskp,
6591 unsigned long *toc_symndx,
6592 bfd_vma *toc_addend,
6593 Elf_Internal_Sym **locsymsp,
6594 const Elf_Internal_Rela *rel,
6597 unsigned long r_symndx;
6599 struct elf_link_hash_entry *h;
6600 Elf_Internal_Sym *sym;
6604 r_symndx = ELF64_R_SYM (rel->r_info);
6605 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6608 if ((*tls_maskp != NULL
6609 && (**tls_maskp & TLS_TLS) != 0
6610 && **tls_maskp != (TLS_TLS | TLS_MARK))
6612 || ppc64_elf_section_data (sec) == NULL
6613 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6616 /* Look inside a TOC section too. */
6619 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6620 off = h->root.u.def.value;
6623 off = sym->st_value;
6624 off += rel->r_addend;
6625 BFD_ASSERT (off % 8 == 0);
6626 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6627 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6628 if (toc_symndx != NULL)
6629 *toc_symndx = r_symndx;
6630 if (toc_addend != NULL)
6631 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6632 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6634 if ((h == NULL || is_static_defined (h))
6635 && (next_r == -1 || next_r == -2))
6640 /* Find (or create) an entry in the tocsave hash table. */
6642 static struct tocsave_entry *
6643 tocsave_find (struct ppc_link_hash_table *htab,
6644 enum insert_option insert,
6645 Elf_Internal_Sym **local_syms,
6646 const Elf_Internal_Rela *irela,
6649 unsigned long r_indx;
6650 struct elf_link_hash_entry *h;
6651 Elf_Internal_Sym *sym;
6652 struct tocsave_entry ent, *p;
6654 struct tocsave_entry **slot;
6656 r_indx = ELF64_R_SYM (irela->r_info);
6657 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6659 if (ent.sec == NULL || ent.sec->output_section == NULL)
6662 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
6667 ent.offset = h->root.u.def.value;
6669 ent.offset = sym->st_value;
6670 ent.offset += irela->r_addend;
6672 hash = tocsave_htab_hash (&ent);
6673 slot = ((struct tocsave_entry **)
6674 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6680 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6689 /* Adjust all global syms defined in opd sections. In gcc generated
6690 code for the old ABI, these will already have been done. */
6693 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6695 struct ppc_link_hash_entry *eh;
6697 struct _opd_sec_data *opd;
6699 if (h->root.type == bfd_link_hash_indirect)
6702 if (h->root.type != bfd_link_hash_defined
6703 && h->root.type != bfd_link_hash_defweak)
6706 eh = (struct ppc_link_hash_entry *) h;
6707 if (eh->adjust_done)
6710 sym_sec = eh->elf.root.u.def.section;
6711 opd = get_opd_info (sym_sec);
6712 if (opd != NULL && opd->adjust != NULL)
6714 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
6717 /* This entry has been deleted. */
6718 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6721 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6722 if (discarded_section (dsec))
6724 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6728 eh->elf.root.u.def.value = 0;
6729 eh->elf.root.u.def.section = dsec;
6732 eh->elf.root.u.def.value += adjust;
6733 eh->adjust_done = 1;
6738 /* Handles decrementing dynamic reloc counts for the reloc specified by
6739 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
6740 have already been determined. */
6743 dec_dynrel_count (bfd_vma r_info,
6745 struct bfd_link_info *info,
6746 Elf_Internal_Sym **local_syms,
6747 struct elf_link_hash_entry *h,
6748 Elf_Internal_Sym *sym)
6750 enum elf_ppc64_reloc_type r_type;
6751 asection *sym_sec = NULL;
6753 /* Can this reloc be dynamic? This switch, and later tests here
6754 should be kept in sync with the code in check_relocs. */
6755 r_type = ELF64_R_TYPE (r_info);
6761 case R_PPC64_TPREL16:
6762 case R_PPC64_TPREL16_LO:
6763 case R_PPC64_TPREL16_HI:
6764 case R_PPC64_TPREL16_HA:
6765 case R_PPC64_TPREL16_DS:
6766 case R_PPC64_TPREL16_LO_DS:
6767 case R_PPC64_TPREL16_HIGH:
6768 case R_PPC64_TPREL16_HIGHA:
6769 case R_PPC64_TPREL16_HIGHER:
6770 case R_PPC64_TPREL16_HIGHERA:
6771 case R_PPC64_TPREL16_HIGHEST:
6772 case R_PPC64_TPREL16_HIGHESTA:
6773 case R_PPC64_TPREL64:
6774 case R_PPC64_DTPMOD64:
6775 case R_PPC64_DTPREL64:
6776 case R_PPC64_ADDR64:
6780 case R_PPC64_ADDR14:
6781 case R_PPC64_ADDR14_BRNTAKEN:
6782 case R_PPC64_ADDR14_BRTAKEN:
6783 case R_PPC64_ADDR16:
6784 case R_PPC64_ADDR16_DS:
6785 case R_PPC64_ADDR16_HA:
6786 case R_PPC64_ADDR16_HI:
6787 case R_PPC64_ADDR16_HIGH:
6788 case R_PPC64_ADDR16_HIGHA:
6789 case R_PPC64_ADDR16_HIGHER:
6790 case R_PPC64_ADDR16_HIGHERA:
6791 case R_PPC64_ADDR16_HIGHEST:
6792 case R_PPC64_ADDR16_HIGHESTA:
6793 case R_PPC64_ADDR16_LO:
6794 case R_PPC64_ADDR16_LO_DS:
6795 case R_PPC64_ADDR24:
6796 case R_PPC64_ADDR32:
6797 case R_PPC64_UADDR16:
6798 case R_PPC64_UADDR32:
6799 case R_PPC64_UADDR64:
6802 case R_PPC64_D34_LO:
6803 case R_PPC64_D34_HI30:
6804 case R_PPC64_D34_HA30:
6805 case R_PPC64_ADDR16_HIGHER34:
6806 case R_PPC64_ADDR16_HIGHERA34:
6807 case R_PPC64_ADDR16_HIGHEST34:
6808 case R_PPC64_ADDR16_HIGHESTA34:
6813 if (local_syms != NULL)
6815 unsigned long r_symndx;
6816 bfd *ibfd = sec->owner;
6818 r_symndx = ELF64_R_SYM (r_info);
6819 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6823 if ((bfd_link_pic (info)
6824 && (must_be_dyn_reloc (info, r_type)
6826 && (!SYMBOLIC_BIND (info, h)
6827 || h->root.type == bfd_link_hash_defweak
6828 || !h->def_regular))))
6829 || (ELIMINATE_COPY_RELOCS
6830 && !bfd_link_pic (info)
6832 && (h->root.type == bfd_link_hash_defweak
6833 || !h->def_regular)))
6840 struct elf_dyn_relocs *p;
6841 struct elf_dyn_relocs **pp;
6842 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6844 /* elf_gc_sweep may have already removed all dyn relocs associated
6845 with local syms for a given section. Also, symbol flags are
6846 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6847 report a dynreloc miscount. */
6848 if (*pp == NULL && info->gc_sections)
6851 while ((p = *pp) != NULL)
6855 if (!must_be_dyn_reloc (info, r_type))
6867 struct ppc_dyn_relocs *p;
6868 struct ppc_dyn_relocs **pp;
6870 bfd_boolean is_ifunc;
6872 if (local_syms == NULL)
6873 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6874 if (sym_sec == NULL)
6877 vpp = &elf_section_data (sym_sec)->local_dynrel;
6878 pp = (struct ppc_dyn_relocs **) vpp;
6880 if (*pp == NULL && info->gc_sections)
6883 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6884 while ((p = *pp) != NULL)
6886 if (p->sec == sec && p->ifunc == is_ifunc)
6897 /* xgettext:c-format */
6898 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6900 bfd_set_error (bfd_error_bad_value);
6904 /* Remove unused Official Procedure Descriptor entries. Currently we
6905 only remove those associated with functions in discarded link-once
6906 sections, or weakly defined functions that have been overridden. It
6907 would be possible to remove many more entries for statically linked
6911 ppc64_elf_edit_opd (struct bfd_link_info *info)
6914 bfd_boolean some_edited = FALSE;
6915 asection *need_pad = NULL;
6916 struct ppc_link_hash_table *htab;
6918 htab = ppc_hash_table (info);
6922 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6925 Elf_Internal_Rela *relstart, *rel, *relend;
6926 Elf_Internal_Shdr *symtab_hdr;
6927 Elf_Internal_Sym *local_syms;
6928 struct _opd_sec_data *opd;
6929 bfd_boolean need_edit, add_aux_fields, broken;
6930 bfd_size_type cnt_16b = 0;
6932 if (!is_ppc64_elf (ibfd))
6935 sec = bfd_get_section_by_name (ibfd, ".opd");
6936 if (sec == NULL || sec->size == 0)
6939 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6942 if (sec->output_section == bfd_abs_section_ptr)
6945 /* Look through the section relocs. */
6946 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6950 symtab_hdr = &elf_symtab_hdr (ibfd);
6952 /* Read the relocations. */
6953 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6955 if (relstart == NULL)
6958 /* First run through the relocs to check they are sane, and to
6959 determine whether we need to edit this opd section. */
6963 relend = relstart + sec->reloc_count;
6964 for (rel = relstart; rel < relend; )
6966 enum elf_ppc64_reloc_type r_type;
6967 unsigned long r_symndx;
6969 struct elf_link_hash_entry *h;
6970 Elf_Internal_Sym *sym;
6973 /* .opd contains an array of 16 or 24 byte entries. We're
6974 only interested in the reloc pointing to a function entry
6976 offset = rel->r_offset;
6977 if (rel + 1 == relend
6978 || rel[1].r_offset != offset + 8)
6980 /* If someone messes with .opd alignment then after a
6981 "ld -r" we might have padding in the middle of .opd.
6982 Also, there's nothing to prevent someone putting
6983 something silly in .opd with the assembler. No .opd
6984 optimization for them! */
6987 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
6992 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6993 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6996 /* xgettext:c-format */
6997 (_("%pB: unexpected reloc type %u in .opd section"),
7003 r_symndx = ELF64_R_SYM (rel->r_info);
7004 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7008 if (sym_sec == NULL || sym_sec->owner == NULL)
7010 const char *sym_name;
7012 sym_name = h->root.root.string;
7014 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7018 /* xgettext:c-format */
7019 (_("%pB: undefined sym `%s' in .opd section"),
7025 /* opd entries are always for functions defined in the
7026 current input bfd. If the symbol isn't defined in the
7027 input bfd, then we won't be using the function in this
7028 bfd; It must be defined in a linkonce section in another
7029 bfd, or is weak. It's also possible that we are
7030 discarding the function due to a linker script /DISCARD/,
7031 which we test for via the output_section. */
7032 if (sym_sec->owner != ibfd
7033 || sym_sec->output_section == bfd_abs_section_ptr)
7037 if (rel + 1 == relend
7038 || (rel + 2 < relend
7039 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7044 if (sec->size == offset + 24)
7049 if (sec->size == offset + 16)
7056 else if (rel + 1 < relend
7057 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7058 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7060 if (rel[0].r_offset == offset + 16)
7062 else if (rel[0].r_offset != offset + 24)
7069 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7071 if (!broken && (need_edit || add_aux_fields))
7073 Elf_Internal_Rela *write_rel;
7074 Elf_Internal_Shdr *rel_hdr;
7075 bfd_byte *rptr, *wptr;
7076 bfd_byte *new_contents;
7079 new_contents = NULL;
7080 amt = OPD_NDX (sec->size) * sizeof (long);
7081 opd = &ppc64_elf_section_data (sec)->u.opd;
7082 opd->adjust = bfd_zalloc (sec->owner, amt);
7083 if (opd->adjust == NULL)
7086 /* This seems a waste of time as input .opd sections are all
7087 zeros as generated by gcc, but I suppose there's no reason
7088 this will always be so. We might start putting something in
7089 the third word of .opd entries. */
7090 if ((sec->flags & SEC_IN_MEMORY) == 0)
7093 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7098 if (local_syms != NULL
7099 && symtab_hdr->contents != (unsigned char *) local_syms)
7101 if (elf_section_data (sec)->relocs != relstart)
7105 sec->contents = loc;
7106 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7109 elf_section_data (sec)->relocs = relstart;
7111 new_contents = sec->contents;
7114 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7115 if (new_contents == NULL)
7119 wptr = new_contents;
7120 rptr = sec->contents;
7121 write_rel = relstart;
7122 for (rel = relstart; rel < relend; )
7124 unsigned long r_symndx;
7126 struct elf_link_hash_entry *h;
7127 struct ppc_link_hash_entry *fdh = NULL;
7128 Elf_Internal_Sym *sym;
7130 Elf_Internal_Rela *next_rel;
7133 r_symndx = ELF64_R_SYM (rel->r_info);
7134 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7139 if (next_rel + 1 == relend
7140 || (next_rel + 2 < relend
7141 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7144 /* See if the .opd entry is full 24 byte or
7145 16 byte (with fd_aux entry overlapped with next
7148 if (next_rel == relend)
7150 if (sec->size == rel->r_offset + 16)
7153 else if (next_rel->r_offset == rel->r_offset + 16)
7157 && h->root.root.string[0] == '.')
7159 fdh = ((struct ppc_link_hash_entry *) h)->oh;
7162 fdh = ppc_follow_link (fdh);
7163 if (fdh->elf.root.type != bfd_link_hash_defined
7164 && fdh->elf.root.type != bfd_link_hash_defweak)
7169 skip = (sym_sec->owner != ibfd
7170 || sym_sec->output_section == bfd_abs_section_ptr);
7173 if (fdh != NULL && sym_sec->owner == ibfd)
7175 /* Arrange for the function descriptor sym
7177 fdh->elf.root.u.def.value = 0;
7178 fdh->elf.root.u.def.section = sym_sec;
7180 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7182 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7187 if (!dec_dynrel_count (rel->r_info, sec, info,
7191 if (++rel == next_rel)
7194 r_symndx = ELF64_R_SYM (rel->r_info);
7195 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7202 /* We'll be keeping this opd entry. */
7207 /* Redefine the function descriptor symbol to
7208 this location in the opd section. It is
7209 necessary to update the value here rather
7210 than using an array of adjustments as we do
7211 for local symbols, because various places
7212 in the generic ELF code use the value
7213 stored in u.def.value. */
7214 fdh->elf.root.u.def.value = wptr - new_contents;
7215 fdh->adjust_done = 1;
7218 /* Local syms are a bit tricky. We could
7219 tweak them as they can be cached, but
7220 we'd need to look through the local syms
7221 for the function descriptor sym which we
7222 don't have at the moment. So keep an
7223 array of adjustments. */
7224 adjust = (wptr - new_contents) - (rptr - sec->contents);
7225 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7228 memcpy (wptr, rptr, opd_ent_size);
7229 wptr += opd_ent_size;
7230 if (add_aux_fields && opd_ent_size == 16)
7232 memset (wptr, '\0', 8);
7236 /* We need to adjust any reloc offsets to point to the
7238 for ( ; rel != next_rel; ++rel)
7240 rel->r_offset += adjust;
7241 if (write_rel != rel)
7242 memcpy (write_rel, rel, sizeof (*rel));
7247 rptr += opd_ent_size;
7250 sec->size = wptr - new_contents;
7251 sec->reloc_count = write_rel - relstart;
7254 free (sec->contents);
7255 sec->contents = new_contents;
7258 /* Fudge the header size too, as this is used later in
7259 elf_bfd_final_link if we are emitting relocs. */
7260 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7261 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7264 else if (elf_section_data (sec)->relocs != relstart)
7267 if (local_syms != NULL
7268 && symtab_hdr->contents != (unsigned char *) local_syms)
7270 if (!info->keep_memory)
7273 symtab_hdr->contents = (unsigned char *) local_syms;
7278 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7280 /* If we are doing a final link and the last .opd entry is just 16 byte
7281 long, add a 8 byte padding after it. */
7282 if (need_pad != NULL && !bfd_link_relocatable (info))
7286 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7288 BFD_ASSERT (need_pad->size > 0);
7290 p = bfd_malloc (need_pad->size + 8);
7294 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7295 p, 0, need_pad->size))
7298 need_pad->contents = p;
7299 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7303 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7307 need_pad->contents = p;
7310 memset (need_pad->contents + need_pad->size, 0, 8);
7311 need_pad->size += 8;
7317 /* Analyze inline PLT call relocations to see whether calls to locally
7318 defined functions can be converted to direct calls. */
7321 ppc64_elf_inline_plt (struct bfd_link_info *info)
7323 struct ppc_link_hash_table *htab;
7326 bfd_vma low_vma, high_vma, limit;
7328 htab = ppc_hash_table (info);
7332 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7333 reduced somewhat to cater for possible stubs that might be added
7334 between the call and its destination. */
7335 if (htab->params->group_size < 0)
7337 limit = -htab->params->group_size;
7343 limit = htab->params->group_size;
7350 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7351 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7353 if (low_vma > sec->vma)
7355 if (high_vma < sec->vma + sec->size)
7356 high_vma = sec->vma + sec->size;
7359 /* If a "bl" can reach anywhere in local code sections, then we can
7360 convert all inline PLT sequences to direct calls when the symbol
7362 if (high_vma - low_vma < limit)
7364 htab->can_convert_all_inline_plt = 1;
7368 /* Otherwise, go looking through relocs for cases where a direct
7369 call won't reach. Mark the symbol on any such reloc to disable
7370 the optimization and keep the PLT entry as it seems likely that
7371 this will be better than creating trampolines. Note that this
7372 will disable the optimization for all inline PLT calls to a
7373 particular symbol, not just those that won't reach. The
7374 difficulty in doing a more precise optimization is that the
7375 linker needs to make a decision depending on whether a
7376 particular R_PPC64_PLTCALL insn can be turned into a direct
7377 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7378 the sequence, and there is nothing that ties those relocs
7379 together except their symbol. */
7381 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7383 Elf_Internal_Shdr *symtab_hdr;
7384 Elf_Internal_Sym *local_syms;
7386 if (!is_ppc64_elf (ibfd))
7390 symtab_hdr = &elf_symtab_hdr (ibfd);
7392 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7393 if (ppc64_elf_section_data (sec)->has_pltcall
7394 && !bfd_is_abs_section (sec->output_section))
7396 Elf_Internal_Rela *relstart, *rel, *relend;
7398 /* Read the relocations. */
7399 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7401 if (relstart == NULL)
7404 relend = relstart + sec->reloc_count;
7405 for (rel = relstart; rel < relend; )
7407 enum elf_ppc64_reloc_type r_type;
7408 unsigned long r_symndx;
7410 struct elf_link_hash_entry *h;
7411 Elf_Internal_Sym *sym;
7412 unsigned char *tls_maskp;
7414 r_type = ELF64_R_TYPE (rel->r_info);
7415 if (r_type != R_PPC64_PLTCALL
7416 && r_type != R_PPC64_PLTCALL_NOTOC)
7419 r_symndx = ELF64_R_SYM (rel->r_info);
7420 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7423 if (elf_section_data (sec)->relocs != relstart)
7425 if (local_syms != NULL
7426 && symtab_hdr->contents != (bfd_byte *) local_syms)
7431 if (sym_sec != NULL && sym_sec->output_section != NULL)
7435 to = h->root.u.def.value;
7438 to += (rel->r_addend
7439 + sym_sec->output_offset
7440 + sym_sec->output_section->vma);
7441 from = (rel->r_offset
7442 + sec->output_offset
7443 + sec->output_section->vma);
7444 if (to - from + limit < 2 * limit
7445 && !(r_type == R_PPC64_PLTCALL_NOTOC
7446 && (((h ? h->other : sym->st_other)
7447 & STO_PPC64_LOCAL_MASK)
7448 != 1 << STO_PPC64_LOCAL_BIT)))
7449 *tls_maskp &= ~PLT_KEEP;
7452 if (elf_section_data (sec)->relocs != relstart)
7456 if (local_syms != NULL
7457 && symtab_hdr->contents != (unsigned char *) local_syms)
7459 if (!info->keep_memory)
7462 symtab_hdr->contents = (unsigned char *) local_syms;
7469 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7472 ppc64_elf_tls_setup (struct bfd_link_info *info)
7474 struct ppc_link_hash_table *htab;
7476 htab = ppc_hash_table (info);
7480 if (abiversion (info->output_bfd) == 1)
7483 if (htab->params->no_multi_toc)
7484 htab->do_multi_toc = 0;
7485 else if (!htab->do_multi_toc)
7486 htab->params->no_multi_toc = 1;
7488 /* Default to --no-plt-localentry, as this option can cause problems
7489 with symbol interposition. For example, glibc libpthread.so and
7490 libc.so duplicate many pthread symbols, with a fallback
7491 implementation in libc.so. In some cases the fallback does more
7492 work than the pthread implementation. __pthread_condattr_destroy
7493 is one such symbol: the libpthread.so implementation is
7494 localentry:0 while the libc.so implementation is localentry:8.
7495 An app that "cleverly" uses dlopen to only load necessary
7496 libraries at runtime may omit loading libpthread.so when not
7497 running multi-threaded, which then results in the libc.so
7498 fallback symbols being used and ld.so complaining. Now there
7499 are workarounds in ld (see non_zero_localentry) to detect the
7500 pthread situation, but that may not be the only case where
7501 --plt-localentry can cause trouble. */
7502 if (htab->params->plt_localentry0 < 0)
7503 htab->params->plt_localentry0 = 0;
7504 if (htab->params->plt_localentry0
7505 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7506 FALSE, FALSE, FALSE) == NULL)
7508 (_("warning: --plt-localentry is especially dangerous without "
7509 "ld.so support to detect ABI violations"));
7511 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7512 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7513 FALSE, FALSE, TRUE));
7514 /* Move dynamic linking info to the function descriptor sym. */
7515 if (htab->tls_get_addr != NULL)
7516 func_desc_adjust (&htab->tls_get_addr->elf, info);
7517 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7518 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7519 FALSE, FALSE, TRUE));
7520 if (htab->params->tls_get_addr_opt)
7522 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7524 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7525 FALSE, FALSE, TRUE);
7527 func_desc_adjust (opt, info);
7528 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7529 FALSE, FALSE, TRUE);
7531 && (opt_fd->root.type == bfd_link_hash_defined
7532 || opt_fd->root.type == bfd_link_hash_defweak))
7534 /* If glibc supports an optimized __tls_get_addr call stub,
7535 signalled by the presence of __tls_get_addr_opt, and we'll
7536 be calling __tls_get_addr via a plt call stub, then
7537 make __tls_get_addr point to __tls_get_addr_opt. */
7538 tga_fd = &htab->tls_get_addr_fd->elf;
7539 if (htab->elf.dynamic_sections_created
7541 && (tga_fd->type == STT_FUNC
7542 || tga_fd->needs_plt)
7543 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7544 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
7546 struct plt_entry *ent;
7548 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7549 if (ent->plt.refcount > 0)
7553 tga_fd->root.type = bfd_link_hash_indirect;
7554 tga_fd->root.u.i.link = &opt_fd->root;
7555 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7557 if (opt_fd->dynindx != -1)
7559 /* Use __tls_get_addr_opt in dynamic relocations. */
7560 opt_fd->dynindx = -1;
7561 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7562 opt_fd->dynstr_index);
7563 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7566 htab->tls_get_addr_fd
7567 = (struct ppc_link_hash_entry *) opt_fd;
7568 tga = &htab->tls_get_addr->elf;
7569 if (opt != NULL && tga != NULL)
7571 tga->root.type = bfd_link_hash_indirect;
7572 tga->root.u.i.link = &opt->root;
7573 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7575 _bfd_elf_link_hash_hide_symbol (info, opt,
7577 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7579 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7580 htab->tls_get_addr_fd->is_func_descriptor = 1;
7581 if (htab->tls_get_addr != NULL)
7583 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7584 htab->tls_get_addr->is_func = 1;
7589 else if (htab->params->tls_get_addr_opt < 0)
7590 htab->params->tls_get_addr_opt = 0;
7592 return _bfd_elf_tls_setup (info->output_bfd, info);
7595 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7599 branch_reloc_hash_match (const bfd *ibfd,
7600 const Elf_Internal_Rela *rel,
7601 const struct ppc_link_hash_entry *hash1,
7602 const struct ppc_link_hash_entry *hash2)
7604 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7605 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7606 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7608 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7610 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7611 struct elf_link_hash_entry *h;
7613 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7614 h = elf_follow_link (h);
7615 if (h == &hash1->elf || h == &hash2->elf)
7621 /* Run through all the TLS relocs looking for optimization
7622 opportunities. The linker has been hacked (see ppc64elf.em) to do
7623 a preliminary section layout so that we know the TLS segment
7624 offsets. We can't optimize earlier because some optimizations need
7625 to know the tp offset, and we need to optimize before allocating
7626 dynamic relocations. */
7629 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7633 struct ppc_link_hash_table *htab;
7634 unsigned char *toc_ref;
7637 if (!bfd_link_executable (info))
7640 htab = ppc_hash_table (info);
7644 /* Make two passes over the relocs. On the first pass, mark toc
7645 entries involved with tls relocs, and check that tls relocs
7646 involved in setting up a tls_get_addr call are indeed followed by
7647 such a call. If they are not, we can't do any tls optimization.
7648 On the second pass twiddle tls_mask flags to notify
7649 relocate_section that optimization can be done, and adjust got
7650 and plt refcounts. */
7652 for (pass = 0; pass < 2; ++pass)
7653 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7655 Elf_Internal_Sym *locsyms = NULL;
7656 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7658 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7659 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7661 Elf_Internal_Rela *relstart, *rel, *relend;
7662 bfd_boolean found_tls_get_addr_arg = 0;
7664 /* Read the relocations. */
7665 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7667 if (relstart == NULL)
7673 relend = relstart + sec->reloc_count;
7674 for (rel = relstart; rel < relend; rel++)
7676 enum elf_ppc64_reloc_type r_type;
7677 unsigned long r_symndx;
7678 struct elf_link_hash_entry *h;
7679 Elf_Internal_Sym *sym;
7681 unsigned char *tls_mask;
7682 unsigned char tls_set, tls_clear, tls_type = 0;
7684 bfd_boolean ok_tprel, is_local;
7685 long toc_ref_index = 0;
7686 int expecting_tls_get_addr = 0;
7687 bfd_boolean ret = FALSE;
7689 r_symndx = ELF64_R_SYM (rel->r_info);
7690 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7694 if (elf_section_data (sec)->relocs != relstart)
7696 if (toc_ref != NULL)
7699 && (elf_symtab_hdr (ibfd).contents
7700 != (unsigned char *) locsyms))
7707 if (h->root.type == bfd_link_hash_defined
7708 || h->root.type == bfd_link_hash_defweak)
7709 value = h->root.u.def.value;
7710 else if (h->root.type == bfd_link_hash_undefweak)
7714 found_tls_get_addr_arg = 0;
7719 /* Symbols referenced by TLS relocs must be of type
7720 STT_TLS. So no need for .opd local sym adjust. */
7721 value = sym->st_value;
7730 && h->root.type == bfd_link_hash_undefweak)
7732 else if (sym_sec != NULL
7733 && sym_sec->output_section != NULL)
7735 value += sym_sec->output_offset;
7736 value += sym_sec->output_section->vma;
7737 value -= htab->elf.tls_sec->vma;
7738 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7739 < (bfd_vma) 1 << 32);
7743 r_type = ELF64_R_TYPE (rel->r_info);
7744 /* If this section has old-style __tls_get_addr calls
7745 without marker relocs, then check that each
7746 __tls_get_addr call reloc is preceded by a reloc
7747 that conceivably belongs to the __tls_get_addr arg
7748 setup insn. If we don't find matching arg setup
7749 relocs, don't do any tls optimization. */
7751 && sec->has_tls_get_addr_call
7753 && (h == &htab->tls_get_addr->elf
7754 || h == &htab->tls_get_addr_fd->elf)
7755 && !found_tls_get_addr_arg
7756 && is_branch_reloc (r_type))
7758 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7759 "TLS optimization disabled\n"),
7760 ibfd, sec, rel->r_offset);
7765 found_tls_get_addr_arg = 0;
7768 case R_PPC64_GOT_TLSLD16:
7769 case R_PPC64_GOT_TLSLD16_LO:
7770 expecting_tls_get_addr = 1;
7771 found_tls_get_addr_arg = 1;
7774 case R_PPC64_GOT_TLSLD16_HI:
7775 case R_PPC64_GOT_TLSLD16_HA:
7776 /* These relocs should never be against a symbol
7777 defined in a shared lib. Leave them alone if
7778 that turns out to be the case. */
7785 tls_type = TLS_TLS | TLS_LD;
7788 case R_PPC64_GOT_TLSGD16:
7789 case R_PPC64_GOT_TLSGD16_LO:
7790 expecting_tls_get_addr = 1;
7791 found_tls_get_addr_arg = 1;
7794 case R_PPC64_GOT_TLSGD16_HI:
7795 case R_PPC64_GOT_TLSGD16_HA:
7801 tls_set = TLS_TLS | TLS_TPRELGD;
7803 tls_type = TLS_TLS | TLS_GD;
7806 case R_PPC64_GOT_TPREL16_DS:
7807 case R_PPC64_GOT_TPREL16_LO_DS:
7808 case R_PPC64_GOT_TPREL16_HI:
7809 case R_PPC64_GOT_TPREL16_HA:
7814 tls_clear = TLS_TPREL;
7815 tls_type = TLS_TLS | TLS_TPREL;
7822 if (rel + 1 < relend
7823 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7826 && (ELF64_R_TYPE (rel[1].r_info)
7828 && (ELF64_R_TYPE (rel[1].r_info)
7829 != R_PPC64_PLTSEQ_NOTOC))
7831 r_symndx = ELF64_R_SYM (rel[1].r_info);
7832 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
7837 struct plt_entry *ent = NULL;
7839 for (ent = h->plt.plist;
7842 if (ent->addend == rel[1].r_addend)
7846 && ent->plt.refcount > 0)
7847 ent->plt.refcount -= 1;
7852 found_tls_get_addr_arg = 1;
7857 case R_PPC64_TOC16_LO:
7858 if (sym_sec == NULL || sym_sec != toc)
7861 /* Mark this toc entry as referenced by a TLS
7862 code sequence. We can do that now in the
7863 case of R_PPC64_TLS, and after checking for
7864 tls_get_addr for the TOC16 relocs. */
7865 if (toc_ref == NULL)
7867 = bfd_zmalloc (toc->output_section->rawsize / 8);
7868 if (toc_ref == NULL)
7872 value = h->root.u.def.value;
7874 value = sym->st_value;
7875 value += rel->r_addend;
7878 BFD_ASSERT (value < toc->size
7879 && toc->output_offset % 8 == 0);
7880 toc_ref_index = (value + toc->output_offset) / 8;
7881 if (r_type == R_PPC64_TLS
7882 || r_type == R_PPC64_TLSGD
7883 || r_type == R_PPC64_TLSLD)
7885 toc_ref[toc_ref_index] = 1;
7889 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7894 expecting_tls_get_addr = 2;
7897 case R_PPC64_TPREL64:
7901 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7906 tls_set = TLS_EXPLICIT;
7907 tls_clear = TLS_TPREL;
7912 case R_PPC64_DTPMOD64:
7916 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7918 if (rel + 1 < relend
7920 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7921 && rel[1].r_offset == rel->r_offset + 8)
7925 tls_set = TLS_EXPLICIT | TLS_GD;
7928 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7937 tls_set = TLS_EXPLICIT;
7948 if (!expecting_tls_get_addr
7949 || !sec->has_tls_get_addr_call)
7952 if (rel + 1 < relend
7953 && branch_reloc_hash_match (ibfd, rel + 1,
7955 htab->tls_get_addr_fd))
7957 if (expecting_tls_get_addr == 2)
7959 /* Check for toc tls entries. */
7960 unsigned char *toc_tls;
7963 retval = get_tls_mask (&toc_tls, NULL, NULL,
7968 if (toc_tls != NULL)
7970 if ((*toc_tls & TLS_TLS) != 0
7971 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
7972 found_tls_get_addr_arg = 1;
7974 toc_ref[toc_ref_index] = 1;
7980 /* Uh oh, we didn't find the expected call. We
7981 could just mark this symbol to exclude it
7982 from tls optimization but it's safer to skip
7983 the entire optimization. */
7984 /* xgettext:c-format */
7985 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
7986 "TLS optimization disabled\n"),
7987 ibfd, sec, rel->r_offset);
7992 /* If we don't have old-style __tls_get_addr calls
7993 without TLSGD/TLSLD marker relocs, and we haven't
7994 found a new-style __tls_get_addr call with a
7995 marker for this symbol, then we either have a
7996 broken object file or an -mlongcall style
7997 indirect call to __tls_get_addr without a marker.
7998 Disable optimization in this case. */
7999 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8000 && (tls_set & TLS_EXPLICIT) == 0
8001 && !sec->has_tls_get_addr_call
8002 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8003 != (TLS_TLS | TLS_MARK)))
8006 if (expecting_tls_get_addr)
8008 struct plt_entry *ent = NULL;
8010 if (htab->tls_get_addr != NULL)
8011 for (ent = htab->tls_get_addr->elf.plt.plist;
8014 if (ent->addend == 0)
8017 if (ent == NULL && htab->tls_get_addr_fd != NULL)
8018 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8021 if (ent->addend == 0)
8025 && ent->plt.refcount > 0)
8026 ent->plt.refcount -= 1;
8032 if ((tls_set & TLS_EXPLICIT) == 0)
8034 struct got_entry *ent;
8036 /* Adjust got entry for this reloc. */
8040 ent = elf_local_got_ents (ibfd)[r_symndx];
8042 for (; ent != NULL; ent = ent->next)
8043 if (ent->addend == rel->r_addend
8044 && ent->owner == ibfd
8045 && ent->tls_type == tls_type)
8052 /* We managed to get rid of a got entry. */
8053 if (ent->got.refcount > 0)
8054 ent->got.refcount -= 1;
8059 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8060 we'll lose one or two dyn relocs. */
8061 if (!dec_dynrel_count (rel->r_info, sec, info,
8065 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8067 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8073 *tls_mask |= tls_set;
8074 *tls_mask &= ~tls_clear;
8077 if (elf_section_data (sec)->relocs != relstart)
8082 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8084 if (!info->keep_memory)
8087 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8091 if (toc_ref != NULL)
8093 htab->do_tls_opt = 1;
8097 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8098 the values of any global symbols in a toc section that has been
8099 edited. Globals in toc sections should be a rarity, so this function
8100 sets a flag if any are found in toc sections other than the one just
8101 edited, so that further hash table traversals can be avoided. */
8103 struct adjust_toc_info
8106 unsigned long *skip;
8107 bfd_boolean global_toc_syms;
8110 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8113 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8115 struct ppc_link_hash_entry *eh;
8116 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8119 if (h->root.type != bfd_link_hash_defined
8120 && h->root.type != bfd_link_hash_defweak)
8123 eh = (struct ppc_link_hash_entry *) h;
8124 if (eh->adjust_done)
8127 if (eh->elf.root.u.def.section == toc_inf->toc)
8129 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8130 i = toc_inf->toc->rawsize >> 3;
8132 i = eh->elf.root.u.def.value >> 3;
8134 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8137 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8140 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8141 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8144 eh->elf.root.u.def.value -= toc_inf->skip[i];
8145 eh->adjust_done = 1;
8147 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8148 toc_inf->global_toc_syms = TRUE;
8153 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8154 on a _LO variety toc/got reloc. */
8157 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8159 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8160 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8161 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8162 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8163 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8164 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8165 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8166 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8167 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8168 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8169 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8170 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8171 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8172 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8173 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8174 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8175 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8176 /* Exclude lfqu by testing reloc. If relocs are ever
8177 defined for the reduced D field in psq_lu then those
8178 will need testing too. */
8179 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8180 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8182 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8183 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8184 /* Exclude stfqu. psq_stu as above for psq_lu. */
8185 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8186 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8187 && (insn & 1) == 0));
8190 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8191 pld ra,symbol@got@pcrel
8194 paddi ra,symbol@pcrel
8196 may be translated to
8197 pload/pstore rt,symbol@pcrel
8199 This function returns true if the optimization is possible, placing
8200 the prefix insn in *PINSN1 and a NOP in *PINSN2.
8202 On entry to this function, the linker has already determined that
8203 the pld can be replaced with paddi: *PINSN1 is that paddi insn,
8204 while *PINSN2 is the second instruction. */
8207 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2)
8209 uint32_t insn2 = *pinsn2 >> 32;
8212 /* Check that regs match. */
8213 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
8216 switch ((insn2 >> 26) & 63)
8232 /* These are the PMLS cases, where we just need to tack a prefix
8233 on the insn. Check that the D field is zero. */
8234 if ((insn2 & 0xffff) != 0)
8236 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8237 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8240 case 58: /* lwa, ld */
8241 if ((insn2 & 0xfffd) != 0)
8243 i1new = ((1ULL << 58) | (1ULL << 52)
8244 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8245 | (insn2 & (31ULL << 21)));
8248 case 57: /* lxsd, lxssp */
8249 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
8251 i1new = ((1ULL << 58) | (1ULL << 52)
8252 | ((40ULL | (insn2 & 3)) << 26)
8253 | (insn2 & (31ULL << 21)));
8256 case 61: /* stxsd, stxssp, lxv, stxv */
8257 if ((insn2 & 3) == 0)
8259 else if ((insn2 & 3) >= 2)
8261 if ((insn2 & 0xfffc) != 0)
8263 i1new = ((1ULL << 58) | (1ULL << 52)
8264 | ((44ULL | (insn2 & 3)) << 26)
8265 | (insn2 & (31ULL << 21)));
8269 if ((insn2 & 0xfff0) != 0)
8271 i1new = ((1ULL << 58) | (1ULL << 52)
8272 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8273 | (insn2 & (31ULL << 21)));
8278 if ((insn2 & 0xffff) != 0)
8280 i1new = ((1ULL << 58) | (1ULL << 52)
8281 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8284 case 62: /* std, stq */
8285 if ((insn2 & 0xfffd) != 0)
8287 i1new = ((1ULL << 58) | (1ULL << 52)
8288 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8289 | (insn2 & (31ULL << 21)));
8294 *pinsn2 = (uint64_t) NOP << 32;
8298 /* Examine all relocs referencing .toc sections in order to remove
8299 unused .toc entries. */
8302 ppc64_elf_edit_toc (struct bfd_link_info *info)
8305 struct adjust_toc_info toc_inf;
8306 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8308 htab->do_toc_opt = 1;
8309 toc_inf.global_toc_syms = TRUE;
8310 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8312 asection *toc, *sec;
8313 Elf_Internal_Shdr *symtab_hdr;
8314 Elf_Internal_Sym *local_syms;
8315 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8316 unsigned long *skip, *drop;
8317 unsigned char *used;
8318 unsigned char *keep, last, some_unused;
8320 if (!is_ppc64_elf (ibfd))
8323 toc = bfd_get_section_by_name (ibfd, ".toc");
8326 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8327 || discarded_section (toc))
8332 symtab_hdr = &elf_symtab_hdr (ibfd);
8334 /* Look at sections dropped from the final link. */
8337 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8339 if (sec->reloc_count == 0
8340 || !discarded_section (sec)
8341 || get_opd_info (sec)
8342 || (sec->flags & SEC_ALLOC) == 0
8343 || (sec->flags & SEC_DEBUGGING) != 0)
8346 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8347 if (relstart == NULL)
8350 /* Run through the relocs to see which toc entries might be
8352 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8354 enum elf_ppc64_reloc_type r_type;
8355 unsigned long r_symndx;
8357 struct elf_link_hash_entry *h;
8358 Elf_Internal_Sym *sym;
8361 r_type = ELF64_R_TYPE (rel->r_info);
8368 case R_PPC64_TOC16_LO:
8369 case R_PPC64_TOC16_HI:
8370 case R_PPC64_TOC16_HA:
8371 case R_PPC64_TOC16_DS:
8372 case R_PPC64_TOC16_LO_DS:
8376 r_symndx = ELF64_R_SYM (rel->r_info);
8377 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8385 val = h->root.u.def.value;
8387 val = sym->st_value;
8388 val += rel->r_addend;
8390 if (val >= toc->size)
8393 /* Anything in the toc ought to be aligned to 8 bytes.
8394 If not, don't mark as unused. */
8400 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8405 skip[val >> 3] = ref_from_discarded;
8408 if (elf_section_data (sec)->relocs != relstart)
8412 /* For largetoc loads of address constants, we can convert
8413 . addis rx,2,addr@got@ha
8414 . ld ry,addr@got@l(rx)
8416 . addis rx,2,addr@toc@ha
8417 . addi ry,rx,addr@toc@l
8418 when addr is within 2G of the toc pointer. This then means
8419 that the word storing "addr" in the toc is no longer needed. */
8421 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8422 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8423 && toc->reloc_count != 0)
8425 /* Read toc relocs. */
8426 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8428 if (toc_relocs == NULL)
8431 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8433 enum elf_ppc64_reloc_type r_type;
8434 unsigned long r_symndx;
8436 struct elf_link_hash_entry *h;
8437 Elf_Internal_Sym *sym;
8440 r_type = ELF64_R_TYPE (rel->r_info);
8441 if (r_type != R_PPC64_ADDR64)
8444 r_symndx = ELF64_R_SYM (rel->r_info);
8445 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8450 || sym_sec->output_section == NULL
8451 || discarded_section (sym_sec))
8454 if (!SYMBOL_REFERENCES_LOCAL (info, h))
8459 if (h->type == STT_GNU_IFUNC)
8461 val = h->root.u.def.value;
8465 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8467 val = sym->st_value;
8469 val += rel->r_addend;
8470 val += sym_sec->output_section->vma + sym_sec->output_offset;
8472 /* We don't yet know the exact toc pointer value, but we
8473 know it will be somewhere in the toc section. Don't
8474 optimize if the difference from any possible toc
8475 pointer is outside [ff..f80008000, 7fff7fff]. */
8476 addr = toc->output_section->vma + TOC_BASE_OFF;
8477 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8480 addr = toc->output_section->vma + toc->output_section->rawsize;
8481 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8486 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8491 skip[rel->r_offset >> 3]
8492 |= can_optimize | ((rel - toc_relocs) << 2);
8499 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8503 if (local_syms != NULL
8504 && symtab_hdr->contents != (unsigned char *) local_syms)
8508 && elf_section_data (sec)->relocs != relstart)
8510 if (toc_relocs != NULL
8511 && elf_section_data (toc)->relocs != toc_relocs)
8518 /* Now check all kept sections that might reference the toc.
8519 Check the toc itself last. */
8520 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8523 sec = (sec == toc ? NULL
8524 : sec->next == NULL ? toc
8525 : sec->next == toc && toc->next ? toc->next
8530 if (sec->reloc_count == 0
8531 || discarded_section (sec)
8532 || get_opd_info (sec)
8533 || (sec->flags & SEC_ALLOC) == 0
8534 || (sec->flags & SEC_DEBUGGING) != 0)
8537 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8539 if (relstart == NULL)
8545 /* Mark toc entries referenced as used. */
8549 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8551 enum elf_ppc64_reloc_type r_type;
8552 unsigned long r_symndx;
8554 struct elf_link_hash_entry *h;
8555 Elf_Internal_Sym *sym;
8557 enum {no_check, check_lo, check_ha} insn_check;
8559 r_type = ELF64_R_TYPE (rel->r_info);
8563 insn_check = no_check;
8566 case R_PPC64_GOT_TLSLD16_HA:
8567 case R_PPC64_GOT_TLSGD16_HA:
8568 case R_PPC64_GOT_TPREL16_HA:
8569 case R_PPC64_GOT_DTPREL16_HA:
8570 case R_PPC64_GOT16_HA:
8571 case R_PPC64_TOC16_HA:
8572 insn_check = check_ha;
8575 case R_PPC64_GOT_TLSLD16_LO:
8576 case R_PPC64_GOT_TLSGD16_LO:
8577 case R_PPC64_GOT_TPREL16_LO_DS:
8578 case R_PPC64_GOT_DTPREL16_LO_DS:
8579 case R_PPC64_GOT16_LO:
8580 case R_PPC64_GOT16_LO_DS:
8581 case R_PPC64_TOC16_LO:
8582 case R_PPC64_TOC16_LO_DS:
8583 insn_check = check_lo;
8587 if (insn_check != no_check)
8589 bfd_vma off = rel->r_offset & ~3;
8590 unsigned char buf[4];
8593 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8598 insn = bfd_get_32 (ibfd, buf);
8599 if (insn_check == check_lo
8600 ? !ok_lo_toc_insn (insn, r_type)
8601 : ((insn & ((0x3f << 26) | 0x1f << 16))
8602 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8606 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8607 sprintf (str, "%#08x", insn);
8608 info->callbacks->einfo
8609 /* xgettext:c-format */
8610 (_("%H: toc optimization is not supported for"
8611 " %s instruction\n"),
8612 ibfd, sec, rel->r_offset & ~3, str);
8619 case R_PPC64_TOC16_LO:
8620 case R_PPC64_TOC16_HI:
8621 case R_PPC64_TOC16_HA:
8622 case R_PPC64_TOC16_DS:
8623 case R_PPC64_TOC16_LO_DS:
8624 /* In case we're taking addresses of toc entries. */
8625 case R_PPC64_ADDR64:
8632 r_symndx = ELF64_R_SYM (rel->r_info);
8633 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8644 val = h->root.u.def.value;
8646 val = sym->st_value;
8647 val += rel->r_addend;
8649 if (val >= toc->size)
8652 if ((skip[val >> 3] & can_optimize) != 0)
8659 case R_PPC64_TOC16_HA:
8662 case R_PPC64_TOC16_LO_DS:
8663 off = rel->r_offset;
8664 off += (bfd_big_endian (ibfd) ? -2 : 3);
8665 if (!bfd_get_section_contents (ibfd, sec, &opc,
8671 if ((opc & (0x3f << 2)) == (58u << 2))
8676 /* Wrong sort of reloc, or not a ld. We may
8677 as well clear ref_from_discarded too. */
8684 /* For the toc section, we only mark as used if this
8685 entry itself isn't unused. */
8686 else if ((used[rel->r_offset >> 3]
8687 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8690 /* Do all the relocs again, to catch reference
8699 if (elf_section_data (sec)->relocs != relstart)
8703 /* Merge the used and skip arrays. Assume that TOC
8704 doublewords not appearing as either used or unused belong
8705 to an entry more than one doubleword in size. */
8706 for (drop = skip, keep = used, last = 0, some_unused = 0;
8707 drop < skip + (toc->size + 7) / 8;
8712 *drop &= ~ref_from_discarded;
8713 if ((*drop & can_optimize) != 0)
8717 else if ((*drop & ref_from_discarded) != 0)
8720 last = ref_from_discarded;
8730 bfd_byte *contents, *src;
8732 Elf_Internal_Sym *sym;
8733 bfd_boolean local_toc_syms = FALSE;
8735 /* Shuffle the toc contents, and at the same time convert the
8736 skip array from booleans into offsets. */
8737 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8740 elf_section_data (toc)->this_hdr.contents = contents;
8742 for (src = contents, off = 0, drop = skip;
8743 src < contents + toc->size;
8746 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8751 memcpy (src - off, src, 8);
8755 toc->rawsize = toc->size;
8756 toc->size = src - contents - off;
8758 /* Adjust addends for relocs against the toc section sym,
8759 and optimize any accesses we can. */
8760 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8762 if (sec->reloc_count == 0
8763 || discarded_section (sec))
8766 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8768 if (relstart == NULL)
8771 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8773 enum elf_ppc64_reloc_type r_type;
8774 unsigned long r_symndx;
8776 struct elf_link_hash_entry *h;
8779 r_type = ELF64_R_TYPE (rel->r_info);
8786 case R_PPC64_TOC16_LO:
8787 case R_PPC64_TOC16_HI:
8788 case R_PPC64_TOC16_HA:
8789 case R_PPC64_TOC16_DS:
8790 case R_PPC64_TOC16_LO_DS:
8791 case R_PPC64_ADDR64:
8795 r_symndx = ELF64_R_SYM (rel->r_info);
8796 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8804 val = h->root.u.def.value;
8807 val = sym->st_value;
8809 local_toc_syms = TRUE;
8812 val += rel->r_addend;
8814 if (val > toc->rawsize)
8816 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8818 else if ((skip[val >> 3] & can_optimize) != 0)
8820 Elf_Internal_Rela *tocrel
8821 = toc_relocs + (skip[val >> 3] >> 2);
8822 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8826 case R_PPC64_TOC16_HA:
8827 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8830 case R_PPC64_TOC16_LO_DS:
8831 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8835 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8837 info->callbacks->einfo
8838 /* xgettext:c-format */
8839 (_("%H: %s references "
8840 "optimized away TOC entry\n"),
8841 ibfd, sec, rel->r_offset,
8842 ppc64_elf_howto_table[r_type]->name);
8843 bfd_set_error (bfd_error_bad_value);
8846 rel->r_addend = tocrel->r_addend;
8847 elf_section_data (sec)->relocs = relstart;
8851 if (h != NULL || sym->st_value != 0)
8854 rel->r_addend -= skip[val >> 3];
8855 elf_section_data (sec)->relocs = relstart;
8858 if (elf_section_data (sec)->relocs != relstart)
8862 /* We shouldn't have local or global symbols defined in the TOC,
8863 but handle them anyway. */
8864 if (local_syms != NULL)
8865 for (sym = local_syms;
8866 sym < local_syms + symtab_hdr->sh_info;
8868 if (sym->st_value != 0
8869 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8873 if (sym->st_value > toc->rawsize)
8874 i = toc->rawsize >> 3;
8876 i = sym->st_value >> 3;
8878 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8882 (_("%s defined on removed toc entry"),
8883 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8886 while ((skip[i] & (ref_from_discarded | can_optimize)));
8887 sym->st_value = (bfd_vma) i << 3;
8890 sym->st_value -= skip[i];
8891 symtab_hdr->contents = (unsigned char *) local_syms;
8894 /* Adjust any global syms defined in this toc input section. */
8895 if (toc_inf.global_toc_syms)
8898 toc_inf.skip = skip;
8899 toc_inf.global_toc_syms = FALSE;
8900 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8904 if (toc->reloc_count != 0)
8906 Elf_Internal_Shdr *rel_hdr;
8907 Elf_Internal_Rela *wrel;
8910 /* Remove unused toc relocs, and adjust those we keep. */
8911 if (toc_relocs == NULL)
8912 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8914 if (toc_relocs == NULL)
8918 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8919 if ((skip[rel->r_offset >> 3]
8920 & (ref_from_discarded | can_optimize)) == 0)
8922 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8923 wrel->r_info = rel->r_info;
8924 wrel->r_addend = rel->r_addend;
8927 else if (!dec_dynrel_count (rel->r_info, toc, info,
8928 &local_syms, NULL, NULL))
8931 elf_section_data (toc)->relocs = toc_relocs;
8932 toc->reloc_count = wrel - toc_relocs;
8933 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8934 sz = rel_hdr->sh_entsize;
8935 rel_hdr->sh_size = toc->reloc_count * sz;
8938 else if (toc_relocs != NULL
8939 && elf_section_data (toc)->relocs != toc_relocs)
8942 if (local_syms != NULL
8943 && symtab_hdr->contents != (unsigned char *) local_syms)
8945 if (!info->keep_memory)
8948 symtab_hdr->contents = (unsigned char *) local_syms;
8953 /* Look for cases where we can change an indirect GOT access to
8954 a GOT relative or PC relative access, possibly reducing the
8955 number of GOT entries. */
8956 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8959 Elf_Internal_Shdr *symtab_hdr;
8960 Elf_Internal_Sym *local_syms;
8961 Elf_Internal_Rela *relstart, *rel;
8964 if (!is_ppc64_elf (ibfd))
8967 if (!ppc64_elf_tdata (ibfd)->has_gotrel)
8970 sec = ppc64_elf_tdata (ibfd)->got;
8971 got = sec->output_section->vma + sec->output_offset + 0x8000;
8974 symtab_hdr = &elf_symtab_hdr (ibfd);
8976 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8978 if (sec->reloc_count == 0
8979 || !ppc64_elf_section_data (sec)->has_gotrel
8980 || discarded_section (sec))
8983 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8985 if (relstart == NULL)
8988 if (local_syms != NULL
8989 && symtab_hdr->contents != (unsigned char *) local_syms)
8993 && elf_section_data (sec)->relocs != relstart)
8998 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9000 enum elf_ppc64_reloc_type r_type;
9001 unsigned long r_symndx;
9002 Elf_Internal_Sym *sym;
9004 struct elf_link_hash_entry *h;
9005 struct got_entry *ent;
9006 bfd_vma sym_addend, val, pc;
9007 unsigned char buf[8];
9010 r_type = ELF64_R_TYPE (rel->r_info);
9016 case R_PPC64_GOT16_DS:
9017 case R_PPC64_GOT16_HA:
9018 case R_PPC64_GOT16_LO_DS:
9019 sym_addend = rel->r_addend;
9022 case R_PPC64_GOT_PCREL34:
9027 r_symndx = ELF64_R_SYM (rel->r_info);
9028 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9032 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9036 val = h->root.u.def.value;
9038 val = sym->st_value;
9040 val += sym_sec->output_section->vma + sym_sec->output_offset;
9047 case R_PPC64_GOT16_DS:
9048 if (val - got + 0x8000 >= 0x10000)
9050 if (!bfd_get_section_contents (ibfd, sec, buf,
9051 rel->r_offset & ~3, 4))
9053 insn = bfd_get_32 (ibfd, buf);
9054 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9058 case R_PPC64_GOT16_HA:
9059 if (val - got + 0x80008000ULL >= 0x100000000ULL)
9062 if (!bfd_get_section_contents (ibfd, sec, buf,
9063 rel->r_offset & ~3, 4))
9065 insn = bfd_get_32 (ibfd, buf);
9066 if (((insn & ((0x3f << 26) | 0x1f << 16))
9067 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9071 case R_PPC64_GOT16_LO_DS:
9072 if (val - got + 0x80008000ULL >= 0x100000000ULL)
9074 if (!bfd_get_section_contents (ibfd, sec, buf,
9075 rel->r_offset & ~3, 4))
9077 insn = bfd_get_32 (ibfd, buf);
9078 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9082 case R_PPC64_GOT_PCREL34:
9084 pc += sec->output_section->vma + sec->output_offset;
9085 if (val - pc + (1ULL << 33) >= 1ULL << 34)
9087 if (!bfd_get_section_contents (ibfd, sec, buf,
9088 rel->r_offset & ~3, 8))
9090 insn = bfd_get_32 (ibfd, buf);
9091 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9093 insn = bfd_get_32 (ibfd, buf + 4);
9094 if ((insn & (0x3f << 26)) != 57u << 26)
9103 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9104 ent = local_got_ents[r_symndx];
9106 for (; ent != NULL; ent = ent->next)
9107 if (ent->addend == sym_addend
9108 && ent->owner == ibfd
9109 && ent->tls_type == 0)
9111 BFD_ASSERT (ent && ent->got.refcount > 0);
9112 ent->got.refcount -= 1;
9115 if (elf_section_data (sec)->relocs != relstart)
9119 if (local_syms != NULL
9120 && symtab_hdr->contents != (unsigned char *) local_syms)
9122 if (!info->keep_memory)
9125 symtab_hdr->contents = (unsigned char *) local_syms;
9132 /* Return true iff input section I references the TOC using
9133 instructions limited to +/-32k offsets. */
9136 ppc64_elf_has_small_toc_reloc (asection *i)
9138 return (is_ppc64_elf (i->owner)
9139 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9142 /* Allocate space for one GOT entry. */
9145 allocate_got (struct elf_link_hash_entry *h,
9146 struct bfd_link_info *info,
9147 struct got_entry *gent)
9149 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9150 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9151 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9153 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9154 ? 2 : 1) * sizeof (Elf64_External_Rela);
9155 asection *got = ppc64_elf_tdata (gent->owner)->got;
9157 gent->got.offset = got->size;
9158 got->size += entsize;
9160 if (h->type == STT_GNU_IFUNC)
9162 htab->elf.irelplt->size += rentsize;
9163 htab->got_reli_size += rentsize;
9165 else if (((bfd_link_pic (info)
9166 && !((gent->tls_type & TLS_TPREL) != 0
9167 && bfd_link_executable (info)
9168 && SYMBOL_REFERENCES_LOCAL (info, h)))
9169 || (htab->elf.dynamic_sections_created
9171 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9172 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9174 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9175 relgot->size += rentsize;
9179 /* This function merges got entries in the same toc group. */
9182 merge_got_entries (struct got_entry **pent)
9184 struct got_entry *ent, *ent2;
9186 for (ent = *pent; ent != NULL; ent = ent->next)
9187 if (!ent->is_indirect)
9188 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9189 if (!ent2->is_indirect
9190 && ent2->addend == ent->addend
9191 && ent2->tls_type == ent->tls_type
9192 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9194 ent2->is_indirect = TRUE;
9195 ent2->got.ent = ent;
9199 /* If H is undefined, make it dynamic if that makes sense. */
9202 ensure_undef_dynamic (struct bfd_link_info *info,
9203 struct elf_link_hash_entry *h)
9205 struct elf_link_hash_table *htab = elf_hash_table (info);
9207 if (htab->dynamic_sections_created
9208 && ((info->dynamic_undefined_weak != 0
9209 && h->root.type == bfd_link_hash_undefweak)
9210 || h->root.type == bfd_link_hash_undefined)
9213 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9214 return bfd_elf_link_record_dynamic_symbol (info, h);
9218 /* Allocate space in .plt, .got and associated reloc sections for
9222 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9224 struct bfd_link_info *info;
9225 struct ppc_link_hash_table *htab;
9227 struct ppc_link_hash_entry *eh;
9228 struct got_entry **pgent, *gent;
9230 if (h->root.type == bfd_link_hash_indirect)
9233 info = (struct bfd_link_info *) inf;
9234 htab = ppc_hash_table (info);
9238 eh = (struct ppc_link_hash_entry *) h;
9239 /* Run through the TLS GD got entries first if we're changing them
9241 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
9242 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9243 if (gent->got.refcount > 0
9244 && (gent->tls_type & TLS_GD) != 0)
9246 /* This was a GD entry that has been converted to TPREL. If
9247 there happens to be a TPREL entry we can use that one. */
9248 struct got_entry *ent;
9249 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9250 if (ent->got.refcount > 0
9251 && (ent->tls_type & TLS_TPREL) != 0
9252 && ent->addend == gent->addend
9253 && ent->owner == gent->owner)
9255 gent->got.refcount = 0;
9259 /* If not, then we'll be using our own TPREL entry. */
9260 if (gent->got.refcount != 0)
9261 gent->tls_type = TLS_TLS | TLS_TPREL;
9264 /* Remove any list entry that won't generate a word in the GOT before
9265 we call merge_got_entries. Otherwise we risk merging to empty
9267 pgent = &h->got.glist;
9268 while ((gent = *pgent) != NULL)
9269 if (gent->got.refcount > 0)
9271 if ((gent->tls_type & TLS_LD) != 0
9274 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9275 *pgent = gent->next;
9278 pgent = &gent->next;
9281 *pgent = gent->next;
9283 if (!htab->do_multi_toc)
9284 merge_got_entries (&h->got.glist);
9286 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9287 if (!gent->is_indirect)
9289 /* Make sure this symbol is output as a dynamic symbol. */
9290 if (!ensure_undef_dynamic (info, h))
9293 if (!is_ppc64_elf (gent->owner))
9296 allocate_got (h, info, gent);
9299 /* If no dynamic sections we can't have dynamic relocs, except for
9300 IFUNCs which are handled even in static executables. */
9301 if (!htab->elf.dynamic_sections_created
9302 && h->type != STT_GNU_IFUNC)
9303 eh->dyn_relocs = NULL;
9305 /* Discard relocs on undefined symbols that must be local. */
9306 else if (h->root.type == bfd_link_hash_undefined
9307 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9308 eh->dyn_relocs = NULL;
9310 /* Also discard relocs on undefined weak syms with non-default
9311 visibility, or when dynamic_undefined_weak says so. */
9312 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9313 eh->dyn_relocs = NULL;
9315 if (eh->dyn_relocs != NULL)
9317 struct elf_dyn_relocs *p, **pp;
9319 /* In the shared -Bsymbolic case, discard space allocated for
9320 dynamic pc-relative relocs against symbols which turn out to
9321 be defined in regular objects. For the normal shared case,
9322 discard space for relocs that have become local due to symbol
9323 visibility changes. */
9325 if (bfd_link_pic (info))
9327 /* Relocs that use pc_count are those that appear on a call
9328 insn, or certain REL relocs (see must_be_dyn_reloc) that
9329 can be generated via assembly. We want calls to
9330 protected symbols to resolve directly to the function
9331 rather than going via the plt. If people want function
9332 pointer comparisons to work as expected then they should
9333 avoid writing weird assembly. */
9334 if (SYMBOL_CALLS_LOCAL (info, h))
9336 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9338 p->count -= p->pc_count;
9347 if (eh->dyn_relocs != NULL)
9349 /* Make sure this symbol is output as a dynamic symbol. */
9350 if (!ensure_undef_dynamic (info, h))
9354 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
9356 /* For the non-pic case, discard space for relocs against
9357 symbols which turn out to need copy relocs or are not
9359 if (h->dynamic_adjusted
9361 && !ELF_COMMON_DEF_P (h))
9363 /* Make sure this symbol is output as a dynamic symbol. */
9364 if (!ensure_undef_dynamic (info, h))
9367 if (h->dynindx == -1)
9368 eh->dyn_relocs = NULL;
9371 eh->dyn_relocs = NULL;
9374 /* Finally, allocate space. */
9375 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9377 asection *sreloc = elf_section_data (p->sec)->sreloc;
9378 if (eh->elf.type == STT_GNU_IFUNC)
9379 sreloc = htab->elf.irelplt;
9380 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9384 /* We might need a PLT entry when the symbol
9387 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9388 d) has plt16 relocs and we are linking statically. */
9389 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9390 || h->type == STT_GNU_IFUNC
9391 || (h->needs_plt && h->dynamic_adjusted)
9394 && !htab->elf.dynamic_sections_created
9395 && !htab->can_convert_all_inline_plt
9396 && (((struct ppc_link_hash_entry *) h)->tls_mask
9397 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
9399 struct plt_entry *pent;
9400 bfd_boolean doneone = FALSE;
9401 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9402 if (pent->plt.refcount > 0)
9404 if (!htab->elf.dynamic_sections_created
9405 || h->dynindx == -1)
9407 if (h->type == STT_GNU_IFUNC)
9410 pent->plt.offset = s->size;
9411 s->size += PLT_ENTRY_SIZE (htab);
9412 s = htab->elf.irelplt;
9417 pent->plt.offset = s->size;
9418 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9419 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9424 /* If this is the first .plt entry, make room for the special
9428 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9430 pent->plt.offset = s->size;
9432 /* Make room for this entry. */
9433 s->size += PLT_ENTRY_SIZE (htab);
9435 /* Make room for the .glink code. */
9438 s->size += GLINK_PLTRESOLVE_SIZE (htab);
9441 /* We need bigger stubs past index 32767. */
9442 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
9449 /* We also need to make an entry in the .rela.plt section. */
9450 s = htab->elf.srelplt;
9453 s->size += sizeof (Elf64_External_Rela);
9457 pent->plt.offset = (bfd_vma) -1;
9460 h->plt.plist = NULL;
9466 h->plt.plist = NULL;
9473 #define PPC_LO(v) ((v) & 0xffff)
9474 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9475 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9477 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9478 #define HA34(v) ((v + (1ULL << 33)) >> 34)
9480 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9481 to set up space for global entry stubs. These are put in glink,
9482 after the branch table. */
9485 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9487 struct bfd_link_info *info;
9488 struct ppc_link_hash_table *htab;
9489 struct plt_entry *pent;
9492 if (h->root.type == bfd_link_hash_indirect)
9495 if (!h->pointer_equality_needed)
9502 htab = ppc_hash_table (info);
9506 s = htab->global_entry;
9507 plt = htab->elf.splt;
9508 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9509 if (pent->plt.offset != (bfd_vma) -1
9510 && pent->addend == 0)
9512 /* For ELFv2, if this symbol is not defined in a regular file
9513 and we are not generating a shared library or pie, then we
9514 need to define the symbol in the executable on a call stub.
9515 This is to avoid text relocations. */
9516 bfd_vma off, stub_align, stub_off, stub_size;
9517 unsigned int align_power;
9521 if (htab->params->plt_stub_align >= 0)
9522 align_power = htab->params->plt_stub_align;
9524 align_power = -htab->params->plt_stub_align;
9525 /* Setting section alignment is delayed until we know it is
9526 non-empty. Otherwise the .text output section will be
9527 aligned at least to plt_stub_align even when no global
9528 entry stubs are needed. */
9529 if (s->alignment_power < align_power)
9530 s->alignment_power = align_power;
9531 stub_align = (bfd_vma) 1 << align_power;
9532 if (htab->params->plt_stub_align >= 0
9533 || ((((stub_off + stub_size - 1) & -stub_align)
9534 - (stub_off & -stub_align))
9535 > ((stub_size - 1) & -stub_align)))
9536 stub_off = (stub_off + stub_align - 1) & -stub_align;
9537 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9538 off -= stub_off + s->output_offset + s->output_section->vma;
9539 /* Note that for --plt-stub-align negative we have a possible
9540 dependency between stub offset and size. Break that
9541 dependency by assuming the max stub size when calculating
9543 if (PPC_HA (off) == 0)
9545 h->root.type = bfd_link_hash_defined;
9546 h->root.u.def.section = s;
9547 h->root.u.def.value = stub_off;
9548 s->size = stub_off + stub_size;
9554 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9555 read-only sections. */
9558 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
9562 if (h->root.type == bfd_link_hash_indirect)
9565 sec = readonly_dynrelocs (h);
9568 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9570 info->flags |= DF_TEXTREL;
9571 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9572 " in read-only section `%pA'\n"),
9573 sec->owner, h->root.root.string, sec);
9575 /* Not an error, just cut short the traversal. */
9581 /* Set the sizes of the dynamic sections. */
9584 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9585 struct bfd_link_info *info)
9587 struct ppc_link_hash_table *htab;
9592 struct got_entry *first_tlsld;
9594 htab = ppc_hash_table (info);
9598 dynobj = htab->elf.dynobj;
9602 if (htab->elf.dynamic_sections_created)
9604 /* Set the contents of the .interp section to the interpreter. */
9605 if (bfd_link_executable (info) && !info->nointerp)
9607 s = bfd_get_linker_section (dynobj, ".interp");
9610 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9611 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9615 /* Set up .got offsets for local syms, and space for local dynamic
9617 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9619 struct got_entry **lgot_ents;
9620 struct got_entry **end_lgot_ents;
9621 struct plt_entry **local_plt;
9622 struct plt_entry **end_local_plt;
9623 unsigned char *lgot_masks;
9624 bfd_size_type locsymcount;
9625 Elf_Internal_Shdr *symtab_hdr;
9627 if (!is_ppc64_elf (ibfd))
9630 for (s = ibfd->sections; s != NULL; s = s->next)
9632 struct ppc_dyn_relocs *p;
9634 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9636 if (!bfd_is_abs_section (p->sec)
9637 && bfd_is_abs_section (p->sec->output_section))
9639 /* Input section has been discarded, either because
9640 it is a copy of a linkonce section or due to
9641 linker script /DISCARD/, so we'll be discarding
9644 else if (p->count != 0)
9646 asection *srel = elf_section_data (p->sec)->sreloc;
9648 srel = htab->elf.irelplt;
9649 srel->size += p->count * sizeof (Elf64_External_Rela);
9650 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9651 info->flags |= DF_TEXTREL;
9656 lgot_ents = elf_local_got_ents (ibfd);
9660 symtab_hdr = &elf_symtab_hdr (ibfd);
9661 locsymcount = symtab_hdr->sh_info;
9662 end_lgot_ents = lgot_ents + locsymcount;
9663 local_plt = (struct plt_entry **) end_lgot_ents;
9664 end_local_plt = local_plt + locsymcount;
9665 lgot_masks = (unsigned char *) end_local_plt;
9666 s = ppc64_elf_tdata (ibfd)->got;
9667 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9669 struct got_entry **pent, *ent;
9672 while ((ent = *pent) != NULL)
9673 if (ent->got.refcount > 0)
9675 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9677 ppc64_tlsld_got (ibfd)->got.refcount += 1;
9682 unsigned int ent_size = 8;
9683 unsigned int rel_size = sizeof (Elf64_External_Rela);
9685 ent->got.offset = s->size;
9686 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9691 s->size += ent_size;
9692 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9694 htab->elf.irelplt->size += rel_size;
9695 htab->got_reli_size += rel_size;
9697 else if (bfd_link_pic (info)
9698 && !((ent->tls_type & TLS_TPREL) != 0
9699 && bfd_link_executable (info)))
9701 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9702 srel->size += rel_size;
9711 /* Allocate space for plt calls to local syms. */
9712 lgot_masks = (unsigned char *) end_local_plt;
9713 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
9715 struct plt_entry *ent;
9717 for (ent = *local_plt; ent != NULL; ent = ent->next)
9718 if (ent->plt.refcount > 0)
9720 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9723 ent->plt.offset = s->size;
9724 s->size += PLT_ENTRY_SIZE (htab);
9725 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9727 else if (htab->can_convert_all_inline_plt
9728 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
9729 ent->plt.offset = (bfd_vma) -1;
9733 ent->plt.offset = s->size;
9734 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9735 if (bfd_link_pic (info))
9736 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9740 ent->plt.offset = (bfd_vma) -1;
9744 /* Allocate global sym .plt and .got entries, and space for global
9745 sym dynamic relocs. */
9746 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9748 if (!htab->opd_abi && !bfd_link_pic (info))
9749 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
9752 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9754 struct got_entry *ent;
9756 if (!is_ppc64_elf (ibfd))
9759 ent = ppc64_tlsld_got (ibfd);
9760 if (ent->got.refcount > 0)
9762 if (!htab->do_multi_toc && first_tlsld != NULL)
9764 ent->is_indirect = TRUE;
9765 ent->got.ent = first_tlsld;
9769 if (first_tlsld == NULL)
9771 s = ppc64_elf_tdata (ibfd)->got;
9772 ent->got.offset = s->size;
9775 if (bfd_link_pic (info))
9777 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9778 srel->size += sizeof (Elf64_External_Rela);
9783 ent->got.offset = (bfd_vma) -1;
9786 /* We now have determined the sizes of the various dynamic sections.
9787 Allocate memory for them. */
9789 for (s = dynobj->sections; s != NULL; s = s->next)
9791 if ((s->flags & SEC_LINKER_CREATED) == 0)
9794 if (s == htab->brlt || s == htab->relbrlt)
9795 /* These haven't been allocated yet; don't strip. */
9797 else if (s == htab->elf.sgot
9798 || s == htab->elf.splt
9799 || s == htab->elf.iplt
9800 || s == htab->pltlocal
9802 || s == htab->global_entry
9803 || s == htab->elf.sdynbss
9804 || s == htab->elf.sdynrelro)
9806 /* Strip this section if we don't need it; see the
9809 else if (s == htab->glink_eh_frame)
9811 if (!bfd_is_abs_section (s->output_section))
9812 /* Not sized yet. */
9815 else if (CONST_STRNEQ (s->name, ".rela"))
9819 if (s != htab->elf.srelplt)
9822 /* We use the reloc_count field as a counter if we need
9823 to copy relocs into the output file. */
9829 /* It's not one of our sections, so don't allocate space. */
9835 /* If we don't need this section, strip it from the
9836 output file. This is mostly to handle .rela.bss and
9837 .rela.plt. We must create both sections in
9838 create_dynamic_sections, because they must be created
9839 before the linker maps input sections to output
9840 sections. The linker does that before
9841 adjust_dynamic_symbol is called, and it is that
9842 function which decides whether anything needs to go
9843 into these sections. */
9844 s->flags |= SEC_EXCLUDE;
9848 if (bfd_is_abs_section (s->output_section))
9849 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9852 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9855 /* Allocate memory for the section contents. We use bfd_zalloc
9856 here in case unused entries are not reclaimed before the
9857 section's contents are written out. This should not happen,
9858 but this way if it does we get a R_PPC64_NONE reloc in .rela
9859 sections instead of garbage.
9860 We also rely on the section contents being zero when writing
9861 the GOT and .dynrelro. */
9862 s->contents = bfd_zalloc (dynobj, s->size);
9863 if (s->contents == NULL)
9867 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9869 if (!is_ppc64_elf (ibfd))
9872 s = ppc64_elf_tdata (ibfd)->got;
9873 if (s != NULL && s != htab->elf.sgot)
9876 s->flags |= SEC_EXCLUDE;
9879 s->contents = bfd_zalloc (ibfd, s->size);
9880 if (s->contents == NULL)
9884 s = ppc64_elf_tdata (ibfd)->relgot;
9888 s->flags |= SEC_EXCLUDE;
9891 s->contents = bfd_zalloc (ibfd, s->size);
9892 if (s->contents == NULL)
9900 if (htab->elf.dynamic_sections_created)
9902 bfd_boolean tls_opt;
9904 /* Add some entries to the .dynamic section. We fill in the
9905 values later, in ppc64_elf_finish_dynamic_sections, but we
9906 must add the entries now so that we get the correct size for
9907 the .dynamic section. The DT_DEBUG entry is filled in by the
9908 dynamic linker and used by the debugger. */
9909 #define add_dynamic_entry(TAG, VAL) \
9910 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9912 if (bfd_link_executable (info))
9914 if (!add_dynamic_entry (DT_DEBUG, 0))
9918 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
9920 if (!add_dynamic_entry (DT_PLTGOT, 0)
9921 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9922 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9923 || !add_dynamic_entry (DT_JMPREL, 0)
9924 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9928 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
9930 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9931 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9935 tls_opt = (htab->params->tls_get_addr_opt
9936 && htab->tls_get_addr_fd != NULL
9937 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9938 if (tls_opt || !htab->opd_abi)
9940 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9946 if (!add_dynamic_entry (DT_RELA, 0)
9947 || !add_dynamic_entry (DT_RELASZ, 0)
9948 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9951 /* If any dynamic relocs apply to a read-only section,
9952 then we need a DT_TEXTREL entry. */
9953 if ((info->flags & DF_TEXTREL) == 0)
9954 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
9956 if ((info->flags & DF_TEXTREL) != 0)
9958 if (!add_dynamic_entry (DT_TEXTREL, 0))
9963 #undef add_dynamic_entry
9968 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
9971 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
9973 if (h->plt.plist != NULL
9975 && !h->pointer_equality_needed)
9978 return _bfd_elf_hash_symbol (h);
9981 /* Determine the type of stub needed, if any, for a call. */
9983 static inline enum ppc_stub_type
9984 ppc_type_of_stub (asection *input_sec,
9985 const Elf_Internal_Rela *rel,
9986 struct ppc_link_hash_entry **hash,
9987 struct plt_entry **plt_ent,
9988 bfd_vma destination,
9989 unsigned long local_off)
9991 struct ppc_link_hash_entry *h = *hash;
9993 bfd_vma branch_offset;
9994 bfd_vma max_branch_offset;
9995 enum elf_ppc64_reloc_type r_type;
9999 struct plt_entry *ent;
10000 struct ppc_link_hash_entry *fdh = h;
10002 && h->oh->is_func_descriptor)
10004 fdh = ppc_follow_link (h->oh);
10008 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10009 if (ent->addend == rel->r_addend
10010 && ent->plt.offset != (bfd_vma) -1)
10013 return ppc_stub_plt_call;
10016 /* Here, we know we don't have a plt entry. If we don't have a
10017 either a defined function descriptor or a defined entry symbol
10018 in a regular object file, then it is pointless trying to make
10019 any other type of stub. */
10020 if (!is_static_defined (&fdh->elf)
10021 && !is_static_defined (&h->elf))
10022 return ppc_stub_none;
10024 else if (elf_local_got_ents (input_sec->owner) != NULL)
10026 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10027 struct plt_entry **local_plt = (struct plt_entry **)
10028 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10029 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10031 if (local_plt[r_symndx] != NULL)
10033 struct plt_entry *ent;
10035 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10036 if (ent->addend == rel->r_addend
10037 && ent->plt.offset != (bfd_vma) -1)
10040 return ppc_stub_plt_call;
10045 /* Determine where the call point is. */
10046 location = (input_sec->output_offset
10047 + input_sec->output_section->vma
10050 branch_offset = destination - location;
10051 r_type = ELF64_R_TYPE (rel->r_info);
10053 /* Determine if a long branch stub is needed. */
10054 max_branch_offset = 1 << 25;
10055 if (r_type == R_PPC64_REL14
10056 || r_type == R_PPC64_REL14_BRTAKEN
10057 || r_type == R_PPC64_REL14_BRNTAKEN)
10058 max_branch_offset = 1 << 15;
10060 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10061 /* We need a stub. Figure out whether a long_branch or plt_branch
10062 is needed later. */
10063 return ppc_stub_long_branch;
10065 return ppc_stub_none;
10068 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10069 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10074 . lis %r12,xxx-1b@highest
10075 . ori %r12,%r12,xxx-1b@higher
10076 . sldi %r12,%r12,32
10077 . oris %r12,%r12,xxx-1b@high
10078 . ori %r12,%r12,xxx-1b@l
10079 . add/ldx %r12,%r11,%r12 */
10082 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10084 bfd_put_32 (abfd, MFLR_R12, p);
10086 bfd_put_32 (abfd, BCL_20_31, p);
10088 bfd_put_32 (abfd, MFLR_R11, p);
10090 bfd_put_32 (abfd, MTLR_R12, p);
10092 if (off + 0x8000 < 0x10000)
10095 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10097 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10100 else if (off + 0x80008000ULL < 0x100000000ULL)
10102 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10105 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10107 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10112 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10114 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10119 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10121 if (((off >> 32) & 0xffff) != 0)
10123 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10127 if (((off >> 32) & 0xffffffffULL) != 0)
10129 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10132 if (PPC_HI (off) != 0)
10134 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10137 if (PPC_LO (off) != 0)
10139 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10143 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10145 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10151 static unsigned int
10152 size_offset (bfd_vma off)
10155 if (off + 0x8000 < 0x10000)
10157 else if (off + 0x80008000ULL < 0x100000000ULL)
10161 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10166 if (((off >> 32) & 0xffff) != 0)
10169 if (((off >> 32) & 0xffffffffULL) != 0)
10171 if (PPC_HI (off) != 0)
10173 if (PPC_LO (off) != 0)
10180 static unsigned int
10181 num_relocs_for_offset (bfd_vma off)
10183 unsigned int num_rel;
10184 if (off + 0x8000 < 0x10000)
10186 else if (off + 0x80008000ULL < 0x100000000ULL)
10191 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10192 && ((off >> 32) & 0xffff) != 0)
10194 if (PPC_HI (off) != 0)
10196 if (PPC_LO (off) != 0)
10202 static Elf_Internal_Rela *
10203 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10204 bfd_vma roff, bfd_vma targ, bfd_vma off)
10206 bfd_vma relative_targ = targ - (roff - 8);
10207 if (bfd_big_endian (info->output_bfd))
10209 r->r_offset = roff;
10210 r->r_addend = relative_targ + roff;
10211 if (off + 0x8000 < 0x10000)
10212 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10213 else if (off + 0x80008000ULL < 0x100000000ULL)
10215 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10218 r->r_offset = roff;
10219 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10220 r->r_addend = relative_targ + roff;
10224 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10225 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10228 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10229 if (((off >> 32) & 0xffff) != 0)
10233 r->r_offset = roff;
10234 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10235 r->r_addend = relative_targ + roff;
10238 if (((off >> 32) & 0xffffffffULL) != 0)
10240 if (PPC_HI (off) != 0)
10244 r->r_offset = roff;
10245 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10246 r->r_addend = relative_targ + roff;
10248 if (PPC_LO (off) != 0)
10252 r->r_offset = roff;
10253 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10254 r->r_addend = relative_targ + roff;
10261 build_powerxx_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10265 if (off - odd + (1ULL << 33) < 1ULL << 34)
10270 bfd_put_32 (abfd, NOP, p);
10276 insn = PADDI_R12_PC;
10278 bfd_put_32 (abfd, insn >> 32, p);
10280 bfd_put_32 (abfd, insn, p);
10282 /* The minimum value for paddi is -0x200000000. The minimum value
10283 for li is -0x8000, which when shifted by 34 and added gives a
10284 minimum value of -0x2000200000000. The maximum value is
10285 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10286 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10289 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10293 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10296 insn = PADDI_R12_PC | D34 (off);
10297 bfd_put_32 (abfd, insn >> 32, p);
10299 bfd_put_32 (abfd, insn, p);
10303 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10307 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10309 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10314 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10316 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10320 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10323 insn = PADDI_R12_PC | D34 (off);
10324 bfd_put_32 (abfd, insn >> 32, p);
10326 bfd_put_32 (abfd, insn, p);
10330 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10334 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10336 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10342 static unsigned int
10343 size_powerxx_offset (bfd_vma off, int odd)
10345 if (off - odd + (1ULL << 33) < 1ULL << 34)
10347 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10353 static unsigned int
10354 num_relocs_for_powerxx_offset (bfd_vma off, int odd)
10356 if (off - odd + (1ULL << 33) < 1ULL << 34)
10358 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10364 static Elf_Internal_Rela *
10365 emit_relocs_for_powerxx_offset (struct bfd_link_info *info,
10366 Elf_Internal_Rela *r, bfd_vma roff,
10367 bfd_vma targ, bfd_vma off, int odd)
10369 if (off - odd + (1ULL << 33) < 1ULL << 34)
10371 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10373 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10374 r->r_offset = roff + d_offset;
10375 r->r_addend = targ + 8 - odd - d_offset;
10376 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10382 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10383 r->r_offset = roff + d_offset;
10384 r->r_addend = targ + 8 + odd - d_offset;
10385 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10388 r->r_offset = roff + d_offset;
10389 r->r_addend = targ + 4 + odd - d_offset;
10390 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10394 r->r_offset = roff;
10395 r->r_addend = targ;
10396 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10400 /* Emit .eh_frame opcode to advance pc by DELTA. */
10403 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10407 *eh++ = DW_CFA_advance_loc + delta;
10408 else if (delta < 256)
10410 *eh++ = DW_CFA_advance_loc1;
10413 else if (delta < 65536)
10415 *eh++ = DW_CFA_advance_loc2;
10416 bfd_put_16 (abfd, delta, eh);
10421 *eh++ = DW_CFA_advance_loc4;
10422 bfd_put_32 (abfd, delta, eh);
10428 /* Size of required .eh_frame opcode to advance pc by DELTA. */
10430 static unsigned int
10431 eh_advance_size (unsigned int delta)
10433 if (delta < 64 * 4)
10434 /* DW_CFA_advance_loc+[1..63]. */
10436 if (delta < 256 * 4)
10437 /* DW_CFA_advance_loc1, byte. */
10439 if (delta < 65536 * 4)
10440 /* DW_CFA_advance_loc2, 2 bytes. */
10442 /* DW_CFA_advance_loc4, 4 bytes. */
10446 /* With power7 weakly ordered memory model, it is possible for ld.so
10447 to update a plt entry in one thread and have another thread see a
10448 stale zero toc entry. To avoid this we need some sort of acquire
10449 barrier in the call stub. One solution is to make the load of the
10450 toc word seem to appear to depend on the load of the function entry
10451 word. Another solution is to test for r2 being zero, and branch to
10452 the appropriate glink entry if so.
10454 . fake dep barrier compare
10455 . ld 12,xxx(2) ld 12,xxx(2)
10456 . mtctr 12 mtctr 12
10457 . xor 11,12,12 ld 2,xxx+8(2)
10458 . add 2,2,11 cmpldi 2,0
10459 . ld 2,xxx+8(2) bnectr+
10460 . bctr b <glink_entry>
10462 The solution involving the compare turns out to be faster, so
10463 that's what we use unless the branch won't reach. */
10465 #define ALWAYS_USE_FAKE_DEP 0
10466 #define ALWAYS_EMIT_R2SAVE 0
10468 static inline unsigned int
10469 plt_stub_size (struct ppc_link_hash_table *htab,
10470 struct ppc_stub_hash_entry *stub_entry,
10475 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10477 if (htab->powerxx_stubs)
10479 bfd_vma start = (stub_entry->stub_offset
10480 + stub_entry->group->stub_sec->output_offset
10481 + stub_entry->group->stub_sec->output_section->vma);
10482 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10484 size = 8 + size_powerxx_offset (off, start & 4);
10487 size = 8 + size_offset (off - 8);
10488 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10494 if (ALWAYS_EMIT_R2SAVE
10495 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10497 if (PPC_HA (off) != 0)
10502 if (htab->params->plt_static_chain)
10504 if (htab->params->plt_thread_safe
10505 && htab->elf.dynamic_sections_created
10506 && stub_entry->h != NULL
10507 && stub_entry->h->elf.dynindx != -1)
10509 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10512 if (stub_entry->h != NULL
10513 && (stub_entry->h == htab->tls_get_addr_fd
10514 || stub_entry->h == htab->tls_get_addr)
10515 && htab->params->tls_get_addr_opt)
10518 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
10524 /* Depending on the sign of plt_stub_align:
10525 If positive, return the padding to align to a 2**plt_stub_align
10527 If negative, if this stub would cross fewer 2**plt_stub_align
10528 boundaries if we align, 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,
10536 unsigned stub_size;
10537 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10539 if (htab->params->plt_stub_align >= 0)
10541 stub_align = 1 << htab->params->plt_stub_align;
10542 if ((stub_off & (stub_align - 1)) != 0)
10543 return stub_align - (stub_off & (stub_align - 1));
10547 stub_align = 1 << -htab->params->plt_stub_align;
10548 stub_size = plt_stub_size (htab, stub_entry, plt_off);
10549 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10550 > ((stub_size - 1) & -stub_align))
10551 return stub_align - (stub_off & (stub_align - 1));
10555 /* Build a .plt call stub. */
10557 static inline bfd_byte *
10558 build_plt_stub (struct ppc_link_hash_table *htab,
10559 struct ppc_stub_hash_entry *stub_entry,
10560 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10562 bfd *obfd = htab->params->stub_bfd;
10563 bfd_boolean plt_load_toc = htab->opd_abi;
10564 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10565 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10566 && htab->elf.dynamic_sections_created
10567 && stub_entry->h != NULL
10568 && stub_entry->h->elf.dynindx != -1);
10569 bfd_boolean use_fake_dep = plt_thread_safe;
10570 bfd_vma cmp_branch_off = 0;
10572 if (!ALWAYS_USE_FAKE_DEP
10575 && !((stub_entry->h == htab->tls_get_addr_fd
10576 || stub_entry->h == htab->tls_get_addr)
10577 && htab->params->tls_get_addr_opt))
10579 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10580 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10581 / PLT_ENTRY_SIZE (htab));
10582 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10585 if (pltindex > 32768)
10586 glinkoff += (pltindex - 32768) * 4;
10588 + htab->glink->output_offset
10589 + htab->glink->output_section->vma);
10590 from = (p - stub_entry->group->stub_sec->contents
10591 + 4 * (ALWAYS_EMIT_R2SAVE
10592 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10593 + 4 * (PPC_HA (offset) != 0)
10594 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10595 != PPC_HA (offset))
10596 + 4 * (plt_static_chain != 0)
10598 + stub_entry->group->stub_sec->output_offset
10599 + stub_entry->group->stub_sec->output_section->vma);
10600 cmp_branch_off = to - from;
10601 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10604 if (PPC_HA (offset) != 0)
10608 if (ALWAYS_EMIT_R2SAVE
10609 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10610 r[0].r_offset += 4;
10611 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10612 r[1].r_offset = r[0].r_offset + 4;
10613 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10614 r[1].r_addend = r[0].r_addend;
10617 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10619 r[2].r_offset = r[1].r_offset + 4;
10620 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10621 r[2].r_addend = r[0].r_addend;
10625 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10626 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10627 r[2].r_addend = r[0].r_addend + 8;
10628 if (plt_static_chain)
10630 r[3].r_offset = r[2].r_offset + 4;
10631 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10632 r[3].r_addend = r[0].r_addend + 16;
10637 if (ALWAYS_EMIT_R2SAVE
10638 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10639 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10642 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10643 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10647 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10648 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10651 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10653 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10656 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10661 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10662 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10664 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10665 if (plt_static_chain)
10666 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10673 if (ALWAYS_EMIT_R2SAVE
10674 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10675 r[0].r_offset += 4;
10676 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10679 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10681 r[1].r_offset = r[0].r_offset + 4;
10682 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10683 r[1].r_addend = r[0].r_addend;
10687 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10688 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10689 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10690 if (plt_static_chain)
10692 r[2].r_offset = r[1].r_offset + 4;
10693 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10694 r[2].r_addend = r[0].r_addend + 8;
10699 if (ALWAYS_EMIT_R2SAVE
10700 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10701 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10702 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10704 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10706 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10709 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10714 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10715 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10717 if (plt_static_chain)
10718 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10719 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10722 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10724 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10725 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
10726 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10729 bfd_put_32 (obfd, BCTR, p), p += 4;
10733 /* Build a special .plt call stub for __tls_get_addr. */
10735 #define LD_R11_0R3 0xe9630000
10736 #define LD_R12_0R3 0xe9830000
10737 #define MR_R0_R3 0x7c601b78
10738 #define CMPDI_R11_0 0x2c2b0000
10739 #define ADD_R3_R12_R13 0x7c6c6a14
10740 #define BEQLR 0x4d820020
10741 #define MR_R3_R0 0x7c030378
10742 #define STD_R11_0R1 0xf9610000
10743 #define BCTRL 0x4e800421
10744 #define LD_R11_0R1 0xe9610000
10745 #define MTLR_R11 0x7d6803a6
10747 static inline bfd_byte *
10748 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10749 struct ppc_stub_hash_entry *stub_entry,
10750 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10752 bfd *obfd = htab->params->stub_bfd;
10755 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10756 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10757 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10758 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10759 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10760 bfd_put_32 (obfd, BEQLR, p), p += 4;
10761 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10763 r[0].r_offset += 7 * 4;
10764 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
10765 return build_plt_stub (htab, stub_entry, p, offset, r);
10767 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10768 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10771 r[0].r_offset += 2 * 4;
10772 p = build_plt_stub (htab, stub_entry, p, offset, r);
10773 bfd_put_32 (obfd, BCTRL, p - 4);
10775 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
10776 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10777 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10778 bfd_put_32 (obfd, BLR, p), p += 4;
10780 if (htab->glink_eh_frame != NULL
10781 && htab->glink_eh_frame->size != 0)
10783 bfd_byte *base, *eh;
10784 unsigned int lr_used, delta;
10786 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10787 eh = base + stub_entry->group->eh_size;
10788 lr_used = stub_entry->stub_offset + (p - 20 - loc);
10789 delta = lr_used - stub_entry->group->lr_restore;
10790 stub_entry->group->lr_restore = lr_used + 16;
10791 eh = eh_advance (htab->elf.dynobj, eh, delta);
10792 *eh++ = DW_CFA_offset_extended_sf;
10794 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10795 *eh++ = DW_CFA_advance_loc + 4;
10796 *eh++ = DW_CFA_restore_extended;
10798 stub_entry->group->eh_size = eh - base;
10803 static Elf_Internal_Rela *
10804 get_relocs (asection *sec, int count)
10806 Elf_Internal_Rela *relocs;
10807 struct bfd_elf_section_data *elfsec_data;
10809 elfsec_data = elf_section_data (sec);
10810 relocs = elfsec_data->relocs;
10811 if (relocs == NULL)
10813 bfd_size_type relsize;
10814 relsize = sec->reloc_count * sizeof (*relocs);
10815 relocs = bfd_alloc (sec->owner, relsize);
10816 if (relocs == NULL)
10818 elfsec_data->relocs = relocs;
10819 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10820 sizeof (Elf_Internal_Shdr));
10821 if (elfsec_data->rela.hdr == NULL)
10823 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10824 * sizeof (Elf64_External_Rela));
10825 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10826 sec->reloc_count = 0;
10828 relocs += sec->reloc_count;
10829 sec->reloc_count += count;
10833 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10834 forms, to the equivalent relocs against the global symbol given by
10838 use_global_in_relocs (struct ppc_link_hash_table *htab,
10839 struct ppc_stub_hash_entry *stub_entry,
10840 Elf_Internal_Rela *r, unsigned int num_rel)
10842 struct elf_link_hash_entry **hashes;
10843 unsigned long symndx;
10844 struct ppc_link_hash_entry *h;
10847 /* Relocs are always against symbols in their own object file. Fake
10848 up global sym hashes for the stub bfd (which has no symbols). */
10849 hashes = elf_sym_hashes (htab->params->stub_bfd);
10850 if (hashes == NULL)
10852 bfd_size_type hsize;
10854 /* When called the first time, stub_globals will contain the
10855 total number of symbols seen during stub sizing. After
10856 allocating, stub_globals is used as an index to fill the
10858 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10859 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10860 if (hashes == NULL)
10862 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10863 htab->stub_globals = 1;
10865 symndx = htab->stub_globals++;
10867 hashes[symndx] = &h->elf;
10868 if (h->oh != NULL && h->oh->is_func)
10869 h = ppc_follow_link (h->oh);
10870 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10871 || h->elf.root.type == bfd_link_hash_defweak);
10872 symval = (h->elf.root.u.def.value
10873 + h->elf.root.u.def.section->output_offset
10874 + h->elf.root.u.def.section->output_section->vma);
10875 while (num_rel-- != 0)
10877 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10878 if (h->elf.root.u.def.section != stub_entry->target_section)
10880 /* H is an opd symbol. The addend must be zero, and the
10881 branch reloc is the only one we can convert. */
10886 r->r_addend -= symval;
10893 get_r2off (struct bfd_link_info *info,
10894 struct ppc_stub_hash_entry *stub_entry)
10896 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10897 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10901 /* Support linking -R objects. Get the toc pointer from the
10904 if (!htab->opd_abi)
10906 asection *opd = stub_entry->h->elf.root.u.def.section;
10907 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10909 if (strcmp (opd->name, ".opd") != 0
10910 || opd->reloc_count != 0)
10912 info->callbacks->einfo
10913 (_("%P: cannot find opd entry toc for `%pT'\n"),
10914 stub_entry->h->elf.root.root.string);
10915 bfd_set_error (bfd_error_bad_value);
10916 return (bfd_vma) -1;
10918 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10919 return (bfd_vma) -1;
10920 r2off = bfd_get_64 (opd->owner, buf);
10921 r2off -= elf_gp (info->output_bfd);
10923 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10928 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10930 struct ppc_stub_hash_entry *stub_entry;
10931 struct ppc_branch_hash_entry *br_entry;
10932 struct bfd_link_info *info;
10933 struct ppc_link_hash_table *htab;
10935 bfd_byte *p, *relp;
10937 Elf_Internal_Rela *r;
10942 /* Massage our args to the form they really have. */
10943 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10946 htab = ppc_hash_table (info);
10950 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
10951 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10953 htab->stub_count[stub_entry->stub_type - 1] += 1;
10954 switch (stub_entry->stub_type)
10956 case ppc_stub_long_branch:
10957 case ppc_stub_long_branch_r2off:
10958 /* Branches are relative. This is where we are going to. */
10959 targ = (stub_entry->target_value
10960 + stub_entry->target_section->output_offset
10961 + stub_entry->target_section->output_section->vma);
10962 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10964 /* And this is where we are coming from. */
10965 off = (stub_entry->stub_offset
10966 + stub_entry->group->stub_sec->output_offset
10967 + stub_entry->group->stub_sec->output_section->vma);
10971 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10973 bfd_vma r2off = get_r2off (info, stub_entry);
10975 if (r2off == (bfd_vma) -1)
10977 htab->stub_error = TRUE;
10980 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10982 if (PPC_HA (r2off) != 0)
10984 bfd_put_32 (htab->params->stub_bfd,
10985 ADDIS_R2_R2 | PPC_HA (r2off), p);
10988 if (PPC_LO (r2off) != 0)
10990 bfd_put_32 (htab->params->stub_bfd,
10991 ADDI_R2_R2 | PPC_LO (r2off), p);
10996 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10999 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11002 (_("long branch stub `%s' offset overflow"),
11003 stub_entry->root.string);
11004 htab->stub_error = TRUE;
11008 if (info->emitrelocations)
11010 r = get_relocs (stub_entry->group->stub_sec, 1);
11013 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11014 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11015 r->r_addend = targ;
11016 if (stub_entry->h != NULL
11017 && !use_global_in_relocs (htab, stub_entry, r, 1))
11022 case ppc_stub_plt_branch:
11023 case ppc_stub_plt_branch_r2off:
11024 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11025 stub_entry->root.string + 9,
11027 if (br_entry == NULL)
11029 _bfd_error_handler (_("can't find branch stub `%s'"),
11030 stub_entry->root.string);
11031 htab->stub_error = TRUE;
11035 targ = (stub_entry->target_value
11036 + stub_entry->target_section->output_offset
11037 + stub_entry->target_section->output_section->vma);
11038 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11039 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11041 bfd_put_64 (htab->brlt->owner, targ,
11042 htab->brlt->contents + br_entry->offset);
11044 if (br_entry->iter == htab->stub_iteration)
11046 br_entry->iter = 0;
11048 if (htab->relbrlt != NULL)
11050 /* Create a reloc for the branch lookup table entry. */
11051 Elf_Internal_Rela rela;
11054 rela.r_offset = (br_entry->offset
11055 + htab->brlt->output_offset
11056 + htab->brlt->output_section->vma);
11057 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11058 rela.r_addend = targ;
11060 rl = htab->relbrlt->contents;
11061 rl += (htab->relbrlt->reloc_count++
11062 * sizeof (Elf64_External_Rela));
11063 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11065 else if (info->emitrelocations)
11067 r = get_relocs (htab->brlt, 1);
11070 /* brlt, being SEC_LINKER_CREATED does not go through the
11071 normal reloc processing. Symbols and offsets are not
11072 translated from input file to output file form, so
11073 set up the offset per the output file. */
11074 r->r_offset = (br_entry->offset
11075 + htab->brlt->output_offset
11076 + htab->brlt->output_section->vma);
11077 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11078 r->r_addend = targ;
11082 targ = (br_entry->offset
11083 + htab->brlt->output_offset
11084 + htab->brlt->output_section->vma);
11086 off = (elf_gp (info->output_bfd)
11087 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11090 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11092 info->callbacks->einfo
11093 (_("%P: linkage table error against `%pT'\n"),
11094 stub_entry->root.string);
11095 bfd_set_error (bfd_error_bad_value);
11096 htab->stub_error = TRUE;
11100 if (info->emitrelocations)
11102 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11105 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11106 if (bfd_big_endian (info->output_bfd))
11107 r[0].r_offset += 2;
11108 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11109 r[0].r_offset += 4;
11110 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11111 r[0].r_addend = targ;
11112 if (PPC_HA (off) != 0)
11114 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11115 r[1].r_offset = r[0].r_offset + 4;
11116 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11117 r[1].r_addend = r[0].r_addend;
11122 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11124 if (PPC_HA (off) != 0)
11126 bfd_put_32 (htab->params->stub_bfd,
11127 ADDIS_R12_R2 | PPC_HA (off), p);
11129 bfd_put_32 (htab->params->stub_bfd,
11130 LD_R12_0R12 | PPC_LO (off), p);
11133 bfd_put_32 (htab->params->stub_bfd,
11134 LD_R12_0R2 | PPC_LO (off), p);
11138 bfd_vma r2off = get_r2off (info, stub_entry);
11140 if (r2off == (bfd_vma) -1)
11142 htab->stub_error = TRUE;
11146 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11148 if (PPC_HA (off) != 0)
11150 bfd_put_32 (htab->params->stub_bfd,
11151 ADDIS_R12_R2 | PPC_HA (off), p);
11153 bfd_put_32 (htab->params->stub_bfd,
11154 LD_R12_0R12 | PPC_LO (off), p);
11157 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11159 if (PPC_HA (r2off) != 0)
11162 bfd_put_32 (htab->params->stub_bfd,
11163 ADDIS_R2_R2 | PPC_HA (r2off), p);
11165 if (PPC_LO (r2off) != 0)
11168 bfd_put_32 (htab->params->stub_bfd,
11169 ADDI_R2_R2 | PPC_LO (r2off), p);
11173 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11175 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11179 case ppc_stub_long_branch_notoc:
11180 case ppc_stub_long_branch_both:
11181 case ppc_stub_plt_branch_notoc:
11182 case ppc_stub_plt_branch_both:
11183 case ppc_stub_plt_call_notoc:
11184 case ppc_stub_plt_call_both:
11186 off = (stub_entry->stub_offset
11187 + stub_entry->group->stub_sec->output_offset
11188 + stub_entry->group->stub_sec->output_section->vma);
11189 if (stub_entry->stub_type == ppc_stub_long_branch_both
11190 || stub_entry->stub_type == ppc_stub_plt_branch_both
11191 || stub_entry->stub_type == ppc_stub_plt_call_both)
11194 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11197 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11199 targ = stub_entry->plt_ent->plt.offset & ~1;
11200 if (targ >= (bfd_vma) -2)
11203 plt = htab->elf.splt;
11204 if (!htab->elf.dynamic_sections_created
11205 || stub_entry->h == NULL
11206 || stub_entry->h->elf.dynindx == -1)
11208 if (stub_entry->symtype == STT_GNU_IFUNC)
11209 plt = htab->elf.iplt;
11211 plt = htab->pltlocal;
11213 targ += plt->output_offset + plt->output_section->vma;
11216 targ = (stub_entry->target_value
11217 + stub_entry->target_section->output_offset
11218 + stub_entry->target_section->output_section->vma);
11224 if (htab->powerxx_stubs)
11226 bfd_boolean load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
11227 p = build_powerxx_offset (htab->params->stub_bfd, p, off, odd, load);
11231 /* The notoc stubs calculate their target (either a PLT entry or
11232 the global entry point of a function) relative to the PC
11233 returned by the "bcl" two instructions past the start of the
11234 sequence emitted by build_offset. The offset is therefore 8
11235 less than calculated from the start of the sequence. */
11237 p = build_offset (htab->params->stub_bfd, p, off,
11238 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11241 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
11245 from = (stub_entry->stub_offset
11246 + stub_entry->group->stub_sec->output_offset
11247 + stub_entry->group->stub_sec->output_section->vma
11249 bfd_put_32 (htab->params->stub_bfd,
11250 B_DOT | ((targ - from) & 0x3fffffc), p);
11254 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11256 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11260 if (info->emitrelocations)
11262 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
11263 if (htab->powerxx_stubs)
11264 num_rel += num_relocs_for_powerxx_offset (off, odd);
11267 num_rel += num_relocs_for_offset (off);
11270 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11273 if (htab->powerxx_stubs)
11274 r = emit_relocs_for_powerxx_offset (info, r, roff, targ, off, odd);
11276 r = emit_relocs_for_offset (info, r, roff, targ, off);
11277 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11278 || stub_entry->stub_type == ppc_stub_long_branch_both)
11281 roff = p - 4 - stub_entry->group->stub_sec->contents;
11282 r->r_offset = roff;
11283 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11284 r->r_addend = targ;
11285 if (stub_entry->h != NULL
11286 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11291 if (!htab->powerxx_stubs
11292 && htab->glink_eh_frame != NULL
11293 && htab->glink_eh_frame->size != 0)
11295 bfd_byte *base, *eh;
11296 unsigned int lr_used, delta;
11298 base = (htab->glink_eh_frame->contents
11299 + stub_entry->group->eh_base + 17);
11300 eh = base + stub_entry->group->eh_size;
11301 lr_used = stub_entry->stub_offset + 8;
11302 if (stub_entry->stub_type == ppc_stub_long_branch_both
11303 || stub_entry->stub_type == ppc_stub_plt_branch_both
11304 || stub_entry->stub_type == ppc_stub_plt_call_both)
11306 delta = lr_used - stub_entry->group->lr_restore;
11307 stub_entry->group->lr_restore = lr_used + 8;
11308 eh = eh_advance (htab->elf.dynobj, eh, delta);
11309 *eh++ = DW_CFA_register;
11312 *eh++ = DW_CFA_advance_loc + 2;
11313 *eh++ = DW_CFA_restore_extended;
11315 stub_entry->group->eh_size = eh - base;
11319 case ppc_stub_plt_call:
11320 case ppc_stub_plt_call_r2save:
11321 if (stub_entry->h != NULL
11322 && stub_entry->h->is_func_descriptor
11323 && stub_entry->h->oh != NULL)
11325 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11327 /* If the old-ABI "dot-symbol" is undefined make it weak so
11328 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11329 if (fh->elf.root.type == bfd_link_hash_undefined
11330 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11331 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11332 fh->elf.root.type = bfd_link_hash_undefweak;
11335 /* Now build the stub. */
11336 targ = stub_entry->plt_ent->plt.offset & ~1;
11337 if (targ >= (bfd_vma) -2)
11340 plt = htab->elf.splt;
11341 if (!htab->elf.dynamic_sections_created
11342 || stub_entry->h == NULL
11343 || stub_entry->h->elf.dynindx == -1)
11345 if (stub_entry->symtype == STT_GNU_IFUNC)
11346 plt = htab->elf.iplt;
11348 plt = htab->pltlocal;
11350 targ += plt->output_offset + plt->output_section->vma;
11352 off = (elf_gp (info->output_bfd)
11353 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11356 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11358 info->callbacks->einfo
11359 /* xgettext:c-format */
11360 (_("%P: linkage table error against `%pT'\n"),
11361 stub_entry->h != NULL
11362 ? stub_entry->h->elf.root.root.string
11364 bfd_set_error (bfd_error_bad_value);
11365 htab->stub_error = TRUE;
11370 if (info->emitrelocations)
11372 r = get_relocs (stub_entry->group->stub_sec,
11373 ((PPC_HA (off) != 0)
11375 ? 2 + (htab->params->plt_static_chain
11376 && PPC_HA (off + 16) == PPC_HA (off))
11380 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11381 if (bfd_big_endian (info->output_bfd))
11382 r[0].r_offset += 2;
11383 r[0].r_addend = targ;
11385 if (stub_entry->h != NULL
11386 && (stub_entry->h == htab->tls_get_addr_fd
11387 || stub_entry->h == htab->tls_get_addr)
11388 && htab->params->tls_get_addr_opt)
11389 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11391 p = build_plt_stub (htab, stub_entry, loc, off, r);
11394 case ppc_stub_save_res:
11402 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
11404 if (htab->params->emit_stub_syms)
11406 struct elf_link_hash_entry *h;
11409 const char *const stub_str[] = { "long_branch",
11422 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11423 len2 = strlen (stub_entry->root.string);
11424 name = bfd_malloc (len1 + len2 + 2);
11427 memcpy (name, stub_entry->root.string, 9);
11428 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11429 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11430 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11433 if (h->root.type == bfd_link_hash_new)
11435 h->root.type = bfd_link_hash_defined;
11436 h->root.u.def.section = stub_entry->group->stub_sec;
11437 h->root.u.def.value = stub_entry->stub_offset;
11438 h->ref_regular = 1;
11439 h->def_regular = 1;
11440 h->ref_regular_nonweak = 1;
11441 h->forced_local = 1;
11443 h->root.linker_def = 1;
11450 /* As above, but don't actually build the stub. Just bump offset so
11451 we know stub section sizes, and select plt_branch stubs where
11452 long_branch stubs won't do. */
11455 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11457 struct ppc_stub_hash_entry *stub_entry;
11458 struct bfd_link_info *info;
11459 struct ppc_link_hash_table *htab;
11461 bfd_vma targ, off, r2off;
11462 unsigned int size, extra, lr_used, delta, odd;
11464 /* Massage our args to the form they really have. */
11465 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11468 htab = ppc_hash_table (info);
11472 /* Make a note of the offset within the stubs for this entry. */
11473 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11475 if (stub_entry->h != NULL
11476 && stub_entry->h->save_res
11477 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11478 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11480 /* Don't make stubs to out-of-line register save/restore
11481 functions. Instead, emit copies of the functions. */
11482 stub_entry->group->needs_save_res = 1;
11483 stub_entry->stub_type = ppc_stub_save_res;
11487 switch (stub_entry->stub_type)
11489 case ppc_stub_plt_branch:
11490 case ppc_stub_plt_branch_r2off:
11491 /* Reset the stub type from the plt branch variant in case we now
11492 can reach with a shorter stub. */
11493 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11494 /* Fall through. */
11495 case ppc_stub_long_branch:
11496 case ppc_stub_long_branch_r2off:
11497 targ = (stub_entry->target_value
11498 + stub_entry->target_section->output_offset
11499 + stub_entry->target_section->output_section->vma);
11500 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11501 off = (stub_entry->stub_offset
11502 + stub_entry->group->stub_sec->output_offset
11503 + stub_entry->group->stub_sec->output_section->vma);
11507 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11509 r2off = get_r2off (info, stub_entry);
11510 if (r2off == (bfd_vma) -1)
11512 htab->stub_error = TRUE;
11516 if (PPC_HA (r2off) != 0)
11518 if (PPC_LO (r2off) != 0)
11524 /* If the branch offset is too big, use a ppc_stub_plt_branch.
11525 Do the same for -R objects without function descriptors. */
11526 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
11528 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
11529 || off + (1 << 25) >= (bfd_vma) (1 << 26))
11531 struct ppc_branch_hash_entry *br_entry;
11533 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11534 stub_entry->root.string + 9,
11536 if (br_entry == NULL)
11538 _bfd_error_handler (_("can't build branch stub `%s'"),
11539 stub_entry->root.string);
11540 htab->stub_error = TRUE;
11544 if (br_entry->iter != htab->stub_iteration)
11546 br_entry->iter = htab->stub_iteration;
11547 br_entry->offset = htab->brlt->size;
11548 htab->brlt->size += 8;
11550 if (htab->relbrlt != NULL)
11551 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11552 else if (info->emitrelocations)
11554 htab->brlt->reloc_count += 1;
11555 htab->brlt->flags |= SEC_RELOC;
11559 targ = (br_entry->offset
11560 + htab->brlt->output_offset
11561 + htab->brlt->output_section->vma);
11562 off = (elf_gp (info->output_bfd)
11563 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11566 if (info->emitrelocations)
11568 stub_entry->group->stub_sec->reloc_count
11569 += 1 + (PPC_HA (off) != 0);
11570 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11573 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11574 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11577 if (PPC_HA (off) != 0)
11583 if (PPC_HA (off) != 0)
11586 if (PPC_HA (r2off) != 0)
11588 if (PPC_LO (r2off) != 0)
11592 else if (info->emitrelocations)
11594 stub_entry->group->stub_sec->reloc_count += 1;
11595 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11599 case ppc_stub_plt_branch_notoc:
11600 case ppc_stub_plt_branch_both:
11601 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11602 /* Fall through. */
11603 case ppc_stub_long_branch_notoc:
11604 case ppc_stub_long_branch_both:
11605 off = (stub_entry->stub_offset
11606 + stub_entry->group->stub_sec->output_offset
11607 + stub_entry->group->stub_sec->output_section->vma);
11609 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11612 targ = (stub_entry->target_value
11613 + stub_entry->target_section->output_offset
11614 + stub_entry->target_section->output_section->vma);
11618 if (info->emitrelocations)
11620 unsigned int num_rel;
11621 if (htab->powerxx_stubs)
11622 num_rel = num_relocs_for_powerxx_offset (off, odd);
11624 num_rel = num_relocs_for_offset (off - 8);
11625 stub_entry->group->stub_sec->reloc_count += num_rel;
11626 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11629 if (htab->powerxx_stubs)
11630 extra = size_powerxx_offset (off, odd);
11632 extra = size_offset (off - 8);
11633 /* Include branch insn plus those in the offset sequence. */
11635 /* The branch insn is at the end, or "extra" bytes along. So
11636 its offset will be "extra" bytes less that that already
11640 if (!htab->powerxx_stubs)
11642 /* After the bcl, lr has been modified so we need to emit
11643 .eh_frame info saying the return address is in r12. */
11644 lr_used = stub_entry->stub_offset + 8;
11645 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11647 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11648 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11649 DW_CFA_restore_extended 65. */
11650 delta = lr_used - stub_entry->group->lr_restore;
11651 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11652 stub_entry->group->lr_restore = lr_used + 8;
11655 /* If the branch can't reach, use a plt_branch. */
11656 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11658 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11659 - ppc_stub_long_branch_notoc);
11662 else if (info->emitrelocations)
11663 stub_entry->group->stub_sec->reloc_count +=1;
11666 case ppc_stub_plt_call_notoc:
11667 case ppc_stub_plt_call_both:
11668 off = (stub_entry->stub_offset
11669 + stub_entry->group->stub_sec->output_offset
11670 + stub_entry->group->stub_sec->output_section->vma);
11671 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11673 targ = stub_entry->plt_ent->plt.offset & ~1;
11674 if (targ >= (bfd_vma) -2)
11677 plt = htab->elf.splt;
11678 if (!htab->elf.dynamic_sections_created
11679 || stub_entry->h == NULL
11680 || stub_entry->h->elf.dynindx == -1)
11682 if (stub_entry->symtype == STT_GNU_IFUNC)
11683 plt = htab->elf.iplt;
11685 plt = htab->pltlocal;
11687 targ += plt->output_offset + plt->output_section->vma;
11691 if (htab->params->plt_stub_align != 0)
11693 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11695 stub_entry->group->stub_sec->size += pad;
11696 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11700 if (info->emitrelocations)
11702 unsigned int num_rel;
11703 if (htab->powerxx_stubs)
11704 num_rel = num_relocs_for_powerxx_offset (off, odd);
11706 num_rel = num_relocs_for_offset (off - 8);
11707 stub_entry->group->stub_sec->reloc_count += num_rel;
11708 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11711 size = plt_stub_size (htab, stub_entry, off);
11713 if (!htab->powerxx_stubs)
11715 /* After the bcl, lr has been modified so we need to emit
11716 .eh_frame info saying the return address is in r12. */
11717 lr_used = stub_entry->stub_offset + 8;
11718 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11720 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11721 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11722 DW_CFA_restore_extended 65. */
11723 delta = lr_used - stub_entry->group->lr_restore;
11724 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11725 stub_entry->group->lr_restore = lr_used + 8;
11729 case ppc_stub_plt_call:
11730 case ppc_stub_plt_call_r2save:
11731 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11732 if (targ >= (bfd_vma) -2)
11734 plt = htab->elf.splt;
11735 if (!htab->elf.dynamic_sections_created
11736 || stub_entry->h == NULL
11737 || stub_entry->h->elf.dynindx == -1)
11739 if (stub_entry->symtype == STT_GNU_IFUNC)
11740 plt = htab->elf.iplt;
11742 plt = htab->pltlocal;
11744 targ += plt->output_offset + plt->output_section->vma;
11746 off = (elf_gp (info->output_bfd)
11747 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11750 if (htab->params->plt_stub_align != 0)
11752 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11754 stub_entry->group->stub_sec->size += pad;
11755 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11758 if (info->emitrelocations)
11760 stub_entry->group->stub_sec->reloc_count
11761 += ((PPC_HA (off) != 0)
11763 ? 2 + (htab->params->plt_static_chain
11764 && PPC_HA (off + 16) == PPC_HA (off))
11766 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11769 size = plt_stub_size (htab, stub_entry, off);
11771 if (stub_entry->h != NULL
11772 && (stub_entry->h == htab->tls_get_addr_fd
11773 || stub_entry->h == htab->tls_get_addr)
11774 && htab->params->tls_get_addr_opt
11775 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11777 /* After the bctrl, lr has been modified so we need to
11778 emit .eh_frame info saying the return address is
11779 on the stack. In fact we put the EH info specifying
11780 that the return address is on the stack *at* the
11781 call rather than after it, because the EH info for a
11782 call needs to be specified by that point.
11783 See libgcc/unwind-dw2.c execute_cfa_program. */
11784 lr_used = stub_entry->stub_offset + size - 20;
11785 /* The eh_frame info will consist of a DW_CFA_advance_loc
11786 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11787 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
11788 delta = lr_used - stub_entry->group->lr_restore;
11789 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11790 stub_entry->group->lr_restore = size - 4;
11799 stub_entry->group->stub_sec->size += size;
11803 /* Set up various things so that we can make a list of input sections
11804 for each output section included in the link. Returns -1 on error,
11805 0 when no stubs will be needed, and 1 on success. */
11808 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11812 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11817 htab->sec_info_arr_size = _bfd_section_id;
11818 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11819 htab->sec_info = bfd_zmalloc (amt);
11820 if (htab->sec_info == NULL)
11823 /* Set toc_off for com, und, abs and ind sections. */
11824 for (id = 0; id < 3; id++)
11825 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11830 /* Set up for first pass at multitoc partitioning. */
11833 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11835 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11837 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11838 htab->toc_bfd = NULL;
11839 htab->toc_first_sec = NULL;
11842 /* The linker repeatedly calls this function for each TOC input section
11843 and linker generated GOT section. Group input bfds such that the toc
11844 within a group is less than 64k in size. */
11847 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11849 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11850 bfd_vma addr, off, limit;
11855 if (!htab->second_toc_pass)
11857 /* Keep track of the first .toc or .got section for this input bfd. */
11858 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11862 htab->toc_bfd = isec->owner;
11863 htab->toc_first_sec = isec;
11866 addr = isec->output_offset + isec->output_section->vma;
11867 off = addr - htab->toc_curr;
11868 limit = 0x80008000;
11869 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11871 if (off + isec->size > limit)
11873 addr = (htab->toc_first_sec->output_offset
11874 + htab->toc_first_sec->output_section->vma);
11875 htab->toc_curr = addr;
11876 htab->toc_curr &= -TOC_BASE_ALIGN;
11879 /* toc_curr is the base address of this toc group. Set elf_gp
11880 for the input section to be the offset relative to the
11881 output toc base plus 0x8000. Making the input elf_gp an
11882 offset allows us to move the toc as a whole without
11883 recalculating input elf_gp. */
11884 off = htab->toc_curr - elf_gp (info->output_bfd);
11885 off += TOC_BASE_OFF;
11887 /* Die if someone uses a linker script that doesn't keep input
11888 file .toc and .got together. */
11890 && elf_gp (isec->owner) != 0
11891 && elf_gp (isec->owner) != off)
11894 elf_gp (isec->owner) = off;
11898 /* During the second pass toc_first_sec points to the start of
11899 a toc group, and toc_curr is used to track the old elf_gp.
11900 We use toc_bfd to ensure we only look at each bfd once. */
11901 if (htab->toc_bfd == isec->owner)
11903 htab->toc_bfd = isec->owner;
11905 if (htab->toc_first_sec == NULL
11906 || htab->toc_curr != elf_gp (isec->owner))
11908 htab->toc_curr = elf_gp (isec->owner);
11909 htab->toc_first_sec = isec;
11911 addr = (htab->toc_first_sec->output_offset
11912 + htab->toc_first_sec->output_section->vma);
11913 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
11914 elf_gp (isec->owner) = off;
11919 /* Called via elf_link_hash_traverse to merge GOT entries for global
11923 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11925 if (h->root.type == bfd_link_hash_indirect)
11928 merge_got_entries (&h->got.glist);
11933 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11937 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11939 struct got_entry *gent;
11941 if (h->root.type == bfd_link_hash_indirect)
11944 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11945 if (!gent->is_indirect)
11946 allocate_got (h, (struct bfd_link_info *) inf, gent);
11950 /* Called on the first multitoc pass after the last call to
11951 ppc64_elf_next_toc_section. This function removes duplicate GOT
11955 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11957 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11958 struct bfd *ibfd, *ibfd2;
11959 bfd_boolean done_something;
11961 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11963 if (!htab->do_multi_toc)
11966 /* Merge global sym got entries within a toc group. */
11967 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11969 /* And tlsld_got. */
11970 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11972 struct got_entry *ent, *ent2;
11974 if (!is_ppc64_elf (ibfd))
11977 ent = ppc64_tlsld_got (ibfd);
11978 if (!ent->is_indirect
11979 && ent->got.offset != (bfd_vma) -1)
11981 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11983 if (!is_ppc64_elf (ibfd2))
11986 ent2 = ppc64_tlsld_got (ibfd2);
11987 if (!ent2->is_indirect
11988 && ent2->got.offset != (bfd_vma) -1
11989 && elf_gp (ibfd2) == elf_gp (ibfd))
11991 ent2->is_indirect = TRUE;
11992 ent2->got.ent = ent;
11998 /* Zap sizes of got sections. */
11999 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12000 htab->elf.irelplt->size -= htab->got_reli_size;
12001 htab->got_reli_size = 0;
12003 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12005 asection *got, *relgot;
12007 if (!is_ppc64_elf (ibfd))
12010 got = ppc64_elf_tdata (ibfd)->got;
12013 got->rawsize = got->size;
12015 relgot = ppc64_elf_tdata (ibfd)->relgot;
12016 relgot->rawsize = relgot->size;
12021 /* Now reallocate the got, local syms first. We don't need to
12022 allocate section contents again since we never increase size. */
12023 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12025 struct got_entry **lgot_ents;
12026 struct got_entry **end_lgot_ents;
12027 struct plt_entry **local_plt;
12028 struct plt_entry **end_local_plt;
12029 unsigned char *lgot_masks;
12030 bfd_size_type locsymcount;
12031 Elf_Internal_Shdr *symtab_hdr;
12034 if (!is_ppc64_elf (ibfd))
12037 lgot_ents = elf_local_got_ents (ibfd);
12041 symtab_hdr = &elf_symtab_hdr (ibfd);
12042 locsymcount = symtab_hdr->sh_info;
12043 end_lgot_ents = lgot_ents + locsymcount;
12044 local_plt = (struct plt_entry **) end_lgot_ents;
12045 end_local_plt = local_plt + locsymcount;
12046 lgot_masks = (unsigned char *) end_local_plt;
12047 s = ppc64_elf_tdata (ibfd)->got;
12048 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12050 struct got_entry *ent;
12052 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12054 unsigned int ent_size = 8;
12055 unsigned int rel_size = sizeof (Elf64_External_Rela);
12057 ent->got.offset = s->size;
12058 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12063 s->size += ent_size;
12064 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12066 htab->elf.irelplt->size += rel_size;
12067 htab->got_reli_size += rel_size;
12069 else if (bfd_link_pic (info)
12070 && !((ent->tls_type & TLS_TPREL) != 0
12071 && bfd_link_executable (info)))
12073 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12074 srel->size += rel_size;
12080 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12082 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12084 struct got_entry *ent;
12086 if (!is_ppc64_elf (ibfd))
12089 ent = ppc64_tlsld_got (ibfd);
12090 if (!ent->is_indirect
12091 && ent->got.offset != (bfd_vma) -1)
12093 asection *s = ppc64_elf_tdata (ibfd)->got;
12094 ent->got.offset = s->size;
12096 if (bfd_link_pic (info))
12098 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12099 srel->size += sizeof (Elf64_External_Rela);
12104 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12105 if (!done_something)
12106 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12110 if (!is_ppc64_elf (ibfd))
12113 got = ppc64_elf_tdata (ibfd)->got;
12116 done_something = got->rawsize != got->size;
12117 if (done_something)
12122 if (done_something)
12123 (*htab->params->layout_sections_again) ();
12125 /* Set up for second pass over toc sections to recalculate elf_gp
12126 on input sections. */
12127 htab->toc_bfd = NULL;
12128 htab->toc_first_sec = NULL;
12129 htab->second_toc_pass = TRUE;
12130 return done_something;
12133 /* Called after second pass of multitoc partitioning. */
12136 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12138 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12140 /* After the second pass, toc_curr tracks the TOC offset used
12141 for code sections below in ppc64_elf_next_input_section. */
12142 htab->toc_curr = TOC_BASE_OFF;
12145 /* No toc references were found in ISEC. If the code in ISEC makes no
12146 calls, then there's no need to use toc adjusting stubs when branching
12147 into ISEC. Actually, indirect calls from ISEC are OK as they will
12148 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12149 needed, and 2 if a cyclical call-graph was found but no other reason
12150 for a stub was detected. If called from the top level, a return of
12151 2 means the same as a return of 0. */
12154 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12158 /* Mark this section as checked. */
12159 isec->call_check_done = 1;
12161 /* We know none of our code bearing sections will need toc stubs. */
12162 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12165 if (isec->size == 0)
12168 if (isec->output_section == NULL)
12172 if (isec->reloc_count != 0)
12174 Elf_Internal_Rela *relstart, *rel;
12175 Elf_Internal_Sym *local_syms;
12176 struct ppc_link_hash_table *htab;
12178 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12179 info->keep_memory);
12180 if (relstart == NULL)
12183 /* Look for branches to outside of this section. */
12185 htab = ppc_hash_table (info);
12189 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12191 enum elf_ppc64_reloc_type r_type;
12192 unsigned long r_symndx;
12193 struct elf_link_hash_entry *h;
12194 struct ppc_link_hash_entry *eh;
12195 Elf_Internal_Sym *sym;
12197 struct _opd_sec_data *opd;
12201 r_type = ELF64_R_TYPE (rel->r_info);
12202 if (r_type != R_PPC64_REL24
12203 && r_type != R_PPC64_REL24_NOTOC
12204 && r_type != R_PPC64_REL14
12205 && r_type != R_PPC64_REL14_BRTAKEN
12206 && r_type != R_PPC64_REL14_BRNTAKEN
12207 && r_type != R_PPC64_PLTCALL
12208 && r_type != R_PPC64_PLTCALL_NOTOC)
12211 r_symndx = ELF64_R_SYM (rel->r_info);
12212 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12219 /* Calls to dynamic lib functions go through a plt call stub
12221 eh = (struct ppc_link_hash_entry *) h;
12223 && (eh->elf.plt.plist != NULL
12225 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12231 if (sym_sec == NULL)
12232 /* Ignore other undefined symbols. */
12235 /* Assume branches to other sections not included in the
12236 link need stubs too, to cover -R and absolute syms. */
12237 if (sym_sec->output_section == NULL)
12244 sym_value = sym->st_value;
12247 if (h->root.type != bfd_link_hash_defined
12248 && h->root.type != bfd_link_hash_defweak)
12250 sym_value = h->root.u.def.value;
12252 sym_value += rel->r_addend;
12254 /* If this branch reloc uses an opd sym, find the code section. */
12255 opd = get_opd_info (sym_sec);
12258 if (h == NULL && opd->adjust != NULL)
12262 adjust = opd->adjust[OPD_NDX (sym_value)];
12264 /* Assume deleted functions won't ever be called. */
12266 sym_value += adjust;
12269 dest = opd_entry_value (sym_sec, sym_value,
12270 &sym_sec, NULL, FALSE);
12271 if (dest == (bfd_vma) -1)
12276 + sym_sec->output_offset
12277 + sym_sec->output_section->vma);
12279 /* Ignore branch to self. */
12280 if (sym_sec == isec)
12283 /* If the called function uses the toc, we need a stub. */
12284 if (sym_sec->has_toc_reloc
12285 || sym_sec->makes_toc_func_call)
12291 /* Assume any branch that needs a long branch stub might in fact
12292 need a plt_branch stub. A plt_branch stub uses r2. */
12293 else if (dest - (isec->output_offset
12294 + isec->output_section->vma
12295 + rel->r_offset) + (1 << 25)
12296 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12304 /* If calling back to a section in the process of being
12305 tested, we can't say for sure that no toc adjusting stubs
12306 are needed, so don't return zero. */
12307 else if (sym_sec->call_check_in_progress)
12310 /* Branches to another section that itself doesn't have any TOC
12311 references are OK. Recursively call ourselves to check. */
12312 else if (!sym_sec->call_check_done)
12316 /* Mark current section as indeterminate, so that other
12317 sections that call back to current won't be marked as
12319 isec->call_check_in_progress = 1;
12320 recur = toc_adjusting_stub_needed (info, sym_sec);
12321 isec->call_check_in_progress = 0;
12332 if (local_syms != NULL
12333 && (elf_symtab_hdr (isec->owner).contents
12334 != (unsigned char *) local_syms))
12336 if (elf_section_data (isec)->relocs != relstart)
12341 && isec->map_head.s != NULL
12342 && (strcmp (isec->output_section->name, ".init") == 0
12343 || strcmp (isec->output_section->name, ".fini") == 0))
12345 if (isec->map_head.s->has_toc_reloc
12346 || isec->map_head.s->makes_toc_func_call)
12348 else if (!isec->map_head.s->call_check_done)
12351 isec->call_check_in_progress = 1;
12352 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12353 isec->call_check_in_progress = 0;
12360 isec->makes_toc_func_call = 1;
12365 /* The linker repeatedly calls this function for each input section,
12366 in the order that input sections are linked into output sections.
12367 Build lists of input sections to determine groupings between which
12368 we may insert linker stubs. */
12371 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12373 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12378 if ((isec->output_section->flags & SEC_CODE) != 0
12379 && isec->output_section->id < htab->sec_info_arr_size)
12381 /* This happens to make the list in reverse order,
12382 which is what we want. */
12383 htab->sec_info[isec->id].u.list
12384 = htab->sec_info[isec->output_section->id].u.list;
12385 htab->sec_info[isec->output_section->id].u.list = isec;
12388 if (htab->multi_toc_needed)
12390 /* Analyse sections that aren't already flagged as needing a
12391 valid toc pointer. Exclude .fixup for the linux kernel.
12392 .fixup contains branches, but only back to the function that
12393 hit an exception. */
12394 if (!(isec->has_toc_reloc
12395 || (isec->flags & SEC_CODE) == 0
12396 || strcmp (isec->name, ".fixup") == 0
12397 || isec->call_check_done))
12399 if (toc_adjusting_stub_needed (info, isec) < 0)
12402 /* Make all sections use the TOC assigned for this object file.
12403 This will be wrong for pasted sections; We fix that in
12404 check_pasted_section(). */
12405 if (elf_gp (isec->owner) != 0)
12406 htab->toc_curr = elf_gp (isec->owner);
12409 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12413 /* Check that all .init and .fini sections use the same toc, if they
12414 have toc relocs. */
12417 check_pasted_section (struct bfd_link_info *info, const char *name)
12419 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12423 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12424 bfd_vma toc_off = 0;
12427 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12428 if (i->has_toc_reloc)
12431 toc_off = htab->sec_info[i->id].toc_off;
12432 else if (toc_off != htab->sec_info[i->id].toc_off)
12437 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12438 if (i->makes_toc_func_call)
12440 toc_off = htab->sec_info[i->id].toc_off;
12444 /* Make sure the whole pasted function uses the same toc offset. */
12446 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12447 htab->sec_info[i->id].toc_off = toc_off;
12453 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12455 return (check_pasted_section (info, ".init")
12456 & check_pasted_section (info, ".fini"));
12459 /* See whether we can group stub sections together. Grouping stub
12460 sections may result in fewer stubs. More importantly, we need to
12461 put all .init* and .fini* stubs at the beginning of the .init or
12462 .fini output sections respectively, because glibc splits the
12463 _init and _fini functions into multiple parts. Putting a stub in
12464 the middle of a function is not a good idea. */
12467 group_sections (struct bfd_link_info *info,
12468 bfd_size_type stub_group_size,
12469 bfd_boolean stubs_always_before_branch)
12471 struct ppc_link_hash_table *htab;
12473 bfd_boolean suppress_size_errors;
12475 htab = ppc_hash_table (info);
12479 suppress_size_errors = FALSE;
12480 if (stub_group_size == 1)
12482 /* Default values. */
12483 if (stubs_always_before_branch)
12484 stub_group_size = 0x1e00000;
12486 stub_group_size = 0x1c00000;
12487 suppress_size_errors = TRUE;
12490 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12494 if (osec->id >= htab->sec_info_arr_size)
12497 tail = htab->sec_info[osec->id].u.list;
12498 while (tail != NULL)
12502 bfd_size_type total;
12503 bfd_boolean big_sec;
12505 struct map_stub *group;
12506 bfd_size_type group_size;
12509 total = tail->size;
12510 group_size = (ppc64_elf_section_data (tail) != NULL
12511 && ppc64_elf_section_data (tail)->has_14bit_branch
12512 ? stub_group_size >> 10 : stub_group_size);
12514 big_sec = total > group_size;
12515 if (big_sec && !suppress_size_errors)
12516 /* xgettext:c-format */
12517 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12518 tail->owner, tail);
12519 curr_toc = htab->sec_info[tail->id].toc_off;
12521 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12522 && ((total += curr->output_offset - prev->output_offset)
12523 < (ppc64_elf_section_data (prev) != NULL
12524 && ppc64_elf_section_data (prev)->has_14bit_branch
12525 ? (group_size = stub_group_size >> 10) : group_size))
12526 && htab->sec_info[prev->id].toc_off == curr_toc)
12529 /* OK, the size from the start of CURR to the end is less
12530 than group_size and thus can be handled by one stub
12531 section. (or the tail section is itself larger than
12532 group_size, in which case we may be toast.) We should
12533 really be keeping track of the total size of stubs added
12534 here, as stubs contribute to the final output section
12535 size. That's a little tricky, and this way will only
12536 break if stubs added make the total size more than 2^25,
12537 ie. for the default stub_group_size, if stubs total more
12538 than 2097152 bytes, or nearly 75000 plt call stubs. */
12539 group = bfd_alloc (curr->owner, sizeof (*group));
12542 group->link_sec = curr;
12543 group->stub_sec = NULL;
12544 group->needs_save_res = 0;
12545 group->lr_restore = 0;
12546 group->eh_size = 0;
12547 group->eh_base = 0;
12548 group->next = htab->group;
12549 htab->group = group;
12552 prev = htab->sec_info[tail->id].u.list;
12553 /* Set up this stub group. */
12554 htab->sec_info[tail->id].u.group = group;
12556 while (tail != curr && (tail = prev) != NULL);
12558 /* But wait, there's more! Input sections up to group_size
12559 bytes before the stub section can be handled by it too.
12560 Don't do this if we have a really large section after the
12561 stubs, as adding more stubs increases the chance that
12562 branches may not reach into the stub section. */
12563 if (!stubs_always_before_branch && !big_sec)
12566 while (prev != NULL
12567 && ((total += tail->output_offset - prev->output_offset)
12568 < (ppc64_elf_section_data (prev) != NULL
12569 && ppc64_elf_section_data (prev)->has_14bit_branch
12570 ? (group_size = stub_group_size >> 10)
12572 && htab->sec_info[prev->id].toc_off == curr_toc)
12575 prev = htab->sec_info[tail->id].u.list;
12576 htab->sec_info[tail->id].u.group = group;
12585 static const unsigned char glink_eh_frame_cie[] =
12587 0, 0, 0, 16, /* length. */
12588 0, 0, 0, 0, /* id. */
12589 1, /* CIE version. */
12590 'z', 'R', 0, /* Augmentation string. */
12591 4, /* Code alignment. */
12592 0x78, /* Data alignment. */
12594 1, /* Augmentation size. */
12595 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12596 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
12599 /* Stripping output sections is normally done before dynamic section
12600 symbols have been allocated. This function is called later, and
12601 handles cases like htab->brlt which is mapped to its own output
12605 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12607 if (isec->size == 0
12608 && isec->output_section->size == 0
12609 && !(isec->output_section->flags & SEC_KEEP)
12610 && !bfd_section_removed_from_list (info->output_bfd,
12611 isec->output_section)
12612 && elf_section_data (isec->output_section)->dynindx == 0)
12614 isec->output_section->flags |= SEC_EXCLUDE;
12615 bfd_section_list_remove (info->output_bfd, isec->output_section);
12616 info->output_bfd->section_count--;
12620 /* Determine and set the size of the stub section for a final link.
12622 The basic idea here is to examine all the relocations looking for
12623 PC-relative calls to a target that is unreachable with a "bl"
12627 ppc64_elf_size_stubs (struct bfd_link_info *info)
12629 bfd_size_type stub_group_size;
12630 bfd_boolean stubs_always_before_branch;
12631 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12636 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12637 htab->params->plt_thread_safe = 1;
12638 if (!htab->opd_abi)
12639 htab->params->plt_thread_safe = 0;
12640 else if (htab->params->plt_thread_safe == -1)
12642 static const char *const thread_starter[] =
12646 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12648 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12649 "mq_notify", "create_timer",
12654 "GOMP_parallel_start",
12655 "GOMP_parallel_loop_static",
12656 "GOMP_parallel_loop_static_start",
12657 "GOMP_parallel_loop_dynamic",
12658 "GOMP_parallel_loop_dynamic_start",
12659 "GOMP_parallel_loop_guided",
12660 "GOMP_parallel_loop_guided_start",
12661 "GOMP_parallel_loop_runtime",
12662 "GOMP_parallel_loop_runtime_start",
12663 "GOMP_parallel_sections",
12664 "GOMP_parallel_sections_start",
12670 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12672 struct elf_link_hash_entry *h;
12673 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12674 FALSE, FALSE, TRUE);
12675 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12676 if (htab->params->plt_thread_safe)
12680 stubs_always_before_branch = htab->params->group_size < 0;
12681 if (htab->params->group_size < 0)
12682 stub_group_size = -htab->params->group_size;
12684 stub_group_size = htab->params->group_size;
12686 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12689 #define STUB_SHRINK_ITER 20
12690 /* Loop until no stubs added. After iteration 20 of this loop we may
12691 exit on a stub section shrinking. This is to break out of a
12692 pathological case where adding stubs on one iteration decreases
12693 section gaps (perhaps due to alignment), which then requires
12694 fewer or smaller stubs on the next iteration. */
12699 unsigned int bfd_indx;
12700 struct map_stub *group;
12702 htab->stub_iteration += 1;
12704 for (input_bfd = info->input_bfds, bfd_indx = 0;
12706 input_bfd = input_bfd->link.next, bfd_indx++)
12708 Elf_Internal_Shdr *symtab_hdr;
12710 Elf_Internal_Sym *local_syms = NULL;
12712 if (!is_ppc64_elf (input_bfd))
12715 /* We'll need the symbol table in a second. */
12716 symtab_hdr = &elf_symtab_hdr (input_bfd);
12717 if (symtab_hdr->sh_info == 0)
12720 /* Walk over each section attached to the input bfd. */
12721 for (section = input_bfd->sections;
12723 section = section->next)
12725 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12727 /* If there aren't any relocs, then there's nothing more
12729 if ((section->flags & SEC_RELOC) == 0
12730 || (section->flags & SEC_ALLOC) == 0
12731 || (section->flags & SEC_LOAD) == 0
12732 || (section->flags & SEC_CODE) == 0
12733 || section->reloc_count == 0)
12736 /* If this section is a link-once section that will be
12737 discarded, then don't create any stubs. */
12738 if (section->output_section == NULL
12739 || section->output_section->owner != info->output_bfd)
12742 /* Get the relocs. */
12744 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12745 info->keep_memory);
12746 if (internal_relocs == NULL)
12747 goto error_ret_free_local;
12749 /* Now examine each relocation. */
12750 irela = internal_relocs;
12751 irelaend = irela + section->reloc_count;
12752 for (; irela < irelaend; irela++)
12754 enum elf_ppc64_reloc_type r_type;
12755 unsigned int r_indx;
12756 enum ppc_stub_type stub_type;
12757 struct ppc_stub_hash_entry *stub_entry;
12758 asection *sym_sec, *code_sec;
12759 bfd_vma sym_value, code_value;
12760 bfd_vma destination;
12761 unsigned long local_off;
12762 bfd_boolean ok_dest;
12763 struct ppc_link_hash_entry *hash;
12764 struct ppc_link_hash_entry *fdh;
12765 struct elf_link_hash_entry *h;
12766 Elf_Internal_Sym *sym;
12768 const asection *id_sec;
12769 struct _opd_sec_data *opd;
12770 struct plt_entry *plt_ent;
12772 r_type = ELF64_R_TYPE (irela->r_info);
12773 r_indx = ELF64_R_SYM (irela->r_info);
12775 if (r_type >= R_PPC64_max)
12777 bfd_set_error (bfd_error_bad_value);
12778 goto error_ret_free_internal;
12781 /* Only look for stubs on branch instructions. */
12782 if (r_type != R_PPC64_REL24
12783 && r_type != R_PPC64_REL24_NOTOC
12784 && r_type != R_PPC64_REL14
12785 && r_type != R_PPC64_REL14_BRTAKEN
12786 && r_type != R_PPC64_REL14_BRNTAKEN)
12789 /* Now determine the call target, its name, value,
12791 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12792 r_indx, input_bfd))
12793 goto error_ret_free_internal;
12794 hash = (struct ppc_link_hash_entry *) h;
12801 sym_value = sym->st_value;
12802 if (sym_sec != NULL
12803 && sym_sec->output_section != NULL)
12806 else if (hash->elf.root.type == bfd_link_hash_defined
12807 || hash->elf.root.type == bfd_link_hash_defweak)
12809 sym_value = hash->elf.root.u.def.value;
12810 if (sym_sec->output_section != NULL)
12813 else if (hash->elf.root.type == bfd_link_hash_undefweak
12814 || hash->elf.root.type == bfd_link_hash_undefined)
12816 /* Recognise an old ABI func code entry sym, and
12817 use the func descriptor sym instead if it is
12819 if (hash->elf.root.root.string[0] == '.'
12820 && hash->oh != NULL)
12822 fdh = ppc_follow_link (hash->oh);
12823 if (fdh->elf.root.type == bfd_link_hash_defined
12824 || fdh->elf.root.type == bfd_link_hash_defweak)
12826 sym_sec = fdh->elf.root.u.def.section;
12827 sym_value = fdh->elf.root.u.def.value;
12828 if (sym_sec->output_section != NULL)
12837 bfd_set_error (bfd_error_bad_value);
12838 goto error_ret_free_internal;
12845 sym_value += irela->r_addend;
12846 destination = (sym_value
12847 + sym_sec->output_offset
12848 + sym_sec->output_section->vma);
12849 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12854 code_sec = sym_sec;
12855 code_value = sym_value;
12856 opd = get_opd_info (sym_sec);
12861 if (hash == NULL && opd->adjust != NULL)
12863 long adjust = opd->adjust[OPD_NDX (sym_value)];
12866 code_value += adjust;
12867 sym_value += adjust;
12869 dest = opd_entry_value (sym_sec, sym_value,
12870 &code_sec, &code_value, FALSE);
12871 if (dest != (bfd_vma) -1)
12873 destination = dest;
12876 /* Fixup old ABI sym to point at code
12878 hash->elf.root.type = bfd_link_hash_defweak;
12879 hash->elf.root.u.def.section = code_sec;
12880 hash->elf.root.u.def.value = code_value;
12885 /* Determine what (if any) linker stub is needed. */
12887 stub_type = ppc_type_of_stub (section, irela, &hash,
12888 &plt_ent, destination,
12891 if (r_type == R_PPC64_REL24_NOTOC)
12893 if (stub_type == ppc_stub_plt_call)
12894 stub_type = ppc_stub_plt_call_notoc;
12895 else if (stub_type == ppc_stub_long_branch
12896 || (code_sec != NULL
12897 && code_sec->output_section != NULL
12898 && (((hash ? hash->elf.other : sym->st_other)
12899 & STO_PPC64_LOCAL_MASK)
12900 != 1 << STO_PPC64_LOCAL_BIT)))
12901 stub_type = ppc_stub_long_branch_notoc;
12903 else if (stub_type != ppc_stub_plt_call)
12905 /* Check whether we need a TOC adjusting stub.
12906 Since the linker pastes together pieces from
12907 different object files when creating the
12908 _init and _fini functions, it may be that a
12909 call to what looks like a local sym is in
12910 fact a call needing a TOC adjustment. */
12911 if ((code_sec != NULL
12912 && code_sec->output_section != NULL
12913 && (htab->sec_info[code_sec->id].toc_off
12914 != htab->sec_info[section->id].toc_off)
12915 && (code_sec->has_toc_reloc
12916 || code_sec->makes_toc_func_call))
12917 || (((hash ? hash->elf.other : sym->st_other)
12918 & STO_PPC64_LOCAL_MASK)
12919 == 1 << STO_PPC64_LOCAL_BIT))
12920 stub_type = ppc_stub_long_branch_r2off;
12923 if (stub_type == ppc_stub_none)
12926 /* __tls_get_addr calls might be eliminated. */
12927 if (stub_type != ppc_stub_plt_call
12928 && stub_type != ppc_stub_plt_call_notoc
12930 && (hash == htab->tls_get_addr
12931 || hash == htab->tls_get_addr_fd)
12932 && section->has_tls_reloc
12933 && irela != internal_relocs)
12935 /* Get tls info. */
12936 unsigned char *tls_mask;
12938 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12939 irela - 1, input_bfd))
12940 goto error_ret_free_internal;
12941 if ((*tls_mask & TLS_TLS) != 0)
12945 if (stub_type == ppc_stub_plt_call)
12948 && htab->params->plt_localentry0 != 0
12949 && is_elfv2_localentry0 (&hash->elf))
12950 htab->has_plt_localentry0 = 1;
12951 else if (irela + 1 < irelaend
12952 && irela[1].r_offset == irela->r_offset + 4
12953 && (ELF64_R_TYPE (irela[1].r_info)
12954 == R_PPC64_TOCSAVE))
12956 if (!tocsave_find (htab, INSERT,
12957 &local_syms, irela + 1, input_bfd))
12958 goto error_ret_free_internal;
12961 stub_type = ppc_stub_plt_call_r2save;
12964 /* Support for grouping stub sections. */
12965 id_sec = htab->sec_info[section->id].u.group->link_sec;
12967 /* Get the name of this stub. */
12968 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12970 goto error_ret_free_internal;
12972 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12973 stub_name, FALSE, FALSE);
12974 if (stub_entry != NULL)
12976 enum ppc_stub_type old_type;
12977 /* A stub has already been created, but it may
12978 not be the required type. We shouldn't be
12979 transitioning from plt_call to long_branch
12980 stubs or vice versa, but we might be
12981 upgrading from plt_call to plt_call_r2save or
12982 from long_branch to long_branch_r2off. */
12984 old_type = stub_entry->stub_type;
12990 case ppc_stub_save_res:
12993 case ppc_stub_plt_call:
12994 case ppc_stub_plt_call_r2save:
12995 case ppc_stub_plt_call_notoc:
12996 case ppc_stub_plt_call_both:
12997 if (stub_type == ppc_stub_plt_call)
12999 else if (stub_type == ppc_stub_plt_call_r2save)
13001 if (old_type == ppc_stub_plt_call_notoc)
13002 stub_type = ppc_stub_plt_call_both;
13004 else if (stub_type == ppc_stub_plt_call_notoc)
13006 if (old_type == ppc_stub_plt_call_r2save)
13007 stub_type = ppc_stub_plt_call_both;
13013 case ppc_stub_plt_branch:
13014 case ppc_stub_plt_branch_r2off:
13015 case ppc_stub_plt_branch_notoc:
13016 case ppc_stub_plt_branch_both:
13017 old_type += (ppc_stub_long_branch
13018 - ppc_stub_plt_branch);
13019 /* Fall through. */
13020 case ppc_stub_long_branch:
13021 case ppc_stub_long_branch_r2off:
13022 case ppc_stub_long_branch_notoc:
13023 case ppc_stub_long_branch_both:
13024 if (stub_type == ppc_stub_long_branch)
13026 else if (stub_type == ppc_stub_long_branch_r2off)
13028 if (old_type == ppc_stub_long_branch_notoc)
13029 stub_type = ppc_stub_long_branch_both;
13031 else if (stub_type == ppc_stub_long_branch_notoc)
13033 if (old_type == ppc_stub_long_branch_r2off)
13034 stub_type = ppc_stub_long_branch_both;
13040 if (old_type < stub_type)
13041 stub_entry->stub_type = stub_type;
13045 stub_entry = ppc_add_stub (stub_name, section, info);
13046 if (stub_entry == NULL)
13049 error_ret_free_internal:
13050 if (elf_section_data (section)->relocs == NULL)
13051 free (internal_relocs);
13052 error_ret_free_local:
13053 if (local_syms != NULL
13054 && (symtab_hdr->contents
13055 != (unsigned char *) local_syms))
13060 stub_entry->stub_type = stub_type;
13061 if (stub_type >= ppc_stub_plt_call
13062 && stub_type <= ppc_stub_plt_call_both)
13064 stub_entry->target_value = sym_value;
13065 stub_entry->target_section = sym_sec;
13069 stub_entry->target_value = code_value;
13070 stub_entry->target_section = code_sec;
13072 stub_entry->h = hash;
13073 stub_entry->plt_ent = plt_ent;
13074 stub_entry->symtype
13075 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
13076 stub_entry->other = hash ? hash->elf.other : sym->st_other;
13079 && (hash->elf.root.type == bfd_link_hash_defined
13080 || hash->elf.root.type == bfd_link_hash_defweak))
13081 htab->stub_globals += 1;
13084 /* We're done with the internal relocs, free them. */
13085 if (elf_section_data (section)->relocs != internal_relocs)
13086 free (internal_relocs);
13089 if (local_syms != NULL
13090 && symtab_hdr->contents != (unsigned char *) local_syms)
13092 if (!info->keep_memory)
13095 symtab_hdr->contents = (unsigned char *) local_syms;
13099 /* We may have added some stubs. Find out the new size of the
13101 for (group = htab->group; group != NULL; group = group->next)
13103 group->lr_restore = 0;
13104 group->eh_size = 0;
13105 if (group->stub_sec != NULL)
13107 asection *stub_sec = group->stub_sec;
13109 if (htab->stub_iteration <= STUB_SHRINK_ITER
13110 || stub_sec->rawsize < stub_sec->size)
13111 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13112 stub_sec->rawsize = stub_sec->size;
13113 stub_sec->size = 0;
13114 stub_sec->reloc_count = 0;
13115 stub_sec->flags &= ~SEC_RELOC;
13119 if (htab->stub_iteration <= STUB_SHRINK_ITER
13120 || htab->brlt->rawsize < htab->brlt->size)
13121 htab->brlt->rawsize = htab->brlt->size;
13122 htab->brlt->size = 0;
13123 htab->brlt->reloc_count = 0;
13124 htab->brlt->flags &= ~SEC_RELOC;
13125 if (htab->relbrlt != NULL)
13126 htab->relbrlt->size = 0;
13128 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
13130 for (group = htab->group; group != NULL; group = group->next)
13131 if (group->needs_save_res)
13132 group->stub_sec->size += htab->sfpr->size;
13134 if (info->emitrelocations
13135 && htab->glink != NULL && htab->glink->size != 0)
13137 htab->glink->reloc_count = 1;
13138 htab->glink->flags |= SEC_RELOC;
13141 if (htab->glink_eh_frame != NULL
13142 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
13143 && htab->glink_eh_frame->output_section->size > 8)
13145 size_t size = 0, align = 4;
13147 for (group = htab->group; group != NULL; group = group->next)
13148 if (group->eh_size != 0)
13149 size += (group->eh_size + 17 + align - 1) & -align;
13150 if (htab->glink != NULL && htab->glink->size != 0)
13151 size += (24 + align - 1) & -align;
13153 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13154 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13155 size = (size + align - 1) & -align;
13156 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13157 htab->glink_eh_frame->size = size;
13160 if (htab->params->plt_stub_align != 0)
13161 for (group = htab->group; group != NULL; group = group->next)
13162 if (group->stub_sec != NULL)
13164 int align = abs (htab->params->plt_stub_align);
13165 group->stub_sec->size
13166 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13169 for (group = htab->group; group != NULL; group = group->next)
13170 if (group->stub_sec != NULL
13171 && group->stub_sec->rawsize != group->stub_sec->size
13172 && (htab->stub_iteration <= STUB_SHRINK_ITER
13173 || group->stub_sec->rawsize < group->stub_sec->size))
13177 && (htab->brlt->rawsize == htab->brlt->size
13178 || (htab->stub_iteration > STUB_SHRINK_ITER
13179 && htab->brlt->rawsize > htab->brlt->size))
13180 && (htab->glink_eh_frame == NULL
13181 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
13184 /* Ask the linker to do its stuff. */
13185 (*htab->params->layout_sections_again) ();
13188 if (htab->glink_eh_frame != NULL
13189 && htab->glink_eh_frame->size != 0)
13192 bfd_byte *p, *last_fde;
13193 size_t last_fde_len, size, align, pad;
13194 struct map_stub *group;
13196 /* It is necessary to at least have a rough outline of the
13197 linker generated CIEs and FDEs written before
13198 bfd_elf_discard_info is run, in order for these FDEs to be
13199 indexed in .eh_frame_hdr. */
13200 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13203 htab->glink_eh_frame->contents = p;
13207 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13208 /* CIE length (rewrite in case little-endian). */
13209 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
13210 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13211 p += last_fde_len + 4;
13213 for (group = htab->group; group != NULL; group = group->next)
13214 if (group->eh_size != 0)
13216 group->eh_base = p - htab->glink_eh_frame->contents;
13218 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
13220 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13223 val = p - htab->glink_eh_frame->contents;
13224 bfd_put_32 (htab->elf.dynobj, val, p);
13226 /* Offset to stub section, written later. */
13228 /* stub section size. */
13229 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
13231 /* Augmentation. */
13233 /* Make sure we don't have all nops. This is enough for
13234 elf-eh-frame.c to detect the last non-nop opcode. */
13235 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
13236 p = last_fde + last_fde_len + 4;
13238 if (htab->glink != NULL && htab->glink->size != 0)
13241 last_fde_len = ((24 + align - 1) & -align) - 4;
13243 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13246 val = p - htab->glink_eh_frame->contents;
13247 bfd_put_32 (htab->elf.dynobj, val, p);
13249 /* Offset to .glink, written later. */
13252 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13254 /* Augmentation. */
13257 *p++ = DW_CFA_advance_loc + 1;
13258 *p++ = DW_CFA_register;
13260 *p++ = htab->opd_abi ? 12 : 0;
13261 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
13262 *p++ = DW_CFA_restore_extended;
13264 p += ((24 + align - 1) & -align) - 24;
13266 /* Subsume any padding into the last FDE if user .eh_frame
13267 sections are aligned more than glink_eh_frame. Otherwise any
13268 zero padding will be seen as a terminator. */
13269 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13270 size = p - htab->glink_eh_frame->contents;
13271 pad = ((size + align - 1) & -align) - size;
13272 htab->glink_eh_frame->size = size + pad;
13273 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13276 maybe_strip_output (info, htab->brlt);
13277 if (htab->glink_eh_frame != NULL)
13278 maybe_strip_output (info, htab->glink_eh_frame);
13283 /* Called after we have determined section placement. If sections
13284 move, we'll be called again. Provide a value for TOCstart. */
13287 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13290 bfd_vma TOCstart, adjust;
13294 struct elf_link_hash_entry *h;
13295 struct elf_link_hash_table *htab = elf_hash_table (info);
13297 if (is_elf_hash_table (htab)
13298 && htab->hgot != NULL)
13302 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13303 if (is_elf_hash_table (htab))
13307 && h->root.type == bfd_link_hash_defined
13308 && !h->root.linker_def
13309 && (!is_elf_hash_table (htab)
13310 || h->def_regular))
13312 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13313 + h->root.u.def.section->output_offset
13314 + h->root.u.def.section->output_section->vma);
13315 _bfd_set_gp_value (obfd, TOCstart);
13320 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13321 order. The TOC starts where the first of these sections starts. */
13322 s = bfd_get_section_by_name (obfd, ".got");
13323 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13324 s = bfd_get_section_by_name (obfd, ".toc");
13325 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13326 s = bfd_get_section_by_name (obfd, ".tocbss");
13327 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13328 s = bfd_get_section_by_name (obfd, ".plt");
13329 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13331 /* This may happen for
13332 o references to TOC base (SYM@toc / TOC[tc0]) without a
13334 o bad linker script
13335 o --gc-sections and empty TOC sections
13337 FIXME: Warn user? */
13339 /* Look for a likely section. We probably won't even be
13341 for (s = obfd->sections; s != NULL; s = s->next)
13342 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13344 == (SEC_ALLOC | SEC_SMALL_DATA))
13347 for (s = obfd->sections; s != NULL; s = s->next)
13348 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13349 == (SEC_ALLOC | SEC_SMALL_DATA))
13352 for (s = obfd->sections; s != NULL; s = s->next)
13353 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13357 for (s = obfd->sections; s != NULL; s = s->next)
13358 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13364 TOCstart = s->output_section->vma + s->output_offset;
13366 /* Force alignment. */
13367 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13368 TOCstart -= adjust;
13369 _bfd_set_gp_value (obfd, TOCstart);
13371 if (info != NULL && s != NULL)
13373 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13377 if (htab->elf.hgot != NULL)
13379 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13380 htab->elf.hgot->root.u.def.section = s;
13385 struct bfd_link_hash_entry *bh = NULL;
13386 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13387 s, TOC_BASE_OFF - adjust,
13388 NULL, FALSE, FALSE, &bh);
13394 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13395 write out any global entry stubs, and PLT relocations. */
13398 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13400 struct bfd_link_info *info;
13401 struct ppc_link_hash_table *htab;
13402 struct plt_entry *ent;
13405 if (h->root.type == bfd_link_hash_indirect)
13409 htab = ppc_hash_table (info);
13413 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13414 if (ent->plt.offset != (bfd_vma) -1)
13416 /* This symbol has an entry in the procedure linkage
13417 table. Set it up. */
13418 Elf_Internal_Rela rela;
13419 asection *plt, *relplt;
13422 if (!htab->elf.dynamic_sections_created
13423 || h->dynindx == -1)
13425 if (!(h->def_regular
13426 && (h->root.type == bfd_link_hash_defined
13427 || h->root.type == bfd_link_hash_defweak)))
13429 if (h->type == STT_GNU_IFUNC)
13431 plt = htab->elf.iplt;
13432 relplt = htab->elf.irelplt;
13433 htab->local_ifunc_resolver = 1;
13435 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13437 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13441 plt = htab->pltlocal;
13442 if (bfd_link_pic (info))
13444 relplt = htab->relpltlocal;
13446 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13448 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13453 rela.r_addend = (h->root.u.def.value
13454 + h->root.u.def.section->output_offset
13455 + h->root.u.def.section->output_section->vma
13458 if (relplt == NULL)
13460 loc = plt->contents + ent->plt.offset;
13461 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13464 bfd_vma toc = elf_gp (info->output_bfd);
13465 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13466 bfd_put_64 (info->output_bfd, toc, loc + 8);
13471 rela.r_offset = (plt->output_section->vma
13472 + plt->output_offset
13473 + ent->plt.offset);
13474 loc = relplt->contents + (relplt->reloc_count++
13475 * sizeof (Elf64_External_Rela));
13476 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13481 rela.r_offset = (htab->elf.splt->output_section->vma
13482 + htab->elf.splt->output_offset
13483 + ent->plt.offset);
13484 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13485 rela.r_addend = ent->addend;
13486 loc = (htab->elf.srelplt->contents
13487 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13488 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13489 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13490 htab->maybe_local_ifunc_resolver = 1;
13491 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13495 if (!h->pointer_equality_needed)
13498 if (h->def_regular)
13501 s = htab->global_entry;
13502 if (s == NULL || s->size == 0)
13505 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13506 if (ent->plt.offset != (bfd_vma) -1
13507 && ent->addend == 0)
13513 p = s->contents + h->root.u.def.value;
13514 plt = htab->elf.splt;
13515 if (!htab->elf.dynamic_sections_created
13516 || h->dynindx == -1)
13518 if (h->type == STT_GNU_IFUNC)
13519 plt = htab->elf.iplt;
13521 plt = htab->pltlocal;
13523 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13524 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13526 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13528 info->callbacks->einfo
13529 (_("%P: linkage table error against `%pT'\n"),
13530 h->root.root.string);
13531 bfd_set_error (bfd_error_bad_value);
13532 htab->stub_error = TRUE;
13535 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13536 if (htab->params->emit_stub_syms)
13538 size_t len = strlen (h->root.root.string);
13539 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13544 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13545 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13548 if (h->root.type == bfd_link_hash_new)
13550 h->root.type = bfd_link_hash_defined;
13551 h->root.u.def.section = s;
13552 h->root.u.def.value = p - s->contents;
13553 h->ref_regular = 1;
13554 h->def_regular = 1;
13555 h->ref_regular_nonweak = 1;
13556 h->forced_local = 1;
13558 h->root.linker_def = 1;
13562 if (PPC_HA (off) != 0)
13564 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13567 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13569 bfd_put_32 (s->owner, MTCTR_R12, p);
13571 bfd_put_32 (s->owner, BCTR, p);
13577 /* Write PLT relocs for locals. */
13580 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13582 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13585 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13587 struct got_entry **lgot_ents, **end_lgot_ents;
13588 struct plt_entry **local_plt, **lplt, **end_local_plt;
13589 Elf_Internal_Shdr *symtab_hdr;
13590 bfd_size_type locsymcount;
13591 Elf_Internal_Sym *local_syms = NULL;
13592 struct plt_entry *ent;
13594 if (!is_ppc64_elf (ibfd))
13597 lgot_ents = elf_local_got_ents (ibfd);
13601 symtab_hdr = &elf_symtab_hdr (ibfd);
13602 locsymcount = symtab_hdr->sh_info;
13603 end_lgot_ents = lgot_ents + locsymcount;
13604 local_plt = (struct plt_entry **) end_lgot_ents;
13605 end_local_plt = local_plt + locsymcount;
13606 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13607 for (ent = *lplt; ent != NULL; ent = ent->next)
13608 if (ent->plt.offset != (bfd_vma) -1)
13610 Elf_Internal_Sym *sym;
13612 asection *plt, *relplt;
13616 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13617 lplt - local_plt, ibfd))
13619 if (local_syms != NULL
13620 && symtab_hdr->contents != (unsigned char *) local_syms)
13625 val = sym->st_value + ent->addend;
13626 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13627 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13628 if (sym_sec != NULL && sym_sec->output_section != NULL)
13629 val += sym_sec->output_offset + sym_sec->output_section->vma;
13631 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13633 htab->local_ifunc_resolver = 1;
13634 plt = htab->elf.iplt;
13635 relplt = htab->elf.irelplt;
13639 plt = htab->pltlocal;
13640 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13643 if (relplt == NULL)
13645 loc = plt->contents + ent->plt.offset;
13646 bfd_put_64 (info->output_bfd, val, loc);
13649 bfd_vma toc = elf_gp (ibfd);
13650 bfd_put_64 (info->output_bfd, toc, loc + 8);
13655 Elf_Internal_Rela rela;
13656 rela.r_offset = (ent->plt.offset
13657 + plt->output_offset
13658 + plt->output_section->vma);
13659 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13662 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13664 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13669 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13671 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13673 rela.r_addend = val;
13674 loc = relplt->contents + (relplt->reloc_count++
13675 * sizeof (Elf64_External_Rela));
13676 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13680 if (local_syms != NULL
13681 && symtab_hdr->contents != (unsigned char *) local_syms)
13683 if (!info->keep_memory)
13686 symtab_hdr->contents = (unsigned char *) local_syms;
13692 /* Build all the stubs associated with the current output file.
13693 The stubs are kept in a hash table attached to the main linker
13694 hash table. This function is called via gldelf64ppc_finish. */
13697 ppc64_elf_build_stubs (struct bfd_link_info *info,
13700 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13701 struct map_stub *group;
13702 asection *stub_sec;
13704 int stub_sec_count = 0;
13709 /* Allocate memory to hold the linker stubs. */
13710 for (group = htab->group; group != NULL; group = group->next)
13712 group->eh_size = 0;
13713 group->lr_restore = 0;
13714 if ((stub_sec = group->stub_sec) != NULL
13715 && stub_sec->size != 0)
13717 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13719 if (stub_sec->contents == NULL)
13721 stub_sec->size = 0;
13725 if (htab->glink != NULL && htab->glink->size != 0)
13730 /* Build the .glink plt call stub. */
13731 if (htab->params->emit_stub_syms)
13733 struct elf_link_hash_entry *h;
13734 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13735 TRUE, FALSE, FALSE);
13738 if (h->root.type == bfd_link_hash_new)
13740 h->root.type = bfd_link_hash_defined;
13741 h->root.u.def.section = htab->glink;
13742 h->root.u.def.value = 8;
13743 h->ref_regular = 1;
13744 h->def_regular = 1;
13745 h->ref_regular_nonweak = 1;
13746 h->forced_local = 1;
13748 h->root.linker_def = 1;
13751 plt0 = (htab->elf.splt->output_section->vma
13752 + htab->elf.splt->output_offset
13754 if (info->emitrelocations)
13756 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13759 r->r_offset = (htab->glink->output_offset
13760 + htab->glink->output_section->vma);
13761 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13762 r->r_addend = plt0;
13764 p = htab->glink->contents;
13765 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13766 bfd_put_64 (htab->glink->owner, plt0, p);
13770 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13772 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13774 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13776 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13778 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13780 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13782 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13784 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13786 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13788 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13793 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13795 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13797 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13799 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13801 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13803 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13805 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13807 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13809 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13811 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13813 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13815 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13817 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13820 bfd_put_32 (htab->glink->owner, BCTR, p);
13822 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13824 /* Build the .glink lazy link call stubs. */
13826 while (p < htab->glink->contents + htab->glink->size)
13832 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13837 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13839 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13844 bfd_put_32 (htab->glink->owner,
13845 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13851 /* Build .glink global entry stubs, and PLT relocs for globals. */
13852 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13854 if (!write_plt_relocs_for_local_syms (info))
13857 if (htab->brlt != NULL && htab->brlt->size != 0)
13859 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13861 if (htab->brlt->contents == NULL)
13864 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13866 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13867 htab->relbrlt->size);
13868 if (htab->relbrlt->contents == NULL)
13872 /* Build the stubs as directed by the stub hash table. */
13873 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13875 for (group = htab->group; group != NULL; group = group->next)
13876 if (group->needs_save_res)
13877 group->stub_sec->size += htab->sfpr->size;
13879 if (htab->relbrlt != NULL)
13880 htab->relbrlt->reloc_count = 0;
13882 if (htab->params->plt_stub_align != 0)
13883 for (group = htab->group; group != NULL; group = group->next)
13884 if ((stub_sec = group->stub_sec) != NULL)
13886 int align = abs (htab->params->plt_stub_align);
13887 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13890 for (group = htab->group; group != NULL; group = group->next)
13891 if (group->needs_save_res)
13893 stub_sec = group->stub_sec;
13894 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13895 htab->sfpr->contents, htab->sfpr->size);
13896 if (htab->params->emit_stub_syms)
13900 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13901 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13906 if (htab->glink_eh_frame != NULL
13907 && htab->glink_eh_frame->size != 0)
13912 p = htab->glink_eh_frame->contents;
13913 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13915 for (group = htab->group; group != NULL; group = group->next)
13916 if (group->eh_size != 0)
13918 /* Offset to stub section. */
13919 val = (group->stub_sec->output_section->vma
13920 + group->stub_sec->output_offset);
13921 val -= (htab->glink_eh_frame->output_section->vma
13922 + htab->glink_eh_frame->output_offset
13923 + (p + 8 - htab->glink_eh_frame->contents));
13924 if (val + 0x80000000 > 0xffffffff)
13927 (_("%s offset too large for .eh_frame sdata4 encoding"),
13928 group->stub_sec->name);
13931 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13932 p += (group->eh_size + 17 + 3) & -4;
13934 if (htab->glink != NULL && htab->glink->size != 0)
13936 /* Offset to .glink. */
13937 val = (htab->glink->output_section->vma
13938 + htab->glink->output_offset
13940 val -= (htab->glink_eh_frame->output_section->vma
13941 + htab->glink_eh_frame->output_offset
13942 + (p + 8 - htab->glink_eh_frame->contents));
13943 if (val + 0x80000000 > 0xffffffff)
13946 (_("%s offset too large for .eh_frame sdata4 encoding"),
13947 htab->glink->name);
13950 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13951 p += (24 + align - 1) & -align;
13955 for (group = htab->group; group != NULL; group = group->next)
13956 if ((stub_sec = group->stub_sec) != NULL)
13958 stub_sec_count += 1;
13959 if (stub_sec->rawsize != stub_sec->size
13960 && (htab->stub_iteration <= STUB_SHRINK_ITER
13961 || stub_sec->rawsize < stub_sec->size))
13967 htab->stub_error = TRUE;
13968 _bfd_error_handler (_("stubs don't match calculated size"));
13971 if (htab->stub_error)
13977 *stats = bfd_malloc (500);
13978 if (*stats == NULL)
13981 len = sprintf (*stats,
13982 ngettext ("linker stubs in %u group\n",
13983 "linker stubs in %u groups\n",
13986 sprintf (*stats + len, _(" branch %lu\n"
13987 " branch toc adj %lu\n"
13988 " branch notoc %lu\n"
13989 " branch both %lu\n"
13990 " long branch %lu\n"
13991 " long toc adj %lu\n"
13992 " long notoc %lu\n"
13995 " plt call save %lu\n"
13996 " plt call notoc %lu\n"
13997 " plt call both %lu\n"
13998 " global entry %lu"),
13999 htab->stub_count[ppc_stub_long_branch - 1],
14000 htab->stub_count[ppc_stub_long_branch_r2off - 1],
14001 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14002 htab->stub_count[ppc_stub_long_branch_both - 1],
14003 htab->stub_count[ppc_stub_plt_branch - 1],
14004 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
14005 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14006 htab->stub_count[ppc_stub_plt_branch_both - 1],
14007 htab->stub_count[ppc_stub_plt_call - 1],
14008 htab->stub_count[ppc_stub_plt_call_r2save - 1],
14009 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14010 htab->stub_count[ppc_stub_plt_call_both - 1],
14011 htab->stub_count[ppc_stub_global_entry - 1]);
14016 /* What to do when ld finds relocations against symbols defined in
14017 discarded sections. */
14019 static unsigned int
14020 ppc64_elf_action_discarded (asection *sec)
14022 if (strcmp (".opd", sec->name) == 0)
14025 if (strcmp (".toc", sec->name) == 0)
14028 if (strcmp (".toc1", sec->name) == 0)
14031 return _bfd_elf_default_action_discarded (sec);
14034 /* The RELOCATE_SECTION function is called by the ELF backend linker
14035 to handle the relocations for a section.
14037 The relocs are always passed as Rela structures; if the section
14038 actually uses Rel structures, the r_addend field will always be
14041 This function is responsible for adjust the section contents as
14042 necessary, and (if using Rela relocs and generating a
14043 relocatable output file) adjusting the reloc addend as
14046 This function does not have to worry about setting the reloc
14047 address or the reloc symbol index.
14049 LOCAL_SYMS is a pointer to the swapped in local symbols.
14051 LOCAL_SECTIONS is an array giving the section in the input file
14052 corresponding to the st_shndx field of each local symbol.
14054 The global hash table entry for the global symbols can be found
14055 via elf_sym_hashes (input_bfd).
14057 When generating relocatable output, this function must handle
14058 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14059 going to be the section symbol corresponding to the output
14060 section, which means that the addend must be adjusted
14064 ppc64_elf_relocate_section (bfd *output_bfd,
14065 struct bfd_link_info *info,
14067 asection *input_section,
14068 bfd_byte *contents,
14069 Elf_Internal_Rela *relocs,
14070 Elf_Internal_Sym *local_syms,
14071 asection **local_sections)
14073 struct ppc_link_hash_table *htab;
14074 Elf_Internal_Shdr *symtab_hdr;
14075 struct elf_link_hash_entry **sym_hashes;
14076 Elf_Internal_Rela *rel;
14077 Elf_Internal_Rela *wrel;
14078 Elf_Internal_Rela *relend;
14079 Elf_Internal_Rela outrel;
14081 struct got_entry **local_got_ents;
14083 bfd_boolean ret = TRUE;
14084 bfd_boolean is_opd;
14085 /* Assume 'at' branch hints. */
14086 bfd_boolean is_isa_v2 = TRUE;
14087 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
14089 /* Initialize howto table if needed. */
14090 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
14093 htab = ppc_hash_table (info);
14097 /* Don't relocate stub sections. */
14098 if (input_section->owner == htab->params->stub_bfd)
14101 if (!is_ppc64_elf (input_bfd))
14103 bfd_set_error (bfd_error_wrong_format);
14107 local_got_ents = elf_local_got_ents (input_bfd);
14108 TOCstart = elf_gp (output_bfd);
14109 symtab_hdr = &elf_symtab_hdr (input_bfd);
14110 sym_hashes = elf_sym_hashes (input_bfd);
14111 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
14113 rel = wrel = relocs;
14114 relend = relocs + input_section->reloc_count;
14115 for (; rel < relend; wrel++, rel++)
14117 enum elf_ppc64_reloc_type r_type;
14119 bfd_reloc_status_type r;
14120 Elf_Internal_Sym *sym;
14122 struct elf_link_hash_entry *h_elf;
14123 struct ppc_link_hash_entry *h;
14124 struct ppc_link_hash_entry *fdh;
14125 const char *sym_name;
14126 unsigned long r_symndx, toc_symndx;
14127 bfd_vma toc_addend;
14128 unsigned char tls_mask, tls_gd, tls_type;
14129 unsigned char sym_type;
14130 bfd_vma relocation;
14131 bfd_boolean unresolved_reloc, save_unresolved_reloc;
14132 bfd_boolean warned;
14133 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
14136 struct ppc_stub_hash_entry *stub_entry;
14137 bfd_vma max_br_offset;
14139 Elf_Internal_Rela orig_rel;
14140 reloc_howto_type *howto;
14141 struct reloc_howto_struct alt_howto;
14148 r_type = ELF64_R_TYPE (rel->r_info);
14149 r_symndx = ELF64_R_SYM (rel->r_info);
14151 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14152 symbol of the previous ADDR64 reloc. The symbol gives us the
14153 proper TOC base to use. */
14154 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
14156 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
14158 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
14164 unresolved_reloc = FALSE;
14167 if (r_symndx < symtab_hdr->sh_info)
14169 /* It's a local symbol. */
14170 struct _opd_sec_data *opd;
14172 sym = local_syms + r_symndx;
14173 sec = local_sections[r_symndx];
14174 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
14175 sym_type = ELF64_ST_TYPE (sym->st_info);
14176 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
14177 opd = get_opd_info (sec);
14178 if (opd != NULL && opd->adjust != NULL)
14180 long adjust = opd->adjust[OPD_NDX (sym->st_value
14186 /* If this is a relocation against the opd section sym
14187 and we have edited .opd, adjust the reloc addend so
14188 that ld -r and ld --emit-relocs output is correct.
14189 If it is a reloc against some other .opd symbol,
14190 then the symbol value will be adjusted later. */
14191 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14192 rel->r_addend += adjust;
14194 relocation += adjust;
14200 bfd_boolean ignored;
14202 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14203 r_symndx, symtab_hdr, sym_hashes,
14204 h_elf, sec, relocation,
14205 unresolved_reloc, warned, ignored);
14206 sym_name = h_elf->root.root.string;
14207 sym_type = h_elf->type;
14209 && sec->owner == output_bfd
14210 && strcmp (sec->name, ".opd") == 0)
14212 /* This is a symbol defined in a linker script. All
14213 such are defined in output sections, even those
14214 defined by simple assignment from a symbol defined in
14215 an input section. Transfer the symbol to an
14216 appropriate input .opd section, so that a branch to
14217 this symbol will be mapped to the location specified
14218 by the opd entry. */
14219 struct bfd_link_order *lo;
14220 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14221 if (lo->type == bfd_indirect_link_order)
14223 asection *isec = lo->u.indirect.section;
14224 if (h_elf->root.u.def.value >= isec->output_offset
14225 && h_elf->root.u.def.value < (isec->output_offset
14228 h_elf->root.u.def.value -= isec->output_offset;
14229 h_elf->root.u.def.section = isec;
14236 h = (struct ppc_link_hash_entry *) h_elf;
14238 if (sec != NULL && discarded_section (sec))
14240 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14241 input_bfd, input_section,
14242 contents, rel->r_offset);
14243 wrel->r_offset = rel->r_offset;
14245 wrel->r_addend = 0;
14247 /* For ld -r, remove relocations in debug sections against
14248 symbols defined in discarded sections. Not done for
14249 non-debug to preserve relocs in .eh_frame which the
14250 eh_frame editing code expects to be present. */
14251 if (bfd_link_relocatable (info)
14252 && (input_section->flags & SEC_DEBUGGING))
14258 if (bfd_link_relocatable (info))
14261 if (h != NULL && &h->elf == htab->elf.hgot)
14263 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14264 sec = bfd_abs_section_ptr;
14265 unresolved_reloc = FALSE;
14268 /* TLS optimizations. Replace instruction sequences and relocs
14269 based on information we collected in tls_optimize. We edit
14270 RELOCS so that --emit-relocs will output something sensible
14271 for the final instruction stream. */
14276 tls_mask = h->tls_mask;
14277 else if (local_got_ents != NULL)
14279 struct plt_entry **local_plt = (struct plt_entry **)
14280 (local_got_ents + symtab_hdr->sh_info);
14281 unsigned char *lgot_masks = (unsigned char *)
14282 (local_plt + symtab_hdr->sh_info);
14283 tls_mask = lgot_masks[r_symndx];
14285 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
14286 && (r_type == R_PPC64_TLS
14287 || r_type == R_PPC64_TLSGD
14288 || r_type == R_PPC64_TLSLD))
14290 /* Check for toc tls entries. */
14291 unsigned char *toc_tls;
14293 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14294 &local_syms, rel, input_bfd))
14298 tls_mask = *toc_tls;
14301 /* Check that tls relocs are used with tls syms, and non-tls
14302 relocs are used with non-tls syms. */
14303 if (r_symndx != STN_UNDEF
14304 && r_type != R_PPC64_NONE
14306 || h->elf.root.type == bfd_link_hash_defined
14307 || h->elf.root.type == bfd_link_hash_defweak)
14308 && (IS_PPC64_TLS_RELOC (r_type)
14309 != (sym_type == STT_TLS
14310 || (sym_type == STT_SECTION
14311 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
14313 if ((tls_mask & TLS_TLS) != 0
14314 && (r_type == R_PPC64_TLS
14315 || r_type == R_PPC64_TLSGD
14316 || r_type == R_PPC64_TLSLD))
14317 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14320 info->callbacks->einfo
14321 (!IS_PPC64_TLS_RELOC (r_type)
14322 /* xgettext:c-format */
14323 ? _("%H: %s used with TLS symbol `%pT'\n")
14324 /* xgettext:c-format */
14325 : _("%H: %s used with non-TLS symbol `%pT'\n"),
14326 input_bfd, input_section, rel->r_offset,
14327 ppc64_elf_howto_table[r_type]->name,
14331 /* Ensure reloc mapping code below stays sane. */
14332 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14333 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14334 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14335 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14336 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14337 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14338 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14339 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14340 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14341 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14349 case R_PPC64_LO_DS_OPT:
14350 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
14351 if ((insn & (0x3f << 26)) != 58u << 26)
14353 insn += (14u << 26) - (58u << 26);
14354 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
14355 r_type = R_PPC64_TOC16_LO;
14356 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14359 case R_PPC64_TOC16:
14360 case R_PPC64_TOC16_LO:
14361 case R_PPC64_TOC16_DS:
14362 case R_PPC64_TOC16_LO_DS:
14364 /* Check for toc tls entries. */
14365 unsigned char *toc_tls;
14368 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14369 &local_syms, rel, input_bfd);
14375 tls_mask = *toc_tls;
14376 if (r_type == R_PPC64_TOC16_DS
14377 || r_type == R_PPC64_TOC16_LO_DS)
14379 if ((tls_mask & TLS_TLS) != 0
14380 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14385 /* If we found a GD reloc pair, then we might be
14386 doing a GD->IE transition. */
14389 tls_gd = TLS_TPRELGD;
14390 if ((tls_mask & TLS_TLS) != 0
14391 && (tls_mask & TLS_GD) == 0)
14394 else if (retval == 3)
14396 if ((tls_mask & TLS_TLS) != 0
14397 && (tls_mask & TLS_LD) == 0)
14405 case R_PPC64_GOT_TPREL16_HI:
14406 case R_PPC64_GOT_TPREL16_HA:
14407 if ((tls_mask & TLS_TLS) != 0
14408 && (tls_mask & TLS_TPREL) == 0)
14410 rel->r_offset -= d_offset;
14411 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14412 r_type = R_PPC64_NONE;
14413 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14417 case R_PPC64_GOT_TPREL16_DS:
14418 case R_PPC64_GOT_TPREL16_LO_DS:
14419 if ((tls_mask & TLS_TLS) != 0
14420 && (tls_mask & TLS_TPREL) == 0)
14423 insn = bfd_get_32 (input_bfd,
14424 contents + rel->r_offset - d_offset);
14426 insn |= 0x3c0d0000; /* addis 0,13,0 */
14427 bfd_put_32 (input_bfd, insn,
14428 contents + rel->r_offset - d_offset);
14429 r_type = R_PPC64_TPREL16_HA;
14430 if (toc_symndx != 0)
14432 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14433 rel->r_addend = toc_addend;
14434 /* We changed the symbol. Start over in order to
14435 get h, sym, sec etc. right. */
14439 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14444 if ((tls_mask & TLS_TLS) != 0
14445 && (tls_mask & TLS_TPREL) == 0)
14447 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14448 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14451 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14452 /* Was PPC64_TLS which sits on insn boundary, now
14453 PPC64_TPREL16_LO which is at low-order half-word. */
14454 rel->r_offset += d_offset;
14455 r_type = R_PPC64_TPREL16_LO;
14456 if (toc_symndx != 0)
14458 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14459 rel->r_addend = toc_addend;
14460 /* We changed the symbol. Start over in order to
14461 get h, sym, sec etc. right. */
14465 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14469 case R_PPC64_GOT_TLSGD16_HI:
14470 case R_PPC64_GOT_TLSGD16_HA:
14471 tls_gd = TLS_TPRELGD;
14472 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14476 case R_PPC64_GOT_TLSLD16_HI:
14477 case R_PPC64_GOT_TLSLD16_HA:
14478 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14481 if ((tls_mask & tls_gd) != 0)
14482 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14483 + R_PPC64_GOT_TPREL16_DS);
14486 rel->r_offset -= d_offset;
14487 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14488 r_type = R_PPC64_NONE;
14490 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14494 case R_PPC64_GOT_TLSGD16:
14495 case R_PPC64_GOT_TLSGD16_LO:
14496 tls_gd = TLS_TPRELGD;
14497 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14501 case R_PPC64_GOT_TLSLD16:
14502 case R_PPC64_GOT_TLSLD16_LO:
14503 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14505 unsigned int insn1, insn2;
14508 offset = (bfd_vma) -1;
14509 /* If not using the newer R_PPC64_TLSGD/LD to mark
14510 __tls_get_addr calls, we must trust that the call
14511 stays with its arg setup insns, ie. that the next
14512 reloc is the __tls_get_addr call associated with
14513 the current reloc. Edit both insns. */
14514 if (input_section->has_tls_get_addr_call
14515 && rel + 1 < relend
14516 && branch_reloc_hash_match (input_bfd, rel + 1,
14517 htab->tls_get_addr,
14518 htab->tls_get_addr_fd))
14519 offset = rel[1].r_offset;
14520 /* We read the low GOT_TLS (or TOC16) insn because we
14521 need to keep the destination reg. It may be
14522 something other than the usual r3, and moved to r3
14523 before the call by intervening code. */
14524 insn1 = bfd_get_32 (input_bfd,
14525 contents + rel->r_offset - d_offset);
14526 if ((tls_mask & tls_gd) != 0)
14529 insn1 &= (0x1f << 21) | (0x1f << 16);
14530 insn1 |= 58 << 26; /* ld */
14531 insn2 = 0x7c636a14; /* add 3,3,13 */
14532 if (offset != (bfd_vma) -1)
14533 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14534 if ((tls_mask & TLS_EXPLICIT) == 0)
14535 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14536 + R_PPC64_GOT_TPREL16_DS);
14538 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14539 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14544 insn1 &= 0x1f << 21;
14545 insn1 |= 0x3c0d0000; /* addis r,13,0 */
14546 insn2 = 0x38630000; /* addi 3,3,0 */
14549 /* Was an LD reloc. */
14551 sec = local_sections[toc_symndx];
14553 r_symndx < symtab_hdr->sh_info;
14555 if (local_sections[r_symndx] == sec)
14557 if (r_symndx >= symtab_hdr->sh_info)
14558 r_symndx = STN_UNDEF;
14559 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14560 if (r_symndx != STN_UNDEF)
14561 rel->r_addend -= (local_syms[r_symndx].st_value
14562 + sec->output_offset
14563 + sec->output_section->vma);
14565 else if (toc_symndx != 0)
14567 r_symndx = toc_symndx;
14568 rel->r_addend = toc_addend;
14570 r_type = R_PPC64_TPREL16_HA;
14571 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14572 if (offset != (bfd_vma) -1)
14574 rel[1].r_info = ELF64_R_INFO (r_symndx,
14575 R_PPC64_TPREL16_LO);
14576 rel[1].r_offset = offset + d_offset;
14577 rel[1].r_addend = rel->r_addend;
14580 bfd_put_32 (input_bfd, insn1,
14581 contents + rel->r_offset - d_offset);
14582 if (offset != (bfd_vma) -1)
14584 bfd_put_32 (input_bfd, insn2, contents + offset);
14585 if (offset + 8 <= input_section->size)
14587 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14588 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14589 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14592 if ((tls_mask & tls_gd) == 0
14593 && (tls_gd == 0 || toc_symndx != 0))
14595 /* We changed the symbol. Start over in order
14596 to get h, sym, sec etc. right. */
14602 case R_PPC64_TLSGD:
14603 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
14604 && rel + 1 < relend)
14606 unsigned int insn2;
14607 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14609 offset = rel->r_offset;
14610 if (is_plt_seq_reloc (r_type1))
14612 bfd_put_32 (output_bfd, NOP, contents + offset);
14613 if (r_type1 == R_PPC64_PLT_PCREL34
14614 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14615 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14616 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14620 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14621 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14623 if ((tls_mask & TLS_TPRELGD) != 0)
14626 r_type = R_PPC64_NONE;
14627 insn2 = 0x7c636a14; /* add 3,3,13 */
14632 if (toc_symndx != 0)
14634 r_symndx = toc_symndx;
14635 rel->r_addend = toc_addend;
14637 r_type = R_PPC64_TPREL16_LO;
14638 rel->r_offset = offset + d_offset;
14639 insn2 = 0x38630000; /* addi 3,3,0 */
14641 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14642 /* Zap the reloc on the _tls_get_addr call too. */
14643 BFD_ASSERT (offset == rel[1].r_offset);
14644 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14645 bfd_put_32 (input_bfd, insn2, contents + offset);
14646 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
14651 case R_PPC64_TLSLD:
14652 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
14653 && rel + 1 < relend)
14655 unsigned int insn2;
14656 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14658 offset = rel->r_offset;
14659 if (is_plt_seq_reloc (r_type1))
14661 bfd_put_32 (output_bfd, NOP, contents + offset);
14662 if (r_type1 == R_PPC64_PLT_PCREL34
14663 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14664 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14665 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14669 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14670 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14673 sec = local_sections[toc_symndx];
14675 r_symndx < symtab_hdr->sh_info;
14677 if (local_sections[r_symndx] == sec)
14679 if (r_symndx >= symtab_hdr->sh_info)
14680 r_symndx = STN_UNDEF;
14681 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14682 if (r_symndx != STN_UNDEF)
14683 rel->r_addend -= (local_syms[r_symndx].st_value
14684 + sec->output_offset
14685 + sec->output_section->vma);
14687 r_type = R_PPC64_TPREL16_LO;
14688 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14689 rel->r_offset = offset + d_offset;
14690 /* Zap the reloc on the _tls_get_addr call too. */
14691 BFD_ASSERT (offset == rel[1].r_offset);
14692 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14693 insn2 = 0x38630000; /* addi 3,3,0 */
14694 bfd_put_32 (input_bfd, insn2, contents + offset);
14699 case R_PPC64_DTPMOD64:
14700 if (rel + 1 < relend
14701 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14702 && rel[1].r_offset == rel->r_offset + 8)
14704 if ((tls_mask & TLS_GD) == 0)
14706 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14707 if ((tls_mask & TLS_TPRELGD) != 0)
14708 r_type = R_PPC64_TPREL64;
14711 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14712 r_type = R_PPC64_NONE;
14714 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14719 if ((tls_mask & TLS_LD) == 0)
14721 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14722 r_type = R_PPC64_NONE;
14723 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14728 case R_PPC64_TPREL64:
14729 if ((tls_mask & TLS_TPREL) == 0)
14731 r_type = R_PPC64_NONE;
14732 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14736 case R_PPC64_ENTRY:
14737 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14738 if (!bfd_link_pic (info)
14739 && !info->traditional_format
14740 && relocation + 0x80008000 <= 0xffffffff)
14742 unsigned int insn1, insn2;
14744 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14745 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14746 if ((insn1 & ~0xfffc) == LD_R2_0R12
14747 && insn2 == ADD_R2_R2_R12)
14749 bfd_put_32 (input_bfd,
14750 LIS_R2 + PPC_HA (relocation),
14751 contents + rel->r_offset);
14752 bfd_put_32 (input_bfd,
14753 ADDI_R2_R2 + PPC_LO (relocation),
14754 contents + rel->r_offset + 4);
14759 relocation -= (rel->r_offset
14760 + input_section->output_offset
14761 + input_section->output_section->vma);
14762 if (relocation + 0x80008000 <= 0xffffffff)
14764 unsigned int insn1, insn2;
14766 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14767 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14768 if ((insn1 & ~0xfffc) == LD_R2_0R12
14769 && insn2 == ADD_R2_R2_R12)
14771 bfd_put_32 (input_bfd,
14772 ADDIS_R2_R12 + PPC_HA (relocation),
14773 contents + rel->r_offset);
14774 bfd_put_32 (input_bfd,
14775 ADDI_R2_R2 + PPC_LO (relocation),
14776 contents + rel->r_offset + 4);
14782 case R_PPC64_REL16_HA:
14783 /* If we are generating a non-PIC executable, edit
14784 . 0: addis 2,12,.TOC.-0b@ha
14785 . addi 2,2,.TOC.-0b@l
14786 used by ELFv2 global entry points to set up r2, to
14789 if .TOC. is in range. */
14790 if (!bfd_link_pic (info)
14791 && !info->traditional_format
14793 && rel->r_addend == d_offset
14794 && h != NULL && &h->elf == htab->elf.hgot
14795 && rel + 1 < relend
14796 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14797 && rel[1].r_offset == rel->r_offset + 4
14798 && rel[1].r_addend == rel->r_addend + 4
14799 && relocation + 0x80008000 <= 0xffffffff)
14801 unsigned int insn1, insn2;
14802 offset = rel->r_offset - d_offset;
14803 insn1 = bfd_get_32 (input_bfd, contents + offset);
14804 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14805 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14806 && (insn2 & 0xffff0000) == ADDI_R2_R2)
14808 r_type = R_PPC64_ADDR16_HA;
14809 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14810 rel->r_addend -= d_offset;
14811 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14812 rel[1].r_addend -= d_offset + 4;
14813 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14819 /* Handle other relocations that tweak non-addend part of insn. */
14821 max_br_offset = 1 << 25;
14822 addend = rel->r_addend;
14823 reloc_dest = DEST_NORMAL;
14829 case R_PPC64_TOCSAVE:
14830 if (relocation + addend == (rel->r_offset
14831 + input_section->output_offset
14832 + input_section->output_section->vma)
14833 && tocsave_find (htab, NO_INSERT,
14834 &local_syms, rel, input_bfd))
14836 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14838 || insn == CROR_151515 || insn == CROR_313131)
14839 bfd_put_32 (input_bfd,
14840 STD_R2_0R1 + STK_TOC (htab),
14841 contents + rel->r_offset);
14845 /* Branch taken prediction relocations. */
14846 case R_PPC64_ADDR14_BRTAKEN:
14847 case R_PPC64_REL14_BRTAKEN:
14848 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
14849 /* Fall through. */
14851 /* Branch not taken prediction relocations. */
14852 case R_PPC64_ADDR14_BRNTAKEN:
14853 case R_PPC64_REL14_BRNTAKEN:
14854 insn |= bfd_get_32 (input_bfd,
14855 contents + rel->r_offset) & ~(0x01 << 21);
14856 /* Fall through. */
14858 case R_PPC64_REL14:
14859 max_br_offset = 1 << 15;
14860 /* Fall through. */
14862 case R_PPC64_REL24:
14863 case R_PPC64_REL24_NOTOC:
14864 case R_PPC64_PLTCALL:
14865 case R_PPC64_PLTCALL_NOTOC:
14866 /* Calls to functions with a different TOC, such as calls to
14867 shared objects, need to alter the TOC pointer. This is
14868 done using a linkage stub. A REL24 branching to these
14869 linkage stubs needs to be followed by a nop, as the nop
14870 will be replaced with an instruction to restore the TOC
14875 && h->oh->is_func_descriptor)
14876 fdh = ppc_follow_link (h->oh);
14877 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14879 if ((r_type == R_PPC64_PLTCALL
14880 || r_type == R_PPC64_PLTCALL_NOTOC)
14881 && stub_entry != NULL
14882 && stub_entry->stub_type >= ppc_stub_plt_call
14883 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14886 if (stub_entry != NULL
14887 && ((stub_entry->stub_type >= ppc_stub_plt_call
14888 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14889 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14890 || stub_entry->stub_type == ppc_stub_plt_branch_both
14891 || stub_entry->stub_type == ppc_stub_long_branch_r2off
14892 || stub_entry->stub_type == ppc_stub_long_branch_both))
14894 bfd_boolean can_plt_call = FALSE;
14896 if (stub_entry->stub_type == ppc_stub_plt_call
14898 && htab->params->plt_localentry0 != 0
14899 && is_elfv2_localentry0 (&h->elf))
14901 /* The function doesn't use or change r2. */
14902 can_plt_call = TRUE;
14904 else if (r_type == R_PPC64_REL24_NOTOC)
14906 /* NOTOC calls don't need to restore r2. */
14907 can_plt_call = TRUE;
14910 /* All of these stubs may modify r2, so there must be a
14911 branch and link followed by a nop. The nop is
14912 replaced by an insn to restore r2. */
14913 else if (rel->r_offset + 8 <= input_section->size)
14917 br = bfd_get_32 (input_bfd,
14918 contents + rel->r_offset);
14923 nop = bfd_get_32 (input_bfd,
14924 contents + rel->r_offset + 4);
14925 if (nop == LD_R2_0R1 + STK_TOC (htab))
14926 can_plt_call = TRUE;
14927 else if (nop == NOP
14928 || nop == CROR_151515
14929 || nop == CROR_313131)
14932 && (h == htab->tls_get_addr_fd
14933 || h == htab->tls_get_addr)
14934 && htab->params->tls_get_addr_opt)
14936 /* Special stub used, leave nop alone. */
14939 bfd_put_32 (input_bfd,
14940 LD_R2_0R1 + STK_TOC (htab),
14941 contents + rel->r_offset + 4);
14942 can_plt_call = TRUE;
14947 if (!can_plt_call && h != NULL)
14949 const char *name = h->elf.root.root.string;
14954 if (strncmp (name, "__libc_start_main", 17) == 0
14955 && (name[17] == 0 || name[17] == '@'))
14957 /* Allow crt1 branch to go via a toc adjusting
14958 stub. Other calls that never return could do
14959 the same, if we could detect such. */
14960 can_plt_call = TRUE;
14966 /* g++ as of 20130507 emits self-calls without a
14967 following nop. This is arguably wrong since we
14968 have conflicting information. On the one hand a
14969 global symbol and on the other a local call
14970 sequence, but don't error for this special case.
14971 It isn't possible to cheaply verify we have
14972 exactly such a call. Allow all calls to the same
14974 asection *code_sec = sec;
14976 if (get_opd_info (sec) != NULL)
14978 bfd_vma off = (relocation + addend
14979 - sec->output_section->vma
14980 - sec->output_offset);
14982 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14984 if (code_sec == input_section)
14985 can_plt_call = TRUE;
14990 if (stub_entry->stub_type >= ppc_stub_plt_call
14991 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14992 info->callbacks->einfo
14993 /* xgettext:c-format */
14994 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14995 "(plt call stub)\n"),
14996 input_bfd, input_section, rel->r_offset, sym_name);
14998 info->callbacks->einfo
14999 /* xgettext:c-format */
15000 (_("%H: call to `%pT' lacks nop, can't restore toc; "
15001 "(toc save/adjust stub)\n"),
15002 input_bfd, input_section, rel->r_offset, sym_name);
15004 bfd_set_error (bfd_error_bad_value);
15009 && stub_entry->stub_type >= ppc_stub_plt_call
15010 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15011 unresolved_reloc = FALSE;
15014 if ((stub_entry == NULL
15015 || stub_entry->stub_type == ppc_stub_long_branch
15016 || stub_entry->stub_type == ppc_stub_plt_branch)
15017 && get_opd_info (sec) != NULL)
15019 /* The branch destination is the value of the opd entry. */
15020 bfd_vma off = (relocation + addend
15021 - sec->output_section->vma
15022 - sec->output_offset);
15023 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
15024 if (dest != (bfd_vma) -1)
15028 reloc_dest = DEST_OPD;
15032 /* If the branch is out of reach we ought to have a long
15034 from = (rel->r_offset
15035 + input_section->output_offset
15036 + input_section->output_section->vma);
15038 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15042 if (stub_entry != NULL
15043 && (stub_entry->stub_type == ppc_stub_long_branch
15044 || stub_entry->stub_type == ppc_stub_plt_branch)
15045 && (r_type == R_PPC64_ADDR14_BRTAKEN
15046 || r_type == R_PPC64_ADDR14_BRNTAKEN
15047 || (relocation + addend - from + max_br_offset
15048 < 2 * max_br_offset)))
15049 /* Don't use the stub if this branch is in range. */
15052 if (stub_entry != NULL
15053 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15054 || stub_entry->stub_type == ppc_stub_long_branch_both
15055 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15056 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15057 && (r_type != R_PPC64_REL24_NOTOC
15058 || ((fdh ? fdh->elf.other : sym->st_other)
15059 & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
15060 && (relocation + addend - from + max_br_offset
15061 < 2 * max_br_offset))
15064 if (stub_entry != NULL
15065 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15066 || stub_entry->stub_type == ppc_stub_long_branch_both
15067 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15068 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15069 && r_type == R_PPC64_REL24_NOTOC
15070 && (relocation + addend - from + max_br_offset
15071 < 2 * max_br_offset))
15074 if (stub_entry != NULL)
15076 /* Munge up the value and addend so that we call the stub
15077 rather than the procedure directly. */
15078 asection *stub_sec = stub_entry->group->stub_sec;
15080 if (stub_entry->stub_type == ppc_stub_save_res)
15081 relocation += (stub_sec->output_offset
15082 + stub_sec->output_section->vma
15083 + stub_sec->size - htab->sfpr->size
15084 - htab->sfpr->output_offset
15085 - htab->sfpr->output_section->vma);
15087 relocation = (stub_entry->stub_offset
15088 + stub_sec->output_offset
15089 + stub_sec->output_section->vma);
15091 reloc_dest = DEST_STUB;
15093 if (((stub_entry->stub_type == ppc_stub_plt_call
15094 && ALWAYS_EMIT_R2SAVE)
15095 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15096 || stub_entry->stub_type == ppc_stub_plt_call_both)
15098 && (h == htab->tls_get_addr_fd
15099 || h == htab->tls_get_addr)
15100 && htab->params->tls_get_addr_opt)
15101 && rel + 1 < relend
15102 && rel[1].r_offset == rel->r_offset + 4
15103 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
15105 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
15106 || stub_entry->stub_type == ppc_stub_plt_branch_both
15107 || stub_entry->stub_type == ppc_stub_plt_call_both)
15108 && r_type == R_PPC64_REL24_NOTOC)
15111 if (r_type == R_PPC64_REL24_NOTOC
15112 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
15113 || stub_entry->stub_type == ppc_stub_plt_call_both))
15114 htab->notoc_plt = 1;
15121 /* Set 'a' bit. This is 0b00010 in BO field for branch
15122 on CR(BI) insns (BO == 001at or 011at), and 0b01000
15123 for branch on CTR insns (BO == 1a00t or 1a01t). */
15124 if ((insn & (0x14 << 21)) == (0x04 << 21))
15125 insn |= 0x02 << 21;
15126 else if ((insn & (0x14 << 21)) == (0x10 << 21))
15127 insn |= 0x08 << 21;
15133 /* Invert 'y' bit if not the default. */
15134 if ((bfd_signed_vma) (relocation + addend - from) < 0)
15135 insn ^= 0x01 << 21;
15138 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15141 /* NOP out calls to undefined weak functions.
15142 We can thus call a weak function without first
15143 checking whether the function is defined. */
15145 && h->elf.root.type == bfd_link_hash_undefweak
15146 && h->elf.dynindx == -1
15147 && (r_type == R_PPC64_REL24
15148 || r_type == R_PPC64_REL24_NOTOC)
15152 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15157 case R_PPC64_GOT16_DS:
15158 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15159 if (relocation + addend - from + 0x8000 < 0x10000
15160 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15162 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15163 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15165 insn += (14u << 26) - (58u << 26);
15166 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15167 r_type = R_PPC64_TOC16;
15168 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15173 case R_PPC64_GOT16_LO_DS:
15174 case R_PPC64_GOT16_HA:
15175 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15176 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
15177 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15179 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15180 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15182 insn += (14u << 26) - (58u << 26);
15183 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15184 r_type = R_PPC64_TOC16_LO;
15185 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15187 else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
15189 r_type = R_PPC64_TOC16_HA;
15190 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15195 case R_PPC64_GOT_PCREL34:
15196 from = (rel->r_offset
15197 + input_section->output_section->vma
15198 + input_section->output_offset);
15199 if (relocation - from + (1ULL << 33) < 1ULL << 34
15200 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15202 offset = rel->r_offset;
15203 pinsn = bfd_get_32 (input_bfd, contents + offset);
15205 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15206 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15207 == ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
15209 /* Replace with paddi. */
15210 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
15211 r_type = R_PPC64_PCREL34;
15212 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15213 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
15214 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
15220 case R_PPC64_PCREL34:
15221 if (SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15223 offset = rel->r_offset;
15224 pinsn = bfd_get_32 (input_bfd, contents + offset);
15226 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15227 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15228 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
15229 | (14ULL << 26) /* paddi */))
15232 if (rel + 1 < relend
15233 && rel[1].r_offset == offset
15234 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT))
15236 bfd_vma off2 = rel[1].r_addend;
15238 /* zero means next insn. */
15241 if (off2 + 4 <= input_section->size)
15244 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
15246 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15248 if (xlate_pcrel_opt (&pinsn, &pinsn2))
15250 bfd_put_32 (input_bfd, pinsn >> 32,
15251 contents + offset);
15252 bfd_put_32 (input_bfd, pinsn,
15253 contents + offset + 4);
15254 bfd_put_32 (input_bfd, pinsn2 >> 32,
15264 /* Set `addend'. */
15266 save_unresolved_reloc = unresolved_reloc;
15270 /* xgettext:c-format */
15271 _bfd_error_handler (_("%pB: %s unsupported"),
15272 input_bfd, ppc64_elf_howto_table[r_type]->name);
15274 bfd_set_error (bfd_error_bad_value);
15280 case R_PPC64_TLSGD:
15281 case R_PPC64_TLSLD:
15282 case R_PPC64_TOCSAVE:
15283 case R_PPC64_GNU_VTINHERIT:
15284 case R_PPC64_GNU_VTENTRY:
15285 case R_PPC64_ENTRY:
15286 case R_PPC64_PCREL_OPT:
15289 /* GOT16 relocations. Like an ADDR16 using the symbol's
15290 address in the GOT as relocation value instead of the
15291 symbol's value itself. Also, create a GOT entry for the
15292 symbol and put the symbol value there. */
15293 case R_PPC64_GOT_TLSGD16:
15294 case R_PPC64_GOT_TLSGD16_LO:
15295 case R_PPC64_GOT_TLSGD16_HI:
15296 case R_PPC64_GOT_TLSGD16_HA:
15297 tls_type = TLS_TLS | TLS_GD;
15300 case R_PPC64_GOT_TLSLD16:
15301 case R_PPC64_GOT_TLSLD16_LO:
15302 case R_PPC64_GOT_TLSLD16_HI:
15303 case R_PPC64_GOT_TLSLD16_HA:
15304 tls_type = TLS_TLS | TLS_LD;
15307 case R_PPC64_GOT_TPREL16_DS:
15308 case R_PPC64_GOT_TPREL16_LO_DS:
15309 case R_PPC64_GOT_TPREL16_HI:
15310 case R_PPC64_GOT_TPREL16_HA:
15311 tls_type = TLS_TLS | TLS_TPREL;
15314 case R_PPC64_GOT_DTPREL16_DS:
15315 case R_PPC64_GOT_DTPREL16_LO_DS:
15316 case R_PPC64_GOT_DTPREL16_HI:
15317 case R_PPC64_GOT_DTPREL16_HA:
15318 tls_type = TLS_TLS | TLS_DTPREL;
15321 case R_PPC64_GOT16:
15322 case R_PPC64_GOT16_LO:
15323 case R_PPC64_GOT16_HI:
15324 case R_PPC64_GOT16_HA:
15325 case R_PPC64_GOT16_DS:
15326 case R_PPC64_GOT16_LO_DS:
15327 case R_PPC64_GOT_PCREL34:
15330 /* Relocation is to the entry for this symbol in the global
15335 unsigned long indx = 0;
15336 struct got_entry *ent;
15337 bfd_vma sym_addend = orig_rel.r_addend;
15339 if (r_type == R_PPC64_GOT_PCREL34)
15342 if (tls_type == (TLS_TLS | TLS_LD)
15344 || !h->elf.def_dynamic))
15345 ent = ppc64_tlsld_got (input_bfd);
15350 if (!htab->elf.dynamic_sections_created
15351 || h->elf.dynindx == -1
15352 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15353 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
15354 /* This is actually a static link, or it is a
15355 -Bsymbolic link and the symbol is defined
15356 locally, or the symbol was forced to be local
15357 because of a version file. */
15361 indx = h->elf.dynindx;
15362 unresolved_reloc = FALSE;
15364 ent = h->elf.got.glist;
15368 if (local_got_ents == NULL)
15370 ent = local_got_ents[r_symndx];
15373 for (; ent != NULL; ent = ent->next)
15374 if (ent->addend == sym_addend
15375 && ent->owner == input_bfd
15376 && ent->tls_type == tls_type)
15382 if (ent->is_indirect)
15383 ent = ent->got.ent;
15384 offp = &ent->got.offset;
15385 got = ppc64_elf_tdata (ent->owner)->got;
15389 /* The offset must always be a multiple of 8. We use the
15390 least significant bit to record whether we have already
15391 processed this entry. */
15393 if ((off & 1) != 0)
15397 /* Generate relocs for the dynamic linker, except in
15398 the case of TLSLD where we'll use one entry per
15406 ? h->elf.type == STT_GNU_IFUNC
15407 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
15410 relgot = htab->elf.irelplt;
15412 htab->local_ifunc_resolver = 1;
15413 else if (is_static_defined (&h->elf))
15414 htab->maybe_local_ifunc_resolver = 1;
15417 || (bfd_link_pic (info)
15419 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
15420 || (tls_type == (TLS_TLS | TLS_LD)
15421 && !h->elf.def_dynamic))
15422 && !(tls_type == (TLS_TLS | TLS_TPREL)
15423 && bfd_link_executable (info)
15424 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
15425 relgot = ppc64_elf_tdata (ent->owner)->relgot;
15426 if (relgot != NULL)
15428 outrel.r_offset = (got->output_section->vma
15429 + got->output_offset
15431 outrel.r_addend = sym_addend;
15432 if (tls_type & (TLS_LD | TLS_GD))
15434 outrel.r_addend = 0;
15435 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
15436 if (tls_type == (TLS_TLS | TLS_GD))
15438 loc = relgot->contents;
15439 loc += (relgot->reloc_count++
15440 * sizeof (Elf64_External_Rela));
15441 bfd_elf64_swap_reloca_out (output_bfd,
15443 outrel.r_offset += 8;
15444 outrel.r_addend = sym_addend;
15446 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15449 else if (tls_type == (TLS_TLS | TLS_DTPREL))
15450 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15451 else if (tls_type == (TLS_TLS | TLS_TPREL))
15452 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
15453 else if (indx != 0)
15454 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15458 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15460 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15462 /* Write the .got section contents for the sake
15464 loc = got->contents + off;
15465 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15469 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
15471 outrel.r_addend += relocation;
15472 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
15474 if (htab->elf.tls_sec == NULL)
15475 outrel.r_addend = 0;
15477 outrel.r_addend -= htab->elf.tls_sec->vma;
15480 loc = relgot->contents;
15481 loc += (relgot->reloc_count++
15482 * sizeof (Elf64_External_Rela));
15483 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15486 /* Init the .got section contents here if we're not
15487 emitting a reloc. */
15490 relocation += sym_addend;
15493 if (htab->elf.tls_sec == NULL)
15497 if (tls_type & TLS_LD)
15500 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
15501 if (tls_type & TLS_TPREL)
15502 relocation += DTP_OFFSET - TP_OFFSET;
15505 if (tls_type & (TLS_GD | TLS_LD))
15507 bfd_put_64 (output_bfd, relocation,
15508 got->contents + off + 8);
15512 bfd_put_64 (output_bfd, relocation,
15513 got->contents + off);
15517 if (off >= (bfd_vma) -2)
15520 relocation = got->output_section->vma + got->output_offset + off;
15521 if (r_type != R_PPC64_GOT_PCREL34)
15522 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
15526 case R_PPC64_PLT16_HA:
15527 case R_PPC64_PLT16_HI:
15528 case R_PPC64_PLT16_LO:
15529 case R_PPC64_PLT16_LO_DS:
15530 case R_PPC64_PLT_PCREL34:
15531 case R_PPC64_PLT_PCREL34_NOTOC:
15532 case R_PPC64_PLT32:
15533 case R_PPC64_PLT64:
15534 case R_PPC64_PLTSEQ:
15535 case R_PPC64_PLTSEQ_NOTOC:
15536 case R_PPC64_PLTCALL:
15537 case R_PPC64_PLTCALL_NOTOC:
15538 /* Relocation is to the entry for this symbol in the
15539 procedure linkage table. */
15540 unresolved_reloc = TRUE;
15542 struct plt_entry **plt_list = NULL;
15544 plt_list = &h->elf.plt.plist;
15545 else if (local_got_ents != NULL)
15547 struct plt_entry **local_plt = (struct plt_entry **)
15548 (local_got_ents + symtab_hdr->sh_info);
15549 plt_list = local_plt + r_symndx;
15553 struct plt_entry *ent;
15554 bfd_vma sym_addend = orig_rel.r_addend;
15556 if (r_type == R_PPC64_PLT_PCREL34
15557 || r_type == R_PPC64_PLT_PCREL34_NOTOC)
15560 for (ent = *plt_list; ent != NULL; ent = ent->next)
15561 if (ent->plt.offset != (bfd_vma) -1
15562 && ent->addend == sym_addend)
15567 plt = htab->elf.splt;
15568 if (!htab->elf.dynamic_sections_created
15570 || h->elf.dynindx == -1)
15573 ? h->elf.type == STT_GNU_IFUNC
15574 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15575 plt = htab->elf.iplt;
15577 plt = htab->pltlocal;
15579 relocation = (plt->output_section->vma
15580 + plt->output_offset
15581 + ent->plt.offset);
15582 if (r_type == R_PPC64_PLT16_HA
15583 || r_type == R_PPC64_PLT16_HI
15584 || r_type == R_PPC64_PLT16_LO
15585 || r_type == R_PPC64_PLT16_LO_DS)
15587 got = (elf_gp (output_bfd)
15588 + htab->sec_info[input_section->id].toc_off);
15591 if (r_type != R_PPC64_PLT_PCREL34
15592 && r_type != R_PPC64_PLT_PCREL34_NOTOC)
15594 unresolved_reloc = FALSE;
15602 /* Relocation value is TOC base. */
15603 relocation = TOCstart;
15604 if (r_symndx == STN_UNDEF)
15605 relocation += htab->sec_info[input_section->id].toc_off;
15606 else if (unresolved_reloc)
15608 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15609 relocation += htab->sec_info[sec->id].toc_off;
15611 unresolved_reloc = TRUE;
15614 /* TOC16 relocs. We want the offset relative to the TOC base,
15615 which is the address of the start of the TOC plus 0x8000.
15616 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15618 case R_PPC64_TOC16:
15619 case R_PPC64_TOC16_LO:
15620 case R_PPC64_TOC16_HI:
15621 case R_PPC64_TOC16_DS:
15622 case R_PPC64_TOC16_LO_DS:
15623 case R_PPC64_TOC16_HA:
15624 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
15627 /* Relocate against the beginning of the section. */
15628 case R_PPC64_SECTOFF:
15629 case R_PPC64_SECTOFF_LO:
15630 case R_PPC64_SECTOFF_HI:
15631 case R_PPC64_SECTOFF_DS:
15632 case R_PPC64_SECTOFF_LO_DS:
15633 case R_PPC64_SECTOFF_HA:
15635 addend -= sec->output_section->vma;
15638 case R_PPC64_REL16:
15639 case R_PPC64_REL16_LO:
15640 case R_PPC64_REL16_HI:
15641 case R_PPC64_REL16_HA:
15642 case R_PPC64_REL16_HIGH:
15643 case R_PPC64_REL16_HIGHA:
15644 case R_PPC64_REL16_HIGHER:
15645 case R_PPC64_REL16_HIGHERA:
15646 case R_PPC64_REL16_HIGHEST:
15647 case R_PPC64_REL16_HIGHESTA:
15648 case R_PPC64_REL16_HIGHER34:
15649 case R_PPC64_REL16_HIGHERA34:
15650 case R_PPC64_REL16_HIGHEST34:
15651 case R_PPC64_REL16_HIGHESTA34:
15652 case R_PPC64_REL16DX_HA:
15653 case R_PPC64_REL14:
15654 case R_PPC64_REL14_BRNTAKEN:
15655 case R_PPC64_REL14_BRTAKEN:
15656 case R_PPC64_REL24:
15657 case R_PPC64_REL24_NOTOC:
15658 case R_PPC64_PCREL34:
15659 case R_PPC64_PCREL28:
15662 case R_PPC64_TPREL16:
15663 case R_PPC64_TPREL16_LO:
15664 case R_PPC64_TPREL16_HI:
15665 case R_PPC64_TPREL16_HA:
15666 case R_PPC64_TPREL16_DS:
15667 case R_PPC64_TPREL16_LO_DS:
15668 case R_PPC64_TPREL16_HIGH:
15669 case R_PPC64_TPREL16_HIGHA:
15670 case R_PPC64_TPREL16_HIGHER:
15671 case R_PPC64_TPREL16_HIGHERA:
15672 case R_PPC64_TPREL16_HIGHEST:
15673 case R_PPC64_TPREL16_HIGHESTA:
15675 && h->elf.root.type == bfd_link_hash_undefweak
15676 && h->elf.dynindx == -1)
15678 /* Make this relocation against an undefined weak symbol
15679 resolve to zero. This is really just a tweak, since
15680 code using weak externs ought to check that they are
15681 defined before using them. */
15682 bfd_byte *p = contents + rel->r_offset - d_offset;
15684 insn = bfd_get_32 (input_bfd, p);
15685 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15687 bfd_put_32 (input_bfd, insn, p);
15690 if (htab->elf.tls_sec != NULL)
15691 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15692 /* The TPREL16 relocs shouldn't really be used in shared
15693 libs or with non-local symbols as that will result in
15694 DT_TEXTREL being set, but support them anyway. */
15697 case R_PPC64_DTPREL16:
15698 case R_PPC64_DTPREL16_LO:
15699 case R_PPC64_DTPREL16_HI:
15700 case R_PPC64_DTPREL16_HA:
15701 case R_PPC64_DTPREL16_DS:
15702 case R_PPC64_DTPREL16_LO_DS:
15703 case R_PPC64_DTPREL16_HIGH:
15704 case R_PPC64_DTPREL16_HIGHA:
15705 case R_PPC64_DTPREL16_HIGHER:
15706 case R_PPC64_DTPREL16_HIGHERA:
15707 case R_PPC64_DTPREL16_HIGHEST:
15708 case R_PPC64_DTPREL16_HIGHESTA:
15709 if (htab->elf.tls_sec != NULL)
15710 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15713 case R_PPC64_ADDR64_LOCAL:
15714 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15719 case R_PPC64_DTPMOD64:
15724 case R_PPC64_TPREL64:
15725 if (htab->elf.tls_sec != NULL)
15726 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15729 case R_PPC64_DTPREL64:
15730 if (htab->elf.tls_sec != NULL)
15731 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15732 /* Fall through. */
15734 /* Relocations that may need to be propagated if this is a
15736 case R_PPC64_REL30:
15737 case R_PPC64_REL32:
15738 case R_PPC64_REL64:
15739 case R_PPC64_ADDR14:
15740 case R_PPC64_ADDR14_BRNTAKEN:
15741 case R_PPC64_ADDR14_BRTAKEN:
15742 case R_PPC64_ADDR16:
15743 case R_PPC64_ADDR16_DS:
15744 case R_PPC64_ADDR16_HA:
15745 case R_PPC64_ADDR16_HI:
15746 case R_PPC64_ADDR16_HIGH:
15747 case R_PPC64_ADDR16_HIGHA:
15748 case R_PPC64_ADDR16_HIGHER:
15749 case R_PPC64_ADDR16_HIGHERA:
15750 case R_PPC64_ADDR16_HIGHEST:
15751 case R_PPC64_ADDR16_HIGHESTA:
15752 case R_PPC64_ADDR16_LO:
15753 case R_PPC64_ADDR16_LO_DS:
15754 case R_PPC64_ADDR16_HIGHER34:
15755 case R_PPC64_ADDR16_HIGHERA34:
15756 case R_PPC64_ADDR16_HIGHEST34:
15757 case R_PPC64_ADDR16_HIGHESTA34:
15758 case R_PPC64_ADDR24:
15759 case R_PPC64_ADDR32:
15760 case R_PPC64_ADDR64:
15761 case R_PPC64_UADDR16:
15762 case R_PPC64_UADDR32:
15763 case R_PPC64_UADDR64:
15765 case R_PPC64_D34_LO:
15766 case R_PPC64_D34_HI30:
15767 case R_PPC64_D34_HA30:
15770 if ((input_section->flags & SEC_ALLOC) == 0)
15773 if (NO_OPD_RELOCS && is_opd)
15776 if (bfd_link_pic (info)
15778 || h->dyn_relocs != NULL)
15779 && ((h != NULL && pc_dynrelocs (h))
15780 || must_be_dyn_reloc (info, r_type)))
15782 ? h->dyn_relocs != NULL
15783 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15785 bfd_boolean skip, relocate;
15790 /* When generating a dynamic object, these relocations
15791 are copied into the output file to be resolved at run
15797 out_off = _bfd_elf_section_offset (output_bfd, info,
15798 input_section, rel->r_offset);
15799 if (out_off == (bfd_vma) -1)
15801 else if (out_off == (bfd_vma) -2)
15802 skip = TRUE, relocate = TRUE;
15803 out_off += (input_section->output_section->vma
15804 + input_section->output_offset);
15805 outrel.r_offset = out_off;
15806 outrel.r_addend = rel->r_addend;
15808 /* Optimize unaligned reloc use. */
15809 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15810 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15811 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15812 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15813 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15814 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15815 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15816 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15817 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15820 memset (&outrel, 0, sizeof outrel);
15821 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15823 && r_type != R_PPC64_TOC)
15825 indx = h->elf.dynindx;
15826 BFD_ASSERT (indx != -1);
15827 outrel.r_info = ELF64_R_INFO (indx, r_type);
15831 /* This symbol is local, or marked to become local,
15832 or this is an opd section reloc which must point
15833 at a local function. */
15834 outrel.r_addend += relocation;
15835 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
15837 if (is_opd && h != NULL)
15839 /* Lie about opd entries. This case occurs
15840 when building shared libraries and we
15841 reference a function in another shared
15842 lib. The same thing happens for a weak
15843 definition in an application that's
15844 overridden by a strong definition in a
15845 shared lib. (I believe this is a generic
15846 bug in binutils handling of weak syms.)
15847 In these cases we won't use the opd
15848 entry in this lib. */
15849 unresolved_reloc = FALSE;
15852 && r_type == R_PPC64_ADDR64
15854 ? h->elf.type == STT_GNU_IFUNC
15855 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15856 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15859 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15861 /* We need to relocate .opd contents for ld.so.
15862 Prelink also wants simple and consistent rules
15863 for relocs. This make all RELATIVE relocs have
15864 *r_offset equal to r_addend. */
15871 ? h->elf.type == STT_GNU_IFUNC
15872 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15874 info->callbacks->einfo
15875 /* xgettext:c-format */
15876 (_("%H: %s for indirect "
15877 "function `%pT' unsupported\n"),
15878 input_bfd, input_section, rel->r_offset,
15879 ppc64_elf_howto_table[r_type]->name,
15883 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
15885 else if (sec == NULL || sec->owner == NULL)
15887 bfd_set_error (bfd_error_bad_value);
15894 osec = sec->output_section;
15895 indx = elf_section_data (osec)->dynindx;
15899 if ((osec->flags & SEC_READONLY) == 0
15900 && htab->elf.data_index_section != NULL)
15901 osec = htab->elf.data_index_section;
15903 osec = htab->elf.text_index_section;
15904 indx = elf_section_data (osec)->dynindx;
15906 BFD_ASSERT (indx != 0);
15908 /* We are turning this relocation into one
15909 against a section symbol, so subtract out
15910 the output section's address but not the
15911 offset of the input section in the output
15913 outrel.r_addend -= osec->vma;
15916 outrel.r_info = ELF64_R_INFO (indx, r_type);
15920 sreloc = elf_section_data (input_section)->sreloc;
15922 ? h->elf.type == STT_GNU_IFUNC
15923 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15925 sreloc = htab->elf.irelplt;
15927 htab->local_ifunc_resolver = 1;
15928 else if (is_static_defined (&h->elf))
15929 htab->maybe_local_ifunc_resolver = 1;
15931 if (sreloc == NULL)
15934 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15937 loc = sreloc->contents;
15938 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
15939 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15941 /* If this reloc is against an external symbol, it will
15942 be computed at runtime, so there's no need to do
15943 anything now. However, for the sake of prelink ensure
15944 that the section contents are a known value. */
15947 unresolved_reloc = FALSE;
15948 /* The value chosen here is quite arbitrary as ld.so
15949 ignores section contents except for the special
15950 case of .opd where the contents might be accessed
15951 before relocation. Choose zero, as that won't
15952 cause reloc overflow. */
15955 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15956 to improve backward compatibility with older
15958 if (r_type == R_PPC64_ADDR64)
15959 addend = outrel.r_addend;
15960 /* Adjust pc_relative relocs to have zero in *r_offset. */
15961 else if (ppc64_elf_howto_table[r_type]->pc_relative)
15962 addend = outrel.r_offset;
15968 case R_PPC64_GLOB_DAT:
15969 case R_PPC64_JMP_SLOT:
15970 case R_PPC64_JMP_IREL:
15971 case R_PPC64_RELATIVE:
15972 /* We shouldn't ever see these dynamic relocs in relocatable
15974 /* Fall through. */
15976 case R_PPC64_PLTGOT16:
15977 case R_PPC64_PLTGOT16_DS:
15978 case R_PPC64_PLTGOT16_HA:
15979 case R_PPC64_PLTGOT16_HI:
15980 case R_PPC64_PLTGOT16_LO:
15981 case R_PPC64_PLTGOT16_LO_DS:
15982 case R_PPC64_PLTREL32:
15983 case R_PPC64_PLTREL64:
15984 /* These ones haven't been implemented yet. */
15986 info->callbacks->einfo
15987 /* xgettext:c-format */
15988 (_("%P: %pB: %s is not supported for `%pT'\n"),
15990 ppc64_elf_howto_table[r_type]->name, sym_name);
15992 bfd_set_error (bfd_error_invalid_operation);
15997 /* Multi-instruction sequences that access the TOC can be
15998 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15999 to nop; addi rb,r2,x; */
16005 case R_PPC64_GOT_TLSLD16_HI:
16006 case R_PPC64_GOT_TLSGD16_HI:
16007 case R_PPC64_GOT_TPREL16_HI:
16008 case R_PPC64_GOT_DTPREL16_HI:
16009 case R_PPC64_GOT16_HI:
16010 case R_PPC64_TOC16_HI:
16011 /* These relocs would only be useful if building up an
16012 offset to later add to r2, perhaps in an indexed
16013 addressing mode instruction. Don't try to optimize.
16014 Unfortunately, the possibility of someone building up an
16015 offset like this or even with the HA relocs, means that
16016 we need to check the high insn when optimizing the low
16020 case R_PPC64_PLTCALL_NOTOC:
16021 if (!unresolved_reloc)
16022 htab->notoc_plt = 1;
16023 /* Fall through. */
16024 case R_PPC64_PLTCALL:
16025 if (unresolved_reloc)
16027 /* No plt entry. Make this into a direct call. */
16028 bfd_byte *p = contents + rel->r_offset;
16029 insn = bfd_get_32 (input_bfd, p);
16031 bfd_put_32 (input_bfd, B_DOT | insn, p);
16032 if (r_type == R_PPC64_PLTCALL)
16033 bfd_put_32 (input_bfd, NOP, p + 4);
16034 unresolved_reloc = save_unresolved_reloc;
16035 r_type = R_PPC64_REL24;
16039 case R_PPC64_PLTSEQ_NOTOC:
16040 case R_PPC64_PLTSEQ:
16041 if (unresolved_reloc)
16043 unresolved_reloc = FALSE;
16048 case R_PPC64_PLT_PCREL34_NOTOC:
16049 if (!unresolved_reloc)
16050 htab->notoc_plt = 1;
16051 /* Fall through. */
16052 case R_PPC64_PLT_PCREL34:
16053 if (unresolved_reloc)
16055 bfd_byte *p = contents + rel->r_offset;
16056 bfd_put_32 (input_bfd, PNOP >> 32, p);
16057 bfd_put_32 (input_bfd, PNOP, p + 4);
16058 unresolved_reloc = FALSE;
16063 case R_PPC64_PLT16_HA:
16064 if (unresolved_reloc)
16066 unresolved_reloc = FALSE;
16069 /* Fall through. */
16070 case R_PPC64_GOT_TLSLD16_HA:
16071 case R_PPC64_GOT_TLSGD16_HA:
16072 case R_PPC64_GOT_TPREL16_HA:
16073 case R_PPC64_GOT_DTPREL16_HA:
16074 case R_PPC64_GOT16_HA:
16075 case R_PPC64_TOC16_HA:
16076 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16077 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16081 p = contents + (rel->r_offset & ~3);
16082 bfd_put_32 (input_bfd, NOP, p);
16087 case R_PPC64_PLT16_LO:
16088 case R_PPC64_PLT16_LO_DS:
16089 if (unresolved_reloc)
16091 unresolved_reloc = FALSE;
16094 /* Fall through. */
16095 case R_PPC64_GOT_TLSLD16_LO:
16096 case R_PPC64_GOT_TLSGD16_LO:
16097 case R_PPC64_GOT_TPREL16_LO_DS:
16098 case R_PPC64_GOT_DTPREL16_LO_DS:
16099 case R_PPC64_GOT16_LO:
16100 case R_PPC64_GOT16_LO_DS:
16101 case R_PPC64_TOC16_LO:
16102 case R_PPC64_TOC16_LO_DS:
16103 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16104 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16106 bfd_byte *p = contents + (rel->r_offset & ~3);
16107 insn = bfd_get_32 (input_bfd, p);
16108 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
16110 /* Transform addic to addi when we change reg. */
16111 insn &= ~((0x3f << 26) | (0x1f << 16));
16112 insn |= (14u << 26) | (2 << 16);
16116 insn &= ~(0x1f << 16);
16119 bfd_put_32 (input_bfd, insn, p);
16123 case R_PPC64_TPREL16_HA:
16124 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16126 bfd_byte *p = contents + (rel->r_offset & ~3);
16127 insn = bfd_get_32 (input_bfd, p);
16128 if ((insn & ((0x3f << 26) | 0x1f << 16))
16129 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
16130 /* xgettext:c-format */
16131 info->callbacks->minfo
16132 (_("%H: warning: %s unexpected insn %#x.\n"),
16133 input_bfd, input_section, rel->r_offset,
16134 ppc64_elf_howto_table[r_type]->name, insn);
16137 bfd_put_32 (input_bfd, NOP, p);
16143 case R_PPC64_TPREL16_LO:
16144 case R_PPC64_TPREL16_LO_DS:
16145 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16147 bfd_byte *p = contents + (rel->r_offset & ~3);
16148 insn = bfd_get_32 (input_bfd, p);
16149 insn &= ~(0x1f << 16);
16151 bfd_put_32 (input_bfd, insn, p);
16156 /* Do any further special processing. */
16162 case R_PPC64_REL16_HA:
16163 case R_PPC64_REL16_HIGHA:
16164 case R_PPC64_REL16_HIGHERA:
16165 case R_PPC64_REL16_HIGHESTA:
16166 case R_PPC64_REL16DX_HA:
16167 case R_PPC64_ADDR16_HA:
16168 case R_PPC64_ADDR16_HIGHA:
16169 case R_PPC64_ADDR16_HIGHERA:
16170 case R_PPC64_ADDR16_HIGHESTA:
16171 case R_PPC64_TOC16_HA:
16172 case R_PPC64_SECTOFF_HA:
16173 case R_PPC64_TPREL16_HA:
16174 case R_PPC64_TPREL16_HIGHA:
16175 case R_PPC64_TPREL16_HIGHERA:
16176 case R_PPC64_TPREL16_HIGHESTA:
16177 case R_PPC64_DTPREL16_HA:
16178 case R_PPC64_DTPREL16_HIGHA:
16179 case R_PPC64_DTPREL16_HIGHERA:
16180 case R_PPC64_DTPREL16_HIGHESTA:
16181 /* It's just possible that this symbol is a weak symbol
16182 that's not actually defined anywhere. In that case,
16183 'sec' would be NULL, and we should leave the symbol
16184 alone (it will be set to zero elsewhere in the link). */
16187 /* Fall through. */
16189 case R_PPC64_GOT16_HA:
16190 case R_PPC64_PLTGOT16_HA:
16191 case R_PPC64_PLT16_HA:
16192 case R_PPC64_GOT_TLSGD16_HA:
16193 case R_PPC64_GOT_TLSLD16_HA:
16194 case R_PPC64_GOT_TPREL16_HA:
16195 case R_PPC64_GOT_DTPREL16_HA:
16196 /* Add 0x10000 if sign bit in 0:15 is set.
16197 Bits 0:15 are not used. */
16201 case R_PPC64_D34_HA30:
16202 case R_PPC64_ADDR16_HIGHERA34:
16203 case R_PPC64_ADDR16_HIGHESTA34:
16204 case R_PPC64_REL16_HIGHERA34:
16205 case R_PPC64_REL16_HIGHESTA34:
16207 addend += 1ULL << 33;
16210 case R_PPC64_ADDR16_DS:
16211 case R_PPC64_ADDR16_LO_DS:
16212 case R_PPC64_GOT16_DS:
16213 case R_PPC64_GOT16_LO_DS:
16214 case R_PPC64_PLT16_LO_DS:
16215 case R_PPC64_SECTOFF_DS:
16216 case R_PPC64_SECTOFF_LO_DS:
16217 case R_PPC64_TOC16_DS:
16218 case R_PPC64_TOC16_LO_DS:
16219 case R_PPC64_PLTGOT16_DS:
16220 case R_PPC64_PLTGOT16_LO_DS:
16221 case R_PPC64_GOT_TPREL16_DS:
16222 case R_PPC64_GOT_TPREL16_LO_DS:
16223 case R_PPC64_GOT_DTPREL16_DS:
16224 case R_PPC64_GOT_DTPREL16_LO_DS:
16225 case R_PPC64_TPREL16_DS:
16226 case R_PPC64_TPREL16_LO_DS:
16227 case R_PPC64_DTPREL16_DS:
16228 case R_PPC64_DTPREL16_LO_DS:
16229 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16231 /* If this reloc is against an lq, lxv, or stxv insn, then
16232 the value must be a multiple of 16. This is somewhat of
16233 a hack, but the "correct" way to do this by defining _DQ
16234 forms of all the _DS relocs bloats all reloc switches in
16235 this file. It doesn't make much sense to use these
16236 relocs in data, so testing the insn should be safe. */
16237 if ((insn & (0x3f << 26)) == (56u << 26)
16238 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
16240 relocation += addend;
16241 addend = insn & (mask ^ 3);
16242 if ((relocation & mask) != 0)
16244 relocation ^= relocation & mask;
16245 info->callbacks->einfo
16246 /* xgettext:c-format */
16247 (_("%H: error: %s not a multiple of %u\n"),
16248 input_bfd, input_section, rel->r_offset,
16249 ppc64_elf_howto_table[r_type]->name,
16251 bfd_set_error (bfd_error_bad_value);
16258 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16259 because such sections are not SEC_ALLOC and thus ld.so will
16260 not process them. */
16261 howto = ppc64_elf_howto_table[(int) r_type];
16262 if (unresolved_reloc
16263 && !((input_section->flags & SEC_DEBUGGING) != 0
16264 && h->elf.def_dynamic)
16265 && _bfd_elf_section_offset (output_bfd, info, input_section,
16266 rel->r_offset) != (bfd_vma) -1)
16268 info->callbacks->einfo
16269 /* xgettext:c-format */
16270 (_("%H: unresolvable %s against `%pT'\n"),
16271 input_bfd, input_section, rel->r_offset,
16273 h->elf.root.root.string);
16277 /* 16-bit fields in insns mostly have signed values, but a
16278 few insns have 16-bit unsigned values. Really, we should
16279 have different reloc types. */
16280 if (howto->complain_on_overflow != complain_overflow_dont
16281 && howto->dst_mask == 0xffff
16282 && (input_section->flags & SEC_CODE) != 0)
16284 enum complain_overflow complain = complain_overflow_signed;
16286 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16287 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16288 complain = complain_overflow_bitfield;
16289 else if (howto->rightshift == 0
16290 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16291 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16292 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16293 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16294 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16295 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
16296 complain = complain_overflow_unsigned;
16297 if (howto->complain_on_overflow != complain)
16299 alt_howto = *howto;
16300 alt_howto.complain_on_overflow = complain;
16301 howto = &alt_howto;
16307 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
16309 case R_PPC64_D34_LO:
16310 case R_PPC64_D34_HI30:
16311 case R_PPC64_D34_HA30:
16312 case R_PPC64_PCREL34:
16313 case R_PPC64_GOT_PCREL34:
16314 case R_PPC64_PLT_PCREL34:
16315 case R_PPC64_PLT_PCREL34_NOTOC:
16317 case R_PPC64_PCREL28:
16318 if (rel->r_offset + 8 > input_section->size)
16319 r = bfd_reloc_outofrange;
16322 relocation += addend;
16323 if (howto->pc_relative)
16324 relocation -= (rel->r_offset
16325 + input_section->output_offset
16326 + input_section->output_section->vma);
16327 relocation >>= howto->rightshift;
16329 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16331 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16333 pinsn &= ~howto->dst_mask;
16334 pinsn |= (((relocation << 16) | (relocation & 0xffff))
16335 & howto->dst_mask);
16336 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
16337 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
16339 if (howto->complain_on_overflow == complain_overflow_signed
16340 && (relocation + (1ULL << (howto->bitsize - 1))
16341 >= 1ULL << howto->bitsize))
16342 r = bfd_reloc_overflow;
16346 case R_PPC64_REL16DX_HA:
16347 if (rel->r_offset + 4 > input_section->size)
16348 r = bfd_reloc_outofrange;
16351 relocation += addend;
16352 relocation -= (rel->r_offset
16353 + input_section->output_offset
16354 + input_section->output_section->vma);
16355 relocation = (bfd_signed_vma) relocation >> 16;
16356 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16358 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
16359 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16361 if (relocation + 0x8000 > 0xffff)
16362 r = bfd_reloc_overflow;
16367 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
16368 contents, rel->r_offset,
16369 relocation, addend);
16372 if (r != bfd_reloc_ok)
16374 char *more_info = NULL;
16375 const char *reloc_name = howto->name;
16377 if (reloc_dest != DEST_NORMAL)
16379 more_info = bfd_malloc (strlen (reloc_name) + 8);
16380 if (more_info != NULL)
16382 strcpy (more_info, reloc_name);
16383 strcat (more_info, (reloc_dest == DEST_OPD
16384 ? " (OPD)" : " (stub)"));
16385 reloc_name = more_info;
16389 if (r == bfd_reloc_overflow)
16391 /* On code like "if (foo) foo();" don't report overflow
16392 on a branch to zero when foo is undefined. */
16394 && (reloc_dest == DEST_STUB
16396 && (h->elf.root.type == bfd_link_hash_undefweak
16397 || h->elf.root.type == bfd_link_hash_undefined)
16398 && is_branch_reloc (r_type))))
16399 info->callbacks->reloc_overflow (info, &h->elf.root,
16400 sym_name, reloc_name,
16402 input_bfd, input_section,
16407 info->callbacks->einfo
16408 /* xgettext:c-format */
16409 (_("%H: %s against `%pT': error %d\n"),
16410 input_bfd, input_section, rel->r_offset,
16411 reloc_name, sym_name, (int) r);
16414 if (more_info != NULL)
16424 Elf_Internal_Shdr *rel_hdr;
16425 size_t deleted = rel - wrel;
16427 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16428 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16429 if (rel_hdr->sh_size == 0)
16431 /* It is too late to remove an empty reloc section. Leave
16433 ??? What is wrong with an empty section??? */
16434 rel_hdr->sh_size = rel_hdr->sh_entsize;
16437 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16438 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16439 input_section->reloc_count -= deleted;
16442 /* If we're emitting relocations, then shortly after this function
16443 returns, reloc offsets and addends for this section will be
16444 adjusted. Worse, reloc symbol indices will be for the output
16445 file rather than the input. Save a copy of the relocs for
16446 opd_entry_value. */
16447 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
16450 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16451 rel = bfd_alloc (input_bfd, amt);
16452 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16453 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
16456 memcpy (rel, relocs, amt);
16461 /* Adjust the value of any local symbols in opd sections. */
16464 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16465 const char *name ATTRIBUTE_UNUSED,
16466 Elf_Internal_Sym *elfsym,
16467 asection *input_sec,
16468 struct elf_link_hash_entry *h)
16470 struct _opd_sec_data *opd;
16477 opd = get_opd_info (input_sec);
16478 if (opd == NULL || opd->adjust == NULL)
16481 value = elfsym->st_value - input_sec->output_offset;
16482 if (!bfd_link_relocatable (info))
16483 value -= input_sec->output_section->vma;
16485 adjust = opd->adjust[OPD_NDX (value)];
16489 elfsym->st_value += adjust;
16493 /* Finish up dynamic symbol handling. We set the contents of various
16494 dynamic sections here. */
16497 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16498 struct bfd_link_info *info,
16499 struct elf_link_hash_entry *h,
16500 Elf_Internal_Sym *sym)
16502 struct ppc_link_hash_table *htab;
16503 struct plt_entry *ent;
16505 htab = ppc_hash_table (info);
16509 if (!htab->opd_abi && !h->def_regular)
16510 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16511 if (ent->plt.offset != (bfd_vma) -1)
16513 /* Mark the symbol as undefined, rather than as
16514 defined in glink. Leave the value if there were
16515 any relocations where pointer equality matters
16516 (this is a clue for the dynamic linker, to make
16517 function pointer comparisons work between an
16518 application and shared library), otherwise set it
16520 sym->st_shndx = SHN_UNDEF;
16521 if (!h->pointer_equality_needed)
16523 else if (!h->ref_regular_nonweak)
16525 /* This breaks function pointer comparisons, but
16526 that is better than breaking tests for a NULL
16527 function pointer. */
16535 /* This symbol needs a copy reloc. Set it up. */
16536 Elf_Internal_Rela rela;
16540 if (h->dynindx == -1
16541 || (h->root.type != bfd_link_hash_defined
16542 && h->root.type != bfd_link_hash_defweak)
16543 || htab->elf.srelbss == NULL
16544 || htab->elf.sreldynrelro == NULL)
16547 rela.r_offset = (h->root.u.def.value
16548 + h->root.u.def.section->output_section->vma
16549 + h->root.u.def.section->output_offset);
16550 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16552 if (h->root.u.def.section == htab->elf.sdynrelro)
16553 srel = htab->elf.sreldynrelro;
16555 srel = htab->elf.srelbss;
16556 loc = srel->contents;
16557 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
16558 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
16564 /* Used to decide how to sort relocs in an optimal manner for the
16565 dynamic linker, before writing them out. */
16567 static enum elf_reloc_type_class
16568 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16569 const asection *rel_sec,
16570 const Elf_Internal_Rela *rela)
16572 enum elf_ppc64_reloc_type r_type;
16573 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16575 if (rel_sec == htab->elf.irelplt)
16576 return reloc_class_ifunc;
16578 r_type = ELF64_R_TYPE (rela->r_info);
16581 case R_PPC64_RELATIVE:
16582 return reloc_class_relative;
16583 case R_PPC64_JMP_SLOT:
16584 return reloc_class_plt;
16586 return reloc_class_copy;
16588 return reloc_class_normal;
16592 /* Finish up the dynamic sections. */
16595 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16596 struct bfd_link_info *info)
16598 struct ppc_link_hash_table *htab;
16602 htab = ppc_hash_table (info);
16606 dynobj = htab->elf.dynobj;
16607 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
16609 if (htab->elf.dynamic_sections_created)
16611 Elf64_External_Dyn *dyncon, *dynconend;
16613 if (sdyn == NULL || htab->elf.sgot == NULL)
16616 dyncon = (Elf64_External_Dyn *) sdyn->contents;
16617 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
16618 for (; dyncon < dynconend; dyncon++)
16620 Elf_Internal_Dyn dyn;
16623 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16630 case DT_PPC64_GLINK:
16632 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16633 /* We stupidly defined DT_PPC64_GLINK to be the start
16634 of glink rather than the first entry point, which is
16635 what ld.so needs, and now have a bigger stub to
16636 support automatic multiple TOCs. */
16637 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
16641 s = bfd_get_section_by_name (output_bfd, ".opd");
16644 dyn.d_un.d_ptr = s->vma;
16648 if ((htab->do_multi_toc && htab->multi_toc_needed)
16649 || htab->notoc_plt)
16650 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
16651 if (htab->has_plt_localentry0)
16652 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
16655 case DT_PPC64_OPDSZ:
16656 s = bfd_get_section_by_name (output_bfd, ".opd");
16659 dyn.d_un.d_val = s->size;
16663 s = htab->elf.splt;
16664 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16668 s = htab->elf.srelplt;
16669 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16673 dyn.d_un.d_val = htab->elf.srelplt->size;
16677 if (htab->local_ifunc_resolver)
16678 info->callbacks->einfo
16679 (_("%X%P: text relocations and GNU indirect "
16680 "functions will result in a segfault at runtime\n"));
16681 else if (htab->maybe_local_ifunc_resolver)
16682 info->callbacks->einfo
16683 (_("%P: warning: text relocations and GNU indirect "
16684 "functions may result in a segfault at runtime\n"));
16688 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
16692 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16693 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
16695 /* Fill in the first entry in the global offset table.
16696 We use it to hold the link-time TOCbase. */
16697 bfd_put_64 (output_bfd,
16698 elf_gp (output_bfd) + TOC_BASE_OFF,
16699 htab->elf.sgot->contents);
16701 /* Set .got entry size. */
16702 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
16706 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16707 && htab->elf.splt->output_section != bfd_abs_section_ptr)
16709 /* Set .plt entry size. */
16710 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
16711 = PLT_ENTRY_SIZE (htab);
16714 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16715 brlt ourselves if emitrelocations. */
16716 if (htab->brlt != NULL
16717 && htab->brlt->reloc_count != 0
16718 && !_bfd_elf_link_output_relocs (output_bfd,
16720 elf_section_data (htab->brlt)->rela.hdr,
16721 elf_section_data (htab->brlt)->relocs,
16725 if (htab->glink != NULL
16726 && htab->glink->reloc_count != 0
16727 && !_bfd_elf_link_output_relocs (output_bfd,
16729 elf_section_data (htab->glink)->rela.hdr,
16730 elf_section_data (htab->glink)->relocs,
16735 if (htab->glink_eh_frame != NULL
16736 && htab->glink_eh_frame->size != 0
16737 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16738 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16739 htab->glink_eh_frame,
16740 htab->glink_eh_frame->contents))
16743 /* We need to handle writing out multiple GOT sections ourselves,
16744 since we didn't add them to DYNOBJ. We know dynobj is the first
16746 while ((dynobj = dynobj->link.next) != NULL)
16750 if (!is_ppc64_elf (dynobj))
16753 s = ppc64_elf_tdata (dynobj)->got;
16756 && s->output_section != bfd_abs_section_ptr
16757 && !bfd_set_section_contents (output_bfd, s->output_section,
16758 s->contents, s->output_offset,
16761 s = ppc64_elf_tdata (dynobj)->relgot;
16764 && s->output_section != bfd_abs_section_ptr
16765 && !bfd_set_section_contents (output_bfd, s->output_section,
16766 s->contents, s->output_offset,
16774 #include "elf64-target.h"
16776 /* FreeBSD support */
16778 #undef TARGET_LITTLE_SYM
16779 #undef TARGET_LITTLE_NAME
16781 #undef TARGET_BIG_SYM
16782 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
16783 #undef TARGET_BIG_NAME
16784 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16787 #define ELF_OSABI ELFOSABI_FREEBSD
16790 #define elf64_bed elf64_powerpc_fbsd_bed
16792 #include "elf64-target.h"