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_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
924 ppc64_elf_unhandled_reloc),
926 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
927 ppc64_elf_unhandled_reloc),
929 HOW (R_PPC64_GOT_TLSGD34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
930 ppc64_elf_unhandled_reloc),
932 HOW (R_PPC64_GOT_TLSLD34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
933 ppc64_elf_unhandled_reloc),
935 HOW (R_PPC64_GOT_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
936 ppc64_elf_unhandled_reloc),
938 HOW (R_PPC64_GOT_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
939 ppc64_elf_unhandled_reloc),
941 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, FALSE, dont,
942 bfd_elf_generic_reloc),
944 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, FALSE, dont,
947 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, FALSE, dont,
948 bfd_elf_generic_reloc),
950 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, FALSE, dont,
953 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, TRUE, dont,
954 bfd_elf_generic_reloc),
956 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, TRUE, dont,
959 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, TRUE, dont,
960 bfd_elf_generic_reloc),
962 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, TRUE, dont,
965 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, FALSE, signed,
966 ppc64_elf_prefix_reloc),
968 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, TRUE, signed,
969 ppc64_elf_prefix_reloc),
971 /* GNU extension to record C++ vtable hierarchy. */
972 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
975 /* GNU extension to record C++ vtable member usage. */
976 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
981 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
985 ppc_howto_init (void)
987 unsigned int i, type;
989 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
991 type = ppc64_elf_howto_raw[i].type;
992 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
993 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
997 static reloc_howto_type *
998 ppc64_elf_reloc_type_lookup (bfd *abfd,
999 bfd_reloc_code_real_type code)
1001 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1003 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1004 /* Initialize howto table if needed. */
1010 /* xgettext:c-format */
1011 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1013 bfd_set_error (bfd_error_bad_value);
1016 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1018 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1020 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1022 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1024 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1026 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1028 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1030 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1032 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1034 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1036 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1038 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1040 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1042 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1044 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1046 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1048 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1050 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1052 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1054 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1056 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1058 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1060 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1062 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1064 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1066 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1068 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1070 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1072 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1074 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1076 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1078 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1080 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1082 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1084 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1086 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1088 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1090 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1092 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1094 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1096 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1098 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1100 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1102 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1104 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1106 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1108 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1110 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1112 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1114 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1116 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1118 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1120 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1122 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1124 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1126 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1128 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1130 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1132 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1134 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1136 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1138 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1140 case BFD_RELOC_PPC64_TLS_PCREL:
1141 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1143 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1145 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1147 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1149 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1151 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1153 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1155 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1157 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1159 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1161 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1163 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1165 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1167 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1169 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1171 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1173 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1175 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1177 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1179 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1181 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1183 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1185 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1187 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1189 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1191 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1193 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1195 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1197 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1199 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1201 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1203 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1205 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1207 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1209 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1211 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1213 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1215 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1217 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1219 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1221 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1223 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1225 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1227 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1229 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1231 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1233 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1235 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1237 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1239 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1241 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1243 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1245 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1247 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1249 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1251 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1253 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1255 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1257 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1259 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1261 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1263 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1265 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1267 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1269 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1271 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1273 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1275 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1277 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1279 case BFD_RELOC_PPC64_GOT_TLSGD34: r = R_PPC64_GOT_TLSGD34;
1281 case BFD_RELOC_PPC64_GOT_TLSLD34: r = R_PPC64_GOT_TLSLD34;
1283 case BFD_RELOC_PPC64_GOT_TPREL34: r = R_PPC64_GOT_TPREL34;
1285 case BFD_RELOC_PPC64_GOT_DTPREL34: r = R_PPC64_GOT_DTPREL34;
1287 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1289 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1291 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1293 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1295 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1297 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1299 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1301 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1303 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1305 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1307 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1309 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1313 return ppc64_elf_howto_table[r];
1316 static reloc_howto_type *
1317 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1322 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1323 if (ppc64_elf_howto_raw[i].name != NULL
1324 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1325 return &ppc64_elf_howto_raw[i];
1330 /* Set the howto pointer for a PowerPC ELF reloc. */
1333 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1334 Elf_Internal_Rela *dst)
1338 /* Initialize howto table if needed. */
1339 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1342 type = ELF64_R_TYPE (dst->r_info);
1343 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1345 /* xgettext:c-format */
1346 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1348 bfd_set_error (bfd_error_bad_value);
1351 cache_ptr->howto = ppc64_elf_howto_table[type];
1352 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1354 /* xgettext:c-format */
1355 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1357 bfd_set_error (bfd_error_bad_value);
1364 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1366 static bfd_reloc_status_type
1367 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1368 void *data, asection *input_section,
1369 bfd *output_bfd, char **error_message)
1371 enum elf_ppc64_reloc_type r_type;
1373 bfd_size_type octets;
1376 /* If this is a relocatable link (output_bfd test tells us), just
1377 call the generic function. Any adjustment will be done at final
1379 if (output_bfd != NULL)
1380 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1381 input_section, output_bfd, error_message);
1383 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1384 We won't actually be using the low bits, so trashing them
1386 r_type = reloc_entry->howto->type;
1387 if (r_type == R_PPC64_ADDR16_HIGHERA34
1388 || r_type == R_PPC64_ADDR16_HIGHESTA34
1389 || r_type == R_PPC64_REL16_HIGHERA34
1390 || r_type == R_PPC64_REL16_HIGHESTA34)
1391 reloc_entry->addend += 1ULL << 33;
1393 reloc_entry->addend += 1U << 15;
1394 if (r_type != R_PPC64_REL16DX_HA)
1395 return bfd_reloc_continue;
1398 if (!bfd_is_com_section (symbol->section))
1399 value = symbol->value;
1400 value += (reloc_entry->addend
1401 + symbol->section->output_offset
1402 + symbol->section->output_section->vma);
1403 value -= (reloc_entry->address
1404 + input_section->output_offset
1405 + input_section->output_section->vma);
1406 value = (bfd_signed_vma) value >> 16;
1408 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1409 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1411 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1412 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1413 if (value + 0x8000 > 0xffff)
1414 return bfd_reloc_overflow;
1415 return bfd_reloc_ok;
1418 static bfd_reloc_status_type
1419 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1420 void *data, asection *input_section,
1421 bfd *output_bfd, char **error_message)
1423 if (output_bfd != NULL)
1424 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1425 input_section, output_bfd, error_message);
1427 if (strcmp (symbol->section->name, ".opd") == 0
1428 && (symbol->section->owner->flags & DYNAMIC) == 0)
1430 bfd_vma dest = opd_entry_value (symbol->section,
1431 symbol->value + reloc_entry->addend,
1433 if (dest != (bfd_vma) -1)
1434 reloc_entry->addend = dest - (symbol->value
1435 + symbol->section->output_section->vma
1436 + symbol->section->output_offset);
1440 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1442 if (symbol->section->owner != abfd
1443 && symbol->section->owner != NULL
1444 && abiversion (symbol->section->owner) >= 2)
1448 for (i = 0; i < symbol->section->owner->symcount; ++i)
1450 asymbol *symdef = symbol->section->owner->outsymbols[i];
1452 if (strcmp (symdef->name, symbol->name) == 0)
1454 elfsym = (elf_symbol_type *) symdef;
1460 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1462 return bfd_reloc_continue;
1465 static bfd_reloc_status_type
1466 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1467 void *data, asection *input_section,
1468 bfd *output_bfd, char **error_message)
1471 enum elf_ppc64_reloc_type r_type;
1472 bfd_size_type octets;
1473 /* Assume 'at' branch hints. */
1474 bfd_boolean is_isa_v2 = TRUE;
1476 /* If this is a relocatable link (output_bfd test tells us), just
1477 call the generic function. Any adjustment will be done at final
1479 if (output_bfd != NULL)
1480 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1481 input_section, output_bfd, error_message);
1483 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1484 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1485 insn &= ~(0x01 << 21);
1486 r_type = reloc_entry->howto->type;
1487 if (r_type == R_PPC64_ADDR14_BRTAKEN
1488 || r_type == R_PPC64_REL14_BRTAKEN)
1489 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1493 /* Set 'a' bit. This is 0b00010 in BO field for branch
1494 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1495 for branch on CTR insns (BO == 1a00t or 1a01t). */
1496 if ((insn & (0x14 << 21)) == (0x04 << 21))
1498 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1508 if (!bfd_is_com_section (symbol->section))
1509 target = symbol->value;
1510 target += symbol->section->output_section->vma;
1511 target += symbol->section->output_offset;
1512 target += reloc_entry->addend;
1514 from = (reloc_entry->address
1515 + input_section->output_offset
1516 + input_section->output_section->vma);
1518 /* Invert 'y' bit if not the default. */
1519 if ((bfd_signed_vma) (target - from) < 0)
1522 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1524 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1525 input_section, output_bfd, error_message);
1528 static bfd_reloc_status_type
1529 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1530 void *data, asection *input_section,
1531 bfd *output_bfd, char **error_message)
1533 /* If this is a relocatable link (output_bfd test tells us), just
1534 call the generic function. Any adjustment will be done at final
1536 if (output_bfd != NULL)
1537 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1538 input_section, output_bfd, error_message);
1540 /* Subtract the symbol section base address. */
1541 reloc_entry->addend -= symbol->section->output_section->vma;
1542 return bfd_reloc_continue;
1545 static bfd_reloc_status_type
1546 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1547 void *data, asection *input_section,
1548 bfd *output_bfd, char **error_message)
1550 /* If this is a relocatable link (output_bfd test tells us), just
1551 call the generic function. Any adjustment will be done at final
1553 if (output_bfd != NULL)
1554 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1555 input_section, output_bfd, error_message);
1557 /* Subtract the symbol section base address. */
1558 reloc_entry->addend -= symbol->section->output_section->vma;
1560 /* Adjust the addend for sign extension of the low 16 bits. */
1561 reloc_entry->addend += 0x8000;
1562 return bfd_reloc_continue;
1565 static bfd_reloc_status_type
1566 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1567 void *data, asection *input_section,
1568 bfd *output_bfd, char **error_message)
1572 /* If this is a relocatable link (output_bfd test tells us), just
1573 call the generic function. Any adjustment will be done at final
1575 if (output_bfd != NULL)
1576 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1577 input_section, output_bfd, error_message);
1579 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1581 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1583 /* Subtract the TOC base address. */
1584 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1585 return bfd_reloc_continue;
1588 static bfd_reloc_status_type
1589 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1590 void *data, asection *input_section,
1591 bfd *output_bfd, char **error_message)
1595 /* If this is a relocatable link (output_bfd test tells us), just
1596 call the generic function. Any adjustment will be done at final
1598 if (output_bfd != NULL)
1599 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1600 input_section, output_bfd, error_message);
1602 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1604 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1606 /* Subtract the TOC base address. */
1607 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1609 /* Adjust the addend for sign extension of the low 16 bits. */
1610 reloc_entry->addend += 0x8000;
1611 return bfd_reloc_continue;
1614 static bfd_reloc_status_type
1615 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1616 void *data, asection *input_section,
1617 bfd *output_bfd, char **error_message)
1620 bfd_size_type octets;
1622 /* If this is a relocatable link (output_bfd test tells us), just
1623 call the generic function. Any adjustment will be done at final
1625 if (output_bfd != NULL)
1626 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1627 input_section, output_bfd, error_message);
1629 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1631 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1633 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1634 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1635 return bfd_reloc_ok;
1638 static bfd_reloc_status_type
1639 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1640 void *data, asection *input_section,
1641 bfd *output_bfd, char **error_message)
1646 if (output_bfd != NULL)
1647 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1648 input_section, output_bfd, error_message);
1650 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1652 insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
1654 targ = (symbol->section->output_section->vma
1655 + symbol->section->output_offset
1656 + reloc_entry->addend);
1657 if (!bfd_is_com_section (symbol->section))
1658 targ += symbol->value;
1659 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1661 if (reloc_entry->howto->pc_relative)
1663 bfd_vma from = (reloc_entry->address
1664 + input_section->output_offset
1665 + input_section->output_section->vma);
1668 targ >>= reloc_entry->howto->rightshift;
1669 insn &= ~reloc_entry->howto->dst_mask;
1670 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1671 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
1672 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
1673 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1674 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1675 >= 1ULL << reloc_entry->howto->bitsize))
1676 return bfd_reloc_overflow;
1677 return bfd_reloc_ok;
1680 static bfd_reloc_status_type
1681 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1682 void *data, asection *input_section,
1683 bfd *output_bfd, char **error_message)
1685 /* If this is a relocatable link (output_bfd test tells us), just
1686 call the generic function. Any adjustment will be done at final
1688 if (output_bfd != NULL)
1689 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1690 input_section, output_bfd, error_message);
1692 if (error_message != NULL)
1694 static char buf[60];
1695 sprintf (buf, "generic linker can't handle %s",
1696 reloc_entry->howto->name);
1697 *error_message = buf;
1699 return bfd_reloc_dangerous;
1702 /* Track GOT entries needed for a given symbol. We might need more
1703 than one got entry per symbol. */
1706 struct got_entry *next;
1708 /* The symbol addend that we'll be placing in the GOT. */
1711 /* Unlike other ELF targets, we use separate GOT entries for the same
1712 symbol referenced from different input files. This is to support
1713 automatic multiple TOC/GOT sections, where the TOC base can vary
1714 from one input file to another. After partitioning into TOC groups
1715 we merge entries within the group.
1717 Point to the BFD owning this GOT entry. */
1720 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1721 TLS_TPREL or TLS_DTPREL for tls entries. */
1722 unsigned char tls_type;
1724 /* Non-zero if got.ent points to real entry. */
1725 unsigned char is_indirect;
1727 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1730 bfd_signed_vma refcount;
1732 struct got_entry *ent;
1736 /* The same for PLT. */
1739 struct plt_entry *next;
1745 bfd_signed_vma refcount;
1750 struct ppc64_elf_obj_tdata
1752 struct elf_obj_tdata elf;
1754 /* Shortcuts to dynamic linker sections. */
1758 /* Used during garbage collection. We attach global symbols defined
1759 on removed .opd entries to this section so that the sym is removed. */
1760 asection *deleted_section;
1762 /* TLS local dynamic got entry handling. Support for multiple GOT
1763 sections means we potentially need one of these for each input bfd. */
1764 struct got_entry tlsld_got;
1768 /* A copy of relocs before they are modified for --emit-relocs. */
1769 Elf_Internal_Rela *relocs;
1771 /* Section contents. */
1775 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1776 the reloc to be in the range -32768 to 32767. */
1777 unsigned int has_small_toc_reloc : 1;
1779 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1780 instruction not one we handle. */
1781 unsigned int unexpected_toc_insn : 1;
1783 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1785 unsigned int has_optrel : 1;
1788 #define ppc64_elf_tdata(bfd) \
1789 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1791 #define ppc64_tlsld_got(bfd) \
1792 (&ppc64_elf_tdata (bfd)->tlsld_got)
1794 #define is_ppc64_elf(bfd) \
1795 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1796 && elf_object_id (bfd) == PPC64_ELF_DATA)
1798 /* Override the generic function because we store some extras. */
1801 ppc64_elf_mkobject (bfd *abfd)
1803 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1807 /* Fix bad default arch selected for a 64 bit input bfd when the
1808 default is 32 bit. Also select arch based on apuinfo. */
1811 ppc64_elf_object_p (bfd *abfd)
1813 if (!abfd->arch_info->the_default)
1816 if (abfd->arch_info->bits_per_word == 32)
1818 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1820 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1822 /* Relies on arch after 32 bit default being 64 bit default. */
1823 abfd->arch_info = abfd->arch_info->next;
1824 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1827 return _bfd_elf_ppc_set_arch (abfd);
1830 /* Support for core dump NOTE sections. */
1833 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1835 size_t offset, size;
1837 if (note->descsz != 504)
1841 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1844 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1850 /* Make a ".reg/999" section. */
1851 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1852 size, note->descpos + offset);
1856 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1858 if (note->descsz != 136)
1861 elf_tdata (abfd)->core->pid
1862 = bfd_get_32 (abfd, note->descdata + 24);
1863 elf_tdata (abfd)->core->program
1864 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1865 elf_tdata (abfd)->core->command
1866 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1872 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1882 char data[136] ATTRIBUTE_NONSTRING;
1885 va_start (ap, note_type);
1886 memset (data, 0, sizeof (data));
1887 strncpy (data + 40, va_arg (ap, const char *), 16);
1888 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1890 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1891 -Wstringop-truncation:
1892 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1894 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1896 strncpy (data + 56, va_arg (ap, const char *), 80);
1897 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1901 return elfcore_write_note (abfd, buf, bufsiz,
1902 "CORE", note_type, data, sizeof (data));
1913 va_start (ap, note_type);
1914 memset (data, 0, 112);
1915 pid = va_arg (ap, long);
1916 bfd_put_32 (abfd, pid, data + 32);
1917 cursig = va_arg (ap, int);
1918 bfd_put_16 (abfd, cursig, data + 12);
1919 greg = va_arg (ap, const void *);
1920 memcpy (data + 112, greg, 384);
1921 memset (data + 496, 0, 8);
1923 return elfcore_write_note (abfd, buf, bufsiz,
1924 "CORE", note_type, data, sizeof (data));
1929 /* Add extra PPC sections. */
1931 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1933 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1934 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1935 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1936 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1937 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1938 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1939 { NULL, 0, 0, 0, 0 }
1942 enum _ppc64_sec_type {
1948 struct _ppc64_elf_section_data
1950 struct bfd_elf_section_data elf;
1954 /* An array with one entry for each opd function descriptor,
1955 and some spares since opd entries may be either 16 or 24 bytes. */
1956 #define OPD_NDX(OFF) ((OFF) >> 4)
1957 struct _opd_sec_data
1959 /* Points to the function code section for local opd entries. */
1960 asection **func_sec;
1962 /* After editing .opd, adjust references to opd local syms. */
1966 /* An array for toc sections, indexed by offset/8. */
1967 struct _toc_sec_data
1969 /* Specifies the relocation symbol index used at a given toc offset. */
1972 /* And the relocation addend. */
1977 enum _ppc64_sec_type sec_type:2;
1979 /* Flag set when small branches are detected. Used to
1980 select suitable defaults for the stub group size. */
1981 unsigned int has_14bit_branch:1;
1983 /* Flag set when PLTCALL relocs are detected. */
1984 unsigned int has_pltcall:1;
1986 /* Flag set when section has PLT/GOT/TOC relocations that can be
1988 unsigned int has_optrel:1;
1991 #define ppc64_elf_section_data(sec) \
1992 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
1995 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
1997 if (!sec->used_by_bfd)
1999 struct _ppc64_elf_section_data *sdata;
2000 bfd_size_type amt = sizeof (*sdata);
2002 sdata = bfd_zalloc (abfd, amt);
2005 sec->used_by_bfd = sdata;
2008 return _bfd_elf_new_section_hook (abfd, sec);
2011 static struct _opd_sec_data *
2012 get_opd_info (asection * sec)
2015 && ppc64_elf_section_data (sec) != NULL
2016 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2017 return &ppc64_elf_section_data (sec)->u.opd;
2021 /* Parameters for the qsort hook. */
2022 static bfd_boolean synthetic_relocatable;
2023 static asection *synthetic_opd;
2025 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2028 compare_symbols (const void *ap, const void *bp)
2030 const asymbol *a = *(const asymbol **) ap;
2031 const asymbol *b = *(const asymbol **) bp;
2033 /* Section symbols first. */
2034 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2036 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2039 /* then .opd symbols. */
2040 if (synthetic_opd != NULL)
2042 if (strcmp (a->section->name, ".opd") == 0
2043 && strcmp (b->section->name, ".opd") != 0)
2045 if (strcmp (a->section->name, ".opd") != 0
2046 && strcmp (b->section->name, ".opd") == 0)
2050 /* then other code symbols. */
2051 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2052 == (SEC_CODE | SEC_ALLOC))
2053 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2054 != (SEC_CODE | SEC_ALLOC)))
2057 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2058 != (SEC_CODE | SEC_ALLOC))
2059 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2060 == (SEC_CODE | SEC_ALLOC)))
2063 if (synthetic_relocatable)
2065 if (a->section->id < b->section->id)
2068 if (a->section->id > b->section->id)
2072 if (a->value + a->section->vma < b->value + b->section->vma)
2075 if (a->value + a->section->vma > b->value + b->section->vma)
2078 /* For syms with the same value, prefer strong dynamic global function
2079 syms over other syms. */
2080 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2083 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2086 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2089 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2092 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2095 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2098 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2101 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2107 /* Search SYMS for a symbol of the given VALUE. */
2110 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
2114 if (id == (unsigned) -1)
2118 mid = (lo + hi) >> 1;
2119 if (syms[mid]->value + syms[mid]->section->vma < value)
2121 else if (syms[mid]->value + syms[mid]->section->vma > value)
2131 mid = (lo + hi) >> 1;
2132 if (syms[mid]->section->id < id)
2134 else if (syms[mid]->section->id > id)
2136 else if (syms[mid]->value < value)
2138 else if (syms[mid]->value > value)
2148 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2150 bfd_vma vma = *(bfd_vma *) ptr;
2151 return ((section->flags & SEC_ALLOC) != 0
2152 && section->vma <= vma
2153 && vma < section->vma + section->size);
2156 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2157 entry syms. Also generate @plt symbols for the glink branch table.
2158 Returns count of synthetic symbols in RET or -1 on error. */
2161 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2162 long static_count, asymbol **static_syms,
2163 long dyn_count, asymbol **dyn_syms,
2169 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2170 asection *opd = NULL;
2171 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2173 int abi = abiversion (abfd);
2179 opd = bfd_get_section_by_name (abfd, ".opd");
2180 if (opd == NULL && abi == 1)
2192 symcount = static_count;
2194 symcount += dyn_count;
2198 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2202 if (!relocatable && static_count != 0 && dyn_count != 0)
2204 /* Use both symbol tables. */
2205 memcpy (syms, static_syms, static_count * sizeof (*syms));
2206 memcpy (syms + static_count, dyn_syms,
2207 (dyn_count + 1) * sizeof (*syms));
2209 else if (!relocatable && static_count == 0)
2210 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2212 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2214 /* Trim uninteresting symbols. Interesting symbols are section,
2215 function, and notype symbols. */
2216 for (i = 0, j = 0; i < symcount; ++i)
2217 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2218 | BSF_RELC | BSF_SRELC)) == 0)
2219 syms[j++] = syms[i];
2222 synthetic_relocatable = relocatable;
2223 synthetic_opd = opd;
2224 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2226 if (!relocatable && symcount > 1)
2228 /* Trim duplicate syms, since we may have merged the normal
2229 and dynamic symbols. Actually, we only care about syms
2230 that have different values, so trim any with the same
2231 value. Don't consider ifunc and ifunc resolver symbols
2232 duplicates however, because GDB wants to know whether a
2233 text symbol is an ifunc resolver. */
2234 for (i = 1, j = 1; i < symcount; ++i)
2236 const asymbol *s0 = syms[i - 1];
2237 const asymbol *s1 = syms[i];
2239 if ((s0->value + s0->section->vma
2240 != s1->value + s1->section->vma)
2241 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2242 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2243 syms[j++] = syms[i];
2249 /* Note that here and in compare_symbols we can't compare opd and
2250 sym->section directly. With separate debug info files, the
2251 symbols will be extracted from the debug file while abfd passed
2252 to this function is the real binary. */
2253 if (strcmp (syms[i]->section->name, ".opd") == 0)
2257 for (; i < symcount; ++i)
2258 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2259 | SEC_THREAD_LOCAL))
2260 != (SEC_CODE | SEC_ALLOC))
2261 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2265 for (; i < symcount; ++i)
2266 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2270 for (; i < symcount; ++i)
2271 if (strcmp (syms[i]->section->name, ".opd") != 0)
2275 for (; i < symcount; ++i)
2276 if (((syms[i]->section->flags
2277 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2278 != (SEC_CODE | SEC_ALLOC))
2286 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2291 if (opdsymend == secsymend)
2294 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2295 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2299 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2306 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2310 while (r < opd->relocation + relcount
2311 && r->address < syms[i]->value + opd->vma)
2314 if (r == opd->relocation + relcount)
2317 if (r->address != syms[i]->value + opd->vma)
2320 if (r->howto->type != R_PPC64_ADDR64)
2323 sym = *r->sym_ptr_ptr;
2324 if (!sym_exists_at (syms, opdsymend, symcount,
2325 sym->section->id, sym->value + r->addend))
2328 size += sizeof (asymbol);
2329 size += strlen (syms[i]->name) + 2;
2335 s = *ret = bfd_malloc (size);
2342 names = (char *) (s + count);
2344 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2348 while (r < opd->relocation + relcount
2349 && r->address < syms[i]->value + opd->vma)
2352 if (r == opd->relocation + relcount)
2355 if (r->address != syms[i]->value + opd->vma)
2358 if (r->howto->type != R_PPC64_ADDR64)
2361 sym = *r->sym_ptr_ptr;
2362 if (!sym_exists_at (syms, opdsymend, symcount,
2363 sym->section->id, sym->value + r->addend))
2368 s->flags |= BSF_SYNTHETIC;
2369 s->section = sym->section;
2370 s->value = sym->value + r->addend;
2373 len = strlen (syms[i]->name);
2374 memcpy (names, syms[i]->name, len + 1);
2376 /* Have udata.p point back to the original symbol this
2377 synthetic symbol was derived from. */
2378 s->udata.p = syms[i];
2385 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2386 bfd_byte *contents = NULL;
2388 size_t plt_count = 0;
2389 bfd_vma glink_vma = 0, resolv_vma = 0;
2390 asection *dynamic, *glink = NULL, *relplt = NULL;
2393 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2395 free_contents_and_exit_err:
2397 free_contents_and_exit:
2404 for (i = secsymend; i < opdsymend; ++i)
2408 /* Ignore bogus symbols. */
2409 if (syms[i]->value > opd->size - 8)
2412 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2413 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2416 size += sizeof (asymbol);
2417 size += strlen (syms[i]->name) + 2;
2421 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2423 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2425 bfd_byte *dynbuf, *extdyn, *extdynend;
2427 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2429 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2430 goto free_contents_and_exit_err;
2432 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2433 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2436 extdynend = extdyn + dynamic->size;
2437 for (; extdyn < extdynend; extdyn += extdynsize)
2439 Elf_Internal_Dyn dyn;
2440 (*swap_dyn_in) (abfd, extdyn, &dyn);
2442 if (dyn.d_tag == DT_NULL)
2445 if (dyn.d_tag == DT_PPC64_GLINK)
2447 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2448 See comment in ppc64_elf_finish_dynamic_sections. */
2449 glink_vma = dyn.d_un.d_val + 8 * 4;
2450 /* The .glink section usually does not survive the final
2451 link; search for the section (usually .text) where the
2452 glink stubs now reside. */
2453 glink = bfd_sections_find_if (abfd, section_covers_vma,
2464 /* Determine __glink trampoline by reading the relative branch
2465 from the first glink stub. */
2467 unsigned int off = 0;
2469 while (bfd_get_section_contents (abfd, glink, buf,
2470 glink_vma + off - glink->vma, 4))
2472 unsigned int insn = bfd_get_32 (abfd, buf);
2474 if ((insn & ~0x3fffffc) == 0)
2477 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2486 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2488 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2491 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2492 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
2493 goto free_contents_and_exit_err;
2495 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2496 size += plt_count * sizeof (asymbol);
2498 p = relplt->relocation;
2499 for (i = 0; i < plt_count; i++, p++)
2501 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2503 size += sizeof ("+0x") - 1 + 16;
2509 goto free_contents_and_exit;
2510 s = *ret = bfd_malloc (size);
2512 goto free_contents_and_exit_err;
2514 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2516 for (i = secsymend; i < opdsymend; ++i)
2520 if (syms[i]->value > opd->size - 8)
2523 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2524 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2528 asection *sec = abfd->sections;
2535 size_t mid = (lo + hi) >> 1;
2536 if (syms[mid]->section->vma < ent)
2538 else if (syms[mid]->section->vma > ent)
2542 sec = syms[mid]->section;
2547 if (lo >= hi && lo > codesecsym)
2548 sec = syms[lo - 1]->section;
2550 for (; sec != NULL; sec = sec->next)
2554 /* SEC_LOAD may not be set if SEC is from a separate debug
2556 if ((sec->flags & SEC_ALLOC) == 0)
2558 if ((sec->flags & SEC_CODE) != 0)
2561 s->flags |= BSF_SYNTHETIC;
2562 s->value = ent - s->section->vma;
2565 len = strlen (syms[i]->name);
2566 memcpy (names, syms[i]->name, len + 1);
2568 /* Have udata.p point back to the original symbol this
2569 synthetic symbol was derived from. */
2570 s->udata.p = syms[i];
2576 if (glink != NULL && relplt != NULL)
2580 /* Add a symbol for the main glink trampoline. */
2581 memset (s, 0, sizeof *s);
2583 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2585 s->value = resolv_vma - glink->vma;
2587 memcpy (names, "__glink_PLTresolve",
2588 sizeof ("__glink_PLTresolve"));
2589 names += sizeof ("__glink_PLTresolve");
2594 /* FIXME: It would be very much nicer to put sym@plt on the
2595 stub rather than on the glink branch table entry. The
2596 objdump disassembler would then use a sensible symbol
2597 name on plt calls. The difficulty in doing so is
2598 a) finding the stubs, and,
2599 b) matching stubs against plt entries, and,
2600 c) there can be multiple stubs for a given plt entry.
2602 Solving (a) could be done by code scanning, but older
2603 ppc64 binaries used different stubs to current code.
2604 (b) is the tricky one since you need to known the toc
2605 pointer for at least one function that uses a pic stub to
2606 be able to calculate the plt address referenced.
2607 (c) means gdb would need to set multiple breakpoints (or
2608 find the glink branch itself) when setting breakpoints
2609 for pending shared library loads. */
2610 p = relplt->relocation;
2611 for (i = 0; i < plt_count; i++, p++)
2615 *s = **p->sym_ptr_ptr;
2616 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2617 we are defining a symbol, ensure one of them is set. */
2618 if ((s->flags & BSF_LOCAL) == 0)
2619 s->flags |= BSF_GLOBAL;
2620 s->flags |= BSF_SYNTHETIC;
2622 s->value = glink_vma - glink->vma;
2625 len = strlen ((*p->sym_ptr_ptr)->name);
2626 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2630 memcpy (names, "+0x", sizeof ("+0x") - 1);
2631 names += sizeof ("+0x") - 1;
2632 bfd_sprintf_vma (abfd, names, p->addend);
2633 names += strlen (names);
2635 memcpy (names, "@plt", sizeof ("@plt"));
2636 names += sizeof ("@plt");
2656 /* The following functions are specific to the ELF linker, while
2657 functions above are used generally. Those named ppc64_elf_* are
2658 called by the main ELF linker code. They appear in this file more
2659 or less in the order in which they are called. eg.
2660 ppc64_elf_check_relocs is called early in the link process,
2661 ppc64_elf_finish_dynamic_sections is one of the last functions
2664 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2665 functions have both a function code symbol and a function descriptor
2666 symbol. A call to foo in a relocatable object file looks like:
2673 The function definition in another object file might be:
2677 . .quad .TOC.@tocbase
2683 When the linker resolves the call during a static link, the branch
2684 unsurprisingly just goes to .foo and the .opd information is unused.
2685 If the function definition is in a shared library, things are a little
2686 different: The call goes via a plt call stub, the opd information gets
2687 copied to the plt, and the linker patches the nop.
2695 . std 2,40(1) # in practice, the call stub
2696 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2697 . addi 11,11,Lfoo@toc@l # this is the general idea
2705 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2707 The "reloc ()" notation is supposed to indicate that the linker emits
2708 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2711 What are the difficulties here? Well, firstly, the relocations
2712 examined by the linker in check_relocs are against the function code
2713 sym .foo, while the dynamic relocation in the plt is emitted against
2714 the function descriptor symbol, foo. Somewhere along the line, we need
2715 to carefully copy dynamic link information from one symbol to the other.
2716 Secondly, the generic part of the elf linker will make .foo a dynamic
2717 symbol as is normal for most other backends. We need foo dynamic
2718 instead, at least for an application final link. However, when
2719 creating a shared library containing foo, we need to have both symbols
2720 dynamic so that references to .foo are satisfied during the early
2721 stages of linking. Otherwise the linker might decide to pull in a
2722 definition from some other object, eg. a static library.
2724 Update: As of August 2004, we support a new convention. Function
2725 calls may use the function descriptor symbol, ie. "bl foo". This
2726 behaves exactly as "bl .foo". */
2728 /* Of those relocs that might be copied as dynamic relocs, this
2729 function selects those that must be copied when linking a shared
2730 library or PIE, even when the symbol is local. */
2733 must_be_dyn_reloc (struct bfd_link_info *info,
2734 enum elf_ppc64_reloc_type r_type)
2739 /* Only relative relocs can be resolved when the object load
2740 address isn't fixed. DTPREL64 is excluded because the
2741 dynamic linker needs to differentiate global dynamic from
2742 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2750 case R_PPC64_TPREL16:
2751 case R_PPC64_TPREL16_LO:
2752 case R_PPC64_TPREL16_HI:
2753 case R_PPC64_TPREL16_HA:
2754 case R_PPC64_TPREL16_DS:
2755 case R_PPC64_TPREL16_LO_DS:
2756 case R_PPC64_TPREL16_HIGH:
2757 case R_PPC64_TPREL16_HIGHA:
2758 case R_PPC64_TPREL16_HIGHER:
2759 case R_PPC64_TPREL16_HIGHERA:
2760 case R_PPC64_TPREL16_HIGHEST:
2761 case R_PPC64_TPREL16_HIGHESTA:
2762 case R_PPC64_TPREL64:
2763 case R_PPC64_TPREL34:
2764 /* These relocations are relative but in a shared library the
2765 linker doesn't know the thread pointer base. */
2766 return bfd_link_dll (info);
2770 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2771 copying dynamic variables from a shared lib into an app's dynbss
2772 section, and instead use a dynamic relocation to point into the
2773 shared lib. With code that gcc generates, it's vital that this be
2774 enabled; In the PowerPC64 ABI, the address of a function is actually
2775 the address of a function descriptor, which resides in the .opd
2776 section. gcc uses the descriptor directly rather than going via the
2777 GOT as some other ABI's do, which means that initialized function
2778 pointers must reference the descriptor. Thus, a function pointer
2779 initialized to the address of a function in a shared library will
2780 either require a copy reloc, or a dynamic reloc. Using a copy reloc
2781 redefines the function descriptor symbol to point to the copy. This
2782 presents a problem as a plt entry for that function is also
2783 initialized from the function descriptor symbol and the copy reloc
2784 may not be initialized first. */
2785 #define ELIMINATE_COPY_RELOCS 1
2787 /* Section name for stubs is the associated section name plus this
2789 #define STUB_SUFFIX ".stub"
2792 ppc_stub_long_branch:
2793 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2794 destination, but a 24 bit branch in a stub section will reach.
2797 ppc_stub_plt_branch:
2798 Similar to the above, but a 24 bit branch in the stub section won't
2799 reach its destination.
2800 . addis %r11,%r2,xxx@toc@ha
2801 . ld %r12,xxx@toc@l(%r11)
2806 Used to call a function in a shared library. If it so happens that
2807 the plt entry referenced crosses a 64k boundary, then an extra
2808 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2809 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
2810 . addis %r11,%r2,xxx@toc@ha
2811 . ld %r12,xxx+0@toc@l(%r11)
2813 . ld %r2,xxx+8@toc@l(%r11)
2814 . ld %r11,xxx+16@toc@l(%r11)
2817 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2818 code to adjust the value and save r2 to support multiple toc sections.
2819 A ppc_stub_long_branch with an r2 offset looks like:
2821 . addis %r2,%r2,off@ha
2822 . addi %r2,%r2,off@l
2825 A ppc_stub_plt_branch with an r2 offset looks like:
2827 . addis %r11,%r2,xxx@toc@ha
2828 . ld %r12,xxx@toc@l(%r11)
2829 . addis %r2,%r2,off@ha
2830 . addi %r2,%r2,off@l
2834 All of the above stubs are shown as their ELFv1 variants. ELFv2
2835 variants exist too, simpler for plt calls since a new toc pointer
2836 and static chain are not loaded by the stub. In addition, ELFv2
2837 has some more complex stubs to handle calls marked with NOTOC
2838 relocs from functions where r2 is not a valid toc pointer. These
2839 come in two flavours, the ones shown below, and _both variants that
2840 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2841 one call is from a function where r2 is used as the toc pointer but
2842 needs a toc adjusting stub for small-model multi-toc, and another
2843 call is from a function where r2 is not valid.
2844 ppc_stub_long_branch_notoc:
2850 . addis %r12,%r11,dest-1b@ha
2851 . addi %r12,%r12,dest-1b@l
2854 ppc_stub_plt_branch_notoc:
2860 . lis %r12,xxx-1b@highest
2861 . ori %r12,%r12,xxx-1b@higher
2863 . oris %r12,%r12,xxx-1b@high
2864 . ori %r12,%r12,xxx-1b@l
2865 . add %r12,%r11,%r12
2869 ppc_stub_plt_call_notoc:
2875 . lis %r12,xxx-1b@highest
2876 . ori %r12,%r12,xxx-1b@higher
2878 . oris %r12,%r12,xxx-1b@high
2879 . ori %r12,%r12,xxx-1b@l
2880 . ldx %r12,%r11,%r12
2884 There are also ELFv1 powerxx variants of these stubs.
2885 ppc_stub_long_branch_notoc:
2886 . pla %r12,dest@pcrel
2888 ppc_stub_plt_branch_notoc:
2889 . lis %r11,(dest-1f)@highesta34
2890 . ori %r11,%r11,(dest-1f)@highera34
2892 . 1: pla %r12,dest@pcrel
2893 . add %r12,%r11,%r12
2896 ppc_stub_plt_call_notoc:
2897 . lis %r11,(xxx-1f)@highesta34
2898 . ori %r11,%r11,(xxx-1f)@highera34
2900 . 1: pla %r12,xxx@pcrel
2901 . ldx %r12,%r11,%r12
2905 In cases where the high instructions would add zero, they are
2906 omitted and following instructions modified in some cases.
2907 For example, a powerxx ppc_stub_plt_call_notoc might simplify down
2909 . pld %r12,xxx@pcrel
2913 For a given stub group (a set of sections all using the same toc
2914 pointer value) there will be just one stub type used for any
2915 particular function symbol. For example, if printf is called from
2916 code with the tocsave optimization (ie. r2 saved in function
2917 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2918 and from other code without the tocsave optimization requiring a
2919 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2920 type will be created. Calls with the tocsave optimization will
2921 enter this stub after the instruction saving r2. A similar
2922 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2923 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2924 to call an external function like printf. If other calls to printf
2925 require a ppc_stub_plt_call linkage stub then a single
2926 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2927 call. If other calls to printf require a ppc_stub_plt_call_r2save
2928 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2929 be created and calls not requiring r2 to be saved will enter the
2930 stub after the r2 save instruction. There is an analogous
2931 hierarchy of long branch and plt branch stubs for local call
2937 ppc_stub_long_branch,
2938 ppc_stub_long_branch_r2off,
2939 ppc_stub_long_branch_notoc,
2940 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
2941 ppc_stub_plt_branch,
2942 ppc_stub_plt_branch_r2off,
2943 ppc_stub_plt_branch_notoc,
2944 ppc_stub_plt_branch_both,
2946 ppc_stub_plt_call_r2save,
2947 ppc_stub_plt_call_notoc,
2948 ppc_stub_plt_call_both,
2949 ppc_stub_global_entry,
2953 /* Information on stub grouping. */
2956 /* The stub section. */
2958 /* This is the section to which stubs in the group will be attached. */
2961 struct map_stub *next;
2962 /* Whether to emit a copy of register save/restore functions in this
2965 /* Current offset within stubs after the insn restoring lr in a
2966 _notoc or _both stub using bcl for pc-relative addressing, or
2967 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2968 unsigned int lr_restore;
2969 /* Accumulated size of EH info emitted to describe return address
2970 if stubs modify lr. Does not include 17 byte FDE header. */
2971 unsigned int eh_size;
2972 /* Offset in glink_eh_frame to the start of EH info for this group. */
2973 unsigned int eh_base;
2976 struct ppc_stub_hash_entry
2978 /* Base hash table entry structure. */
2979 struct bfd_hash_entry root;
2981 enum ppc_stub_type stub_type;
2983 /* Group information. */
2984 struct map_stub *group;
2986 /* Offset within stub_sec of the beginning of this stub. */
2987 bfd_vma stub_offset;
2989 /* Given the symbol's value and its section we can determine its final
2990 value when building the stubs (so the stub knows where to jump. */
2991 bfd_vma target_value;
2992 asection *target_section;
2994 /* The symbol table entry, if any, that this was derived from. */
2995 struct ppc_link_hash_entry *h;
2996 struct plt_entry *plt_ent;
2999 unsigned char symtype;
3001 /* Symbol st_other. */
3002 unsigned char other;
3005 struct ppc_branch_hash_entry
3007 /* Base hash table entry structure. */
3008 struct bfd_hash_entry root;
3010 /* Offset within branch lookup table. */
3011 unsigned int offset;
3013 /* Generation marker. */
3017 /* Used to track dynamic relocations for local symbols. */
3018 struct ppc_dyn_relocs
3020 struct ppc_dyn_relocs *next;
3022 /* The input section of the reloc. */
3025 /* Total number of relocs copied for the input section. */
3026 unsigned int count : 31;
3028 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3029 unsigned int ifunc : 1;
3032 struct ppc_link_hash_entry
3034 struct elf_link_hash_entry elf;
3038 /* A pointer to the most recently used stub hash entry against this
3040 struct ppc_stub_hash_entry *stub_cache;
3042 /* A pointer to the next symbol starting with a '.' */
3043 struct ppc_link_hash_entry *next_dot_sym;
3046 /* Track dynamic relocs copied for this symbol. */
3047 struct elf_dyn_relocs *dyn_relocs;
3049 /* Link between function code and descriptor symbols. */
3050 struct ppc_link_hash_entry *oh;
3052 /* Flag function code and descriptor symbols. */
3053 unsigned int is_func:1;
3054 unsigned int is_func_descriptor:1;
3055 unsigned int fake:1;
3057 /* Whether global opd/toc sym has been adjusted or not.
3058 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3059 should be set for all globals defined in any opd/toc section. */
3060 unsigned int adjust_done:1;
3062 /* Set if this is an out-of-line register save/restore function,
3063 with non-standard calling convention. */
3064 unsigned int save_res:1;
3066 /* Set if a duplicate symbol with non-zero localentry is detected,
3067 even when the duplicate symbol does not provide a definition. */
3068 unsigned int non_zero_localentry:1;
3070 /* Contexts in which symbol is used in the GOT (or TOC).
3071 Bits are or'd into the mask as the corresponding relocs are
3072 encountered during check_relocs, with TLS_TLS being set when any
3073 of the other TLS bits are set. tls_optimize clears bits when
3074 optimizing to indicate the corresponding GOT entry type is not
3075 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3076 set TLS_GDIE when a GD reloc turns into an IE one.
3077 These flags are also kept for local symbols. */
3078 #define TLS_TLS 1 /* Any TLS reloc. */
3079 #define TLS_GD 2 /* GD reloc. */
3080 #define TLS_LD 4 /* LD reloc. */
3081 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
3082 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3083 #define TLS_MARK 32 /* __tls_get_addr call marked. */
3084 #define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
3085 #define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
3086 unsigned char tls_mask;
3088 /* The above field is also used to mark function symbols. In which
3089 case TLS_TLS will be 0. */
3090 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
3091 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
3092 #define NON_GOT 256 /* local symbol plt, not stored. */
3095 /* ppc64 ELF linker hash table. */
3097 struct ppc_link_hash_table
3099 struct elf_link_hash_table elf;
3101 /* The stub hash table. */
3102 struct bfd_hash_table stub_hash_table;
3104 /* Another hash table for plt_branch stubs. */
3105 struct bfd_hash_table branch_hash_table;
3107 /* Hash table for function prologue tocsave. */
3108 htab_t tocsave_htab;
3110 /* Various options and other info passed from the linker. */
3111 struct ppc64_elf_params *params;
3113 /* The size of sec_info below. */
3114 unsigned int sec_info_arr_size;
3116 /* Per-section array of extra section info. Done this way rather
3117 than as part of ppc64_elf_section_data so we have the info for
3118 non-ppc64 sections. */
3121 /* Along with elf_gp, specifies the TOC pointer used by this section. */
3126 /* The section group that this section belongs to. */
3127 struct map_stub *group;
3128 /* A temp section list pointer. */
3133 /* Linked list of groups. */
3134 struct map_stub *group;
3136 /* Temp used when calculating TOC pointers. */
3139 asection *toc_first_sec;
3141 /* Used when adding symbols. */
3142 struct ppc_link_hash_entry *dot_syms;
3144 /* Shortcuts to get to dynamic linker sections. */
3146 asection *global_entry;
3149 asection *relpltlocal;
3152 asection *glink_eh_frame;
3154 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3155 struct ppc_link_hash_entry *tls_get_addr;
3156 struct ppc_link_hash_entry *tls_get_addr_fd;
3158 /* The size of reliplt used by got entry relocs. */
3159 bfd_size_type got_reli_size;
3162 unsigned long stub_count[ppc_stub_global_entry];
3164 /* Number of stubs against global syms. */
3165 unsigned long stub_globals;
3167 /* Set if we're linking code with function descriptors. */
3168 unsigned int opd_abi:1;
3170 /* Support for multiple toc sections. */
3171 unsigned int do_multi_toc:1;
3172 unsigned int multi_toc_needed:1;
3173 unsigned int second_toc_pass:1;
3174 unsigned int do_toc_opt:1;
3176 /* Set if tls optimization is enabled. */
3177 unsigned int do_tls_opt:1;
3179 /* Set if inline plt calls should be converted to direct calls. */
3180 unsigned int can_convert_all_inline_plt:1;
3183 unsigned int stub_error:1;
3185 /* Whether func_desc_adjust needs to be run over symbols. */
3186 unsigned int need_func_desc_adj:1;
3188 /* Whether there exist local gnu indirect function resolvers,
3189 referenced by dynamic relocations. */
3190 unsigned int local_ifunc_resolver:1;
3191 unsigned int maybe_local_ifunc_resolver:1;
3193 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3194 unsigned int has_plt_localentry0:1;
3196 /* Whether calls are made via the PLT from NOTOC functions. */
3197 unsigned int notoc_plt:1;
3199 /* Whether to use powerxx instructions in linkage stubs. */
3200 unsigned int powerxx_stubs:1;
3202 /* Incremented every time we size stubs. */
3203 unsigned int stub_iteration;
3205 /* Small local sym cache. */
3206 struct sym_cache sym_cache;
3209 /* Rename some of the generic section flags to better document how they
3212 /* Nonzero if this section has TLS related relocations. */
3213 #define has_tls_reloc sec_flg0
3215 /* Nonzero if this section has an old-style call to __tls_get_addr. */
3216 #define has_tls_get_addr_call sec_flg1
3218 /* Nonzero if this section has any toc or got relocs. */
3219 #define has_toc_reloc sec_flg2
3221 /* Nonzero if this section has a call to another section that uses
3223 #define makes_toc_func_call sec_flg3
3225 /* Recursion protection when determining above flag. */
3226 #define call_check_in_progress sec_flg4
3227 #define call_check_done sec_flg5
3229 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3231 #define ppc_hash_table(p) \
3232 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3233 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3235 #define ppc_stub_hash_lookup(table, string, create, copy) \
3236 ((struct ppc_stub_hash_entry *) \
3237 bfd_hash_lookup ((table), (string), (create), (copy)))
3239 #define ppc_branch_hash_lookup(table, string, create, copy) \
3240 ((struct ppc_branch_hash_entry *) \
3241 bfd_hash_lookup ((table), (string), (create), (copy)))
3243 /* Create an entry in the stub hash table. */
3245 static struct bfd_hash_entry *
3246 stub_hash_newfunc (struct bfd_hash_entry *entry,
3247 struct bfd_hash_table *table,
3250 /* Allocate the structure if it has not already been allocated by a
3254 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3259 /* Call the allocation method of the superclass. */
3260 entry = bfd_hash_newfunc (entry, table, string);
3263 struct ppc_stub_hash_entry *eh;
3265 /* Initialize the local fields. */
3266 eh = (struct ppc_stub_hash_entry *) entry;
3267 eh->stub_type = ppc_stub_none;
3269 eh->stub_offset = 0;
3270 eh->target_value = 0;
3271 eh->target_section = NULL;
3280 /* Create an entry in the branch hash table. */
3282 static struct bfd_hash_entry *
3283 branch_hash_newfunc (struct bfd_hash_entry *entry,
3284 struct bfd_hash_table *table,
3287 /* Allocate the structure if it has not already been allocated by a
3291 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3296 /* Call the allocation method of the superclass. */
3297 entry = bfd_hash_newfunc (entry, table, string);
3300 struct ppc_branch_hash_entry *eh;
3302 /* Initialize the local fields. */
3303 eh = (struct ppc_branch_hash_entry *) entry;
3311 /* Create an entry in a ppc64 ELF linker hash table. */
3313 static struct bfd_hash_entry *
3314 link_hash_newfunc (struct bfd_hash_entry *entry,
3315 struct bfd_hash_table *table,
3318 /* Allocate the structure if it has not already been allocated by a
3322 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3327 /* Call the allocation method of the superclass. */
3328 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3331 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3333 memset (&eh->u.stub_cache, 0,
3334 (sizeof (struct ppc_link_hash_entry)
3335 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3337 /* When making function calls, old ABI code references function entry
3338 points (dot symbols), while new ABI code references the function
3339 descriptor symbol. We need to make any combination of reference and
3340 definition work together, without breaking archive linking.
3342 For a defined function "foo" and an undefined call to "bar":
3343 An old object defines "foo" and ".foo", references ".bar" (possibly
3345 A new object defines "foo" and references "bar".
3347 A new object thus has no problem with its undefined symbols being
3348 satisfied by definitions in an old object. On the other hand, the
3349 old object won't have ".bar" satisfied by a new object.
3351 Keep a list of newly added dot-symbols. */
3353 if (string[0] == '.')
3355 struct ppc_link_hash_table *htab;
3357 htab = (struct ppc_link_hash_table *) table;
3358 eh->u.next_dot_sym = htab->dot_syms;
3359 htab->dot_syms = eh;
3366 struct tocsave_entry
3373 tocsave_htab_hash (const void *p)
3375 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3376 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3380 tocsave_htab_eq (const void *p1, const void *p2)
3382 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3383 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3384 return e1->sec == e2->sec && e1->offset == e2->offset;
3387 /* Destroy a ppc64 ELF linker hash table. */
3390 ppc64_elf_link_hash_table_free (bfd *obfd)
3392 struct ppc_link_hash_table *htab;
3394 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3395 if (htab->tocsave_htab)
3396 htab_delete (htab->tocsave_htab);
3397 bfd_hash_table_free (&htab->branch_hash_table);
3398 bfd_hash_table_free (&htab->stub_hash_table);
3399 _bfd_elf_link_hash_table_free (obfd);
3402 /* Create a ppc64 ELF linker hash table. */
3404 static struct bfd_link_hash_table *
3405 ppc64_elf_link_hash_table_create (bfd *abfd)
3407 struct ppc_link_hash_table *htab;
3408 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3410 htab = bfd_zmalloc (amt);
3414 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3415 sizeof (struct ppc_link_hash_entry),
3422 /* Init the stub hash table too. */
3423 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3424 sizeof (struct ppc_stub_hash_entry)))
3426 _bfd_elf_link_hash_table_free (abfd);
3430 /* And the branch hash table. */
3431 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3432 sizeof (struct ppc_branch_hash_entry)))
3434 bfd_hash_table_free (&htab->stub_hash_table);
3435 _bfd_elf_link_hash_table_free (abfd);
3439 htab->tocsave_htab = htab_try_create (1024,
3443 if (htab->tocsave_htab == NULL)
3445 ppc64_elf_link_hash_table_free (abfd);
3448 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3450 /* Initializing two fields of the union is just cosmetic. We really
3451 only care about glist, but when compiled on a 32-bit host the
3452 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3453 debugger inspection of these fields look nicer. */
3454 htab->elf.init_got_refcount.refcount = 0;
3455 htab->elf.init_got_refcount.glist = NULL;
3456 htab->elf.init_plt_refcount.refcount = 0;
3457 htab->elf.init_plt_refcount.glist = NULL;
3458 htab->elf.init_got_offset.offset = 0;
3459 htab->elf.init_got_offset.glist = NULL;
3460 htab->elf.init_plt_offset.offset = 0;
3461 htab->elf.init_plt_offset.glist = NULL;
3463 return &htab->elf.root;
3466 /* Create sections for linker generated code. */
3469 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3471 struct ppc_link_hash_table *htab;
3474 htab = ppc_hash_table (info);
3476 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3477 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3478 if (htab->params->save_restore_funcs)
3480 /* Create .sfpr for code to save and restore fp regs. */
3481 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3483 if (htab->sfpr == NULL
3484 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3488 if (bfd_link_relocatable (info))
3491 /* Create .glink for lazy dynamic linking support. */
3492 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3494 if (htab->glink == NULL
3495 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
3498 /* The part of .glink used by global entry stubs, separate so that
3499 it can be aligned appropriately without affecting htab->glink. */
3500 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3502 if (htab->global_entry == NULL
3503 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
3506 if (!info->no_ld_generated_unwind_info)
3508 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3509 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3510 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3513 if (htab->glink_eh_frame == NULL
3514 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3518 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3519 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3520 if (htab->elf.iplt == NULL
3521 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
3524 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3525 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3527 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3528 if (htab->elf.irelplt == NULL
3529 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
3532 /* Create branch lookup table for plt_branch stubs. */
3533 flags = (SEC_ALLOC | SEC_LOAD
3534 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3535 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3537 if (htab->brlt == NULL
3538 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
3541 /* Local plt entries, put in .branch_lt but a separate section for
3543 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3545 if (htab->pltlocal == NULL
3546 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
3549 if (!bfd_link_pic (info))
3552 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3553 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3555 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3556 if (htab->relbrlt == NULL
3557 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3561 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3562 if (htab->relpltlocal == NULL
3563 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
3569 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3572 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3573 struct ppc64_elf_params *params)
3575 struct ppc_link_hash_table *htab;
3577 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3579 /* Always hook our dynamic sections into the first bfd, which is the
3580 linker created stub bfd. This ensures that the GOT header is at
3581 the start of the output TOC section. */
3582 htab = ppc_hash_table (info);
3583 htab->elf.dynobj = params->stub_bfd;
3584 htab->params = params;
3586 return create_linkage_sections (htab->elf.dynobj, info);
3589 /* Build a name for an entry in the stub hash table. */
3592 ppc_stub_name (const asection *input_section,
3593 const asection *sym_sec,
3594 const struct ppc_link_hash_entry *h,
3595 const Elf_Internal_Rela *rel)
3600 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3601 offsets from a sym as a branch target? In fact, we could
3602 probably assume the addend is always zero. */
3603 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3607 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3608 stub_name = bfd_malloc (len);
3609 if (stub_name == NULL)
3612 len = sprintf (stub_name, "%08x.%s+%x",
3613 input_section->id & 0xffffffff,
3614 h->elf.root.root.string,
3615 (int) rel->r_addend & 0xffffffff);
3619 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3620 stub_name = bfd_malloc (len);
3621 if (stub_name == NULL)
3624 len = sprintf (stub_name, "%08x.%x:%x+%x",
3625 input_section->id & 0xffffffff,
3626 sym_sec->id & 0xffffffff,
3627 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3628 (int) rel->r_addend & 0xffffffff);
3630 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3631 stub_name[len - 2] = 0;
3635 /* Look up an entry in the stub hash. Stub entries are cached because
3636 creating the stub name takes a bit of time. */
3638 static struct ppc_stub_hash_entry *
3639 ppc_get_stub_entry (const asection *input_section,
3640 const asection *sym_sec,
3641 struct ppc_link_hash_entry *h,
3642 const Elf_Internal_Rela *rel,
3643 struct ppc_link_hash_table *htab)
3645 struct ppc_stub_hash_entry *stub_entry;
3646 struct map_stub *group;
3648 /* If this input section is part of a group of sections sharing one
3649 stub section, then use the id of the first section in the group.
3650 Stub names need to include a section id, as there may well be
3651 more than one stub used to reach say, printf, and we need to
3652 distinguish between them. */
3653 group = htab->sec_info[input_section->id].u.group;
3657 if (h != NULL && h->u.stub_cache != NULL
3658 && h->u.stub_cache->h == h
3659 && h->u.stub_cache->group == group)
3661 stub_entry = h->u.stub_cache;
3667 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3668 if (stub_name == NULL)
3671 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3672 stub_name, FALSE, FALSE);
3674 h->u.stub_cache = stub_entry;
3682 /* Add a new stub entry to the stub hash. Not all fields of the new
3683 stub entry are initialised. */
3685 static struct ppc_stub_hash_entry *
3686 ppc_add_stub (const char *stub_name,
3688 struct bfd_link_info *info)
3690 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3691 struct map_stub *group;
3694 struct ppc_stub_hash_entry *stub_entry;
3696 group = htab->sec_info[section->id].u.group;
3697 link_sec = group->link_sec;
3698 stub_sec = group->stub_sec;
3699 if (stub_sec == NULL)
3705 namelen = strlen (link_sec->name);
3706 len = namelen + sizeof (STUB_SUFFIX);
3707 s_name = bfd_alloc (htab->params->stub_bfd, len);
3711 memcpy (s_name, link_sec->name, namelen);
3712 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3713 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3714 if (stub_sec == NULL)
3716 group->stub_sec = stub_sec;
3719 /* Enter this entry into the linker stub hash table. */
3720 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3722 if (stub_entry == NULL)
3724 /* xgettext:c-format */
3725 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3726 section->owner, stub_name);
3730 stub_entry->group = group;
3731 stub_entry->stub_offset = 0;
3735 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3736 not already done. */
3739 create_got_section (bfd *abfd, struct bfd_link_info *info)
3741 asection *got, *relgot;
3743 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3745 if (!is_ppc64_elf (abfd))
3751 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3754 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3755 | SEC_LINKER_CREATED);
3757 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3759 || !bfd_set_section_alignment (abfd, got, 3))
3762 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3763 flags | SEC_READONLY);
3765 || !bfd_set_section_alignment (abfd, relgot, 3))
3768 ppc64_elf_tdata (abfd)->got = got;
3769 ppc64_elf_tdata (abfd)->relgot = relgot;
3773 /* Follow indirect and warning symbol links. */
3775 static inline struct bfd_link_hash_entry *
3776 follow_link (struct bfd_link_hash_entry *h)
3778 while (h->type == bfd_link_hash_indirect
3779 || h->type == bfd_link_hash_warning)
3784 static inline struct elf_link_hash_entry *
3785 elf_follow_link (struct elf_link_hash_entry *h)
3787 return (struct elf_link_hash_entry *) follow_link (&h->root);
3790 static inline struct ppc_link_hash_entry *
3791 ppc_follow_link (struct ppc_link_hash_entry *h)
3793 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3796 /* Merge PLT info on FROM with that on TO. */
3799 move_plt_plist (struct ppc_link_hash_entry *from,
3800 struct ppc_link_hash_entry *to)
3802 if (from->elf.plt.plist != NULL)
3804 if (to->elf.plt.plist != NULL)
3806 struct plt_entry **entp;
3807 struct plt_entry *ent;
3809 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3811 struct plt_entry *dent;
3813 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3814 if (dent->addend == ent->addend)
3816 dent->plt.refcount += ent->plt.refcount;
3823 *entp = to->elf.plt.plist;
3826 to->elf.plt.plist = from->elf.plt.plist;
3827 from->elf.plt.plist = NULL;
3831 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3834 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3835 struct elf_link_hash_entry *dir,
3836 struct elf_link_hash_entry *ind)
3838 struct ppc_link_hash_entry *edir, *eind;
3840 edir = (struct ppc_link_hash_entry *) dir;
3841 eind = (struct ppc_link_hash_entry *) ind;
3843 edir->is_func |= eind->is_func;
3844 edir->is_func_descriptor |= eind->is_func_descriptor;
3845 edir->tls_mask |= eind->tls_mask;
3846 if (eind->oh != NULL)
3847 edir->oh = ppc_follow_link (eind->oh);
3849 if (edir->elf.versioned != versioned_hidden)
3850 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3851 edir->elf.ref_regular |= eind->elf.ref_regular;
3852 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3853 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3854 edir->elf.needs_plt |= eind->elf.needs_plt;
3855 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3857 /* If we were called to copy over info for a weak sym, don't copy
3858 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3859 in order to simplify readonly_dynrelocs and save a field in the
3860 symbol hash entry, but that means dyn_relocs can't be used in any
3861 tests about a specific symbol, or affect other symbol flags which
3863 if (eind->elf.root.type != bfd_link_hash_indirect)
3866 /* Copy over any dynamic relocs we may have on the indirect sym. */
3867 if (eind->dyn_relocs != NULL)
3869 if (edir->dyn_relocs != NULL)
3871 struct elf_dyn_relocs **pp;
3872 struct elf_dyn_relocs *p;
3874 /* Add reloc counts against the indirect sym to the direct sym
3875 list. Merge any entries against the same section. */
3876 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3878 struct elf_dyn_relocs *q;
3880 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3881 if (q->sec == p->sec)
3883 q->pc_count += p->pc_count;
3884 q->count += p->count;
3891 *pp = edir->dyn_relocs;
3894 edir->dyn_relocs = eind->dyn_relocs;
3895 eind->dyn_relocs = NULL;
3898 /* Copy over got entries that we may have already seen to the
3899 symbol which just became indirect. */
3900 if (eind->elf.got.glist != NULL)
3902 if (edir->elf.got.glist != NULL)
3904 struct got_entry **entp;
3905 struct got_entry *ent;
3907 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3909 struct got_entry *dent;
3911 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3912 if (dent->addend == ent->addend
3913 && dent->owner == ent->owner
3914 && dent->tls_type == ent->tls_type)
3916 dent->got.refcount += ent->got.refcount;
3923 *entp = edir->elf.got.glist;
3926 edir->elf.got.glist = eind->elf.got.glist;
3927 eind->elf.got.glist = NULL;
3930 /* And plt entries. */
3931 move_plt_plist (eind, edir);
3933 if (eind->elf.dynindx != -1)
3935 if (edir->elf.dynindx != -1)
3936 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3937 edir->elf.dynstr_index);
3938 edir->elf.dynindx = eind->elf.dynindx;
3939 edir->elf.dynstr_index = eind->elf.dynstr_index;
3940 eind->elf.dynindx = -1;
3941 eind->elf.dynstr_index = 0;
3945 /* Find the function descriptor hash entry from the given function code
3946 hash entry FH. Link the entries via their OH fields. */
3948 static struct ppc_link_hash_entry *
3949 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3951 struct ppc_link_hash_entry *fdh = fh->oh;
3955 const char *fd_name = fh->elf.root.root.string + 1;
3957 fdh = (struct ppc_link_hash_entry *)
3958 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
3962 fdh->is_func_descriptor = 1;
3968 fdh = ppc_follow_link (fdh);
3969 fdh->is_func_descriptor = 1;
3974 /* Make a fake function descriptor sym for the undefined code sym FH. */
3976 static struct ppc_link_hash_entry *
3977 make_fdh (struct bfd_link_info *info,
3978 struct ppc_link_hash_entry *fh)
3980 bfd *abfd = fh->elf.root.u.undef.abfd;
3981 struct bfd_link_hash_entry *bh = NULL;
3982 struct ppc_link_hash_entry *fdh;
3983 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3987 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3988 fh->elf.root.root.string + 1,
3989 flags, bfd_und_section_ptr, 0,
3990 NULL, FALSE, FALSE, &bh))
3993 fdh = (struct ppc_link_hash_entry *) bh;
3994 fdh->elf.non_elf = 0;
3996 fdh->is_func_descriptor = 1;
4003 /* Fix function descriptor symbols defined in .opd sections to be
4007 ppc64_elf_add_symbol_hook (bfd *ibfd,
4008 struct bfd_link_info *info,
4009 Elf_Internal_Sym *isym,
4011 flagword *flags ATTRIBUTE_UNUSED,
4016 && strcmp ((*sec)->name, ".opd") == 0)
4020 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4021 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4022 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4024 /* If the symbol is a function defined in .opd, and the function
4025 code is in a discarded group, let it appear to be undefined. */
4026 if (!bfd_link_relocatable (info)
4027 && (*sec)->reloc_count != 0
4028 && opd_entry_value (*sec, *value, &code_sec, NULL,
4029 FALSE) != (bfd_vma) -1
4030 && discarded_section (code_sec))
4032 *sec = bfd_und_section_ptr;
4033 isym->st_shndx = SHN_UNDEF;
4036 else if (*sec != NULL
4037 && strcmp ((*sec)->name, ".toc") == 0
4038 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4040 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4042 htab->params->object_in_toc = 1;
4045 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4047 if (abiversion (ibfd) == 0)
4048 set_abiversion (ibfd, 2);
4049 else if (abiversion (ibfd) == 1)
4051 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4052 " for ABI version 1"), *name);
4053 bfd_set_error (bfd_error_bad_value);
4061 /* Merge non-visibility st_other attributes: local entry point. */
4064 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4065 const Elf_Internal_Sym *isym,
4066 bfd_boolean definition,
4067 bfd_boolean dynamic)
4069 if (definition && (!dynamic || !h->def_regular))
4070 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4071 | ELF_ST_VISIBILITY (h->other));
4074 /* Hook called on merging a symbol. We use this to clear "fake" since
4075 we now have a real symbol. */
4078 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4079 const Elf_Internal_Sym *isym,
4080 asection **psec ATTRIBUTE_UNUSED,
4081 bfd_boolean newdef ATTRIBUTE_UNUSED,
4082 bfd_boolean olddef ATTRIBUTE_UNUSED,
4083 bfd *oldbfd ATTRIBUTE_UNUSED,
4084 const asection *oldsec ATTRIBUTE_UNUSED)
4086 ((struct ppc_link_hash_entry *) h)->fake = 0;
4087 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4088 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
4092 /* This function makes an old ABI object reference to ".bar" cause the
4093 inclusion of a new ABI object archive that defines "bar".
4094 NAME is a symbol defined in an archive. Return a symbol in the hash
4095 table that might be satisfied by the archive symbols. */
4097 static struct elf_link_hash_entry *
4098 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4099 struct bfd_link_info *info,
4102 struct elf_link_hash_entry *h;
4106 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4108 /* Don't return this sym if it is a fake function descriptor
4109 created by add_symbol_adjust. */
4110 && !((struct ppc_link_hash_entry *) h)->fake)
4116 len = strlen (name);
4117 dot_name = bfd_alloc (abfd, len + 2);
4118 if (dot_name == NULL)
4119 return (struct elf_link_hash_entry *) -1;
4121 memcpy (dot_name + 1, name, len + 1);
4122 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4123 bfd_release (abfd, dot_name);
4127 /* This function satisfies all old ABI object references to ".bar" if a
4128 new ABI object defines "bar". Well, at least, undefined dot symbols
4129 are made weak. This stops later archive searches from including an
4130 object if we already have a function descriptor definition. It also
4131 prevents the linker complaining about undefined symbols.
4132 We also check and correct mismatched symbol visibility here. The
4133 most restrictive visibility of the function descriptor and the
4134 function entry symbol is used. */
4137 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4139 struct ppc_link_hash_table *htab;
4140 struct ppc_link_hash_entry *fdh;
4142 if (eh->elf.root.type == bfd_link_hash_warning)
4143 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4145 if (eh->elf.root.type == bfd_link_hash_indirect)
4148 if (eh->elf.root.root.string[0] != '.')
4151 htab = ppc_hash_table (info);
4155 fdh = lookup_fdh (eh, htab);
4157 && !bfd_link_relocatable (info)
4158 && (eh->elf.root.type == bfd_link_hash_undefined
4159 || eh->elf.root.type == bfd_link_hash_undefweak)
4160 && eh->elf.ref_regular)
4162 /* Make an undefined function descriptor sym, in order to
4163 pull in an --as-needed shared lib. Archives are handled
4165 fdh = make_fdh (info, eh);
4172 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4173 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4175 /* Make both descriptor and entry symbol have the most
4176 constraining visibility of either symbol. */
4177 if (entry_vis < descr_vis)
4178 fdh->elf.other += entry_vis - descr_vis;
4179 else if (entry_vis > descr_vis)
4180 eh->elf.other += descr_vis - entry_vis;
4182 /* Propagate reference flags from entry symbol to function
4183 descriptor symbol. */
4184 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4185 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4186 fdh->elf.ref_regular |= eh->elf.ref_regular;
4187 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4189 if (!fdh->elf.forced_local
4190 && fdh->elf.dynindx == -1
4191 && fdh->elf.versioned != versioned_hidden
4192 && (bfd_link_dll (info)
4193 || fdh->elf.def_dynamic
4194 || fdh->elf.ref_dynamic)
4195 && (eh->elf.ref_regular
4196 || eh->elf.def_regular))
4198 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4206 /* Set up opd section info and abiversion for IBFD, and process list
4207 of dot-symbols we made in link_hash_newfunc. */
4210 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4212 struct ppc_link_hash_table *htab;
4213 struct ppc_link_hash_entry **p, *eh;
4214 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4216 if (opd != NULL && opd->size != 0)
4218 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4219 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4221 if (abiversion (ibfd) == 0)
4222 set_abiversion (ibfd, 1);
4223 else if (abiversion (ibfd) >= 2)
4225 /* xgettext:c-format */
4226 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4227 ibfd, abiversion (ibfd));
4228 bfd_set_error (bfd_error_bad_value);
4233 if (is_ppc64_elf (info->output_bfd))
4235 /* For input files without an explicit abiversion in e_flags
4236 we should have flagged any with symbol st_other bits set
4237 as ELFv1 and above flagged those with .opd as ELFv2.
4238 Set the output abiversion if not yet set, and for any input
4239 still ambiguous, take its abiversion from the output.
4240 Differences in ABI are reported later. */
4241 if (abiversion (info->output_bfd) == 0)
4242 set_abiversion (info->output_bfd, abiversion (ibfd));
4243 else if (abiversion (ibfd) == 0)
4244 set_abiversion (ibfd, abiversion (info->output_bfd));
4247 htab = ppc_hash_table (info);
4251 if (opd != NULL && opd->size != 0
4252 && (ibfd->flags & DYNAMIC) == 0
4253 && (opd->flags & SEC_RELOC) != 0
4254 && opd->reloc_count != 0
4255 && !bfd_is_abs_section (opd->output_section)
4256 && info->gc_sections)
4258 /* Garbage collection needs some extra help with .opd sections.
4259 We don't want to necessarily keep everything referenced by
4260 relocs in .opd, as that would keep all functions. Instead,
4261 if we reference an .opd symbol (a function descriptor), we
4262 want to keep the function code symbol's section. This is
4263 easy for global symbols, but for local syms we need to keep
4264 information about the associated function section. */
4266 asection **opd_sym_map;
4267 Elf_Internal_Shdr *symtab_hdr;
4268 Elf_Internal_Rela *relocs, *rel_end, *rel;
4270 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4271 opd_sym_map = bfd_zalloc (ibfd, amt);
4272 if (opd_sym_map == NULL)
4274 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4275 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4279 symtab_hdr = &elf_symtab_hdr (ibfd);
4280 rel_end = relocs + opd->reloc_count - 1;
4281 for (rel = relocs; rel < rel_end; rel++)
4283 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4284 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4286 if (r_type == R_PPC64_ADDR64
4287 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4288 && r_symndx < symtab_hdr->sh_info)
4290 Elf_Internal_Sym *isym;
4293 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4296 if (elf_section_data (opd)->relocs != relocs)
4301 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4302 if (s != NULL && s != opd)
4303 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4306 if (elf_section_data (opd)->relocs != relocs)
4310 p = &htab->dot_syms;
4311 while ((eh = *p) != NULL)
4314 if (&eh->elf == htab->elf.hgot)
4316 else if (htab->elf.hgot == NULL
4317 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4318 htab->elf.hgot = &eh->elf;
4319 else if (abiversion (ibfd) <= 1)
4321 htab->need_func_desc_adj = 1;
4322 if (!add_symbol_adjust (eh, info))
4325 p = &eh->u.next_dot_sym;
4330 /* Undo hash table changes when an --as-needed input file is determined
4331 not to be needed. */
4334 ppc64_elf_notice_as_needed (bfd *ibfd,
4335 struct bfd_link_info *info,
4336 enum notice_asneeded_action act)
4338 if (act == notice_not_needed)
4340 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4345 htab->dot_syms = NULL;
4347 return _bfd_elf_notice_as_needed (ibfd, info, act);
4350 /* If --just-symbols against a final linked binary, then assume we need
4351 toc adjusting stubs when calling functions defined there. */
4354 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4356 if ((sec->flags & SEC_CODE) != 0
4357 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4358 && is_ppc64_elf (sec->owner))
4360 if (abiversion (sec->owner) >= 2
4361 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4362 sec->has_toc_reloc = 1;
4364 _bfd_elf_link_just_syms (sec, info);
4367 static struct plt_entry **
4368 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4369 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4371 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4372 struct plt_entry **local_plt;
4373 unsigned char *local_got_tls_masks;
4375 if (local_got_ents == NULL)
4377 bfd_size_type size = symtab_hdr->sh_info;
4379 size *= (sizeof (*local_got_ents)
4380 + sizeof (*local_plt)
4381 + sizeof (*local_got_tls_masks));
4382 local_got_ents = bfd_zalloc (abfd, size);
4383 if (local_got_ents == NULL)
4385 elf_local_got_ents (abfd) = local_got_ents;
4388 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4390 struct got_entry *ent;
4392 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4393 if (ent->addend == r_addend
4394 && ent->owner == abfd
4395 && ent->tls_type == tls_type)
4399 bfd_size_type amt = sizeof (*ent);
4400 ent = bfd_alloc (abfd, amt);
4403 ent->next = local_got_ents[r_symndx];
4404 ent->addend = r_addend;
4406 ent->tls_type = tls_type;
4407 ent->is_indirect = FALSE;
4408 ent->got.refcount = 0;
4409 local_got_ents[r_symndx] = ent;
4411 ent->got.refcount += 1;
4414 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4415 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4416 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4418 return local_plt + r_symndx;
4422 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4424 struct plt_entry *ent;
4426 for (ent = *plist; ent != NULL; ent = ent->next)
4427 if (ent->addend == addend)
4431 bfd_size_type amt = sizeof (*ent);
4432 ent = bfd_alloc (abfd, amt);
4436 ent->addend = addend;
4437 ent->plt.refcount = 0;
4440 ent->plt.refcount += 1;
4445 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4447 return (r_type == R_PPC64_REL24
4448 || r_type == R_PPC64_REL24_NOTOC
4449 || r_type == R_PPC64_REL14
4450 || r_type == R_PPC64_REL14_BRTAKEN
4451 || r_type == R_PPC64_REL14_BRNTAKEN
4452 || r_type == R_PPC64_ADDR24
4453 || r_type == R_PPC64_ADDR14
4454 || r_type == R_PPC64_ADDR14_BRTAKEN
4455 || r_type == R_PPC64_ADDR14_BRNTAKEN
4456 || r_type == R_PPC64_PLTCALL
4457 || r_type == R_PPC64_PLTCALL_NOTOC);
4460 /* Relocs on inline plt call sequence insns prior to the call. */
4463 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4465 return (r_type == R_PPC64_PLT16_HA
4466 || r_type == R_PPC64_PLT16_HI
4467 || r_type == R_PPC64_PLT16_LO
4468 || r_type == R_PPC64_PLT16_LO_DS
4469 || r_type == R_PPC64_PLT_PCREL34
4470 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4471 || r_type == R_PPC64_PLTSEQ
4472 || r_type == R_PPC64_PLTSEQ_NOTOC);
4475 /* Look through the relocs for a section during the first phase, and
4476 calculate needed space in the global offset table, procedure
4477 linkage table, and dynamic reloc sections. */
4480 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4481 asection *sec, const Elf_Internal_Rela *relocs)
4483 struct ppc_link_hash_table *htab;
4484 Elf_Internal_Shdr *symtab_hdr;
4485 struct elf_link_hash_entry **sym_hashes;
4486 const Elf_Internal_Rela *rel;
4487 const Elf_Internal_Rela *rel_end;
4489 struct elf_link_hash_entry *tga, *dottga;
4492 if (bfd_link_relocatable (info))
4495 /* Don't do anything special with non-loaded, non-alloced sections.
4496 In particular, any relocs in such sections should not affect GOT
4497 and PLT reference counting (ie. we don't allow them to create GOT
4498 or PLT entries), there's no possibility or desire to optimize TLS
4499 relocs, and there's not much point in propagating relocs to shared
4500 libs that the dynamic linker won't relocate. */
4501 if ((sec->flags & SEC_ALLOC) == 0)
4504 BFD_ASSERT (is_ppc64_elf (abfd));
4506 htab = ppc_hash_table (info);
4510 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4511 FALSE, FALSE, TRUE);
4512 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4513 FALSE, FALSE, TRUE);
4514 symtab_hdr = &elf_symtab_hdr (abfd);
4515 sym_hashes = elf_sym_hashes (abfd);
4517 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4518 rel_end = relocs + sec->reloc_count;
4519 for (rel = relocs; rel < rel_end; rel++)
4521 unsigned long r_symndx;
4522 struct elf_link_hash_entry *h;
4523 enum elf_ppc64_reloc_type r_type;
4525 struct _ppc64_elf_section_data *ppc64_sec;
4526 struct plt_entry **ifunc, **plt_list;
4529 r_symndx = ELF64_R_SYM (rel->r_info);
4530 if (r_symndx < symtab_hdr->sh_info)
4534 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4535 h = elf_follow_link (h);
4537 if (h == htab->elf.hgot)
4538 sec->has_toc_reloc = 1;
4543 r_type = ELF64_R_TYPE (rel->r_info);
4547 case R_PPC64_D34_LO:
4548 case R_PPC64_D34_HI30:
4549 case R_PPC64_D34_HA30:
4551 case R_PPC64_TPREL34:
4552 case R_PPC64_DTPREL34:
4553 htab->powerxx_stubs = 1;
4556 /* Somewhat foolishly, because the ABIs don't specifically
4557 allow it, ppc64 gas and ld support GOT and PLT relocs
4558 with non-zero addends where the addend results in
4559 sym+addend being stored in the GOT or PLT entry. This
4560 can't be supported for pcrel relocs because the addend is
4561 used to specify the pcrel offset. */
4562 sym_addend = rel->r_addend;
4565 case R_PPC64_PCREL34:
4566 case R_PPC64_GOT_PCREL34:
4567 case R_PPC64_GOT_TLSGD34:
4568 case R_PPC64_GOT_TLSLD34:
4569 case R_PPC64_GOT_TPREL34:
4570 case R_PPC64_GOT_DTPREL34:
4571 case R_PPC64_PLT_PCREL34:
4572 case R_PPC64_PLT_PCREL34_NOTOC:
4573 case R_PPC64_PCREL28:
4574 htab->powerxx_stubs = 1;
4581 case R_PPC64_PLT16_HA:
4582 case R_PPC64_GOT_TLSLD16_HA:
4583 case R_PPC64_GOT_TLSGD16_HA:
4584 case R_PPC64_GOT_TPREL16_HA:
4585 case R_PPC64_GOT_DTPREL16_HA:
4586 case R_PPC64_GOT16_HA:
4587 case R_PPC64_TOC16_HA:
4588 case R_PPC64_PLT16_LO:
4589 case R_PPC64_PLT16_LO_DS:
4590 case R_PPC64_GOT_TLSLD16_LO:
4591 case R_PPC64_GOT_TLSGD16_LO:
4592 case R_PPC64_GOT_TPREL16_LO_DS:
4593 case R_PPC64_GOT_DTPREL16_LO_DS:
4594 case R_PPC64_GOT16_LO:
4595 case R_PPC64_GOT16_LO_DS:
4596 case R_PPC64_TOC16_LO:
4597 case R_PPC64_TOC16_LO_DS:
4598 case R_PPC64_GOT_PCREL34:
4599 ppc64_elf_tdata (abfd)->has_optrel = 1;
4600 ppc64_elf_section_data (sec)->has_optrel = 1;
4608 if (h->type == STT_GNU_IFUNC)
4611 ifunc = &h->plt.plist;
4616 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4621 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4623 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4625 NON_GOT | PLT_IFUNC);
4635 /* These special tls relocs tie a call to __tls_get_addr with
4636 its parameter symbol. */
4638 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4640 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4642 NON_GOT | TLS_TLS | TLS_MARK))
4644 sec->has_tls_reloc = 1;
4647 case R_PPC64_GOT_TLSLD16:
4648 case R_PPC64_GOT_TLSLD16_LO:
4649 case R_PPC64_GOT_TLSLD16_HI:
4650 case R_PPC64_GOT_TLSLD16_HA:
4651 case R_PPC64_GOT_TLSLD34:
4652 tls_type = TLS_TLS | TLS_LD;
4655 case R_PPC64_GOT_TLSGD16:
4656 case R_PPC64_GOT_TLSGD16_LO:
4657 case R_PPC64_GOT_TLSGD16_HI:
4658 case R_PPC64_GOT_TLSGD16_HA:
4659 case R_PPC64_GOT_TLSGD34:
4660 tls_type = TLS_TLS | TLS_GD;
4663 case R_PPC64_GOT_TPREL16_DS:
4664 case R_PPC64_GOT_TPREL16_LO_DS:
4665 case R_PPC64_GOT_TPREL16_HI:
4666 case R_PPC64_GOT_TPREL16_HA:
4667 case R_PPC64_GOT_TPREL34:
4668 if (bfd_link_dll (info))
4669 info->flags |= DF_STATIC_TLS;
4670 tls_type = TLS_TLS | TLS_TPREL;
4673 case R_PPC64_GOT_DTPREL16_DS:
4674 case R_PPC64_GOT_DTPREL16_LO_DS:
4675 case R_PPC64_GOT_DTPREL16_HI:
4676 case R_PPC64_GOT_DTPREL16_HA:
4677 case R_PPC64_GOT_DTPREL34:
4678 tls_type = TLS_TLS | TLS_DTPREL;
4680 sec->has_tls_reloc = 1;
4684 case R_PPC64_GOT16_LO:
4685 case R_PPC64_GOT16_HI:
4686 case R_PPC64_GOT16_HA:
4687 case R_PPC64_GOT16_DS:
4688 case R_PPC64_GOT16_LO_DS:
4689 case R_PPC64_GOT_PCREL34:
4691 /* This symbol requires a global offset table entry. */
4692 sec->has_toc_reloc = 1;
4693 if (r_type == R_PPC64_GOT_TLSLD16
4694 || r_type == R_PPC64_GOT_TLSGD16
4695 || r_type == R_PPC64_GOT_TPREL16_DS
4696 || r_type == R_PPC64_GOT_DTPREL16_DS
4697 || r_type == R_PPC64_GOT16
4698 || r_type == R_PPC64_GOT16_DS)
4700 htab->do_multi_toc = 1;
4701 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4704 if (ppc64_elf_tdata (abfd)->got == NULL
4705 && !create_got_section (abfd, info))
4710 struct ppc_link_hash_entry *eh;
4711 struct got_entry *ent;
4713 eh = (struct ppc_link_hash_entry *) h;
4714 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4715 if (ent->addend == sym_addend
4716 && ent->owner == abfd
4717 && ent->tls_type == tls_type)
4721 bfd_size_type amt = sizeof (*ent);
4722 ent = bfd_alloc (abfd, amt);
4725 ent->next = eh->elf.got.glist;
4726 ent->addend = sym_addend;
4728 ent->tls_type = tls_type;
4729 ent->is_indirect = FALSE;
4730 ent->got.refcount = 0;
4731 eh->elf.got.glist = ent;
4733 ent->got.refcount += 1;
4734 eh->tls_mask |= tls_type;
4737 /* This is a global offset table entry for a local symbol. */
4738 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4739 sym_addend, tls_type))
4742 /* We may also need a plt entry if the symbol turns out to be
4744 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
4746 if (!update_plt_info (abfd, &h->plt.plist, sym_addend))
4751 case R_PPC64_PLT16_HA:
4752 case R_PPC64_PLT16_HI:
4753 case R_PPC64_PLT16_LO:
4754 case R_PPC64_PLT16_LO_DS:
4755 case R_PPC64_PLT_PCREL34:
4756 case R_PPC64_PLT_PCREL34_NOTOC:
4759 /* This symbol requires a procedure linkage table entry. */
4764 if (h->root.root.string[0] == '.'
4765 && h->root.root.string[1] != '\0')
4766 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4767 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
4768 plt_list = &h->plt.plist;
4770 if (plt_list == NULL)
4771 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4773 NON_GOT | PLT_KEEP);
4774 if (!update_plt_info (abfd, plt_list, sym_addend))
4778 /* The following relocations don't need to propagate the
4779 relocation if linking a shared object since they are
4780 section relative. */
4781 case R_PPC64_SECTOFF:
4782 case R_PPC64_SECTOFF_LO:
4783 case R_PPC64_SECTOFF_HI:
4784 case R_PPC64_SECTOFF_HA:
4785 case R_PPC64_SECTOFF_DS:
4786 case R_PPC64_SECTOFF_LO_DS:
4787 case R_PPC64_DTPREL16:
4788 case R_PPC64_DTPREL16_LO:
4789 case R_PPC64_DTPREL16_HI:
4790 case R_PPC64_DTPREL16_HA:
4791 case R_PPC64_DTPREL16_DS:
4792 case R_PPC64_DTPREL16_LO_DS:
4793 case R_PPC64_DTPREL16_HIGH:
4794 case R_PPC64_DTPREL16_HIGHA:
4795 case R_PPC64_DTPREL16_HIGHER:
4796 case R_PPC64_DTPREL16_HIGHERA:
4797 case R_PPC64_DTPREL16_HIGHEST:
4798 case R_PPC64_DTPREL16_HIGHESTA:
4803 case R_PPC64_REL16_LO:
4804 case R_PPC64_REL16_HI:
4805 case R_PPC64_REL16_HA:
4806 case R_PPC64_REL16_HIGH:
4807 case R_PPC64_REL16_HIGHA:
4808 case R_PPC64_REL16_HIGHER:
4809 case R_PPC64_REL16_HIGHERA:
4810 case R_PPC64_REL16_HIGHEST:
4811 case R_PPC64_REL16_HIGHESTA:
4812 case R_PPC64_REL16_HIGHER34:
4813 case R_PPC64_REL16_HIGHERA34:
4814 case R_PPC64_REL16_HIGHEST34:
4815 case R_PPC64_REL16_HIGHESTA34:
4816 case R_PPC64_REL16DX_HA:
4819 /* Not supported as a dynamic relocation. */
4820 case R_PPC64_ADDR64_LOCAL:
4821 if (bfd_link_pic (info))
4823 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4825 /* xgettext:c-format */
4826 info->callbacks->einfo (_("%H: %s reloc unsupported "
4827 "in shared libraries and PIEs\n"),
4828 abfd, sec, rel->r_offset,
4829 ppc64_elf_howto_table[r_type]->name);
4830 bfd_set_error (bfd_error_bad_value);
4836 case R_PPC64_TOC16_DS:
4837 htab->do_multi_toc = 1;
4838 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4840 case R_PPC64_TOC16_LO:
4841 case R_PPC64_TOC16_HI:
4842 case R_PPC64_TOC16_HA:
4843 case R_PPC64_TOC16_LO_DS:
4844 sec->has_toc_reloc = 1;
4851 /* This relocation describes the C++ object vtable hierarchy.
4852 Reconstruct it for later use during GC. */
4853 case R_PPC64_GNU_VTINHERIT:
4854 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4858 /* This relocation describes which C++ vtable entries are actually
4859 used. Record for later use during GC. */
4860 case R_PPC64_GNU_VTENTRY:
4861 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4866 case R_PPC64_REL14_BRTAKEN:
4867 case R_PPC64_REL14_BRNTAKEN:
4869 asection *dest = NULL;
4871 /* Heuristic: If jumping outside our section, chances are
4872 we are going to need a stub. */
4875 /* If the sym is weak it may be overridden later, so
4876 don't assume we know where a weak sym lives. */
4877 if (h->root.type == bfd_link_hash_defined)
4878 dest = h->root.u.def.section;
4882 Elf_Internal_Sym *isym;
4884 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4889 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4893 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
4897 case R_PPC64_PLTCALL:
4898 case R_PPC64_PLTCALL_NOTOC:
4899 ppc64_elf_section_data (sec)->has_pltcall = 1;
4903 case R_PPC64_REL24_NOTOC:
4909 if (h->root.root.string[0] == '.'
4910 && h->root.root.string[1] != '\0')
4911 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4913 if (h == tga || h == dottga)
4915 sec->has_tls_reloc = 1;
4917 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4918 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4919 /* We have a new-style __tls_get_addr call with
4923 /* Mark this section as having an old-style call. */
4924 sec->has_tls_get_addr_call = 1;
4926 plt_list = &h->plt.plist;
4929 /* We may need a .plt entry if the function this reloc
4930 refers to is in a shared lib. */
4932 && !update_plt_info (abfd, plt_list, sym_addend))
4936 case R_PPC64_ADDR14:
4937 case R_PPC64_ADDR14_BRNTAKEN:
4938 case R_PPC64_ADDR14_BRTAKEN:
4939 case R_PPC64_ADDR24:
4942 case R_PPC64_TPREL64:
4943 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4944 if (bfd_link_dll (info))
4945 info->flags |= DF_STATIC_TLS;
4948 case R_PPC64_DTPMOD64:
4949 if (rel + 1 < rel_end
4950 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4951 && rel[1].r_offset == rel->r_offset + 8)
4952 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4954 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4957 case R_PPC64_DTPREL64:
4958 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4960 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4961 && rel[-1].r_offset == rel->r_offset - 8)
4962 /* This is the second reloc of a dtpmod, dtprel pair.
4963 Don't mark with TLS_DTPREL. */
4967 sec->has_tls_reloc = 1;
4970 struct ppc_link_hash_entry *eh;
4971 eh = (struct ppc_link_hash_entry *) h;
4972 eh->tls_mask |= tls_type & 0xff;
4975 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4976 sym_addend, tls_type))
4979 ppc64_sec = ppc64_elf_section_data (sec);
4980 if (ppc64_sec->sec_type != sec_toc)
4984 /* One extra to simplify get_tls_mask. */
4985 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4986 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4987 if (ppc64_sec->u.toc.symndx == NULL)
4989 amt = sec->size * sizeof (bfd_vma) / 8;
4990 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4991 if (ppc64_sec->u.toc.add == NULL)
4993 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4994 ppc64_sec->sec_type = sec_toc;
4996 BFD_ASSERT (rel->r_offset % 8 == 0);
4997 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
4998 ppc64_sec->u.toc.add[rel->r_offset / 8] = sym_addend;
5000 /* Mark the second slot of a GD or LD entry.
5001 -1 to indicate GD and -2 to indicate LD. */
5002 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5003 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5004 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5005 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5008 case R_PPC64_TPREL16:
5009 case R_PPC64_TPREL16_LO:
5010 case R_PPC64_TPREL16_HI:
5011 case R_PPC64_TPREL16_HA:
5012 case R_PPC64_TPREL16_DS:
5013 case R_PPC64_TPREL16_LO_DS:
5014 case R_PPC64_TPREL16_HIGH:
5015 case R_PPC64_TPREL16_HIGHA:
5016 case R_PPC64_TPREL16_HIGHER:
5017 case R_PPC64_TPREL16_HIGHERA:
5018 case R_PPC64_TPREL16_HIGHEST:
5019 case R_PPC64_TPREL16_HIGHESTA:
5020 case R_PPC64_TPREL34:
5021 if (bfd_link_dll (info))
5022 info->flags |= DF_STATIC_TLS;
5025 case R_PPC64_ADDR64:
5027 && rel + 1 < rel_end
5028 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5031 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5035 case R_PPC64_ADDR16:
5036 case R_PPC64_ADDR16_DS:
5037 case R_PPC64_ADDR16_HA:
5038 case R_PPC64_ADDR16_HI:
5039 case R_PPC64_ADDR16_HIGH:
5040 case R_PPC64_ADDR16_HIGHA:
5041 case R_PPC64_ADDR16_HIGHER:
5042 case R_PPC64_ADDR16_HIGHERA:
5043 case R_PPC64_ADDR16_HIGHEST:
5044 case R_PPC64_ADDR16_HIGHESTA:
5045 case R_PPC64_ADDR16_LO:
5046 case R_PPC64_ADDR16_LO_DS:
5048 case R_PPC64_D34_LO:
5049 case R_PPC64_D34_HI30:
5050 case R_PPC64_D34_HA30:
5051 case R_PPC64_ADDR16_HIGHER34:
5052 case R_PPC64_ADDR16_HIGHERA34:
5053 case R_PPC64_ADDR16_HIGHEST34:
5054 case R_PPC64_ADDR16_HIGHESTA34:
5056 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5057 && rel->r_addend == 0)
5059 /* We may need a .plt entry if this reloc refers to a
5060 function in a shared lib. */
5061 if (!update_plt_info (abfd, &h->plt.plist, 0))
5063 h->pointer_equality_needed = 1;
5070 case R_PPC64_ADDR32:
5071 case R_PPC64_UADDR16:
5072 case R_PPC64_UADDR32:
5073 case R_PPC64_UADDR64:
5075 if (h != NULL && !bfd_link_pic (info))
5076 /* We may need a copy reloc. */
5079 /* Don't propagate .opd relocs. */
5080 if (NO_OPD_RELOCS && is_opd)
5083 /* If we are creating a shared library, and this is a reloc
5084 against a global symbol, or a non PC relative reloc
5085 against a local symbol, then we need to copy the reloc
5086 into the shared library. However, if we are linking with
5087 -Bsymbolic, we do not need to copy a reloc against a
5088 global symbol which is defined in an object we are
5089 including in the link (i.e., DEF_REGULAR is set). At
5090 this point we have not seen all the input files, so it is
5091 possible that DEF_REGULAR is not set now but will be set
5092 later (it is never cleared). In case of a weak definition,
5093 DEF_REGULAR may be cleared later by a strong definition in
5094 a shared library. We account for that possibility below by
5095 storing information in the dyn_relocs field of the hash
5096 table entry. A similar situation occurs when creating
5097 shared libraries and symbol visibility changes render the
5100 If on the other hand, we are creating an executable, we
5101 may need to keep relocations for symbols satisfied by a
5102 dynamic library if we manage to avoid copy relocs for the
5105 if ((bfd_link_pic (info)
5106 && (must_be_dyn_reloc (info, r_type)
5108 && (!SYMBOLIC_BIND (info, h)
5109 || h->root.type == bfd_link_hash_defweak
5110 || !h->def_regular))))
5111 || (ELIMINATE_COPY_RELOCS
5112 && !bfd_link_pic (info)
5114 && (h->root.type == bfd_link_hash_defweak
5115 || !h->def_regular))
5116 || (!bfd_link_pic (info)
5119 /* We must copy these reloc types into the output file.
5120 Create a reloc section in dynobj and make room for
5124 sreloc = _bfd_elf_make_dynamic_reloc_section
5125 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5131 /* If this is a global symbol, we count the number of
5132 relocations we need for this symbol. */
5135 struct elf_dyn_relocs *p;
5136 struct elf_dyn_relocs **head;
5138 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5140 if (p == NULL || p->sec != sec)
5142 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5152 if (!must_be_dyn_reloc (info, r_type))
5157 /* Track dynamic relocs needed for local syms too.
5158 We really need local syms available to do this
5160 struct ppc_dyn_relocs *p;
5161 struct ppc_dyn_relocs **head;
5162 bfd_boolean is_ifunc;
5165 Elf_Internal_Sym *isym;
5167 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5172 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5176 vpp = &elf_section_data (s)->local_dynrel;
5177 head = (struct ppc_dyn_relocs **) vpp;
5178 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5180 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5182 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5184 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5190 p->ifunc = is_ifunc;
5206 /* Merge backend specific data from an object file to the output
5207 object file when linking. */
5210 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5212 bfd *obfd = info->output_bfd;
5213 unsigned long iflags, oflags;
5215 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5218 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5221 if (!_bfd_generic_verify_endian_match (ibfd, info))
5224 iflags = elf_elfheader (ibfd)->e_flags;
5225 oflags = elf_elfheader (obfd)->e_flags;
5227 if (iflags & ~EF_PPC64_ABI)
5230 /* xgettext:c-format */
5231 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5232 bfd_set_error (bfd_error_bad_value);
5235 else if (iflags != oflags && iflags != 0)
5238 /* xgettext:c-format */
5239 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5240 ibfd, iflags, oflags);
5241 bfd_set_error (bfd_error_bad_value);
5245 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5248 /* Merge Tag_compatibility attributes and any common GNU ones. */
5249 return _bfd_elf_merge_object_attributes (ibfd, info);
5253 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5255 /* Print normal ELF private data. */
5256 _bfd_elf_print_private_bfd_data (abfd, ptr);
5258 if (elf_elfheader (abfd)->e_flags != 0)
5262 fprintf (file, _("private flags = 0x%lx:"),
5263 elf_elfheader (abfd)->e_flags);
5265 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5266 fprintf (file, _(" [abiv%ld]"),
5267 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5274 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5275 of the code entry point, and its section, which must be in the same
5276 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
5279 opd_entry_value (asection *opd_sec,
5281 asection **code_sec,
5283 bfd_boolean in_code_sec)
5285 bfd *opd_bfd = opd_sec->owner;
5286 Elf_Internal_Rela *relocs;
5287 Elf_Internal_Rela *lo, *hi, *look;
5290 /* No relocs implies we are linking a --just-symbols object, or looking
5291 at a final linked executable with addr2line or somesuch. */
5292 if (opd_sec->reloc_count == 0)
5294 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5296 if (contents == NULL)
5298 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5299 return (bfd_vma) -1;
5300 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5303 /* PR 17512: file: 64b9dfbb. */
5304 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5305 return (bfd_vma) -1;
5307 val = bfd_get_64 (opd_bfd, contents + offset);
5308 if (code_sec != NULL)
5310 asection *sec, *likely = NULL;
5316 && val < sec->vma + sec->size)
5322 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5324 && (sec->flags & SEC_LOAD) != 0
5325 && (sec->flags & SEC_ALLOC) != 0)
5330 if (code_off != NULL)
5331 *code_off = val - likely->vma;
5337 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5339 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5341 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5342 /* PR 17512: file: df8e1fd6. */
5344 return (bfd_vma) -1;
5346 /* Go find the opd reloc at the sym address. */
5348 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5352 look = lo + (hi - lo) / 2;
5353 if (look->r_offset < offset)
5355 else if (look->r_offset > offset)
5359 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5361 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5362 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5364 unsigned long symndx = ELF64_R_SYM (look->r_info);
5365 asection *sec = NULL;
5367 if (symndx >= symtab_hdr->sh_info
5368 && elf_sym_hashes (opd_bfd) != NULL)
5370 struct elf_link_hash_entry **sym_hashes;
5371 struct elf_link_hash_entry *rh;
5373 sym_hashes = elf_sym_hashes (opd_bfd);
5374 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5377 rh = elf_follow_link (rh);
5378 if (rh->root.type != bfd_link_hash_defined
5379 && rh->root.type != bfd_link_hash_defweak)
5381 if (rh->root.u.def.section->owner == opd_bfd)
5383 val = rh->root.u.def.value;
5384 sec = rh->root.u.def.section;
5391 Elf_Internal_Sym *sym;
5393 if (symndx < symtab_hdr->sh_info)
5395 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5398 size_t symcnt = symtab_hdr->sh_info;
5399 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5404 symtab_hdr->contents = (bfd_byte *) sym;
5410 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5416 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5419 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5420 val = sym->st_value;
5423 val += look->r_addend;
5424 if (code_off != NULL)
5426 if (code_sec != NULL)
5428 if (in_code_sec && *code_sec != sec)
5433 if (sec->output_section != NULL)
5434 val += sec->output_section->vma + sec->output_offset;
5443 /* If the ELF symbol SYM might be a function in SEC, return the
5444 function size and set *CODE_OFF to the function's entry point,
5445 otherwise return zero. */
5447 static bfd_size_type
5448 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5453 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5454 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5458 if (!(sym->flags & BSF_SYNTHETIC))
5459 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5461 if (strcmp (sym->section->name, ".opd") == 0)
5463 struct _opd_sec_data *opd = get_opd_info (sym->section);
5464 bfd_vma symval = sym->value;
5467 && opd->adjust != NULL
5468 && elf_section_data (sym->section)->relocs != NULL)
5470 /* opd_entry_value will use cached relocs that have been
5471 adjusted, but with raw symbols. That means both local
5472 and global symbols need adjusting. */
5473 long adjust = opd->adjust[OPD_NDX (symval)];
5479 if (opd_entry_value (sym->section, symval,
5480 &sec, code_off, TRUE) == (bfd_vma) -1)
5482 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5483 symbol. This size has nothing to do with the code size of the
5484 function, which is what we're supposed to return, but the
5485 code size isn't available without looking up the dot-sym.
5486 However, doing that would be a waste of time particularly
5487 since elf_find_function will look at the dot-sym anyway.
5488 Now, elf_find_function will keep the largest size of any
5489 function sym found at the code address of interest, so return
5490 1 here to avoid it incorrectly caching a larger function size
5491 for a small function. This does mean we return the wrong
5492 size for a new-ABI function of size 24, but all that does is
5493 disable caching for such functions. */
5499 if (sym->section != sec)
5501 *code_off = sym->value;
5508 /* Return true if symbol is a strong function defined in an ELFv2
5509 object with st_other localentry bits of zero, ie. its local entry
5510 point coincides with its global entry point. */
5513 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5516 && h->type == STT_FUNC
5517 && h->root.type == bfd_link_hash_defined
5518 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5519 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
5520 && is_ppc64_elf (h->root.u.def.section->owner)
5521 && abiversion (h->root.u.def.section->owner) >= 2);
5524 /* Return true if symbol is defined in a regular object file. */
5527 is_static_defined (struct elf_link_hash_entry *h)
5529 return ((h->root.type == bfd_link_hash_defined
5530 || h->root.type == bfd_link_hash_defweak)
5531 && h->root.u.def.section != NULL
5532 && h->root.u.def.section->output_section != NULL);
5535 /* If FDH is a function descriptor symbol, return the associated code
5536 entry symbol if it is defined. Return NULL otherwise. */
5538 static struct ppc_link_hash_entry *
5539 defined_code_entry (struct ppc_link_hash_entry *fdh)
5541 if (fdh->is_func_descriptor)
5543 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5544 if (fh->elf.root.type == bfd_link_hash_defined
5545 || fh->elf.root.type == bfd_link_hash_defweak)
5551 /* If FH is a function code entry symbol, return the associated
5552 function descriptor symbol if it is defined. Return NULL otherwise. */
5554 static struct ppc_link_hash_entry *
5555 defined_func_desc (struct ppc_link_hash_entry *fh)
5558 && fh->oh->is_func_descriptor)
5560 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5561 if (fdh->elf.root.type == bfd_link_hash_defined
5562 || fdh->elf.root.type == bfd_link_hash_defweak)
5568 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5570 /* Garbage collect sections, after first dealing with dot-symbols. */
5573 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5575 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5577 if (htab != NULL && htab->need_func_desc_adj)
5579 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5580 htab->need_func_desc_adj = 0;
5582 return bfd_elf_gc_sections (abfd, info);
5585 /* Mark all our entry sym sections, both opd and code section. */
5588 ppc64_elf_gc_keep (struct bfd_link_info *info)
5590 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5591 struct bfd_sym_chain *sym;
5596 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5598 struct ppc_link_hash_entry *eh, *fh;
5601 eh = (struct ppc_link_hash_entry *)
5602 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5605 if (eh->elf.root.type != bfd_link_hash_defined
5606 && eh->elf.root.type != bfd_link_hash_defweak)
5609 fh = defined_code_entry (eh);
5612 sec = fh->elf.root.u.def.section;
5613 sec->flags |= SEC_KEEP;
5615 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5616 && opd_entry_value (eh->elf.root.u.def.section,
5617 eh->elf.root.u.def.value,
5618 &sec, NULL, FALSE) != (bfd_vma) -1)
5619 sec->flags |= SEC_KEEP;
5621 sec = eh->elf.root.u.def.section;
5622 sec->flags |= SEC_KEEP;
5626 /* Mark sections containing dynamically referenced symbols. When
5627 building shared libraries, we must assume that any visible symbol is
5631 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5633 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5634 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5635 struct ppc_link_hash_entry *fdh;
5636 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5638 /* Dynamic linking info is on the func descriptor sym. */
5639 fdh = defined_func_desc (eh);
5643 if ((eh->elf.root.type == bfd_link_hash_defined
5644 || eh->elf.root.type == bfd_link_hash_defweak)
5645 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5646 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5647 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5648 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5649 && (!bfd_link_executable (info)
5650 || info->gc_keep_exported
5651 || info->export_dynamic
5654 && (*d->match) (&d->head, NULL,
5655 eh->elf.root.root.string)))
5656 && (eh->elf.versioned >= versioned
5657 || !bfd_hide_sym_by_version (info->version_info,
5658 eh->elf.root.root.string)))))
5661 struct ppc_link_hash_entry *fh;
5663 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5665 /* Function descriptor syms cause the associated
5666 function code sym section to be marked. */
5667 fh = defined_code_entry (eh);
5670 code_sec = fh->elf.root.u.def.section;
5671 code_sec->flags |= SEC_KEEP;
5673 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5674 && opd_entry_value (eh->elf.root.u.def.section,
5675 eh->elf.root.u.def.value,
5676 &code_sec, NULL, FALSE) != (bfd_vma) -1)
5677 code_sec->flags |= SEC_KEEP;
5683 /* Return the section that should be marked against GC for a given
5687 ppc64_elf_gc_mark_hook (asection *sec,
5688 struct bfd_link_info *info,
5689 Elf_Internal_Rela *rel,
5690 struct elf_link_hash_entry *h,
5691 Elf_Internal_Sym *sym)
5695 /* Syms return NULL if we're marking .opd, so we avoid marking all
5696 function sections, as all functions are referenced in .opd. */
5698 if (get_opd_info (sec) != NULL)
5703 enum elf_ppc64_reloc_type r_type;
5704 struct ppc_link_hash_entry *eh, *fh, *fdh;
5706 r_type = ELF64_R_TYPE (rel->r_info);
5709 case R_PPC64_GNU_VTINHERIT:
5710 case R_PPC64_GNU_VTENTRY:
5714 switch (h->root.type)
5716 case bfd_link_hash_defined:
5717 case bfd_link_hash_defweak:
5718 eh = (struct ppc_link_hash_entry *) h;
5719 fdh = defined_func_desc (eh);
5722 /* -mcall-aixdesc code references the dot-symbol on
5723 a call reloc. Mark the function descriptor too
5724 against garbage collection. */
5726 if (fdh->elf.is_weakalias)
5727 weakdef (&fdh->elf)->mark = 1;
5731 /* Function descriptor syms cause the associated
5732 function code sym section to be marked. */
5733 fh = defined_code_entry (eh);
5736 /* They also mark their opd section. */
5737 eh->elf.root.u.def.section->gc_mark = 1;
5739 rsec = fh->elf.root.u.def.section;
5741 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5742 && opd_entry_value (eh->elf.root.u.def.section,
5743 eh->elf.root.u.def.value,
5744 &rsec, NULL, FALSE) != (bfd_vma) -1)
5745 eh->elf.root.u.def.section->gc_mark = 1;
5747 rsec = h->root.u.def.section;
5750 case bfd_link_hash_common:
5751 rsec = h->root.u.c.p->section;
5755 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5761 struct _opd_sec_data *opd;
5763 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5764 opd = get_opd_info (rsec);
5765 if (opd != NULL && opd->func_sec != NULL)
5769 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
5776 /* The maximum size of .sfpr. */
5777 #define SFPR_MAX (218*4)
5779 struct sfpr_def_parms
5781 const char name[12];
5782 unsigned char lo, hi;
5783 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5784 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
5787 /* Auto-generate _save*, _rest* functions in .sfpr.
5788 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5792 sfpr_define (struct bfd_link_info *info,
5793 const struct sfpr_def_parms *parm,
5796 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5798 size_t len = strlen (parm->name);
5799 bfd_boolean writing = FALSE;
5805 memcpy (sym, parm->name, len);
5808 for (i = parm->lo; i <= parm->hi; i++)
5810 struct ppc_link_hash_entry *h;
5812 sym[len + 0] = i / 10 + '0';
5813 sym[len + 1] = i % 10 + '0';
5814 h = (struct ppc_link_hash_entry *)
5815 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
5816 if (stub_sec != NULL)
5819 && h->elf.root.type == bfd_link_hash_defined
5820 && h->elf.root.u.def.section == htab->sfpr)
5822 struct elf_link_hash_entry *s;
5824 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5825 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5828 if (s->root.type == bfd_link_hash_new
5829 || (s->root.type = bfd_link_hash_defined
5830 && s->root.u.def.section == stub_sec))
5832 s->root.type = bfd_link_hash_defined;
5833 s->root.u.def.section = stub_sec;
5834 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
5835 + h->elf.root.u.def.value);
5838 s->ref_regular_nonweak = 1;
5839 s->forced_local = 1;
5841 s->root.linker_def = 1;
5849 if (!h->elf.def_regular)
5851 h->elf.root.type = bfd_link_hash_defined;
5852 h->elf.root.u.def.section = htab->sfpr;
5853 h->elf.root.u.def.value = htab->sfpr->size;
5854 h->elf.type = STT_FUNC;
5855 h->elf.def_regular = 1;
5857 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5859 if (htab->sfpr->contents == NULL)
5861 htab->sfpr->contents
5862 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5863 if (htab->sfpr->contents == NULL)
5870 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5872 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5874 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5875 htab->sfpr->size = p - htab->sfpr->contents;
5883 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5885 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5890 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5892 p = savegpr0 (abfd, p, r);
5893 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5895 bfd_put_32 (abfd, BLR, p);
5900 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5902 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5907 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5909 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5911 p = restgpr0 (abfd, p, r);
5912 bfd_put_32 (abfd, MTLR_R0, p);
5916 p = restgpr0 (abfd, p, 30);
5917 p = restgpr0 (abfd, p, 31);
5919 bfd_put_32 (abfd, BLR, p);
5924 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5926 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5931 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5933 p = savegpr1 (abfd, p, r);
5934 bfd_put_32 (abfd, BLR, p);
5939 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5941 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5946 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5948 p = restgpr1 (abfd, p, r);
5949 bfd_put_32 (abfd, BLR, p);
5954 savefpr (bfd *abfd, bfd_byte *p, int r)
5956 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5961 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5963 p = savefpr (abfd, p, r);
5964 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5966 bfd_put_32 (abfd, BLR, p);
5971 restfpr (bfd *abfd, bfd_byte *p, int r)
5973 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5978 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5980 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5982 p = restfpr (abfd, p, r);
5983 bfd_put_32 (abfd, MTLR_R0, p);
5987 p = restfpr (abfd, p, 30);
5988 p = restfpr (abfd, p, 31);
5990 bfd_put_32 (abfd, BLR, p);
5995 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5997 p = savefpr (abfd, p, r);
5998 bfd_put_32 (abfd, BLR, p);
6003 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6005 p = restfpr (abfd, p, r);
6006 bfd_put_32 (abfd, BLR, p);
6011 savevr (bfd *abfd, bfd_byte *p, int r)
6013 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6015 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6020 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6022 p = savevr (abfd, p, r);
6023 bfd_put_32 (abfd, BLR, p);
6028 restvr (bfd *abfd, bfd_byte *p, int r)
6030 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6032 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6037 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6039 p = restvr (abfd, p, r);
6040 bfd_put_32 (abfd, BLR, p);
6044 /* Called via elf_link_hash_traverse to transfer dynamic linking
6045 information on function code symbol entries to their corresponding
6046 function descriptor symbol entries. */
6049 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6051 struct bfd_link_info *info;
6052 struct ppc_link_hash_table *htab;
6053 struct ppc_link_hash_entry *fh;
6054 struct ppc_link_hash_entry *fdh;
6055 bfd_boolean force_local;
6057 fh = (struct ppc_link_hash_entry *) h;
6058 if (fh->elf.root.type == bfd_link_hash_indirect)
6064 if (fh->elf.root.root.string[0] != '.'
6065 || fh->elf.root.root.string[1] == '\0')
6069 htab = ppc_hash_table (info);
6073 /* Find the corresponding function descriptor symbol. */
6074 fdh = lookup_fdh (fh, htab);
6076 /* Resolve undefined references to dot-symbols as the value
6077 in the function descriptor, if we have one in a regular object.
6078 This is to satisfy cases like ".quad .foo". Calls to functions
6079 in dynamic objects are handled elsewhere. */
6080 if ((fh->elf.root.type == bfd_link_hash_undefined
6081 || fh->elf.root.type == bfd_link_hash_undefweak)
6082 && (fdh->elf.root.type == bfd_link_hash_defined
6083 || fdh->elf.root.type == bfd_link_hash_defweak)
6084 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6085 && opd_entry_value (fdh->elf.root.u.def.section,
6086 fdh->elf.root.u.def.value,
6087 &fh->elf.root.u.def.section,
6088 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6090 fh->elf.root.type = fdh->elf.root.type;
6091 fh->elf.forced_local = 1;
6092 fh->elf.def_regular = fdh->elf.def_regular;
6093 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6096 if (!fh->elf.dynamic)
6098 struct plt_entry *ent;
6100 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6101 if (ent->plt.refcount > 0)
6107 /* Create a descriptor as undefined if necessary. */
6109 && !bfd_link_executable (info)
6110 && (fh->elf.root.type == bfd_link_hash_undefined
6111 || fh->elf.root.type == bfd_link_hash_undefweak))
6113 fdh = make_fdh (info, fh);
6118 /* We can't support overriding of symbols on a fake descriptor. */
6121 && (fh->elf.root.type == bfd_link_hash_defined
6122 || fh->elf.root.type == bfd_link_hash_defweak))
6123 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6125 /* Transfer dynamic linking information to the function descriptor. */
6128 fdh->elf.ref_regular |= fh->elf.ref_regular;
6129 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6130 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6131 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6132 fdh->elf.dynamic |= fh->elf.dynamic;
6133 fdh->elf.needs_plt |= (fh->elf.needs_plt
6134 || fh->elf.type == STT_FUNC
6135 || fh->elf.type == STT_GNU_IFUNC);
6136 move_plt_plist (fh, fdh);
6138 if (!fdh->elf.forced_local
6139 && fh->elf.dynindx != -1)
6140 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6144 /* Now that the info is on the function descriptor, clear the
6145 function code sym info. Any function code syms for which we
6146 don't have a definition in a regular file, we force local.
6147 This prevents a shared library from exporting syms that have
6148 been imported from another library. Function code syms that
6149 are really in the library we must leave global to prevent the
6150 linker dragging in a definition from a static library. */
6151 force_local = (!fh->elf.def_regular
6153 || !fdh->elf.def_regular
6154 || fdh->elf.forced_local);
6155 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6160 static const struct sfpr_def_parms save_res_funcs[] =
6162 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6163 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6164 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6165 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6166 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6167 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6168 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6169 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6170 { "._savef", 14, 31, savefpr, savefpr1_tail },
6171 { "._restf", 14, 31, restfpr, restfpr1_tail },
6172 { "_savevr_", 20, 31, savevr, savevr_tail },
6173 { "_restvr_", 20, 31, restvr, restvr_tail }
6176 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6177 this hook to a) provide some gcc support functions, and b) transfer
6178 dynamic linking information gathered so far on function code symbol
6179 entries, to their corresponding function descriptor symbol entries. */
6182 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6183 struct bfd_link_info *info)
6185 struct ppc_link_hash_table *htab;
6187 htab = ppc_hash_table (info);
6191 /* Provide any missing _save* and _rest* functions. */
6192 if (htab->sfpr != NULL)
6196 htab->sfpr->size = 0;
6197 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6198 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6200 if (htab->sfpr->size == 0)
6201 htab->sfpr->flags |= SEC_EXCLUDE;
6204 if (bfd_link_relocatable (info))
6207 if (htab->elf.hgot != NULL)
6209 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6210 /* Make .TOC. defined so as to prevent it being made dynamic.
6211 The wrong value here is fixed later in ppc64_elf_set_toc. */
6212 if (!htab->elf.hgot->def_regular
6213 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6215 htab->elf.hgot->root.type = bfd_link_hash_defined;
6216 htab->elf.hgot->root.u.def.value = 0;
6217 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6218 htab->elf.hgot->def_regular = 1;
6219 htab->elf.hgot->root.linker_def = 1;
6221 htab->elf.hgot->type = STT_OBJECT;
6222 htab->elf.hgot->other
6223 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6226 if (htab->need_func_desc_adj)
6228 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6229 htab->need_func_desc_adj = 0;
6235 /* Find dynamic relocs for H that apply to read-only sections. */
6238 readonly_dynrelocs (struct elf_link_hash_entry *h)
6240 struct ppc_link_hash_entry *eh;
6241 struct elf_dyn_relocs *p;
6243 eh = (struct ppc_link_hash_entry *) h;
6244 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6246 asection *s = p->sec->output_section;
6248 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6254 /* Return true if we have dynamic relocs against H or any of its weak
6255 aliases, that apply to read-only sections. Cannot be used after
6256 size_dynamic_sections. */
6259 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6261 struct ppc_link_hash_entry *eh;
6263 eh = (struct ppc_link_hash_entry *) h;
6266 if (readonly_dynrelocs (&eh->elf))
6268 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
6270 while (eh != NULL && &eh->elf != h);
6275 /* Return whether EH has pc-relative dynamic relocs. */
6278 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6280 struct elf_dyn_relocs *p;
6282 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6283 if (p->pc_count != 0)
6288 /* Return true if a global entry stub will be created for H. Valid
6289 for ELFv2 before plt entries have been allocated. */
6292 global_entry_stub (struct elf_link_hash_entry *h)
6294 struct plt_entry *pent;
6296 if (!h->pointer_equality_needed
6300 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6301 if (pent->plt.refcount > 0
6302 && pent->addend == 0)
6308 /* Adjust a symbol defined by a dynamic object and referenced by a
6309 regular object. The current definition is in some section of the
6310 dynamic object, but we're not including those sections. We have to
6311 change the definition to something the rest of the link can
6315 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6316 struct elf_link_hash_entry *h)
6318 struct ppc_link_hash_table *htab;
6321 htab = ppc_hash_table (info);
6325 /* Deal with function syms. */
6326 if (h->type == STT_FUNC
6327 || h->type == STT_GNU_IFUNC
6330 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6331 || SYMBOL_CALLS_LOCAL (info, h)
6332 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6333 /* Discard dyn_relocs when non-pic if we've decided that a
6334 function symbol is local and not an ifunc. We keep dynamic
6335 relocs for ifuncs when local rather than always emitting a
6336 plt call stub for them and defining the symbol on the call
6337 stub. We can't do that for ELFv1 anyway (a function symbol
6338 is defined on a descriptor, not code) and it can be faster at
6339 run-time due to not needing to bounce through a stub. The
6340 dyn_relocs for ifuncs will be applied even in a static
6342 if (!bfd_link_pic (info)
6343 && h->type != STT_GNU_IFUNC
6345 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6347 /* Clear procedure linkage table information for any symbol that
6348 won't need a .plt entry. */
6349 struct plt_entry *ent;
6350 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6351 if (ent->plt.refcount > 0)
6354 || (h->type != STT_GNU_IFUNC
6356 && (htab->can_convert_all_inline_plt
6357 || (((struct ppc_link_hash_entry *) h)->tls_mask
6358 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6360 h->plt.plist = NULL;
6362 h->pointer_equality_needed = 0;
6364 else if (abiversion (info->output_bfd) >= 2)
6366 /* Taking a function's address in a read/write section
6367 doesn't require us to define the function symbol in the
6368 executable on a global entry stub. A dynamic reloc can
6369 be used instead. The reason we prefer a few more dynamic
6370 relocs is that calling via a global entry stub costs a
6371 few more instructions, and pointer_equality_needed causes
6372 extra work in ld.so when resolving these symbols. */
6373 if (global_entry_stub (h))
6375 if (!readonly_dynrelocs (h))
6377 h->pointer_equality_needed = 0;
6378 /* If we haven't seen a branch reloc and the symbol
6379 isn't an ifunc then we don't need a plt entry. */
6381 h->plt.plist = NULL;
6383 else if (!bfd_link_pic (info))
6384 /* We are going to be defining the function symbol on the
6385 plt stub, so no dyn_relocs needed when non-pic. */
6386 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6389 /* ELFv2 function symbols can't have copy relocs. */
6392 else if (!h->needs_plt
6393 && !readonly_dynrelocs (h))
6395 /* If we haven't seen a branch reloc and the symbol isn't an
6396 ifunc then we don't need a plt entry. */
6397 h->plt.plist = NULL;
6398 h->pointer_equality_needed = 0;
6403 h->plt.plist = NULL;
6405 /* If this is a weak symbol, and there is a real definition, the
6406 processor independent code will have arranged for us to see the
6407 real definition first, and we can just use the same value. */
6408 if (h->is_weakalias)
6410 struct elf_link_hash_entry *def = weakdef (h);
6411 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6412 h->root.u.def.section = def->root.u.def.section;
6413 h->root.u.def.value = def->root.u.def.value;
6414 if (def->root.u.def.section == htab->elf.sdynbss
6415 || def->root.u.def.section == htab->elf.sdynrelro)
6416 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6420 /* If we are creating a shared library, we must presume that the
6421 only references to the symbol are via the global offset table.
6422 For such cases we need not do anything here; the relocations will
6423 be handled correctly by relocate_section. */
6424 if (bfd_link_pic (info))
6427 /* If there are no references to this symbol that do not use the
6428 GOT, we don't need to generate a copy reloc. */
6429 if (!h->non_got_ref)
6432 /* Don't generate a copy reloc for symbols defined in the executable. */
6433 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6435 /* If -z nocopyreloc was given, don't generate them either. */
6436 || info->nocopyreloc
6438 /* If we don't find any dynamic relocs in read-only sections, then
6439 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6440 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
6442 /* Protected variables do not work with .dynbss. The copy in
6443 .dynbss won't be used by the shared library with the protected
6444 definition for the variable. Text relocations are preferable
6445 to an incorrect program. */
6446 || h->protected_def)
6449 if (h->plt.plist != NULL)
6451 /* We should never get here, but unfortunately there are versions
6452 of gcc out there that improperly (for this ABI) put initialized
6453 function pointers, vtable refs and suchlike in read-only
6454 sections. Allow them to proceed, but warn that this might
6455 break at runtime. */
6456 info->callbacks->einfo
6457 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6458 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6459 h->root.root.string);
6462 /* This is a reference to a symbol defined by a dynamic object which
6463 is not a function. */
6465 /* We must allocate the symbol in our .dynbss section, which will
6466 become part of the .bss section of the executable. There will be
6467 an entry for this symbol in the .dynsym section. The dynamic
6468 object will contain position independent code, so all references
6469 from the dynamic object to this symbol will go through the global
6470 offset table. The dynamic linker will use the .dynsym entry to
6471 determine the address it must put in the global offset table, so
6472 both the dynamic object and the regular object will refer to the
6473 same memory location for the variable. */
6474 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6476 s = htab->elf.sdynrelro;
6477 srel = htab->elf.sreldynrelro;
6481 s = htab->elf.sdynbss;
6482 srel = htab->elf.srelbss;
6484 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6486 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6487 linker to copy the initial value out of the dynamic object
6488 and into the runtime process image. */
6489 srel->size += sizeof (Elf64_External_Rela);
6493 /* We no longer want dyn_relocs. */
6494 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6495 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6498 /* If given a function descriptor symbol, hide both the function code
6499 sym and the descriptor. */
6501 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6502 struct elf_link_hash_entry *h,
6503 bfd_boolean force_local)
6505 struct ppc_link_hash_entry *eh;
6506 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6508 if (ppc_hash_table (info) == NULL)
6511 eh = (struct ppc_link_hash_entry *) h;
6512 if (eh->is_func_descriptor)
6514 struct ppc_link_hash_entry *fh = eh->oh;
6519 struct elf_link_hash_table *htab = elf_hash_table (info);
6522 /* We aren't supposed to use alloca in BFD because on
6523 systems which do not have alloca the version in libiberty
6524 calls xmalloc, which might cause the program to crash
6525 when it runs out of memory. This function doesn't have a
6526 return status, so there's no way to gracefully return an
6527 error. So cheat. We know that string[-1] can be safely
6528 accessed; It's either a string in an ELF string table,
6529 or allocated in an objalloc structure. */
6531 p = eh->elf.root.root.string - 1;
6534 fh = (struct ppc_link_hash_entry *)
6535 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6538 /* Unfortunately, if it so happens that the string we were
6539 looking for was allocated immediately before this string,
6540 then we overwrote the string terminator. That's the only
6541 reason the lookup should fail. */
6544 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6545 while (q >= eh->elf.root.root.string && *q == *p)
6547 if (q < eh->elf.root.root.string && *p == '.')
6548 fh = (struct ppc_link_hash_entry *)
6549 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6558 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6563 get_sym_h (struct elf_link_hash_entry **hp,
6564 Elf_Internal_Sym **symp,
6566 unsigned char **tls_maskp,
6567 Elf_Internal_Sym **locsymsp,
6568 unsigned long r_symndx,
6571 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6573 if (r_symndx >= symtab_hdr->sh_info)
6575 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6576 struct elf_link_hash_entry *h;
6578 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6579 h = elf_follow_link (h);
6587 if (symsecp != NULL)
6589 asection *symsec = NULL;
6590 if (h->root.type == bfd_link_hash_defined
6591 || h->root.type == bfd_link_hash_defweak)
6592 symsec = h->root.u.def.section;
6596 if (tls_maskp != NULL)
6598 struct ppc_link_hash_entry *eh;
6600 eh = (struct ppc_link_hash_entry *) h;
6601 *tls_maskp = &eh->tls_mask;
6606 Elf_Internal_Sym *sym;
6607 Elf_Internal_Sym *locsyms = *locsymsp;
6609 if (locsyms == NULL)
6611 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6612 if (locsyms == NULL)
6613 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6614 symtab_hdr->sh_info,
6615 0, NULL, NULL, NULL);
6616 if (locsyms == NULL)
6618 *locsymsp = locsyms;
6620 sym = locsyms + r_symndx;
6628 if (symsecp != NULL)
6629 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6631 if (tls_maskp != NULL)
6633 struct got_entry **lgot_ents;
6634 unsigned char *tls_mask;
6637 lgot_ents = elf_local_got_ents (ibfd);
6638 if (lgot_ents != NULL)
6640 struct plt_entry **local_plt = (struct plt_entry **)
6641 (lgot_ents + symtab_hdr->sh_info);
6642 unsigned char *lgot_masks = (unsigned char *)
6643 (local_plt + symtab_hdr->sh_info);
6644 tls_mask = &lgot_masks[r_symndx];
6646 *tls_maskp = tls_mask;
6652 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6653 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6654 type suitable for optimization, and 1 otherwise. */
6657 get_tls_mask (unsigned char **tls_maskp,
6658 unsigned long *toc_symndx,
6659 bfd_vma *toc_addend,
6660 Elf_Internal_Sym **locsymsp,
6661 const Elf_Internal_Rela *rel,
6664 unsigned long r_symndx;
6666 struct elf_link_hash_entry *h;
6667 Elf_Internal_Sym *sym;
6671 r_symndx = ELF64_R_SYM (rel->r_info);
6672 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6675 if ((*tls_maskp != NULL
6676 && (**tls_maskp & TLS_TLS) != 0
6677 && **tls_maskp != (TLS_TLS | TLS_MARK))
6679 || ppc64_elf_section_data (sec) == NULL
6680 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6683 /* Look inside a TOC section too. */
6686 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6687 off = h->root.u.def.value;
6690 off = sym->st_value;
6691 off += rel->r_addend;
6692 BFD_ASSERT (off % 8 == 0);
6693 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6694 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6695 if (toc_symndx != NULL)
6696 *toc_symndx = r_symndx;
6697 if (toc_addend != NULL)
6698 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6699 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6701 if ((h == NULL || is_static_defined (h))
6702 && (next_r == -1 || next_r == -2))
6707 /* Find (or create) an entry in the tocsave hash table. */
6709 static struct tocsave_entry *
6710 tocsave_find (struct ppc_link_hash_table *htab,
6711 enum insert_option insert,
6712 Elf_Internal_Sym **local_syms,
6713 const Elf_Internal_Rela *irela,
6716 unsigned long r_indx;
6717 struct elf_link_hash_entry *h;
6718 Elf_Internal_Sym *sym;
6719 struct tocsave_entry ent, *p;
6721 struct tocsave_entry **slot;
6723 r_indx = ELF64_R_SYM (irela->r_info);
6724 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6726 if (ent.sec == NULL || ent.sec->output_section == NULL)
6729 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
6734 ent.offset = h->root.u.def.value;
6736 ent.offset = sym->st_value;
6737 ent.offset += irela->r_addend;
6739 hash = tocsave_htab_hash (&ent);
6740 slot = ((struct tocsave_entry **)
6741 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6747 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6756 /* Adjust all global syms defined in opd sections. In gcc generated
6757 code for the old ABI, these will already have been done. */
6760 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6762 struct ppc_link_hash_entry *eh;
6764 struct _opd_sec_data *opd;
6766 if (h->root.type == bfd_link_hash_indirect)
6769 if (h->root.type != bfd_link_hash_defined
6770 && h->root.type != bfd_link_hash_defweak)
6773 eh = (struct ppc_link_hash_entry *) h;
6774 if (eh->adjust_done)
6777 sym_sec = eh->elf.root.u.def.section;
6778 opd = get_opd_info (sym_sec);
6779 if (opd != NULL && opd->adjust != NULL)
6781 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
6784 /* This entry has been deleted. */
6785 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6788 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6789 if (discarded_section (dsec))
6791 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6795 eh->elf.root.u.def.value = 0;
6796 eh->elf.root.u.def.section = dsec;
6799 eh->elf.root.u.def.value += adjust;
6800 eh->adjust_done = 1;
6805 /* Handles decrementing dynamic reloc counts for the reloc specified by
6806 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
6807 have already been determined. */
6810 dec_dynrel_count (bfd_vma r_info,
6812 struct bfd_link_info *info,
6813 Elf_Internal_Sym **local_syms,
6814 struct elf_link_hash_entry *h,
6815 Elf_Internal_Sym *sym)
6817 enum elf_ppc64_reloc_type r_type;
6818 asection *sym_sec = NULL;
6820 /* Can this reloc be dynamic? This switch, and later tests here
6821 should be kept in sync with the code in check_relocs. */
6822 r_type = ELF64_R_TYPE (r_info);
6828 case R_PPC64_TPREL16:
6829 case R_PPC64_TPREL16_LO:
6830 case R_PPC64_TPREL16_HI:
6831 case R_PPC64_TPREL16_HA:
6832 case R_PPC64_TPREL16_DS:
6833 case R_PPC64_TPREL16_LO_DS:
6834 case R_PPC64_TPREL16_HIGH:
6835 case R_PPC64_TPREL16_HIGHA:
6836 case R_PPC64_TPREL16_HIGHER:
6837 case R_PPC64_TPREL16_HIGHERA:
6838 case R_PPC64_TPREL16_HIGHEST:
6839 case R_PPC64_TPREL16_HIGHESTA:
6840 case R_PPC64_TPREL64:
6841 case R_PPC64_TPREL34:
6842 case R_PPC64_DTPMOD64:
6843 case R_PPC64_DTPREL64:
6844 case R_PPC64_ADDR64:
6848 case R_PPC64_ADDR14:
6849 case R_PPC64_ADDR14_BRNTAKEN:
6850 case R_PPC64_ADDR14_BRTAKEN:
6851 case R_PPC64_ADDR16:
6852 case R_PPC64_ADDR16_DS:
6853 case R_PPC64_ADDR16_HA:
6854 case R_PPC64_ADDR16_HI:
6855 case R_PPC64_ADDR16_HIGH:
6856 case R_PPC64_ADDR16_HIGHA:
6857 case R_PPC64_ADDR16_HIGHER:
6858 case R_PPC64_ADDR16_HIGHERA:
6859 case R_PPC64_ADDR16_HIGHEST:
6860 case R_PPC64_ADDR16_HIGHESTA:
6861 case R_PPC64_ADDR16_LO:
6862 case R_PPC64_ADDR16_LO_DS:
6863 case R_PPC64_ADDR24:
6864 case R_PPC64_ADDR32:
6865 case R_PPC64_UADDR16:
6866 case R_PPC64_UADDR32:
6867 case R_PPC64_UADDR64:
6870 case R_PPC64_D34_LO:
6871 case R_PPC64_D34_HI30:
6872 case R_PPC64_D34_HA30:
6873 case R_PPC64_ADDR16_HIGHER34:
6874 case R_PPC64_ADDR16_HIGHERA34:
6875 case R_PPC64_ADDR16_HIGHEST34:
6876 case R_PPC64_ADDR16_HIGHESTA34:
6881 if (local_syms != NULL)
6883 unsigned long r_symndx;
6884 bfd *ibfd = sec->owner;
6886 r_symndx = ELF64_R_SYM (r_info);
6887 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6891 if ((bfd_link_pic (info)
6892 && (must_be_dyn_reloc (info, r_type)
6894 && (!SYMBOLIC_BIND (info, h)
6895 || h->root.type == bfd_link_hash_defweak
6896 || !h->def_regular))))
6897 || (ELIMINATE_COPY_RELOCS
6898 && !bfd_link_pic (info)
6900 && (h->root.type == bfd_link_hash_defweak
6901 || !h->def_regular)))
6908 struct elf_dyn_relocs *p;
6909 struct elf_dyn_relocs **pp;
6910 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6912 /* elf_gc_sweep may have already removed all dyn relocs associated
6913 with local syms for a given section. Also, symbol flags are
6914 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6915 report a dynreloc miscount. */
6916 if (*pp == NULL && info->gc_sections)
6919 while ((p = *pp) != NULL)
6923 if (!must_be_dyn_reloc (info, r_type))
6935 struct ppc_dyn_relocs *p;
6936 struct ppc_dyn_relocs **pp;
6938 bfd_boolean is_ifunc;
6940 if (local_syms == NULL)
6941 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6942 if (sym_sec == NULL)
6945 vpp = &elf_section_data (sym_sec)->local_dynrel;
6946 pp = (struct ppc_dyn_relocs **) vpp;
6948 if (*pp == NULL && info->gc_sections)
6951 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6952 while ((p = *pp) != NULL)
6954 if (p->sec == sec && p->ifunc == is_ifunc)
6965 /* xgettext:c-format */
6966 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6968 bfd_set_error (bfd_error_bad_value);
6972 /* Remove unused Official Procedure Descriptor entries. Currently we
6973 only remove those associated with functions in discarded link-once
6974 sections, or weakly defined functions that have been overridden. It
6975 would be possible to remove many more entries for statically linked
6979 ppc64_elf_edit_opd (struct bfd_link_info *info)
6982 bfd_boolean some_edited = FALSE;
6983 asection *need_pad = NULL;
6984 struct ppc_link_hash_table *htab;
6986 htab = ppc_hash_table (info);
6990 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6993 Elf_Internal_Rela *relstart, *rel, *relend;
6994 Elf_Internal_Shdr *symtab_hdr;
6995 Elf_Internal_Sym *local_syms;
6996 struct _opd_sec_data *opd;
6997 bfd_boolean need_edit, add_aux_fields, broken;
6998 bfd_size_type cnt_16b = 0;
7000 if (!is_ppc64_elf (ibfd))
7003 sec = bfd_get_section_by_name (ibfd, ".opd");
7004 if (sec == NULL || sec->size == 0)
7007 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7010 if (sec->output_section == bfd_abs_section_ptr)
7013 /* Look through the section relocs. */
7014 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7018 symtab_hdr = &elf_symtab_hdr (ibfd);
7020 /* Read the relocations. */
7021 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7023 if (relstart == NULL)
7026 /* First run through the relocs to check they are sane, and to
7027 determine whether we need to edit this opd section. */
7031 relend = relstart + sec->reloc_count;
7032 for (rel = relstart; rel < relend; )
7034 enum elf_ppc64_reloc_type r_type;
7035 unsigned long r_symndx;
7037 struct elf_link_hash_entry *h;
7038 Elf_Internal_Sym *sym;
7041 /* .opd contains an array of 16 or 24 byte entries. We're
7042 only interested in the reloc pointing to a function entry
7044 offset = rel->r_offset;
7045 if (rel + 1 == relend
7046 || rel[1].r_offset != offset + 8)
7048 /* If someone messes with .opd alignment then after a
7049 "ld -r" we might have padding in the middle of .opd.
7050 Also, there's nothing to prevent someone putting
7051 something silly in .opd with the assembler. No .opd
7052 optimization for them! */
7055 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7060 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7061 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7064 /* xgettext:c-format */
7065 (_("%pB: unexpected reloc type %u in .opd section"),
7071 r_symndx = ELF64_R_SYM (rel->r_info);
7072 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7076 if (sym_sec == NULL || sym_sec->owner == NULL)
7078 const char *sym_name;
7080 sym_name = h->root.root.string;
7082 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7086 /* xgettext:c-format */
7087 (_("%pB: undefined sym `%s' in .opd section"),
7093 /* opd entries are always for functions defined in the
7094 current input bfd. If the symbol isn't defined in the
7095 input bfd, then we won't be using the function in this
7096 bfd; It must be defined in a linkonce section in another
7097 bfd, or is weak. It's also possible that we are
7098 discarding the function due to a linker script /DISCARD/,
7099 which we test for via the output_section. */
7100 if (sym_sec->owner != ibfd
7101 || sym_sec->output_section == bfd_abs_section_ptr)
7105 if (rel + 1 == relend
7106 || (rel + 2 < relend
7107 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7112 if (sec->size == offset + 24)
7117 if (sec->size == offset + 16)
7124 else if (rel + 1 < relend
7125 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7126 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7128 if (rel[0].r_offset == offset + 16)
7130 else if (rel[0].r_offset != offset + 24)
7137 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7139 if (!broken && (need_edit || add_aux_fields))
7141 Elf_Internal_Rela *write_rel;
7142 Elf_Internal_Shdr *rel_hdr;
7143 bfd_byte *rptr, *wptr;
7144 bfd_byte *new_contents;
7147 new_contents = NULL;
7148 amt = OPD_NDX (sec->size) * sizeof (long);
7149 opd = &ppc64_elf_section_data (sec)->u.opd;
7150 opd->adjust = bfd_zalloc (sec->owner, amt);
7151 if (opd->adjust == NULL)
7154 /* This seems a waste of time as input .opd sections are all
7155 zeros as generated by gcc, but I suppose there's no reason
7156 this will always be so. We might start putting something in
7157 the third word of .opd entries. */
7158 if ((sec->flags & SEC_IN_MEMORY) == 0)
7161 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7166 if (local_syms != NULL
7167 && symtab_hdr->contents != (unsigned char *) local_syms)
7169 if (elf_section_data (sec)->relocs != relstart)
7173 sec->contents = loc;
7174 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7177 elf_section_data (sec)->relocs = relstart;
7179 new_contents = sec->contents;
7182 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7183 if (new_contents == NULL)
7187 wptr = new_contents;
7188 rptr = sec->contents;
7189 write_rel = relstart;
7190 for (rel = relstart; rel < relend; )
7192 unsigned long r_symndx;
7194 struct elf_link_hash_entry *h;
7195 struct ppc_link_hash_entry *fdh = NULL;
7196 Elf_Internal_Sym *sym;
7198 Elf_Internal_Rela *next_rel;
7201 r_symndx = ELF64_R_SYM (rel->r_info);
7202 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7207 if (next_rel + 1 == relend
7208 || (next_rel + 2 < relend
7209 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7212 /* See if the .opd entry is full 24 byte or
7213 16 byte (with fd_aux entry overlapped with next
7216 if (next_rel == relend)
7218 if (sec->size == rel->r_offset + 16)
7221 else if (next_rel->r_offset == rel->r_offset + 16)
7225 && h->root.root.string[0] == '.')
7227 fdh = ((struct ppc_link_hash_entry *) h)->oh;
7230 fdh = ppc_follow_link (fdh);
7231 if (fdh->elf.root.type != bfd_link_hash_defined
7232 && fdh->elf.root.type != bfd_link_hash_defweak)
7237 skip = (sym_sec->owner != ibfd
7238 || sym_sec->output_section == bfd_abs_section_ptr);
7241 if (fdh != NULL && sym_sec->owner == ibfd)
7243 /* Arrange for the function descriptor sym
7245 fdh->elf.root.u.def.value = 0;
7246 fdh->elf.root.u.def.section = sym_sec;
7248 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7250 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7255 if (!dec_dynrel_count (rel->r_info, sec, info,
7259 if (++rel == next_rel)
7262 r_symndx = ELF64_R_SYM (rel->r_info);
7263 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7270 /* We'll be keeping this opd entry. */
7275 /* Redefine the function descriptor symbol to
7276 this location in the opd section. It is
7277 necessary to update the value here rather
7278 than using an array of adjustments as we do
7279 for local symbols, because various places
7280 in the generic ELF code use the value
7281 stored in u.def.value. */
7282 fdh->elf.root.u.def.value = wptr - new_contents;
7283 fdh->adjust_done = 1;
7286 /* Local syms are a bit tricky. We could
7287 tweak them as they can be cached, but
7288 we'd need to look through the local syms
7289 for the function descriptor sym which we
7290 don't have at the moment. So keep an
7291 array of adjustments. */
7292 adjust = (wptr - new_contents) - (rptr - sec->contents);
7293 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7296 memcpy (wptr, rptr, opd_ent_size);
7297 wptr += opd_ent_size;
7298 if (add_aux_fields && opd_ent_size == 16)
7300 memset (wptr, '\0', 8);
7304 /* We need to adjust any reloc offsets to point to the
7306 for ( ; rel != next_rel; ++rel)
7308 rel->r_offset += adjust;
7309 if (write_rel != rel)
7310 memcpy (write_rel, rel, sizeof (*rel));
7315 rptr += opd_ent_size;
7318 sec->size = wptr - new_contents;
7319 sec->reloc_count = write_rel - relstart;
7322 free (sec->contents);
7323 sec->contents = new_contents;
7326 /* Fudge the header size too, as this is used later in
7327 elf_bfd_final_link if we are emitting relocs. */
7328 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7329 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7332 else if (elf_section_data (sec)->relocs != relstart)
7335 if (local_syms != NULL
7336 && symtab_hdr->contents != (unsigned char *) local_syms)
7338 if (!info->keep_memory)
7341 symtab_hdr->contents = (unsigned char *) local_syms;
7346 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7348 /* If we are doing a final link and the last .opd entry is just 16 byte
7349 long, add a 8 byte padding after it. */
7350 if (need_pad != NULL && !bfd_link_relocatable (info))
7354 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7356 BFD_ASSERT (need_pad->size > 0);
7358 p = bfd_malloc (need_pad->size + 8);
7362 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7363 p, 0, need_pad->size))
7366 need_pad->contents = p;
7367 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7371 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7375 need_pad->contents = p;
7378 memset (need_pad->contents + need_pad->size, 0, 8);
7379 need_pad->size += 8;
7385 /* Analyze inline PLT call relocations to see whether calls to locally
7386 defined functions can be converted to direct calls. */
7389 ppc64_elf_inline_plt (struct bfd_link_info *info)
7391 struct ppc_link_hash_table *htab;
7394 bfd_vma low_vma, high_vma, limit;
7396 htab = ppc_hash_table (info);
7400 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7401 reduced somewhat to cater for possible stubs that might be added
7402 between the call and its destination. */
7403 if (htab->params->group_size < 0)
7405 limit = -htab->params->group_size;
7411 limit = htab->params->group_size;
7418 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7419 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7421 if (low_vma > sec->vma)
7423 if (high_vma < sec->vma + sec->size)
7424 high_vma = sec->vma + sec->size;
7427 /* If a "bl" can reach anywhere in local code sections, then we can
7428 convert all inline PLT sequences to direct calls when the symbol
7430 if (high_vma - low_vma < limit)
7432 htab->can_convert_all_inline_plt = 1;
7436 /* Otherwise, go looking through relocs for cases where a direct
7437 call won't reach. Mark the symbol on any such reloc to disable
7438 the optimization and keep the PLT entry as it seems likely that
7439 this will be better than creating trampolines. Note that this
7440 will disable the optimization for all inline PLT calls to a
7441 particular symbol, not just those that won't reach. The
7442 difficulty in doing a more precise optimization is that the
7443 linker needs to make a decision depending on whether a
7444 particular R_PPC64_PLTCALL insn can be turned into a direct
7445 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7446 the sequence, and there is nothing that ties those relocs
7447 together except their symbol. */
7449 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7451 Elf_Internal_Shdr *symtab_hdr;
7452 Elf_Internal_Sym *local_syms;
7454 if (!is_ppc64_elf (ibfd))
7458 symtab_hdr = &elf_symtab_hdr (ibfd);
7460 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7461 if (ppc64_elf_section_data (sec)->has_pltcall
7462 && !bfd_is_abs_section (sec->output_section))
7464 Elf_Internal_Rela *relstart, *rel, *relend;
7466 /* Read the relocations. */
7467 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7469 if (relstart == NULL)
7472 relend = relstart + sec->reloc_count;
7473 for (rel = relstart; rel < relend; )
7475 enum elf_ppc64_reloc_type r_type;
7476 unsigned long r_symndx;
7478 struct elf_link_hash_entry *h;
7479 Elf_Internal_Sym *sym;
7480 unsigned char *tls_maskp;
7482 r_type = ELF64_R_TYPE (rel->r_info);
7483 if (r_type != R_PPC64_PLTCALL
7484 && r_type != R_PPC64_PLTCALL_NOTOC)
7487 r_symndx = ELF64_R_SYM (rel->r_info);
7488 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7491 if (elf_section_data (sec)->relocs != relstart)
7493 if (local_syms != NULL
7494 && symtab_hdr->contents != (bfd_byte *) local_syms)
7499 if (sym_sec != NULL && sym_sec->output_section != NULL)
7503 to = h->root.u.def.value;
7506 to += (rel->r_addend
7507 + sym_sec->output_offset
7508 + sym_sec->output_section->vma);
7509 from = (rel->r_offset
7510 + sec->output_offset
7511 + sec->output_section->vma);
7512 if (to - from + limit < 2 * limit
7513 && !(r_type == R_PPC64_PLTCALL_NOTOC
7514 && (((h ? h->other : sym->st_other)
7515 & STO_PPC64_LOCAL_MASK)
7516 > 1 << STO_PPC64_LOCAL_BIT)))
7517 *tls_maskp &= ~PLT_KEEP;
7520 if (elf_section_data (sec)->relocs != relstart)
7524 if (local_syms != NULL
7525 && symtab_hdr->contents != (unsigned char *) local_syms)
7527 if (!info->keep_memory)
7530 symtab_hdr->contents = (unsigned char *) local_syms;
7537 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7540 ppc64_elf_tls_setup (struct bfd_link_info *info)
7542 struct ppc_link_hash_table *htab;
7544 htab = ppc_hash_table (info);
7548 if (abiversion (info->output_bfd) == 1)
7551 if (htab->params->no_multi_toc)
7552 htab->do_multi_toc = 0;
7553 else if (!htab->do_multi_toc)
7554 htab->params->no_multi_toc = 1;
7556 /* Default to --no-plt-localentry, as this option can cause problems
7557 with symbol interposition. For example, glibc libpthread.so and
7558 libc.so duplicate many pthread symbols, with a fallback
7559 implementation in libc.so. In some cases the fallback does more
7560 work than the pthread implementation. __pthread_condattr_destroy
7561 is one such symbol: the libpthread.so implementation is
7562 localentry:0 while the libc.so implementation is localentry:8.
7563 An app that "cleverly" uses dlopen to only load necessary
7564 libraries at runtime may omit loading libpthread.so when not
7565 running multi-threaded, which then results in the libc.so
7566 fallback symbols being used and ld.so complaining. Now there
7567 are workarounds in ld (see non_zero_localentry) to detect the
7568 pthread situation, but that may not be the only case where
7569 --plt-localentry can cause trouble. */
7570 if (htab->params->plt_localentry0 < 0)
7571 htab->params->plt_localentry0 = 0;
7572 if (htab->params->plt_localentry0
7573 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7574 FALSE, FALSE, FALSE) == NULL)
7576 (_("warning: --plt-localentry is especially dangerous without "
7577 "ld.so support to detect ABI violations"));
7579 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7580 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7581 FALSE, FALSE, TRUE));
7582 /* Move dynamic linking info to the function descriptor sym. */
7583 if (htab->tls_get_addr != NULL)
7584 func_desc_adjust (&htab->tls_get_addr->elf, info);
7585 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7586 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7587 FALSE, FALSE, TRUE));
7588 if (htab->params->tls_get_addr_opt)
7590 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7592 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7593 FALSE, FALSE, TRUE);
7595 func_desc_adjust (opt, info);
7596 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7597 FALSE, FALSE, TRUE);
7599 && (opt_fd->root.type == bfd_link_hash_defined
7600 || opt_fd->root.type == bfd_link_hash_defweak))
7602 /* If glibc supports an optimized __tls_get_addr call stub,
7603 signalled by the presence of __tls_get_addr_opt, and we'll
7604 be calling __tls_get_addr via a plt call stub, then
7605 make __tls_get_addr point to __tls_get_addr_opt. */
7606 tga_fd = &htab->tls_get_addr_fd->elf;
7607 if (htab->elf.dynamic_sections_created
7609 && (tga_fd->type == STT_FUNC
7610 || tga_fd->needs_plt)
7611 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7612 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
7614 struct plt_entry *ent;
7616 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7617 if (ent->plt.refcount > 0)
7621 tga_fd->root.type = bfd_link_hash_indirect;
7622 tga_fd->root.u.i.link = &opt_fd->root;
7623 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7625 if (opt_fd->dynindx != -1)
7627 /* Use __tls_get_addr_opt in dynamic relocations. */
7628 opt_fd->dynindx = -1;
7629 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7630 opt_fd->dynstr_index);
7631 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7634 htab->tls_get_addr_fd
7635 = (struct ppc_link_hash_entry *) opt_fd;
7636 tga = &htab->tls_get_addr->elf;
7637 if (opt != NULL && tga != NULL)
7639 tga->root.type = bfd_link_hash_indirect;
7640 tga->root.u.i.link = &opt->root;
7641 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7643 _bfd_elf_link_hash_hide_symbol (info, opt,
7645 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7647 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7648 htab->tls_get_addr_fd->is_func_descriptor = 1;
7649 if (htab->tls_get_addr != NULL)
7651 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7652 htab->tls_get_addr->is_func = 1;
7657 else if (htab->params->tls_get_addr_opt < 0)
7658 htab->params->tls_get_addr_opt = 0;
7660 return _bfd_elf_tls_setup (info->output_bfd, info);
7663 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7667 branch_reloc_hash_match (const bfd *ibfd,
7668 const Elf_Internal_Rela *rel,
7669 const struct ppc_link_hash_entry *hash1,
7670 const struct ppc_link_hash_entry *hash2)
7672 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7673 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7674 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7676 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7678 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7679 struct elf_link_hash_entry *h;
7681 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7682 h = elf_follow_link (h);
7683 if (h == &hash1->elf || h == &hash2->elf)
7689 /* Run through all the TLS relocs looking for optimization
7690 opportunities. The linker has been hacked (see ppc64elf.em) to do
7691 a preliminary section layout so that we know the TLS segment
7692 offsets. We can't optimize earlier because some optimizations need
7693 to know the tp offset, and we need to optimize before allocating
7694 dynamic relocations. */
7697 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7701 struct ppc_link_hash_table *htab;
7702 unsigned char *toc_ref;
7705 if (!bfd_link_executable (info))
7708 htab = ppc_hash_table (info);
7712 /* Make two passes over the relocs. On the first pass, mark toc
7713 entries involved with tls relocs, and check that tls relocs
7714 involved in setting up a tls_get_addr call are indeed followed by
7715 such a call. If they are not, we can't do any tls optimization.
7716 On the second pass twiddle tls_mask flags to notify
7717 relocate_section that optimization can be done, and adjust got
7718 and plt refcounts. */
7720 for (pass = 0; pass < 2; ++pass)
7721 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7723 Elf_Internal_Sym *locsyms = NULL;
7724 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7726 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7727 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7729 Elf_Internal_Rela *relstart, *rel, *relend;
7730 bfd_boolean found_tls_get_addr_arg = 0;
7732 /* Read the relocations. */
7733 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7735 if (relstart == NULL)
7741 relend = relstart + sec->reloc_count;
7742 for (rel = relstart; rel < relend; rel++)
7744 enum elf_ppc64_reloc_type r_type;
7745 unsigned long r_symndx;
7746 struct elf_link_hash_entry *h;
7747 Elf_Internal_Sym *sym;
7749 unsigned char *tls_mask;
7750 unsigned int tls_set, tls_clear, tls_type = 0;
7752 bfd_boolean ok_tprel, is_local;
7753 long toc_ref_index = 0;
7754 int expecting_tls_get_addr = 0;
7755 bfd_boolean ret = FALSE;
7757 r_symndx = ELF64_R_SYM (rel->r_info);
7758 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7762 if (elf_section_data (sec)->relocs != relstart)
7764 if (toc_ref != NULL)
7767 && (elf_symtab_hdr (ibfd).contents
7768 != (unsigned char *) locsyms))
7775 if (h->root.type == bfd_link_hash_defined
7776 || h->root.type == bfd_link_hash_defweak)
7777 value = h->root.u.def.value;
7778 else if (h->root.type == bfd_link_hash_undefweak)
7782 found_tls_get_addr_arg = 0;
7787 /* Symbols referenced by TLS relocs must be of type
7788 STT_TLS. So no need for .opd local sym adjust. */
7789 value = sym->st_value;
7798 && h->root.type == bfd_link_hash_undefweak)
7800 else if (sym_sec != NULL
7801 && sym_sec->output_section != NULL)
7803 value += sym_sec->output_offset;
7804 value += sym_sec->output_section->vma;
7805 value -= htab->elf.tls_sec->vma + TP_OFFSET;
7806 /* Note that even though the prefix insns
7807 allow a 1<<33 offset we use the same test
7808 as for addis;addi. There may be a mix of
7809 pcrel and non-pcrel code and the decision
7810 to optimise is per symbol, not per TLS
7812 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
7816 r_type = ELF64_R_TYPE (rel->r_info);
7817 /* If this section has old-style __tls_get_addr calls
7818 without marker relocs, then check that each
7819 __tls_get_addr call reloc is preceded by a reloc
7820 that conceivably belongs to the __tls_get_addr arg
7821 setup insn. If we don't find matching arg setup
7822 relocs, don't do any tls optimization. */
7824 && sec->has_tls_get_addr_call
7826 && (h == &htab->tls_get_addr->elf
7827 || h == &htab->tls_get_addr_fd->elf)
7828 && !found_tls_get_addr_arg
7829 && is_branch_reloc (r_type))
7831 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7832 "TLS optimization disabled\n"),
7833 ibfd, sec, rel->r_offset);
7838 found_tls_get_addr_arg = 0;
7841 case R_PPC64_GOT_TLSLD16:
7842 case R_PPC64_GOT_TLSLD16_LO:
7843 case R_PPC64_GOT_TLSLD34:
7844 expecting_tls_get_addr = 1;
7845 found_tls_get_addr_arg = 1;
7848 case R_PPC64_GOT_TLSLD16_HI:
7849 case R_PPC64_GOT_TLSLD16_HA:
7850 /* These relocs should never be against a symbol
7851 defined in a shared lib. Leave them alone if
7852 that turns out to be the case. */
7859 tls_type = TLS_TLS | TLS_LD;
7862 case R_PPC64_GOT_TLSGD16:
7863 case R_PPC64_GOT_TLSGD16_LO:
7864 case R_PPC64_GOT_TLSGD34:
7865 expecting_tls_get_addr = 1;
7866 found_tls_get_addr_arg = 1;
7869 case R_PPC64_GOT_TLSGD16_HI:
7870 case R_PPC64_GOT_TLSGD16_HA:
7876 tls_set = TLS_TLS | TLS_GDIE;
7878 tls_type = TLS_TLS | TLS_GD;
7881 case R_PPC64_GOT_TPREL34:
7882 case R_PPC64_GOT_TPREL16_DS:
7883 case R_PPC64_GOT_TPREL16_LO_DS:
7884 case R_PPC64_GOT_TPREL16_HI:
7885 case R_PPC64_GOT_TPREL16_HA:
7890 tls_clear = TLS_TPREL;
7891 tls_type = TLS_TLS | TLS_TPREL;
7898 if (rel + 1 < relend
7899 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7902 && (ELF64_R_TYPE (rel[1].r_info)
7904 && (ELF64_R_TYPE (rel[1].r_info)
7905 != R_PPC64_PLTSEQ_NOTOC))
7907 r_symndx = ELF64_R_SYM (rel[1].r_info);
7908 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
7913 struct plt_entry *ent = NULL;
7915 for (ent = h->plt.plist;
7918 if (ent->addend == rel[1].r_addend)
7922 && ent->plt.refcount > 0)
7923 ent->plt.refcount -= 1;
7928 found_tls_get_addr_arg = 1;
7933 case R_PPC64_TOC16_LO:
7934 if (sym_sec == NULL || sym_sec != toc)
7937 /* Mark this toc entry as referenced by a TLS
7938 code sequence. We can do that now in the
7939 case of R_PPC64_TLS, and after checking for
7940 tls_get_addr for the TOC16 relocs. */
7941 if (toc_ref == NULL)
7943 = bfd_zmalloc (toc->output_section->rawsize / 8);
7944 if (toc_ref == NULL)
7948 value = h->root.u.def.value;
7950 value = sym->st_value;
7951 value += rel->r_addend;
7954 BFD_ASSERT (value < toc->size
7955 && toc->output_offset % 8 == 0);
7956 toc_ref_index = (value + toc->output_offset) / 8;
7957 if (r_type == R_PPC64_TLS
7958 || r_type == R_PPC64_TLSGD
7959 || r_type == R_PPC64_TLSLD)
7961 toc_ref[toc_ref_index] = 1;
7965 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7970 expecting_tls_get_addr = 2;
7973 case R_PPC64_TPREL64:
7977 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7982 tls_set = TLS_EXPLICIT;
7983 tls_clear = TLS_TPREL;
7988 case R_PPC64_DTPMOD64:
7992 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7994 if (rel + 1 < relend
7996 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7997 && rel[1].r_offset == rel->r_offset + 8)
8001 tls_set = TLS_EXPLICIT | TLS_GD;
8004 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8013 tls_set = TLS_EXPLICIT;
8024 if (!expecting_tls_get_addr
8025 || !sec->has_tls_get_addr_call)
8028 if (rel + 1 < relend
8029 && branch_reloc_hash_match (ibfd, rel + 1,
8031 htab->tls_get_addr_fd))
8033 if (expecting_tls_get_addr == 2)
8035 /* Check for toc tls entries. */
8036 unsigned char *toc_tls;
8039 retval = get_tls_mask (&toc_tls, NULL, NULL,
8044 if (toc_tls != NULL)
8046 if ((*toc_tls & TLS_TLS) != 0
8047 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8048 found_tls_get_addr_arg = 1;
8050 toc_ref[toc_ref_index] = 1;
8056 /* Uh oh, we didn't find the expected call. We
8057 could just mark this symbol to exclude it
8058 from tls optimization but it's safer to skip
8059 the entire optimization. */
8060 /* xgettext:c-format */
8061 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8062 "TLS optimization disabled\n"),
8063 ibfd, sec, rel->r_offset);
8068 /* If we don't have old-style __tls_get_addr calls
8069 without TLSGD/TLSLD marker relocs, and we haven't
8070 found a new-style __tls_get_addr call with a
8071 marker for this symbol, then we either have a
8072 broken object file or an -mlongcall style
8073 indirect call to __tls_get_addr without a marker.
8074 Disable optimization in this case. */
8075 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8076 && (tls_set & TLS_EXPLICIT) == 0
8077 && !sec->has_tls_get_addr_call
8078 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8079 != (TLS_TLS | TLS_MARK)))
8082 if (expecting_tls_get_addr)
8084 struct plt_entry *ent = NULL;
8086 if (htab->tls_get_addr != NULL)
8087 for (ent = htab->tls_get_addr->elf.plt.plist;
8090 if (ent->addend == 0)
8093 if (ent == NULL && htab->tls_get_addr_fd != NULL)
8094 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8097 if (ent->addend == 0)
8101 && ent->plt.refcount > 0)
8102 ent->plt.refcount -= 1;
8108 if ((tls_set & TLS_EXPLICIT) == 0)
8110 struct got_entry *ent;
8112 /* Adjust got entry for this reloc. */
8116 ent = elf_local_got_ents (ibfd)[r_symndx];
8118 for (; ent != NULL; ent = ent->next)
8119 if (ent->addend == rel->r_addend
8120 && ent->owner == ibfd
8121 && ent->tls_type == tls_type)
8128 /* We managed to get rid of a got entry. */
8129 if (ent->got.refcount > 0)
8130 ent->got.refcount -= 1;
8135 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8136 we'll lose one or two dyn relocs. */
8137 if (!dec_dynrel_count (rel->r_info, sec, info,
8141 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8143 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8149 *tls_mask |= tls_set & 0xff;
8150 *tls_mask &= ~tls_clear;
8153 if (elf_section_data (sec)->relocs != relstart)
8158 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8160 if (!info->keep_memory)
8163 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8167 if (toc_ref != NULL)
8169 htab->do_tls_opt = 1;
8173 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8174 the values of any global symbols in a toc section that has been
8175 edited. Globals in toc sections should be a rarity, so this function
8176 sets a flag if any are found in toc sections other than the one just
8177 edited, so that further hash table traversals can be avoided. */
8179 struct adjust_toc_info
8182 unsigned long *skip;
8183 bfd_boolean global_toc_syms;
8186 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8189 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8191 struct ppc_link_hash_entry *eh;
8192 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8195 if (h->root.type != bfd_link_hash_defined
8196 && h->root.type != bfd_link_hash_defweak)
8199 eh = (struct ppc_link_hash_entry *) h;
8200 if (eh->adjust_done)
8203 if (eh->elf.root.u.def.section == toc_inf->toc)
8205 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8206 i = toc_inf->toc->rawsize >> 3;
8208 i = eh->elf.root.u.def.value >> 3;
8210 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8213 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8216 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8217 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8220 eh->elf.root.u.def.value -= toc_inf->skip[i];
8221 eh->adjust_done = 1;
8223 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8224 toc_inf->global_toc_syms = TRUE;
8229 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8230 on a _LO variety toc/got reloc. */
8233 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8235 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8236 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8237 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8238 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8239 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8240 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8241 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8242 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8243 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8244 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8245 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8246 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8247 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8248 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8249 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8250 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8251 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8252 /* Exclude lfqu by testing reloc. If relocs are ever
8253 defined for the reduced D field in psq_lu then those
8254 will need testing too. */
8255 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8256 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8258 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8259 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8260 /* Exclude stfqu. psq_stu as above for psq_lu. */
8261 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8262 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8263 && (insn & 1) == 0));
8266 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8267 pld ra,symbol@got@pcrel
8272 may be translated to
8273 pload/pstore rt,symbol@pcrel
8275 This function returns true if the optimization is possible, placing
8276 the prefix insn in *PINSN1 and a NOP in *PINSN2.
8278 On entry to this function, the linker has already determined that
8279 the pld can be replaced with pla: *PINSN1 is that pla insn,
8280 while *PINSN2 is the second instruction. */
8283 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2)
8285 uint32_t insn2 = *pinsn2 >> 32;
8288 /* Check that regs match. */
8289 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
8292 switch ((insn2 >> 26) & 63)
8308 /* These are the PMLS cases, where we just need to tack a prefix
8309 on the insn. Check that the D field is zero. */
8310 if ((insn2 & 0xffff) != 0)
8312 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8313 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8316 case 58: /* lwa, ld */
8317 if ((insn2 & 0xfffd) != 0)
8319 i1new = ((1ULL << 58) | (1ULL << 52)
8320 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8321 | (insn2 & (31ULL << 21)));
8324 case 57: /* lxsd, lxssp */
8325 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
8327 i1new = ((1ULL << 58) | (1ULL << 52)
8328 | ((40ULL | (insn2 & 3)) << 26)
8329 | (insn2 & (31ULL << 21)));
8332 case 61: /* stxsd, stxssp, lxv, stxv */
8333 if ((insn2 & 3) == 0)
8335 else if ((insn2 & 3) >= 2)
8337 if ((insn2 & 0xfffc) != 0)
8339 i1new = ((1ULL << 58) | (1ULL << 52)
8340 | ((44ULL | (insn2 & 3)) << 26)
8341 | (insn2 & (31ULL << 21)));
8345 if ((insn2 & 0xfff0) != 0)
8347 i1new = ((1ULL << 58) | (1ULL << 52)
8348 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8349 | (insn2 & (31ULL << 21)));
8354 if ((insn2 & 0xffff) != 0)
8356 i1new = ((1ULL << 58) | (1ULL << 52)
8357 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8360 case 62: /* std, stq */
8361 if ((insn2 & 0xfffd) != 0)
8363 i1new = ((1ULL << 58) | (1ULL << 52)
8364 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8365 | (insn2 & (31ULL << 21)));
8370 *pinsn2 = (uint64_t) NOP << 32;
8374 /* Examine all relocs referencing .toc sections in order to remove
8375 unused .toc entries. */
8378 ppc64_elf_edit_toc (struct bfd_link_info *info)
8381 struct adjust_toc_info toc_inf;
8382 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8384 htab->do_toc_opt = 1;
8385 toc_inf.global_toc_syms = TRUE;
8386 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8388 asection *toc, *sec;
8389 Elf_Internal_Shdr *symtab_hdr;
8390 Elf_Internal_Sym *local_syms;
8391 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8392 unsigned long *skip, *drop;
8393 unsigned char *used;
8394 unsigned char *keep, last, some_unused;
8396 if (!is_ppc64_elf (ibfd))
8399 toc = bfd_get_section_by_name (ibfd, ".toc");
8402 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8403 || discarded_section (toc))
8408 symtab_hdr = &elf_symtab_hdr (ibfd);
8410 /* Look at sections dropped from the final link. */
8413 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8415 if (sec->reloc_count == 0
8416 || !discarded_section (sec)
8417 || get_opd_info (sec)
8418 || (sec->flags & SEC_ALLOC) == 0
8419 || (sec->flags & SEC_DEBUGGING) != 0)
8422 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8423 if (relstart == NULL)
8426 /* Run through the relocs to see which toc entries might be
8428 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8430 enum elf_ppc64_reloc_type r_type;
8431 unsigned long r_symndx;
8433 struct elf_link_hash_entry *h;
8434 Elf_Internal_Sym *sym;
8437 r_type = ELF64_R_TYPE (rel->r_info);
8444 case R_PPC64_TOC16_LO:
8445 case R_PPC64_TOC16_HI:
8446 case R_PPC64_TOC16_HA:
8447 case R_PPC64_TOC16_DS:
8448 case R_PPC64_TOC16_LO_DS:
8452 r_symndx = ELF64_R_SYM (rel->r_info);
8453 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8461 val = h->root.u.def.value;
8463 val = sym->st_value;
8464 val += rel->r_addend;
8466 if (val >= toc->size)
8469 /* Anything in the toc ought to be aligned to 8 bytes.
8470 If not, don't mark as unused. */
8476 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8481 skip[val >> 3] = ref_from_discarded;
8484 if (elf_section_data (sec)->relocs != relstart)
8488 /* For largetoc loads of address constants, we can convert
8489 . addis rx,2,addr@got@ha
8490 . ld ry,addr@got@l(rx)
8492 . addis rx,2,addr@toc@ha
8493 . addi ry,rx,addr@toc@l
8494 when addr is within 2G of the toc pointer. This then means
8495 that the word storing "addr" in the toc is no longer needed. */
8497 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8498 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8499 && toc->reloc_count != 0)
8501 /* Read toc relocs. */
8502 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8504 if (toc_relocs == NULL)
8507 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8509 enum elf_ppc64_reloc_type r_type;
8510 unsigned long r_symndx;
8512 struct elf_link_hash_entry *h;
8513 Elf_Internal_Sym *sym;
8516 r_type = ELF64_R_TYPE (rel->r_info);
8517 if (r_type != R_PPC64_ADDR64)
8520 r_symndx = ELF64_R_SYM (rel->r_info);
8521 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8526 || sym_sec->output_section == NULL
8527 || discarded_section (sym_sec))
8530 if (!SYMBOL_REFERENCES_LOCAL (info, h))
8535 if (h->type == STT_GNU_IFUNC)
8537 val = h->root.u.def.value;
8541 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8543 val = sym->st_value;
8545 val += rel->r_addend;
8546 val += sym_sec->output_section->vma + sym_sec->output_offset;
8548 /* We don't yet know the exact toc pointer value, but we
8549 know it will be somewhere in the toc section. Don't
8550 optimize if the difference from any possible toc
8551 pointer is outside [ff..f80008000, 7fff7fff]. */
8552 addr = toc->output_section->vma + TOC_BASE_OFF;
8553 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8556 addr = toc->output_section->vma + toc->output_section->rawsize;
8557 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8562 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8567 skip[rel->r_offset >> 3]
8568 |= can_optimize | ((rel - toc_relocs) << 2);
8575 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8579 if (local_syms != NULL
8580 && symtab_hdr->contents != (unsigned char *) local_syms)
8584 && elf_section_data (sec)->relocs != relstart)
8586 if (toc_relocs != NULL
8587 && elf_section_data (toc)->relocs != toc_relocs)
8594 /* Now check all kept sections that might reference the toc.
8595 Check the toc itself last. */
8596 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8599 sec = (sec == toc ? NULL
8600 : sec->next == NULL ? toc
8601 : sec->next == toc && toc->next ? toc->next
8606 if (sec->reloc_count == 0
8607 || discarded_section (sec)
8608 || get_opd_info (sec)
8609 || (sec->flags & SEC_ALLOC) == 0
8610 || (sec->flags & SEC_DEBUGGING) != 0)
8613 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8615 if (relstart == NULL)
8621 /* Mark toc entries referenced as used. */
8625 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8627 enum elf_ppc64_reloc_type r_type;
8628 unsigned long r_symndx;
8630 struct elf_link_hash_entry *h;
8631 Elf_Internal_Sym *sym;
8634 r_type = ELF64_R_TYPE (rel->r_info);
8638 case R_PPC64_TOC16_LO:
8639 case R_PPC64_TOC16_HI:
8640 case R_PPC64_TOC16_HA:
8641 case R_PPC64_TOC16_DS:
8642 case R_PPC64_TOC16_LO_DS:
8643 /* In case we're taking addresses of toc entries. */
8644 case R_PPC64_ADDR64:
8651 r_symndx = ELF64_R_SYM (rel->r_info);
8652 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8663 val = h->root.u.def.value;
8665 val = sym->st_value;
8666 val += rel->r_addend;
8668 if (val >= toc->size)
8671 if ((skip[val >> 3] & can_optimize) != 0)
8678 case R_PPC64_TOC16_HA:
8681 case R_PPC64_TOC16_LO_DS:
8682 off = rel->r_offset;
8683 off += (bfd_big_endian (ibfd) ? -2 : 3);
8684 if (!bfd_get_section_contents (ibfd, sec, &opc,
8690 if ((opc & (0x3f << 2)) == (58u << 2))
8695 /* Wrong sort of reloc, or not a ld. We may
8696 as well clear ref_from_discarded too. */
8703 /* For the toc section, we only mark as used if this
8704 entry itself isn't unused. */
8705 else if ((used[rel->r_offset >> 3]
8706 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8709 /* Do all the relocs again, to catch reference
8718 if (elf_section_data (sec)->relocs != relstart)
8722 /* Merge the used and skip arrays. Assume that TOC
8723 doublewords not appearing as either used or unused belong
8724 to an entry more than one doubleword in size. */
8725 for (drop = skip, keep = used, last = 0, some_unused = 0;
8726 drop < skip + (toc->size + 7) / 8;
8731 *drop &= ~ref_from_discarded;
8732 if ((*drop & can_optimize) != 0)
8736 else if ((*drop & ref_from_discarded) != 0)
8739 last = ref_from_discarded;
8749 bfd_byte *contents, *src;
8751 Elf_Internal_Sym *sym;
8752 bfd_boolean local_toc_syms = FALSE;
8754 /* Shuffle the toc contents, and at the same time convert the
8755 skip array from booleans into offsets. */
8756 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8759 elf_section_data (toc)->this_hdr.contents = contents;
8761 for (src = contents, off = 0, drop = skip;
8762 src < contents + toc->size;
8765 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8770 memcpy (src - off, src, 8);
8774 toc->rawsize = toc->size;
8775 toc->size = src - contents - off;
8777 /* Adjust addends for relocs against the toc section sym,
8778 and optimize any accesses we can. */
8779 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8781 if (sec->reloc_count == 0
8782 || discarded_section (sec))
8785 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8787 if (relstart == NULL)
8790 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8792 enum elf_ppc64_reloc_type r_type;
8793 unsigned long r_symndx;
8795 struct elf_link_hash_entry *h;
8798 r_type = ELF64_R_TYPE (rel->r_info);
8805 case R_PPC64_TOC16_LO:
8806 case R_PPC64_TOC16_HI:
8807 case R_PPC64_TOC16_HA:
8808 case R_PPC64_TOC16_DS:
8809 case R_PPC64_TOC16_LO_DS:
8810 case R_PPC64_ADDR64:
8814 r_symndx = ELF64_R_SYM (rel->r_info);
8815 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8823 val = h->root.u.def.value;
8826 val = sym->st_value;
8828 local_toc_syms = TRUE;
8831 val += rel->r_addend;
8833 if (val > toc->rawsize)
8835 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8837 else if ((skip[val >> 3] & can_optimize) != 0)
8839 Elf_Internal_Rela *tocrel
8840 = toc_relocs + (skip[val >> 3] >> 2);
8841 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8845 case R_PPC64_TOC16_HA:
8846 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8849 case R_PPC64_TOC16_LO_DS:
8850 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8854 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8856 info->callbacks->einfo
8857 /* xgettext:c-format */
8858 (_("%H: %s references "
8859 "optimized away TOC entry\n"),
8860 ibfd, sec, rel->r_offset,
8861 ppc64_elf_howto_table[r_type]->name);
8862 bfd_set_error (bfd_error_bad_value);
8865 rel->r_addend = tocrel->r_addend;
8866 elf_section_data (sec)->relocs = relstart;
8870 if (h != NULL || sym->st_value != 0)
8873 rel->r_addend -= skip[val >> 3];
8874 elf_section_data (sec)->relocs = relstart;
8877 if (elf_section_data (sec)->relocs != relstart)
8881 /* We shouldn't have local or global symbols defined in the TOC,
8882 but handle them anyway. */
8883 if (local_syms != NULL)
8884 for (sym = local_syms;
8885 sym < local_syms + symtab_hdr->sh_info;
8887 if (sym->st_value != 0
8888 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8892 if (sym->st_value > toc->rawsize)
8893 i = toc->rawsize >> 3;
8895 i = sym->st_value >> 3;
8897 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8901 (_("%s defined on removed toc entry"),
8902 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8905 while ((skip[i] & (ref_from_discarded | can_optimize)));
8906 sym->st_value = (bfd_vma) i << 3;
8909 sym->st_value -= skip[i];
8910 symtab_hdr->contents = (unsigned char *) local_syms;
8913 /* Adjust any global syms defined in this toc input section. */
8914 if (toc_inf.global_toc_syms)
8917 toc_inf.skip = skip;
8918 toc_inf.global_toc_syms = FALSE;
8919 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8923 if (toc->reloc_count != 0)
8925 Elf_Internal_Shdr *rel_hdr;
8926 Elf_Internal_Rela *wrel;
8929 /* Remove unused toc relocs, and adjust those we keep. */
8930 if (toc_relocs == NULL)
8931 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8933 if (toc_relocs == NULL)
8937 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8938 if ((skip[rel->r_offset >> 3]
8939 & (ref_from_discarded | can_optimize)) == 0)
8941 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8942 wrel->r_info = rel->r_info;
8943 wrel->r_addend = rel->r_addend;
8946 else if (!dec_dynrel_count (rel->r_info, toc, info,
8947 &local_syms, NULL, NULL))
8950 elf_section_data (toc)->relocs = toc_relocs;
8951 toc->reloc_count = wrel - toc_relocs;
8952 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8953 sz = rel_hdr->sh_entsize;
8954 rel_hdr->sh_size = toc->reloc_count * sz;
8957 else if (toc_relocs != NULL
8958 && elf_section_data (toc)->relocs != toc_relocs)
8961 if (local_syms != NULL
8962 && symtab_hdr->contents != (unsigned char *) local_syms)
8964 if (!info->keep_memory)
8967 symtab_hdr->contents = (unsigned char *) local_syms;
8972 /* Look for cases where we can change an indirect GOT access to
8973 a GOT relative or PC relative access, possibly reducing the
8974 number of GOT entries. */
8975 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8978 Elf_Internal_Shdr *symtab_hdr;
8979 Elf_Internal_Sym *local_syms;
8980 Elf_Internal_Rela *relstart, *rel;
8983 if (!is_ppc64_elf (ibfd))
8986 if (!ppc64_elf_tdata (ibfd)->has_optrel)
8989 sec = ppc64_elf_tdata (ibfd)->got;
8992 got = sec->output_section->vma + sec->output_offset + 0x8000;
8995 symtab_hdr = &elf_symtab_hdr (ibfd);
8997 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8999 if (sec->reloc_count == 0
9000 || !ppc64_elf_section_data (sec)->has_optrel
9001 || discarded_section (sec))
9004 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9006 if (relstart == NULL)
9009 if (local_syms != NULL
9010 && symtab_hdr->contents != (unsigned char *) local_syms)
9014 && elf_section_data (sec)->relocs != relstart)
9019 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9021 enum elf_ppc64_reloc_type r_type;
9022 unsigned long r_symndx;
9023 Elf_Internal_Sym *sym;
9025 struct elf_link_hash_entry *h;
9026 struct got_entry *ent;
9027 bfd_vma sym_addend, val, pc;
9028 unsigned char buf[8];
9030 enum {no_check, check_lo, check_ha} insn_check;
9032 r_type = ELF64_R_TYPE (rel->r_info);
9036 insn_check = no_check;
9039 case R_PPC64_PLT16_HA:
9040 case R_PPC64_GOT_TLSLD16_HA:
9041 case R_PPC64_GOT_TLSGD16_HA:
9042 case R_PPC64_GOT_TPREL16_HA:
9043 case R_PPC64_GOT_DTPREL16_HA:
9044 case R_PPC64_GOT16_HA:
9045 case R_PPC64_TOC16_HA:
9046 insn_check = check_ha;
9049 case R_PPC64_PLT16_LO:
9050 case R_PPC64_PLT16_LO_DS:
9051 case R_PPC64_GOT_TLSLD16_LO:
9052 case R_PPC64_GOT_TLSGD16_LO:
9053 case R_PPC64_GOT_TPREL16_LO_DS:
9054 case R_PPC64_GOT_DTPREL16_LO_DS:
9055 case R_PPC64_GOT16_LO:
9056 case R_PPC64_GOT16_LO_DS:
9057 case R_PPC64_TOC16_LO:
9058 case R_PPC64_TOC16_LO_DS:
9059 insn_check = check_lo;
9063 if (insn_check != no_check)
9065 bfd_vma off = rel->r_offset & ~3;
9067 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9070 insn = bfd_get_32 (ibfd, buf);
9071 if (insn_check == check_lo
9072 ? !ok_lo_toc_insn (insn, r_type)
9073 : ((insn & ((0x3f << 26) | 0x1f << 16))
9074 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9078 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9079 sprintf (str, "%#08x", insn);
9080 info->callbacks->einfo
9081 /* xgettext:c-format */
9082 (_("%H: got/toc optimization is not supported for"
9083 " %s instruction\n"),
9084 ibfd, sec, rel->r_offset & ~3, str);
9091 /* Note that we don't delete GOT entries for
9092 R_PPC64_GOT16_DS since we'd need a lot more
9093 analysis. For starters, the preliminary layout is
9094 before the GOT, PLT, dynamic sections and stubs are
9095 laid out. Then we'd need to allow for changes in
9096 distance between sections caused by alignment. */
9100 case R_PPC64_GOT16_HA:
9101 case R_PPC64_GOT16_LO_DS:
9102 sym_addend = rel->r_addend;
9105 case R_PPC64_GOT_PCREL34:
9110 r_symndx = ELF64_R_SYM (rel->r_info);
9111 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9115 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9119 val = h->root.u.def.value;
9121 val = sym->st_value;
9123 val += sym_sec->output_section->vma + sym_sec->output_offset;
9125 /* Fudge factor to allow for the fact that the preliminary layout
9126 isn't exact. Reduce limits by this factor. */
9127 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9134 case R_PPC64_GOT16_HA:
9135 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9136 >= LIMIT_ADJUST (0x100000000ULL))
9139 if (!bfd_get_section_contents (ibfd, sec, buf,
9140 rel->r_offset & ~3, 4))
9142 insn = bfd_get_32 (ibfd, buf);
9143 if (((insn & ((0x3f << 26) | 0x1f << 16))
9144 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9148 case R_PPC64_GOT16_LO_DS:
9149 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9150 >= LIMIT_ADJUST (0x100000000ULL))
9152 if (!bfd_get_section_contents (ibfd, sec, buf,
9153 rel->r_offset & ~3, 4))
9155 insn = bfd_get_32 (ibfd, buf);
9156 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9160 case R_PPC64_GOT_PCREL34:
9162 pc += sec->output_section->vma + sec->output_offset;
9163 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9164 >= LIMIT_ADJUST (1ULL << 34))
9166 if (!bfd_get_section_contents (ibfd, sec, buf,
9167 rel->r_offset & ~3, 8))
9169 insn = bfd_get_32 (ibfd, buf);
9170 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9172 insn = bfd_get_32 (ibfd, buf + 4);
9173 if ((insn & (0x3f << 26)) != 57u << 26)
9183 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9184 ent = local_got_ents[r_symndx];
9186 for (; ent != NULL; ent = ent->next)
9187 if (ent->addend == sym_addend
9188 && ent->owner == ibfd
9189 && ent->tls_type == 0)
9191 BFD_ASSERT (ent && ent->got.refcount > 0);
9192 ent->got.refcount -= 1;
9195 if (elf_section_data (sec)->relocs != relstart)
9199 if (local_syms != NULL
9200 && symtab_hdr->contents != (unsigned char *) local_syms)
9202 if (!info->keep_memory)
9205 symtab_hdr->contents = (unsigned char *) local_syms;
9212 /* Return true iff input section I references the TOC using
9213 instructions limited to +/-32k offsets. */
9216 ppc64_elf_has_small_toc_reloc (asection *i)
9218 return (is_ppc64_elf (i->owner)
9219 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9222 /* Allocate space for one GOT entry. */
9225 allocate_got (struct elf_link_hash_entry *h,
9226 struct bfd_link_info *info,
9227 struct got_entry *gent)
9229 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9230 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9231 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9233 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9234 ? 2 : 1) * sizeof (Elf64_External_Rela);
9235 asection *got = ppc64_elf_tdata (gent->owner)->got;
9237 gent->got.offset = got->size;
9238 got->size += entsize;
9240 if (h->type == STT_GNU_IFUNC)
9242 htab->elf.irelplt->size += rentsize;
9243 htab->got_reli_size += rentsize;
9245 else if (((bfd_link_pic (info)
9246 && !((gent->tls_type & TLS_TPREL) != 0
9247 && bfd_link_executable (info)
9248 && SYMBOL_REFERENCES_LOCAL (info, h)))
9249 || (htab->elf.dynamic_sections_created
9251 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9252 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9254 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9255 relgot->size += rentsize;
9259 /* This function merges got entries in the same toc group. */
9262 merge_got_entries (struct got_entry **pent)
9264 struct got_entry *ent, *ent2;
9266 for (ent = *pent; ent != NULL; ent = ent->next)
9267 if (!ent->is_indirect)
9268 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9269 if (!ent2->is_indirect
9270 && ent2->addend == ent->addend
9271 && ent2->tls_type == ent->tls_type
9272 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9274 ent2->is_indirect = TRUE;
9275 ent2->got.ent = ent;
9279 /* If H is undefined, make it dynamic if that makes sense. */
9282 ensure_undef_dynamic (struct bfd_link_info *info,
9283 struct elf_link_hash_entry *h)
9285 struct elf_link_hash_table *htab = elf_hash_table (info);
9287 if (htab->dynamic_sections_created
9288 && ((info->dynamic_undefined_weak != 0
9289 && h->root.type == bfd_link_hash_undefweak)
9290 || h->root.type == bfd_link_hash_undefined)
9293 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9294 return bfd_elf_link_record_dynamic_symbol (info, h);
9298 /* Allocate space in .plt, .got and associated reloc sections for
9302 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9304 struct bfd_link_info *info;
9305 struct ppc_link_hash_table *htab;
9307 struct ppc_link_hash_entry *eh;
9308 struct got_entry **pgent, *gent;
9310 if (h->root.type == bfd_link_hash_indirect)
9313 info = (struct bfd_link_info *) inf;
9314 htab = ppc_hash_table (info);
9318 eh = (struct ppc_link_hash_entry *) h;
9319 /* Run through the TLS GD got entries first if we're changing them
9321 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9322 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9323 if (gent->got.refcount > 0
9324 && (gent->tls_type & TLS_GD) != 0)
9326 /* This was a GD entry that has been converted to TPREL. If
9327 there happens to be a TPREL entry we can use that one. */
9328 struct got_entry *ent;
9329 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9330 if (ent->got.refcount > 0
9331 && (ent->tls_type & TLS_TPREL) != 0
9332 && ent->addend == gent->addend
9333 && ent->owner == gent->owner)
9335 gent->got.refcount = 0;
9339 /* If not, then we'll be using our own TPREL entry. */
9340 if (gent->got.refcount != 0)
9341 gent->tls_type = TLS_TLS | TLS_TPREL;
9344 /* Remove any list entry that won't generate a word in the GOT before
9345 we call merge_got_entries. Otherwise we risk merging to empty
9347 pgent = &h->got.glist;
9348 while ((gent = *pgent) != NULL)
9349 if (gent->got.refcount > 0)
9351 if ((gent->tls_type & TLS_LD) != 0
9354 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9355 *pgent = gent->next;
9358 pgent = &gent->next;
9361 *pgent = gent->next;
9363 if (!htab->do_multi_toc)
9364 merge_got_entries (&h->got.glist);
9366 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9367 if (!gent->is_indirect)
9369 /* Make sure this symbol is output as a dynamic symbol. */
9370 if (!ensure_undef_dynamic (info, h))
9373 if (!is_ppc64_elf (gent->owner))
9376 allocate_got (h, info, gent);
9379 /* If no dynamic sections we can't have dynamic relocs, except for
9380 IFUNCs which are handled even in static executables. */
9381 if (!htab->elf.dynamic_sections_created
9382 && h->type != STT_GNU_IFUNC)
9383 eh->dyn_relocs = NULL;
9385 /* Discard relocs on undefined symbols that must be local. */
9386 else if (h->root.type == bfd_link_hash_undefined
9387 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9388 eh->dyn_relocs = NULL;
9390 /* Also discard relocs on undefined weak syms with non-default
9391 visibility, or when dynamic_undefined_weak says so. */
9392 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9393 eh->dyn_relocs = NULL;
9395 if (eh->dyn_relocs != NULL)
9397 struct elf_dyn_relocs *p, **pp;
9399 /* In the shared -Bsymbolic case, discard space allocated for
9400 dynamic pc-relative relocs against symbols which turn out to
9401 be defined in regular objects. For the normal shared case,
9402 discard space for relocs that have become local due to symbol
9403 visibility changes. */
9405 if (bfd_link_pic (info))
9407 /* Relocs that use pc_count are those that appear on a call
9408 insn, or certain REL relocs (see must_be_dyn_reloc) that
9409 can be generated via assembly. We want calls to
9410 protected symbols to resolve directly to the function
9411 rather than going via the plt. If people want function
9412 pointer comparisons to work as expected then they should
9413 avoid writing weird assembly. */
9414 if (SYMBOL_CALLS_LOCAL (info, h))
9416 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9418 p->count -= p->pc_count;
9427 if (eh->dyn_relocs != NULL)
9429 /* Make sure this symbol is output as a dynamic symbol. */
9430 if (!ensure_undef_dynamic (info, h))
9434 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
9436 /* For the non-pic case, discard space for relocs against
9437 symbols which turn out to need copy relocs or are not
9439 if (h->dynamic_adjusted
9441 && !ELF_COMMON_DEF_P (h))
9443 /* Make sure this symbol is output as a dynamic symbol. */
9444 if (!ensure_undef_dynamic (info, h))
9447 if (h->dynindx == -1)
9448 eh->dyn_relocs = NULL;
9451 eh->dyn_relocs = NULL;
9454 /* Finally, allocate space. */
9455 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9457 asection *sreloc = elf_section_data (p->sec)->sreloc;
9458 if (eh->elf.type == STT_GNU_IFUNC)
9459 sreloc = htab->elf.irelplt;
9460 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9464 /* We might need a PLT entry when the symbol
9467 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9468 d) has plt16 relocs and we are linking statically. */
9469 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9470 || h->type == STT_GNU_IFUNC
9471 || (h->needs_plt && h->dynamic_adjusted)
9474 && !htab->elf.dynamic_sections_created
9475 && !htab->can_convert_all_inline_plt
9476 && (((struct ppc_link_hash_entry *) h)->tls_mask
9477 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
9479 struct plt_entry *pent;
9480 bfd_boolean doneone = FALSE;
9481 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9482 if (pent->plt.refcount > 0)
9484 if (!htab->elf.dynamic_sections_created
9485 || h->dynindx == -1)
9487 if (h->type == STT_GNU_IFUNC)
9490 pent->plt.offset = s->size;
9491 s->size += PLT_ENTRY_SIZE (htab);
9492 s = htab->elf.irelplt;
9497 pent->plt.offset = s->size;
9498 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9499 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9504 /* If this is the first .plt entry, make room for the special
9508 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9510 pent->plt.offset = s->size;
9512 /* Make room for this entry. */
9513 s->size += PLT_ENTRY_SIZE (htab);
9515 /* Make room for the .glink code. */
9518 s->size += GLINK_PLTRESOLVE_SIZE (htab);
9521 /* We need bigger stubs past index 32767. */
9522 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
9529 /* We also need to make an entry in the .rela.plt section. */
9530 s = htab->elf.srelplt;
9533 s->size += sizeof (Elf64_External_Rela);
9537 pent->plt.offset = (bfd_vma) -1;
9540 h->plt.plist = NULL;
9546 h->plt.plist = NULL;
9553 #define PPC_LO(v) ((v) & 0xffff)
9554 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9555 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9557 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9558 #define HA34(v) ((v + (1ULL << 33)) >> 34)
9560 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9561 to set up space for global entry stubs. These are put in glink,
9562 after the branch table. */
9565 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9567 struct bfd_link_info *info;
9568 struct ppc_link_hash_table *htab;
9569 struct plt_entry *pent;
9572 if (h->root.type == bfd_link_hash_indirect)
9575 if (!h->pointer_equality_needed)
9582 htab = ppc_hash_table (info);
9586 s = htab->global_entry;
9587 plt = htab->elf.splt;
9588 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9589 if (pent->plt.offset != (bfd_vma) -1
9590 && pent->addend == 0)
9592 /* For ELFv2, if this symbol is not defined in a regular file
9593 and we are not generating a shared library or pie, then we
9594 need to define the symbol in the executable on a call stub.
9595 This is to avoid text relocations. */
9596 bfd_vma off, stub_align, stub_off, stub_size;
9597 unsigned int align_power;
9601 if (htab->params->plt_stub_align >= 0)
9602 align_power = htab->params->plt_stub_align;
9604 align_power = -htab->params->plt_stub_align;
9605 /* Setting section alignment is delayed until we know it is
9606 non-empty. Otherwise the .text output section will be
9607 aligned at least to plt_stub_align even when no global
9608 entry stubs are needed. */
9609 if (s->alignment_power < align_power)
9610 s->alignment_power = align_power;
9611 stub_align = (bfd_vma) 1 << align_power;
9612 if (htab->params->plt_stub_align >= 0
9613 || ((((stub_off + stub_size - 1) & -stub_align)
9614 - (stub_off & -stub_align))
9615 > ((stub_size - 1) & -stub_align)))
9616 stub_off = (stub_off + stub_align - 1) & -stub_align;
9617 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9618 off -= stub_off + s->output_offset + s->output_section->vma;
9619 /* Note that for --plt-stub-align negative we have a possible
9620 dependency between stub offset and size. Break that
9621 dependency by assuming the max stub size when calculating
9623 if (PPC_HA (off) == 0)
9625 h->root.type = bfd_link_hash_defined;
9626 h->root.u.def.section = s;
9627 h->root.u.def.value = stub_off;
9628 s->size = stub_off + stub_size;
9634 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9635 read-only sections. */
9638 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
9642 if (h->root.type == bfd_link_hash_indirect)
9645 sec = readonly_dynrelocs (h);
9648 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9650 info->flags |= DF_TEXTREL;
9651 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9652 " in read-only section `%pA'\n"),
9653 sec->owner, h->root.root.string, sec);
9655 /* Not an error, just cut short the traversal. */
9661 /* Set the sizes of the dynamic sections. */
9664 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9665 struct bfd_link_info *info)
9667 struct ppc_link_hash_table *htab;
9672 struct got_entry *first_tlsld;
9674 htab = ppc_hash_table (info);
9678 dynobj = htab->elf.dynobj;
9682 if (htab->elf.dynamic_sections_created)
9684 /* Set the contents of the .interp section to the interpreter. */
9685 if (bfd_link_executable (info) && !info->nointerp)
9687 s = bfd_get_linker_section (dynobj, ".interp");
9690 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9691 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9695 /* Set up .got offsets for local syms, and space for local dynamic
9697 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9699 struct got_entry **lgot_ents;
9700 struct got_entry **end_lgot_ents;
9701 struct plt_entry **local_plt;
9702 struct plt_entry **end_local_plt;
9703 unsigned char *lgot_masks;
9704 bfd_size_type locsymcount;
9705 Elf_Internal_Shdr *symtab_hdr;
9707 if (!is_ppc64_elf (ibfd))
9710 for (s = ibfd->sections; s != NULL; s = s->next)
9712 struct ppc_dyn_relocs *p;
9714 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9716 if (!bfd_is_abs_section (p->sec)
9717 && bfd_is_abs_section (p->sec->output_section))
9719 /* Input section has been discarded, either because
9720 it is a copy of a linkonce section or due to
9721 linker script /DISCARD/, so we'll be discarding
9724 else if (p->count != 0)
9726 asection *srel = elf_section_data (p->sec)->sreloc;
9728 srel = htab->elf.irelplt;
9729 srel->size += p->count * sizeof (Elf64_External_Rela);
9730 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9731 info->flags |= DF_TEXTREL;
9736 lgot_ents = elf_local_got_ents (ibfd);
9740 symtab_hdr = &elf_symtab_hdr (ibfd);
9741 locsymcount = symtab_hdr->sh_info;
9742 end_lgot_ents = lgot_ents + locsymcount;
9743 local_plt = (struct plt_entry **) end_lgot_ents;
9744 end_local_plt = local_plt + locsymcount;
9745 lgot_masks = (unsigned char *) end_local_plt;
9746 s = ppc64_elf_tdata (ibfd)->got;
9747 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9749 struct got_entry **pent, *ent;
9752 while ((ent = *pent) != NULL)
9753 if (ent->got.refcount > 0)
9755 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9757 ppc64_tlsld_got (ibfd)->got.refcount += 1;
9762 unsigned int ent_size = 8;
9763 unsigned int rel_size = sizeof (Elf64_External_Rela);
9765 ent->got.offset = s->size;
9766 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9771 s->size += ent_size;
9772 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9774 htab->elf.irelplt->size += rel_size;
9775 htab->got_reli_size += rel_size;
9777 else if (bfd_link_pic (info)
9778 && !((ent->tls_type & TLS_TPREL) != 0
9779 && bfd_link_executable (info)))
9781 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9782 srel->size += rel_size;
9791 /* Allocate space for plt calls to local syms. */
9792 lgot_masks = (unsigned char *) end_local_plt;
9793 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
9795 struct plt_entry *ent;
9797 for (ent = *local_plt; ent != NULL; ent = ent->next)
9798 if (ent->plt.refcount > 0)
9800 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9803 ent->plt.offset = s->size;
9804 s->size += PLT_ENTRY_SIZE (htab);
9805 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9807 else if (htab->can_convert_all_inline_plt
9808 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
9809 ent->plt.offset = (bfd_vma) -1;
9813 ent->plt.offset = s->size;
9814 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9815 if (bfd_link_pic (info))
9816 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9820 ent->plt.offset = (bfd_vma) -1;
9824 /* Allocate global sym .plt and .got entries, and space for global
9825 sym dynamic relocs. */
9826 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9828 if (!htab->opd_abi && !bfd_link_pic (info))
9829 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
9832 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9834 struct got_entry *ent;
9836 if (!is_ppc64_elf (ibfd))
9839 ent = ppc64_tlsld_got (ibfd);
9840 if (ent->got.refcount > 0)
9842 if (!htab->do_multi_toc && first_tlsld != NULL)
9844 ent->is_indirect = TRUE;
9845 ent->got.ent = first_tlsld;
9849 if (first_tlsld == NULL)
9851 s = ppc64_elf_tdata (ibfd)->got;
9852 ent->got.offset = s->size;
9855 if (bfd_link_pic (info))
9857 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9858 srel->size += sizeof (Elf64_External_Rela);
9863 ent->got.offset = (bfd_vma) -1;
9866 /* We now have determined the sizes of the various dynamic sections.
9867 Allocate memory for them. */
9869 for (s = dynobj->sections; s != NULL; s = s->next)
9871 if ((s->flags & SEC_LINKER_CREATED) == 0)
9874 if (s == htab->brlt || s == htab->relbrlt)
9875 /* These haven't been allocated yet; don't strip. */
9877 else if (s == htab->elf.sgot
9878 || s == htab->elf.splt
9879 || s == htab->elf.iplt
9880 || s == htab->pltlocal
9882 || s == htab->global_entry
9883 || s == htab->elf.sdynbss
9884 || s == htab->elf.sdynrelro)
9886 /* Strip this section if we don't need it; see the
9889 else if (s == htab->glink_eh_frame)
9891 if (!bfd_is_abs_section (s->output_section))
9892 /* Not sized yet. */
9895 else if (CONST_STRNEQ (s->name, ".rela"))
9899 if (s != htab->elf.srelplt)
9902 /* We use the reloc_count field as a counter if we need
9903 to copy relocs into the output file. */
9909 /* It's not one of our sections, so don't allocate space. */
9915 /* If we don't need this section, strip it from the
9916 output file. This is mostly to handle .rela.bss and
9917 .rela.plt. We must create both sections in
9918 create_dynamic_sections, because they must be created
9919 before the linker maps input sections to output
9920 sections. The linker does that before
9921 adjust_dynamic_symbol is called, and it is that
9922 function which decides whether anything needs to go
9923 into these sections. */
9924 s->flags |= SEC_EXCLUDE;
9928 if (bfd_is_abs_section (s->output_section))
9929 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9932 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9935 /* Allocate memory for the section contents. We use bfd_zalloc
9936 here in case unused entries are not reclaimed before the
9937 section's contents are written out. This should not happen,
9938 but this way if it does we get a R_PPC64_NONE reloc in .rela
9939 sections instead of garbage.
9940 We also rely on the section contents being zero when writing
9941 the GOT and .dynrelro. */
9942 s->contents = bfd_zalloc (dynobj, s->size);
9943 if (s->contents == NULL)
9947 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9949 if (!is_ppc64_elf (ibfd))
9952 s = ppc64_elf_tdata (ibfd)->got;
9953 if (s != NULL && s != htab->elf.sgot)
9956 s->flags |= SEC_EXCLUDE;
9959 s->contents = bfd_zalloc (ibfd, s->size);
9960 if (s->contents == NULL)
9964 s = ppc64_elf_tdata (ibfd)->relgot;
9968 s->flags |= SEC_EXCLUDE;
9971 s->contents = bfd_zalloc (ibfd, s->size);
9972 if (s->contents == NULL)
9980 if (htab->elf.dynamic_sections_created)
9982 bfd_boolean tls_opt;
9984 /* Add some entries to the .dynamic section. We fill in the
9985 values later, in ppc64_elf_finish_dynamic_sections, but we
9986 must add the entries now so that we get the correct size for
9987 the .dynamic section. The DT_DEBUG entry is filled in by the
9988 dynamic linker and used by the debugger. */
9989 #define add_dynamic_entry(TAG, VAL) \
9990 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9992 if (bfd_link_executable (info))
9994 if (!add_dynamic_entry (DT_DEBUG, 0))
9998 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10000 if (!add_dynamic_entry (DT_PLTGOT, 0)
10001 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10002 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10003 || !add_dynamic_entry (DT_JMPREL, 0)
10004 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10008 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10010 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10011 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10015 tls_opt = (htab->params->tls_get_addr_opt
10016 && htab->tls_get_addr_fd != NULL
10017 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10018 if (tls_opt || !htab->opd_abi)
10020 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10026 if (!add_dynamic_entry (DT_RELA, 0)
10027 || !add_dynamic_entry (DT_RELASZ, 0)
10028 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10031 /* If any dynamic relocs apply to a read-only section,
10032 then we need a DT_TEXTREL entry. */
10033 if ((info->flags & DF_TEXTREL) == 0)
10034 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10036 if ((info->flags & DF_TEXTREL) != 0)
10038 if (!add_dynamic_entry (DT_TEXTREL, 0))
10043 #undef add_dynamic_entry
10048 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10051 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10053 if (h->plt.plist != NULL
10055 && !h->pointer_equality_needed)
10058 return _bfd_elf_hash_symbol (h);
10061 /* Determine the type of stub needed, if any, for a call. */
10063 static inline enum ppc_stub_type
10064 ppc_type_of_stub (asection *input_sec,
10065 const Elf_Internal_Rela *rel,
10066 struct ppc_link_hash_entry **hash,
10067 struct plt_entry **plt_ent,
10068 bfd_vma destination,
10069 unsigned long local_off)
10071 struct ppc_link_hash_entry *h = *hash;
10073 bfd_vma branch_offset;
10074 bfd_vma max_branch_offset;
10075 enum elf_ppc64_reloc_type r_type;
10079 struct plt_entry *ent;
10080 struct ppc_link_hash_entry *fdh = h;
10082 && h->oh->is_func_descriptor)
10084 fdh = ppc_follow_link (h->oh);
10088 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10089 if (ent->addend == rel->r_addend
10090 && ent->plt.offset != (bfd_vma) -1)
10093 return ppc_stub_plt_call;
10096 /* Here, we know we don't have a plt entry. If we don't have a
10097 either a defined function descriptor or a defined entry symbol
10098 in a regular object file, then it is pointless trying to make
10099 any other type of stub. */
10100 if (!is_static_defined (&fdh->elf)
10101 && !is_static_defined (&h->elf))
10102 return ppc_stub_none;
10104 else if (elf_local_got_ents (input_sec->owner) != NULL)
10106 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10107 struct plt_entry **local_plt = (struct plt_entry **)
10108 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10109 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10111 if (local_plt[r_symndx] != NULL)
10113 struct plt_entry *ent;
10115 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10116 if (ent->addend == rel->r_addend
10117 && ent->plt.offset != (bfd_vma) -1)
10120 return ppc_stub_plt_call;
10125 /* Determine where the call point is. */
10126 location = (input_sec->output_offset
10127 + input_sec->output_section->vma
10130 branch_offset = destination - location;
10131 r_type = ELF64_R_TYPE (rel->r_info);
10133 /* Determine if a long branch stub is needed. */
10134 max_branch_offset = 1 << 25;
10135 if (r_type == R_PPC64_REL14
10136 || r_type == R_PPC64_REL14_BRTAKEN
10137 || r_type == R_PPC64_REL14_BRNTAKEN)
10138 max_branch_offset = 1 << 15;
10140 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10141 /* We need a stub. Figure out whether a long_branch or plt_branch
10142 is needed later. */
10143 return ppc_stub_long_branch;
10145 return ppc_stub_none;
10148 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10149 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10154 . lis %r12,xxx-1b@highest
10155 . ori %r12,%r12,xxx-1b@higher
10156 . sldi %r12,%r12,32
10157 . oris %r12,%r12,xxx-1b@high
10158 . ori %r12,%r12,xxx-1b@l
10159 . add/ldx %r12,%r11,%r12 */
10162 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10164 bfd_put_32 (abfd, MFLR_R12, p);
10166 bfd_put_32 (abfd, BCL_20_31, p);
10168 bfd_put_32 (abfd, MFLR_R11, p);
10170 bfd_put_32 (abfd, MTLR_R12, p);
10172 if (off + 0x8000 < 0x10000)
10175 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10177 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10180 else if (off + 0x80008000ULL < 0x100000000ULL)
10182 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10185 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10187 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10192 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10194 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10199 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10201 if (((off >> 32) & 0xffff) != 0)
10203 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10207 if (((off >> 32) & 0xffffffffULL) != 0)
10209 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10212 if (PPC_HI (off) != 0)
10214 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10217 if (PPC_LO (off) != 0)
10219 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10223 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10225 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10231 static unsigned int
10232 size_offset (bfd_vma off)
10235 if (off + 0x8000 < 0x10000)
10237 else if (off + 0x80008000ULL < 0x100000000ULL)
10241 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10246 if (((off >> 32) & 0xffff) != 0)
10249 if (((off >> 32) & 0xffffffffULL) != 0)
10251 if (PPC_HI (off) != 0)
10253 if (PPC_LO (off) != 0)
10260 static unsigned int
10261 num_relocs_for_offset (bfd_vma off)
10263 unsigned int num_rel;
10264 if (off + 0x8000 < 0x10000)
10266 else if (off + 0x80008000ULL < 0x100000000ULL)
10271 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10272 && ((off >> 32) & 0xffff) != 0)
10274 if (PPC_HI (off) != 0)
10276 if (PPC_LO (off) != 0)
10282 static Elf_Internal_Rela *
10283 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10284 bfd_vma roff, bfd_vma targ, bfd_vma off)
10286 bfd_vma relative_targ = targ - (roff - 8);
10287 if (bfd_big_endian (info->output_bfd))
10289 r->r_offset = roff;
10290 r->r_addend = relative_targ + roff;
10291 if (off + 0x8000 < 0x10000)
10292 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10293 else if (off + 0x80008000ULL < 0x100000000ULL)
10295 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10298 r->r_offset = roff;
10299 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10300 r->r_addend = relative_targ + roff;
10304 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10305 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10308 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10309 if (((off >> 32) & 0xffff) != 0)
10313 r->r_offset = roff;
10314 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10315 r->r_addend = relative_targ + roff;
10318 if (((off >> 32) & 0xffffffffULL) != 0)
10320 if (PPC_HI (off) != 0)
10324 r->r_offset = roff;
10325 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10326 r->r_addend = relative_targ + roff;
10328 if (PPC_LO (off) != 0)
10332 r->r_offset = roff;
10333 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10334 r->r_addend = relative_targ + roff;
10341 build_powerxx_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10345 if (off - odd + (1ULL << 33) < 1ULL << 34)
10350 bfd_put_32 (abfd, NOP, p);
10356 insn = PADDI_R12_PC;
10358 bfd_put_32 (abfd, insn >> 32, p);
10360 bfd_put_32 (abfd, insn, p);
10362 /* The minimum value for paddi is -0x200000000. The minimum value
10363 for li is -0x8000, which when shifted by 34 and added gives a
10364 minimum value of -0x2000200000000. The maximum value is
10365 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10366 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10369 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10373 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10376 insn = PADDI_R12_PC | D34 (off);
10377 bfd_put_32 (abfd, insn >> 32, p);
10379 bfd_put_32 (abfd, insn, p);
10383 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10387 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10389 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10394 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10396 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10400 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10403 insn = PADDI_R12_PC | D34 (off);
10404 bfd_put_32 (abfd, insn >> 32, p);
10406 bfd_put_32 (abfd, insn, p);
10410 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10414 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10416 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10422 static unsigned int
10423 size_powerxx_offset (bfd_vma off, int odd)
10425 if (off - odd + (1ULL << 33) < 1ULL << 34)
10427 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10433 static unsigned int
10434 num_relocs_for_powerxx_offset (bfd_vma off, int odd)
10436 if (off - odd + (1ULL << 33) < 1ULL << 34)
10438 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10444 static Elf_Internal_Rela *
10445 emit_relocs_for_powerxx_offset (struct bfd_link_info *info,
10446 Elf_Internal_Rela *r, bfd_vma roff,
10447 bfd_vma targ, bfd_vma off, int odd)
10449 if (off - odd + (1ULL << 33) < 1ULL << 34)
10451 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10453 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10454 r->r_offset = roff + d_offset;
10455 r->r_addend = targ + 8 - odd - d_offset;
10456 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10462 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10463 r->r_offset = roff + d_offset;
10464 r->r_addend = targ + 8 + odd - d_offset;
10465 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10468 r->r_offset = roff + d_offset;
10469 r->r_addend = targ + 4 + odd - d_offset;
10470 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10474 r->r_offset = roff;
10475 r->r_addend = targ;
10476 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10480 /* Emit .eh_frame opcode to advance pc by DELTA. */
10483 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10487 *eh++ = DW_CFA_advance_loc + delta;
10488 else if (delta < 256)
10490 *eh++ = DW_CFA_advance_loc1;
10493 else if (delta < 65536)
10495 *eh++ = DW_CFA_advance_loc2;
10496 bfd_put_16 (abfd, delta, eh);
10501 *eh++ = DW_CFA_advance_loc4;
10502 bfd_put_32 (abfd, delta, eh);
10508 /* Size of required .eh_frame opcode to advance pc by DELTA. */
10510 static unsigned int
10511 eh_advance_size (unsigned int delta)
10513 if (delta < 64 * 4)
10514 /* DW_CFA_advance_loc+[1..63]. */
10516 if (delta < 256 * 4)
10517 /* DW_CFA_advance_loc1, byte. */
10519 if (delta < 65536 * 4)
10520 /* DW_CFA_advance_loc2, 2 bytes. */
10522 /* DW_CFA_advance_loc4, 4 bytes. */
10526 /* With power7 weakly ordered memory model, it is possible for ld.so
10527 to update a plt entry in one thread and have another thread see a
10528 stale zero toc entry. To avoid this we need some sort of acquire
10529 barrier in the call stub. One solution is to make the load of the
10530 toc word seem to appear to depend on the load of the function entry
10531 word. Another solution is to test for r2 being zero, and branch to
10532 the appropriate glink entry if so.
10534 . fake dep barrier compare
10535 . ld 12,xxx(2) ld 12,xxx(2)
10536 . mtctr 12 mtctr 12
10537 . xor 11,12,12 ld 2,xxx+8(2)
10538 . add 2,2,11 cmpldi 2,0
10539 . ld 2,xxx+8(2) bnectr+
10540 . bctr b <glink_entry>
10542 The solution involving the compare turns out to be faster, so
10543 that's what we use unless the branch won't reach. */
10545 #define ALWAYS_USE_FAKE_DEP 0
10546 #define ALWAYS_EMIT_R2SAVE 0
10548 static inline unsigned int
10549 plt_stub_size (struct ppc_link_hash_table *htab,
10550 struct ppc_stub_hash_entry *stub_entry,
10555 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10557 if (htab->powerxx_stubs)
10559 bfd_vma start = (stub_entry->stub_offset
10560 + stub_entry->group->stub_sec->output_offset
10561 + stub_entry->group->stub_sec->output_section->vma);
10562 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10564 size = 8 + size_powerxx_offset (off, start & 4);
10567 size = 8 + size_offset (off - 8);
10568 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10574 if (ALWAYS_EMIT_R2SAVE
10575 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10577 if (PPC_HA (off) != 0)
10582 if (htab->params->plt_static_chain)
10584 if (htab->params->plt_thread_safe
10585 && htab->elf.dynamic_sections_created
10586 && stub_entry->h != NULL
10587 && stub_entry->h->elf.dynindx != -1)
10589 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10592 if (stub_entry->h != NULL
10593 && (stub_entry->h == htab->tls_get_addr_fd
10594 || stub_entry->h == htab->tls_get_addr)
10595 && htab->params->tls_get_addr_opt)
10598 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
10604 /* Depending on the sign of plt_stub_align:
10605 If positive, return the padding to align to a 2**plt_stub_align
10607 If negative, if this stub would cross fewer 2**plt_stub_align
10608 boundaries if we align, then return the padding needed to do so. */
10610 static inline unsigned int
10611 plt_stub_pad (struct ppc_link_hash_table *htab,
10612 struct ppc_stub_hash_entry *stub_entry,
10616 unsigned stub_size;
10617 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10619 if (htab->params->plt_stub_align >= 0)
10621 stub_align = 1 << htab->params->plt_stub_align;
10622 if ((stub_off & (stub_align - 1)) != 0)
10623 return stub_align - (stub_off & (stub_align - 1));
10627 stub_align = 1 << -htab->params->plt_stub_align;
10628 stub_size = plt_stub_size (htab, stub_entry, plt_off);
10629 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10630 > ((stub_size - 1) & -stub_align))
10631 return stub_align - (stub_off & (stub_align - 1));
10635 /* Build a .plt call stub. */
10637 static inline bfd_byte *
10638 build_plt_stub (struct ppc_link_hash_table *htab,
10639 struct ppc_stub_hash_entry *stub_entry,
10640 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10642 bfd *obfd = htab->params->stub_bfd;
10643 bfd_boolean plt_load_toc = htab->opd_abi;
10644 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10645 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10646 && htab->elf.dynamic_sections_created
10647 && stub_entry->h != NULL
10648 && stub_entry->h->elf.dynindx != -1);
10649 bfd_boolean use_fake_dep = plt_thread_safe;
10650 bfd_vma cmp_branch_off = 0;
10652 if (!ALWAYS_USE_FAKE_DEP
10655 && !((stub_entry->h == htab->tls_get_addr_fd
10656 || stub_entry->h == htab->tls_get_addr)
10657 && htab->params->tls_get_addr_opt))
10659 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10660 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10661 / PLT_ENTRY_SIZE (htab));
10662 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10665 if (pltindex > 32768)
10666 glinkoff += (pltindex - 32768) * 4;
10668 + htab->glink->output_offset
10669 + htab->glink->output_section->vma);
10670 from = (p - stub_entry->group->stub_sec->contents
10671 + 4 * (ALWAYS_EMIT_R2SAVE
10672 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10673 + 4 * (PPC_HA (offset) != 0)
10674 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10675 != PPC_HA (offset))
10676 + 4 * (plt_static_chain != 0)
10678 + stub_entry->group->stub_sec->output_offset
10679 + stub_entry->group->stub_sec->output_section->vma);
10680 cmp_branch_off = to - from;
10681 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10684 if (PPC_HA (offset) != 0)
10688 if (ALWAYS_EMIT_R2SAVE
10689 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10690 r[0].r_offset += 4;
10691 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10692 r[1].r_offset = r[0].r_offset + 4;
10693 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10694 r[1].r_addend = r[0].r_addend;
10697 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10699 r[2].r_offset = r[1].r_offset + 4;
10700 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10701 r[2].r_addend = r[0].r_addend;
10705 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10706 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10707 r[2].r_addend = r[0].r_addend + 8;
10708 if (plt_static_chain)
10710 r[3].r_offset = r[2].r_offset + 4;
10711 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10712 r[3].r_addend = r[0].r_addend + 16;
10717 if (ALWAYS_EMIT_R2SAVE
10718 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10719 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10722 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10723 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10727 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10728 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10731 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10733 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10736 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10741 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10742 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10744 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10745 if (plt_static_chain)
10746 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10753 if (ALWAYS_EMIT_R2SAVE
10754 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10755 r[0].r_offset += 4;
10756 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10759 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10761 r[1].r_offset = r[0].r_offset + 4;
10762 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10763 r[1].r_addend = r[0].r_addend;
10767 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10768 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10769 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10770 if (plt_static_chain)
10772 r[2].r_offset = r[1].r_offset + 4;
10773 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10774 r[2].r_addend = r[0].r_addend + 8;
10779 if (ALWAYS_EMIT_R2SAVE
10780 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10781 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10782 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10784 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10786 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10789 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10794 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10795 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10797 if (plt_static_chain)
10798 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10799 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10802 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10804 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10805 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
10806 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10809 bfd_put_32 (obfd, BCTR, p), p += 4;
10813 /* Build a special .plt call stub for __tls_get_addr. */
10815 #define LD_R11_0R3 0xe9630000
10816 #define LD_R12_0R3 0xe9830000
10817 #define MR_R0_R3 0x7c601b78
10818 #define CMPDI_R11_0 0x2c2b0000
10819 #define ADD_R3_R12_R13 0x7c6c6a14
10820 #define BEQLR 0x4d820020
10821 #define MR_R3_R0 0x7c030378
10822 #define STD_R11_0R1 0xf9610000
10823 #define BCTRL 0x4e800421
10824 #define LD_R11_0R1 0xe9610000
10825 #define MTLR_R11 0x7d6803a6
10827 static inline bfd_byte *
10828 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10829 struct ppc_stub_hash_entry *stub_entry,
10830 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10832 bfd *obfd = htab->params->stub_bfd;
10835 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10836 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10837 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10838 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10839 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10840 bfd_put_32 (obfd, BEQLR, p), p += 4;
10841 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10843 r[0].r_offset += 7 * 4;
10844 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
10845 return build_plt_stub (htab, stub_entry, p, offset, r);
10847 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10848 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10851 r[0].r_offset += 2 * 4;
10852 p = build_plt_stub (htab, stub_entry, p, offset, r);
10853 bfd_put_32 (obfd, BCTRL, p - 4);
10855 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
10856 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10857 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10858 bfd_put_32 (obfd, BLR, p), p += 4;
10860 if (htab->glink_eh_frame != NULL
10861 && htab->glink_eh_frame->size != 0)
10863 bfd_byte *base, *eh;
10864 unsigned int lr_used, delta;
10866 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10867 eh = base + stub_entry->group->eh_size;
10868 lr_used = stub_entry->stub_offset + (p - 20 - loc);
10869 delta = lr_used - stub_entry->group->lr_restore;
10870 stub_entry->group->lr_restore = lr_used + 16;
10871 eh = eh_advance (htab->elf.dynobj, eh, delta);
10872 *eh++ = DW_CFA_offset_extended_sf;
10874 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10875 *eh++ = DW_CFA_advance_loc + 4;
10876 *eh++ = DW_CFA_restore_extended;
10878 stub_entry->group->eh_size = eh - base;
10883 static Elf_Internal_Rela *
10884 get_relocs (asection *sec, int count)
10886 Elf_Internal_Rela *relocs;
10887 struct bfd_elf_section_data *elfsec_data;
10889 elfsec_data = elf_section_data (sec);
10890 relocs = elfsec_data->relocs;
10891 if (relocs == NULL)
10893 bfd_size_type relsize;
10894 relsize = sec->reloc_count * sizeof (*relocs);
10895 relocs = bfd_alloc (sec->owner, relsize);
10896 if (relocs == NULL)
10898 elfsec_data->relocs = relocs;
10899 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10900 sizeof (Elf_Internal_Shdr));
10901 if (elfsec_data->rela.hdr == NULL)
10903 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10904 * sizeof (Elf64_External_Rela));
10905 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10906 sec->reloc_count = 0;
10908 relocs += sec->reloc_count;
10909 sec->reloc_count += count;
10913 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10914 forms, to the equivalent relocs against the global symbol given by
10918 use_global_in_relocs (struct ppc_link_hash_table *htab,
10919 struct ppc_stub_hash_entry *stub_entry,
10920 Elf_Internal_Rela *r, unsigned int num_rel)
10922 struct elf_link_hash_entry **hashes;
10923 unsigned long symndx;
10924 struct ppc_link_hash_entry *h;
10927 /* Relocs are always against symbols in their own object file. Fake
10928 up global sym hashes for the stub bfd (which has no symbols). */
10929 hashes = elf_sym_hashes (htab->params->stub_bfd);
10930 if (hashes == NULL)
10932 bfd_size_type hsize;
10934 /* When called the first time, stub_globals will contain the
10935 total number of symbols seen during stub sizing. After
10936 allocating, stub_globals is used as an index to fill the
10938 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10939 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10940 if (hashes == NULL)
10942 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10943 htab->stub_globals = 1;
10945 symndx = htab->stub_globals++;
10947 hashes[symndx] = &h->elf;
10948 if (h->oh != NULL && h->oh->is_func)
10949 h = ppc_follow_link (h->oh);
10950 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10951 || h->elf.root.type == bfd_link_hash_defweak);
10952 symval = (h->elf.root.u.def.value
10953 + h->elf.root.u.def.section->output_offset
10954 + h->elf.root.u.def.section->output_section->vma);
10955 while (num_rel-- != 0)
10957 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10958 if (h->elf.root.u.def.section != stub_entry->target_section)
10960 /* H is an opd symbol. The addend must be zero, and the
10961 branch reloc is the only one we can convert. */
10966 r->r_addend -= symval;
10973 get_r2off (struct bfd_link_info *info,
10974 struct ppc_stub_hash_entry *stub_entry)
10976 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10977 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10981 /* Support linking -R objects. Get the toc pointer from the
10984 if (!htab->opd_abi)
10986 asection *opd = stub_entry->h->elf.root.u.def.section;
10987 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10989 if (strcmp (opd->name, ".opd") != 0
10990 || opd->reloc_count != 0)
10992 info->callbacks->einfo
10993 (_("%P: cannot find opd entry toc for `%pT'\n"),
10994 stub_entry->h->elf.root.root.string);
10995 bfd_set_error (bfd_error_bad_value);
10996 return (bfd_vma) -1;
10998 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10999 return (bfd_vma) -1;
11000 r2off = bfd_get_64 (opd->owner, buf);
11001 r2off -= elf_gp (info->output_bfd);
11003 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11008 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11010 struct ppc_stub_hash_entry *stub_entry;
11011 struct ppc_branch_hash_entry *br_entry;
11012 struct bfd_link_info *info;
11013 struct ppc_link_hash_table *htab;
11015 bfd_byte *p, *relp;
11017 Elf_Internal_Rela *r;
11022 /* Massage our args to the form they really have. */
11023 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11026 htab = ppc_hash_table (info);
11030 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
11031 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11033 htab->stub_count[stub_entry->stub_type - 1] += 1;
11034 switch (stub_entry->stub_type)
11036 case ppc_stub_long_branch:
11037 case ppc_stub_long_branch_r2off:
11038 /* Branches are relative. This is where we are going to. */
11039 targ = (stub_entry->target_value
11040 + stub_entry->target_section->output_offset
11041 + stub_entry->target_section->output_section->vma);
11042 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11044 /* And this is where we are coming from. */
11045 off = (stub_entry->stub_offset
11046 + stub_entry->group->stub_sec->output_offset
11047 + stub_entry->group->stub_sec->output_section->vma);
11051 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11053 bfd_vma r2off = get_r2off (info, stub_entry);
11055 if (r2off == (bfd_vma) -1)
11057 htab->stub_error = TRUE;
11060 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11062 if (PPC_HA (r2off) != 0)
11064 bfd_put_32 (htab->params->stub_bfd,
11065 ADDIS_R2_R2 | PPC_HA (r2off), p);
11068 if (PPC_LO (r2off) != 0)
11070 bfd_put_32 (htab->params->stub_bfd,
11071 ADDI_R2_R2 | PPC_LO (r2off), p);
11076 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11079 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11082 (_("long branch stub `%s' offset overflow"),
11083 stub_entry->root.string);
11084 htab->stub_error = TRUE;
11088 if (info->emitrelocations)
11090 r = get_relocs (stub_entry->group->stub_sec, 1);
11093 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11094 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11095 r->r_addend = targ;
11096 if (stub_entry->h != NULL
11097 && !use_global_in_relocs (htab, stub_entry, r, 1))
11102 case ppc_stub_plt_branch:
11103 case ppc_stub_plt_branch_r2off:
11104 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11105 stub_entry->root.string + 9,
11107 if (br_entry == NULL)
11109 _bfd_error_handler (_("can't find branch stub `%s'"),
11110 stub_entry->root.string);
11111 htab->stub_error = TRUE;
11115 targ = (stub_entry->target_value
11116 + stub_entry->target_section->output_offset
11117 + stub_entry->target_section->output_section->vma);
11118 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11119 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11121 bfd_put_64 (htab->brlt->owner, targ,
11122 htab->brlt->contents + br_entry->offset);
11124 if (br_entry->iter == htab->stub_iteration)
11126 br_entry->iter = 0;
11128 if (htab->relbrlt != NULL)
11130 /* Create a reloc for the branch lookup table entry. */
11131 Elf_Internal_Rela rela;
11134 rela.r_offset = (br_entry->offset
11135 + htab->brlt->output_offset
11136 + htab->brlt->output_section->vma);
11137 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11138 rela.r_addend = targ;
11140 rl = htab->relbrlt->contents;
11141 rl += (htab->relbrlt->reloc_count++
11142 * sizeof (Elf64_External_Rela));
11143 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11145 else if (info->emitrelocations)
11147 r = get_relocs (htab->brlt, 1);
11150 /* brlt, being SEC_LINKER_CREATED does not go through the
11151 normal reloc processing. Symbols and offsets are not
11152 translated from input file to output file form, so
11153 set up the offset per the output file. */
11154 r->r_offset = (br_entry->offset
11155 + htab->brlt->output_offset
11156 + htab->brlt->output_section->vma);
11157 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11158 r->r_addend = targ;
11162 targ = (br_entry->offset
11163 + htab->brlt->output_offset
11164 + htab->brlt->output_section->vma);
11166 off = (elf_gp (info->output_bfd)
11167 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11170 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11172 info->callbacks->einfo
11173 (_("%P: linkage table error against `%pT'\n"),
11174 stub_entry->root.string);
11175 bfd_set_error (bfd_error_bad_value);
11176 htab->stub_error = TRUE;
11180 if (info->emitrelocations)
11182 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11185 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11186 if (bfd_big_endian (info->output_bfd))
11187 r[0].r_offset += 2;
11188 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11189 r[0].r_offset += 4;
11190 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11191 r[0].r_addend = targ;
11192 if (PPC_HA (off) != 0)
11194 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11195 r[1].r_offset = r[0].r_offset + 4;
11196 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11197 r[1].r_addend = r[0].r_addend;
11202 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11204 if (PPC_HA (off) != 0)
11206 bfd_put_32 (htab->params->stub_bfd,
11207 ADDIS_R12_R2 | PPC_HA (off), p);
11209 bfd_put_32 (htab->params->stub_bfd,
11210 LD_R12_0R12 | PPC_LO (off), p);
11213 bfd_put_32 (htab->params->stub_bfd,
11214 LD_R12_0R2 | PPC_LO (off), p);
11218 bfd_vma r2off = get_r2off (info, stub_entry);
11220 if (r2off == (bfd_vma) -1)
11222 htab->stub_error = TRUE;
11226 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11228 if (PPC_HA (off) != 0)
11230 bfd_put_32 (htab->params->stub_bfd,
11231 ADDIS_R12_R2 | PPC_HA (off), p);
11233 bfd_put_32 (htab->params->stub_bfd,
11234 LD_R12_0R12 | PPC_LO (off), p);
11237 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11239 if (PPC_HA (r2off) != 0)
11242 bfd_put_32 (htab->params->stub_bfd,
11243 ADDIS_R2_R2 | PPC_HA (r2off), p);
11245 if (PPC_LO (r2off) != 0)
11248 bfd_put_32 (htab->params->stub_bfd,
11249 ADDI_R2_R2 | PPC_LO (r2off), p);
11253 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11255 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11259 case ppc_stub_long_branch_notoc:
11260 case ppc_stub_long_branch_both:
11261 case ppc_stub_plt_branch_notoc:
11262 case ppc_stub_plt_branch_both:
11263 case ppc_stub_plt_call_notoc:
11264 case ppc_stub_plt_call_both:
11266 off = (stub_entry->stub_offset
11267 + stub_entry->group->stub_sec->output_offset
11268 + stub_entry->group->stub_sec->output_section->vma);
11269 if (stub_entry->stub_type == ppc_stub_long_branch_both
11270 || stub_entry->stub_type == ppc_stub_plt_branch_both
11271 || stub_entry->stub_type == ppc_stub_plt_call_both)
11274 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11277 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11279 targ = stub_entry->plt_ent->plt.offset & ~1;
11280 if (targ >= (bfd_vma) -2)
11283 plt = htab->elf.splt;
11284 if (!htab->elf.dynamic_sections_created
11285 || stub_entry->h == NULL
11286 || stub_entry->h->elf.dynindx == -1)
11288 if (stub_entry->symtype == STT_GNU_IFUNC)
11289 plt = htab->elf.iplt;
11291 plt = htab->pltlocal;
11293 targ += plt->output_offset + plt->output_section->vma;
11296 targ = (stub_entry->target_value
11297 + stub_entry->target_section->output_offset
11298 + stub_entry->target_section->output_section->vma);
11304 if (htab->powerxx_stubs)
11306 bfd_boolean load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
11307 p = build_powerxx_offset (htab->params->stub_bfd, p, off, odd, load);
11311 /* The notoc stubs calculate their target (either a PLT entry or
11312 the global entry point of a function) relative to the PC
11313 returned by the "bcl" two instructions past the start of the
11314 sequence emitted by build_offset. The offset is therefore 8
11315 less than calculated from the start of the sequence. */
11317 p = build_offset (htab->params->stub_bfd, p, off,
11318 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11321 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
11325 from = (stub_entry->stub_offset
11326 + stub_entry->group->stub_sec->output_offset
11327 + stub_entry->group->stub_sec->output_section->vma
11329 bfd_put_32 (htab->params->stub_bfd,
11330 B_DOT | ((targ - from) & 0x3fffffc), p);
11334 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11336 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11340 if (info->emitrelocations)
11342 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
11343 if (htab->powerxx_stubs)
11344 num_rel += num_relocs_for_powerxx_offset (off, odd);
11347 num_rel += num_relocs_for_offset (off);
11350 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11353 if (htab->powerxx_stubs)
11354 r = emit_relocs_for_powerxx_offset (info, r, roff, targ, off, odd);
11356 r = emit_relocs_for_offset (info, r, roff, targ, off);
11357 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11358 || stub_entry->stub_type == ppc_stub_long_branch_both)
11361 roff = p - 4 - stub_entry->group->stub_sec->contents;
11362 r->r_offset = roff;
11363 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11364 r->r_addend = targ;
11365 if (stub_entry->h != NULL
11366 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11371 if (!htab->powerxx_stubs
11372 && htab->glink_eh_frame != NULL
11373 && htab->glink_eh_frame->size != 0)
11375 bfd_byte *base, *eh;
11376 unsigned int lr_used, delta;
11378 base = (htab->glink_eh_frame->contents
11379 + stub_entry->group->eh_base + 17);
11380 eh = base + stub_entry->group->eh_size;
11381 lr_used = stub_entry->stub_offset + 8;
11382 if (stub_entry->stub_type == ppc_stub_long_branch_both
11383 || stub_entry->stub_type == ppc_stub_plt_branch_both
11384 || stub_entry->stub_type == ppc_stub_plt_call_both)
11386 delta = lr_used - stub_entry->group->lr_restore;
11387 stub_entry->group->lr_restore = lr_used + 8;
11388 eh = eh_advance (htab->elf.dynobj, eh, delta);
11389 *eh++ = DW_CFA_register;
11392 *eh++ = DW_CFA_advance_loc + 2;
11393 *eh++ = DW_CFA_restore_extended;
11395 stub_entry->group->eh_size = eh - base;
11399 case ppc_stub_plt_call:
11400 case ppc_stub_plt_call_r2save:
11401 if (stub_entry->h != NULL
11402 && stub_entry->h->is_func_descriptor
11403 && stub_entry->h->oh != NULL)
11405 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11407 /* If the old-ABI "dot-symbol" is undefined make it weak so
11408 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11409 if (fh->elf.root.type == bfd_link_hash_undefined
11410 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11411 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11412 fh->elf.root.type = bfd_link_hash_undefweak;
11415 /* Now build the stub. */
11416 targ = stub_entry->plt_ent->plt.offset & ~1;
11417 if (targ >= (bfd_vma) -2)
11420 plt = htab->elf.splt;
11421 if (!htab->elf.dynamic_sections_created
11422 || stub_entry->h == NULL
11423 || stub_entry->h->elf.dynindx == -1)
11425 if (stub_entry->symtype == STT_GNU_IFUNC)
11426 plt = htab->elf.iplt;
11428 plt = htab->pltlocal;
11430 targ += plt->output_offset + plt->output_section->vma;
11432 off = (elf_gp (info->output_bfd)
11433 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11436 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11438 info->callbacks->einfo
11439 /* xgettext:c-format */
11440 (_("%P: linkage table error against `%pT'\n"),
11441 stub_entry->h != NULL
11442 ? stub_entry->h->elf.root.root.string
11444 bfd_set_error (bfd_error_bad_value);
11445 htab->stub_error = TRUE;
11450 if (info->emitrelocations)
11452 r = get_relocs (stub_entry->group->stub_sec,
11453 ((PPC_HA (off) != 0)
11455 ? 2 + (htab->params->plt_static_chain
11456 && PPC_HA (off + 16) == PPC_HA (off))
11460 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11461 if (bfd_big_endian (info->output_bfd))
11462 r[0].r_offset += 2;
11463 r[0].r_addend = targ;
11465 if (stub_entry->h != NULL
11466 && (stub_entry->h == htab->tls_get_addr_fd
11467 || stub_entry->h == htab->tls_get_addr)
11468 && htab->params->tls_get_addr_opt)
11469 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11471 p = build_plt_stub (htab, stub_entry, loc, off, r);
11474 case ppc_stub_save_res:
11482 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
11484 if (htab->params->emit_stub_syms)
11486 struct elf_link_hash_entry *h;
11489 const char *const stub_str[] = { "long_branch",
11502 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11503 len2 = strlen (stub_entry->root.string);
11504 name = bfd_malloc (len1 + len2 + 2);
11507 memcpy (name, stub_entry->root.string, 9);
11508 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11509 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11510 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11513 if (h->root.type == bfd_link_hash_new)
11515 h->root.type = bfd_link_hash_defined;
11516 h->root.u.def.section = stub_entry->group->stub_sec;
11517 h->root.u.def.value = stub_entry->stub_offset;
11518 h->ref_regular = 1;
11519 h->def_regular = 1;
11520 h->ref_regular_nonweak = 1;
11521 h->forced_local = 1;
11523 h->root.linker_def = 1;
11530 /* As above, but don't actually build the stub. Just bump offset so
11531 we know stub section sizes, and select plt_branch stubs where
11532 long_branch stubs won't do. */
11535 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11537 struct ppc_stub_hash_entry *stub_entry;
11538 struct bfd_link_info *info;
11539 struct ppc_link_hash_table *htab;
11541 bfd_vma targ, off, r2off;
11542 unsigned int size, extra, lr_used, delta, odd;
11544 /* Massage our args to the form they really have. */
11545 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11548 htab = ppc_hash_table (info);
11552 /* Make a note of the offset within the stubs for this entry. */
11553 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11555 if (stub_entry->h != NULL
11556 && stub_entry->h->save_res
11557 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11558 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11560 /* Don't make stubs to out-of-line register save/restore
11561 functions. Instead, emit copies of the functions. */
11562 stub_entry->group->needs_save_res = 1;
11563 stub_entry->stub_type = ppc_stub_save_res;
11567 switch (stub_entry->stub_type)
11569 case ppc_stub_plt_branch:
11570 case ppc_stub_plt_branch_r2off:
11571 /* Reset the stub type from the plt branch variant in case we now
11572 can reach with a shorter stub. */
11573 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11574 /* Fall through. */
11575 case ppc_stub_long_branch:
11576 case ppc_stub_long_branch_r2off:
11577 targ = (stub_entry->target_value
11578 + stub_entry->target_section->output_offset
11579 + stub_entry->target_section->output_section->vma);
11580 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11581 off = (stub_entry->stub_offset
11582 + stub_entry->group->stub_sec->output_offset
11583 + stub_entry->group->stub_sec->output_section->vma);
11587 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11589 r2off = get_r2off (info, stub_entry);
11590 if (r2off == (bfd_vma) -1)
11592 htab->stub_error = TRUE;
11596 if (PPC_HA (r2off) != 0)
11598 if (PPC_LO (r2off) != 0)
11604 /* If the branch offset is too big, use a ppc_stub_plt_branch.
11605 Do the same for -R objects without function descriptors. */
11606 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
11608 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
11609 || off + (1 << 25) >= (bfd_vma) (1 << 26))
11611 struct ppc_branch_hash_entry *br_entry;
11613 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11614 stub_entry->root.string + 9,
11616 if (br_entry == NULL)
11618 _bfd_error_handler (_("can't build branch stub `%s'"),
11619 stub_entry->root.string);
11620 htab->stub_error = TRUE;
11624 if (br_entry->iter != htab->stub_iteration)
11626 br_entry->iter = htab->stub_iteration;
11627 br_entry->offset = htab->brlt->size;
11628 htab->brlt->size += 8;
11630 if (htab->relbrlt != NULL)
11631 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11632 else if (info->emitrelocations)
11634 htab->brlt->reloc_count += 1;
11635 htab->brlt->flags |= SEC_RELOC;
11639 targ = (br_entry->offset
11640 + htab->brlt->output_offset
11641 + htab->brlt->output_section->vma);
11642 off = (elf_gp (info->output_bfd)
11643 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11646 if (info->emitrelocations)
11648 stub_entry->group->stub_sec->reloc_count
11649 += 1 + (PPC_HA (off) != 0);
11650 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11653 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11654 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11657 if (PPC_HA (off) != 0)
11663 if (PPC_HA (off) != 0)
11666 if (PPC_HA (r2off) != 0)
11668 if (PPC_LO (r2off) != 0)
11672 else if (info->emitrelocations)
11674 stub_entry->group->stub_sec->reloc_count += 1;
11675 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11679 case ppc_stub_plt_branch_notoc:
11680 case ppc_stub_plt_branch_both:
11681 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11682 /* Fall through. */
11683 case ppc_stub_long_branch_notoc:
11684 case ppc_stub_long_branch_both:
11685 off = (stub_entry->stub_offset
11686 + stub_entry->group->stub_sec->output_offset
11687 + stub_entry->group->stub_sec->output_section->vma);
11689 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11692 targ = (stub_entry->target_value
11693 + stub_entry->target_section->output_offset
11694 + stub_entry->target_section->output_section->vma);
11698 if (info->emitrelocations)
11700 unsigned int num_rel;
11701 if (htab->powerxx_stubs)
11702 num_rel = num_relocs_for_powerxx_offset (off, odd);
11704 num_rel = num_relocs_for_offset (off - 8);
11705 stub_entry->group->stub_sec->reloc_count += num_rel;
11706 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11709 if (htab->powerxx_stubs)
11710 extra = size_powerxx_offset (off, odd);
11712 extra = size_offset (off - 8);
11713 /* Include branch insn plus those in the offset sequence. */
11715 /* The branch insn is at the end, or "extra" bytes along. So
11716 its offset will be "extra" bytes less that that already
11720 if (!htab->powerxx_stubs)
11722 /* After the bcl, lr has been modified so we need to emit
11723 .eh_frame info saying the return address is in r12. */
11724 lr_used = stub_entry->stub_offset + 8;
11725 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11727 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11728 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11729 DW_CFA_restore_extended 65. */
11730 delta = lr_used - stub_entry->group->lr_restore;
11731 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11732 stub_entry->group->lr_restore = lr_used + 8;
11735 /* If the branch can't reach, use a plt_branch. */
11736 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11738 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11739 - ppc_stub_long_branch_notoc);
11742 else if (info->emitrelocations)
11743 stub_entry->group->stub_sec->reloc_count +=1;
11746 case ppc_stub_plt_call_notoc:
11747 case ppc_stub_plt_call_both:
11748 off = (stub_entry->stub_offset
11749 + stub_entry->group->stub_sec->output_offset
11750 + stub_entry->group->stub_sec->output_section->vma);
11751 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11753 targ = stub_entry->plt_ent->plt.offset & ~1;
11754 if (targ >= (bfd_vma) -2)
11757 plt = htab->elf.splt;
11758 if (!htab->elf.dynamic_sections_created
11759 || stub_entry->h == NULL
11760 || stub_entry->h->elf.dynindx == -1)
11762 if (stub_entry->symtype == STT_GNU_IFUNC)
11763 plt = htab->elf.iplt;
11765 plt = htab->pltlocal;
11767 targ += plt->output_offset + plt->output_section->vma;
11771 if (htab->params->plt_stub_align != 0)
11773 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11775 stub_entry->group->stub_sec->size += pad;
11776 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11780 if (info->emitrelocations)
11782 unsigned int num_rel;
11783 if (htab->powerxx_stubs)
11784 num_rel = num_relocs_for_powerxx_offset (off, odd);
11786 num_rel = num_relocs_for_offset (off - 8);
11787 stub_entry->group->stub_sec->reloc_count += num_rel;
11788 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11791 size = plt_stub_size (htab, stub_entry, off);
11793 if (!htab->powerxx_stubs)
11795 /* After the bcl, lr has been modified so we need to emit
11796 .eh_frame info saying the return address is in r12. */
11797 lr_used = stub_entry->stub_offset + 8;
11798 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11800 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11801 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11802 DW_CFA_restore_extended 65. */
11803 delta = lr_used - stub_entry->group->lr_restore;
11804 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11805 stub_entry->group->lr_restore = lr_used + 8;
11809 case ppc_stub_plt_call:
11810 case ppc_stub_plt_call_r2save:
11811 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11812 if (targ >= (bfd_vma) -2)
11814 plt = htab->elf.splt;
11815 if (!htab->elf.dynamic_sections_created
11816 || stub_entry->h == NULL
11817 || stub_entry->h->elf.dynindx == -1)
11819 if (stub_entry->symtype == STT_GNU_IFUNC)
11820 plt = htab->elf.iplt;
11822 plt = htab->pltlocal;
11824 targ += plt->output_offset + plt->output_section->vma;
11826 off = (elf_gp (info->output_bfd)
11827 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11830 if (htab->params->plt_stub_align != 0)
11832 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11834 stub_entry->group->stub_sec->size += pad;
11835 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11838 if (info->emitrelocations)
11840 stub_entry->group->stub_sec->reloc_count
11841 += ((PPC_HA (off) != 0)
11843 ? 2 + (htab->params->plt_static_chain
11844 && PPC_HA (off + 16) == PPC_HA (off))
11846 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11849 size = plt_stub_size (htab, stub_entry, off);
11851 if (stub_entry->h != NULL
11852 && (stub_entry->h == htab->tls_get_addr_fd
11853 || stub_entry->h == htab->tls_get_addr)
11854 && htab->params->tls_get_addr_opt
11855 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11857 /* After the bctrl, lr has been modified so we need to
11858 emit .eh_frame info saying the return address is
11859 on the stack. In fact we put the EH info specifying
11860 that the return address is on the stack *at* the
11861 call rather than after it, because the EH info for a
11862 call needs to be specified by that point.
11863 See libgcc/unwind-dw2.c execute_cfa_program. */
11864 lr_used = stub_entry->stub_offset + size - 20;
11865 /* The eh_frame info will consist of a DW_CFA_advance_loc
11866 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11867 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
11868 delta = lr_used - stub_entry->group->lr_restore;
11869 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11870 stub_entry->group->lr_restore = size - 4;
11879 stub_entry->group->stub_sec->size += size;
11883 /* Set up various things so that we can make a list of input sections
11884 for each output section included in the link. Returns -1 on error,
11885 0 when no stubs will be needed, and 1 on success. */
11888 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11892 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11897 htab->sec_info_arr_size = _bfd_section_id;
11898 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11899 htab->sec_info = bfd_zmalloc (amt);
11900 if (htab->sec_info == NULL)
11903 /* Set toc_off for com, und, abs and ind sections. */
11904 for (id = 0; id < 3; id++)
11905 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11910 /* Set up for first pass at multitoc partitioning. */
11913 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11915 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11917 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11918 htab->toc_bfd = NULL;
11919 htab->toc_first_sec = NULL;
11922 /* The linker repeatedly calls this function for each TOC input section
11923 and linker generated GOT section. Group input bfds such that the toc
11924 within a group is less than 64k in size. */
11927 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11929 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11930 bfd_vma addr, off, limit;
11935 if (!htab->second_toc_pass)
11937 /* Keep track of the first .toc or .got section for this input bfd. */
11938 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11942 htab->toc_bfd = isec->owner;
11943 htab->toc_first_sec = isec;
11946 addr = isec->output_offset + isec->output_section->vma;
11947 off = addr - htab->toc_curr;
11948 limit = 0x80008000;
11949 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11951 if (off + isec->size > limit)
11953 addr = (htab->toc_first_sec->output_offset
11954 + htab->toc_first_sec->output_section->vma);
11955 htab->toc_curr = addr;
11956 htab->toc_curr &= -TOC_BASE_ALIGN;
11959 /* toc_curr is the base address of this toc group. Set elf_gp
11960 for the input section to be the offset relative to the
11961 output toc base plus 0x8000. Making the input elf_gp an
11962 offset allows us to move the toc as a whole without
11963 recalculating input elf_gp. */
11964 off = htab->toc_curr - elf_gp (info->output_bfd);
11965 off += TOC_BASE_OFF;
11967 /* Die if someone uses a linker script that doesn't keep input
11968 file .toc and .got together. */
11970 && elf_gp (isec->owner) != 0
11971 && elf_gp (isec->owner) != off)
11974 elf_gp (isec->owner) = off;
11978 /* During the second pass toc_first_sec points to the start of
11979 a toc group, and toc_curr is used to track the old elf_gp.
11980 We use toc_bfd to ensure we only look at each bfd once. */
11981 if (htab->toc_bfd == isec->owner)
11983 htab->toc_bfd = isec->owner;
11985 if (htab->toc_first_sec == NULL
11986 || htab->toc_curr != elf_gp (isec->owner))
11988 htab->toc_curr = elf_gp (isec->owner);
11989 htab->toc_first_sec = isec;
11991 addr = (htab->toc_first_sec->output_offset
11992 + htab->toc_first_sec->output_section->vma);
11993 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
11994 elf_gp (isec->owner) = off;
11999 /* Called via elf_link_hash_traverse to merge GOT entries for global
12003 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12005 if (h->root.type == bfd_link_hash_indirect)
12008 merge_got_entries (&h->got.glist);
12013 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12017 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12019 struct got_entry *gent;
12021 if (h->root.type == bfd_link_hash_indirect)
12024 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12025 if (!gent->is_indirect)
12026 allocate_got (h, (struct bfd_link_info *) inf, gent);
12030 /* Called on the first multitoc pass after the last call to
12031 ppc64_elf_next_toc_section. This function removes duplicate GOT
12035 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12037 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12038 struct bfd *ibfd, *ibfd2;
12039 bfd_boolean done_something;
12041 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12043 if (!htab->do_multi_toc)
12046 /* Merge global sym got entries within a toc group. */
12047 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12049 /* And tlsld_got. */
12050 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12052 struct got_entry *ent, *ent2;
12054 if (!is_ppc64_elf (ibfd))
12057 ent = ppc64_tlsld_got (ibfd);
12058 if (!ent->is_indirect
12059 && ent->got.offset != (bfd_vma) -1)
12061 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12063 if (!is_ppc64_elf (ibfd2))
12066 ent2 = ppc64_tlsld_got (ibfd2);
12067 if (!ent2->is_indirect
12068 && ent2->got.offset != (bfd_vma) -1
12069 && elf_gp (ibfd2) == elf_gp (ibfd))
12071 ent2->is_indirect = TRUE;
12072 ent2->got.ent = ent;
12078 /* Zap sizes of got sections. */
12079 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12080 htab->elf.irelplt->size -= htab->got_reli_size;
12081 htab->got_reli_size = 0;
12083 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12085 asection *got, *relgot;
12087 if (!is_ppc64_elf (ibfd))
12090 got = ppc64_elf_tdata (ibfd)->got;
12093 got->rawsize = got->size;
12095 relgot = ppc64_elf_tdata (ibfd)->relgot;
12096 relgot->rawsize = relgot->size;
12101 /* Now reallocate the got, local syms first. We don't need to
12102 allocate section contents again since we never increase size. */
12103 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12105 struct got_entry **lgot_ents;
12106 struct got_entry **end_lgot_ents;
12107 struct plt_entry **local_plt;
12108 struct plt_entry **end_local_plt;
12109 unsigned char *lgot_masks;
12110 bfd_size_type locsymcount;
12111 Elf_Internal_Shdr *symtab_hdr;
12114 if (!is_ppc64_elf (ibfd))
12117 lgot_ents = elf_local_got_ents (ibfd);
12121 symtab_hdr = &elf_symtab_hdr (ibfd);
12122 locsymcount = symtab_hdr->sh_info;
12123 end_lgot_ents = lgot_ents + locsymcount;
12124 local_plt = (struct plt_entry **) end_lgot_ents;
12125 end_local_plt = local_plt + locsymcount;
12126 lgot_masks = (unsigned char *) end_local_plt;
12127 s = ppc64_elf_tdata (ibfd)->got;
12128 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12130 struct got_entry *ent;
12132 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12134 unsigned int ent_size = 8;
12135 unsigned int rel_size = sizeof (Elf64_External_Rela);
12137 ent->got.offset = s->size;
12138 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12143 s->size += ent_size;
12144 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12146 htab->elf.irelplt->size += rel_size;
12147 htab->got_reli_size += rel_size;
12149 else if (bfd_link_pic (info)
12150 && !((ent->tls_type & TLS_TPREL) != 0
12151 && bfd_link_executable (info)))
12153 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12154 srel->size += rel_size;
12160 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12162 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12164 struct got_entry *ent;
12166 if (!is_ppc64_elf (ibfd))
12169 ent = ppc64_tlsld_got (ibfd);
12170 if (!ent->is_indirect
12171 && ent->got.offset != (bfd_vma) -1)
12173 asection *s = ppc64_elf_tdata (ibfd)->got;
12174 ent->got.offset = s->size;
12176 if (bfd_link_pic (info))
12178 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12179 srel->size += sizeof (Elf64_External_Rela);
12184 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12185 if (!done_something)
12186 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12190 if (!is_ppc64_elf (ibfd))
12193 got = ppc64_elf_tdata (ibfd)->got;
12196 done_something = got->rawsize != got->size;
12197 if (done_something)
12202 if (done_something)
12203 (*htab->params->layout_sections_again) ();
12205 /* Set up for second pass over toc sections to recalculate elf_gp
12206 on input sections. */
12207 htab->toc_bfd = NULL;
12208 htab->toc_first_sec = NULL;
12209 htab->second_toc_pass = TRUE;
12210 return done_something;
12213 /* Called after second pass of multitoc partitioning. */
12216 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12218 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12220 /* After the second pass, toc_curr tracks the TOC offset used
12221 for code sections below in ppc64_elf_next_input_section. */
12222 htab->toc_curr = TOC_BASE_OFF;
12225 /* No toc references were found in ISEC. If the code in ISEC makes no
12226 calls, then there's no need to use toc adjusting stubs when branching
12227 into ISEC. Actually, indirect calls from ISEC are OK as they will
12228 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12229 needed, and 2 if a cyclical call-graph was found but no other reason
12230 for a stub was detected. If called from the top level, a return of
12231 2 means the same as a return of 0. */
12234 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12238 /* Mark this section as checked. */
12239 isec->call_check_done = 1;
12241 /* We know none of our code bearing sections will need toc stubs. */
12242 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12245 if (isec->size == 0)
12248 if (isec->output_section == NULL)
12252 if (isec->reloc_count != 0)
12254 Elf_Internal_Rela *relstart, *rel;
12255 Elf_Internal_Sym *local_syms;
12256 struct ppc_link_hash_table *htab;
12258 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12259 info->keep_memory);
12260 if (relstart == NULL)
12263 /* Look for branches to outside of this section. */
12265 htab = ppc_hash_table (info);
12269 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12271 enum elf_ppc64_reloc_type r_type;
12272 unsigned long r_symndx;
12273 struct elf_link_hash_entry *h;
12274 struct ppc_link_hash_entry *eh;
12275 Elf_Internal_Sym *sym;
12277 struct _opd_sec_data *opd;
12281 r_type = ELF64_R_TYPE (rel->r_info);
12282 if (r_type != R_PPC64_REL24
12283 && r_type != R_PPC64_REL24_NOTOC
12284 && r_type != R_PPC64_REL14
12285 && r_type != R_PPC64_REL14_BRTAKEN
12286 && r_type != R_PPC64_REL14_BRNTAKEN
12287 && r_type != R_PPC64_PLTCALL
12288 && r_type != R_PPC64_PLTCALL_NOTOC)
12291 r_symndx = ELF64_R_SYM (rel->r_info);
12292 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12299 /* Calls to dynamic lib functions go through a plt call stub
12301 eh = (struct ppc_link_hash_entry *) h;
12303 && (eh->elf.plt.plist != NULL
12305 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12311 if (sym_sec == NULL)
12312 /* Ignore other undefined symbols. */
12315 /* Assume branches to other sections not included in the
12316 link need stubs too, to cover -R and absolute syms. */
12317 if (sym_sec->output_section == NULL)
12324 sym_value = sym->st_value;
12327 if (h->root.type != bfd_link_hash_defined
12328 && h->root.type != bfd_link_hash_defweak)
12330 sym_value = h->root.u.def.value;
12332 sym_value += rel->r_addend;
12334 /* If this branch reloc uses an opd sym, find the code section. */
12335 opd = get_opd_info (sym_sec);
12338 if (h == NULL && opd->adjust != NULL)
12342 adjust = opd->adjust[OPD_NDX (sym_value)];
12344 /* Assume deleted functions won't ever be called. */
12346 sym_value += adjust;
12349 dest = opd_entry_value (sym_sec, sym_value,
12350 &sym_sec, NULL, FALSE);
12351 if (dest == (bfd_vma) -1)
12356 + sym_sec->output_offset
12357 + sym_sec->output_section->vma);
12359 /* Ignore branch to self. */
12360 if (sym_sec == isec)
12363 /* If the called function uses the toc, we need a stub. */
12364 if (sym_sec->has_toc_reloc
12365 || sym_sec->makes_toc_func_call)
12371 /* Assume any branch that needs a long branch stub might in fact
12372 need a plt_branch stub. A plt_branch stub uses r2. */
12373 else if (dest - (isec->output_offset
12374 + isec->output_section->vma
12375 + rel->r_offset) + (1 << 25)
12376 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12384 /* If calling back to a section in the process of being
12385 tested, we can't say for sure that no toc adjusting stubs
12386 are needed, so don't return zero. */
12387 else if (sym_sec->call_check_in_progress)
12390 /* Branches to another section that itself doesn't have any TOC
12391 references are OK. Recursively call ourselves to check. */
12392 else if (!sym_sec->call_check_done)
12396 /* Mark current section as indeterminate, so that other
12397 sections that call back to current won't be marked as
12399 isec->call_check_in_progress = 1;
12400 recur = toc_adjusting_stub_needed (info, sym_sec);
12401 isec->call_check_in_progress = 0;
12412 if (local_syms != NULL
12413 && (elf_symtab_hdr (isec->owner).contents
12414 != (unsigned char *) local_syms))
12416 if (elf_section_data (isec)->relocs != relstart)
12421 && isec->map_head.s != NULL
12422 && (strcmp (isec->output_section->name, ".init") == 0
12423 || strcmp (isec->output_section->name, ".fini") == 0))
12425 if (isec->map_head.s->has_toc_reloc
12426 || isec->map_head.s->makes_toc_func_call)
12428 else if (!isec->map_head.s->call_check_done)
12431 isec->call_check_in_progress = 1;
12432 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12433 isec->call_check_in_progress = 0;
12440 isec->makes_toc_func_call = 1;
12445 /* The linker repeatedly calls this function for each input section,
12446 in the order that input sections are linked into output sections.
12447 Build lists of input sections to determine groupings between which
12448 we may insert linker stubs. */
12451 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12453 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12458 if ((isec->output_section->flags & SEC_CODE) != 0
12459 && isec->output_section->id < htab->sec_info_arr_size)
12461 /* This happens to make the list in reverse order,
12462 which is what we want. */
12463 htab->sec_info[isec->id].u.list
12464 = htab->sec_info[isec->output_section->id].u.list;
12465 htab->sec_info[isec->output_section->id].u.list = isec;
12468 if (htab->multi_toc_needed)
12470 /* Analyse sections that aren't already flagged as needing a
12471 valid toc pointer. Exclude .fixup for the linux kernel.
12472 .fixup contains branches, but only back to the function that
12473 hit an exception. */
12474 if (!(isec->has_toc_reloc
12475 || (isec->flags & SEC_CODE) == 0
12476 || strcmp (isec->name, ".fixup") == 0
12477 || isec->call_check_done))
12479 if (toc_adjusting_stub_needed (info, isec) < 0)
12482 /* Make all sections use the TOC assigned for this object file.
12483 This will be wrong for pasted sections; We fix that in
12484 check_pasted_section(). */
12485 if (elf_gp (isec->owner) != 0)
12486 htab->toc_curr = elf_gp (isec->owner);
12489 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12493 /* Check that all .init and .fini sections use the same toc, if they
12494 have toc relocs. */
12497 check_pasted_section (struct bfd_link_info *info, const char *name)
12499 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12503 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12504 bfd_vma toc_off = 0;
12507 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12508 if (i->has_toc_reloc)
12511 toc_off = htab->sec_info[i->id].toc_off;
12512 else if (toc_off != htab->sec_info[i->id].toc_off)
12517 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12518 if (i->makes_toc_func_call)
12520 toc_off = htab->sec_info[i->id].toc_off;
12524 /* Make sure the whole pasted function uses the same toc offset. */
12526 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12527 htab->sec_info[i->id].toc_off = toc_off;
12533 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12535 return (check_pasted_section (info, ".init")
12536 & check_pasted_section (info, ".fini"));
12539 /* See whether we can group stub sections together. Grouping stub
12540 sections may result in fewer stubs. More importantly, we need to
12541 put all .init* and .fini* stubs at the beginning of the .init or
12542 .fini output sections respectively, because glibc splits the
12543 _init and _fini functions into multiple parts. Putting a stub in
12544 the middle of a function is not a good idea. */
12547 group_sections (struct bfd_link_info *info,
12548 bfd_size_type stub_group_size,
12549 bfd_boolean stubs_always_before_branch)
12551 struct ppc_link_hash_table *htab;
12553 bfd_boolean suppress_size_errors;
12555 htab = ppc_hash_table (info);
12559 suppress_size_errors = FALSE;
12560 if (stub_group_size == 1)
12562 /* Default values. */
12563 if (stubs_always_before_branch)
12564 stub_group_size = 0x1e00000;
12566 stub_group_size = 0x1c00000;
12567 suppress_size_errors = TRUE;
12570 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12574 if (osec->id >= htab->sec_info_arr_size)
12577 tail = htab->sec_info[osec->id].u.list;
12578 while (tail != NULL)
12582 bfd_size_type total;
12583 bfd_boolean big_sec;
12585 struct map_stub *group;
12586 bfd_size_type group_size;
12589 total = tail->size;
12590 group_size = (ppc64_elf_section_data (tail) != NULL
12591 && ppc64_elf_section_data (tail)->has_14bit_branch
12592 ? stub_group_size >> 10 : stub_group_size);
12594 big_sec = total > group_size;
12595 if (big_sec && !suppress_size_errors)
12596 /* xgettext:c-format */
12597 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12598 tail->owner, tail);
12599 curr_toc = htab->sec_info[tail->id].toc_off;
12601 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12602 && ((total += curr->output_offset - prev->output_offset)
12603 < (ppc64_elf_section_data (prev) != NULL
12604 && ppc64_elf_section_data (prev)->has_14bit_branch
12605 ? (group_size = stub_group_size >> 10) : group_size))
12606 && htab->sec_info[prev->id].toc_off == curr_toc)
12609 /* OK, the size from the start of CURR to the end is less
12610 than group_size and thus can be handled by one stub
12611 section. (or the tail section is itself larger than
12612 group_size, in which case we may be toast.) We should
12613 really be keeping track of the total size of stubs added
12614 here, as stubs contribute to the final output section
12615 size. That's a little tricky, and this way will only
12616 break if stubs added make the total size more than 2^25,
12617 ie. for the default stub_group_size, if stubs total more
12618 than 2097152 bytes, or nearly 75000 plt call stubs. */
12619 group = bfd_alloc (curr->owner, sizeof (*group));
12622 group->link_sec = curr;
12623 group->stub_sec = NULL;
12624 group->needs_save_res = 0;
12625 group->lr_restore = 0;
12626 group->eh_size = 0;
12627 group->eh_base = 0;
12628 group->next = htab->group;
12629 htab->group = group;
12632 prev = htab->sec_info[tail->id].u.list;
12633 /* Set up this stub group. */
12634 htab->sec_info[tail->id].u.group = group;
12636 while (tail != curr && (tail = prev) != NULL);
12638 /* But wait, there's more! Input sections up to group_size
12639 bytes before the stub section can be handled by it too.
12640 Don't do this if we have a really large section after the
12641 stubs, as adding more stubs increases the chance that
12642 branches may not reach into the stub section. */
12643 if (!stubs_always_before_branch && !big_sec)
12646 while (prev != NULL
12647 && ((total += tail->output_offset - prev->output_offset)
12648 < (ppc64_elf_section_data (prev) != NULL
12649 && ppc64_elf_section_data (prev)->has_14bit_branch
12650 ? (group_size = stub_group_size >> 10)
12652 && htab->sec_info[prev->id].toc_off == curr_toc)
12655 prev = htab->sec_info[tail->id].u.list;
12656 htab->sec_info[tail->id].u.group = group;
12665 static const unsigned char glink_eh_frame_cie[] =
12667 0, 0, 0, 16, /* length. */
12668 0, 0, 0, 0, /* id. */
12669 1, /* CIE version. */
12670 'z', 'R', 0, /* Augmentation string. */
12671 4, /* Code alignment. */
12672 0x78, /* Data alignment. */
12674 1, /* Augmentation size. */
12675 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12676 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
12679 /* Stripping output sections is normally done before dynamic section
12680 symbols have been allocated. This function is called later, and
12681 handles cases like htab->brlt which is mapped to its own output
12685 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12687 if (isec->size == 0
12688 && isec->output_section->size == 0
12689 && !(isec->output_section->flags & SEC_KEEP)
12690 && !bfd_section_removed_from_list (info->output_bfd,
12691 isec->output_section)
12692 && elf_section_data (isec->output_section)->dynindx == 0)
12694 isec->output_section->flags |= SEC_EXCLUDE;
12695 bfd_section_list_remove (info->output_bfd, isec->output_section);
12696 info->output_bfd->section_count--;
12700 /* Determine and set the size of the stub section for a final link.
12702 The basic idea here is to examine all the relocations looking for
12703 PC-relative calls to a target that is unreachable with a "bl"
12707 ppc64_elf_size_stubs (struct bfd_link_info *info)
12709 bfd_size_type stub_group_size;
12710 bfd_boolean stubs_always_before_branch;
12711 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12716 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12717 htab->params->plt_thread_safe = 1;
12718 if (!htab->opd_abi)
12719 htab->params->plt_thread_safe = 0;
12720 else if (htab->params->plt_thread_safe == -1)
12722 static const char *const thread_starter[] =
12726 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12728 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12729 "mq_notify", "create_timer",
12734 "GOMP_parallel_start",
12735 "GOMP_parallel_loop_static",
12736 "GOMP_parallel_loop_static_start",
12737 "GOMP_parallel_loop_dynamic",
12738 "GOMP_parallel_loop_dynamic_start",
12739 "GOMP_parallel_loop_guided",
12740 "GOMP_parallel_loop_guided_start",
12741 "GOMP_parallel_loop_runtime",
12742 "GOMP_parallel_loop_runtime_start",
12743 "GOMP_parallel_sections",
12744 "GOMP_parallel_sections_start",
12750 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12752 struct elf_link_hash_entry *h;
12753 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12754 FALSE, FALSE, TRUE);
12755 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12756 if (htab->params->plt_thread_safe)
12760 stubs_always_before_branch = htab->params->group_size < 0;
12761 if (htab->params->group_size < 0)
12762 stub_group_size = -htab->params->group_size;
12764 stub_group_size = htab->params->group_size;
12766 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12769 #define STUB_SHRINK_ITER 20
12770 /* Loop until no stubs added. After iteration 20 of this loop we may
12771 exit on a stub section shrinking. This is to break out of a
12772 pathological case where adding stubs on one iteration decreases
12773 section gaps (perhaps due to alignment), which then requires
12774 fewer or smaller stubs on the next iteration. */
12779 unsigned int bfd_indx;
12780 struct map_stub *group;
12782 htab->stub_iteration += 1;
12784 for (input_bfd = info->input_bfds, bfd_indx = 0;
12786 input_bfd = input_bfd->link.next, bfd_indx++)
12788 Elf_Internal_Shdr *symtab_hdr;
12790 Elf_Internal_Sym *local_syms = NULL;
12792 if (!is_ppc64_elf (input_bfd))
12795 /* We'll need the symbol table in a second. */
12796 symtab_hdr = &elf_symtab_hdr (input_bfd);
12797 if (symtab_hdr->sh_info == 0)
12800 /* Walk over each section attached to the input bfd. */
12801 for (section = input_bfd->sections;
12803 section = section->next)
12805 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12807 /* If there aren't any relocs, then there's nothing more
12809 if ((section->flags & SEC_RELOC) == 0
12810 || (section->flags & SEC_ALLOC) == 0
12811 || (section->flags & SEC_LOAD) == 0
12812 || (section->flags & SEC_CODE) == 0
12813 || section->reloc_count == 0)
12816 /* If this section is a link-once section that will be
12817 discarded, then don't create any stubs. */
12818 if (section->output_section == NULL
12819 || section->output_section->owner != info->output_bfd)
12822 /* Get the relocs. */
12824 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12825 info->keep_memory);
12826 if (internal_relocs == NULL)
12827 goto error_ret_free_local;
12829 /* Now examine each relocation. */
12830 irela = internal_relocs;
12831 irelaend = irela + section->reloc_count;
12832 for (; irela < irelaend; irela++)
12834 enum elf_ppc64_reloc_type r_type;
12835 unsigned int r_indx;
12836 enum ppc_stub_type stub_type;
12837 struct ppc_stub_hash_entry *stub_entry;
12838 asection *sym_sec, *code_sec;
12839 bfd_vma sym_value, code_value;
12840 bfd_vma destination;
12841 unsigned long local_off;
12842 bfd_boolean ok_dest;
12843 struct ppc_link_hash_entry *hash;
12844 struct ppc_link_hash_entry *fdh;
12845 struct elf_link_hash_entry *h;
12846 Elf_Internal_Sym *sym;
12848 const asection *id_sec;
12849 struct _opd_sec_data *opd;
12850 struct plt_entry *plt_ent;
12852 r_type = ELF64_R_TYPE (irela->r_info);
12853 r_indx = ELF64_R_SYM (irela->r_info);
12855 if (r_type >= R_PPC64_max)
12857 bfd_set_error (bfd_error_bad_value);
12858 goto error_ret_free_internal;
12861 /* Only look for stubs on branch instructions. */
12862 if (r_type != R_PPC64_REL24
12863 && r_type != R_PPC64_REL24_NOTOC
12864 && r_type != R_PPC64_REL14
12865 && r_type != R_PPC64_REL14_BRTAKEN
12866 && r_type != R_PPC64_REL14_BRNTAKEN)
12869 /* Now determine the call target, its name, value,
12871 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12872 r_indx, input_bfd))
12873 goto error_ret_free_internal;
12874 hash = (struct ppc_link_hash_entry *) h;
12881 sym_value = sym->st_value;
12882 if (sym_sec != NULL
12883 && sym_sec->output_section != NULL)
12886 else if (hash->elf.root.type == bfd_link_hash_defined
12887 || hash->elf.root.type == bfd_link_hash_defweak)
12889 sym_value = hash->elf.root.u.def.value;
12890 if (sym_sec->output_section != NULL)
12893 else if (hash->elf.root.type == bfd_link_hash_undefweak
12894 || hash->elf.root.type == bfd_link_hash_undefined)
12896 /* Recognise an old ABI func code entry sym, and
12897 use the func descriptor sym instead if it is
12899 if (hash->elf.root.root.string[0] == '.'
12900 && hash->oh != NULL)
12902 fdh = ppc_follow_link (hash->oh);
12903 if (fdh->elf.root.type == bfd_link_hash_defined
12904 || fdh->elf.root.type == bfd_link_hash_defweak)
12906 sym_sec = fdh->elf.root.u.def.section;
12907 sym_value = fdh->elf.root.u.def.value;
12908 if (sym_sec->output_section != NULL)
12917 bfd_set_error (bfd_error_bad_value);
12918 goto error_ret_free_internal;
12925 sym_value += irela->r_addend;
12926 destination = (sym_value
12927 + sym_sec->output_offset
12928 + sym_sec->output_section->vma);
12929 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12934 code_sec = sym_sec;
12935 code_value = sym_value;
12936 opd = get_opd_info (sym_sec);
12941 if (hash == NULL && opd->adjust != NULL)
12943 long adjust = opd->adjust[OPD_NDX (sym_value)];
12946 code_value += adjust;
12947 sym_value += adjust;
12949 dest = opd_entry_value (sym_sec, sym_value,
12950 &code_sec, &code_value, FALSE);
12951 if (dest != (bfd_vma) -1)
12953 destination = dest;
12956 /* Fixup old ABI sym to point at code
12958 hash->elf.root.type = bfd_link_hash_defweak;
12959 hash->elf.root.u.def.section = code_sec;
12960 hash->elf.root.u.def.value = code_value;
12965 /* Determine what (if any) linker stub is needed. */
12967 stub_type = ppc_type_of_stub (section, irela, &hash,
12968 &plt_ent, destination,
12971 if (r_type == R_PPC64_REL24_NOTOC)
12973 if (stub_type == ppc_stub_plt_call)
12974 stub_type = ppc_stub_plt_call_notoc;
12975 else if (stub_type == ppc_stub_long_branch
12976 || (code_sec != NULL
12977 && code_sec->output_section != NULL
12978 && (((hash ? hash->elf.other : sym->st_other)
12979 & STO_PPC64_LOCAL_MASK)
12980 > 1 << STO_PPC64_LOCAL_BIT)))
12981 stub_type = ppc_stub_long_branch_notoc;
12983 else if (stub_type != ppc_stub_plt_call)
12985 /* Check whether we need a TOC adjusting stub.
12986 Since the linker pastes together pieces from
12987 different object files when creating the
12988 _init and _fini functions, it may be that a
12989 call to what looks like a local sym is in
12990 fact a call needing a TOC adjustment. */
12991 if ((code_sec != NULL
12992 && code_sec->output_section != NULL
12993 && (htab->sec_info[code_sec->id].toc_off
12994 != htab->sec_info[section->id].toc_off)
12995 && (code_sec->has_toc_reloc
12996 || code_sec->makes_toc_func_call))
12997 || (((hash ? hash->elf.other : sym->st_other)
12998 & STO_PPC64_LOCAL_MASK)
12999 == 1 << STO_PPC64_LOCAL_BIT))
13000 stub_type = ppc_stub_long_branch_r2off;
13003 if (stub_type == ppc_stub_none)
13006 /* __tls_get_addr calls might be eliminated. */
13007 if (stub_type != ppc_stub_plt_call
13008 && stub_type != ppc_stub_plt_call_notoc
13010 && (hash == htab->tls_get_addr
13011 || hash == htab->tls_get_addr_fd)
13012 && section->has_tls_reloc
13013 && irela != internal_relocs)
13015 /* Get tls info. */
13016 unsigned char *tls_mask;
13018 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
13019 irela - 1, input_bfd))
13020 goto error_ret_free_internal;
13021 if ((*tls_mask & TLS_TLS) != 0)
13025 if (stub_type == ppc_stub_plt_call)
13028 && htab->params->plt_localentry0 != 0
13029 && is_elfv2_localentry0 (&hash->elf))
13030 htab->has_plt_localentry0 = 1;
13031 else if (irela + 1 < irelaend
13032 && irela[1].r_offset == irela->r_offset + 4
13033 && (ELF64_R_TYPE (irela[1].r_info)
13034 == R_PPC64_TOCSAVE))
13036 if (!tocsave_find (htab, INSERT,
13037 &local_syms, irela + 1, input_bfd))
13038 goto error_ret_free_internal;
13041 stub_type = ppc_stub_plt_call_r2save;
13044 /* Support for grouping stub sections. */
13045 id_sec = htab->sec_info[section->id].u.group->link_sec;
13047 /* Get the name of this stub. */
13048 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13050 goto error_ret_free_internal;
13052 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
13053 stub_name, FALSE, FALSE);
13054 if (stub_entry != NULL)
13056 enum ppc_stub_type old_type;
13057 /* A stub has already been created, but it may
13058 not be the required type. We shouldn't be
13059 transitioning from plt_call to long_branch
13060 stubs or vice versa, but we might be
13061 upgrading from plt_call to plt_call_r2save or
13062 from long_branch to long_branch_r2off. */
13064 old_type = stub_entry->stub_type;
13070 case ppc_stub_save_res:
13073 case ppc_stub_plt_call:
13074 case ppc_stub_plt_call_r2save:
13075 case ppc_stub_plt_call_notoc:
13076 case ppc_stub_plt_call_both:
13077 if (stub_type == ppc_stub_plt_call)
13079 else if (stub_type == ppc_stub_plt_call_r2save)
13081 if (old_type == ppc_stub_plt_call_notoc)
13082 stub_type = ppc_stub_plt_call_both;
13084 else if (stub_type == ppc_stub_plt_call_notoc)
13086 if (old_type == ppc_stub_plt_call_r2save)
13087 stub_type = ppc_stub_plt_call_both;
13093 case ppc_stub_plt_branch:
13094 case ppc_stub_plt_branch_r2off:
13095 case ppc_stub_plt_branch_notoc:
13096 case ppc_stub_plt_branch_both:
13097 old_type += (ppc_stub_long_branch
13098 - ppc_stub_plt_branch);
13099 /* Fall through. */
13100 case ppc_stub_long_branch:
13101 case ppc_stub_long_branch_r2off:
13102 case ppc_stub_long_branch_notoc:
13103 case ppc_stub_long_branch_both:
13104 if (stub_type == ppc_stub_long_branch)
13106 else if (stub_type == ppc_stub_long_branch_r2off)
13108 if (old_type == ppc_stub_long_branch_notoc)
13109 stub_type = ppc_stub_long_branch_both;
13111 else if (stub_type == ppc_stub_long_branch_notoc)
13113 if (old_type == ppc_stub_long_branch_r2off)
13114 stub_type = ppc_stub_long_branch_both;
13120 if (old_type < stub_type)
13121 stub_entry->stub_type = stub_type;
13125 stub_entry = ppc_add_stub (stub_name, section, info);
13126 if (stub_entry == NULL)
13129 error_ret_free_internal:
13130 if (elf_section_data (section)->relocs == NULL)
13131 free (internal_relocs);
13132 error_ret_free_local:
13133 if (local_syms != NULL
13134 && (symtab_hdr->contents
13135 != (unsigned char *) local_syms))
13140 stub_entry->stub_type = stub_type;
13141 if (stub_type >= ppc_stub_plt_call
13142 && stub_type <= ppc_stub_plt_call_both)
13144 stub_entry->target_value = sym_value;
13145 stub_entry->target_section = sym_sec;
13149 stub_entry->target_value = code_value;
13150 stub_entry->target_section = code_sec;
13152 stub_entry->h = hash;
13153 stub_entry->plt_ent = plt_ent;
13154 stub_entry->symtype
13155 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
13156 stub_entry->other = hash ? hash->elf.other : sym->st_other;
13159 && (hash->elf.root.type == bfd_link_hash_defined
13160 || hash->elf.root.type == bfd_link_hash_defweak))
13161 htab->stub_globals += 1;
13164 /* We're done with the internal relocs, free them. */
13165 if (elf_section_data (section)->relocs != internal_relocs)
13166 free (internal_relocs);
13169 if (local_syms != NULL
13170 && symtab_hdr->contents != (unsigned char *) local_syms)
13172 if (!info->keep_memory)
13175 symtab_hdr->contents = (unsigned char *) local_syms;
13179 /* We may have added some stubs. Find out the new size of the
13181 for (group = htab->group; group != NULL; group = group->next)
13183 group->lr_restore = 0;
13184 group->eh_size = 0;
13185 if (group->stub_sec != NULL)
13187 asection *stub_sec = group->stub_sec;
13189 if (htab->stub_iteration <= STUB_SHRINK_ITER
13190 || stub_sec->rawsize < stub_sec->size)
13191 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13192 stub_sec->rawsize = stub_sec->size;
13193 stub_sec->size = 0;
13194 stub_sec->reloc_count = 0;
13195 stub_sec->flags &= ~SEC_RELOC;
13199 if (htab->stub_iteration <= STUB_SHRINK_ITER
13200 || htab->brlt->rawsize < htab->brlt->size)
13201 htab->brlt->rawsize = htab->brlt->size;
13202 htab->brlt->size = 0;
13203 htab->brlt->reloc_count = 0;
13204 htab->brlt->flags &= ~SEC_RELOC;
13205 if (htab->relbrlt != NULL)
13206 htab->relbrlt->size = 0;
13208 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
13210 for (group = htab->group; group != NULL; group = group->next)
13211 if (group->needs_save_res)
13212 group->stub_sec->size += htab->sfpr->size;
13214 if (info->emitrelocations
13215 && htab->glink != NULL && htab->glink->size != 0)
13217 htab->glink->reloc_count = 1;
13218 htab->glink->flags |= SEC_RELOC;
13221 if (htab->glink_eh_frame != NULL
13222 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
13223 && htab->glink_eh_frame->output_section->size > 8)
13225 size_t size = 0, align = 4;
13227 for (group = htab->group; group != NULL; group = group->next)
13228 if (group->eh_size != 0)
13229 size += (group->eh_size + 17 + align - 1) & -align;
13230 if (htab->glink != NULL && htab->glink->size != 0)
13231 size += (24 + align - 1) & -align;
13233 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13234 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13235 size = (size + align - 1) & -align;
13236 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13237 htab->glink_eh_frame->size = size;
13240 if (htab->params->plt_stub_align != 0)
13241 for (group = htab->group; group != NULL; group = group->next)
13242 if (group->stub_sec != NULL)
13244 int align = abs (htab->params->plt_stub_align);
13245 group->stub_sec->size
13246 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13249 for (group = htab->group; group != NULL; group = group->next)
13250 if (group->stub_sec != NULL
13251 && group->stub_sec->rawsize != group->stub_sec->size
13252 && (htab->stub_iteration <= STUB_SHRINK_ITER
13253 || group->stub_sec->rawsize < group->stub_sec->size))
13257 && (htab->brlt->rawsize == htab->brlt->size
13258 || (htab->stub_iteration > STUB_SHRINK_ITER
13259 && htab->brlt->rawsize > htab->brlt->size))
13260 && (htab->glink_eh_frame == NULL
13261 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
13264 /* Ask the linker to do its stuff. */
13265 (*htab->params->layout_sections_again) ();
13268 if (htab->glink_eh_frame != NULL
13269 && htab->glink_eh_frame->size != 0)
13272 bfd_byte *p, *last_fde;
13273 size_t last_fde_len, size, align, pad;
13274 struct map_stub *group;
13276 /* It is necessary to at least have a rough outline of the
13277 linker generated CIEs and FDEs written before
13278 bfd_elf_discard_info is run, in order for these FDEs to be
13279 indexed in .eh_frame_hdr. */
13280 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13283 htab->glink_eh_frame->contents = p;
13287 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13288 /* CIE length (rewrite in case little-endian). */
13289 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
13290 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13291 p += last_fde_len + 4;
13293 for (group = htab->group; group != NULL; group = group->next)
13294 if (group->eh_size != 0)
13296 group->eh_base = p - htab->glink_eh_frame->contents;
13298 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
13300 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13303 val = p - htab->glink_eh_frame->contents;
13304 bfd_put_32 (htab->elf.dynobj, val, p);
13306 /* Offset to stub section, written later. */
13308 /* stub section size. */
13309 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
13311 /* Augmentation. */
13313 /* Make sure we don't have all nops. This is enough for
13314 elf-eh-frame.c to detect the last non-nop opcode. */
13315 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
13316 p = last_fde + last_fde_len + 4;
13318 if (htab->glink != NULL && htab->glink->size != 0)
13321 last_fde_len = ((24 + align - 1) & -align) - 4;
13323 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13326 val = p - htab->glink_eh_frame->contents;
13327 bfd_put_32 (htab->elf.dynobj, val, p);
13329 /* Offset to .glink, written later. */
13332 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13334 /* Augmentation. */
13337 *p++ = DW_CFA_advance_loc + 1;
13338 *p++ = DW_CFA_register;
13340 *p++ = htab->opd_abi ? 12 : 0;
13341 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
13342 *p++ = DW_CFA_restore_extended;
13344 p += ((24 + align - 1) & -align) - 24;
13346 /* Subsume any padding into the last FDE if user .eh_frame
13347 sections are aligned more than glink_eh_frame. Otherwise any
13348 zero padding will be seen as a terminator. */
13349 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13350 size = p - htab->glink_eh_frame->contents;
13351 pad = ((size + align - 1) & -align) - size;
13352 htab->glink_eh_frame->size = size + pad;
13353 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13356 maybe_strip_output (info, htab->brlt);
13357 if (htab->glink_eh_frame != NULL)
13358 maybe_strip_output (info, htab->glink_eh_frame);
13363 /* Called after we have determined section placement. If sections
13364 move, we'll be called again. Provide a value for TOCstart. */
13367 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13370 bfd_vma TOCstart, adjust;
13374 struct elf_link_hash_entry *h;
13375 struct elf_link_hash_table *htab = elf_hash_table (info);
13377 if (is_elf_hash_table (htab)
13378 && htab->hgot != NULL)
13382 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13383 if (is_elf_hash_table (htab))
13387 && h->root.type == bfd_link_hash_defined
13388 && !h->root.linker_def
13389 && (!is_elf_hash_table (htab)
13390 || h->def_regular))
13392 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13393 + h->root.u.def.section->output_offset
13394 + h->root.u.def.section->output_section->vma);
13395 _bfd_set_gp_value (obfd, TOCstart);
13400 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13401 order. The TOC starts where the first of these sections starts. */
13402 s = bfd_get_section_by_name (obfd, ".got");
13403 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13404 s = bfd_get_section_by_name (obfd, ".toc");
13405 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13406 s = bfd_get_section_by_name (obfd, ".tocbss");
13407 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13408 s = bfd_get_section_by_name (obfd, ".plt");
13409 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13411 /* This may happen for
13412 o references to TOC base (SYM@toc / TOC[tc0]) without a
13414 o bad linker script
13415 o --gc-sections and empty TOC sections
13417 FIXME: Warn user? */
13419 /* Look for a likely section. We probably won't even be
13421 for (s = obfd->sections; s != NULL; s = s->next)
13422 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13424 == (SEC_ALLOC | SEC_SMALL_DATA))
13427 for (s = obfd->sections; s != NULL; s = s->next)
13428 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13429 == (SEC_ALLOC | SEC_SMALL_DATA))
13432 for (s = obfd->sections; s != NULL; s = s->next)
13433 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13437 for (s = obfd->sections; s != NULL; s = s->next)
13438 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13444 TOCstart = s->output_section->vma + s->output_offset;
13446 /* Force alignment. */
13447 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13448 TOCstart -= adjust;
13449 _bfd_set_gp_value (obfd, TOCstart);
13451 if (info != NULL && s != NULL)
13453 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13457 if (htab->elf.hgot != NULL)
13459 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13460 htab->elf.hgot->root.u.def.section = s;
13465 struct bfd_link_hash_entry *bh = NULL;
13466 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13467 s, TOC_BASE_OFF - adjust,
13468 NULL, FALSE, FALSE, &bh);
13474 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13475 write out any global entry stubs, and PLT relocations. */
13478 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13480 struct bfd_link_info *info;
13481 struct ppc_link_hash_table *htab;
13482 struct plt_entry *ent;
13485 if (h->root.type == bfd_link_hash_indirect)
13489 htab = ppc_hash_table (info);
13493 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13494 if (ent->plt.offset != (bfd_vma) -1)
13496 /* This symbol has an entry in the procedure linkage
13497 table. Set it up. */
13498 Elf_Internal_Rela rela;
13499 asection *plt, *relplt;
13502 if (!htab->elf.dynamic_sections_created
13503 || h->dynindx == -1)
13505 if (!(h->def_regular
13506 && (h->root.type == bfd_link_hash_defined
13507 || h->root.type == bfd_link_hash_defweak)))
13509 if (h->type == STT_GNU_IFUNC)
13511 plt = htab->elf.iplt;
13512 relplt = htab->elf.irelplt;
13513 htab->local_ifunc_resolver = 1;
13515 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13517 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13521 plt = htab->pltlocal;
13522 if (bfd_link_pic (info))
13524 relplt = htab->relpltlocal;
13526 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13528 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13533 rela.r_addend = (h->root.u.def.value
13534 + h->root.u.def.section->output_offset
13535 + h->root.u.def.section->output_section->vma
13538 if (relplt == NULL)
13540 loc = plt->contents + ent->plt.offset;
13541 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13544 bfd_vma toc = elf_gp (info->output_bfd);
13545 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13546 bfd_put_64 (info->output_bfd, toc, loc + 8);
13551 rela.r_offset = (plt->output_section->vma
13552 + plt->output_offset
13553 + ent->plt.offset);
13554 loc = relplt->contents + (relplt->reloc_count++
13555 * sizeof (Elf64_External_Rela));
13556 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13561 rela.r_offset = (htab->elf.splt->output_section->vma
13562 + htab->elf.splt->output_offset
13563 + ent->plt.offset);
13564 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13565 rela.r_addend = ent->addend;
13566 loc = (htab->elf.srelplt->contents
13567 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13568 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13569 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13570 htab->maybe_local_ifunc_resolver = 1;
13571 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13575 if (!h->pointer_equality_needed)
13578 if (h->def_regular)
13581 s = htab->global_entry;
13582 if (s == NULL || s->size == 0)
13585 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13586 if (ent->plt.offset != (bfd_vma) -1
13587 && ent->addend == 0)
13593 p = s->contents + h->root.u.def.value;
13594 plt = htab->elf.splt;
13595 if (!htab->elf.dynamic_sections_created
13596 || h->dynindx == -1)
13598 if (h->type == STT_GNU_IFUNC)
13599 plt = htab->elf.iplt;
13601 plt = htab->pltlocal;
13603 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13604 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13606 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13608 info->callbacks->einfo
13609 (_("%P: linkage table error against `%pT'\n"),
13610 h->root.root.string);
13611 bfd_set_error (bfd_error_bad_value);
13612 htab->stub_error = TRUE;
13615 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13616 if (htab->params->emit_stub_syms)
13618 size_t len = strlen (h->root.root.string);
13619 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13624 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13625 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13628 if (h->root.type == bfd_link_hash_new)
13630 h->root.type = bfd_link_hash_defined;
13631 h->root.u.def.section = s;
13632 h->root.u.def.value = p - s->contents;
13633 h->ref_regular = 1;
13634 h->def_regular = 1;
13635 h->ref_regular_nonweak = 1;
13636 h->forced_local = 1;
13638 h->root.linker_def = 1;
13642 if (PPC_HA (off) != 0)
13644 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13647 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13649 bfd_put_32 (s->owner, MTCTR_R12, p);
13651 bfd_put_32 (s->owner, BCTR, p);
13657 /* Write PLT relocs for locals. */
13660 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13662 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13665 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13667 struct got_entry **lgot_ents, **end_lgot_ents;
13668 struct plt_entry **local_plt, **lplt, **end_local_plt;
13669 Elf_Internal_Shdr *symtab_hdr;
13670 bfd_size_type locsymcount;
13671 Elf_Internal_Sym *local_syms = NULL;
13672 struct plt_entry *ent;
13674 if (!is_ppc64_elf (ibfd))
13677 lgot_ents = elf_local_got_ents (ibfd);
13681 symtab_hdr = &elf_symtab_hdr (ibfd);
13682 locsymcount = symtab_hdr->sh_info;
13683 end_lgot_ents = lgot_ents + locsymcount;
13684 local_plt = (struct plt_entry **) end_lgot_ents;
13685 end_local_plt = local_plt + locsymcount;
13686 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13687 for (ent = *lplt; ent != NULL; ent = ent->next)
13688 if (ent->plt.offset != (bfd_vma) -1)
13690 Elf_Internal_Sym *sym;
13692 asection *plt, *relplt;
13696 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13697 lplt - local_plt, ibfd))
13699 if (local_syms != NULL
13700 && symtab_hdr->contents != (unsigned char *) local_syms)
13705 val = sym->st_value + ent->addend;
13706 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13707 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13708 if (sym_sec != NULL && sym_sec->output_section != NULL)
13709 val += sym_sec->output_offset + sym_sec->output_section->vma;
13711 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13713 htab->local_ifunc_resolver = 1;
13714 plt = htab->elf.iplt;
13715 relplt = htab->elf.irelplt;
13719 plt = htab->pltlocal;
13720 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13723 if (relplt == NULL)
13725 loc = plt->contents + ent->plt.offset;
13726 bfd_put_64 (info->output_bfd, val, loc);
13729 bfd_vma toc = elf_gp (ibfd);
13730 bfd_put_64 (info->output_bfd, toc, loc + 8);
13735 Elf_Internal_Rela rela;
13736 rela.r_offset = (ent->plt.offset
13737 + plt->output_offset
13738 + plt->output_section->vma);
13739 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13742 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13744 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13749 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13751 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13753 rela.r_addend = val;
13754 loc = relplt->contents + (relplt->reloc_count++
13755 * sizeof (Elf64_External_Rela));
13756 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13760 if (local_syms != NULL
13761 && symtab_hdr->contents != (unsigned char *) local_syms)
13763 if (!info->keep_memory)
13766 symtab_hdr->contents = (unsigned char *) local_syms;
13772 /* Build all the stubs associated with the current output file.
13773 The stubs are kept in a hash table attached to the main linker
13774 hash table. This function is called via gldelf64ppc_finish. */
13777 ppc64_elf_build_stubs (struct bfd_link_info *info,
13780 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13781 struct map_stub *group;
13782 asection *stub_sec;
13784 int stub_sec_count = 0;
13789 /* Allocate memory to hold the linker stubs. */
13790 for (group = htab->group; group != NULL; group = group->next)
13792 group->eh_size = 0;
13793 group->lr_restore = 0;
13794 if ((stub_sec = group->stub_sec) != NULL
13795 && stub_sec->size != 0)
13797 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13799 if (stub_sec->contents == NULL)
13801 stub_sec->size = 0;
13805 if (htab->glink != NULL && htab->glink->size != 0)
13810 /* Build the .glink plt call stub. */
13811 if (htab->params->emit_stub_syms)
13813 struct elf_link_hash_entry *h;
13814 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13815 TRUE, FALSE, FALSE);
13818 if (h->root.type == bfd_link_hash_new)
13820 h->root.type = bfd_link_hash_defined;
13821 h->root.u.def.section = htab->glink;
13822 h->root.u.def.value = 8;
13823 h->ref_regular = 1;
13824 h->def_regular = 1;
13825 h->ref_regular_nonweak = 1;
13826 h->forced_local = 1;
13828 h->root.linker_def = 1;
13831 plt0 = (htab->elf.splt->output_section->vma
13832 + htab->elf.splt->output_offset
13834 if (info->emitrelocations)
13836 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13839 r->r_offset = (htab->glink->output_offset
13840 + htab->glink->output_section->vma);
13841 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13842 r->r_addend = plt0;
13844 p = htab->glink->contents;
13845 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13846 bfd_put_64 (htab->glink->owner, plt0, p);
13850 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13852 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13854 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13856 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13858 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13860 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13862 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13864 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13866 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13868 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13873 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13875 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13877 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13879 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13881 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13883 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13885 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13887 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13889 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13891 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13893 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13895 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13897 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13900 bfd_put_32 (htab->glink->owner, BCTR, p);
13902 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13904 /* Build the .glink lazy link call stubs. */
13906 while (p < htab->glink->contents + htab->glink->size)
13912 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13917 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13919 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13924 bfd_put_32 (htab->glink->owner,
13925 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13931 /* Build .glink global entry stubs, and PLT relocs for globals. */
13932 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13934 if (!write_plt_relocs_for_local_syms (info))
13937 if (htab->brlt != NULL && htab->brlt->size != 0)
13939 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13941 if (htab->brlt->contents == NULL)
13944 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13946 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13947 htab->relbrlt->size);
13948 if (htab->relbrlt->contents == NULL)
13952 /* Build the stubs as directed by the stub hash table. */
13953 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13955 for (group = htab->group; group != NULL; group = group->next)
13956 if (group->needs_save_res)
13957 group->stub_sec->size += htab->sfpr->size;
13959 if (htab->relbrlt != NULL)
13960 htab->relbrlt->reloc_count = 0;
13962 if (htab->params->plt_stub_align != 0)
13963 for (group = htab->group; group != NULL; group = group->next)
13964 if ((stub_sec = group->stub_sec) != NULL)
13966 int align = abs (htab->params->plt_stub_align);
13967 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13970 for (group = htab->group; group != NULL; group = group->next)
13971 if (group->needs_save_res)
13973 stub_sec = group->stub_sec;
13974 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13975 htab->sfpr->contents, htab->sfpr->size);
13976 if (htab->params->emit_stub_syms)
13980 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13981 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13986 if (htab->glink_eh_frame != NULL
13987 && htab->glink_eh_frame->size != 0)
13992 p = htab->glink_eh_frame->contents;
13993 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13995 for (group = htab->group; group != NULL; group = group->next)
13996 if (group->eh_size != 0)
13998 /* Offset to stub section. */
13999 val = (group->stub_sec->output_section->vma
14000 + group->stub_sec->output_offset);
14001 val -= (htab->glink_eh_frame->output_section->vma
14002 + htab->glink_eh_frame->output_offset
14003 + (p + 8 - htab->glink_eh_frame->contents));
14004 if (val + 0x80000000 > 0xffffffff)
14007 (_("%s offset too large for .eh_frame sdata4 encoding"),
14008 group->stub_sec->name);
14011 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14012 p += (group->eh_size + 17 + 3) & -4;
14014 if (htab->glink != NULL && htab->glink->size != 0)
14016 /* Offset to .glink. */
14017 val = (htab->glink->output_section->vma
14018 + htab->glink->output_offset
14020 val -= (htab->glink_eh_frame->output_section->vma
14021 + htab->glink_eh_frame->output_offset
14022 + (p + 8 - htab->glink_eh_frame->contents));
14023 if (val + 0x80000000 > 0xffffffff)
14026 (_("%s offset too large for .eh_frame sdata4 encoding"),
14027 htab->glink->name);
14030 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14031 p += (24 + align - 1) & -align;
14035 for (group = htab->group; group != NULL; group = group->next)
14036 if ((stub_sec = group->stub_sec) != NULL)
14038 stub_sec_count += 1;
14039 if (stub_sec->rawsize != stub_sec->size
14040 && (htab->stub_iteration <= STUB_SHRINK_ITER
14041 || stub_sec->rawsize < stub_sec->size))
14047 htab->stub_error = TRUE;
14048 _bfd_error_handler (_("stubs don't match calculated size"));
14051 if (htab->stub_error)
14057 *stats = bfd_malloc (500);
14058 if (*stats == NULL)
14061 len = sprintf (*stats,
14062 ngettext ("linker stubs in %u group\n",
14063 "linker stubs in %u groups\n",
14066 sprintf (*stats + len, _(" branch %lu\n"
14067 " branch toc adj %lu\n"
14068 " branch notoc %lu\n"
14069 " branch both %lu\n"
14070 " long branch %lu\n"
14071 " long toc adj %lu\n"
14072 " long notoc %lu\n"
14075 " plt call save %lu\n"
14076 " plt call notoc %lu\n"
14077 " plt call both %lu\n"
14078 " global entry %lu"),
14079 htab->stub_count[ppc_stub_long_branch - 1],
14080 htab->stub_count[ppc_stub_long_branch_r2off - 1],
14081 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14082 htab->stub_count[ppc_stub_long_branch_both - 1],
14083 htab->stub_count[ppc_stub_plt_branch - 1],
14084 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
14085 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14086 htab->stub_count[ppc_stub_plt_branch_both - 1],
14087 htab->stub_count[ppc_stub_plt_call - 1],
14088 htab->stub_count[ppc_stub_plt_call_r2save - 1],
14089 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14090 htab->stub_count[ppc_stub_plt_call_both - 1],
14091 htab->stub_count[ppc_stub_global_entry - 1]);
14096 /* What to do when ld finds relocations against symbols defined in
14097 discarded sections. */
14099 static unsigned int
14100 ppc64_elf_action_discarded (asection *sec)
14102 if (strcmp (".opd", sec->name) == 0)
14105 if (strcmp (".toc", sec->name) == 0)
14108 if (strcmp (".toc1", sec->name) == 0)
14111 return _bfd_elf_default_action_discarded (sec);
14114 /* The RELOCATE_SECTION function is called by the ELF backend linker
14115 to handle the relocations for a section.
14117 The relocs are always passed as Rela structures; if the section
14118 actually uses Rel structures, the r_addend field will always be
14121 This function is responsible for adjust the section contents as
14122 necessary, and (if using Rela relocs and generating a
14123 relocatable output file) adjusting the reloc addend as
14126 This function does not have to worry about setting the reloc
14127 address or the reloc symbol index.
14129 LOCAL_SYMS is a pointer to the swapped in local symbols.
14131 LOCAL_SECTIONS is an array giving the section in the input file
14132 corresponding to the st_shndx field of each local symbol.
14134 The global hash table entry for the global symbols can be found
14135 via elf_sym_hashes (input_bfd).
14137 When generating relocatable output, this function must handle
14138 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14139 going to be the section symbol corresponding to the output
14140 section, which means that the addend must be adjusted
14144 ppc64_elf_relocate_section (bfd *output_bfd,
14145 struct bfd_link_info *info,
14147 asection *input_section,
14148 bfd_byte *contents,
14149 Elf_Internal_Rela *relocs,
14150 Elf_Internal_Sym *local_syms,
14151 asection **local_sections)
14153 struct ppc_link_hash_table *htab;
14154 Elf_Internal_Shdr *symtab_hdr;
14155 struct elf_link_hash_entry **sym_hashes;
14156 Elf_Internal_Rela *rel;
14157 Elf_Internal_Rela *wrel;
14158 Elf_Internal_Rela *relend;
14159 Elf_Internal_Rela outrel;
14161 struct got_entry **local_got_ents;
14163 bfd_boolean ret = TRUE;
14164 bfd_boolean is_opd;
14165 /* Assume 'at' branch hints. */
14166 bfd_boolean is_isa_v2 = TRUE;
14167 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
14169 /* Initialize howto table if needed. */
14170 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
14173 htab = ppc_hash_table (info);
14177 /* Don't relocate stub sections. */
14178 if (input_section->owner == htab->params->stub_bfd)
14181 if (!is_ppc64_elf (input_bfd))
14183 bfd_set_error (bfd_error_wrong_format);
14187 local_got_ents = elf_local_got_ents (input_bfd);
14188 TOCstart = elf_gp (output_bfd);
14189 symtab_hdr = &elf_symtab_hdr (input_bfd);
14190 sym_hashes = elf_sym_hashes (input_bfd);
14191 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
14193 rel = wrel = relocs;
14194 relend = relocs + input_section->reloc_count;
14195 for (; rel < relend; wrel++, rel++)
14197 enum elf_ppc64_reloc_type r_type;
14199 bfd_reloc_status_type r;
14200 Elf_Internal_Sym *sym;
14202 struct elf_link_hash_entry *h_elf;
14203 struct ppc_link_hash_entry *h;
14204 struct ppc_link_hash_entry *fdh;
14205 const char *sym_name;
14206 unsigned long r_symndx, toc_symndx;
14207 bfd_vma toc_addend;
14208 unsigned char tls_mask, tls_gd, tls_type;
14209 unsigned char sym_type;
14210 bfd_vma relocation;
14211 bfd_boolean unresolved_reloc, save_unresolved_reloc;
14212 bfd_boolean warned;
14213 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
14216 struct ppc_stub_hash_entry *stub_entry;
14217 bfd_vma max_br_offset;
14219 Elf_Internal_Rela orig_rel;
14220 reloc_howto_type *howto;
14221 struct reloc_howto_struct alt_howto;
14228 r_type = ELF64_R_TYPE (rel->r_info);
14229 r_symndx = ELF64_R_SYM (rel->r_info);
14231 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14232 symbol of the previous ADDR64 reloc. The symbol gives us the
14233 proper TOC base to use. */
14234 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
14236 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
14238 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
14244 unresolved_reloc = FALSE;
14247 if (r_symndx < symtab_hdr->sh_info)
14249 /* It's a local symbol. */
14250 struct _opd_sec_data *opd;
14252 sym = local_syms + r_symndx;
14253 sec = local_sections[r_symndx];
14254 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
14255 sym_type = ELF64_ST_TYPE (sym->st_info);
14256 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
14257 opd = get_opd_info (sec);
14258 if (opd != NULL && opd->adjust != NULL)
14260 long adjust = opd->adjust[OPD_NDX (sym->st_value
14266 /* If this is a relocation against the opd section sym
14267 and we have edited .opd, adjust the reloc addend so
14268 that ld -r and ld --emit-relocs output is correct.
14269 If it is a reloc against some other .opd symbol,
14270 then the symbol value will be adjusted later. */
14271 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14272 rel->r_addend += adjust;
14274 relocation += adjust;
14280 bfd_boolean ignored;
14282 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14283 r_symndx, symtab_hdr, sym_hashes,
14284 h_elf, sec, relocation,
14285 unresolved_reloc, warned, ignored);
14286 sym_name = h_elf->root.root.string;
14287 sym_type = h_elf->type;
14289 && sec->owner == output_bfd
14290 && strcmp (sec->name, ".opd") == 0)
14292 /* This is a symbol defined in a linker script. All
14293 such are defined in output sections, even those
14294 defined by simple assignment from a symbol defined in
14295 an input section. Transfer the symbol to an
14296 appropriate input .opd section, so that a branch to
14297 this symbol will be mapped to the location specified
14298 by the opd entry. */
14299 struct bfd_link_order *lo;
14300 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14301 if (lo->type == bfd_indirect_link_order)
14303 asection *isec = lo->u.indirect.section;
14304 if (h_elf->root.u.def.value >= isec->output_offset
14305 && h_elf->root.u.def.value < (isec->output_offset
14308 h_elf->root.u.def.value -= isec->output_offset;
14309 h_elf->root.u.def.section = isec;
14316 h = (struct ppc_link_hash_entry *) h_elf;
14318 if (sec != NULL && discarded_section (sec))
14320 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14321 input_bfd, input_section,
14322 contents, rel->r_offset);
14323 wrel->r_offset = rel->r_offset;
14325 wrel->r_addend = 0;
14327 /* For ld -r, remove relocations in debug sections against
14328 symbols defined in discarded sections. Not done for
14329 non-debug to preserve relocs in .eh_frame which the
14330 eh_frame editing code expects to be present. */
14331 if (bfd_link_relocatable (info)
14332 && (input_section->flags & SEC_DEBUGGING))
14338 if (bfd_link_relocatable (info))
14341 if (h != NULL && &h->elf == htab->elf.hgot)
14343 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14344 sec = bfd_abs_section_ptr;
14345 unresolved_reloc = FALSE;
14348 /* TLS optimizations. Replace instruction sequences and relocs
14349 based on information we collected in tls_optimize. We edit
14350 RELOCS so that --emit-relocs will output something sensible
14351 for the final instruction stream. */
14356 tls_mask = h->tls_mask;
14357 else if (local_got_ents != NULL)
14359 struct plt_entry **local_plt = (struct plt_entry **)
14360 (local_got_ents + symtab_hdr->sh_info);
14361 unsigned char *lgot_masks = (unsigned char *)
14362 (local_plt + symtab_hdr->sh_info);
14363 tls_mask = lgot_masks[r_symndx];
14365 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
14366 && (r_type == R_PPC64_TLS
14367 || r_type == R_PPC64_TLSGD
14368 || r_type == R_PPC64_TLSLD))
14370 /* Check for toc tls entries. */
14371 unsigned char *toc_tls;
14373 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14374 &local_syms, rel, input_bfd))
14378 tls_mask = *toc_tls;
14381 /* Check that tls relocs are used with tls syms, and non-tls
14382 relocs are used with non-tls syms. */
14383 if (r_symndx != STN_UNDEF
14384 && r_type != R_PPC64_NONE
14386 || h->elf.root.type == bfd_link_hash_defined
14387 || h->elf.root.type == bfd_link_hash_defweak)
14388 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
14390 if ((tls_mask & TLS_TLS) != 0
14391 && (r_type == R_PPC64_TLS
14392 || r_type == R_PPC64_TLSGD
14393 || r_type == R_PPC64_TLSLD))
14394 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14397 info->callbacks->einfo
14398 (!IS_PPC64_TLS_RELOC (r_type)
14399 /* xgettext:c-format */
14400 ? _("%H: %s used with TLS symbol `%pT'\n")
14401 /* xgettext:c-format */
14402 : _("%H: %s used with non-TLS symbol `%pT'\n"),
14403 input_bfd, input_section, rel->r_offset,
14404 ppc64_elf_howto_table[r_type]->name,
14408 /* Ensure reloc mapping code below stays sane. */
14409 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14410 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14411 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14412 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14413 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14414 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14415 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14416 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14417 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14418 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14426 case R_PPC64_LO_DS_OPT:
14427 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
14428 if ((insn & (0x3f << 26)) != 58u << 26)
14430 insn += (14u << 26) - (58u << 26);
14431 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
14432 r_type = R_PPC64_TOC16_LO;
14433 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14436 case R_PPC64_TOC16:
14437 case R_PPC64_TOC16_LO:
14438 case R_PPC64_TOC16_DS:
14439 case R_PPC64_TOC16_LO_DS:
14441 /* Check for toc tls entries. */
14442 unsigned char *toc_tls;
14445 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14446 &local_syms, rel, input_bfd);
14452 tls_mask = *toc_tls;
14453 if (r_type == R_PPC64_TOC16_DS
14454 || r_type == R_PPC64_TOC16_LO_DS)
14456 if ((tls_mask & TLS_TLS) != 0
14457 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14462 /* If we found a GD reloc pair, then we might be
14463 doing a GD->IE transition. */
14467 if ((tls_mask & TLS_TLS) != 0
14468 && (tls_mask & TLS_GD) == 0)
14471 else if (retval == 3)
14473 if ((tls_mask & TLS_TLS) != 0
14474 && (tls_mask & TLS_LD) == 0)
14482 case R_PPC64_GOT_TPREL16_HI:
14483 case R_PPC64_GOT_TPREL16_HA:
14484 if ((tls_mask & TLS_TLS) != 0
14485 && (tls_mask & TLS_TPREL) == 0)
14487 rel->r_offset -= d_offset;
14488 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14489 r_type = R_PPC64_NONE;
14490 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14494 case R_PPC64_GOT_TPREL16_DS:
14495 case R_PPC64_GOT_TPREL16_LO_DS:
14496 if ((tls_mask & TLS_TLS) != 0
14497 && (tls_mask & TLS_TPREL) == 0)
14500 insn = bfd_get_32 (input_bfd,
14501 contents + rel->r_offset - d_offset);
14503 insn |= 0x3c0d0000; /* addis 0,13,0 */
14504 bfd_put_32 (input_bfd, insn,
14505 contents + rel->r_offset - d_offset);
14506 r_type = R_PPC64_TPREL16_HA;
14507 if (toc_symndx != 0)
14509 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14510 rel->r_addend = toc_addend;
14511 /* We changed the symbol. Start over in order to
14512 get h, sym, sec etc. right. */
14516 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14520 case R_PPC64_GOT_TPREL34:
14521 if ((tls_mask & TLS_TLS) != 0
14522 && (tls_mask & TLS_TPREL) == 0)
14524 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
14525 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14527 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14528 pinsn += ((2ULL << 56) + (-1ULL << 52)
14529 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
14530 bfd_put_32 (input_bfd, pinsn >> 32,
14531 contents + rel->r_offset);
14532 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14533 contents + rel->r_offset + 4);
14534 r_type = R_PPC64_TPREL34;
14535 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14540 if ((tls_mask & TLS_TLS) != 0
14541 && (tls_mask & TLS_TPREL) == 0)
14543 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14544 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14547 if ((rel->r_offset & 3) == 0)
14549 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14550 /* Was PPC64_TLS which sits on insn boundary, now
14551 PPC64_TPREL16_LO which is at low-order half-word. */
14552 rel->r_offset += d_offset;
14553 r_type = R_PPC64_TPREL16_LO;
14554 if (toc_symndx != 0)
14556 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14557 rel->r_addend = toc_addend;
14558 /* We changed the symbol. Start over in order to
14559 get h, sym, sec etc. right. */
14563 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14565 else if ((rel->r_offset & 3) == 1)
14567 /* For pcrel IE to LE we already have the full
14568 offset and thus don't need an addi here. A nop
14570 if ((insn & (0x3f << 26)) == 14 << 26)
14572 /* Extract regs from addi rt,ra,si. */
14573 unsigned int rt = (insn >> 21) & 0x1f;
14574 unsigned int ra = (insn >> 16) & 0x1f;
14579 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
14580 insn = (rt << 16) | (ra << 21) | (ra << 11);
14581 insn |= (31u << 26) | (444u << 1);
14584 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
14589 case R_PPC64_GOT_TLSGD16_HI:
14590 case R_PPC64_GOT_TLSGD16_HA:
14592 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14596 case R_PPC64_GOT_TLSLD16_HI:
14597 case R_PPC64_GOT_TLSLD16_HA:
14598 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14601 if ((tls_mask & tls_gd) != 0)
14602 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14603 + R_PPC64_GOT_TPREL16_DS);
14606 rel->r_offset -= d_offset;
14607 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14608 r_type = R_PPC64_NONE;
14610 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14614 case R_PPC64_GOT_TLSGD16:
14615 case R_PPC64_GOT_TLSGD16_LO:
14617 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14621 case R_PPC64_GOT_TLSLD16:
14622 case R_PPC64_GOT_TLSLD16_LO:
14623 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14625 unsigned int insn1, insn2;
14628 offset = (bfd_vma) -1;
14629 /* If not using the newer R_PPC64_TLSGD/LD to mark
14630 __tls_get_addr calls, we must trust that the call
14631 stays with its arg setup insns, ie. that the next
14632 reloc is the __tls_get_addr call associated with
14633 the current reloc. Edit both insns. */
14634 if (input_section->has_tls_get_addr_call
14635 && rel + 1 < relend
14636 && branch_reloc_hash_match (input_bfd, rel + 1,
14637 htab->tls_get_addr,
14638 htab->tls_get_addr_fd))
14639 offset = rel[1].r_offset;
14640 /* We read the low GOT_TLS (or TOC16) insn because we
14641 need to keep the destination reg. It may be
14642 something other than the usual r3, and moved to r3
14643 before the call by intervening code. */
14644 insn1 = bfd_get_32 (input_bfd,
14645 contents + rel->r_offset - d_offset);
14646 if ((tls_mask & tls_gd) != 0)
14649 insn1 &= (0x1f << 21) | (0x1f << 16);
14650 insn1 |= 58 << 26; /* ld */
14651 insn2 = 0x7c636a14; /* add 3,3,13 */
14652 if (offset != (bfd_vma) -1)
14653 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14654 if (r_type == R_PPC64_TOC16
14655 || r_type == R_PPC64_TOC16_LO)
14656 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14658 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
14659 + R_PPC64_GOT_TPREL16_DS);
14660 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14665 insn1 &= 0x1f << 21;
14666 insn1 |= 0x3c0d0000; /* addis r,13,0 */
14667 insn2 = 0x38630000; /* addi 3,3,0 */
14670 /* Was an LD reloc. */
14671 r_symndx = STN_UNDEF;
14672 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14674 else if (toc_symndx != 0)
14676 r_symndx = toc_symndx;
14677 rel->r_addend = toc_addend;
14679 r_type = R_PPC64_TPREL16_HA;
14680 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14681 if (offset != (bfd_vma) -1)
14683 rel[1].r_info = ELF64_R_INFO (r_symndx,
14684 R_PPC64_TPREL16_LO);
14685 rel[1].r_offset = offset + d_offset;
14686 rel[1].r_addend = rel->r_addend;
14689 bfd_put_32 (input_bfd, insn1,
14690 contents + rel->r_offset - d_offset);
14691 if (offset != (bfd_vma) -1)
14693 bfd_put_32 (input_bfd, insn2, contents + offset);
14694 if (offset + 8 <= input_section->size)
14696 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14697 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14698 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14701 if ((tls_mask & tls_gd) == 0
14702 && (tls_gd == 0 || toc_symndx != 0))
14704 /* We changed the symbol. Start over in order
14705 to get h, sym, sec etc. right. */
14711 case R_PPC64_GOT_TLSGD34:
14712 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14714 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14716 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14717 if ((tls_mask & TLS_GDIE) != 0)
14719 /* IE, pla -> pld */
14720 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
14721 r_type = R_PPC64_GOT_TPREL34;
14725 /* LE, pla pcrel -> paddi r13 */
14726 pinsn += (-1ULL << 52) + (13ULL << 16);
14727 r_type = R_PPC64_TPREL34;
14729 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14730 bfd_put_32 (input_bfd, pinsn >> 32,
14731 contents + rel->r_offset);
14732 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14733 contents + rel->r_offset + 4);
14737 case R_PPC64_GOT_TLSLD34:
14738 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14740 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14742 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14743 pinsn += (-1ULL << 52) + (13ULL << 16);
14744 bfd_put_32 (input_bfd, pinsn >> 32,
14745 contents + rel->r_offset);
14746 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14747 contents + rel->r_offset + 4);
14748 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14749 r_symndx = STN_UNDEF;
14750 r_type = R_PPC64_TPREL34;
14751 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14756 case R_PPC64_TLSGD:
14757 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
14758 && rel + 1 < relend)
14760 unsigned int insn2;
14761 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14763 offset = rel->r_offset;
14764 if (is_plt_seq_reloc (r_type1))
14766 bfd_put_32 (output_bfd, NOP, contents + offset);
14767 if (r_type1 == R_PPC64_PLT_PCREL34
14768 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14769 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14770 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14774 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14775 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14777 if ((tls_mask & TLS_GDIE) != 0)
14780 r_type = R_PPC64_NONE;
14781 insn2 = 0x7c636a14; /* add 3,3,13 */
14786 if (toc_symndx != 0)
14788 r_symndx = toc_symndx;
14789 rel->r_addend = toc_addend;
14791 if (r_type1 == R_PPC64_REL24_NOTOC
14792 || r_type1 == R_PPC64_PLTCALL_NOTOC)
14794 r_type = R_PPC64_NONE;
14799 rel->r_offset = offset + d_offset;
14800 r_type = R_PPC64_TPREL16_LO;
14801 insn2 = 0x38630000; /* addi 3,3,0 */
14804 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14805 /* Zap the reloc on the _tls_get_addr call too. */
14806 BFD_ASSERT (offset == rel[1].r_offset);
14807 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14808 bfd_put_32 (input_bfd, insn2, contents + offset);
14809 if ((tls_mask & TLS_GDIE) == 0
14811 && r_type != R_PPC64_NONE)
14816 case R_PPC64_TLSLD:
14817 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
14818 && rel + 1 < relend)
14820 unsigned int insn2;
14821 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14823 offset = rel->r_offset;
14824 if (is_plt_seq_reloc (r_type1))
14826 bfd_put_32 (output_bfd, NOP, contents + offset);
14827 if (r_type1 == R_PPC64_PLT_PCREL34
14828 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14829 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14830 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14834 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14835 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14837 if (r_type1 == R_PPC64_REL24_NOTOC
14838 || r_type1 == R_PPC64_PLTCALL_NOTOC)
14840 r_type = R_PPC64_NONE;
14845 rel->r_offset = offset + d_offset;
14846 r_symndx = STN_UNDEF;
14847 r_type = R_PPC64_TPREL16_LO;
14848 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14849 insn2 = 0x38630000; /* addi 3,3,0 */
14851 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14852 /* Zap the reloc on the _tls_get_addr call too. */
14853 BFD_ASSERT (offset == rel[1].r_offset);
14854 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14855 bfd_put_32 (input_bfd, insn2, contents + offset);
14856 if (r_type != R_PPC64_NONE)
14861 case R_PPC64_DTPMOD64:
14862 if (rel + 1 < relend
14863 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14864 && rel[1].r_offset == rel->r_offset + 8)
14866 if ((tls_mask & TLS_GD) == 0)
14868 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14869 if ((tls_mask & TLS_GDIE) != 0)
14870 r_type = R_PPC64_TPREL64;
14873 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14874 r_type = R_PPC64_NONE;
14876 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14881 if ((tls_mask & TLS_LD) == 0)
14883 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14884 r_type = R_PPC64_NONE;
14885 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14890 case R_PPC64_TPREL64:
14891 if ((tls_mask & TLS_TPREL) == 0)
14893 r_type = R_PPC64_NONE;
14894 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14898 case R_PPC64_ENTRY:
14899 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14900 if (!bfd_link_pic (info)
14901 && !info->traditional_format
14902 && relocation + 0x80008000 <= 0xffffffff)
14904 unsigned int insn1, insn2;
14906 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14907 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14908 if ((insn1 & ~0xfffc) == LD_R2_0R12
14909 && insn2 == ADD_R2_R2_R12)
14911 bfd_put_32 (input_bfd,
14912 LIS_R2 + PPC_HA (relocation),
14913 contents + rel->r_offset);
14914 bfd_put_32 (input_bfd,
14915 ADDI_R2_R2 + PPC_LO (relocation),
14916 contents + rel->r_offset + 4);
14921 relocation -= (rel->r_offset
14922 + input_section->output_offset
14923 + input_section->output_section->vma);
14924 if (relocation + 0x80008000 <= 0xffffffff)
14926 unsigned int insn1, insn2;
14928 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14929 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14930 if ((insn1 & ~0xfffc) == LD_R2_0R12
14931 && insn2 == ADD_R2_R2_R12)
14933 bfd_put_32 (input_bfd,
14934 ADDIS_R2_R12 + PPC_HA (relocation),
14935 contents + rel->r_offset);
14936 bfd_put_32 (input_bfd,
14937 ADDI_R2_R2 + PPC_LO (relocation),
14938 contents + rel->r_offset + 4);
14944 case R_PPC64_REL16_HA:
14945 /* If we are generating a non-PIC executable, edit
14946 . 0: addis 2,12,.TOC.-0b@ha
14947 . addi 2,2,.TOC.-0b@l
14948 used by ELFv2 global entry points to set up r2, to
14951 if .TOC. is in range. */
14952 if (!bfd_link_pic (info)
14953 && !info->traditional_format
14955 && rel->r_addend == d_offset
14956 && h != NULL && &h->elf == htab->elf.hgot
14957 && rel + 1 < relend
14958 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14959 && rel[1].r_offset == rel->r_offset + 4
14960 && rel[1].r_addend == rel->r_addend + 4
14961 && relocation + 0x80008000 <= 0xffffffff)
14963 unsigned int insn1, insn2;
14964 offset = rel->r_offset - d_offset;
14965 insn1 = bfd_get_32 (input_bfd, contents + offset);
14966 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14967 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14968 && (insn2 & 0xffff0000) == ADDI_R2_R2)
14970 r_type = R_PPC64_ADDR16_HA;
14971 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14972 rel->r_addend -= d_offset;
14973 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14974 rel[1].r_addend -= d_offset + 4;
14975 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14981 /* Handle other relocations that tweak non-addend part of insn. */
14983 max_br_offset = 1 << 25;
14984 addend = rel->r_addend;
14985 reloc_dest = DEST_NORMAL;
14991 case R_PPC64_TOCSAVE:
14992 if (relocation + addend == (rel->r_offset
14993 + input_section->output_offset
14994 + input_section->output_section->vma)
14995 && tocsave_find (htab, NO_INSERT,
14996 &local_syms, rel, input_bfd))
14998 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15000 || insn == CROR_151515 || insn == CROR_313131)
15001 bfd_put_32 (input_bfd,
15002 STD_R2_0R1 + STK_TOC (htab),
15003 contents + rel->r_offset);
15007 /* Branch taken prediction relocations. */
15008 case R_PPC64_ADDR14_BRTAKEN:
15009 case R_PPC64_REL14_BRTAKEN:
15010 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
15011 /* Fall through. */
15013 /* Branch not taken prediction relocations. */
15014 case R_PPC64_ADDR14_BRNTAKEN:
15015 case R_PPC64_REL14_BRNTAKEN:
15016 insn |= bfd_get_32 (input_bfd,
15017 contents + rel->r_offset) & ~(0x01 << 21);
15018 /* Fall through. */
15020 case R_PPC64_REL14:
15021 max_br_offset = 1 << 15;
15022 /* Fall through. */
15024 case R_PPC64_REL24:
15025 case R_PPC64_REL24_NOTOC:
15026 case R_PPC64_PLTCALL:
15027 case R_PPC64_PLTCALL_NOTOC:
15028 /* Calls to functions with a different TOC, such as calls to
15029 shared objects, need to alter the TOC pointer. This is
15030 done using a linkage stub. A REL24 branching to these
15031 linkage stubs needs to be followed by a nop, as the nop
15032 will be replaced with an instruction to restore the TOC
15037 && h->oh->is_func_descriptor)
15038 fdh = ppc_follow_link (h->oh);
15039 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15041 if ((r_type == R_PPC64_PLTCALL
15042 || r_type == R_PPC64_PLTCALL_NOTOC)
15043 && stub_entry != NULL
15044 && stub_entry->stub_type >= ppc_stub_plt_call
15045 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15048 if (stub_entry != NULL
15049 && ((stub_entry->stub_type >= ppc_stub_plt_call
15050 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15051 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15052 || stub_entry->stub_type == ppc_stub_plt_branch_both
15053 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15054 || stub_entry->stub_type == ppc_stub_long_branch_both))
15056 bfd_boolean can_plt_call = FALSE;
15058 if (stub_entry->stub_type == ppc_stub_plt_call
15060 && htab->params->plt_localentry0 != 0
15061 && is_elfv2_localentry0 (&h->elf))
15063 /* The function doesn't use or change r2. */
15064 can_plt_call = TRUE;
15066 else if (r_type == R_PPC64_REL24_NOTOC)
15068 /* NOTOC calls don't need to restore r2. */
15069 can_plt_call = TRUE;
15072 /* All of these stubs may modify r2, so there must be a
15073 branch and link followed by a nop. The nop is
15074 replaced by an insn to restore r2. */
15075 else if (rel->r_offset + 8 <= input_section->size)
15079 br = bfd_get_32 (input_bfd,
15080 contents + rel->r_offset);
15085 nop = bfd_get_32 (input_bfd,
15086 contents + rel->r_offset + 4);
15087 if (nop == LD_R2_0R1 + STK_TOC (htab))
15088 can_plt_call = TRUE;
15089 else if (nop == NOP
15090 || nop == CROR_151515
15091 || nop == CROR_313131)
15094 && (h == htab->tls_get_addr_fd
15095 || h == htab->tls_get_addr)
15096 && htab->params->tls_get_addr_opt)
15098 /* Special stub used, leave nop alone. */
15101 bfd_put_32 (input_bfd,
15102 LD_R2_0R1 + STK_TOC (htab),
15103 contents + rel->r_offset + 4);
15104 can_plt_call = TRUE;
15109 if (!can_plt_call && h != NULL)
15111 const char *name = h->elf.root.root.string;
15116 if (strncmp (name, "__libc_start_main", 17) == 0
15117 && (name[17] == 0 || name[17] == '@'))
15119 /* Allow crt1 branch to go via a toc adjusting
15120 stub. Other calls that never return could do
15121 the same, if we could detect such. */
15122 can_plt_call = TRUE;
15128 /* g++ as of 20130507 emits self-calls without a
15129 following nop. This is arguably wrong since we
15130 have conflicting information. On the one hand a
15131 global symbol and on the other a local call
15132 sequence, but don't error for this special case.
15133 It isn't possible to cheaply verify we have
15134 exactly such a call. Allow all calls to the same
15136 asection *code_sec = sec;
15138 if (get_opd_info (sec) != NULL)
15140 bfd_vma off = (relocation + addend
15141 - sec->output_section->vma
15142 - sec->output_offset);
15144 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
15146 if (code_sec == input_section)
15147 can_plt_call = TRUE;
15152 if (stub_entry->stub_type >= ppc_stub_plt_call
15153 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15154 info->callbacks->einfo
15155 /* xgettext:c-format */
15156 (_("%H: call to `%pT' lacks nop, can't restore toc; "
15157 "(plt call stub)\n"),
15158 input_bfd, input_section, rel->r_offset, sym_name);
15160 info->callbacks->einfo
15161 /* xgettext:c-format */
15162 (_("%H: call to `%pT' lacks nop, can't restore toc; "
15163 "(toc save/adjust stub)\n"),
15164 input_bfd, input_section, rel->r_offset, sym_name);
15166 bfd_set_error (bfd_error_bad_value);
15171 && stub_entry->stub_type >= ppc_stub_plt_call
15172 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15173 unresolved_reloc = FALSE;
15176 if ((stub_entry == NULL
15177 || stub_entry->stub_type == ppc_stub_long_branch
15178 || stub_entry->stub_type == ppc_stub_plt_branch)
15179 && get_opd_info (sec) != NULL)
15181 /* The branch destination is the value of the opd entry. */
15182 bfd_vma off = (relocation + addend
15183 - sec->output_section->vma
15184 - sec->output_offset);
15185 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
15186 if (dest != (bfd_vma) -1)
15190 reloc_dest = DEST_OPD;
15194 /* If the branch is out of reach we ought to have a long
15196 from = (rel->r_offset
15197 + input_section->output_offset
15198 + input_section->output_section->vma);
15200 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15204 if (stub_entry != NULL
15205 && (stub_entry->stub_type == ppc_stub_long_branch
15206 || stub_entry->stub_type == ppc_stub_plt_branch)
15207 && (r_type == R_PPC64_ADDR14_BRTAKEN
15208 || r_type == R_PPC64_ADDR14_BRNTAKEN
15209 || (relocation + addend - from + max_br_offset
15210 < 2 * max_br_offset)))
15211 /* Don't use the stub if this branch is in range. */
15214 if (stub_entry != NULL
15215 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15216 || stub_entry->stub_type == ppc_stub_long_branch_both
15217 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15218 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15219 && (r_type != R_PPC64_REL24_NOTOC
15220 || ((fdh ? fdh->elf.other : sym->st_other)
15221 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
15222 && (relocation + addend - from + max_br_offset
15223 < 2 * max_br_offset))
15226 if (stub_entry != NULL
15227 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15228 || stub_entry->stub_type == ppc_stub_long_branch_both
15229 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15230 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15231 && r_type == R_PPC64_REL24_NOTOC
15232 && (relocation + addend - from + max_br_offset
15233 < 2 * max_br_offset))
15236 if (stub_entry != NULL)
15238 /* Munge up the value and addend so that we call the stub
15239 rather than the procedure directly. */
15240 asection *stub_sec = stub_entry->group->stub_sec;
15242 if (stub_entry->stub_type == ppc_stub_save_res)
15243 relocation += (stub_sec->output_offset
15244 + stub_sec->output_section->vma
15245 + stub_sec->size - htab->sfpr->size
15246 - htab->sfpr->output_offset
15247 - htab->sfpr->output_section->vma);
15249 relocation = (stub_entry->stub_offset
15250 + stub_sec->output_offset
15251 + stub_sec->output_section->vma);
15253 reloc_dest = DEST_STUB;
15255 if (((stub_entry->stub_type == ppc_stub_plt_call
15256 && ALWAYS_EMIT_R2SAVE)
15257 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15258 || stub_entry->stub_type == ppc_stub_plt_call_both)
15260 && (h == htab->tls_get_addr_fd
15261 || h == htab->tls_get_addr)
15262 && htab->params->tls_get_addr_opt)
15263 && rel + 1 < relend
15264 && rel[1].r_offset == rel->r_offset + 4
15265 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
15267 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
15268 || stub_entry->stub_type == ppc_stub_plt_branch_both
15269 || stub_entry->stub_type == ppc_stub_plt_call_both)
15270 && r_type == R_PPC64_REL24_NOTOC)
15273 if (r_type == R_PPC64_REL24_NOTOC
15274 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
15275 || stub_entry->stub_type == ppc_stub_plt_call_both))
15276 htab->notoc_plt = 1;
15283 /* Set 'a' bit. This is 0b00010 in BO field for branch
15284 on CR(BI) insns (BO == 001at or 011at), and 0b01000
15285 for branch on CTR insns (BO == 1a00t or 1a01t). */
15286 if ((insn & (0x14 << 21)) == (0x04 << 21))
15287 insn |= 0x02 << 21;
15288 else if ((insn & (0x14 << 21)) == (0x10 << 21))
15289 insn |= 0x08 << 21;
15295 /* Invert 'y' bit if not the default. */
15296 if ((bfd_signed_vma) (relocation + addend - from) < 0)
15297 insn ^= 0x01 << 21;
15300 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15303 /* NOP out calls to undefined weak functions.
15304 We can thus call a weak function without first
15305 checking whether the function is defined. */
15307 && h->elf.root.type == bfd_link_hash_undefweak
15308 && h->elf.dynindx == -1
15309 && (r_type == R_PPC64_REL24
15310 || r_type == R_PPC64_REL24_NOTOC)
15314 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15319 case R_PPC64_GOT16_DS:
15320 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15321 if (relocation + addend - from + 0x8000 < 0x10000
15322 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15324 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15325 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15327 insn += (14u << 26) - (58u << 26);
15328 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15329 r_type = R_PPC64_TOC16;
15330 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15335 case R_PPC64_GOT16_LO_DS:
15336 case R_PPC64_GOT16_HA:
15337 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15338 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
15339 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15341 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15342 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15344 insn += (14u << 26) - (58u << 26);
15345 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15346 r_type = R_PPC64_TOC16_LO;
15347 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15349 else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
15351 r_type = R_PPC64_TOC16_HA;
15352 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15357 case R_PPC64_GOT_PCREL34:
15358 from = (rel->r_offset
15359 + input_section->output_section->vma
15360 + input_section->output_offset);
15361 if (relocation - from + (1ULL << 33) < 1ULL << 34
15362 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15364 offset = rel->r_offset;
15365 pinsn = bfd_get_32 (input_bfd, contents + offset);
15367 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15368 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15369 == ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
15371 /* Replace with paddi. */
15372 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
15373 r_type = R_PPC64_PCREL34;
15374 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15375 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
15376 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
15382 case R_PPC64_PCREL34:
15383 if (SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15385 offset = rel->r_offset;
15386 pinsn = bfd_get_32 (input_bfd, contents + offset);
15388 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15389 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15390 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
15391 | (14ULL << 26) /* paddi */))
15394 if (rel + 1 < relend
15395 && rel[1].r_offset == offset
15396 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT))
15398 bfd_vma off2 = rel[1].r_addend;
15400 /* zero means next insn. */
15403 if (off2 + 4 <= input_section->size)
15406 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
15408 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15410 if (xlate_pcrel_opt (&pinsn, &pinsn2))
15412 bfd_put_32 (input_bfd, pinsn >> 32,
15413 contents + offset);
15414 bfd_put_32 (input_bfd, pinsn,
15415 contents + offset + 4);
15416 bfd_put_32 (input_bfd, pinsn2 >> 32,
15426 /* Set `addend'. */
15428 save_unresolved_reloc = unresolved_reloc;
15432 /* xgettext:c-format */
15433 _bfd_error_handler (_("%pB: %s unsupported"),
15434 input_bfd, ppc64_elf_howto_table[r_type]->name);
15436 bfd_set_error (bfd_error_bad_value);
15442 case R_PPC64_TLSGD:
15443 case R_PPC64_TLSLD:
15444 case R_PPC64_TOCSAVE:
15445 case R_PPC64_GNU_VTINHERIT:
15446 case R_PPC64_GNU_VTENTRY:
15447 case R_PPC64_ENTRY:
15448 case R_PPC64_PCREL_OPT:
15451 /* GOT16 relocations. Like an ADDR16 using the symbol's
15452 address in the GOT as relocation value instead of the
15453 symbol's value itself. Also, create a GOT entry for the
15454 symbol and put the symbol value there. */
15455 case R_PPC64_GOT_TLSGD16:
15456 case R_PPC64_GOT_TLSGD16_LO:
15457 case R_PPC64_GOT_TLSGD16_HI:
15458 case R_PPC64_GOT_TLSGD16_HA:
15459 case R_PPC64_GOT_TLSGD34:
15460 tls_type = TLS_TLS | TLS_GD;
15463 case R_PPC64_GOT_TLSLD16:
15464 case R_PPC64_GOT_TLSLD16_LO:
15465 case R_PPC64_GOT_TLSLD16_HI:
15466 case R_PPC64_GOT_TLSLD16_HA:
15467 case R_PPC64_GOT_TLSLD34:
15468 tls_type = TLS_TLS | TLS_LD;
15471 case R_PPC64_GOT_TPREL16_DS:
15472 case R_PPC64_GOT_TPREL16_LO_DS:
15473 case R_PPC64_GOT_TPREL16_HI:
15474 case R_PPC64_GOT_TPREL16_HA:
15475 case R_PPC64_GOT_TPREL34:
15476 tls_type = TLS_TLS | TLS_TPREL;
15479 case R_PPC64_GOT_DTPREL16_DS:
15480 case R_PPC64_GOT_DTPREL16_LO_DS:
15481 case R_PPC64_GOT_DTPREL16_HI:
15482 case R_PPC64_GOT_DTPREL16_HA:
15483 case R_PPC64_GOT_DTPREL34:
15484 tls_type = TLS_TLS | TLS_DTPREL;
15487 case R_PPC64_GOT16:
15488 case R_PPC64_GOT16_LO:
15489 case R_PPC64_GOT16_HI:
15490 case R_PPC64_GOT16_HA:
15491 case R_PPC64_GOT16_DS:
15492 case R_PPC64_GOT16_LO_DS:
15493 case R_PPC64_GOT_PCREL34:
15496 /* Relocation is to the entry for this symbol in the global
15501 unsigned long indx = 0;
15502 struct got_entry *ent;
15503 bfd_vma sym_addend = orig_rel.r_addend;
15505 if (r_type == R_PPC64_GOT_PCREL34
15506 || r_type == R_PPC64_GOT_TLSGD34
15507 || r_type == R_PPC64_GOT_TLSLD34
15508 || r_type == R_PPC64_GOT_TPREL34
15509 || r_type == R_PPC64_GOT_DTPREL34)
15512 if (tls_type == (TLS_TLS | TLS_LD)
15514 || !h->elf.def_dynamic))
15515 ent = ppc64_tlsld_got (input_bfd);
15520 if (!htab->elf.dynamic_sections_created
15521 || h->elf.dynindx == -1
15522 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15523 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
15524 /* This is actually a static link, or it is a
15525 -Bsymbolic link and the symbol is defined
15526 locally, or the symbol was forced to be local
15527 because of a version file. */
15531 indx = h->elf.dynindx;
15532 unresolved_reloc = FALSE;
15534 ent = h->elf.got.glist;
15538 if (local_got_ents == NULL)
15540 ent = local_got_ents[r_symndx];
15543 for (; ent != NULL; ent = ent->next)
15544 if (ent->addend == sym_addend
15545 && ent->owner == input_bfd
15546 && ent->tls_type == tls_type)
15552 if (ent->is_indirect)
15553 ent = ent->got.ent;
15554 offp = &ent->got.offset;
15555 got = ppc64_elf_tdata (ent->owner)->got;
15559 /* The offset must always be a multiple of 8. We use the
15560 least significant bit to record whether we have already
15561 processed this entry. */
15563 if ((off & 1) != 0)
15567 /* Generate relocs for the dynamic linker, except in
15568 the case of TLSLD where we'll use one entry per
15576 ? h->elf.type == STT_GNU_IFUNC
15577 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
15580 relgot = htab->elf.irelplt;
15582 htab->local_ifunc_resolver = 1;
15583 else if (is_static_defined (&h->elf))
15584 htab->maybe_local_ifunc_resolver = 1;
15587 || (bfd_link_pic (info)
15589 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
15590 || (tls_type == (TLS_TLS | TLS_LD)
15591 && !h->elf.def_dynamic))
15592 && !(tls_type == (TLS_TLS | TLS_TPREL)
15593 && bfd_link_executable (info)
15594 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
15595 relgot = ppc64_elf_tdata (ent->owner)->relgot;
15596 if (relgot != NULL)
15598 outrel.r_offset = (got->output_section->vma
15599 + got->output_offset
15601 outrel.r_addend = sym_addend;
15602 if (tls_type & (TLS_LD | TLS_GD))
15604 outrel.r_addend = 0;
15605 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
15606 if (tls_type == (TLS_TLS | TLS_GD))
15608 loc = relgot->contents;
15609 loc += (relgot->reloc_count++
15610 * sizeof (Elf64_External_Rela));
15611 bfd_elf64_swap_reloca_out (output_bfd,
15613 outrel.r_offset += 8;
15614 outrel.r_addend = sym_addend;
15616 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15619 else if (tls_type == (TLS_TLS | TLS_DTPREL))
15620 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15621 else if (tls_type == (TLS_TLS | TLS_TPREL))
15622 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
15623 else if (indx != 0)
15624 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15628 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15630 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15632 /* Write the .got section contents for the sake
15634 loc = got->contents + off;
15635 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15639 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
15641 outrel.r_addend += relocation;
15642 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
15644 if (htab->elf.tls_sec == NULL)
15645 outrel.r_addend = 0;
15647 outrel.r_addend -= htab->elf.tls_sec->vma;
15650 loc = relgot->contents;
15651 loc += (relgot->reloc_count++
15652 * sizeof (Elf64_External_Rela));
15653 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15656 /* Init the .got section contents here if we're not
15657 emitting a reloc. */
15660 relocation += sym_addend;
15663 if (htab->elf.tls_sec == NULL)
15667 if (tls_type & TLS_LD)
15670 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
15671 if (tls_type & TLS_TPREL)
15672 relocation += DTP_OFFSET - TP_OFFSET;
15675 if (tls_type & (TLS_GD | TLS_LD))
15677 bfd_put_64 (output_bfd, relocation,
15678 got->contents + off + 8);
15682 bfd_put_64 (output_bfd, relocation,
15683 got->contents + off);
15687 if (off >= (bfd_vma) -2)
15690 relocation = got->output_section->vma + got->output_offset + off;
15691 if (!(r_type == R_PPC64_GOT_PCREL34
15692 || r_type == R_PPC64_GOT_TLSGD34
15693 || r_type == R_PPC64_GOT_TLSLD34
15694 || r_type == R_PPC64_GOT_TPREL34
15695 || r_type == R_PPC64_GOT_DTPREL34))
15696 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
15700 case R_PPC64_PLT16_HA:
15701 case R_PPC64_PLT16_HI:
15702 case R_PPC64_PLT16_LO:
15703 case R_PPC64_PLT16_LO_DS:
15704 case R_PPC64_PLT_PCREL34:
15705 case R_PPC64_PLT_PCREL34_NOTOC:
15706 case R_PPC64_PLT32:
15707 case R_PPC64_PLT64:
15708 case R_PPC64_PLTSEQ:
15709 case R_PPC64_PLTSEQ_NOTOC:
15710 case R_PPC64_PLTCALL:
15711 case R_PPC64_PLTCALL_NOTOC:
15712 /* Relocation is to the entry for this symbol in the
15713 procedure linkage table. */
15714 unresolved_reloc = TRUE;
15716 struct plt_entry **plt_list = NULL;
15718 plt_list = &h->elf.plt.plist;
15719 else if (local_got_ents != NULL)
15721 struct plt_entry **local_plt = (struct plt_entry **)
15722 (local_got_ents + symtab_hdr->sh_info);
15723 plt_list = local_plt + r_symndx;
15727 struct plt_entry *ent;
15728 bfd_vma sym_addend = orig_rel.r_addend;
15730 if (r_type == R_PPC64_PLT_PCREL34
15731 || r_type == R_PPC64_PLT_PCREL34_NOTOC)
15734 for (ent = *plt_list; ent != NULL; ent = ent->next)
15735 if (ent->plt.offset != (bfd_vma) -1
15736 && ent->addend == sym_addend)
15741 plt = htab->elf.splt;
15742 if (!htab->elf.dynamic_sections_created
15744 || h->elf.dynindx == -1)
15747 ? h->elf.type == STT_GNU_IFUNC
15748 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15749 plt = htab->elf.iplt;
15751 plt = htab->pltlocal;
15753 relocation = (plt->output_section->vma
15754 + plt->output_offset
15755 + ent->plt.offset);
15756 if (r_type == R_PPC64_PLT16_HA
15757 || r_type == R_PPC64_PLT16_HI
15758 || r_type == R_PPC64_PLT16_LO
15759 || r_type == R_PPC64_PLT16_LO_DS)
15761 got = (elf_gp (output_bfd)
15762 + htab->sec_info[input_section->id].toc_off);
15765 if (r_type != R_PPC64_PLT_PCREL34
15766 && r_type != R_PPC64_PLT_PCREL34_NOTOC)
15768 unresolved_reloc = FALSE;
15776 /* Relocation value is TOC base. */
15777 relocation = TOCstart;
15778 if (r_symndx == STN_UNDEF)
15779 relocation += htab->sec_info[input_section->id].toc_off;
15780 else if (unresolved_reloc)
15782 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15783 relocation += htab->sec_info[sec->id].toc_off;
15785 unresolved_reloc = TRUE;
15788 /* TOC16 relocs. We want the offset relative to the TOC base,
15789 which is the address of the start of the TOC plus 0x8000.
15790 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15792 case R_PPC64_TOC16:
15793 case R_PPC64_TOC16_LO:
15794 case R_PPC64_TOC16_HI:
15795 case R_PPC64_TOC16_DS:
15796 case R_PPC64_TOC16_LO_DS:
15797 case R_PPC64_TOC16_HA:
15798 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
15801 /* Relocate against the beginning of the section. */
15802 case R_PPC64_SECTOFF:
15803 case R_PPC64_SECTOFF_LO:
15804 case R_PPC64_SECTOFF_HI:
15805 case R_PPC64_SECTOFF_DS:
15806 case R_PPC64_SECTOFF_LO_DS:
15807 case R_PPC64_SECTOFF_HA:
15809 addend -= sec->output_section->vma;
15812 case R_PPC64_REL16:
15813 case R_PPC64_REL16_LO:
15814 case R_PPC64_REL16_HI:
15815 case R_PPC64_REL16_HA:
15816 case R_PPC64_REL16_HIGH:
15817 case R_PPC64_REL16_HIGHA:
15818 case R_PPC64_REL16_HIGHER:
15819 case R_PPC64_REL16_HIGHERA:
15820 case R_PPC64_REL16_HIGHEST:
15821 case R_PPC64_REL16_HIGHESTA:
15822 case R_PPC64_REL16_HIGHER34:
15823 case R_PPC64_REL16_HIGHERA34:
15824 case R_PPC64_REL16_HIGHEST34:
15825 case R_PPC64_REL16_HIGHESTA34:
15826 case R_PPC64_REL16DX_HA:
15827 case R_PPC64_REL14:
15828 case R_PPC64_REL14_BRNTAKEN:
15829 case R_PPC64_REL14_BRTAKEN:
15830 case R_PPC64_REL24:
15831 case R_PPC64_REL24_NOTOC:
15832 case R_PPC64_PCREL34:
15833 case R_PPC64_PCREL28:
15836 case R_PPC64_TPREL16:
15837 case R_PPC64_TPREL16_LO:
15838 case R_PPC64_TPREL16_HI:
15839 case R_PPC64_TPREL16_HA:
15840 case R_PPC64_TPREL16_DS:
15841 case R_PPC64_TPREL16_LO_DS:
15842 case R_PPC64_TPREL16_HIGH:
15843 case R_PPC64_TPREL16_HIGHA:
15844 case R_PPC64_TPREL16_HIGHER:
15845 case R_PPC64_TPREL16_HIGHERA:
15846 case R_PPC64_TPREL16_HIGHEST:
15847 case R_PPC64_TPREL16_HIGHESTA:
15848 case R_PPC64_TPREL34:
15850 && h->elf.root.type == bfd_link_hash_undefweak
15851 && h->elf.dynindx == -1)
15853 /* Make this relocation against an undefined weak symbol
15854 resolve to zero. This is really just a tweak, since
15855 code using weak externs ought to check that they are
15856 defined before using them. */
15857 bfd_byte *p = contents + rel->r_offset - d_offset;
15859 insn = bfd_get_32 (input_bfd, p);
15860 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15862 bfd_put_32 (input_bfd, insn, p);
15865 if (htab->elf.tls_sec != NULL)
15866 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15867 /* The TPREL16 relocs shouldn't really be used in shared
15868 libs or with non-local symbols as that will result in
15869 DT_TEXTREL being set, but support them anyway. */
15872 case R_PPC64_DTPREL16:
15873 case R_PPC64_DTPREL16_LO:
15874 case R_PPC64_DTPREL16_HI:
15875 case R_PPC64_DTPREL16_HA:
15876 case R_PPC64_DTPREL16_DS:
15877 case R_PPC64_DTPREL16_LO_DS:
15878 case R_PPC64_DTPREL16_HIGH:
15879 case R_PPC64_DTPREL16_HIGHA:
15880 case R_PPC64_DTPREL16_HIGHER:
15881 case R_PPC64_DTPREL16_HIGHERA:
15882 case R_PPC64_DTPREL16_HIGHEST:
15883 case R_PPC64_DTPREL16_HIGHESTA:
15884 case R_PPC64_DTPREL34:
15885 if (htab->elf.tls_sec != NULL)
15886 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15889 case R_PPC64_ADDR64_LOCAL:
15890 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15895 case R_PPC64_DTPMOD64:
15900 case R_PPC64_TPREL64:
15901 if (htab->elf.tls_sec != NULL)
15902 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15905 case R_PPC64_DTPREL64:
15906 if (htab->elf.tls_sec != NULL)
15907 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15908 /* Fall through. */
15910 /* Relocations that may need to be propagated if this is a
15912 case R_PPC64_REL30:
15913 case R_PPC64_REL32:
15914 case R_PPC64_REL64:
15915 case R_PPC64_ADDR14:
15916 case R_PPC64_ADDR14_BRNTAKEN:
15917 case R_PPC64_ADDR14_BRTAKEN:
15918 case R_PPC64_ADDR16:
15919 case R_PPC64_ADDR16_DS:
15920 case R_PPC64_ADDR16_HA:
15921 case R_PPC64_ADDR16_HI:
15922 case R_PPC64_ADDR16_HIGH:
15923 case R_PPC64_ADDR16_HIGHA:
15924 case R_PPC64_ADDR16_HIGHER:
15925 case R_PPC64_ADDR16_HIGHERA:
15926 case R_PPC64_ADDR16_HIGHEST:
15927 case R_PPC64_ADDR16_HIGHESTA:
15928 case R_PPC64_ADDR16_LO:
15929 case R_PPC64_ADDR16_LO_DS:
15930 case R_PPC64_ADDR16_HIGHER34:
15931 case R_PPC64_ADDR16_HIGHERA34:
15932 case R_PPC64_ADDR16_HIGHEST34:
15933 case R_PPC64_ADDR16_HIGHESTA34:
15934 case R_PPC64_ADDR24:
15935 case R_PPC64_ADDR32:
15936 case R_PPC64_ADDR64:
15937 case R_PPC64_UADDR16:
15938 case R_PPC64_UADDR32:
15939 case R_PPC64_UADDR64:
15941 case R_PPC64_D34_LO:
15942 case R_PPC64_D34_HI30:
15943 case R_PPC64_D34_HA30:
15946 if ((input_section->flags & SEC_ALLOC) == 0)
15949 if (NO_OPD_RELOCS && is_opd)
15952 if (bfd_link_pic (info)
15954 || h->dyn_relocs != NULL)
15955 && ((h != NULL && pc_dynrelocs (h))
15956 || must_be_dyn_reloc (info, r_type)))
15958 ? h->dyn_relocs != NULL
15959 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15961 bfd_boolean skip, relocate;
15966 /* When generating a dynamic object, these relocations
15967 are copied into the output file to be resolved at run
15973 out_off = _bfd_elf_section_offset (output_bfd, info,
15974 input_section, rel->r_offset);
15975 if (out_off == (bfd_vma) -1)
15977 else if (out_off == (bfd_vma) -2)
15978 skip = TRUE, relocate = TRUE;
15979 out_off += (input_section->output_section->vma
15980 + input_section->output_offset);
15981 outrel.r_offset = out_off;
15982 outrel.r_addend = rel->r_addend;
15984 /* Optimize unaligned reloc use. */
15985 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15986 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15987 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15988 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15989 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15990 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15991 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15992 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15993 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15996 memset (&outrel, 0, sizeof outrel);
15997 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15999 && r_type != R_PPC64_TOC)
16001 indx = h->elf.dynindx;
16002 BFD_ASSERT (indx != -1);
16003 outrel.r_info = ELF64_R_INFO (indx, r_type);
16007 /* This symbol is local, or marked to become local,
16008 or this is an opd section reloc which must point
16009 at a local function. */
16010 outrel.r_addend += relocation;
16011 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
16013 if (is_opd && h != NULL)
16015 /* Lie about opd entries. This case occurs
16016 when building shared libraries and we
16017 reference a function in another shared
16018 lib. The same thing happens for a weak
16019 definition in an application that's
16020 overridden by a strong definition in a
16021 shared lib. (I believe this is a generic
16022 bug in binutils handling of weak syms.)
16023 In these cases we won't use the opd
16024 entry in this lib. */
16025 unresolved_reloc = FALSE;
16028 && r_type == R_PPC64_ADDR64
16030 ? h->elf.type == STT_GNU_IFUNC
16031 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16032 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16035 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16037 /* We need to relocate .opd contents for ld.so.
16038 Prelink also wants simple and consistent rules
16039 for relocs. This make all RELATIVE relocs have
16040 *r_offset equal to r_addend. */
16047 ? h->elf.type == STT_GNU_IFUNC
16048 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16050 info->callbacks->einfo
16051 /* xgettext:c-format */
16052 (_("%H: %s for indirect "
16053 "function `%pT' unsupported\n"),
16054 input_bfd, input_section, rel->r_offset,
16055 ppc64_elf_howto_table[r_type]->name,
16059 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
16061 else if (sec == NULL || sec->owner == NULL)
16063 bfd_set_error (bfd_error_bad_value);
16068 asection *osec = sec->output_section;
16070 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
16072 /* TLS symbol values are relative to the
16073 TLS segment. Dynamic relocations for
16074 local TLS symbols therefore can't be
16075 reduced to a relocation against their
16076 section symbol because it holds the
16077 address of the section, not a value
16078 relative to the TLS segment. We could
16079 change the .tdata dynamic section symbol
16080 to be zero value but STN_UNDEF works
16081 and is used elsewhere, eg. for TPREL64
16082 GOT relocs against local TLS symbols. */
16083 osec = htab->elf.tls_sec;
16088 indx = elf_section_data (osec)->dynindx;
16091 if ((osec->flags & SEC_READONLY) == 0
16092 && htab->elf.data_index_section != NULL)
16093 osec = htab->elf.data_index_section;
16095 osec = htab->elf.text_index_section;
16096 indx = elf_section_data (osec)->dynindx;
16098 BFD_ASSERT (indx != 0);
16101 /* We are turning this relocation into one
16102 against a section symbol, so subtract out
16103 the output section's address but not the
16104 offset of the input section in the output
16106 outrel.r_addend -= osec->vma;
16109 outrel.r_info = ELF64_R_INFO (indx, r_type);
16113 sreloc = elf_section_data (input_section)->sreloc;
16115 ? h->elf.type == STT_GNU_IFUNC
16116 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16118 sreloc = htab->elf.irelplt;
16120 htab->local_ifunc_resolver = 1;
16121 else if (is_static_defined (&h->elf))
16122 htab->maybe_local_ifunc_resolver = 1;
16124 if (sreloc == NULL)
16127 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16130 loc = sreloc->contents;
16131 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
16132 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16134 /* If this reloc is against an external symbol, it will
16135 be computed at runtime, so there's no need to do
16136 anything now. However, for the sake of prelink ensure
16137 that the section contents are a known value. */
16140 unresolved_reloc = FALSE;
16141 /* The value chosen here is quite arbitrary as ld.so
16142 ignores section contents except for the special
16143 case of .opd where the contents might be accessed
16144 before relocation. Choose zero, as that won't
16145 cause reloc overflow. */
16148 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16149 to improve backward compatibility with older
16151 if (r_type == R_PPC64_ADDR64)
16152 addend = outrel.r_addend;
16153 /* Adjust pc_relative relocs to have zero in *r_offset. */
16154 else if (ppc64_elf_howto_table[r_type]->pc_relative)
16155 addend = outrel.r_offset;
16161 case R_PPC64_GLOB_DAT:
16162 case R_PPC64_JMP_SLOT:
16163 case R_PPC64_JMP_IREL:
16164 case R_PPC64_RELATIVE:
16165 /* We shouldn't ever see these dynamic relocs in relocatable
16167 /* Fall through. */
16169 case R_PPC64_PLTGOT16:
16170 case R_PPC64_PLTGOT16_DS:
16171 case R_PPC64_PLTGOT16_HA:
16172 case R_PPC64_PLTGOT16_HI:
16173 case R_PPC64_PLTGOT16_LO:
16174 case R_PPC64_PLTGOT16_LO_DS:
16175 case R_PPC64_PLTREL32:
16176 case R_PPC64_PLTREL64:
16177 /* These ones haven't been implemented yet. */
16179 info->callbacks->einfo
16180 /* xgettext:c-format */
16181 (_("%P: %pB: %s is not supported for `%pT'\n"),
16183 ppc64_elf_howto_table[r_type]->name, sym_name);
16185 bfd_set_error (bfd_error_invalid_operation);
16190 /* Multi-instruction sequences that access the TOC can be
16191 optimized, eg. addis ra,r2,0; addi rb,ra,x;
16192 to nop; addi rb,r2,x; */
16198 case R_PPC64_GOT_TLSLD16_HI:
16199 case R_PPC64_GOT_TLSGD16_HI:
16200 case R_PPC64_GOT_TPREL16_HI:
16201 case R_PPC64_GOT_DTPREL16_HI:
16202 case R_PPC64_GOT16_HI:
16203 case R_PPC64_TOC16_HI:
16204 /* These relocs would only be useful if building up an
16205 offset to later add to r2, perhaps in an indexed
16206 addressing mode instruction. Don't try to optimize.
16207 Unfortunately, the possibility of someone building up an
16208 offset like this or even with the HA relocs, means that
16209 we need to check the high insn when optimizing the low
16213 case R_PPC64_PLTCALL_NOTOC:
16214 if (!unresolved_reloc)
16215 htab->notoc_plt = 1;
16216 /* Fall through. */
16217 case R_PPC64_PLTCALL:
16218 if (unresolved_reloc)
16220 /* No plt entry. Make this into a direct call. */
16221 bfd_byte *p = contents + rel->r_offset;
16222 insn = bfd_get_32 (input_bfd, p);
16224 bfd_put_32 (input_bfd, B_DOT | insn, p);
16225 if (r_type == R_PPC64_PLTCALL)
16226 bfd_put_32 (input_bfd, NOP, p + 4);
16227 unresolved_reloc = save_unresolved_reloc;
16228 r_type = R_PPC64_REL24;
16232 case R_PPC64_PLTSEQ_NOTOC:
16233 case R_PPC64_PLTSEQ:
16234 if (unresolved_reloc)
16236 unresolved_reloc = FALSE;
16241 case R_PPC64_PLT_PCREL34_NOTOC:
16242 if (!unresolved_reloc)
16243 htab->notoc_plt = 1;
16244 /* Fall through. */
16245 case R_PPC64_PLT_PCREL34:
16246 if (unresolved_reloc)
16248 bfd_byte *p = contents + rel->r_offset;
16249 bfd_put_32 (input_bfd, PNOP >> 32, p);
16250 bfd_put_32 (input_bfd, PNOP, p + 4);
16251 unresolved_reloc = FALSE;
16256 case R_PPC64_PLT16_HA:
16257 if (unresolved_reloc)
16259 unresolved_reloc = FALSE;
16262 /* Fall through. */
16263 case R_PPC64_GOT_TLSLD16_HA:
16264 case R_PPC64_GOT_TLSGD16_HA:
16265 case R_PPC64_GOT_TPREL16_HA:
16266 case R_PPC64_GOT_DTPREL16_HA:
16267 case R_PPC64_GOT16_HA:
16268 case R_PPC64_TOC16_HA:
16269 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16270 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16274 p = contents + (rel->r_offset & ~3);
16275 bfd_put_32 (input_bfd, NOP, p);
16280 case R_PPC64_PLT16_LO:
16281 case R_PPC64_PLT16_LO_DS:
16282 if (unresolved_reloc)
16284 unresolved_reloc = FALSE;
16287 /* Fall through. */
16288 case R_PPC64_GOT_TLSLD16_LO:
16289 case R_PPC64_GOT_TLSGD16_LO:
16290 case R_PPC64_GOT_TPREL16_LO_DS:
16291 case R_PPC64_GOT_DTPREL16_LO_DS:
16292 case R_PPC64_GOT16_LO:
16293 case R_PPC64_GOT16_LO_DS:
16294 case R_PPC64_TOC16_LO:
16295 case R_PPC64_TOC16_LO_DS:
16296 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16297 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16299 bfd_byte *p = contents + (rel->r_offset & ~3);
16300 insn = bfd_get_32 (input_bfd, p);
16301 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
16303 /* Transform addic to addi when we change reg. */
16304 insn &= ~((0x3f << 26) | (0x1f << 16));
16305 insn |= (14u << 26) | (2 << 16);
16309 insn &= ~(0x1f << 16);
16312 bfd_put_32 (input_bfd, insn, p);
16316 case R_PPC64_TPREL16_HA:
16317 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16319 bfd_byte *p = contents + (rel->r_offset & ~3);
16320 insn = bfd_get_32 (input_bfd, p);
16321 if ((insn & ((0x3f << 26) | 0x1f << 16))
16322 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
16323 /* xgettext:c-format */
16324 info->callbacks->minfo
16325 (_("%H: warning: %s unexpected insn %#x.\n"),
16326 input_bfd, input_section, rel->r_offset,
16327 ppc64_elf_howto_table[r_type]->name, insn);
16330 bfd_put_32 (input_bfd, NOP, p);
16336 case R_PPC64_TPREL16_LO:
16337 case R_PPC64_TPREL16_LO_DS:
16338 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16340 bfd_byte *p = contents + (rel->r_offset & ~3);
16341 insn = bfd_get_32 (input_bfd, p);
16342 insn &= ~(0x1f << 16);
16344 bfd_put_32 (input_bfd, insn, p);
16349 /* Do any further special processing. */
16355 case R_PPC64_REL16_HA:
16356 case R_PPC64_REL16_HIGHA:
16357 case R_PPC64_REL16_HIGHERA:
16358 case R_PPC64_REL16_HIGHESTA:
16359 case R_PPC64_REL16DX_HA:
16360 case R_PPC64_ADDR16_HA:
16361 case R_PPC64_ADDR16_HIGHA:
16362 case R_PPC64_ADDR16_HIGHERA:
16363 case R_PPC64_ADDR16_HIGHESTA:
16364 case R_PPC64_TOC16_HA:
16365 case R_PPC64_SECTOFF_HA:
16366 case R_PPC64_TPREL16_HA:
16367 case R_PPC64_TPREL16_HIGHA:
16368 case R_PPC64_TPREL16_HIGHERA:
16369 case R_PPC64_TPREL16_HIGHESTA:
16370 case R_PPC64_DTPREL16_HA:
16371 case R_PPC64_DTPREL16_HIGHA:
16372 case R_PPC64_DTPREL16_HIGHERA:
16373 case R_PPC64_DTPREL16_HIGHESTA:
16374 /* It's just possible that this symbol is a weak symbol
16375 that's not actually defined anywhere. In that case,
16376 'sec' would be NULL, and we should leave the symbol
16377 alone (it will be set to zero elsewhere in the link). */
16380 /* Fall through. */
16382 case R_PPC64_GOT16_HA:
16383 case R_PPC64_PLTGOT16_HA:
16384 case R_PPC64_PLT16_HA:
16385 case R_PPC64_GOT_TLSGD16_HA:
16386 case R_PPC64_GOT_TLSLD16_HA:
16387 case R_PPC64_GOT_TPREL16_HA:
16388 case R_PPC64_GOT_DTPREL16_HA:
16389 /* Add 0x10000 if sign bit in 0:15 is set.
16390 Bits 0:15 are not used. */
16394 case R_PPC64_D34_HA30:
16395 case R_PPC64_ADDR16_HIGHERA34:
16396 case R_PPC64_ADDR16_HIGHESTA34:
16397 case R_PPC64_REL16_HIGHERA34:
16398 case R_PPC64_REL16_HIGHESTA34:
16400 addend += 1ULL << 33;
16403 case R_PPC64_ADDR16_DS:
16404 case R_PPC64_ADDR16_LO_DS:
16405 case R_PPC64_GOT16_DS:
16406 case R_PPC64_GOT16_LO_DS:
16407 case R_PPC64_PLT16_LO_DS:
16408 case R_PPC64_SECTOFF_DS:
16409 case R_PPC64_SECTOFF_LO_DS:
16410 case R_PPC64_TOC16_DS:
16411 case R_PPC64_TOC16_LO_DS:
16412 case R_PPC64_PLTGOT16_DS:
16413 case R_PPC64_PLTGOT16_LO_DS:
16414 case R_PPC64_GOT_TPREL16_DS:
16415 case R_PPC64_GOT_TPREL16_LO_DS:
16416 case R_PPC64_GOT_DTPREL16_DS:
16417 case R_PPC64_GOT_DTPREL16_LO_DS:
16418 case R_PPC64_TPREL16_DS:
16419 case R_PPC64_TPREL16_LO_DS:
16420 case R_PPC64_DTPREL16_DS:
16421 case R_PPC64_DTPREL16_LO_DS:
16422 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16424 /* If this reloc is against an lq, lxv, or stxv insn, then
16425 the value must be a multiple of 16. This is somewhat of
16426 a hack, but the "correct" way to do this by defining _DQ
16427 forms of all the _DS relocs bloats all reloc switches in
16428 this file. It doesn't make much sense to use these
16429 relocs in data, so testing the insn should be safe. */
16430 if ((insn & (0x3f << 26)) == (56u << 26)
16431 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
16433 relocation += addend;
16434 addend = insn & (mask ^ 3);
16435 if ((relocation & mask) != 0)
16437 relocation ^= relocation & mask;
16438 info->callbacks->einfo
16439 /* xgettext:c-format */
16440 (_("%H: error: %s not a multiple of %u\n"),
16441 input_bfd, input_section, rel->r_offset,
16442 ppc64_elf_howto_table[r_type]->name,
16444 bfd_set_error (bfd_error_bad_value);
16451 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16452 because such sections are not SEC_ALLOC and thus ld.so will
16453 not process them. */
16454 howto = ppc64_elf_howto_table[(int) r_type];
16455 if (unresolved_reloc
16456 && !((input_section->flags & SEC_DEBUGGING) != 0
16457 && h->elf.def_dynamic)
16458 && _bfd_elf_section_offset (output_bfd, info, input_section,
16459 rel->r_offset) != (bfd_vma) -1)
16461 info->callbacks->einfo
16462 /* xgettext:c-format */
16463 (_("%H: unresolvable %s against `%pT'\n"),
16464 input_bfd, input_section, rel->r_offset,
16466 h->elf.root.root.string);
16470 /* 16-bit fields in insns mostly have signed values, but a
16471 few insns have 16-bit unsigned values. Really, we should
16472 have different reloc types. */
16473 if (howto->complain_on_overflow != complain_overflow_dont
16474 && howto->dst_mask == 0xffff
16475 && (input_section->flags & SEC_CODE) != 0)
16477 enum complain_overflow complain = complain_overflow_signed;
16479 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16480 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16481 complain = complain_overflow_bitfield;
16482 else if (howto->rightshift == 0
16483 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16484 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16485 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16486 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16487 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16488 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
16489 complain = complain_overflow_unsigned;
16490 if (howto->complain_on_overflow != complain)
16492 alt_howto = *howto;
16493 alt_howto.complain_on_overflow = complain;
16494 howto = &alt_howto;
16500 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
16502 case R_PPC64_D34_LO:
16503 case R_PPC64_D34_HI30:
16504 case R_PPC64_D34_HA30:
16505 case R_PPC64_PCREL34:
16506 case R_PPC64_GOT_PCREL34:
16507 case R_PPC64_TPREL34:
16508 case R_PPC64_DTPREL34:
16509 case R_PPC64_GOT_TLSGD34:
16510 case R_PPC64_GOT_TLSLD34:
16511 case R_PPC64_GOT_TPREL34:
16512 case R_PPC64_GOT_DTPREL34:
16513 case R_PPC64_PLT_PCREL34:
16514 case R_PPC64_PLT_PCREL34_NOTOC:
16516 case R_PPC64_PCREL28:
16517 if (rel->r_offset + 8 > input_section->size)
16518 r = bfd_reloc_outofrange;
16521 relocation += addend;
16522 if (howto->pc_relative)
16523 relocation -= (rel->r_offset
16524 + input_section->output_offset
16525 + input_section->output_section->vma);
16526 relocation >>= howto->rightshift;
16528 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16530 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16532 pinsn &= ~howto->dst_mask;
16533 pinsn |= (((relocation << 16) | (relocation & 0xffff))
16534 & howto->dst_mask);
16535 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
16536 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
16538 if (howto->complain_on_overflow == complain_overflow_signed
16539 && (relocation + (1ULL << (howto->bitsize - 1))
16540 >= 1ULL << howto->bitsize))
16541 r = bfd_reloc_overflow;
16545 case R_PPC64_REL16DX_HA:
16546 if (rel->r_offset + 4 > input_section->size)
16547 r = bfd_reloc_outofrange;
16550 relocation += addend;
16551 relocation -= (rel->r_offset
16552 + input_section->output_offset
16553 + input_section->output_section->vma);
16554 relocation = (bfd_signed_vma) relocation >> 16;
16555 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16557 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
16558 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16560 if (relocation + 0x8000 > 0xffff)
16561 r = bfd_reloc_overflow;
16566 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
16567 contents, rel->r_offset,
16568 relocation, addend);
16571 if (r != bfd_reloc_ok)
16573 char *more_info = NULL;
16574 const char *reloc_name = howto->name;
16576 if (reloc_dest != DEST_NORMAL)
16578 more_info = bfd_malloc (strlen (reloc_name) + 8);
16579 if (more_info != NULL)
16581 strcpy (more_info, reloc_name);
16582 strcat (more_info, (reloc_dest == DEST_OPD
16583 ? " (OPD)" : " (stub)"));
16584 reloc_name = more_info;
16588 if (r == bfd_reloc_overflow)
16590 /* On code like "if (foo) foo();" don't report overflow
16591 on a branch to zero when foo is undefined. */
16593 && (reloc_dest == DEST_STUB
16595 && (h->elf.root.type == bfd_link_hash_undefweak
16596 || h->elf.root.type == bfd_link_hash_undefined)
16597 && is_branch_reloc (r_type))))
16598 info->callbacks->reloc_overflow (info, &h->elf.root,
16599 sym_name, reloc_name,
16601 input_bfd, input_section,
16606 info->callbacks->einfo
16607 /* xgettext:c-format */
16608 (_("%H: %s against `%pT': error %d\n"),
16609 input_bfd, input_section, rel->r_offset,
16610 reloc_name, sym_name, (int) r);
16613 if (more_info != NULL)
16623 Elf_Internal_Shdr *rel_hdr;
16624 size_t deleted = rel - wrel;
16626 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16627 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16628 if (rel_hdr->sh_size == 0)
16630 /* It is too late to remove an empty reloc section. Leave
16632 ??? What is wrong with an empty section??? */
16633 rel_hdr->sh_size = rel_hdr->sh_entsize;
16636 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16637 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16638 input_section->reloc_count -= deleted;
16641 /* If we're emitting relocations, then shortly after this function
16642 returns, reloc offsets and addends for this section will be
16643 adjusted. Worse, reloc symbol indices will be for the output
16644 file rather than the input. Save a copy of the relocs for
16645 opd_entry_value. */
16646 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
16649 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16650 rel = bfd_alloc (input_bfd, amt);
16651 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16652 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
16655 memcpy (rel, relocs, amt);
16660 /* Adjust the value of any local symbols in opd sections. */
16663 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16664 const char *name ATTRIBUTE_UNUSED,
16665 Elf_Internal_Sym *elfsym,
16666 asection *input_sec,
16667 struct elf_link_hash_entry *h)
16669 struct _opd_sec_data *opd;
16676 opd = get_opd_info (input_sec);
16677 if (opd == NULL || opd->adjust == NULL)
16680 value = elfsym->st_value - input_sec->output_offset;
16681 if (!bfd_link_relocatable (info))
16682 value -= input_sec->output_section->vma;
16684 adjust = opd->adjust[OPD_NDX (value)];
16688 elfsym->st_value += adjust;
16692 /* Finish up dynamic symbol handling. We set the contents of various
16693 dynamic sections here. */
16696 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16697 struct bfd_link_info *info,
16698 struct elf_link_hash_entry *h,
16699 Elf_Internal_Sym *sym)
16701 struct ppc_link_hash_table *htab;
16702 struct plt_entry *ent;
16704 htab = ppc_hash_table (info);
16708 if (!htab->opd_abi && !h->def_regular)
16709 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16710 if (ent->plt.offset != (bfd_vma) -1)
16712 /* Mark the symbol as undefined, rather than as
16713 defined in glink. Leave the value if there were
16714 any relocations where pointer equality matters
16715 (this is a clue for the dynamic linker, to make
16716 function pointer comparisons work between an
16717 application and shared library), otherwise set it
16719 sym->st_shndx = SHN_UNDEF;
16720 if (!h->pointer_equality_needed)
16722 else if (!h->ref_regular_nonweak)
16724 /* This breaks function pointer comparisons, but
16725 that is better than breaking tests for a NULL
16726 function pointer. */
16734 /* This symbol needs a copy reloc. Set it up. */
16735 Elf_Internal_Rela rela;
16739 if (h->dynindx == -1
16740 || (h->root.type != bfd_link_hash_defined
16741 && h->root.type != bfd_link_hash_defweak)
16742 || htab->elf.srelbss == NULL
16743 || htab->elf.sreldynrelro == NULL)
16746 rela.r_offset = (h->root.u.def.value
16747 + h->root.u.def.section->output_section->vma
16748 + h->root.u.def.section->output_offset);
16749 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16751 if (h->root.u.def.section == htab->elf.sdynrelro)
16752 srel = htab->elf.sreldynrelro;
16754 srel = htab->elf.srelbss;
16755 loc = srel->contents;
16756 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
16757 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
16763 /* Used to decide how to sort relocs in an optimal manner for the
16764 dynamic linker, before writing them out. */
16766 static enum elf_reloc_type_class
16767 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16768 const asection *rel_sec,
16769 const Elf_Internal_Rela *rela)
16771 enum elf_ppc64_reloc_type r_type;
16772 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16774 if (rel_sec == htab->elf.irelplt)
16775 return reloc_class_ifunc;
16777 r_type = ELF64_R_TYPE (rela->r_info);
16780 case R_PPC64_RELATIVE:
16781 return reloc_class_relative;
16782 case R_PPC64_JMP_SLOT:
16783 return reloc_class_plt;
16785 return reloc_class_copy;
16787 return reloc_class_normal;
16791 /* Finish up the dynamic sections. */
16794 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16795 struct bfd_link_info *info)
16797 struct ppc_link_hash_table *htab;
16801 htab = ppc_hash_table (info);
16805 dynobj = htab->elf.dynobj;
16806 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
16808 if (htab->elf.dynamic_sections_created)
16810 Elf64_External_Dyn *dyncon, *dynconend;
16812 if (sdyn == NULL || htab->elf.sgot == NULL)
16815 dyncon = (Elf64_External_Dyn *) sdyn->contents;
16816 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
16817 for (; dyncon < dynconend; dyncon++)
16819 Elf_Internal_Dyn dyn;
16822 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16829 case DT_PPC64_GLINK:
16831 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16832 /* We stupidly defined DT_PPC64_GLINK to be the start
16833 of glink rather than the first entry point, which is
16834 what ld.so needs, and now have a bigger stub to
16835 support automatic multiple TOCs. */
16836 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
16840 s = bfd_get_section_by_name (output_bfd, ".opd");
16843 dyn.d_un.d_ptr = s->vma;
16847 if ((htab->do_multi_toc && htab->multi_toc_needed)
16848 || htab->notoc_plt)
16849 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
16850 if (htab->has_plt_localentry0)
16851 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
16854 case DT_PPC64_OPDSZ:
16855 s = bfd_get_section_by_name (output_bfd, ".opd");
16858 dyn.d_un.d_val = s->size;
16862 s = htab->elf.splt;
16863 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16867 s = htab->elf.srelplt;
16868 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16872 dyn.d_un.d_val = htab->elf.srelplt->size;
16876 if (htab->local_ifunc_resolver)
16877 info->callbacks->einfo
16878 (_("%X%P: text relocations and GNU indirect "
16879 "functions will result in a segfault at runtime\n"));
16880 else if (htab->maybe_local_ifunc_resolver)
16881 info->callbacks->einfo
16882 (_("%P: warning: text relocations and GNU indirect "
16883 "functions may result in a segfault at runtime\n"));
16887 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
16891 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16892 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
16894 /* Fill in the first entry in the global offset table.
16895 We use it to hold the link-time TOCbase. */
16896 bfd_put_64 (output_bfd,
16897 elf_gp (output_bfd) + TOC_BASE_OFF,
16898 htab->elf.sgot->contents);
16900 /* Set .got entry size. */
16901 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
16905 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16906 && htab->elf.splt->output_section != bfd_abs_section_ptr)
16908 /* Set .plt entry size. */
16909 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
16910 = PLT_ENTRY_SIZE (htab);
16913 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16914 brlt ourselves if emitrelocations. */
16915 if (htab->brlt != NULL
16916 && htab->brlt->reloc_count != 0
16917 && !_bfd_elf_link_output_relocs (output_bfd,
16919 elf_section_data (htab->brlt)->rela.hdr,
16920 elf_section_data (htab->brlt)->relocs,
16924 if (htab->glink != NULL
16925 && htab->glink->reloc_count != 0
16926 && !_bfd_elf_link_output_relocs (output_bfd,
16928 elf_section_data (htab->glink)->rela.hdr,
16929 elf_section_data (htab->glink)->relocs,
16934 if (htab->glink_eh_frame != NULL
16935 && htab->glink_eh_frame->size != 0
16936 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16937 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16938 htab->glink_eh_frame,
16939 htab->glink_eh_frame->contents))
16942 /* We need to handle writing out multiple GOT sections ourselves,
16943 since we didn't add them to DYNOBJ. We know dynobj is the first
16945 while ((dynobj = dynobj->link.next) != NULL)
16949 if (!is_ppc64_elf (dynobj))
16952 s = ppc64_elf_tdata (dynobj)->got;
16955 && s->output_section != bfd_abs_section_ptr
16956 && !bfd_set_section_contents (output_bfd, s->output_section,
16957 s->contents, s->output_offset,
16960 s = ppc64_elf_tdata (dynobj)->relgot;
16963 && s->output_section != bfd_abs_section_ptr
16964 && !bfd_set_section_contents (output_bfd, s->output_section,
16965 s->contents, s->output_offset,
16973 #include "elf64-target.h"
16975 /* FreeBSD support */
16977 #undef TARGET_LITTLE_SYM
16978 #undef TARGET_LITTLE_NAME
16980 #undef TARGET_BIG_SYM
16981 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
16982 #undef TARGET_BIG_NAME
16983 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16986 #define ELF_OSABI ELFOSABI_FREEBSD
16989 #define elf64_bed elf64_powerpc_fbsd_bed
16991 #include "elf64-target.h"