1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2019 Free Software Foundation, Inc.
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
5 Largely rewritten by Alan Modra.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
24 /* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
34 #include "elf/ppc64.h"
35 #include "elf64-ppc.h"
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_prefix_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58 static bfd_vma opd_entry_value
59 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
61 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
62 #define TARGET_LITTLE_NAME "elf64-powerpcle"
63 #define TARGET_BIG_SYM powerpc_elf64_vec
64 #define TARGET_BIG_NAME "elf64-powerpc"
65 #define ELF_ARCH bfd_arch_powerpc
66 #define ELF_TARGET_ID PPC64_ELF_DATA
67 #define ELF_MACHINE_CODE EM_PPC64
68 #define ELF_MAXPAGESIZE 0x10000
69 #define ELF_COMMONPAGESIZE 0x1000
70 #define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
71 #define elf_info_to_howto ppc64_elf_info_to_howto
73 #define elf_backend_want_got_sym 0
74 #define elf_backend_want_plt_sym 0
75 #define elf_backend_plt_alignment 3
76 #define elf_backend_plt_not_loaded 1
77 #define elf_backend_got_header_size 8
78 #define elf_backend_want_dynrelro 1
79 #define elf_backend_can_gc_sections 1
80 #define elf_backend_can_refcount 1
81 #define elf_backend_rela_normal 1
82 #define elf_backend_dtrel_excludes_plt 1
83 #define elf_backend_default_execstack 0
85 #define bfd_elf64_mkobject ppc64_elf_mkobject
86 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
87 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
88 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
89 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
90 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
91 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
92 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
93 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
94 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
96 #define elf_backend_object_p ppc64_elf_object_p
97 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
98 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
99 #define elf_backend_write_core_note ppc64_elf_write_core_note
100 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
101 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
102 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
103 #define elf_backend_check_directives ppc64_elf_before_check_relocs
104 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
105 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
106 #define elf_backend_check_relocs ppc64_elf_check_relocs
107 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
108 #define elf_backend_gc_keep ppc64_elf_gc_keep
109 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
110 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
111 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
112 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
113 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
114 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
115 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
116 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
117 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
118 #define elf_backend_action_discarded ppc64_elf_action_discarded
119 #define elf_backend_relocate_section ppc64_elf_relocate_section
120 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
121 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
122 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
123 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
124 #define elf_backend_special_sections ppc64_elf_special_sections
125 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
126 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
127 #define elf_backend_get_reloc_section bfd_get_section_by_name
129 /* The name of the dynamic interpreter. This is put in the .interp
131 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
133 /* The size in bytes of an entry in the procedure linkage table. */
134 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
135 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
137 /* The initial size of the plt reserved for the dynamic linker. */
138 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
140 /* Offsets to some stack save slots. */
142 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
143 /* This one is dodgy. ELFv2 does not have a linker word, so use the
144 CR save slot. Used only by optimised __tls_get_addr call stub,
145 relying on __tls_get_addr_opt not saving CR.. */
146 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
148 /* TOC base pointers offset from start of TOC. */
149 #define TOC_BASE_OFF 0x8000
150 /* TOC base alignment. */
151 #define TOC_BASE_ALIGN 256
153 /* Offset of tp and dtp pointers from start of TLS block. */
154 #define TP_OFFSET 0x7000
155 #define DTP_OFFSET 0x8000
157 /* .plt call stub instructions. The normal stub is like this, but
158 sometimes the .plt entry crosses a 64k boundary and we need to
159 insert an addi to adjust r11. */
160 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
161 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
162 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
163 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
164 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
165 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
166 #define BCTR 0x4e800420 /* bctr */
168 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
169 #define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
170 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
171 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
172 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
174 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
175 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
176 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
177 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
178 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
179 #define BNECTR 0x4ca20420 /* bnectr+ */
180 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
182 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
183 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
184 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
186 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
187 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
188 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
190 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
191 #define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
192 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
193 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
194 #define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
195 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
196 #define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
197 #define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
198 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
199 #define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
200 #define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
201 #define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
202 #define PNOP 0x0700000000000000ULL
204 /* __glink_PLTresolve stub instructions. We enter with the index in R0. */
205 #define GLINK_PLTRESOLVE_SIZE(htab) \
206 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
210 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
211 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
213 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
214 /* ld %2,(0b-1b)(%11) */
215 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
216 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
222 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
223 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
224 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
225 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
226 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
229 #define NOP 0x60000000
231 /* Some other nops. */
232 #define CROR_151515 0x4def7b82
233 #define CROR_313131 0x4ffffb82
235 /* .glink entries for the first 32k functions are two instructions. */
236 #define LI_R0_0 0x38000000 /* li %r0,0 */
237 #define B_DOT 0x48000000 /* b . */
239 /* After that, we need two instructions to load the index, followed by
241 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
242 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
244 /* Instructions used by the save and restore reg functions. */
245 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
246 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
247 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
248 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
249 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
250 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
251 #define LI_R12_0 0x39800000 /* li %r12,0 */
252 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
253 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
254 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
255 #define BLR 0x4e800020 /* blr */
257 /* Since .opd is an array of descriptors and each entry will end up
258 with identical R_PPC64_RELATIVE relocs, there is really no need to
259 propagate .opd relocs; The dynamic linker should be taught to
260 relocate .opd without reloc entries. */
261 #ifndef NO_OPD_RELOCS
262 #define NO_OPD_RELOCS 0
266 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
270 abiversion (bfd *abfd)
272 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
276 set_abiversion (bfd *abfd, int ver)
278 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
279 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
282 /* Relocation HOWTO's. */
283 /* Like other ELF RELA targets that don't apply multiple
284 field-altering relocations to the same localation, src_mask is
285 always zero and pcrel_offset is the same as pc_relative.
286 PowerPC can always use a zero bitpos, even when the field is not at
287 the LSB. For example, a REL24 could use rightshift=2, bisize=24
288 and bitpos=2 which matches the ABI description, or as we do here,
289 rightshift=0, bitsize=26 and bitpos=0. */
290 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
291 complain, special_func) \
292 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
293 complain_overflow_ ## complain, special_func, \
294 #type, FALSE, 0, mask, pc_relative)
296 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
298 static reloc_howto_type ppc64_elf_howto_raw[] =
300 /* This reloc does nothing. */
301 HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
302 bfd_elf_generic_reloc),
304 /* A standard 32 bit relocation. */
305 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
306 bfd_elf_generic_reloc),
308 /* An absolute 26 bit branch; the lower two bits must be zero.
309 FIXME: we don't check that, we just clear them. */
310 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
311 bfd_elf_generic_reloc),
313 /* A standard 16 bit relocation. */
314 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
315 bfd_elf_generic_reloc),
317 /* A 16 bit relocation without overflow. */
318 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
319 bfd_elf_generic_reloc),
321 /* Bits 16-31 of an address. */
322 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
323 bfd_elf_generic_reloc),
325 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
326 bits, treated as a signed number, is negative. */
327 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
330 /* An absolute 16 bit branch; the lower two bits must be zero.
331 FIXME: we don't check that, we just clear them. */
332 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
333 ppc64_elf_branch_reloc),
335 /* An absolute 16 bit branch, for which bit 10 should be set to
336 indicate that the branch is expected to be taken. The lower two
337 bits must be zero. */
338 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
339 ppc64_elf_brtaken_reloc),
341 /* An absolute 16 bit branch, for which bit 10 should be set to
342 indicate that the branch is not expected to be taken. The lower
343 two bits must be zero. */
344 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
345 ppc64_elf_brtaken_reloc),
347 /* A relative 26 bit branch; the lower two bits must be zero. */
348 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
349 ppc64_elf_branch_reloc),
351 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
352 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
353 ppc64_elf_branch_reloc),
355 /* A relative 16 bit branch; the lower two bits must be zero. */
356 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
357 ppc64_elf_branch_reloc),
359 /* A relative 16 bit branch. Bit 10 should be set to indicate that
360 the branch is expected to be taken. The lower two bits must be
362 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
363 ppc64_elf_brtaken_reloc),
365 /* A relative 16 bit branch. Bit 10 should be set to indicate that
366 the branch is not expected to be taken. The lower two bits must
368 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
369 ppc64_elf_brtaken_reloc),
371 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
373 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
374 ppc64_elf_unhandled_reloc),
376 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
378 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
379 ppc64_elf_unhandled_reloc),
381 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
383 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
384 ppc64_elf_unhandled_reloc),
386 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
388 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
389 ppc64_elf_unhandled_reloc),
391 /* This is used only by the dynamic linker. The symbol should exist
392 both in the object being run and in some shared library. The
393 dynamic linker copies the data addressed by the symbol from the
394 shared library into the object, because the object being
395 run has to have the data at some particular address. */
396 HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
397 ppc64_elf_unhandled_reloc),
399 /* Like R_PPC64_ADDR64, but used when setting global offset table
401 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
402 ppc64_elf_unhandled_reloc),
404 /* Created by the link editor. Marks a procedure linkage table
405 entry for a symbol. */
406 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
407 ppc64_elf_unhandled_reloc),
409 /* Used only by the dynamic linker. When the object is run, this
410 doubleword64 is set to the load address of the object, plus the
412 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
413 bfd_elf_generic_reloc),
415 /* Like R_PPC64_ADDR32, but may be unaligned. */
416 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
417 bfd_elf_generic_reloc),
419 /* Like R_PPC64_ADDR16, but may be unaligned. */
420 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
421 bfd_elf_generic_reloc),
423 /* 32-bit PC relative. */
424 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
425 bfd_elf_generic_reloc),
427 /* 32-bit relocation to the symbol's procedure linkage table. */
428 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
429 ppc64_elf_unhandled_reloc),
431 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
432 FIXME: R_PPC64_PLTREL32 not supported. */
433 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
434 ppc64_elf_unhandled_reloc),
436 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
438 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
439 ppc64_elf_unhandled_reloc),
441 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
443 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
444 ppc64_elf_unhandled_reloc),
446 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
448 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
449 ppc64_elf_unhandled_reloc),
451 /* 16-bit section relative relocation. */
452 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
453 ppc64_elf_sectoff_reloc),
455 /* Like R_PPC64_SECTOFF, but no overflow warning. */
456 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
457 ppc64_elf_sectoff_reloc),
459 /* 16-bit upper half section relative relocation. */
460 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
461 ppc64_elf_sectoff_reloc),
463 /* 16-bit upper half adjusted section relative relocation. */
464 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
465 ppc64_elf_sectoff_ha_reloc),
467 /* Like R_PPC64_REL24 without touching the two least significant bits. */
468 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
469 bfd_elf_generic_reloc),
471 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
473 /* A standard 64-bit relocation. */
474 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
475 bfd_elf_generic_reloc),
477 /* The bits 32-47 of an address. */
478 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
479 bfd_elf_generic_reloc),
481 /* The bits 32-47 of an address, plus 1 if the contents of the low
482 16 bits, treated as a signed number, is negative. */
483 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
486 /* The bits 48-63 of an address. */
487 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
488 bfd_elf_generic_reloc),
490 /* The bits 48-63 of an address, plus 1 if the contents of the low
491 16 bits, treated as a signed number, is negative. */
492 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
495 /* Like ADDR64, but may be unaligned. */
496 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
497 bfd_elf_generic_reloc),
499 /* 64-bit relative relocation. */
500 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
501 bfd_elf_generic_reloc),
503 /* 64-bit relocation to the symbol's procedure linkage table. */
504 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
505 ppc64_elf_unhandled_reloc),
507 /* 64-bit PC relative relocation to the symbol's procedure linkage
509 /* FIXME: R_PPC64_PLTREL64 not supported. */
510 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
511 ppc64_elf_unhandled_reloc),
513 /* 16 bit TOC-relative relocation. */
514 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
515 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
516 ppc64_elf_toc_reloc),
518 /* 16 bit TOC-relative relocation without overflow. */
519 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
520 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
521 ppc64_elf_toc_reloc),
523 /* 16 bit TOC-relative relocation, high 16 bits. */
524 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
525 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
526 ppc64_elf_toc_reloc),
528 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
529 contents of the low 16 bits, treated as a signed number, is
531 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
532 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
533 ppc64_elf_toc_ha_reloc),
535 /* 64-bit relocation; insert value of TOC base (.TOC.). */
536 /* R_PPC64_TOC 51 doubleword64 .TOC. */
537 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
538 ppc64_elf_toc64_reloc),
540 /* Like R_PPC64_GOT16, but also informs the link editor that the
541 value to relocate may (!) refer to a PLT entry which the link
542 editor (a) may replace with the symbol value. If the link editor
543 is unable to fully resolve the symbol, it may (b) create a PLT
544 entry and store the address to the new PLT entry in the GOT.
545 This permits lazy resolution of function symbols at run time.
546 The link editor may also skip all of this and just (c) emit a
547 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
548 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
549 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
550 ppc64_elf_unhandled_reloc),
552 /* Like R_PPC64_PLTGOT16, but without overflow. */
553 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
554 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
555 ppc64_elf_unhandled_reloc),
557 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
558 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
559 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
560 ppc64_elf_unhandled_reloc),
562 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
563 1 if the contents of the low 16 bits, treated as a signed number,
565 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
566 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
567 ppc64_elf_unhandled_reloc),
569 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
570 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
571 bfd_elf_generic_reloc),
573 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
574 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
575 bfd_elf_generic_reloc),
577 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
578 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
579 ppc64_elf_unhandled_reloc),
581 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
582 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
583 ppc64_elf_unhandled_reloc),
585 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
586 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
587 ppc64_elf_unhandled_reloc),
589 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
590 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
591 ppc64_elf_sectoff_reloc),
593 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
594 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
595 ppc64_elf_sectoff_reloc),
597 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
598 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
599 ppc64_elf_toc_reloc),
601 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
602 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
603 ppc64_elf_toc_reloc),
605 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
606 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
607 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
608 ppc64_elf_unhandled_reloc),
610 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
611 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
612 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
613 ppc64_elf_unhandled_reloc),
615 /* Marker relocs for TLS. */
616 HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
617 bfd_elf_generic_reloc),
619 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
620 bfd_elf_generic_reloc),
622 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
623 bfd_elf_generic_reloc),
625 /* Marker reloc for optimizing r2 save in prologue rather than on
626 each plt call stub. */
627 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
628 bfd_elf_generic_reloc),
630 /* Marker relocs on inline plt call instructions. */
631 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
632 bfd_elf_generic_reloc),
634 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
635 bfd_elf_generic_reloc),
637 /* Computes the load module index of the load module that contains the
638 definition of its TLS sym. */
639 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
640 ppc64_elf_unhandled_reloc),
642 /* Computes a dtv-relative displacement, the difference between the value
643 of sym+add and the base address of the thread-local storage block that
644 contains the definition of sym, minus 0x8000. */
645 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
646 ppc64_elf_unhandled_reloc),
648 /* A 16 bit dtprel reloc. */
649 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
650 ppc64_elf_unhandled_reloc),
652 /* Like DTPREL16, but no overflow. */
653 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
654 ppc64_elf_unhandled_reloc),
656 /* Like DTPREL16_LO, but next higher group of 16 bits. */
657 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
658 ppc64_elf_unhandled_reloc),
660 /* Like DTPREL16_HI, but adjust for low 16 bits. */
661 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
662 ppc64_elf_unhandled_reloc),
664 /* Like DTPREL16_HI, but next higher group of 16 bits. */
665 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
666 ppc64_elf_unhandled_reloc),
668 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
669 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
670 ppc64_elf_unhandled_reloc),
672 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
673 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
674 ppc64_elf_unhandled_reloc),
676 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
677 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
678 ppc64_elf_unhandled_reloc),
680 /* Like DTPREL16, but for insns with a DS field. */
681 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
682 ppc64_elf_unhandled_reloc),
684 /* Like DTPREL16_DS, but no overflow. */
685 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
686 ppc64_elf_unhandled_reloc),
688 /* Computes a tp-relative displacement, the difference between the value of
689 sym+add and the value of the thread pointer (r13). */
690 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
691 ppc64_elf_unhandled_reloc),
693 /* A 16 bit tprel reloc. */
694 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
695 ppc64_elf_unhandled_reloc),
697 /* Like TPREL16, but no overflow. */
698 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
699 ppc64_elf_unhandled_reloc),
701 /* Like TPREL16_LO, but next higher group of 16 bits. */
702 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
703 ppc64_elf_unhandled_reloc),
705 /* Like TPREL16_HI, but adjust for low 16 bits. */
706 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
707 ppc64_elf_unhandled_reloc),
709 /* Like TPREL16_HI, but next higher group of 16 bits. */
710 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
711 ppc64_elf_unhandled_reloc),
713 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
714 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
715 ppc64_elf_unhandled_reloc),
717 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
718 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
719 ppc64_elf_unhandled_reloc),
721 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
722 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
723 ppc64_elf_unhandled_reloc),
725 /* Like TPREL16, but for insns with a DS field. */
726 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
727 ppc64_elf_unhandled_reloc),
729 /* Like TPREL16_DS, but no overflow. */
730 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
731 ppc64_elf_unhandled_reloc),
733 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
734 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
735 to the first entry relative to the TOC base (r2). */
736 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
737 ppc64_elf_unhandled_reloc),
739 /* Like GOT_TLSGD16, but no overflow. */
740 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
741 ppc64_elf_unhandled_reloc),
743 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
744 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
745 ppc64_elf_unhandled_reloc),
747 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
748 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
749 ppc64_elf_unhandled_reloc),
751 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
752 with values (sym+add)@dtpmod and zero, and computes the offset to the
753 first entry relative to the TOC base (r2). */
754 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
755 ppc64_elf_unhandled_reloc),
757 /* Like GOT_TLSLD16, but no overflow. */
758 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
759 ppc64_elf_unhandled_reloc),
761 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
762 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
763 ppc64_elf_unhandled_reloc),
765 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
766 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
767 ppc64_elf_unhandled_reloc),
769 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
770 the offset to the entry relative to the TOC base (r2). */
771 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
772 ppc64_elf_unhandled_reloc),
774 /* Like GOT_DTPREL16_DS, but no overflow. */
775 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
776 ppc64_elf_unhandled_reloc),
778 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
779 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
780 ppc64_elf_unhandled_reloc),
782 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
783 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
784 ppc64_elf_unhandled_reloc),
786 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
787 offset to the entry relative to the TOC base (r2). */
788 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
789 ppc64_elf_unhandled_reloc),
791 /* Like GOT_TPREL16_DS, but no overflow. */
792 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
793 ppc64_elf_unhandled_reloc),
795 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
796 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
797 ppc64_elf_unhandled_reloc),
799 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
800 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
801 ppc64_elf_unhandled_reloc),
803 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
804 ppc64_elf_unhandled_reloc),
806 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
807 bfd_elf_generic_reloc),
809 /* A 16 bit relative relocation. */
810 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
811 bfd_elf_generic_reloc),
813 /* A 16 bit relative relocation without overflow. */
814 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
815 bfd_elf_generic_reloc),
817 /* The high order 16 bits of a relative address. */
818 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
819 bfd_elf_generic_reloc),
821 /* The high order 16 bits of a relative address, plus 1 if the contents of
822 the low 16 bits, treated as a signed number, is negative. */
823 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
826 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
827 bfd_elf_generic_reloc),
829 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
832 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
833 bfd_elf_generic_reloc),
835 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
838 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
839 bfd_elf_generic_reloc),
841 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
844 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
845 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
848 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
849 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
852 /* Like R_PPC64_ADDR16_HI, but no overflow. */
853 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
854 bfd_elf_generic_reloc),
856 /* Like R_PPC64_ADDR16_HA, but no overflow. */
857 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
860 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
861 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
862 ppc64_elf_unhandled_reloc),
864 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
865 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
866 ppc64_elf_unhandled_reloc),
868 /* Like R_PPC64_TPREL16_HI, but no overflow. */
869 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
870 ppc64_elf_unhandled_reloc),
872 /* Like R_PPC64_TPREL16_HA, but no overflow. */
873 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
874 ppc64_elf_unhandled_reloc),
876 /* Marker reloc on ELFv2 large-model function entry. */
877 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
878 bfd_elf_generic_reloc),
880 /* Like ADDR64, but use local entry point of function. */
881 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
882 bfd_elf_generic_reloc),
884 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, FALSE, dont,
885 bfd_elf_generic_reloc),
887 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, FALSE, dont,
888 bfd_elf_generic_reloc),
890 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, FALSE, dont,
891 bfd_elf_generic_reloc),
893 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
894 ppc64_elf_prefix_reloc),
896 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, dont,
897 ppc64_elf_prefix_reloc),
899 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
900 ppc64_elf_prefix_reloc),
902 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
903 ppc64_elf_prefix_reloc),
905 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
906 ppc64_elf_prefix_reloc),
908 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
909 ppc64_elf_unhandled_reloc),
911 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
912 ppc64_elf_unhandled_reloc),
914 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
915 ppc64_elf_unhandled_reloc),
917 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, FALSE, dont,
918 bfd_elf_generic_reloc),
920 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, FALSE, dont,
923 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, FALSE, dont,
924 bfd_elf_generic_reloc),
926 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, FALSE, dont,
929 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, TRUE, dont,
930 bfd_elf_generic_reloc),
932 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, TRUE, dont,
935 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, TRUE, dont,
936 bfd_elf_generic_reloc),
938 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, TRUE, dont,
941 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, FALSE, signed,
942 ppc64_elf_prefix_reloc),
944 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, TRUE, signed,
945 ppc64_elf_prefix_reloc),
947 /* GNU extension to record C++ vtable hierarchy. */
948 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
951 /* GNU extension to record C++ vtable member usage. */
952 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
957 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
961 ppc_howto_init (void)
963 unsigned int i, type;
965 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
967 type = ppc64_elf_howto_raw[i].type;
968 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
969 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
973 static reloc_howto_type *
974 ppc64_elf_reloc_type_lookup (bfd *abfd,
975 bfd_reloc_code_real_type code)
977 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
979 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
980 /* Initialize howto table if needed. */
986 /* xgettext:c-format */
987 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
989 bfd_set_error (bfd_error_bad_value);
992 case BFD_RELOC_NONE: r = R_PPC64_NONE;
994 case BFD_RELOC_32: r = R_PPC64_ADDR32;
996 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
998 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1000 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1002 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1004 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1006 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1008 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1010 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1012 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1014 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1016 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1018 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1020 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1022 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1024 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1026 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1028 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1030 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1032 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1034 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1036 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1038 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1040 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1042 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1044 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1046 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1048 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1050 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1052 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1054 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1056 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1058 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1060 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1062 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1064 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1066 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1068 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1070 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1072 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1074 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1076 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1078 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1080 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1082 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1084 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1086 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1088 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1090 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1092 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1094 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1096 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1098 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1100 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1102 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1104 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1106 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1108 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1110 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1112 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1114 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1116 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1118 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1120 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1122 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1124 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1126 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1128 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1130 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1132 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1134 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1136 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1138 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1140 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1142 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1144 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1146 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1148 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1150 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1152 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1154 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1156 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1158 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1160 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1162 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1164 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1166 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1168 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1170 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1172 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1174 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1176 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1178 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1180 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1182 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1184 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1186 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1188 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1190 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1192 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1194 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1196 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1198 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1200 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1202 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1204 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1206 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1208 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1210 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1212 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1214 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1216 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1218 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1220 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1222 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1224 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1226 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1228 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1230 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1232 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1234 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1236 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1238 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1240 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1242 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1244 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1246 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1248 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1250 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1252 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1254 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1256 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1258 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1260 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1262 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1264 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1266 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1268 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1270 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1272 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1276 return ppc64_elf_howto_table[r];
1279 static reloc_howto_type *
1280 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1285 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1286 if (ppc64_elf_howto_raw[i].name != NULL
1287 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1288 return &ppc64_elf_howto_raw[i];
1293 /* Set the howto pointer for a PowerPC ELF reloc. */
1296 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1297 Elf_Internal_Rela *dst)
1301 /* Initialize howto table if needed. */
1302 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1305 type = ELF64_R_TYPE (dst->r_info);
1306 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1308 /* xgettext:c-format */
1309 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1311 bfd_set_error (bfd_error_bad_value);
1314 cache_ptr->howto = ppc64_elf_howto_table[type];
1315 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1317 /* xgettext:c-format */
1318 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1320 bfd_set_error (bfd_error_bad_value);
1327 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1329 static bfd_reloc_status_type
1330 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1331 void *data, asection *input_section,
1332 bfd *output_bfd, char **error_message)
1334 enum elf_ppc64_reloc_type r_type;
1336 bfd_size_type octets;
1339 /* If this is a relocatable link (output_bfd test tells us), just
1340 call the generic function. Any adjustment will be done at final
1342 if (output_bfd != NULL)
1343 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1344 input_section, output_bfd, error_message);
1346 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1347 We won't actually be using the low bits, so trashing them
1349 r_type = reloc_entry->howto->type;
1350 if (r_type == R_PPC64_ADDR16_HIGHERA34
1351 || r_type == R_PPC64_ADDR16_HIGHESTA34
1352 || r_type == R_PPC64_REL16_HIGHERA34
1353 || r_type == R_PPC64_REL16_HIGHESTA34)
1354 reloc_entry->addend += 1ULL << 33;
1356 reloc_entry->addend += 1U << 15;
1357 if (r_type != R_PPC64_REL16DX_HA)
1358 return bfd_reloc_continue;
1361 if (!bfd_is_com_section (symbol->section))
1362 value = symbol->value;
1363 value += (reloc_entry->addend
1364 + symbol->section->output_offset
1365 + symbol->section->output_section->vma);
1366 value -= (reloc_entry->address
1367 + input_section->output_offset
1368 + input_section->output_section->vma);
1369 value = (bfd_signed_vma) value >> 16;
1371 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1372 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1374 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1375 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1376 if (value + 0x8000 > 0xffff)
1377 return bfd_reloc_overflow;
1378 return bfd_reloc_ok;
1381 static bfd_reloc_status_type
1382 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1383 void *data, asection *input_section,
1384 bfd *output_bfd, char **error_message)
1386 if (output_bfd != NULL)
1387 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1388 input_section, output_bfd, error_message);
1390 if (strcmp (symbol->section->name, ".opd") == 0
1391 && (symbol->section->owner->flags & DYNAMIC) == 0)
1393 bfd_vma dest = opd_entry_value (symbol->section,
1394 symbol->value + reloc_entry->addend,
1396 if (dest != (bfd_vma) -1)
1397 reloc_entry->addend = dest - (symbol->value
1398 + symbol->section->output_section->vma
1399 + symbol->section->output_offset);
1403 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1405 if (symbol->section->owner != abfd
1406 && symbol->section->owner != NULL
1407 && abiversion (symbol->section->owner) >= 2)
1411 for (i = 0; i < symbol->section->owner->symcount; ++i)
1413 asymbol *symdef = symbol->section->owner->outsymbols[i];
1415 if (strcmp (symdef->name, symbol->name) == 0)
1417 elfsym = (elf_symbol_type *) symdef;
1423 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1425 return bfd_reloc_continue;
1428 static bfd_reloc_status_type
1429 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1430 void *data, asection *input_section,
1431 bfd *output_bfd, char **error_message)
1434 enum elf_ppc64_reloc_type r_type;
1435 bfd_size_type octets;
1436 /* Assume 'at' branch hints. */
1437 bfd_boolean is_isa_v2 = TRUE;
1439 /* If this is a relocatable link (output_bfd test tells us), just
1440 call the generic function. Any adjustment will be done at final
1442 if (output_bfd != NULL)
1443 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1444 input_section, output_bfd, error_message);
1446 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1447 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1448 insn &= ~(0x01 << 21);
1449 r_type = reloc_entry->howto->type;
1450 if (r_type == R_PPC64_ADDR14_BRTAKEN
1451 || r_type == R_PPC64_REL14_BRTAKEN)
1452 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1456 /* Set 'a' bit. This is 0b00010 in BO field for branch
1457 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1458 for branch on CTR insns (BO == 1a00t or 1a01t). */
1459 if ((insn & (0x14 << 21)) == (0x04 << 21))
1461 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1471 if (!bfd_is_com_section (symbol->section))
1472 target = symbol->value;
1473 target += symbol->section->output_section->vma;
1474 target += symbol->section->output_offset;
1475 target += reloc_entry->addend;
1477 from = (reloc_entry->address
1478 + input_section->output_offset
1479 + input_section->output_section->vma);
1481 /* Invert 'y' bit if not the default. */
1482 if ((bfd_signed_vma) (target - from) < 0)
1485 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1487 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1488 input_section, output_bfd, error_message);
1491 static bfd_reloc_status_type
1492 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1493 void *data, asection *input_section,
1494 bfd *output_bfd, char **error_message)
1496 /* If this is a relocatable link (output_bfd test tells us), just
1497 call the generic function. Any adjustment will be done at final
1499 if (output_bfd != NULL)
1500 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1501 input_section, output_bfd, error_message);
1503 /* Subtract the symbol section base address. */
1504 reloc_entry->addend -= symbol->section->output_section->vma;
1505 return bfd_reloc_continue;
1508 static bfd_reloc_status_type
1509 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1510 void *data, asection *input_section,
1511 bfd *output_bfd, char **error_message)
1513 /* If this is a relocatable link (output_bfd test tells us), just
1514 call the generic function. Any adjustment will be done at final
1516 if (output_bfd != NULL)
1517 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1518 input_section, output_bfd, error_message);
1520 /* Subtract the symbol section base address. */
1521 reloc_entry->addend -= symbol->section->output_section->vma;
1523 /* Adjust the addend for sign extension of the low 16 bits. */
1524 reloc_entry->addend += 0x8000;
1525 return bfd_reloc_continue;
1528 static bfd_reloc_status_type
1529 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1530 void *data, asection *input_section,
1531 bfd *output_bfd, char **error_message)
1535 /* If this is a relocatable link (output_bfd test tells us), just
1536 call the generic function. Any adjustment will be done at final
1538 if (output_bfd != NULL)
1539 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1540 input_section, output_bfd, error_message);
1542 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1544 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1546 /* Subtract the TOC base address. */
1547 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1548 return bfd_reloc_continue;
1551 static bfd_reloc_status_type
1552 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1553 void *data, asection *input_section,
1554 bfd *output_bfd, char **error_message)
1558 /* If this is a relocatable link (output_bfd test tells us), just
1559 call the generic function. Any adjustment will be done at final
1561 if (output_bfd != NULL)
1562 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1563 input_section, output_bfd, error_message);
1565 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1567 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1569 /* Subtract the TOC base address. */
1570 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1572 /* Adjust the addend for sign extension of the low 16 bits. */
1573 reloc_entry->addend += 0x8000;
1574 return bfd_reloc_continue;
1577 static bfd_reloc_status_type
1578 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1579 void *data, asection *input_section,
1580 bfd *output_bfd, char **error_message)
1583 bfd_size_type octets;
1585 /* If this is a relocatable link (output_bfd test tells us), just
1586 call the generic function. Any adjustment will be done at final
1588 if (output_bfd != NULL)
1589 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1590 input_section, output_bfd, error_message);
1592 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1594 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1596 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1597 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1598 return bfd_reloc_ok;
1601 static bfd_reloc_status_type
1602 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1603 void *data, asection *input_section,
1604 bfd *output_bfd, char **error_message)
1609 if (output_bfd != NULL)
1610 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1611 input_section, output_bfd, error_message);
1613 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1615 insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
1617 targ = (symbol->section->output_section->vma
1618 + symbol->section->output_offset
1619 + reloc_entry->addend);
1620 if (!bfd_is_com_section (symbol->section))
1621 targ += symbol->value;
1622 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1624 if (reloc_entry->howto->pc_relative)
1626 bfd_vma from = (reloc_entry->address
1627 + input_section->output_offset
1628 + input_section->output_section->vma);
1631 targ >>= reloc_entry->howto->rightshift;
1632 insn &= ~reloc_entry->howto->dst_mask;
1633 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1634 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
1635 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
1636 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1637 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1638 >= 1ULL << reloc_entry->howto->bitsize))
1639 return bfd_reloc_overflow;
1640 return bfd_reloc_ok;
1643 static bfd_reloc_status_type
1644 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1645 void *data, asection *input_section,
1646 bfd *output_bfd, char **error_message)
1648 /* If this is a relocatable link (output_bfd test tells us), just
1649 call the generic function. Any adjustment will be done at final
1651 if (output_bfd != NULL)
1652 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1653 input_section, output_bfd, error_message);
1655 if (error_message != NULL)
1657 static char buf[60];
1658 sprintf (buf, "generic linker can't handle %s",
1659 reloc_entry->howto->name);
1660 *error_message = buf;
1662 return bfd_reloc_dangerous;
1665 /* Track GOT entries needed for a given symbol. We might need more
1666 than one got entry per symbol. */
1669 struct got_entry *next;
1671 /* The symbol addend that we'll be placing in the GOT. */
1674 /* Unlike other ELF targets, we use separate GOT entries for the same
1675 symbol referenced from different input files. This is to support
1676 automatic multiple TOC/GOT sections, where the TOC base can vary
1677 from one input file to another. After partitioning into TOC groups
1678 we merge entries within the group.
1680 Point to the BFD owning this GOT entry. */
1683 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1684 TLS_TPREL or TLS_DTPREL for tls entries. */
1685 unsigned char tls_type;
1687 /* Non-zero if got.ent points to real entry. */
1688 unsigned char is_indirect;
1690 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1693 bfd_signed_vma refcount;
1695 struct got_entry *ent;
1699 /* The same for PLT. */
1702 struct plt_entry *next;
1708 bfd_signed_vma refcount;
1713 struct ppc64_elf_obj_tdata
1715 struct elf_obj_tdata elf;
1717 /* Shortcuts to dynamic linker sections. */
1721 /* Used during garbage collection. We attach global symbols defined
1722 on removed .opd entries to this section so that the sym is removed. */
1723 asection *deleted_section;
1725 /* TLS local dynamic got entry handling. Support for multiple GOT
1726 sections means we potentially need one of these for each input bfd. */
1727 struct got_entry tlsld_got;
1731 /* A copy of relocs before they are modified for --emit-relocs. */
1732 Elf_Internal_Rela *relocs;
1734 /* Section contents. */
1738 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1739 the reloc to be in the range -32768 to 32767. */
1740 unsigned int has_small_toc_reloc : 1;
1742 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1743 instruction not one we handle. */
1744 unsigned int unexpected_toc_insn : 1;
1746 /* Set if got relocs that can be optimised are present in this file. */
1747 unsigned int has_gotrel : 1;
1750 #define ppc64_elf_tdata(bfd) \
1751 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1753 #define ppc64_tlsld_got(bfd) \
1754 (&ppc64_elf_tdata (bfd)->tlsld_got)
1756 #define is_ppc64_elf(bfd) \
1757 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1758 && elf_object_id (bfd) == PPC64_ELF_DATA)
1760 /* Override the generic function because we store some extras. */
1763 ppc64_elf_mkobject (bfd *abfd)
1765 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1769 /* Fix bad default arch selected for a 64 bit input bfd when the
1770 default is 32 bit. Also select arch based on apuinfo. */
1773 ppc64_elf_object_p (bfd *abfd)
1775 if (!abfd->arch_info->the_default)
1778 if (abfd->arch_info->bits_per_word == 32)
1780 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1782 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1784 /* Relies on arch after 32 bit default being 64 bit default. */
1785 abfd->arch_info = abfd->arch_info->next;
1786 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1789 return _bfd_elf_ppc_set_arch (abfd);
1792 /* Support for core dump NOTE sections. */
1795 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1797 size_t offset, size;
1799 if (note->descsz != 504)
1803 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1806 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1812 /* Make a ".reg/999" section. */
1813 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1814 size, note->descpos + offset);
1818 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1820 if (note->descsz != 136)
1823 elf_tdata (abfd)->core->pid
1824 = bfd_get_32 (abfd, note->descdata + 24);
1825 elf_tdata (abfd)->core->program
1826 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1827 elf_tdata (abfd)->core->command
1828 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1834 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1844 char data[136] ATTRIBUTE_NONSTRING;
1847 va_start (ap, note_type);
1848 memset (data, 0, sizeof (data));
1849 strncpy (data + 40, va_arg (ap, const char *), 16);
1850 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1852 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1853 -Wstringop-truncation:
1854 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1856 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1858 strncpy (data + 56, va_arg (ap, const char *), 80);
1859 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1863 return elfcore_write_note (abfd, buf, bufsiz,
1864 "CORE", note_type, data, sizeof (data));
1875 va_start (ap, note_type);
1876 memset (data, 0, 112);
1877 pid = va_arg (ap, long);
1878 bfd_put_32 (abfd, pid, data + 32);
1879 cursig = va_arg (ap, int);
1880 bfd_put_16 (abfd, cursig, data + 12);
1881 greg = va_arg (ap, const void *);
1882 memcpy (data + 112, greg, 384);
1883 memset (data + 496, 0, 8);
1885 return elfcore_write_note (abfd, buf, bufsiz,
1886 "CORE", note_type, data, sizeof (data));
1891 /* Add extra PPC sections. */
1893 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1895 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1896 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1897 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1898 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1899 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1900 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1901 { NULL, 0, 0, 0, 0 }
1904 enum _ppc64_sec_type {
1910 struct _ppc64_elf_section_data
1912 struct bfd_elf_section_data elf;
1916 /* An array with one entry for each opd function descriptor,
1917 and some spares since opd entries may be either 16 or 24 bytes. */
1918 #define OPD_NDX(OFF) ((OFF) >> 4)
1919 struct _opd_sec_data
1921 /* Points to the function code section for local opd entries. */
1922 asection **func_sec;
1924 /* After editing .opd, adjust references to opd local syms. */
1928 /* An array for toc sections, indexed by offset/8. */
1929 struct _toc_sec_data
1931 /* Specifies the relocation symbol index used at a given toc offset. */
1934 /* And the relocation addend. */
1939 enum _ppc64_sec_type sec_type:2;
1941 /* Flag set when small branches are detected. Used to
1942 select suitable defaults for the stub group size. */
1943 unsigned int has_14bit_branch:1;
1945 /* Flag set when PLTCALL relocs are detected. */
1946 unsigned int has_pltcall:1;
1948 /* Flag set when section has GOT relocations that can be optimised. */
1949 unsigned int has_gotrel:1;
1952 #define ppc64_elf_section_data(sec) \
1953 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
1956 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
1958 if (!sec->used_by_bfd)
1960 struct _ppc64_elf_section_data *sdata;
1961 bfd_size_type amt = sizeof (*sdata);
1963 sdata = bfd_zalloc (abfd, amt);
1966 sec->used_by_bfd = sdata;
1969 return _bfd_elf_new_section_hook (abfd, sec);
1972 static struct _opd_sec_data *
1973 get_opd_info (asection * sec)
1976 && ppc64_elf_section_data (sec) != NULL
1977 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
1978 return &ppc64_elf_section_data (sec)->u.opd;
1982 /* Parameters for the qsort hook. */
1983 static bfd_boolean synthetic_relocatable;
1984 static asection *synthetic_opd;
1986 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
1989 compare_symbols (const void *ap, const void *bp)
1991 const asymbol *a = *(const asymbol **) ap;
1992 const asymbol *b = *(const asymbol **) bp;
1994 /* Section symbols first. */
1995 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
1997 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2000 /* then .opd symbols. */
2001 if (synthetic_opd != NULL)
2003 if (strcmp (a->section->name, ".opd") == 0
2004 && strcmp (b->section->name, ".opd") != 0)
2006 if (strcmp (a->section->name, ".opd") != 0
2007 && strcmp (b->section->name, ".opd") == 0)
2011 /* then other code symbols. */
2012 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2013 == (SEC_CODE | SEC_ALLOC))
2014 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2015 != (SEC_CODE | SEC_ALLOC)))
2018 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2019 != (SEC_CODE | SEC_ALLOC))
2020 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2021 == (SEC_CODE | SEC_ALLOC)))
2024 if (synthetic_relocatable)
2026 if (a->section->id < b->section->id)
2029 if (a->section->id > b->section->id)
2033 if (a->value + a->section->vma < b->value + b->section->vma)
2036 if (a->value + a->section->vma > b->value + b->section->vma)
2039 /* For syms with the same value, prefer strong dynamic global function
2040 syms over other syms. */
2041 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2044 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2047 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2050 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2053 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2056 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2059 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2062 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2068 /* Search SYMS for a symbol of the given VALUE. */
2071 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
2075 if (id == (unsigned) -1)
2079 mid = (lo + hi) >> 1;
2080 if (syms[mid]->value + syms[mid]->section->vma < value)
2082 else if (syms[mid]->value + syms[mid]->section->vma > value)
2092 mid = (lo + hi) >> 1;
2093 if (syms[mid]->section->id < id)
2095 else if (syms[mid]->section->id > id)
2097 else if (syms[mid]->value < value)
2099 else if (syms[mid]->value > value)
2109 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2111 bfd_vma vma = *(bfd_vma *) ptr;
2112 return ((section->flags & SEC_ALLOC) != 0
2113 && section->vma <= vma
2114 && vma < section->vma + section->size);
2117 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2118 entry syms. Also generate @plt symbols for the glink branch table.
2119 Returns count of synthetic symbols in RET or -1 on error. */
2122 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2123 long static_count, asymbol **static_syms,
2124 long dyn_count, asymbol **dyn_syms,
2130 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2131 asection *opd = NULL;
2132 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2134 int abi = abiversion (abfd);
2140 opd = bfd_get_section_by_name (abfd, ".opd");
2141 if (opd == NULL && abi == 1)
2153 symcount = static_count;
2155 symcount += dyn_count;
2159 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2163 if (!relocatable && static_count != 0 && dyn_count != 0)
2165 /* Use both symbol tables. */
2166 memcpy (syms, static_syms, static_count * sizeof (*syms));
2167 memcpy (syms + static_count, dyn_syms,
2168 (dyn_count + 1) * sizeof (*syms));
2170 else if (!relocatable && static_count == 0)
2171 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2173 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2175 /* Trim uninteresting symbols. Interesting symbols are section,
2176 function, and notype symbols. */
2177 for (i = 0, j = 0; i < symcount; ++i)
2178 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2179 | BSF_RELC | BSF_SRELC)) == 0)
2180 syms[j++] = syms[i];
2183 synthetic_relocatable = relocatable;
2184 synthetic_opd = opd;
2185 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2187 if (!relocatable && symcount > 1)
2189 /* Trim duplicate syms, since we may have merged the normal
2190 and dynamic symbols. Actually, we only care about syms
2191 that have different values, so trim any with the same
2192 value. Don't consider ifunc and ifunc resolver symbols
2193 duplicates however, because GDB wants to know whether a
2194 text symbol is an ifunc resolver. */
2195 for (i = 1, j = 1; i < symcount; ++i)
2197 const asymbol *s0 = syms[i - 1];
2198 const asymbol *s1 = syms[i];
2200 if ((s0->value + s0->section->vma
2201 != s1->value + s1->section->vma)
2202 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2203 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2204 syms[j++] = syms[i];
2210 /* Note that here and in compare_symbols we can't compare opd and
2211 sym->section directly. With separate debug info files, the
2212 symbols will be extracted from the debug file while abfd passed
2213 to this function is the real binary. */
2214 if (strcmp (syms[i]->section->name, ".opd") == 0)
2218 for (; i < symcount; ++i)
2219 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2220 | SEC_THREAD_LOCAL))
2221 != (SEC_CODE | SEC_ALLOC))
2222 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2226 for (; i < symcount; ++i)
2227 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2231 for (; i < symcount; ++i)
2232 if (strcmp (syms[i]->section->name, ".opd") != 0)
2236 for (; i < symcount; ++i)
2237 if (((syms[i]->section->flags
2238 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2239 != (SEC_CODE | SEC_ALLOC))
2247 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2252 if (opdsymend == secsymend)
2255 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2256 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2260 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2267 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2271 while (r < opd->relocation + relcount
2272 && r->address < syms[i]->value + opd->vma)
2275 if (r == opd->relocation + relcount)
2278 if (r->address != syms[i]->value + opd->vma)
2281 if (r->howto->type != R_PPC64_ADDR64)
2284 sym = *r->sym_ptr_ptr;
2285 if (!sym_exists_at (syms, opdsymend, symcount,
2286 sym->section->id, sym->value + r->addend))
2289 size += sizeof (asymbol);
2290 size += strlen (syms[i]->name) + 2;
2296 s = *ret = bfd_malloc (size);
2303 names = (char *) (s + count);
2305 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2309 while (r < opd->relocation + relcount
2310 && r->address < syms[i]->value + opd->vma)
2313 if (r == opd->relocation + relcount)
2316 if (r->address != syms[i]->value + opd->vma)
2319 if (r->howto->type != R_PPC64_ADDR64)
2322 sym = *r->sym_ptr_ptr;
2323 if (!sym_exists_at (syms, opdsymend, symcount,
2324 sym->section->id, sym->value + r->addend))
2329 s->flags |= BSF_SYNTHETIC;
2330 s->section = sym->section;
2331 s->value = sym->value + r->addend;
2334 len = strlen (syms[i]->name);
2335 memcpy (names, syms[i]->name, len + 1);
2337 /* Have udata.p point back to the original symbol this
2338 synthetic symbol was derived from. */
2339 s->udata.p = syms[i];
2346 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2347 bfd_byte *contents = NULL;
2349 size_t plt_count = 0;
2350 bfd_vma glink_vma = 0, resolv_vma = 0;
2351 asection *dynamic, *glink = NULL, *relplt = NULL;
2354 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2356 free_contents_and_exit_err:
2358 free_contents_and_exit:
2365 for (i = secsymend; i < opdsymend; ++i)
2369 /* Ignore bogus symbols. */
2370 if (syms[i]->value > opd->size - 8)
2373 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2374 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2377 size += sizeof (asymbol);
2378 size += strlen (syms[i]->name) + 2;
2382 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2384 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2386 bfd_byte *dynbuf, *extdyn, *extdynend;
2388 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2390 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2391 goto free_contents_and_exit_err;
2393 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2394 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2397 extdynend = extdyn + dynamic->size;
2398 for (; extdyn < extdynend; extdyn += extdynsize)
2400 Elf_Internal_Dyn dyn;
2401 (*swap_dyn_in) (abfd, extdyn, &dyn);
2403 if (dyn.d_tag == DT_NULL)
2406 if (dyn.d_tag == DT_PPC64_GLINK)
2408 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2409 See comment in ppc64_elf_finish_dynamic_sections. */
2410 glink_vma = dyn.d_un.d_val + 8 * 4;
2411 /* The .glink section usually does not survive the final
2412 link; search for the section (usually .text) where the
2413 glink stubs now reside. */
2414 glink = bfd_sections_find_if (abfd, section_covers_vma,
2425 /* Determine __glink trampoline by reading the relative branch
2426 from the first glink stub. */
2428 unsigned int off = 0;
2430 while (bfd_get_section_contents (abfd, glink, buf,
2431 glink_vma + off - glink->vma, 4))
2433 unsigned int insn = bfd_get_32 (abfd, buf);
2435 if ((insn & ~0x3fffffc) == 0)
2438 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2447 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2449 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2452 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2453 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
2454 goto free_contents_and_exit_err;
2456 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2457 size += plt_count * sizeof (asymbol);
2459 p = relplt->relocation;
2460 for (i = 0; i < plt_count; i++, p++)
2462 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2464 size += sizeof ("+0x") - 1 + 16;
2470 goto free_contents_and_exit;
2471 s = *ret = bfd_malloc (size);
2473 goto free_contents_and_exit_err;
2475 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2477 for (i = secsymend; i < opdsymend; ++i)
2481 if (syms[i]->value > opd->size - 8)
2484 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2485 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2489 asection *sec = abfd->sections;
2496 size_t mid = (lo + hi) >> 1;
2497 if (syms[mid]->section->vma < ent)
2499 else if (syms[mid]->section->vma > ent)
2503 sec = syms[mid]->section;
2508 if (lo >= hi && lo > codesecsym)
2509 sec = syms[lo - 1]->section;
2511 for (; sec != NULL; sec = sec->next)
2515 /* SEC_LOAD may not be set if SEC is from a separate debug
2517 if ((sec->flags & SEC_ALLOC) == 0)
2519 if ((sec->flags & SEC_CODE) != 0)
2522 s->flags |= BSF_SYNTHETIC;
2523 s->value = ent - s->section->vma;
2526 len = strlen (syms[i]->name);
2527 memcpy (names, syms[i]->name, len + 1);
2529 /* Have udata.p point back to the original symbol this
2530 synthetic symbol was derived from. */
2531 s->udata.p = syms[i];
2537 if (glink != NULL && relplt != NULL)
2541 /* Add a symbol for the main glink trampoline. */
2542 memset (s, 0, sizeof *s);
2544 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2546 s->value = resolv_vma - glink->vma;
2548 memcpy (names, "__glink_PLTresolve",
2549 sizeof ("__glink_PLTresolve"));
2550 names += sizeof ("__glink_PLTresolve");
2555 /* FIXME: It would be very much nicer to put sym@plt on the
2556 stub rather than on the glink branch table entry. The
2557 objdump disassembler would then use a sensible symbol
2558 name on plt calls. The difficulty in doing so is
2559 a) finding the stubs, and,
2560 b) matching stubs against plt entries, and,
2561 c) there can be multiple stubs for a given plt entry.
2563 Solving (a) could be done by code scanning, but older
2564 ppc64 binaries used different stubs to current code.
2565 (b) is the tricky one since you need to known the toc
2566 pointer for at least one function that uses a pic stub to
2567 be able to calculate the plt address referenced.
2568 (c) means gdb would need to set multiple breakpoints (or
2569 find the glink branch itself) when setting breakpoints
2570 for pending shared library loads. */
2571 p = relplt->relocation;
2572 for (i = 0; i < plt_count; i++, p++)
2576 *s = **p->sym_ptr_ptr;
2577 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2578 we are defining a symbol, ensure one of them is set. */
2579 if ((s->flags & BSF_LOCAL) == 0)
2580 s->flags |= BSF_GLOBAL;
2581 s->flags |= BSF_SYNTHETIC;
2583 s->value = glink_vma - glink->vma;
2586 len = strlen ((*p->sym_ptr_ptr)->name);
2587 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2591 memcpy (names, "+0x", sizeof ("+0x") - 1);
2592 names += sizeof ("+0x") - 1;
2593 bfd_sprintf_vma (abfd, names, p->addend);
2594 names += strlen (names);
2596 memcpy (names, "@plt", sizeof ("@plt"));
2597 names += sizeof ("@plt");
2617 /* The following functions are specific to the ELF linker, while
2618 functions above are used generally. Those named ppc64_elf_* are
2619 called by the main ELF linker code. They appear in this file more
2620 or less in the order in which they are called. eg.
2621 ppc64_elf_check_relocs is called early in the link process,
2622 ppc64_elf_finish_dynamic_sections is one of the last functions
2625 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2626 functions have both a function code symbol and a function descriptor
2627 symbol. A call to foo in a relocatable object file looks like:
2634 The function definition in another object file might be:
2638 . .quad .TOC.@tocbase
2644 When the linker resolves the call during a static link, the branch
2645 unsurprisingly just goes to .foo and the .opd information is unused.
2646 If the function definition is in a shared library, things are a little
2647 different: The call goes via a plt call stub, the opd information gets
2648 copied to the plt, and the linker patches the nop.
2656 . std 2,40(1) # in practice, the call stub
2657 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2658 . addi 11,11,Lfoo@toc@l # this is the general idea
2666 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2668 The "reloc ()" notation is supposed to indicate that the linker emits
2669 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2672 What are the difficulties here? Well, firstly, the relocations
2673 examined by the linker in check_relocs are against the function code
2674 sym .foo, while the dynamic relocation in the plt is emitted against
2675 the function descriptor symbol, foo. Somewhere along the line, we need
2676 to carefully copy dynamic link information from one symbol to the other.
2677 Secondly, the generic part of the elf linker will make .foo a dynamic
2678 symbol as is normal for most other backends. We need foo dynamic
2679 instead, at least for an application final link. However, when
2680 creating a shared library containing foo, we need to have both symbols
2681 dynamic so that references to .foo are satisfied during the early
2682 stages of linking. Otherwise the linker might decide to pull in a
2683 definition from some other object, eg. a static library.
2685 Update: As of August 2004, we support a new convention. Function
2686 calls may use the function descriptor symbol, ie. "bl foo". This
2687 behaves exactly as "bl .foo". */
2689 /* Of those relocs that might be copied as dynamic relocs, this
2690 function selects those that must be copied when linking a shared
2691 library or PIE, even when the symbol is local. */
2694 must_be_dyn_reloc (struct bfd_link_info *info,
2695 enum elf_ppc64_reloc_type r_type)
2700 /* Only relative relocs can be resolved when the object load
2701 address isn't fixed. DTPREL64 is excluded because the
2702 dynamic linker needs to differentiate global dynamic from
2703 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2711 case R_PPC64_TPREL16:
2712 case R_PPC64_TPREL16_LO:
2713 case R_PPC64_TPREL16_HI:
2714 case R_PPC64_TPREL16_HA:
2715 case R_PPC64_TPREL16_DS:
2716 case R_PPC64_TPREL16_LO_DS:
2717 case R_PPC64_TPREL16_HIGH:
2718 case R_PPC64_TPREL16_HIGHA:
2719 case R_PPC64_TPREL16_HIGHER:
2720 case R_PPC64_TPREL16_HIGHERA:
2721 case R_PPC64_TPREL16_HIGHEST:
2722 case R_PPC64_TPREL16_HIGHESTA:
2723 case R_PPC64_TPREL64:
2724 /* These relocations are relative but in a shared library the
2725 linker doesn't know the thread pointer base. */
2726 return bfd_link_dll (info);
2730 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2731 copying dynamic variables from a shared lib into an app's dynbss
2732 section, and instead use a dynamic relocation to point into the
2733 shared lib. With code that gcc generates, it's vital that this be
2734 enabled; In the PowerPC64 ABI, the address of a function is actually
2735 the address of a function descriptor, which resides in the .opd
2736 section. gcc uses the descriptor directly rather than going via the
2737 GOT as some other ABI's do, which means that initialized function
2738 pointers must reference the descriptor. Thus, a function pointer
2739 initialized to the address of a function in a shared library will
2740 either require a copy reloc, or a dynamic reloc. Using a copy reloc
2741 redefines the function descriptor symbol to point to the copy. This
2742 presents a problem as a plt entry for that function is also
2743 initialized from the function descriptor symbol and the copy reloc
2744 may not be initialized first. */
2745 #define ELIMINATE_COPY_RELOCS 1
2747 /* Section name for stubs is the associated section name plus this
2749 #define STUB_SUFFIX ".stub"
2752 ppc_stub_long_branch:
2753 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2754 destination, but a 24 bit branch in a stub section will reach.
2757 ppc_stub_plt_branch:
2758 Similar to the above, but a 24 bit branch in the stub section won't
2759 reach its destination.
2760 . addis %r11,%r2,xxx@toc@ha
2761 . ld %r12,xxx@toc@l(%r11)
2766 Used to call a function in a shared library. If it so happens that
2767 the plt entry referenced crosses a 64k boundary, then an extra
2768 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2769 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
2770 . addis %r11,%r2,xxx@toc@ha
2771 . ld %r12,xxx+0@toc@l(%r11)
2773 . ld %r2,xxx+8@toc@l(%r11)
2774 . ld %r11,xxx+16@toc@l(%r11)
2777 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2778 code to adjust the value and save r2 to support multiple toc sections.
2779 A ppc_stub_long_branch with an r2 offset looks like:
2781 . addis %r2,%r2,off@ha
2782 . addi %r2,%r2,off@l
2785 A ppc_stub_plt_branch with an r2 offset looks like:
2787 . addis %r11,%r2,xxx@toc@ha
2788 . ld %r12,xxx@toc@l(%r11)
2789 . addis %r2,%r2,off@ha
2790 . addi %r2,%r2,off@l
2794 All of the above stubs are shown as their ELFv1 variants. ELFv2
2795 variants exist too, simpler for plt calls since a new toc pointer
2796 and static chain are not loaded by the stub. In addition, ELFv2
2797 has some more complex stubs to handle calls marked with NOTOC
2798 relocs from functions where r2 is not a valid toc pointer. These
2799 come in two flavours, the ones shown below, and _both variants that
2800 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2801 one call is from a function where r2 is used as the toc pointer but
2802 needs a toc adjusting stub for small-model multi-toc, and another
2803 call is from a function where r2 is not valid.
2804 ppc_stub_long_branch_notoc:
2810 . addis %r12,%r11,dest-1b@ha
2811 . addi %r12,%r12,dest-1b@l
2814 ppc_stub_plt_branch_notoc:
2820 . lis %r12,xxx-1b@highest
2821 . ori %r12,%r12,xxx-1b@higher
2823 . oris %r12,%r12,xxx-1b@high
2824 . ori %r12,%r12,xxx-1b@l
2825 . add %r12,%r11,%r12
2829 ppc_stub_plt_call_notoc:
2835 . lis %r12,xxx-1b@highest
2836 . ori %r12,%r12,xxx-1b@higher
2838 . oris %r12,%r12,xxx-1b@high
2839 . ori %r12,%r12,xxx-1b@l
2840 . ldx %r12,%r11,%r12
2844 In cases where the high instructions would add zero, they are
2845 omitted and following instructions modified in some cases.
2847 For a given stub group (a set of sections all using the same toc
2848 pointer value) there will be just one stub type used for any
2849 particular function symbol. For example, if printf is called from
2850 code with the tocsave optimization (ie. r2 saved in function
2851 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2852 and from other code without the tocsave optimization requiring a
2853 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2854 type will be created. Calls with the tocsave optimization will
2855 enter this stub after the instruction saving r2. A similar
2856 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2857 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2858 to call an external function like printf. If other calls to printf
2859 require a ppc_stub_plt_call linkage stub then a single
2860 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2861 call. If other calls to printf require a ppc_stub_plt_call_r2save
2862 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2863 be created and calls not requiring r2 to be saved will enter the
2864 stub after the r2 save instruction. There is an analogous
2865 hierarchy of long branch and plt branch stubs for local call
2871 ppc_stub_long_branch,
2872 ppc_stub_long_branch_r2off,
2873 ppc_stub_long_branch_notoc,
2874 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
2875 ppc_stub_plt_branch,
2876 ppc_stub_plt_branch_r2off,
2877 ppc_stub_plt_branch_notoc,
2878 ppc_stub_plt_branch_both,
2880 ppc_stub_plt_call_r2save,
2881 ppc_stub_plt_call_notoc,
2882 ppc_stub_plt_call_both,
2883 ppc_stub_global_entry,
2887 /* Information on stub grouping. */
2890 /* The stub section. */
2892 /* This is the section to which stubs in the group will be attached. */
2895 struct map_stub *next;
2896 /* Whether to emit a copy of register save/restore functions in this
2899 /* Current offset within stubs after the insn restoring lr in a
2900 _notoc or _both stub using bcl for pc-relative addressing, or
2901 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2902 unsigned int lr_restore;
2903 /* Accumulated size of EH info emitted to describe return address
2904 if stubs modify lr. Does not include 17 byte FDE header. */
2905 unsigned int eh_size;
2906 /* Offset in glink_eh_frame to the start of EH info for this group. */
2907 unsigned int eh_base;
2910 struct ppc_stub_hash_entry
2912 /* Base hash table entry structure. */
2913 struct bfd_hash_entry root;
2915 enum ppc_stub_type stub_type;
2917 /* Group information. */
2918 struct map_stub *group;
2920 /* Offset within stub_sec of the beginning of this stub. */
2921 bfd_vma stub_offset;
2923 /* Given the symbol's value and its section we can determine its final
2924 value when building the stubs (so the stub knows where to jump. */
2925 bfd_vma target_value;
2926 asection *target_section;
2928 /* The symbol table entry, if any, that this was derived from. */
2929 struct ppc_link_hash_entry *h;
2930 struct plt_entry *plt_ent;
2933 unsigned char symtype;
2935 /* Symbol st_other. */
2936 unsigned char other;
2939 struct ppc_branch_hash_entry
2941 /* Base hash table entry structure. */
2942 struct bfd_hash_entry root;
2944 /* Offset within branch lookup table. */
2945 unsigned int offset;
2947 /* Generation marker. */
2951 /* Used to track dynamic relocations for local symbols. */
2952 struct ppc_dyn_relocs
2954 struct ppc_dyn_relocs *next;
2956 /* The input section of the reloc. */
2959 /* Total number of relocs copied for the input section. */
2960 unsigned int count : 31;
2962 /* Whether this entry is for STT_GNU_IFUNC symbols. */
2963 unsigned int ifunc : 1;
2966 struct ppc_link_hash_entry
2968 struct elf_link_hash_entry elf;
2972 /* A pointer to the most recently used stub hash entry against this
2974 struct ppc_stub_hash_entry *stub_cache;
2976 /* A pointer to the next symbol starting with a '.' */
2977 struct ppc_link_hash_entry *next_dot_sym;
2980 /* Track dynamic relocs copied for this symbol. */
2981 struct elf_dyn_relocs *dyn_relocs;
2983 /* Link between function code and descriptor symbols. */
2984 struct ppc_link_hash_entry *oh;
2986 /* Flag function code and descriptor symbols. */
2987 unsigned int is_func:1;
2988 unsigned int is_func_descriptor:1;
2989 unsigned int fake:1;
2991 /* Whether global opd/toc sym has been adjusted or not.
2992 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
2993 should be set for all globals defined in any opd/toc section. */
2994 unsigned int adjust_done:1;
2996 /* Set if this is an out-of-line register save/restore function,
2997 with non-standard calling convention. */
2998 unsigned int save_res:1;
3000 /* Set if a duplicate symbol with non-zero localentry is detected,
3001 even when the duplicate symbol does not provide a definition. */
3002 unsigned int non_zero_localentry:1;
3004 /* Contexts in which symbol is used in the GOT (or TOC).
3005 Bits are or'd into the mask as the corresponding relocs are
3006 encountered during check_relocs, with TLS_TLS being set when any
3007 of the other TLS bits are set. tls_optimize clears bits when
3008 optimizing to indicate the corresponding GOT entry type is not
3009 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3010 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
3011 separate flag rather than setting TPREL just for convenience in
3012 distinguishing the two cases.
3013 These flags are also kept for local symbols. */
3014 #define TLS_TLS 1 /* Any TLS reloc. */
3015 #define TLS_GD 2 /* GD reloc. */
3016 #define TLS_LD 4 /* LD reloc. */
3017 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
3018 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3019 #define TLS_MARK 32 /* __tls_get_addr call marked. */
3020 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
3021 #define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
3022 unsigned char tls_mask;
3024 /* The above field is also used to mark function symbols. In which
3025 case TLS_TLS will be 0. */
3026 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
3027 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
3028 #define NON_GOT 256 /* local symbol plt, not stored. */
3031 /* ppc64 ELF linker hash table. */
3033 struct ppc_link_hash_table
3035 struct elf_link_hash_table elf;
3037 /* The stub hash table. */
3038 struct bfd_hash_table stub_hash_table;
3040 /* Another hash table for plt_branch stubs. */
3041 struct bfd_hash_table branch_hash_table;
3043 /* Hash table for function prologue tocsave. */
3044 htab_t tocsave_htab;
3046 /* Various options and other info passed from the linker. */
3047 struct ppc64_elf_params *params;
3049 /* The size of sec_info below. */
3050 unsigned int sec_info_arr_size;
3052 /* Per-section array of extra section info. Done this way rather
3053 than as part of ppc64_elf_section_data so we have the info for
3054 non-ppc64 sections. */
3057 /* Along with elf_gp, specifies the TOC pointer used by this section. */
3062 /* The section group that this section belongs to. */
3063 struct map_stub *group;
3064 /* A temp section list pointer. */
3069 /* Linked list of groups. */
3070 struct map_stub *group;
3072 /* Temp used when calculating TOC pointers. */
3075 asection *toc_first_sec;
3077 /* Used when adding symbols. */
3078 struct ppc_link_hash_entry *dot_syms;
3080 /* Shortcuts to get to dynamic linker sections. */
3082 asection *global_entry;
3085 asection *relpltlocal;
3088 asection *glink_eh_frame;
3090 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3091 struct ppc_link_hash_entry *tls_get_addr;
3092 struct ppc_link_hash_entry *tls_get_addr_fd;
3094 /* The size of reliplt used by got entry relocs. */
3095 bfd_size_type got_reli_size;
3098 unsigned long stub_count[ppc_stub_global_entry];
3100 /* Number of stubs against global syms. */
3101 unsigned long stub_globals;
3103 /* Set if we're linking code with function descriptors. */
3104 unsigned int opd_abi:1;
3106 /* Support for multiple toc sections. */
3107 unsigned int do_multi_toc:1;
3108 unsigned int multi_toc_needed:1;
3109 unsigned int second_toc_pass:1;
3110 unsigned int do_toc_opt:1;
3112 /* Set if tls optimization is enabled. */
3113 unsigned int do_tls_opt:1;
3115 /* Set if inline plt calls should be converted to direct calls. */
3116 unsigned int can_convert_all_inline_plt:1;
3119 unsigned int stub_error:1;
3121 /* Whether func_desc_adjust needs to be run over symbols. */
3122 unsigned int need_func_desc_adj:1;
3124 /* Whether there exist local gnu indirect function resolvers,
3125 referenced by dynamic relocations. */
3126 unsigned int local_ifunc_resolver:1;
3127 unsigned int maybe_local_ifunc_resolver:1;
3129 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3130 unsigned int has_plt_localentry0:1;
3132 /* Whether calls are made via the PLT from NOTOC functions. */
3133 unsigned int notoc_plt:1;
3135 /* Incremented every time we size stubs. */
3136 unsigned int stub_iteration;
3138 /* Small local sym cache. */
3139 struct sym_cache sym_cache;
3142 /* Rename some of the generic section flags to better document how they
3145 /* Nonzero if this section has TLS related relocations. */
3146 #define has_tls_reloc sec_flg0
3148 /* Nonzero if this section has an old-style call to __tls_get_addr. */
3149 #define has_tls_get_addr_call sec_flg1
3151 /* Nonzero if this section has any toc or got relocs. */
3152 #define has_toc_reloc sec_flg2
3154 /* Nonzero if this section has a call to another section that uses
3156 #define makes_toc_func_call sec_flg3
3158 /* Recursion protection when determining above flag. */
3159 #define call_check_in_progress sec_flg4
3160 #define call_check_done sec_flg5
3162 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3164 #define ppc_hash_table(p) \
3165 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3166 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3168 #define ppc_stub_hash_lookup(table, string, create, copy) \
3169 ((struct ppc_stub_hash_entry *) \
3170 bfd_hash_lookup ((table), (string), (create), (copy)))
3172 #define ppc_branch_hash_lookup(table, string, create, copy) \
3173 ((struct ppc_branch_hash_entry *) \
3174 bfd_hash_lookup ((table), (string), (create), (copy)))
3176 /* Create an entry in the stub hash table. */
3178 static struct bfd_hash_entry *
3179 stub_hash_newfunc (struct bfd_hash_entry *entry,
3180 struct bfd_hash_table *table,
3183 /* Allocate the structure if it has not already been allocated by a
3187 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3192 /* Call the allocation method of the superclass. */
3193 entry = bfd_hash_newfunc (entry, table, string);
3196 struct ppc_stub_hash_entry *eh;
3198 /* Initialize the local fields. */
3199 eh = (struct ppc_stub_hash_entry *) entry;
3200 eh->stub_type = ppc_stub_none;
3202 eh->stub_offset = 0;
3203 eh->target_value = 0;
3204 eh->target_section = NULL;
3213 /* Create an entry in the branch hash table. */
3215 static struct bfd_hash_entry *
3216 branch_hash_newfunc (struct bfd_hash_entry *entry,
3217 struct bfd_hash_table *table,
3220 /* Allocate the structure if it has not already been allocated by a
3224 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3229 /* Call the allocation method of the superclass. */
3230 entry = bfd_hash_newfunc (entry, table, string);
3233 struct ppc_branch_hash_entry *eh;
3235 /* Initialize the local fields. */
3236 eh = (struct ppc_branch_hash_entry *) entry;
3244 /* Create an entry in a ppc64 ELF linker hash table. */
3246 static struct bfd_hash_entry *
3247 link_hash_newfunc (struct bfd_hash_entry *entry,
3248 struct bfd_hash_table *table,
3251 /* Allocate the structure if it has not already been allocated by a
3255 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3260 /* Call the allocation method of the superclass. */
3261 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3264 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3266 memset (&eh->u.stub_cache, 0,
3267 (sizeof (struct ppc_link_hash_entry)
3268 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3270 /* When making function calls, old ABI code references function entry
3271 points (dot symbols), while new ABI code references the function
3272 descriptor symbol. We need to make any combination of reference and
3273 definition work together, without breaking archive linking.
3275 For a defined function "foo" and an undefined call to "bar":
3276 An old object defines "foo" and ".foo", references ".bar" (possibly
3278 A new object defines "foo" and references "bar".
3280 A new object thus has no problem with its undefined symbols being
3281 satisfied by definitions in an old object. On the other hand, the
3282 old object won't have ".bar" satisfied by a new object.
3284 Keep a list of newly added dot-symbols. */
3286 if (string[0] == '.')
3288 struct ppc_link_hash_table *htab;
3290 htab = (struct ppc_link_hash_table *) table;
3291 eh->u.next_dot_sym = htab->dot_syms;
3292 htab->dot_syms = eh;
3299 struct tocsave_entry
3306 tocsave_htab_hash (const void *p)
3308 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3309 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3313 tocsave_htab_eq (const void *p1, const void *p2)
3315 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3316 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3317 return e1->sec == e2->sec && e1->offset == e2->offset;
3320 /* Destroy a ppc64 ELF linker hash table. */
3323 ppc64_elf_link_hash_table_free (bfd *obfd)
3325 struct ppc_link_hash_table *htab;
3327 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3328 if (htab->tocsave_htab)
3329 htab_delete (htab->tocsave_htab);
3330 bfd_hash_table_free (&htab->branch_hash_table);
3331 bfd_hash_table_free (&htab->stub_hash_table);
3332 _bfd_elf_link_hash_table_free (obfd);
3335 /* Create a ppc64 ELF linker hash table. */
3337 static struct bfd_link_hash_table *
3338 ppc64_elf_link_hash_table_create (bfd *abfd)
3340 struct ppc_link_hash_table *htab;
3341 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3343 htab = bfd_zmalloc (amt);
3347 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3348 sizeof (struct ppc_link_hash_entry),
3355 /* Init the stub hash table too. */
3356 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3357 sizeof (struct ppc_stub_hash_entry)))
3359 _bfd_elf_link_hash_table_free (abfd);
3363 /* And the branch hash table. */
3364 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3365 sizeof (struct ppc_branch_hash_entry)))
3367 bfd_hash_table_free (&htab->stub_hash_table);
3368 _bfd_elf_link_hash_table_free (abfd);
3372 htab->tocsave_htab = htab_try_create (1024,
3376 if (htab->tocsave_htab == NULL)
3378 ppc64_elf_link_hash_table_free (abfd);
3381 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3383 /* Initializing two fields of the union is just cosmetic. We really
3384 only care about glist, but when compiled on a 32-bit host the
3385 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3386 debugger inspection of these fields look nicer. */
3387 htab->elf.init_got_refcount.refcount = 0;
3388 htab->elf.init_got_refcount.glist = NULL;
3389 htab->elf.init_plt_refcount.refcount = 0;
3390 htab->elf.init_plt_refcount.glist = NULL;
3391 htab->elf.init_got_offset.offset = 0;
3392 htab->elf.init_got_offset.glist = NULL;
3393 htab->elf.init_plt_offset.offset = 0;
3394 htab->elf.init_plt_offset.glist = NULL;
3396 return &htab->elf.root;
3399 /* Create sections for linker generated code. */
3402 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3404 struct ppc_link_hash_table *htab;
3407 htab = ppc_hash_table (info);
3409 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3410 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3411 if (htab->params->save_restore_funcs)
3413 /* Create .sfpr for code to save and restore fp regs. */
3414 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3416 if (htab->sfpr == NULL
3417 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3421 if (bfd_link_relocatable (info))
3424 /* Create .glink for lazy dynamic linking support. */
3425 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3427 if (htab->glink == NULL
3428 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
3431 /* The part of .glink used by global entry stubs, separate so that
3432 it can be aligned appropriately without affecting htab->glink. */
3433 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3435 if (htab->global_entry == NULL
3436 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
3439 if (!info->no_ld_generated_unwind_info)
3441 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3442 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3443 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3446 if (htab->glink_eh_frame == NULL
3447 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3451 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3452 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3453 if (htab->elf.iplt == NULL
3454 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
3457 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3458 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3460 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3461 if (htab->elf.irelplt == NULL
3462 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
3465 /* Create branch lookup table for plt_branch stubs. */
3466 flags = (SEC_ALLOC | SEC_LOAD
3467 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3468 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3470 if (htab->brlt == NULL
3471 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
3474 /* Local plt entries, put in .branch_lt but a separate section for
3476 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3478 if (htab->pltlocal == NULL
3479 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
3482 if (!bfd_link_pic (info))
3485 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3486 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3488 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3489 if (htab->relbrlt == NULL
3490 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3494 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3495 if (htab->relpltlocal == NULL
3496 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
3502 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3505 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3506 struct ppc64_elf_params *params)
3508 struct ppc_link_hash_table *htab;
3510 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3512 /* Always hook our dynamic sections into the first bfd, which is the
3513 linker created stub bfd. This ensures that the GOT header is at
3514 the start of the output TOC section. */
3515 htab = ppc_hash_table (info);
3516 htab->elf.dynobj = params->stub_bfd;
3517 htab->params = params;
3519 return create_linkage_sections (htab->elf.dynobj, info);
3522 /* Build a name for an entry in the stub hash table. */
3525 ppc_stub_name (const asection *input_section,
3526 const asection *sym_sec,
3527 const struct ppc_link_hash_entry *h,
3528 const Elf_Internal_Rela *rel)
3533 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3534 offsets from a sym as a branch target? In fact, we could
3535 probably assume the addend is always zero. */
3536 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3540 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3541 stub_name = bfd_malloc (len);
3542 if (stub_name == NULL)
3545 len = sprintf (stub_name, "%08x.%s+%x",
3546 input_section->id & 0xffffffff,
3547 h->elf.root.root.string,
3548 (int) rel->r_addend & 0xffffffff);
3552 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3553 stub_name = bfd_malloc (len);
3554 if (stub_name == NULL)
3557 len = sprintf (stub_name, "%08x.%x:%x+%x",
3558 input_section->id & 0xffffffff,
3559 sym_sec->id & 0xffffffff,
3560 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3561 (int) rel->r_addend & 0xffffffff);
3563 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3564 stub_name[len - 2] = 0;
3568 /* Look up an entry in the stub hash. Stub entries are cached because
3569 creating the stub name takes a bit of time. */
3571 static struct ppc_stub_hash_entry *
3572 ppc_get_stub_entry (const asection *input_section,
3573 const asection *sym_sec,
3574 struct ppc_link_hash_entry *h,
3575 const Elf_Internal_Rela *rel,
3576 struct ppc_link_hash_table *htab)
3578 struct ppc_stub_hash_entry *stub_entry;
3579 struct map_stub *group;
3581 /* If this input section is part of a group of sections sharing one
3582 stub section, then use the id of the first section in the group.
3583 Stub names need to include a section id, as there may well be
3584 more than one stub used to reach say, printf, and we need to
3585 distinguish between them. */
3586 group = htab->sec_info[input_section->id].u.group;
3590 if (h != NULL && h->u.stub_cache != NULL
3591 && h->u.stub_cache->h == h
3592 && h->u.stub_cache->group == group)
3594 stub_entry = h->u.stub_cache;
3600 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3601 if (stub_name == NULL)
3604 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3605 stub_name, FALSE, FALSE);
3607 h->u.stub_cache = stub_entry;
3615 /* Add a new stub entry to the stub hash. Not all fields of the new
3616 stub entry are initialised. */
3618 static struct ppc_stub_hash_entry *
3619 ppc_add_stub (const char *stub_name,
3621 struct bfd_link_info *info)
3623 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3624 struct map_stub *group;
3627 struct ppc_stub_hash_entry *stub_entry;
3629 group = htab->sec_info[section->id].u.group;
3630 link_sec = group->link_sec;
3631 stub_sec = group->stub_sec;
3632 if (stub_sec == NULL)
3638 namelen = strlen (link_sec->name);
3639 len = namelen + sizeof (STUB_SUFFIX);
3640 s_name = bfd_alloc (htab->params->stub_bfd, len);
3644 memcpy (s_name, link_sec->name, namelen);
3645 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3646 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3647 if (stub_sec == NULL)
3649 group->stub_sec = stub_sec;
3652 /* Enter this entry into the linker stub hash table. */
3653 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3655 if (stub_entry == NULL)
3657 /* xgettext:c-format */
3658 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3659 section->owner, stub_name);
3663 stub_entry->group = group;
3664 stub_entry->stub_offset = 0;
3668 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3669 not already done. */
3672 create_got_section (bfd *abfd, struct bfd_link_info *info)
3674 asection *got, *relgot;
3676 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3678 if (!is_ppc64_elf (abfd))
3684 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3687 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3688 | SEC_LINKER_CREATED);
3690 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3692 || !bfd_set_section_alignment (abfd, got, 3))
3695 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3696 flags | SEC_READONLY);
3698 || !bfd_set_section_alignment (abfd, relgot, 3))
3701 ppc64_elf_tdata (abfd)->got = got;
3702 ppc64_elf_tdata (abfd)->relgot = relgot;
3706 /* Follow indirect and warning symbol links. */
3708 static inline struct bfd_link_hash_entry *
3709 follow_link (struct bfd_link_hash_entry *h)
3711 while (h->type == bfd_link_hash_indirect
3712 || h->type == bfd_link_hash_warning)
3717 static inline struct elf_link_hash_entry *
3718 elf_follow_link (struct elf_link_hash_entry *h)
3720 return (struct elf_link_hash_entry *) follow_link (&h->root);
3723 static inline struct ppc_link_hash_entry *
3724 ppc_follow_link (struct ppc_link_hash_entry *h)
3726 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3729 /* Merge PLT info on FROM with that on TO. */
3732 move_plt_plist (struct ppc_link_hash_entry *from,
3733 struct ppc_link_hash_entry *to)
3735 if (from->elf.plt.plist != NULL)
3737 if (to->elf.plt.plist != NULL)
3739 struct plt_entry **entp;
3740 struct plt_entry *ent;
3742 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3744 struct plt_entry *dent;
3746 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3747 if (dent->addend == ent->addend)
3749 dent->plt.refcount += ent->plt.refcount;
3756 *entp = to->elf.plt.plist;
3759 to->elf.plt.plist = from->elf.plt.plist;
3760 from->elf.plt.plist = NULL;
3764 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3767 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3768 struct elf_link_hash_entry *dir,
3769 struct elf_link_hash_entry *ind)
3771 struct ppc_link_hash_entry *edir, *eind;
3773 edir = (struct ppc_link_hash_entry *) dir;
3774 eind = (struct ppc_link_hash_entry *) ind;
3776 edir->is_func |= eind->is_func;
3777 edir->is_func_descriptor |= eind->is_func_descriptor;
3778 edir->tls_mask |= eind->tls_mask;
3779 if (eind->oh != NULL)
3780 edir->oh = ppc_follow_link (eind->oh);
3782 if (edir->elf.versioned != versioned_hidden)
3783 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3784 edir->elf.ref_regular |= eind->elf.ref_regular;
3785 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3786 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3787 edir->elf.needs_plt |= eind->elf.needs_plt;
3788 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3790 /* If we were called to copy over info for a weak sym, don't copy
3791 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3792 in order to simplify readonly_dynrelocs and save a field in the
3793 symbol hash entry, but that means dyn_relocs can't be used in any
3794 tests about a specific symbol, or affect other symbol flags which
3796 if (eind->elf.root.type != bfd_link_hash_indirect)
3799 /* Copy over any dynamic relocs we may have on the indirect sym. */
3800 if (eind->dyn_relocs != NULL)
3802 if (edir->dyn_relocs != NULL)
3804 struct elf_dyn_relocs **pp;
3805 struct elf_dyn_relocs *p;
3807 /* Add reloc counts against the indirect sym to the direct sym
3808 list. Merge any entries against the same section. */
3809 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3811 struct elf_dyn_relocs *q;
3813 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3814 if (q->sec == p->sec)
3816 q->pc_count += p->pc_count;
3817 q->count += p->count;
3824 *pp = edir->dyn_relocs;
3827 edir->dyn_relocs = eind->dyn_relocs;
3828 eind->dyn_relocs = NULL;
3831 /* Copy over got entries that we may have already seen to the
3832 symbol which just became indirect. */
3833 if (eind->elf.got.glist != NULL)
3835 if (edir->elf.got.glist != NULL)
3837 struct got_entry **entp;
3838 struct got_entry *ent;
3840 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3842 struct got_entry *dent;
3844 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3845 if (dent->addend == ent->addend
3846 && dent->owner == ent->owner
3847 && dent->tls_type == ent->tls_type)
3849 dent->got.refcount += ent->got.refcount;
3856 *entp = edir->elf.got.glist;
3859 edir->elf.got.glist = eind->elf.got.glist;
3860 eind->elf.got.glist = NULL;
3863 /* And plt entries. */
3864 move_plt_plist (eind, edir);
3866 if (eind->elf.dynindx != -1)
3868 if (edir->elf.dynindx != -1)
3869 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3870 edir->elf.dynstr_index);
3871 edir->elf.dynindx = eind->elf.dynindx;
3872 edir->elf.dynstr_index = eind->elf.dynstr_index;
3873 eind->elf.dynindx = -1;
3874 eind->elf.dynstr_index = 0;
3878 /* Find the function descriptor hash entry from the given function code
3879 hash entry FH. Link the entries via their OH fields. */
3881 static struct ppc_link_hash_entry *
3882 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3884 struct ppc_link_hash_entry *fdh = fh->oh;
3888 const char *fd_name = fh->elf.root.root.string + 1;
3890 fdh = (struct ppc_link_hash_entry *)
3891 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
3895 fdh->is_func_descriptor = 1;
3901 fdh = ppc_follow_link (fdh);
3902 fdh->is_func_descriptor = 1;
3907 /* Make a fake function descriptor sym for the undefined code sym FH. */
3909 static struct ppc_link_hash_entry *
3910 make_fdh (struct bfd_link_info *info,
3911 struct ppc_link_hash_entry *fh)
3913 bfd *abfd = fh->elf.root.u.undef.abfd;
3914 struct bfd_link_hash_entry *bh = NULL;
3915 struct ppc_link_hash_entry *fdh;
3916 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3920 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3921 fh->elf.root.root.string + 1,
3922 flags, bfd_und_section_ptr, 0,
3923 NULL, FALSE, FALSE, &bh))
3926 fdh = (struct ppc_link_hash_entry *) bh;
3927 fdh->elf.non_elf = 0;
3929 fdh->is_func_descriptor = 1;
3936 /* Fix function descriptor symbols defined in .opd sections to be
3940 ppc64_elf_add_symbol_hook (bfd *ibfd,
3941 struct bfd_link_info *info,
3942 Elf_Internal_Sym *isym,
3944 flagword *flags ATTRIBUTE_UNUSED,
3949 && strcmp ((*sec)->name, ".opd") == 0)
3953 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3954 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
3955 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
3957 /* If the symbol is a function defined in .opd, and the function
3958 code is in a discarded group, let it appear to be undefined. */
3959 if (!bfd_link_relocatable (info)
3960 && (*sec)->reloc_count != 0
3961 && opd_entry_value (*sec, *value, &code_sec, NULL,
3962 FALSE) != (bfd_vma) -1
3963 && discarded_section (code_sec))
3965 *sec = bfd_und_section_ptr;
3966 isym->st_shndx = SHN_UNDEF;
3969 else if (*sec != NULL
3970 && strcmp ((*sec)->name, ".toc") == 0
3971 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
3973 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3975 htab->params->object_in_toc = 1;
3978 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3980 if (abiversion (ibfd) == 0)
3981 set_abiversion (ibfd, 2);
3982 else if (abiversion (ibfd) == 1)
3984 _bfd_error_handler (_("symbol '%s' has invalid st_other"
3985 " for ABI version 1"), *name);
3986 bfd_set_error (bfd_error_bad_value);
3994 /* Merge non-visibility st_other attributes: local entry point. */
3997 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
3998 const Elf_Internal_Sym *isym,
3999 bfd_boolean definition,
4000 bfd_boolean dynamic)
4002 if (definition && (!dynamic || !h->def_regular))
4003 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4004 | ELF_ST_VISIBILITY (h->other));
4007 /* Hook called on merging a symbol. We use this to clear "fake" since
4008 we now have a real symbol. */
4011 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4012 const Elf_Internal_Sym *isym,
4013 asection **psec ATTRIBUTE_UNUSED,
4014 bfd_boolean newdef ATTRIBUTE_UNUSED,
4015 bfd_boolean olddef ATTRIBUTE_UNUSED,
4016 bfd *oldbfd ATTRIBUTE_UNUSED,
4017 const asection *oldsec ATTRIBUTE_UNUSED)
4019 ((struct ppc_link_hash_entry *) h)->fake = 0;
4020 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4021 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
4025 /* This function makes an old ABI object reference to ".bar" cause the
4026 inclusion of a new ABI object archive that defines "bar".
4027 NAME is a symbol defined in an archive. Return a symbol in the hash
4028 table that might be satisfied by the archive symbols. */
4030 static struct elf_link_hash_entry *
4031 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4032 struct bfd_link_info *info,
4035 struct elf_link_hash_entry *h;
4039 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4041 /* Don't return this sym if it is a fake function descriptor
4042 created by add_symbol_adjust. */
4043 && !((struct ppc_link_hash_entry *) h)->fake)
4049 len = strlen (name);
4050 dot_name = bfd_alloc (abfd, len + 2);
4051 if (dot_name == NULL)
4052 return (struct elf_link_hash_entry *) -1;
4054 memcpy (dot_name + 1, name, len + 1);
4055 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4056 bfd_release (abfd, dot_name);
4060 /* This function satisfies all old ABI object references to ".bar" if a
4061 new ABI object defines "bar". Well, at least, undefined dot symbols
4062 are made weak. This stops later archive searches from including an
4063 object if we already have a function descriptor definition. It also
4064 prevents the linker complaining about undefined symbols.
4065 We also check and correct mismatched symbol visibility here. The
4066 most restrictive visibility of the function descriptor and the
4067 function entry symbol is used. */
4070 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4072 struct ppc_link_hash_table *htab;
4073 struct ppc_link_hash_entry *fdh;
4075 if (eh->elf.root.type == bfd_link_hash_warning)
4076 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4078 if (eh->elf.root.type == bfd_link_hash_indirect)
4081 if (eh->elf.root.root.string[0] != '.')
4084 htab = ppc_hash_table (info);
4088 fdh = lookup_fdh (eh, htab);
4090 && !bfd_link_relocatable (info)
4091 && (eh->elf.root.type == bfd_link_hash_undefined
4092 || eh->elf.root.type == bfd_link_hash_undefweak)
4093 && eh->elf.ref_regular)
4095 /* Make an undefined function descriptor sym, in order to
4096 pull in an --as-needed shared lib. Archives are handled
4098 fdh = make_fdh (info, eh);
4105 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4106 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4108 /* Make both descriptor and entry symbol have the most
4109 constraining visibility of either symbol. */
4110 if (entry_vis < descr_vis)
4111 fdh->elf.other += entry_vis - descr_vis;
4112 else if (entry_vis > descr_vis)
4113 eh->elf.other += descr_vis - entry_vis;
4115 /* Propagate reference flags from entry symbol to function
4116 descriptor symbol. */
4117 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4118 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4119 fdh->elf.ref_regular |= eh->elf.ref_regular;
4120 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4122 if (!fdh->elf.forced_local
4123 && fdh->elf.dynindx == -1
4124 && fdh->elf.versioned != versioned_hidden
4125 && (bfd_link_dll (info)
4126 || fdh->elf.def_dynamic
4127 || fdh->elf.ref_dynamic)
4128 && (eh->elf.ref_regular
4129 || eh->elf.def_regular))
4131 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4139 /* Set up opd section info and abiversion for IBFD, and process list
4140 of dot-symbols we made in link_hash_newfunc. */
4143 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4145 struct ppc_link_hash_table *htab;
4146 struct ppc_link_hash_entry **p, *eh;
4147 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4149 if (opd != NULL && opd->size != 0)
4151 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4152 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4154 if (abiversion (ibfd) == 0)
4155 set_abiversion (ibfd, 1);
4156 else if (abiversion (ibfd) >= 2)
4158 /* xgettext:c-format */
4159 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4160 ibfd, abiversion (ibfd));
4161 bfd_set_error (bfd_error_bad_value);
4166 if (is_ppc64_elf (info->output_bfd))
4168 /* For input files without an explicit abiversion in e_flags
4169 we should have flagged any with symbol st_other bits set
4170 as ELFv1 and above flagged those with .opd as ELFv2.
4171 Set the output abiversion if not yet set, and for any input
4172 still ambiguous, take its abiversion from the output.
4173 Differences in ABI are reported later. */
4174 if (abiversion (info->output_bfd) == 0)
4175 set_abiversion (info->output_bfd, abiversion (ibfd));
4176 else if (abiversion (ibfd) == 0)
4177 set_abiversion (ibfd, abiversion (info->output_bfd));
4180 htab = ppc_hash_table (info);
4184 if (opd != NULL && opd->size != 0
4185 && (ibfd->flags & DYNAMIC) == 0
4186 && (opd->flags & SEC_RELOC) != 0
4187 && opd->reloc_count != 0
4188 && !bfd_is_abs_section (opd->output_section)
4189 && info->gc_sections)
4191 /* Garbage collection needs some extra help with .opd sections.
4192 We don't want to necessarily keep everything referenced by
4193 relocs in .opd, as that would keep all functions. Instead,
4194 if we reference an .opd symbol (a function descriptor), we
4195 want to keep the function code symbol's section. This is
4196 easy for global symbols, but for local syms we need to keep
4197 information about the associated function section. */
4199 asection **opd_sym_map;
4200 Elf_Internal_Shdr *symtab_hdr;
4201 Elf_Internal_Rela *relocs, *rel_end, *rel;
4203 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4204 opd_sym_map = bfd_zalloc (ibfd, amt);
4205 if (opd_sym_map == NULL)
4207 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4208 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4212 symtab_hdr = &elf_symtab_hdr (ibfd);
4213 rel_end = relocs + opd->reloc_count - 1;
4214 for (rel = relocs; rel < rel_end; rel++)
4216 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4217 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4219 if (r_type == R_PPC64_ADDR64
4220 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4221 && r_symndx < symtab_hdr->sh_info)
4223 Elf_Internal_Sym *isym;
4226 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4229 if (elf_section_data (opd)->relocs != relocs)
4234 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4235 if (s != NULL && s != opd)
4236 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4239 if (elf_section_data (opd)->relocs != relocs)
4243 p = &htab->dot_syms;
4244 while ((eh = *p) != NULL)
4247 if (&eh->elf == htab->elf.hgot)
4249 else if (htab->elf.hgot == NULL
4250 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4251 htab->elf.hgot = &eh->elf;
4252 else if (abiversion (ibfd) <= 1)
4254 htab->need_func_desc_adj = 1;
4255 if (!add_symbol_adjust (eh, info))
4258 p = &eh->u.next_dot_sym;
4263 /* Undo hash table changes when an --as-needed input file is determined
4264 not to be needed. */
4267 ppc64_elf_notice_as_needed (bfd *ibfd,
4268 struct bfd_link_info *info,
4269 enum notice_asneeded_action act)
4271 if (act == notice_not_needed)
4273 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4278 htab->dot_syms = NULL;
4280 return _bfd_elf_notice_as_needed (ibfd, info, act);
4283 /* If --just-symbols against a final linked binary, then assume we need
4284 toc adjusting stubs when calling functions defined there. */
4287 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4289 if ((sec->flags & SEC_CODE) != 0
4290 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4291 && is_ppc64_elf (sec->owner))
4293 if (abiversion (sec->owner) >= 2
4294 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4295 sec->has_toc_reloc = 1;
4297 _bfd_elf_link_just_syms (sec, info);
4300 static struct plt_entry **
4301 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4302 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4304 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4305 struct plt_entry **local_plt;
4306 unsigned char *local_got_tls_masks;
4308 if (local_got_ents == NULL)
4310 bfd_size_type size = symtab_hdr->sh_info;
4312 size *= (sizeof (*local_got_ents)
4313 + sizeof (*local_plt)
4314 + sizeof (*local_got_tls_masks));
4315 local_got_ents = bfd_zalloc (abfd, size);
4316 if (local_got_ents == NULL)
4318 elf_local_got_ents (abfd) = local_got_ents;
4321 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4323 struct got_entry *ent;
4325 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4326 if (ent->addend == r_addend
4327 && ent->owner == abfd
4328 && ent->tls_type == tls_type)
4332 bfd_size_type amt = sizeof (*ent);
4333 ent = bfd_alloc (abfd, amt);
4336 ent->next = local_got_ents[r_symndx];
4337 ent->addend = r_addend;
4339 ent->tls_type = tls_type;
4340 ent->is_indirect = FALSE;
4341 ent->got.refcount = 0;
4342 local_got_ents[r_symndx] = ent;
4344 ent->got.refcount += 1;
4347 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4348 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4349 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4351 return local_plt + r_symndx;
4355 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4357 struct plt_entry *ent;
4359 for (ent = *plist; ent != NULL; ent = ent->next)
4360 if (ent->addend == addend)
4364 bfd_size_type amt = sizeof (*ent);
4365 ent = bfd_alloc (abfd, amt);
4369 ent->addend = addend;
4370 ent->plt.refcount = 0;
4373 ent->plt.refcount += 1;
4378 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4380 return (r_type == R_PPC64_REL24
4381 || r_type == R_PPC64_REL24_NOTOC
4382 || r_type == R_PPC64_REL14
4383 || r_type == R_PPC64_REL14_BRTAKEN
4384 || r_type == R_PPC64_REL14_BRNTAKEN
4385 || r_type == R_PPC64_ADDR24
4386 || r_type == R_PPC64_ADDR14
4387 || r_type == R_PPC64_ADDR14_BRTAKEN
4388 || r_type == R_PPC64_ADDR14_BRNTAKEN
4389 || r_type == R_PPC64_PLTCALL
4390 || r_type == R_PPC64_PLTCALL_NOTOC);
4393 /* Relocs on inline plt call sequence insns prior to the call. */
4396 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4398 return (r_type == R_PPC64_PLT16_HA
4399 || r_type == R_PPC64_PLT16_HI
4400 || r_type == R_PPC64_PLT16_LO
4401 || r_type == R_PPC64_PLT16_LO_DS
4402 || r_type == R_PPC64_PLT_PCREL34
4403 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4404 || r_type == R_PPC64_PLTSEQ
4405 || r_type == R_PPC64_PLTSEQ_NOTOC);
4408 /* Look through the relocs for a section during the first phase, and
4409 calculate needed space in the global offset table, procedure
4410 linkage table, and dynamic reloc sections. */
4413 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4414 asection *sec, const Elf_Internal_Rela *relocs)
4416 struct ppc_link_hash_table *htab;
4417 Elf_Internal_Shdr *symtab_hdr;
4418 struct elf_link_hash_entry **sym_hashes;
4419 const Elf_Internal_Rela *rel;
4420 const Elf_Internal_Rela *rel_end;
4422 struct elf_link_hash_entry *tga, *dottga;
4425 if (bfd_link_relocatable (info))
4428 /* Don't do anything special with non-loaded, non-alloced sections.
4429 In particular, any relocs in such sections should not affect GOT
4430 and PLT reference counting (ie. we don't allow them to create GOT
4431 or PLT entries), there's no possibility or desire to optimize TLS
4432 relocs, and there's not much point in propagating relocs to shared
4433 libs that the dynamic linker won't relocate. */
4434 if ((sec->flags & SEC_ALLOC) == 0)
4437 BFD_ASSERT (is_ppc64_elf (abfd));
4439 htab = ppc_hash_table (info);
4443 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4444 FALSE, FALSE, TRUE);
4445 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4446 FALSE, FALSE, TRUE);
4447 symtab_hdr = &elf_symtab_hdr (abfd);
4448 sym_hashes = elf_sym_hashes (abfd);
4450 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4451 rel_end = relocs + sec->reloc_count;
4452 for (rel = relocs; rel < rel_end; rel++)
4454 unsigned long r_symndx;
4455 struct elf_link_hash_entry *h;
4456 enum elf_ppc64_reloc_type r_type;
4458 struct _ppc64_elf_section_data *ppc64_sec;
4459 struct plt_entry **ifunc, **plt_list;
4462 r_symndx = ELF64_R_SYM (rel->r_info);
4463 if (r_symndx < symtab_hdr->sh_info)
4467 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4468 h = elf_follow_link (h);
4470 if (h == htab->elf.hgot)
4471 sec->has_toc_reloc = 1;
4476 r_type = ELF64_R_TYPE (rel->r_info);
4480 /* Somewhat foolishly, because the ABIs don't specifically
4481 allow it, ppc64 gas and ld support GOT and PLT relocs
4482 with non-zero addends where the addend results in
4483 sym+addend being stored in the GOT or PLT entry. This
4484 can't be supported for pcrel relocs because the addend is
4485 used to specify the pcrel offset. */
4486 sym_addend = rel->r_addend;
4488 case R_PPC64_GOT_PCREL34:
4489 case R_PPC64_PLT_PCREL34:
4490 case R_PPC64_PLT_PCREL34_NOTOC:
4496 if (h->type == STT_GNU_IFUNC)
4499 ifunc = &h->plt.plist;
4504 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4509 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4511 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4513 NON_GOT | PLT_IFUNC);
4523 /* These special tls relocs tie a call to __tls_get_addr with
4524 its parameter symbol. */
4526 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4528 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4530 NON_GOT | TLS_TLS | TLS_MARK))
4532 sec->has_tls_reloc = 1;
4535 case R_PPC64_GOT_TLSLD16:
4536 case R_PPC64_GOT_TLSLD16_LO:
4537 case R_PPC64_GOT_TLSLD16_HI:
4538 case R_PPC64_GOT_TLSLD16_HA:
4539 tls_type = TLS_TLS | TLS_LD;
4542 case R_PPC64_GOT_TLSGD16:
4543 case R_PPC64_GOT_TLSGD16_LO:
4544 case R_PPC64_GOT_TLSGD16_HI:
4545 case R_PPC64_GOT_TLSGD16_HA:
4546 tls_type = TLS_TLS | TLS_GD;
4549 case R_PPC64_GOT_TPREL16_DS:
4550 case R_PPC64_GOT_TPREL16_LO_DS:
4551 case R_PPC64_GOT_TPREL16_HI:
4552 case R_PPC64_GOT_TPREL16_HA:
4553 if (bfd_link_dll (info))
4554 info->flags |= DF_STATIC_TLS;
4555 tls_type = TLS_TLS | TLS_TPREL;
4558 case R_PPC64_GOT_DTPREL16_DS:
4559 case R_PPC64_GOT_DTPREL16_LO_DS:
4560 case R_PPC64_GOT_DTPREL16_HI:
4561 case R_PPC64_GOT_DTPREL16_HA:
4562 tls_type = TLS_TLS | TLS_DTPREL;
4564 sec->has_tls_reloc = 1;
4567 case R_PPC64_GOT16_DS:
4568 case R_PPC64_GOT16_HA:
4569 case R_PPC64_GOT16_LO_DS:
4570 case R_PPC64_GOT_PCREL34:
4571 ppc64_elf_tdata (abfd)->has_gotrel = 1;
4572 ppc64_elf_section_data (sec)->has_gotrel = 1;
4576 case R_PPC64_GOT16_HI:
4577 case R_PPC64_GOT16_LO:
4579 /* This symbol requires a global offset table entry. */
4580 sec->has_toc_reloc = 1;
4581 if (r_type == R_PPC64_GOT_TLSLD16
4582 || r_type == R_PPC64_GOT_TLSGD16
4583 || r_type == R_PPC64_GOT_TPREL16_DS
4584 || r_type == R_PPC64_GOT_DTPREL16_DS
4585 || r_type == R_PPC64_GOT16
4586 || r_type == R_PPC64_GOT16_DS)
4588 htab->do_multi_toc = 1;
4589 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4592 if (ppc64_elf_tdata (abfd)->got == NULL
4593 && !create_got_section (abfd, info))
4598 struct ppc_link_hash_entry *eh;
4599 struct got_entry *ent;
4601 eh = (struct ppc_link_hash_entry *) h;
4602 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4603 if (ent->addend == sym_addend
4604 && ent->owner == abfd
4605 && ent->tls_type == tls_type)
4609 bfd_size_type amt = sizeof (*ent);
4610 ent = bfd_alloc (abfd, amt);
4613 ent->next = eh->elf.got.glist;
4614 ent->addend = sym_addend;
4616 ent->tls_type = tls_type;
4617 ent->is_indirect = FALSE;
4618 ent->got.refcount = 0;
4619 eh->elf.got.glist = ent;
4621 ent->got.refcount += 1;
4622 eh->tls_mask |= tls_type;
4625 /* This is a global offset table entry for a local symbol. */
4626 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4627 sym_addend, tls_type))
4630 /* We may also need a plt entry if the symbol turns out to be
4632 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
4634 if (!update_plt_info (abfd, &h->plt.plist, sym_addend))
4639 case R_PPC64_PLT16_HA:
4640 case R_PPC64_PLT16_HI:
4641 case R_PPC64_PLT16_LO:
4642 case R_PPC64_PLT16_LO_DS:
4643 case R_PPC64_PLT_PCREL34:
4644 case R_PPC64_PLT_PCREL34_NOTOC:
4647 /* This symbol requires a procedure linkage table entry. */
4652 if (h->root.root.string[0] == '.'
4653 && h->root.root.string[1] != '\0')
4654 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4655 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
4656 plt_list = &h->plt.plist;
4658 if (plt_list == NULL)
4659 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4661 NON_GOT | PLT_KEEP);
4662 if (!update_plt_info (abfd, plt_list, sym_addend))
4666 /* The following relocations don't need to propagate the
4667 relocation if linking a shared object since they are
4668 section relative. */
4669 case R_PPC64_SECTOFF:
4670 case R_PPC64_SECTOFF_LO:
4671 case R_PPC64_SECTOFF_HI:
4672 case R_PPC64_SECTOFF_HA:
4673 case R_PPC64_SECTOFF_DS:
4674 case R_PPC64_SECTOFF_LO_DS:
4675 case R_PPC64_DTPREL16:
4676 case R_PPC64_DTPREL16_LO:
4677 case R_PPC64_DTPREL16_HI:
4678 case R_PPC64_DTPREL16_HA:
4679 case R_PPC64_DTPREL16_DS:
4680 case R_PPC64_DTPREL16_LO_DS:
4681 case R_PPC64_DTPREL16_HIGH:
4682 case R_PPC64_DTPREL16_HIGHA:
4683 case R_PPC64_DTPREL16_HIGHER:
4684 case R_PPC64_DTPREL16_HIGHERA:
4685 case R_PPC64_DTPREL16_HIGHEST:
4686 case R_PPC64_DTPREL16_HIGHESTA:
4691 case R_PPC64_REL16_LO:
4692 case R_PPC64_REL16_HI:
4693 case R_PPC64_REL16_HA:
4694 case R_PPC64_REL16_HIGH:
4695 case R_PPC64_REL16_HIGHA:
4696 case R_PPC64_REL16_HIGHER:
4697 case R_PPC64_REL16_HIGHERA:
4698 case R_PPC64_REL16_HIGHEST:
4699 case R_PPC64_REL16_HIGHESTA:
4700 case R_PPC64_REL16_HIGHER34:
4701 case R_PPC64_REL16_HIGHERA34:
4702 case R_PPC64_REL16_HIGHEST34:
4703 case R_PPC64_REL16_HIGHESTA34:
4704 case R_PPC64_REL16DX_HA:
4707 /* Not supported as a dynamic relocation. */
4708 case R_PPC64_ADDR64_LOCAL:
4709 if (bfd_link_pic (info))
4711 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4713 /* xgettext:c-format */
4714 info->callbacks->einfo (_("%H: %s reloc unsupported "
4715 "in shared libraries and PIEs\n"),
4716 abfd, sec, rel->r_offset,
4717 ppc64_elf_howto_table[r_type]->name);
4718 bfd_set_error (bfd_error_bad_value);
4724 case R_PPC64_TOC16_DS:
4725 htab->do_multi_toc = 1;
4726 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4728 case R_PPC64_TOC16_LO:
4729 case R_PPC64_TOC16_HI:
4730 case R_PPC64_TOC16_HA:
4731 case R_PPC64_TOC16_LO_DS:
4732 sec->has_toc_reloc = 1;
4739 /* This relocation describes the C++ object vtable hierarchy.
4740 Reconstruct it for later use during GC. */
4741 case R_PPC64_GNU_VTINHERIT:
4742 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4746 /* This relocation describes which C++ vtable entries are actually
4747 used. Record for later use during GC. */
4748 case R_PPC64_GNU_VTENTRY:
4749 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4754 case R_PPC64_REL14_BRTAKEN:
4755 case R_PPC64_REL14_BRNTAKEN:
4757 asection *dest = NULL;
4759 /* Heuristic: If jumping outside our section, chances are
4760 we are going to need a stub. */
4763 /* If the sym is weak it may be overridden later, so
4764 don't assume we know where a weak sym lives. */
4765 if (h->root.type == bfd_link_hash_defined)
4766 dest = h->root.u.def.section;
4770 Elf_Internal_Sym *isym;
4772 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4777 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4781 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
4785 case R_PPC64_PLTCALL:
4786 case R_PPC64_PLTCALL_NOTOC:
4787 ppc64_elf_section_data (sec)->has_pltcall = 1;
4791 case R_PPC64_REL24_NOTOC:
4797 if (h->root.root.string[0] == '.'
4798 && h->root.root.string[1] != '\0')
4799 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4801 if (h == tga || h == dottga)
4803 sec->has_tls_reloc = 1;
4805 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4806 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4807 /* We have a new-style __tls_get_addr call with
4811 /* Mark this section as having an old-style call. */
4812 sec->has_tls_get_addr_call = 1;
4814 plt_list = &h->plt.plist;
4817 /* We may need a .plt entry if the function this reloc
4818 refers to is in a shared lib. */
4820 && !update_plt_info (abfd, plt_list, sym_addend))
4824 case R_PPC64_ADDR14:
4825 case R_PPC64_ADDR14_BRNTAKEN:
4826 case R_PPC64_ADDR14_BRTAKEN:
4827 case R_PPC64_ADDR24:
4830 case R_PPC64_TPREL64:
4831 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4832 if (bfd_link_dll (info))
4833 info->flags |= DF_STATIC_TLS;
4836 case R_PPC64_DTPMOD64:
4837 if (rel + 1 < rel_end
4838 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4839 && rel[1].r_offset == rel->r_offset + 8)
4840 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4842 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4845 case R_PPC64_DTPREL64:
4846 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4848 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4849 && rel[-1].r_offset == rel->r_offset - 8)
4850 /* This is the second reloc of a dtpmod, dtprel pair.
4851 Don't mark with TLS_DTPREL. */
4855 sec->has_tls_reloc = 1;
4858 struct ppc_link_hash_entry *eh;
4859 eh = (struct ppc_link_hash_entry *) h;
4860 eh->tls_mask |= tls_type;
4863 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4864 sym_addend, tls_type))
4867 ppc64_sec = ppc64_elf_section_data (sec);
4868 if (ppc64_sec->sec_type != sec_toc)
4872 /* One extra to simplify get_tls_mask. */
4873 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4874 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4875 if (ppc64_sec->u.toc.symndx == NULL)
4877 amt = sec->size * sizeof (bfd_vma) / 8;
4878 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4879 if (ppc64_sec->u.toc.add == NULL)
4881 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4882 ppc64_sec->sec_type = sec_toc;
4884 BFD_ASSERT (rel->r_offset % 8 == 0);
4885 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
4886 ppc64_sec->u.toc.add[rel->r_offset / 8] = sym_addend;
4888 /* Mark the second slot of a GD or LD entry.
4889 -1 to indicate GD and -2 to indicate LD. */
4890 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
4891 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
4892 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
4893 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
4896 case R_PPC64_TPREL16:
4897 case R_PPC64_TPREL16_LO:
4898 case R_PPC64_TPREL16_HI:
4899 case R_PPC64_TPREL16_HA:
4900 case R_PPC64_TPREL16_DS:
4901 case R_PPC64_TPREL16_LO_DS:
4902 case R_PPC64_TPREL16_HIGH:
4903 case R_PPC64_TPREL16_HIGHA:
4904 case R_PPC64_TPREL16_HIGHER:
4905 case R_PPC64_TPREL16_HIGHERA:
4906 case R_PPC64_TPREL16_HIGHEST:
4907 case R_PPC64_TPREL16_HIGHESTA:
4908 if (bfd_link_dll (info))
4909 info->flags |= DF_STATIC_TLS;
4912 case R_PPC64_ADDR64:
4914 && rel + 1 < rel_end
4915 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
4918 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4922 case R_PPC64_ADDR16:
4923 case R_PPC64_ADDR16_DS:
4924 case R_PPC64_ADDR16_HA:
4925 case R_PPC64_ADDR16_HI:
4926 case R_PPC64_ADDR16_HIGH:
4927 case R_PPC64_ADDR16_HIGHA:
4928 case R_PPC64_ADDR16_HIGHER:
4929 case R_PPC64_ADDR16_HIGHERA:
4930 case R_PPC64_ADDR16_HIGHEST:
4931 case R_PPC64_ADDR16_HIGHESTA:
4932 case R_PPC64_ADDR16_LO:
4933 case R_PPC64_ADDR16_LO_DS:
4935 case R_PPC64_D34_LO:
4936 case R_PPC64_D34_HI30:
4937 case R_PPC64_D34_HA30:
4938 case R_PPC64_ADDR16_HIGHER34:
4939 case R_PPC64_ADDR16_HIGHERA34:
4940 case R_PPC64_ADDR16_HIGHEST34:
4941 case R_PPC64_ADDR16_HIGHESTA34:
4943 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
4944 && rel->r_addend == 0)
4946 /* We may need a .plt entry if this reloc refers to a
4947 function in a shared lib. */
4948 if (!update_plt_info (abfd, &h->plt.plist, 0))
4950 h->pointer_equality_needed = 1;
4957 case R_PPC64_ADDR32:
4958 case R_PPC64_UADDR16:
4959 case R_PPC64_UADDR32:
4960 case R_PPC64_UADDR64:
4962 if (h != NULL && !bfd_link_pic (info))
4963 /* We may need a copy reloc. */
4966 /* Don't propagate .opd relocs. */
4967 if (NO_OPD_RELOCS && is_opd)
4970 /* If we are creating a shared library, and this is a reloc
4971 against a global symbol, or a non PC relative reloc
4972 against a local symbol, then we need to copy the reloc
4973 into the shared library. However, if we are linking with
4974 -Bsymbolic, we do not need to copy a reloc against a
4975 global symbol which is defined in an object we are
4976 including in the link (i.e., DEF_REGULAR is set). At
4977 this point we have not seen all the input files, so it is
4978 possible that DEF_REGULAR is not set now but will be set
4979 later (it is never cleared). In case of a weak definition,
4980 DEF_REGULAR may be cleared later by a strong definition in
4981 a shared library. We account for that possibility below by
4982 storing information in the dyn_relocs field of the hash
4983 table entry. A similar situation occurs when creating
4984 shared libraries and symbol visibility changes render the
4987 If on the other hand, we are creating an executable, we
4988 may need to keep relocations for symbols satisfied by a
4989 dynamic library if we manage to avoid copy relocs for the
4992 if ((bfd_link_pic (info)
4993 && (must_be_dyn_reloc (info, r_type)
4995 && (!SYMBOLIC_BIND (info, h)
4996 || h->root.type == bfd_link_hash_defweak
4997 || !h->def_regular))))
4998 || (ELIMINATE_COPY_RELOCS
4999 && !bfd_link_pic (info)
5001 && (h->root.type == bfd_link_hash_defweak
5002 || !h->def_regular))
5003 || (!bfd_link_pic (info)
5006 /* We must copy these reloc types into the output file.
5007 Create a reloc section in dynobj and make room for
5011 sreloc = _bfd_elf_make_dynamic_reloc_section
5012 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5018 /* If this is a global symbol, we count the number of
5019 relocations we need for this symbol. */
5022 struct elf_dyn_relocs *p;
5023 struct elf_dyn_relocs **head;
5025 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5027 if (p == NULL || p->sec != sec)
5029 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5039 if (!must_be_dyn_reloc (info, r_type))
5044 /* Track dynamic relocs needed for local syms too.
5045 We really need local syms available to do this
5047 struct ppc_dyn_relocs *p;
5048 struct ppc_dyn_relocs **head;
5049 bfd_boolean is_ifunc;
5052 Elf_Internal_Sym *isym;
5054 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5059 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5063 vpp = &elf_section_data (s)->local_dynrel;
5064 head = (struct ppc_dyn_relocs **) vpp;
5065 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5067 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5069 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5071 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5077 p->ifunc = is_ifunc;
5093 /* Merge backend specific data from an object file to the output
5094 object file when linking. */
5097 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5099 bfd *obfd = info->output_bfd;
5100 unsigned long iflags, oflags;
5102 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5105 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5108 if (!_bfd_generic_verify_endian_match (ibfd, info))
5111 iflags = elf_elfheader (ibfd)->e_flags;
5112 oflags = elf_elfheader (obfd)->e_flags;
5114 if (iflags & ~EF_PPC64_ABI)
5117 /* xgettext:c-format */
5118 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5119 bfd_set_error (bfd_error_bad_value);
5122 else if (iflags != oflags && iflags != 0)
5125 /* xgettext:c-format */
5126 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5127 ibfd, iflags, oflags);
5128 bfd_set_error (bfd_error_bad_value);
5132 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5135 /* Merge Tag_compatibility attributes and any common GNU ones. */
5136 return _bfd_elf_merge_object_attributes (ibfd, info);
5140 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5142 /* Print normal ELF private data. */
5143 _bfd_elf_print_private_bfd_data (abfd, ptr);
5145 if (elf_elfheader (abfd)->e_flags != 0)
5149 fprintf (file, _("private flags = 0x%lx:"),
5150 elf_elfheader (abfd)->e_flags);
5152 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5153 fprintf (file, _(" [abiv%ld]"),
5154 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5161 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5162 of the code entry point, and its section, which must be in the same
5163 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
5166 opd_entry_value (asection *opd_sec,
5168 asection **code_sec,
5170 bfd_boolean in_code_sec)
5172 bfd *opd_bfd = opd_sec->owner;
5173 Elf_Internal_Rela *relocs;
5174 Elf_Internal_Rela *lo, *hi, *look;
5177 /* No relocs implies we are linking a --just-symbols object, or looking
5178 at a final linked executable with addr2line or somesuch. */
5179 if (opd_sec->reloc_count == 0)
5181 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5183 if (contents == NULL)
5185 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5186 return (bfd_vma) -1;
5187 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5190 /* PR 17512: file: 64b9dfbb. */
5191 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5192 return (bfd_vma) -1;
5194 val = bfd_get_64 (opd_bfd, contents + offset);
5195 if (code_sec != NULL)
5197 asection *sec, *likely = NULL;
5203 && val < sec->vma + sec->size)
5209 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5211 && (sec->flags & SEC_LOAD) != 0
5212 && (sec->flags & SEC_ALLOC) != 0)
5217 if (code_off != NULL)
5218 *code_off = val - likely->vma;
5224 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5226 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5228 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5229 /* PR 17512: file: df8e1fd6. */
5231 return (bfd_vma) -1;
5233 /* Go find the opd reloc at the sym address. */
5235 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5239 look = lo + (hi - lo) / 2;
5240 if (look->r_offset < offset)
5242 else if (look->r_offset > offset)
5246 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5248 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5249 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5251 unsigned long symndx = ELF64_R_SYM (look->r_info);
5252 asection *sec = NULL;
5254 if (symndx >= symtab_hdr->sh_info
5255 && elf_sym_hashes (opd_bfd) != NULL)
5257 struct elf_link_hash_entry **sym_hashes;
5258 struct elf_link_hash_entry *rh;
5260 sym_hashes = elf_sym_hashes (opd_bfd);
5261 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5264 rh = elf_follow_link (rh);
5265 if (rh->root.type != bfd_link_hash_defined
5266 && rh->root.type != bfd_link_hash_defweak)
5268 if (rh->root.u.def.section->owner == opd_bfd)
5270 val = rh->root.u.def.value;
5271 sec = rh->root.u.def.section;
5278 Elf_Internal_Sym *sym;
5280 if (symndx < symtab_hdr->sh_info)
5282 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5285 size_t symcnt = symtab_hdr->sh_info;
5286 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5291 symtab_hdr->contents = (bfd_byte *) sym;
5297 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5303 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5306 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5307 val = sym->st_value;
5310 val += look->r_addend;
5311 if (code_off != NULL)
5313 if (code_sec != NULL)
5315 if (in_code_sec && *code_sec != sec)
5320 if (sec->output_section != NULL)
5321 val += sec->output_section->vma + sec->output_offset;
5330 /* If the ELF symbol SYM might be a function in SEC, return the
5331 function size and set *CODE_OFF to the function's entry point,
5332 otherwise return zero. */
5334 static bfd_size_type
5335 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5340 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5341 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5345 if (!(sym->flags & BSF_SYNTHETIC))
5346 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5348 if (strcmp (sym->section->name, ".opd") == 0)
5350 struct _opd_sec_data *opd = get_opd_info (sym->section);
5351 bfd_vma symval = sym->value;
5354 && opd->adjust != NULL
5355 && elf_section_data (sym->section)->relocs != NULL)
5357 /* opd_entry_value will use cached relocs that have been
5358 adjusted, but with raw symbols. That means both local
5359 and global symbols need adjusting. */
5360 long adjust = opd->adjust[OPD_NDX (symval)];
5366 if (opd_entry_value (sym->section, symval,
5367 &sec, code_off, TRUE) == (bfd_vma) -1)
5369 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5370 symbol. This size has nothing to do with the code size of the
5371 function, which is what we're supposed to return, but the
5372 code size isn't available without looking up the dot-sym.
5373 However, doing that would be a waste of time particularly
5374 since elf_find_function will look at the dot-sym anyway.
5375 Now, elf_find_function will keep the largest size of any
5376 function sym found at the code address of interest, so return
5377 1 here to avoid it incorrectly caching a larger function size
5378 for a small function. This does mean we return the wrong
5379 size for a new-ABI function of size 24, but all that does is
5380 disable caching for such functions. */
5386 if (sym->section != sec)
5388 *code_off = sym->value;
5395 /* Return true if symbol is a strong function defined in an ELFv2
5396 object with st_other localentry bits of zero, ie. its local entry
5397 point coincides with its global entry point. */
5400 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5403 && h->type == STT_FUNC
5404 && h->root.type == bfd_link_hash_defined
5405 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5406 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
5407 && is_ppc64_elf (h->root.u.def.section->owner)
5408 && abiversion (h->root.u.def.section->owner) >= 2);
5411 /* Return true if symbol is defined in a regular object file. */
5414 is_static_defined (struct elf_link_hash_entry *h)
5416 return ((h->root.type == bfd_link_hash_defined
5417 || h->root.type == bfd_link_hash_defweak)
5418 && h->root.u.def.section != NULL
5419 && h->root.u.def.section->output_section != NULL);
5422 /* If FDH is a function descriptor symbol, return the associated code
5423 entry symbol if it is defined. Return NULL otherwise. */
5425 static struct ppc_link_hash_entry *
5426 defined_code_entry (struct ppc_link_hash_entry *fdh)
5428 if (fdh->is_func_descriptor)
5430 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5431 if (fh->elf.root.type == bfd_link_hash_defined
5432 || fh->elf.root.type == bfd_link_hash_defweak)
5438 /* If FH is a function code entry symbol, return the associated
5439 function descriptor symbol if it is defined. Return NULL otherwise. */
5441 static struct ppc_link_hash_entry *
5442 defined_func_desc (struct ppc_link_hash_entry *fh)
5445 && fh->oh->is_func_descriptor)
5447 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5448 if (fdh->elf.root.type == bfd_link_hash_defined
5449 || fdh->elf.root.type == bfd_link_hash_defweak)
5455 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5457 /* Garbage collect sections, after first dealing with dot-symbols. */
5460 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5462 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5464 if (htab != NULL && htab->need_func_desc_adj)
5466 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5467 htab->need_func_desc_adj = 0;
5469 return bfd_elf_gc_sections (abfd, info);
5472 /* Mark all our entry sym sections, both opd and code section. */
5475 ppc64_elf_gc_keep (struct bfd_link_info *info)
5477 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5478 struct bfd_sym_chain *sym;
5483 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5485 struct ppc_link_hash_entry *eh, *fh;
5488 eh = (struct ppc_link_hash_entry *)
5489 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5492 if (eh->elf.root.type != bfd_link_hash_defined
5493 && eh->elf.root.type != bfd_link_hash_defweak)
5496 fh = defined_code_entry (eh);
5499 sec = fh->elf.root.u.def.section;
5500 sec->flags |= SEC_KEEP;
5502 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5503 && opd_entry_value (eh->elf.root.u.def.section,
5504 eh->elf.root.u.def.value,
5505 &sec, NULL, FALSE) != (bfd_vma) -1)
5506 sec->flags |= SEC_KEEP;
5508 sec = eh->elf.root.u.def.section;
5509 sec->flags |= SEC_KEEP;
5513 /* Mark sections containing dynamically referenced symbols. When
5514 building shared libraries, we must assume that any visible symbol is
5518 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5520 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5521 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5522 struct ppc_link_hash_entry *fdh;
5523 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5525 /* Dynamic linking info is on the func descriptor sym. */
5526 fdh = defined_func_desc (eh);
5530 if ((eh->elf.root.type == bfd_link_hash_defined
5531 || eh->elf.root.type == bfd_link_hash_defweak)
5532 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5533 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5534 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5535 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5536 && (!bfd_link_executable (info)
5537 || info->gc_keep_exported
5538 || info->export_dynamic
5541 && (*d->match) (&d->head, NULL,
5542 eh->elf.root.root.string)))
5543 && (eh->elf.versioned >= versioned
5544 || !bfd_hide_sym_by_version (info->version_info,
5545 eh->elf.root.root.string)))))
5548 struct ppc_link_hash_entry *fh;
5550 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5552 /* Function descriptor syms cause the associated
5553 function code sym section to be marked. */
5554 fh = defined_code_entry (eh);
5557 code_sec = fh->elf.root.u.def.section;
5558 code_sec->flags |= SEC_KEEP;
5560 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5561 && opd_entry_value (eh->elf.root.u.def.section,
5562 eh->elf.root.u.def.value,
5563 &code_sec, NULL, FALSE) != (bfd_vma) -1)
5564 code_sec->flags |= SEC_KEEP;
5570 /* Return the section that should be marked against GC for a given
5574 ppc64_elf_gc_mark_hook (asection *sec,
5575 struct bfd_link_info *info,
5576 Elf_Internal_Rela *rel,
5577 struct elf_link_hash_entry *h,
5578 Elf_Internal_Sym *sym)
5582 /* Syms return NULL if we're marking .opd, so we avoid marking all
5583 function sections, as all functions are referenced in .opd. */
5585 if (get_opd_info (sec) != NULL)
5590 enum elf_ppc64_reloc_type r_type;
5591 struct ppc_link_hash_entry *eh, *fh, *fdh;
5593 r_type = ELF64_R_TYPE (rel->r_info);
5596 case R_PPC64_GNU_VTINHERIT:
5597 case R_PPC64_GNU_VTENTRY:
5601 switch (h->root.type)
5603 case bfd_link_hash_defined:
5604 case bfd_link_hash_defweak:
5605 eh = (struct ppc_link_hash_entry *) h;
5606 fdh = defined_func_desc (eh);
5609 /* -mcall-aixdesc code references the dot-symbol on
5610 a call reloc. Mark the function descriptor too
5611 against garbage collection. */
5613 if (fdh->elf.is_weakalias)
5614 weakdef (&fdh->elf)->mark = 1;
5618 /* Function descriptor syms cause the associated
5619 function code sym section to be marked. */
5620 fh = defined_code_entry (eh);
5623 /* They also mark their opd section. */
5624 eh->elf.root.u.def.section->gc_mark = 1;
5626 rsec = fh->elf.root.u.def.section;
5628 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5629 && opd_entry_value (eh->elf.root.u.def.section,
5630 eh->elf.root.u.def.value,
5631 &rsec, NULL, FALSE) != (bfd_vma) -1)
5632 eh->elf.root.u.def.section->gc_mark = 1;
5634 rsec = h->root.u.def.section;
5637 case bfd_link_hash_common:
5638 rsec = h->root.u.c.p->section;
5642 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5648 struct _opd_sec_data *opd;
5650 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5651 opd = get_opd_info (rsec);
5652 if (opd != NULL && opd->func_sec != NULL)
5656 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
5663 /* The maximum size of .sfpr. */
5664 #define SFPR_MAX (218*4)
5666 struct sfpr_def_parms
5668 const char name[12];
5669 unsigned char lo, hi;
5670 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5671 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
5674 /* Auto-generate _save*, _rest* functions in .sfpr.
5675 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5679 sfpr_define (struct bfd_link_info *info,
5680 const struct sfpr_def_parms *parm,
5683 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5685 size_t len = strlen (parm->name);
5686 bfd_boolean writing = FALSE;
5692 memcpy (sym, parm->name, len);
5695 for (i = parm->lo; i <= parm->hi; i++)
5697 struct ppc_link_hash_entry *h;
5699 sym[len + 0] = i / 10 + '0';
5700 sym[len + 1] = i % 10 + '0';
5701 h = (struct ppc_link_hash_entry *)
5702 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
5703 if (stub_sec != NULL)
5706 && h->elf.root.type == bfd_link_hash_defined
5707 && h->elf.root.u.def.section == htab->sfpr)
5709 struct elf_link_hash_entry *s;
5711 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5712 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5715 if (s->root.type == bfd_link_hash_new
5716 || (s->root.type = bfd_link_hash_defined
5717 && s->root.u.def.section == stub_sec))
5719 s->root.type = bfd_link_hash_defined;
5720 s->root.u.def.section = stub_sec;
5721 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
5722 + h->elf.root.u.def.value);
5725 s->ref_regular_nonweak = 1;
5726 s->forced_local = 1;
5728 s->root.linker_def = 1;
5736 if (!h->elf.def_regular)
5738 h->elf.root.type = bfd_link_hash_defined;
5739 h->elf.root.u.def.section = htab->sfpr;
5740 h->elf.root.u.def.value = htab->sfpr->size;
5741 h->elf.type = STT_FUNC;
5742 h->elf.def_regular = 1;
5744 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5746 if (htab->sfpr->contents == NULL)
5748 htab->sfpr->contents
5749 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5750 if (htab->sfpr->contents == NULL)
5757 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5759 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5761 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5762 htab->sfpr->size = p - htab->sfpr->contents;
5770 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5772 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5777 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5779 p = savegpr0 (abfd, p, r);
5780 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5782 bfd_put_32 (abfd, BLR, p);
5787 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5789 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5794 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5796 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5798 p = restgpr0 (abfd, p, r);
5799 bfd_put_32 (abfd, MTLR_R0, p);
5803 p = restgpr0 (abfd, p, 30);
5804 p = restgpr0 (abfd, p, 31);
5806 bfd_put_32 (abfd, BLR, p);
5811 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5813 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5818 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5820 p = savegpr1 (abfd, p, r);
5821 bfd_put_32 (abfd, BLR, p);
5826 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5828 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5833 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5835 p = restgpr1 (abfd, p, r);
5836 bfd_put_32 (abfd, BLR, p);
5841 savefpr (bfd *abfd, bfd_byte *p, int r)
5843 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5848 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5850 p = savefpr (abfd, p, r);
5851 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5853 bfd_put_32 (abfd, BLR, p);
5858 restfpr (bfd *abfd, bfd_byte *p, int r)
5860 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5865 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5867 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5869 p = restfpr (abfd, p, r);
5870 bfd_put_32 (abfd, MTLR_R0, p);
5874 p = restfpr (abfd, p, 30);
5875 p = restfpr (abfd, p, 31);
5877 bfd_put_32 (abfd, BLR, p);
5882 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5884 p = savefpr (abfd, p, r);
5885 bfd_put_32 (abfd, BLR, p);
5890 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5892 p = restfpr (abfd, p, r);
5893 bfd_put_32 (abfd, BLR, p);
5898 savevr (bfd *abfd, bfd_byte *p, int r)
5900 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5902 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5907 savevr_tail (bfd *abfd, bfd_byte *p, int r)
5909 p = savevr (abfd, p, r);
5910 bfd_put_32 (abfd, BLR, p);
5915 restvr (bfd *abfd, bfd_byte *p, int r)
5917 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5919 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5924 restvr_tail (bfd *abfd, bfd_byte *p, int r)
5926 p = restvr (abfd, p, r);
5927 bfd_put_32 (abfd, BLR, p);
5931 /* Called via elf_link_hash_traverse to transfer dynamic linking
5932 information on function code symbol entries to their corresponding
5933 function descriptor symbol entries. */
5936 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5938 struct bfd_link_info *info;
5939 struct ppc_link_hash_table *htab;
5940 struct ppc_link_hash_entry *fh;
5941 struct ppc_link_hash_entry *fdh;
5942 bfd_boolean force_local;
5944 fh = (struct ppc_link_hash_entry *) h;
5945 if (fh->elf.root.type == bfd_link_hash_indirect)
5951 if (fh->elf.root.root.string[0] != '.'
5952 || fh->elf.root.root.string[1] == '\0')
5956 htab = ppc_hash_table (info);
5960 /* Find the corresponding function descriptor symbol. */
5961 fdh = lookup_fdh (fh, htab);
5963 /* Resolve undefined references to dot-symbols as the value
5964 in the function descriptor, if we have one in a regular object.
5965 This is to satisfy cases like ".quad .foo". Calls to functions
5966 in dynamic objects are handled elsewhere. */
5967 if ((fh->elf.root.type == bfd_link_hash_undefined
5968 || fh->elf.root.type == bfd_link_hash_undefweak)
5969 && (fdh->elf.root.type == bfd_link_hash_defined
5970 || fdh->elf.root.type == bfd_link_hash_defweak)
5971 && get_opd_info (fdh->elf.root.u.def.section) != NULL
5972 && opd_entry_value (fdh->elf.root.u.def.section,
5973 fdh->elf.root.u.def.value,
5974 &fh->elf.root.u.def.section,
5975 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
5977 fh->elf.root.type = fdh->elf.root.type;
5978 fh->elf.forced_local = 1;
5979 fh->elf.def_regular = fdh->elf.def_regular;
5980 fh->elf.def_dynamic = fdh->elf.def_dynamic;
5983 if (!fh->elf.dynamic)
5985 struct plt_entry *ent;
5987 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
5988 if (ent->plt.refcount > 0)
5994 /* Create a descriptor as undefined if necessary. */
5996 && !bfd_link_executable (info)
5997 && (fh->elf.root.type == bfd_link_hash_undefined
5998 || fh->elf.root.type == bfd_link_hash_undefweak))
6000 fdh = make_fdh (info, fh);
6005 /* We can't support overriding of symbols on a fake descriptor. */
6008 && (fh->elf.root.type == bfd_link_hash_defined
6009 || fh->elf.root.type == bfd_link_hash_defweak))
6010 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6012 /* Transfer dynamic linking information to the function descriptor. */
6015 fdh->elf.ref_regular |= fh->elf.ref_regular;
6016 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6017 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6018 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6019 fdh->elf.dynamic |= fh->elf.dynamic;
6020 fdh->elf.needs_plt |= (fh->elf.needs_plt
6021 || fh->elf.type == STT_FUNC
6022 || fh->elf.type == STT_GNU_IFUNC);
6023 move_plt_plist (fh, fdh);
6025 if (!fdh->elf.forced_local
6026 && fh->elf.dynindx != -1)
6027 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6031 /* Now that the info is on the function descriptor, clear the
6032 function code sym info. Any function code syms for which we
6033 don't have a definition in a regular file, we force local.
6034 This prevents a shared library from exporting syms that have
6035 been imported from another library. Function code syms that
6036 are really in the library we must leave global to prevent the
6037 linker dragging in a definition from a static library. */
6038 force_local = (!fh->elf.def_regular
6040 || !fdh->elf.def_regular
6041 || fdh->elf.forced_local);
6042 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6047 static const struct sfpr_def_parms save_res_funcs[] =
6049 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6050 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6051 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6052 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6053 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6054 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6055 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6056 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6057 { "._savef", 14, 31, savefpr, savefpr1_tail },
6058 { "._restf", 14, 31, restfpr, restfpr1_tail },
6059 { "_savevr_", 20, 31, savevr, savevr_tail },
6060 { "_restvr_", 20, 31, restvr, restvr_tail }
6063 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6064 this hook to a) provide some gcc support functions, and b) transfer
6065 dynamic linking information gathered so far on function code symbol
6066 entries, to their corresponding function descriptor symbol entries. */
6069 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6070 struct bfd_link_info *info)
6072 struct ppc_link_hash_table *htab;
6074 htab = ppc_hash_table (info);
6078 /* Provide any missing _save* and _rest* functions. */
6079 if (htab->sfpr != NULL)
6083 htab->sfpr->size = 0;
6084 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6085 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6087 if (htab->sfpr->size == 0)
6088 htab->sfpr->flags |= SEC_EXCLUDE;
6091 if (bfd_link_relocatable (info))
6094 if (htab->elf.hgot != NULL)
6096 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6097 /* Make .TOC. defined so as to prevent it being made dynamic.
6098 The wrong value here is fixed later in ppc64_elf_set_toc. */
6099 if (!htab->elf.hgot->def_regular
6100 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6102 htab->elf.hgot->root.type = bfd_link_hash_defined;
6103 htab->elf.hgot->root.u.def.value = 0;
6104 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6105 htab->elf.hgot->def_regular = 1;
6106 htab->elf.hgot->root.linker_def = 1;
6108 htab->elf.hgot->type = STT_OBJECT;
6109 htab->elf.hgot->other
6110 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6113 if (htab->need_func_desc_adj)
6115 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6116 htab->need_func_desc_adj = 0;
6122 /* Find dynamic relocs for H that apply to read-only sections. */
6125 readonly_dynrelocs (struct elf_link_hash_entry *h)
6127 struct ppc_link_hash_entry *eh;
6128 struct elf_dyn_relocs *p;
6130 eh = (struct ppc_link_hash_entry *) h;
6131 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6133 asection *s = p->sec->output_section;
6135 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6141 /* Return true if we have dynamic relocs against H or any of its weak
6142 aliases, that apply to read-only sections. Cannot be used after
6143 size_dynamic_sections. */
6146 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6148 struct ppc_link_hash_entry *eh;
6150 eh = (struct ppc_link_hash_entry *) h;
6153 if (readonly_dynrelocs (&eh->elf))
6155 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
6157 while (eh != NULL && &eh->elf != h);
6162 /* Return whether EH has pc-relative dynamic relocs. */
6165 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6167 struct elf_dyn_relocs *p;
6169 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6170 if (p->pc_count != 0)
6175 /* Return true if a global entry stub will be created for H. Valid
6176 for ELFv2 before plt entries have been allocated. */
6179 global_entry_stub (struct elf_link_hash_entry *h)
6181 struct plt_entry *pent;
6183 if (!h->pointer_equality_needed
6187 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6188 if (pent->plt.refcount > 0
6189 && pent->addend == 0)
6195 /* Adjust a symbol defined by a dynamic object and referenced by a
6196 regular object. The current definition is in some section of the
6197 dynamic object, but we're not including those sections. We have to
6198 change the definition to something the rest of the link can
6202 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6203 struct elf_link_hash_entry *h)
6205 struct ppc_link_hash_table *htab;
6208 htab = ppc_hash_table (info);
6212 /* Deal with function syms. */
6213 if (h->type == STT_FUNC
6214 || h->type == STT_GNU_IFUNC
6217 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6218 || SYMBOL_CALLS_LOCAL (info, h)
6219 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6220 /* Discard dyn_relocs when non-pic if we've decided that a
6221 function symbol is local and not an ifunc. We keep dynamic
6222 relocs for ifuncs when local rather than always emitting a
6223 plt call stub for them and defining the symbol on the call
6224 stub. We can't do that for ELFv1 anyway (a function symbol
6225 is defined on a descriptor, not code) and it can be faster at
6226 run-time due to not needing to bounce through a stub. The
6227 dyn_relocs for ifuncs will be applied even in a static
6229 if (!bfd_link_pic (info)
6230 && h->type != STT_GNU_IFUNC
6232 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6234 /* Clear procedure linkage table information for any symbol that
6235 won't need a .plt entry. */
6236 struct plt_entry *ent;
6237 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6238 if (ent->plt.refcount > 0)
6241 || (h->type != STT_GNU_IFUNC
6243 && (htab->can_convert_all_inline_plt
6244 || (((struct ppc_link_hash_entry *) h)->tls_mask
6245 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6247 h->plt.plist = NULL;
6249 h->pointer_equality_needed = 0;
6251 else if (abiversion (info->output_bfd) >= 2)
6253 /* Taking a function's address in a read/write section
6254 doesn't require us to define the function symbol in the
6255 executable on a global entry stub. A dynamic reloc can
6256 be used instead. The reason we prefer a few more dynamic
6257 relocs is that calling via a global entry stub costs a
6258 few more instructions, and pointer_equality_needed causes
6259 extra work in ld.so when resolving these symbols. */
6260 if (global_entry_stub (h))
6262 if (!readonly_dynrelocs (h))
6264 h->pointer_equality_needed = 0;
6265 /* If we haven't seen a branch reloc and the symbol
6266 isn't an ifunc then we don't need a plt entry. */
6268 h->plt.plist = NULL;
6270 else if (!bfd_link_pic (info))
6271 /* We are going to be defining the function symbol on the
6272 plt stub, so no dyn_relocs needed when non-pic. */
6273 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6276 /* ELFv2 function symbols can't have copy relocs. */
6279 else if (!h->needs_plt
6280 && !readonly_dynrelocs (h))
6282 /* If we haven't seen a branch reloc and the symbol isn't an
6283 ifunc then we don't need a plt entry. */
6284 h->plt.plist = NULL;
6285 h->pointer_equality_needed = 0;
6290 h->plt.plist = NULL;
6292 /* If this is a weak symbol, and there is a real definition, the
6293 processor independent code will have arranged for us to see the
6294 real definition first, and we can just use the same value. */
6295 if (h->is_weakalias)
6297 struct elf_link_hash_entry *def = weakdef (h);
6298 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6299 h->root.u.def.section = def->root.u.def.section;
6300 h->root.u.def.value = def->root.u.def.value;
6301 if (def->root.u.def.section == htab->elf.sdynbss
6302 || def->root.u.def.section == htab->elf.sdynrelro)
6303 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6307 /* If we are creating a shared library, we must presume that the
6308 only references to the symbol are via the global offset table.
6309 For such cases we need not do anything here; the relocations will
6310 be handled correctly by relocate_section. */
6311 if (bfd_link_pic (info))
6314 /* If there are no references to this symbol that do not use the
6315 GOT, we don't need to generate a copy reloc. */
6316 if (!h->non_got_ref)
6319 /* Don't generate a copy reloc for symbols defined in the executable. */
6320 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6322 /* If -z nocopyreloc was given, don't generate them either. */
6323 || info->nocopyreloc
6325 /* If we don't find any dynamic relocs in read-only sections, then
6326 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6327 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
6329 /* Protected variables do not work with .dynbss. The copy in
6330 .dynbss won't be used by the shared library with the protected
6331 definition for the variable. Text relocations are preferable
6332 to an incorrect program. */
6333 || h->protected_def)
6336 if (h->plt.plist != NULL)
6338 /* We should never get here, but unfortunately there are versions
6339 of gcc out there that improperly (for this ABI) put initialized
6340 function pointers, vtable refs and suchlike in read-only
6341 sections. Allow them to proceed, but warn that this might
6342 break at runtime. */
6343 info->callbacks->einfo
6344 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6345 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6346 h->root.root.string);
6349 /* This is a reference to a symbol defined by a dynamic object which
6350 is not a function. */
6352 /* We must allocate the symbol in our .dynbss section, which will
6353 become part of the .bss section of the executable. There will be
6354 an entry for this symbol in the .dynsym section. The dynamic
6355 object will contain position independent code, so all references
6356 from the dynamic object to this symbol will go through the global
6357 offset table. The dynamic linker will use the .dynsym entry to
6358 determine the address it must put in the global offset table, so
6359 both the dynamic object and the regular object will refer to the
6360 same memory location for the variable. */
6361 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6363 s = htab->elf.sdynrelro;
6364 srel = htab->elf.sreldynrelro;
6368 s = htab->elf.sdynbss;
6369 srel = htab->elf.srelbss;
6371 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6373 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6374 linker to copy the initial value out of the dynamic object
6375 and into the runtime process image. */
6376 srel->size += sizeof (Elf64_External_Rela);
6380 /* We no longer want dyn_relocs. */
6381 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6382 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6385 /* If given a function descriptor symbol, hide both the function code
6386 sym and the descriptor. */
6388 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6389 struct elf_link_hash_entry *h,
6390 bfd_boolean force_local)
6392 struct ppc_link_hash_entry *eh;
6393 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6395 if (ppc_hash_table (info) == NULL)
6398 eh = (struct ppc_link_hash_entry *) h;
6399 if (eh->is_func_descriptor)
6401 struct ppc_link_hash_entry *fh = eh->oh;
6406 struct elf_link_hash_table *htab = elf_hash_table (info);
6409 /* We aren't supposed to use alloca in BFD because on
6410 systems which do not have alloca the version in libiberty
6411 calls xmalloc, which might cause the program to crash
6412 when it runs out of memory. This function doesn't have a
6413 return status, so there's no way to gracefully return an
6414 error. So cheat. We know that string[-1] can be safely
6415 accessed; It's either a string in an ELF string table,
6416 or allocated in an objalloc structure. */
6418 p = eh->elf.root.root.string - 1;
6421 fh = (struct ppc_link_hash_entry *)
6422 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6425 /* Unfortunately, if it so happens that the string we were
6426 looking for was allocated immediately before this string,
6427 then we overwrote the string terminator. That's the only
6428 reason the lookup should fail. */
6431 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6432 while (q >= eh->elf.root.root.string && *q == *p)
6434 if (q < eh->elf.root.root.string && *p == '.')
6435 fh = (struct ppc_link_hash_entry *)
6436 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6445 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6450 get_sym_h (struct elf_link_hash_entry **hp,
6451 Elf_Internal_Sym **symp,
6453 unsigned char **tls_maskp,
6454 Elf_Internal_Sym **locsymsp,
6455 unsigned long r_symndx,
6458 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6460 if (r_symndx >= symtab_hdr->sh_info)
6462 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6463 struct elf_link_hash_entry *h;
6465 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6466 h = elf_follow_link (h);
6474 if (symsecp != NULL)
6476 asection *symsec = NULL;
6477 if (h->root.type == bfd_link_hash_defined
6478 || h->root.type == bfd_link_hash_defweak)
6479 symsec = h->root.u.def.section;
6483 if (tls_maskp != NULL)
6485 struct ppc_link_hash_entry *eh;
6487 eh = (struct ppc_link_hash_entry *) h;
6488 *tls_maskp = &eh->tls_mask;
6493 Elf_Internal_Sym *sym;
6494 Elf_Internal_Sym *locsyms = *locsymsp;
6496 if (locsyms == NULL)
6498 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6499 if (locsyms == NULL)
6500 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6501 symtab_hdr->sh_info,
6502 0, NULL, NULL, NULL);
6503 if (locsyms == NULL)
6505 *locsymsp = locsyms;
6507 sym = locsyms + r_symndx;
6515 if (symsecp != NULL)
6516 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6518 if (tls_maskp != NULL)
6520 struct got_entry **lgot_ents;
6521 unsigned char *tls_mask;
6524 lgot_ents = elf_local_got_ents (ibfd);
6525 if (lgot_ents != NULL)
6527 struct plt_entry **local_plt = (struct plt_entry **)
6528 (lgot_ents + symtab_hdr->sh_info);
6529 unsigned char *lgot_masks = (unsigned char *)
6530 (local_plt + symtab_hdr->sh_info);
6531 tls_mask = &lgot_masks[r_symndx];
6533 *tls_maskp = tls_mask;
6539 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6540 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6541 type suitable for optimization, and 1 otherwise. */
6544 get_tls_mask (unsigned char **tls_maskp,
6545 unsigned long *toc_symndx,
6546 bfd_vma *toc_addend,
6547 Elf_Internal_Sym **locsymsp,
6548 const Elf_Internal_Rela *rel,
6551 unsigned long r_symndx;
6553 struct elf_link_hash_entry *h;
6554 Elf_Internal_Sym *sym;
6558 r_symndx = ELF64_R_SYM (rel->r_info);
6559 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6562 if ((*tls_maskp != NULL
6563 && (**tls_maskp & TLS_TLS) != 0
6564 && **tls_maskp != (TLS_TLS | TLS_MARK))
6566 || ppc64_elf_section_data (sec) == NULL
6567 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6570 /* Look inside a TOC section too. */
6573 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6574 off = h->root.u.def.value;
6577 off = sym->st_value;
6578 off += rel->r_addend;
6579 BFD_ASSERT (off % 8 == 0);
6580 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6581 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6582 if (toc_symndx != NULL)
6583 *toc_symndx = r_symndx;
6584 if (toc_addend != NULL)
6585 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6586 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6588 if ((h == NULL || is_static_defined (h))
6589 && (next_r == -1 || next_r == -2))
6594 /* Find (or create) an entry in the tocsave hash table. */
6596 static struct tocsave_entry *
6597 tocsave_find (struct ppc_link_hash_table *htab,
6598 enum insert_option insert,
6599 Elf_Internal_Sym **local_syms,
6600 const Elf_Internal_Rela *irela,
6603 unsigned long r_indx;
6604 struct elf_link_hash_entry *h;
6605 Elf_Internal_Sym *sym;
6606 struct tocsave_entry ent, *p;
6608 struct tocsave_entry **slot;
6610 r_indx = ELF64_R_SYM (irela->r_info);
6611 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6613 if (ent.sec == NULL || ent.sec->output_section == NULL)
6616 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
6621 ent.offset = h->root.u.def.value;
6623 ent.offset = sym->st_value;
6624 ent.offset += irela->r_addend;
6626 hash = tocsave_htab_hash (&ent);
6627 slot = ((struct tocsave_entry **)
6628 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6634 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6643 /* Adjust all global syms defined in opd sections. In gcc generated
6644 code for the old ABI, these will already have been done. */
6647 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6649 struct ppc_link_hash_entry *eh;
6651 struct _opd_sec_data *opd;
6653 if (h->root.type == bfd_link_hash_indirect)
6656 if (h->root.type != bfd_link_hash_defined
6657 && h->root.type != bfd_link_hash_defweak)
6660 eh = (struct ppc_link_hash_entry *) h;
6661 if (eh->adjust_done)
6664 sym_sec = eh->elf.root.u.def.section;
6665 opd = get_opd_info (sym_sec);
6666 if (opd != NULL && opd->adjust != NULL)
6668 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
6671 /* This entry has been deleted. */
6672 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6675 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6676 if (discarded_section (dsec))
6678 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6682 eh->elf.root.u.def.value = 0;
6683 eh->elf.root.u.def.section = dsec;
6686 eh->elf.root.u.def.value += adjust;
6687 eh->adjust_done = 1;
6692 /* Handles decrementing dynamic reloc counts for the reloc specified by
6693 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
6694 have already been determined. */
6697 dec_dynrel_count (bfd_vma r_info,
6699 struct bfd_link_info *info,
6700 Elf_Internal_Sym **local_syms,
6701 struct elf_link_hash_entry *h,
6702 Elf_Internal_Sym *sym)
6704 enum elf_ppc64_reloc_type r_type;
6705 asection *sym_sec = NULL;
6707 /* Can this reloc be dynamic? This switch, and later tests here
6708 should be kept in sync with the code in check_relocs. */
6709 r_type = ELF64_R_TYPE (r_info);
6715 case R_PPC64_TPREL16:
6716 case R_PPC64_TPREL16_LO:
6717 case R_PPC64_TPREL16_HI:
6718 case R_PPC64_TPREL16_HA:
6719 case R_PPC64_TPREL16_DS:
6720 case R_PPC64_TPREL16_LO_DS:
6721 case R_PPC64_TPREL16_HIGH:
6722 case R_PPC64_TPREL16_HIGHA:
6723 case R_PPC64_TPREL16_HIGHER:
6724 case R_PPC64_TPREL16_HIGHERA:
6725 case R_PPC64_TPREL16_HIGHEST:
6726 case R_PPC64_TPREL16_HIGHESTA:
6727 case R_PPC64_TPREL64:
6728 case R_PPC64_DTPMOD64:
6729 case R_PPC64_DTPREL64:
6730 case R_PPC64_ADDR64:
6734 case R_PPC64_ADDR14:
6735 case R_PPC64_ADDR14_BRNTAKEN:
6736 case R_PPC64_ADDR14_BRTAKEN:
6737 case R_PPC64_ADDR16:
6738 case R_PPC64_ADDR16_DS:
6739 case R_PPC64_ADDR16_HA:
6740 case R_PPC64_ADDR16_HI:
6741 case R_PPC64_ADDR16_HIGH:
6742 case R_PPC64_ADDR16_HIGHA:
6743 case R_PPC64_ADDR16_HIGHER:
6744 case R_PPC64_ADDR16_HIGHERA:
6745 case R_PPC64_ADDR16_HIGHEST:
6746 case R_PPC64_ADDR16_HIGHESTA:
6747 case R_PPC64_ADDR16_LO:
6748 case R_PPC64_ADDR16_LO_DS:
6749 case R_PPC64_ADDR24:
6750 case R_PPC64_ADDR32:
6751 case R_PPC64_UADDR16:
6752 case R_PPC64_UADDR32:
6753 case R_PPC64_UADDR64:
6756 case R_PPC64_D34_LO:
6757 case R_PPC64_D34_HI30:
6758 case R_PPC64_D34_HA30:
6759 case R_PPC64_ADDR16_HIGHER34:
6760 case R_PPC64_ADDR16_HIGHERA34:
6761 case R_PPC64_ADDR16_HIGHEST34:
6762 case R_PPC64_ADDR16_HIGHESTA34:
6767 if (local_syms != NULL)
6769 unsigned long r_symndx;
6770 bfd *ibfd = sec->owner;
6772 r_symndx = ELF64_R_SYM (r_info);
6773 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6777 if ((bfd_link_pic (info)
6778 && (must_be_dyn_reloc (info, r_type)
6780 && (!SYMBOLIC_BIND (info, h)
6781 || h->root.type == bfd_link_hash_defweak
6782 || !h->def_regular))))
6783 || (ELIMINATE_COPY_RELOCS
6784 && !bfd_link_pic (info)
6786 && (h->root.type == bfd_link_hash_defweak
6787 || !h->def_regular)))
6794 struct elf_dyn_relocs *p;
6795 struct elf_dyn_relocs **pp;
6796 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6798 /* elf_gc_sweep may have already removed all dyn relocs associated
6799 with local syms for a given section. Also, symbol flags are
6800 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6801 report a dynreloc miscount. */
6802 if (*pp == NULL && info->gc_sections)
6805 while ((p = *pp) != NULL)
6809 if (!must_be_dyn_reloc (info, r_type))
6821 struct ppc_dyn_relocs *p;
6822 struct ppc_dyn_relocs **pp;
6824 bfd_boolean is_ifunc;
6826 if (local_syms == NULL)
6827 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6828 if (sym_sec == NULL)
6831 vpp = &elf_section_data (sym_sec)->local_dynrel;
6832 pp = (struct ppc_dyn_relocs **) vpp;
6834 if (*pp == NULL && info->gc_sections)
6837 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6838 while ((p = *pp) != NULL)
6840 if (p->sec == sec && p->ifunc == is_ifunc)
6851 /* xgettext:c-format */
6852 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6854 bfd_set_error (bfd_error_bad_value);
6858 /* Remove unused Official Procedure Descriptor entries. Currently we
6859 only remove those associated with functions in discarded link-once
6860 sections, or weakly defined functions that have been overridden. It
6861 would be possible to remove many more entries for statically linked
6865 ppc64_elf_edit_opd (struct bfd_link_info *info)
6868 bfd_boolean some_edited = FALSE;
6869 asection *need_pad = NULL;
6870 struct ppc_link_hash_table *htab;
6872 htab = ppc_hash_table (info);
6876 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6879 Elf_Internal_Rela *relstart, *rel, *relend;
6880 Elf_Internal_Shdr *symtab_hdr;
6881 Elf_Internal_Sym *local_syms;
6882 struct _opd_sec_data *opd;
6883 bfd_boolean need_edit, add_aux_fields, broken;
6884 bfd_size_type cnt_16b = 0;
6886 if (!is_ppc64_elf (ibfd))
6889 sec = bfd_get_section_by_name (ibfd, ".opd");
6890 if (sec == NULL || sec->size == 0)
6893 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6896 if (sec->output_section == bfd_abs_section_ptr)
6899 /* Look through the section relocs. */
6900 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6904 symtab_hdr = &elf_symtab_hdr (ibfd);
6906 /* Read the relocations. */
6907 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6909 if (relstart == NULL)
6912 /* First run through the relocs to check they are sane, and to
6913 determine whether we need to edit this opd section. */
6917 relend = relstart + sec->reloc_count;
6918 for (rel = relstart; rel < relend; )
6920 enum elf_ppc64_reloc_type r_type;
6921 unsigned long r_symndx;
6923 struct elf_link_hash_entry *h;
6924 Elf_Internal_Sym *sym;
6927 /* .opd contains an array of 16 or 24 byte entries. We're
6928 only interested in the reloc pointing to a function entry
6930 offset = rel->r_offset;
6931 if (rel + 1 == relend
6932 || rel[1].r_offset != offset + 8)
6934 /* If someone messes with .opd alignment then after a
6935 "ld -r" we might have padding in the middle of .opd.
6936 Also, there's nothing to prevent someone putting
6937 something silly in .opd with the assembler. No .opd
6938 optimization for them! */
6941 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
6946 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6947 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6950 /* xgettext:c-format */
6951 (_("%pB: unexpected reloc type %u in .opd section"),
6957 r_symndx = ELF64_R_SYM (rel->r_info);
6958 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6962 if (sym_sec == NULL || sym_sec->owner == NULL)
6964 const char *sym_name;
6966 sym_name = h->root.root.string;
6968 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6972 /* xgettext:c-format */
6973 (_("%pB: undefined sym `%s' in .opd section"),
6979 /* opd entries are always for functions defined in the
6980 current input bfd. If the symbol isn't defined in the
6981 input bfd, then we won't be using the function in this
6982 bfd; It must be defined in a linkonce section in another
6983 bfd, or is weak. It's also possible that we are
6984 discarding the function due to a linker script /DISCARD/,
6985 which we test for via the output_section. */
6986 if (sym_sec->owner != ibfd
6987 || sym_sec->output_section == bfd_abs_section_ptr)
6991 if (rel + 1 == relend
6992 || (rel + 2 < relend
6993 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
6998 if (sec->size == offset + 24)
7003 if (sec->size == offset + 16)
7010 else if (rel + 1 < relend
7011 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7012 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7014 if (rel[0].r_offset == offset + 16)
7016 else if (rel[0].r_offset != offset + 24)
7023 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7025 if (!broken && (need_edit || add_aux_fields))
7027 Elf_Internal_Rela *write_rel;
7028 Elf_Internal_Shdr *rel_hdr;
7029 bfd_byte *rptr, *wptr;
7030 bfd_byte *new_contents;
7033 new_contents = NULL;
7034 amt = OPD_NDX (sec->size) * sizeof (long);
7035 opd = &ppc64_elf_section_data (sec)->u.opd;
7036 opd->adjust = bfd_zalloc (sec->owner, amt);
7037 if (opd->adjust == NULL)
7040 /* This seems a waste of time as input .opd sections are all
7041 zeros as generated by gcc, but I suppose there's no reason
7042 this will always be so. We might start putting something in
7043 the third word of .opd entries. */
7044 if ((sec->flags & SEC_IN_MEMORY) == 0)
7047 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7052 if (local_syms != NULL
7053 && symtab_hdr->contents != (unsigned char *) local_syms)
7055 if (elf_section_data (sec)->relocs != relstart)
7059 sec->contents = loc;
7060 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7063 elf_section_data (sec)->relocs = relstart;
7065 new_contents = sec->contents;
7068 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7069 if (new_contents == NULL)
7073 wptr = new_contents;
7074 rptr = sec->contents;
7075 write_rel = relstart;
7076 for (rel = relstart; rel < relend; )
7078 unsigned long r_symndx;
7080 struct elf_link_hash_entry *h;
7081 struct ppc_link_hash_entry *fdh = NULL;
7082 Elf_Internal_Sym *sym;
7084 Elf_Internal_Rela *next_rel;
7087 r_symndx = ELF64_R_SYM (rel->r_info);
7088 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7093 if (next_rel + 1 == relend
7094 || (next_rel + 2 < relend
7095 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7098 /* See if the .opd entry is full 24 byte or
7099 16 byte (with fd_aux entry overlapped with next
7102 if (next_rel == relend)
7104 if (sec->size == rel->r_offset + 16)
7107 else if (next_rel->r_offset == rel->r_offset + 16)
7111 && h->root.root.string[0] == '.')
7113 fdh = ((struct ppc_link_hash_entry *) h)->oh;
7116 fdh = ppc_follow_link (fdh);
7117 if (fdh->elf.root.type != bfd_link_hash_defined
7118 && fdh->elf.root.type != bfd_link_hash_defweak)
7123 skip = (sym_sec->owner != ibfd
7124 || sym_sec->output_section == bfd_abs_section_ptr);
7127 if (fdh != NULL && sym_sec->owner == ibfd)
7129 /* Arrange for the function descriptor sym
7131 fdh->elf.root.u.def.value = 0;
7132 fdh->elf.root.u.def.section = sym_sec;
7134 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7136 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7141 if (!dec_dynrel_count (rel->r_info, sec, info,
7145 if (++rel == next_rel)
7148 r_symndx = ELF64_R_SYM (rel->r_info);
7149 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7156 /* We'll be keeping this opd entry. */
7161 /* Redefine the function descriptor symbol to
7162 this location in the opd section. It is
7163 necessary to update the value here rather
7164 than using an array of adjustments as we do
7165 for local symbols, because various places
7166 in the generic ELF code use the value
7167 stored in u.def.value. */
7168 fdh->elf.root.u.def.value = wptr - new_contents;
7169 fdh->adjust_done = 1;
7172 /* Local syms are a bit tricky. We could
7173 tweak them as they can be cached, but
7174 we'd need to look through the local syms
7175 for the function descriptor sym which we
7176 don't have at the moment. So keep an
7177 array of adjustments. */
7178 adjust = (wptr - new_contents) - (rptr - sec->contents);
7179 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7182 memcpy (wptr, rptr, opd_ent_size);
7183 wptr += opd_ent_size;
7184 if (add_aux_fields && opd_ent_size == 16)
7186 memset (wptr, '\0', 8);
7190 /* We need to adjust any reloc offsets to point to the
7192 for ( ; rel != next_rel; ++rel)
7194 rel->r_offset += adjust;
7195 if (write_rel != rel)
7196 memcpy (write_rel, rel, sizeof (*rel));
7201 rptr += opd_ent_size;
7204 sec->size = wptr - new_contents;
7205 sec->reloc_count = write_rel - relstart;
7208 free (sec->contents);
7209 sec->contents = new_contents;
7212 /* Fudge the header size too, as this is used later in
7213 elf_bfd_final_link if we are emitting relocs. */
7214 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7215 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7218 else if (elf_section_data (sec)->relocs != relstart)
7221 if (local_syms != NULL
7222 && symtab_hdr->contents != (unsigned char *) local_syms)
7224 if (!info->keep_memory)
7227 symtab_hdr->contents = (unsigned char *) local_syms;
7232 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7234 /* If we are doing a final link and the last .opd entry is just 16 byte
7235 long, add a 8 byte padding after it. */
7236 if (need_pad != NULL && !bfd_link_relocatable (info))
7240 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7242 BFD_ASSERT (need_pad->size > 0);
7244 p = bfd_malloc (need_pad->size + 8);
7248 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7249 p, 0, need_pad->size))
7252 need_pad->contents = p;
7253 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7257 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7261 need_pad->contents = p;
7264 memset (need_pad->contents + need_pad->size, 0, 8);
7265 need_pad->size += 8;
7271 /* Analyze inline PLT call relocations to see whether calls to locally
7272 defined functions can be converted to direct calls. */
7275 ppc64_elf_inline_plt (struct bfd_link_info *info)
7277 struct ppc_link_hash_table *htab;
7280 bfd_vma low_vma, high_vma, limit;
7282 htab = ppc_hash_table (info);
7286 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7287 reduced somewhat to cater for possible stubs that might be added
7288 between the call and its destination. */
7289 if (htab->params->group_size < 0)
7291 limit = -htab->params->group_size;
7297 limit = htab->params->group_size;
7304 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7305 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7307 if (low_vma > sec->vma)
7309 if (high_vma < sec->vma + sec->size)
7310 high_vma = sec->vma + sec->size;
7313 /* If a "bl" can reach anywhere in local code sections, then we can
7314 convert all inline PLT sequences to direct calls when the symbol
7316 if (high_vma - low_vma < limit)
7318 htab->can_convert_all_inline_plt = 1;
7322 /* Otherwise, go looking through relocs for cases where a direct
7323 call won't reach. Mark the symbol on any such reloc to disable
7324 the optimization and keep the PLT entry as it seems likely that
7325 this will be better than creating trampolines. Note that this
7326 will disable the optimization for all inline PLT calls to a
7327 particular symbol, not just those that won't reach. The
7328 difficulty in doing a more precise optimization is that the
7329 linker needs to make a decision depending on whether a
7330 particular R_PPC64_PLTCALL insn can be turned into a direct
7331 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7332 the sequence, and there is nothing that ties those relocs
7333 together except their symbol. */
7335 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7337 Elf_Internal_Shdr *symtab_hdr;
7338 Elf_Internal_Sym *local_syms;
7340 if (!is_ppc64_elf (ibfd))
7344 symtab_hdr = &elf_symtab_hdr (ibfd);
7346 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7347 if (ppc64_elf_section_data (sec)->has_pltcall
7348 && !bfd_is_abs_section (sec->output_section))
7350 Elf_Internal_Rela *relstart, *rel, *relend;
7352 /* Read the relocations. */
7353 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7355 if (relstart == NULL)
7358 relend = relstart + sec->reloc_count;
7359 for (rel = relstart; rel < relend; )
7361 enum elf_ppc64_reloc_type r_type;
7362 unsigned long r_symndx;
7364 struct elf_link_hash_entry *h;
7365 Elf_Internal_Sym *sym;
7366 unsigned char *tls_maskp;
7368 r_type = ELF64_R_TYPE (rel->r_info);
7369 if (r_type != R_PPC64_PLTCALL
7370 && r_type != R_PPC64_PLTCALL_NOTOC)
7373 r_symndx = ELF64_R_SYM (rel->r_info);
7374 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7377 if (elf_section_data (sec)->relocs != relstart)
7379 if (local_syms != NULL
7380 && symtab_hdr->contents != (bfd_byte *) local_syms)
7385 if (sym_sec != NULL && sym_sec->output_section != NULL)
7389 to = h->root.u.def.value;
7392 to += (rel->r_addend
7393 + sym_sec->output_offset
7394 + sym_sec->output_section->vma);
7395 from = (rel->r_offset
7396 + sec->output_offset
7397 + sec->output_section->vma);
7398 if (to - from + limit < 2 * limit
7399 && !(r_type == R_PPC64_PLTCALL_NOTOC
7400 && (((h ? h->other : sym->st_other)
7401 & STO_PPC64_LOCAL_MASK)
7402 != 1 << STO_PPC64_LOCAL_BIT)))
7403 *tls_maskp &= ~PLT_KEEP;
7406 if (elf_section_data (sec)->relocs != relstart)
7410 if (local_syms != NULL
7411 && symtab_hdr->contents != (unsigned char *) local_syms)
7413 if (!info->keep_memory)
7416 symtab_hdr->contents = (unsigned char *) local_syms;
7423 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7426 ppc64_elf_tls_setup (struct bfd_link_info *info)
7428 struct ppc_link_hash_table *htab;
7430 htab = ppc_hash_table (info);
7434 if (abiversion (info->output_bfd) == 1)
7437 if (htab->params->no_multi_toc)
7438 htab->do_multi_toc = 0;
7439 else if (!htab->do_multi_toc)
7440 htab->params->no_multi_toc = 1;
7442 /* Default to --no-plt-localentry, as this option can cause problems
7443 with symbol interposition. For example, glibc libpthread.so and
7444 libc.so duplicate many pthread symbols, with a fallback
7445 implementation in libc.so. In some cases the fallback does more
7446 work than the pthread implementation. __pthread_condattr_destroy
7447 is one such symbol: the libpthread.so implementation is
7448 localentry:0 while the libc.so implementation is localentry:8.
7449 An app that "cleverly" uses dlopen to only load necessary
7450 libraries at runtime may omit loading libpthread.so when not
7451 running multi-threaded, which then results in the libc.so
7452 fallback symbols being used and ld.so complaining. Now there
7453 are workarounds in ld (see non_zero_localentry) to detect the
7454 pthread situation, but that may not be the only case where
7455 --plt-localentry can cause trouble. */
7456 if (htab->params->plt_localentry0 < 0)
7457 htab->params->plt_localentry0 = 0;
7458 if (htab->params->plt_localentry0
7459 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7460 FALSE, FALSE, FALSE) == NULL)
7462 (_("warning: --plt-localentry is especially dangerous without "
7463 "ld.so support to detect ABI violations"));
7465 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7466 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7467 FALSE, FALSE, TRUE));
7468 /* Move dynamic linking info to the function descriptor sym. */
7469 if (htab->tls_get_addr != NULL)
7470 func_desc_adjust (&htab->tls_get_addr->elf, info);
7471 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7472 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7473 FALSE, FALSE, TRUE));
7474 if (htab->params->tls_get_addr_opt)
7476 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7478 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7479 FALSE, FALSE, TRUE);
7481 func_desc_adjust (opt, info);
7482 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7483 FALSE, FALSE, TRUE);
7485 && (opt_fd->root.type == bfd_link_hash_defined
7486 || opt_fd->root.type == bfd_link_hash_defweak))
7488 /* If glibc supports an optimized __tls_get_addr call stub,
7489 signalled by the presence of __tls_get_addr_opt, and we'll
7490 be calling __tls_get_addr via a plt call stub, then
7491 make __tls_get_addr point to __tls_get_addr_opt. */
7492 tga_fd = &htab->tls_get_addr_fd->elf;
7493 if (htab->elf.dynamic_sections_created
7495 && (tga_fd->type == STT_FUNC
7496 || tga_fd->needs_plt)
7497 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7498 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
7500 struct plt_entry *ent;
7502 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7503 if (ent->plt.refcount > 0)
7507 tga_fd->root.type = bfd_link_hash_indirect;
7508 tga_fd->root.u.i.link = &opt_fd->root;
7509 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7511 if (opt_fd->dynindx != -1)
7513 /* Use __tls_get_addr_opt in dynamic relocations. */
7514 opt_fd->dynindx = -1;
7515 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7516 opt_fd->dynstr_index);
7517 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7520 htab->tls_get_addr_fd
7521 = (struct ppc_link_hash_entry *) opt_fd;
7522 tga = &htab->tls_get_addr->elf;
7523 if (opt != NULL && tga != NULL)
7525 tga->root.type = bfd_link_hash_indirect;
7526 tga->root.u.i.link = &opt->root;
7527 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7529 _bfd_elf_link_hash_hide_symbol (info, opt,
7531 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7533 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7534 htab->tls_get_addr_fd->is_func_descriptor = 1;
7535 if (htab->tls_get_addr != NULL)
7537 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7538 htab->tls_get_addr->is_func = 1;
7543 else if (htab->params->tls_get_addr_opt < 0)
7544 htab->params->tls_get_addr_opt = 0;
7546 return _bfd_elf_tls_setup (info->output_bfd, info);
7549 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7553 branch_reloc_hash_match (const bfd *ibfd,
7554 const Elf_Internal_Rela *rel,
7555 const struct ppc_link_hash_entry *hash1,
7556 const struct ppc_link_hash_entry *hash2)
7558 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7559 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7560 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7562 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7564 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7565 struct elf_link_hash_entry *h;
7567 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7568 h = elf_follow_link (h);
7569 if (h == &hash1->elf || h == &hash2->elf)
7575 /* Run through all the TLS relocs looking for optimization
7576 opportunities. The linker has been hacked (see ppc64elf.em) to do
7577 a preliminary section layout so that we know the TLS segment
7578 offsets. We can't optimize earlier because some optimizations need
7579 to know the tp offset, and we need to optimize before allocating
7580 dynamic relocations. */
7583 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7587 struct ppc_link_hash_table *htab;
7588 unsigned char *toc_ref;
7591 if (!bfd_link_executable (info))
7594 htab = ppc_hash_table (info);
7598 /* Make two passes over the relocs. On the first pass, mark toc
7599 entries involved with tls relocs, and check that tls relocs
7600 involved in setting up a tls_get_addr call are indeed followed by
7601 such a call. If they are not, we can't do any tls optimization.
7602 On the second pass twiddle tls_mask flags to notify
7603 relocate_section that optimization can be done, and adjust got
7604 and plt refcounts. */
7606 for (pass = 0; pass < 2; ++pass)
7607 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7609 Elf_Internal_Sym *locsyms = NULL;
7610 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7612 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7613 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7615 Elf_Internal_Rela *relstart, *rel, *relend;
7616 bfd_boolean found_tls_get_addr_arg = 0;
7618 /* Read the relocations. */
7619 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7621 if (relstart == NULL)
7627 relend = relstart + sec->reloc_count;
7628 for (rel = relstart; rel < relend; rel++)
7630 enum elf_ppc64_reloc_type r_type;
7631 unsigned long r_symndx;
7632 struct elf_link_hash_entry *h;
7633 Elf_Internal_Sym *sym;
7635 unsigned char *tls_mask;
7636 unsigned char tls_set, tls_clear, tls_type = 0;
7638 bfd_boolean ok_tprel, is_local;
7639 long toc_ref_index = 0;
7640 int expecting_tls_get_addr = 0;
7641 bfd_boolean ret = FALSE;
7643 r_symndx = ELF64_R_SYM (rel->r_info);
7644 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7648 if (elf_section_data (sec)->relocs != relstart)
7650 if (toc_ref != NULL)
7653 && (elf_symtab_hdr (ibfd).contents
7654 != (unsigned char *) locsyms))
7661 if (h->root.type == bfd_link_hash_defined
7662 || h->root.type == bfd_link_hash_defweak)
7663 value = h->root.u.def.value;
7664 else if (h->root.type == bfd_link_hash_undefweak)
7668 found_tls_get_addr_arg = 0;
7673 /* Symbols referenced by TLS relocs must be of type
7674 STT_TLS. So no need for .opd local sym adjust. */
7675 value = sym->st_value;
7684 && h->root.type == bfd_link_hash_undefweak)
7686 else if (sym_sec != NULL
7687 && sym_sec->output_section != NULL)
7689 value += sym_sec->output_offset;
7690 value += sym_sec->output_section->vma;
7691 value -= htab->elf.tls_sec->vma;
7692 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7693 < (bfd_vma) 1 << 32);
7697 r_type = ELF64_R_TYPE (rel->r_info);
7698 /* If this section has old-style __tls_get_addr calls
7699 without marker relocs, then check that each
7700 __tls_get_addr call reloc is preceded by a reloc
7701 that conceivably belongs to the __tls_get_addr arg
7702 setup insn. If we don't find matching arg setup
7703 relocs, don't do any tls optimization. */
7705 && sec->has_tls_get_addr_call
7707 && (h == &htab->tls_get_addr->elf
7708 || h == &htab->tls_get_addr_fd->elf)
7709 && !found_tls_get_addr_arg
7710 && is_branch_reloc (r_type))
7712 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7713 "TLS optimization disabled\n"),
7714 ibfd, sec, rel->r_offset);
7719 found_tls_get_addr_arg = 0;
7722 case R_PPC64_GOT_TLSLD16:
7723 case R_PPC64_GOT_TLSLD16_LO:
7724 expecting_tls_get_addr = 1;
7725 found_tls_get_addr_arg = 1;
7728 case R_PPC64_GOT_TLSLD16_HI:
7729 case R_PPC64_GOT_TLSLD16_HA:
7730 /* These relocs should never be against a symbol
7731 defined in a shared lib. Leave them alone if
7732 that turns out to be the case. */
7739 tls_type = TLS_TLS | TLS_LD;
7742 case R_PPC64_GOT_TLSGD16:
7743 case R_PPC64_GOT_TLSGD16_LO:
7744 expecting_tls_get_addr = 1;
7745 found_tls_get_addr_arg = 1;
7748 case R_PPC64_GOT_TLSGD16_HI:
7749 case R_PPC64_GOT_TLSGD16_HA:
7755 tls_set = TLS_TLS | TLS_TPRELGD;
7757 tls_type = TLS_TLS | TLS_GD;
7760 case R_PPC64_GOT_TPREL16_DS:
7761 case R_PPC64_GOT_TPREL16_LO_DS:
7762 case R_PPC64_GOT_TPREL16_HI:
7763 case R_PPC64_GOT_TPREL16_HA:
7768 tls_clear = TLS_TPREL;
7769 tls_type = TLS_TLS | TLS_TPREL;
7776 if (rel + 1 < relend
7777 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7780 && (ELF64_R_TYPE (rel[1].r_info)
7782 && (ELF64_R_TYPE (rel[1].r_info)
7783 != R_PPC64_PLTSEQ_NOTOC))
7785 r_symndx = ELF64_R_SYM (rel[1].r_info);
7786 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
7791 struct plt_entry *ent = NULL;
7793 for (ent = h->plt.plist;
7796 if (ent->addend == rel[1].r_addend)
7800 && ent->plt.refcount > 0)
7801 ent->plt.refcount -= 1;
7806 found_tls_get_addr_arg = 1;
7811 case R_PPC64_TOC16_LO:
7812 if (sym_sec == NULL || sym_sec != toc)
7815 /* Mark this toc entry as referenced by a TLS
7816 code sequence. We can do that now in the
7817 case of R_PPC64_TLS, and after checking for
7818 tls_get_addr for the TOC16 relocs. */
7819 if (toc_ref == NULL)
7821 = bfd_zmalloc (toc->output_section->rawsize / 8);
7822 if (toc_ref == NULL)
7826 value = h->root.u.def.value;
7828 value = sym->st_value;
7829 value += rel->r_addend;
7832 BFD_ASSERT (value < toc->size
7833 && toc->output_offset % 8 == 0);
7834 toc_ref_index = (value + toc->output_offset) / 8;
7835 if (r_type == R_PPC64_TLS
7836 || r_type == R_PPC64_TLSGD
7837 || r_type == R_PPC64_TLSLD)
7839 toc_ref[toc_ref_index] = 1;
7843 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7848 expecting_tls_get_addr = 2;
7851 case R_PPC64_TPREL64:
7855 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7860 tls_set = TLS_EXPLICIT;
7861 tls_clear = TLS_TPREL;
7866 case R_PPC64_DTPMOD64:
7870 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7872 if (rel + 1 < relend
7874 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7875 && rel[1].r_offset == rel->r_offset + 8)
7879 tls_set = TLS_EXPLICIT | TLS_GD;
7882 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7891 tls_set = TLS_EXPLICIT;
7902 if (!expecting_tls_get_addr
7903 || !sec->has_tls_get_addr_call)
7906 if (rel + 1 < relend
7907 && branch_reloc_hash_match (ibfd, rel + 1,
7909 htab->tls_get_addr_fd))
7911 if (expecting_tls_get_addr == 2)
7913 /* Check for toc tls entries. */
7914 unsigned char *toc_tls;
7917 retval = get_tls_mask (&toc_tls, NULL, NULL,
7922 if (toc_tls != NULL)
7924 if ((*toc_tls & TLS_TLS) != 0
7925 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
7926 found_tls_get_addr_arg = 1;
7928 toc_ref[toc_ref_index] = 1;
7934 /* Uh oh, we didn't find the expected call. We
7935 could just mark this symbol to exclude it
7936 from tls optimization but it's safer to skip
7937 the entire optimization. */
7938 /* xgettext:c-format */
7939 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
7940 "TLS optimization disabled\n"),
7941 ibfd, sec, rel->r_offset);
7946 /* If we don't have old-style __tls_get_addr calls
7947 without TLSGD/TLSLD marker relocs, and we haven't
7948 found a new-style __tls_get_addr call with a
7949 marker for this symbol, then we either have a
7950 broken object file or an -mlongcall style
7951 indirect call to __tls_get_addr without a marker.
7952 Disable optimization in this case. */
7953 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
7954 && (tls_set & TLS_EXPLICIT) == 0
7955 && !sec->has_tls_get_addr_call
7956 && ((*tls_mask & (TLS_TLS | TLS_MARK))
7957 != (TLS_TLS | TLS_MARK)))
7960 if (expecting_tls_get_addr)
7962 struct plt_entry *ent = NULL;
7964 if (htab->tls_get_addr != NULL)
7965 for (ent = htab->tls_get_addr->elf.plt.plist;
7968 if (ent->addend == 0)
7971 if (ent == NULL && htab->tls_get_addr_fd != NULL)
7972 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7975 if (ent->addend == 0)
7979 && ent->plt.refcount > 0)
7980 ent->plt.refcount -= 1;
7986 if ((tls_set & TLS_EXPLICIT) == 0)
7988 struct got_entry *ent;
7990 /* Adjust got entry for this reloc. */
7994 ent = elf_local_got_ents (ibfd)[r_symndx];
7996 for (; ent != NULL; ent = ent->next)
7997 if (ent->addend == rel->r_addend
7998 && ent->owner == ibfd
7999 && ent->tls_type == tls_type)
8006 /* We managed to get rid of a got entry. */
8007 if (ent->got.refcount > 0)
8008 ent->got.refcount -= 1;
8013 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8014 we'll lose one or two dyn relocs. */
8015 if (!dec_dynrel_count (rel->r_info, sec, info,
8019 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8021 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8027 *tls_mask |= tls_set;
8028 *tls_mask &= ~tls_clear;
8031 if (elf_section_data (sec)->relocs != relstart)
8036 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8038 if (!info->keep_memory)
8041 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8045 if (toc_ref != NULL)
8047 htab->do_tls_opt = 1;
8051 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8052 the values of any global symbols in a toc section that has been
8053 edited. Globals in toc sections should be a rarity, so this function
8054 sets a flag if any are found in toc sections other than the one just
8055 edited, so that further hash table traversals can be avoided. */
8057 struct adjust_toc_info
8060 unsigned long *skip;
8061 bfd_boolean global_toc_syms;
8064 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8067 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8069 struct ppc_link_hash_entry *eh;
8070 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8073 if (h->root.type != bfd_link_hash_defined
8074 && h->root.type != bfd_link_hash_defweak)
8077 eh = (struct ppc_link_hash_entry *) h;
8078 if (eh->adjust_done)
8081 if (eh->elf.root.u.def.section == toc_inf->toc)
8083 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8084 i = toc_inf->toc->rawsize >> 3;
8086 i = eh->elf.root.u.def.value >> 3;
8088 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8091 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8094 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8095 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8098 eh->elf.root.u.def.value -= toc_inf->skip[i];
8099 eh->adjust_done = 1;
8101 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8102 toc_inf->global_toc_syms = TRUE;
8107 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8108 on a _LO variety toc/got reloc. */
8111 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8113 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8114 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8115 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8116 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8117 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8118 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8119 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8120 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8121 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8122 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8123 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8124 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8125 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8126 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8127 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8128 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8129 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8130 /* Exclude lfqu by testing reloc. If relocs are ever
8131 defined for the reduced D field in psq_lu then those
8132 will need testing too. */
8133 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8134 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8136 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8137 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8138 /* Exclude stfqu. psq_stu as above for psq_lu. */
8139 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8140 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8141 && (insn & 1) == 0));
8144 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8145 pld ra,symbol@got@pcrel
8148 paddi ra,symbol@pcrel
8150 may be translated to
8151 pload/pstore rt,symbol@pcrel
8153 This function returns true if the optimization is possible, placing
8154 the prefix insn in *PINSN1 and a NOP in *PINSN2.
8156 On entry to this function, the linker has already determined that
8157 the pld can be replaced with paddi: *PINSN1 is that paddi insn,
8158 while *PINSN2 is the second instruction. */
8161 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2)
8163 uint32_t insn2 = *pinsn2 >> 32;
8166 /* Check that regs match. */
8167 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
8170 switch ((insn2 >> 26) & 63)
8186 /* These are the PMLS cases, where we just need to tack a prefix
8187 on the insn. Check that the D field is zero. */
8188 if ((insn2 & 0xffff) != 0)
8190 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8191 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8194 case 58: /* lwa, ld */
8195 if ((insn2 & 0xfffd) != 0)
8197 i1new = ((1ULL << 58) | (1ULL << 52)
8198 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8199 | (insn2 & (31ULL << 21)));
8202 case 57: /* lxsd, lxssp */
8203 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
8205 i1new = ((1ULL << 58) | (1ULL << 52)
8206 | ((40ULL | (insn2 & 3)) << 26)
8207 | (insn2 & (31ULL << 21)));
8210 case 61: /* stxsd, stxssp, lxv, stxv */
8211 if ((insn2 & 3) == 0)
8213 else if ((insn2 & 3) >= 2)
8215 if ((insn2 & 0xfffc) != 0)
8217 i1new = ((1ULL << 58) | (1ULL << 52)
8218 | ((44ULL | (insn2 & 3)) << 26)
8219 | (insn2 & (31ULL << 21)));
8223 if ((insn2 & 0xfff0) != 0)
8225 i1new = ((1ULL << 58) | (1ULL << 52)
8226 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8227 | (insn2 & (31ULL << 21)));
8232 if ((insn2 & 0xffff) != 0)
8234 i1new = ((1ULL << 58) | (1ULL << 52)
8235 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8238 case 62: /* std, stq */
8239 if ((insn2 & 0xfffd) != 0)
8241 i1new = ((1ULL << 58) | (1ULL << 52)
8242 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8243 | (insn2 & (31ULL << 21)));
8248 *pinsn2 = (uint64_t) NOP << 32;
8252 /* Examine all relocs referencing .toc sections in order to remove
8253 unused .toc entries. */
8256 ppc64_elf_edit_toc (struct bfd_link_info *info)
8259 struct adjust_toc_info toc_inf;
8260 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8262 htab->do_toc_opt = 1;
8263 toc_inf.global_toc_syms = TRUE;
8264 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8266 asection *toc, *sec;
8267 Elf_Internal_Shdr *symtab_hdr;
8268 Elf_Internal_Sym *local_syms;
8269 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8270 unsigned long *skip, *drop;
8271 unsigned char *used;
8272 unsigned char *keep, last, some_unused;
8274 if (!is_ppc64_elf (ibfd))
8277 toc = bfd_get_section_by_name (ibfd, ".toc");
8280 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8281 || discarded_section (toc))
8286 symtab_hdr = &elf_symtab_hdr (ibfd);
8288 /* Look at sections dropped from the final link. */
8291 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8293 if (sec->reloc_count == 0
8294 || !discarded_section (sec)
8295 || get_opd_info (sec)
8296 || (sec->flags & SEC_ALLOC) == 0
8297 || (sec->flags & SEC_DEBUGGING) != 0)
8300 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8301 if (relstart == NULL)
8304 /* Run through the relocs to see which toc entries might be
8306 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8308 enum elf_ppc64_reloc_type r_type;
8309 unsigned long r_symndx;
8311 struct elf_link_hash_entry *h;
8312 Elf_Internal_Sym *sym;
8315 r_type = ELF64_R_TYPE (rel->r_info);
8322 case R_PPC64_TOC16_LO:
8323 case R_PPC64_TOC16_HI:
8324 case R_PPC64_TOC16_HA:
8325 case R_PPC64_TOC16_DS:
8326 case R_PPC64_TOC16_LO_DS:
8330 r_symndx = ELF64_R_SYM (rel->r_info);
8331 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8339 val = h->root.u.def.value;
8341 val = sym->st_value;
8342 val += rel->r_addend;
8344 if (val >= toc->size)
8347 /* Anything in the toc ought to be aligned to 8 bytes.
8348 If not, don't mark as unused. */
8354 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8359 skip[val >> 3] = ref_from_discarded;
8362 if (elf_section_data (sec)->relocs != relstart)
8366 /* For largetoc loads of address constants, we can convert
8367 . addis rx,2,addr@got@ha
8368 . ld ry,addr@got@l(rx)
8370 . addis rx,2,addr@toc@ha
8371 . addi ry,rx,addr@toc@l
8372 when addr is within 2G of the toc pointer. This then means
8373 that the word storing "addr" in the toc is no longer needed. */
8375 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8376 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8377 && toc->reloc_count != 0)
8379 /* Read toc relocs. */
8380 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8382 if (toc_relocs == NULL)
8385 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8387 enum elf_ppc64_reloc_type r_type;
8388 unsigned long r_symndx;
8390 struct elf_link_hash_entry *h;
8391 Elf_Internal_Sym *sym;
8394 r_type = ELF64_R_TYPE (rel->r_info);
8395 if (r_type != R_PPC64_ADDR64)
8398 r_symndx = ELF64_R_SYM (rel->r_info);
8399 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8404 || sym_sec->output_section == NULL
8405 || discarded_section (sym_sec))
8408 if (!SYMBOL_REFERENCES_LOCAL (info, h))
8413 if (h->type == STT_GNU_IFUNC)
8415 val = h->root.u.def.value;
8419 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8421 val = sym->st_value;
8423 val += rel->r_addend;
8424 val += sym_sec->output_section->vma + sym_sec->output_offset;
8426 /* We don't yet know the exact toc pointer value, but we
8427 know it will be somewhere in the toc section. Don't
8428 optimize if the difference from any possible toc
8429 pointer is outside [ff..f80008000, 7fff7fff]. */
8430 addr = toc->output_section->vma + TOC_BASE_OFF;
8431 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8434 addr = toc->output_section->vma + toc->output_section->rawsize;
8435 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8440 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8445 skip[rel->r_offset >> 3]
8446 |= can_optimize | ((rel - toc_relocs) << 2);
8453 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8457 if (local_syms != NULL
8458 && symtab_hdr->contents != (unsigned char *) local_syms)
8462 && elf_section_data (sec)->relocs != relstart)
8464 if (toc_relocs != NULL
8465 && elf_section_data (toc)->relocs != toc_relocs)
8472 /* Now check all kept sections that might reference the toc.
8473 Check the toc itself last. */
8474 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8477 sec = (sec == toc ? NULL
8478 : sec->next == NULL ? toc
8479 : sec->next == toc && toc->next ? toc->next
8484 if (sec->reloc_count == 0
8485 || discarded_section (sec)
8486 || get_opd_info (sec)
8487 || (sec->flags & SEC_ALLOC) == 0
8488 || (sec->flags & SEC_DEBUGGING) != 0)
8491 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8493 if (relstart == NULL)
8499 /* Mark toc entries referenced as used. */
8503 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8505 enum elf_ppc64_reloc_type r_type;
8506 unsigned long r_symndx;
8508 struct elf_link_hash_entry *h;
8509 Elf_Internal_Sym *sym;
8511 enum {no_check, check_lo, check_ha} insn_check;
8513 r_type = ELF64_R_TYPE (rel->r_info);
8517 insn_check = no_check;
8520 case R_PPC64_GOT_TLSLD16_HA:
8521 case R_PPC64_GOT_TLSGD16_HA:
8522 case R_PPC64_GOT_TPREL16_HA:
8523 case R_PPC64_GOT_DTPREL16_HA:
8524 case R_PPC64_GOT16_HA:
8525 case R_PPC64_TOC16_HA:
8526 insn_check = check_ha;
8529 case R_PPC64_GOT_TLSLD16_LO:
8530 case R_PPC64_GOT_TLSGD16_LO:
8531 case R_PPC64_GOT_TPREL16_LO_DS:
8532 case R_PPC64_GOT_DTPREL16_LO_DS:
8533 case R_PPC64_GOT16_LO:
8534 case R_PPC64_GOT16_LO_DS:
8535 case R_PPC64_TOC16_LO:
8536 case R_PPC64_TOC16_LO_DS:
8537 insn_check = check_lo;
8541 if (insn_check != no_check)
8543 bfd_vma off = rel->r_offset & ~3;
8544 unsigned char buf[4];
8547 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8552 insn = bfd_get_32 (ibfd, buf);
8553 if (insn_check == check_lo
8554 ? !ok_lo_toc_insn (insn, r_type)
8555 : ((insn & ((0x3f << 26) | 0x1f << 16))
8556 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8560 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8561 sprintf (str, "%#08x", insn);
8562 info->callbacks->einfo
8563 /* xgettext:c-format */
8564 (_("%H: toc optimization is not supported for"
8565 " %s instruction\n"),
8566 ibfd, sec, rel->r_offset & ~3, str);
8573 case R_PPC64_TOC16_LO:
8574 case R_PPC64_TOC16_HI:
8575 case R_PPC64_TOC16_HA:
8576 case R_PPC64_TOC16_DS:
8577 case R_PPC64_TOC16_LO_DS:
8578 /* In case we're taking addresses of toc entries. */
8579 case R_PPC64_ADDR64:
8586 r_symndx = ELF64_R_SYM (rel->r_info);
8587 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8598 val = h->root.u.def.value;
8600 val = sym->st_value;
8601 val += rel->r_addend;
8603 if (val >= toc->size)
8606 if ((skip[val >> 3] & can_optimize) != 0)
8613 case R_PPC64_TOC16_HA:
8616 case R_PPC64_TOC16_LO_DS:
8617 off = rel->r_offset;
8618 off += (bfd_big_endian (ibfd) ? -2 : 3);
8619 if (!bfd_get_section_contents (ibfd, sec, &opc,
8625 if ((opc & (0x3f << 2)) == (58u << 2))
8630 /* Wrong sort of reloc, or not a ld. We may
8631 as well clear ref_from_discarded too. */
8638 /* For the toc section, we only mark as used if this
8639 entry itself isn't unused. */
8640 else if ((used[rel->r_offset >> 3]
8641 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8644 /* Do all the relocs again, to catch reference
8653 if (elf_section_data (sec)->relocs != relstart)
8657 /* Merge the used and skip arrays. Assume that TOC
8658 doublewords not appearing as either used or unused belong
8659 to an entry more than one doubleword in size. */
8660 for (drop = skip, keep = used, last = 0, some_unused = 0;
8661 drop < skip + (toc->size + 7) / 8;
8666 *drop &= ~ref_from_discarded;
8667 if ((*drop & can_optimize) != 0)
8671 else if ((*drop & ref_from_discarded) != 0)
8674 last = ref_from_discarded;
8684 bfd_byte *contents, *src;
8686 Elf_Internal_Sym *sym;
8687 bfd_boolean local_toc_syms = FALSE;
8689 /* Shuffle the toc contents, and at the same time convert the
8690 skip array from booleans into offsets. */
8691 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8694 elf_section_data (toc)->this_hdr.contents = contents;
8696 for (src = contents, off = 0, drop = skip;
8697 src < contents + toc->size;
8700 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8705 memcpy (src - off, src, 8);
8709 toc->rawsize = toc->size;
8710 toc->size = src - contents - off;
8712 /* Adjust addends for relocs against the toc section sym,
8713 and optimize any accesses we can. */
8714 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8716 if (sec->reloc_count == 0
8717 || discarded_section (sec))
8720 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8722 if (relstart == NULL)
8725 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8727 enum elf_ppc64_reloc_type r_type;
8728 unsigned long r_symndx;
8730 struct elf_link_hash_entry *h;
8733 r_type = ELF64_R_TYPE (rel->r_info);
8740 case R_PPC64_TOC16_LO:
8741 case R_PPC64_TOC16_HI:
8742 case R_PPC64_TOC16_HA:
8743 case R_PPC64_TOC16_DS:
8744 case R_PPC64_TOC16_LO_DS:
8745 case R_PPC64_ADDR64:
8749 r_symndx = ELF64_R_SYM (rel->r_info);
8750 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8758 val = h->root.u.def.value;
8761 val = sym->st_value;
8763 local_toc_syms = TRUE;
8766 val += rel->r_addend;
8768 if (val > toc->rawsize)
8770 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8772 else if ((skip[val >> 3] & can_optimize) != 0)
8774 Elf_Internal_Rela *tocrel
8775 = toc_relocs + (skip[val >> 3] >> 2);
8776 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8780 case R_PPC64_TOC16_HA:
8781 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8784 case R_PPC64_TOC16_LO_DS:
8785 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8789 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8791 info->callbacks->einfo
8792 /* xgettext:c-format */
8793 (_("%H: %s references "
8794 "optimized away TOC entry\n"),
8795 ibfd, sec, rel->r_offset,
8796 ppc64_elf_howto_table[r_type]->name);
8797 bfd_set_error (bfd_error_bad_value);
8800 rel->r_addend = tocrel->r_addend;
8801 elf_section_data (sec)->relocs = relstart;
8805 if (h != NULL || sym->st_value != 0)
8808 rel->r_addend -= skip[val >> 3];
8809 elf_section_data (sec)->relocs = relstart;
8812 if (elf_section_data (sec)->relocs != relstart)
8816 /* We shouldn't have local or global symbols defined in the TOC,
8817 but handle them anyway. */
8818 if (local_syms != NULL)
8819 for (sym = local_syms;
8820 sym < local_syms + symtab_hdr->sh_info;
8822 if (sym->st_value != 0
8823 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8827 if (sym->st_value > toc->rawsize)
8828 i = toc->rawsize >> 3;
8830 i = sym->st_value >> 3;
8832 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8836 (_("%s defined on removed toc entry"),
8837 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8840 while ((skip[i] & (ref_from_discarded | can_optimize)));
8841 sym->st_value = (bfd_vma) i << 3;
8844 sym->st_value -= skip[i];
8845 symtab_hdr->contents = (unsigned char *) local_syms;
8848 /* Adjust any global syms defined in this toc input section. */
8849 if (toc_inf.global_toc_syms)
8852 toc_inf.skip = skip;
8853 toc_inf.global_toc_syms = FALSE;
8854 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8858 if (toc->reloc_count != 0)
8860 Elf_Internal_Shdr *rel_hdr;
8861 Elf_Internal_Rela *wrel;
8864 /* Remove unused toc relocs, and adjust those we keep. */
8865 if (toc_relocs == NULL)
8866 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8868 if (toc_relocs == NULL)
8872 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8873 if ((skip[rel->r_offset >> 3]
8874 & (ref_from_discarded | can_optimize)) == 0)
8876 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8877 wrel->r_info = rel->r_info;
8878 wrel->r_addend = rel->r_addend;
8881 else if (!dec_dynrel_count (rel->r_info, toc, info,
8882 &local_syms, NULL, NULL))
8885 elf_section_data (toc)->relocs = toc_relocs;
8886 toc->reloc_count = wrel - toc_relocs;
8887 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8888 sz = rel_hdr->sh_entsize;
8889 rel_hdr->sh_size = toc->reloc_count * sz;
8892 else if (toc_relocs != NULL
8893 && elf_section_data (toc)->relocs != toc_relocs)
8896 if (local_syms != NULL
8897 && symtab_hdr->contents != (unsigned char *) local_syms)
8899 if (!info->keep_memory)
8902 symtab_hdr->contents = (unsigned char *) local_syms;
8907 /* Look for cases where we can change an indirect GOT access to
8908 a GOT relative or PC relative access, possibly reducing the
8909 number of GOT entries. */
8910 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8913 Elf_Internal_Shdr *symtab_hdr;
8914 Elf_Internal_Sym *local_syms;
8915 Elf_Internal_Rela *relstart, *rel;
8918 if (!is_ppc64_elf (ibfd))
8921 if (!ppc64_elf_tdata (ibfd)->has_gotrel)
8924 sec = ppc64_elf_tdata (ibfd)->got;
8925 got = sec->output_section->vma + sec->output_offset + 0x8000;
8928 symtab_hdr = &elf_symtab_hdr (ibfd);
8930 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8932 if (sec->reloc_count == 0
8933 || !ppc64_elf_section_data (sec)->has_gotrel
8934 || discarded_section (sec))
8937 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8939 if (relstart == NULL)
8942 if (local_syms != NULL
8943 && symtab_hdr->contents != (unsigned char *) local_syms)
8947 && elf_section_data (sec)->relocs != relstart)
8952 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8954 enum elf_ppc64_reloc_type r_type;
8955 unsigned long r_symndx;
8956 Elf_Internal_Sym *sym;
8958 struct elf_link_hash_entry *h;
8959 struct got_entry *ent;
8960 bfd_vma sym_addend, val, pc;
8961 unsigned char buf[8];
8964 r_type = ELF64_R_TYPE (rel->r_info);
8970 case R_PPC64_GOT16_DS:
8971 case R_PPC64_GOT16_HA:
8972 case R_PPC64_GOT16_LO_DS:
8973 sym_addend = rel->r_addend;
8976 case R_PPC64_GOT_PCREL34:
8981 r_symndx = ELF64_R_SYM (rel->r_info);
8982 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8986 if (!SYMBOL_REFERENCES_LOCAL (info, h))
8990 val = h->root.u.def.value;
8992 val = sym->st_value;
8994 val += sym_sec->output_section->vma + sym_sec->output_offset;
9001 case R_PPC64_GOT16_DS:
9002 if (val - got + 0x8000 >= 0x10000)
9004 if (!bfd_get_section_contents (ibfd, sec, buf,
9005 rel->r_offset & ~3, 4))
9007 insn = bfd_get_32 (ibfd, buf);
9008 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9012 case R_PPC64_GOT16_HA:
9013 if (val - got + 0x80008000ULL >= 0x100000000ULL)
9016 if (!bfd_get_section_contents (ibfd, sec, buf,
9017 rel->r_offset & ~3, 4))
9019 insn = bfd_get_32 (ibfd, buf);
9020 if (((insn & ((0x3f << 26) | 0x1f << 16))
9021 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9025 case R_PPC64_GOT16_LO_DS:
9026 if (val - got + 0x80008000ULL >= 0x100000000ULL)
9028 if (!bfd_get_section_contents (ibfd, sec, buf,
9029 rel->r_offset & ~3, 4))
9031 insn = bfd_get_32 (ibfd, buf);
9032 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9036 case R_PPC64_GOT_PCREL34:
9038 pc += sec->output_section->vma + sec->output_offset;
9039 if (val - pc + (1ULL << 33) >= 1ULL << 34)
9041 if (!bfd_get_section_contents (ibfd, sec, buf,
9042 rel->r_offset & ~3, 8))
9044 insn = bfd_get_32 (ibfd, buf);
9045 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9047 insn = bfd_get_32 (ibfd, buf + 4);
9048 if ((insn & (0x3f << 26)) != 57u << 26)
9057 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9058 ent = local_got_ents[r_symndx];
9060 for (; ent != NULL; ent = ent->next)
9061 if (ent->addend == sym_addend
9062 && ent->owner == ibfd
9063 && ent->tls_type == 0)
9065 BFD_ASSERT (ent && ent->got.refcount > 0);
9066 ent->got.refcount -= 1;
9069 if (elf_section_data (sec)->relocs != relstart)
9073 if (local_syms != NULL
9074 && symtab_hdr->contents != (unsigned char *) local_syms)
9076 if (!info->keep_memory)
9079 symtab_hdr->contents = (unsigned char *) local_syms;
9086 /* Return true iff input section I references the TOC using
9087 instructions limited to +/-32k offsets. */
9090 ppc64_elf_has_small_toc_reloc (asection *i)
9092 return (is_ppc64_elf (i->owner)
9093 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9096 /* Allocate space for one GOT entry. */
9099 allocate_got (struct elf_link_hash_entry *h,
9100 struct bfd_link_info *info,
9101 struct got_entry *gent)
9103 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9104 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9105 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9107 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9108 ? 2 : 1) * sizeof (Elf64_External_Rela);
9109 asection *got = ppc64_elf_tdata (gent->owner)->got;
9111 gent->got.offset = got->size;
9112 got->size += entsize;
9114 if (h->type == STT_GNU_IFUNC)
9116 htab->elf.irelplt->size += rentsize;
9117 htab->got_reli_size += rentsize;
9119 else if (((bfd_link_pic (info)
9120 && !((gent->tls_type & TLS_TPREL) != 0
9121 && bfd_link_executable (info)
9122 && SYMBOL_REFERENCES_LOCAL (info, h)))
9123 || (htab->elf.dynamic_sections_created
9125 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9126 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9128 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9129 relgot->size += rentsize;
9133 /* This function merges got entries in the same toc group. */
9136 merge_got_entries (struct got_entry **pent)
9138 struct got_entry *ent, *ent2;
9140 for (ent = *pent; ent != NULL; ent = ent->next)
9141 if (!ent->is_indirect)
9142 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9143 if (!ent2->is_indirect
9144 && ent2->addend == ent->addend
9145 && ent2->tls_type == ent->tls_type
9146 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9148 ent2->is_indirect = TRUE;
9149 ent2->got.ent = ent;
9153 /* If H is undefined, make it dynamic if that makes sense. */
9156 ensure_undef_dynamic (struct bfd_link_info *info,
9157 struct elf_link_hash_entry *h)
9159 struct elf_link_hash_table *htab = elf_hash_table (info);
9161 if (htab->dynamic_sections_created
9162 && ((info->dynamic_undefined_weak != 0
9163 && h->root.type == bfd_link_hash_undefweak)
9164 || h->root.type == bfd_link_hash_undefined)
9167 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9168 return bfd_elf_link_record_dynamic_symbol (info, h);
9172 /* Allocate space in .plt, .got and associated reloc sections for
9176 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9178 struct bfd_link_info *info;
9179 struct ppc_link_hash_table *htab;
9181 struct ppc_link_hash_entry *eh;
9182 struct got_entry **pgent, *gent;
9184 if (h->root.type == bfd_link_hash_indirect)
9187 info = (struct bfd_link_info *) inf;
9188 htab = ppc_hash_table (info);
9192 eh = (struct ppc_link_hash_entry *) h;
9193 /* Run through the TLS GD got entries first if we're changing them
9195 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
9196 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9197 if (gent->got.refcount > 0
9198 && (gent->tls_type & TLS_GD) != 0)
9200 /* This was a GD entry that has been converted to TPREL. If
9201 there happens to be a TPREL entry we can use that one. */
9202 struct got_entry *ent;
9203 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9204 if (ent->got.refcount > 0
9205 && (ent->tls_type & TLS_TPREL) != 0
9206 && ent->addend == gent->addend
9207 && ent->owner == gent->owner)
9209 gent->got.refcount = 0;
9213 /* If not, then we'll be using our own TPREL entry. */
9214 if (gent->got.refcount != 0)
9215 gent->tls_type = TLS_TLS | TLS_TPREL;
9218 /* Remove any list entry that won't generate a word in the GOT before
9219 we call merge_got_entries. Otherwise we risk merging to empty
9221 pgent = &h->got.glist;
9222 while ((gent = *pgent) != NULL)
9223 if (gent->got.refcount > 0)
9225 if ((gent->tls_type & TLS_LD) != 0
9228 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9229 *pgent = gent->next;
9232 pgent = &gent->next;
9235 *pgent = gent->next;
9237 if (!htab->do_multi_toc)
9238 merge_got_entries (&h->got.glist);
9240 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9241 if (!gent->is_indirect)
9243 /* Make sure this symbol is output as a dynamic symbol. */
9244 if (!ensure_undef_dynamic (info, h))
9247 if (!is_ppc64_elf (gent->owner))
9250 allocate_got (h, info, gent);
9253 /* If no dynamic sections we can't have dynamic relocs, except for
9254 IFUNCs which are handled even in static executables. */
9255 if (!htab->elf.dynamic_sections_created
9256 && h->type != STT_GNU_IFUNC)
9257 eh->dyn_relocs = NULL;
9259 /* Discard relocs on undefined symbols that must be local. */
9260 else if (h->root.type == bfd_link_hash_undefined
9261 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9262 eh->dyn_relocs = NULL;
9264 /* Also discard relocs on undefined weak syms with non-default
9265 visibility, or when dynamic_undefined_weak says so. */
9266 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9267 eh->dyn_relocs = NULL;
9269 if (eh->dyn_relocs != NULL)
9271 struct elf_dyn_relocs *p, **pp;
9273 /* In the shared -Bsymbolic case, discard space allocated for
9274 dynamic pc-relative relocs against symbols which turn out to
9275 be defined in regular objects. For the normal shared case,
9276 discard space for relocs that have become local due to symbol
9277 visibility changes. */
9279 if (bfd_link_pic (info))
9281 /* Relocs that use pc_count are those that appear on a call
9282 insn, or certain REL relocs (see must_be_dyn_reloc) that
9283 can be generated via assembly. We want calls to
9284 protected symbols to resolve directly to the function
9285 rather than going via the plt. If people want function
9286 pointer comparisons to work as expected then they should
9287 avoid writing weird assembly. */
9288 if (SYMBOL_CALLS_LOCAL (info, h))
9290 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9292 p->count -= p->pc_count;
9301 if (eh->dyn_relocs != NULL)
9303 /* Make sure this symbol is output as a dynamic symbol. */
9304 if (!ensure_undef_dynamic (info, h))
9308 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
9310 /* For the non-pic case, discard space for relocs against
9311 symbols which turn out to need copy relocs or are not
9313 if (h->dynamic_adjusted
9315 && !ELF_COMMON_DEF_P (h))
9317 /* Make sure this symbol is output as a dynamic symbol. */
9318 if (!ensure_undef_dynamic (info, h))
9321 if (h->dynindx == -1)
9322 eh->dyn_relocs = NULL;
9325 eh->dyn_relocs = NULL;
9328 /* Finally, allocate space. */
9329 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9331 asection *sreloc = elf_section_data (p->sec)->sreloc;
9332 if (eh->elf.type == STT_GNU_IFUNC)
9333 sreloc = htab->elf.irelplt;
9334 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9338 /* We might need a PLT entry when the symbol
9341 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9342 d) has plt16 relocs and we are linking statically. */
9343 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9344 || h->type == STT_GNU_IFUNC
9345 || (h->needs_plt && h->dynamic_adjusted)
9348 && !htab->elf.dynamic_sections_created
9349 && !htab->can_convert_all_inline_plt
9350 && (((struct ppc_link_hash_entry *) h)->tls_mask
9351 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
9353 struct plt_entry *pent;
9354 bfd_boolean doneone = FALSE;
9355 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9356 if (pent->plt.refcount > 0)
9358 if (!htab->elf.dynamic_sections_created
9359 || h->dynindx == -1)
9361 if (h->type == STT_GNU_IFUNC)
9364 pent->plt.offset = s->size;
9365 s->size += PLT_ENTRY_SIZE (htab);
9366 s = htab->elf.irelplt;
9371 pent->plt.offset = s->size;
9372 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9373 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9378 /* If this is the first .plt entry, make room for the special
9382 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9384 pent->plt.offset = s->size;
9386 /* Make room for this entry. */
9387 s->size += PLT_ENTRY_SIZE (htab);
9389 /* Make room for the .glink code. */
9392 s->size += GLINK_PLTRESOLVE_SIZE (htab);
9395 /* We need bigger stubs past index 32767. */
9396 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
9403 /* We also need to make an entry in the .rela.plt section. */
9404 s = htab->elf.srelplt;
9407 s->size += sizeof (Elf64_External_Rela);
9411 pent->plt.offset = (bfd_vma) -1;
9414 h->plt.plist = NULL;
9420 h->plt.plist = NULL;
9427 #define PPC_LO(v) ((v) & 0xffff)
9428 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9429 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9431 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9432 to set up space for global entry stubs. These are put in glink,
9433 after the branch table. */
9436 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9438 struct bfd_link_info *info;
9439 struct ppc_link_hash_table *htab;
9440 struct plt_entry *pent;
9443 if (h->root.type == bfd_link_hash_indirect)
9446 if (!h->pointer_equality_needed)
9453 htab = ppc_hash_table (info);
9457 s = htab->global_entry;
9458 plt = htab->elf.splt;
9459 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9460 if (pent->plt.offset != (bfd_vma) -1
9461 && pent->addend == 0)
9463 /* For ELFv2, if this symbol is not defined in a regular file
9464 and we are not generating a shared library or pie, then we
9465 need to define the symbol in the executable on a call stub.
9466 This is to avoid text relocations. */
9467 bfd_vma off, stub_align, stub_off, stub_size;
9468 unsigned int align_power;
9472 if (htab->params->plt_stub_align >= 0)
9473 align_power = htab->params->plt_stub_align;
9475 align_power = -htab->params->plt_stub_align;
9476 /* Setting section alignment is delayed until we know it is
9477 non-empty. Otherwise the .text output section will be
9478 aligned at least to plt_stub_align even when no global
9479 entry stubs are needed. */
9480 if (s->alignment_power < align_power)
9481 s->alignment_power = align_power;
9482 stub_align = (bfd_vma) 1 << align_power;
9483 if (htab->params->plt_stub_align >= 0
9484 || ((((stub_off + stub_size - 1) & -stub_align)
9485 - (stub_off & -stub_align))
9486 > ((stub_size - 1) & -stub_align)))
9487 stub_off = (stub_off + stub_align - 1) & -stub_align;
9488 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9489 off -= stub_off + s->output_offset + s->output_section->vma;
9490 /* Note that for --plt-stub-align negative we have a possible
9491 dependency between stub offset and size. Break that
9492 dependency by assuming the max stub size when calculating
9494 if (PPC_HA (off) == 0)
9496 h->root.type = bfd_link_hash_defined;
9497 h->root.u.def.section = s;
9498 h->root.u.def.value = stub_off;
9499 s->size = stub_off + stub_size;
9505 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9506 read-only sections. */
9509 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
9513 if (h->root.type == bfd_link_hash_indirect)
9516 sec = readonly_dynrelocs (h);
9519 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9521 info->flags |= DF_TEXTREL;
9522 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9523 " in read-only section `%pA'\n"),
9524 sec->owner, h->root.root.string, sec);
9526 /* Not an error, just cut short the traversal. */
9532 /* Set the sizes of the dynamic sections. */
9535 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9536 struct bfd_link_info *info)
9538 struct ppc_link_hash_table *htab;
9543 struct got_entry *first_tlsld;
9545 htab = ppc_hash_table (info);
9549 dynobj = htab->elf.dynobj;
9553 if (htab->elf.dynamic_sections_created)
9555 /* Set the contents of the .interp section to the interpreter. */
9556 if (bfd_link_executable (info) && !info->nointerp)
9558 s = bfd_get_linker_section (dynobj, ".interp");
9561 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9562 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9566 /* Set up .got offsets for local syms, and space for local dynamic
9568 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9570 struct got_entry **lgot_ents;
9571 struct got_entry **end_lgot_ents;
9572 struct plt_entry **local_plt;
9573 struct plt_entry **end_local_plt;
9574 unsigned char *lgot_masks;
9575 bfd_size_type locsymcount;
9576 Elf_Internal_Shdr *symtab_hdr;
9578 if (!is_ppc64_elf (ibfd))
9581 for (s = ibfd->sections; s != NULL; s = s->next)
9583 struct ppc_dyn_relocs *p;
9585 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9587 if (!bfd_is_abs_section (p->sec)
9588 && bfd_is_abs_section (p->sec->output_section))
9590 /* Input section has been discarded, either because
9591 it is a copy of a linkonce section or due to
9592 linker script /DISCARD/, so we'll be discarding
9595 else if (p->count != 0)
9597 asection *srel = elf_section_data (p->sec)->sreloc;
9599 srel = htab->elf.irelplt;
9600 srel->size += p->count * sizeof (Elf64_External_Rela);
9601 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9602 info->flags |= DF_TEXTREL;
9607 lgot_ents = elf_local_got_ents (ibfd);
9611 symtab_hdr = &elf_symtab_hdr (ibfd);
9612 locsymcount = symtab_hdr->sh_info;
9613 end_lgot_ents = lgot_ents + locsymcount;
9614 local_plt = (struct plt_entry **) end_lgot_ents;
9615 end_local_plt = local_plt + locsymcount;
9616 lgot_masks = (unsigned char *) end_local_plt;
9617 s = ppc64_elf_tdata (ibfd)->got;
9618 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9620 struct got_entry **pent, *ent;
9623 while ((ent = *pent) != NULL)
9624 if (ent->got.refcount > 0)
9626 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9628 ppc64_tlsld_got (ibfd)->got.refcount += 1;
9633 unsigned int ent_size = 8;
9634 unsigned int rel_size = sizeof (Elf64_External_Rela);
9636 ent->got.offset = s->size;
9637 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9642 s->size += ent_size;
9643 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9645 htab->elf.irelplt->size += rel_size;
9646 htab->got_reli_size += rel_size;
9648 else if (bfd_link_pic (info)
9649 && !((ent->tls_type & TLS_TPREL) != 0
9650 && bfd_link_executable (info)))
9652 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9653 srel->size += rel_size;
9662 /* Allocate space for plt calls to local syms. */
9663 lgot_masks = (unsigned char *) end_local_plt;
9664 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
9666 struct plt_entry *ent;
9668 for (ent = *local_plt; ent != NULL; ent = ent->next)
9669 if (ent->plt.refcount > 0)
9671 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9674 ent->plt.offset = s->size;
9675 s->size += PLT_ENTRY_SIZE (htab);
9676 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9678 else if (htab->can_convert_all_inline_plt
9679 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
9680 ent->plt.offset = (bfd_vma) -1;
9684 ent->plt.offset = s->size;
9685 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9686 if (bfd_link_pic (info))
9687 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9691 ent->plt.offset = (bfd_vma) -1;
9695 /* Allocate global sym .plt and .got entries, and space for global
9696 sym dynamic relocs. */
9697 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9699 if (!htab->opd_abi && !bfd_link_pic (info))
9700 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
9703 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9705 struct got_entry *ent;
9707 if (!is_ppc64_elf (ibfd))
9710 ent = ppc64_tlsld_got (ibfd);
9711 if (ent->got.refcount > 0)
9713 if (!htab->do_multi_toc && first_tlsld != NULL)
9715 ent->is_indirect = TRUE;
9716 ent->got.ent = first_tlsld;
9720 if (first_tlsld == NULL)
9722 s = ppc64_elf_tdata (ibfd)->got;
9723 ent->got.offset = s->size;
9726 if (bfd_link_pic (info))
9728 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9729 srel->size += sizeof (Elf64_External_Rela);
9734 ent->got.offset = (bfd_vma) -1;
9737 /* We now have determined the sizes of the various dynamic sections.
9738 Allocate memory for them. */
9740 for (s = dynobj->sections; s != NULL; s = s->next)
9742 if ((s->flags & SEC_LINKER_CREATED) == 0)
9745 if (s == htab->brlt || s == htab->relbrlt)
9746 /* These haven't been allocated yet; don't strip. */
9748 else if (s == htab->elf.sgot
9749 || s == htab->elf.splt
9750 || s == htab->elf.iplt
9751 || s == htab->pltlocal
9753 || s == htab->global_entry
9754 || s == htab->elf.sdynbss
9755 || s == htab->elf.sdynrelro)
9757 /* Strip this section if we don't need it; see the
9760 else if (s == htab->glink_eh_frame)
9762 if (!bfd_is_abs_section (s->output_section))
9763 /* Not sized yet. */
9766 else if (CONST_STRNEQ (s->name, ".rela"))
9770 if (s != htab->elf.srelplt)
9773 /* We use the reloc_count field as a counter if we need
9774 to copy relocs into the output file. */
9780 /* It's not one of our sections, so don't allocate space. */
9786 /* If we don't need this section, strip it from the
9787 output file. This is mostly to handle .rela.bss and
9788 .rela.plt. We must create both sections in
9789 create_dynamic_sections, because they must be created
9790 before the linker maps input sections to output
9791 sections. The linker does that before
9792 adjust_dynamic_symbol is called, and it is that
9793 function which decides whether anything needs to go
9794 into these sections. */
9795 s->flags |= SEC_EXCLUDE;
9799 if (bfd_is_abs_section (s->output_section))
9800 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9803 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9806 /* Allocate memory for the section contents. We use bfd_zalloc
9807 here in case unused entries are not reclaimed before the
9808 section's contents are written out. This should not happen,
9809 but this way if it does we get a R_PPC64_NONE reloc in .rela
9810 sections instead of garbage.
9811 We also rely on the section contents being zero when writing
9812 the GOT and .dynrelro. */
9813 s->contents = bfd_zalloc (dynobj, s->size);
9814 if (s->contents == NULL)
9818 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9820 if (!is_ppc64_elf (ibfd))
9823 s = ppc64_elf_tdata (ibfd)->got;
9824 if (s != NULL && s != htab->elf.sgot)
9827 s->flags |= SEC_EXCLUDE;
9830 s->contents = bfd_zalloc (ibfd, s->size);
9831 if (s->contents == NULL)
9835 s = ppc64_elf_tdata (ibfd)->relgot;
9839 s->flags |= SEC_EXCLUDE;
9842 s->contents = bfd_zalloc (ibfd, s->size);
9843 if (s->contents == NULL)
9851 if (htab->elf.dynamic_sections_created)
9853 bfd_boolean tls_opt;
9855 /* Add some entries to the .dynamic section. We fill in the
9856 values later, in ppc64_elf_finish_dynamic_sections, but we
9857 must add the entries now so that we get the correct size for
9858 the .dynamic section. The DT_DEBUG entry is filled in by the
9859 dynamic linker and used by the debugger. */
9860 #define add_dynamic_entry(TAG, VAL) \
9861 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9863 if (bfd_link_executable (info))
9865 if (!add_dynamic_entry (DT_DEBUG, 0))
9869 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
9871 if (!add_dynamic_entry (DT_PLTGOT, 0)
9872 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9873 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9874 || !add_dynamic_entry (DT_JMPREL, 0)
9875 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9879 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
9881 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9882 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9886 tls_opt = (htab->params->tls_get_addr_opt
9887 && htab->tls_get_addr_fd != NULL
9888 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9889 if (tls_opt || !htab->opd_abi)
9891 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9897 if (!add_dynamic_entry (DT_RELA, 0)
9898 || !add_dynamic_entry (DT_RELASZ, 0)
9899 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9902 /* If any dynamic relocs apply to a read-only section,
9903 then we need a DT_TEXTREL entry. */
9904 if ((info->flags & DF_TEXTREL) == 0)
9905 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
9907 if ((info->flags & DF_TEXTREL) != 0)
9909 if (!add_dynamic_entry (DT_TEXTREL, 0))
9914 #undef add_dynamic_entry
9919 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
9922 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
9924 if (h->plt.plist != NULL
9926 && !h->pointer_equality_needed)
9929 return _bfd_elf_hash_symbol (h);
9932 /* Determine the type of stub needed, if any, for a call. */
9934 static inline enum ppc_stub_type
9935 ppc_type_of_stub (asection *input_sec,
9936 const Elf_Internal_Rela *rel,
9937 struct ppc_link_hash_entry **hash,
9938 struct plt_entry **plt_ent,
9939 bfd_vma destination,
9940 unsigned long local_off)
9942 struct ppc_link_hash_entry *h = *hash;
9944 bfd_vma branch_offset;
9945 bfd_vma max_branch_offset;
9946 enum elf_ppc64_reloc_type r_type;
9950 struct plt_entry *ent;
9951 struct ppc_link_hash_entry *fdh = h;
9953 && h->oh->is_func_descriptor)
9955 fdh = ppc_follow_link (h->oh);
9959 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9960 if (ent->addend == rel->r_addend
9961 && ent->plt.offset != (bfd_vma) -1)
9964 return ppc_stub_plt_call;
9967 /* Here, we know we don't have a plt entry. If we don't have a
9968 either a defined function descriptor or a defined entry symbol
9969 in a regular object file, then it is pointless trying to make
9970 any other type of stub. */
9971 if (!is_static_defined (&fdh->elf)
9972 && !is_static_defined (&h->elf))
9973 return ppc_stub_none;
9975 else if (elf_local_got_ents (input_sec->owner) != NULL)
9977 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9978 struct plt_entry **local_plt = (struct plt_entry **)
9979 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9980 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9982 if (local_plt[r_symndx] != NULL)
9984 struct plt_entry *ent;
9986 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9987 if (ent->addend == rel->r_addend
9988 && ent->plt.offset != (bfd_vma) -1)
9991 return ppc_stub_plt_call;
9996 /* Determine where the call point is. */
9997 location = (input_sec->output_offset
9998 + input_sec->output_section->vma
10001 branch_offset = destination - location;
10002 r_type = ELF64_R_TYPE (rel->r_info);
10004 /* Determine if a long branch stub is needed. */
10005 max_branch_offset = 1 << 25;
10006 if (r_type == R_PPC64_REL14
10007 || r_type == R_PPC64_REL14_BRTAKEN
10008 || r_type == R_PPC64_REL14_BRNTAKEN)
10009 max_branch_offset = 1 << 15;
10011 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10012 /* We need a stub. Figure out whether a long_branch or plt_branch
10013 is needed later. */
10014 return ppc_stub_long_branch;
10016 return ppc_stub_none;
10019 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10020 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10025 . lis %r12,xxx-1b@highest
10026 . ori %r12,%r12,xxx-1b@higher
10027 . sldi %r12,%r12,32
10028 . oris %r12,%r12,xxx-1b@high
10029 . ori %r12,%r12,xxx-1b@l
10030 . add/ldx %r12,%r11,%r12 */
10033 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10035 bfd_put_32 (abfd, MFLR_R12, p);
10037 bfd_put_32 (abfd, BCL_20_31, p);
10039 bfd_put_32 (abfd, MFLR_R11, p);
10041 bfd_put_32 (abfd, MTLR_R12, p);
10043 if (off + 0x8000 < 0x10000)
10046 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10048 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10051 else if (off + 0x80008000ULL < 0x100000000ULL)
10053 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10056 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10058 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10063 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10065 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10070 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10072 if (((off >> 32) & 0xffff) != 0)
10074 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10078 if (((off >> 32) & 0xffffffffULL) != 0)
10080 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10083 if (PPC_HI (off) != 0)
10085 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10088 if (PPC_LO (off) != 0)
10090 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10094 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10096 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10102 static unsigned int
10103 size_offset (bfd_vma off)
10106 if (off + 0x8000 < 0x10000)
10108 else if (off + 0x80008000ULL < 0x100000000ULL)
10112 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10117 if (((off >> 32) & 0xffff) != 0)
10120 if (((off >> 32) & 0xffffffffULL) != 0)
10122 if (PPC_HI (off) != 0)
10124 if (PPC_LO (off) != 0)
10131 static unsigned int
10132 num_relocs_for_offset (bfd_vma off)
10134 unsigned int num_rel;
10135 if (off + 0x8000 < 0x10000)
10137 else if (off + 0x80008000ULL < 0x100000000ULL)
10142 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10143 && ((off >> 32) & 0xffff) != 0)
10145 if (PPC_HI (off) != 0)
10147 if (PPC_LO (off) != 0)
10153 static Elf_Internal_Rela *
10154 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10155 bfd_vma roff, bfd_vma targ, bfd_vma off)
10157 bfd_vma relative_targ = targ - (roff - 8);
10158 if (bfd_big_endian (info->output_bfd))
10160 r->r_offset = roff;
10161 r->r_addend = relative_targ + roff;
10162 if (off + 0x8000 < 0x10000)
10163 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10164 else if (off + 0x80008000ULL < 0x100000000ULL)
10166 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10169 r->r_offset = roff;
10170 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10171 r->r_addend = relative_targ + roff;
10175 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10176 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10179 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10180 if (((off >> 32) & 0xffff) != 0)
10184 r->r_offset = roff;
10185 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10186 r->r_addend = relative_targ + roff;
10189 if (((off >> 32) & 0xffffffffULL) != 0)
10191 if (PPC_HI (off) != 0)
10195 r->r_offset = roff;
10196 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10197 r->r_addend = relative_targ + roff;
10199 if (PPC_LO (off) != 0)
10203 r->r_offset = roff;
10204 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10205 r->r_addend = relative_targ + roff;
10211 /* Emit .eh_frame opcode to advance pc by DELTA. */
10214 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10218 *eh++ = DW_CFA_advance_loc + delta;
10219 else if (delta < 256)
10221 *eh++ = DW_CFA_advance_loc1;
10224 else if (delta < 65536)
10226 *eh++ = DW_CFA_advance_loc2;
10227 bfd_put_16 (abfd, delta, eh);
10232 *eh++ = DW_CFA_advance_loc4;
10233 bfd_put_32 (abfd, delta, eh);
10239 /* Size of required .eh_frame opcode to advance pc by DELTA. */
10241 static unsigned int
10242 eh_advance_size (unsigned int delta)
10244 if (delta < 64 * 4)
10245 /* DW_CFA_advance_loc+[1..63]. */
10247 if (delta < 256 * 4)
10248 /* DW_CFA_advance_loc1, byte. */
10250 if (delta < 65536 * 4)
10251 /* DW_CFA_advance_loc2, 2 bytes. */
10253 /* DW_CFA_advance_loc4, 4 bytes. */
10257 /* With power7 weakly ordered memory model, it is possible for ld.so
10258 to update a plt entry in one thread and have another thread see a
10259 stale zero toc entry. To avoid this we need some sort of acquire
10260 barrier in the call stub. One solution is to make the load of the
10261 toc word seem to appear to depend on the load of the function entry
10262 word. Another solution is to test for r2 being zero, and branch to
10263 the appropriate glink entry if so.
10265 . fake dep barrier compare
10266 . ld 12,xxx(2) ld 12,xxx(2)
10267 . mtctr 12 mtctr 12
10268 . xor 11,12,12 ld 2,xxx+8(2)
10269 . add 2,2,11 cmpldi 2,0
10270 . ld 2,xxx+8(2) bnectr+
10271 . bctr b <glink_entry>
10273 The solution involving the compare turns out to be faster, so
10274 that's what we use unless the branch won't reach. */
10276 #define ALWAYS_USE_FAKE_DEP 0
10277 #define ALWAYS_EMIT_R2SAVE 0
10279 static inline unsigned int
10280 plt_stub_size (struct ppc_link_hash_table *htab,
10281 struct ppc_stub_hash_entry *stub_entry,
10286 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10288 size = 8 + size_offset (off - 8);
10289 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10295 if (ALWAYS_EMIT_R2SAVE
10296 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10298 if (PPC_HA (off) != 0)
10303 if (htab->params->plt_static_chain)
10305 if (htab->params->plt_thread_safe
10306 && htab->elf.dynamic_sections_created
10307 && stub_entry->h != NULL
10308 && stub_entry->h->elf.dynindx != -1)
10310 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10313 if (stub_entry->h != NULL
10314 && (stub_entry->h == htab->tls_get_addr_fd
10315 || stub_entry->h == htab->tls_get_addr)
10316 && htab->params->tls_get_addr_opt)
10319 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
10325 /* Depending on the sign of plt_stub_align:
10326 If positive, return the padding to align to a 2**plt_stub_align
10328 If negative, if this stub would cross fewer 2**plt_stub_align
10329 boundaries if we align, then return the padding needed to do so. */
10331 static inline unsigned int
10332 plt_stub_pad (struct ppc_link_hash_table *htab,
10333 struct ppc_stub_hash_entry *stub_entry,
10337 unsigned stub_size;
10338 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10340 if (htab->params->plt_stub_align >= 0)
10342 stub_align = 1 << htab->params->plt_stub_align;
10343 if ((stub_off & (stub_align - 1)) != 0)
10344 return stub_align - (stub_off & (stub_align - 1));
10348 stub_align = 1 << -htab->params->plt_stub_align;
10349 stub_size = plt_stub_size (htab, stub_entry, plt_off);
10350 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10351 > ((stub_size - 1) & -stub_align))
10352 return stub_align - (stub_off & (stub_align - 1));
10356 /* Build a .plt call stub. */
10358 static inline bfd_byte *
10359 build_plt_stub (struct ppc_link_hash_table *htab,
10360 struct ppc_stub_hash_entry *stub_entry,
10361 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10363 bfd *obfd = htab->params->stub_bfd;
10364 bfd_boolean plt_load_toc = htab->opd_abi;
10365 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10366 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10367 && htab->elf.dynamic_sections_created
10368 && stub_entry->h != NULL
10369 && stub_entry->h->elf.dynindx != -1);
10370 bfd_boolean use_fake_dep = plt_thread_safe;
10371 bfd_vma cmp_branch_off = 0;
10373 if (!ALWAYS_USE_FAKE_DEP
10376 && !((stub_entry->h == htab->tls_get_addr_fd
10377 || stub_entry->h == htab->tls_get_addr)
10378 && htab->params->tls_get_addr_opt))
10380 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10381 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10382 / PLT_ENTRY_SIZE (htab));
10383 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10386 if (pltindex > 32768)
10387 glinkoff += (pltindex - 32768) * 4;
10389 + htab->glink->output_offset
10390 + htab->glink->output_section->vma);
10391 from = (p - stub_entry->group->stub_sec->contents
10392 + 4 * (ALWAYS_EMIT_R2SAVE
10393 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10394 + 4 * (PPC_HA (offset) != 0)
10395 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10396 != PPC_HA (offset))
10397 + 4 * (plt_static_chain != 0)
10399 + stub_entry->group->stub_sec->output_offset
10400 + stub_entry->group->stub_sec->output_section->vma);
10401 cmp_branch_off = to - from;
10402 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10405 if (PPC_HA (offset) != 0)
10409 if (ALWAYS_EMIT_R2SAVE
10410 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10411 r[0].r_offset += 4;
10412 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10413 r[1].r_offset = r[0].r_offset + 4;
10414 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10415 r[1].r_addend = r[0].r_addend;
10418 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10420 r[2].r_offset = r[1].r_offset + 4;
10421 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10422 r[2].r_addend = r[0].r_addend;
10426 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10427 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10428 r[2].r_addend = r[0].r_addend + 8;
10429 if (plt_static_chain)
10431 r[3].r_offset = r[2].r_offset + 4;
10432 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10433 r[3].r_addend = r[0].r_addend + 16;
10438 if (ALWAYS_EMIT_R2SAVE
10439 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10440 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10443 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10444 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10448 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10449 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10452 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10454 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10457 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10462 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10463 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10465 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10466 if (plt_static_chain)
10467 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10474 if (ALWAYS_EMIT_R2SAVE
10475 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10476 r[0].r_offset += 4;
10477 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10480 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10482 r[1].r_offset = r[0].r_offset + 4;
10483 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10484 r[1].r_addend = r[0].r_addend;
10488 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10489 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10490 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10491 if (plt_static_chain)
10493 r[2].r_offset = r[1].r_offset + 4;
10494 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10495 r[2].r_addend = r[0].r_addend + 8;
10500 if (ALWAYS_EMIT_R2SAVE
10501 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10502 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10503 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10505 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10507 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10510 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10515 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10516 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10518 if (plt_static_chain)
10519 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10520 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10523 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10525 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10526 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
10527 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10530 bfd_put_32 (obfd, BCTR, p), p += 4;
10534 /* Build a special .plt call stub for __tls_get_addr. */
10536 #define LD_R11_0R3 0xe9630000
10537 #define LD_R12_0R3 0xe9830000
10538 #define MR_R0_R3 0x7c601b78
10539 #define CMPDI_R11_0 0x2c2b0000
10540 #define ADD_R3_R12_R13 0x7c6c6a14
10541 #define BEQLR 0x4d820020
10542 #define MR_R3_R0 0x7c030378
10543 #define STD_R11_0R1 0xf9610000
10544 #define BCTRL 0x4e800421
10545 #define LD_R11_0R1 0xe9610000
10546 #define MTLR_R11 0x7d6803a6
10548 static inline bfd_byte *
10549 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10550 struct ppc_stub_hash_entry *stub_entry,
10551 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10553 bfd *obfd = htab->params->stub_bfd;
10556 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10557 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10558 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10559 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10560 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10561 bfd_put_32 (obfd, BEQLR, p), p += 4;
10562 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10564 r[0].r_offset += 7 * 4;
10565 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
10566 return build_plt_stub (htab, stub_entry, p, offset, r);
10568 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10569 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10572 r[0].r_offset += 2 * 4;
10573 p = build_plt_stub (htab, stub_entry, p, offset, r);
10574 bfd_put_32 (obfd, BCTRL, p - 4);
10576 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
10577 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10578 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10579 bfd_put_32 (obfd, BLR, p), p += 4;
10581 if (htab->glink_eh_frame != NULL
10582 && htab->glink_eh_frame->size != 0)
10584 bfd_byte *base, *eh;
10585 unsigned int lr_used, delta;
10587 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10588 eh = base + stub_entry->group->eh_size;
10589 lr_used = stub_entry->stub_offset + (p - 20 - loc);
10590 delta = lr_used - stub_entry->group->lr_restore;
10591 stub_entry->group->lr_restore = lr_used + 16;
10592 eh = eh_advance (htab->elf.dynobj, eh, delta);
10593 *eh++ = DW_CFA_offset_extended_sf;
10595 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10596 *eh++ = DW_CFA_advance_loc + 4;
10597 *eh++ = DW_CFA_restore_extended;
10599 stub_entry->group->eh_size = eh - base;
10604 static Elf_Internal_Rela *
10605 get_relocs (asection *sec, int count)
10607 Elf_Internal_Rela *relocs;
10608 struct bfd_elf_section_data *elfsec_data;
10610 elfsec_data = elf_section_data (sec);
10611 relocs = elfsec_data->relocs;
10612 if (relocs == NULL)
10614 bfd_size_type relsize;
10615 relsize = sec->reloc_count * sizeof (*relocs);
10616 relocs = bfd_alloc (sec->owner, relsize);
10617 if (relocs == NULL)
10619 elfsec_data->relocs = relocs;
10620 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10621 sizeof (Elf_Internal_Shdr));
10622 if (elfsec_data->rela.hdr == NULL)
10624 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10625 * sizeof (Elf64_External_Rela));
10626 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10627 sec->reloc_count = 0;
10629 relocs += sec->reloc_count;
10630 sec->reloc_count += count;
10634 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10635 forms, to the equivalent relocs against the global symbol given by
10639 use_global_in_relocs (struct ppc_link_hash_table *htab,
10640 struct ppc_stub_hash_entry *stub_entry,
10641 Elf_Internal_Rela *r, unsigned int num_rel)
10643 struct elf_link_hash_entry **hashes;
10644 unsigned long symndx;
10645 struct ppc_link_hash_entry *h;
10648 /* Relocs are always against symbols in their own object file. Fake
10649 up global sym hashes for the stub bfd (which has no symbols). */
10650 hashes = elf_sym_hashes (htab->params->stub_bfd);
10651 if (hashes == NULL)
10653 bfd_size_type hsize;
10655 /* When called the first time, stub_globals will contain the
10656 total number of symbols seen during stub sizing. After
10657 allocating, stub_globals is used as an index to fill the
10659 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10660 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10661 if (hashes == NULL)
10663 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10664 htab->stub_globals = 1;
10666 symndx = htab->stub_globals++;
10668 hashes[symndx] = &h->elf;
10669 if (h->oh != NULL && h->oh->is_func)
10670 h = ppc_follow_link (h->oh);
10671 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10672 || h->elf.root.type == bfd_link_hash_defweak);
10673 symval = (h->elf.root.u.def.value
10674 + h->elf.root.u.def.section->output_offset
10675 + h->elf.root.u.def.section->output_section->vma);
10676 while (num_rel-- != 0)
10678 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10679 if (h->elf.root.u.def.section != stub_entry->target_section)
10681 /* H is an opd symbol. The addend must be zero, and the
10682 branch reloc is the only one we can convert. */
10687 r->r_addend -= symval;
10694 get_r2off (struct bfd_link_info *info,
10695 struct ppc_stub_hash_entry *stub_entry)
10697 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10698 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10702 /* Support linking -R objects. Get the toc pointer from the
10705 if (!htab->opd_abi)
10707 asection *opd = stub_entry->h->elf.root.u.def.section;
10708 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10710 if (strcmp (opd->name, ".opd") != 0
10711 || opd->reloc_count != 0)
10713 info->callbacks->einfo
10714 (_("%P: cannot find opd entry toc for `%pT'\n"),
10715 stub_entry->h->elf.root.root.string);
10716 bfd_set_error (bfd_error_bad_value);
10717 return (bfd_vma) -1;
10719 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10720 return (bfd_vma) -1;
10721 r2off = bfd_get_64 (opd->owner, buf);
10722 r2off -= elf_gp (info->output_bfd);
10724 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10729 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10731 struct ppc_stub_hash_entry *stub_entry;
10732 struct ppc_branch_hash_entry *br_entry;
10733 struct bfd_link_info *info;
10734 struct ppc_link_hash_table *htab;
10736 bfd_byte *p, *relp;
10738 Elf_Internal_Rela *r;
10742 /* Massage our args to the form they really have. */
10743 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10746 htab = ppc_hash_table (info);
10750 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
10751 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10753 htab->stub_count[stub_entry->stub_type - 1] += 1;
10754 switch (stub_entry->stub_type)
10756 case ppc_stub_long_branch:
10757 case ppc_stub_long_branch_r2off:
10758 /* Branches are relative. This is where we are going to. */
10759 targ = (stub_entry->target_value
10760 + stub_entry->target_section->output_offset
10761 + stub_entry->target_section->output_section->vma);
10762 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10764 /* And this is where we are coming from. */
10765 off = (stub_entry->stub_offset
10766 + stub_entry->group->stub_sec->output_offset
10767 + stub_entry->group->stub_sec->output_section->vma);
10771 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10773 bfd_vma r2off = get_r2off (info, stub_entry);
10775 if (r2off == (bfd_vma) -1)
10777 htab->stub_error = TRUE;
10780 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10782 if (PPC_HA (r2off) != 0)
10784 bfd_put_32 (htab->params->stub_bfd,
10785 ADDIS_R2_R2 | PPC_HA (r2off), p);
10788 if (PPC_LO (r2off) != 0)
10790 bfd_put_32 (htab->params->stub_bfd,
10791 ADDI_R2_R2 | PPC_LO (r2off), p);
10796 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10799 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10802 (_("long branch stub `%s' offset overflow"),
10803 stub_entry->root.string);
10804 htab->stub_error = TRUE;
10808 if (info->emitrelocations)
10810 r = get_relocs (stub_entry->group->stub_sec, 1);
10813 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
10814 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10815 r->r_addend = targ;
10816 if (stub_entry->h != NULL
10817 && !use_global_in_relocs (htab, stub_entry, r, 1))
10822 case ppc_stub_plt_branch:
10823 case ppc_stub_plt_branch_r2off:
10824 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10825 stub_entry->root.string + 9,
10827 if (br_entry == NULL)
10829 _bfd_error_handler (_("can't find branch stub `%s'"),
10830 stub_entry->root.string);
10831 htab->stub_error = TRUE;
10835 targ = (stub_entry->target_value
10836 + stub_entry->target_section->output_offset
10837 + stub_entry->target_section->output_section->vma);
10838 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10839 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10841 bfd_put_64 (htab->brlt->owner, targ,
10842 htab->brlt->contents + br_entry->offset);
10844 if (br_entry->iter == htab->stub_iteration)
10846 br_entry->iter = 0;
10848 if (htab->relbrlt != NULL)
10850 /* Create a reloc for the branch lookup table entry. */
10851 Elf_Internal_Rela rela;
10854 rela.r_offset = (br_entry->offset
10855 + htab->brlt->output_offset
10856 + htab->brlt->output_section->vma);
10857 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10858 rela.r_addend = targ;
10860 rl = htab->relbrlt->contents;
10861 rl += (htab->relbrlt->reloc_count++
10862 * sizeof (Elf64_External_Rela));
10863 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10865 else if (info->emitrelocations)
10867 r = get_relocs (htab->brlt, 1);
10870 /* brlt, being SEC_LINKER_CREATED does not go through the
10871 normal reloc processing. Symbols and offsets are not
10872 translated from input file to output file form, so
10873 set up the offset per the output file. */
10874 r->r_offset = (br_entry->offset
10875 + htab->brlt->output_offset
10876 + htab->brlt->output_section->vma);
10877 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10878 r->r_addend = targ;
10882 targ = (br_entry->offset
10883 + htab->brlt->output_offset
10884 + htab->brlt->output_section->vma);
10886 off = (elf_gp (info->output_bfd)
10887 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10890 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10892 info->callbacks->einfo
10893 (_("%P: linkage table error against `%pT'\n"),
10894 stub_entry->root.string);
10895 bfd_set_error (bfd_error_bad_value);
10896 htab->stub_error = TRUE;
10900 if (info->emitrelocations)
10902 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
10905 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10906 if (bfd_big_endian (info->output_bfd))
10907 r[0].r_offset += 2;
10908 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10909 r[0].r_offset += 4;
10910 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10911 r[0].r_addend = targ;
10912 if (PPC_HA (off) != 0)
10914 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10915 r[1].r_offset = r[0].r_offset + 4;
10916 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10917 r[1].r_addend = r[0].r_addend;
10922 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10924 if (PPC_HA (off) != 0)
10926 bfd_put_32 (htab->params->stub_bfd,
10927 ADDIS_R12_R2 | PPC_HA (off), p);
10929 bfd_put_32 (htab->params->stub_bfd,
10930 LD_R12_0R12 | PPC_LO (off), p);
10933 bfd_put_32 (htab->params->stub_bfd,
10934 LD_R12_0R2 | PPC_LO (off), p);
10938 bfd_vma r2off = get_r2off (info, stub_entry);
10940 if (r2off == (bfd_vma) -1)
10942 htab->stub_error = TRUE;
10946 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10948 if (PPC_HA (off) != 0)
10950 bfd_put_32 (htab->params->stub_bfd,
10951 ADDIS_R12_R2 | PPC_HA (off), p);
10953 bfd_put_32 (htab->params->stub_bfd,
10954 LD_R12_0R12 | PPC_LO (off), p);
10957 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
10959 if (PPC_HA (r2off) != 0)
10962 bfd_put_32 (htab->params->stub_bfd,
10963 ADDIS_R2_R2 | PPC_HA (r2off), p);
10965 if (PPC_LO (r2off) != 0)
10968 bfd_put_32 (htab->params->stub_bfd,
10969 ADDI_R2_R2 | PPC_LO (r2off), p);
10973 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10975 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10979 case ppc_stub_long_branch_notoc:
10980 case ppc_stub_long_branch_both:
10981 case ppc_stub_plt_branch_notoc:
10982 case ppc_stub_plt_branch_both:
10983 case ppc_stub_plt_call_notoc:
10984 case ppc_stub_plt_call_both:
10986 off = (stub_entry->stub_offset
10987 + stub_entry->group->stub_sec->output_offset
10988 + stub_entry->group->stub_sec->output_section->vma);
10989 if (stub_entry->stub_type == ppc_stub_long_branch_both
10990 || stub_entry->stub_type == ppc_stub_plt_branch_both
10991 || stub_entry->stub_type == ppc_stub_plt_call_both)
10994 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10997 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10999 targ = stub_entry->plt_ent->plt.offset & ~1;
11000 if (targ >= (bfd_vma) -2)
11003 plt = htab->elf.splt;
11004 if (!htab->elf.dynamic_sections_created
11005 || stub_entry->h == NULL
11006 || stub_entry->h->elf.dynindx == -1)
11008 if (stub_entry->symtype == STT_GNU_IFUNC)
11009 plt = htab->elf.iplt;
11011 plt = htab->pltlocal;
11013 targ += plt->output_offset + plt->output_section->vma;
11016 targ = (stub_entry->target_value
11017 + stub_entry->target_section->output_offset
11018 + stub_entry->target_section->output_section->vma);
11023 /* The notoc stubs calculate their target (either a PLT entry or
11024 the global entry point of a function) relative to the PC
11025 returned by the "bcl" two instructions past the start of the
11026 sequence emitted by build_offset. The offset is therefore 8
11027 less than calculated from the start of the sequence. */
11029 p = build_offset (htab->params->stub_bfd, p, off,
11030 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11031 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
11035 from = (stub_entry->stub_offset
11036 + stub_entry->group->stub_sec->output_offset
11037 + stub_entry->group->stub_sec->output_section->vma
11039 bfd_put_32 (htab->params->stub_bfd,
11040 B_DOT | ((targ - from) & 0x3fffffc), p);
11044 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11046 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11050 if (info->emitrelocations)
11053 num_rel += num_relocs_for_offset (off);
11054 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11057 roff = relp + 16 - stub_entry->group->stub_sec->contents;
11058 r = emit_relocs_for_offset (info, r, roff, targ, off);
11059 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11060 || stub_entry->stub_type == ppc_stub_long_branch_both)
11063 roff = p - 4 - stub_entry->group->stub_sec->contents;
11064 r->r_offset = roff;
11065 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11066 r->r_addend = targ;
11067 if (stub_entry->h != NULL
11068 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11073 if (htab->glink_eh_frame != NULL
11074 && htab->glink_eh_frame->size != 0)
11076 bfd_byte *base, *eh;
11077 unsigned int lr_used, delta;
11079 base = (htab->glink_eh_frame->contents
11080 + stub_entry->group->eh_base + 17);
11081 eh = base + stub_entry->group->eh_size;
11082 lr_used = stub_entry->stub_offset + 8;
11083 if (stub_entry->stub_type == ppc_stub_long_branch_both
11084 || stub_entry->stub_type == ppc_stub_plt_branch_both
11085 || stub_entry->stub_type == ppc_stub_plt_call_both)
11087 delta = lr_used - stub_entry->group->lr_restore;
11088 stub_entry->group->lr_restore = lr_used + 8;
11089 eh = eh_advance (htab->elf.dynobj, eh, delta);
11090 *eh++ = DW_CFA_register;
11093 *eh++ = DW_CFA_advance_loc + 2;
11094 *eh++ = DW_CFA_restore_extended;
11096 stub_entry->group->eh_size = eh - base;
11100 case ppc_stub_plt_call:
11101 case ppc_stub_plt_call_r2save:
11102 if (stub_entry->h != NULL
11103 && stub_entry->h->is_func_descriptor
11104 && stub_entry->h->oh != NULL)
11106 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11108 /* If the old-ABI "dot-symbol" is undefined make it weak so
11109 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11110 if (fh->elf.root.type == bfd_link_hash_undefined
11111 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11112 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11113 fh->elf.root.type = bfd_link_hash_undefweak;
11116 /* Now build the stub. */
11117 targ = stub_entry->plt_ent->plt.offset & ~1;
11118 if (targ >= (bfd_vma) -2)
11121 plt = htab->elf.splt;
11122 if (!htab->elf.dynamic_sections_created
11123 || stub_entry->h == NULL
11124 || stub_entry->h->elf.dynindx == -1)
11126 if (stub_entry->symtype == STT_GNU_IFUNC)
11127 plt = htab->elf.iplt;
11129 plt = htab->pltlocal;
11131 targ += plt->output_offset + plt->output_section->vma;
11133 off = (elf_gp (info->output_bfd)
11134 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11137 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11139 info->callbacks->einfo
11140 /* xgettext:c-format */
11141 (_("%P: linkage table error against `%pT'\n"),
11142 stub_entry->h != NULL
11143 ? stub_entry->h->elf.root.root.string
11145 bfd_set_error (bfd_error_bad_value);
11146 htab->stub_error = TRUE;
11151 if (info->emitrelocations)
11153 r = get_relocs (stub_entry->group->stub_sec,
11154 ((PPC_HA (off) != 0)
11156 ? 2 + (htab->params->plt_static_chain
11157 && PPC_HA (off + 16) == PPC_HA (off))
11161 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11162 if (bfd_big_endian (info->output_bfd))
11163 r[0].r_offset += 2;
11164 r[0].r_addend = targ;
11166 if (stub_entry->h != NULL
11167 && (stub_entry->h == htab->tls_get_addr_fd
11168 || stub_entry->h == htab->tls_get_addr)
11169 && htab->params->tls_get_addr_opt)
11170 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11172 p = build_plt_stub (htab, stub_entry, loc, off, r);
11175 case ppc_stub_save_res:
11183 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
11185 if (htab->params->emit_stub_syms)
11187 struct elf_link_hash_entry *h;
11190 const char *const stub_str[] = { "long_branch",
11203 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11204 len2 = strlen (stub_entry->root.string);
11205 name = bfd_malloc (len1 + len2 + 2);
11208 memcpy (name, stub_entry->root.string, 9);
11209 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11210 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11211 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11214 if (h->root.type == bfd_link_hash_new)
11216 h->root.type = bfd_link_hash_defined;
11217 h->root.u.def.section = stub_entry->group->stub_sec;
11218 h->root.u.def.value = stub_entry->stub_offset;
11219 h->ref_regular = 1;
11220 h->def_regular = 1;
11221 h->ref_regular_nonweak = 1;
11222 h->forced_local = 1;
11224 h->root.linker_def = 1;
11231 /* As above, but don't actually build the stub. Just bump offset so
11232 we know stub section sizes, and select plt_branch stubs where
11233 long_branch stubs won't do. */
11236 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11238 struct ppc_stub_hash_entry *stub_entry;
11239 struct bfd_link_info *info;
11240 struct ppc_link_hash_table *htab;
11242 bfd_vma targ, off, r2off;
11243 unsigned int size, extra, lr_used, delta;
11245 /* Massage our args to the form they really have. */
11246 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11249 htab = ppc_hash_table (info);
11253 /* Make a note of the offset within the stubs for this entry. */
11254 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11256 if (stub_entry->h != NULL
11257 && stub_entry->h->save_res
11258 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11259 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11261 /* Don't make stubs to out-of-line register save/restore
11262 functions. Instead, emit copies of the functions. */
11263 stub_entry->group->needs_save_res = 1;
11264 stub_entry->stub_type = ppc_stub_save_res;
11268 switch (stub_entry->stub_type)
11270 case ppc_stub_plt_branch:
11271 case ppc_stub_plt_branch_r2off:
11272 /* Reset the stub type from the plt branch variant in case we now
11273 can reach with a shorter stub. */
11274 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11275 /* Fall through. */
11276 case ppc_stub_long_branch:
11277 case ppc_stub_long_branch_r2off:
11278 targ = (stub_entry->target_value
11279 + stub_entry->target_section->output_offset
11280 + stub_entry->target_section->output_section->vma);
11281 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11282 off = (stub_entry->stub_offset
11283 + stub_entry->group->stub_sec->output_offset
11284 + stub_entry->group->stub_sec->output_section->vma);
11288 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11290 r2off = get_r2off (info, stub_entry);
11291 if (r2off == (bfd_vma) -1)
11293 htab->stub_error = TRUE;
11297 if (PPC_HA (r2off) != 0)
11299 if (PPC_LO (r2off) != 0)
11305 /* If the branch offset is too big, use a ppc_stub_plt_branch.
11306 Do the same for -R objects without function descriptors. */
11307 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
11309 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
11310 || off + (1 << 25) >= (bfd_vma) (1 << 26))
11312 struct ppc_branch_hash_entry *br_entry;
11314 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11315 stub_entry->root.string + 9,
11317 if (br_entry == NULL)
11319 _bfd_error_handler (_("can't build branch stub `%s'"),
11320 stub_entry->root.string);
11321 htab->stub_error = TRUE;
11325 if (br_entry->iter != htab->stub_iteration)
11327 br_entry->iter = htab->stub_iteration;
11328 br_entry->offset = htab->brlt->size;
11329 htab->brlt->size += 8;
11331 if (htab->relbrlt != NULL)
11332 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11333 else if (info->emitrelocations)
11335 htab->brlt->reloc_count += 1;
11336 htab->brlt->flags |= SEC_RELOC;
11340 targ = (br_entry->offset
11341 + htab->brlt->output_offset
11342 + htab->brlt->output_section->vma);
11343 off = (elf_gp (info->output_bfd)
11344 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11347 if (info->emitrelocations)
11349 stub_entry->group->stub_sec->reloc_count
11350 += 1 + (PPC_HA (off) != 0);
11351 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11354 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11355 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11358 if (PPC_HA (off) != 0)
11364 if (PPC_HA (off) != 0)
11367 if (PPC_HA (r2off) != 0)
11369 if (PPC_LO (r2off) != 0)
11373 else if (info->emitrelocations)
11375 stub_entry->group->stub_sec->reloc_count += 1;
11376 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11380 case ppc_stub_plt_branch_notoc:
11381 case ppc_stub_plt_branch_both:
11382 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11383 /* Fall through. */
11384 case ppc_stub_long_branch_notoc:
11385 case ppc_stub_long_branch_both:
11386 off = (stub_entry->stub_offset
11387 + stub_entry->group->stub_sec->output_offset
11388 + stub_entry->group->stub_sec->output_section->vma);
11390 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11393 targ = (stub_entry->target_value
11394 + stub_entry->target_section->output_offset
11395 + stub_entry->target_section->output_section->vma);
11398 if (info->emitrelocations)
11400 stub_entry->group->stub_sec->reloc_count
11401 += num_relocs_for_offset (off);
11402 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11405 extra = size_offset (off - 8);
11406 /* Include branch insn plus those in the offset sequence. */
11408 /* The branch insn is at the end, or "extra" bytes along. So
11409 its offset will be "extra" bytes less that that already
11413 /* After the bcl, lr has been modified so we need to emit
11414 .eh_frame info saying the return address is in r12. */
11415 lr_used = stub_entry->stub_offset + 8;
11416 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11418 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11419 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11420 DW_CFA_restore_extended 65. */
11421 delta = lr_used - stub_entry->group->lr_restore;
11422 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11423 stub_entry->group->lr_restore = lr_used + 8;
11425 /* If the branch can't reach, use a plt_branch. */
11426 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11428 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11429 - ppc_stub_long_branch_notoc);
11432 else if (info->emitrelocations)
11433 stub_entry->group->stub_sec->reloc_count +=1;
11436 case ppc_stub_plt_call_notoc:
11437 case ppc_stub_plt_call_both:
11438 off = (stub_entry->stub_offset
11439 + stub_entry->group->stub_sec->output_offset
11440 + stub_entry->group->stub_sec->output_section->vma);
11441 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11443 targ = stub_entry->plt_ent->plt.offset & ~1;
11444 if (targ >= (bfd_vma) -2)
11447 plt = htab->elf.splt;
11448 if (!htab->elf.dynamic_sections_created
11449 || stub_entry->h == NULL
11450 || stub_entry->h->elf.dynindx == -1)
11452 if (stub_entry->symtype == STT_GNU_IFUNC)
11453 plt = htab->elf.iplt;
11455 plt = htab->pltlocal;
11457 targ += plt->output_offset + plt->output_section->vma;
11460 if (htab->params->plt_stub_align != 0)
11462 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11464 stub_entry->group->stub_sec->size += pad;
11465 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11469 if (info->emitrelocations)
11471 stub_entry->group->stub_sec->reloc_count
11472 += num_relocs_for_offset (off - 8);
11473 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11476 size = plt_stub_size (htab, stub_entry, off);
11478 /* After the bcl, lr has been modified so we need to emit
11479 .eh_frame info saying the return address is in r12. */
11480 lr_used = stub_entry->stub_offset + 8;
11481 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11483 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11484 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11485 DW_CFA_restore_extended 65. */
11486 delta = lr_used - stub_entry->group->lr_restore;
11487 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11488 stub_entry->group->lr_restore = lr_used + 8;
11491 case ppc_stub_plt_call:
11492 case ppc_stub_plt_call_r2save:
11493 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11494 if (targ >= (bfd_vma) -2)
11496 plt = htab->elf.splt;
11497 if (!htab->elf.dynamic_sections_created
11498 || stub_entry->h == NULL
11499 || stub_entry->h->elf.dynindx == -1)
11501 if (stub_entry->symtype == STT_GNU_IFUNC)
11502 plt = htab->elf.iplt;
11504 plt = htab->pltlocal;
11506 targ += plt->output_offset + plt->output_section->vma;
11508 off = (elf_gp (info->output_bfd)
11509 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11512 if (htab->params->plt_stub_align != 0)
11514 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11516 stub_entry->group->stub_sec->size += pad;
11517 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11520 if (info->emitrelocations)
11522 stub_entry->group->stub_sec->reloc_count
11523 += ((PPC_HA (off) != 0)
11525 ? 2 + (htab->params->plt_static_chain
11526 && PPC_HA (off + 16) == PPC_HA (off))
11528 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11531 size = plt_stub_size (htab, stub_entry, off);
11533 if (stub_entry->h != NULL
11534 && (stub_entry->h == htab->tls_get_addr_fd
11535 || stub_entry->h == htab->tls_get_addr)
11536 && htab->params->tls_get_addr_opt
11537 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11539 /* After the bctrl, lr has been modified so we need to
11540 emit .eh_frame info saying the return address is
11541 on the stack. In fact we put the EH info specifying
11542 that the return address is on the stack *at* the
11543 call rather than after it, because the EH info for a
11544 call needs to be specified by that point.
11545 See libgcc/unwind-dw2.c execute_cfa_program. */
11546 lr_used = stub_entry->stub_offset + size - 20;
11547 /* The eh_frame info will consist of a DW_CFA_advance_loc
11548 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11549 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
11550 delta = lr_used - stub_entry->group->lr_restore;
11551 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11552 stub_entry->group->lr_restore = size - 4;
11561 stub_entry->group->stub_sec->size += size;
11565 /* Set up various things so that we can make a list of input sections
11566 for each output section included in the link. Returns -1 on error,
11567 0 when no stubs will be needed, and 1 on success. */
11570 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11574 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11579 htab->sec_info_arr_size = _bfd_section_id;
11580 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11581 htab->sec_info = bfd_zmalloc (amt);
11582 if (htab->sec_info == NULL)
11585 /* Set toc_off for com, und, abs and ind sections. */
11586 for (id = 0; id < 3; id++)
11587 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11592 /* Set up for first pass at multitoc partitioning. */
11595 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11597 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11599 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11600 htab->toc_bfd = NULL;
11601 htab->toc_first_sec = NULL;
11604 /* The linker repeatedly calls this function for each TOC input section
11605 and linker generated GOT section. Group input bfds such that the toc
11606 within a group is less than 64k in size. */
11609 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11611 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11612 bfd_vma addr, off, limit;
11617 if (!htab->second_toc_pass)
11619 /* Keep track of the first .toc or .got section for this input bfd. */
11620 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11624 htab->toc_bfd = isec->owner;
11625 htab->toc_first_sec = isec;
11628 addr = isec->output_offset + isec->output_section->vma;
11629 off = addr - htab->toc_curr;
11630 limit = 0x80008000;
11631 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11633 if (off + isec->size > limit)
11635 addr = (htab->toc_first_sec->output_offset
11636 + htab->toc_first_sec->output_section->vma);
11637 htab->toc_curr = addr;
11638 htab->toc_curr &= -TOC_BASE_ALIGN;
11641 /* toc_curr is the base address of this toc group. Set elf_gp
11642 for the input section to be the offset relative to the
11643 output toc base plus 0x8000. Making the input elf_gp an
11644 offset allows us to move the toc as a whole without
11645 recalculating input elf_gp. */
11646 off = htab->toc_curr - elf_gp (info->output_bfd);
11647 off += TOC_BASE_OFF;
11649 /* Die if someone uses a linker script that doesn't keep input
11650 file .toc and .got together. */
11652 && elf_gp (isec->owner) != 0
11653 && elf_gp (isec->owner) != off)
11656 elf_gp (isec->owner) = off;
11660 /* During the second pass toc_first_sec points to the start of
11661 a toc group, and toc_curr is used to track the old elf_gp.
11662 We use toc_bfd to ensure we only look at each bfd once. */
11663 if (htab->toc_bfd == isec->owner)
11665 htab->toc_bfd = isec->owner;
11667 if (htab->toc_first_sec == NULL
11668 || htab->toc_curr != elf_gp (isec->owner))
11670 htab->toc_curr = elf_gp (isec->owner);
11671 htab->toc_first_sec = isec;
11673 addr = (htab->toc_first_sec->output_offset
11674 + htab->toc_first_sec->output_section->vma);
11675 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
11676 elf_gp (isec->owner) = off;
11681 /* Called via elf_link_hash_traverse to merge GOT entries for global
11685 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11687 if (h->root.type == bfd_link_hash_indirect)
11690 merge_got_entries (&h->got.glist);
11695 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11699 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11701 struct got_entry *gent;
11703 if (h->root.type == bfd_link_hash_indirect)
11706 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11707 if (!gent->is_indirect)
11708 allocate_got (h, (struct bfd_link_info *) inf, gent);
11712 /* Called on the first multitoc pass after the last call to
11713 ppc64_elf_next_toc_section. This function removes duplicate GOT
11717 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11719 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11720 struct bfd *ibfd, *ibfd2;
11721 bfd_boolean done_something;
11723 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11725 if (!htab->do_multi_toc)
11728 /* Merge global sym got entries within a toc group. */
11729 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11731 /* And tlsld_got. */
11732 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11734 struct got_entry *ent, *ent2;
11736 if (!is_ppc64_elf (ibfd))
11739 ent = ppc64_tlsld_got (ibfd);
11740 if (!ent->is_indirect
11741 && ent->got.offset != (bfd_vma) -1)
11743 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11745 if (!is_ppc64_elf (ibfd2))
11748 ent2 = ppc64_tlsld_got (ibfd2);
11749 if (!ent2->is_indirect
11750 && ent2->got.offset != (bfd_vma) -1
11751 && elf_gp (ibfd2) == elf_gp (ibfd))
11753 ent2->is_indirect = TRUE;
11754 ent2->got.ent = ent;
11760 /* Zap sizes of got sections. */
11761 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11762 htab->elf.irelplt->size -= htab->got_reli_size;
11763 htab->got_reli_size = 0;
11765 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11767 asection *got, *relgot;
11769 if (!is_ppc64_elf (ibfd))
11772 got = ppc64_elf_tdata (ibfd)->got;
11775 got->rawsize = got->size;
11777 relgot = ppc64_elf_tdata (ibfd)->relgot;
11778 relgot->rawsize = relgot->size;
11783 /* Now reallocate the got, local syms first. We don't need to
11784 allocate section contents again since we never increase size. */
11785 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11787 struct got_entry **lgot_ents;
11788 struct got_entry **end_lgot_ents;
11789 struct plt_entry **local_plt;
11790 struct plt_entry **end_local_plt;
11791 unsigned char *lgot_masks;
11792 bfd_size_type locsymcount;
11793 Elf_Internal_Shdr *symtab_hdr;
11796 if (!is_ppc64_elf (ibfd))
11799 lgot_ents = elf_local_got_ents (ibfd);
11803 symtab_hdr = &elf_symtab_hdr (ibfd);
11804 locsymcount = symtab_hdr->sh_info;
11805 end_lgot_ents = lgot_ents + locsymcount;
11806 local_plt = (struct plt_entry **) end_lgot_ents;
11807 end_local_plt = local_plt + locsymcount;
11808 lgot_masks = (unsigned char *) end_local_plt;
11809 s = ppc64_elf_tdata (ibfd)->got;
11810 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11812 struct got_entry *ent;
11814 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11816 unsigned int ent_size = 8;
11817 unsigned int rel_size = sizeof (Elf64_External_Rela);
11819 ent->got.offset = s->size;
11820 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11825 s->size += ent_size;
11826 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
11828 htab->elf.irelplt->size += rel_size;
11829 htab->got_reli_size += rel_size;
11831 else if (bfd_link_pic (info)
11832 && !((ent->tls_type & TLS_TPREL) != 0
11833 && bfd_link_executable (info)))
11835 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11836 srel->size += rel_size;
11842 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11844 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11846 struct got_entry *ent;
11848 if (!is_ppc64_elf (ibfd))
11851 ent = ppc64_tlsld_got (ibfd);
11852 if (!ent->is_indirect
11853 && ent->got.offset != (bfd_vma) -1)
11855 asection *s = ppc64_elf_tdata (ibfd)->got;
11856 ent->got.offset = s->size;
11858 if (bfd_link_pic (info))
11860 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11861 srel->size += sizeof (Elf64_External_Rela);
11866 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11867 if (!done_something)
11868 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11872 if (!is_ppc64_elf (ibfd))
11875 got = ppc64_elf_tdata (ibfd)->got;
11878 done_something = got->rawsize != got->size;
11879 if (done_something)
11884 if (done_something)
11885 (*htab->params->layout_sections_again) ();
11887 /* Set up for second pass over toc sections to recalculate elf_gp
11888 on input sections. */
11889 htab->toc_bfd = NULL;
11890 htab->toc_first_sec = NULL;
11891 htab->second_toc_pass = TRUE;
11892 return done_something;
11895 /* Called after second pass of multitoc partitioning. */
11898 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11900 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11902 /* After the second pass, toc_curr tracks the TOC offset used
11903 for code sections below in ppc64_elf_next_input_section. */
11904 htab->toc_curr = TOC_BASE_OFF;
11907 /* No toc references were found in ISEC. If the code in ISEC makes no
11908 calls, then there's no need to use toc adjusting stubs when branching
11909 into ISEC. Actually, indirect calls from ISEC are OK as they will
11910 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11911 needed, and 2 if a cyclical call-graph was found but no other reason
11912 for a stub was detected. If called from the top level, a return of
11913 2 means the same as a return of 0. */
11916 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11920 /* Mark this section as checked. */
11921 isec->call_check_done = 1;
11923 /* We know none of our code bearing sections will need toc stubs. */
11924 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11927 if (isec->size == 0)
11930 if (isec->output_section == NULL)
11934 if (isec->reloc_count != 0)
11936 Elf_Internal_Rela *relstart, *rel;
11937 Elf_Internal_Sym *local_syms;
11938 struct ppc_link_hash_table *htab;
11940 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11941 info->keep_memory);
11942 if (relstart == NULL)
11945 /* Look for branches to outside of this section. */
11947 htab = ppc_hash_table (info);
11951 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11953 enum elf_ppc64_reloc_type r_type;
11954 unsigned long r_symndx;
11955 struct elf_link_hash_entry *h;
11956 struct ppc_link_hash_entry *eh;
11957 Elf_Internal_Sym *sym;
11959 struct _opd_sec_data *opd;
11963 r_type = ELF64_R_TYPE (rel->r_info);
11964 if (r_type != R_PPC64_REL24
11965 && r_type != R_PPC64_REL24_NOTOC
11966 && r_type != R_PPC64_REL14
11967 && r_type != R_PPC64_REL14_BRTAKEN
11968 && r_type != R_PPC64_REL14_BRNTAKEN
11969 && r_type != R_PPC64_PLTCALL
11970 && r_type != R_PPC64_PLTCALL_NOTOC)
11973 r_symndx = ELF64_R_SYM (rel->r_info);
11974 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11981 /* Calls to dynamic lib functions go through a plt call stub
11983 eh = (struct ppc_link_hash_entry *) h;
11985 && (eh->elf.plt.plist != NULL
11987 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11993 if (sym_sec == NULL)
11994 /* Ignore other undefined symbols. */
11997 /* Assume branches to other sections not included in the
11998 link need stubs too, to cover -R and absolute syms. */
11999 if (sym_sec->output_section == NULL)
12006 sym_value = sym->st_value;
12009 if (h->root.type != bfd_link_hash_defined
12010 && h->root.type != bfd_link_hash_defweak)
12012 sym_value = h->root.u.def.value;
12014 sym_value += rel->r_addend;
12016 /* If this branch reloc uses an opd sym, find the code section. */
12017 opd = get_opd_info (sym_sec);
12020 if (h == NULL && opd->adjust != NULL)
12024 adjust = opd->adjust[OPD_NDX (sym_value)];
12026 /* Assume deleted functions won't ever be called. */
12028 sym_value += adjust;
12031 dest = opd_entry_value (sym_sec, sym_value,
12032 &sym_sec, NULL, FALSE);
12033 if (dest == (bfd_vma) -1)
12038 + sym_sec->output_offset
12039 + sym_sec->output_section->vma);
12041 /* Ignore branch to self. */
12042 if (sym_sec == isec)
12045 /* If the called function uses the toc, we need a stub. */
12046 if (sym_sec->has_toc_reloc
12047 || sym_sec->makes_toc_func_call)
12053 /* Assume any branch that needs a long branch stub might in fact
12054 need a plt_branch stub. A plt_branch stub uses r2. */
12055 else if (dest - (isec->output_offset
12056 + isec->output_section->vma
12057 + rel->r_offset) + (1 << 25)
12058 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12066 /* If calling back to a section in the process of being
12067 tested, we can't say for sure that no toc adjusting stubs
12068 are needed, so don't return zero. */
12069 else if (sym_sec->call_check_in_progress)
12072 /* Branches to another section that itself doesn't have any TOC
12073 references are OK. Recursively call ourselves to check. */
12074 else if (!sym_sec->call_check_done)
12078 /* Mark current section as indeterminate, so that other
12079 sections that call back to current won't be marked as
12081 isec->call_check_in_progress = 1;
12082 recur = toc_adjusting_stub_needed (info, sym_sec);
12083 isec->call_check_in_progress = 0;
12094 if (local_syms != NULL
12095 && (elf_symtab_hdr (isec->owner).contents
12096 != (unsigned char *) local_syms))
12098 if (elf_section_data (isec)->relocs != relstart)
12103 && isec->map_head.s != NULL
12104 && (strcmp (isec->output_section->name, ".init") == 0
12105 || strcmp (isec->output_section->name, ".fini") == 0))
12107 if (isec->map_head.s->has_toc_reloc
12108 || isec->map_head.s->makes_toc_func_call)
12110 else if (!isec->map_head.s->call_check_done)
12113 isec->call_check_in_progress = 1;
12114 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12115 isec->call_check_in_progress = 0;
12122 isec->makes_toc_func_call = 1;
12127 /* The linker repeatedly calls this function for each input section,
12128 in the order that input sections are linked into output sections.
12129 Build lists of input sections to determine groupings between which
12130 we may insert linker stubs. */
12133 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12135 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12140 if ((isec->output_section->flags & SEC_CODE) != 0
12141 && isec->output_section->id < htab->sec_info_arr_size)
12143 /* This happens to make the list in reverse order,
12144 which is what we want. */
12145 htab->sec_info[isec->id].u.list
12146 = htab->sec_info[isec->output_section->id].u.list;
12147 htab->sec_info[isec->output_section->id].u.list = isec;
12150 if (htab->multi_toc_needed)
12152 /* Analyse sections that aren't already flagged as needing a
12153 valid toc pointer. Exclude .fixup for the linux kernel.
12154 .fixup contains branches, but only back to the function that
12155 hit an exception. */
12156 if (!(isec->has_toc_reloc
12157 || (isec->flags & SEC_CODE) == 0
12158 || strcmp (isec->name, ".fixup") == 0
12159 || isec->call_check_done))
12161 if (toc_adjusting_stub_needed (info, isec) < 0)
12164 /* Make all sections use the TOC assigned for this object file.
12165 This will be wrong for pasted sections; We fix that in
12166 check_pasted_section(). */
12167 if (elf_gp (isec->owner) != 0)
12168 htab->toc_curr = elf_gp (isec->owner);
12171 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12175 /* Check that all .init and .fini sections use the same toc, if they
12176 have toc relocs. */
12179 check_pasted_section (struct bfd_link_info *info, const char *name)
12181 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12185 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12186 bfd_vma toc_off = 0;
12189 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12190 if (i->has_toc_reloc)
12193 toc_off = htab->sec_info[i->id].toc_off;
12194 else if (toc_off != htab->sec_info[i->id].toc_off)
12199 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12200 if (i->makes_toc_func_call)
12202 toc_off = htab->sec_info[i->id].toc_off;
12206 /* Make sure the whole pasted function uses the same toc offset. */
12208 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12209 htab->sec_info[i->id].toc_off = toc_off;
12215 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12217 return (check_pasted_section (info, ".init")
12218 & check_pasted_section (info, ".fini"));
12221 /* See whether we can group stub sections together. Grouping stub
12222 sections may result in fewer stubs. More importantly, we need to
12223 put all .init* and .fini* stubs at the beginning of the .init or
12224 .fini output sections respectively, because glibc splits the
12225 _init and _fini functions into multiple parts. Putting a stub in
12226 the middle of a function is not a good idea. */
12229 group_sections (struct bfd_link_info *info,
12230 bfd_size_type stub_group_size,
12231 bfd_boolean stubs_always_before_branch)
12233 struct ppc_link_hash_table *htab;
12235 bfd_boolean suppress_size_errors;
12237 htab = ppc_hash_table (info);
12241 suppress_size_errors = FALSE;
12242 if (stub_group_size == 1)
12244 /* Default values. */
12245 if (stubs_always_before_branch)
12246 stub_group_size = 0x1e00000;
12248 stub_group_size = 0x1c00000;
12249 suppress_size_errors = TRUE;
12252 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12256 if (osec->id >= htab->sec_info_arr_size)
12259 tail = htab->sec_info[osec->id].u.list;
12260 while (tail != NULL)
12264 bfd_size_type total;
12265 bfd_boolean big_sec;
12267 struct map_stub *group;
12268 bfd_size_type group_size;
12271 total = tail->size;
12272 group_size = (ppc64_elf_section_data (tail) != NULL
12273 && ppc64_elf_section_data (tail)->has_14bit_branch
12274 ? stub_group_size >> 10 : stub_group_size);
12276 big_sec = total > group_size;
12277 if (big_sec && !suppress_size_errors)
12278 /* xgettext:c-format */
12279 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12280 tail->owner, tail);
12281 curr_toc = htab->sec_info[tail->id].toc_off;
12283 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12284 && ((total += curr->output_offset - prev->output_offset)
12285 < (ppc64_elf_section_data (prev) != NULL
12286 && ppc64_elf_section_data (prev)->has_14bit_branch
12287 ? (group_size = stub_group_size >> 10) : group_size))
12288 && htab->sec_info[prev->id].toc_off == curr_toc)
12291 /* OK, the size from the start of CURR to the end is less
12292 than group_size and thus can be handled by one stub
12293 section. (or the tail section is itself larger than
12294 group_size, in which case we may be toast.) We should
12295 really be keeping track of the total size of stubs added
12296 here, as stubs contribute to the final output section
12297 size. That's a little tricky, and this way will only
12298 break if stubs added make the total size more than 2^25,
12299 ie. for the default stub_group_size, if stubs total more
12300 than 2097152 bytes, or nearly 75000 plt call stubs. */
12301 group = bfd_alloc (curr->owner, sizeof (*group));
12304 group->link_sec = curr;
12305 group->stub_sec = NULL;
12306 group->needs_save_res = 0;
12307 group->lr_restore = 0;
12308 group->eh_size = 0;
12309 group->eh_base = 0;
12310 group->next = htab->group;
12311 htab->group = group;
12314 prev = htab->sec_info[tail->id].u.list;
12315 /* Set up this stub group. */
12316 htab->sec_info[tail->id].u.group = group;
12318 while (tail != curr && (tail = prev) != NULL);
12320 /* But wait, there's more! Input sections up to group_size
12321 bytes before the stub section can be handled by it too.
12322 Don't do this if we have a really large section after the
12323 stubs, as adding more stubs increases the chance that
12324 branches may not reach into the stub section. */
12325 if (!stubs_always_before_branch && !big_sec)
12328 while (prev != NULL
12329 && ((total += tail->output_offset - prev->output_offset)
12330 < (ppc64_elf_section_data (prev) != NULL
12331 && ppc64_elf_section_data (prev)->has_14bit_branch
12332 ? (group_size = stub_group_size >> 10)
12334 && htab->sec_info[prev->id].toc_off == curr_toc)
12337 prev = htab->sec_info[tail->id].u.list;
12338 htab->sec_info[tail->id].u.group = group;
12347 static const unsigned char glink_eh_frame_cie[] =
12349 0, 0, 0, 16, /* length. */
12350 0, 0, 0, 0, /* id. */
12351 1, /* CIE version. */
12352 'z', 'R', 0, /* Augmentation string. */
12353 4, /* Code alignment. */
12354 0x78, /* Data alignment. */
12356 1, /* Augmentation size. */
12357 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12358 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
12361 /* Stripping output sections is normally done before dynamic section
12362 symbols have been allocated. This function is called later, and
12363 handles cases like htab->brlt which is mapped to its own output
12367 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12369 if (isec->size == 0
12370 && isec->output_section->size == 0
12371 && !(isec->output_section->flags & SEC_KEEP)
12372 && !bfd_section_removed_from_list (info->output_bfd,
12373 isec->output_section)
12374 && elf_section_data (isec->output_section)->dynindx == 0)
12376 isec->output_section->flags |= SEC_EXCLUDE;
12377 bfd_section_list_remove (info->output_bfd, isec->output_section);
12378 info->output_bfd->section_count--;
12382 /* Determine and set the size of the stub section for a final link.
12384 The basic idea here is to examine all the relocations looking for
12385 PC-relative calls to a target that is unreachable with a "bl"
12389 ppc64_elf_size_stubs (struct bfd_link_info *info)
12391 bfd_size_type stub_group_size;
12392 bfd_boolean stubs_always_before_branch;
12393 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12398 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12399 htab->params->plt_thread_safe = 1;
12400 if (!htab->opd_abi)
12401 htab->params->plt_thread_safe = 0;
12402 else if (htab->params->plt_thread_safe == -1)
12404 static const char *const thread_starter[] =
12408 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12410 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12411 "mq_notify", "create_timer",
12416 "GOMP_parallel_start",
12417 "GOMP_parallel_loop_static",
12418 "GOMP_parallel_loop_static_start",
12419 "GOMP_parallel_loop_dynamic",
12420 "GOMP_parallel_loop_dynamic_start",
12421 "GOMP_parallel_loop_guided",
12422 "GOMP_parallel_loop_guided_start",
12423 "GOMP_parallel_loop_runtime",
12424 "GOMP_parallel_loop_runtime_start",
12425 "GOMP_parallel_sections",
12426 "GOMP_parallel_sections_start",
12432 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12434 struct elf_link_hash_entry *h;
12435 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12436 FALSE, FALSE, TRUE);
12437 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12438 if (htab->params->plt_thread_safe)
12442 stubs_always_before_branch = htab->params->group_size < 0;
12443 if (htab->params->group_size < 0)
12444 stub_group_size = -htab->params->group_size;
12446 stub_group_size = htab->params->group_size;
12448 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12451 #define STUB_SHRINK_ITER 20
12452 /* Loop until no stubs added. After iteration 20 of this loop we may
12453 exit on a stub section shrinking. This is to break out of a
12454 pathological case where adding stubs on one iteration decreases
12455 section gaps (perhaps due to alignment), which then requires
12456 fewer or smaller stubs on the next iteration. */
12461 unsigned int bfd_indx;
12462 struct map_stub *group;
12464 htab->stub_iteration += 1;
12466 for (input_bfd = info->input_bfds, bfd_indx = 0;
12468 input_bfd = input_bfd->link.next, bfd_indx++)
12470 Elf_Internal_Shdr *symtab_hdr;
12472 Elf_Internal_Sym *local_syms = NULL;
12474 if (!is_ppc64_elf (input_bfd))
12477 /* We'll need the symbol table in a second. */
12478 symtab_hdr = &elf_symtab_hdr (input_bfd);
12479 if (symtab_hdr->sh_info == 0)
12482 /* Walk over each section attached to the input bfd. */
12483 for (section = input_bfd->sections;
12485 section = section->next)
12487 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12489 /* If there aren't any relocs, then there's nothing more
12491 if ((section->flags & SEC_RELOC) == 0
12492 || (section->flags & SEC_ALLOC) == 0
12493 || (section->flags & SEC_LOAD) == 0
12494 || (section->flags & SEC_CODE) == 0
12495 || section->reloc_count == 0)
12498 /* If this section is a link-once section that will be
12499 discarded, then don't create any stubs. */
12500 if (section->output_section == NULL
12501 || section->output_section->owner != info->output_bfd)
12504 /* Get the relocs. */
12506 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12507 info->keep_memory);
12508 if (internal_relocs == NULL)
12509 goto error_ret_free_local;
12511 /* Now examine each relocation. */
12512 irela = internal_relocs;
12513 irelaend = irela + section->reloc_count;
12514 for (; irela < irelaend; irela++)
12516 enum elf_ppc64_reloc_type r_type;
12517 unsigned int r_indx;
12518 enum ppc_stub_type stub_type;
12519 struct ppc_stub_hash_entry *stub_entry;
12520 asection *sym_sec, *code_sec;
12521 bfd_vma sym_value, code_value;
12522 bfd_vma destination;
12523 unsigned long local_off;
12524 bfd_boolean ok_dest;
12525 struct ppc_link_hash_entry *hash;
12526 struct ppc_link_hash_entry *fdh;
12527 struct elf_link_hash_entry *h;
12528 Elf_Internal_Sym *sym;
12530 const asection *id_sec;
12531 struct _opd_sec_data *opd;
12532 struct plt_entry *plt_ent;
12534 r_type = ELF64_R_TYPE (irela->r_info);
12535 r_indx = ELF64_R_SYM (irela->r_info);
12537 if (r_type >= R_PPC64_max)
12539 bfd_set_error (bfd_error_bad_value);
12540 goto error_ret_free_internal;
12543 /* Only look for stubs on branch instructions. */
12544 if (r_type != R_PPC64_REL24
12545 && r_type != R_PPC64_REL24_NOTOC
12546 && r_type != R_PPC64_REL14
12547 && r_type != R_PPC64_REL14_BRTAKEN
12548 && r_type != R_PPC64_REL14_BRNTAKEN)
12551 /* Now determine the call target, its name, value,
12553 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12554 r_indx, input_bfd))
12555 goto error_ret_free_internal;
12556 hash = (struct ppc_link_hash_entry *) h;
12563 sym_value = sym->st_value;
12564 if (sym_sec != NULL
12565 && sym_sec->output_section != NULL)
12568 else if (hash->elf.root.type == bfd_link_hash_defined
12569 || hash->elf.root.type == bfd_link_hash_defweak)
12571 sym_value = hash->elf.root.u.def.value;
12572 if (sym_sec->output_section != NULL)
12575 else if (hash->elf.root.type == bfd_link_hash_undefweak
12576 || hash->elf.root.type == bfd_link_hash_undefined)
12578 /* Recognise an old ABI func code entry sym, and
12579 use the func descriptor sym instead if it is
12581 if (hash->elf.root.root.string[0] == '.'
12582 && hash->oh != NULL)
12584 fdh = ppc_follow_link (hash->oh);
12585 if (fdh->elf.root.type == bfd_link_hash_defined
12586 || fdh->elf.root.type == bfd_link_hash_defweak)
12588 sym_sec = fdh->elf.root.u.def.section;
12589 sym_value = fdh->elf.root.u.def.value;
12590 if (sym_sec->output_section != NULL)
12599 bfd_set_error (bfd_error_bad_value);
12600 goto error_ret_free_internal;
12607 sym_value += irela->r_addend;
12608 destination = (sym_value
12609 + sym_sec->output_offset
12610 + sym_sec->output_section->vma);
12611 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12616 code_sec = sym_sec;
12617 code_value = sym_value;
12618 opd = get_opd_info (sym_sec);
12623 if (hash == NULL && opd->adjust != NULL)
12625 long adjust = opd->adjust[OPD_NDX (sym_value)];
12628 code_value += adjust;
12629 sym_value += adjust;
12631 dest = opd_entry_value (sym_sec, sym_value,
12632 &code_sec, &code_value, FALSE);
12633 if (dest != (bfd_vma) -1)
12635 destination = dest;
12638 /* Fixup old ABI sym to point at code
12640 hash->elf.root.type = bfd_link_hash_defweak;
12641 hash->elf.root.u.def.section = code_sec;
12642 hash->elf.root.u.def.value = code_value;
12647 /* Determine what (if any) linker stub is needed. */
12649 stub_type = ppc_type_of_stub (section, irela, &hash,
12650 &plt_ent, destination,
12653 if (r_type == R_PPC64_REL24_NOTOC)
12655 if (stub_type == ppc_stub_plt_call)
12656 stub_type = ppc_stub_plt_call_notoc;
12657 else if (stub_type == ppc_stub_long_branch
12658 || (code_sec != NULL
12659 && code_sec->output_section != NULL
12660 && (((hash ? hash->elf.other : sym->st_other)
12661 & STO_PPC64_LOCAL_MASK)
12662 != 1 << STO_PPC64_LOCAL_BIT)))
12663 stub_type = ppc_stub_long_branch_notoc;
12665 else if (stub_type != ppc_stub_plt_call)
12667 /* Check whether we need a TOC adjusting stub.
12668 Since the linker pastes together pieces from
12669 different object files when creating the
12670 _init and _fini functions, it may be that a
12671 call to what looks like a local sym is in
12672 fact a call needing a TOC adjustment. */
12673 if ((code_sec != NULL
12674 && code_sec->output_section != NULL
12675 && (htab->sec_info[code_sec->id].toc_off
12676 != htab->sec_info[section->id].toc_off)
12677 && (code_sec->has_toc_reloc
12678 || code_sec->makes_toc_func_call))
12679 || (((hash ? hash->elf.other : sym->st_other)
12680 & STO_PPC64_LOCAL_MASK)
12681 == 1 << STO_PPC64_LOCAL_BIT))
12682 stub_type = ppc_stub_long_branch_r2off;
12685 if (stub_type == ppc_stub_none)
12688 /* __tls_get_addr calls might be eliminated. */
12689 if (stub_type != ppc_stub_plt_call
12690 && stub_type != ppc_stub_plt_call_notoc
12692 && (hash == htab->tls_get_addr
12693 || hash == htab->tls_get_addr_fd)
12694 && section->has_tls_reloc
12695 && irela != internal_relocs)
12697 /* Get tls info. */
12698 unsigned char *tls_mask;
12700 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12701 irela - 1, input_bfd))
12702 goto error_ret_free_internal;
12703 if ((*tls_mask & TLS_TLS) != 0)
12707 if (stub_type == ppc_stub_plt_call)
12710 && htab->params->plt_localentry0 != 0
12711 && is_elfv2_localentry0 (&hash->elf))
12712 htab->has_plt_localentry0 = 1;
12713 else if (irela + 1 < irelaend
12714 && irela[1].r_offset == irela->r_offset + 4
12715 && (ELF64_R_TYPE (irela[1].r_info)
12716 == R_PPC64_TOCSAVE))
12718 if (!tocsave_find (htab, INSERT,
12719 &local_syms, irela + 1, input_bfd))
12720 goto error_ret_free_internal;
12723 stub_type = ppc_stub_plt_call_r2save;
12726 /* Support for grouping stub sections. */
12727 id_sec = htab->sec_info[section->id].u.group->link_sec;
12729 /* Get the name of this stub. */
12730 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12732 goto error_ret_free_internal;
12734 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12735 stub_name, FALSE, FALSE);
12736 if (stub_entry != NULL)
12738 enum ppc_stub_type old_type;
12739 /* A stub has already been created, but it may
12740 not be the required type. We shouldn't be
12741 transitioning from plt_call to long_branch
12742 stubs or vice versa, but we might be
12743 upgrading from plt_call to plt_call_r2save or
12744 from long_branch to long_branch_r2off. */
12746 old_type = stub_entry->stub_type;
12752 case ppc_stub_save_res:
12755 case ppc_stub_plt_call:
12756 case ppc_stub_plt_call_r2save:
12757 case ppc_stub_plt_call_notoc:
12758 case ppc_stub_plt_call_both:
12759 if (stub_type == ppc_stub_plt_call)
12761 else if (stub_type == ppc_stub_plt_call_r2save)
12763 if (old_type == ppc_stub_plt_call_notoc)
12764 stub_type = ppc_stub_plt_call_both;
12766 else if (stub_type == ppc_stub_plt_call_notoc)
12768 if (old_type == ppc_stub_plt_call_r2save)
12769 stub_type = ppc_stub_plt_call_both;
12775 case ppc_stub_plt_branch:
12776 case ppc_stub_plt_branch_r2off:
12777 case ppc_stub_plt_branch_notoc:
12778 case ppc_stub_plt_branch_both:
12779 old_type += (ppc_stub_long_branch
12780 - ppc_stub_plt_branch);
12781 /* Fall through. */
12782 case ppc_stub_long_branch:
12783 case ppc_stub_long_branch_r2off:
12784 case ppc_stub_long_branch_notoc:
12785 case ppc_stub_long_branch_both:
12786 if (stub_type == ppc_stub_long_branch)
12788 else if (stub_type == ppc_stub_long_branch_r2off)
12790 if (old_type == ppc_stub_long_branch_notoc)
12791 stub_type = ppc_stub_long_branch_both;
12793 else if (stub_type == ppc_stub_long_branch_notoc)
12795 if (old_type == ppc_stub_long_branch_r2off)
12796 stub_type = ppc_stub_long_branch_both;
12802 if (old_type < stub_type)
12803 stub_entry->stub_type = stub_type;
12807 stub_entry = ppc_add_stub (stub_name, section, info);
12808 if (stub_entry == NULL)
12811 error_ret_free_internal:
12812 if (elf_section_data (section)->relocs == NULL)
12813 free (internal_relocs);
12814 error_ret_free_local:
12815 if (local_syms != NULL
12816 && (symtab_hdr->contents
12817 != (unsigned char *) local_syms))
12822 stub_entry->stub_type = stub_type;
12823 if (stub_type >= ppc_stub_plt_call
12824 && stub_type <= ppc_stub_plt_call_both)
12826 stub_entry->target_value = sym_value;
12827 stub_entry->target_section = sym_sec;
12831 stub_entry->target_value = code_value;
12832 stub_entry->target_section = code_sec;
12834 stub_entry->h = hash;
12835 stub_entry->plt_ent = plt_ent;
12836 stub_entry->symtype
12837 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
12838 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12841 && (hash->elf.root.type == bfd_link_hash_defined
12842 || hash->elf.root.type == bfd_link_hash_defweak))
12843 htab->stub_globals += 1;
12846 /* We're done with the internal relocs, free them. */
12847 if (elf_section_data (section)->relocs != internal_relocs)
12848 free (internal_relocs);
12851 if (local_syms != NULL
12852 && symtab_hdr->contents != (unsigned char *) local_syms)
12854 if (!info->keep_memory)
12857 symtab_hdr->contents = (unsigned char *) local_syms;
12861 /* We may have added some stubs. Find out the new size of the
12863 for (group = htab->group; group != NULL; group = group->next)
12865 group->lr_restore = 0;
12866 group->eh_size = 0;
12867 if (group->stub_sec != NULL)
12869 asection *stub_sec = group->stub_sec;
12871 if (htab->stub_iteration <= STUB_SHRINK_ITER
12872 || stub_sec->rawsize < stub_sec->size)
12873 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12874 stub_sec->rawsize = stub_sec->size;
12875 stub_sec->size = 0;
12876 stub_sec->reloc_count = 0;
12877 stub_sec->flags &= ~SEC_RELOC;
12881 if (htab->stub_iteration <= STUB_SHRINK_ITER
12882 || htab->brlt->rawsize < htab->brlt->size)
12883 htab->brlt->rawsize = htab->brlt->size;
12884 htab->brlt->size = 0;
12885 htab->brlt->reloc_count = 0;
12886 htab->brlt->flags &= ~SEC_RELOC;
12887 if (htab->relbrlt != NULL)
12888 htab->relbrlt->size = 0;
12890 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12892 for (group = htab->group; group != NULL; group = group->next)
12893 if (group->needs_save_res)
12894 group->stub_sec->size += htab->sfpr->size;
12896 if (info->emitrelocations
12897 && htab->glink != NULL && htab->glink->size != 0)
12899 htab->glink->reloc_count = 1;
12900 htab->glink->flags |= SEC_RELOC;
12903 if (htab->glink_eh_frame != NULL
12904 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12905 && htab->glink_eh_frame->output_section->size > 8)
12907 size_t size = 0, align = 4;
12909 for (group = htab->group; group != NULL; group = group->next)
12910 if (group->eh_size != 0)
12911 size += (group->eh_size + 17 + align - 1) & -align;
12912 if (htab->glink != NULL && htab->glink->size != 0)
12913 size += (24 + align - 1) & -align;
12915 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12916 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12917 size = (size + align - 1) & -align;
12918 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12919 htab->glink_eh_frame->size = size;
12922 if (htab->params->plt_stub_align != 0)
12923 for (group = htab->group; group != NULL; group = group->next)
12924 if (group->stub_sec != NULL)
12926 int align = abs (htab->params->plt_stub_align);
12927 group->stub_sec->size
12928 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12931 for (group = htab->group; group != NULL; group = group->next)
12932 if (group->stub_sec != NULL
12933 && group->stub_sec->rawsize != group->stub_sec->size
12934 && (htab->stub_iteration <= STUB_SHRINK_ITER
12935 || group->stub_sec->rawsize < group->stub_sec->size))
12939 && (htab->brlt->rawsize == htab->brlt->size
12940 || (htab->stub_iteration > STUB_SHRINK_ITER
12941 && htab->brlt->rawsize > htab->brlt->size))
12942 && (htab->glink_eh_frame == NULL
12943 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12946 /* Ask the linker to do its stuff. */
12947 (*htab->params->layout_sections_again) ();
12950 if (htab->glink_eh_frame != NULL
12951 && htab->glink_eh_frame->size != 0)
12954 bfd_byte *p, *last_fde;
12955 size_t last_fde_len, size, align, pad;
12956 struct map_stub *group;
12958 /* It is necessary to at least have a rough outline of the
12959 linker generated CIEs and FDEs written before
12960 bfd_elf_discard_info is run, in order for these FDEs to be
12961 indexed in .eh_frame_hdr. */
12962 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12965 htab->glink_eh_frame->contents = p;
12969 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12970 /* CIE length (rewrite in case little-endian). */
12971 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
12972 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12973 p += last_fde_len + 4;
12975 for (group = htab->group; group != NULL; group = group->next)
12976 if (group->eh_size != 0)
12978 group->eh_base = p - htab->glink_eh_frame->contents;
12980 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
12982 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12985 val = p - htab->glink_eh_frame->contents;
12986 bfd_put_32 (htab->elf.dynobj, val, p);
12988 /* Offset to stub section, written later. */
12990 /* stub section size. */
12991 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
12993 /* Augmentation. */
12995 /* Make sure we don't have all nops. This is enough for
12996 elf-eh-frame.c to detect the last non-nop opcode. */
12997 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
12998 p = last_fde + last_fde_len + 4;
13000 if (htab->glink != NULL && htab->glink->size != 0)
13003 last_fde_len = ((24 + align - 1) & -align) - 4;
13005 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13008 val = p - htab->glink_eh_frame->contents;
13009 bfd_put_32 (htab->elf.dynobj, val, p);
13011 /* Offset to .glink, written later. */
13014 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13016 /* Augmentation. */
13019 *p++ = DW_CFA_advance_loc + 1;
13020 *p++ = DW_CFA_register;
13022 *p++ = htab->opd_abi ? 12 : 0;
13023 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
13024 *p++ = DW_CFA_restore_extended;
13026 p += ((24 + align - 1) & -align) - 24;
13028 /* Subsume any padding into the last FDE if user .eh_frame
13029 sections are aligned more than glink_eh_frame. Otherwise any
13030 zero padding will be seen as a terminator. */
13031 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13032 size = p - htab->glink_eh_frame->contents;
13033 pad = ((size + align - 1) & -align) - size;
13034 htab->glink_eh_frame->size = size + pad;
13035 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13038 maybe_strip_output (info, htab->brlt);
13039 if (htab->glink_eh_frame != NULL)
13040 maybe_strip_output (info, htab->glink_eh_frame);
13045 /* Called after we have determined section placement. If sections
13046 move, we'll be called again. Provide a value for TOCstart. */
13049 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13052 bfd_vma TOCstart, adjust;
13056 struct elf_link_hash_entry *h;
13057 struct elf_link_hash_table *htab = elf_hash_table (info);
13059 if (is_elf_hash_table (htab)
13060 && htab->hgot != NULL)
13064 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13065 if (is_elf_hash_table (htab))
13069 && h->root.type == bfd_link_hash_defined
13070 && !h->root.linker_def
13071 && (!is_elf_hash_table (htab)
13072 || h->def_regular))
13074 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13075 + h->root.u.def.section->output_offset
13076 + h->root.u.def.section->output_section->vma);
13077 _bfd_set_gp_value (obfd, TOCstart);
13082 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13083 order. The TOC starts where the first of these sections starts. */
13084 s = bfd_get_section_by_name (obfd, ".got");
13085 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13086 s = bfd_get_section_by_name (obfd, ".toc");
13087 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13088 s = bfd_get_section_by_name (obfd, ".tocbss");
13089 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13090 s = bfd_get_section_by_name (obfd, ".plt");
13091 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13093 /* This may happen for
13094 o references to TOC base (SYM@toc / TOC[tc0]) without a
13096 o bad linker script
13097 o --gc-sections and empty TOC sections
13099 FIXME: Warn user? */
13101 /* Look for a likely section. We probably won't even be
13103 for (s = obfd->sections; s != NULL; s = s->next)
13104 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13106 == (SEC_ALLOC | SEC_SMALL_DATA))
13109 for (s = obfd->sections; s != NULL; s = s->next)
13110 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13111 == (SEC_ALLOC | SEC_SMALL_DATA))
13114 for (s = obfd->sections; s != NULL; s = s->next)
13115 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13119 for (s = obfd->sections; s != NULL; s = s->next)
13120 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13126 TOCstart = s->output_section->vma + s->output_offset;
13128 /* Force alignment. */
13129 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13130 TOCstart -= adjust;
13131 _bfd_set_gp_value (obfd, TOCstart);
13133 if (info != NULL && s != NULL)
13135 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13139 if (htab->elf.hgot != NULL)
13141 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13142 htab->elf.hgot->root.u.def.section = s;
13147 struct bfd_link_hash_entry *bh = NULL;
13148 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13149 s, TOC_BASE_OFF - adjust,
13150 NULL, FALSE, FALSE, &bh);
13156 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13157 write out any global entry stubs, and PLT relocations. */
13160 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13162 struct bfd_link_info *info;
13163 struct ppc_link_hash_table *htab;
13164 struct plt_entry *ent;
13167 if (h->root.type == bfd_link_hash_indirect)
13171 htab = ppc_hash_table (info);
13175 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13176 if (ent->plt.offset != (bfd_vma) -1)
13178 /* This symbol has an entry in the procedure linkage
13179 table. Set it up. */
13180 Elf_Internal_Rela rela;
13181 asection *plt, *relplt;
13184 if (!htab->elf.dynamic_sections_created
13185 || h->dynindx == -1)
13187 if (!(h->def_regular
13188 && (h->root.type == bfd_link_hash_defined
13189 || h->root.type == bfd_link_hash_defweak)))
13191 if (h->type == STT_GNU_IFUNC)
13193 plt = htab->elf.iplt;
13194 relplt = htab->elf.irelplt;
13195 htab->local_ifunc_resolver = 1;
13197 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13199 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13203 plt = htab->pltlocal;
13204 if (bfd_link_pic (info))
13206 relplt = htab->relpltlocal;
13208 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13210 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13215 rela.r_addend = (h->root.u.def.value
13216 + h->root.u.def.section->output_offset
13217 + h->root.u.def.section->output_section->vma
13220 if (relplt == NULL)
13222 loc = plt->contents + ent->plt.offset;
13223 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13226 bfd_vma toc = elf_gp (info->output_bfd);
13227 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13228 bfd_put_64 (info->output_bfd, toc, loc + 8);
13233 rela.r_offset = (plt->output_section->vma
13234 + plt->output_offset
13235 + ent->plt.offset);
13236 loc = relplt->contents + (relplt->reloc_count++
13237 * sizeof (Elf64_External_Rela));
13238 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13243 rela.r_offset = (htab->elf.splt->output_section->vma
13244 + htab->elf.splt->output_offset
13245 + ent->plt.offset);
13246 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13247 rela.r_addend = ent->addend;
13248 loc = (htab->elf.srelplt->contents
13249 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13250 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13251 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13252 htab->maybe_local_ifunc_resolver = 1;
13253 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13257 if (!h->pointer_equality_needed)
13260 if (h->def_regular)
13263 s = htab->global_entry;
13264 if (s == NULL || s->size == 0)
13267 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13268 if (ent->plt.offset != (bfd_vma) -1
13269 && ent->addend == 0)
13275 p = s->contents + h->root.u.def.value;
13276 plt = htab->elf.splt;
13277 if (!htab->elf.dynamic_sections_created
13278 || h->dynindx == -1)
13280 if (h->type == STT_GNU_IFUNC)
13281 plt = htab->elf.iplt;
13283 plt = htab->pltlocal;
13285 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13286 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13288 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13290 info->callbacks->einfo
13291 (_("%P: linkage table error against `%pT'\n"),
13292 h->root.root.string);
13293 bfd_set_error (bfd_error_bad_value);
13294 htab->stub_error = TRUE;
13297 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13298 if (htab->params->emit_stub_syms)
13300 size_t len = strlen (h->root.root.string);
13301 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13306 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13307 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13310 if (h->root.type == bfd_link_hash_new)
13312 h->root.type = bfd_link_hash_defined;
13313 h->root.u.def.section = s;
13314 h->root.u.def.value = p - s->contents;
13315 h->ref_regular = 1;
13316 h->def_regular = 1;
13317 h->ref_regular_nonweak = 1;
13318 h->forced_local = 1;
13320 h->root.linker_def = 1;
13324 if (PPC_HA (off) != 0)
13326 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13329 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13331 bfd_put_32 (s->owner, MTCTR_R12, p);
13333 bfd_put_32 (s->owner, BCTR, p);
13339 /* Write PLT relocs for locals. */
13342 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13344 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13347 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13349 struct got_entry **lgot_ents, **end_lgot_ents;
13350 struct plt_entry **local_plt, **lplt, **end_local_plt;
13351 Elf_Internal_Shdr *symtab_hdr;
13352 bfd_size_type locsymcount;
13353 Elf_Internal_Sym *local_syms = NULL;
13354 struct plt_entry *ent;
13356 if (!is_ppc64_elf (ibfd))
13359 lgot_ents = elf_local_got_ents (ibfd);
13363 symtab_hdr = &elf_symtab_hdr (ibfd);
13364 locsymcount = symtab_hdr->sh_info;
13365 end_lgot_ents = lgot_ents + locsymcount;
13366 local_plt = (struct plt_entry **) end_lgot_ents;
13367 end_local_plt = local_plt + locsymcount;
13368 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13369 for (ent = *lplt; ent != NULL; ent = ent->next)
13370 if (ent->plt.offset != (bfd_vma) -1)
13372 Elf_Internal_Sym *sym;
13374 asection *plt, *relplt;
13378 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13379 lplt - local_plt, ibfd))
13381 if (local_syms != NULL
13382 && symtab_hdr->contents != (unsigned char *) local_syms)
13387 val = sym->st_value + ent->addend;
13388 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13389 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13390 if (sym_sec != NULL && sym_sec->output_section != NULL)
13391 val += sym_sec->output_offset + sym_sec->output_section->vma;
13393 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13395 htab->local_ifunc_resolver = 1;
13396 plt = htab->elf.iplt;
13397 relplt = htab->elf.irelplt;
13401 plt = htab->pltlocal;
13402 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13405 if (relplt == NULL)
13407 loc = plt->contents + ent->plt.offset;
13408 bfd_put_64 (info->output_bfd, val, loc);
13411 bfd_vma toc = elf_gp (ibfd);
13412 bfd_put_64 (info->output_bfd, toc, loc + 8);
13417 Elf_Internal_Rela rela;
13418 rela.r_offset = (ent->plt.offset
13419 + plt->output_offset
13420 + plt->output_section->vma);
13421 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13424 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13426 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13431 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13433 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13435 rela.r_addend = val;
13436 loc = relplt->contents + (relplt->reloc_count++
13437 * sizeof (Elf64_External_Rela));
13438 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13442 if (local_syms != NULL
13443 && symtab_hdr->contents != (unsigned char *) local_syms)
13445 if (!info->keep_memory)
13448 symtab_hdr->contents = (unsigned char *) local_syms;
13454 /* Build all the stubs associated with the current output file.
13455 The stubs are kept in a hash table attached to the main linker
13456 hash table. This function is called via gldelf64ppc_finish. */
13459 ppc64_elf_build_stubs (struct bfd_link_info *info,
13462 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13463 struct map_stub *group;
13464 asection *stub_sec;
13466 int stub_sec_count = 0;
13471 /* Allocate memory to hold the linker stubs. */
13472 for (group = htab->group; group != NULL; group = group->next)
13474 group->eh_size = 0;
13475 group->lr_restore = 0;
13476 if ((stub_sec = group->stub_sec) != NULL
13477 && stub_sec->size != 0)
13479 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13481 if (stub_sec->contents == NULL)
13483 stub_sec->size = 0;
13487 if (htab->glink != NULL && htab->glink->size != 0)
13492 /* Build the .glink plt call stub. */
13493 if (htab->params->emit_stub_syms)
13495 struct elf_link_hash_entry *h;
13496 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13497 TRUE, FALSE, FALSE);
13500 if (h->root.type == bfd_link_hash_new)
13502 h->root.type = bfd_link_hash_defined;
13503 h->root.u.def.section = htab->glink;
13504 h->root.u.def.value = 8;
13505 h->ref_regular = 1;
13506 h->def_regular = 1;
13507 h->ref_regular_nonweak = 1;
13508 h->forced_local = 1;
13510 h->root.linker_def = 1;
13513 plt0 = (htab->elf.splt->output_section->vma
13514 + htab->elf.splt->output_offset
13516 if (info->emitrelocations)
13518 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13521 r->r_offset = (htab->glink->output_offset
13522 + htab->glink->output_section->vma);
13523 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13524 r->r_addend = plt0;
13526 p = htab->glink->contents;
13527 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13528 bfd_put_64 (htab->glink->owner, plt0, p);
13532 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13534 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13536 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13538 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13540 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13542 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13544 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13546 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13548 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13550 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13555 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13557 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13559 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13561 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13563 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13565 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13567 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13569 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13571 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13573 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13575 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13577 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13579 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13582 bfd_put_32 (htab->glink->owner, BCTR, p);
13584 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13586 /* Build the .glink lazy link call stubs. */
13588 while (p < htab->glink->contents + htab->glink->size)
13594 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13599 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13601 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13606 bfd_put_32 (htab->glink->owner,
13607 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13613 /* Build .glink global entry stubs, and PLT relocs for globals. */
13614 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13616 if (!write_plt_relocs_for_local_syms (info))
13619 if (htab->brlt != NULL && htab->brlt->size != 0)
13621 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13623 if (htab->brlt->contents == NULL)
13626 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13628 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13629 htab->relbrlt->size);
13630 if (htab->relbrlt->contents == NULL)
13634 /* Build the stubs as directed by the stub hash table. */
13635 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13637 for (group = htab->group; group != NULL; group = group->next)
13638 if (group->needs_save_res)
13639 group->stub_sec->size += htab->sfpr->size;
13641 if (htab->relbrlt != NULL)
13642 htab->relbrlt->reloc_count = 0;
13644 if (htab->params->plt_stub_align != 0)
13645 for (group = htab->group; group != NULL; group = group->next)
13646 if ((stub_sec = group->stub_sec) != NULL)
13648 int align = abs (htab->params->plt_stub_align);
13649 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13652 for (group = htab->group; group != NULL; group = group->next)
13653 if (group->needs_save_res)
13655 stub_sec = group->stub_sec;
13656 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13657 htab->sfpr->contents, htab->sfpr->size);
13658 if (htab->params->emit_stub_syms)
13662 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13663 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13668 if (htab->glink_eh_frame != NULL
13669 && htab->glink_eh_frame->size != 0)
13674 p = htab->glink_eh_frame->contents;
13675 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13677 for (group = htab->group; group != NULL; group = group->next)
13678 if (group->eh_size != 0)
13680 /* Offset to stub section. */
13681 val = (group->stub_sec->output_section->vma
13682 + group->stub_sec->output_offset);
13683 val -= (htab->glink_eh_frame->output_section->vma
13684 + htab->glink_eh_frame->output_offset
13685 + (p + 8 - htab->glink_eh_frame->contents));
13686 if (val + 0x80000000 > 0xffffffff)
13689 (_("%s offset too large for .eh_frame sdata4 encoding"),
13690 group->stub_sec->name);
13693 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13694 p += (group->eh_size + 17 + 3) & -4;
13696 if (htab->glink != NULL && htab->glink->size != 0)
13698 /* Offset to .glink. */
13699 val = (htab->glink->output_section->vma
13700 + htab->glink->output_offset
13702 val -= (htab->glink_eh_frame->output_section->vma
13703 + htab->glink_eh_frame->output_offset
13704 + (p + 8 - htab->glink_eh_frame->contents));
13705 if (val + 0x80000000 > 0xffffffff)
13708 (_("%s offset too large for .eh_frame sdata4 encoding"),
13709 htab->glink->name);
13712 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13713 p += (24 + align - 1) & -align;
13717 for (group = htab->group; group != NULL; group = group->next)
13718 if ((stub_sec = group->stub_sec) != NULL)
13720 stub_sec_count += 1;
13721 if (stub_sec->rawsize != stub_sec->size
13722 && (htab->stub_iteration <= STUB_SHRINK_ITER
13723 || stub_sec->rawsize < stub_sec->size))
13729 htab->stub_error = TRUE;
13730 _bfd_error_handler (_("stubs don't match calculated size"));
13733 if (htab->stub_error)
13739 *stats = bfd_malloc (500);
13740 if (*stats == NULL)
13743 len = sprintf (*stats,
13744 ngettext ("linker stubs in %u group\n",
13745 "linker stubs in %u groups\n",
13748 sprintf (*stats + len, _(" branch %lu\n"
13749 " branch toc adj %lu\n"
13750 " branch notoc %lu\n"
13751 " branch both %lu\n"
13752 " long branch %lu\n"
13753 " long toc adj %lu\n"
13754 " long notoc %lu\n"
13757 " plt call save %lu\n"
13758 " plt call notoc %lu\n"
13759 " plt call both %lu\n"
13760 " global entry %lu"),
13761 htab->stub_count[ppc_stub_long_branch - 1],
13762 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13763 htab->stub_count[ppc_stub_long_branch_notoc - 1],
13764 htab->stub_count[ppc_stub_long_branch_both - 1],
13765 htab->stub_count[ppc_stub_plt_branch - 1],
13766 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13767 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
13768 htab->stub_count[ppc_stub_plt_branch_both - 1],
13769 htab->stub_count[ppc_stub_plt_call - 1],
13770 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13771 htab->stub_count[ppc_stub_plt_call_notoc - 1],
13772 htab->stub_count[ppc_stub_plt_call_both - 1],
13773 htab->stub_count[ppc_stub_global_entry - 1]);
13778 /* What to do when ld finds relocations against symbols defined in
13779 discarded sections. */
13781 static unsigned int
13782 ppc64_elf_action_discarded (asection *sec)
13784 if (strcmp (".opd", sec->name) == 0)
13787 if (strcmp (".toc", sec->name) == 0)
13790 if (strcmp (".toc1", sec->name) == 0)
13793 return _bfd_elf_default_action_discarded (sec);
13796 /* The RELOCATE_SECTION function is called by the ELF backend linker
13797 to handle the relocations for a section.
13799 The relocs are always passed as Rela structures; if the section
13800 actually uses Rel structures, the r_addend field will always be
13803 This function is responsible for adjust the section contents as
13804 necessary, and (if using Rela relocs and generating a
13805 relocatable output file) adjusting the reloc addend as
13808 This function does not have to worry about setting the reloc
13809 address or the reloc symbol index.
13811 LOCAL_SYMS is a pointer to the swapped in local symbols.
13813 LOCAL_SECTIONS is an array giving the section in the input file
13814 corresponding to the st_shndx field of each local symbol.
13816 The global hash table entry for the global symbols can be found
13817 via elf_sym_hashes (input_bfd).
13819 When generating relocatable output, this function must handle
13820 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13821 going to be the section symbol corresponding to the output
13822 section, which means that the addend must be adjusted
13826 ppc64_elf_relocate_section (bfd *output_bfd,
13827 struct bfd_link_info *info,
13829 asection *input_section,
13830 bfd_byte *contents,
13831 Elf_Internal_Rela *relocs,
13832 Elf_Internal_Sym *local_syms,
13833 asection **local_sections)
13835 struct ppc_link_hash_table *htab;
13836 Elf_Internal_Shdr *symtab_hdr;
13837 struct elf_link_hash_entry **sym_hashes;
13838 Elf_Internal_Rela *rel;
13839 Elf_Internal_Rela *wrel;
13840 Elf_Internal_Rela *relend;
13841 Elf_Internal_Rela outrel;
13843 struct got_entry **local_got_ents;
13845 bfd_boolean ret = TRUE;
13846 bfd_boolean is_opd;
13847 /* Assume 'at' branch hints. */
13848 bfd_boolean is_isa_v2 = TRUE;
13849 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13851 /* Initialize howto table if needed. */
13852 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13855 htab = ppc_hash_table (info);
13859 /* Don't relocate stub sections. */
13860 if (input_section->owner == htab->params->stub_bfd)
13863 if (!is_ppc64_elf (input_bfd))
13865 bfd_set_error (bfd_error_wrong_format);
13869 local_got_ents = elf_local_got_ents (input_bfd);
13870 TOCstart = elf_gp (output_bfd);
13871 symtab_hdr = &elf_symtab_hdr (input_bfd);
13872 sym_hashes = elf_sym_hashes (input_bfd);
13873 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13875 rel = wrel = relocs;
13876 relend = relocs + input_section->reloc_count;
13877 for (; rel < relend; wrel++, rel++)
13879 enum elf_ppc64_reloc_type r_type;
13881 bfd_reloc_status_type r;
13882 Elf_Internal_Sym *sym;
13884 struct elf_link_hash_entry *h_elf;
13885 struct ppc_link_hash_entry *h;
13886 struct ppc_link_hash_entry *fdh;
13887 const char *sym_name;
13888 unsigned long r_symndx, toc_symndx;
13889 bfd_vma toc_addend;
13890 unsigned char tls_mask, tls_gd, tls_type;
13891 unsigned char sym_type;
13892 bfd_vma relocation;
13893 bfd_boolean unresolved_reloc, save_unresolved_reloc;
13894 bfd_boolean warned;
13895 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13898 struct ppc_stub_hash_entry *stub_entry;
13899 bfd_vma max_br_offset;
13901 Elf_Internal_Rela orig_rel;
13902 reloc_howto_type *howto;
13903 struct reloc_howto_struct alt_howto;
13910 r_type = ELF64_R_TYPE (rel->r_info);
13911 r_symndx = ELF64_R_SYM (rel->r_info);
13913 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13914 symbol of the previous ADDR64 reloc. The symbol gives us the
13915 proper TOC base to use. */
13916 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13918 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13920 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13926 unresolved_reloc = FALSE;
13929 if (r_symndx < symtab_hdr->sh_info)
13931 /* It's a local symbol. */
13932 struct _opd_sec_data *opd;
13934 sym = local_syms + r_symndx;
13935 sec = local_sections[r_symndx];
13936 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13937 sym_type = ELF64_ST_TYPE (sym->st_info);
13938 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13939 opd = get_opd_info (sec);
13940 if (opd != NULL && opd->adjust != NULL)
13942 long adjust = opd->adjust[OPD_NDX (sym->st_value
13948 /* If this is a relocation against the opd section sym
13949 and we have edited .opd, adjust the reloc addend so
13950 that ld -r and ld --emit-relocs output is correct.
13951 If it is a reloc against some other .opd symbol,
13952 then the symbol value will be adjusted later. */
13953 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13954 rel->r_addend += adjust;
13956 relocation += adjust;
13962 bfd_boolean ignored;
13964 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13965 r_symndx, symtab_hdr, sym_hashes,
13966 h_elf, sec, relocation,
13967 unresolved_reloc, warned, ignored);
13968 sym_name = h_elf->root.root.string;
13969 sym_type = h_elf->type;
13971 && sec->owner == output_bfd
13972 && strcmp (sec->name, ".opd") == 0)
13974 /* This is a symbol defined in a linker script. All
13975 such are defined in output sections, even those
13976 defined by simple assignment from a symbol defined in
13977 an input section. Transfer the symbol to an
13978 appropriate input .opd section, so that a branch to
13979 this symbol will be mapped to the location specified
13980 by the opd entry. */
13981 struct bfd_link_order *lo;
13982 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13983 if (lo->type == bfd_indirect_link_order)
13985 asection *isec = lo->u.indirect.section;
13986 if (h_elf->root.u.def.value >= isec->output_offset
13987 && h_elf->root.u.def.value < (isec->output_offset
13990 h_elf->root.u.def.value -= isec->output_offset;
13991 h_elf->root.u.def.section = isec;
13998 h = (struct ppc_link_hash_entry *) h_elf;
14000 if (sec != NULL && discarded_section (sec))
14002 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14003 input_bfd, input_section,
14004 contents, rel->r_offset);
14005 wrel->r_offset = rel->r_offset;
14007 wrel->r_addend = 0;
14009 /* For ld -r, remove relocations in debug sections against
14010 symbols defined in discarded sections. Not done for
14011 non-debug to preserve relocs in .eh_frame which the
14012 eh_frame editing code expects to be present. */
14013 if (bfd_link_relocatable (info)
14014 && (input_section->flags & SEC_DEBUGGING))
14020 if (bfd_link_relocatable (info))
14023 if (h != NULL && &h->elf == htab->elf.hgot)
14025 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14026 sec = bfd_abs_section_ptr;
14027 unresolved_reloc = FALSE;
14030 /* TLS optimizations. Replace instruction sequences and relocs
14031 based on information we collected in tls_optimize. We edit
14032 RELOCS so that --emit-relocs will output something sensible
14033 for the final instruction stream. */
14038 tls_mask = h->tls_mask;
14039 else if (local_got_ents != NULL)
14041 struct plt_entry **local_plt = (struct plt_entry **)
14042 (local_got_ents + symtab_hdr->sh_info);
14043 unsigned char *lgot_masks = (unsigned char *)
14044 (local_plt + symtab_hdr->sh_info);
14045 tls_mask = lgot_masks[r_symndx];
14047 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
14048 && (r_type == R_PPC64_TLS
14049 || r_type == R_PPC64_TLSGD
14050 || r_type == R_PPC64_TLSLD))
14052 /* Check for toc tls entries. */
14053 unsigned char *toc_tls;
14055 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14056 &local_syms, rel, input_bfd))
14060 tls_mask = *toc_tls;
14063 /* Check that tls relocs are used with tls syms, and non-tls
14064 relocs are used with non-tls syms. */
14065 if (r_symndx != STN_UNDEF
14066 && r_type != R_PPC64_NONE
14068 || h->elf.root.type == bfd_link_hash_defined
14069 || h->elf.root.type == bfd_link_hash_defweak)
14070 && (IS_PPC64_TLS_RELOC (r_type)
14071 != (sym_type == STT_TLS
14072 || (sym_type == STT_SECTION
14073 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
14075 if ((tls_mask & TLS_TLS) != 0
14076 && (r_type == R_PPC64_TLS
14077 || r_type == R_PPC64_TLSGD
14078 || r_type == R_PPC64_TLSLD))
14079 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14082 info->callbacks->einfo
14083 (!IS_PPC64_TLS_RELOC (r_type)
14084 /* xgettext:c-format */
14085 ? _("%H: %s used with TLS symbol `%pT'\n")
14086 /* xgettext:c-format */
14087 : _("%H: %s used with non-TLS symbol `%pT'\n"),
14088 input_bfd, input_section, rel->r_offset,
14089 ppc64_elf_howto_table[r_type]->name,
14093 /* Ensure reloc mapping code below stays sane. */
14094 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14095 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14096 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14097 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14098 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14099 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14100 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14101 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14102 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14103 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14111 case R_PPC64_LO_DS_OPT:
14112 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
14113 if ((insn & (0x3f << 26)) != 58u << 26)
14115 insn += (14u << 26) - (58u << 26);
14116 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
14117 r_type = R_PPC64_TOC16_LO;
14118 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14121 case R_PPC64_TOC16:
14122 case R_PPC64_TOC16_LO:
14123 case R_PPC64_TOC16_DS:
14124 case R_PPC64_TOC16_LO_DS:
14126 /* Check for toc tls entries. */
14127 unsigned char *toc_tls;
14130 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14131 &local_syms, rel, input_bfd);
14137 tls_mask = *toc_tls;
14138 if (r_type == R_PPC64_TOC16_DS
14139 || r_type == R_PPC64_TOC16_LO_DS)
14141 if ((tls_mask & TLS_TLS) != 0
14142 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14147 /* If we found a GD reloc pair, then we might be
14148 doing a GD->IE transition. */
14151 tls_gd = TLS_TPRELGD;
14152 if ((tls_mask & TLS_TLS) != 0
14153 && (tls_mask & TLS_GD) == 0)
14156 else if (retval == 3)
14158 if ((tls_mask & TLS_TLS) != 0
14159 && (tls_mask & TLS_LD) == 0)
14167 case R_PPC64_GOT_TPREL16_HI:
14168 case R_PPC64_GOT_TPREL16_HA:
14169 if ((tls_mask & TLS_TLS) != 0
14170 && (tls_mask & TLS_TPREL) == 0)
14172 rel->r_offset -= d_offset;
14173 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14174 r_type = R_PPC64_NONE;
14175 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14179 case R_PPC64_GOT_TPREL16_DS:
14180 case R_PPC64_GOT_TPREL16_LO_DS:
14181 if ((tls_mask & TLS_TLS) != 0
14182 && (tls_mask & TLS_TPREL) == 0)
14185 insn = bfd_get_32 (input_bfd,
14186 contents + rel->r_offset - d_offset);
14188 insn |= 0x3c0d0000; /* addis 0,13,0 */
14189 bfd_put_32 (input_bfd, insn,
14190 contents + rel->r_offset - d_offset);
14191 r_type = R_PPC64_TPREL16_HA;
14192 if (toc_symndx != 0)
14194 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14195 rel->r_addend = toc_addend;
14196 /* We changed the symbol. Start over in order to
14197 get h, sym, sec etc. right. */
14201 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14206 if ((tls_mask & TLS_TLS) != 0
14207 && (tls_mask & TLS_TPREL) == 0)
14209 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14210 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14213 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14214 /* Was PPC64_TLS which sits on insn boundary, now
14215 PPC64_TPREL16_LO which is at low-order half-word. */
14216 rel->r_offset += d_offset;
14217 r_type = R_PPC64_TPREL16_LO;
14218 if (toc_symndx != 0)
14220 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14221 rel->r_addend = toc_addend;
14222 /* We changed the symbol. Start over in order to
14223 get h, sym, sec etc. right. */
14227 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14231 case R_PPC64_GOT_TLSGD16_HI:
14232 case R_PPC64_GOT_TLSGD16_HA:
14233 tls_gd = TLS_TPRELGD;
14234 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14238 case R_PPC64_GOT_TLSLD16_HI:
14239 case R_PPC64_GOT_TLSLD16_HA:
14240 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14243 if ((tls_mask & tls_gd) != 0)
14244 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14245 + R_PPC64_GOT_TPREL16_DS);
14248 rel->r_offset -= d_offset;
14249 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14250 r_type = R_PPC64_NONE;
14252 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14256 case R_PPC64_GOT_TLSGD16:
14257 case R_PPC64_GOT_TLSGD16_LO:
14258 tls_gd = TLS_TPRELGD;
14259 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14263 case R_PPC64_GOT_TLSLD16:
14264 case R_PPC64_GOT_TLSLD16_LO:
14265 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14267 unsigned int insn1, insn2;
14270 offset = (bfd_vma) -1;
14271 /* If not using the newer R_PPC64_TLSGD/LD to mark
14272 __tls_get_addr calls, we must trust that the call
14273 stays with its arg setup insns, ie. that the next
14274 reloc is the __tls_get_addr call associated with
14275 the current reloc. Edit both insns. */
14276 if (input_section->has_tls_get_addr_call
14277 && rel + 1 < relend
14278 && branch_reloc_hash_match (input_bfd, rel + 1,
14279 htab->tls_get_addr,
14280 htab->tls_get_addr_fd))
14281 offset = rel[1].r_offset;
14282 /* We read the low GOT_TLS (or TOC16) insn because we
14283 need to keep the destination reg. It may be
14284 something other than the usual r3, and moved to r3
14285 before the call by intervening code. */
14286 insn1 = bfd_get_32 (input_bfd,
14287 contents + rel->r_offset - d_offset);
14288 if ((tls_mask & tls_gd) != 0)
14291 insn1 &= (0x1f << 21) | (0x1f << 16);
14292 insn1 |= 58 << 26; /* ld */
14293 insn2 = 0x7c636a14; /* add 3,3,13 */
14294 if (offset != (bfd_vma) -1)
14295 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14296 if ((tls_mask & TLS_EXPLICIT) == 0)
14297 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14298 + R_PPC64_GOT_TPREL16_DS);
14300 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14301 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14306 insn1 &= 0x1f << 21;
14307 insn1 |= 0x3c0d0000; /* addis r,13,0 */
14308 insn2 = 0x38630000; /* addi 3,3,0 */
14311 /* Was an LD reloc. */
14313 sec = local_sections[toc_symndx];
14315 r_symndx < symtab_hdr->sh_info;
14317 if (local_sections[r_symndx] == sec)
14319 if (r_symndx >= symtab_hdr->sh_info)
14320 r_symndx = STN_UNDEF;
14321 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14322 if (r_symndx != STN_UNDEF)
14323 rel->r_addend -= (local_syms[r_symndx].st_value
14324 + sec->output_offset
14325 + sec->output_section->vma);
14327 else if (toc_symndx != 0)
14329 r_symndx = toc_symndx;
14330 rel->r_addend = toc_addend;
14332 r_type = R_PPC64_TPREL16_HA;
14333 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14334 if (offset != (bfd_vma) -1)
14336 rel[1].r_info = ELF64_R_INFO (r_symndx,
14337 R_PPC64_TPREL16_LO);
14338 rel[1].r_offset = offset + d_offset;
14339 rel[1].r_addend = rel->r_addend;
14342 bfd_put_32 (input_bfd, insn1,
14343 contents + rel->r_offset - d_offset);
14344 if (offset != (bfd_vma) -1)
14346 bfd_put_32 (input_bfd, insn2, contents + offset);
14347 if (offset + 8 <= input_section->size)
14349 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14350 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14351 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14354 if ((tls_mask & tls_gd) == 0
14355 && (tls_gd == 0 || toc_symndx != 0))
14357 /* We changed the symbol. Start over in order
14358 to get h, sym, sec etc. right. */
14364 case R_PPC64_TLSGD:
14365 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
14366 && rel + 1 < relend)
14368 unsigned int insn2;
14369 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14371 offset = rel->r_offset;
14372 if (is_plt_seq_reloc (r_type1))
14374 bfd_put_32 (output_bfd, NOP, contents + offset);
14375 if (r_type1 == R_PPC64_PLT_PCREL34
14376 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14377 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14378 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14382 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14383 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14385 if ((tls_mask & TLS_TPRELGD) != 0)
14388 r_type = R_PPC64_NONE;
14389 insn2 = 0x7c636a14; /* add 3,3,13 */
14394 if (toc_symndx != 0)
14396 r_symndx = toc_symndx;
14397 rel->r_addend = toc_addend;
14399 r_type = R_PPC64_TPREL16_LO;
14400 rel->r_offset = offset + d_offset;
14401 insn2 = 0x38630000; /* addi 3,3,0 */
14403 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14404 /* Zap the reloc on the _tls_get_addr call too. */
14405 BFD_ASSERT (offset == rel[1].r_offset);
14406 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14407 bfd_put_32 (input_bfd, insn2, contents + offset);
14408 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
14413 case R_PPC64_TLSLD:
14414 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
14415 && rel + 1 < relend)
14417 unsigned int insn2;
14418 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14420 offset = rel->r_offset;
14421 if (is_plt_seq_reloc (r_type1))
14423 bfd_put_32 (output_bfd, NOP, contents + offset);
14424 if (r_type1 == R_PPC64_PLT_PCREL34
14425 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14426 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14427 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14431 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14432 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14435 sec = local_sections[toc_symndx];
14437 r_symndx < symtab_hdr->sh_info;
14439 if (local_sections[r_symndx] == sec)
14441 if (r_symndx >= symtab_hdr->sh_info)
14442 r_symndx = STN_UNDEF;
14443 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14444 if (r_symndx != STN_UNDEF)
14445 rel->r_addend -= (local_syms[r_symndx].st_value
14446 + sec->output_offset
14447 + sec->output_section->vma);
14449 r_type = R_PPC64_TPREL16_LO;
14450 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14451 rel->r_offset = offset + d_offset;
14452 /* Zap the reloc on the _tls_get_addr call too. */
14453 BFD_ASSERT (offset == rel[1].r_offset);
14454 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14455 insn2 = 0x38630000; /* addi 3,3,0 */
14456 bfd_put_32 (input_bfd, insn2, contents + offset);
14461 case R_PPC64_DTPMOD64:
14462 if (rel + 1 < relend
14463 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14464 && rel[1].r_offset == rel->r_offset + 8)
14466 if ((tls_mask & TLS_GD) == 0)
14468 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14469 if ((tls_mask & TLS_TPRELGD) != 0)
14470 r_type = R_PPC64_TPREL64;
14473 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14474 r_type = R_PPC64_NONE;
14476 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14481 if ((tls_mask & TLS_LD) == 0)
14483 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14484 r_type = R_PPC64_NONE;
14485 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14490 case R_PPC64_TPREL64:
14491 if ((tls_mask & TLS_TPREL) == 0)
14493 r_type = R_PPC64_NONE;
14494 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14498 case R_PPC64_ENTRY:
14499 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14500 if (!bfd_link_pic (info)
14501 && !info->traditional_format
14502 && relocation + 0x80008000 <= 0xffffffff)
14504 unsigned int insn1, insn2;
14506 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14507 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14508 if ((insn1 & ~0xfffc) == LD_R2_0R12
14509 && insn2 == ADD_R2_R2_R12)
14511 bfd_put_32 (input_bfd,
14512 LIS_R2 + PPC_HA (relocation),
14513 contents + rel->r_offset);
14514 bfd_put_32 (input_bfd,
14515 ADDI_R2_R2 + PPC_LO (relocation),
14516 contents + rel->r_offset + 4);
14521 relocation -= (rel->r_offset
14522 + input_section->output_offset
14523 + input_section->output_section->vma);
14524 if (relocation + 0x80008000 <= 0xffffffff)
14526 unsigned int insn1, insn2;
14528 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14529 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14530 if ((insn1 & ~0xfffc) == LD_R2_0R12
14531 && insn2 == ADD_R2_R2_R12)
14533 bfd_put_32 (input_bfd,
14534 ADDIS_R2_R12 + PPC_HA (relocation),
14535 contents + rel->r_offset);
14536 bfd_put_32 (input_bfd,
14537 ADDI_R2_R2 + PPC_LO (relocation),
14538 contents + rel->r_offset + 4);
14544 case R_PPC64_REL16_HA:
14545 /* If we are generating a non-PIC executable, edit
14546 . 0: addis 2,12,.TOC.-0b@ha
14547 . addi 2,2,.TOC.-0b@l
14548 used by ELFv2 global entry points to set up r2, to
14551 if .TOC. is in range. */
14552 if (!bfd_link_pic (info)
14553 && !info->traditional_format
14555 && rel->r_addend == d_offset
14556 && h != NULL && &h->elf == htab->elf.hgot
14557 && rel + 1 < relend
14558 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14559 && rel[1].r_offset == rel->r_offset + 4
14560 && rel[1].r_addend == rel->r_addend + 4
14561 && relocation + 0x80008000 <= 0xffffffff)
14563 unsigned int insn1, insn2;
14564 offset = rel->r_offset - d_offset;
14565 insn1 = bfd_get_32 (input_bfd, contents + offset);
14566 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14567 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14568 && (insn2 & 0xffff0000) == ADDI_R2_R2)
14570 r_type = R_PPC64_ADDR16_HA;
14571 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14572 rel->r_addend -= d_offset;
14573 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14574 rel[1].r_addend -= d_offset + 4;
14575 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14581 /* Handle other relocations that tweak non-addend part of insn. */
14583 max_br_offset = 1 << 25;
14584 addend = rel->r_addend;
14585 reloc_dest = DEST_NORMAL;
14591 case R_PPC64_TOCSAVE:
14592 if (relocation + addend == (rel->r_offset
14593 + input_section->output_offset
14594 + input_section->output_section->vma)
14595 && tocsave_find (htab, NO_INSERT,
14596 &local_syms, rel, input_bfd))
14598 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14600 || insn == CROR_151515 || insn == CROR_313131)
14601 bfd_put_32 (input_bfd,
14602 STD_R2_0R1 + STK_TOC (htab),
14603 contents + rel->r_offset);
14607 /* Branch taken prediction relocations. */
14608 case R_PPC64_ADDR14_BRTAKEN:
14609 case R_PPC64_REL14_BRTAKEN:
14610 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
14611 /* Fall through. */
14613 /* Branch not taken prediction relocations. */
14614 case R_PPC64_ADDR14_BRNTAKEN:
14615 case R_PPC64_REL14_BRNTAKEN:
14616 insn |= bfd_get_32 (input_bfd,
14617 contents + rel->r_offset) & ~(0x01 << 21);
14618 /* Fall through. */
14620 case R_PPC64_REL14:
14621 max_br_offset = 1 << 15;
14622 /* Fall through. */
14624 case R_PPC64_REL24:
14625 case R_PPC64_REL24_NOTOC:
14626 case R_PPC64_PLTCALL:
14627 case R_PPC64_PLTCALL_NOTOC:
14628 /* Calls to functions with a different TOC, such as calls to
14629 shared objects, need to alter the TOC pointer. This is
14630 done using a linkage stub. A REL24 branching to these
14631 linkage stubs needs to be followed by a nop, as the nop
14632 will be replaced with an instruction to restore the TOC
14637 && h->oh->is_func_descriptor)
14638 fdh = ppc_follow_link (h->oh);
14639 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14641 if ((r_type == R_PPC64_PLTCALL
14642 || r_type == R_PPC64_PLTCALL_NOTOC)
14643 && stub_entry != NULL
14644 && stub_entry->stub_type >= ppc_stub_plt_call
14645 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14648 if (stub_entry != NULL
14649 && ((stub_entry->stub_type >= ppc_stub_plt_call
14650 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14651 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14652 || stub_entry->stub_type == ppc_stub_plt_branch_both
14653 || stub_entry->stub_type == ppc_stub_long_branch_r2off
14654 || stub_entry->stub_type == ppc_stub_long_branch_both))
14656 bfd_boolean can_plt_call = FALSE;
14658 if (stub_entry->stub_type == ppc_stub_plt_call
14660 && htab->params->plt_localentry0 != 0
14661 && is_elfv2_localentry0 (&h->elf))
14663 /* The function doesn't use or change r2. */
14664 can_plt_call = TRUE;
14666 else if (r_type == R_PPC64_REL24_NOTOC)
14668 /* NOTOC calls don't need to restore r2. */
14669 can_plt_call = TRUE;
14672 /* All of these stubs may modify r2, so there must be a
14673 branch and link followed by a nop. The nop is
14674 replaced by an insn to restore r2. */
14675 else if (rel->r_offset + 8 <= input_section->size)
14679 br = bfd_get_32 (input_bfd,
14680 contents + rel->r_offset);
14685 nop = bfd_get_32 (input_bfd,
14686 contents + rel->r_offset + 4);
14687 if (nop == LD_R2_0R1 + STK_TOC (htab))
14688 can_plt_call = TRUE;
14689 else if (nop == NOP
14690 || nop == CROR_151515
14691 || nop == CROR_313131)
14694 && (h == htab->tls_get_addr_fd
14695 || h == htab->tls_get_addr)
14696 && htab->params->tls_get_addr_opt)
14698 /* Special stub used, leave nop alone. */
14701 bfd_put_32 (input_bfd,
14702 LD_R2_0R1 + STK_TOC (htab),
14703 contents + rel->r_offset + 4);
14704 can_plt_call = TRUE;
14709 if (!can_plt_call && h != NULL)
14711 const char *name = h->elf.root.root.string;
14716 if (strncmp (name, "__libc_start_main", 17) == 0
14717 && (name[17] == 0 || name[17] == '@'))
14719 /* Allow crt1 branch to go via a toc adjusting
14720 stub. Other calls that never return could do
14721 the same, if we could detect such. */
14722 can_plt_call = TRUE;
14728 /* g++ as of 20130507 emits self-calls without a
14729 following nop. This is arguably wrong since we
14730 have conflicting information. On the one hand a
14731 global symbol and on the other a local call
14732 sequence, but don't error for this special case.
14733 It isn't possible to cheaply verify we have
14734 exactly such a call. Allow all calls to the same
14736 asection *code_sec = sec;
14738 if (get_opd_info (sec) != NULL)
14740 bfd_vma off = (relocation + addend
14741 - sec->output_section->vma
14742 - sec->output_offset);
14744 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14746 if (code_sec == input_section)
14747 can_plt_call = TRUE;
14752 if (stub_entry->stub_type >= ppc_stub_plt_call
14753 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14754 info->callbacks->einfo
14755 /* xgettext:c-format */
14756 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14757 "(plt call stub)\n"),
14758 input_bfd, input_section, rel->r_offset, sym_name);
14760 info->callbacks->einfo
14761 /* xgettext:c-format */
14762 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14763 "(toc save/adjust stub)\n"),
14764 input_bfd, input_section, rel->r_offset, sym_name);
14766 bfd_set_error (bfd_error_bad_value);
14771 && stub_entry->stub_type >= ppc_stub_plt_call
14772 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14773 unresolved_reloc = FALSE;
14776 if ((stub_entry == NULL
14777 || stub_entry->stub_type == ppc_stub_long_branch
14778 || stub_entry->stub_type == ppc_stub_plt_branch)
14779 && get_opd_info (sec) != NULL)
14781 /* The branch destination is the value of the opd entry. */
14782 bfd_vma off = (relocation + addend
14783 - sec->output_section->vma
14784 - sec->output_offset);
14785 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14786 if (dest != (bfd_vma) -1)
14790 reloc_dest = DEST_OPD;
14794 /* If the branch is out of reach we ought to have a long
14796 from = (rel->r_offset
14797 + input_section->output_offset
14798 + input_section->output_section->vma);
14800 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14804 if (stub_entry != NULL
14805 && (stub_entry->stub_type == ppc_stub_long_branch
14806 || stub_entry->stub_type == ppc_stub_plt_branch)
14807 && (r_type == R_PPC64_ADDR14_BRTAKEN
14808 || r_type == R_PPC64_ADDR14_BRNTAKEN
14809 || (relocation + addend - from + max_br_offset
14810 < 2 * max_br_offset)))
14811 /* Don't use the stub if this branch is in range. */
14814 if (stub_entry != NULL
14815 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
14816 || stub_entry->stub_type == ppc_stub_long_branch_both
14817 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
14818 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14819 && (r_type != R_PPC64_REL24_NOTOC
14820 || ((fdh ? fdh->elf.other : sym->st_other)
14821 & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
14822 && (relocation + addend - from + max_br_offset
14823 < 2 * max_br_offset))
14826 if (stub_entry != NULL
14827 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
14828 || stub_entry->stub_type == ppc_stub_long_branch_both
14829 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14830 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14831 && r_type == R_PPC64_REL24_NOTOC
14832 && (relocation + addend - from + max_br_offset
14833 < 2 * max_br_offset))
14836 if (stub_entry != NULL)
14838 /* Munge up the value and addend so that we call the stub
14839 rather than the procedure directly. */
14840 asection *stub_sec = stub_entry->group->stub_sec;
14842 if (stub_entry->stub_type == ppc_stub_save_res)
14843 relocation += (stub_sec->output_offset
14844 + stub_sec->output_section->vma
14845 + stub_sec->size - htab->sfpr->size
14846 - htab->sfpr->output_offset
14847 - htab->sfpr->output_section->vma);
14849 relocation = (stub_entry->stub_offset
14850 + stub_sec->output_offset
14851 + stub_sec->output_section->vma);
14853 reloc_dest = DEST_STUB;
14855 if (((stub_entry->stub_type == ppc_stub_plt_call
14856 && ALWAYS_EMIT_R2SAVE)
14857 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14858 || stub_entry->stub_type == ppc_stub_plt_call_both)
14860 && (h == htab->tls_get_addr_fd
14861 || h == htab->tls_get_addr)
14862 && htab->params->tls_get_addr_opt)
14863 && rel + 1 < relend
14864 && rel[1].r_offset == rel->r_offset + 4
14865 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14867 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
14868 || stub_entry->stub_type == ppc_stub_plt_branch_both
14869 || stub_entry->stub_type == ppc_stub_plt_call_both)
14870 && r_type == R_PPC64_REL24_NOTOC)
14873 if (r_type == R_PPC64_REL24_NOTOC
14874 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
14875 || stub_entry->stub_type == ppc_stub_plt_call_both))
14876 htab->notoc_plt = 1;
14883 /* Set 'a' bit. This is 0b00010 in BO field for branch
14884 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14885 for branch on CTR insns (BO == 1a00t or 1a01t). */
14886 if ((insn & (0x14 << 21)) == (0x04 << 21))
14887 insn |= 0x02 << 21;
14888 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14889 insn |= 0x08 << 21;
14895 /* Invert 'y' bit if not the default. */
14896 if ((bfd_signed_vma) (relocation + addend - from) < 0)
14897 insn ^= 0x01 << 21;
14900 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14903 /* NOP out calls to undefined weak functions.
14904 We can thus call a weak function without first
14905 checking whether the function is defined. */
14907 && h->elf.root.type == bfd_link_hash_undefweak
14908 && h->elf.dynindx == -1
14909 && (r_type == R_PPC64_REL24
14910 || r_type == R_PPC64_REL24_NOTOC)
14914 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14919 case R_PPC64_GOT16_DS:
14920 from = TOCstart + htab->sec_info[input_section->id].toc_off;
14921 if (relocation + addend - from + 0x8000 < 0x10000
14922 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14924 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14925 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
14927 insn += (14u << 26) - (58u << 26);
14928 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
14929 r_type = R_PPC64_TOC16;
14930 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14935 case R_PPC64_GOT16_LO_DS:
14936 case R_PPC64_GOT16_HA:
14937 from = TOCstart + htab->sec_info[input_section->id].toc_off;
14938 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
14939 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14941 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14942 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
14944 insn += (14u << 26) - (58u << 26);
14945 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
14946 r_type = R_PPC64_TOC16_LO;
14947 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14949 else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
14951 r_type = R_PPC64_TOC16_HA;
14952 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14957 case R_PPC64_GOT_PCREL34:
14958 from = (rel->r_offset
14959 + input_section->output_section->vma
14960 + input_section->output_offset);
14961 if (relocation - from + (1ULL << 33) < 1ULL << 34
14962 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14964 offset = rel->r_offset;
14965 pinsn = bfd_get_32 (input_bfd, contents + offset);
14967 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
14968 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
14969 == ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
14971 /* Replace with paddi. */
14972 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
14973 r_type = R_PPC64_PCREL34;
14974 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14975 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
14976 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
14982 case R_PPC64_PCREL34:
14983 if (SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14985 offset = rel->r_offset;
14986 pinsn = bfd_get_32 (input_bfd, contents + offset);
14988 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
14989 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
14990 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
14991 | (14ULL << 26) /* paddi */))
14994 if (rel + 1 < relend
14995 && rel[1].r_offset == offset
14996 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT))
14998 bfd_vma off2 = rel[1].r_addend;
15000 /* zero means next insn. */
15003 if (off2 + 4 <= input_section->size)
15006 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
15008 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15010 if (xlate_pcrel_opt (&pinsn, &pinsn2))
15012 bfd_put_32 (input_bfd, pinsn >> 32,
15013 contents + offset);
15014 bfd_put_32 (input_bfd, pinsn,
15015 contents + offset + 4);
15016 bfd_put_32 (input_bfd, pinsn2 >> 32,
15026 /* Set `addend'. */
15028 save_unresolved_reloc = unresolved_reloc;
15032 /* xgettext:c-format */
15033 _bfd_error_handler (_("%pB: %s unsupported"),
15034 input_bfd, ppc64_elf_howto_table[r_type]->name);
15036 bfd_set_error (bfd_error_bad_value);
15042 case R_PPC64_TLSGD:
15043 case R_PPC64_TLSLD:
15044 case R_PPC64_TOCSAVE:
15045 case R_PPC64_GNU_VTINHERIT:
15046 case R_PPC64_GNU_VTENTRY:
15047 case R_PPC64_ENTRY:
15048 case R_PPC64_PCREL_OPT:
15051 /* GOT16 relocations. Like an ADDR16 using the symbol's
15052 address in the GOT as relocation value instead of the
15053 symbol's value itself. Also, create a GOT entry for the
15054 symbol and put the symbol value there. */
15055 case R_PPC64_GOT_TLSGD16:
15056 case R_PPC64_GOT_TLSGD16_LO:
15057 case R_PPC64_GOT_TLSGD16_HI:
15058 case R_PPC64_GOT_TLSGD16_HA:
15059 tls_type = TLS_TLS | TLS_GD;
15062 case R_PPC64_GOT_TLSLD16:
15063 case R_PPC64_GOT_TLSLD16_LO:
15064 case R_PPC64_GOT_TLSLD16_HI:
15065 case R_PPC64_GOT_TLSLD16_HA:
15066 tls_type = TLS_TLS | TLS_LD;
15069 case R_PPC64_GOT_TPREL16_DS:
15070 case R_PPC64_GOT_TPREL16_LO_DS:
15071 case R_PPC64_GOT_TPREL16_HI:
15072 case R_PPC64_GOT_TPREL16_HA:
15073 tls_type = TLS_TLS | TLS_TPREL;
15076 case R_PPC64_GOT_DTPREL16_DS:
15077 case R_PPC64_GOT_DTPREL16_LO_DS:
15078 case R_PPC64_GOT_DTPREL16_HI:
15079 case R_PPC64_GOT_DTPREL16_HA:
15080 tls_type = TLS_TLS | TLS_DTPREL;
15083 case R_PPC64_GOT16:
15084 case R_PPC64_GOT16_LO:
15085 case R_PPC64_GOT16_HI:
15086 case R_PPC64_GOT16_HA:
15087 case R_PPC64_GOT16_DS:
15088 case R_PPC64_GOT16_LO_DS:
15089 case R_PPC64_GOT_PCREL34:
15092 /* Relocation is to the entry for this symbol in the global
15097 unsigned long indx = 0;
15098 struct got_entry *ent;
15099 bfd_vma sym_addend = orig_rel.r_addend;
15101 if (r_type == R_PPC64_GOT_PCREL34)
15104 if (tls_type == (TLS_TLS | TLS_LD)
15106 || !h->elf.def_dynamic))
15107 ent = ppc64_tlsld_got (input_bfd);
15112 if (!htab->elf.dynamic_sections_created
15113 || h->elf.dynindx == -1
15114 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15115 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
15116 /* This is actually a static link, or it is a
15117 -Bsymbolic link and the symbol is defined
15118 locally, or the symbol was forced to be local
15119 because of a version file. */
15123 indx = h->elf.dynindx;
15124 unresolved_reloc = FALSE;
15126 ent = h->elf.got.glist;
15130 if (local_got_ents == NULL)
15132 ent = local_got_ents[r_symndx];
15135 for (; ent != NULL; ent = ent->next)
15136 if (ent->addend == sym_addend
15137 && ent->owner == input_bfd
15138 && ent->tls_type == tls_type)
15144 if (ent->is_indirect)
15145 ent = ent->got.ent;
15146 offp = &ent->got.offset;
15147 got = ppc64_elf_tdata (ent->owner)->got;
15151 /* The offset must always be a multiple of 8. We use the
15152 least significant bit to record whether we have already
15153 processed this entry. */
15155 if ((off & 1) != 0)
15159 /* Generate relocs for the dynamic linker, except in
15160 the case of TLSLD where we'll use one entry per
15168 ? h->elf.type == STT_GNU_IFUNC
15169 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
15172 relgot = htab->elf.irelplt;
15174 htab->local_ifunc_resolver = 1;
15175 else if (is_static_defined (&h->elf))
15176 htab->maybe_local_ifunc_resolver = 1;
15179 || (bfd_link_pic (info)
15181 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
15182 || (tls_type == (TLS_TLS | TLS_LD)
15183 && !h->elf.def_dynamic))
15184 && !(tls_type == (TLS_TLS | TLS_TPREL)
15185 && bfd_link_executable (info)
15186 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
15187 relgot = ppc64_elf_tdata (ent->owner)->relgot;
15188 if (relgot != NULL)
15190 outrel.r_offset = (got->output_section->vma
15191 + got->output_offset
15193 outrel.r_addend = sym_addend;
15194 if (tls_type & (TLS_LD | TLS_GD))
15196 outrel.r_addend = 0;
15197 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
15198 if (tls_type == (TLS_TLS | TLS_GD))
15200 loc = relgot->contents;
15201 loc += (relgot->reloc_count++
15202 * sizeof (Elf64_External_Rela));
15203 bfd_elf64_swap_reloca_out (output_bfd,
15205 outrel.r_offset += 8;
15206 outrel.r_addend = sym_addend;
15208 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15211 else if (tls_type == (TLS_TLS | TLS_DTPREL))
15212 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15213 else if (tls_type == (TLS_TLS | TLS_TPREL))
15214 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
15215 else if (indx != 0)
15216 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15220 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15222 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15224 /* Write the .got section contents for the sake
15226 loc = got->contents + off;
15227 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15231 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
15233 outrel.r_addend += relocation;
15234 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
15236 if (htab->elf.tls_sec == NULL)
15237 outrel.r_addend = 0;
15239 outrel.r_addend -= htab->elf.tls_sec->vma;
15242 loc = relgot->contents;
15243 loc += (relgot->reloc_count++
15244 * sizeof (Elf64_External_Rela));
15245 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15248 /* Init the .got section contents here if we're not
15249 emitting a reloc. */
15252 relocation += sym_addend;
15255 if (htab->elf.tls_sec == NULL)
15259 if (tls_type & TLS_LD)
15262 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
15263 if (tls_type & TLS_TPREL)
15264 relocation += DTP_OFFSET - TP_OFFSET;
15267 if (tls_type & (TLS_GD | TLS_LD))
15269 bfd_put_64 (output_bfd, relocation,
15270 got->contents + off + 8);
15274 bfd_put_64 (output_bfd, relocation,
15275 got->contents + off);
15279 if (off >= (bfd_vma) -2)
15282 relocation = got->output_section->vma + got->output_offset + off;
15283 if (r_type != R_PPC64_GOT_PCREL34)
15284 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
15288 case R_PPC64_PLT16_HA:
15289 case R_PPC64_PLT16_HI:
15290 case R_PPC64_PLT16_LO:
15291 case R_PPC64_PLT16_LO_DS:
15292 case R_PPC64_PLT_PCREL34:
15293 case R_PPC64_PLT_PCREL34_NOTOC:
15294 case R_PPC64_PLT32:
15295 case R_PPC64_PLT64:
15296 case R_PPC64_PLTSEQ:
15297 case R_PPC64_PLTSEQ_NOTOC:
15298 case R_PPC64_PLTCALL:
15299 case R_PPC64_PLTCALL_NOTOC:
15300 /* Relocation is to the entry for this symbol in the
15301 procedure linkage table. */
15302 unresolved_reloc = TRUE;
15304 struct plt_entry **plt_list = NULL;
15306 plt_list = &h->elf.plt.plist;
15307 else if (local_got_ents != NULL)
15309 struct plt_entry **local_plt = (struct plt_entry **)
15310 (local_got_ents + symtab_hdr->sh_info);
15311 plt_list = local_plt + r_symndx;
15315 struct plt_entry *ent;
15316 bfd_vma sym_addend = orig_rel.r_addend;
15318 if (r_type == R_PPC64_PLT_PCREL34
15319 || r_type == R_PPC64_PLT_PCREL34_NOTOC)
15322 for (ent = *plt_list; ent != NULL; ent = ent->next)
15323 if (ent->plt.offset != (bfd_vma) -1
15324 && ent->addend == sym_addend)
15329 plt = htab->elf.splt;
15330 if (!htab->elf.dynamic_sections_created
15332 || h->elf.dynindx == -1)
15335 ? h->elf.type == STT_GNU_IFUNC
15336 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15337 plt = htab->elf.iplt;
15339 plt = htab->pltlocal;
15341 relocation = (plt->output_section->vma
15342 + plt->output_offset
15343 + ent->plt.offset);
15344 if (r_type == R_PPC64_PLT16_HA
15345 || r_type == R_PPC64_PLT16_HI
15346 || r_type == R_PPC64_PLT16_LO
15347 || r_type == R_PPC64_PLT16_LO_DS)
15349 got = (elf_gp (output_bfd)
15350 + htab->sec_info[input_section->id].toc_off);
15353 if (r_type != R_PPC64_PLT_PCREL34
15354 && r_type != R_PPC64_PLT_PCREL34_NOTOC)
15356 unresolved_reloc = FALSE;
15364 /* Relocation value is TOC base. */
15365 relocation = TOCstart;
15366 if (r_symndx == STN_UNDEF)
15367 relocation += htab->sec_info[input_section->id].toc_off;
15368 else if (unresolved_reloc)
15370 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15371 relocation += htab->sec_info[sec->id].toc_off;
15373 unresolved_reloc = TRUE;
15376 /* TOC16 relocs. We want the offset relative to the TOC base,
15377 which is the address of the start of the TOC plus 0x8000.
15378 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15380 case R_PPC64_TOC16:
15381 case R_PPC64_TOC16_LO:
15382 case R_PPC64_TOC16_HI:
15383 case R_PPC64_TOC16_DS:
15384 case R_PPC64_TOC16_LO_DS:
15385 case R_PPC64_TOC16_HA:
15386 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
15389 /* Relocate against the beginning of the section. */
15390 case R_PPC64_SECTOFF:
15391 case R_PPC64_SECTOFF_LO:
15392 case R_PPC64_SECTOFF_HI:
15393 case R_PPC64_SECTOFF_DS:
15394 case R_PPC64_SECTOFF_LO_DS:
15395 case R_PPC64_SECTOFF_HA:
15397 addend -= sec->output_section->vma;
15400 case R_PPC64_REL16:
15401 case R_PPC64_REL16_LO:
15402 case R_PPC64_REL16_HI:
15403 case R_PPC64_REL16_HA:
15404 case R_PPC64_REL16_HIGH:
15405 case R_PPC64_REL16_HIGHA:
15406 case R_PPC64_REL16_HIGHER:
15407 case R_PPC64_REL16_HIGHERA:
15408 case R_PPC64_REL16_HIGHEST:
15409 case R_PPC64_REL16_HIGHESTA:
15410 case R_PPC64_REL16_HIGHER34:
15411 case R_PPC64_REL16_HIGHERA34:
15412 case R_PPC64_REL16_HIGHEST34:
15413 case R_PPC64_REL16_HIGHESTA34:
15414 case R_PPC64_REL16DX_HA:
15415 case R_PPC64_REL14:
15416 case R_PPC64_REL14_BRNTAKEN:
15417 case R_PPC64_REL14_BRTAKEN:
15418 case R_PPC64_REL24:
15419 case R_PPC64_REL24_NOTOC:
15420 case R_PPC64_PCREL34:
15421 case R_PPC64_PCREL28:
15424 case R_PPC64_TPREL16:
15425 case R_PPC64_TPREL16_LO:
15426 case R_PPC64_TPREL16_HI:
15427 case R_PPC64_TPREL16_HA:
15428 case R_PPC64_TPREL16_DS:
15429 case R_PPC64_TPREL16_LO_DS:
15430 case R_PPC64_TPREL16_HIGH:
15431 case R_PPC64_TPREL16_HIGHA:
15432 case R_PPC64_TPREL16_HIGHER:
15433 case R_PPC64_TPREL16_HIGHERA:
15434 case R_PPC64_TPREL16_HIGHEST:
15435 case R_PPC64_TPREL16_HIGHESTA:
15437 && h->elf.root.type == bfd_link_hash_undefweak
15438 && h->elf.dynindx == -1)
15440 /* Make this relocation against an undefined weak symbol
15441 resolve to zero. This is really just a tweak, since
15442 code using weak externs ought to check that they are
15443 defined before using them. */
15444 bfd_byte *p = contents + rel->r_offset - d_offset;
15446 insn = bfd_get_32 (input_bfd, p);
15447 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15449 bfd_put_32 (input_bfd, insn, p);
15452 if (htab->elf.tls_sec != NULL)
15453 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15454 /* The TPREL16 relocs shouldn't really be used in shared
15455 libs or with non-local symbols as that will result in
15456 DT_TEXTREL being set, but support them anyway. */
15459 case R_PPC64_DTPREL16:
15460 case R_PPC64_DTPREL16_LO:
15461 case R_PPC64_DTPREL16_HI:
15462 case R_PPC64_DTPREL16_HA:
15463 case R_PPC64_DTPREL16_DS:
15464 case R_PPC64_DTPREL16_LO_DS:
15465 case R_PPC64_DTPREL16_HIGH:
15466 case R_PPC64_DTPREL16_HIGHA:
15467 case R_PPC64_DTPREL16_HIGHER:
15468 case R_PPC64_DTPREL16_HIGHERA:
15469 case R_PPC64_DTPREL16_HIGHEST:
15470 case R_PPC64_DTPREL16_HIGHESTA:
15471 if (htab->elf.tls_sec != NULL)
15472 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15475 case R_PPC64_ADDR64_LOCAL:
15476 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15481 case R_PPC64_DTPMOD64:
15486 case R_PPC64_TPREL64:
15487 if (htab->elf.tls_sec != NULL)
15488 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15491 case R_PPC64_DTPREL64:
15492 if (htab->elf.tls_sec != NULL)
15493 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15494 /* Fall through. */
15496 /* Relocations that may need to be propagated if this is a
15498 case R_PPC64_REL30:
15499 case R_PPC64_REL32:
15500 case R_PPC64_REL64:
15501 case R_PPC64_ADDR14:
15502 case R_PPC64_ADDR14_BRNTAKEN:
15503 case R_PPC64_ADDR14_BRTAKEN:
15504 case R_PPC64_ADDR16:
15505 case R_PPC64_ADDR16_DS:
15506 case R_PPC64_ADDR16_HA:
15507 case R_PPC64_ADDR16_HI:
15508 case R_PPC64_ADDR16_HIGH:
15509 case R_PPC64_ADDR16_HIGHA:
15510 case R_PPC64_ADDR16_HIGHER:
15511 case R_PPC64_ADDR16_HIGHERA:
15512 case R_PPC64_ADDR16_HIGHEST:
15513 case R_PPC64_ADDR16_HIGHESTA:
15514 case R_PPC64_ADDR16_LO:
15515 case R_PPC64_ADDR16_LO_DS:
15516 case R_PPC64_ADDR16_HIGHER34:
15517 case R_PPC64_ADDR16_HIGHERA34:
15518 case R_PPC64_ADDR16_HIGHEST34:
15519 case R_PPC64_ADDR16_HIGHESTA34:
15520 case R_PPC64_ADDR24:
15521 case R_PPC64_ADDR32:
15522 case R_PPC64_ADDR64:
15523 case R_PPC64_UADDR16:
15524 case R_PPC64_UADDR32:
15525 case R_PPC64_UADDR64:
15527 case R_PPC64_D34_LO:
15528 case R_PPC64_D34_HI30:
15529 case R_PPC64_D34_HA30:
15532 if ((input_section->flags & SEC_ALLOC) == 0)
15535 if (NO_OPD_RELOCS && is_opd)
15538 if (bfd_link_pic (info)
15540 || h->dyn_relocs != NULL)
15541 && ((h != NULL && pc_dynrelocs (h))
15542 || must_be_dyn_reloc (info, r_type)))
15544 ? h->dyn_relocs != NULL
15545 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15547 bfd_boolean skip, relocate;
15552 /* When generating a dynamic object, these relocations
15553 are copied into the output file to be resolved at run
15559 out_off = _bfd_elf_section_offset (output_bfd, info,
15560 input_section, rel->r_offset);
15561 if (out_off == (bfd_vma) -1)
15563 else if (out_off == (bfd_vma) -2)
15564 skip = TRUE, relocate = TRUE;
15565 out_off += (input_section->output_section->vma
15566 + input_section->output_offset);
15567 outrel.r_offset = out_off;
15568 outrel.r_addend = rel->r_addend;
15570 /* Optimize unaligned reloc use. */
15571 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15572 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15573 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15574 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15575 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15576 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15577 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15578 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15579 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15582 memset (&outrel, 0, sizeof outrel);
15583 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15585 && r_type != R_PPC64_TOC)
15587 indx = h->elf.dynindx;
15588 BFD_ASSERT (indx != -1);
15589 outrel.r_info = ELF64_R_INFO (indx, r_type);
15593 /* This symbol is local, or marked to become local,
15594 or this is an opd section reloc which must point
15595 at a local function. */
15596 outrel.r_addend += relocation;
15597 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
15599 if (is_opd && h != NULL)
15601 /* Lie about opd entries. This case occurs
15602 when building shared libraries and we
15603 reference a function in another shared
15604 lib. The same thing happens for a weak
15605 definition in an application that's
15606 overridden by a strong definition in a
15607 shared lib. (I believe this is a generic
15608 bug in binutils handling of weak syms.)
15609 In these cases we won't use the opd
15610 entry in this lib. */
15611 unresolved_reloc = FALSE;
15614 && r_type == R_PPC64_ADDR64
15616 ? h->elf.type == STT_GNU_IFUNC
15617 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15618 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15621 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15623 /* We need to relocate .opd contents for ld.so.
15624 Prelink also wants simple and consistent rules
15625 for relocs. This make all RELATIVE relocs have
15626 *r_offset equal to r_addend. */
15633 ? h->elf.type == STT_GNU_IFUNC
15634 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15636 info->callbacks->einfo
15637 /* xgettext:c-format */
15638 (_("%H: %s for indirect "
15639 "function `%pT' unsupported\n"),
15640 input_bfd, input_section, rel->r_offset,
15641 ppc64_elf_howto_table[r_type]->name,
15645 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
15647 else if (sec == NULL || sec->owner == NULL)
15649 bfd_set_error (bfd_error_bad_value);
15656 osec = sec->output_section;
15657 indx = elf_section_data (osec)->dynindx;
15661 if ((osec->flags & SEC_READONLY) == 0
15662 && htab->elf.data_index_section != NULL)
15663 osec = htab->elf.data_index_section;
15665 osec = htab->elf.text_index_section;
15666 indx = elf_section_data (osec)->dynindx;
15668 BFD_ASSERT (indx != 0);
15670 /* We are turning this relocation into one
15671 against a section symbol, so subtract out
15672 the output section's address but not the
15673 offset of the input section in the output
15675 outrel.r_addend -= osec->vma;
15678 outrel.r_info = ELF64_R_INFO (indx, r_type);
15682 sreloc = elf_section_data (input_section)->sreloc;
15684 ? h->elf.type == STT_GNU_IFUNC
15685 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15687 sreloc = htab->elf.irelplt;
15689 htab->local_ifunc_resolver = 1;
15690 else if (is_static_defined (&h->elf))
15691 htab->maybe_local_ifunc_resolver = 1;
15693 if (sreloc == NULL)
15696 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15699 loc = sreloc->contents;
15700 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
15701 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15703 /* If this reloc is against an external symbol, it will
15704 be computed at runtime, so there's no need to do
15705 anything now. However, for the sake of prelink ensure
15706 that the section contents are a known value. */
15709 unresolved_reloc = FALSE;
15710 /* The value chosen here is quite arbitrary as ld.so
15711 ignores section contents except for the special
15712 case of .opd where the contents might be accessed
15713 before relocation. Choose zero, as that won't
15714 cause reloc overflow. */
15717 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15718 to improve backward compatibility with older
15720 if (r_type == R_PPC64_ADDR64)
15721 addend = outrel.r_addend;
15722 /* Adjust pc_relative relocs to have zero in *r_offset. */
15723 else if (ppc64_elf_howto_table[r_type]->pc_relative)
15724 addend = outrel.r_offset;
15730 case R_PPC64_GLOB_DAT:
15731 case R_PPC64_JMP_SLOT:
15732 case R_PPC64_JMP_IREL:
15733 case R_PPC64_RELATIVE:
15734 /* We shouldn't ever see these dynamic relocs in relocatable
15736 /* Fall through. */
15738 case R_PPC64_PLTGOT16:
15739 case R_PPC64_PLTGOT16_DS:
15740 case R_PPC64_PLTGOT16_HA:
15741 case R_PPC64_PLTGOT16_HI:
15742 case R_PPC64_PLTGOT16_LO:
15743 case R_PPC64_PLTGOT16_LO_DS:
15744 case R_PPC64_PLTREL32:
15745 case R_PPC64_PLTREL64:
15746 /* These ones haven't been implemented yet. */
15748 info->callbacks->einfo
15749 /* xgettext:c-format */
15750 (_("%P: %pB: %s is not supported for `%pT'\n"),
15752 ppc64_elf_howto_table[r_type]->name, sym_name);
15754 bfd_set_error (bfd_error_invalid_operation);
15759 /* Multi-instruction sequences that access the TOC can be
15760 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15761 to nop; addi rb,r2,x; */
15767 case R_PPC64_GOT_TLSLD16_HI:
15768 case R_PPC64_GOT_TLSGD16_HI:
15769 case R_PPC64_GOT_TPREL16_HI:
15770 case R_PPC64_GOT_DTPREL16_HI:
15771 case R_PPC64_GOT16_HI:
15772 case R_PPC64_TOC16_HI:
15773 /* These relocs would only be useful if building up an
15774 offset to later add to r2, perhaps in an indexed
15775 addressing mode instruction. Don't try to optimize.
15776 Unfortunately, the possibility of someone building up an
15777 offset like this or even with the HA relocs, means that
15778 we need to check the high insn when optimizing the low
15782 case R_PPC64_PLTCALL_NOTOC:
15783 if (!unresolved_reloc)
15784 htab->notoc_plt = 1;
15785 /* Fall through. */
15786 case R_PPC64_PLTCALL:
15787 if (unresolved_reloc)
15789 /* No plt entry. Make this into a direct call. */
15790 bfd_byte *p = contents + rel->r_offset;
15791 insn = bfd_get_32 (input_bfd, p);
15793 bfd_put_32 (input_bfd, B_DOT | insn, p);
15794 if (r_type == R_PPC64_PLTCALL)
15795 bfd_put_32 (input_bfd, NOP, p + 4);
15796 unresolved_reloc = save_unresolved_reloc;
15797 r_type = R_PPC64_REL24;
15801 case R_PPC64_PLTSEQ_NOTOC:
15802 case R_PPC64_PLTSEQ:
15803 if (unresolved_reloc)
15805 unresolved_reloc = FALSE;
15810 case R_PPC64_PLT_PCREL34_NOTOC:
15811 if (!unresolved_reloc)
15812 htab->notoc_plt = 1;
15813 /* Fall through. */
15814 case R_PPC64_PLT_PCREL34:
15815 if (unresolved_reloc)
15817 bfd_byte *p = contents + rel->r_offset;
15818 bfd_put_32 (input_bfd, PNOP >> 32, p);
15819 bfd_put_32 (input_bfd, PNOP, p + 4);
15820 unresolved_reloc = FALSE;
15825 case R_PPC64_PLT16_HA:
15826 if (unresolved_reloc)
15828 unresolved_reloc = FALSE;
15831 /* Fall through. */
15832 case R_PPC64_GOT_TLSLD16_HA:
15833 case R_PPC64_GOT_TLSGD16_HA:
15834 case R_PPC64_GOT_TPREL16_HA:
15835 case R_PPC64_GOT_DTPREL16_HA:
15836 case R_PPC64_GOT16_HA:
15837 case R_PPC64_TOC16_HA:
15838 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15839 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15843 p = contents + (rel->r_offset & ~3);
15844 bfd_put_32 (input_bfd, NOP, p);
15849 case R_PPC64_PLT16_LO:
15850 case R_PPC64_PLT16_LO_DS:
15851 if (unresolved_reloc)
15853 unresolved_reloc = FALSE;
15856 /* Fall through. */
15857 case R_PPC64_GOT_TLSLD16_LO:
15858 case R_PPC64_GOT_TLSGD16_LO:
15859 case R_PPC64_GOT_TPREL16_LO_DS:
15860 case R_PPC64_GOT_DTPREL16_LO_DS:
15861 case R_PPC64_GOT16_LO:
15862 case R_PPC64_GOT16_LO_DS:
15863 case R_PPC64_TOC16_LO:
15864 case R_PPC64_TOC16_LO_DS:
15865 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15866 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15868 bfd_byte *p = contents + (rel->r_offset & ~3);
15869 insn = bfd_get_32 (input_bfd, p);
15870 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15872 /* Transform addic to addi when we change reg. */
15873 insn &= ~((0x3f << 26) | (0x1f << 16));
15874 insn |= (14u << 26) | (2 << 16);
15878 insn &= ~(0x1f << 16);
15881 bfd_put_32 (input_bfd, insn, p);
15885 case R_PPC64_TPREL16_HA:
15886 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15888 bfd_byte *p = contents + (rel->r_offset & ~3);
15889 insn = bfd_get_32 (input_bfd, p);
15890 if ((insn & ((0x3f << 26) | 0x1f << 16))
15891 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15892 /* xgettext:c-format */
15893 info->callbacks->minfo
15894 (_("%H: warning: %s unexpected insn %#x.\n"),
15895 input_bfd, input_section, rel->r_offset,
15896 ppc64_elf_howto_table[r_type]->name, insn);
15899 bfd_put_32 (input_bfd, NOP, p);
15905 case R_PPC64_TPREL16_LO:
15906 case R_PPC64_TPREL16_LO_DS:
15907 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15909 bfd_byte *p = contents + (rel->r_offset & ~3);
15910 insn = bfd_get_32 (input_bfd, p);
15911 insn &= ~(0x1f << 16);
15913 bfd_put_32 (input_bfd, insn, p);
15918 /* Do any further special processing. */
15924 case R_PPC64_REL16_HA:
15925 case R_PPC64_REL16_HIGHA:
15926 case R_PPC64_REL16_HIGHERA:
15927 case R_PPC64_REL16_HIGHESTA:
15928 case R_PPC64_REL16DX_HA:
15929 case R_PPC64_ADDR16_HA:
15930 case R_PPC64_ADDR16_HIGHA:
15931 case R_PPC64_ADDR16_HIGHERA:
15932 case R_PPC64_ADDR16_HIGHESTA:
15933 case R_PPC64_TOC16_HA:
15934 case R_PPC64_SECTOFF_HA:
15935 case R_PPC64_TPREL16_HA:
15936 case R_PPC64_TPREL16_HIGHA:
15937 case R_PPC64_TPREL16_HIGHERA:
15938 case R_PPC64_TPREL16_HIGHESTA:
15939 case R_PPC64_DTPREL16_HA:
15940 case R_PPC64_DTPREL16_HIGHA:
15941 case R_PPC64_DTPREL16_HIGHERA:
15942 case R_PPC64_DTPREL16_HIGHESTA:
15943 /* It's just possible that this symbol is a weak symbol
15944 that's not actually defined anywhere. In that case,
15945 'sec' would be NULL, and we should leave the symbol
15946 alone (it will be set to zero elsewhere in the link). */
15949 /* Fall through. */
15951 case R_PPC64_GOT16_HA:
15952 case R_PPC64_PLTGOT16_HA:
15953 case R_PPC64_PLT16_HA:
15954 case R_PPC64_GOT_TLSGD16_HA:
15955 case R_PPC64_GOT_TLSLD16_HA:
15956 case R_PPC64_GOT_TPREL16_HA:
15957 case R_PPC64_GOT_DTPREL16_HA:
15958 /* Add 0x10000 if sign bit in 0:15 is set.
15959 Bits 0:15 are not used. */
15963 case R_PPC64_D34_HA30:
15964 case R_PPC64_ADDR16_HIGHERA34:
15965 case R_PPC64_ADDR16_HIGHESTA34:
15966 case R_PPC64_REL16_HIGHERA34:
15967 case R_PPC64_REL16_HIGHESTA34:
15969 addend += 1ULL << 33;
15972 case R_PPC64_ADDR16_DS:
15973 case R_PPC64_ADDR16_LO_DS:
15974 case R_PPC64_GOT16_DS:
15975 case R_PPC64_GOT16_LO_DS:
15976 case R_PPC64_PLT16_LO_DS:
15977 case R_PPC64_SECTOFF_DS:
15978 case R_PPC64_SECTOFF_LO_DS:
15979 case R_PPC64_TOC16_DS:
15980 case R_PPC64_TOC16_LO_DS:
15981 case R_PPC64_PLTGOT16_DS:
15982 case R_PPC64_PLTGOT16_LO_DS:
15983 case R_PPC64_GOT_TPREL16_DS:
15984 case R_PPC64_GOT_TPREL16_LO_DS:
15985 case R_PPC64_GOT_DTPREL16_DS:
15986 case R_PPC64_GOT_DTPREL16_LO_DS:
15987 case R_PPC64_TPREL16_DS:
15988 case R_PPC64_TPREL16_LO_DS:
15989 case R_PPC64_DTPREL16_DS:
15990 case R_PPC64_DTPREL16_LO_DS:
15991 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15993 /* If this reloc is against an lq, lxv, or stxv insn, then
15994 the value must be a multiple of 16. This is somewhat of
15995 a hack, but the "correct" way to do this by defining _DQ
15996 forms of all the _DS relocs bloats all reloc switches in
15997 this file. It doesn't make much sense to use these
15998 relocs in data, so testing the insn should be safe. */
15999 if ((insn & (0x3f << 26)) == (56u << 26)
16000 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
16002 relocation += addend;
16003 addend = insn & (mask ^ 3);
16004 if ((relocation & mask) != 0)
16006 relocation ^= relocation & mask;
16007 info->callbacks->einfo
16008 /* xgettext:c-format */
16009 (_("%H: error: %s not a multiple of %u\n"),
16010 input_bfd, input_section, rel->r_offset,
16011 ppc64_elf_howto_table[r_type]->name,
16013 bfd_set_error (bfd_error_bad_value);
16020 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16021 because such sections are not SEC_ALLOC and thus ld.so will
16022 not process them. */
16023 howto = ppc64_elf_howto_table[(int) r_type];
16024 if (unresolved_reloc
16025 && !((input_section->flags & SEC_DEBUGGING) != 0
16026 && h->elf.def_dynamic)
16027 && _bfd_elf_section_offset (output_bfd, info, input_section,
16028 rel->r_offset) != (bfd_vma) -1)
16030 info->callbacks->einfo
16031 /* xgettext:c-format */
16032 (_("%H: unresolvable %s against `%pT'\n"),
16033 input_bfd, input_section, rel->r_offset,
16035 h->elf.root.root.string);
16039 /* 16-bit fields in insns mostly have signed values, but a
16040 few insns have 16-bit unsigned values. Really, we should
16041 have different reloc types. */
16042 if (howto->complain_on_overflow != complain_overflow_dont
16043 && howto->dst_mask == 0xffff
16044 && (input_section->flags & SEC_CODE) != 0)
16046 enum complain_overflow complain = complain_overflow_signed;
16048 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16049 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16050 complain = complain_overflow_bitfield;
16051 else if (howto->rightshift == 0
16052 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16053 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16054 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16055 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16056 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16057 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
16058 complain = complain_overflow_unsigned;
16059 if (howto->complain_on_overflow != complain)
16061 alt_howto = *howto;
16062 alt_howto.complain_on_overflow = complain;
16063 howto = &alt_howto;
16069 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
16071 case R_PPC64_D34_LO:
16072 case R_PPC64_D34_HI30:
16073 case R_PPC64_D34_HA30:
16074 case R_PPC64_PCREL34:
16075 case R_PPC64_GOT_PCREL34:
16076 case R_PPC64_PLT_PCREL34:
16077 case R_PPC64_PLT_PCREL34_NOTOC:
16079 case R_PPC64_PCREL28:
16080 if (rel->r_offset + 8 > input_section->size)
16081 r = bfd_reloc_outofrange;
16084 relocation += addend;
16085 if (howto->pc_relative)
16086 relocation -= (rel->r_offset
16087 + input_section->output_offset
16088 + input_section->output_section->vma);
16089 relocation >>= howto->rightshift;
16091 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16093 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16095 pinsn &= ~howto->dst_mask;
16096 pinsn |= (((relocation << 16) | (relocation & 0xffff))
16097 & howto->dst_mask);
16098 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
16099 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
16101 if (howto->complain_on_overflow == complain_overflow_signed
16102 && (relocation + (1ULL << (howto->bitsize - 1))
16103 >= 1ULL << howto->bitsize))
16104 r = bfd_reloc_overflow;
16108 case R_PPC64_REL16DX_HA:
16109 if (rel->r_offset + 4 > input_section->size)
16110 r = bfd_reloc_outofrange;
16113 relocation += addend;
16114 relocation -= (rel->r_offset
16115 + input_section->output_offset
16116 + input_section->output_section->vma);
16117 relocation = (bfd_signed_vma) relocation >> 16;
16118 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16120 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
16121 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16123 if (relocation + 0x8000 > 0xffff)
16124 r = bfd_reloc_overflow;
16129 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
16130 contents, rel->r_offset,
16131 relocation, addend);
16134 if (r != bfd_reloc_ok)
16136 char *more_info = NULL;
16137 const char *reloc_name = howto->name;
16139 if (reloc_dest != DEST_NORMAL)
16141 more_info = bfd_malloc (strlen (reloc_name) + 8);
16142 if (more_info != NULL)
16144 strcpy (more_info, reloc_name);
16145 strcat (more_info, (reloc_dest == DEST_OPD
16146 ? " (OPD)" : " (stub)"));
16147 reloc_name = more_info;
16151 if (r == bfd_reloc_overflow)
16153 /* On code like "if (foo) foo();" don't report overflow
16154 on a branch to zero when foo is undefined. */
16156 && (reloc_dest == DEST_STUB
16158 && (h->elf.root.type == bfd_link_hash_undefweak
16159 || h->elf.root.type == bfd_link_hash_undefined)
16160 && is_branch_reloc (r_type))))
16161 info->callbacks->reloc_overflow (info, &h->elf.root,
16162 sym_name, reloc_name,
16164 input_bfd, input_section,
16169 info->callbacks->einfo
16170 /* xgettext:c-format */
16171 (_("%H: %s against `%pT': error %d\n"),
16172 input_bfd, input_section, rel->r_offset,
16173 reloc_name, sym_name, (int) r);
16176 if (more_info != NULL)
16186 Elf_Internal_Shdr *rel_hdr;
16187 size_t deleted = rel - wrel;
16189 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16190 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16191 if (rel_hdr->sh_size == 0)
16193 /* It is too late to remove an empty reloc section. Leave
16195 ??? What is wrong with an empty section??? */
16196 rel_hdr->sh_size = rel_hdr->sh_entsize;
16199 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16200 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16201 input_section->reloc_count -= deleted;
16204 /* If we're emitting relocations, then shortly after this function
16205 returns, reloc offsets and addends for this section will be
16206 adjusted. Worse, reloc symbol indices will be for the output
16207 file rather than the input. Save a copy of the relocs for
16208 opd_entry_value. */
16209 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
16212 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16213 rel = bfd_alloc (input_bfd, amt);
16214 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16215 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
16218 memcpy (rel, relocs, amt);
16223 /* Adjust the value of any local symbols in opd sections. */
16226 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16227 const char *name ATTRIBUTE_UNUSED,
16228 Elf_Internal_Sym *elfsym,
16229 asection *input_sec,
16230 struct elf_link_hash_entry *h)
16232 struct _opd_sec_data *opd;
16239 opd = get_opd_info (input_sec);
16240 if (opd == NULL || opd->adjust == NULL)
16243 value = elfsym->st_value - input_sec->output_offset;
16244 if (!bfd_link_relocatable (info))
16245 value -= input_sec->output_section->vma;
16247 adjust = opd->adjust[OPD_NDX (value)];
16251 elfsym->st_value += adjust;
16255 /* Finish up dynamic symbol handling. We set the contents of various
16256 dynamic sections here. */
16259 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16260 struct bfd_link_info *info,
16261 struct elf_link_hash_entry *h,
16262 Elf_Internal_Sym *sym)
16264 struct ppc_link_hash_table *htab;
16265 struct plt_entry *ent;
16267 htab = ppc_hash_table (info);
16271 if (!htab->opd_abi && !h->def_regular)
16272 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16273 if (ent->plt.offset != (bfd_vma) -1)
16275 /* Mark the symbol as undefined, rather than as
16276 defined in glink. Leave the value if there were
16277 any relocations where pointer equality matters
16278 (this is a clue for the dynamic linker, to make
16279 function pointer comparisons work between an
16280 application and shared library), otherwise set it
16282 sym->st_shndx = SHN_UNDEF;
16283 if (!h->pointer_equality_needed)
16285 else if (!h->ref_regular_nonweak)
16287 /* This breaks function pointer comparisons, but
16288 that is better than breaking tests for a NULL
16289 function pointer. */
16297 /* This symbol needs a copy reloc. Set it up. */
16298 Elf_Internal_Rela rela;
16302 if (h->dynindx == -1
16303 || (h->root.type != bfd_link_hash_defined
16304 && h->root.type != bfd_link_hash_defweak)
16305 || htab->elf.srelbss == NULL
16306 || htab->elf.sreldynrelro == NULL)
16309 rela.r_offset = (h->root.u.def.value
16310 + h->root.u.def.section->output_section->vma
16311 + h->root.u.def.section->output_offset);
16312 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16314 if (h->root.u.def.section == htab->elf.sdynrelro)
16315 srel = htab->elf.sreldynrelro;
16317 srel = htab->elf.srelbss;
16318 loc = srel->contents;
16319 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
16320 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
16326 /* Used to decide how to sort relocs in an optimal manner for the
16327 dynamic linker, before writing them out. */
16329 static enum elf_reloc_type_class
16330 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16331 const asection *rel_sec,
16332 const Elf_Internal_Rela *rela)
16334 enum elf_ppc64_reloc_type r_type;
16335 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16337 if (rel_sec == htab->elf.irelplt)
16338 return reloc_class_ifunc;
16340 r_type = ELF64_R_TYPE (rela->r_info);
16343 case R_PPC64_RELATIVE:
16344 return reloc_class_relative;
16345 case R_PPC64_JMP_SLOT:
16346 return reloc_class_plt;
16348 return reloc_class_copy;
16350 return reloc_class_normal;
16354 /* Finish up the dynamic sections. */
16357 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16358 struct bfd_link_info *info)
16360 struct ppc_link_hash_table *htab;
16364 htab = ppc_hash_table (info);
16368 dynobj = htab->elf.dynobj;
16369 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
16371 if (htab->elf.dynamic_sections_created)
16373 Elf64_External_Dyn *dyncon, *dynconend;
16375 if (sdyn == NULL || htab->elf.sgot == NULL)
16378 dyncon = (Elf64_External_Dyn *) sdyn->contents;
16379 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
16380 for (; dyncon < dynconend; dyncon++)
16382 Elf_Internal_Dyn dyn;
16385 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16392 case DT_PPC64_GLINK:
16394 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16395 /* We stupidly defined DT_PPC64_GLINK to be the start
16396 of glink rather than the first entry point, which is
16397 what ld.so needs, and now have a bigger stub to
16398 support automatic multiple TOCs. */
16399 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
16403 s = bfd_get_section_by_name (output_bfd, ".opd");
16406 dyn.d_un.d_ptr = s->vma;
16410 if ((htab->do_multi_toc && htab->multi_toc_needed)
16411 || htab->notoc_plt)
16412 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
16413 if (htab->has_plt_localentry0)
16414 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
16417 case DT_PPC64_OPDSZ:
16418 s = bfd_get_section_by_name (output_bfd, ".opd");
16421 dyn.d_un.d_val = s->size;
16425 s = htab->elf.splt;
16426 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16430 s = htab->elf.srelplt;
16431 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16435 dyn.d_un.d_val = htab->elf.srelplt->size;
16439 if (htab->local_ifunc_resolver)
16440 info->callbacks->einfo
16441 (_("%X%P: text relocations and GNU indirect "
16442 "functions will result in a segfault at runtime\n"));
16443 else if (htab->maybe_local_ifunc_resolver)
16444 info->callbacks->einfo
16445 (_("%P: warning: text relocations and GNU indirect "
16446 "functions may result in a segfault at runtime\n"));
16450 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
16454 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16455 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
16457 /* Fill in the first entry in the global offset table.
16458 We use it to hold the link-time TOCbase. */
16459 bfd_put_64 (output_bfd,
16460 elf_gp (output_bfd) + TOC_BASE_OFF,
16461 htab->elf.sgot->contents);
16463 /* Set .got entry size. */
16464 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
16468 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16469 && htab->elf.splt->output_section != bfd_abs_section_ptr)
16471 /* Set .plt entry size. */
16472 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
16473 = PLT_ENTRY_SIZE (htab);
16476 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16477 brlt ourselves if emitrelocations. */
16478 if (htab->brlt != NULL
16479 && htab->brlt->reloc_count != 0
16480 && !_bfd_elf_link_output_relocs (output_bfd,
16482 elf_section_data (htab->brlt)->rela.hdr,
16483 elf_section_data (htab->brlt)->relocs,
16487 if (htab->glink != NULL
16488 && htab->glink->reloc_count != 0
16489 && !_bfd_elf_link_output_relocs (output_bfd,
16491 elf_section_data (htab->glink)->rela.hdr,
16492 elf_section_data (htab->glink)->relocs,
16497 if (htab->glink_eh_frame != NULL
16498 && htab->glink_eh_frame->size != 0
16499 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16500 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16501 htab->glink_eh_frame,
16502 htab->glink_eh_frame->contents))
16505 /* We need to handle writing out multiple GOT sections ourselves,
16506 since we didn't add them to DYNOBJ. We know dynobj is the first
16508 while ((dynobj = dynobj->link.next) != NULL)
16512 if (!is_ppc64_elf (dynobj))
16515 s = ppc64_elf_tdata (dynobj)->got;
16518 && s->output_section != bfd_abs_section_ptr
16519 && !bfd_set_section_contents (output_bfd, s->output_section,
16520 s->contents, s->output_offset,
16523 s = ppc64_elf_tdata (dynobj)->relgot;
16526 && s->output_section != bfd_abs_section_ptr
16527 && !bfd_set_section_contents (output_bfd, s->output_section,
16528 s->contents, s->output_offset,
16536 #include "elf64-target.h"
16538 /* FreeBSD support */
16540 #undef TARGET_LITTLE_SYM
16541 #undef TARGET_LITTLE_NAME
16543 #undef TARGET_BIG_SYM
16544 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
16545 #undef TARGET_BIG_NAME
16546 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16549 #define ELF_OSABI ELFOSABI_FREEBSD
16552 #define elf64_bed elf64_powerpc_fbsd_bed
16554 #include "elf64-target.h"