1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2018 Free Software Foundation, Inc.
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
5 Largely rewritten by Alan Modra.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
24 /* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
34 #include "elf/ppc64.h"
35 #include "elf64-ppc.h"
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_vma opd_entry_value
57 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
59 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
60 #define TARGET_LITTLE_NAME "elf64-powerpcle"
61 #define TARGET_BIG_SYM powerpc_elf64_vec
62 #define TARGET_BIG_NAME "elf64-powerpc"
63 #define ELF_ARCH bfd_arch_powerpc
64 #define ELF_TARGET_ID PPC64_ELF_DATA
65 #define ELF_MACHINE_CODE EM_PPC64
66 #define ELF_MAXPAGESIZE 0x10000
67 #define ELF_COMMONPAGESIZE 0x1000
68 #define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
69 #define elf_info_to_howto ppc64_elf_info_to_howto
71 #define elf_backend_want_got_sym 0
72 #define elf_backend_want_plt_sym 0
73 #define elf_backend_plt_alignment 3
74 #define elf_backend_plt_not_loaded 1
75 #define elf_backend_got_header_size 8
76 #define elf_backend_want_dynrelro 1
77 #define elf_backend_can_gc_sections 1
78 #define elf_backend_can_refcount 1
79 #define elf_backend_rela_normal 1
80 #define elf_backend_dtrel_excludes_plt 1
81 #define elf_backend_default_execstack 0
83 #define bfd_elf64_mkobject ppc64_elf_mkobject
84 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
85 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
86 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
87 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
88 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
89 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
91 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
92 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
94 #define elf_backend_object_p ppc64_elf_object_p
95 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
96 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
97 #define elf_backend_write_core_note ppc64_elf_write_core_note
98 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
99 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
100 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
101 #define elf_backend_check_directives ppc64_elf_before_check_relocs
102 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
103 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
104 #define elf_backend_check_relocs ppc64_elf_check_relocs
105 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
106 #define elf_backend_gc_keep ppc64_elf_gc_keep
107 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
108 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
109 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
110 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
111 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
112 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
113 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
114 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
115 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
116 #define elf_backend_action_discarded ppc64_elf_action_discarded
117 #define elf_backend_relocate_section ppc64_elf_relocate_section
118 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
119 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
120 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
121 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
122 #define elf_backend_special_sections ppc64_elf_special_sections
123 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
124 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
125 #define elf_backend_get_reloc_section bfd_get_section_by_name
127 /* The name of the dynamic interpreter. This is put in the .interp
129 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
131 /* The size in bytes of an entry in the procedure linkage table. */
132 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
133 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
135 /* The initial size of the plt reserved for the dynamic linker. */
136 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
138 /* Offsets to some stack save slots. */
140 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
141 /* This one is dodgy. ELFv2 does not have a linker word, so use the
142 CR save slot. Used only by optimised __tls_get_addr call stub,
143 relying on __tls_get_addr_opt not saving CR.. */
144 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
146 /* TOC base pointers offset from start of TOC. */
147 #define TOC_BASE_OFF 0x8000
148 /* TOC base alignment. */
149 #define TOC_BASE_ALIGN 256
151 /* Offset of tp and dtp pointers from start of TLS block. */
152 #define TP_OFFSET 0x7000
153 #define DTP_OFFSET 0x8000
155 /* .plt call stub instructions. The normal stub is like this, but
156 sometimes the .plt entry crosses a 64k boundary and we need to
157 insert an addi to adjust r11. */
158 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
159 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
160 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
161 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
162 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
163 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
164 #define BCTR 0x4e800420 /* bctr */
166 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
167 #define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
168 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
169 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
170 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
172 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
173 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
174 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
175 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
176 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
177 #define BNECTR 0x4ca20420 /* bnectr+ */
178 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
180 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
181 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
182 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
184 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
185 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
186 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
188 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
189 #define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
190 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
191 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
192 #define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
193 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
194 #define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
195 #define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
196 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
197 #define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
198 #define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
199 #define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
201 /* __glink_PLTresolve stub instructions. We enter with the index in R0. */
202 #define GLINK_PLTRESOLVE_SIZE(htab) \
203 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
207 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
208 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
210 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
211 /* ld %2,(0b-1b)(%11) */
212 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
213 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
219 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
220 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
221 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
222 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
223 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
226 #define NOP 0x60000000
228 /* Some other nops. */
229 #define CROR_151515 0x4def7b82
230 #define CROR_313131 0x4ffffb82
232 /* .glink entries for the first 32k functions are two instructions. */
233 #define LI_R0_0 0x38000000 /* li %r0,0 */
234 #define B_DOT 0x48000000 /* b . */
236 /* After that, we need two instructions to load the index, followed by
238 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
239 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
241 /* Instructions used by the save and restore reg functions. */
242 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
243 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
244 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
245 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
246 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
247 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
248 #define LI_R12_0 0x39800000 /* li %r12,0 */
249 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
250 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
251 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
252 #define BLR 0x4e800020 /* blr */
254 /* Since .opd is an array of descriptors and each entry will end up
255 with identical R_PPC64_RELATIVE relocs, there is really no need to
256 propagate .opd relocs; The dynamic linker should be taught to
257 relocate .opd without reloc entries. */
258 #ifndef NO_OPD_RELOCS
259 #define NO_OPD_RELOCS 0
263 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
267 abiversion (bfd *abfd)
269 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
273 set_abiversion (bfd *abfd, int ver)
275 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
276 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
279 /* Relocation HOWTO's. */
280 /* Like other ELF RELA targets that don't apply multiple
281 field-altering relocations to the same localation, src_mask is
282 always zero and pcrel_offset is the same as pc_relative.
283 PowerPC can always use a zero bitpos, even when the field is not at
284 the LSB. For example, a REL24 could use rightshift=2, bisize=24
285 and bitpos=2 which matches the ABI description, or as we do here,
286 rightshift=0, bitsize=26 and bitpos=0. */
287 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
288 complain, special_func) \
289 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
290 complain_overflow_ ## complain, special_func, \
291 #type, FALSE, 0, mask, pc_relative)
293 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
295 static reloc_howto_type ppc64_elf_howto_raw[] =
297 /* This reloc does nothing. */
298 HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
299 bfd_elf_generic_reloc),
301 /* A standard 32 bit relocation. */
302 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
303 bfd_elf_generic_reloc),
305 /* An absolute 26 bit branch; the lower two bits must be zero.
306 FIXME: we don't check that, we just clear them. */
307 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
308 bfd_elf_generic_reloc),
310 /* A standard 16 bit relocation. */
311 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
312 bfd_elf_generic_reloc),
314 /* A 16 bit relocation without overflow. */
315 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
316 bfd_elf_generic_reloc),
318 /* Bits 16-31 of an address. */
319 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
320 bfd_elf_generic_reloc),
322 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
323 bits, treated as a signed number, is negative. */
324 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
327 /* An absolute 16 bit branch; the lower two bits must be zero.
328 FIXME: we don't check that, we just clear them. */
329 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
330 ppc64_elf_branch_reloc),
332 /* An absolute 16 bit branch, for which bit 10 should be set to
333 indicate that the branch is expected to be taken. The lower two
334 bits must be zero. */
335 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
336 ppc64_elf_brtaken_reloc),
338 /* An absolute 16 bit branch, for which bit 10 should be set to
339 indicate that the branch is not expected to be taken. The lower
340 two bits must be zero. */
341 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
342 ppc64_elf_brtaken_reloc),
344 /* A relative 26 bit branch; the lower two bits must be zero. */
345 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
346 ppc64_elf_branch_reloc),
348 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
349 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
350 ppc64_elf_branch_reloc),
352 /* A relative 16 bit branch; the lower two bits must be zero. */
353 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
354 ppc64_elf_branch_reloc),
356 /* A relative 16 bit branch. Bit 10 should be set to indicate that
357 the branch is expected to be taken. The lower two bits must be
359 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
360 ppc64_elf_brtaken_reloc),
362 /* A relative 16 bit branch. Bit 10 should be set to indicate that
363 the branch is not expected to be taken. The lower two bits must
365 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
366 ppc64_elf_brtaken_reloc),
368 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
370 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
371 ppc64_elf_unhandled_reloc),
373 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
375 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
376 ppc64_elf_unhandled_reloc),
378 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
380 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
381 ppc64_elf_unhandled_reloc),
383 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
385 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
386 ppc64_elf_unhandled_reloc),
388 /* This is used only by the dynamic linker. The symbol should exist
389 both in the object being run and in some shared library. The
390 dynamic linker copies the data addressed by the symbol from the
391 shared library into the object, because the object being
392 run has to have the data at some particular address. */
393 HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
394 ppc64_elf_unhandled_reloc),
396 /* Like R_PPC64_ADDR64, but used when setting global offset table
398 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
399 ppc64_elf_unhandled_reloc),
401 /* Created by the link editor. Marks a procedure linkage table
402 entry for a symbol. */
403 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
404 ppc64_elf_unhandled_reloc),
406 /* Used only by the dynamic linker. When the object is run, this
407 doubleword64 is set to the load address of the object, plus the
409 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
410 bfd_elf_generic_reloc),
412 /* Like R_PPC64_ADDR32, but may be unaligned. */
413 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
414 bfd_elf_generic_reloc),
416 /* Like R_PPC64_ADDR16, but may be unaligned. */
417 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
418 bfd_elf_generic_reloc),
420 /* 32-bit PC relative. */
421 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
422 bfd_elf_generic_reloc),
424 /* 32-bit relocation to the symbol's procedure linkage table. */
425 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
426 ppc64_elf_unhandled_reloc),
428 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
429 FIXME: R_PPC64_PLTREL32 not supported. */
430 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
431 ppc64_elf_unhandled_reloc),
433 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
435 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
436 ppc64_elf_unhandled_reloc),
438 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
440 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
441 ppc64_elf_unhandled_reloc),
443 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
445 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
446 ppc64_elf_unhandled_reloc),
448 /* 16-bit section relative relocation. */
449 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
450 ppc64_elf_sectoff_reloc),
452 /* Like R_PPC64_SECTOFF, but no overflow warning. */
453 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
454 ppc64_elf_sectoff_reloc),
456 /* 16-bit upper half section relative relocation. */
457 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
458 ppc64_elf_sectoff_reloc),
460 /* 16-bit upper half adjusted section relative relocation. */
461 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
462 ppc64_elf_sectoff_ha_reloc),
464 /* Like R_PPC64_REL24 without touching the two least significant bits. */
465 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
466 bfd_elf_generic_reloc),
468 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
470 /* A standard 64-bit relocation. */
471 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
472 bfd_elf_generic_reloc),
474 /* The bits 32-47 of an address. */
475 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
476 bfd_elf_generic_reloc),
478 /* The bits 32-47 of an address, plus 1 if the contents of the low
479 16 bits, treated as a signed number, is negative. */
480 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
483 /* The bits 48-63 of an address. */
484 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
485 bfd_elf_generic_reloc),
487 /* The bits 48-63 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_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
492 /* Like ADDR64, but may be unaligned. */
493 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
494 bfd_elf_generic_reloc),
496 /* 64-bit relative relocation. */
497 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
498 bfd_elf_generic_reloc),
500 /* 64-bit relocation to the symbol's procedure linkage table. */
501 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
502 ppc64_elf_unhandled_reloc),
504 /* 64-bit PC relative relocation to the symbol's procedure linkage
506 /* FIXME: R_PPC64_PLTREL64 not supported. */
507 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
508 ppc64_elf_unhandled_reloc),
510 /* 16 bit TOC-relative relocation. */
511 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
512 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
513 ppc64_elf_toc_reloc),
515 /* 16 bit TOC-relative relocation without overflow. */
516 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
517 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
518 ppc64_elf_toc_reloc),
520 /* 16 bit TOC-relative relocation, high 16 bits. */
521 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
522 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
523 ppc64_elf_toc_reloc),
525 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
526 contents of the low 16 bits, treated as a signed number, is
528 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
529 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
530 ppc64_elf_toc_ha_reloc),
532 /* 64-bit relocation; insert value of TOC base (.TOC.). */
533 /* R_PPC64_TOC 51 doubleword64 .TOC. */
534 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
535 ppc64_elf_toc64_reloc),
537 /* Like R_PPC64_GOT16, but also informs the link editor that the
538 value to relocate may (!) refer to a PLT entry which the link
539 editor (a) may replace with the symbol value. If the link editor
540 is unable to fully resolve the symbol, it may (b) create a PLT
541 entry and store the address to the new PLT entry in the GOT.
542 This permits lazy resolution of function symbols at run time.
543 The link editor may also skip all of this and just (c) emit a
544 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
545 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
546 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
547 ppc64_elf_unhandled_reloc),
549 /* Like R_PPC64_PLTGOT16, but without overflow. */
550 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
551 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
552 ppc64_elf_unhandled_reloc),
554 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
555 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
556 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
557 ppc64_elf_unhandled_reloc),
559 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
560 1 if the contents of the low 16 bits, treated as a signed number,
562 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
563 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
564 ppc64_elf_unhandled_reloc),
566 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
567 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
568 bfd_elf_generic_reloc),
570 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
571 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
572 bfd_elf_generic_reloc),
574 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
575 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
576 ppc64_elf_unhandled_reloc),
578 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
579 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
580 ppc64_elf_unhandled_reloc),
582 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
583 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
584 ppc64_elf_unhandled_reloc),
586 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
587 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
588 ppc64_elf_sectoff_reloc),
590 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
591 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
592 ppc64_elf_sectoff_reloc),
594 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
595 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
596 ppc64_elf_toc_reloc),
598 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
599 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
600 ppc64_elf_toc_reloc),
602 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
603 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
604 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
605 ppc64_elf_unhandled_reloc),
607 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
608 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
609 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
610 ppc64_elf_unhandled_reloc),
612 /* Marker relocs for TLS. */
613 HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
614 bfd_elf_generic_reloc),
616 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
617 bfd_elf_generic_reloc),
619 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
620 bfd_elf_generic_reloc),
622 /* Marker reloc for optimizing r2 save in prologue rather than on
623 each plt call stub. */
624 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
625 bfd_elf_generic_reloc),
627 /* Marker relocs on inline plt call instructions. */
628 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
629 bfd_elf_generic_reloc),
631 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
632 bfd_elf_generic_reloc),
634 /* Computes the load module index of the load module that contains the
635 definition of its TLS sym. */
636 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
637 ppc64_elf_unhandled_reloc),
639 /* Computes a dtv-relative displacement, the difference between the value
640 of sym+add and the base address of the thread-local storage block that
641 contains the definition of sym, minus 0x8000. */
642 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
643 ppc64_elf_unhandled_reloc),
645 /* A 16 bit dtprel reloc. */
646 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
647 ppc64_elf_unhandled_reloc),
649 /* Like DTPREL16, but no overflow. */
650 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
651 ppc64_elf_unhandled_reloc),
653 /* Like DTPREL16_LO, but next higher group of 16 bits. */
654 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
655 ppc64_elf_unhandled_reloc),
657 /* Like DTPREL16_HI, but adjust for low 16 bits. */
658 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
659 ppc64_elf_unhandled_reloc),
661 /* Like DTPREL16_HI, but next higher group of 16 bits. */
662 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
663 ppc64_elf_unhandled_reloc),
665 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
666 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
667 ppc64_elf_unhandled_reloc),
669 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
670 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
671 ppc64_elf_unhandled_reloc),
673 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
674 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
675 ppc64_elf_unhandled_reloc),
677 /* Like DTPREL16, but for insns with a DS field. */
678 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
679 ppc64_elf_unhandled_reloc),
681 /* Like DTPREL16_DS, but no overflow. */
682 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
683 ppc64_elf_unhandled_reloc),
685 /* Computes a tp-relative displacement, the difference between the value of
686 sym+add and the value of the thread pointer (r13). */
687 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
688 ppc64_elf_unhandled_reloc),
690 /* A 16 bit tprel reloc. */
691 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
692 ppc64_elf_unhandled_reloc),
694 /* Like TPREL16, but no overflow. */
695 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
696 ppc64_elf_unhandled_reloc),
698 /* Like TPREL16_LO, but next higher group of 16 bits. */
699 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
700 ppc64_elf_unhandled_reloc),
702 /* Like TPREL16_HI, but adjust for low 16 bits. */
703 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
704 ppc64_elf_unhandled_reloc),
706 /* Like TPREL16_HI, but next higher group of 16 bits. */
707 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
708 ppc64_elf_unhandled_reloc),
710 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
711 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
712 ppc64_elf_unhandled_reloc),
714 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
715 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
716 ppc64_elf_unhandled_reloc),
718 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
719 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
720 ppc64_elf_unhandled_reloc),
722 /* Like TPREL16, but for insns with a DS field. */
723 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
724 ppc64_elf_unhandled_reloc),
726 /* Like TPREL16_DS, but no overflow. */
727 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
728 ppc64_elf_unhandled_reloc),
730 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
731 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
732 to the first entry relative to the TOC base (r2). */
733 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
734 ppc64_elf_unhandled_reloc),
736 /* Like GOT_TLSGD16, but no overflow. */
737 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
738 ppc64_elf_unhandled_reloc),
740 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
741 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
742 ppc64_elf_unhandled_reloc),
744 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
745 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
746 ppc64_elf_unhandled_reloc),
748 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
749 with values (sym+add)@dtpmod and zero, and computes the offset to the
750 first entry relative to the TOC base (r2). */
751 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
752 ppc64_elf_unhandled_reloc),
754 /* Like GOT_TLSLD16, but no overflow. */
755 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
756 ppc64_elf_unhandled_reloc),
758 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
759 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
760 ppc64_elf_unhandled_reloc),
762 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
763 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
764 ppc64_elf_unhandled_reloc),
766 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
767 the offset to the entry relative to the TOC base (r2). */
768 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
769 ppc64_elf_unhandled_reloc),
771 /* Like GOT_DTPREL16_DS, but no overflow. */
772 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
773 ppc64_elf_unhandled_reloc),
775 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
776 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
777 ppc64_elf_unhandled_reloc),
779 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
780 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
781 ppc64_elf_unhandled_reloc),
783 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
784 offset to the entry relative to the TOC base (r2). */
785 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
786 ppc64_elf_unhandled_reloc),
788 /* Like GOT_TPREL16_DS, but no overflow. */
789 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
790 ppc64_elf_unhandled_reloc),
792 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
793 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
794 ppc64_elf_unhandled_reloc),
796 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
797 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
798 ppc64_elf_unhandled_reloc),
800 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
801 ppc64_elf_unhandled_reloc),
803 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
804 bfd_elf_generic_reloc),
806 /* A 16 bit relative relocation. */
807 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
808 bfd_elf_generic_reloc),
810 /* A 16 bit relative relocation without overflow. */
811 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
812 bfd_elf_generic_reloc),
814 /* The high order 16 bits of a relative address. */
815 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
816 bfd_elf_generic_reloc),
818 /* The high order 16 bits of a relative address, plus 1 if the contents of
819 the low 16 bits, treated as a signed number, is negative. */
820 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
823 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
824 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
827 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
828 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
831 /* Like R_PPC64_ADDR16_HI, but no overflow. */
832 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
833 bfd_elf_generic_reloc),
835 /* Like R_PPC64_ADDR16_HA, but no overflow. */
836 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
839 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
840 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
841 ppc64_elf_unhandled_reloc),
843 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
844 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
845 ppc64_elf_unhandled_reloc),
847 /* Like R_PPC64_TPREL16_HI, but no overflow. */
848 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
849 ppc64_elf_unhandled_reloc),
851 /* Like R_PPC64_TPREL16_HA, but no overflow. */
852 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
853 ppc64_elf_unhandled_reloc),
855 /* Marker reloc on ELFv2 large-model function entry. */
856 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
857 bfd_elf_generic_reloc),
859 /* Like ADDR64, but use local entry point of function. */
860 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
861 bfd_elf_generic_reloc),
863 /* GNU extension to record C++ vtable hierarchy. */
864 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
867 /* GNU extension to record C++ vtable member usage. */
868 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
873 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
877 ppc_howto_init (void)
879 unsigned int i, type;
881 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
883 type = ppc64_elf_howto_raw[i].type;
884 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
885 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
889 static reloc_howto_type *
890 ppc64_elf_reloc_type_lookup (bfd *abfd,
891 bfd_reloc_code_real_type code)
893 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
895 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
896 /* Initialize howto table if needed. */
902 /* xgettext:c-format */
903 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
905 bfd_set_error (bfd_error_bad_value);
908 case BFD_RELOC_NONE: r = R_PPC64_NONE;
910 case BFD_RELOC_32: r = R_PPC64_ADDR32;
912 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
914 case BFD_RELOC_16: r = R_PPC64_ADDR16;
916 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
918 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
920 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
922 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
924 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
926 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
928 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
930 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
932 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
934 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
936 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
938 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
940 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
942 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
944 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
946 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
948 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
950 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
952 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
954 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
956 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
958 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
960 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
962 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
964 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
966 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
968 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
970 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
972 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
974 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
976 case BFD_RELOC_64: r = R_PPC64_ADDR64;
978 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
980 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
982 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
984 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
986 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
988 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
990 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
992 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
994 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
996 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
998 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1000 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1002 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1004 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1006 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1008 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1010 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1012 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1014 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1016 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1018 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1020 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1022 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1024 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1026 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1028 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1030 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1032 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1034 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1036 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1038 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1040 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1042 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1044 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1046 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1048 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1050 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1052 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1054 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1056 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1058 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1060 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1062 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1064 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1066 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1068 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1070 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1072 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1074 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1076 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1078 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1080 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1082 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1084 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1086 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1088 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1090 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1092 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1094 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1096 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1098 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1100 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1102 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1104 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1106 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1108 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1110 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1112 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1114 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1116 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1118 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1120 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1122 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1124 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1126 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1128 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1130 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1132 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1134 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1136 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1138 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1140 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1142 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1146 return ppc64_elf_howto_table[r];
1149 static reloc_howto_type *
1150 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1155 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1156 if (ppc64_elf_howto_raw[i].name != NULL
1157 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1158 return &ppc64_elf_howto_raw[i];
1163 /* Set the howto pointer for a PowerPC ELF reloc. */
1166 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1167 Elf_Internal_Rela *dst)
1171 /* Initialize howto table if needed. */
1172 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1175 type = ELF64_R_TYPE (dst->r_info);
1176 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1178 /* xgettext:c-format */
1179 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1181 bfd_set_error (bfd_error_bad_value);
1184 cache_ptr->howto = ppc64_elf_howto_table[type];
1185 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1187 /* xgettext:c-format */
1188 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1190 bfd_set_error (bfd_error_bad_value);
1197 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1199 static bfd_reloc_status_type
1200 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1201 void *data, asection *input_section,
1202 bfd *output_bfd, char **error_message)
1204 enum elf_ppc64_reloc_type r_type;
1206 bfd_size_type octets;
1209 /* If this is a relocatable link (output_bfd test tells us), just
1210 call the generic function. Any adjustment will be done at final
1212 if (output_bfd != NULL)
1213 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1214 input_section, output_bfd, error_message);
1216 /* Adjust the addend for sign extension of the low 16 bits.
1217 We won't actually be using the low 16 bits, so trashing them
1219 reloc_entry->addend += 0x8000;
1220 r_type = reloc_entry->howto->type;
1221 if (r_type != R_PPC64_REL16DX_HA)
1222 return bfd_reloc_continue;
1225 if (!bfd_is_com_section (symbol->section))
1226 value = symbol->value;
1227 value += (reloc_entry->addend
1228 + symbol->section->output_offset
1229 + symbol->section->output_section->vma);
1230 value -= (reloc_entry->address
1231 + input_section->output_offset
1232 + input_section->output_section->vma);
1233 value = (bfd_signed_vma) value >> 16;
1235 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1236 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1238 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1239 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1240 if (value + 0x8000 > 0xffff)
1241 return bfd_reloc_overflow;
1242 return bfd_reloc_ok;
1245 static bfd_reloc_status_type
1246 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1247 void *data, asection *input_section,
1248 bfd *output_bfd, char **error_message)
1250 if (output_bfd != NULL)
1251 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1252 input_section, output_bfd, error_message);
1254 if (strcmp (symbol->section->name, ".opd") == 0
1255 && (symbol->section->owner->flags & DYNAMIC) == 0)
1257 bfd_vma dest = opd_entry_value (symbol->section,
1258 symbol->value + reloc_entry->addend,
1260 if (dest != (bfd_vma) -1)
1261 reloc_entry->addend = dest - (symbol->value
1262 + symbol->section->output_section->vma
1263 + symbol->section->output_offset);
1267 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1269 if (symbol->section->owner != abfd
1270 && symbol->section->owner != NULL
1271 && abiversion (symbol->section->owner) >= 2)
1275 for (i = 0; i < symbol->section->owner->symcount; ++i)
1277 asymbol *symdef = symbol->section->owner->outsymbols[i];
1279 if (strcmp (symdef->name, symbol->name) == 0)
1281 elfsym = (elf_symbol_type *) symdef;
1287 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1289 return bfd_reloc_continue;
1292 static bfd_reloc_status_type
1293 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1294 void *data, asection *input_section,
1295 bfd *output_bfd, char **error_message)
1298 enum elf_ppc64_reloc_type r_type;
1299 bfd_size_type octets;
1300 /* Assume 'at' branch hints. */
1301 bfd_boolean is_isa_v2 = TRUE;
1303 /* If this is a relocatable link (output_bfd test tells us), just
1304 call the generic function. Any adjustment will be done at final
1306 if (output_bfd != NULL)
1307 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1308 input_section, output_bfd, error_message);
1310 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1311 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1312 insn &= ~(0x01 << 21);
1313 r_type = reloc_entry->howto->type;
1314 if (r_type == R_PPC64_ADDR14_BRTAKEN
1315 || r_type == R_PPC64_REL14_BRTAKEN)
1316 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1320 /* Set 'a' bit. This is 0b00010 in BO field for branch
1321 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1322 for branch on CTR insns (BO == 1a00t or 1a01t). */
1323 if ((insn & (0x14 << 21)) == (0x04 << 21))
1325 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1335 if (!bfd_is_com_section (symbol->section))
1336 target = symbol->value;
1337 target += symbol->section->output_section->vma;
1338 target += symbol->section->output_offset;
1339 target += reloc_entry->addend;
1341 from = (reloc_entry->address
1342 + input_section->output_offset
1343 + input_section->output_section->vma);
1345 /* Invert 'y' bit if not the default. */
1346 if ((bfd_signed_vma) (target - from) < 0)
1349 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1351 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1352 input_section, output_bfd, error_message);
1355 static bfd_reloc_status_type
1356 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1357 void *data, asection *input_section,
1358 bfd *output_bfd, char **error_message)
1360 /* If this is a relocatable link (output_bfd test tells us), just
1361 call the generic function. Any adjustment will be done at final
1363 if (output_bfd != NULL)
1364 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1365 input_section, output_bfd, error_message);
1367 /* Subtract the symbol section base address. */
1368 reloc_entry->addend -= symbol->section->output_section->vma;
1369 return bfd_reloc_continue;
1372 static bfd_reloc_status_type
1373 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1374 void *data, asection *input_section,
1375 bfd *output_bfd, char **error_message)
1377 /* If this is a relocatable link (output_bfd test tells us), just
1378 call the generic function. Any adjustment will be done at final
1380 if (output_bfd != NULL)
1381 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1382 input_section, output_bfd, error_message);
1384 /* Subtract the symbol section base address. */
1385 reloc_entry->addend -= symbol->section->output_section->vma;
1387 /* Adjust the addend for sign extension of the low 16 bits. */
1388 reloc_entry->addend += 0x8000;
1389 return bfd_reloc_continue;
1392 static bfd_reloc_status_type
1393 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1394 void *data, asection *input_section,
1395 bfd *output_bfd, char **error_message)
1399 /* If this is a relocatable link (output_bfd test tells us), just
1400 call the generic function. Any adjustment will be done at final
1402 if (output_bfd != NULL)
1403 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1404 input_section, output_bfd, error_message);
1406 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1408 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1410 /* Subtract the TOC base address. */
1411 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1412 return bfd_reloc_continue;
1415 static bfd_reloc_status_type
1416 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1417 void *data, asection *input_section,
1418 bfd *output_bfd, char **error_message)
1422 /* If this is a relocatable link (output_bfd test tells us), just
1423 call the generic function. Any adjustment will be done at final
1425 if (output_bfd != NULL)
1426 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1427 input_section, output_bfd, error_message);
1429 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1431 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1433 /* Subtract the TOC base address. */
1434 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1436 /* Adjust the addend for sign extension of the low 16 bits. */
1437 reloc_entry->addend += 0x8000;
1438 return bfd_reloc_continue;
1441 static bfd_reloc_status_type
1442 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1443 void *data, asection *input_section,
1444 bfd *output_bfd, char **error_message)
1447 bfd_size_type octets;
1449 /* If this is a relocatable link (output_bfd test tells us), just
1450 call the generic function. Any adjustment will be done at final
1452 if (output_bfd != NULL)
1453 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1454 input_section, output_bfd, error_message);
1456 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1458 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1460 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1461 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1462 return bfd_reloc_ok;
1465 static bfd_reloc_status_type
1466 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1467 void *data, asection *input_section,
1468 bfd *output_bfd, char **error_message)
1470 /* If this is a relocatable link (output_bfd test tells us), just
1471 call the generic function. Any adjustment will be done at final
1473 if (output_bfd != NULL)
1474 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1475 input_section, output_bfd, error_message);
1477 if (error_message != NULL)
1479 static char buf[60];
1480 sprintf (buf, "generic linker can't handle %s",
1481 reloc_entry->howto->name);
1482 *error_message = buf;
1484 return bfd_reloc_dangerous;
1487 /* Track GOT entries needed for a given symbol. We might need more
1488 than one got entry per symbol. */
1491 struct got_entry *next;
1493 /* The symbol addend that we'll be placing in the GOT. */
1496 /* Unlike other ELF targets, we use separate GOT entries for the same
1497 symbol referenced from different input files. This is to support
1498 automatic multiple TOC/GOT sections, where the TOC base can vary
1499 from one input file to another. After partitioning into TOC groups
1500 we merge entries within the group.
1502 Point to the BFD owning this GOT entry. */
1505 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1506 TLS_TPREL or TLS_DTPREL for tls entries. */
1507 unsigned char tls_type;
1509 /* Non-zero if got.ent points to real entry. */
1510 unsigned char is_indirect;
1512 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1515 bfd_signed_vma refcount;
1517 struct got_entry *ent;
1521 /* The same for PLT. */
1524 struct plt_entry *next;
1530 bfd_signed_vma refcount;
1535 struct ppc64_elf_obj_tdata
1537 struct elf_obj_tdata elf;
1539 /* Shortcuts to dynamic linker sections. */
1543 /* Used during garbage collection. We attach global symbols defined
1544 on removed .opd entries to this section so that the sym is removed. */
1545 asection *deleted_section;
1547 /* TLS local dynamic got entry handling. Support for multiple GOT
1548 sections means we potentially need one of these for each input bfd. */
1549 struct got_entry tlsld_got;
1553 /* A copy of relocs before they are modified for --emit-relocs. */
1554 Elf_Internal_Rela *relocs;
1556 /* Section contents. */
1560 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1561 the reloc to be in the range -32768 to 32767. */
1562 unsigned int has_small_toc_reloc : 1;
1564 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1565 instruction not one we handle. */
1566 unsigned int unexpected_toc_insn : 1;
1569 #define ppc64_elf_tdata(bfd) \
1570 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1572 #define ppc64_tlsld_got(bfd) \
1573 (&ppc64_elf_tdata (bfd)->tlsld_got)
1575 #define is_ppc64_elf(bfd) \
1576 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1577 && elf_object_id (bfd) == PPC64_ELF_DATA)
1579 /* Override the generic function because we store some extras. */
1582 ppc64_elf_mkobject (bfd *abfd)
1584 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1588 /* Fix bad default arch selected for a 64 bit input bfd when the
1589 default is 32 bit. Also select arch based on apuinfo. */
1592 ppc64_elf_object_p (bfd *abfd)
1594 if (!abfd->arch_info->the_default)
1597 if (abfd->arch_info->bits_per_word == 32)
1599 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1601 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1603 /* Relies on arch after 32 bit default being 64 bit default. */
1604 abfd->arch_info = abfd->arch_info->next;
1605 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1608 return _bfd_elf_ppc_set_arch (abfd);
1611 /* Support for core dump NOTE sections. */
1614 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1616 size_t offset, size;
1618 if (note->descsz != 504)
1622 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1625 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1631 /* Make a ".reg/999" section. */
1632 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1633 size, note->descpos + offset);
1637 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1639 if (note->descsz != 136)
1642 elf_tdata (abfd)->core->pid
1643 = bfd_get_32 (abfd, note->descdata + 24);
1644 elf_tdata (abfd)->core->program
1645 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1646 elf_tdata (abfd)->core->command
1647 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1653 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1663 char data[136] ATTRIBUTE_NONSTRING;
1666 va_start (ap, note_type);
1667 memset (data, 0, sizeof (data));
1668 strncpy (data + 40, va_arg (ap, const char *), 16);
1669 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1671 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1672 -Wstringop-truncation:
1673 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1675 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1677 strncpy (data + 56, va_arg (ap, const char *), 80);
1678 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1682 return elfcore_write_note (abfd, buf, bufsiz,
1683 "CORE", note_type, data, sizeof (data));
1694 va_start (ap, note_type);
1695 memset (data, 0, 112);
1696 pid = va_arg (ap, long);
1697 bfd_put_32 (abfd, pid, data + 32);
1698 cursig = va_arg (ap, int);
1699 bfd_put_16 (abfd, cursig, data + 12);
1700 greg = va_arg (ap, const void *);
1701 memcpy (data + 112, greg, 384);
1702 memset (data + 496, 0, 8);
1704 return elfcore_write_note (abfd, buf, bufsiz,
1705 "CORE", note_type, data, sizeof (data));
1710 /* Add extra PPC sections. */
1712 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1714 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1715 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1716 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1717 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1718 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1719 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1720 { NULL, 0, 0, 0, 0 }
1723 enum _ppc64_sec_type {
1729 struct _ppc64_elf_section_data
1731 struct bfd_elf_section_data elf;
1735 /* An array with one entry for each opd function descriptor,
1736 and some spares since opd entries may be either 16 or 24 bytes. */
1737 #define OPD_NDX(OFF) ((OFF) >> 4)
1738 struct _opd_sec_data
1740 /* Points to the function code section for local opd entries. */
1741 asection **func_sec;
1743 /* After editing .opd, adjust references to opd local syms. */
1747 /* An array for toc sections, indexed by offset/8. */
1748 struct _toc_sec_data
1750 /* Specifies the relocation symbol index used at a given toc offset. */
1753 /* And the relocation addend. */
1758 enum _ppc64_sec_type sec_type:2;
1760 /* Flag set when small branches are detected. Used to
1761 select suitable defaults for the stub group size. */
1762 unsigned int has_14bit_branch:1;
1764 /* Flag set when PLTCALL relocs are detected. */
1765 unsigned int has_pltcall:1;
1768 #define ppc64_elf_section_data(sec) \
1769 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
1772 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
1774 if (!sec->used_by_bfd)
1776 struct _ppc64_elf_section_data *sdata;
1777 bfd_size_type amt = sizeof (*sdata);
1779 sdata = bfd_zalloc (abfd, amt);
1782 sec->used_by_bfd = sdata;
1785 return _bfd_elf_new_section_hook (abfd, sec);
1788 static struct _opd_sec_data *
1789 get_opd_info (asection * sec)
1792 && ppc64_elf_section_data (sec) != NULL
1793 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
1794 return &ppc64_elf_section_data (sec)->u.opd;
1798 /* Parameters for the qsort hook. */
1799 static bfd_boolean synthetic_relocatable;
1800 static asection *synthetic_opd;
1802 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
1805 compare_symbols (const void *ap, const void *bp)
1807 const asymbol *a = *(const asymbol **) ap;
1808 const asymbol *b = *(const asymbol **) bp;
1810 /* Section symbols first. */
1811 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
1813 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
1816 /* then .opd symbols. */
1817 if (synthetic_opd != NULL)
1819 if (strcmp (a->section->name, ".opd") == 0
1820 && strcmp (b->section->name, ".opd") != 0)
1822 if (strcmp (a->section->name, ".opd") != 0
1823 && strcmp (b->section->name, ".opd") == 0)
1827 /* then other code symbols. */
1828 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1829 == (SEC_CODE | SEC_ALLOC))
1830 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1831 != (SEC_CODE | SEC_ALLOC)))
1834 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1835 != (SEC_CODE | SEC_ALLOC))
1836 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1837 == (SEC_CODE | SEC_ALLOC)))
1840 if (synthetic_relocatable)
1842 if (a->section->id < b->section->id)
1845 if (a->section->id > b->section->id)
1849 if (a->value + a->section->vma < b->value + b->section->vma)
1852 if (a->value + a->section->vma > b->value + b->section->vma)
1855 /* For syms with the same value, prefer strong dynamic global function
1856 syms over other syms. */
1857 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
1860 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
1863 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
1866 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
1869 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
1872 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
1875 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
1878 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
1884 /* Search SYMS for a symbol of the given VALUE. */
1887 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
1891 if (id == (unsigned) -1)
1895 mid = (lo + hi) >> 1;
1896 if (syms[mid]->value + syms[mid]->section->vma < value)
1898 else if (syms[mid]->value + syms[mid]->section->vma > value)
1908 mid = (lo + hi) >> 1;
1909 if (syms[mid]->section->id < id)
1911 else if (syms[mid]->section->id > id)
1913 else if (syms[mid]->value < value)
1915 else if (syms[mid]->value > value)
1925 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
1927 bfd_vma vma = *(bfd_vma *) ptr;
1928 return ((section->flags & SEC_ALLOC) != 0
1929 && section->vma <= vma
1930 && vma < section->vma + section->size);
1933 /* Create synthetic symbols, effectively restoring "dot-symbol" function
1934 entry syms. Also generate @plt symbols for the glink branch table.
1935 Returns count of synthetic symbols in RET or -1 on error. */
1938 ppc64_elf_get_synthetic_symtab (bfd *abfd,
1939 long static_count, asymbol **static_syms,
1940 long dyn_count, asymbol **dyn_syms,
1946 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
1947 asection *opd = NULL;
1948 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
1950 int abi = abiversion (abfd);
1956 opd = bfd_get_section_by_name (abfd, ".opd");
1957 if (opd == NULL && abi == 1)
1969 symcount = static_count;
1971 symcount += dyn_count;
1975 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
1979 if (!relocatable && static_count != 0 && dyn_count != 0)
1981 /* Use both symbol tables. */
1982 memcpy (syms, static_syms, static_count * sizeof (*syms));
1983 memcpy (syms + static_count, dyn_syms,
1984 (dyn_count + 1) * sizeof (*syms));
1986 else if (!relocatable && static_count == 0)
1987 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
1989 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
1991 /* Trim uninteresting symbols. Interesting symbols are section,
1992 function, and notype symbols. */
1993 for (i = 0, j = 0; i < symcount; ++i)
1994 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
1995 | BSF_RELC | BSF_SRELC)) == 0)
1996 syms[j++] = syms[i];
1999 synthetic_relocatable = relocatable;
2000 synthetic_opd = opd;
2001 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2003 if (!relocatable && symcount > 1)
2005 /* Trim duplicate syms, since we may have merged the normal
2006 and dynamic symbols. Actually, we only care about syms
2007 that have different values, so trim any with the same
2008 value. Don't consider ifunc and ifunc resolver symbols
2009 duplicates however, because GDB wants to know whether a
2010 text symbol is an ifunc resolver. */
2011 for (i = 1, j = 1; i < symcount; ++i)
2013 const asymbol *s0 = syms[i - 1];
2014 const asymbol *s1 = syms[i];
2016 if ((s0->value + s0->section->vma
2017 != s1->value + s1->section->vma)
2018 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2019 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2020 syms[j++] = syms[i];
2026 /* Note that here and in compare_symbols we can't compare opd and
2027 sym->section directly. With separate debug info files, the
2028 symbols will be extracted from the debug file while abfd passed
2029 to this function is the real binary. */
2030 if (strcmp (syms[i]->section->name, ".opd") == 0)
2034 for (; i < symcount; ++i)
2035 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2036 | SEC_THREAD_LOCAL))
2037 != (SEC_CODE | SEC_ALLOC))
2038 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2042 for (; i < symcount; ++i)
2043 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2047 for (; i < symcount; ++i)
2048 if (strcmp (syms[i]->section->name, ".opd") != 0)
2052 for (; i < symcount; ++i)
2053 if (((syms[i]->section->flags
2054 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2055 != (SEC_CODE | SEC_ALLOC))
2063 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2068 if (opdsymend == secsymend)
2071 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2072 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2076 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2083 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2087 while (r < opd->relocation + relcount
2088 && r->address < syms[i]->value + opd->vma)
2091 if (r == opd->relocation + relcount)
2094 if (r->address != syms[i]->value + opd->vma)
2097 if (r->howto->type != R_PPC64_ADDR64)
2100 sym = *r->sym_ptr_ptr;
2101 if (!sym_exists_at (syms, opdsymend, symcount,
2102 sym->section->id, sym->value + r->addend))
2105 size += sizeof (asymbol);
2106 size += strlen (syms[i]->name) + 2;
2112 s = *ret = bfd_malloc (size);
2119 names = (char *) (s + count);
2121 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2125 while (r < opd->relocation + relcount
2126 && r->address < syms[i]->value + opd->vma)
2129 if (r == opd->relocation + relcount)
2132 if (r->address != syms[i]->value + opd->vma)
2135 if (r->howto->type != R_PPC64_ADDR64)
2138 sym = *r->sym_ptr_ptr;
2139 if (!sym_exists_at (syms, opdsymend, symcount,
2140 sym->section->id, sym->value + r->addend))
2145 s->flags |= BSF_SYNTHETIC;
2146 s->section = sym->section;
2147 s->value = sym->value + r->addend;
2150 len = strlen (syms[i]->name);
2151 memcpy (names, syms[i]->name, len + 1);
2153 /* Have udata.p point back to the original symbol this
2154 synthetic symbol was derived from. */
2155 s->udata.p = syms[i];
2162 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2163 bfd_byte *contents = NULL;
2165 size_t plt_count = 0;
2166 bfd_vma glink_vma = 0, resolv_vma = 0;
2167 asection *dynamic, *glink = NULL, *relplt = NULL;
2170 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2172 free_contents_and_exit_err:
2174 free_contents_and_exit:
2181 for (i = secsymend; i < opdsymend; ++i)
2185 /* Ignore bogus symbols. */
2186 if (syms[i]->value > opd->size - 8)
2189 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2190 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2193 size += sizeof (asymbol);
2194 size += strlen (syms[i]->name) + 2;
2198 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2200 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2202 bfd_byte *dynbuf, *extdyn, *extdynend;
2204 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2206 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2207 goto free_contents_and_exit_err;
2209 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2210 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2213 extdynend = extdyn + dynamic->size;
2214 for (; extdyn < extdynend; extdyn += extdynsize)
2216 Elf_Internal_Dyn dyn;
2217 (*swap_dyn_in) (abfd, extdyn, &dyn);
2219 if (dyn.d_tag == DT_NULL)
2222 if (dyn.d_tag == DT_PPC64_GLINK)
2224 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2225 See comment in ppc64_elf_finish_dynamic_sections. */
2226 glink_vma = dyn.d_un.d_val + 8 * 4;
2227 /* The .glink section usually does not survive the final
2228 link; search for the section (usually .text) where the
2229 glink stubs now reside. */
2230 glink = bfd_sections_find_if (abfd, section_covers_vma,
2241 /* Determine __glink trampoline by reading the relative branch
2242 from the first glink stub. */
2244 unsigned int off = 0;
2246 while (bfd_get_section_contents (abfd, glink, buf,
2247 glink_vma + off - glink->vma, 4))
2249 unsigned int insn = bfd_get_32 (abfd, buf);
2251 if ((insn & ~0x3fffffc) == 0)
2254 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2263 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2265 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2268 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2269 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
2270 goto free_contents_and_exit_err;
2272 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2273 size += plt_count * sizeof (asymbol);
2275 p = relplt->relocation;
2276 for (i = 0; i < plt_count; i++, p++)
2278 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2280 size += sizeof ("+0x") - 1 + 16;
2286 goto free_contents_and_exit;
2287 s = *ret = bfd_malloc (size);
2289 goto free_contents_and_exit_err;
2291 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2293 for (i = secsymend; i < opdsymend; ++i)
2297 if (syms[i]->value > opd->size - 8)
2300 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2301 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2305 asection *sec = abfd->sections;
2312 size_t mid = (lo + hi) >> 1;
2313 if (syms[mid]->section->vma < ent)
2315 else if (syms[mid]->section->vma > ent)
2319 sec = syms[mid]->section;
2324 if (lo >= hi && lo > codesecsym)
2325 sec = syms[lo - 1]->section;
2327 for (; sec != NULL; sec = sec->next)
2331 /* SEC_LOAD may not be set if SEC is from a separate debug
2333 if ((sec->flags & SEC_ALLOC) == 0)
2335 if ((sec->flags & SEC_CODE) != 0)
2338 s->flags |= BSF_SYNTHETIC;
2339 s->value = ent - s->section->vma;
2342 len = strlen (syms[i]->name);
2343 memcpy (names, syms[i]->name, len + 1);
2345 /* Have udata.p point back to the original symbol this
2346 synthetic symbol was derived from. */
2347 s->udata.p = syms[i];
2353 if (glink != NULL && relplt != NULL)
2357 /* Add a symbol for the main glink trampoline. */
2358 memset (s, 0, sizeof *s);
2360 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2362 s->value = resolv_vma - glink->vma;
2364 memcpy (names, "__glink_PLTresolve",
2365 sizeof ("__glink_PLTresolve"));
2366 names += sizeof ("__glink_PLTresolve");
2371 /* FIXME: It would be very much nicer to put sym@plt on the
2372 stub rather than on the glink branch table entry. The
2373 objdump disassembler would then use a sensible symbol
2374 name on plt calls. The difficulty in doing so is
2375 a) finding the stubs, and,
2376 b) matching stubs against plt entries, and,
2377 c) there can be multiple stubs for a given plt entry.
2379 Solving (a) could be done by code scanning, but older
2380 ppc64 binaries used different stubs to current code.
2381 (b) is the tricky one since you need to known the toc
2382 pointer for at least one function that uses a pic stub to
2383 be able to calculate the plt address referenced.
2384 (c) means gdb would need to set multiple breakpoints (or
2385 find the glink branch itself) when setting breakpoints
2386 for pending shared library loads. */
2387 p = relplt->relocation;
2388 for (i = 0; i < plt_count; i++, p++)
2392 *s = **p->sym_ptr_ptr;
2393 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2394 we are defining a symbol, ensure one of them is set. */
2395 if ((s->flags & BSF_LOCAL) == 0)
2396 s->flags |= BSF_GLOBAL;
2397 s->flags |= BSF_SYNTHETIC;
2399 s->value = glink_vma - glink->vma;
2402 len = strlen ((*p->sym_ptr_ptr)->name);
2403 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2407 memcpy (names, "+0x", sizeof ("+0x") - 1);
2408 names += sizeof ("+0x") - 1;
2409 bfd_sprintf_vma (abfd, names, p->addend);
2410 names += strlen (names);
2412 memcpy (names, "@plt", sizeof ("@plt"));
2413 names += sizeof ("@plt");
2433 /* The following functions are specific to the ELF linker, while
2434 functions above are used generally. Those named ppc64_elf_* are
2435 called by the main ELF linker code. They appear in this file more
2436 or less in the order in which they are called. eg.
2437 ppc64_elf_check_relocs is called early in the link process,
2438 ppc64_elf_finish_dynamic_sections is one of the last functions
2441 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2442 functions have both a function code symbol and a function descriptor
2443 symbol. A call to foo in a relocatable object file looks like:
2450 The function definition in another object file might be:
2454 . .quad .TOC.@tocbase
2460 When the linker resolves the call during a static link, the branch
2461 unsurprisingly just goes to .foo and the .opd information is unused.
2462 If the function definition is in a shared library, things are a little
2463 different: The call goes via a plt call stub, the opd information gets
2464 copied to the plt, and the linker patches the nop.
2472 . std 2,40(1) # in practice, the call stub
2473 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2474 . addi 11,11,Lfoo@toc@l # this is the general idea
2482 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2484 The "reloc ()" notation is supposed to indicate that the linker emits
2485 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2488 What are the difficulties here? Well, firstly, the relocations
2489 examined by the linker in check_relocs are against the function code
2490 sym .foo, while the dynamic relocation in the plt is emitted against
2491 the function descriptor symbol, foo. Somewhere along the line, we need
2492 to carefully copy dynamic link information from one symbol to the other.
2493 Secondly, the generic part of the elf linker will make .foo a dynamic
2494 symbol as is normal for most other backends. We need foo dynamic
2495 instead, at least for an application final link. However, when
2496 creating a shared library containing foo, we need to have both symbols
2497 dynamic so that references to .foo are satisfied during the early
2498 stages of linking. Otherwise the linker might decide to pull in a
2499 definition from some other object, eg. a static library.
2501 Update: As of August 2004, we support a new convention. Function
2502 calls may use the function descriptor symbol, ie. "bl foo". This
2503 behaves exactly as "bl .foo". */
2505 /* Of those relocs that might be copied as dynamic relocs, this
2506 function selects those that must be copied when linking a shared
2507 library or PIE, even when the symbol is local. */
2510 must_be_dyn_reloc (struct bfd_link_info *info,
2511 enum elf_ppc64_reloc_type r_type)
2516 /* Only relative relocs can be resolved when the object load
2517 address isn't fixed. DTPREL64 is excluded because the
2518 dynamic linker needs to differentiate global dynamic from
2519 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2527 case R_PPC64_TPREL16:
2528 case R_PPC64_TPREL16_LO:
2529 case R_PPC64_TPREL16_HI:
2530 case R_PPC64_TPREL16_HA:
2531 case R_PPC64_TPREL16_DS:
2532 case R_PPC64_TPREL16_LO_DS:
2533 case R_PPC64_TPREL16_HIGH:
2534 case R_PPC64_TPREL16_HIGHA:
2535 case R_PPC64_TPREL16_HIGHER:
2536 case R_PPC64_TPREL16_HIGHERA:
2537 case R_PPC64_TPREL16_HIGHEST:
2538 case R_PPC64_TPREL16_HIGHESTA:
2539 case R_PPC64_TPREL64:
2540 /* These relocations are relative but in a shared library the
2541 linker doesn't know the thread pointer base. */
2542 return bfd_link_dll (info);
2546 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2547 copying dynamic variables from a shared lib into an app's dynbss
2548 section, and instead use a dynamic relocation to point into the
2549 shared lib. With code that gcc generates, it's vital that this be
2550 enabled; In the PowerPC64 ABI, the address of a function is actually
2551 the address of a function descriptor, which resides in the .opd
2552 section. gcc uses the descriptor directly rather than going via the
2553 GOT as some other ABI's do, which means that initialized function
2554 pointers must reference the descriptor. Thus, a function pointer
2555 initialized to the address of a function in a shared library will
2556 either require a copy reloc, or a dynamic reloc. Using a copy reloc
2557 redefines the function descriptor symbol to point to the copy. This
2558 presents a problem as a plt entry for that function is also
2559 initialized from the function descriptor symbol and the copy reloc
2560 may not be initialized first. */
2561 #define ELIMINATE_COPY_RELOCS 1
2563 /* Section name for stubs is the associated section name plus this
2565 #define STUB_SUFFIX ".stub"
2568 ppc_stub_long_branch:
2569 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2570 destination, but a 24 bit branch in a stub section will reach.
2573 ppc_stub_plt_branch:
2574 Similar to the above, but a 24 bit branch in the stub section won't
2575 reach its destination.
2576 . addis %r11,%r2,xxx@toc@ha
2577 . ld %r12,xxx@toc@l(%r11)
2582 Used to call a function in a shared library. If it so happens that
2583 the plt entry referenced crosses a 64k boundary, then an extra
2584 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2585 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
2586 . addis %r11,%r2,xxx@toc@ha
2587 . ld %r12,xxx+0@toc@l(%r11)
2589 . ld %r2,xxx+8@toc@l(%r11)
2590 . ld %r11,xxx+16@toc@l(%r11)
2593 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2594 code to adjust the value and save r2 to support multiple toc sections.
2595 A ppc_stub_long_branch with an r2 offset looks like:
2597 . addis %r2,%r2,off@ha
2598 . addi %r2,%r2,off@l
2601 A ppc_stub_plt_branch with an r2 offset looks like:
2603 . addis %r11,%r2,xxx@toc@ha
2604 . ld %r12,xxx@toc@l(%r11)
2605 . addis %r2,%r2,off@ha
2606 . addi %r2,%r2,off@l
2610 All of the above stubs are shown as their ELFv1 variants. ELFv2
2611 variants exist too, simpler for plt calls since a new toc pointer
2612 and static chain are not loaded by the stub. In addition, ELFv2
2613 has some more complex stubs to handle calls marked with NOTOC
2614 relocs from functions where r2 is not a valid toc pointer. These
2615 come in two flavours, the ones shown below, and _both variants that
2616 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2617 one call is from a function where r2 is used as the toc pointer but
2618 needs a toc adjusting stub for small-model multi-toc, and another
2619 call is from a function where r2 is not valid.
2620 ppc_stub_long_branch_notoc:
2626 . addis %r12,%r11,dest-1b@ha
2627 . addi %r12,%r12,dest-1b@l
2630 ppc_stub_plt_branch_notoc:
2636 . lis %r12,xxx-1b@highest
2637 . ori %r12,xxx-1b@higher
2639 . oris %r12,%r12,xxx-1b@hi
2640 . ori %r12,%r12,xxx-1b@l
2641 . add %r12,%r11,%r12
2645 ppc_stub_plt_call_notoc:
2651 . lis %r12,xxx-1b@highest
2652 . ori %r12,xxx-1b@higher
2654 . oris %r12,%r12,xxx-1b@hi
2655 . ori %r12,%r12,xxx-1b@l
2656 . ldx %r12,%r11,%r12
2660 In cases where the high instructions would add zero, they are
2661 omitted and following instructions modified in some cases.
2663 For a given stub group (a set of sections all using the same toc
2664 pointer value) there will be just one stub type used for any
2665 particular function symbol. For example, if printf is called from
2666 code with the tocsave optimization (ie. r2 saved in function
2667 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2668 and from other code without the tocsave optimization requiring a
2669 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2670 type will be created. Calls with the tocsave optimization will
2671 enter this stub after the instruction saving r2. A similar
2672 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2673 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2674 to call an external function like printf. If other calls to printf
2675 require a ppc_stub_plt_call linkage stub then a single
2676 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2677 call. If other calls to printf require a ppc_stub_plt_call_r2save
2678 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2679 be created and calls not requiring r2 to be saved will enter the
2680 stub after the r2 save instruction. There is an analogous
2681 hierarchy of long branch and plt branch stubs for local call
2687 ppc_stub_long_branch,
2688 ppc_stub_long_branch_r2off,
2689 ppc_stub_long_branch_notoc,
2690 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
2691 ppc_stub_plt_branch,
2692 ppc_stub_plt_branch_r2off,
2693 ppc_stub_plt_branch_notoc,
2694 ppc_stub_plt_branch_both,
2696 ppc_stub_plt_call_r2save,
2697 ppc_stub_plt_call_notoc,
2698 ppc_stub_plt_call_both,
2699 ppc_stub_global_entry,
2703 /* Information on stub grouping. */
2706 /* The stub section. */
2708 /* This is the section to which stubs in the group will be attached. */
2711 struct map_stub *next;
2712 /* Whether to emit a copy of register save/restore functions in this
2715 /* Current offset within stubs after the insn restoring lr in a
2716 _notoc or _both stub using bcl for pc-relative addressing, or
2717 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2718 unsigned int lr_restore;
2719 /* Accumulated size of EH info emitted to describe return address
2720 if stubs modify lr. Does not include 17 byte FDE header. */
2721 unsigned int eh_size;
2722 /* Offset in glink_eh_frame to the start of EH info for this group. */
2723 unsigned int eh_base;
2726 struct ppc_stub_hash_entry
2728 /* Base hash table entry structure. */
2729 struct bfd_hash_entry root;
2731 enum ppc_stub_type stub_type;
2733 /* Group information. */
2734 struct map_stub *group;
2736 /* Offset within stub_sec of the beginning of this stub. */
2737 bfd_vma stub_offset;
2739 /* Given the symbol's value and its section we can determine its final
2740 value when building the stubs (so the stub knows where to jump. */
2741 bfd_vma target_value;
2742 asection *target_section;
2744 /* The symbol table entry, if any, that this was derived from. */
2745 struct ppc_link_hash_entry *h;
2746 struct plt_entry *plt_ent;
2749 unsigned char symtype;
2751 /* Symbol st_other. */
2752 unsigned char other;
2755 struct ppc_branch_hash_entry
2757 /* Base hash table entry structure. */
2758 struct bfd_hash_entry root;
2760 /* Offset within branch lookup table. */
2761 unsigned int offset;
2763 /* Generation marker. */
2767 /* Used to track dynamic relocations for local symbols. */
2768 struct ppc_dyn_relocs
2770 struct ppc_dyn_relocs *next;
2772 /* The input section of the reloc. */
2775 /* Total number of relocs copied for the input section. */
2776 unsigned int count : 31;
2778 /* Whether this entry is for STT_GNU_IFUNC symbols. */
2779 unsigned int ifunc : 1;
2782 struct ppc_link_hash_entry
2784 struct elf_link_hash_entry elf;
2788 /* A pointer to the most recently used stub hash entry against this
2790 struct ppc_stub_hash_entry *stub_cache;
2792 /* A pointer to the next symbol starting with a '.' */
2793 struct ppc_link_hash_entry *next_dot_sym;
2796 /* Track dynamic relocs copied for this symbol. */
2797 struct elf_dyn_relocs *dyn_relocs;
2799 /* Link between function code and descriptor symbols. */
2800 struct ppc_link_hash_entry *oh;
2802 /* Flag function code and descriptor symbols. */
2803 unsigned int is_func:1;
2804 unsigned int is_func_descriptor:1;
2805 unsigned int fake:1;
2807 /* Whether global opd/toc sym has been adjusted or not.
2808 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
2809 should be set for all globals defined in any opd/toc section. */
2810 unsigned int adjust_done:1;
2812 /* Set if this is an out-of-line register save/restore function,
2813 with non-standard calling convention. */
2814 unsigned int save_res:1;
2816 /* Set if a duplicate symbol with non-zero localentry is detected,
2817 even when the duplicate symbol does not provide a definition. */
2818 unsigned int non_zero_localentry:1;
2820 /* Contexts in which symbol is used in the GOT (or TOC).
2821 Bits are or'd into the mask as the corresponding relocs are
2822 encountered during check_relocs, with TLS_TLS being set when any
2823 of the other TLS bits are set. tls_optimize clears bits when
2824 optimizing to indicate the corresponding GOT entry type is not
2825 needed. If set, TLS_TLS is never cleared. tls_optimize may also
2826 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
2827 separate flag rather than setting TPREL just for convenience in
2828 distinguishing the two cases.
2829 These flags are also kept for local symbols. */
2830 #define TLS_TLS 1 /* Any TLS reloc. */
2831 #define TLS_GD 2 /* GD reloc. */
2832 #define TLS_LD 4 /* LD reloc. */
2833 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
2834 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
2835 #define TLS_MARK 32 /* __tls_get_addr call marked. */
2836 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
2837 #define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
2838 unsigned char tls_mask;
2840 /* The above field is also used to mark function symbols. In which
2841 case TLS_TLS will be 0. */
2842 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2843 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
2844 #define NON_GOT 256 /* local symbol plt, not stored. */
2847 /* ppc64 ELF linker hash table. */
2849 struct ppc_link_hash_table
2851 struct elf_link_hash_table elf;
2853 /* The stub hash table. */
2854 struct bfd_hash_table stub_hash_table;
2856 /* Another hash table for plt_branch stubs. */
2857 struct bfd_hash_table branch_hash_table;
2859 /* Hash table for function prologue tocsave. */
2860 htab_t tocsave_htab;
2862 /* Various options and other info passed from the linker. */
2863 struct ppc64_elf_params *params;
2865 /* The size of sec_info below. */
2866 unsigned int sec_info_arr_size;
2868 /* Per-section array of extra section info. Done this way rather
2869 than as part of ppc64_elf_section_data so we have the info for
2870 non-ppc64 sections. */
2873 /* Along with elf_gp, specifies the TOC pointer used by this section. */
2878 /* The section group that this section belongs to. */
2879 struct map_stub *group;
2880 /* A temp section list pointer. */
2885 /* Linked list of groups. */
2886 struct map_stub *group;
2888 /* Temp used when calculating TOC pointers. */
2891 asection *toc_first_sec;
2893 /* Used when adding symbols. */
2894 struct ppc_link_hash_entry *dot_syms;
2896 /* Shortcuts to get to dynamic linker sections. */
2898 asection *global_entry;
2901 asection *relpltlocal;
2904 asection *glink_eh_frame;
2906 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
2907 struct ppc_link_hash_entry *tls_get_addr;
2908 struct ppc_link_hash_entry *tls_get_addr_fd;
2910 /* The size of reliplt used by got entry relocs. */
2911 bfd_size_type got_reli_size;
2914 unsigned long stub_count[ppc_stub_global_entry];
2916 /* Number of stubs against global syms. */
2917 unsigned long stub_globals;
2919 /* Set if we're linking code with function descriptors. */
2920 unsigned int opd_abi:1;
2922 /* Support for multiple toc sections. */
2923 unsigned int do_multi_toc:1;
2924 unsigned int multi_toc_needed:1;
2925 unsigned int second_toc_pass:1;
2926 unsigned int do_toc_opt:1;
2928 /* Set if tls optimization is enabled. */
2929 unsigned int do_tls_opt:1;
2931 /* Set if inline plt calls should be converted to direct calls. */
2932 unsigned int can_convert_all_inline_plt:1;
2935 unsigned int stub_error:1;
2937 /* Whether func_desc_adjust needs to be run over symbols. */
2938 unsigned int need_func_desc_adj:1;
2940 /* Whether there exist local gnu indirect function resolvers,
2941 referenced by dynamic relocations. */
2942 unsigned int local_ifunc_resolver:1;
2943 unsigned int maybe_local_ifunc_resolver:1;
2945 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
2946 unsigned int has_plt_localentry0:1;
2948 /* Incremented every time we size stubs. */
2949 unsigned int stub_iteration;
2951 /* Small local sym cache. */
2952 struct sym_cache sym_cache;
2955 /* Rename some of the generic section flags to better document how they
2958 /* Nonzero if this section has TLS related relocations. */
2959 #define has_tls_reloc sec_flg0
2961 /* Nonzero if this section has an old-style call to __tls_get_addr. */
2962 #define has_tls_get_addr_call sec_flg1
2964 /* Nonzero if this section has any toc or got relocs. */
2965 #define has_toc_reloc sec_flg2
2967 /* Nonzero if this section has a call to another section that uses
2969 #define makes_toc_func_call sec_flg3
2971 /* Recursion protection when determining above flag. */
2972 #define call_check_in_progress sec_flg4
2973 #define call_check_done sec_flg5
2975 /* Get the ppc64 ELF linker hash table from a link_info structure. */
2977 #define ppc_hash_table(p) \
2978 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2979 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
2981 #define ppc_stub_hash_lookup(table, string, create, copy) \
2982 ((struct ppc_stub_hash_entry *) \
2983 bfd_hash_lookup ((table), (string), (create), (copy)))
2985 #define ppc_branch_hash_lookup(table, string, create, copy) \
2986 ((struct ppc_branch_hash_entry *) \
2987 bfd_hash_lookup ((table), (string), (create), (copy)))
2989 /* Create an entry in the stub hash table. */
2991 static struct bfd_hash_entry *
2992 stub_hash_newfunc (struct bfd_hash_entry *entry,
2993 struct bfd_hash_table *table,
2996 /* Allocate the structure if it has not already been allocated by a
3000 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3005 /* Call the allocation method of the superclass. */
3006 entry = bfd_hash_newfunc (entry, table, string);
3009 struct ppc_stub_hash_entry *eh;
3011 /* Initialize the local fields. */
3012 eh = (struct ppc_stub_hash_entry *) entry;
3013 eh->stub_type = ppc_stub_none;
3015 eh->stub_offset = 0;
3016 eh->target_value = 0;
3017 eh->target_section = NULL;
3026 /* Create an entry in the branch hash table. */
3028 static struct bfd_hash_entry *
3029 branch_hash_newfunc (struct bfd_hash_entry *entry,
3030 struct bfd_hash_table *table,
3033 /* Allocate the structure if it has not already been allocated by a
3037 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3042 /* Call the allocation method of the superclass. */
3043 entry = bfd_hash_newfunc (entry, table, string);
3046 struct ppc_branch_hash_entry *eh;
3048 /* Initialize the local fields. */
3049 eh = (struct ppc_branch_hash_entry *) entry;
3057 /* Create an entry in a ppc64 ELF linker hash table. */
3059 static struct bfd_hash_entry *
3060 link_hash_newfunc (struct bfd_hash_entry *entry,
3061 struct bfd_hash_table *table,
3064 /* Allocate the structure if it has not already been allocated by a
3068 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3073 /* Call the allocation method of the superclass. */
3074 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3077 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3079 memset (&eh->u.stub_cache, 0,
3080 (sizeof (struct ppc_link_hash_entry)
3081 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3083 /* When making function calls, old ABI code references function entry
3084 points (dot symbols), while new ABI code references the function
3085 descriptor symbol. We need to make any combination of reference and
3086 definition work together, without breaking archive linking.
3088 For a defined function "foo" and an undefined call to "bar":
3089 An old object defines "foo" and ".foo", references ".bar" (possibly
3091 A new object defines "foo" and references "bar".
3093 A new object thus has no problem with its undefined symbols being
3094 satisfied by definitions in an old object. On the other hand, the
3095 old object won't have ".bar" satisfied by a new object.
3097 Keep a list of newly added dot-symbols. */
3099 if (string[0] == '.')
3101 struct ppc_link_hash_table *htab;
3103 htab = (struct ppc_link_hash_table *) table;
3104 eh->u.next_dot_sym = htab->dot_syms;
3105 htab->dot_syms = eh;
3112 struct tocsave_entry
3119 tocsave_htab_hash (const void *p)
3121 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3122 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3126 tocsave_htab_eq (const void *p1, const void *p2)
3128 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3129 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3130 return e1->sec == e2->sec && e1->offset == e2->offset;
3133 /* Destroy a ppc64 ELF linker hash table. */
3136 ppc64_elf_link_hash_table_free (bfd *obfd)
3138 struct ppc_link_hash_table *htab;
3140 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3141 if (htab->tocsave_htab)
3142 htab_delete (htab->tocsave_htab);
3143 bfd_hash_table_free (&htab->branch_hash_table);
3144 bfd_hash_table_free (&htab->stub_hash_table);
3145 _bfd_elf_link_hash_table_free (obfd);
3148 /* Create a ppc64 ELF linker hash table. */
3150 static struct bfd_link_hash_table *
3151 ppc64_elf_link_hash_table_create (bfd *abfd)
3153 struct ppc_link_hash_table *htab;
3154 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3156 htab = bfd_zmalloc (amt);
3160 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3161 sizeof (struct ppc_link_hash_entry),
3168 /* Init the stub hash table too. */
3169 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3170 sizeof (struct ppc_stub_hash_entry)))
3172 _bfd_elf_link_hash_table_free (abfd);
3176 /* And the branch hash table. */
3177 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3178 sizeof (struct ppc_branch_hash_entry)))
3180 bfd_hash_table_free (&htab->stub_hash_table);
3181 _bfd_elf_link_hash_table_free (abfd);
3185 htab->tocsave_htab = htab_try_create (1024,
3189 if (htab->tocsave_htab == NULL)
3191 ppc64_elf_link_hash_table_free (abfd);
3194 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3196 /* Initializing two fields of the union is just cosmetic. We really
3197 only care about glist, but when compiled on a 32-bit host the
3198 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3199 debugger inspection of these fields look nicer. */
3200 htab->elf.init_got_refcount.refcount = 0;
3201 htab->elf.init_got_refcount.glist = NULL;
3202 htab->elf.init_plt_refcount.refcount = 0;
3203 htab->elf.init_plt_refcount.glist = NULL;
3204 htab->elf.init_got_offset.offset = 0;
3205 htab->elf.init_got_offset.glist = NULL;
3206 htab->elf.init_plt_offset.offset = 0;
3207 htab->elf.init_plt_offset.glist = NULL;
3209 return &htab->elf.root;
3212 /* Create sections for linker generated code. */
3215 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3217 struct ppc_link_hash_table *htab;
3220 htab = ppc_hash_table (info);
3222 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3223 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3224 if (htab->params->save_restore_funcs)
3226 /* Create .sfpr for code to save and restore fp regs. */
3227 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3229 if (htab->sfpr == NULL
3230 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3234 if (bfd_link_relocatable (info))
3237 /* Create .glink for lazy dynamic linking support. */
3238 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3240 if (htab->glink == NULL
3241 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
3244 /* The part of .glink used by global entry stubs, separate so that
3245 it can be aligned appropriately without affecting htab->glink. */
3246 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3248 if (htab->global_entry == NULL
3249 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
3252 if (!info->no_ld_generated_unwind_info)
3254 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3255 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3256 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3259 if (htab->glink_eh_frame == NULL
3260 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3264 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3265 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3266 if (htab->elf.iplt == NULL
3267 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
3270 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3271 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3273 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3274 if (htab->elf.irelplt == NULL
3275 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
3278 /* Create branch lookup table for plt_branch stubs. */
3279 flags = (SEC_ALLOC | SEC_LOAD
3280 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3281 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3283 if (htab->brlt == NULL
3284 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
3287 /* Local plt entries, put in .branch_lt but a separate section for
3289 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3291 if (htab->pltlocal == NULL
3292 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
3295 if (!bfd_link_pic (info))
3298 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3299 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3301 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3302 if (htab->relbrlt == NULL
3303 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3307 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3308 if (htab->relpltlocal == NULL
3309 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
3315 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3318 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3319 struct ppc64_elf_params *params)
3321 struct ppc_link_hash_table *htab;
3323 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3325 /* Always hook our dynamic sections into the first bfd, which is the
3326 linker created stub bfd. This ensures that the GOT header is at
3327 the start of the output TOC section. */
3328 htab = ppc_hash_table (info);
3329 htab->elf.dynobj = params->stub_bfd;
3330 htab->params = params;
3332 return create_linkage_sections (htab->elf.dynobj, info);
3335 /* Build a name for an entry in the stub hash table. */
3338 ppc_stub_name (const asection *input_section,
3339 const asection *sym_sec,
3340 const struct ppc_link_hash_entry *h,
3341 const Elf_Internal_Rela *rel)
3346 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3347 offsets from a sym as a branch target? In fact, we could
3348 probably assume the addend is always zero. */
3349 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3353 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3354 stub_name = bfd_malloc (len);
3355 if (stub_name == NULL)
3358 len = sprintf (stub_name, "%08x.%s+%x",
3359 input_section->id & 0xffffffff,
3360 h->elf.root.root.string,
3361 (int) rel->r_addend & 0xffffffff);
3365 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3366 stub_name = bfd_malloc (len);
3367 if (stub_name == NULL)
3370 len = sprintf (stub_name, "%08x.%x:%x+%x",
3371 input_section->id & 0xffffffff,
3372 sym_sec->id & 0xffffffff,
3373 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3374 (int) rel->r_addend & 0xffffffff);
3376 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3377 stub_name[len - 2] = 0;
3381 /* Look up an entry in the stub hash. Stub entries are cached because
3382 creating the stub name takes a bit of time. */
3384 static struct ppc_stub_hash_entry *
3385 ppc_get_stub_entry (const asection *input_section,
3386 const asection *sym_sec,
3387 struct ppc_link_hash_entry *h,
3388 const Elf_Internal_Rela *rel,
3389 struct ppc_link_hash_table *htab)
3391 struct ppc_stub_hash_entry *stub_entry;
3392 struct map_stub *group;
3394 /* If this input section is part of a group of sections sharing one
3395 stub section, then use the id of the first section in the group.
3396 Stub names need to include a section id, as there may well be
3397 more than one stub used to reach say, printf, and we need to
3398 distinguish between them. */
3399 group = htab->sec_info[input_section->id].u.group;
3403 if (h != NULL && h->u.stub_cache != NULL
3404 && h->u.stub_cache->h == h
3405 && h->u.stub_cache->group == group)
3407 stub_entry = h->u.stub_cache;
3413 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3414 if (stub_name == NULL)
3417 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3418 stub_name, FALSE, FALSE);
3420 h->u.stub_cache = stub_entry;
3428 /* Add a new stub entry to the stub hash. Not all fields of the new
3429 stub entry are initialised. */
3431 static struct ppc_stub_hash_entry *
3432 ppc_add_stub (const char *stub_name,
3434 struct bfd_link_info *info)
3436 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3437 struct map_stub *group;
3440 struct ppc_stub_hash_entry *stub_entry;
3442 group = htab->sec_info[section->id].u.group;
3443 link_sec = group->link_sec;
3444 stub_sec = group->stub_sec;
3445 if (stub_sec == NULL)
3451 namelen = strlen (link_sec->name);
3452 len = namelen + sizeof (STUB_SUFFIX);
3453 s_name = bfd_alloc (htab->params->stub_bfd, len);
3457 memcpy (s_name, link_sec->name, namelen);
3458 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3459 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3460 if (stub_sec == NULL)
3462 group->stub_sec = stub_sec;
3465 /* Enter this entry into the linker stub hash table. */
3466 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3468 if (stub_entry == NULL)
3470 /* xgettext:c-format */
3471 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3472 section->owner, stub_name);
3476 stub_entry->group = group;
3477 stub_entry->stub_offset = 0;
3481 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3482 not already done. */
3485 create_got_section (bfd *abfd, struct bfd_link_info *info)
3487 asection *got, *relgot;
3489 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3491 if (!is_ppc64_elf (abfd))
3497 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3500 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3501 | SEC_LINKER_CREATED);
3503 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3505 || !bfd_set_section_alignment (abfd, got, 3))
3508 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3509 flags | SEC_READONLY);
3511 || !bfd_set_section_alignment (abfd, relgot, 3))
3514 ppc64_elf_tdata (abfd)->got = got;
3515 ppc64_elf_tdata (abfd)->relgot = relgot;
3519 /* Follow indirect and warning symbol links. */
3521 static inline struct bfd_link_hash_entry *
3522 follow_link (struct bfd_link_hash_entry *h)
3524 while (h->type == bfd_link_hash_indirect
3525 || h->type == bfd_link_hash_warning)
3530 static inline struct elf_link_hash_entry *
3531 elf_follow_link (struct elf_link_hash_entry *h)
3533 return (struct elf_link_hash_entry *) follow_link (&h->root);
3536 static inline struct ppc_link_hash_entry *
3537 ppc_follow_link (struct ppc_link_hash_entry *h)
3539 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3542 /* Merge PLT info on FROM with that on TO. */
3545 move_plt_plist (struct ppc_link_hash_entry *from,
3546 struct ppc_link_hash_entry *to)
3548 if (from->elf.plt.plist != NULL)
3550 if (to->elf.plt.plist != NULL)
3552 struct plt_entry **entp;
3553 struct plt_entry *ent;
3555 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3557 struct plt_entry *dent;
3559 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3560 if (dent->addend == ent->addend)
3562 dent->plt.refcount += ent->plt.refcount;
3569 *entp = to->elf.plt.plist;
3572 to->elf.plt.plist = from->elf.plt.plist;
3573 from->elf.plt.plist = NULL;
3577 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3580 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3581 struct elf_link_hash_entry *dir,
3582 struct elf_link_hash_entry *ind)
3584 struct ppc_link_hash_entry *edir, *eind;
3586 edir = (struct ppc_link_hash_entry *) dir;
3587 eind = (struct ppc_link_hash_entry *) ind;
3589 edir->is_func |= eind->is_func;
3590 edir->is_func_descriptor |= eind->is_func_descriptor;
3591 edir->tls_mask |= eind->tls_mask;
3592 if (eind->oh != NULL)
3593 edir->oh = ppc_follow_link (eind->oh);
3595 if (edir->elf.versioned != versioned_hidden)
3596 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3597 edir->elf.ref_regular |= eind->elf.ref_regular;
3598 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3599 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3600 edir->elf.needs_plt |= eind->elf.needs_plt;
3601 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3603 /* If we were called to copy over info for a weak sym, don't copy
3604 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3605 in order to simplify readonly_dynrelocs and save a field in the
3606 symbol hash entry, but that means dyn_relocs can't be used in any
3607 tests about a specific symbol, or affect other symbol flags which
3609 if (eind->elf.root.type != bfd_link_hash_indirect)
3612 /* Copy over any dynamic relocs we may have on the indirect sym. */
3613 if (eind->dyn_relocs != NULL)
3615 if (edir->dyn_relocs != NULL)
3617 struct elf_dyn_relocs **pp;
3618 struct elf_dyn_relocs *p;
3620 /* Add reloc counts against the indirect sym to the direct sym
3621 list. Merge any entries against the same section. */
3622 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3624 struct elf_dyn_relocs *q;
3626 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3627 if (q->sec == p->sec)
3629 q->pc_count += p->pc_count;
3630 q->count += p->count;
3637 *pp = edir->dyn_relocs;
3640 edir->dyn_relocs = eind->dyn_relocs;
3641 eind->dyn_relocs = NULL;
3644 /* Copy over got entries that we may have already seen to the
3645 symbol which just became indirect. */
3646 if (eind->elf.got.glist != NULL)
3648 if (edir->elf.got.glist != NULL)
3650 struct got_entry **entp;
3651 struct got_entry *ent;
3653 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3655 struct got_entry *dent;
3657 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3658 if (dent->addend == ent->addend
3659 && dent->owner == ent->owner
3660 && dent->tls_type == ent->tls_type)
3662 dent->got.refcount += ent->got.refcount;
3669 *entp = edir->elf.got.glist;
3672 edir->elf.got.glist = eind->elf.got.glist;
3673 eind->elf.got.glist = NULL;
3676 /* And plt entries. */
3677 move_plt_plist (eind, edir);
3679 if (eind->elf.dynindx != -1)
3681 if (edir->elf.dynindx != -1)
3682 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3683 edir->elf.dynstr_index);
3684 edir->elf.dynindx = eind->elf.dynindx;
3685 edir->elf.dynstr_index = eind->elf.dynstr_index;
3686 eind->elf.dynindx = -1;
3687 eind->elf.dynstr_index = 0;
3691 /* Find the function descriptor hash entry from the given function code
3692 hash entry FH. Link the entries via their OH fields. */
3694 static struct ppc_link_hash_entry *
3695 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3697 struct ppc_link_hash_entry *fdh = fh->oh;
3701 const char *fd_name = fh->elf.root.root.string + 1;
3703 fdh = (struct ppc_link_hash_entry *)
3704 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
3708 fdh->is_func_descriptor = 1;
3714 fdh = ppc_follow_link (fdh);
3715 fdh->is_func_descriptor = 1;
3720 /* Make a fake function descriptor sym for the undefined code sym FH. */
3722 static struct ppc_link_hash_entry *
3723 make_fdh (struct bfd_link_info *info,
3724 struct ppc_link_hash_entry *fh)
3726 bfd *abfd = fh->elf.root.u.undef.abfd;
3727 struct bfd_link_hash_entry *bh = NULL;
3728 struct ppc_link_hash_entry *fdh;
3729 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3733 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3734 fh->elf.root.root.string + 1,
3735 flags, bfd_und_section_ptr, 0,
3736 NULL, FALSE, FALSE, &bh))
3739 fdh = (struct ppc_link_hash_entry *) bh;
3740 fdh->elf.non_elf = 0;
3742 fdh->is_func_descriptor = 1;
3749 /* Fix function descriptor symbols defined in .opd sections to be
3753 ppc64_elf_add_symbol_hook (bfd *ibfd,
3754 struct bfd_link_info *info,
3755 Elf_Internal_Sym *isym,
3757 flagword *flags ATTRIBUTE_UNUSED,
3762 && strcmp ((*sec)->name, ".opd") == 0)
3766 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3767 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
3768 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
3770 /* If the symbol is a function defined in .opd, and the function
3771 code is in a discarded group, let it appear to be undefined. */
3772 if (!bfd_link_relocatable (info)
3773 && (*sec)->reloc_count != 0
3774 && opd_entry_value (*sec, *value, &code_sec, NULL,
3775 FALSE) != (bfd_vma) -1
3776 && discarded_section (code_sec))
3778 *sec = bfd_und_section_ptr;
3779 isym->st_shndx = SHN_UNDEF;
3782 else if (*sec != NULL
3783 && strcmp ((*sec)->name, ".toc") == 0
3784 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
3786 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3788 htab->params->object_in_toc = 1;
3791 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3793 if (abiversion (ibfd) == 0)
3794 set_abiversion (ibfd, 2);
3795 else if (abiversion (ibfd) == 1)
3797 _bfd_error_handler (_("symbol '%s' has invalid st_other"
3798 " for ABI version 1"), *name);
3799 bfd_set_error (bfd_error_bad_value);
3807 /* Merge non-visibility st_other attributes: local entry point. */
3810 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
3811 const Elf_Internal_Sym *isym,
3812 bfd_boolean definition,
3813 bfd_boolean dynamic)
3815 if (definition && (!dynamic || !h->def_regular))
3816 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
3817 | ELF_ST_VISIBILITY (h->other));
3820 /* Hook called on merging a symbol. We use this to clear "fake" since
3821 we now have a real symbol. */
3824 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
3825 const Elf_Internal_Sym *isym,
3826 asection **psec ATTRIBUTE_UNUSED,
3827 bfd_boolean newdef ATTRIBUTE_UNUSED,
3828 bfd_boolean olddef ATTRIBUTE_UNUSED,
3829 bfd *oldbfd ATTRIBUTE_UNUSED,
3830 const asection *oldsec ATTRIBUTE_UNUSED)
3832 ((struct ppc_link_hash_entry *) h)->fake = 0;
3833 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3834 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
3838 /* This function makes an old ABI object reference to ".bar" cause the
3839 inclusion of a new ABI object archive that defines "bar".
3840 NAME is a symbol defined in an archive. Return a symbol in the hash
3841 table that might be satisfied by the archive symbols. */
3843 static struct elf_link_hash_entry *
3844 ppc64_elf_archive_symbol_lookup (bfd *abfd,
3845 struct bfd_link_info *info,
3848 struct elf_link_hash_entry *h;
3852 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
3854 /* Don't return this sym if it is a fake function descriptor
3855 created by add_symbol_adjust. */
3856 && !((struct ppc_link_hash_entry *) h)->fake)
3862 len = strlen (name);
3863 dot_name = bfd_alloc (abfd, len + 2);
3864 if (dot_name == NULL)
3865 return (struct elf_link_hash_entry *) -1;
3867 memcpy (dot_name + 1, name, len + 1);
3868 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
3869 bfd_release (abfd, dot_name);
3873 /* This function satisfies all old ABI object references to ".bar" if a
3874 new ABI object defines "bar". Well, at least, undefined dot symbols
3875 are made weak. This stops later archive searches from including an
3876 object if we already have a function descriptor definition. It also
3877 prevents the linker complaining about undefined symbols.
3878 We also check and correct mismatched symbol visibility here. The
3879 most restrictive visibility of the function descriptor and the
3880 function entry symbol is used. */
3883 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
3885 struct ppc_link_hash_table *htab;
3886 struct ppc_link_hash_entry *fdh;
3888 if (eh->elf.root.type == bfd_link_hash_warning)
3889 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
3891 if (eh->elf.root.type == bfd_link_hash_indirect)
3894 if (eh->elf.root.root.string[0] != '.')
3897 htab = ppc_hash_table (info);
3901 fdh = lookup_fdh (eh, htab);
3903 && !bfd_link_relocatable (info)
3904 && (eh->elf.root.type == bfd_link_hash_undefined
3905 || eh->elf.root.type == bfd_link_hash_undefweak)
3906 && eh->elf.ref_regular)
3908 /* Make an undefined function descriptor sym, in order to
3909 pull in an --as-needed shared lib. Archives are handled
3911 fdh = make_fdh (info, eh);
3918 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
3919 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
3921 /* Make both descriptor and entry symbol have the most
3922 constraining visibility of either symbol. */
3923 if (entry_vis < descr_vis)
3924 fdh->elf.other += entry_vis - descr_vis;
3925 else if (entry_vis > descr_vis)
3926 eh->elf.other += descr_vis - entry_vis;
3928 /* Propagate reference flags from entry symbol to function
3929 descriptor symbol. */
3930 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
3931 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
3932 fdh->elf.ref_regular |= eh->elf.ref_regular;
3933 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
3935 if (!fdh->elf.forced_local
3936 && fdh->elf.dynindx == -1
3937 && fdh->elf.versioned != versioned_hidden
3938 && (bfd_link_dll (info)
3939 || fdh->elf.def_dynamic
3940 || fdh->elf.ref_dynamic)
3941 && (eh->elf.ref_regular
3942 || eh->elf.def_regular))
3944 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
3952 /* Set up opd section info and abiversion for IBFD, and process list
3953 of dot-symbols we made in link_hash_newfunc. */
3956 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
3958 struct ppc_link_hash_table *htab;
3959 struct ppc_link_hash_entry **p, *eh;
3960 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
3962 if (opd != NULL && opd->size != 0)
3964 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
3965 ppc64_elf_section_data (opd)->sec_type = sec_opd;
3967 if (abiversion (ibfd) == 0)
3968 set_abiversion (ibfd, 1);
3969 else if (abiversion (ibfd) >= 2)
3971 /* xgettext:c-format */
3972 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
3973 ibfd, abiversion (ibfd));
3974 bfd_set_error (bfd_error_bad_value);
3979 if (is_ppc64_elf (info->output_bfd))
3981 /* For input files without an explicit abiversion in e_flags
3982 we should have flagged any with symbol st_other bits set
3983 as ELFv1 and above flagged those with .opd as ELFv2.
3984 Set the output abiversion if not yet set, and for any input
3985 still ambiguous, take its abiversion from the output.
3986 Differences in ABI are reported later. */
3987 if (abiversion (info->output_bfd) == 0)
3988 set_abiversion (info->output_bfd, abiversion (ibfd));
3989 else if (abiversion (ibfd) == 0)
3990 set_abiversion (ibfd, abiversion (info->output_bfd));
3993 htab = ppc_hash_table (info);
3997 if (opd != NULL && opd->size != 0
3998 && (ibfd->flags & DYNAMIC) == 0
3999 && (opd->flags & SEC_RELOC) != 0
4000 && opd->reloc_count != 0
4001 && !bfd_is_abs_section (opd->output_section)
4002 && info->gc_sections)
4004 /* Garbage collection needs some extra help with .opd sections.
4005 We don't want to necessarily keep everything referenced by
4006 relocs in .opd, as that would keep all functions. Instead,
4007 if we reference an .opd symbol (a function descriptor), we
4008 want to keep the function code symbol's section. This is
4009 easy for global symbols, but for local syms we need to keep
4010 information about the associated function section. */
4012 asection **opd_sym_map;
4013 Elf_Internal_Shdr *symtab_hdr;
4014 Elf_Internal_Rela *relocs, *rel_end, *rel;
4016 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4017 opd_sym_map = bfd_zalloc (ibfd, amt);
4018 if (opd_sym_map == NULL)
4020 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4021 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4025 symtab_hdr = &elf_symtab_hdr (ibfd);
4026 rel_end = relocs + opd->reloc_count - 1;
4027 for (rel = relocs; rel < rel_end; rel++)
4029 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4030 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4032 if (r_type == R_PPC64_ADDR64
4033 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4034 && r_symndx < symtab_hdr->sh_info)
4036 Elf_Internal_Sym *isym;
4039 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4042 if (elf_section_data (opd)->relocs != relocs)
4047 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4048 if (s != NULL && s != opd)
4049 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4052 if (elf_section_data (opd)->relocs != relocs)
4056 p = &htab->dot_syms;
4057 while ((eh = *p) != NULL)
4060 if (&eh->elf == htab->elf.hgot)
4062 else if (htab->elf.hgot == NULL
4063 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4064 htab->elf.hgot = &eh->elf;
4065 else if (abiversion (ibfd) <= 1)
4067 htab->need_func_desc_adj = 1;
4068 if (!add_symbol_adjust (eh, info))
4071 p = &eh->u.next_dot_sym;
4076 /* Undo hash table changes when an --as-needed input file is determined
4077 not to be needed. */
4080 ppc64_elf_notice_as_needed (bfd *ibfd,
4081 struct bfd_link_info *info,
4082 enum notice_asneeded_action act)
4084 if (act == notice_not_needed)
4086 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4091 htab->dot_syms = NULL;
4093 return _bfd_elf_notice_as_needed (ibfd, info, act);
4096 /* If --just-symbols against a final linked binary, then assume we need
4097 toc adjusting stubs when calling functions defined there. */
4100 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4102 if ((sec->flags & SEC_CODE) != 0
4103 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4104 && is_ppc64_elf (sec->owner))
4106 if (abiversion (sec->owner) >= 2
4107 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4108 sec->has_toc_reloc = 1;
4110 _bfd_elf_link_just_syms (sec, info);
4113 static struct plt_entry **
4114 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4115 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4117 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4118 struct plt_entry **local_plt;
4119 unsigned char *local_got_tls_masks;
4121 if (local_got_ents == NULL)
4123 bfd_size_type size = symtab_hdr->sh_info;
4125 size *= (sizeof (*local_got_ents)
4126 + sizeof (*local_plt)
4127 + sizeof (*local_got_tls_masks));
4128 local_got_ents = bfd_zalloc (abfd, size);
4129 if (local_got_ents == NULL)
4131 elf_local_got_ents (abfd) = local_got_ents;
4134 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4136 struct got_entry *ent;
4138 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4139 if (ent->addend == r_addend
4140 && ent->owner == abfd
4141 && ent->tls_type == tls_type)
4145 bfd_size_type amt = sizeof (*ent);
4146 ent = bfd_alloc (abfd, amt);
4149 ent->next = local_got_ents[r_symndx];
4150 ent->addend = r_addend;
4152 ent->tls_type = tls_type;
4153 ent->is_indirect = FALSE;
4154 ent->got.refcount = 0;
4155 local_got_ents[r_symndx] = ent;
4157 ent->got.refcount += 1;
4160 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4161 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4162 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4164 return local_plt + r_symndx;
4168 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4170 struct plt_entry *ent;
4172 for (ent = *plist; ent != NULL; ent = ent->next)
4173 if (ent->addend == addend)
4177 bfd_size_type amt = sizeof (*ent);
4178 ent = bfd_alloc (abfd, amt);
4182 ent->addend = addend;
4183 ent->plt.refcount = 0;
4186 ent->plt.refcount += 1;
4191 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4193 return (r_type == R_PPC64_REL24
4194 || r_type == R_PPC64_REL24_NOTOC
4195 || r_type == R_PPC64_REL14
4196 || r_type == R_PPC64_REL14_BRTAKEN
4197 || r_type == R_PPC64_REL14_BRNTAKEN
4198 || r_type == R_PPC64_ADDR24
4199 || r_type == R_PPC64_ADDR14
4200 || r_type == R_PPC64_ADDR14_BRTAKEN
4201 || r_type == R_PPC64_ADDR14_BRNTAKEN
4202 || r_type == R_PPC64_PLTCALL);
4205 /* Relocs on inline plt call sequence insns prior to the call. */
4208 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4210 return (r_type == R_PPC64_PLT16_HA
4211 || r_type == R_PPC64_PLT16_HI
4212 || r_type == R_PPC64_PLT16_LO
4213 || r_type == R_PPC64_PLT16_LO_DS
4214 || r_type == R_PPC64_PLTSEQ);
4217 /* Look through the relocs for a section during the first phase, and
4218 calculate needed space in the global offset table, procedure
4219 linkage table, and dynamic reloc sections. */
4222 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4223 asection *sec, const Elf_Internal_Rela *relocs)
4225 struct ppc_link_hash_table *htab;
4226 Elf_Internal_Shdr *symtab_hdr;
4227 struct elf_link_hash_entry **sym_hashes;
4228 const Elf_Internal_Rela *rel;
4229 const Elf_Internal_Rela *rel_end;
4231 struct elf_link_hash_entry *tga, *dottga;
4234 if (bfd_link_relocatable (info))
4237 /* Don't do anything special with non-loaded, non-alloced sections.
4238 In particular, any relocs in such sections should not affect GOT
4239 and PLT reference counting (ie. we don't allow them to create GOT
4240 or PLT entries), there's no possibility or desire to optimize TLS
4241 relocs, and there's not much point in propagating relocs to shared
4242 libs that the dynamic linker won't relocate. */
4243 if ((sec->flags & SEC_ALLOC) == 0)
4246 BFD_ASSERT (is_ppc64_elf (abfd));
4248 htab = ppc_hash_table (info);
4252 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4253 FALSE, FALSE, TRUE);
4254 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4255 FALSE, FALSE, TRUE);
4256 symtab_hdr = &elf_symtab_hdr (abfd);
4257 sym_hashes = elf_sym_hashes (abfd);
4259 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4260 rel_end = relocs + sec->reloc_count;
4261 for (rel = relocs; rel < rel_end; rel++)
4263 unsigned long r_symndx;
4264 struct elf_link_hash_entry *h;
4265 enum elf_ppc64_reloc_type r_type;
4267 struct _ppc64_elf_section_data *ppc64_sec;
4268 struct plt_entry **ifunc, **plt_list;
4270 r_symndx = ELF64_R_SYM (rel->r_info);
4271 if (r_symndx < symtab_hdr->sh_info)
4275 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4276 h = elf_follow_link (h);
4278 if (h == htab->elf.hgot)
4279 sec->has_toc_reloc = 1;
4286 if (h->type == STT_GNU_IFUNC)
4289 ifunc = &h->plt.plist;
4294 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4299 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4301 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4303 NON_GOT | PLT_IFUNC);
4309 r_type = ELF64_R_TYPE (rel->r_info);
4314 /* These special tls relocs tie a call to __tls_get_addr with
4315 its parameter symbol. */
4317 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4319 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4321 NON_GOT | TLS_TLS | TLS_MARK))
4323 sec->has_tls_reloc = 1;
4326 case R_PPC64_GOT_TLSLD16:
4327 case R_PPC64_GOT_TLSLD16_LO:
4328 case R_PPC64_GOT_TLSLD16_HI:
4329 case R_PPC64_GOT_TLSLD16_HA:
4330 tls_type = TLS_TLS | TLS_LD;
4333 case R_PPC64_GOT_TLSGD16:
4334 case R_PPC64_GOT_TLSGD16_LO:
4335 case R_PPC64_GOT_TLSGD16_HI:
4336 case R_PPC64_GOT_TLSGD16_HA:
4337 tls_type = TLS_TLS | TLS_GD;
4340 case R_PPC64_GOT_TPREL16_DS:
4341 case R_PPC64_GOT_TPREL16_LO_DS:
4342 case R_PPC64_GOT_TPREL16_HI:
4343 case R_PPC64_GOT_TPREL16_HA:
4344 if (bfd_link_dll (info))
4345 info->flags |= DF_STATIC_TLS;
4346 tls_type = TLS_TLS | TLS_TPREL;
4349 case R_PPC64_GOT_DTPREL16_DS:
4350 case R_PPC64_GOT_DTPREL16_LO_DS:
4351 case R_PPC64_GOT_DTPREL16_HI:
4352 case R_PPC64_GOT_DTPREL16_HA:
4353 tls_type = TLS_TLS | TLS_DTPREL;
4355 sec->has_tls_reloc = 1;
4359 case R_PPC64_GOT16_DS:
4360 case R_PPC64_GOT16_HA:
4361 case R_PPC64_GOT16_HI:
4362 case R_PPC64_GOT16_LO:
4363 case R_PPC64_GOT16_LO_DS:
4364 /* This symbol requires a global offset table entry. */
4365 sec->has_toc_reloc = 1;
4366 if (r_type == R_PPC64_GOT_TLSLD16
4367 || r_type == R_PPC64_GOT_TLSGD16
4368 || r_type == R_PPC64_GOT_TPREL16_DS
4369 || r_type == R_PPC64_GOT_DTPREL16_DS
4370 || r_type == R_PPC64_GOT16
4371 || r_type == R_PPC64_GOT16_DS)
4373 htab->do_multi_toc = 1;
4374 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4377 if (ppc64_elf_tdata (abfd)->got == NULL
4378 && !create_got_section (abfd, info))
4383 struct ppc_link_hash_entry *eh;
4384 struct got_entry *ent;
4386 eh = (struct ppc_link_hash_entry *) h;
4387 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4388 if (ent->addend == rel->r_addend
4389 && ent->owner == abfd
4390 && ent->tls_type == tls_type)
4394 bfd_size_type amt = sizeof (*ent);
4395 ent = bfd_alloc (abfd, amt);
4398 ent->next = eh->elf.got.glist;
4399 ent->addend = rel->r_addend;
4401 ent->tls_type = tls_type;
4402 ent->is_indirect = FALSE;
4403 ent->got.refcount = 0;
4404 eh->elf.got.glist = ent;
4406 ent->got.refcount += 1;
4407 eh->tls_mask |= tls_type;
4410 /* This is a global offset table entry for a local symbol. */
4411 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4412 rel->r_addend, tls_type))
4415 /* We may also need a plt entry if the symbol turns out to be
4417 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
4419 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4424 case R_PPC64_PLT16_HA:
4425 case R_PPC64_PLT16_HI:
4426 case R_PPC64_PLT16_LO:
4427 case R_PPC64_PLT16_LO_DS:
4430 /* This symbol requires a procedure linkage table entry. */
4435 if (h->root.root.string[0] == '.'
4436 && h->root.root.string[1] != '\0')
4437 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4438 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
4439 plt_list = &h->plt.plist;
4441 if (plt_list == NULL)
4442 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4444 NON_GOT | PLT_KEEP);
4445 if (!update_plt_info (abfd, plt_list, rel->r_addend))
4449 /* The following relocations don't need to propagate the
4450 relocation if linking a shared object since they are
4451 section relative. */
4452 case R_PPC64_SECTOFF:
4453 case R_PPC64_SECTOFF_LO:
4454 case R_PPC64_SECTOFF_HI:
4455 case R_PPC64_SECTOFF_HA:
4456 case R_PPC64_SECTOFF_DS:
4457 case R_PPC64_SECTOFF_LO_DS:
4458 case R_PPC64_DTPREL16:
4459 case R_PPC64_DTPREL16_LO:
4460 case R_PPC64_DTPREL16_HI:
4461 case R_PPC64_DTPREL16_HA:
4462 case R_PPC64_DTPREL16_DS:
4463 case R_PPC64_DTPREL16_LO_DS:
4464 case R_PPC64_DTPREL16_HIGH:
4465 case R_PPC64_DTPREL16_HIGHA:
4466 case R_PPC64_DTPREL16_HIGHER:
4467 case R_PPC64_DTPREL16_HIGHERA:
4468 case R_PPC64_DTPREL16_HIGHEST:
4469 case R_PPC64_DTPREL16_HIGHESTA:
4474 case R_PPC64_REL16_LO:
4475 case R_PPC64_REL16_HI:
4476 case R_PPC64_REL16_HA:
4477 case R_PPC64_REL16DX_HA:
4480 /* Not supported as a dynamic relocation. */
4481 case R_PPC64_ADDR64_LOCAL:
4482 if (bfd_link_pic (info))
4484 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4486 /* xgettext:c-format */
4487 info->callbacks->einfo (_("%H: %s reloc unsupported "
4488 "in shared libraries and PIEs\n"),
4489 abfd, sec, rel->r_offset,
4490 ppc64_elf_howto_table[r_type]->name);
4491 bfd_set_error (bfd_error_bad_value);
4497 case R_PPC64_TOC16_DS:
4498 htab->do_multi_toc = 1;
4499 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4501 case R_PPC64_TOC16_LO:
4502 case R_PPC64_TOC16_HI:
4503 case R_PPC64_TOC16_HA:
4504 case R_PPC64_TOC16_LO_DS:
4505 sec->has_toc_reloc = 1;
4512 /* This relocation describes the C++ object vtable hierarchy.
4513 Reconstruct it for later use during GC. */
4514 case R_PPC64_GNU_VTINHERIT:
4515 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4519 /* This relocation describes which C++ vtable entries are actually
4520 used. Record for later use during GC. */
4521 case R_PPC64_GNU_VTENTRY:
4522 BFD_ASSERT (h != NULL);
4524 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4529 case R_PPC64_REL14_BRTAKEN:
4530 case R_PPC64_REL14_BRNTAKEN:
4532 asection *dest = NULL;
4534 /* Heuristic: If jumping outside our section, chances are
4535 we are going to need a stub. */
4538 /* If the sym is weak it may be overridden later, so
4539 don't assume we know where a weak sym lives. */
4540 if (h->root.type == bfd_link_hash_defined)
4541 dest = h->root.u.def.section;
4545 Elf_Internal_Sym *isym;
4547 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4552 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4556 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
4560 case R_PPC64_PLTCALL:
4561 ppc64_elf_section_data (sec)->has_pltcall = 1;
4565 case R_PPC64_REL24_NOTOC:
4571 if (h->root.root.string[0] == '.'
4572 && h->root.root.string[1] != '\0')
4573 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4575 if (h == tga || h == dottga)
4577 sec->has_tls_reloc = 1;
4579 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4580 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4581 /* We have a new-style __tls_get_addr call with
4585 /* Mark this section as having an old-style call. */
4586 sec->has_tls_get_addr_call = 1;
4588 plt_list = &h->plt.plist;
4591 /* We may need a .plt entry if the function this reloc
4592 refers to is in a shared lib. */
4594 && !update_plt_info (abfd, plt_list, rel->r_addend))
4598 case R_PPC64_ADDR14:
4599 case R_PPC64_ADDR14_BRNTAKEN:
4600 case R_PPC64_ADDR14_BRTAKEN:
4601 case R_PPC64_ADDR24:
4604 case R_PPC64_TPREL64:
4605 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4606 if (bfd_link_dll (info))
4607 info->flags |= DF_STATIC_TLS;
4610 case R_PPC64_DTPMOD64:
4611 if (rel + 1 < rel_end
4612 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4613 && rel[1].r_offset == rel->r_offset + 8)
4614 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4616 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4619 case R_PPC64_DTPREL64:
4620 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4622 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4623 && rel[-1].r_offset == rel->r_offset - 8)
4624 /* This is the second reloc of a dtpmod, dtprel pair.
4625 Don't mark with TLS_DTPREL. */
4629 sec->has_tls_reloc = 1;
4632 struct ppc_link_hash_entry *eh;
4633 eh = (struct ppc_link_hash_entry *) h;
4634 eh->tls_mask |= tls_type;
4637 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4638 rel->r_addend, tls_type))
4641 ppc64_sec = ppc64_elf_section_data (sec);
4642 if (ppc64_sec->sec_type != sec_toc)
4646 /* One extra to simplify get_tls_mask. */
4647 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4648 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4649 if (ppc64_sec->u.toc.symndx == NULL)
4651 amt = sec->size * sizeof (bfd_vma) / 8;
4652 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4653 if (ppc64_sec->u.toc.add == NULL)
4655 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4656 ppc64_sec->sec_type = sec_toc;
4658 BFD_ASSERT (rel->r_offset % 8 == 0);
4659 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
4660 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
4662 /* Mark the second slot of a GD or LD entry.
4663 -1 to indicate GD and -2 to indicate LD. */
4664 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
4665 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
4666 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
4667 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
4670 case R_PPC64_TPREL16:
4671 case R_PPC64_TPREL16_LO:
4672 case R_PPC64_TPREL16_HI:
4673 case R_PPC64_TPREL16_HA:
4674 case R_PPC64_TPREL16_DS:
4675 case R_PPC64_TPREL16_LO_DS:
4676 case R_PPC64_TPREL16_HIGH:
4677 case R_PPC64_TPREL16_HIGHA:
4678 case R_PPC64_TPREL16_HIGHER:
4679 case R_PPC64_TPREL16_HIGHERA:
4680 case R_PPC64_TPREL16_HIGHEST:
4681 case R_PPC64_TPREL16_HIGHESTA:
4682 if (bfd_link_dll (info))
4683 info->flags |= DF_STATIC_TLS;
4686 case R_PPC64_ADDR64:
4688 && rel + 1 < rel_end
4689 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
4692 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4696 case R_PPC64_ADDR16:
4697 case R_PPC64_ADDR16_DS:
4698 case R_PPC64_ADDR16_HA:
4699 case R_PPC64_ADDR16_HI:
4700 case R_PPC64_ADDR16_HIGH:
4701 case R_PPC64_ADDR16_HIGHA:
4702 case R_PPC64_ADDR16_HIGHER:
4703 case R_PPC64_ADDR16_HIGHERA:
4704 case R_PPC64_ADDR16_HIGHEST:
4705 case R_PPC64_ADDR16_HIGHESTA:
4706 case R_PPC64_ADDR16_LO:
4707 case R_PPC64_ADDR16_LO_DS:
4708 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
4709 && rel->r_addend == 0)
4711 /* We may need a .plt entry if this reloc refers to a
4712 function in a shared lib. */
4713 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4715 h->pointer_equality_needed = 1;
4722 case R_PPC64_ADDR32:
4723 case R_PPC64_UADDR16:
4724 case R_PPC64_UADDR32:
4725 case R_PPC64_UADDR64:
4727 if (h != NULL && !bfd_link_pic (info))
4728 /* We may need a copy reloc. */
4731 /* Don't propagate .opd relocs. */
4732 if (NO_OPD_RELOCS && is_opd)
4735 /* If we are creating a shared library, and this is a reloc
4736 against a global symbol, or a non PC relative reloc
4737 against a local symbol, then we need to copy the reloc
4738 into the shared library. However, if we are linking with
4739 -Bsymbolic, we do not need to copy a reloc against a
4740 global symbol which is defined in an object we are
4741 including in the link (i.e., DEF_REGULAR is set). At
4742 this point we have not seen all the input files, so it is
4743 possible that DEF_REGULAR is not set now but will be set
4744 later (it is never cleared). In case of a weak definition,
4745 DEF_REGULAR may be cleared later by a strong definition in
4746 a shared library. We account for that possibility below by
4747 storing information in the dyn_relocs field of the hash
4748 table entry. A similar situation occurs when creating
4749 shared libraries and symbol visibility changes render the
4752 If on the other hand, we are creating an executable, we
4753 may need to keep relocations for symbols satisfied by a
4754 dynamic library if we manage to avoid copy relocs for the
4757 if ((bfd_link_pic (info)
4758 && (must_be_dyn_reloc (info, r_type)
4760 && (!SYMBOLIC_BIND (info, h)
4761 || h->root.type == bfd_link_hash_defweak
4762 || !h->def_regular))))
4763 || (ELIMINATE_COPY_RELOCS
4764 && !bfd_link_pic (info)
4766 && (h->root.type == bfd_link_hash_defweak
4767 || !h->def_regular))
4768 || (!bfd_link_pic (info)
4771 /* We must copy these reloc types into the output file.
4772 Create a reloc section in dynobj and make room for
4776 sreloc = _bfd_elf_make_dynamic_reloc_section
4777 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
4783 /* If this is a global symbol, we count the number of
4784 relocations we need for this symbol. */
4787 struct elf_dyn_relocs *p;
4788 struct elf_dyn_relocs **head;
4790 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
4792 if (p == NULL || p->sec != sec)
4794 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4804 if (!must_be_dyn_reloc (info, r_type))
4809 /* Track dynamic relocs needed for local syms too.
4810 We really need local syms available to do this
4812 struct ppc_dyn_relocs *p;
4813 struct ppc_dyn_relocs **head;
4814 bfd_boolean is_ifunc;
4817 Elf_Internal_Sym *isym;
4819 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4824 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
4828 vpp = &elf_section_data (s)->local_dynrel;
4829 head = (struct ppc_dyn_relocs **) vpp;
4830 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
4832 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
4834 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
4836 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4842 p->ifunc = is_ifunc;
4858 /* Merge backend specific data from an object file to the output
4859 object file when linking. */
4862 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
4864 bfd *obfd = info->output_bfd;
4865 unsigned long iflags, oflags;
4867 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
4870 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
4873 if (!_bfd_generic_verify_endian_match (ibfd, info))
4876 iflags = elf_elfheader (ibfd)->e_flags;
4877 oflags = elf_elfheader (obfd)->e_flags;
4879 if (iflags & ~EF_PPC64_ABI)
4882 /* xgettext:c-format */
4883 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
4884 bfd_set_error (bfd_error_bad_value);
4887 else if (iflags != oflags && iflags != 0)
4890 /* xgettext:c-format */
4891 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
4892 ibfd, iflags, oflags);
4893 bfd_set_error (bfd_error_bad_value);
4897 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
4900 /* Merge Tag_compatibility attributes and any common GNU ones. */
4901 return _bfd_elf_merge_object_attributes (ibfd, info);
4905 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
4907 /* Print normal ELF private data. */
4908 _bfd_elf_print_private_bfd_data (abfd, ptr);
4910 if (elf_elfheader (abfd)->e_flags != 0)
4914 fprintf (file, _("private flags = 0x%lx:"),
4915 elf_elfheader (abfd)->e_flags);
4917 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
4918 fprintf (file, _(" [abiv%ld]"),
4919 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
4926 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
4927 of the code entry point, and its section, which must be in the same
4928 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
4931 opd_entry_value (asection *opd_sec,
4933 asection **code_sec,
4935 bfd_boolean in_code_sec)
4937 bfd *opd_bfd = opd_sec->owner;
4938 Elf_Internal_Rela *relocs;
4939 Elf_Internal_Rela *lo, *hi, *look;
4942 /* No relocs implies we are linking a --just-symbols object, or looking
4943 at a final linked executable with addr2line or somesuch. */
4944 if (opd_sec->reloc_count == 0)
4946 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
4948 if (contents == NULL)
4950 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
4951 return (bfd_vma) -1;
4952 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
4955 /* PR 17512: file: 64b9dfbb. */
4956 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
4957 return (bfd_vma) -1;
4959 val = bfd_get_64 (opd_bfd, contents + offset);
4960 if (code_sec != NULL)
4962 asection *sec, *likely = NULL;
4968 && val < sec->vma + sec->size)
4974 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
4976 && (sec->flags & SEC_LOAD) != 0
4977 && (sec->flags & SEC_ALLOC) != 0)
4982 if (code_off != NULL)
4983 *code_off = val - likely->vma;
4989 BFD_ASSERT (is_ppc64_elf (opd_bfd));
4991 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
4993 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
4994 /* PR 17512: file: df8e1fd6. */
4996 return (bfd_vma) -1;
4998 /* Go find the opd reloc at the sym address. */
5000 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5004 look = lo + (hi - lo) / 2;
5005 if (look->r_offset < offset)
5007 else if (look->r_offset > offset)
5011 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5013 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5014 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5016 unsigned long symndx = ELF64_R_SYM (look->r_info);
5017 asection *sec = NULL;
5019 if (symndx >= symtab_hdr->sh_info
5020 && elf_sym_hashes (opd_bfd) != NULL)
5022 struct elf_link_hash_entry **sym_hashes;
5023 struct elf_link_hash_entry *rh;
5025 sym_hashes = elf_sym_hashes (opd_bfd);
5026 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5029 rh = elf_follow_link (rh);
5030 if (rh->root.type != bfd_link_hash_defined
5031 && rh->root.type != bfd_link_hash_defweak)
5033 if (rh->root.u.def.section->owner == opd_bfd)
5035 val = rh->root.u.def.value;
5036 sec = rh->root.u.def.section;
5043 Elf_Internal_Sym *sym;
5045 if (symndx < symtab_hdr->sh_info)
5047 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5050 size_t symcnt = symtab_hdr->sh_info;
5051 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5056 symtab_hdr->contents = (bfd_byte *) sym;
5062 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5068 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5071 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5072 val = sym->st_value;
5075 val += look->r_addend;
5076 if (code_off != NULL)
5078 if (code_sec != NULL)
5080 if (in_code_sec && *code_sec != sec)
5085 if (sec->output_section != NULL)
5086 val += sec->output_section->vma + sec->output_offset;
5095 /* If the ELF symbol SYM might be a function in SEC, return the
5096 function size and set *CODE_OFF to the function's entry point,
5097 otherwise return zero. */
5099 static bfd_size_type
5100 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5105 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5106 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5110 if (!(sym->flags & BSF_SYNTHETIC))
5111 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5113 if (strcmp (sym->section->name, ".opd") == 0)
5115 struct _opd_sec_data *opd = get_opd_info (sym->section);
5116 bfd_vma symval = sym->value;
5119 && opd->adjust != NULL
5120 && elf_section_data (sym->section)->relocs != NULL)
5122 /* opd_entry_value will use cached relocs that have been
5123 adjusted, but with raw symbols. That means both local
5124 and global symbols need adjusting. */
5125 long adjust = opd->adjust[OPD_NDX (symval)];
5131 if (opd_entry_value (sym->section, symval,
5132 &sec, code_off, TRUE) == (bfd_vma) -1)
5134 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5135 symbol. This size has nothing to do with the code size of the
5136 function, which is what we're supposed to return, but the
5137 code size isn't available without looking up the dot-sym.
5138 However, doing that would be a waste of time particularly
5139 since elf_find_function will look at the dot-sym anyway.
5140 Now, elf_find_function will keep the largest size of any
5141 function sym found at the code address of interest, so return
5142 1 here to avoid it incorrectly caching a larger function size
5143 for a small function. This does mean we return the wrong
5144 size for a new-ABI function of size 24, but all that does is
5145 disable caching for such functions. */
5151 if (sym->section != sec)
5153 *code_off = sym->value;
5160 /* Return true if symbol is a strong function defined in an ELFv2
5161 object with st_other localentry bits of zero, ie. its local entry
5162 point coincides with its global entry point. */
5165 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5168 && h->type == STT_FUNC
5169 && h->root.type == bfd_link_hash_defined
5170 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5171 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
5172 && is_ppc64_elf (h->root.u.def.section->owner)
5173 && abiversion (h->root.u.def.section->owner) >= 2);
5176 /* Return true if symbol is defined in a regular object file. */
5179 is_static_defined (struct elf_link_hash_entry *h)
5181 return ((h->root.type == bfd_link_hash_defined
5182 || h->root.type == bfd_link_hash_defweak)
5183 && h->root.u.def.section != NULL
5184 && h->root.u.def.section->output_section != NULL);
5187 /* If FDH is a function descriptor symbol, return the associated code
5188 entry symbol if it is defined. Return NULL otherwise. */
5190 static struct ppc_link_hash_entry *
5191 defined_code_entry (struct ppc_link_hash_entry *fdh)
5193 if (fdh->is_func_descriptor)
5195 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5196 if (fh->elf.root.type == bfd_link_hash_defined
5197 || fh->elf.root.type == bfd_link_hash_defweak)
5203 /* If FH is a function code entry symbol, return the associated
5204 function descriptor symbol if it is defined. Return NULL otherwise. */
5206 static struct ppc_link_hash_entry *
5207 defined_func_desc (struct ppc_link_hash_entry *fh)
5210 && fh->oh->is_func_descriptor)
5212 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5213 if (fdh->elf.root.type == bfd_link_hash_defined
5214 || fdh->elf.root.type == bfd_link_hash_defweak)
5220 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5222 /* Garbage collect sections, after first dealing with dot-symbols. */
5225 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5227 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5229 if (htab != NULL && htab->need_func_desc_adj)
5231 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5232 htab->need_func_desc_adj = 0;
5234 return bfd_elf_gc_sections (abfd, info);
5237 /* Mark all our entry sym sections, both opd and code section. */
5240 ppc64_elf_gc_keep (struct bfd_link_info *info)
5242 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5243 struct bfd_sym_chain *sym;
5248 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5250 struct ppc_link_hash_entry *eh, *fh;
5253 eh = (struct ppc_link_hash_entry *)
5254 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5257 if (eh->elf.root.type != bfd_link_hash_defined
5258 && eh->elf.root.type != bfd_link_hash_defweak)
5261 fh = defined_code_entry (eh);
5264 sec = fh->elf.root.u.def.section;
5265 sec->flags |= SEC_KEEP;
5267 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5268 && opd_entry_value (eh->elf.root.u.def.section,
5269 eh->elf.root.u.def.value,
5270 &sec, NULL, FALSE) != (bfd_vma) -1)
5271 sec->flags |= SEC_KEEP;
5273 sec = eh->elf.root.u.def.section;
5274 sec->flags |= SEC_KEEP;
5278 /* Mark sections containing dynamically referenced symbols. When
5279 building shared libraries, we must assume that any visible symbol is
5283 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5285 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5286 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5287 struct ppc_link_hash_entry *fdh;
5288 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5290 /* Dynamic linking info is on the func descriptor sym. */
5291 fdh = defined_func_desc (eh);
5295 if ((eh->elf.root.type == bfd_link_hash_defined
5296 || eh->elf.root.type == bfd_link_hash_defweak)
5297 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5298 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5299 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5300 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5301 && (!bfd_link_executable (info)
5302 || info->gc_keep_exported
5303 || info->export_dynamic
5306 && (*d->match) (&d->head, NULL,
5307 eh->elf.root.root.string)))
5308 && (eh->elf.versioned >= versioned
5309 || !bfd_hide_sym_by_version (info->version_info,
5310 eh->elf.root.root.string)))))
5313 struct ppc_link_hash_entry *fh;
5315 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5317 /* Function descriptor syms cause the associated
5318 function code sym section to be marked. */
5319 fh = defined_code_entry (eh);
5322 code_sec = fh->elf.root.u.def.section;
5323 code_sec->flags |= SEC_KEEP;
5325 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5326 && opd_entry_value (eh->elf.root.u.def.section,
5327 eh->elf.root.u.def.value,
5328 &code_sec, NULL, FALSE) != (bfd_vma) -1)
5329 code_sec->flags |= SEC_KEEP;
5335 /* Return the section that should be marked against GC for a given
5339 ppc64_elf_gc_mark_hook (asection *sec,
5340 struct bfd_link_info *info,
5341 Elf_Internal_Rela *rel,
5342 struct elf_link_hash_entry *h,
5343 Elf_Internal_Sym *sym)
5347 /* Syms return NULL if we're marking .opd, so we avoid marking all
5348 function sections, as all functions are referenced in .opd. */
5350 if (get_opd_info (sec) != NULL)
5355 enum elf_ppc64_reloc_type r_type;
5356 struct ppc_link_hash_entry *eh, *fh, *fdh;
5358 r_type = ELF64_R_TYPE (rel->r_info);
5361 case R_PPC64_GNU_VTINHERIT:
5362 case R_PPC64_GNU_VTENTRY:
5366 switch (h->root.type)
5368 case bfd_link_hash_defined:
5369 case bfd_link_hash_defweak:
5370 eh = (struct ppc_link_hash_entry *) h;
5371 fdh = defined_func_desc (eh);
5374 /* -mcall-aixdesc code references the dot-symbol on
5375 a call reloc. Mark the function descriptor too
5376 against garbage collection. */
5378 if (fdh->elf.is_weakalias)
5379 weakdef (&fdh->elf)->mark = 1;
5383 /* Function descriptor syms cause the associated
5384 function code sym section to be marked. */
5385 fh = defined_code_entry (eh);
5388 /* They also mark their opd section. */
5389 eh->elf.root.u.def.section->gc_mark = 1;
5391 rsec = fh->elf.root.u.def.section;
5393 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5394 && opd_entry_value (eh->elf.root.u.def.section,
5395 eh->elf.root.u.def.value,
5396 &rsec, NULL, FALSE) != (bfd_vma) -1)
5397 eh->elf.root.u.def.section->gc_mark = 1;
5399 rsec = h->root.u.def.section;
5402 case bfd_link_hash_common:
5403 rsec = h->root.u.c.p->section;
5407 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5413 struct _opd_sec_data *opd;
5415 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5416 opd = get_opd_info (rsec);
5417 if (opd != NULL && opd->func_sec != NULL)
5421 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
5428 /* The maximum size of .sfpr. */
5429 #define SFPR_MAX (218*4)
5431 struct sfpr_def_parms
5433 const char name[12];
5434 unsigned char lo, hi;
5435 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5436 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
5439 /* Auto-generate _save*, _rest* functions in .sfpr.
5440 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5444 sfpr_define (struct bfd_link_info *info,
5445 const struct sfpr_def_parms *parm,
5448 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5450 size_t len = strlen (parm->name);
5451 bfd_boolean writing = FALSE;
5457 memcpy (sym, parm->name, len);
5460 for (i = parm->lo; i <= parm->hi; i++)
5462 struct ppc_link_hash_entry *h;
5464 sym[len + 0] = i / 10 + '0';
5465 sym[len + 1] = i % 10 + '0';
5466 h = (struct ppc_link_hash_entry *)
5467 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
5468 if (stub_sec != NULL)
5471 && h->elf.root.type == bfd_link_hash_defined
5472 && h->elf.root.u.def.section == htab->sfpr)
5474 struct elf_link_hash_entry *s;
5476 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5477 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5480 if (s->root.type == bfd_link_hash_new
5481 || (s->root.type = bfd_link_hash_defined
5482 && s->root.u.def.section == stub_sec))
5484 s->root.type = bfd_link_hash_defined;
5485 s->root.u.def.section = stub_sec;
5486 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
5487 + h->elf.root.u.def.value);
5490 s->ref_regular_nonweak = 1;
5491 s->forced_local = 1;
5493 s->root.linker_def = 1;
5501 if (!h->elf.def_regular)
5503 h->elf.root.type = bfd_link_hash_defined;
5504 h->elf.root.u.def.section = htab->sfpr;
5505 h->elf.root.u.def.value = htab->sfpr->size;
5506 h->elf.type = STT_FUNC;
5507 h->elf.def_regular = 1;
5509 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5511 if (htab->sfpr->contents == NULL)
5513 htab->sfpr->contents
5514 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5515 if (htab->sfpr->contents == NULL)
5522 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5524 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5526 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5527 htab->sfpr->size = p - htab->sfpr->contents;
5535 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5537 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5542 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5544 p = savegpr0 (abfd, p, r);
5545 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5547 bfd_put_32 (abfd, BLR, p);
5552 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5554 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5559 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5561 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5563 p = restgpr0 (abfd, p, r);
5564 bfd_put_32 (abfd, MTLR_R0, p);
5568 p = restgpr0 (abfd, p, 30);
5569 p = restgpr0 (abfd, p, 31);
5571 bfd_put_32 (abfd, BLR, p);
5576 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5578 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5583 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5585 p = savegpr1 (abfd, p, r);
5586 bfd_put_32 (abfd, BLR, p);
5591 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5593 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5598 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5600 p = restgpr1 (abfd, p, r);
5601 bfd_put_32 (abfd, BLR, p);
5606 savefpr (bfd *abfd, bfd_byte *p, int r)
5608 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5613 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5615 p = savefpr (abfd, p, r);
5616 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5618 bfd_put_32 (abfd, BLR, p);
5623 restfpr (bfd *abfd, bfd_byte *p, int r)
5625 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5630 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5632 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5634 p = restfpr (abfd, p, r);
5635 bfd_put_32 (abfd, MTLR_R0, p);
5639 p = restfpr (abfd, p, 30);
5640 p = restfpr (abfd, p, 31);
5642 bfd_put_32 (abfd, BLR, p);
5647 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5649 p = savefpr (abfd, p, r);
5650 bfd_put_32 (abfd, BLR, p);
5655 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5657 p = restfpr (abfd, p, r);
5658 bfd_put_32 (abfd, BLR, p);
5663 savevr (bfd *abfd, bfd_byte *p, int r)
5665 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5667 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5672 savevr_tail (bfd *abfd, bfd_byte *p, int r)
5674 p = savevr (abfd, p, r);
5675 bfd_put_32 (abfd, BLR, p);
5680 restvr (bfd *abfd, bfd_byte *p, int r)
5682 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5684 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5689 restvr_tail (bfd *abfd, bfd_byte *p, int r)
5691 p = restvr (abfd, p, r);
5692 bfd_put_32 (abfd, BLR, p);
5696 /* Called via elf_link_hash_traverse to transfer dynamic linking
5697 information on function code symbol entries to their corresponding
5698 function descriptor symbol entries. */
5701 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5703 struct bfd_link_info *info;
5704 struct ppc_link_hash_table *htab;
5705 struct ppc_link_hash_entry *fh;
5706 struct ppc_link_hash_entry *fdh;
5707 bfd_boolean force_local;
5709 fh = (struct ppc_link_hash_entry *) h;
5710 if (fh->elf.root.type == bfd_link_hash_indirect)
5716 if (fh->elf.root.root.string[0] != '.'
5717 || fh->elf.root.root.string[1] == '\0')
5721 htab = ppc_hash_table (info);
5725 /* Find the corresponding function descriptor symbol. */
5726 fdh = lookup_fdh (fh, htab);
5728 /* Resolve undefined references to dot-symbols as the value
5729 in the function descriptor, if we have one in a regular object.
5730 This is to satisfy cases like ".quad .foo". Calls to functions
5731 in dynamic objects are handled elsewhere. */
5732 if ((fh->elf.root.type == bfd_link_hash_undefined
5733 || fh->elf.root.type == bfd_link_hash_undefweak)
5734 && (fdh->elf.root.type == bfd_link_hash_defined
5735 || fdh->elf.root.type == bfd_link_hash_defweak)
5736 && get_opd_info (fdh->elf.root.u.def.section) != NULL
5737 && opd_entry_value (fdh->elf.root.u.def.section,
5738 fdh->elf.root.u.def.value,
5739 &fh->elf.root.u.def.section,
5740 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
5742 fh->elf.root.type = fdh->elf.root.type;
5743 fh->elf.forced_local = 1;
5744 fh->elf.def_regular = fdh->elf.def_regular;
5745 fh->elf.def_dynamic = fdh->elf.def_dynamic;
5748 if (!fh->elf.dynamic)
5750 struct plt_entry *ent;
5752 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
5753 if (ent->plt.refcount > 0)
5759 /* Create a descriptor as undefined if necessary. */
5761 && !bfd_link_executable (info)
5762 && (fh->elf.root.type == bfd_link_hash_undefined
5763 || fh->elf.root.type == bfd_link_hash_undefweak))
5765 fdh = make_fdh (info, fh);
5770 /* We can't support overriding of symbols on a fake descriptor. */
5773 && (fh->elf.root.type == bfd_link_hash_defined
5774 || fh->elf.root.type == bfd_link_hash_defweak))
5775 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
5777 /* Transfer dynamic linking information to the function descriptor. */
5780 fdh->elf.ref_regular |= fh->elf.ref_regular;
5781 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
5782 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
5783 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
5784 fdh->elf.dynamic |= fh->elf.dynamic;
5785 fdh->elf.needs_plt |= (fh->elf.needs_plt
5786 || fh->elf.type == STT_FUNC
5787 || fh->elf.type == STT_GNU_IFUNC);
5788 move_plt_plist (fh, fdh);
5790 if (!fdh->elf.forced_local
5791 && fh->elf.dynindx != -1)
5792 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5796 /* Now that the info is on the function descriptor, clear the
5797 function code sym info. Any function code syms for which we
5798 don't have a definition in a regular file, we force local.
5799 This prevents a shared library from exporting syms that have
5800 been imported from another library. Function code syms that
5801 are really in the library we must leave global to prevent the
5802 linker dragging in a definition from a static library. */
5803 force_local = (!fh->elf.def_regular
5805 || !fdh->elf.def_regular
5806 || fdh->elf.forced_local);
5807 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5812 static const struct sfpr_def_parms save_res_funcs[] =
5814 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
5815 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
5816 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
5817 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
5818 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
5819 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
5820 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
5821 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
5822 { "._savef", 14, 31, savefpr, savefpr1_tail },
5823 { "._restf", 14, 31, restfpr, restfpr1_tail },
5824 { "_savevr_", 20, 31, savevr, savevr_tail },
5825 { "_restvr_", 20, 31, restvr, restvr_tail }
5828 /* Called near the start of bfd_elf_size_dynamic_sections. We use
5829 this hook to a) provide some gcc support functions, and b) transfer
5830 dynamic linking information gathered so far on function code symbol
5831 entries, to their corresponding function descriptor symbol entries. */
5834 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
5835 struct bfd_link_info *info)
5837 struct ppc_link_hash_table *htab;
5839 htab = ppc_hash_table (info);
5843 /* Provide any missing _save* and _rest* functions. */
5844 if (htab->sfpr != NULL)
5848 htab->sfpr->size = 0;
5849 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
5850 if (!sfpr_define (info, &save_res_funcs[i], NULL))
5852 if (htab->sfpr->size == 0)
5853 htab->sfpr->flags |= SEC_EXCLUDE;
5856 if (bfd_link_relocatable (info))
5859 if (htab->elf.hgot != NULL)
5861 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
5862 /* Make .TOC. defined so as to prevent it being made dynamic.
5863 The wrong value here is fixed later in ppc64_elf_set_toc. */
5864 if (!htab->elf.hgot->def_regular
5865 || htab->elf.hgot->root.type != bfd_link_hash_defined)
5867 htab->elf.hgot->root.type = bfd_link_hash_defined;
5868 htab->elf.hgot->root.u.def.value = 0;
5869 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
5870 htab->elf.hgot->def_regular = 1;
5871 htab->elf.hgot->root.linker_def = 1;
5873 htab->elf.hgot->type = STT_OBJECT;
5874 htab->elf.hgot->other
5875 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5878 if (htab->need_func_desc_adj)
5880 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5881 htab->need_func_desc_adj = 0;
5887 /* Find dynamic relocs for H that apply to read-only sections. */
5890 readonly_dynrelocs (struct elf_link_hash_entry *h)
5892 struct ppc_link_hash_entry *eh;
5893 struct elf_dyn_relocs *p;
5895 eh = (struct ppc_link_hash_entry *) h;
5896 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5898 asection *s = p->sec->output_section;
5900 if (s != NULL && (s->flags & SEC_READONLY) != 0)
5906 /* Return true if we have dynamic relocs against H or any of its weak
5907 aliases, that apply to read-only sections. Cannot be used after
5908 size_dynamic_sections. */
5911 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
5913 struct ppc_link_hash_entry *eh;
5915 eh = (struct ppc_link_hash_entry *) h;
5918 if (readonly_dynrelocs (&eh->elf))
5920 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
5922 while (eh != NULL && &eh->elf != h);
5927 /* Return whether EH has pc-relative dynamic relocs. */
5930 pc_dynrelocs (struct ppc_link_hash_entry *eh)
5932 struct elf_dyn_relocs *p;
5934 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5935 if (p->pc_count != 0)
5940 /* Return true if a global entry stub will be created for H. Valid
5941 for ELFv2 before plt entries have been allocated. */
5944 global_entry_stub (struct elf_link_hash_entry *h)
5946 struct plt_entry *pent;
5948 if (!h->pointer_equality_needed
5952 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
5953 if (pent->plt.refcount > 0
5954 && pent->addend == 0)
5960 /* Adjust a symbol defined by a dynamic object and referenced by a
5961 regular object. The current definition is in some section of the
5962 dynamic object, but we're not including those sections. We have to
5963 change the definition to something the rest of the link can
5967 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
5968 struct elf_link_hash_entry *h)
5970 struct ppc_link_hash_table *htab;
5973 htab = ppc_hash_table (info);
5977 /* Deal with function syms. */
5978 if (h->type == STT_FUNC
5979 || h->type == STT_GNU_IFUNC
5982 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
5983 || SYMBOL_CALLS_LOCAL (info, h)
5984 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
5985 /* Discard dyn_relocs when non-pic if we've decided that a
5986 function symbol is local and not an ifunc. We keep dynamic
5987 relocs for ifuncs when local rather than always emitting a
5988 plt call stub for them and defining the symbol on the call
5989 stub. We can't do that for ELFv1 anyway (a function symbol
5990 is defined on a descriptor, not code) and it can be faster at
5991 run-time due to not needing to bounce through a stub. The
5992 dyn_relocs for ifuncs will be applied even in a static
5994 if (!bfd_link_pic (info)
5995 && h->type != STT_GNU_IFUNC
5997 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
5999 /* Clear procedure linkage table information for any symbol that
6000 won't need a .plt entry. */
6001 struct plt_entry *ent;
6002 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6003 if (ent->plt.refcount > 0)
6006 || (h->type != STT_GNU_IFUNC
6008 && (htab->can_convert_all_inline_plt
6009 || (((struct ppc_link_hash_entry *) h)->tls_mask
6010 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6012 h->plt.plist = NULL;
6014 h->pointer_equality_needed = 0;
6016 else if (abiversion (info->output_bfd) >= 2)
6018 /* Taking a function's address in a read/write section
6019 doesn't require us to define the function symbol in the
6020 executable on a global entry stub. A dynamic reloc can
6021 be used instead. The reason we prefer a few more dynamic
6022 relocs is that calling via a global entry stub costs a
6023 few more instructions, and pointer_equality_needed causes
6024 extra work in ld.so when resolving these symbols. */
6025 if (global_entry_stub (h))
6027 if (!readonly_dynrelocs (h))
6029 h->pointer_equality_needed = 0;
6030 /* If we haven't seen a branch reloc and the symbol
6031 isn't an ifunc then we don't need a plt entry. */
6033 h->plt.plist = NULL;
6035 else if (!bfd_link_pic (info))
6036 /* We are going to be defining the function symbol on the
6037 plt stub, so no dyn_relocs needed when non-pic. */
6038 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6041 /* ELFv2 function symbols can't have copy relocs. */
6044 else if (!h->needs_plt
6045 && !readonly_dynrelocs (h))
6047 /* If we haven't seen a branch reloc and the symbol isn't an
6048 ifunc then we don't need a plt entry. */
6049 h->plt.plist = NULL;
6050 h->pointer_equality_needed = 0;
6055 h->plt.plist = NULL;
6057 /* If this is a weak symbol, and there is a real definition, the
6058 processor independent code will have arranged for us to see the
6059 real definition first, and we can just use the same value. */
6060 if (h->is_weakalias)
6062 struct elf_link_hash_entry *def = weakdef (h);
6063 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6064 h->root.u.def.section = def->root.u.def.section;
6065 h->root.u.def.value = def->root.u.def.value;
6066 if (def->root.u.def.section == htab->elf.sdynbss
6067 || def->root.u.def.section == htab->elf.sdynrelro)
6068 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6072 /* If we are creating a shared library, we must presume that the
6073 only references to the symbol are via the global offset table.
6074 For such cases we need not do anything here; the relocations will
6075 be handled correctly by relocate_section. */
6076 if (bfd_link_pic (info))
6079 /* If there are no references to this symbol that do not use the
6080 GOT, we don't need to generate a copy reloc. */
6081 if (!h->non_got_ref)
6084 /* Don't generate a copy reloc for symbols defined in the executable. */
6085 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6087 /* If -z nocopyreloc was given, don't generate them either. */
6088 || info->nocopyreloc
6090 /* If we don't find any dynamic relocs in read-only sections, then
6091 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6092 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
6094 /* Protected variables do not work with .dynbss. The copy in
6095 .dynbss won't be used by the shared library with the protected
6096 definition for the variable. Text relocations are preferable
6097 to an incorrect program. */
6098 || h->protected_def)
6101 if (h->plt.plist != NULL)
6103 /* We should never get here, but unfortunately there are versions
6104 of gcc out there that improperly (for this ABI) put initialized
6105 function pointers, vtable refs and suchlike in read-only
6106 sections. Allow them to proceed, but warn that this might
6107 break at runtime. */
6108 info->callbacks->einfo
6109 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6110 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6111 h->root.root.string);
6114 /* This is a reference to a symbol defined by a dynamic object which
6115 is not a function. */
6117 /* We must allocate the symbol in our .dynbss section, which will
6118 become part of the .bss section of the executable. There will be
6119 an entry for this symbol in the .dynsym section. The dynamic
6120 object will contain position independent code, so all references
6121 from the dynamic object to this symbol will go through the global
6122 offset table. The dynamic linker will use the .dynsym entry to
6123 determine the address it must put in the global offset table, so
6124 both the dynamic object and the regular object will refer to the
6125 same memory location for the variable. */
6126 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6128 s = htab->elf.sdynrelro;
6129 srel = htab->elf.sreldynrelro;
6133 s = htab->elf.sdynbss;
6134 srel = htab->elf.srelbss;
6136 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6138 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6139 linker to copy the initial value out of the dynamic object
6140 and into the runtime process image. */
6141 srel->size += sizeof (Elf64_External_Rela);
6145 /* We no longer want dyn_relocs. */
6146 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6147 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6150 /* If given a function descriptor symbol, hide both the function code
6151 sym and the descriptor. */
6153 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6154 struct elf_link_hash_entry *h,
6155 bfd_boolean force_local)
6157 struct ppc_link_hash_entry *eh;
6158 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6160 eh = (struct ppc_link_hash_entry *) h;
6161 if (eh->is_func_descriptor)
6163 struct ppc_link_hash_entry *fh = eh->oh;
6168 struct elf_link_hash_table *htab = elf_hash_table (info);
6171 /* We aren't supposed to use alloca in BFD because on
6172 systems which do not have alloca the version in libiberty
6173 calls xmalloc, which might cause the program to crash
6174 when it runs out of memory. This function doesn't have a
6175 return status, so there's no way to gracefully return an
6176 error. So cheat. We know that string[-1] can be safely
6177 accessed; It's either a string in an ELF string table,
6178 or allocated in an objalloc structure. */
6180 p = eh->elf.root.root.string - 1;
6183 fh = (struct ppc_link_hash_entry *)
6184 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6187 /* Unfortunately, if it so happens that the string we were
6188 looking for was allocated immediately before this string,
6189 then we overwrote the string terminator. That's the only
6190 reason the lookup should fail. */
6193 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6194 while (q >= eh->elf.root.root.string && *q == *p)
6196 if (q < eh->elf.root.root.string && *p == '.')
6197 fh = (struct ppc_link_hash_entry *)
6198 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6207 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6212 get_sym_h (struct elf_link_hash_entry **hp,
6213 Elf_Internal_Sym **symp,
6215 unsigned char **tls_maskp,
6216 Elf_Internal_Sym **locsymsp,
6217 unsigned long r_symndx,
6220 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6222 if (r_symndx >= symtab_hdr->sh_info)
6224 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6225 struct elf_link_hash_entry *h;
6227 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6228 h = elf_follow_link (h);
6236 if (symsecp != NULL)
6238 asection *symsec = NULL;
6239 if (h->root.type == bfd_link_hash_defined
6240 || h->root.type == bfd_link_hash_defweak)
6241 symsec = h->root.u.def.section;
6245 if (tls_maskp != NULL)
6247 struct ppc_link_hash_entry *eh;
6249 eh = (struct ppc_link_hash_entry *) h;
6250 *tls_maskp = &eh->tls_mask;
6255 Elf_Internal_Sym *sym;
6256 Elf_Internal_Sym *locsyms = *locsymsp;
6258 if (locsyms == NULL)
6260 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6261 if (locsyms == NULL)
6262 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6263 symtab_hdr->sh_info,
6264 0, NULL, NULL, NULL);
6265 if (locsyms == NULL)
6267 *locsymsp = locsyms;
6269 sym = locsyms + r_symndx;
6277 if (symsecp != NULL)
6278 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6280 if (tls_maskp != NULL)
6282 struct got_entry **lgot_ents;
6283 unsigned char *tls_mask;
6286 lgot_ents = elf_local_got_ents (ibfd);
6287 if (lgot_ents != NULL)
6289 struct plt_entry **local_plt = (struct plt_entry **)
6290 (lgot_ents + symtab_hdr->sh_info);
6291 unsigned char *lgot_masks = (unsigned char *)
6292 (local_plt + symtab_hdr->sh_info);
6293 tls_mask = &lgot_masks[r_symndx];
6295 *tls_maskp = tls_mask;
6301 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6302 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6303 type suitable for optimization, and 1 otherwise. */
6306 get_tls_mask (unsigned char **tls_maskp,
6307 unsigned long *toc_symndx,
6308 bfd_vma *toc_addend,
6309 Elf_Internal_Sym **locsymsp,
6310 const Elf_Internal_Rela *rel,
6313 unsigned long r_symndx;
6315 struct elf_link_hash_entry *h;
6316 Elf_Internal_Sym *sym;
6320 r_symndx = ELF64_R_SYM (rel->r_info);
6321 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6324 if ((*tls_maskp != NULL
6325 && (**tls_maskp & TLS_TLS) != 0
6326 && **tls_maskp != (TLS_TLS | TLS_MARK))
6328 || ppc64_elf_section_data (sec) == NULL
6329 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6332 /* Look inside a TOC section too. */
6335 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6336 off = h->root.u.def.value;
6339 off = sym->st_value;
6340 off += rel->r_addend;
6341 BFD_ASSERT (off % 8 == 0);
6342 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6343 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6344 if (toc_symndx != NULL)
6345 *toc_symndx = r_symndx;
6346 if (toc_addend != NULL)
6347 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6348 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6350 if ((h == NULL || is_static_defined (h))
6351 && (next_r == -1 || next_r == -2))
6356 /* Find (or create) an entry in the tocsave hash table. */
6358 static struct tocsave_entry *
6359 tocsave_find (struct ppc_link_hash_table *htab,
6360 enum insert_option insert,
6361 Elf_Internal_Sym **local_syms,
6362 const Elf_Internal_Rela *irela,
6365 unsigned long r_indx;
6366 struct elf_link_hash_entry *h;
6367 Elf_Internal_Sym *sym;
6368 struct tocsave_entry ent, *p;
6370 struct tocsave_entry **slot;
6372 r_indx = ELF64_R_SYM (irela->r_info);
6373 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6375 if (ent.sec == NULL || ent.sec->output_section == NULL)
6378 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
6383 ent.offset = h->root.u.def.value;
6385 ent.offset = sym->st_value;
6386 ent.offset += irela->r_addend;
6388 hash = tocsave_htab_hash (&ent);
6389 slot = ((struct tocsave_entry **)
6390 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6396 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6405 /* Adjust all global syms defined in opd sections. In gcc generated
6406 code for the old ABI, these will already have been done. */
6409 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6411 struct ppc_link_hash_entry *eh;
6413 struct _opd_sec_data *opd;
6415 if (h->root.type == bfd_link_hash_indirect)
6418 if (h->root.type != bfd_link_hash_defined
6419 && h->root.type != bfd_link_hash_defweak)
6422 eh = (struct ppc_link_hash_entry *) h;
6423 if (eh->adjust_done)
6426 sym_sec = eh->elf.root.u.def.section;
6427 opd = get_opd_info (sym_sec);
6428 if (opd != NULL && opd->adjust != NULL)
6430 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
6433 /* This entry has been deleted. */
6434 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6437 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6438 if (discarded_section (dsec))
6440 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6444 eh->elf.root.u.def.value = 0;
6445 eh->elf.root.u.def.section = dsec;
6448 eh->elf.root.u.def.value += adjust;
6449 eh->adjust_done = 1;
6454 /* Handles decrementing dynamic reloc counts for the reloc specified by
6455 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
6456 have already been determined. */
6459 dec_dynrel_count (bfd_vma r_info,
6461 struct bfd_link_info *info,
6462 Elf_Internal_Sym **local_syms,
6463 struct elf_link_hash_entry *h,
6464 Elf_Internal_Sym *sym)
6466 enum elf_ppc64_reloc_type r_type;
6467 asection *sym_sec = NULL;
6469 /* Can this reloc be dynamic? This switch, and later tests here
6470 should be kept in sync with the code in check_relocs. */
6471 r_type = ELF64_R_TYPE (r_info);
6477 case R_PPC64_TPREL16:
6478 case R_PPC64_TPREL16_LO:
6479 case R_PPC64_TPREL16_HI:
6480 case R_PPC64_TPREL16_HA:
6481 case R_PPC64_TPREL16_DS:
6482 case R_PPC64_TPREL16_LO_DS:
6483 case R_PPC64_TPREL16_HIGH:
6484 case R_PPC64_TPREL16_HIGHA:
6485 case R_PPC64_TPREL16_HIGHER:
6486 case R_PPC64_TPREL16_HIGHERA:
6487 case R_PPC64_TPREL16_HIGHEST:
6488 case R_PPC64_TPREL16_HIGHESTA:
6489 case R_PPC64_TPREL64:
6490 case R_PPC64_DTPMOD64:
6491 case R_PPC64_DTPREL64:
6492 case R_PPC64_ADDR64:
6496 case R_PPC64_ADDR14:
6497 case R_PPC64_ADDR14_BRNTAKEN:
6498 case R_PPC64_ADDR14_BRTAKEN:
6499 case R_PPC64_ADDR16:
6500 case R_PPC64_ADDR16_DS:
6501 case R_PPC64_ADDR16_HA:
6502 case R_PPC64_ADDR16_HI:
6503 case R_PPC64_ADDR16_HIGH:
6504 case R_PPC64_ADDR16_HIGHA:
6505 case R_PPC64_ADDR16_HIGHER:
6506 case R_PPC64_ADDR16_HIGHERA:
6507 case R_PPC64_ADDR16_HIGHEST:
6508 case R_PPC64_ADDR16_HIGHESTA:
6509 case R_PPC64_ADDR16_LO:
6510 case R_PPC64_ADDR16_LO_DS:
6511 case R_PPC64_ADDR24:
6512 case R_PPC64_ADDR32:
6513 case R_PPC64_UADDR16:
6514 case R_PPC64_UADDR32:
6515 case R_PPC64_UADDR64:
6520 if (local_syms != NULL)
6522 unsigned long r_symndx;
6523 bfd *ibfd = sec->owner;
6525 r_symndx = ELF64_R_SYM (r_info);
6526 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6530 if ((bfd_link_pic (info)
6531 && (must_be_dyn_reloc (info, r_type)
6533 && (!SYMBOLIC_BIND (info, h)
6534 || h->root.type == bfd_link_hash_defweak
6535 || !h->def_regular))))
6536 || (ELIMINATE_COPY_RELOCS
6537 && !bfd_link_pic (info)
6539 && (h->root.type == bfd_link_hash_defweak
6540 || !h->def_regular)))
6547 struct elf_dyn_relocs *p;
6548 struct elf_dyn_relocs **pp;
6549 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6551 /* elf_gc_sweep may have already removed all dyn relocs associated
6552 with local syms for a given section. Also, symbol flags are
6553 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6554 report a dynreloc miscount. */
6555 if (*pp == NULL && info->gc_sections)
6558 while ((p = *pp) != NULL)
6562 if (!must_be_dyn_reloc (info, r_type))
6574 struct ppc_dyn_relocs *p;
6575 struct ppc_dyn_relocs **pp;
6577 bfd_boolean is_ifunc;
6579 if (local_syms == NULL)
6580 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6581 if (sym_sec == NULL)
6584 vpp = &elf_section_data (sym_sec)->local_dynrel;
6585 pp = (struct ppc_dyn_relocs **) vpp;
6587 if (*pp == NULL && info->gc_sections)
6590 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6591 while ((p = *pp) != NULL)
6593 if (p->sec == sec && p->ifunc == is_ifunc)
6604 /* xgettext:c-format */
6605 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6607 bfd_set_error (bfd_error_bad_value);
6611 /* Remove unused Official Procedure Descriptor entries. Currently we
6612 only remove those associated with functions in discarded link-once
6613 sections, or weakly defined functions that have been overridden. It
6614 would be possible to remove many more entries for statically linked
6618 ppc64_elf_edit_opd (struct bfd_link_info *info)
6621 bfd_boolean some_edited = FALSE;
6622 asection *need_pad = NULL;
6623 struct ppc_link_hash_table *htab;
6625 htab = ppc_hash_table (info);
6629 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6632 Elf_Internal_Rela *relstart, *rel, *relend;
6633 Elf_Internal_Shdr *symtab_hdr;
6634 Elf_Internal_Sym *local_syms;
6635 struct _opd_sec_data *opd;
6636 bfd_boolean need_edit, add_aux_fields, broken;
6637 bfd_size_type cnt_16b = 0;
6639 if (!is_ppc64_elf (ibfd))
6642 sec = bfd_get_section_by_name (ibfd, ".opd");
6643 if (sec == NULL || sec->size == 0)
6646 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6649 if (sec->output_section == bfd_abs_section_ptr)
6652 /* Look through the section relocs. */
6653 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6657 symtab_hdr = &elf_symtab_hdr (ibfd);
6659 /* Read the relocations. */
6660 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6662 if (relstart == NULL)
6665 /* First run through the relocs to check they are sane, and to
6666 determine whether we need to edit this opd section. */
6670 relend = relstart + sec->reloc_count;
6671 for (rel = relstart; rel < relend; )
6673 enum elf_ppc64_reloc_type r_type;
6674 unsigned long r_symndx;
6676 struct elf_link_hash_entry *h;
6677 Elf_Internal_Sym *sym;
6680 /* .opd contains an array of 16 or 24 byte entries. We're
6681 only interested in the reloc pointing to a function entry
6683 offset = rel->r_offset;
6684 if (rel + 1 == relend
6685 || rel[1].r_offset != offset + 8)
6687 /* If someone messes with .opd alignment then after a
6688 "ld -r" we might have padding in the middle of .opd.
6689 Also, there's nothing to prevent someone putting
6690 something silly in .opd with the assembler. No .opd
6691 optimization for them! */
6694 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
6699 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6700 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6703 /* xgettext:c-format */
6704 (_("%pB: unexpected reloc type %u in .opd section"),
6710 r_symndx = ELF64_R_SYM (rel->r_info);
6711 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6715 if (sym_sec == NULL || sym_sec->owner == NULL)
6717 const char *sym_name;
6719 sym_name = h->root.root.string;
6721 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6725 /* xgettext:c-format */
6726 (_("%pB: undefined sym `%s' in .opd section"),
6732 /* opd entries are always for functions defined in the
6733 current input bfd. If the symbol isn't defined in the
6734 input bfd, then we won't be using the function in this
6735 bfd; It must be defined in a linkonce section in another
6736 bfd, or is weak. It's also possible that we are
6737 discarding the function due to a linker script /DISCARD/,
6738 which we test for via the output_section. */
6739 if (sym_sec->owner != ibfd
6740 || sym_sec->output_section == bfd_abs_section_ptr)
6744 if (rel + 1 == relend
6745 || (rel + 2 < relend
6746 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
6751 if (sec->size == offset + 24)
6756 if (sec->size == offset + 16)
6763 else if (rel + 1 < relend
6764 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
6765 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
6767 if (rel[0].r_offset == offset + 16)
6769 else if (rel[0].r_offset != offset + 24)
6776 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
6778 if (!broken && (need_edit || add_aux_fields))
6780 Elf_Internal_Rela *write_rel;
6781 Elf_Internal_Shdr *rel_hdr;
6782 bfd_byte *rptr, *wptr;
6783 bfd_byte *new_contents;
6786 new_contents = NULL;
6787 amt = OPD_NDX (sec->size) * sizeof (long);
6788 opd = &ppc64_elf_section_data (sec)->u.opd;
6789 opd->adjust = bfd_zalloc (sec->owner, amt);
6790 if (opd->adjust == NULL)
6793 /* This seems a waste of time as input .opd sections are all
6794 zeros as generated by gcc, but I suppose there's no reason
6795 this will always be so. We might start putting something in
6796 the third word of .opd entries. */
6797 if ((sec->flags & SEC_IN_MEMORY) == 0)
6800 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6805 if (local_syms != NULL
6806 && symtab_hdr->contents != (unsigned char *) local_syms)
6808 if (elf_section_data (sec)->relocs != relstart)
6812 sec->contents = loc;
6813 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6816 elf_section_data (sec)->relocs = relstart;
6818 new_contents = sec->contents;
6821 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
6822 if (new_contents == NULL)
6826 wptr = new_contents;
6827 rptr = sec->contents;
6828 write_rel = relstart;
6829 for (rel = relstart; rel < relend; )
6831 unsigned long r_symndx;
6833 struct elf_link_hash_entry *h;
6834 struct ppc_link_hash_entry *fdh = NULL;
6835 Elf_Internal_Sym *sym;
6837 Elf_Internal_Rela *next_rel;
6840 r_symndx = ELF64_R_SYM (rel->r_info);
6841 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6846 if (next_rel + 1 == relend
6847 || (next_rel + 2 < relend
6848 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
6851 /* See if the .opd entry is full 24 byte or
6852 16 byte (with fd_aux entry overlapped with next
6855 if (next_rel == relend)
6857 if (sec->size == rel->r_offset + 16)
6860 else if (next_rel->r_offset == rel->r_offset + 16)
6864 && h->root.root.string[0] == '.')
6866 fdh = ((struct ppc_link_hash_entry *) h)->oh;
6869 fdh = ppc_follow_link (fdh);
6870 if (fdh->elf.root.type != bfd_link_hash_defined
6871 && fdh->elf.root.type != bfd_link_hash_defweak)
6876 skip = (sym_sec->owner != ibfd
6877 || sym_sec->output_section == bfd_abs_section_ptr);
6880 if (fdh != NULL && sym_sec->owner == ibfd)
6882 /* Arrange for the function descriptor sym
6884 fdh->elf.root.u.def.value = 0;
6885 fdh->elf.root.u.def.section = sym_sec;
6887 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
6889 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
6894 if (!dec_dynrel_count (rel->r_info, sec, info,
6898 if (++rel == next_rel)
6901 r_symndx = ELF64_R_SYM (rel->r_info);
6902 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6909 /* We'll be keeping this opd entry. */
6914 /* Redefine the function descriptor symbol to
6915 this location in the opd section. It is
6916 necessary to update the value here rather
6917 than using an array of adjustments as we do
6918 for local symbols, because various places
6919 in the generic ELF code use the value
6920 stored in u.def.value. */
6921 fdh->elf.root.u.def.value = wptr - new_contents;
6922 fdh->adjust_done = 1;
6925 /* Local syms are a bit tricky. We could
6926 tweak them as they can be cached, but
6927 we'd need to look through the local syms
6928 for the function descriptor sym which we
6929 don't have at the moment. So keep an
6930 array of adjustments. */
6931 adjust = (wptr - new_contents) - (rptr - sec->contents);
6932 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
6935 memcpy (wptr, rptr, opd_ent_size);
6936 wptr += opd_ent_size;
6937 if (add_aux_fields && opd_ent_size == 16)
6939 memset (wptr, '\0', 8);
6943 /* We need to adjust any reloc offsets to point to the
6945 for ( ; rel != next_rel; ++rel)
6947 rel->r_offset += adjust;
6948 if (write_rel != rel)
6949 memcpy (write_rel, rel, sizeof (*rel));
6954 rptr += opd_ent_size;
6957 sec->size = wptr - new_contents;
6958 sec->reloc_count = write_rel - relstart;
6961 free (sec->contents);
6962 sec->contents = new_contents;
6965 /* Fudge the header size too, as this is used later in
6966 elf_bfd_final_link if we are emitting relocs. */
6967 rel_hdr = _bfd_elf_single_rel_hdr (sec);
6968 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
6971 else if (elf_section_data (sec)->relocs != relstart)
6974 if (local_syms != NULL
6975 && symtab_hdr->contents != (unsigned char *) local_syms)
6977 if (!info->keep_memory)
6980 symtab_hdr->contents = (unsigned char *) local_syms;
6985 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
6987 /* If we are doing a final link and the last .opd entry is just 16 byte
6988 long, add a 8 byte padding after it. */
6989 if (need_pad != NULL && !bfd_link_relocatable (info))
6993 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
6995 BFD_ASSERT (need_pad->size > 0);
6997 p = bfd_malloc (need_pad->size + 8);
7001 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7002 p, 0, need_pad->size))
7005 need_pad->contents = p;
7006 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7010 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7014 need_pad->contents = p;
7017 memset (need_pad->contents + need_pad->size, 0, 8);
7018 need_pad->size += 8;
7024 /* Analyze inline PLT call relocations to see whether calls to locally
7025 defined functions can be converted to direct calls. */
7028 ppc64_elf_inline_plt (struct bfd_link_info *info)
7030 struct ppc_link_hash_table *htab;
7033 bfd_vma low_vma, high_vma, limit;
7035 htab = ppc_hash_table (info);
7039 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7040 reduced somewhat to cater for possible stubs that might be added
7041 between the call and its destination. */
7042 if (htab->params->group_size < 0)
7044 limit = -htab->params->group_size;
7050 limit = htab->params->group_size;
7057 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7058 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7060 if (low_vma > sec->vma)
7062 if (high_vma < sec->vma + sec->size)
7063 high_vma = sec->vma + sec->size;
7066 /* If a "bl" can reach anywhere in local code sections, then we can
7067 convert all inline PLT sequences to direct calls when the symbol
7069 if (high_vma - low_vma < limit)
7071 htab->can_convert_all_inline_plt = 1;
7075 /* Otherwise, go looking through relocs for cases where a direct
7076 call won't reach. Mark the symbol on any such reloc to disable
7077 the optimization and keep the PLT entry as it seems likely that
7078 this will be better than creating trampolines. Note that this
7079 will disable the optimization for all inline PLT calls to a
7080 particular symbol, not just those that won't reach. The
7081 difficulty in doing a more precise optimization is that the
7082 linker needs to make a decision depending on whether a
7083 particular R_PPC64_PLTCALL insn can be turned into a direct
7084 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7085 the sequence, and there is nothing that ties those relocs
7086 together except their symbol. */
7088 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7090 Elf_Internal_Shdr *symtab_hdr;
7091 Elf_Internal_Sym *local_syms;
7093 if (!is_ppc64_elf (ibfd))
7097 symtab_hdr = &elf_symtab_hdr (ibfd);
7099 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7100 if (ppc64_elf_section_data (sec)->has_pltcall
7101 && !bfd_is_abs_section (sec->output_section))
7103 Elf_Internal_Rela *relstart, *rel, *relend;
7105 /* Read the relocations. */
7106 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7108 if (relstart == NULL)
7111 relend = relstart + sec->reloc_count;
7112 for (rel = relstart; rel < relend; )
7114 enum elf_ppc64_reloc_type r_type;
7115 unsigned long r_symndx;
7117 struct elf_link_hash_entry *h;
7118 Elf_Internal_Sym *sym;
7119 unsigned char *tls_maskp;
7121 r_type = ELF64_R_TYPE (rel->r_info);
7122 if (r_type != R_PPC64_PLTCALL)
7125 r_symndx = ELF64_R_SYM (rel->r_info);
7126 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7129 if (elf_section_data (sec)->relocs != relstart)
7131 if (local_syms != NULL
7132 && symtab_hdr->contents != (bfd_byte *) local_syms)
7137 if (sym_sec != NULL && sym_sec->output_section != NULL)
7141 to = h->root.u.def.value;
7144 to += (rel->r_addend
7145 + sym_sec->output_offset
7146 + sym_sec->output_section->vma);
7147 from = (rel->r_offset
7148 + sec->output_offset
7149 + sec->output_section->vma);
7150 if (to - from + limit < 2 * limit)
7151 *tls_maskp &= ~PLT_KEEP;
7154 if (elf_section_data (sec)->relocs != relstart)
7158 if (local_syms != NULL
7159 && symtab_hdr->contents != (unsigned char *) local_syms)
7161 if (!info->keep_memory)
7164 symtab_hdr->contents = (unsigned char *) local_syms;
7171 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7174 ppc64_elf_tls_setup (struct bfd_link_info *info)
7176 struct ppc_link_hash_table *htab;
7178 htab = ppc_hash_table (info);
7182 if (abiversion (info->output_bfd) == 1)
7185 if (htab->params->no_multi_toc)
7186 htab->do_multi_toc = 0;
7187 else if (!htab->do_multi_toc)
7188 htab->params->no_multi_toc = 1;
7190 /* Default to --no-plt-localentry, as this option can cause problems
7191 with symbol interposition. For example, glibc libpthread.so and
7192 libc.so duplicate many pthread symbols, with a fallback
7193 implementation in libc.so. In some cases the fallback does more
7194 work than the pthread implementation. __pthread_condattr_destroy
7195 is one such symbol: the libpthread.so implementation is
7196 localentry:0 while the libc.so implementation is localentry:8.
7197 An app that "cleverly" uses dlopen to only load necessary
7198 libraries at runtime may omit loading libpthread.so when not
7199 running multi-threaded, which then results in the libc.so
7200 fallback symbols being used and ld.so complaining. Now there
7201 are workarounds in ld (see non_zero_localentry) to detect the
7202 pthread situation, but that may not be the only case where
7203 --plt-localentry can cause trouble. */
7204 if (htab->params->plt_localentry0 < 0)
7205 htab->params->plt_localentry0 = 0;
7206 if (htab->params->plt_localentry0
7207 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7208 FALSE, FALSE, FALSE) == NULL)
7210 (_("warning: --plt-localentry is especially dangerous without "
7211 "ld.so support to detect ABI violations"));
7213 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7214 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7215 FALSE, FALSE, TRUE));
7216 /* Move dynamic linking info to the function descriptor sym. */
7217 if (htab->tls_get_addr != NULL)
7218 func_desc_adjust (&htab->tls_get_addr->elf, info);
7219 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7220 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7221 FALSE, FALSE, TRUE));
7222 if (htab->params->tls_get_addr_opt)
7224 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7226 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7227 FALSE, FALSE, TRUE);
7229 func_desc_adjust (opt, info);
7230 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7231 FALSE, FALSE, TRUE);
7233 && (opt_fd->root.type == bfd_link_hash_defined
7234 || opt_fd->root.type == bfd_link_hash_defweak))
7236 /* If glibc supports an optimized __tls_get_addr call stub,
7237 signalled by the presence of __tls_get_addr_opt, and we'll
7238 be calling __tls_get_addr via a plt call stub, then
7239 make __tls_get_addr point to __tls_get_addr_opt. */
7240 tga_fd = &htab->tls_get_addr_fd->elf;
7241 if (htab->elf.dynamic_sections_created
7243 && (tga_fd->type == STT_FUNC
7244 || tga_fd->needs_plt)
7245 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7246 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
7248 struct plt_entry *ent;
7250 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7251 if (ent->plt.refcount > 0)
7255 tga_fd->root.type = bfd_link_hash_indirect;
7256 tga_fd->root.u.i.link = &opt_fd->root;
7257 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7259 if (opt_fd->dynindx != -1)
7261 /* Use __tls_get_addr_opt in dynamic relocations. */
7262 opt_fd->dynindx = -1;
7263 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7264 opt_fd->dynstr_index);
7265 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7268 htab->tls_get_addr_fd
7269 = (struct ppc_link_hash_entry *) opt_fd;
7270 tga = &htab->tls_get_addr->elf;
7271 if (opt != NULL && tga != NULL)
7273 tga->root.type = bfd_link_hash_indirect;
7274 tga->root.u.i.link = &opt->root;
7275 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7277 _bfd_elf_link_hash_hide_symbol (info, opt,
7279 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7281 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7282 htab->tls_get_addr_fd->is_func_descriptor = 1;
7283 if (htab->tls_get_addr != NULL)
7285 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7286 htab->tls_get_addr->is_func = 1;
7291 else if (htab->params->tls_get_addr_opt < 0)
7292 htab->params->tls_get_addr_opt = 0;
7294 return _bfd_elf_tls_setup (info->output_bfd, info);
7297 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7301 branch_reloc_hash_match (const bfd *ibfd,
7302 const Elf_Internal_Rela *rel,
7303 const struct ppc_link_hash_entry *hash1,
7304 const struct ppc_link_hash_entry *hash2)
7306 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7307 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7308 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7310 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7312 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7313 struct elf_link_hash_entry *h;
7315 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7316 h = elf_follow_link (h);
7317 if (h == &hash1->elf || h == &hash2->elf)
7323 /* Run through all the TLS relocs looking for optimization
7324 opportunities. The linker has been hacked (see ppc64elf.em) to do
7325 a preliminary section layout so that we know the TLS segment
7326 offsets. We can't optimize earlier because some optimizations need
7327 to know the tp offset, and we need to optimize before allocating
7328 dynamic relocations. */
7331 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7335 struct ppc_link_hash_table *htab;
7336 unsigned char *toc_ref;
7339 if (!bfd_link_executable (info))
7342 htab = ppc_hash_table (info);
7346 /* Make two passes over the relocs. On the first pass, mark toc
7347 entries involved with tls relocs, and check that tls relocs
7348 involved in setting up a tls_get_addr call are indeed followed by
7349 such a call. If they are not, we can't do any tls optimization.
7350 On the second pass twiddle tls_mask flags to notify
7351 relocate_section that optimization can be done, and adjust got
7352 and plt refcounts. */
7354 for (pass = 0; pass < 2; ++pass)
7355 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7357 Elf_Internal_Sym *locsyms = NULL;
7358 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7360 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7361 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7363 Elf_Internal_Rela *relstart, *rel, *relend;
7364 bfd_boolean found_tls_get_addr_arg = 0;
7366 /* Read the relocations. */
7367 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7369 if (relstart == NULL)
7375 relend = relstart + sec->reloc_count;
7376 for (rel = relstart; rel < relend; rel++)
7378 enum elf_ppc64_reloc_type r_type;
7379 unsigned long r_symndx;
7380 struct elf_link_hash_entry *h;
7381 Elf_Internal_Sym *sym;
7383 unsigned char *tls_mask;
7384 unsigned char tls_set, tls_clear, tls_type = 0;
7386 bfd_boolean ok_tprel, is_local;
7387 long toc_ref_index = 0;
7388 int expecting_tls_get_addr = 0;
7389 bfd_boolean ret = FALSE;
7391 r_symndx = ELF64_R_SYM (rel->r_info);
7392 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7396 if (elf_section_data (sec)->relocs != relstart)
7398 if (toc_ref != NULL)
7401 && (elf_symtab_hdr (ibfd).contents
7402 != (unsigned char *) locsyms))
7409 if (h->root.type == bfd_link_hash_defined
7410 || h->root.type == bfd_link_hash_defweak)
7411 value = h->root.u.def.value;
7412 else if (h->root.type == bfd_link_hash_undefweak)
7416 found_tls_get_addr_arg = 0;
7421 /* Symbols referenced by TLS relocs must be of type
7422 STT_TLS. So no need for .opd local sym adjust. */
7423 value = sym->st_value;
7432 && h->root.type == bfd_link_hash_undefweak)
7434 else if (sym_sec != NULL
7435 && sym_sec->output_section != NULL)
7437 value += sym_sec->output_offset;
7438 value += sym_sec->output_section->vma;
7439 value -= htab->elf.tls_sec->vma;
7440 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7441 < (bfd_vma) 1 << 32);
7445 r_type = ELF64_R_TYPE (rel->r_info);
7446 /* If this section has old-style __tls_get_addr calls
7447 without marker relocs, then check that each
7448 __tls_get_addr call reloc is preceded by a reloc
7449 that conceivably belongs to the __tls_get_addr arg
7450 setup insn. If we don't find matching arg setup
7451 relocs, don't do any tls optimization. */
7453 && sec->has_tls_get_addr_call
7455 && (h == &htab->tls_get_addr->elf
7456 || h == &htab->tls_get_addr_fd->elf)
7457 && !found_tls_get_addr_arg
7458 && is_branch_reloc (r_type))
7460 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7461 "TLS optimization disabled\n"),
7462 ibfd, sec, rel->r_offset);
7467 found_tls_get_addr_arg = 0;
7470 case R_PPC64_GOT_TLSLD16:
7471 case R_PPC64_GOT_TLSLD16_LO:
7472 expecting_tls_get_addr = 1;
7473 found_tls_get_addr_arg = 1;
7476 case R_PPC64_GOT_TLSLD16_HI:
7477 case R_PPC64_GOT_TLSLD16_HA:
7478 /* These relocs should never be against a symbol
7479 defined in a shared lib. Leave them alone if
7480 that turns out to be the case. */
7487 tls_type = TLS_TLS | TLS_LD;
7490 case R_PPC64_GOT_TLSGD16:
7491 case R_PPC64_GOT_TLSGD16_LO:
7492 expecting_tls_get_addr = 1;
7493 found_tls_get_addr_arg = 1;
7496 case R_PPC64_GOT_TLSGD16_HI:
7497 case R_PPC64_GOT_TLSGD16_HA:
7503 tls_set = TLS_TLS | TLS_TPRELGD;
7505 tls_type = TLS_TLS | TLS_GD;
7508 case R_PPC64_GOT_TPREL16_DS:
7509 case R_PPC64_GOT_TPREL16_LO_DS:
7510 case R_PPC64_GOT_TPREL16_HI:
7511 case R_PPC64_GOT_TPREL16_HA:
7516 tls_clear = TLS_TPREL;
7517 tls_type = TLS_TLS | TLS_TPREL;
7524 if (rel + 1 < relend
7525 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7528 && (ELF64_R_TYPE (rel[1].r_info)
7531 r_symndx = ELF64_R_SYM (rel[1].r_info);
7532 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
7537 struct plt_entry *ent = NULL;
7539 for (ent = h->plt.plist;
7542 if (ent->addend == rel[1].r_addend)
7546 && ent->plt.refcount > 0)
7547 ent->plt.refcount -= 1;
7552 found_tls_get_addr_arg = 1;
7557 case R_PPC64_TOC16_LO:
7558 if (sym_sec == NULL || sym_sec != toc)
7561 /* Mark this toc entry as referenced by a TLS
7562 code sequence. We can do that now in the
7563 case of R_PPC64_TLS, and after checking for
7564 tls_get_addr for the TOC16 relocs. */
7565 if (toc_ref == NULL)
7567 = bfd_zmalloc (toc->output_section->rawsize / 8);
7568 if (toc_ref == NULL)
7572 value = h->root.u.def.value;
7574 value = sym->st_value;
7575 value += rel->r_addend;
7578 BFD_ASSERT (value < toc->size
7579 && toc->output_offset % 8 == 0);
7580 toc_ref_index = (value + toc->output_offset) / 8;
7581 if (r_type == R_PPC64_TLS
7582 || r_type == R_PPC64_TLSGD
7583 || r_type == R_PPC64_TLSLD)
7585 toc_ref[toc_ref_index] = 1;
7589 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7594 expecting_tls_get_addr = 2;
7597 case R_PPC64_TPREL64:
7601 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7606 tls_set = TLS_EXPLICIT;
7607 tls_clear = TLS_TPREL;
7612 case R_PPC64_DTPMOD64:
7616 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7618 if (rel + 1 < relend
7620 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7621 && rel[1].r_offset == rel->r_offset + 8)
7625 tls_set = TLS_EXPLICIT | TLS_GD;
7628 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7637 tls_set = TLS_EXPLICIT;
7648 if (!expecting_tls_get_addr
7649 || !sec->has_tls_get_addr_call)
7652 if (rel + 1 < relend
7653 && branch_reloc_hash_match (ibfd, rel + 1,
7655 htab->tls_get_addr_fd))
7657 if (expecting_tls_get_addr == 2)
7659 /* Check for toc tls entries. */
7660 unsigned char *toc_tls;
7663 retval = get_tls_mask (&toc_tls, NULL, NULL,
7668 if (toc_tls != NULL)
7670 if ((*toc_tls & TLS_TLS) != 0
7671 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
7672 found_tls_get_addr_arg = 1;
7674 toc_ref[toc_ref_index] = 1;
7680 /* Uh oh, we didn't find the expected call. We
7681 could just mark this symbol to exclude it
7682 from tls optimization but it's safer to skip
7683 the entire optimization. */
7684 /* xgettext:c-format */
7685 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
7686 "TLS optimization disabled\n"),
7687 ibfd, sec, rel->r_offset);
7692 /* If we don't have old-style __tls_get_addr calls
7693 without TLSGD/TLSLD marker relocs, and we haven't
7694 found a new-style __tls_get_addr call with a
7695 marker for this symbol, then we either have a
7696 broken object file or an -mlongcall style
7697 indirect call to __tls_get_addr without a marker.
7698 Disable optimization in this case. */
7699 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
7700 && (tls_set & TLS_EXPLICIT) == 0
7701 && !sec->has_tls_get_addr_call
7702 && ((*tls_mask & (TLS_TLS | TLS_MARK))
7703 != (TLS_TLS | TLS_MARK)))
7706 if (expecting_tls_get_addr)
7708 struct plt_entry *ent = NULL;
7710 if (htab->tls_get_addr != NULL)
7711 for (ent = htab->tls_get_addr->elf.plt.plist;
7714 if (ent->addend == 0)
7717 if (ent == NULL && htab->tls_get_addr_fd != NULL)
7718 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7721 if (ent->addend == 0)
7725 && ent->plt.refcount > 0)
7726 ent->plt.refcount -= 1;
7732 if ((tls_set & TLS_EXPLICIT) == 0)
7734 struct got_entry *ent;
7736 /* Adjust got entry for this reloc. */
7740 ent = elf_local_got_ents (ibfd)[r_symndx];
7742 for (; ent != NULL; ent = ent->next)
7743 if (ent->addend == rel->r_addend
7744 && ent->owner == ibfd
7745 && ent->tls_type == tls_type)
7752 /* We managed to get rid of a got entry. */
7753 if (ent->got.refcount > 0)
7754 ent->got.refcount -= 1;
7759 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7760 we'll lose one or two dyn relocs. */
7761 if (!dec_dynrel_count (rel->r_info, sec, info,
7765 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7767 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7773 *tls_mask |= tls_set;
7774 *tls_mask &= ~tls_clear;
7777 if (elf_section_data (sec)->relocs != relstart)
7782 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7784 if (!info->keep_memory)
7787 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7791 if (toc_ref != NULL)
7793 htab->do_tls_opt = 1;
7797 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7798 the values of any global symbols in a toc section that has been
7799 edited. Globals in toc sections should be a rarity, so this function
7800 sets a flag if any are found in toc sections other than the one just
7801 edited, so that further hash table traversals can be avoided. */
7803 struct adjust_toc_info
7806 unsigned long *skip;
7807 bfd_boolean global_toc_syms;
7810 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7813 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7815 struct ppc_link_hash_entry *eh;
7816 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7819 if (h->root.type != bfd_link_hash_defined
7820 && h->root.type != bfd_link_hash_defweak)
7823 eh = (struct ppc_link_hash_entry *) h;
7824 if (eh->adjust_done)
7827 if (eh->elf.root.u.def.section == toc_inf->toc)
7829 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7830 i = toc_inf->toc->rawsize >> 3;
7832 i = eh->elf.root.u.def.value >> 3;
7834 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
7837 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7840 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
7841 eh->elf.root.u.def.value = (bfd_vma) i << 3;
7844 eh->elf.root.u.def.value -= toc_inf->skip[i];
7845 eh->adjust_done = 1;
7847 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7848 toc_inf->global_toc_syms = TRUE;
7853 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
7854 on a _LO variety toc/got reloc. */
7857 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
7859 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7860 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
7861 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7862 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7863 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7864 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7865 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7866 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7867 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7868 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7869 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7870 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7871 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7872 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7873 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
7874 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7875 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7876 /* Exclude lfqu by testing reloc. If relocs are ever
7877 defined for the reduced D field in psq_lu then those
7878 will need testing too. */
7879 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
7880 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7882 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7883 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7884 /* Exclude stfqu. psq_stu as above for psq_lu. */
7885 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
7886 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7887 && (insn & 1) == 0));
7890 /* Examine all relocs referencing .toc sections in order to remove
7891 unused .toc entries. */
7894 ppc64_elf_edit_toc (struct bfd_link_info *info)
7897 struct adjust_toc_info toc_inf;
7898 struct ppc_link_hash_table *htab = ppc_hash_table (info);
7900 htab->do_toc_opt = 1;
7901 toc_inf.global_toc_syms = TRUE;
7902 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7904 asection *toc, *sec;
7905 Elf_Internal_Shdr *symtab_hdr;
7906 Elf_Internal_Sym *local_syms;
7907 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
7908 unsigned long *skip, *drop;
7909 unsigned char *used;
7910 unsigned char *keep, last, some_unused;
7912 if (!is_ppc64_elf (ibfd))
7915 toc = bfd_get_section_by_name (ibfd, ".toc");
7918 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
7919 || discarded_section (toc))
7924 symtab_hdr = &elf_symtab_hdr (ibfd);
7926 /* Look at sections dropped from the final link. */
7929 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7931 if (sec->reloc_count == 0
7932 || !discarded_section (sec)
7933 || get_opd_info (sec)
7934 || (sec->flags & SEC_ALLOC) == 0
7935 || (sec->flags & SEC_DEBUGGING) != 0)
7938 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7939 if (relstart == NULL)
7942 /* Run through the relocs to see which toc entries might be
7944 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7946 enum elf_ppc64_reloc_type r_type;
7947 unsigned long r_symndx;
7949 struct elf_link_hash_entry *h;
7950 Elf_Internal_Sym *sym;
7953 r_type = ELF64_R_TYPE (rel->r_info);
7960 case R_PPC64_TOC16_LO:
7961 case R_PPC64_TOC16_HI:
7962 case R_PPC64_TOC16_HA:
7963 case R_PPC64_TOC16_DS:
7964 case R_PPC64_TOC16_LO_DS:
7968 r_symndx = ELF64_R_SYM (rel->r_info);
7969 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7977 val = h->root.u.def.value;
7979 val = sym->st_value;
7980 val += rel->r_addend;
7982 if (val >= toc->size)
7985 /* Anything in the toc ought to be aligned to 8 bytes.
7986 If not, don't mark as unused. */
7992 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
7997 skip[val >> 3] = ref_from_discarded;
8000 if (elf_section_data (sec)->relocs != relstart)
8004 /* For largetoc loads of address constants, we can convert
8005 . addis rx,2,addr@got@ha
8006 . ld ry,addr@got@l(rx)
8008 . addis rx,2,addr@toc@ha
8009 . addi ry,rx,addr@toc@l
8010 when addr is within 2G of the toc pointer. This then means
8011 that the word storing "addr" in the toc is no longer needed. */
8013 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8014 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8015 && toc->reloc_count != 0)
8017 /* Read toc relocs. */
8018 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8020 if (toc_relocs == NULL)
8023 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8025 enum elf_ppc64_reloc_type r_type;
8026 unsigned long r_symndx;
8028 struct elf_link_hash_entry *h;
8029 Elf_Internal_Sym *sym;
8032 r_type = ELF64_R_TYPE (rel->r_info);
8033 if (r_type != R_PPC64_ADDR64)
8036 r_symndx = ELF64_R_SYM (rel->r_info);
8037 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8042 || sym_sec->output_section == NULL
8043 || discarded_section (sym_sec))
8046 if (!SYMBOL_REFERENCES_LOCAL (info, h))
8051 if (h->type == STT_GNU_IFUNC)
8053 val = h->root.u.def.value;
8057 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8059 val = sym->st_value;
8061 val += rel->r_addend;
8062 val += sym_sec->output_section->vma + sym_sec->output_offset;
8064 /* We don't yet know the exact toc pointer value, but we
8065 know it will be somewhere in the toc section. Don't
8066 optimize if the difference from any possible toc
8067 pointer is outside [ff..f80008000, 7fff7fff]. */
8068 addr = toc->output_section->vma + TOC_BASE_OFF;
8069 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8072 addr = toc->output_section->vma + toc->output_section->rawsize;
8073 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8078 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8083 skip[rel->r_offset >> 3]
8084 |= can_optimize | ((rel - toc_relocs) << 2);
8091 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8095 if (local_syms != NULL
8096 && symtab_hdr->contents != (unsigned char *) local_syms)
8100 && elf_section_data (sec)->relocs != relstart)
8102 if (toc_relocs != NULL
8103 && elf_section_data (toc)->relocs != toc_relocs)
8110 /* Now check all kept sections that might reference the toc.
8111 Check the toc itself last. */
8112 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8115 sec = (sec == toc ? NULL
8116 : sec->next == NULL ? toc
8117 : sec->next == toc && toc->next ? toc->next
8122 if (sec->reloc_count == 0
8123 || discarded_section (sec)
8124 || get_opd_info (sec)
8125 || (sec->flags & SEC_ALLOC) == 0
8126 || (sec->flags & SEC_DEBUGGING) != 0)
8129 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8131 if (relstart == NULL)
8137 /* Mark toc entries referenced as used. */
8141 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8143 enum elf_ppc64_reloc_type r_type;
8144 unsigned long r_symndx;
8146 struct elf_link_hash_entry *h;
8147 Elf_Internal_Sym *sym;
8149 enum {no_check, check_lo, check_ha} insn_check;
8151 r_type = ELF64_R_TYPE (rel->r_info);
8155 insn_check = no_check;
8158 case R_PPC64_GOT_TLSLD16_HA:
8159 case R_PPC64_GOT_TLSGD16_HA:
8160 case R_PPC64_GOT_TPREL16_HA:
8161 case R_PPC64_GOT_DTPREL16_HA:
8162 case R_PPC64_GOT16_HA:
8163 case R_PPC64_TOC16_HA:
8164 insn_check = check_ha;
8167 case R_PPC64_GOT_TLSLD16_LO:
8168 case R_PPC64_GOT_TLSGD16_LO:
8169 case R_PPC64_GOT_TPREL16_LO_DS:
8170 case R_PPC64_GOT_DTPREL16_LO_DS:
8171 case R_PPC64_GOT16_LO:
8172 case R_PPC64_GOT16_LO_DS:
8173 case R_PPC64_TOC16_LO:
8174 case R_PPC64_TOC16_LO_DS:
8175 insn_check = check_lo;
8179 if (insn_check != no_check)
8181 bfd_vma off = rel->r_offset & ~3;
8182 unsigned char buf[4];
8185 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8190 insn = bfd_get_32 (ibfd, buf);
8191 if (insn_check == check_lo
8192 ? !ok_lo_toc_insn (insn, r_type)
8193 : ((insn & ((0x3f << 26) | 0x1f << 16))
8194 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8198 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8199 sprintf (str, "%#08x", insn);
8200 info->callbacks->einfo
8201 /* xgettext:c-format */
8202 (_("%H: toc optimization is not supported for"
8203 " %s instruction\n"),
8204 ibfd, sec, rel->r_offset & ~3, str);
8211 case R_PPC64_TOC16_LO:
8212 case R_PPC64_TOC16_HI:
8213 case R_PPC64_TOC16_HA:
8214 case R_PPC64_TOC16_DS:
8215 case R_PPC64_TOC16_LO_DS:
8216 /* In case we're taking addresses of toc entries. */
8217 case R_PPC64_ADDR64:
8224 r_symndx = ELF64_R_SYM (rel->r_info);
8225 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8236 val = h->root.u.def.value;
8238 val = sym->st_value;
8239 val += rel->r_addend;
8241 if (val >= toc->size)
8244 if ((skip[val >> 3] & can_optimize) != 0)
8251 case R_PPC64_TOC16_HA:
8254 case R_PPC64_TOC16_LO_DS:
8255 off = rel->r_offset;
8256 off += (bfd_big_endian (ibfd) ? -2 : 3);
8257 if (!bfd_get_section_contents (ibfd, sec, &opc,
8263 if ((opc & (0x3f << 2)) == (58u << 2))
8268 /* Wrong sort of reloc, or not a ld. We may
8269 as well clear ref_from_discarded too. */
8276 /* For the toc section, we only mark as used if this
8277 entry itself isn't unused. */
8278 else if ((used[rel->r_offset >> 3]
8279 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8282 /* Do all the relocs again, to catch reference
8291 if (elf_section_data (sec)->relocs != relstart)
8295 /* Merge the used and skip arrays. Assume that TOC
8296 doublewords not appearing as either used or unused belong
8297 to an entry more than one doubleword in size. */
8298 for (drop = skip, keep = used, last = 0, some_unused = 0;
8299 drop < skip + (toc->size + 7) / 8;
8304 *drop &= ~ref_from_discarded;
8305 if ((*drop & can_optimize) != 0)
8309 else if ((*drop & ref_from_discarded) != 0)
8312 last = ref_from_discarded;
8322 bfd_byte *contents, *src;
8324 Elf_Internal_Sym *sym;
8325 bfd_boolean local_toc_syms = FALSE;
8327 /* Shuffle the toc contents, and at the same time convert the
8328 skip array from booleans into offsets. */
8329 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8332 elf_section_data (toc)->this_hdr.contents = contents;
8334 for (src = contents, off = 0, drop = skip;
8335 src < contents + toc->size;
8338 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8343 memcpy (src - off, src, 8);
8347 toc->rawsize = toc->size;
8348 toc->size = src - contents - off;
8350 /* Adjust addends for relocs against the toc section sym,
8351 and optimize any accesses we can. */
8352 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8354 if (sec->reloc_count == 0
8355 || discarded_section (sec))
8358 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8360 if (relstart == NULL)
8363 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8365 enum elf_ppc64_reloc_type r_type;
8366 unsigned long r_symndx;
8368 struct elf_link_hash_entry *h;
8371 r_type = ELF64_R_TYPE (rel->r_info);
8378 case R_PPC64_TOC16_LO:
8379 case R_PPC64_TOC16_HI:
8380 case R_PPC64_TOC16_HA:
8381 case R_PPC64_TOC16_DS:
8382 case R_PPC64_TOC16_LO_DS:
8383 case R_PPC64_ADDR64:
8387 r_symndx = ELF64_R_SYM (rel->r_info);
8388 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8396 val = h->root.u.def.value;
8399 val = sym->st_value;
8401 local_toc_syms = TRUE;
8404 val += rel->r_addend;
8406 if (val > toc->rawsize)
8408 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8410 else if ((skip[val >> 3] & can_optimize) != 0)
8412 Elf_Internal_Rela *tocrel
8413 = toc_relocs + (skip[val >> 3] >> 2);
8414 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8418 case R_PPC64_TOC16_HA:
8419 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8422 case R_PPC64_TOC16_LO_DS:
8423 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8427 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8429 info->callbacks->einfo
8430 /* xgettext:c-format */
8431 (_("%H: %s references "
8432 "optimized away TOC entry\n"),
8433 ibfd, sec, rel->r_offset,
8434 ppc64_elf_howto_table[r_type]->name);
8435 bfd_set_error (bfd_error_bad_value);
8438 rel->r_addend = tocrel->r_addend;
8439 elf_section_data (sec)->relocs = relstart;
8443 if (h != NULL || sym->st_value != 0)
8446 rel->r_addend -= skip[val >> 3];
8447 elf_section_data (sec)->relocs = relstart;
8450 if (elf_section_data (sec)->relocs != relstart)
8454 /* We shouldn't have local or global symbols defined in the TOC,
8455 but handle them anyway. */
8456 if (local_syms != NULL)
8457 for (sym = local_syms;
8458 sym < local_syms + symtab_hdr->sh_info;
8460 if (sym->st_value != 0
8461 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8465 if (sym->st_value > toc->rawsize)
8466 i = toc->rawsize >> 3;
8468 i = sym->st_value >> 3;
8470 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8474 (_("%s defined on removed toc entry"),
8475 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8478 while ((skip[i] & (ref_from_discarded | can_optimize)));
8479 sym->st_value = (bfd_vma) i << 3;
8482 sym->st_value -= skip[i];
8483 symtab_hdr->contents = (unsigned char *) local_syms;
8486 /* Adjust any global syms defined in this toc input section. */
8487 if (toc_inf.global_toc_syms)
8490 toc_inf.skip = skip;
8491 toc_inf.global_toc_syms = FALSE;
8492 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8496 if (toc->reloc_count != 0)
8498 Elf_Internal_Shdr *rel_hdr;
8499 Elf_Internal_Rela *wrel;
8502 /* Remove unused toc relocs, and adjust those we keep. */
8503 if (toc_relocs == NULL)
8504 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8506 if (toc_relocs == NULL)
8510 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8511 if ((skip[rel->r_offset >> 3]
8512 & (ref_from_discarded | can_optimize)) == 0)
8514 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8515 wrel->r_info = rel->r_info;
8516 wrel->r_addend = rel->r_addend;
8519 else if (!dec_dynrel_count (rel->r_info, toc, info,
8520 &local_syms, NULL, NULL))
8523 elf_section_data (toc)->relocs = toc_relocs;
8524 toc->reloc_count = wrel - toc_relocs;
8525 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8526 sz = rel_hdr->sh_entsize;
8527 rel_hdr->sh_size = toc->reloc_count * sz;
8530 else if (toc_relocs != NULL
8531 && elf_section_data (toc)->relocs != toc_relocs)
8534 if (local_syms != NULL
8535 && symtab_hdr->contents != (unsigned char *) local_syms)
8537 if (!info->keep_memory)
8540 symtab_hdr->contents = (unsigned char *) local_syms;
8548 /* Return true iff input section I references the TOC using
8549 instructions limited to +/-32k offsets. */
8552 ppc64_elf_has_small_toc_reloc (asection *i)
8554 return (is_ppc64_elf (i->owner)
8555 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8558 /* Allocate space for one GOT entry. */
8561 allocate_got (struct elf_link_hash_entry *h,
8562 struct bfd_link_info *info,
8563 struct got_entry *gent)
8565 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8566 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8567 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8569 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8570 ? 2 : 1) * sizeof (Elf64_External_Rela);
8571 asection *got = ppc64_elf_tdata (gent->owner)->got;
8573 gent->got.offset = got->size;
8574 got->size += entsize;
8576 if (h->type == STT_GNU_IFUNC)
8578 htab->elf.irelplt->size += rentsize;
8579 htab->got_reli_size += rentsize;
8581 else if (((bfd_link_pic (info)
8582 && !((gent->tls_type & TLS_TPREL) != 0
8583 && bfd_link_executable (info)
8584 && SYMBOL_REFERENCES_LOCAL (info, h)))
8585 || (htab->elf.dynamic_sections_created
8587 && !SYMBOL_REFERENCES_LOCAL (info, h)))
8588 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8590 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8591 relgot->size += rentsize;
8595 /* This function merges got entries in the same toc group. */
8598 merge_got_entries (struct got_entry **pent)
8600 struct got_entry *ent, *ent2;
8602 for (ent = *pent; ent != NULL; ent = ent->next)
8603 if (!ent->is_indirect)
8604 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8605 if (!ent2->is_indirect
8606 && ent2->addend == ent->addend
8607 && ent2->tls_type == ent->tls_type
8608 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8610 ent2->is_indirect = TRUE;
8611 ent2->got.ent = ent;
8615 /* If H is undefined, make it dynamic if that makes sense. */
8618 ensure_undef_dynamic (struct bfd_link_info *info,
8619 struct elf_link_hash_entry *h)
8621 struct elf_link_hash_table *htab = elf_hash_table (info);
8623 if (htab->dynamic_sections_created
8624 && ((info->dynamic_undefined_weak != 0
8625 && h->root.type == bfd_link_hash_undefweak)
8626 || h->root.type == bfd_link_hash_undefined)
8629 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
8630 return bfd_elf_link_record_dynamic_symbol (info, h);
8634 /* Allocate space in .plt, .got and associated reloc sections for
8638 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8640 struct bfd_link_info *info;
8641 struct ppc_link_hash_table *htab;
8643 struct ppc_link_hash_entry *eh;
8644 struct got_entry **pgent, *gent;
8646 if (h->root.type == bfd_link_hash_indirect)
8649 info = (struct bfd_link_info *) inf;
8650 htab = ppc_hash_table (info);
8654 eh = (struct ppc_link_hash_entry *) h;
8655 /* Run through the TLS GD got entries first if we're changing them
8657 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
8658 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8659 if (gent->got.refcount > 0
8660 && (gent->tls_type & TLS_GD) != 0)
8662 /* This was a GD entry that has been converted to TPREL. If
8663 there happens to be a TPREL entry we can use that one. */
8664 struct got_entry *ent;
8665 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8666 if (ent->got.refcount > 0
8667 && (ent->tls_type & TLS_TPREL) != 0
8668 && ent->addend == gent->addend
8669 && ent->owner == gent->owner)
8671 gent->got.refcount = 0;
8675 /* If not, then we'll be using our own TPREL entry. */
8676 if (gent->got.refcount != 0)
8677 gent->tls_type = TLS_TLS | TLS_TPREL;
8680 /* Remove any list entry that won't generate a word in the GOT before
8681 we call merge_got_entries. Otherwise we risk merging to empty
8683 pgent = &h->got.glist;
8684 while ((gent = *pgent) != NULL)
8685 if (gent->got.refcount > 0)
8687 if ((gent->tls_type & TLS_LD) != 0
8690 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8691 *pgent = gent->next;
8694 pgent = &gent->next;
8697 *pgent = gent->next;
8699 if (!htab->do_multi_toc)
8700 merge_got_entries (&h->got.glist);
8702 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8703 if (!gent->is_indirect)
8705 /* Make sure this symbol is output as a dynamic symbol. */
8706 if (!ensure_undef_dynamic (info, h))
8709 if (!is_ppc64_elf (gent->owner))
8712 allocate_got (h, info, gent);
8715 /* If no dynamic sections we can't have dynamic relocs, except for
8716 IFUNCs which are handled even in static executables. */
8717 if (!htab->elf.dynamic_sections_created
8718 && h->type != STT_GNU_IFUNC)
8719 eh->dyn_relocs = NULL;
8721 /* Discard relocs on undefined symbols that must be local. */
8722 else if (h->root.type == bfd_link_hash_undefined
8723 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8724 eh->dyn_relocs = NULL;
8726 /* Also discard relocs on undefined weak syms with non-default
8727 visibility, or when dynamic_undefined_weak says so. */
8728 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8729 eh->dyn_relocs = NULL;
8731 if (eh->dyn_relocs != NULL)
8733 struct elf_dyn_relocs *p, **pp;
8735 /* In the shared -Bsymbolic case, discard space allocated for
8736 dynamic pc-relative relocs against symbols which turn out to
8737 be defined in regular objects. For the normal shared case,
8738 discard space for relocs that have become local due to symbol
8739 visibility changes. */
8741 if (bfd_link_pic (info))
8743 /* Relocs that use pc_count are those that appear on a call
8744 insn, or certain REL relocs (see must_be_dyn_reloc) that
8745 can be generated via assembly. We want calls to
8746 protected symbols to resolve directly to the function
8747 rather than going via the plt. If people want function
8748 pointer comparisons to work as expected then they should
8749 avoid writing weird assembly. */
8750 if (SYMBOL_CALLS_LOCAL (info, h))
8752 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
8754 p->count -= p->pc_count;
8763 if (eh->dyn_relocs != NULL)
8765 /* Make sure this symbol is output as a dynamic symbol. */
8766 if (!ensure_undef_dynamic (info, h))
8770 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
8772 /* For the non-pic case, discard space for relocs against
8773 symbols which turn out to need copy relocs or are not
8775 if (h->dynamic_adjusted
8777 && !ELF_COMMON_DEF_P (h))
8779 /* Make sure this symbol is output as a dynamic symbol. */
8780 if (!ensure_undef_dynamic (info, h))
8783 if (h->dynindx == -1)
8784 eh->dyn_relocs = NULL;
8787 eh->dyn_relocs = NULL;
8790 /* Finally, allocate space. */
8791 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8793 asection *sreloc = elf_section_data (p->sec)->sreloc;
8794 if (eh->elf.type == STT_GNU_IFUNC)
8795 sreloc = htab->elf.irelplt;
8796 sreloc->size += p->count * sizeof (Elf64_External_Rela);
8800 /* We might need a PLT entry when the symbol
8803 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
8804 d) has plt16 relocs and we are linking statically. */
8805 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
8806 || h->type == STT_GNU_IFUNC
8807 || (h->needs_plt && h->dynamic_adjusted)
8810 && !htab->elf.dynamic_sections_created
8811 && !htab->can_convert_all_inline_plt
8812 && (((struct ppc_link_hash_entry *) h)->tls_mask
8813 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
8815 struct plt_entry *pent;
8816 bfd_boolean doneone = FALSE;
8817 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8818 if (pent->plt.refcount > 0)
8820 if (!htab->elf.dynamic_sections_created
8821 || h->dynindx == -1)
8823 if (h->type == STT_GNU_IFUNC)
8826 pent->plt.offset = s->size;
8827 s->size += PLT_ENTRY_SIZE (htab);
8828 s = htab->elf.irelplt;
8833 pent->plt.offset = s->size;
8834 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
8835 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
8840 /* If this is the first .plt entry, make room for the special
8844 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
8846 pent->plt.offset = s->size;
8848 /* Make room for this entry. */
8849 s->size += PLT_ENTRY_SIZE (htab);
8851 /* Make room for the .glink code. */
8854 s->size += GLINK_PLTRESOLVE_SIZE (htab);
8857 /* We need bigger stubs past index 32767. */
8858 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
8865 /* We also need to make an entry in the .rela.plt section. */
8866 s = htab->elf.srelplt;
8869 s->size += sizeof (Elf64_External_Rela);
8873 pent->plt.offset = (bfd_vma) -1;
8876 h->plt.plist = NULL;
8882 h->plt.plist = NULL;
8889 #define PPC_LO(v) ((v) & 0xffff)
8890 #define PPC_HI(v) (((v) >> 16) & 0xffff)
8891 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
8893 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
8894 to set up space for global entry stubs. These are put in glink,
8895 after the branch table. */
8898 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
8900 struct bfd_link_info *info;
8901 struct ppc_link_hash_table *htab;
8902 struct plt_entry *pent;
8905 if (h->root.type == bfd_link_hash_indirect)
8908 if (!h->pointer_equality_needed)
8915 htab = ppc_hash_table (info);
8919 s = htab->global_entry;
8920 plt = htab->elf.splt;
8921 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8922 if (pent->plt.offset != (bfd_vma) -1
8923 && pent->addend == 0)
8925 /* For ELFv2, if this symbol is not defined in a regular file
8926 and we are not generating a shared library or pie, then we
8927 need to define the symbol in the executable on a call stub.
8928 This is to avoid text relocations. */
8929 bfd_vma off, stub_align, stub_off, stub_size;
8930 unsigned int align_power;
8934 if (htab->params->plt_stub_align >= 0)
8935 align_power = htab->params->plt_stub_align;
8937 align_power = -htab->params->plt_stub_align;
8938 /* Setting section alignment is delayed until we know it is
8939 non-empty. Otherwise the .text output section will be
8940 aligned at least to plt_stub_align even when no global
8941 entry stubs are needed. */
8942 if (s->alignment_power < align_power)
8943 s->alignment_power = align_power;
8944 stub_align = (bfd_vma) 1 << align_power;
8945 if (htab->params->plt_stub_align >= 0
8946 || ((((stub_off + stub_size - 1) & -stub_align)
8947 - (stub_off & -stub_align))
8948 > ((stub_size - 1) & -stub_align)))
8949 stub_off = (stub_off + stub_align - 1) & -stub_align;
8950 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
8951 off -= stub_off + s->output_offset + s->output_section->vma;
8952 /* Note that for --plt-stub-align negative we have a possible
8953 dependency between stub offset and size. Break that
8954 dependency by assuming the max stub size when calculating
8956 if (PPC_HA (off) == 0)
8958 h->root.type = bfd_link_hash_defined;
8959 h->root.u.def.section = s;
8960 h->root.u.def.value = stub_off;
8961 s->size = stub_off + stub_size;
8967 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
8968 read-only sections. */
8971 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
8975 if (h->root.type == bfd_link_hash_indirect)
8978 sec = readonly_dynrelocs (h);
8981 struct bfd_link_info *info = (struct bfd_link_info *) inf;
8983 info->flags |= DF_TEXTREL;
8984 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
8985 " in read-only section `%pA'\n"),
8986 sec->owner, h->root.root.string, sec);
8988 /* Not an error, just cut short the traversal. */
8994 /* Set the sizes of the dynamic sections. */
8997 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
8998 struct bfd_link_info *info)
9000 struct ppc_link_hash_table *htab;
9005 struct got_entry *first_tlsld;
9007 htab = ppc_hash_table (info);
9011 dynobj = htab->elf.dynobj;
9015 if (htab->elf.dynamic_sections_created)
9017 /* Set the contents of the .interp section to the interpreter. */
9018 if (bfd_link_executable (info) && !info->nointerp)
9020 s = bfd_get_linker_section (dynobj, ".interp");
9023 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9024 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9028 /* Set up .got offsets for local syms, and space for local dynamic
9030 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9032 struct got_entry **lgot_ents;
9033 struct got_entry **end_lgot_ents;
9034 struct plt_entry **local_plt;
9035 struct plt_entry **end_local_plt;
9036 unsigned char *lgot_masks;
9037 bfd_size_type locsymcount;
9038 Elf_Internal_Shdr *symtab_hdr;
9040 if (!is_ppc64_elf (ibfd))
9043 for (s = ibfd->sections; s != NULL; s = s->next)
9045 struct ppc_dyn_relocs *p;
9047 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9049 if (!bfd_is_abs_section (p->sec)
9050 && bfd_is_abs_section (p->sec->output_section))
9052 /* Input section has been discarded, either because
9053 it is a copy of a linkonce section or due to
9054 linker script /DISCARD/, so we'll be discarding
9057 else if (p->count != 0)
9059 asection *srel = elf_section_data (p->sec)->sreloc;
9061 srel = htab->elf.irelplt;
9062 srel->size += p->count * sizeof (Elf64_External_Rela);
9063 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9064 info->flags |= DF_TEXTREL;
9069 lgot_ents = elf_local_got_ents (ibfd);
9073 symtab_hdr = &elf_symtab_hdr (ibfd);
9074 locsymcount = symtab_hdr->sh_info;
9075 end_lgot_ents = lgot_ents + locsymcount;
9076 local_plt = (struct plt_entry **) end_lgot_ents;
9077 end_local_plt = local_plt + locsymcount;
9078 lgot_masks = (unsigned char *) end_local_plt;
9079 s = ppc64_elf_tdata (ibfd)->got;
9080 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9082 struct got_entry **pent, *ent;
9085 while ((ent = *pent) != NULL)
9086 if (ent->got.refcount > 0)
9088 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9090 ppc64_tlsld_got (ibfd)->got.refcount += 1;
9095 unsigned int ent_size = 8;
9096 unsigned int rel_size = sizeof (Elf64_External_Rela);
9098 ent->got.offset = s->size;
9099 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9104 s->size += ent_size;
9105 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9107 htab->elf.irelplt->size += rel_size;
9108 htab->got_reli_size += rel_size;
9110 else if (bfd_link_pic (info)
9111 && !((ent->tls_type & TLS_TPREL) != 0
9112 && bfd_link_executable (info)))
9114 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9115 srel->size += rel_size;
9124 /* Allocate space for plt calls to local syms. */
9125 lgot_masks = (unsigned char *) end_local_plt;
9126 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
9128 struct plt_entry *ent;
9130 for (ent = *local_plt; ent != NULL; ent = ent->next)
9131 if (ent->plt.refcount > 0)
9133 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9136 ent->plt.offset = s->size;
9137 s->size += PLT_ENTRY_SIZE (htab);
9138 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9140 else if (htab->can_convert_all_inline_plt
9141 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
9142 ent->plt.offset = (bfd_vma) -1;
9146 ent->plt.offset = s->size;
9147 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9148 if (bfd_link_pic (info))
9149 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9153 ent->plt.offset = (bfd_vma) -1;
9157 /* Allocate global sym .plt and .got entries, and space for global
9158 sym dynamic relocs. */
9159 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9161 if (!htab->opd_abi && !bfd_link_pic (info))
9162 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
9165 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9167 struct got_entry *ent;
9169 if (!is_ppc64_elf (ibfd))
9172 ent = ppc64_tlsld_got (ibfd);
9173 if (ent->got.refcount > 0)
9175 if (!htab->do_multi_toc && first_tlsld != NULL)
9177 ent->is_indirect = TRUE;
9178 ent->got.ent = first_tlsld;
9182 if (first_tlsld == NULL)
9184 s = ppc64_elf_tdata (ibfd)->got;
9185 ent->got.offset = s->size;
9188 if (bfd_link_pic (info))
9190 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9191 srel->size += sizeof (Elf64_External_Rela);
9196 ent->got.offset = (bfd_vma) -1;
9199 /* We now have determined the sizes of the various dynamic sections.
9200 Allocate memory for them. */
9202 for (s = dynobj->sections; s != NULL; s = s->next)
9204 if ((s->flags & SEC_LINKER_CREATED) == 0)
9207 if (s == htab->brlt || s == htab->relbrlt)
9208 /* These haven't been allocated yet; don't strip. */
9210 else if (s == htab->elf.sgot
9211 || s == htab->elf.splt
9212 || s == htab->elf.iplt
9213 || s == htab->pltlocal
9215 || s == htab->global_entry
9216 || s == htab->elf.sdynbss
9217 || s == htab->elf.sdynrelro)
9219 /* Strip this section if we don't need it; see the
9222 else if (s == htab->glink_eh_frame)
9224 if (!bfd_is_abs_section (s->output_section))
9225 /* Not sized yet. */
9228 else if (CONST_STRNEQ (s->name, ".rela"))
9232 if (s != htab->elf.srelplt)
9235 /* We use the reloc_count field as a counter if we need
9236 to copy relocs into the output file. */
9242 /* It's not one of our sections, so don't allocate space. */
9248 /* If we don't need this section, strip it from the
9249 output file. This is mostly to handle .rela.bss and
9250 .rela.plt. We must create both sections in
9251 create_dynamic_sections, because they must be created
9252 before the linker maps input sections to output
9253 sections. The linker does that before
9254 adjust_dynamic_symbol is called, and it is that
9255 function which decides whether anything needs to go
9256 into these sections. */
9257 s->flags |= SEC_EXCLUDE;
9261 if (bfd_is_abs_section (s->output_section))
9262 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9265 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9268 /* Allocate memory for the section contents. We use bfd_zalloc
9269 here in case unused entries are not reclaimed before the
9270 section's contents are written out. This should not happen,
9271 but this way if it does we get a R_PPC64_NONE reloc in .rela
9272 sections instead of garbage.
9273 We also rely on the section contents being zero when writing
9274 the GOT and .dynrelro. */
9275 s->contents = bfd_zalloc (dynobj, s->size);
9276 if (s->contents == NULL)
9280 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9282 if (!is_ppc64_elf (ibfd))
9285 s = ppc64_elf_tdata (ibfd)->got;
9286 if (s != NULL && s != htab->elf.sgot)
9289 s->flags |= SEC_EXCLUDE;
9292 s->contents = bfd_zalloc (ibfd, s->size);
9293 if (s->contents == NULL)
9297 s = ppc64_elf_tdata (ibfd)->relgot;
9301 s->flags |= SEC_EXCLUDE;
9304 s->contents = bfd_zalloc (ibfd, s->size);
9305 if (s->contents == NULL)
9313 if (htab->elf.dynamic_sections_created)
9315 bfd_boolean tls_opt;
9317 /* Add some entries to the .dynamic section. We fill in the
9318 values later, in ppc64_elf_finish_dynamic_sections, but we
9319 must add the entries now so that we get the correct size for
9320 the .dynamic section. The DT_DEBUG entry is filled in by the
9321 dynamic linker and used by the debugger. */
9322 #define add_dynamic_entry(TAG, VAL) \
9323 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9325 if (bfd_link_executable (info))
9327 if (!add_dynamic_entry (DT_DEBUG, 0))
9331 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
9333 if (!add_dynamic_entry (DT_PLTGOT, 0)
9334 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9335 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9336 || !add_dynamic_entry (DT_JMPREL, 0)
9337 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9341 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
9343 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9344 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9348 tls_opt = (htab->params->tls_get_addr_opt
9349 && htab->tls_get_addr_fd != NULL
9350 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9351 if (tls_opt || !htab->opd_abi)
9353 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9359 if (!add_dynamic_entry (DT_RELA, 0)
9360 || !add_dynamic_entry (DT_RELASZ, 0)
9361 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9364 /* If any dynamic relocs apply to a read-only section,
9365 then we need a DT_TEXTREL entry. */
9366 if ((info->flags & DF_TEXTREL) == 0)
9367 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
9369 if ((info->flags & DF_TEXTREL) != 0)
9371 if (!add_dynamic_entry (DT_TEXTREL, 0))
9376 #undef add_dynamic_entry
9381 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
9384 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
9386 if (h->plt.plist != NULL
9388 && !h->pointer_equality_needed)
9391 return _bfd_elf_hash_symbol (h);
9394 /* Determine the type of stub needed, if any, for a call. */
9396 static inline enum ppc_stub_type
9397 ppc_type_of_stub (asection *input_sec,
9398 const Elf_Internal_Rela *rel,
9399 struct ppc_link_hash_entry **hash,
9400 struct plt_entry **plt_ent,
9401 bfd_vma destination,
9402 unsigned long local_off)
9404 struct ppc_link_hash_entry *h = *hash;
9406 bfd_vma branch_offset;
9407 bfd_vma max_branch_offset;
9408 enum elf_ppc64_reloc_type r_type;
9412 struct plt_entry *ent;
9413 struct ppc_link_hash_entry *fdh = h;
9415 && h->oh->is_func_descriptor)
9417 fdh = ppc_follow_link (h->oh);
9421 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9422 if (ent->addend == rel->r_addend
9423 && ent->plt.offset != (bfd_vma) -1)
9426 return ppc_stub_plt_call;
9429 /* Here, we know we don't have a plt entry. If we don't have a
9430 either a defined function descriptor or a defined entry symbol
9431 in a regular object file, then it is pointless trying to make
9432 any other type of stub. */
9433 if (!is_static_defined (&fdh->elf)
9434 && !is_static_defined (&h->elf))
9435 return ppc_stub_none;
9437 else if (elf_local_got_ents (input_sec->owner) != NULL)
9439 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9440 struct plt_entry **local_plt = (struct plt_entry **)
9441 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9442 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9444 if (local_plt[r_symndx] != NULL)
9446 struct plt_entry *ent;
9448 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9449 if (ent->addend == rel->r_addend
9450 && ent->plt.offset != (bfd_vma) -1)
9453 return ppc_stub_plt_call;
9458 /* Determine where the call point is. */
9459 location = (input_sec->output_offset
9460 + input_sec->output_section->vma
9463 branch_offset = destination - location;
9464 r_type = ELF64_R_TYPE (rel->r_info);
9466 /* Determine if a long branch stub is needed. */
9467 max_branch_offset = 1 << 25;
9468 if (r_type == R_PPC64_REL14
9469 || r_type == R_PPC64_REL14_BRTAKEN
9470 || r_type == R_PPC64_REL14_BRNTAKEN)
9471 max_branch_offset = 1 << 15;
9473 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
9474 /* We need a stub. Figure out whether a long_branch or plt_branch
9476 return ppc_stub_long_branch;
9478 return ppc_stub_none;
9481 /* Builds a 64-bit offset in r12 then adds it to r11 (LOAD false) or
9482 loads r12 from r11+r12 (LOAD true).
9483 . lis %r12,xxx-1b@highest
9484 . ori %r12,xxx-1b@higher
9486 . oris %r12,%r12,xxx-1b@hi
9487 . ori %r12,%r12,xxx-1b@l
9488 . add %r12,%r11,%r12 */
9491 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
9493 if (off + 0x8000 < 0x10000)
9496 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
9498 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
9501 else if (off + 0x80008000ULL < 0x100000000ULL)
9503 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
9506 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
9508 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
9513 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9515 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
9520 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
9522 if (((off >> 32) & 0xffff) != 0)
9524 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
9528 if (((off >> 32) & 0xffffffffULL) != 0)
9530 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
9533 if (PPC_HI (off) != 0)
9535 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
9538 if (PPC_LO (off) != 0)
9540 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
9544 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
9546 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
9553 size_offset (bfd_vma off)
9556 if (off + 0x8000 < 0x10000)
9558 else if (off + 0x80008000ULL < 0x100000000ULL)
9562 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9567 if (((off >> 32) & 0xffff) != 0)
9570 if (((off >> 32) & 0xffffffffULL) != 0)
9572 if (PPC_HI (off) != 0)
9574 if (PPC_LO (off) != 0)
9581 /* Emit .eh_frame opcode to advance pc by DELTA. */
9584 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
9588 *eh++ = DW_CFA_advance_loc + delta;
9589 else if (delta < 256)
9591 *eh++ = DW_CFA_advance_loc1;
9594 else if (delta < 65536)
9596 *eh++ = DW_CFA_advance_loc2;
9597 bfd_put_16 (abfd, delta, eh);
9602 *eh++ = DW_CFA_advance_loc4;
9603 bfd_put_32 (abfd, delta, eh);
9609 /* Size of required .eh_frame opcode to advance pc by DELTA. */
9612 eh_advance_size (unsigned int delta)
9615 /* DW_CFA_advance_loc+[1..63]. */
9617 if (delta < 256 * 4)
9618 /* DW_CFA_advance_loc1, byte. */
9620 if (delta < 65536 * 4)
9621 /* DW_CFA_advance_loc2, 2 bytes. */
9623 /* DW_CFA_advance_loc4, 4 bytes. */
9627 /* With power7 weakly ordered memory model, it is possible for ld.so
9628 to update a plt entry in one thread and have another thread see a
9629 stale zero toc entry. To avoid this we need some sort of acquire
9630 barrier in the call stub. One solution is to make the load of the
9631 toc word seem to appear to depend on the load of the function entry
9632 word. Another solution is to test for r2 being zero, and branch to
9633 the appropriate glink entry if so.
9635 . fake dep barrier compare
9636 . ld 12,xxx(2) ld 12,xxx(2)
9638 . xor 11,12,12 ld 2,xxx+8(2)
9639 . add 2,2,11 cmpldi 2,0
9640 . ld 2,xxx+8(2) bnectr+
9641 . bctr b <glink_entry>
9643 The solution involving the compare turns out to be faster, so
9644 that's what we use unless the branch won't reach. */
9646 #define ALWAYS_USE_FAKE_DEP 0
9647 #define ALWAYS_EMIT_R2SAVE 0
9649 static inline unsigned int
9650 plt_stub_size (struct ppc_link_hash_table *htab,
9651 struct ppc_stub_hash_entry *stub_entry,
9656 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
9658 size = 24 + size_offset (off);
9659 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
9665 if (ALWAYS_EMIT_R2SAVE
9666 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9668 if (PPC_HA (off) != 0)
9673 if (htab->params->plt_static_chain)
9675 if (htab->params->plt_thread_safe
9676 && htab->elf.dynamic_sections_created
9677 && stub_entry->h != NULL
9678 && stub_entry->h->elf.dynindx != -1)
9680 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
9683 if (stub_entry->h != NULL
9684 && (stub_entry->h == htab->tls_get_addr_fd
9685 || stub_entry->h == htab->tls_get_addr)
9686 && htab->params->tls_get_addr_opt)
9689 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
9695 /* Depending on the sign of plt_stub_align:
9696 If positive, return the padding to align to a 2**plt_stub_align
9698 If negative, if this stub would cross fewer 2**plt_stub_align
9699 boundaries if we align, then return the padding needed to do so. */
9701 static inline unsigned int
9702 plt_stub_pad (struct ppc_link_hash_table *htab,
9703 struct ppc_stub_hash_entry *stub_entry,
9708 bfd_vma stub_off = stub_entry->group->stub_sec->size;
9710 if (htab->params->plt_stub_align >= 0)
9712 stub_align = 1 << htab->params->plt_stub_align;
9713 if ((stub_off & (stub_align - 1)) != 0)
9714 return stub_align - (stub_off & (stub_align - 1));
9718 stub_align = 1 << -htab->params->plt_stub_align;
9719 stub_size = plt_stub_size (htab, stub_entry, plt_off);
9720 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
9721 > ((stub_size - 1) & -stub_align))
9722 return stub_align - (stub_off & (stub_align - 1));
9726 /* Build a .plt call stub. */
9728 static inline bfd_byte *
9729 build_plt_stub (struct ppc_link_hash_table *htab,
9730 struct ppc_stub_hash_entry *stub_entry,
9731 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9733 bfd *obfd = htab->params->stub_bfd;
9734 bfd_boolean plt_load_toc = htab->opd_abi;
9735 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
9736 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
9737 && htab->elf.dynamic_sections_created
9738 && stub_entry->h != NULL
9739 && stub_entry->h->elf.dynindx != -1);
9740 bfd_boolean use_fake_dep = plt_thread_safe;
9741 bfd_vma cmp_branch_off = 0;
9743 if (!ALWAYS_USE_FAKE_DEP
9746 && !((stub_entry->h == htab->tls_get_addr_fd
9747 || stub_entry->h == htab->tls_get_addr)
9748 && htab->params->tls_get_addr_opt))
9750 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
9751 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
9752 / PLT_ENTRY_SIZE (htab));
9753 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
9756 if (pltindex > 32768)
9757 glinkoff += (pltindex - 32768) * 4;
9759 + htab->glink->output_offset
9760 + htab->glink->output_section->vma);
9761 from = (p - stub_entry->group->stub_sec->contents
9762 + 4 * (ALWAYS_EMIT_R2SAVE
9763 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9764 + 4 * (PPC_HA (offset) != 0)
9765 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
9767 + 4 * (plt_static_chain != 0)
9769 + stub_entry->group->stub_sec->output_offset
9770 + stub_entry->group->stub_sec->output_section->vma);
9771 cmp_branch_off = to - from;
9772 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
9775 if (PPC_HA (offset) != 0)
9779 if (ALWAYS_EMIT_R2SAVE
9780 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9782 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9783 r[1].r_offset = r[0].r_offset + 4;
9784 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9785 r[1].r_addend = r[0].r_addend;
9788 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9790 r[2].r_offset = r[1].r_offset + 4;
9791 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9792 r[2].r_addend = r[0].r_addend;
9796 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
9797 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9798 r[2].r_addend = r[0].r_addend + 8;
9799 if (plt_static_chain)
9801 r[3].r_offset = r[2].r_offset + 4;
9802 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9803 r[3].r_addend = r[0].r_addend + 16;
9808 if (ALWAYS_EMIT_R2SAVE
9809 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9810 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
9813 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
9814 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
9818 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
9819 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
9822 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9824 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
9827 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
9832 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
9833 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
9835 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
9836 if (plt_static_chain)
9837 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
9844 if (ALWAYS_EMIT_R2SAVE
9845 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9847 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9850 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9852 r[1].r_offset = r[0].r_offset + 4;
9853 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9854 r[1].r_addend = r[0].r_addend;
9858 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
9859 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9860 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
9861 if (plt_static_chain)
9863 r[2].r_offset = r[1].r_offset + 4;
9864 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9865 r[2].r_addend = r[0].r_addend + 8;
9870 if (ALWAYS_EMIT_R2SAVE
9871 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9872 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
9873 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
9875 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9877 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
9880 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
9885 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
9886 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
9888 if (plt_static_chain)
9889 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
9890 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
9893 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
9895 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
9896 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
9897 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
9900 bfd_put_32 (obfd, BCTR, p), p += 4;
9904 /* Build a special .plt call stub for __tls_get_addr. */
9906 #define LD_R11_0R3 0xe9630000
9907 #define LD_R12_0R3 0xe9830000
9908 #define MR_R0_R3 0x7c601b78
9909 #define CMPDI_R11_0 0x2c2b0000
9910 #define ADD_R3_R12_R13 0x7c6c6a14
9911 #define BEQLR 0x4d820020
9912 #define MR_R3_R0 0x7c030378
9913 #define STD_R11_0R1 0xf9610000
9914 #define BCTRL 0x4e800421
9915 #define LD_R11_0R1 0xe9610000
9916 #define MTLR_R11 0x7d6803a6
9918 static inline bfd_byte *
9919 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
9920 struct ppc_stub_hash_entry *stub_entry,
9921 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9923 bfd *obfd = htab->params->stub_bfd;
9926 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
9927 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
9928 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
9929 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
9930 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
9931 bfd_put_32 (obfd, BEQLR, p), p += 4;
9932 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9934 r[0].r_offset += 7 * 4;
9935 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
9936 return build_plt_stub (htab, stub_entry, p, offset, r);
9938 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
9939 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
9942 r[0].r_offset += 2 * 4;
9943 p = build_plt_stub (htab, stub_entry, p, offset, r);
9944 bfd_put_32 (obfd, BCTRL, p - 4);
9946 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
9947 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
9948 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
9949 bfd_put_32 (obfd, BLR, p), p += 4;
9951 if (htab->glink_eh_frame != NULL
9952 && htab->glink_eh_frame->size != 0)
9954 bfd_byte *base, *eh;
9955 unsigned int lr_used, delta;
9957 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
9958 eh = base + stub_entry->group->eh_size;
9959 lr_used = stub_entry->stub_offset + (p - 20 - loc);
9960 delta = lr_used - stub_entry->group->lr_restore;
9961 stub_entry->group->lr_restore = lr_used + 16;
9962 eh = eh_advance (htab->elf.dynobj, eh, delta);
9963 *eh++ = DW_CFA_offset_extended_sf;
9965 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
9966 *eh++ = DW_CFA_advance_loc + 4;
9967 *eh++ = DW_CFA_restore_extended;
9969 stub_entry->group->eh_size = eh - base;
9974 static Elf_Internal_Rela *
9975 get_relocs (asection *sec, int count)
9977 Elf_Internal_Rela *relocs;
9978 struct bfd_elf_section_data *elfsec_data;
9980 elfsec_data = elf_section_data (sec);
9981 relocs = elfsec_data->relocs;
9984 bfd_size_type relsize;
9985 relsize = sec->reloc_count * sizeof (*relocs);
9986 relocs = bfd_alloc (sec->owner, relsize);
9989 elfsec_data->relocs = relocs;
9990 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9991 sizeof (Elf_Internal_Shdr));
9992 if (elfsec_data->rela.hdr == NULL)
9994 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9995 * sizeof (Elf64_External_Rela));
9996 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
9997 sec->reloc_count = 0;
9999 relocs += sec->reloc_count;
10000 sec->reloc_count += count;
10005 get_r2off (struct bfd_link_info *info,
10006 struct ppc_stub_hash_entry *stub_entry)
10008 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10009 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10013 /* Support linking -R objects. Get the toc pointer from the
10016 if (!htab->opd_abi)
10018 asection *opd = stub_entry->h->elf.root.u.def.section;
10019 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10021 if (strcmp (opd->name, ".opd") != 0
10022 || opd->reloc_count != 0)
10024 info->callbacks->einfo
10025 (_("%P: cannot find opd entry toc for `%pT'\n"),
10026 stub_entry->h->elf.root.root.string);
10027 bfd_set_error (bfd_error_bad_value);
10028 return (bfd_vma) -1;
10030 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10031 return (bfd_vma) -1;
10032 r2off = bfd_get_64 (opd->owner, buf);
10033 r2off -= elf_gp (info->output_bfd);
10035 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10040 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10042 struct ppc_stub_hash_entry *stub_entry;
10043 struct ppc_branch_hash_entry *br_entry;
10044 struct bfd_link_info *info;
10045 struct ppc_link_hash_table *htab;
10049 Elf_Internal_Rela *r;
10052 /* Massage our args to the form they really have. */
10053 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10056 htab = ppc_hash_table (info);
10060 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
10061 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10063 htab->stub_count[stub_entry->stub_type - 1] += 1;
10064 switch (stub_entry->stub_type)
10066 case ppc_stub_long_branch:
10067 case ppc_stub_long_branch_r2off:
10068 /* Branches are relative. This is where we are going to. */
10069 targ = (stub_entry->target_value
10070 + stub_entry->target_section->output_offset
10071 + stub_entry->target_section->output_section->vma);
10072 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10074 /* And this is where we are coming from. */
10075 off = (stub_entry->stub_offset
10076 + stub_entry->group->stub_sec->output_offset
10077 + stub_entry->group->stub_sec->output_section->vma);
10081 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10083 bfd_vma r2off = get_r2off (info, stub_entry);
10085 if (r2off == (bfd_vma) -1)
10087 htab->stub_error = TRUE;
10090 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10092 if (PPC_HA (r2off) != 0)
10094 bfd_put_32 (htab->params->stub_bfd,
10095 ADDIS_R2_R2 | PPC_HA (r2off), p);
10098 if (PPC_LO (r2off) != 0)
10100 bfd_put_32 (htab->params->stub_bfd,
10101 ADDI_R2_R2 | PPC_LO (r2off), p);
10106 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10109 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10112 (_("long branch stub `%s' offset overflow"),
10113 stub_entry->root.string);
10114 htab->stub_error = TRUE;
10118 if (info->emitrelocations)
10120 r = get_relocs (stub_entry->group->stub_sec, 1);
10123 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
10124 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10125 r->r_addend = targ;
10126 if (stub_entry->h != NULL)
10128 struct elf_link_hash_entry **hashes;
10129 unsigned long symndx;
10130 struct ppc_link_hash_entry *h;
10132 hashes = elf_sym_hashes (htab->params->stub_bfd);
10133 if (hashes == NULL)
10135 bfd_size_type hsize;
10137 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10138 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10139 if (hashes == NULL)
10141 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10142 htab->stub_globals = 1;
10144 symndx = htab->stub_globals++;
10146 hashes[symndx] = &h->elf;
10147 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10148 if (h->oh != NULL && h->oh->is_func)
10149 h = ppc_follow_link (h->oh);
10150 if (h->elf.root.u.def.section != stub_entry->target_section)
10151 /* H is an opd symbol. The addend must be zero. */
10155 off = (h->elf.root.u.def.value
10156 + h->elf.root.u.def.section->output_offset
10157 + h->elf.root.u.def.section->output_section->vma);
10158 r->r_addend -= off;
10164 case ppc_stub_plt_branch:
10165 case ppc_stub_plt_branch_r2off:
10166 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10167 stub_entry->root.string + 9,
10169 if (br_entry == NULL)
10171 _bfd_error_handler (_("can't find branch stub `%s'"),
10172 stub_entry->root.string);
10173 htab->stub_error = TRUE;
10177 targ = (stub_entry->target_value
10178 + stub_entry->target_section->output_offset
10179 + stub_entry->target_section->output_section->vma);
10180 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10181 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10183 bfd_put_64 (htab->brlt->owner, targ,
10184 htab->brlt->contents + br_entry->offset);
10186 if (br_entry->iter == htab->stub_iteration)
10188 br_entry->iter = 0;
10190 if (htab->relbrlt != NULL)
10192 /* Create a reloc for the branch lookup table entry. */
10193 Elf_Internal_Rela rela;
10196 rela.r_offset = (br_entry->offset
10197 + htab->brlt->output_offset
10198 + htab->brlt->output_section->vma);
10199 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10200 rela.r_addend = targ;
10202 rl = htab->relbrlt->contents;
10203 rl += (htab->relbrlt->reloc_count++
10204 * sizeof (Elf64_External_Rela));
10205 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10207 else if (info->emitrelocations)
10209 r = get_relocs (htab->brlt, 1);
10212 /* brlt, being SEC_LINKER_CREATED does not go through the
10213 normal reloc processing. Symbols and offsets are not
10214 translated from input file to output file form, so
10215 set up the offset per the output file. */
10216 r->r_offset = (br_entry->offset
10217 + htab->brlt->output_offset
10218 + htab->brlt->output_section->vma);
10219 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10220 r->r_addend = targ;
10224 targ = (br_entry->offset
10225 + htab->brlt->output_offset
10226 + htab->brlt->output_section->vma);
10228 off = (elf_gp (info->output_bfd)
10229 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10232 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10234 info->callbacks->einfo
10235 (_("%P: linkage table error against `%pT'\n"),
10236 stub_entry->root.string);
10237 bfd_set_error (bfd_error_bad_value);
10238 htab->stub_error = TRUE;
10242 if (info->emitrelocations)
10244 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
10247 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10248 if (bfd_big_endian (info->output_bfd))
10249 r[0].r_offset += 2;
10250 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10251 r[0].r_offset += 4;
10252 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10253 r[0].r_addend = targ;
10254 if (PPC_HA (off) != 0)
10256 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10257 r[1].r_offset = r[0].r_offset + 4;
10258 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10259 r[1].r_addend = r[0].r_addend;
10264 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10266 if (PPC_HA (off) != 0)
10268 bfd_put_32 (htab->params->stub_bfd,
10269 ADDIS_R12_R2 | PPC_HA (off), p);
10271 bfd_put_32 (htab->params->stub_bfd,
10272 LD_R12_0R12 | PPC_LO (off), p);
10275 bfd_put_32 (htab->params->stub_bfd,
10276 LD_R12_0R2 | PPC_LO (off), p);
10280 bfd_vma r2off = get_r2off (info, stub_entry);
10282 if (r2off == (bfd_vma) -1)
10284 htab->stub_error = TRUE;
10288 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10290 if (PPC_HA (off) != 0)
10292 bfd_put_32 (htab->params->stub_bfd,
10293 ADDIS_R12_R2 | PPC_HA (off), p);
10295 bfd_put_32 (htab->params->stub_bfd,
10296 LD_R12_0R12 | PPC_LO (off), p);
10299 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
10301 if (PPC_HA (r2off) != 0)
10304 bfd_put_32 (htab->params->stub_bfd,
10305 ADDIS_R2_R2 | PPC_HA (r2off), p);
10307 if (PPC_LO (r2off) != 0)
10310 bfd_put_32 (htab->params->stub_bfd,
10311 ADDI_R2_R2 | PPC_LO (r2off), p);
10315 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10317 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10321 case ppc_stub_long_branch_notoc:
10322 case ppc_stub_long_branch_both:
10323 case ppc_stub_plt_branch_notoc:
10324 case ppc_stub_plt_branch_both:
10325 case ppc_stub_plt_call_notoc:
10326 case ppc_stub_plt_call_both:
10328 off = (8 + stub_entry->stub_offset
10329 + stub_entry->group->stub_sec->output_offset
10330 + stub_entry->group->stub_sec->output_section->vma);
10331 if (stub_entry->stub_type == ppc_stub_long_branch_both
10332 || stub_entry->stub_type == ppc_stub_plt_branch_both
10333 || stub_entry->stub_type == ppc_stub_plt_call_both)
10336 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10339 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10341 targ = stub_entry->plt_ent->plt.offset & ~1;
10342 if (targ >= (bfd_vma) -2)
10345 plt = htab->elf.splt;
10346 if (!htab->elf.dynamic_sections_created
10347 || stub_entry->h == NULL
10348 || stub_entry->h->elf.dynindx == -1)
10350 if (stub_entry->symtype == STT_GNU_IFUNC)
10351 plt = htab->elf.iplt;
10353 plt = htab->pltlocal;
10355 targ += plt->output_offset + plt->output_section->vma;
10358 targ = (stub_entry->target_value
10359 + stub_entry->target_section->output_offset
10360 + stub_entry->target_section->output_section->vma);
10362 bfd_put_32 (htab->params->stub_bfd, MFLR_R12, p);
10364 bfd_put_32 (htab->params->stub_bfd, BCL_20_31, p);
10366 bfd_put_32 (htab->params->stub_bfd, MFLR_R11, p);
10368 bfd_put_32 (htab->params->stub_bfd, MTLR_R12, p);
10370 p = build_offset (htab->params->stub_bfd, p, off,
10371 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
10372 if (stub_entry->stub_type == ppc_stub_long_branch_notoc)
10375 bfd_put_32 (htab->params->stub_bfd,
10376 B_DOT | ((off - (p - loc)) & 0x3fffffc), p);
10378 else if (stub_entry->stub_type == ppc_stub_long_branch_both)
10381 bfd_put_32 (htab->params->stub_bfd,
10382 B_DOT | ((off - (p - loc)) & 0x3fffffc), p);
10386 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10388 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10392 if (htab->glink_eh_frame != NULL
10393 && htab->glink_eh_frame->size != 0)
10395 bfd_byte *base, *eh;
10396 unsigned int lr_used, delta;
10398 base = (htab->glink_eh_frame->contents
10399 + stub_entry->group->eh_base + 17);
10400 eh = base + stub_entry->group->eh_size;
10401 lr_used = stub_entry->stub_offset + 8;
10402 if (stub_entry->stub_type == ppc_stub_long_branch_both
10403 || stub_entry->stub_type == ppc_stub_plt_branch_both
10404 || stub_entry->stub_type == ppc_stub_plt_call_both)
10406 delta = lr_used - stub_entry->group->lr_restore;
10407 stub_entry->group->lr_restore = lr_used + 8;
10408 eh = eh_advance (htab->elf.dynobj, eh, delta);
10409 *eh++ = DW_CFA_register;
10412 *eh++ = DW_CFA_advance_loc + 2;
10413 *eh++ = DW_CFA_restore_extended;
10415 stub_entry->group->eh_size = eh - base;
10419 case ppc_stub_plt_call:
10420 case ppc_stub_plt_call_r2save:
10421 if (stub_entry->h != NULL
10422 && stub_entry->h->is_func_descriptor
10423 && stub_entry->h->oh != NULL)
10425 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10427 /* If the old-ABI "dot-symbol" is undefined make it weak so
10428 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
10429 if (fh->elf.root.type == bfd_link_hash_undefined
10430 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
10431 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
10432 fh->elf.root.type = bfd_link_hash_undefweak;
10435 /* Now build the stub. */
10436 targ = stub_entry->plt_ent->plt.offset & ~1;
10437 if (targ >= (bfd_vma) -2)
10440 plt = htab->elf.splt;
10441 if (!htab->elf.dynamic_sections_created
10442 || stub_entry->h == NULL
10443 || stub_entry->h->elf.dynindx == -1)
10445 if (stub_entry->symtype == STT_GNU_IFUNC)
10446 plt = htab->elf.iplt;
10448 plt = htab->pltlocal;
10450 targ += plt->output_offset + plt->output_section->vma;
10452 off = (elf_gp (info->output_bfd)
10453 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10456 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10458 info->callbacks->einfo
10459 /* xgettext:c-format */
10460 (_("%P: linkage table error against `%pT'\n"),
10461 stub_entry->h != NULL
10462 ? stub_entry->h->elf.root.root.string
10464 bfd_set_error (bfd_error_bad_value);
10465 htab->stub_error = TRUE;
10470 if (info->emitrelocations)
10472 r = get_relocs (stub_entry->group->stub_sec,
10473 ((PPC_HA (off) != 0)
10475 ? 2 + (htab->params->plt_static_chain
10476 && PPC_HA (off + 16) == PPC_HA (off))
10480 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10481 if (bfd_big_endian (info->output_bfd))
10482 r[0].r_offset += 2;
10483 r[0].r_addend = targ;
10485 if (stub_entry->h != NULL
10486 && (stub_entry->h == htab->tls_get_addr_fd
10487 || stub_entry->h == htab->tls_get_addr)
10488 && htab->params->tls_get_addr_opt)
10489 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
10491 p = build_plt_stub (htab, stub_entry, loc, off, r);
10494 case ppc_stub_save_res:
10502 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
10504 if (htab->params->emit_stub_syms)
10506 struct elf_link_hash_entry *h;
10509 const char *const stub_str[] = { "long_branch",
10522 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10523 len2 = strlen (stub_entry->root.string);
10524 name = bfd_malloc (len1 + len2 + 2);
10527 memcpy (name, stub_entry->root.string, 9);
10528 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10529 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10530 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
10533 if (h->root.type == bfd_link_hash_new)
10535 h->root.type = bfd_link_hash_defined;
10536 h->root.u.def.section = stub_entry->group->stub_sec;
10537 h->root.u.def.value = stub_entry->stub_offset;
10538 h->ref_regular = 1;
10539 h->def_regular = 1;
10540 h->ref_regular_nonweak = 1;
10541 h->forced_local = 1;
10543 h->root.linker_def = 1;
10550 /* As above, but don't actually build the stub. Just bump offset so
10551 we know stub section sizes, and select plt_branch stubs where
10552 long_branch stubs won't do. */
10555 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10557 struct ppc_stub_hash_entry *stub_entry;
10558 struct bfd_link_info *info;
10559 struct ppc_link_hash_table *htab;
10563 /* Massage our args to the form they really have. */
10564 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10567 htab = ppc_hash_table (info);
10571 /* Make a note of the offset within the stubs for this entry. */
10572 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10574 if (stub_entry->h != NULL
10575 && stub_entry->h->save_res
10576 && stub_entry->h->elf.root.type == bfd_link_hash_defined
10577 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
10579 /* Don't make stubs to out-of-line register save/restore
10580 functions. Instead, emit copies of the functions. */
10581 stub_entry->group->needs_save_res = 1;
10582 stub_entry->stub_type = ppc_stub_save_res;
10586 if (stub_entry->stub_type >= ppc_stub_plt_call
10587 && stub_entry->stub_type <= ppc_stub_plt_call_both)
10590 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
10591 if (targ >= (bfd_vma) -2)
10593 plt = htab->elf.splt;
10594 if (!htab->elf.dynamic_sections_created
10595 || stub_entry->h == NULL
10596 || stub_entry->h->elf.dynindx == -1)
10598 if (stub_entry->symtype == STT_GNU_IFUNC)
10599 plt = htab->elf.iplt;
10601 plt = htab->pltlocal;
10603 targ += plt->output_offset + plt->output_section->vma;
10605 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10607 off = (8 + stub_entry->stub_offset
10608 + stub_entry->group->stub_sec->output_offset
10609 + stub_entry->group->stub_sec->output_section->vma);
10610 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10614 off = (elf_gp (info->output_bfd)
10615 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10617 if (htab->params->plt_stub_align != 0)
10619 unsigned pad = plt_stub_pad (htab, stub_entry, targ - off);
10621 stub_entry->group->stub_sec->size += pad;
10622 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10623 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10628 size = plt_stub_size (htab, stub_entry, off);
10630 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10632 /* After the bcl, lr has been modified so we need to emit
10633 .eh_frame info saying the return address is in r12. */
10634 unsigned int lr_used = stub_entry->stub_offset + 8;
10635 unsigned int delta;
10636 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10638 /* The eh_frame info will consist of a DW_CFA_advance_loc or
10639 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
10640 DW_CFA_restore_extended 65. */
10641 delta = lr_used - stub_entry->group->lr_restore;
10642 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
10643 stub_entry->group->lr_restore = lr_used + 8;
10647 if (stub_entry->h != NULL
10648 && (stub_entry->h == htab->tls_get_addr_fd
10649 || stub_entry->h == htab->tls_get_addr)
10650 && htab->params->tls_get_addr_opt
10651 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
10653 /* After the bctrl, lr has been modified so we need to
10654 emit .eh_frame info saying the return address is
10655 on the stack. In fact we put the EH info specifying
10656 that the return address is on the stack *at* the
10657 call rather than after it, because the EH info for a
10658 call needs to be specified by that point.
10659 See libgcc/unwind-dw2.c execute_cfa_program. */
10660 unsigned int lr_used = stub_entry->stub_offset + size - 20;
10661 unsigned int delta;
10662 /* The eh_frame info will consist of a DW_CFA_advance_loc
10663 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
10664 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
10665 delta = lr_used - stub_entry->group->lr_restore;
10666 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
10667 stub_entry->group->lr_restore = size - 4;
10670 if (info->emitrelocations)
10672 stub_entry->group->stub_sec->reloc_count
10673 += ((PPC_HA (off) != 0)
10675 ? 2 + (htab->params->plt_static_chain
10676 && PPC_HA (off + 16) == PPC_HA (off))
10678 stub_entry->group->stub_sec->flags |= SEC_RELOC;
10684 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10687 bfd_vma local_off = 0;
10689 targ = (stub_entry->target_value
10690 + stub_entry->target_section->output_offset
10691 + stub_entry->target_section->output_section->vma);
10692 off = (stub_entry->stub_offset
10693 + stub_entry->group->stub_sec->output_offset
10694 + stub_entry->group->stub_sec->output_section->vma);
10696 /* Reset the stub type from the plt variant in case we now
10697 can reach with a shorter stub. */
10698 if (stub_entry->stub_type >= ppc_stub_plt_branch)
10699 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10702 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10704 r2off = get_r2off (info, stub_entry);
10705 if (r2off == (bfd_vma) -1)
10707 htab->stub_error = TRUE;
10711 if (PPC_HA (r2off) != 0)
10713 if (PPC_LO (r2off) != 0)
10717 else if (stub_entry->stub_type >= ppc_stub_long_branch_notoc)
10719 size = 20 + size_offset (targ - (off + 8));
10720 if (stub_entry->stub_type > ppc_stub_long_branch_notoc)
10726 if (stub_entry->stub_type >= ppc_stub_long_branch_notoc)
10728 /* After the bcl, lr has been modified so we need to emit
10729 .eh_frame info saying the return address is in r12. */
10730 unsigned int lr_used = stub_entry->stub_offset + 8;
10731 unsigned int delta;
10732 if (stub_entry->stub_type > ppc_stub_long_branch_notoc)
10734 /* The eh_frame info will consist of a DW_CFA_advance_loc or
10735 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
10736 DW_CFA_restore_extended 65. */
10737 delta = lr_used - stub_entry->group->lr_restore;
10738 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
10739 stub_entry->group->lr_restore = lr_used + 8;
10741 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10743 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
10744 - ppc_stub_long_branch_notoc);
10750 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10752 /* If the branch offset is too big, use a ppc_stub_plt_branch.
10753 Do the same for -R objects without function descriptors. */
10754 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
10756 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
10757 || off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off)
10759 struct ppc_branch_hash_entry *br_entry;
10761 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10762 stub_entry->root.string + 9,
10764 if (br_entry == NULL)
10766 _bfd_error_handler (_("can't build branch stub `%s'"),
10767 stub_entry->root.string);
10768 htab->stub_error = TRUE;
10772 if (br_entry->iter != htab->stub_iteration)
10774 br_entry->iter = htab->stub_iteration;
10775 br_entry->offset = htab->brlt->size;
10776 htab->brlt->size += 8;
10778 if (htab->relbrlt != NULL)
10779 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10780 else if (info->emitrelocations)
10782 htab->brlt->reloc_count += 1;
10783 htab->brlt->flags |= SEC_RELOC;
10787 targ = (br_entry->offset
10788 + htab->brlt->output_offset
10789 + htab->brlt->output_section->vma);
10790 off = (elf_gp (info->output_bfd)
10791 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10794 if (info->emitrelocations)
10796 stub_entry->group->stub_sec->reloc_count
10797 += 1 + (PPC_HA (off) != 0);
10798 stub_entry->group->stub_sec->flags |= SEC_RELOC;
10801 stub_entry->stub_type
10802 += ppc_stub_plt_branch - ppc_stub_long_branch;
10803 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10806 if (PPC_HA (off) != 0)
10812 if (PPC_HA (off) != 0)
10815 if (PPC_HA (r2off) != 0)
10817 if (PPC_LO (r2off) != 0)
10821 else if (info->emitrelocations)
10823 stub_entry->group->stub_sec->reloc_count += 1;
10824 stub_entry->group->stub_sec->flags |= SEC_RELOC;
10829 stub_entry->group->stub_sec->size += size;
10833 /* Set up various things so that we can make a list of input sections
10834 for each output section included in the link. Returns -1 on error,
10835 0 when no stubs will be needed, and 1 on success. */
10838 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
10842 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10847 htab->sec_info_arr_size = _bfd_section_id;
10848 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
10849 htab->sec_info = bfd_zmalloc (amt);
10850 if (htab->sec_info == NULL)
10853 /* Set toc_off for com, und, abs and ind sections. */
10854 for (id = 0; id < 3; id++)
10855 htab->sec_info[id].toc_off = TOC_BASE_OFF;
10860 /* Set up for first pass at multitoc partitioning. */
10863 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10865 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10867 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
10868 htab->toc_bfd = NULL;
10869 htab->toc_first_sec = NULL;
10872 /* The linker repeatedly calls this function for each TOC input section
10873 and linker generated GOT section. Group input bfds such that the toc
10874 within a group is less than 64k in size. */
10877 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10879 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10880 bfd_vma addr, off, limit;
10885 if (!htab->second_toc_pass)
10887 /* Keep track of the first .toc or .got section for this input bfd. */
10888 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
10892 htab->toc_bfd = isec->owner;
10893 htab->toc_first_sec = isec;
10896 addr = isec->output_offset + isec->output_section->vma;
10897 off = addr - htab->toc_curr;
10898 limit = 0x80008000;
10899 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10901 if (off + isec->size > limit)
10903 addr = (htab->toc_first_sec->output_offset
10904 + htab->toc_first_sec->output_section->vma);
10905 htab->toc_curr = addr;
10906 htab->toc_curr &= -TOC_BASE_ALIGN;
10909 /* toc_curr is the base address of this toc group. Set elf_gp
10910 for the input section to be the offset relative to the
10911 output toc base plus 0x8000. Making the input elf_gp an
10912 offset allows us to move the toc as a whole without
10913 recalculating input elf_gp. */
10914 off = htab->toc_curr - elf_gp (info->output_bfd);
10915 off += TOC_BASE_OFF;
10917 /* Die if someone uses a linker script that doesn't keep input
10918 file .toc and .got together. */
10920 && elf_gp (isec->owner) != 0
10921 && elf_gp (isec->owner) != off)
10924 elf_gp (isec->owner) = off;
10928 /* During the second pass toc_first_sec points to the start of
10929 a toc group, and toc_curr is used to track the old elf_gp.
10930 We use toc_bfd to ensure we only look at each bfd once. */
10931 if (htab->toc_bfd == isec->owner)
10933 htab->toc_bfd = isec->owner;
10935 if (htab->toc_first_sec == NULL
10936 || htab->toc_curr != elf_gp (isec->owner))
10938 htab->toc_curr = elf_gp (isec->owner);
10939 htab->toc_first_sec = isec;
10941 addr = (htab->toc_first_sec->output_offset
10942 + htab->toc_first_sec->output_section->vma);
10943 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
10944 elf_gp (isec->owner) = off;
10949 /* Called via elf_link_hash_traverse to merge GOT entries for global
10953 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10955 if (h->root.type == bfd_link_hash_indirect)
10958 merge_got_entries (&h->got.glist);
10963 /* Called via elf_link_hash_traverse to allocate GOT entries for global
10967 reallocate_got (struct elf_link_hash_entry *h, void *inf)
10969 struct got_entry *gent;
10971 if (h->root.type == bfd_link_hash_indirect)
10974 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10975 if (!gent->is_indirect)
10976 allocate_got (h, (struct bfd_link_info *) inf, gent);
10980 /* Called on the first multitoc pass after the last call to
10981 ppc64_elf_next_toc_section. This function removes duplicate GOT
10985 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
10987 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10988 struct bfd *ibfd, *ibfd2;
10989 bfd_boolean done_something;
10991 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
10993 if (!htab->do_multi_toc)
10996 /* Merge global sym got entries within a toc group. */
10997 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10999 /* And tlsld_got. */
11000 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11002 struct got_entry *ent, *ent2;
11004 if (!is_ppc64_elf (ibfd))
11007 ent = ppc64_tlsld_got (ibfd);
11008 if (!ent->is_indirect
11009 && ent->got.offset != (bfd_vma) -1)
11011 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11013 if (!is_ppc64_elf (ibfd2))
11016 ent2 = ppc64_tlsld_got (ibfd2);
11017 if (!ent2->is_indirect
11018 && ent2->got.offset != (bfd_vma) -1
11019 && elf_gp (ibfd2) == elf_gp (ibfd))
11021 ent2->is_indirect = TRUE;
11022 ent2->got.ent = ent;
11028 /* Zap sizes of got sections. */
11029 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11030 htab->elf.irelplt->size -= htab->got_reli_size;
11031 htab->got_reli_size = 0;
11033 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11035 asection *got, *relgot;
11037 if (!is_ppc64_elf (ibfd))
11040 got = ppc64_elf_tdata (ibfd)->got;
11043 got->rawsize = got->size;
11045 relgot = ppc64_elf_tdata (ibfd)->relgot;
11046 relgot->rawsize = relgot->size;
11051 /* Now reallocate the got, local syms first. We don't need to
11052 allocate section contents again since we never increase size. */
11053 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11055 struct got_entry **lgot_ents;
11056 struct got_entry **end_lgot_ents;
11057 struct plt_entry **local_plt;
11058 struct plt_entry **end_local_plt;
11059 unsigned char *lgot_masks;
11060 bfd_size_type locsymcount;
11061 Elf_Internal_Shdr *symtab_hdr;
11064 if (!is_ppc64_elf (ibfd))
11067 lgot_ents = elf_local_got_ents (ibfd);
11071 symtab_hdr = &elf_symtab_hdr (ibfd);
11072 locsymcount = symtab_hdr->sh_info;
11073 end_lgot_ents = lgot_ents + locsymcount;
11074 local_plt = (struct plt_entry **) end_lgot_ents;
11075 end_local_plt = local_plt + locsymcount;
11076 lgot_masks = (unsigned char *) end_local_plt;
11077 s = ppc64_elf_tdata (ibfd)->got;
11078 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11080 struct got_entry *ent;
11082 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11084 unsigned int ent_size = 8;
11085 unsigned int rel_size = sizeof (Elf64_External_Rela);
11087 ent->got.offset = s->size;
11088 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11093 s->size += ent_size;
11094 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
11096 htab->elf.irelplt->size += rel_size;
11097 htab->got_reli_size += rel_size;
11099 else if (bfd_link_pic (info)
11100 && !((ent->tls_type & TLS_TPREL) != 0
11101 && bfd_link_executable (info)))
11103 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11104 srel->size += rel_size;
11110 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11112 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11114 struct got_entry *ent;
11116 if (!is_ppc64_elf (ibfd))
11119 ent = ppc64_tlsld_got (ibfd);
11120 if (!ent->is_indirect
11121 && ent->got.offset != (bfd_vma) -1)
11123 asection *s = ppc64_elf_tdata (ibfd)->got;
11124 ent->got.offset = s->size;
11126 if (bfd_link_pic (info))
11128 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11129 srel->size += sizeof (Elf64_External_Rela);
11134 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11135 if (!done_something)
11136 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11140 if (!is_ppc64_elf (ibfd))
11143 got = ppc64_elf_tdata (ibfd)->got;
11146 done_something = got->rawsize != got->size;
11147 if (done_something)
11152 if (done_something)
11153 (*htab->params->layout_sections_again) ();
11155 /* Set up for second pass over toc sections to recalculate elf_gp
11156 on input sections. */
11157 htab->toc_bfd = NULL;
11158 htab->toc_first_sec = NULL;
11159 htab->second_toc_pass = TRUE;
11160 return done_something;
11163 /* Called after second pass of multitoc partitioning. */
11166 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11168 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11170 /* After the second pass, toc_curr tracks the TOC offset used
11171 for code sections below in ppc64_elf_next_input_section. */
11172 htab->toc_curr = TOC_BASE_OFF;
11175 /* No toc references were found in ISEC. If the code in ISEC makes no
11176 calls, then there's no need to use toc adjusting stubs when branching
11177 into ISEC. Actually, indirect calls from ISEC are OK as they will
11178 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11179 needed, and 2 if a cyclical call-graph was found but no other reason
11180 for a stub was detected. If called from the top level, a return of
11181 2 means the same as a return of 0. */
11184 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11188 /* Mark this section as checked. */
11189 isec->call_check_done = 1;
11191 /* We know none of our code bearing sections will need toc stubs. */
11192 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11195 if (isec->size == 0)
11198 if (isec->output_section == NULL)
11202 if (isec->reloc_count != 0)
11204 Elf_Internal_Rela *relstart, *rel;
11205 Elf_Internal_Sym *local_syms;
11206 struct ppc_link_hash_table *htab;
11208 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11209 info->keep_memory);
11210 if (relstart == NULL)
11213 /* Look for branches to outside of this section. */
11215 htab = ppc_hash_table (info);
11219 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11221 enum elf_ppc64_reloc_type r_type;
11222 unsigned long r_symndx;
11223 struct elf_link_hash_entry *h;
11224 struct ppc_link_hash_entry *eh;
11225 Elf_Internal_Sym *sym;
11227 struct _opd_sec_data *opd;
11231 r_type = ELF64_R_TYPE (rel->r_info);
11232 if (r_type != R_PPC64_REL24
11233 && r_type != R_PPC64_REL24_NOTOC
11234 && r_type != R_PPC64_REL14
11235 && r_type != R_PPC64_REL14_BRTAKEN
11236 && r_type != R_PPC64_REL14_BRNTAKEN
11237 && r_type != R_PPC64_PLTCALL)
11240 r_symndx = ELF64_R_SYM (rel->r_info);
11241 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11248 /* Calls to dynamic lib functions go through a plt call stub
11250 eh = (struct ppc_link_hash_entry *) h;
11252 && (eh->elf.plt.plist != NULL
11254 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11260 if (sym_sec == NULL)
11261 /* Ignore other undefined symbols. */
11264 /* Assume branches to other sections not included in the
11265 link need stubs too, to cover -R and absolute syms. */
11266 if (sym_sec->output_section == NULL)
11273 sym_value = sym->st_value;
11276 if (h->root.type != bfd_link_hash_defined
11277 && h->root.type != bfd_link_hash_defweak)
11279 sym_value = h->root.u.def.value;
11281 sym_value += rel->r_addend;
11283 /* If this branch reloc uses an opd sym, find the code section. */
11284 opd = get_opd_info (sym_sec);
11287 if (h == NULL && opd->adjust != NULL)
11291 adjust = opd->adjust[OPD_NDX (sym_value)];
11293 /* Assume deleted functions won't ever be called. */
11295 sym_value += adjust;
11298 dest = opd_entry_value (sym_sec, sym_value,
11299 &sym_sec, NULL, FALSE);
11300 if (dest == (bfd_vma) -1)
11305 + sym_sec->output_offset
11306 + sym_sec->output_section->vma);
11308 /* Ignore branch to self. */
11309 if (sym_sec == isec)
11312 /* If the called function uses the toc, we need a stub. */
11313 if (sym_sec->has_toc_reloc
11314 || sym_sec->makes_toc_func_call)
11320 /* Assume any branch that needs a long branch stub might in fact
11321 need a plt_branch stub. A plt_branch stub uses r2. */
11322 else if (dest - (isec->output_offset
11323 + isec->output_section->vma
11324 + rel->r_offset) + (1 << 25)
11325 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11333 /* If calling back to a section in the process of being
11334 tested, we can't say for sure that no toc adjusting stubs
11335 are needed, so don't return zero. */
11336 else if (sym_sec->call_check_in_progress)
11339 /* Branches to another section that itself doesn't have any TOC
11340 references are OK. Recursively call ourselves to check. */
11341 else if (!sym_sec->call_check_done)
11345 /* Mark current section as indeterminate, so that other
11346 sections that call back to current won't be marked as
11348 isec->call_check_in_progress = 1;
11349 recur = toc_adjusting_stub_needed (info, sym_sec);
11350 isec->call_check_in_progress = 0;
11361 if (local_syms != NULL
11362 && (elf_symtab_hdr (isec->owner).contents
11363 != (unsigned char *) local_syms))
11365 if (elf_section_data (isec)->relocs != relstart)
11370 && isec->map_head.s != NULL
11371 && (strcmp (isec->output_section->name, ".init") == 0
11372 || strcmp (isec->output_section->name, ".fini") == 0))
11374 if (isec->map_head.s->has_toc_reloc
11375 || isec->map_head.s->makes_toc_func_call)
11377 else if (!isec->map_head.s->call_check_done)
11380 isec->call_check_in_progress = 1;
11381 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11382 isec->call_check_in_progress = 0;
11389 isec->makes_toc_func_call = 1;
11394 /* The linker repeatedly calls this function for each input section,
11395 in the order that input sections are linked into output sections.
11396 Build lists of input sections to determine groupings between which
11397 we may insert linker stubs. */
11400 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11402 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11407 if ((isec->output_section->flags & SEC_CODE) != 0
11408 && isec->output_section->id < htab->sec_info_arr_size)
11410 /* This happens to make the list in reverse order,
11411 which is what we want. */
11412 htab->sec_info[isec->id].u.list
11413 = htab->sec_info[isec->output_section->id].u.list;
11414 htab->sec_info[isec->output_section->id].u.list = isec;
11417 if (htab->multi_toc_needed)
11419 /* Analyse sections that aren't already flagged as needing a
11420 valid toc pointer. Exclude .fixup for the linux kernel.
11421 .fixup contains branches, but only back to the function that
11422 hit an exception. */
11423 if (!(isec->has_toc_reloc
11424 || (isec->flags & SEC_CODE) == 0
11425 || strcmp (isec->name, ".fixup") == 0
11426 || isec->call_check_done))
11428 if (toc_adjusting_stub_needed (info, isec) < 0)
11431 /* Make all sections use the TOC assigned for this object file.
11432 This will be wrong for pasted sections; We fix that in
11433 check_pasted_section(). */
11434 if (elf_gp (isec->owner) != 0)
11435 htab->toc_curr = elf_gp (isec->owner);
11438 htab->sec_info[isec->id].toc_off = htab->toc_curr;
11442 /* Check that all .init and .fini sections use the same toc, if they
11443 have toc relocs. */
11446 check_pasted_section (struct bfd_link_info *info, const char *name)
11448 asection *o = bfd_get_section_by_name (info->output_bfd, name);
11452 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11453 bfd_vma toc_off = 0;
11456 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11457 if (i->has_toc_reloc)
11460 toc_off = htab->sec_info[i->id].toc_off;
11461 else if (toc_off != htab->sec_info[i->id].toc_off)
11466 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11467 if (i->makes_toc_func_call)
11469 toc_off = htab->sec_info[i->id].toc_off;
11473 /* Make sure the whole pasted function uses the same toc offset. */
11475 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11476 htab->sec_info[i->id].toc_off = toc_off;
11482 ppc64_elf_check_init_fini (struct bfd_link_info *info)
11484 return (check_pasted_section (info, ".init")
11485 & check_pasted_section (info, ".fini"));
11488 /* See whether we can group stub sections together. Grouping stub
11489 sections may result in fewer stubs. More importantly, we need to
11490 put all .init* and .fini* stubs at the beginning of the .init or
11491 .fini output sections respectively, because glibc splits the
11492 _init and _fini functions into multiple parts. Putting a stub in
11493 the middle of a function is not a good idea. */
11496 group_sections (struct bfd_link_info *info,
11497 bfd_size_type stub_group_size,
11498 bfd_boolean stubs_always_before_branch)
11500 struct ppc_link_hash_table *htab;
11502 bfd_boolean suppress_size_errors;
11504 htab = ppc_hash_table (info);
11508 suppress_size_errors = FALSE;
11509 if (stub_group_size == 1)
11511 /* Default values. */
11512 if (stubs_always_before_branch)
11513 stub_group_size = 0x1e00000;
11515 stub_group_size = 0x1c00000;
11516 suppress_size_errors = TRUE;
11519 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
11523 if (osec->id >= htab->sec_info_arr_size)
11526 tail = htab->sec_info[osec->id].u.list;
11527 while (tail != NULL)
11531 bfd_size_type total;
11532 bfd_boolean big_sec;
11534 struct map_stub *group;
11535 bfd_size_type group_size;
11538 total = tail->size;
11539 group_size = (ppc64_elf_section_data (tail) != NULL
11540 && ppc64_elf_section_data (tail)->has_14bit_branch
11541 ? stub_group_size >> 10 : stub_group_size);
11543 big_sec = total > group_size;
11544 if (big_sec && !suppress_size_errors)
11545 /* xgettext:c-format */
11546 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
11547 tail->owner, tail);
11548 curr_toc = htab->sec_info[tail->id].toc_off;
11550 while ((prev = htab->sec_info[curr->id].u.list) != NULL
11551 && ((total += curr->output_offset - prev->output_offset)
11552 < (ppc64_elf_section_data (prev) != NULL
11553 && ppc64_elf_section_data (prev)->has_14bit_branch
11554 ? (group_size = stub_group_size >> 10) : group_size))
11555 && htab->sec_info[prev->id].toc_off == curr_toc)
11558 /* OK, the size from the start of CURR to the end is less
11559 than group_size and thus can be handled by one stub
11560 section. (or the tail section is itself larger than
11561 group_size, in which case we may be toast.) We should
11562 really be keeping track of the total size of stubs added
11563 here, as stubs contribute to the final output section
11564 size. That's a little tricky, and this way will only
11565 break if stubs added make the total size more than 2^25,
11566 ie. for the default stub_group_size, if stubs total more
11567 than 2097152 bytes, or nearly 75000 plt call stubs. */
11568 group = bfd_alloc (curr->owner, sizeof (*group));
11571 group->link_sec = curr;
11572 group->stub_sec = NULL;
11573 group->needs_save_res = 0;
11574 group->lr_restore = 0;
11575 group->eh_size = 0;
11576 group->eh_base = 0;
11577 group->next = htab->group;
11578 htab->group = group;
11581 prev = htab->sec_info[tail->id].u.list;
11582 /* Set up this stub group. */
11583 htab->sec_info[tail->id].u.group = group;
11585 while (tail != curr && (tail = prev) != NULL);
11587 /* But wait, there's more! Input sections up to group_size
11588 bytes before the stub section can be handled by it too.
11589 Don't do this if we have a really large section after the
11590 stubs, as adding more stubs increases the chance that
11591 branches may not reach into the stub section. */
11592 if (!stubs_always_before_branch && !big_sec)
11595 while (prev != NULL
11596 && ((total += tail->output_offset - prev->output_offset)
11597 < (ppc64_elf_section_data (prev) != NULL
11598 && ppc64_elf_section_data (prev)->has_14bit_branch
11599 ? (group_size = stub_group_size >> 10)
11601 && htab->sec_info[prev->id].toc_off == curr_toc)
11604 prev = htab->sec_info[tail->id].u.list;
11605 htab->sec_info[tail->id].u.group = group;
11614 static const unsigned char glink_eh_frame_cie[] =
11616 0, 0, 0, 16, /* length. */
11617 0, 0, 0, 0, /* id. */
11618 1, /* CIE version. */
11619 'z', 'R', 0, /* Augmentation string. */
11620 4, /* Code alignment. */
11621 0x78, /* Data alignment. */
11623 1, /* Augmentation size. */
11624 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
11625 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
11628 /* Stripping output sections is normally done before dynamic section
11629 symbols have been allocated. This function is called later, and
11630 handles cases like htab->brlt which is mapped to its own output
11634 maybe_strip_output (struct bfd_link_info *info, asection *isec)
11636 if (isec->size == 0
11637 && isec->output_section->size == 0
11638 && !(isec->output_section->flags & SEC_KEEP)
11639 && !bfd_section_removed_from_list (info->output_bfd,
11640 isec->output_section)
11641 && elf_section_data (isec->output_section)->dynindx == 0)
11643 isec->output_section->flags |= SEC_EXCLUDE;
11644 bfd_section_list_remove (info->output_bfd, isec->output_section);
11645 info->output_bfd->section_count--;
11649 /* Determine and set the size of the stub section for a final link.
11651 The basic idea here is to examine all the relocations looking for
11652 PC-relative calls to a target that is unreachable with a "bl"
11656 ppc64_elf_size_stubs (struct bfd_link_info *info)
11658 bfd_size_type stub_group_size;
11659 bfd_boolean stubs_always_before_branch;
11660 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11665 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
11666 htab->params->plt_thread_safe = 1;
11667 if (!htab->opd_abi)
11668 htab->params->plt_thread_safe = 0;
11669 else if (htab->params->plt_thread_safe == -1)
11671 static const char *const thread_starter[] =
11675 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11677 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11678 "mq_notify", "create_timer",
11683 "GOMP_parallel_start",
11684 "GOMP_parallel_loop_static",
11685 "GOMP_parallel_loop_static_start",
11686 "GOMP_parallel_loop_dynamic",
11687 "GOMP_parallel_loop_dynamic_start",
11688 "GOMP_parallel_loop_guided",
11689 "GOMP_parallel_loop_guided_start",
11690 "GOMP_parallel_loop_runtime",
11691 "GOMP_parallel_loop_runtime_start",
11692 "GOMP_parallel_sections",
11693 "GOMP_parallel_sections_start",
11699 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
11701 struct elf_link_hash_entry *h;
11702 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11703 FALSE, FALSE, TRUE);
11704 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
11705 if (htab->params->plt_thread_safe)
11709 stubs_always_before_branch = htab->params->group_size < 0;
11710 if (htab->params->group_size < 0)
11711 stub_group_size = -htab->params->group_size;
11713 stub_group_size = htab->params->group_size;
11715 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
11718 #define STUB_SHRINK_ITER 20
11719 /* Loop until no stubs added. After iteration 20 of this loop we may
11720 exit on a stub section shrinking. This is to break out of a
11721 pathological case where adding stubs on one iteration decreases
11722 section gaps (perhaps due to alignment), which then requires
11723 fewer or smaller stubs on the next iteration. */
11728 unsigned int bfd_indx;
11729 struct map_stub *group;
11731 htab->stub_iteration += 1;
11733 for (input_bfd = info->input_bfds, bfd_indx = 0;
11735 input_bfd = input_bfd->link.next, bfd_indx++)
11737 Elf_Internal_Shdr *symtab_hdr;
11739 Elf_Internal_Sym *local_syms = NULL;
11741 if (!is_ppc64_elf (input_bfd))
11744 /* We'll need the symbol table in a second. */
11745 symtab_hdr = &elf_symtab_hdr (input_bfd);
11746 if (symtab_hdr->sh_info == 0)
11749 /* Walk over each section attached to the input bfd. */
11750 for (section = input_bfd->sections;
11752 section = section->next)
11754 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
11756 /* If there aren't any relocs, then there's nothing more
11758 if ((section->flags & SEC_RELOC) == 0
11759 || (section->flags & SEC_ALLOC) == 0
11760 || (section->flags & SEC_LOAD) == 0
11761 || (section->flags & SEC_CODE) == 0
11762 || section->reloc_count == 0)
11765 /* If this section is a link-once section that will be
11766 discarded, then don't create any stubs. */
11767 if (section->output_section == NULL
11768 || section->output_section->owner != info->output_bfd)
11771 /* Get the relocs. */
11773 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
11774 info->keep_memory);
11775 if (internal_relocs == NULL)
11776 goto error_ret_free_local;
11778 /* Now examine each relocation. */
11779 irela = internal_relocs;
11780 irelaend = irela + section->reloc_count;
11781 for (; irela < irelaend; irela++)
11783 enum elf_ppc64_reloc_type r_type;
11784 unsigned int r_indx;
11785 enum ppc_stub_type stub_type;
11786 struct ppc_stub_hash_entry *stub_entry;
11787 asection *sym_sec, *code_sec;
11788 bfd_vma sym_value, code_value;
11789 bfd_vma destination;
11790 unsigned long local_off;
11791 bfd_boolean ok_dest;
11792 struct ppc_link_hash_entry *hash;
11793 struct ppc_link_hash_entry *fdh;
11794 struct elf_link_hash_entry *h;
11795 Elf_Internal_Sym *sym;
11797 const asection *id_sec;
11798 struct _opd_sec_data *opd;
11799 struct plt_entry *plt_ent;
11801 r_type = ELF64_R_TYPE (irela->r_info);
11802 r_indx = ELF64_R_SYM (irela->r_info);
11804 if (r_type >= R_PPC64_max)
11806 bfd_set_error (bfd_error_bad_value);
11807 goto error_ret_free_internal;
11810 /* Only look for stubs on branch instructions. */
11811 if (r_type != R_PPC64_REL24
11812 && r_type != R_PPC64_REL24_NOTOC
11813 && r_type != R_PPC64_REL14
11814 && r_type != R_PPC64_REL14_BRTAKEN
11815 && r_type != R_PPC64_REL14_BRNTAKEN)
11818 /* Now determine the call target, its name, value,
11820 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11821 r_indx, input_bfd))
11822 goto error_ret_free_internal;
11823 hash = (struct ppc_link_hash_entry *) h;
11830 sym_value = sym->st_value;
11831 if (sym_sec != NULL
11832 && sym_sec->output_section != NULL)
11835 else if (hash->elf.root.type == bfd_link_hash_defined
11836 || hash->elf.root.type == bfd_link_hash_defweak)
11838 sym_value = hash->elf.root.u.def.value;
11839 if (sym_sec->output_section != NULL)
11842 else if (hash->elf.root.type == bfd_link_hash_undefweak
11843 || hash->elf.root.type == bfd_link_hash_undefined)
11845 /* Recognise an old ABI func code entry sym, and
11846 use the func descriptor sym instead if it is
11848 if (hash->elf.root.root.string[0] == '.'
11849 && hash->oh != NULL)
11851 fdh = ppc_follow_link (hash->oh);
11852 if (fdh->elf.root.type == bfd_link_hash_defined
11853 || fdh->elf.root.type == bfd_link_hash_defweak)
11855 sym_sec = fdh->elf.root.u.def.section;
11856 sym_value = fdh->elf.root.u.def.value;
11857 if (sym_sec->output_section != NULL)
11866 bfd_set_error (bfd_error_bad_value);
11867 goto error_ret_free_internal;
11874 sym_value += irela->r_addend;
11875 destination = (sym_value
11876 + sym_sec->output_offset
11877 + sym_sec->output_section->vma);
11878 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
11883 code_sec = sym_sec;
11884 code_value = sym_value;
11885 opd = get_opd_info (sym_sec);
11890 if (hash == NULL && opd->adjust != NULL)
11892 long adjust = opd->adjust[OPD_NDX (sym_value)];
11895 code_value += adjust;
11896 sym_value += adjust;
11898 dest = opd_entry_value (sym_sec, sym_value,
11899 &code_sec, &code_value, FALSE);
11900 if (dest != (bfd_vma) -1)
11902 destination = dest;
11905 /* Fixup old ABI sym to point at code
11907 hash->elf.root.type = bfd_link_hash_defweak;
11908 hash->elf.root.u.def.section = code_sec;
11909 hash->elf.root.u.def.value = code_value;
11914 /* Determine what (if any) linker stub is needed. */
11916 stub_type = ppc_type_of_stub (section, irela, &hash,
11917 &plt_ent, destination,
11920 if (r_type == R_PPC64_REL24_NOTOC)
11922 if (stub_type == ppc_stub_plt_call)
11923 stub_type = ppc_stub_plt_call_notoc;
11924 else if (stub_type == ppc_stub_long_branch
11925 || (code_sec != NULL
11926 && code_sec->output_section != NULL
11927 && (((hash ? hash->elf.other : sym->st_other)
11928 & STO_PPC64_LOCAL_MASK)
11929 != 1 << STO_PPC64_LOCAL_BIT)))
11930 stub_type = ppc_stub_long_branch_notoc;
11932 else if (stub_type != ppc_stub_plt_call)
11934 /* Check whether we need a TOC adjusting stub.
11935 Since the linker pastes together pieces from
11936 different object files when creating the
11937 _init and _fini functions, it may be that a
11938 call to what looks like a local sym is in
11939 fact a call needing a TOC adjustment. */
11940 if ((code_sec != NULL
11941 && code_sec->output_section != NULL
11942 && (htab->sec_info[code_sec->id].toc_off
11943 != htab->sec_info[section->id].toc_off)
11944 && (code_sec->has_toc_reloc
11945 || code_sec->makes_toc_func_call))
11946 || (((hash ? hash->elf.other : sym->st_other)
11947 & STO_PPC64_LOCAL_MASK)
11948 == 1 << STO_PPC64_LOCAL_BIT))
11949 stub_type = ppc_stub_long_branch_r2off;
11952 if (stub_type == ppc_stub_none)
11955 /* __tls_get_addr calls might be eliminated. */
11956 if (stub_type != ppc_stub_plt_call
11957 && stub_type != ppc_stub_plt_call_notoc
11959 && (hash == htab->tls_get_addr
11960 || hash == htab->tls_get_addr_fd)
11961 && section->has_tls_reloc
11962 && irela != internal_relocs)
11964 /* Get tls info. */
11965 unsigned char *tls_mask;
11967 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
11968 irela - 1, input_bfd))
11969 goto error_ret_free_internal;
11970 if ((*tls_mask & TLS_TLS) != 0)
11974 if (stub_type == ppc_stub_plt_call)
11977 && htab->params->plt_localentry0 != 0
11978 && is_elfv2_localentry0 (&hash->elf))
11979 htab->has_plt_localentry0 = 1;
11980 else if (irela + 1 < irelaend
11981 && irela[1].r_offset == irela->r_offset + 4
11982 && (ELF64_R_TYPE (irela[1].r_info)
11983 == R_PPC64_TOCSAVE))
11985 if (!tocsave_find (htab, INSERT,
11986 &local_syms, irela + 1, input_bfd))
11987 goto error_ret_free_internal;
11990 stub_type = ppc_stub_plt_call_r2save;
11993 /* Support for grouping stub sections. */
11994 id_sec = htab->sec_info[section->id].u.group->link_sec;
11996 /* Get the name of this stub. */
11997 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11999 goto error_ret_free_internal;
12001 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12002 stub_name, FALSE, FALSE);
12003 if (stub_entry != NULL)
12005 enum ppc_stub_type old_type;
12006 /* A stub has already been created, but it may
12007 not be the required type. We shouldn't be
12008 transitioning from plt_call to long_branch
12009 stubs or vice versa, but we might be
12010 upgrading from plt_call to plt_call_r2save or
12011 from long_branch to long_branch_r2off. */
12013 old_type = stub_entry->stub_type;
12019 case ppc_stub_save_res:
12022 case ppc_stub_plt_call:
12023 case ppc_stub_plt_call_r2save:
12024 case ppc_stub_plt_call_notoc:
12025 case ppc_stub_plt_call_both:
12026 if (stub_type == ppc_stub_plt_call)
12028 else if (stub_type == ppc_stub_plt_call_r2save)
12030 if (old_type == ppc_stub_plt_call_notoc)
12031 stub_type = ppc_stub_plt_call_both;
12033 else if (stub_type == ppc_stub_plt_call_notoc)
12035 if (old_type == ppc_stub_plt_call_r2save)
12036 stub_type = ppc_stub_plt_call_both;
12042 case ppc_stub_plt_branch:
12043 case ppc_stub_plt_branch_r2off:
12044 case ppc_stub_plt_branch_notoc:
12045 case ppc_stub_plt_branch_both:
12046 old_type += (ppc_stub_long_branch
12047 - ppc_stub_plt_branch);
12048 /* Fall through. */
12049 case ppc_stub_long_branch:
12050 case ppc_stub_long_branch_r2off:
12051 case ppc_stub_long_branch_notoc:
12052 case ppc_stub_long_branch_both:
12053 if (stub_type == ppc_stub_long_branch)
12055 else if (stub_type == ppc_stub_long_branch_r2off)
12057 if (old_type == ppc_stub_long_branch_notoc)
12058 stub_type = ppc_stub_long_branch_both;
12060 else if (stub_type == ppc_stub_long_branch_notoc)
12062 if (old_type == ppc_stub_long_branch_r2off)
12063 stub_type = ppc_stub_long_branch_both;
12069 if (old_type < stub_type)
12070 stub_entry->stub_type = stub_type;
12074 stub_entry = ppc_add_stub (stub_name, section, info);
12075 if (stub_entry == NULL)
12078 error_ret_free_internal:
12079 if (elf_section_data (section)->relocs == NULL)
12080 free (internal_relocs);
12081 error_ret_free_local:
12082 if (local_syms != NULL
12083 && (symtab_hdr->contents
12084 != (unsigned char *) local_syms))
12089 stub_entry->stub_type = stub_type;
12090 if (stub_type >= ppc_stub_plt_call
12091 && stub_type <= ppc_stub_plt_call_both)
12093 stub_entry->target_value = sym_value;
12094 stub_entry->target_section = sym_sec;
12098 stub_entry->target_value = code_value;
12099 stub_entry->target_section = code_sec;
12101 stub_entry->h = hash;
12102 stub_entry->plt_ent = plt_ent;
12103 stub_entry->symtype
12104 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
12105 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12107 if (stub_entry->h != NULL)
12108 htab->stub_globals += 1;
12111 /* We're done with the internal relocs, free them. */
12112 if (elf_section_data (section)->relocs != internal_relocs)
12113 free (internal_relocs);
12116 if (local_syms != NULL
12117 && symtab_hdr->contents != (unsigned char *) local_syms)
12119 if (!info->keep_memory)
12122 symtab_hdr->contents = (unsigned char *) local_syms;
12126 /* We may have added some stubs. Find out the new size of the
12128 for (group = htab->group; group != NULL; group = group->next)
12130 group->lr_restore = 0;
12131 group->eh_size = 0;
12132 if (group->stub_sec != NULL)
12134 asection *stub_sec = group->stub_sec;
12136 if (htab->stub_iteration <= STUB_SHRINK_ITER
12137 || stub_sec->rawsize < stub_sec->size)
12138 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12139 stub_sec->rawsize = stub_sec->size;
12140 stub_sec->size = 0;
12141 stub_sec->reloc_count = 0;
12142 stub_sec->flags &= ~SEC_RELOC;
12146 if (htab->stub_iteration <= STUB_SHRINK_ITER
12147 || htab->brlt->rawsize < htab->brlt->size)
12148 htab->brlt->rawsize = htab->brlt->size;
12149 htab->brlt->size = 0;
12150 htab->brlt->reloc_count = 0;
12151 htab->brlt->flags &= ~SEC_RELOC;
12152 if (htab->relbrlt != NULL)
12153 htab->relbrlt->size = 0;
12155 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12157 for (group = htab->group; group != NULL; group = group->next)
12158 if (group->needs_save_res)
12159 group->stub_sec->size += htab->sfpr->size;
12161 if (info->emitrelocations
12162 && htab->glink != NULL && htab->glink->size != 0)
12164 htab->glink->reloc_count = 1;
12165 htab->glink->flags |= SEC_RELOC;
12168 if (htab->glink_eh_frame != NULL
12169 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12170 && htab->glink_eh_frame->output_section->size > 8)
12172 size_t size = 0, align = 4;
12174 for (group = htab->group; group != NULL; group = group->next)
12175 if (group->eh_size != 0)
12176 size += (group->eh_size + 17 + align - 1) & -align;
12177 if (htab->glink != NULL && htab->glink->size != 0)
12178 size += (24 + align - 1) & -align;
12180 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12181 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12182 size = (size + align - 1) & -align;
12183 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12184 htab->glink_eh_frame->size = size;
12187 if (htab->params->plt_stub_align != 0)
12188 for (group = htab->group; group != NULL; group = group->next)
12189 if (group->stub_sec != NULL)
12191 int align = abs (htab->params->plt_stub_align);
12192 group->stub_sec->size
12193 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12196 for (group = htab->group; group != NULL; group = group->next)
12197 if (group->stub_sec != NULL
12198 && group->stub_sec->rawsize != group->stub_sec->size
12199 && (htab->stub_iteration <= STUB_SHRINK_ITER
12200 || group->stub_sec->rawsize < group->stub_sec->size))
12204 && (htab->brlt->rawsize == htab->brlt->size
12205 || (htab->stub_iteration > STUB_SHRINK_ITER
12206 && htab->brlt->rawsize > htab->brlt->size))
12207 && (htab->glink_eh_frame == NULL
12208 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12211 /* Ask the linker to do its stuff. */
12212 (*htab->params->layout_sections_again) ();
12215 if (htab->glink_eh_frame != NULL
12216 && htab->glink_eh_frame->size != 0)
12219 bfd_byte *p, *last_fde;
12220 size_t last_fde_len, size, align, pad;
12221 struct map_stub *group;
12223 /* It is necessary to at least have a rough outline of the
12224 linker generated CIEs and FDEs written before
12225 bfd_elf_discard_info is run, in order for these FDEs to be
12226 indexed in .eh_frame_hdr. */
12227 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12230 htab->glink_eh_frame->contents = p;
12234 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12235 /* CIE length (rewrite in case little-endian). */
12236 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
12237 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12238 p += last_fde_len + 4;
12240 for (group = htab->group; group != NULL; group = group->next)
12241 if (group->eh_size != 0)
12243 group->eh_base = p - htab->glink_eh_frame->contents;
12245 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
12247 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12250 val = p - htab->glink_eh_frame->contents;
12251 bfd_put_32 (htab->elf.dynobj, val, p);
12253 /* Offset to stub section, written later. */
12255 /* stub section size. */
12256 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
12258 /* Augmentation. */
12260 /* Make sure we don't have all nops. This is enough for
12261 elf-eh-frame.c to detect the last non-nop opcode. */
12262 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
12263 p = last_fde + last_fde_len + 4;
12265 if (htab->glink != NULL && htab->glink->size != 0)
12268 last_fde_len = ((24 + align - 1) & -align) - 4;
12270 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12273 val = p - htab->glink_eh_frame->contents;
12274 bfd_put_32 (htab->elf.dynobj, val, p);
12276 /* Offset to .glink, written later. */
12279 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12281 /* Augmentation. */
12284 *p++ = DW_CFA_advance_loc + 1;
12285 *p++ = DW_CFA_register;
12287 *p++ = htab->opd_abi ? 12 : 0;
12288 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
12289 *p++ = DW_CFA_restore_extended;
12291 p += ((24 + align - 1) & -align) - 24;
12293 /* Subsume any padding into the last FDE if user .eh_frame
12294 sections are aligned more than glink_eh_frame. Otherwise any
12295 zero padding will be seen as a terminator. */
12296 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12297 size = p - htab->glink_eh_frame->contents;
12298 pad = ((size + align - 1) & -align) - size;
12299 htab->glink_eh_frame->size = size + pad;
12300 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12303 maybe_strip_output (info, htab->brlt);
12304 if (htab->glink_eh_frame != NULL)
12305 maybe_strip_output (info, htab->glink_eh_frame);
12310 /* Called after we have determined section placement. If sections
12311 move, we'll be called again. Provide a value for TOCstart. */
12314 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12317 bfd_vma TOCstart, adjust;
12321 struct elf_link_hash_entry *h;
12322 struct elf_link_hash_table *htab = elf_hash_table (info);
12324 if (is_elf_hash_table (htab)
12325 && htab->hgot != NULL)
12329 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12330 if (is_elf_hash_table (htab))
12334 && h->root.type == bfd_link_hash_defined
12335 && !h->root.linker_def
12336 && (!is_elf_hash_table (htab)
12337 || h->def_regular))
12339 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12340 + h->root.u.def.section->output_offset
12341 + h->root.u.def.section->output_section->vma);
12342 _bfd_set_gp_value (obfd, TOCstart);
12347 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12348 order. The TOC starts where the first of these sections starts. */
12349 s = bfd_get_section_by_name (obfd, ".got");
12350 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12351 s = bfd_get_section_by_name (obfd, ".toc");
12352 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12353 s = bfd_get_section_by_name (obfd, ".tocbss");
12354 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12355 s = bfd_get_section_by_name (obfd, ".plt");
12356 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12358 /* This may happen for
12359 o references to TOC base (SYM@toc / TOC[tc0]) without a
12361 o bad linker script
12362 o --gc-sections and empty TOC sections
12364 FIXME: Warn user? */
12366 /* Look for a likely section. We probably won't even be
12368 for (s = obfd->sections; s != NULL; s = s->next)
12369 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12371 == (SEC_ALLOC | SEC_SMALL_DATA))
12374 for (s = obfd->sections; s != NULL; s = s->next)
12375 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12376 == (SEC_ALLOC | SEC_SMALL_DATA))
12379 for (s = obfd->sections; s != NULL; s = s->next)
12380 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12384 for (s = obfd->sections; s != NULL; s = s->next)
12385 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12391 TOCstart = s->output_section->vma + s->output_offset;
12393 /* Force alignment. */
12394 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12395 TOCstart -= adjust;
12396 _bfd_set_gp_value (obfd, TOCstart);
12398 if (info != NULL && s != NULL)
12400 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12404 if (htab->elf.hgot != NULL)
12406 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
12407 htab->elf.hgot->root.u.def.section = s;
12412 struct bfd_link_hash_entry *bh = NULL;
12413 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
12414 s, TOC_BASE_OFF - adjust,
12415 NULL, FALSE, FALSE, &bh);
12421 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12422 write out any global entry stubs, and PLT relocations. */
12425 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
12427 struct bfd_link_info *info;
12428 struct ppc_link_hash_table *htab;
12429 struct plt_entry *ent;
12432 if (h->root.type == bfd_link_hash_indirect)
12436 htab = ppc_hash_table (info);
12440 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
12441 if (ent->plt.offset != (bfd_vma) -1)
12443 /* This symbol has an entry in the procedure linkage
12444 table. Set it up. */
12445 Elf_Internal_Rela rela;
12446 asection *plt, *relplt;
12449 if (!htab->elf.dynamic_sections_created
12450 || h->dynindx == -1)
12452 if (!(h->def_regular
12453 && (h->root.type == bfd_link_hash_defined
12454 || h->root.type == bfd_link_hash_defweak)))
12456 if (h->type == STT_GNU_IFUNC)
12458 plt = htab->elf.iplt;
12459 relplt = htab->elf.irelplt;
12460 htab->local_ifunc_resolver = 1;
12462 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
12464 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12468 plt = htab->pltlocal;
12469 if (bfd_link_pic (info))
12471 relplt = htab->relpltlocal;
12473 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
12475 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12480 rela.r_addend = (h->root.u.def.value
12481 + h->root.u.def.section->output_offset
12482 + h->root.u.def.section->output_section->vma
12485 if (relplt == NULL)
12487 loc = plt->contents + ent->plt.offset;
12488 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
12491 bfd_vma toc = elf_gp (info->output_bfd);
12492 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
12493 bfd_put_64 (info->output_bfd, toc, loc + 8);
12498 rela.r_offset = (plt->output_section->vma
12499 + plt->output_offset
12500 + ent->plt.offset);
12501 loc = relplt->contents + (relplt->reloc_count++
12502 * sizeof (Elf64_External_Rela));
12503 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12508 rela.r_offset = (htab->elf.splt->output_section->vma
12509 + htab->elf.splt->output_offset
12510 + ent->plt.offset);
12511 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
12512 rela.r_addend = ent->addend;
12513 loc = (htab->elf.srelplt->contents
12514 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
12515 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
12516 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
12517 htab->maybe_local_ifunc_resolver = 1;
12518 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12522 if (!h->pointer_equality_needed)
12525 if (h->def_regular)
12528 s = htab->global_entry;
12529 if (s == NULL || s->size == 0)
12532 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
12533 if (ent->plt.offset != (bfd_vma) -1
12534 && ent->addend == 0)
12540 p = s->contents + h->root.u.def.value;
12541 plt = htab->elf.splt;
12542 if (!htab->elf.dynamic_sections_created
12543 || h->dynindx == -1)
12545 if (h->type == STT_GNU_IFUNC)
12546 plt = htab->elf.iplt;
12548 plt = htab->pltlocal;
12550 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
12551 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12553 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12555 info->callbacks->einfo
12556 (_("%P: linkage table error against `%pT'\n"),
12557 h->root.root.string);
12558 bfd_set_error (bfd_error_bad_value);
12559 htab->stub_error = TRUE;
12562 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12563 if (htab->params->emit_stub_syms)
12565 size_t len = strlen (h->root.root.string);
12566 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12571 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12572 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12575 if (h->root.type == bfd_link_hash_new)
12577 h->root.type = bfd_link_hash_defined;
12578 h->root.u.def.section = s;
12579 h->root.u.def.value = p - s->contents;
12580 h->ref_regular = 1;
12581 h->def_regular = 1;
12582 h->ref_regular_nonweak = 1;
12583 h->forced_local = 1;
12585 h->root.linker_def = 1;
12589 if (PPC_HA (off) != 0)
12591 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12594 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12596 bfd_put_32 (s->owner, MTCTR_R12, p);
12598 bfd_put_32 (s->owner, BCTR, p);
12604 /* Write PLT relocs for locals. */
12607 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
12609 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12612 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12614 struct got_entry **lgot_ents, **end_lgot_ents;
12615 struct plt_entry **local_plt, **lplt, **end_local_plt;
12616 Elf_Internal_Shdr *symtab_hdr;
12617 bfd_size_type locsymcount;
12618 Elf_Internal_Sym *local_syms = NULL;
12619 struct plt_entry *ent;
12621 if (!is_ppc64_elf (ibfd))
12624 lgot_ents = elf_local_got_ents (ibfd);
12628 symtab_hdr = &elf_symtab_hdr (ibfd);
12629 locsymcount = symtab_hdr->sh_info;
12630 end_lgot_ents = lgot_ents + locsymcount;
12631 local_plt = (struct plt_entry **) end_lgot_ents;
12632 end_local_plt = local_plt + locsymcount;
12633 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
12634 for (ent = *lplt; ent != NULL; ent = ent->next)
12635 if (ent->plt.offset != (bfd_vma) -1)
12637 Elf_Internal_Sym *sym;
12639 asection *plt, *relplt;
12643 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
12644 lplt - local_plt, ibfd))
12646 if (local_syms != NULL
12647 && symtab_hdr->contents != (unsigned char *) local_syms)
12652 val = sym->st_value + ent->addend;
12653 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
12654 if (sym_sec != NULL && sym_sec->output_section != NULL)
12655 val += sym_sec->output_offset + sym_sec->output_section->vma;
12657 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12659 htab->local_ifunc_resolver = 1;
12660 plt = htab->elf.iplt;
12661 relplt = htab->elf.irelplt;
12665 plt = htab->pltlocal;
12666 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
12669 if (relplt == NULL)
12671 loc = plt->contents + ent->plt.offset;
12672 bfd_put_64 (info->output_bfd, val, loc);
12675 bfd_vma toc = elf_gp (ibfd);
12676 bfd_put_64 (info->output_bfd, toc, loc + 8);
12681 Elf_Internal_Rela rela;
12682 rela.r_offset = (ent->plt.offset
12683 + plt->output_offset
12684 + plt->output_section->vma);
12685 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12688 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
12690 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12695 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
12697 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12699 rela.r_addend = val;
12700 loc = relplt->contents + (relplt->reloc_count++
12701 * sizeof (Elf64_External_Rela));
12702 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12706 if (local_syms != NULL
12707 && symtab_hdr->contents != (unsigned char *) local_syms)
12709 if (!info->keep_memory)
12712 symtab_hdr->contents = (unsigned char *) local_syms;
12718 /* Build all the stubs associated with the current output file.
12719 The stubs are kept in a hash table attached to the main linker
12720 hash table. This function is called via gldelf64ppc_finish. */
12723 ppc64_elf_build_stubs (struct bfd_link_info *info,
12726 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12727 struct map_stub *group;
12728 asection *stub_sec;
12730 int stub_sec_count = 0;
12735 /* Allocate memory to hold the linker stubs. */
12736 for (group = htab->group; group != NULL; group = group->next)
12738 group->eh_size = 0;
12739 group->lr_restore = 0;
12740 if ((stub_sec = group->stub_sec) != NULL
12741 && stub_sec->size != 0)
12743 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
12745 if (stub_sec->contents == NULL)
12747 stub_sec->size = 0;
12751 if (htab->glink != NULL && htab->glink->size != 0)
12756 /* Build the .glink plt call stub. */
12757 if (htab->params->emit_stub_syms)
12759 struct elf_link_hash_entry *h;
12760 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12761 TRUE, FALSE, FALSE);
12764 if (h->root.type == bfd_link_hash_new)
12766 h->root.type = bfd_link_hash_defined;
12767 h->root.u.def.section = htab->glink;
12768 h->root.u.def.value = 8;
12769 h->ref_regular = 1;
12770 h->def_regular = 1;
12771 h->ref_regular_nonweak = 1;
12772 h->forced_local = 1;
12774 h->root.linker_def = 1;
12777 plt0 = (htab->elf.splt->output_section->vma
12778 + htab->elf.splt->output_offset
12780 if (info->emitrelocations)
12782 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12785 r->r_offset = (htab->glink->output_offset
12786 + htab->glink->output_section->vma);
12787 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12788 r->r_addend = plt0;
12790 p = htab->glink->contents;
12791 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
12792 bfd_put_64 (htab->glink->owner, plt0, p);
12796 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12798 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12800 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12802 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12804 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12806 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12808 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12810 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12812 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12814 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12819 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
12821 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12823 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12825 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
12827 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12829 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
12831 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
12833 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12835 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
12837 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12839 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
12841 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12843 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
12846 bfd_put_32 (htab->glink->owner, BCTR, p);
12848 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
12850 /* Build the .glink lazy link call stubs. */
12852 while (p < htab->glink->contents + htab->glink->size)
12858 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
12863 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
12865 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
12870 bfd_put_32 (htab->glink->owner,
12871 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
12877 /* Build .glink global entry stubs, and PLT relocs for globals. */
12878 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
12880 if (!write_plt_relocs_for_local_syms (info))
12883 if (htab->brlt != NULL && htab->brlt->size != 0)
12885 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
12887 if (htab->brlt->contents == NULL)
12890 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
12892 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
12893 htab->relbrlt->size);
12894 if (htab->relbrlt->contents == NULL)
12898 /* Build the stubs as directed by the stub hash table. */
12899 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
12901 for (group = htab->group; group != NULL; group = group->next)
12902 if (group->needs_save_res)
12903 group->stub_sec->size += htab->sfpr->size;
12905 if (htab->relbrlt != NULL)
12906 htab->relbrlt->reloc_count = 0;
12908 if (htab->params->plt_stub_align != 0)
12909 for (group = htab->group; group != NULL; group = group->next)
12910 if ((stub_sec = group->stub_sec) != NULL)
12912 int align = abs (htab->params->plt_stub_align);
12913 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
12916 for (group = htab->group; group != NULL; group = group->next)
12917 if (group->needs_save_res)
12919 stub_sec = group->stub_sec;
12920 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
12921 htab->sfpr->contents, htab->sfpr->size);
12922 if (htab->params->emit_stub_syms)
12926 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
12927 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
12932 if (htab->glink_eh_frame != NULL
12933 && htab->glink_eh_frame->size != 0)
12938 p = htab->glink_eh_frame->contents;
12939 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12941 for (group = htab->group; group != NULL; group = group->next)
12942 if (group->eh_size != 0)
12944 /* Offset to stub section. */
12945 val = (group->stub_sec->output_section->vma
12946 + group->stub_sec->output_offset);
12947 val -= (htab->glink_eh_frame->output_section->vma
12948 + htab->glink_eh_frame->output_offset
12949 + (p + 8 - htab->glink_eh_frame->contents));
12950 if (val + 0x80000000 > 0xffffffff)
12953 (_("%s offset too large for .eh_frame sdata4 encoding"),
12954 group->stub_sec->name);
12957 bfd_put_32 (htab->elf.dynobj, val, p + 8);
12958 p += (group->eh_size + 17 + 3) & -4;
12960 if (htab->glink != NULL && htab->glink->size != 0)
12962 /* Offset to .glink. */
12963 val = (htab->glink->output_section->vma
12964 + htab->glink->output_offset
12966 val -= (htab->glink_eh_frame->output_section->vma
12967 + htab->glink_eh_frame->output_offset
12968 + (p + 8 - htab->glink_eh_frame->contents));
12969 if (val + 0x80000000 > 0xffffffff)
12972 (_("%s offset too large for .eh_frame sdata4 encoding"),
12973 htab->glink->name);
12976 bfd_put_32 (htab->elf.dynobj, val, p + 8);
12977 p += (24 + align - 1) & -align;
12981 for (group = htab->group; group != NULL; group = group->next)
12982 if ((stub_sec = group->stub_sec) != NULL)
12984 stub_sec_count += 1;
12985 if (stub_sec->rawsize != stub_sec->size
12986 && (htab->stub_iteration <= STUB_SHRINK_ITER
12987 || stub_sec->rawsize < stub_sec->size))
12993 htab->stub_error = TRUE;
12994 _bfd_error_handler (_("stubs don't match calculated size"));
12997 if (htab->stub_error)
13003 *stats = bfd_malloc (500);
13004 if (*stats == NULL)
13007 len = sprintf (*stats,
13008 ngettext ("linker stubs in %u group\n",
13009 "linker stubs in %u groups\n",
13012 sprintf (*stats + len, _(" branch %lu\n"
13013 " branch toc adj %lu\n"
13014 " branch notoc %lu\n"
13015 " branch both %lu\n"
13016 " long branch %lu\n"
13017 " long toc adj %lu\n"
13018 " long notoc %lu\n"
13021 " plt call save %lu\n"
13022 " plt call notoc %lu\n"
13023 " plt call both %lu\n"
13024 " global entry %lu"),
13025 htab->stub_count[ppc_stub_long_branch - 1],
13026 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13027 htab->stub_count[ppc_stub_long_branch_notoc - 1],
13028 htab->stub_count[ppc_stub_long_branch_both - 1],
13029 htab->stub_count[ppc_stub_plt_branch - 1],
13030 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13031 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
13032 htab->stub_count[ppc_stub_plt_branch_both - 1],
13033 htab->stub_count[ppc_stub_plt_call - 1],
13034 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13035 htab->stub_count[ppc_stub_plt_call_notoc - 1],
13036 htab->stub_count[ppc_stub_plt_call_both - 1],
13037 htab->stub_count[ppc_stub_global_entry - 1]);
13042 /* What to do when ld finds relocations against symbols defined in
13043 discarded sections. */
13045 static unsigned int
13046 ppc64_elf_action_discarded (asection *sec)
13048 if (strcmp (".opd", sec->name) == 0)
13051 if (strcmp (".toc", sec->name) == 0)
13054 if (strcmp (".toc1", sec->name) == 0)
13057 return _bfd_elf_default_action_discarded (sec);
13060 /* The RELOCATE_SECTION function is called by the ELF backend linker
13061 to handle the relocations for a section.
13063 The relocs are always passed as Rela structures; if the section
13064 actually uses Rel structures, the r_addend field will always be
13067 This function is responsible for adjust the section contents as
13068 necessary, and (if using Rela relocs and generating a
13069 relocatable output file) adjusting the reloc addend as
13072 This function does not have to worry about setting the reloc
13073 address or the reloc symbol index.
13075 LOCAL_SYMS is a pointer to the swapped in local symbols.
13077 LOCAL_SECTIONS is an array giving the section in the input file
13078 corresponding to the st_shndx field of each local symbol.
13080 The global hash table entry for the global symbols can be found
13081 via elf_sym_hashes (input_bfd).
13083 When generating relocatable output, this function must handle
13084 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13085 going to be the section symbol corresponding to the output
13086 section, which means that the addend must be adjusted
13090 ppc64_elf_relocate_section (bfd *output_bfd,
13091 struct bfd_link_info *info,
13093 asection *input_section,
13094 bfd_byte *contents,
13095 Elf_Internal_Rela *relocs,
13096 Elf_Internal_Sym *local_syms,
13097 asection **local_sections)
13099 struct ppc_link_hash_table *htab;
13100 Elf_Internal_Shdr *symtab_hdr;
13101 struct elf_link_hash_entry **sym_hashes;
13102 Elf_Internal_Rela *rel;
13103 Elf_Internal_Rela *wrel;
13104 Elf_Internal_Rela *relend;
13105 Elf_Internal_Rela outrel;
13107 struct got_entry **local_got_ents;
13109 bfd_boolean ret = TRUE;
13110 bfd_boolean is_opd;
13111 /* Assume 'at' branch hints. */
13112 bfd_boolean is_isa_v2 = TRUE;
13113 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13115 /* Initialize howto table if needed. */
13116 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13119 htab = ppc_hash_table (info);
13123 /* Don't relocate stub sections. */
13124 if (input_section->owner == htab->params->stub_bfd)
13127 BFD_ASSERT (is_ppc64_elf (input_bfd));
13129 local_got_ents = elf_local_got_ents (input_bfd);
13130 TOCstart = elf_gp (output_bfd);
13131 symtab_hdr = &elf_symtab_hdr (input_bfd);
13132 sym_hashes = elf_sym_hashes (input_bfd);
13133 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13135 rel = wrel = relocs;
13136 relend = relocs + input_section->reloc_count;
13137 for (; rel < relend; wrel++, rel++)
13139 enum elf_ppc64_reloc_type r_type;
13141 bfd_reloc_status_type r;
13142 Elf_Internal_Sym *sym;
13144 struct elf_link_hash_entry *h_elf;
13145 struct ppc_link_hash_entry *h;
13146 struct ppc_link_hash_entry *fdh;
13147 const char *sym_name;
13148 unsigned long r_symndx, toc_symndx;
13149 bfd_vma toc_addend;
13150 unsigned char tls_mask, tls_gd, tls_type;
13151 unsigned char sym_type;
13152 bfd_vma relocation;
13153 bfd_boolean unresolved_reloc, save_unresolved_reloc;
13154 bfd_boolean warned;
13155 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13158 struct ppc_stub_hash_entry *stub_entry;
13159 bfd_vma max_br_offset;
13161 Elf_Internal_Rela orig_rel;
13162 reloc_howto_type *howto;
13163 struct reloc_howto_struct alt_howto;
13168 r_type = ELF64_R_TYPE (rel->r_info);
13169 r_symndx = ELF64_R_SYM (rel->r_info);
13171 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13172 symbol of the previous ADDR64 reloc. The symbol gives us the
13173 proper TOC base to use. */
13174 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13176 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13178 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13184 unresolved_reloc = FALSE;
13187 if (r_symndx < symtab_hdr->sh_info)
13189 /* It's a local symbol. */
13190 struct _opd_sec_data *opd;
13192 sym = local_syms + r_symndx;
13193 sec = local_sections[r_symndx];
13194 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13195 sym_type = ELF64_ST_TYPE (sym->st_info);
13196 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13197 opd = get_opd_info (sec);
13198 if (opd != NULL && opd->adjust != NULL)
13200 long adjust = opd->adjust[OPD_NDX (sym->st_value
13206 /* If this is a relocation against the opd section sym
13207 and we have edited .opd, adjust the reloc addend so
13208 that ld -r and ld --emit-relocs output is correct.
13209 If it is a reloc against some other .opd symbol,
13210 then the symbol value will be adjusted later. */
13211 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13212 rel->r_addend += adjust;
13214 relocation += adjust;
13220 bfd_boolean ignored;
13222 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13223 r_symndx, symtab_hdr, sym_hashes,
13224 h_elf, sec, relocation,
13225 unresolved_reloc, warned, ignored);
13226 sym_name = h_elf->root.root.string;
13227 sym_type = h_elf->type;
13229 && sec->owner == output_bfd
13230 && strcmp (sec->name, ".opd") == 0)
13232 /* This is a symbol defined in a linker script. All
13233 such are defined in output sections, even those
13234 defined by simple assignment from a symbol defined in
13235 an input section. Transfer the symbol to an
13236 appropriate input .opd section, so that a branch to
13237 this symbol will be mapped to the location specified
13238 by the opd entry. */
13239 struct bfd_link_order *lo;
13240 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13241 if (lo->type == bfd_indirect_link_order)
13243 asection *isec = lo->u.indirect.section;
13244 if (h_elf->root.u.def.value >= isec->output_offset
13245 && h_elf->root.u.def.value < (isec->output_offset
13248 h_elf->root.u.def.value -= isec->output_offset;
13249 h_elf->root.u.def.section = isec;
13256 h = (struct ppc_link_hash_entry *) h_elf;
13258 if (sec != NULL && discarded_section (sec))
13260 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13261 input_bfd, input_section,
13262 contents + rel->r_offset);
13263 wrel->r_offset = rel->r_offset;
13265 wrel->r_addend = 0;
13267 /* For ld -r, remove relocations in debug sections against
13268 symbols defined in discarded sections. Not done for
13269 non-debug to preserve relocs in .eh_frame which the
13270 eh_frame editing code expects to be present. */
13271 if (bfd_link_relocatable (info)
13272 && (input_section->flags & SEC_DEBUGGING))
13278 if (bfd_link_relocatable (info))
13281 if (h != NULL && &h->elf == htab->elf.hgot)
13283 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13284 sec = bfd_abs_section_ptr;
13285 unresolved_reloc = FALSE;
13288 /* TLS optimizations. Replace instruction sequences and relocs
13289 based on information we collected in tls_optimize. We edit
13290 RELOCS so that --emit-relocs will output something sensible
13291 for the final instruction stream. */
13296 tls_mask = h->tls_mask;
13297 else if (local_got_ents != NULL)
13299 struct plt_entry **local_plt = (struct plt_entry **)
13300 (local_got_ents + symtab_hdr->sh_info);
13301 unsigned char *lgot_masks = (unsigned char *)
13302 (local_plt + symtab_hdr->sh_info);
13303 tls_mask = lgot_masks[r_symndx];
13305 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
13306 && (r_type == R_PPC64_TLS
13307 || r_type == R_PPC64_TLSGD
13308 || r_type == R_PPC64_TLSLD))
13310 /* Check for toc tls entries. */
13311 unsigned char *toc_tls;
13313 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13314 &local_syms, rel, input_bfd))
13318 tls_mask = *toc_tls;
13321 /* Check that tls relocs are used with tls syms, and non-tls
13322 relocs are used with non-tls syms. */
13323 if (r_symndx != STN_UNDEF
13324 && r_type != R_PPC64_NONE
13326 || h->elf.root.type == bfd_link_hash_defined
13327 || h->elf.root.type == bfd_link_hash_defweak)
13328 && (IS_PPC64_TLS_RELOC (r_type)
13329 != (sym_type == STT_TLS
13330 || (sym_type == STT_SECTION
13331 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13333 if ((tls_mask & TLS_TLS) != 0
13334 && (r_type == R_PPC64_TLS
13335 || r_type == R_PPC64_TLSGD
13336 || r_type == R_PPC64_TLSLD))
13337 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13340 info->callbacks->einfo
13341 (!IS_PPC64_TLS_RELOC (r_type)
13342 /* xgettext:c-format */
13343 ? _("%H: %s used with TLS symbol `%pT'\n")
13344 /* xgettext:c-format */
13345 : _("%H: %s used with non-TLS symbol `%pT'\n"),
13346 input_bfd, input_section, rel->r_offset,
13347 ppc64_elf_howto_table[r_type]->name,
13351 /* Ensure reloc mapping code below stays sane. */
13352 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13353 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13354 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13355 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13356 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13357 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13358 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13359 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13360 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13361 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13369 case R_PPC64_LO_DS_OPT:
13370 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13371 if ((insn & (0x3f << 26)) != 58u << 26)
13373 insn += (14u << 26) - (58u << 26);
13374 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13375 r_type = R_PPC64_TOC16_LO;
13376 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13379 case R_PPC64_TOC16:
13380 case R_PPC64_TOC16_LO:
13381 case R_PPC64_TOC16_DS:
13382 case R_PPC64_TOC16_LO_DS:
13384 /* Check for toc tls entries. */
13385 unsigned char *toc_tls;
13388 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13389 &local_syms, rel, input_bfd);
13395 tls_mask = *toc_tls;
13396 if (r_type == R_PPC64_TOC16_DS
13397 || r_type == R_PPC64_TOC16_LO_DS)
13399 if ((tls_mask & TLS_TLS) != 0
13400 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13405 /* If we found a GD reloc pair, then we might be
13406 doing a GD->IE transition. */
13409 tls_gd = TLS_TPRELGD;
13410 if ((tls_mask & TLS_TLS) != 0
13411 && (tls_mask & TLS_GD) == 0)
13414 else if (retval == 3)
13416 if ((tls_mask & TLS_TLS) != 0
13417 && (tls_mask & TLS_LD) == 0)
13425 case R_PPC64_GOT_TPREL16_HI:
13426 case R_PPC64_GOT_TPREL16_HA:
13427 if ((tls_mask & TLS_TLS) != 0
13428 && (tls_mask & TLS_TPREL) == 0)
13430 rel->r_offset -= d_offset;
13431 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13432 r_type = R_PPC64_NONE;
13433 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13437 case R_PPC64_GOT_TPREL16_DS:
13438 case R_PPC64_GOT_TPREL16_LO_DS:
13439 if ((tls_mask & TLS_TLS) != 0
13440 && (tls_mask & TLS_TPREL) == 0)
13443 insn = bfd_get_32 (input_bfd,
13444 contents + rel->r_offset - d_offset);
13446 insn |= 0x3c0d0000; /* addis 0,13,0 */
13447 bfd_put_32 (input_bfd, insn,
13448 contents + rel->r_offset - d_offset);
13449 r_type = R_PPC64_TPREL16_HA;
13450 if (toc_symndx != 0)
13452 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13453 rel->r_addend = toc_addend;
13454 /* We changed the symbol. Start over in order to
13455 get h, sym, sec etc. right. */
13459 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13464 if ((tls_mask & TLS_TLS) != 0
13465 && (tls_mask & TLS_TPREL) == 0)
13467 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13468 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13471 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
13472 /* Was PPC64_TLS which sits on insn boundary, now
13473 PPC64_TPREL16_LO which is at low-order half-word. */
13474 rel->r_offset += d_offset;
13475 r_type = R_PPC64_TPREL16_LO;
13476 if (toc_symndx != 0)
13478 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13479 rel->r_addend = toc_addend;
13480 /* We changed the symbol. Start over in order to
13481 get h, sym, sec etc. right. */
13485 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13489 case R_PPC64_GOT_TLSGD16_HI:
13490 case R_PPC64_GOT_TLSGD16_HA:
13491 tls_gd = TLS_TPRELGD;
13492 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
13496 case R_PPC64_GOT_TLSLD16_HI:
13497 case R_PPC64_GOT_TLSLD16_HA:
13498 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
13501 if ((tls_mask & tls_gd) != 0)
13502 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13503 + R_PPC64_GOT_TPREL16_DS);
13506 rel->r_offset -= d_offset;
13507 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13508 r_type = R_PPC64_NONE;
13510 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13514 case R_PPC64_GOT_TLSGD16:
13515 case R_PPC64_GOT_TLSGD16_LO:
13516 tls_gd = TLS_TPRELGD;
13517 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
13521 case R_PPC64_GOT_TLSLD16:
13522 case R_PPC64_GOT_TLSLD16_LO:
13523 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
13525 unsigned int insn1, insn2;
13529 offset = (bfd_vma) -1;
13530 /* If not using the newer R_PPC64_TLSGD/LD to mark
13531 __tls_get_addr calls, we must trust that the call
13532 stays with its arg setup insns, ie. that the next
13533 reloc is the __tls_get_addr call associated with
13534 the current reloc. Edit both insns. */
13535 if (input_section->has_tls_get_addr_call
13536 && rel + 1 < relend
13537 && branch_reloc_hash_match (input_bfd, rel + 1,
13538 htab->tls_get_addr,
13539 htab->tls_get_addr_fd))
13540 offset = rel[1].r_offset;
13541 /* We read the low GOT_TLS (or TOC16) insn because we
13542 need to keep the destination reg. It may be
13543 something other than the usual r3, and moved to r3
13544 before the call by intervening code. */
13545 insn1 = bfd_get_32 (input_bfd,
13546 contents + rel->r_offset - d_offset);
13547 if ((tls_mask & tls_gd) != 0)
13550 insn1 &= (0x1f << 21) | (0x1f << 16);
13551 insn1 |= 58 << 26; /* ld */
13552 insn2 = 0x7c636a14; /* add 3,3,13 */
13553 if (offset != (bfd_vma) -1)
13554 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13555 if ((tls_mask & TLS_EXPLICIT) == 0)
13556 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13557 + R_PPC64_GOT_TPREL16_DS);
13559 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13560 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13565 insn1 &= 0x1f << 21;
13566 insn1 |= 0x3c0d0000; /* addis r,13,0 */
13567 insn2 = 0x38630000; /* addi 3,3,0 */
13570 /* Was an LD reloc. */
13572 sec = local_sections[toc_symndx];
13574 r_symndx < symtab_hdr->sh_info;
13576 if (local_sections[r_symndx] == sec)
13578 if (r_symndx >= symtab_hdr->sh_info)
13579 r_symndx = STN_UNDEF;
13580 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13581 if (r_symndx != STN_UNDEF)
13582 rel->r_addend -= (local_syms[r_symndx].st_value
13583 + sec->output_offset
13584 + sec->output_section->vma);
13586 else if (toc_symndx != 0)
13588 r_symndx = toc_symndx;
13589 rel->r_addend = toc_addend;
13591 r_type = R_PPC64_TPREL16_HA;
13592 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13593 if (offset != (bfd_vma) -1)
13595 rel[1].r_info = ELF64_R_INFO (r_symndx,
13596 R_PPC64_TPREL16_LO);
13597 rel[1].r_offset = offset + d_offset;
13598 rel[1].r_addend = rel->r_addend;
13601 bfd_put_32 (input_bfd, insn1,
13602 contents + rel->r_offset - d_offset);
13603 if (offset != (bfd_vma) -1)
13604 bfd_put_32 (input_bfd, insn2, contents + offset);
13605 if ((tls_mask & tls_gd) == 0
13606 && (tls_gd == 0 || toc_symndx != 0))
13608 /* We changed the symbol. Start over in order
13609 to get h, sym, sec etc. right. */
13615 case R_PPC64_TLSGD:
13616 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
13617 && rel + 1 < relend)
13619 unsigned int insn2;
13620 bfd_vma offset = rel->r_offset;
13622 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
13624 bfd_put_32 (output_bfd, NOP, contents + offset);
13625 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13629 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
13630 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
13632 if ((tls_mask & TLS_TPRELGD) != 0)
13635 r_type = R_PPC64_NONE;
13636 insn2 = 0x7c636a14; /* add 3,3,13 */
13641 if (toc_symndx != 0)
13643 r_symndx = toc_symndx;
13644 rel->r_addend = toc_addend;
13646 r_type = R_PPC64_TPREL16_LO;
13647 rel->r_offset = offset + d_offset;
13648 insn2 = 0x38630000; /* addi 3,3,0 */
13650 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13651 /* Zap the reloc on the _tls_get_addr call too. */
13652 BFD_ASSERT (offset == rel[1].r_offset);
13653 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13654 bfd_put_32 (input_bfd, insn2, contents + offset);
13655 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13660 case R_PPC64_TLSLD:
13661 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
13662 && rel + 1 < relend)
13664 unsigned int insn2;
13665 bfd_vma offset = rel->r_offset;
13667 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
13669 bfd_put_32 (output_bfd, NOP, contents + offset);
13670 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13674 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
13675 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
13678 sec = local_sections[toc_symndx];
13680 r_symndx < symtab_hdr->sh_info;
13682 if (local_sections[r_symndx] == sec)
13684 if (r_symndx >= symtab_hdr->sh_info)
13685 r_symndx = STN_UNDEF;
13686 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13687 if (r_symndx != STN_UNDEF)
13688 rel->r_addend -= (local_syms[r_symndx].st_value
13689 + sec->output_offset
13690 + sec->output_section->vma);
13692 r_type = R_PPC64_TPREL16_LO;
13693 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13694 rel->r_offset = offset + d_offset;
13695 /* Zap the reloc on the _tls_get_addr call too. */
13696 BFD_ASSERT (offset == rel[1].r_offset);
13697 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13698 insn2 = 0x38630000; /* addi 3,3,0 */
13699 bfd_put_32 (input_bfd, insn2, contents + offset);
13704 case R_PPC64_DTPMOD64:
13705 if (rel + 1 < relend
13706 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13707 && rel[1].r_offset == rel->r_offset + 8)
13709 if ((tls_mask & TLS_GD) == 0)
13711 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13712 if ((tls_mask & TLS_TPRELGD) != 0)
13713 r_type = R_PPC64_TPREL64;
13716 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13717 r_type = R_PPC64_NONE;
13719 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13724 if ((tls_mask & TLS_LD) == 0)
13726 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13727 r_type = R_PPC64_NONE;
13728 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13733 case R_PPC64_TPREL64:
13734 if ((tls_mask & TLS_TPREL) == 0)
13736 r_type = R_PPC64_NONE;
13737 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13741 case R_PPC64_ENTRY:
13742 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13743 if (!bfd_link_pic (info)
13744 && !info->traditional_format
13745 && relocation + 0x80008000 <= 0xffffffff)
13747 unsigned int insn1, insn2;
13749 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13750 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13751 if ((insn1 & ~0xfffc) == LD_R2_0R12
13752 && insn2 == ADD_R2_R2_R12)
13754 bfd_put_32 (input_bfd,
13755 LIS_R2 + PPC_HA (relocation),
13756 contents + rel->r_offset);
13757 bfd_put_32 (input_bfd,
13758 ADDI_R2_R2 + PPC_LO (relocation),
13759 contents + rel->r_offset + 4);
13764 relocation -= (rel->r_offset
13765 + input_section->output_offset
13766 + input_section->output_section->vma);
13767 if (relocation + 0x80008000 <= 0xffffffff)
13769 unsigned int insn1, insn2;
13771 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13772 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13773 if ((insn1 & ~0xfffc) == LD_R2_0R12
13774 && insn2 == ADD_R2_R2_R12)
13776 bfd_put_32 (input_bfd,
13777 ADDIS_R2_R12 + PPC_HA (relocation),
13778 contents + rel->r_offset);
13779 bfd_put_32 (input_bfd,
13780 ADDI_R2_R2 + PPC_LO (relocation),
13781 contents + rel->r_offset + 4);
13787 case R_PPC64_REL16_HA:
13788 /* If we are generating a non-PIC executable, edit
13789 . 0: addis 2,12,.TOC.-0b@ha
13790 . addi 2,2,.TOC.-0b@l
13791 used by ELFv2 global entry points to set up r2, to
13794 if .TOC. is in range. */
13795 if (!bfd_link_pic (info)
13796 && !info->traditional_format
13798 && rel->r_addend == d_offset
13799 && h != NULL && &h->elf == htab->elf.hgot
13800 && rel + 1 < relend
13801 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
13802 && rel[1].r_offset == rel->r_offset + 4
13803 && rel[1].r_addend == rel->r_addend + 4
13804 && relocation + 0x80008000 <= 0xffffffff)
13806 unsigned int insn1, insn2;
13807 bfd_vma offset = rel->r_offset - d_offset;
13808 insn1 = bfd_get_32 (input_bfd, contents + offset);
13809 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
13810 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
13811 && (insn2 & 0xffff0000) == ADDI_R2_R2)
13813 r_type = R_PPC64_ADDR16_HA;
13814 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13815 rel->r_addend -= d_offset;
13816 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
13817 rel[1].r_addend -= d_offset + 4;
13818 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
13824 /* Handle other relocations that tweak non-addend part of insn. */
13826 max_br_offset = 1 << 25;
13827 addend = rel->r_addend;
13828 reloc_dest = DEST_NORMAL;
13834 case R_PPC64_TOCSAVE:
13835 if (relocation + addend == (rel->r_offset
13836 + input_section->output_offset
13837 + input_section->output_section->vma)
13838 && tocsave_find (htab, NO_INSERT,
13839 &local_syms, rel, input_bfd))
13841 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13843 || insn == CROR_151515 || insn == CROR_313131)
13844 bfd_put_32 (input_bfd,
13845 STD_R2_0R1 + STK_TOC (htab),
13846 contents + rel->r_offset);
13850 /* Branch taken prediction relocations. */
13851 case R_PPC64_ADDR14_BRTAKEN:
13852 case R_PPC64_REL14_BRTAKEN:
13853 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
13854 /* Fall through. */
13856 /* Branch not taken prediction relocations. */
13857 case R_PPC64_ADDR14_BRNTAKEN:
13858 case R_PPC64_REL14_BRNTAKEN:
13859 insn |= bfd_get_32 (input_bfd,
13860 contents + rel->r_offset) & ~(0x01 << 21);
13861 /* Fall through. */
13863 case R_PPC64_REL14:
13864 max_br_offset = 1 << 15;
13865 /* Fall through. */
13867 case R_PPC64_REL24:
13868 case R_PPC64_REL24_NOTOC:
13869 case R_PPC64_PLTCALL:
13870 /* Calls to functions with a different TOC, such as calls to
13871 shared objects, need to alter the TOC pointer. This is
13872 done using a linkage stub. A REL24 branching to these
13873 linkage stubs needs to be followed by a nop, as the nop
13874 will be replaced with an instruction to restore the TOC
13879 && h->oh->is_func_descriptor)
13880 fdh = ppc_follow_link (h->oh);
13881 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13883 if (r_type == R_PPC64_PLTCALL
13884 && stub_entry != NULL
13885 && stub_entry->stub_type >= ppc_stub_plt_call
13886 && stub_entry->stub_type <= ppc_stub_plt_call_both)
13889 if (stub_entry != NULL
13890 && ((stub_entry->stub_type >= ppc_stub_plt_call
13891 && stub_entry->stub_type <= ppc_stub_plt_call_both)
13892 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13893 || stub_entry->stub_type == ppc_stub_plt_branch_both
13894 || stub_entry->stub_type == ppc_stub_long_branch_r2off
13895 || stub_entry->stub_type == ppc_stub_long_branch_both))
13897 bfd_boolean can_plt_call = FALSE;
13899 if (stub_entry->stub_type == ppc_stub_plt_call
13901 && htab->params->plt_localentry0 != 0
13902 && is_elfv2_localentry0 (&h->elf))
13904 /* The function doesn't use or change r2. */
13905 can_plt_call = TRUE;
13907 else if (r_type == R_PPC64_REL24_NOTOC)
13909 /* NOTOC calls don't need to restore r2. */
13910 can_plt_call = TRUE;
13913 /* All of these stubs may modify r2, so there must be a
13914 branch and link followed by a nop. The nop is
13915 replaced by an insn to restore r2. */
13916 else if (rel->r_offset + 8 <= input_section->size)
13920 br = bfd_get_32 (input_bfd,
13921 contents + rel->r_offset);
13926 nop = bfd_get_32 (input_bfd,
13927 contents + rel->r_offset + 4);
13928 if (nop == LD_R2_0R1 + STK_TOC (htab))
13929 can_plt_call = TRUE;
13930 else if (nop == NOP
13931 || nop == CROR_151515
13932 || nop == CROR_313131)
13935 && (h == htab->tls_get_addr_fd
13936 || h == htab->tls_get_addr)
13937 && htab->params->tls_get_addr_opt)
13939 /* Special stub used, leave nop alone. */
13942 bfd_put_32 (input_bfd,
13943 LD_R2_0R1 + STK_TOC (htab),
13944 contents + rel->r_offset + 4);
13945 can_plt_call = TRUE;
13950 if (!can_plt_call && h != NULL)
13952 const char *name = h->elf.root.root.string;
13957 if (strncmp (name, "__libc_start_main", 17) == 0
13958 && (name[17] == 0 || name[17] == '@'))
13960 /* Allow crt1 branch to go via a toc adjusting
13961 stub. Other calls that never return could do
13962 the same, if we could detect such. */
13963 can_plt_call = TRUE;
13969 /* g++ as of 20130507 emits self-calls without a
13970 following nop. This is arguably wrong since we
13971 have conflicting information. On the one hand a
13972 global symbol and on the other a local call
13973 sequence, but don't error for this special case.
13974 It isn't possible to cheaply verify we have
13975 exactly such a call. Allow all calls to the same
13977 asection *code_sec = sec;
13979 if (get_opd_info (sec) != NULL)
13981 bfd_vma off = (relocation + addend
13982 - sec->output_section->vma
13983 - sec->output_offset);
13985 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
13987 if (code_sec == input_section)
13988 can_plt_call = TRUE;
13993 if (stub_entry->stub_type >= ppc_stub_plt_call
13994 && stub_entry->stub_type <= ppc_stub_plt_call_both)
13995 info->callbacks->einfo
13996 /* xgettext:c-format */
13997 (_("%H: call to `%pT' lacks nop, can't restore toc; "
13998 "(plt call stub)\n"),
13999 input_bfd, input_section, rel->r_offset, sym_name);
14001 info->callbacks->einfo
14002 /* xgettext:c-format */
14003 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14004 "(toc save/adjust stub)\n"),
14005 input_bfd, input_section, rel->r_offset, sym_name);
14007 bfd_set_error (bfd_error_bad_value);
14012 && stub_entry->stub_type >= ppc_stub_plt_call
14013 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14014 unresolved_reloc = FALSE;
14017 if ((stub_entry == NULL
14018 || stub_entry->stub_type == ppc_stub_long_branch
14019 || stub_entry->stub_type == ppc_stub_plt_branch)
14020 && get_opd_info (sec) != NULL)
14022 /* The branch destination is the value of the opd entry. */
14023 bfd_vma off = (relocation + addend
14024 - sec->output_section->vma
14025 - sec->output_offset);
14026 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14027 if (dest != (bfd_vma) -1)
14031 reloc_dest = DEST_OPD;
14035 /* If the branch is out of reach we ought to have a long
14037 from = (rel->r_offset
14038 + input_section->output_offset
14039 + input_section->output_section->vma);
14041 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14045 if (stub_entry != NULL
14046 && (stub_entry->stub_type == ppc_stub_long_branch
14047 || stub_entry->stub_type == ppc_stub_plt_branch)
14048 && (r_type == R_PPC64_ADDR14_BRTAKEN
14049 || r_type == R_PPC64_ADDR14_BRNTAKEN
14050 || (relocation + addend - from + max_br_offset
14051 < 2 * max_br_offset)))
14052 /* Don't use the stub if this branch is in range. */
14055 if (stub_entry != NULL
14056 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
14057 || stub_entry->stub_type == ppc_stub_long_branch_both
14058 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
14059 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14060 && (r_type != R_PPC64_REL24_NOTOC
14061 || ((fdh ? fdh->elf.other : sym->st_other)
14062 & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
14063 && (relocation + addend - from + max_br_offset
14064 < 2 * max_br_offset))
14067 if (stub_entry != NULL
14068 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
14069 || stub_entry->stub_type == ppc_stub_long_branch_both
14070 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14071 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14072 && r_type == R_PPC64_REL24_NOTOC
14073 && (relocation + addend - from + max_br_offset
14074 < 2 * max_br_offset))
14077 if (stub_entry != NULL)
14079 /* Munge up the value and addend so that we call the stub
14080 rather than the procedure directly. */
14081 asection *stub_sec = stub_entry->group->stub_sec;
14083 if (stub_entry->stub_type == ppc_stub_save_res)
14084 relocation += (stub_sec->output_offset
14085 + stub_sec->output_section->vma
14086 + stub_sec->size - htab->sfpr->size
14087 - htab->sfpr->output_offset
14088 - htab->sfpr->output_section->vma);
14090 relocation = (stub_entry->stub_offset
14091 + stub_sec->output_offset
14092 + stub_sec->output_section->vma);
14094 reloc_dest = DEST_STUB;
14096 if (((stub_entry->stub_type == ppc_stub_plt_call
14097 && ALWAYS_EMIT_R2SAVE)
14098 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14099 || stub_entry->stub_type == ppc_stub_plt_call_both)
14101 && (h == htab->tls_get_addr_fd
14102 || h == htab->tls_get_addr)
14103 && htab->params->tls_get_addr_opt)
14104 && rel + 1 < relend
14105 && rel[1].r_offset == rel->r_offset + 4
14106 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14108 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
14109 || stub_entry->stub_type == ppc_stub_plt_branch_both
14110 || stub_entry->stub_type == ppc_stub_plt_call_both)
14111 && r_type == R_PPC64_REL24_NOTOC)
14119 /* Set 'a' bit. This is 0b00010 in BO field for branch
14120 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14121 for branch on CTR insns (BO == 1a00t or 1a01t). */
14122 if ((insn & (0x14 << 21)) == (0x04 << 21))
14123 insn |= 0x02 << 21;
14124 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14125 insn |= 0x08 << 21;
14131 /* Invert 'y' bit if not the default. */
14132 if ((bfd_signed_vma) (relocation + addend - from) < 0)
14133 insn ^= 0x01 << 21;
14136 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14139 /* NOP out calls to undefined weak functions.
14140 We can thus call a weak function without first
14141 checking whether the function is defined. */
14143 && h->elf.root.type == bfd_link_hash_undefweak
14144 && h->elf.dynindx == -1
14145 && (r_type == R_PPC64_REL24
14146 || r_type == R_PPC64_REL24_NOTOC)
14150 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14156 /* Set `addend'. */
14158 save_unresolved_reloc = unresolved_reloc;
14162 /* xgettext:c-format */
14163 _bfd_error_handler (_("%pB: %s unsupported"),
14164 input_bfd, ppc64_elf_howto_table[r_type]->name);
14166 bfd_set_error (bfd_error_bad_value);
14172 case R_PPC64_TLSGD:
14173 case R_PPC64_TLSLD:
14174 case R_PPC64_TOCSAVE:
14175 case R_PPC64_GNU_VTINHERIT:
14176 case R_PPC64_GNU_VTENTRY:
14177 case R_PPC64_ENTRY:
14180 /* GOT16 relocations. Like an ADDR16 using the symbol's
14181 address in the GOT as relocation value instead of the
14182 symbol's value itself. Also, create a GOT entry for the
14183 symbol and put the symbol value there. */
14184 case R_PPC64_GOT_TLSGD16:
14185 case R_PPC64_GOT_TLSGD16_LO:
14186 case R_PPC64_GOT_TLSGD16_HI:
14187 case R_PPC64_GOT_TLSGD16_HA:
14188 tls_type = TLS_TLS | TLS_GD;
14191 case R_PPC64_GOT_TLSLD16:
14192 case R_PPC64_GOT_TLSLD16_LO:
14193 case R_PPC64_GOT_TLSLD16_HI:
14194 case R_PPC64_GOT_TLSLD16_HA:
14195 tls_type = TLS_TLS | TLS_LD;
14198 case R_PPC64_GOT_TPREL16_DS:
14199 case R_PPC64_GOT_TPREL16_LO_DS:
14200 case R_PPC64_GOT_TPREL16_HI:
14201 case R_PPC64_GOT_TPREL16_HA:
14202 tls_type = TLS_TLS | TLS_TPREL;
14205 case R_PPC64_GOT_DTPREL16_DS:
14206 case R_PPC64_GOT_DTPREL16_LO_DS:
14207 case R_PPC64_GOT_DTPREL16_HI:
14208 case R_PPC64_GOT_DTPREL16_HA:
14209 tls_type = TLS_TLS | TLS_DTPREL;
14212 case R_PPC64_GOT16:
14213 case R_PPC64_GOT16_LO:
14214 case R_PPC64_GOT16_HI:
14215 case R_PPC64_GOT16_HA:
14216 case R_PPC64_GOT16_DS:
14217 case R_PPC64_GOT16_LO_DS:
14220 /* Relocation is to the entry for this symbol in the global
14225 unsigned long indx = 0;
14226 struct got_entry *ent;
14228 if (tls_type == (TLS_TLS | TLS_LD)
14230 || !h->elf.def_dynamic))
14231 ent = ppc64_tlsld_got (input_bfd);
14236 if (!htab->elf.dynamic_sections_created
14237 || h->elf.dynindx == -1
14238 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14239 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
14240 /* This is actually a static link, or it is a
14241 -Bsymbolic link and the symbol is defined
14242 locally, or the symbol was forced to be local
14243 because of a version file. */
14247 indx = h->elf.dynindx;
14248 unresolved_reloc = FALSE;
14250 ent = h->elf.got.glist;
14254 if (local_got_ents == NULL)
14256 ent = local_got_ents[r_symndx];
14259 for (; ent != NULL; ent = ent->next)
14260 if (ent->addend == orig_rel.r_addend
14261 && ent->owner == input_bfd
14262 && ent->tls_type == tls_type)
14268 if (ent->is_indirect)
14269 ent = ent->got.ent;
14270 offp = &ent->got.offset;
14271 got = ppc64_elf_tdata (ent->owner)->got;
14275 /* The offset must always be a multiple of 8. We use the
14276 least significant bit to record whether we have already
14277 processed this entry. */
14279 if ((off & 1) != 0)
14283 /* Generate relocs for the dynamic linker, except in
14284 the case of TLSLD where we'll use one entry per
14292 ? h->elf.type == STT_GNU_IFUNC
14293 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14296 relgot = htab->elf.irelplt;
14298 htab->local_ifunc_resolver = 1;
14299 else if (is_static_defined (&h->elf))
14300 htab->maybe_local_ifunc_resolver = 1;
14303 || (bfd_link_pic (info)
14305 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
14306 || (tls_type == (TLS_TLS | TLS_LD)
14307 && !h->elf.def_dynamic))
14308 && !(tls_type == (TLS_TLS | TLS_TPREL)
14309 && bfd_link_executable (info)
14310 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
14311 relgot = ppc64_elf_tdata (ent->owner)->relgot;
14312 if (relgot != NULL)
14314 outrel.r_offset = (got->output_section->vma
14315 + got->output_offset
14317 outrel.r_addend = addend;
14318 if (tls_type & (TLS_LD | TLS_GD))
14320 outrel.r_addend = 0;
14321 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14322 if (tls_type == (TLS_TLS | TLS_GD))
14324 loc = relgot->contents;
14325 loc += (relgot->reloc_count++
14326 * sizeof (Elf64_External_Rela));
14327 bfd_elf64_swap_reloca_out (output_bfd,
14329 outrel.r_offset += 8;
14330 outrel.r_addend = addend;
14332 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14335 else if (tls_type == (TLS_TLS | TLS_DTPREL))
14336 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14337 else if (tls_type == (TLS_TLS | TLS_TPREL))
14338 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14339 else if (indx != 0)
14340 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14344 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14346 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14348 /* Write the .got section contents for the sake
14350 loc = got->contents + off;
14351 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14355 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14357 outrel.r_addend += relocation;
14358 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14360 if (htab->elf.tls_sec == NULL)
14361 outrel.r_addend = 0;
14363 outrel.r_addend -= htab->elf.tls_sec->vma;
14366 loc = relgot->contents;
14367 loc += (relgot->reloc_count++
14368 * sizeof (Elf64_External_Rela));
14369 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14372 /* Init the .got section contents here if we're not
14373 emitting a reloc. */
14376 relocation += addend;
14379 if (htab->elf.tls_sec == NULL)
14383 if (tls_type & TLS_LD)
14386 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14387 if (tls_type & TLS_TPREL)
14388 relocation += DTP_OFFSET - TP_OFFSET;
14391 if (tls_type & (TLS_GD | TLS_LD))
14393 bfd_put_64 (output_bfd, relocation,
14394 got->contents + off + 8);
14398 bfd_put_64 (output_bfd, relocation,
14399 got->contents + off);
14403 if (off >= (bfd_vma) -2)
14406 relocation = got->output_section->vma + got->output_offset + off;
14407 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14411 case R_PPC64_PLT16_HA:
14412 case R_PPC64_PLT16_HI:
14413 case R_PPC64_PLT16_LO:
14414 case R_PPC64_PLT16_LO_DS:
14415 case R_PPC64_PLT32:
14416 case R_PPC64_PLT64:
14417 case R_PPC64_PLTSEQ:
14418 case R_PPC64_PLTCALL:
14419 /* Relocation is to the entry for this symbol in the
14420 procedure linkage table. */
14421 unresolved_reloc = TRUE;
14423 struct plt_entry **plt_list = NULL;
14425 plt_list = &h->elf.plt.plist;
14426 else if (local_got_ents != NULL)
14428 struct plt_entry **local_plt = (struct plt_entry **)
14429 (local_got_ents + symtab_hdr->sh_info);
14430 plt_list = local_plt + r_symndx;
14434 struct plt_entry *ent;
14436 for (ent = *plt_list; ent != NULL; ent = ent->next)
14437 if (ent->plt.offset != (bfd_vma) -1
14438 && ent->addend == orig_rel.r_addend)
14443 plt = htab->elf.splt;
14444 if (!htab->elf.dynamic_sections_created
14446 || h->elf.dynindx == -1)
14449 ? h->elf.type == STT_GNU_IFUNC
14450 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14451 plt = htab->elf.iplt;
14453 plt = htab->pltlocal;
14455 relocation = (plt->output_section->vma
14456 + plt->output_offset
14457 + ent->plt.offset);
14458 if (r_type == R_PPC64_PLT16_HA
14459 || r_type == R_PPC64_PLT16_HI
14460 || r_type == R_PPC64_PLT16_LO
14461 || r_type == R_PPC64_PLT16_LO_DS)
14463 got = (elf_gp (output_bfd)
14464 + htab->sec_info[input_section->id].toc_off);
14468 unresolved_reloc = FALSE;
14476 /* Relocation value is TOC base. */
14477 relocation = TOCstart;
14478 if (r_symndx == STN_UNDEF)
14479 relocation += htab->sec_info[input_section->id].toc_off;
14480 else if (unresolved_reloc)
14482 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14483 relocation += htab->sec_info[sec->id].toc_off;
14485 unresolved_reloc = TRUE;
14488 /* TOC16 relocs. We want the offset relative to the TOC base,
14489 which is the address of the start of the TOC plus 0x8000.
14490 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14492 case R_PPC64_TOC16:
14493 case R_PPC64_TOC16_LO:
14494 case R_PPC64_TOC16_HI:
14495 case R_PPC64_TOC16_DS:
14496 case R_PPC64_TOC16_LO_DS:
14497 case R_PPC64_TOC16_HA:
14498 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14501 /* Relocate against the beginning of the section. */
14502 case R_PPC64_SECTOFF:
14503 case R_PPC64_SECTOFF_LO:
14504 case R_PPC64_SECTOFF_HI:
14505 case R_PPC64_SECTOFF_DS:
14506 case R_PPC64_SECTOFF_LO_DS:
14507 case R_PPC64_SECTOFF_HA:
14509 addend -= sec->output_section->vma;
14512 case R_PPC64_REL16:
14513 case R_PPC64_REL16_LO:
14514 case R_PPC64_REL16_HI:
14515 case R_PPC64_REL16_HA:
14516 case R_PPC64_REL16DX_HA:
14519 case R_PPC64_REL14:
14520 case R_PPC64_REL14_BRNTAKEN:
14521 case R_PPC64_REL14_BRTAKEN:
14522 case R_PPC64_REL24:
14523 case R_PPC64_REL24_NOTOC:
14526 case R_PPC64_TPREL16:
14527 case R_PPC64_TPREL16_LO:
14528 case R_PPC64_TPREL16_HI:
14529 case R_PPC64_TPREL16_HA:
14530 case R_PPC64_TPREL16_DS:
14531 case R_PPC64_TPREL16_LO_DS:
14532 case R_PPC64_TPREL16_HIGH:
14533 case R_PPC64_TPREL16_HIGHA:
14534 case R_PPC64_TPREL16_HIGHER:
14535 case R_PPC64_TPREL16_HIGHERA:
14536 case R_PPC64_TPREL16_HIGHEST:
14537 case R_PPC64_TPREL16_HIGHESTA:
14539 && h->elf.root.type == bfd_link_hash_undefweak
14540 && h->elf.dynindx == -1)
14542 /* Make this relocation against an undefined weak symbol
14543 resolve to zero. This is really just a tweak, since
14544 code using weak externs ought to check that they are
14545 defined before using them. */
14546 bfd_byte *p = contents + rel->r_offset - d_offset;
14548 insn = bfd_get_32 (input_bfd, p);
14549 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14551 bfd_put_32 (input_bfd, insn, p);
14554 if (htab->elf.tls_sec != NULL)
14555 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14556 /* The TPREL16 relocs shouldn't really be used in shared
14557 libs or with non-local symbols as that will result in
14558 DT_TEXTREL being set, but support them anyway. */
14561 case R_PPC64_DTPREL16:
14562 case R_PPC64_DTPREL16_LO:
14563 case R_PPC64_DTPREL16_HI:
14564 case R_PPC64_DTPREL16_HA:
14565 case R_PPC64_DTPREL16_DS:
14566 case R_PPC64_DTPREL16_LO_DS:
14567 case R_PPC64_DTPREL16_HIGH:
14568 case R_PPC64_DTPREL16_HIGHA:
14569 case R_PPC64_DTPREL16_HIGHER:
14570 case R_PPC64_DTPREL16_HIGHERA:
14571 case R_PPC64_DTPREL16_HIGHEST:
14572 case R_PPC64_DTPREL16_HIGHESTA:
14573 if (htab->elf.tls_sec != NULL)
14574 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14577 case R_PPC64_ADDR64_LOCAL:
14578 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14583 case R_PPC64_DTPMOD64:
14588 case R_PPC64_TPREL64:
14589 if (htab->elf.tls_sec != NULL)
14590 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14593 case R_PPC64_DTPREL64:
14594 if (htab->elf.tls_sec != NULL)
14595 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14596 /* Fall through. */
14598 /* Relocations that may need to be propagated if this is a
14600 case R_PPC64_REL30:
14601 case R_PPC64_REL32:
14602 case R_PPC64_REL64:
14603 case R_PPC64_ADDR14:
14604 case R_PPC64_ADDR14_BRNTAKEN:
14605 case R_PPC64_ADDR14_BRTAKEN:
14606 case R_PPC64_ADDR16:
14607 case R_PPC64_ADDR16_DS:
14608 case R_PPC64_ADDR16_HA:
14609 case R_PPC64_ADDR16_HI:
14610 case R_PPC64_ADDR16_HIGH:
14611 case R_PPC64_ADDR16_HIGHA:
14612 case R_PPC64_ADDR16_HIGHER:
14613 case R_PPC64_ADDR16_HIGHERA:
14614 case R_PPC64_ADDR16_HIGHEST:
14615 case R_PPC64_ADDR16_HIGHESTA:
14616 case R_PPC64_ADDR16_LO:
14617 case R_PPC64_ADDR16_LO_DS:
14618 case R_PPC64_ADDR24:
14619 case R_PPC64_ADDR32:
14620 case R_PPC64_ADDR64:
14621 case R_PPC64_UADDR16:
14622 case R_PPC64_UADDR32:
14623 case R_PPC64_UADDR64:
14625 if ((input_section->flags & SEC_ALLOC) == 0)
14628 if (NO_OPD_RELOCS && is_opd)
14631 if (bfd_link_pic (info)
14633 || h->dyn_relocs != NULL)
14634 && ((h != NULL && pc_dynrelocs (h))
14635 || must_be_dyn_reloc (info, r_type)))
14637 ? h->dyn_relocs != NULL
14638 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14640 bfd_boolean skip, relocate;
14645 /* When generating a dynamic object, these relocations
14646 are copied into the output file to be resolved at run
14652 out_off = _bfd_elf_section_offset (output_bfd, info,
14653 input_section, rel->r_offset);
14654 if (out_off == (bfd_vma) -1)
14656 else if (out_off == (bfd_vma) -2)
14657 skip = TRUE, relocate = TRUE;
14658 out_off += (input_section->output_section->vma
14659 + input_section->output_offset);
14660 outrel.r_offset = out_off;
14661 outrel.r_addend = rel->r_addend;
14663 /* Optimize unaligned reloc use. */
14664 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14665 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14666 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14667 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14668 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14669 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14670 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14671 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14672 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14675 memset (&outrel, 0, sizeof outrel);
14676 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14678 && r_type != R_PPC64_TOC)
14680 indx = h->elf.dynindx;
14681 BFD_ASSERT (indx != -1);
14682 outrel.r_info = ELF64_R_INFO (indx, r_type);
14686 /* This symbol is local, or marked to become local,
14687 or this is an opd section reloc which must point
14688 at a local function. */
14689 outrel.r_addend += relocation;
14690 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14692 if (is_opd && h != NULL)
14694 /* Lie about opd entries. This case occurs
14695 when building shared libraries and we
14696 reference a function in another shared
14697 lib. The same thing happens for a weak
14698 definition in an application that's
14699 overridden by a strong definition in a
14700 shared lib. (I believe this is a generic
14701 bug in binutils handling of weak syms.)
14702 In these cases we won't use the opd
14703 entry in this lib. */
14704 unresolved_reloc = FALSE;
14707 && r_type == R_PPC64_ADDR64
14709 ? h->elf.type == STT_GNU_IFUNC
14710 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14711 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14714 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14716 /* We need to relocate .opd contents for ld.so.
14717 Prelink also wants simple and consistent rules
14718 for relocs. This make all RELATIVE relocs have
14719 *r_offset equal to r_addend. */
14726 ? h->elf.type == STT_GNU_IFUNC
14727 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14729 info->callbacks->einfo
14730 /* xgettext:c-format */
14731 (_("%H: %s for indirect "
14732 "function `%pT' unsupported\n"),
14733 input_bfd, input_section, rel->r_offset,
14734 ppc64_elf_howto_table[r_type]->name,
14738 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14740 else if (sec == NULL || sec->owner == NULL)
14742 bfd_set_error (bfd_error_bad_value);
14749 osec = sec->output_section;
14750 indx = elf_section_data (osec)->dynindx;
14754 if ((osec->flags & SEC_READONLY) == 0
14755 && htab->elf.data_index_section != NULL)
14756 osec = htab->elf.data_index_section;
14758 osec = htab->elf.text_index_section;
14759 indx = elf_section_data (osec)->dynindx;
14761 BFD_ASSERT (indx != 0);
14763 /* We are turning this relocation into one
14764 against a section symbol, so subtract out
14765 the output section's address but not the
14766 offset of the input section in the output
14768 outrel.r_addend -= osec->vma;
14771 outrel.r_info = ELF64_R_INFO (indx, r_type);
14775 sreloc = elf_section_data (input_section)->sreloc;
14777 ? h->elf.type == STT_GNU_IFUNC
14778 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14780 sreloc = htab->elf.irelplt;
14782 htab->local_ifunc_resolver = 1;
14783 else if (is_static_defined (&h->elf))
14784 htab->maybe_local_ifunc_resolver = 1;
14786 if (sreloc == NULL)
14789 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14792 loc = sreloc->contents;
14793 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14794 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14796 /* If this reloc is against an external symbol, it will
14797 be computed at runtime, so there's no need to do
14798 anything now. However, for the sake of prelink ensure
14799 that the section contents are a known value. */
14802 unresolved_reloc = FALSE;
14803 /* The value chosen here is quite arbitrary as ld.so
14804 ignores section contents except for the special
14805 case of .opd where the contents might be accessed
14806 before relocation. Choose zero, as that won't
14807 cause reloc overflow. */
14810 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14811 to improve backward compatibility with older
14813 if (r_type == R_PPC64_ADDR64)
14814 addend = outrel.r_addend;
14815 /* Adjust pc_relative relocs to have zero in *r_offset. */
14816 else if (ppc64_elf_howto_table[r_type]->pc_relative)
14817 addend = outrel.r_offset;
14823 case R_PPC64_GLOB_DAT:
14824 case R_PPC64_JMP_SLOT:
14825 case R_PPC64_JMP_IREL:
14826 case R_PPC64_RELATIVE:
14827 /* We shouldn't ever see these dynamic relocs in relocatable
14829 /* Fall through. */
14831 case R_PPC64_PLTGOT16:
14832 case R_PPC64_PLTGOT16_DS:
14833 case R_PPC64_PLTGOT16_HA:
14834 case R_PPC64_PLTGOT16_HI:
14835 case R_PPC64_PLTGOT16_LO:
14836 case R_PPC64_PLTGOT16_LO_DS:
14837 case R_PPC64_PLTREL32:
14838 case R_PPC64_PLTREL64:
14839 /* These ones haven't been implemented yet. */
14841 info->callbacks->einfo
14842 /* xgettext:c-format */
14843 (_("%P: %pB: %s is not supported for `%pT'\n"),
14845 ppc64_elf_howto_table[r_type]->name, sym_name);
14847 bfd_set_error (bfd_error_invalid_operation);
14852 /* Multi-instruction sequences that access the TOC can be
14853 optimized, eg. addis ra,r2,0; addi rb,ra,x;
14854 to nop; addi rb,r2,x; */
14860 case R_PPC64_GOT_TLSLD16_HI:
14861 case R_PPC64_GOT_TLSGD16_HI:
14862 case R_PPC64_GOT_TPREL16_HI:
14863 case R_PPC64_GOT_DTPREL16_HI:
14864 case R_PPC64_GOT16_HI:
14865 case R_PPC64_TOC16_HI:
14866 /* These relocs would only be useful if building up an
14867 offset to later add to r2, perhaps in an indexed
14868 addressing mode instruction. Don't try to optimize.
14869 Unfortunately, the possibility of someone building up an
14870 offset like this or even with the HA relocs, means that
14871 we need to check the high insn when optimizing the low
14875 case R_PPC64_PLTCALL:
14876 if (unresolved_reloc)
14878 /* No plt entry. Make this into a direct call. */
14879 bfd_byte *p = contents + rel->r_offset;
14880 insn = bfd_get_32 (input_bfd, p);
14882 bfd_put_32 (input_bfd, B_DOT | insn, p);
14883 bfd_put_32 (input_bfd, NOP, p + 4);
14884 unresolved_reloc = save_unresolved_reloc;
14885 r_type = R_PPC64_REL24;
14889 case R_PPC64_PLTSEQ:
14890 if (unresolved_reloc)
14892 unresolved_reloc = FALSE;
14897 case R_PPC64_PLT16_HA:
14898 if (unresolved_reloc)
14900 unresolved_reloc = FALSE;
14903 /* Fall through. */
14904 case R_PPC64_GOT_TLSLD16_HA:
14905 case R_PPC64_GOT_TLSGD16_HA:
14906 case R_PPC64_GOT_TPREL16_HA:
14907 case R_PPC64_GOT_DTPREL16_HA:
14908 case R_PPC64_GOT16_HA:
14909 case R_PPC64_TOC16_HA:
14910 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14911 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14915 p = contents + (rel->r_offset & ~3);
14916 bfd_put_32 (input_bfd, NOP, p);
14921 case R_PPC64_PLT16_LO:
14922 case R_PPC64_PLT16_LO_DS:
14923 if (unresolved_reloc)
14925 unresolved_reloc = FALSE;
14928 /* Fall through. */
14929 case R_PPC64_GOT_TLSLD16_LO:
14930 case R_PPC64_GOT_TLSGD16_LO:
14931 case R_PPC64_GOT_TPREL16_LO_DS:
14932 case R_PPC64_GOT_DTPREL16_LO_DS:
14933 case R_PPC64_GOT16_LO:
14934 case R_PPC64_GOT16_LO_DS:
14935 case R_PPC64_TOC16_LO:
14936 case R_PPC64_TOC16_LO_DS:
14937 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14938 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14940 bfd_byte *p = contents + (rel->r_offset & ~3);
14941 insn = bfd_get_32 (input_bfd, p);
14942 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14944 /* Transform addic to addi when we change reg. */
14945 insn &= ~((0x3f << 26) | (0x1f << 16));
14946 insn |= (14u << 26) | (2 << 16);
14950 insn &= ~(0x1f << 16);
14953 bfd_put_32 (input_bfd, insn, p);
14957 case R_PPC64_TPREL16_HA:
14958 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
14960 bfd_byte *p = contents + (rel->r_offset & ~3);
14961 insn = bfd_get_32 (input_bfd, p);
14962 if ((insn & ((0x3f << 26) | 0x1f << 16))
14963 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
14964 /* xgettext:c-format */
14965 info->callbacks->minfo
14966 (_("%H: warning: %s unexpected insn %#x.\n"),
14967 input_bfd, input_section, rel->r_offset,
14968 ppc64_elf_howto_table[r_type]->name, insn);
14971 bfd_put_32 (input_bfd, NOP, p);
14977 case R_PPC64_TPREL16_LO:
14978 case R_PPC64_TPREL16_LO_DS:
14979 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
14981 bfd_byte *p = contents + (rel->r_offset & ~3);
14982 insn = bfd_get_32 (input_bfd, p);
14983 insn &= ~(0x1f << 16);
14985 bfd_put_32 (input_bfd, insn, p);
14990 /* Do any further special processing. */
14996 case R_PPC64_REL16_HA:
14997 case R_PPC64_REL16DX_HA:
14998 case R_PPC64_ADDR16_HA:
14999 case R_PPC64_ADDR16_HIGHA:
15000 case R_PPC64_ADDR16_HIGHERA:
15001 case R_PPC64_ADDR16_HIGHESTA:
15002 case R_PPC64_TOC16_HA:
15003 case R_PPC64_SECTOFF_HA:
15004 case R_PPC64_TPREL16_HA:
15005 case R_PPC64_TPREL16_HIGHA:
15006 case R_PPC64_TPREL16_HIGHERA:
15007 case R_PPC64_TPREL16_HIGHESTA:
15008 case R_PPC64_DTPREL16_HA:
15009 case R_PPC64_DTPREL16_HIGHA:
15010 case R_PPC64_DTPREL16_HIGHERA:
15011 case R_PPC64_DTPREL16_HIGHESTA:
15012 /* It's just possible that this symbol is a weak symbol
15013 that's not actually defined anywhere. In that case,
15014 'sec' would be NULL, and we should leave the symbol
15015 alone (it will be set to zero elsewhere in the link). */
15018 /* Fall through. */
15020 case R_PPC64_GOT16_HA:
15021 case R_PPC64_PLTGOT16_HA:
15022 case R_PPC64_PLT16_HA:
15023 case R_PPC64_GOT_TLSGD16_HA:
15024 case R_PPC64_GOT_TLSLD16_HA:
15025 case R_PPC64_GOT_TPREL16_HA:
15026 case R_PPC64_GOT_DTPREL16_HA:
15027 /* Add 0x10000 if sign bit in 0:15 is set.
15028 Bits 0:15 are not used. */
15032 case R_PPC64_ADDR16_DS:
15033 case R_PPC64_ADDR16_LO_DS:
15034 case R_PPC64_GOT16_DS:
15035 case R_PPC64_GOT16_LO_DS:
15036 case R_PPC64_PLT16_LO_DS:
15037 case R_PPC64_SECTOFF_DS:
15038 case R_PPC64_SECTOFF_LO_DS:
15039 case R_PPC64_TOC16_DS:
15040 case R_PPC64_TOC16_LO_DS:
15041 case R_PPC64_PLTGOT16_DS:
15042 case R_PPC64_PLTGOT16_LO_DS:
15043 case R_PPC64_GOT_TPREL16_DS:
15044 case R_PPC64_GOT_TPREL16_LO_DS:
15045 case R_PPC64_GOT_DTPREL16_DS:
15046 case R_PPC64_GOT_DTPREL16_LO_DS:
15047 case R_PPC64_TPREL16_DS:
15048 case R_PPC64_TPREL16_LO_DS:
15049 case R_PPC64_DTPREL16_DS:
15050 case R_PPC64_DTPREL16_LO_DS:
15051 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15053 /* If this reloc is against an lq, lxv, or stxv insn, then
15054 the value must be a multiple of 16. This is somewhat of
15055 a hack, but the "correct" way to do this by defining _DQ
15056 forms of all the _DS relocs bloats all reloc switches in
15057 this file. It doesn't make much sense to use these
15058 relocs in data, so testing the insn should be safe. */
15059 if ((insn & (0x3f << 26)) == (56u << 26)
15060 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15062 relocation += addend;
15063 addend = insn & (mask ^ 3);
15064 if ((relocation & mask) != 0)
15066 relocation ^= relocation & mask;
15067 info->callbacks->einfo
15068 /* xgettext:c-format */
15069 (_("%H: error: %s not a multiple of %u\n"),
15070 input_bfd, input_section, rel->r_offset,
15071 ppc64_elf_howto_table[r_type]->name,
15073 bfd_set_error (bfd_error_bad_value);
15080 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15081 because such sections are not SEC_ALLOC and thus ld.so will
15082 not process them. */
15083 howto = ppc64_elf_howto_table[(int) r_type];
15084 if (unresolved_reloc
15085 && !((input_section->flags & SEC_DEBUGGING) != 0
15086 && h->elf.def_dynamic)
15087 && _bfd_elf_section_offset (output_bfd, info, input_section,
15088 rel->r_offset) != (bfd_vma) -1)
15090 info->callbacks->einfo
15091 /* xgettext:c-format */
15092 (_("%H: unresolvable %s against `%pT'\n"),
15093 input_bfd, input_section, rel->r_offset,
15095 h->elf.root.root.string);
15099 /* 16-bit fields in insns mostly have signed values, but a
15100 few insns have 16-bit unsigned values. Really, we should
15101 have different reloc types. */
15102 if (howto->complain_on_overflow != complain_overflow_dont
15103 && howto->dst_mask == 0xffff
15104 && (input_section->flags & SEC_CODE) != 0)
15106 enum complain_overflow complain = complain_overflow_signed;
15108 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15109 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15110 complain = complain_overflow_bitfield;
15111 else if (howto->rightshift == 0
15112 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15113 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15114 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15115 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15116 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15117 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15118 complain = complain_overflow_unsigned;
15119 if (howto->complain_on_overflow != complain)
15121 alt_howto = *howto;
15122 alt_howto.complain_on_overflow = complain;
15123 howto = &alt_howto;
15127 if (r_type == R_PPC64_REL16DX_HA)
15129 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15130 if (rel->r_offset + 4 > input_section->size)
15131 r = bfd_reloc_outofrange;
15134 relocation += addend;
15135 relocation -= (rel->r_offset
15136 + input_section->output_offset
15137 + input_section->output_section->vma);
15138 relocation = (bfd_signed_vma) relocation >> 16;
15139 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15141 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15142 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15144 if (relocation + 0x8000 > 0xffff)
15145 r = bfd_reloc_overflow;
15149 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15150 rel->r_offset, relocation, addend);
15152 if (r != bfd_reloc_ok)
15154 char *more_info = NULL;
15155 const char *reloc_name = howto->name;
15157 if (reloc_dest != DEST_NORMAL)
15159 more_info = bfd_malloc (strlen (reloc_name) + 8);
15160 if (more_info != NULL)
15162 strcpy (more_info, reloc_name);
15163 strcat (more_info, (reloc_dest == DEST_OPD
15164 ? " (OPD)" : " (stub)"));
15165 reloc_name = more_info;
15169 if (r == bfd_reloc_overflow)
15171 /* On code like "if (foo) foo();" don't report overflow
15172 on a branch to zero when foo is undefined. */
15174 && (reloc_dest == DEST_STUB
15176 && (h->elf.root.type == bfd_link_hash_undefweak
15177 || h->elf.root.type == bfd_link_hash_undefined)
15178 && is_branch_reloc (r_type))))
15179 info->callbacks->reloc_overflow (info, &h->elf.root,
15180 sym_name, reloc_name,
15182 input_bfd, input_section,
15187 info->callbacks->einfo
15188 /* xgettext:c-format */
15189 (_("%H: %s against `%pT': error %d\n"),
15190 input_bfd, input_section, rel->r_offset,
15191 reloc_name, sym_name, (int) r);
15194 if (more_info != NULL)
15204 Elf_Internal_Shdr *rel_hdr;
15205 size_t deleted = rel - wrel;
15207 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15208 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15209 if (rel_hdr->sh_size == 0)
15211 /* It is too late to remove an empty reloc section. Leave
15213 ??? What is wrong with an empty section??? */
15214 rel_hdr->sh_size = rel_hdr->sh_entsize;
15217 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15218 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15219 input_section->reloc_count -= deleted;
15222 /* If we're emitting relocations, then shortly after this function
15223 returns, reloc offsets and addends for this section will be
15224 adjusted. Worse, reloc symbol indices will be for the output
15225 file rather than the input. Save a copy of the relocs for
15226 opd_entry_value. */
15227 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15230 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15231 rel = bfd_alloc (input_bfd, amt);
15232 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15233 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15236 memcpy (rel, relocs, amt);
15241 /* Adjust the value of any local symbols in opd sections. */
15244 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15245 const char *name ATTRIBUTE_UNUSED,
15246 Elf_Internal_Sym *elfsym,
15247 asection *input_sec,
15248 struct elf_link_hash_entry *h)
15250 struct _opd_sec_data *opd;
15257 opd = get_opd_info (input_sec);
15258 if (opd == NULL || opd->adjust == NULL)
15261 value = elfsym->st_value - input_sec->output_offset;
15262 if (!bfd_link_relocatable (info))
15263 value -= input_sec->output_section->vma;
15265 adjust = opd->adjust[OPD_NDX (value)];
15269 elfsym->st_value += adjust;
15273 /* Finish up dynamic symbol handling. We set the contents of various
15274 dynamic sections here. */
15277 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15278 struct bfd_link_info *info,
15279 struct elf_link_hash_entry *h,
15280 Elf_Internal_Sym *sym)
15282 struct ppc_link_hash_table *htab;
15283 struct plt_entry *ent;
15285 htab = ppc_hash_table (info);
15289 if (!htab->opd_abi && !h->def_regular)
15290 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15291 if (ent->plt.offset != (bfd_vma) -1)
15293 /* Mark the symbol as undefined, rather than as
15294 defined in glink. Leave the value if there were
15295 any relocations where pointer equality matters
15296 (this is a clue for the dynamic linker, to make
15297 function pointer comparisons work between an
15298 application and shared library), otherwise set it
15300 sym->st_shndx = SHN_UNDEF;
15301 if (!h->pointer_equality_needed)
15303 else if (!h->ref_regular_nonweak)
15305 /* This breaks function pointer comparisons, but
15306 that is better than breaking tests for a NULL
15307 function pointer. */
15315 /* This symbol needs a copy reloc. Set it up. */
15316 Elf_Internal_Rela rela;
15320 if (h->dynindx == -1
15321 || (h->root.type != bfd_link_hash_defined
15322 && h->root.type != bfd_link_hash_defweak)
15323 || htab->elf.srelbss == NULL
15324 || htab->elf.sreldynrelro == NULL)
15327 rela.r_offset = (h->root.u.def.value
15328 + h->root.u.def.section->output_section->vma
15329 + h->root.u.def.section->output_offset);
15330 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15332 if (h->root.u.def.section == htab->elf.sdynrelro)
15333 srel = htab->elf.sreldynrelro;
15335 srel = htab->elf.srelbss;
15336 loc = srel->contents;
15337 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
15338 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15344 /* Used to decide how to sort relocs in an optimal manner for the
15345 dynamic linker, before writing them out. */
15347 static enum elf_reloc_type_class
15348 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15349 const asection *rel_sec,
15350 const Elf_Internal_Rela *rela)
15352 enum elf_ppc64_reloc_type r_type;
15353 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15355 if (rel_sec == htab->elf.irelplt)
15356 return reloc_class_ifunc;
15358 r_type = ELF64_R_TYPE (rela->r_info);
15361 case R_PPC64_RELATIVE:
15362 return reloc_class_relative;
15363 case R_PPC64_JMP_SLOT:
15364 return reloc_class_plt;
15366 return reloc_class_copy;
15368 return reloc_class_normal;
15372 /* Finish up the dynamic sections. */
15375 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15376 struct bfd_link_info *info)
15378 struct ppc_link_hash_table *htab;
15382 htab = ppc_hash_table (info);
15386 dynobj = htab->elf.dynobj;
15387 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15389 if (htab->elf.dynamic_sections_created)
15391 Elf64_External_Dyn *dyncon, *dynconend;
15393 if (sdyn == NULL || htab->elf.sgot == NULL)
15396 dyncon = (Elf64_External_Dyn *) sdyn->contents;
15397 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15398 for (; dyncon < dynconend; dyncon++)
15400 Elf_Internal_Dyn dyn;
15403 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15410 case DT_PPC64_GLINK:
15412 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15413 /* We stupidly defined DT_PPC64_GLINK to be the start
15414 of glink rather than the first entry point, which is
15415 what ld.so needs, and now have a bigger stub to
15416 support automatic multiple TOCs. */
15417 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
15421 s = bfd_get_section_by_name (output_bfd, ".opd");
15424 dyn.d_un.d_ptr = s->vma;
15428 if (htab->do_multi_toc && htab->multi_toc_needed)
15429 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15430 if (htab->has_plt_localentry0)
15431 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
15434 case DT_PPC64_OPDSZ:
15435 s = bfd_get_section_by_name (output_bfd, ".opd");
15438 dyn.d_un.d_val = s->size;
15442 s = htab->elf.splt;
15443 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15447 s = htab->elf.srelplt;
15448 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15452 dyn.d_un.d_val = htab->elf.srelplt->size;
15456 if (htab->local_ifunc_resolver)
15457 info->callbacks->einfo
15458 (_("%X%P: text relocations and GNU indirect "
15459 "functions will result in a segfault at runtime\n"));
15460 else if (htab->maybe_local_ifunc_resolver)
15461 info->callbacks->einfo
15462 (_("%P: warning: text relocations and GNU indirect "
15463 "functions may result in a segfault at runtime\n"));
15467 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15471 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15472 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
15474 /* Fill in the first entry in the global offset table.
15475 We use it to hold the link-time TOCbase. */
15476 bfd_put_64 (output_bfd,
15477 elf_gp (output_bfd) + TOC_BASE_OFF,
15478 htab->elf.sgot->contents);
15480 /* Set .got entry size. */
15481 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
15485 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15486 && htab->elf.splt->output_section != bfd_abs_section_ptr)
15488 /* Set .plt entry size. */
15489 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15490 = PLT_ENTRY_SIZE (htab);
15493 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15494 brlt ourselves if emitrelocations. */
15495 if (htab->brlt != NULL
15496 && htab->brlt->reloc_count != 0
15497 && !_bfd_elf_link_output_relocs (output_bfd,
15499 elf_section_data (htab->brlt)->rela.hdr,
15500 elf_section_data (htab->brlt)->relocs,
15504 if (htab->glink != NULL
15505 && htab->glink->reloc_count != 0
15506 && !_bfd_elf_link_output_relocs (output_bfd,
15508 elf_section_data (htab->glink)->rela.hdr,
15509 elf_section_data (htab->glink)->relocs,
15514 if (htab->glink_eh_frame != NULL
15515 && htab->glink_eh_frame->size != 0
15516 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15517 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15518 htab->glink_eh_frame,
15519 htab->glink_eh_frame->contents))
15522 /* We need to handle writing out multiple GOT sections ourselves,
15523 since we didn't add them to DYNOBJ. We know dynobj is the first
15525 while ((dynobj = dynobj->link.next) != NULL)
15529 if (!is_ppc64_elf (dynobj))
15532 s = ppc64_elf_tdata (dynobj)->got;
15535 && s->output_section != bfd_abs_section_ptr
15536 && !bfd_set_section_contents (output_bfd, s->output_section,
15537 s->contents, s->output_offset,
15540 s = ppc64_elf_tdata (dynobj)->relgot;
15543 && s->output_section != bfd_abs_section_ptr
15544 && !bfd_set_section_contents (output_bfd, s->output_section,
15545 s->contents, s->output_offset,
15553 #include "elf64-target.h"
15555 /* FreeBSD support */
15557 #undef TARGET_LITTLE_SYM
15558 #undef TARGET_LITTLE_NAME
15560 #undef TARGET_BIG_SYM
15561 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
15562 #undef TARGET_BIG_NAME
15563 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15566 #define ELF_OSABI ELFOSABI_FREEBSD
15569 #define elf64_bed elf64_powerpc_fbsd_bed
15571 #include "elf64-target.h"