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 ppc64_elf_tdata (abfd)->has_gotrel = 1;
4571 ppc64_elf_section_data (sec)->has_gotrel = 1;
4575 case R_PPC64_GOT16_HI:
4576 case R_PPC64_GOT16_LO:
4577 case R_PPC64_GOT_PCREL34:
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 /* Examine all relocs referencing .toc sections in order to remove
8145 unused .toc entries. */
8148 ppc64_elf_edit_toc (struct bfd_link_info *info)
8151 struct adjust_toc_info toc_inf;
8152 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8154 htab->do_toc_opt = 1;
8155 toc_inf.global_toc_syms = TRUE;
8156 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8158 asection *toc, *sec;
8159 Elf_Internal_Shdr *symtab_hdr;
8160 Elf_Internal_Sym *local_syms;
8161 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8162 unsigned long *skip, *drop;
8163 unsigned char *used;
8164 unsigned char *keep, last, some_unused;
8166 if (!is_ppc64_elf (ibfd))
8169 toc = bfd_get_section_by_name (ibfd, ".toc");
8172 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8173 || discarded_section (toc))
8178 symtab_hdr = &elf_symtab_hdr (ibfd);
8180 /* Look at sections dropped from the final link. */
8183 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8185 if (sec->reloc_count == 0
8186 || !discarded_section (sec)
8187 || get_opd_info (sec)
8188 || (sec->flags & SEC_ALLOC) == 0
8189 || (sec->flags & SEC_DEBUGGING) != 0)
8192 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8193 if (relstart == NULL)
8196 /* Run through the relocs to see which toc entries might be
8198 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8200 enum elf_ppc64_reloc_type r_type;
8201 unsigned long r_symndx;
8203 struct elf_link_hash_entry *h;
8204 Elf_Internal_Sym *sym;
8207 r_type = ELF64_R_TYPE (rel->r_info);
8214 case R_PPC64_TOC16_LO:
8215 case R_PPC64_TOC16_HI:
8216 case R_PPC64_TOC16_HA:
8217 case R_PPC64_TOC16_DS:
8218 case R_PPC64_TOC16_LO_DS:
8222 r_symndx = ELF64_R_SYM (rel->r_info);
8223 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8231 val = h->root.u.def.value;
8233 val = sym->st_value;
8234 val += rel->r_addend;
8236 if (val >= toc->size)
8239 /* Anything in the toc ought to be aligned to 8 bytes.
8240 If not, don't mark as unused. */
8246 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8251 skip[val >> 3] = ref_from_discarded;
8254 if (elf_section_data (sec)->relocs != relstart)
8258 /* For largetoc loads of address constants, we can convert
8259 . addis rx,2,addr@got@ha
8260 . ld ry,addr@got@l(rx)
8262 . addis rx,2,addr@toc@ha
8263 . addi ry,rx,addr@toc@l
8264 when addr is within 2G of the toc pointer. This then means
8265 that the word storing "addr" in the toc is no longer needed. */
8267 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8268 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8269 && toc->reloc_count != 0)
8271 /* Read toc relocs. */
8272 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8274 if (toc_relocs == NULL)
8277 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8279 enum elf_ppc64_reloc_type r_type;
8280 unsigned long r_symndx;
8282 struct elf_link_hash_entry *h;
8283 Elf_Internal_Sym *sym;
8286 r_type = ELF64_R_TYPE (rel->r_info);
8287 if (r_type != R_PPC64_ADDR64)
8290 r_symndx = ELF64_R_SYM (rel->r_info);
8291 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8296 || sym_sec->output_section == NULL
8297 || discarded_section (sym_sec))
8300 if (!SYMBOL_REFERENCES_LOCAL (info, h))
8305 if (h->type == STT_GNU_IFUNC)
8307 val = h->root.u.def.value;
8311 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8313 val = sym->st_value;
8315 val += rel->r_addend;
8316 val += sym_sec->output_section->vma + sym_sec->output_offset;
8318 /* We don't yet know the exact toc pointer value, but we
8319 know it will be somewhere in the toc section. Don't
8320 optimize if the difference from any possible toc
8321 pointer is outside [ff..f80008000, 7fff7fff]. */
8322 addr = toc->output_section->vma + TOC_BASE_OFF;
8323 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8326 addr = toc->output_section->vma + toc->output_section->rawsize;
8327 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8332 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8337 skip[rel->r_offset >> 3]
8338 |= can_optimize | ((rel - toc_relocs) << 2);
8345 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8349 if (local_syms != NULL
8350 && symtab_hdr->contents != (unsigned char *) local_syms)
8354 && elf_section_data (sec)->relocs != relstart)
8356 if (toc_relocs != NULL
8357 && elf_section_data (toc)->relocs != toc_relocs)
8364 /* Now check all kept sections that might reference the toc.
8365 Check the toc itself last. */
8366 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8369 sec = (sec == toc ? NULL
8370 : sec->next == NULL ? toc
8371 : sec->next == toc && toc->next ? toc->next
8376 if (sec->reloc_count == 0
8377 || discarded_section (sec)
8378 || get_opd_info (sec)
8379 || (sec->flags & SEC_ALLOC) == 0
8380 || (sec->flags & SEC_DEBUGGING) != 0)
8383 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8385 if (relstart == NULL)
8391 /* Mark toc entries referenced as used. */
8395 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8397 enum elf_ppc64_reloc_type r_type;
8398 unsigned long r_symndx;
8400 struct elf_link_hash_entry *h;
8401 Elf_Internal_Sym *sym;
8403 enum {no_check, check_lo, check_ha} insn_check;
8405 r_type = ELF64_R_TYPE (rel->r_info);
8409 insn_check = no_check;
8412 case R_PPC64_GOT_TLSLD16_HA:
8413 case R_PPC64_GOT_TLSGD16_HA:
8414 case R_PPC64_GOT_TPREL16_HA:
8415 case R_PPC64_GOT_DTPREL16_HA:
8416 case R_PPC64_GOT16_HA:
8417 case R_PPC64_TOC16_HA:
8418 insn_check = check_ha;
8421 case R_PPC64_GOT_TLSLD16_LO:
8422 case R_PPC64_GOT_TLSGD16_LO:
8423 case R_PPC64_GOT_TPREL16_LO_DS:
8424 case R_PPC64_GOT_DTPREL16_LO_DS:
8425 case R_PPC64_GOT16_LO:
8426 case R_PPC64_GOT16_LO_DS:
8427 case R_PPC64_TOC16_LO:
8428 case R_PPC64_TOC16_LO_DS:
8429 insn_check = check_lo;
8433 if (insn_check != no_check)
8435 bfd_vma off = rel->r_offset & ~3;
8436 unsigned char buf[4];
8439 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8444 insn = bfd_get_32 (ibfd, buf);
8445 if (insn_check == check_lo
8446 ? !ok_lo_toc_insn (insn, r_type)
8447 : ((insn & ((0x3f << 26) | 0x1f << 16))
8448 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8452 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8453 sprintf (str, "%#08x", insn);
8454 info->callbacks->einfo
8455 /* xgettext:c-format */
8456 (_("%H: toc optimization is not supported for"
8457 " %s instruction\n"),
8458 ibfd, sec, rel->r_offset & ~3, str);
8465 case R_PPC64_TOC16_LO:
8466 case R_PPC64_TOC16_HI:
8467 case R_PPC64_TOC16_HA:
8468 case R_PPC64_TOC16_DS:
8469 case R_PPC64_TOC16_LO_DS:
8470 /* In case we're taking addresses of toc entries. */
8471 case R_PPC64_ADDR64:
8478 r_symndx = ELF64_R_SYM (rel->r_info);
8479 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8490 val = h->root.u.def.value;
8492 val = sym->st_value;
8493 val += rel->r_addend;
8495 if (val >= toc->size)
8498 if ((skip[val >> 3] & can_optimize) != 0)
8505 case R_PPC64_TOC16_HA:
8508 case R_PPC64_TOC16_LO_DS:
8509 off = rel->r_offset;
8510 off += (bfd_big_endian (ibfd) ? -2 : 3);
8511 if (!bfd_get_section_contents (ibfd, sec, &opc,
8517 if ((opc & (0x3f << 2)) == (58u << 2))
8522 /* Wrong sort of reloc, or not a ld. We may
8523 as well clear ref_from_discarded too. */
8530 /* For the toc section, we only mark as used if this
8531 entry itself isn't unused. */
8532 else if ((used[rel->r_offset >> 3]
8533 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8536 /* Do all the relocs again, to catch reference
8545 if (elf_section_data (sec)->relocs != relstart)
8549 /* Merge the used and skip arrays. Assume that TOC
8550 doublewords not appearing as either used or unused belong
8551 to an entry more than one doubleword in size. */
8552 for (drop = skip, keep = used, last = 0, some_unused = 0;
8553 drop < skip + (toc->size + 7) / 8;
8558 *drop &= ~ref_from_discarded;
8559 if ((*drop & can_optimize) != 0)
8563 else if ((*drop & ref_from_discarded) != 0)
8566 last = ref_from_discarded;
8576 bfd_byte *contents, *src;
8578 Elf_Internal_Sym *sym;
8579 bfd_boolean local_toc_syms = FALSE;
8581 /* Shuffle the toc contents, and at the same time convert the
8582 skip array from booleans into offsets. */
8583 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8586 elf_section_data (toc)->this_hdr.contents = contents;
8588 for (src = contents, off = 0, drop = skip;
8589 src < contents + toc->size;
8592 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8597 memcpy (src - off, src, 8);
8601 toc->rawsize = toc->size;
8602 toc->size = src - contents - off;
8604 /* Adjust addends for relocs against the toc section sym,
8605 and optimize any accesses we can. */
8606 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8608 if (sec->reloc_count == 0
8609 || discarded_section (sec))
8612 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8614 if (relstart == NULL)
8617 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8619 enum elf_ppc64_reloc_type r_type;
8620 unsigned long r_symndx;
8622 struct elf_link_hash_entry *h;
8625 r_type = ELF64_R_TYPE (rel->r_info);
8632 case R_PPC64_TOC16_LO:
8633 case R_PPC64_TOC16_HI:
8634 case R_PPC64_TOC16_HA:
8635 case R_PPC64_TOC16_DS:
8636 case R_PPC64_TOC16_LO_DS:
8637 case R_PPC64_ADDR64:
8641 r_symndx = ELF64_R_SYM (rel->r_info);
8642 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8650 val = h->root.u.def.value;
8653 val = sym->st_value;
8655 local_toc_syms = TRUE;
8658 val += rel->r_addend;
8660 if (val > toc->rawsize)
8662 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8664 else if ((skip[val >> 3] & can_optimize) != 0)
8666 Elf_Internal_Rela *tocrel
8667 = toc_relocs + (skip[val >> 3] >> 2);
8668 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8672 case R_PPC64_TOC16_HA:
8673 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8676 case R_PPC64_TOC16_LO_DS:
8677 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8681 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8683 info->callbacks->einfo
8684 /* xgettext:c-format */
8685 (_("%H: %s references "
8686 "optimized away TOC entry\n"),
8687 ibfd, sec, rel->r_offset,
8688 ppc64_elf_howto_table[r_type]->name);
8689 bfd_set_error (bfd_error_bad_value);
8692 rel->r_addend = tocrel->r_addend;
8693 elf_section_data (sec)->relocs = relstart;
8697 if (h != NULL || sym->st_value != 0)
8700 rel->r_addend -= skip[val >> 3];
8701 elf_section_data (sec)->relocs = relstart;
8704 if (elf_section_data (sec)->relocs != relstart)
8708 /* We shouldn't have local or global symbols defined in the TOC,
8709 but handle them anyway. */
8710 if (local_syms != NULL)
8711 for (sym = local_syms;
8712 sym < local_syms + symtab_hdr->sh_info;
8714 if (sym->st_value != 0
8715 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8719 if (sym->st_value > toc->rawsize)
8720 i = toc->rawsize >> 3;
8722 i = sym->st_value >> 3;
8724 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8728 (_("%s defined on removed toc entry"),
8729 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8732 while ((skip[i] & (ref_from_discarded | can_optimize)));
8733 sym->st_value = (bfd_vma) i << 3;
8736 sym->st_value -= skip[i];
8737 symtab_hdr->contents = (unsigned char *) local_syms;
8740 /* Adjust any global syms defined in this toc input section. */
8741 if (toc_inf.global_toc_syms)
8744 toc_inf.skip = skip;
8745 toc_inf.global_toc_syms = FALSE;
8746 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8750 if (toc->reloc_count != 0)
8752 Elf_Internal_Shdr *rel_hdr;
8753 Elf_Internal_Rela *wrel;
8756 /* Remove unused toc relocs, and adjust those we keep. */
8757 if (toc_relocs == NULL)
8758 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8760 if (toc_relocs == NULL)
8764 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8765 if ((skip[rel->r_offset >> 3]
8766 & (ref_from_discarded | can_optimize)) == 0)
8768 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8769 wrel->r_info = rel->r_info;
8770 wrel->r_addend = rel->r_addend;
8773 else if (!dec_dynrel_count (rel->r_info, toc, info,
8774 &local_syms, NULL, NULL))
8777 elf_section_data (toc)->relocs = toc_relocs;
8778 toc->reloc_count = wrel - toc_relocs;
8779 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8780 sz = rel_hdr->sh_entsize;
8781 rel_hdr->sh_size = toc->reloc_count * sz;
8784 else if (toc_relocs != NULL
8785 && elf_section_data (toc)->relocs != toc_relocs)
8788 if (local_syms != NULL
8789 && symtab_hdr->contents != (unsigned char *) local_syms)
8791 if (!info->keep_memory)
8794 symtab_hdr->contents = (unsigned char *) local_syms;
8799 /* Look for cases where we can change an indirect GOT access to
8800 a GOT relative access, possibly reducing the number of GOT
8802 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8805 Elf_Internal_Shdr *symtab_hdr;
8806 Elf_Internal_Sym *local_syms;
8807 Elf_Internal_Rela *relstart, *rel;
8810 if (!is_ppc64_elf (ibfd))
8813 if (!ppc64_elf_tdata (ibfd)->has_gotrel)
8816 sec = ppc64_elf_tdata (ibfd)->got;
8817 got = sec->output_section->vma + sec->output_offset + 0x8000;
8820 symtab_hdr = &elf_symtab_hdr (ibfd);
8822 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8824 if (sec->reloc_count == 0
8825 || !ppc64_elf_section_data (sec)->has_gotrel
8826 || discarded_section (sec))
8829 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8831 if (relstart == NULL)
8834 if (local_syms != NULL
8835 && symtab_hdr->contents != (unsigned char *) local_syms)
8839 && elf_section_data (sec)->relocs != relstart)
8844 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8846 enum elf_ppc64_reloc_type r_type;
8847 unsigned long r_symndx;
8848 Elf_Internal_Sym *sym;
8850 struct elf_link_hash_entry *h;
8851 struct got_entry *ent;
8853 unsigned char buf[4];
8856 r_type = ELF64_R_TYPE (rel->r_info);
8862 case R_PPC64_GOT16_DS:
8863 case R_PPC64_GOT16_HA:
8864 case R_PPC64_GOT16_LO_DS:
8868 r_symndx = ELF64_R_SYM (rel->r_info);
8869 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8873 if (!SYMBOL_REFERENCES_LOCAL (info, h))
8877 val = h->root.u.def.value;
8879 val = sym->st_value;
8880 val += rel->r_addend;
8881 val += sym_sec->output_section->vma + sym_sec->output_offset;
8888 case R_PPC64_GOT16_DS:
8889 if (val - got + 0x8000 >= 0x10000)
8891 if (!bfd_get_section_contents (ibfd, sec, buf,
8892 rel->r_offset & ~3, 4))
8894 insn = bfd_get_32 (ibfd, buf);
8895 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
8899 case R_PPC64_GOT16_HA:
8900 if (val - got + 0x80008000ULL >= 0x100000000ULL)
8903 if (!bfd_get_section_contents (ibfd, sec, buf,
8904 rel->r_offset & ~3, 4))
8906 insn = bfd_get_32 (ibfd, buf);
8907 if (((insn & ((0x3f << 26) | 0x1f << 16))
8908 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8912 case R_PPC64_GOT16_LO_DS:
8913 if (val - got + 0x80008000ULL >= 0x100000000ULL)
8915 if (!bfd_get_section_contents (ibfd, sec, buf,
8916 rel->r_offset & ~3, 4))
8918 insn = bfd_get_32 (ibfd, buf);
8919 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
8928 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
8929 ent = local_got_ents[r_symndx];
8931 for (; ent != NULL; ent = ent->next)
8932 if (ent->addend == rel->r_addend
8933 && ent->owner == ibfd
8934 && ent->tls_type == 0)
8936 BFD_ASSERT (ent && ent->got.refcount > 0);
8937 ent->got.refcount -= 1;
8940 if (elf_section_data (sec)->relocs != relstart)
8944 if (local_syms != NULL
8945 && symtab_hdr->contents != (unsigned char *) local_syms)
8947 if (!info->keep_memory)
8950 symtab_hdr->contents = (unsigned char *) local_syms;
8957 /* Return true iff input section I references the TOC using
8958 instructions limited to +/-32k offsets. */
8961 ppc64_elf_has_small_toc_reloc (asection *i)
8963 return (is_ppc64_elf (i->owner)
8964 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8967 /* Allocate space for one GOT entry. */
8970 allocate_got (struct elf_link_hash_entry *h,
8971 struct bfd_link_info *info,
8972 struct got_entry *gent)
8974 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8975 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8976 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8978 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8979 ? 2 : 1) * sizeof (Elf64_External_Rela);
8980 asection *got = ppc64_elf_tdata (gent->owner)->got;
8982 gent->got.offset = got->size;
8983 got->size += entsize;
8985 if (h->type == STT_GNU_IFUNC)
8987 htab->elf.irelplt->size += rentsize;
8988 htab->got_reli_size += rentsize;
8990 else if (((bfd_link_pic (info)
8991 && !((gent->tls_type & TLS_TPREL) != 0
8992 && bfd_link_executable (info)
8993 && SYMBOL_REFERENCES_LOCAL (info, h)))
8994 || (htab->elf.dynamic_sections_created
8996 && !SYMBOL_REFERENCES_LOCAL (info, h)))
8997 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8999 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9000 relgot->size += rentsize;
9004 /* This function merges got entries in the same toc group. */
9007 merge_got_entries (struct got_entry **pent)
9009 struct got_entry *ent, *ent2;
9011 for (ent = *pent; ent != NULL; ent = ent->next)
9012 if (!ent->is_indirect)
9013 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9014 if (!ent2->is_indirect
9015 && ent2->addend == ent->addend
9016 && ent2->tls_type == ent->tls_type
9017 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9019 ent2->is_indirect = TRUE;
9020 ent2->got.ent = ent;
9024 /* If H is undefined, make it dynamic if that makes sense. */
9027 ensure_undef_dynamic (struct bfd_link_info *info,
9028 struct elf_link_hash_entry *h)
9030 struct elf_link_hash_table *htab = elf_hash_table (info);
9032 if (htab->dynamic_sections_created
9033 && ((info->dynamic_undefined_weak != 0
9034 && h->root.type == bfd_link_hash_undefweak)
9035 || h->root.type == bfd_link_hash_undefined)
9038 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9039 return bfd_elf_link_record_dynamic_symbol (info, h);
9043 /* Allocate space in .plt, .got and associated reloc sections for
9047 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9049 struct bfd_link_info *info;
9050 struct ppc_link_hash_table *htab;
9052 struct ppc_link_hash_entry *eh;
9053 struct got_entry **pgent, *gent;
9055 if (h->root.type == bfd_link_hash_indirect)
9058 info = (struct bfd_link_info *) inf;
9059 htab = ppc_hash_table (info);
9063 eh = (struct ppc_link_hash_entry *) h;
9064 /* Run through the TLS GD got entries first if we're changing them
9066 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
9067 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9068 if (gent->got.refcount > 0
9069 && (gent->tls_type & TLS_GD) != 0)
9071 /* This was a GD entry that has been converted to TPREL. If
9072 there happens to be a TPREL entry we can use that one. */
9073 struct got_entry *ent;
9074 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9075 if (ent->got.refcount > 0
9076 && (ent->tls_type & TLS_TPREL) != 0
9077 && ent->addend == gent->addend
9078 && ent->owner == gent->owner)
9080 gent->got.refcount = 0;
9084 /* If not, then we'll be using our own TPREL entry. */
9085 if (gent->got.refcount != 0)
9086 gent->tls_type = TLS_TLS | TLS_TPREL;
9089 /* Remove any list entry that won't generate a word in the GOT before
9090 we call merge_got_entries. Otherwise we risk merging to empty
9092 pgent = &h->got.glist;
9093 while ((gent = *pgent) != NULL)
9094 if (gent->got.refcount > 0)
9096 if ((gent->tls_type & TLS_LD) != 0
9099 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9100 *pgent = gent->next;
9103 pgent = &gent->next;
9106 *pgent = gent->next;
9108 if (!htab->do_multi_toc)
9109 merge_got_entries (&h->got.glist);
9111 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9112 if (!gent->is_indirect)
9114 /* Make sure this symbol is output as a dynamic symbol. */
9115 if (!ensure_undef_dynamic (info, h))
9118 if (!is_ppc64_elf (gent->owner))
9121 allocate_got (h, info, gent);
9124 /* If no dynamic sections we can't have dynamic relocs, except for
9125 IFUNCs which are handled even in static executables. */
9126 if (!htab->elf.dynamic_sections_created
9127 && h->type != STT_GNU_IFUNC)
9128 eh->dyn_relocs = NULL;
9130 /* Discard relocs on undefined symbols that must be local. */
9131 else if (h->root.type == bfd_link_hash_undefined
9132 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9133 eh->dyn_relocs = NULL;
9135 /* Also discard relocs on undefined weak syms with non-default
9136 visibility, or when dynamic_undefined_weak says so. */
9137 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9138 eh->dyn_relocs = NULL;
9140 if (eh->dyn_relocs != NULL)
9142 struct elf_dyn_relocs *p, **pp;
9144 /* In the shared -Bsymbolic case, discard space allocated for
9145 dynamic pc-relative relocs against symbols which turn out to
9146 be defined in regular objects. For the normal shared case,
9147 discard space for relocs that have become local due to symbol
9148 visibility changes. */
9150 if (bfd_link_pic (info))
9152 /* Relocs that use pc_count are those that appear on a call
9153 insn, or certain REL relocs (see must_be_dyn_reloc) that
9154 can be generated via assembly. We want calls to
9155 protected symbols to resolve directly to the function
9156 rather than going via the plt. If people want function
9157 pointer comparisons to work as expected then they should
9158 avoid writing weird assembly. */
9159 if (SYMBOL_CALLS_LOCAL (info, h))
9161 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9163 p->count -= p->pc_count;
9172 if (eh->dyn_relocs != NULL)
9174 /* Make sure this symbol is output as a dynamic symbol. */
9175 if (!ensure_undef_dynamic (info, h))
9179 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
9181 /* For the non-pic case, discard space for relocs against
9182 symbols which turn out to need copy relocs or are not
9184 if (h->dynamic_adjusted
9186 && !ELF_COMMON_DEF_P (h))
9188 /* Make sure this symbol is output as a dynamic symbol. */
9189 if (!ensure_undef_dynamic (info, h))
9192 if (h->dynindx == -1)
9193 eh->dyn_relocs = NULL;
9196 eh->dyn_relocs = NULL;
9199 /* Finally, allocate space. */
9200 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9202 asection *sreloc = elf_section_data (p->sec)->sreloc;
9203 if (eh->elf.type == STT_GNU_IFUNC)
9204 sreloc = htab->elf.irelplt;
9205 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9209 /* We might need a PLT entry when the symbol
9212 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9213 d) has plt16 relocs and we are linking statically. */
9214 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9215 || h->type == STT_GNU_IFUNC
9216 || (h->needs_plt && h->dynamic_adjusted)
9219 && !htab->elf.dynamic_sections_created
9220 && !htab->can_convert_all_inline_plt
9221 && (((struct ppc_link_hash_entry *) h)->tls_mask
9222 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
9224 struct plt_entry *pent;
9225 bfd_boolean doneone = FALSE;
9226 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9227 if (pent->plt.refcount > 0)
9229 if (!htab->elf.dynamic_sections_created
9230 || h->dynindx == -1)
9232 if (h->type == STT_GNU_IFUNC)
9235 pent->plt.offset = s->size;
9236 s->size += PLT_ENTRY_SIZE (htab);
9237 s = htab->elf.irelplt;
9242 pent->plt.offset = s->size;
9243 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9244 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9249 /* If this is the first .plt entry, make room for the special
9253 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9255 pent->plt.offset = s->size;
9257 /* Make room for this entry. */
9258 s->size += PLT_ENTRY_SIZE (htab);
9260 /* Make room for the .glink code. */
9263 s->size += GLINK_PLTRESOLVE_SIZE (htab);
9266 /* We need bigger stubs past index 32767. */
9267 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
9274 /* We also need to make an entry in the .rela.plt section. */
9275 s = htab->elf.srelplt;
9278 s->size += sizeof (Elf64_External_Rela);
9282 pent->plt.offset = (bfd_vma) -1;
9285 h->plt.plist = NULL;
9291 h->plt.plist = NULL;
9298 #define PPC_LO(v) ((v) & 0xffff)
9299 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9300 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9302 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9303 to set up space for global entry stubs. These are put in glink,
9304 after the branch table. */
9307 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9309 struct bfd_link_info *info;
9310 struct ppc_link_hash_table *htab;
9311 struct plt_entry *pent;
9314 if (h->root.type == bfd_link_hash_indirect)
9317 if (!h->pointer_equality_needed)
9324 htab = ppc_hash_table (info);
9328 s = htab->global_entry;
9329 plt = htab->elf.splt;
9330 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9331 if (pent->plt.offset != (bfd_vma) -1
9332 && pent->addend == 0)
9334 /* For ELFv2, if this symbol is not defined in a regular file
9335 and we are not generating a shared library or pie, then we
9336 need to define the symbol in the executable on a call stub.
9337 This is to avoid text relocations. */
9338 bfd_vma off, stub_align, stub_off, stub_size;
9339 unsigned int align_power;
9343 if (htab->params->plt_stub_align >= 0)
9344 align_power = htab->params->plt_stub_align;
9346 align_power = -htab->params->plt_stub_align;
9347 /* Setting section alignment is delayed until we know it is
9348 non-empty. Otherwise the .text output section will be
9349 aligned at least to plt_stub_align even when no global
9350 entry stubs are needed. */
9351 if (s->alignment_power < align_power)
9352 s->alignment_power = align_power;
9353 stub_align = (bfd_vma) 1 << align_power;
9354 if (htab->params->plt_stub_align >= 0
9355 || ((((stub_off + stub_size - 1) & -stub_align)
9356 - (stub_off & -stub_align))
9357 > ((stub_size - 1) & -stub_align)))
9358 stub_off = (stub_off + stub_align - 1) & -stub_align;
9359 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9360 off -= stub_off + s->output_offset + s->output_section->vma;
9361 /* Note that for --plt-stub-align negative we have a possible
9362 dependency between stub offset and size. Break that
9363 dependency by assuming the max stub size when calculating
9365 if (PPC_HA (off) == 0)
9367 h->root.type = bfd_link_hash_defined;
9368 h->root.u.def.section = s;
9369 h->root.u.def.value = stub_off;
9370 s->size = stub_off + stub_size;
9376 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9377 read-only sections. */
9380 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
9384 if (h->root.type == bfd_link_hash_indirect)
9387 sec = readonly_dynrelocs (h);
9390 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9392 info->flags |= DF_TEXTREL;
9393 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9394 " in read-only section `%pA'\n"),
9395 sec->owner, h->root.root.string, sec);
9397 /* Not an error, just cut short the traversal. */
9403 /* Set the sizes of the dynamic sections. */
9406 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9407 struct bfd_link_info *info)
9409 struct ppc_link_hash_table *htab;
9414 struct got_entry *first_tlsld;
9416 htab = ppc_hash_table (info);
9420 dynobj = htab->elf.dynobj;
9424 if (htab->elf.dynamic_sections_created)
9426 /* Set the contents of the .interp section to the interpreter. */
9427 if (bfd_link_executable (info) && !info->nointerp)
9429 s = bfd_get_linker_section (dynobj, ".interp");
9432 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9433 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9437 /* Set up .got offsets for local syms, and space for local dynamic
9439 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9441 struct got_entry **lgot_ents;
9442 struct got_entry **end_lgot_ents;
9443 struct plt_entry **local_plt;
9444 struct plt_entry **end_local_plt;
9445 unsigned char *lgot_masks;
9446 bfd_size_type locsymcount;
9447 Elf_Internal_Shdr *symtab_hdr;
9449 if (!is_ppc64_elf (ibfd))
9452 for (s = ibfd->sections; s != NULL; s = s->next)
9454 struct ppc_dyn_relocs *p;
9456 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9458 if (!bfd_is_abs_section (p->sec)
9459 && bfd_is_abs_section (p->sec->output_section))
9461 /* Input section has been discarded, either because
9462 it is a copy of a linkonce section or due to
9463 linker script /DISCARD/, so we'll be discarding
9466 else if (p->count != 0)
9468 asection *srel = elf_section_data (p->sec)->sreloc;
9470 srel = htab->elf.irelplt;
9471 srel->size += p->count * sizeof (Elf64_External_Rela);
9472 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9473 info->flags |= DF_TEXTREL;
9478 lgot_ents = elf_local_got_ents (ibfd);
9482 symtab_hdr = &elf_symtab_hdr (ibfd);
9483 locsymcount = symtab_hdr->sh_info;
9484 end_lgot_ents = lgot_ents + locsymcount;
9485 local_plt = (struct plt_entry **) end_lgot_ents;
9486 end_local_plt = local_plt + locsymcount;
9487 lgot_masks = (unsigned char *) end_local_plt;
9488 s = ppc64_elf_tdata (ibfd)->got;
9489 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9491 struct got_entry **pent, *ent;
9494 while ((ent = *pent) != NULL)
9495 if (ent->got.refcount > 0)
9497 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9499 ppc64_tlsld_got (ibfd)->got.refcount += 1;
9504 unsigned int ent_size = 8;
9505 unsigned int rel_size = sizeof (Elf64_External_Rela);
9507 ent->got.offset = s->size;
9508 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9513 s->size += ent_size;
9514 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9516 htab->elf.irelplt->size += rel_size;
9517 htab->got_reli_size += rel_size;
9519 else if (bfd_link_pic (info)
9520 && !((ent->tls_type & TLS_TPREL) != 0
9521 && bfd_link_executable (info)))
9523 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9524 srel->size += rel_size;
9533 /* Allocate space for plt calls to local syms. */
9534 lgot_masks = (unsigned char *) end_local_plt;
9535 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
9537 struct plt_entry *ent;
9539 for (ent = *local_plt; ent != NULL; ent = ent->next)
9540 if (ent->plt.refcount > 0)
9542 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9545 ent->plt.offset = s->size;
9546 s->size += PLT_ENTRY_SIZE (htab);
9547 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9549 else if (htab->can_convert_all_inline_plt
9550 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
9551 ent->plt.offset = (bfd_vma) -1;
9555 ent->plt.offset = s->size;
9556 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9557 if (bfd_link_pic (info))
9558 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9562 ent->plt.offset = (bfd_vma) -1;
9566 /* Allocate global sym .plt and .got entries, and space for global
9567 sym dynamic relocs. */
9568 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9570 if (!htab->opd_abi && !bfd_link_pic (info))
9571 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
9574 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9576 struct got_entry *ent;
9578 if (!is_ppc64_elf (ibfd))
9581 ent = ppc64_tlsld_got (ibfd);
9582 if (ent->got.refcount > 0)
9584 if (!htab->do_multi_toc && first_tlsld != NULL)
9586 ent->is_indirect = TRUE;
9587 ent->got.ent = first_tlsld;
9591 if (first_tlsld == NULL)
9593 s = ppc64_elf_tdata (ibfd)->got;
9594 ent->got.offset = s->size;
9597 if (bfd_link_pic (info))
9599 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9600 srel->size += sizeof (Elf64_External_Rela);
9605 ent->got.offset = (bfd_vma) -1;
9608 /* We now have determined the sizes of the various dynamic sections.
9609 Allocate memory for them. */
9611 for (s = dynobj->sections; s != NULL; s = s->next)
9613 if ((s->flags & SEC_LINKER_CREATED) == 0)
9616 if (s == htab->brlt || s == htab->relbrlt)
9617 /* These haven't been allocated yet; don't strip. */
9619 else if (s == htab->elf.sgot
9620 || s == htab->elf.splt
9621 || s == htab->elf.iplt
9622 || s == htab->pltlocal
9624 || s == htab->global_entry
9625 || s == htab->elf.sdynbss
9626 || s == htab->elf.sdynrelro)
9628 /* Strip this section if we don't need it; see the
9631 else if (s == htab->glink_eh_frame)
9633 if (!bfd_is_abs_section (s->output_section))
9634 /* Not sized yet. */
9637 else if (CONST_STRNEQ (s->name, ".rela"))
9641 if (s != htab->elf.srelplt)
9644 /* We use the reloc_count field as a counter if we need
9645 to copy relocs into the output file. */
9651 /* It's not one of our sections, so don't allocate space. */
9657 /* If we don't need this section, strip it from the
9658 output file. This is mostly to handle .rela.bss and
9659 .rela.plt. We must create both sections in
9660 create_dynamic_sections, because they must be created
9661 before the linker maps input sections to output
9662 sections. The linker does that before
9663 adjust_dynamic_symbol is called, and it is that
9664 function which decides whether anything needs to go
9665 into these sections. */
9666 s->flags |= SEC_EXCLUDE;
9670 if (bfd_is_abs_section (s->output_section))
9671 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9674 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9677 /* Allocate memory for the section contents. We use bfd_zalloc
9678 here in case unused entries are not reclaimed before the
9679 section's contents are written out. This should not happen,
9680 but this way if it does we get a R_PPC64_NONE reloc in .rela
9681 sections instead of garbage.
9682 We also rely on the section contents being zero when writing
9683 the GOT and .dynrelro. */
9684 s->contents = bfd_zalloc (dynobj, s->size);
9685 if (s->contents == NULL)
9689 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9691 if (!is_ppc64_elf (ibfd))
9694 s = ppc64_elf_tdata (ibfd)->got;
9695 if (s != NULL && s != htab->elf.sgot)
9698 s->flags |= SEC_EXCLUDE;
9701 s->contents = bfd_zalloc (ibfd, s->size);
9702 if (s->contents == NULL)
9706 s = ppc64_elf_tdata (ibfd)->relgot;
9710 s->flags |= SEC_EXCLUDE;
9713 s->contents = bfd_zalloc (ibfd, s->size);
9714 if (s->contents == NULL)
9722 if (htab->elf.dynamic_sections_created)
9724 bfd_boolean tls_opt;
9726 /* Add some entries to the .dynamic section. We fill in the
9727 values later, in ppc64_elf_finish_dynamic_sections, but we
9728 must add the entries now so that we get the correct size for
9729 the .dynamic section. The DT_DEBUG entry is filled in by the
9730 dynamic linker and used by the debugger. */
9731 #define add_dynamic_entry(TAG, VAL) \
9732 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9734 if (bfd_link_executable (info))
9736 if (!add_dynamic_entry (DT_DEBUG, 0))
9740 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
9742 if (!add_dynamic_entry (DT_PLTGOT, 0)
9743 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9744 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9745 || !add_dynamic_entry (DT_JMPREL, 0)
9746 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9750 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
9752 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9753 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9757 tls_opt = (htab->params->tls_get_addr_opt
9758 && htab->tls_get_addr_fd != NULL
9759 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9760 if (tls_opt || !htab->opd_abi)
9762 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9768 if (!add_dynamic_entry (DT_RELA, 0)
9769 || !add_dynamic_entry (DT_RELASZ, 0)
9770 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9773 /* If any dynamic relocs apply to a read-only section,
9774 then we need a DT_TEXTREL entry. */
9775 if ((info->flags & DF_TEXTREL) == 0)
9776 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
9778 if ((info->flags & DF_TEXTREL) != 0)
9780 if (!add_dynamic_entry (DT_TEXTREL, 0))
9785 #undef add_dynamic_entry
9790 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
9793 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
9795 if (h->plt.plist != NULL
9797 && !h->pointer_equality_needed)
9800 return _bfd_elf_hash_symbol (h);
9803 /* Determine the type of stub needed, if any, for a call. */
9805 static inline enum ppc_stub_type
9806 ppc_type_of_stub (asection *input_sec,
9807 const Elf_Internal_Rela *rel,
9808 struct ppc_link_hash_entry **hash,
9809 struct plt_entry **plt_ent,
9810 bfd_vma destination,
9811 unsigned long local_off)
9813 struct ppc_link_hash_entry *h = *hash;
9815 bfd_vma branch_offset;
9816 bfd_vma max_branch_offset;
9817 enum elf_ppc64_reloc_type r_type;
9821 struct plt_entry *ent;
9822 struct ppc_link_hash_entry *fdh = h;
9824 && h->oh->is_func_descriptor)
9826 fdh = ppc_follow_link (h->oh);
9830 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9831 if (ent->addend == rel->r_addend
9832 && ent->plt.offset != (bfd_vma) -1)
9835 return ppc_stub_plt_call;
9838 /* Here, we know we don't have a plt entry. If we don't have a
9839 either a defined function descriptor or a defined entry symbol
9840 in a regular object file, then it is pointless trying to make
9841 any other type of stub. */
9842 if (!is_static_defined (&fdh->elf)
9843 && !is_static_defined (&h->elf))
9844 return ppc_stub_none;
9846 else if (elf_local_got_ents (input_sec->owner) != NULL)
9848 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9849 struct plt_entry **local_plt = (struct plt_entry **)
9850 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9851 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9853 if (local_plt[r_symndx] != NULL)
9855 struct plt_entry *ent;
9857 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9858 if (ent->addend == rel->r_addend
9859 && ent->plt.offset != (bfd_vma) -1)
9862 return ppc_stub_plt_call;
9867 /* Determine where the call point is. */
9868 location = (input_sec->output_offset
9869 + input_sec->output_section->vma
9872 branch_offset = destination - location;
9873 r_type = ELF64_R_TYPE (rel->r_info);
9875 /* Determine if a long branch stub is needed. */
9876 max_branch_offset = 1 << 25;
9877 if (r_type == R_PPC64_REL14
9878 || r_type == R_PPC64_REL14_BRTAKEN
9879 || r_type == R_PPC64_REL14_BRNTAKEN)
9880 max_branch_offset = 1 << 15;
9882 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
9883 /* We need a stub. Figure out whether a long_branch or plt_branch
9885 return ppc_stub_long_branch;
9887 return ppc_stub_none;
9890 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
9891 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
9896 . lis %r12,xxx-1b@highest
9897 . ori %r12,%r12,xxx-1b@higher
9899 . oris %r12,%r12,xxx-1b@high
9900 . ori %r12,%r12,xxx-1b@l
9901 . add/ldx %r12,%r11,%r12 */
9904 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
9906 bfd_put_32 (abfd, MFLR_R12, p);
9908 bfd_put_32 (abfd, BCL_20_31, p);
9910 bfd_put_32 (abfd, MFLR_R11, p);
9912 bfd_put_32 (abfd, MTLR_R12, p);
9914 if (off + 0x8000 < 0x10000)
9917 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
9919 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
9922 else if (off + 0x80008000ULL < 0x100000000ULL)
9924 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
9927 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
9929 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
9934 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9936 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
9941 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
9943 if (((off >> 32) & 0xffff) != 0)
9945 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
9949 if (((off >> 32) & 0xffffffffULL) != 0)
9951 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
9954 if (PPC_HI (off) != 0)
9956 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
9959 if (PPC_LO (off) != 0)
9961 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
9965 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
9967 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
9974 size_offset (bfd_vma off)
9977 if (off + 0x8000 < 0x10000)
9979 else if (off + 0x80008000ULL < 0x100000000ULL)
9983 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9988 if (((off >> 32) & 0xffff) != 0)
9991 if (((off >> 32) & 0xffffffffULL) != 0)
9993 if (PPC_HI (off) != 0)
9995 if (PPC_LO (off) != 0)
10002 static unsigned int
10003 num_relocs_for_offset (bfd_vma off)
10005 unsigned int num_rel;
10006 if (off + 0x8000 < 0x10000)
10008 else if (off + 0x80008000ULL < 0x100000000ULL)
10013 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10014 && ((off >> 32) & 0xffff) != 0)
10016 if (PPC_HI (off) != 0)
10018 if (PPC_LO (off) != 0)
10024 static Elf_Internal_Rela *
10025 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10026 bfd_vma roff, bfd_vma targ, bfd_vma off)
10028 bfd_vma relative_targ = targ - (roff - 8);
10029 if (bfd_big_endian (info->output_bfd))
10031 r->r_offset = roff;
10032 r->r_addend = relative_targ + roff;
10033 if (off + 0x8000 < 0x10000)
10034 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10035 else if (off + 0x80008000ULL < 0x100000000ULL)
10037 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10040 r->r_offset = roff;
10041 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10042 r->r_addend = relative_targ + roff;
10046 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10047 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10050 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10051 if (((off >> 32) & 0xffff) != 0)
10055 r->r_offset = roff;
10056 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10057 r->r_addend = relative_targ + roff;
10060 if (((off >> 32) & 0xffffffffULL) != 0)
10062 if (PPC_HI (off) != 0)
10066 r->r_offset = roff;
10067 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10068 r->r_addend = relative_targ + roff;
10070 if (PPC_LO (off) != 0)
10074 r->r_offset = roff;
10075 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10076 r->r_addend = relative_targ + roff;
10082 /* Emit .eh_frame opcode to advance pc by DELTA. */
10085 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10089 *eh++ = DW_CFA_advance_loc + delta;
10090 else if (delta < 256)
10092 *eh++ = DW_CFA_advance_loc1;
10095 else if (delta < 65536)
10097 *eh++ = DW_CFA_advance_loc2;
10098 bfd_put_16 (abfd, delta, eh);
10103 *eh++ = DW_CFA_advance_loc4;
10104 bfd_put_32 (abfd, delta, eh);
10110 /* Size of required .eh_frame opcode to advance pc by DELTA. */
10112 static unsigned int
10113 eh_advance_size (unsigned int delta)
10115 if (delta < 64 * 4)
10116 /* DW_CFA_advance_loc+[1..63]. */
10118 if (delta < 256 * 4)
10119 /* DW_CFA_advance_loc1, byte. */
10121 if (delta < 65536 * 4)
10122 /* DW_CFA_advance_loc2, 2 bytes. */
10124 /* DW_CFA_advance_loc4, 4 bytes. */
10128 /* With power7 weakly ordered memory model, it is possible for ld.so
10129 to update a plt entry in one thread and have another thread see a
10130 stale zero toc entry. To avoid this we need some sort of acquire
10131 barrier in the call stub. One solution is to make the load of the
10132 toc word seem to appear to depend on the load of the function entry
10133 word. Another solution is to test for r2 being zero, and branch to
10134 the appropriate glink entry if so.
10136 . fake dep barrier compare
10137 . ld 12,xxx(2) ld 12,xxx(2)
10138 . mtctr 12 mtctr 12
10139 . xor 11,12,12 ld 2,xxx+8(2)
10140 . add 2,2,11 cmpldi 2,0
10141 . ld 2,xxx+8(2) bnectr+
10142 . bctr b <glink_entry>
10144 The solution involving the compare turns out to be faster, so
10145 that's what we use unless the branch won't reach. */
10147 #define ALWAYS_USE_FAKE_DEP 0
10148 #define ALWAYS_EMIT_R2SAVE 0
10150 static inline unsigned int
10151 plt_stub_size (struct ppc_link_hash_table *htab,
10152 struct ppc_stub_hash_entry *stub_entry,
10157 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10159 size = 8 + size_offset (off - 8);
10160 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10166 if (ALWAYS_EMIT_R2SAVE
10167 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10169 if (PPC_HA (off) != 0)
10174 if (htab->params->plt_static_chain)
10176 if (htab->params->plt_thread_safe
10177 && htab->elf.dynamic_sections_created
10178 && stub_entry->h != NULL
10179 && stub_entry->h->elf.dynindx != -1)
10181 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10184 if (stub_entry->h != NULL
10185 && (stub_entry->h == htab->tls_get_addr_fd
10186 || stub_entry->h == htab->tls_get_addr)
10187 && htab->params->tls_get_addr_opt)
10190 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
10196 /* Depending on the sign of plt_stub_align:
10197 If positive, return the padding to align to a 2**plt_stub_align
10199 If negative, if this stub would cross fewer 2**plt_stub_align
10200 boundaries if we align, then return the padding needed to do so. */
10202 static inline unsigned int
10203 plt_stub_pad (struct ppc_link_hash_table *htab,
10204 struct ppc_stub_hash_entry *stub_entry,
10208 unsigned stub_size;
10209 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10211 if (htab->params->plt_stub_align >= 0)
10213 stub_align = 1 << htab->params->plt_stub_align;
10214 if ((stub_off & (stub_align - 1)) != 0)
10215 return stub_align - (stub_off & (stub_align - 1));
10219 stub_align = 1 << -htab->params->plt_stub_align;
10220 stub_size = plt_stub_size (htab, stub_entry, plt_off);
10221 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10222 > ((stub_size - 1) & -stub_align))
10223 return stub_align - (stub_off & (stub_align - 1));
10227 /* Build a .plt call stub. */
10229 static inline bfd_byte *
10230 build_plt_stub (struct ppc_link_hash_table *htab,
10231 struct ppc_stub_hash_entry *stub_entry,
10232 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10234 bfd *obfd = htab->params->stub_bfd;
10235 bfd_boolean plt_load_toc = htab->opd_abi;
10236 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10237 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10238 && htab->elf.dynamic_sections_created
10239 && stub_entry->h != NULL
10240 && stub_entry->h->elf.dynindx != -1);
10241 bfd_boolean use_fake_dep = plt_thread_safe;
10242 bfd_vma cmp_branch_off = 0;
10244 if (!ALWAYS_USE_FAKE_DEP
10247 && !((stub_entry->h == htab->tls_get_addr_fd
10248 || stub_entry->h == htab->tls_get_addr)
10249 && htab->params->tls_get_addr_opt))
10251 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10252 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10253 / PLT_ENTRY_SIZE (htab));
10254 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10257 if (pltindex > 32768)
10258 glinkoff += (pltindex - 32768) * 4;
10260 + htab->glink->output_offset
10261 + htab->glink->output_section->vma);
10262 from = (p - stub_entry->group->stub_sec->contents
10263 + 4 * (ALWAYS_EMIT_R2SAVE
10264 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10265 + 4 * (PPC_HA (offset) != 0)
10266 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10267 != PPC_HA (offset))
10268 + 4 * (plt_static_chain != 0)
10270 + stub_entry->group->stub_sec->output_offset
10271 + stub_entry->group->stub_sec->output_section->vma);
10272 cmp_branch_off = to - from;
10273 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10276 if (PPC_HA (offset) != 0)
10280 if (ALWAYS_EMIT_R2SAVE
10281 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10282 r[0].r_offset += 4;
10283 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10284 r[1].r_offset = r[0].r_offset + 4;
10285 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10286 r[1].r_addend = r[0].r_addend;
10289 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10291 r[2].r_offset = r[1].r_offset + 4;
10292 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10293 r[2].r_addend = r[0].r_addend;
10297 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10298 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10299 r[2].r_addend = r[0].r_addend + 8;
10300 if (plt_static_chain)
10302 r[3].r_offset = r[2].r_offset + 4;
10303 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10304 r[3].r_addend = r[0].r_addend + 16;
10309 if (ALWAYS_EMIT_R2SAVE
10310 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10311 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10314 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10315 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10319 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10320 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10323 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10325 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10328 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10333 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10334 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10336 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10337 if (plt_static_chain)
10338 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10345 if (ALWAYS_EMIT_R2SAVE
10346 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10347 r[0].r_offset += 4;
10348 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10351 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10353 r[1].r_offset = r[0].r_offset + 4;
10354 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10355 r[1].r_addend = r[0].r_addend;
10359 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10360 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10361 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10362 if (plt_static_chain)
10364 r[2].r_offset = r[1].r_offset + 4;
10365 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10366 r[2].r_addend = r[0].r_addend + 8;
10371 if (ALWAYS_EMIT_R2SAVE
10372 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10373 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10374 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10376 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10378 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10381 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10386 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10387 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10389 if (plt_static_chain)
10390 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10391 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10394 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10396 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10397 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
10398 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10401 bfd_put_32 (obfd, BCTR, p), p += 4;
10405 /* Build a special .plt call stub for __tls_get_addr. */
10407 #define LD_R11_0R3 0xe9630000
10408 #define LD_R12_0R3 0xe9830000
10409 #define MR_R0_R3 0x7c601b78
10410 #define CMPDI_R11_0 0x2c2b0000
10411 #define ADD_R3_R12_R13 0x7c6c6a14
10412 #define BEQLR 0x4d820020
10413 #define MR_R3_R0 0x7c030378
10414 #define STD_R11_0R1 0xf9610000
10415 #define BCTRL 0x4e800421
10416 #define LD_R11_0R1 0xe9610000
10417 #define MTLR_R11 0x7d6803a6
10419 static inline bfd_byte *
10420 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10421 struct ppc_stub_hash_entry *stub_entry,
10422 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10424 bfd *obfd = htab->params->stub_bfd;
10427 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10428 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10429 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10430 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10431 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10432 bfd_put_32 (obfd, BEQLR, p), p += 4;
10433 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10435 r[0].r_offset += 7 * 4;
10436 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
10437 return build_plt_stub (htab, stub_entry, p, offset, r);
10439 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10440 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10443 r[0].r_offset += 2 * 4;
10444 p = build_plt_stub (htab, stub_entry, p, offset, r);
10445 bfd_put_32 (obfd, BCTRL, p - 4);
10447 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
10448 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10449 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10450 bfd_put_32 (obfd, BLR, p), p += 4;
10452 if (htab->glink_eh_frame != NULL
10453 && htab->glink_eh_frame->size != 0)
10455 bfd_byte *base, *eh;
10456 unsigned int lr_used, delta;
10458 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10459 eh = base + stub_entry->group->eh_size;
10460 lr_used = stub_entry->stub_offset + (p - 20 - loc);
10461 delta = lr_used - stub_entry->group->lr_restore;
10462 stub_entry->group->lr_restore = lr_used + 16;
10463 eh = eh_advance (htab->elf.dynobj, eh, delta);
10464 *eh++ = DW_CFA_offset_extended_sf;
10466 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10467 *eh++ = DW_CFA_advance_loc + 4;
10468 *eh++ = DW_CFA_restore_extended;
10470 stub_entry->group->eh_size = eh - base;
10475 static Elf_Internal_Rela *
10476 get_relocs (asection *sec, int count)
10478 Elf_Internal_Rela *relocs;
10479 struct bfd_elf_section_data *elfsec_data;
10481 elfsec_data = elf_section_data (sec);
10482 relocs = elfsec_data->relocs;
10483 if (relocs == NULL)
10485 bfd_size_type relsize;
10486 relsize = sec->reloc_count * sizeof (*relocs);
10487 relocs = bfd_alloc (sec->owner, relsize);
10488 if (relocs == NULL)
10490 elfsec_data->relocs = relocs;
10491 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10492 sizeof (Elf_Internal_Shdr));
10493 if (elfsec_data->rela.hdr == NULL)
10495 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10496 * sizeof (Elf64_External_Rela));
10497 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10498 sec->reloc_count = 0;
10500 relocs += sec->reloc_count;
10501 sec->reloc_count += count;
10505 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10506 forms, to the equivalent relocs against the global symbol given by
10510 use_global_in_relocs (struct ppc_link_hash_table *htab,
10511 struct ppc_stub_hash_entry *stub_entry,
10512 Elf_Internal_Rela *r, unsigned int num_rel)
10514 struct elf_link_hash_entry **hashes;
10515 unsigned long symndx;
10516 struct ppc_link_hash_entry *h;
10519 /* Relocs are always against symbols in their own object file. Fake
10520 up global sym hashes for the stub bfd (which has no symbols). */
10521 hashes = elf_sym_hashes (htab->params->stub_bfd);
10522 if (hashes == NULL)
10524 bfd_size_type hsize;
10526 /* When called the first time, stub_globals will contain the
10527 total number of symbols seen during stub sizing. After
10528 allocating, stub_globals is used as an index to fill the
10530 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10531 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10532 if (hashes == NULL)
10534 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10535 htab->stub_globals = 1;
10537 symndx = htab->stub_globals++;
10539 hashes[symndx] = &h->elf;
10540 if (h->oh != NULL && h->oh->is_func)
10541 h = ppc_follow_link (h->oh);
10542 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10543 || h->elf.root.type == bfd_link_hash_defweak);
10544 symval = (h->elf.root.u.def.value
10545 + h->elf.root.u.def.section->output_offset
10546 + h->elf.root.u.def.section->output_section->vma);
10547 while (num_rel-- != 0)
10549 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10550 if (h->elf.root.u.def.section != stub_entry->target_section)
10552 /* H is an opd symbol. The addend must be zero, and the
10553 branch reloc is the only one we can convert. */
10558 r->r_addend -= symval;
10565 get_r2off (struct bfd_link_info *info,
10566 struct ppc_stub_hash_entry *stub_entry)
10568 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10569 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10573 /* Support linking -R objects. Get the toc pointer from the
10576 if (!htab->opd_abi)
10578 asection *opd = stub_entry->h->elf.root.u.def.section;
10579 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10581 if (strcmp (opd->name, ".opd") != 0
10582 || opd->reloc_count != 0)
10584 info->callbacks->einfo
10585 (_("%P: cannot find opd entry toc for `%pT'\n"),
10586 stub_entry->h->elf.root.root.string);
10587 bfd_set_error (bfd_error_bad_value);
10588 return (bfd_vma) -1;
10590 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10591 return (bfd_vma) -1;
10592 r2off = bfd_get_64 (opd->owner, buf);
10593 r2off -= elf_gp (info->output_bfd);
10595 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10600 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10602 struct ppc_stub_hash_entry *stub_entry;
10603 struct ppc_branch_hash_entry *br_entry;
10604 struct bfd_link_info *info;
10605 struct ppc_link_hash_table *htab;
10607 bfd_byte *p, *relp;
10609 Elf_Internal_Rela *r;
10613 /* Massage our args to the form they really have. */
10614 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10617 htab = ppc_hash_table (info);
10621 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
10622 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10624 htab->stub_count[stub_entry->stub_type - 1] += 1;
10625 switch (stub_entry->stub_type)
10627 case ppc_stub_long_branch:
10628 case ppc_stub_long_branch_r2off:
10629 /* Branches are relative. This is where we are going to. */
10630 targ = (stub_entry->target_value
10631 + stub_entry->target_section->output_offset
10632 + stub_entry->target_section->output_section->vma);
10633 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10635 /* And this is where we are coming from. */
10636 off = (stub_entry->stub_offset
10637 + stub_entry->group->stub_sec->output_offset
10638 + stub_entry->group->stub_sec->output_section->vma);
10642 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10644 bfd_vma r2off = get_r2off (info, stub_entry);
10646 if (r2off == (bfd_vma) -1)
10648 htab->stub_error = TRUE;
10651 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10653 if (PPC_HA (r2off) != 0)
10655 bfd_put_32 (htab->params->stub_bfd,
10656 ADDIS_R2_R2 | PPC_HA (r2off), p);
10659 if (PPC_LO (r2off) != 0)
10661 bfd_put_32 (htab->params->stub_bfd,
10662 ADDI_R2_R2 | PPC_LO (r2off), p);
10667 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10670 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10673 (_("long branch stub `%s' offset overflow"),
10674 stub_entry->root.string);
10675 htab->stub_error = TRUE;
10679 if (info->emitrelocations)
10681 r = get_relocs (stub_entry->group->stub_sec, 1);
10684 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
10685 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10686 r->r_addend = targ;
10687 if (stub_entry->h != NULL
10688 && !use_global_in_relocs (htab, stub_entry, r, 1))
10693 case ppc_stub_plt_branch:
10694 case ppc_stub_plt_branch_r2off:
10695 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10696 stub_entry->root.string + 9,
10698 if (br_entry == NULL)
10700 _bfd_error_handler (_("can't find branch stub `%s'"),
10701 stub_entry->root.string);
10702 htab->stub_error = TRUE;
10706 targ = (stub_entry->target_value
10707 + stub_entry->target_section->output_offset
10708 + stub_entry->target_section->output_section->vma);
10709 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10710 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10712 bfd_put_64 (htab->brlt->owner, targ,
10713 htab->brlt->contents + br_entry->offset);
10715 if (br_entry->iter == htab->stub_iteration)
10717 br_entry->iter = 0;
10719 if (htab->relbrlt != NULL)
10721 /* Create a reloc for the branch lookup table entry. */
10722 Elf_Internal_Rela rela;
10725 rela.r_offset = (br_entry->offset
10726 + htab->brlt->output_offset
10727 + htab->brlt->output_section->vma);
10728 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10729 rela.r_addend = targ;
10731 rl = htab->relbrlt->contents;
10732 rl += (htab->relbrlt->reloc_count++
10733 * sizeof (Elf64_External_Rela));
10734 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10736 else if (info->emitrelocations)
10738 r = get_relocs (htab->brlt, 1);
10741 /* brlt, being SEC_LINKER_CREATED does not go through the
10742 normal reloc processing. Symbols and offsets are not
10743 translated from input file to output file form, so
10744 set up the offset per the output file. */
10745 r->r_offset = (br_entry->offset
10746 + htab->brlt->output_offset
10747 + htab->brlt->output_section->vma);
10748 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10749 r->r_addend = targ;
10753 targ = (br_entry->offset
10754 + htab->brlt->output_offset
10755 + htab->brlt->output_section->vma);
10757 off = (elf_gp (info->output_bfd)
10758 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10761 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10763 info->callbacks->einfo
10764 (_("%P: linkage table error against `%pT'\n"),
10765 stub_entry->root.string);
10766 bfd_set_error (bfd_error_bad_value);
10767 htab->stub_error = TRUE;
10771 if (info->emitrelocations)
10773 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
10776 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10777 if (bfd_big_endian (info->output_bfd))
10778 r[0].r_offset += 2;
10779 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10780 r[0].r_offset += 4;
10781 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10782 r[0].r_addend = targ;
10783 if (PPC_HA (off) != 0)
10785 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10786 r[1].r_offset = r[0].r_offset + 4;
10787 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10788 r[1].r_addend = r[0].r_addend;
10793 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10795 if (PPC_HA (off) != 0)
10797 bfd_put_32 (htab->params->stub_bfd,
10798 ADDIS_R12_R2 | PPC_HA (off), p);
10800 bfd_put_32 (htab->params->stub_bfd,
10801 LD_R12_0R12 | PPC_LO (off), p);
10804 bfd_put_32 (htab->params->stub_bfd,
10805 LD_R12_0R2 | PPC_LO (off), p);
10809 bfd_vma r2off = get_r2off (info, stub_entry);
10811 if (r2off == (bfd_vma) -1)
10813 htab->stub_error = TRUE;
10817 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10819 if (PPC_HA (off) != 0)
10821 bfd_put_32 (htab->params->stub_bfd,
10822 ADDIS_R12_R2 | PPC_HA (off), p);
10824 bfd_put_32 (htab->params->stub_bfd,
10825 LD_R12_0R12 | PPC_LO (off), p);
10828 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
10830 if (PPC_HA (r2off) != 0)
10833 bfd_put_32 (htab->params->stub_bfd,
10834 ADDIS_R2_R2 | PPC_HA (r2off), p);
10836 if (PPC_LO (r2off) != 0)
10839 bfd_put_32 (htab->params->stub_bfd,
10840 ADDI_R2_R2 | PPC_LO (r2off), p);
10844 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10846 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10850 case ppc_stub_long_branch_notoc:
10851 case ppc_stub_long_branch_both:
10852 case ppc_stub_plt_branch_notoc:
10853 case ppc_stub_plt_branch_both:
10854 case ppc_stub_plt_call_notoc:
10855 case ppc_stub_plt_call_both:
10857 off = (stub_entry->stub_offset
10858 + stub_entry->group->stub_sec->output_offset
10859 + stub_entry->group->stub_sec->output_section->vma);
10860 if (stub_entry->stub_type == ppc_stub_long_branch_both
10861 || stub_entry->stub_type == ppc_stub_plt_branch_both
10862 || stub_entry->stub_type == ppc_stub_plt_call_both)
10865 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10868 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10870 targ = stub_entry->plt_ent->plt.offset & ~1;
10871 if (targ >= (bfd_vma) -2)
10874 plt = htab->elf.splt;
10875 if (!htab->elf.dynamic_sections_created
10876 || stub_entry->h == NULL
10877 || stub_entry->h->elf.dynindx == -1)
10879 if (stub_entry->symtype == STT_GNU_IFUNC)
10880 plt = htab->elf.iplt;
10882 plt = htab->pltlocal;
10884 targ += plt->output_offset + plt->output_section->vma;
10887 targ = (stub_entry->target_value
10888 + stub_entry->target_section->output_offset
10889 + stub_entry->target_section->output_section->vma);
10894 /* The notoc stubs calculate their target (either a PLT entry or
10895 the global entry point of a function) relative to the PC
10896 returned by the "bcl" two instructions past the start of the
10897 sequence emitted by build_offset. The offset is therefore 8
10898 less than calculated from the start of the sequence. */
10900 p = build_offset (htab->params->stub_bfd, p, off,
10901 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
10902 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
10906 from = (stub_entry->stub_offset
10907 + stub_entry->group->stub_sec->output_offset
10908 + stub_entry->group->stub_sec->output_section->vma
10910 bfd_put_32 (htab->params->stub_bfd,
10911 B_DOT | ((targ - from) & 0x3fffffc), p);
10915 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10917 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10921 if (info->emitrelocations)
10924 num_rel += num_relocs_for_offset (off);
10925 r = get_relocs (stub_entry->group->stub_sec, num_rel);
10928 roff = relp + 16 - stub_entry->group->stub_sec->contents;
10929 r = emit_relocs_for_offset (info, r, roff, targ, off);
10930 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
10931 || stub_entry->stub_type == ppc_stub_long_branch_both)
10934 roff = p - 4 - stub_entry->group->stub_sec->contents;
10935 r->r_offset = roff;
10936 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10937 r->r_addend = targ;
10938 if (stub_entry->h != NULL
10939 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
10944 if (htab->glink_eh_frame != NULL
10945 && htab->glink_eh_frame->size != 0)
10947 bfd_byte *base, *eh;
10948 unsigned int lr_used, delta;
10950 base = (htab->glink_eh_frame->contents
10951 + stub_entry->group->eh_base + 17);
10952 eh = base + stub_entry->group->eh_size;
10953 lr_used = stub_entry->stub_offset + 8;
10954 if (stub_entry->stub_type == ppc_stub_long_branch_both
10955 || stub_entry->stub_type == ppc_stub_plt_branch_both
10956 || stub_entry->stub_type == ppc_stub_plt_call_both)
10958 delta = lr_used - stub_entry->group->lr_restore;
10959 stub_entry->group->lr_restore = lr_used + 8;
10960 eh = eh_advance (htab->elf.dynobj, eh, delta);
10961 *eh++ = DW_CFA_register;
10964 *eh++ = DW_CFA_advance_loc + 2;
10965 *eh++ = DW_CFA_restore_extended;
10967 stub_entry->group->eh_size = eh - base;
10971 case ppc_stub_plt_call:
10972 case ppc_stub_plt_call_r2save:
10973 if (stub_entry->h != NULL
10974 && stub_entry->h->is_func_descriptor
10975 && stub_entry->h->oh != NULL)
10977 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10979 /* If the old-ABI "dot-symbol" is undefined make it weak so
10980 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
10981 if (fh->elf.root.type == bfd_link_hash_undefined
10982 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
10983 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
10984 fh->elf.root.type = bfd_link_hash_undefweak;
10987 /* Now build the stub. */
10988 targ = stub_entry->plt_ent->plt.offset & ~1;
10989 if (targ >= (bfd_vma) -2)
10992 plt = htab->elf.splt;
10993 if (!htab->elf.dynamic_sections_created
10994 || stub_entry->h == NULL
10995 || stub_entry->h->elf.dynindx == -1)
10997 if (stub_entry->symtype == STT_GNU_IFUNC)
10998 plt = htab->elf.iplt;
11000 plt = htab->pltlocal;
11002 targ += plt->output_offset + plt->output_section->vma;
11004 off = (elf_gp (info->output_bfd)
11005 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11008 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11010 info->callbacks->einfo
11011 /* xgettext:c-format */
11012 (_("%P: linkage table error against `%pT'\n"),
11013 stub_entry->h != NULL
11014 ? stub_entry->h->elf.root.root.string
11016 bfd_set_error (bfd_error_bad_value);
11017 htab->stub_error = TRUE;
11022 if (info->emitrelocations)
11024 r = get_relocs (stub_entry->group->stub_sec,
11025 ((PPC_HA (off) != 0)
11027 ? 2 + (htab->params->plt_static_chain
11028 && PPC_HA (off + 16) == PPC_HA (off))
11032 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11033 if (bfd_big_endian (info->output_bfd))
11034 r[0].r_offset += 2;
11035 r[0].r_addend = targ;
11037 if (stub_entry->h != NULL
11038 && (stub_entry->h == htab->tls_get_addr_fd
11039 || stub_entry->h == htab->tls_get_addr)
11040 && htab->params->tls_get_addr_opt)
11041 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11043 p = build_plt_stub (htab, stub_entry, loc, off, r);
11046 case ppc_stub_save_res:
11054 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
11056 if (htab->params->emit_stub_syms)
11058 struct elf_link_hash_entry *h;
11061 const char *const stub_str[] = { "long_branch",
11074 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11075 len2 = strlen (stub_entry->root.string);
11076 name = bfd_malloc (len1 + len2 + 2);
11079 memcpy (name, stub_entry->root.string, 9);
11080 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11081 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11082 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11085 if (h->root.type == bfd_link_hash_new)
11087 h->root.type = bfd_link_hash_defined;
11088 h->root.u.def.section = stub_entry->group->stub_sec;
11089 h->root.u.def.value = stub_entry->stub_offset;
11090 h->ref_regular = 1;
11091 h->def_regular = 1;
11092 h->ref_regular_nonweak = 1;
11093 h->forced_local = 1;
11095 h->root.linker_def = 1;
11102 /* As above, but don't actually build the stub. Just bump offset so
11103 we know stub section sizes, and select plt_branch stubs where
11104 long_branch stubs won't do. */
11107 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11109 struct ppc_stub_hash_entry *stub_entry;
11110 struct bfd_link_info *info;
11111 struct ppc_link_hash_table *htab;
11113 bfd_vma targ, off, r2off;
11114 unsigned int size, extra, lr_used, delta;
11116 /* Massage our args to the form they really have. */
11117 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11120 htab = ppc_hash_table (info);
11124 /* Make a note of the offset within the stubs for this entry. */
11125 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11127 if (stub_entry->h != NULL
11128 && stub_entry->h->save_res
11129 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11130 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11132 /* Don't make stubs to out-of-line register save/restore
11133 functions. Instead, emit copies of the functions. */
11134 stub_entry->group->needs_save_res = 1;
11135 stub_entry->stub_type = ppc_stub_save_res;
11139 switch (stub_entry->stub_type)
11141 case ppc_stub_plt_branch:
11142 case ppc_stub_plt_branch_r2off:
11143 /* Reset the stub type from the plt branch variant in case we now
11144 can reach with a shorter stub. */
11145 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11146 /* Fall through. */
11147 case ppc_stub_long_branch:
11148 case ppc_stub_long_branch_r2off:
11149 targ = (stub_entry->target_value
11150 + stub_entry->target_section->output_offset
11151 + stub_entry->target_section->output_section->vma);
11152 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11153 off = (stub_entry->stub_offset
11154 + stub_entry->group->stub_sec->output_offset
11155 + stub_entry->group->stub_sec->output_section->vma);
11159 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11161 r2off = get_r2off (info, stub_entry);
11162 if (r2off == (bfd_vma) -1)
11164 htab->stub_error = TRUE;
11168 if (PPC_HA (r2off) != 0)
11170 if (PPC_LO (r2off) != 0)
11176 /* If the branch offset is too big, use a ppc_stub_plt_branch.
11177 Do the same for -R objects without function descriptors. */
11178 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
11180 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
11181 || off + (1 << 25) >= (bfd_vma) (1 << 26))
11183 struct ppc_branch_hash_entry *br_entry;
11185 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11186 stub_entry->root.string + 9,
11188 if (br_entry == NULL)
11190 _bfd_error_handler (_("can't build branch stub `%s'"),
11191 stub_entry->root.string);
11192 htab->stub_error = TRUE;
11196 if (br_entry->iter != htab->stub_iteration)
11198 br_entry->iter = htab->stub_iteration;
11199 br_entry->offset = htab->brlt->size;
11200 htab->brlt->size += 8;
11202 if (htab->relbrlt != NULL)
11203 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11204 else if (info->emitrelocations)
11206 htab->brlt->reloc_count += 1;
11207 htab->brlt->flags |= SEC_RELOC;
11211 targ = (br_entry->offset
11212 + htab->brlt->output_offset
11213 + htab->brlt->output_section->vma);
11214 off = (elf_gp (info->output_bfd)
11215 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11218 if (info->emitrelocations)
11220 stub_entry->group->stub_sec->reloc_count
11221 += 1 + (PPC_HA (off) != 0);
11222 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11225 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11226 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11229 if (PPC_HA (off) != 0)
11235 if (PPC_HA (off) != 0)
11238 if (PPC_HA (r2off) != 0)
11240 if (PPC_LO (r2off) != 0)
11244 else if (info->emitrelocations)
11246 stub_entry->group->stub_sec->reloc_count += 1;
11247 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11251 case ppc_stub_plt_branch_notoc:
11252 case ppc_stub_plt_branch_both:
11253 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11254 /* Fall through. */
11255 case ppc_stub_long_branch_notoc:
11256 case ppc_stub_long_branch_both:
11257 off = (stub_entry->stub_offset
11258 + stub_entry->group->stub_sec->output_offset
11259 + stub_entry->group->stub_sec->output_section->vma);
11261 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11264 targ = (stub_entry->target_value
11265 + stub_entry->target_section->output_offset
11266 + stub_entry->target_section->output_section->vma);
11269 if (info->emitrelocations)
11271 stub_entry->group->stub_sec->reloc_count
11272 += num_relocs_for_offset (off);
11273 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11276 extra = size_offset (off - 8);
11277 /* Include branch insn plus those in the offset sequence. */
11279 /* The branch insn is at the end, or "extra" bytes along. So
11280 its offset will be "extra" bytes less that that already
11284 /* After the bcl, lr has been modified so we need to emit
11285 .eh_frame info saying the return address is in r12. */
11286 lr_used = stub_entry->stub_offset + 8;
11287 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11289 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11290 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11291 DW_CFA_restore_extended 65. */
11292 delta = lr_used - stub_entry->group->lr_restore;
11293 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11294 stub_entry->group->lr_restore = lr_used + 8;
11296 /* If the branch can't reach, use a plt_branch. */
11297 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11299 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11300 - ppc_stub_long_branch_notoc);
11303 else if (info->emitrelocations)
11304 stub_entry->group->stub_sec->reloc_count +=1;
11307 case ppc_stub_plt_call_notoc:
11308 case ppc_stub_plt_call_both:
11309 off = (stub_entry->stub_offset
11310 + stub_entry->group->stub_sec->output_offset
11311 + stub_entry->group->stub_sec->output_section->vma);
11312 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11314 targ = stub_entry->plt_ent->plt.offset & ~1;
11315 if (targ >= (bfd_vma) -2)
11318 plt = htab->elf.splt;
11319 if (!htab->elf.dynamic_sections_created
11320 || stub_entry->h == NULL
11321 || stub_entry->h->elf.dynindx == -1)
11323 if (stub_entry->symtype == STT_GNU_IFUNC)
11324 plt = htab->elf.iplt;
11326 plt = htab->pltlocal;
11328 targ += plt->output_offset + plt->output_section->vma;
11331 if (htab->params->plt_stub_align != 0)
11333 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11335 stub_entry->group->stub_sec->size += pad;
11336 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11340 if (info->emitrelocations)
11342 stub_entry->group->stub_sec->reloc_count
11343 += num_relocs_for_offset (off - 8);
11344 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11347 size = plt_stub_size (htab, stub_entry, off);
11349 /* After the bcl, lr has been modified so we need to emit
11350 .eh_frame info saying the return address is in r12. */
11351 lr_used = stub_entry->stub_offset + 8;
11352 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11354 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11355 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11356 DW_CFA_restore_extended 65. */
11357 delta = lr_used - stub_entry->group->lr_restore;
11358 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11359 stub_entry->group->lr_restore = lr_used + 8;
11362 case ppc_stub_plt_call:
11363 case ppc_stub_plt_call_r2save:
11364 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11365 if (targ >= (bfd_vma) -2)
11367 plt = htab->elf.splt;
11368 if (!htab->elf.dynamic_sections_created
11369 || stub_entry->h == NULL
11370 || stub_entry->h->elf.dynindx == -1)
11372 if (stub_entry->symtype == STT_GNU_IFUNC)
11373 plt = htab->elf.iplt;
11375 plt = htab->pltlocal;
11377 targ += plt->output_offset + plt->output_section->vma;
11379 off = (elf_gp (info->output_bfd)
11380 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11383 if (htab->params->plt_stub_align != 0)
11385 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11387 stub_entry->group->stub_sec->size += pad;
11388 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11391 if (info->emitrelocations)
11393 stub_entry->group->stub_sec->reloc_count
11394 += ((PPC_HA (off) != 0)
11396 ? 2 + (htab->params->plt_static_chain
11397 && PPC_HA (off + 16) == PPC_HA (off))
11399 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11402 size = plt_stub_size (htab, stub_entry, off);
11404 if (stub_entry->h != NULL
11405 && (stub_entry->h == htab->tls_get_addr_fd
11406 || stub_entry->h == htab->tls_get_addr)
11407 && htab->params->tls_get_addr_opt
11408 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11410 /* After the bctrl, lr has been modified so we need to
11411 emit .eh_frame info saying the return address is
11412 on the stack. In fact we put the EH info specifying
11413 that the return address is on the stack *at* the
11414 call rather than after it, because the EH info for a
11415 call needs to be specified by that point.
11416 See libgcc/unwind-dw2.c execute_cfa_program. */
11417 lr_used = stub_entry->stub_offset + size - 20;
11418 /* The eh_frame info will consist of a DW_CFA_advance_loc
11419 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11420 DW_CFA_advance_loc+4, 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 = size - 4;
11432 stub_entry->group->stub_sec->size += size;
11436 /* Set up various things so that we can make a list of input sections
11437 for each output section included in the link. Returns -1 on error,
11438 0 when no stubs will be needed, and 1 on success. */
11441 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11445 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11450 htab->sec_info_arr_size = _bfd_section_id;
11451 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11452 htab->sec_info = bfd_zmalloc (amt);
11453 if (htab->sec_info == NULL)
11456 /* Set toc_off for com, und, abs and ind sections. */
11457 for (id = 0; id < 3; id++)
11458 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11463 /* Set up for first pass at multitoc partitioning. */
11466 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11468 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11470 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11471 htab->toc_bfd = NULL;
11472 htab->toc_first_sec = NULL;
11475 /* The linker repeatedly calls this function for each TOC input section
11476 and linker generated GOT section. Group input bfds such that the toc
11477 within a group is less than 64k in size. */
11480 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11482 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11483 bfd_vma addr, off, limit;
11488 if (!htab->second_toc_pass)
11490 /* Keep track of the first .toc or .got section for this input bfd. */
11491 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11495 htab->toc_bfd = isec->owner;
11496 htab->toc_first_sec = isec;
11499 addr = isec->output_offset + isec->output_section->vma;
11500 off = addr - htab->toc_curr;
11501 limit = 0x80008000;
11502 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11504 if (off + isec->size > limit)
11506 addr = (htab->toc_first_sec->output_offset
11507 + htab->toc_first_sec->output_section->vma);
11508 htab->toc_curr = addr;
11509 htab->toc_curr &= -TOC_BASE_ALIGN;
11512 /* toc_curr is the base address of this toc group. Set elf_gp
11513 for the input section to be the offset relative to the
11514 output toc base plus 0x8000. Making the input elf_gp an
11515 offset allows us to move the toc as a whole without
11516 recalculating input elf_gp. */
11517 off = htab->toc_curr - elf_gp (info->output_bfd);
11518 off += TOC_BASE_OFF;
11520 /* Die if someone uses a linker script that doesn't keep input
11521 file .toc and .got together. */
11523 && elf_gp (isec->owner) != 0
11524 && elf_gp (isec->owner) != off)
11527 elf_gp (isec->owner) = off;
11531 /* During the second pass toc_first_sec points to the start of
11532 a toc group, and toc_curr is used to track the old elf_gp.
11533 We use toc_bfd to ensure we only look at each bfd once. */
11534 if (htab->toc_bfd == isec->owner)
11536 htab->toc_bfd = isec->owner;
11538 if (htab->toc_first_sec == NULL
11539 || htab->toc_curr != elf_gp (isec->owner))
11541 htab->toc_curr = elf_gp (isec->owner);
11542 htab->toc_first_sec = isec;
11544 addr = (htab->toc_first_sec->output_offset
11545 + htab->toc_first_sec->output_section->vma);
11546 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
11547 elf_gp (isec->owner) = off;
11552 /* Called via elf_link_hash_traverse to merge GOT entries for global
11556 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11558 if (h->root.type == bfd_link_hash_indirect)
11561 merge_got_entries (&h->got.glist);
11566 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11570 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11572 struct got_entry *gent;
11574 if (h->root.type == bfd_link_hash_indirect)
11577 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11578 if (!gent->is_indirect)
11579 allocate_got (h, (struct bfd_link_info *) inf, gent);
11583 /* Called on the first multitoc pass after the last call to
11584 ppc64_elf_next_toc_section. This function removes duplicate GOT
11588 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11590 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11591 struct bfd *ibfd, *ibfd2;
11592 bfd_boolean done_something;
11594 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11596 if (!htab->do_multi_toc)
11599 /* Merge global sym got entries within a toc group. */
11600 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11602 /* And tlsld_got. */
11603 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11605 struct got_entry *ent, *ent2;
11607 if (!is_ppc64_elf (ibfd))
11610 ent = ppc64_tlsld_got (ibfd);
11611 if (!ent->is_indirect
11612 && ent->got.offset != (bfd_vma) -1)
11614 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11616 if (!is_ppc64_elf (ibfd2))
11619 ent2 = ppc64_tlsld_got (ibfd2);
11620 if (!ent2->is_indirect
11621 && ent2->got.offset != (bfd_vma) -1
11622 && elf_gp (ibfd2) == elf_gp (ibfd))
11624 ent2->is_indirect = TRUE;
11625 ent2->got.ent = ent;
11631 /* Zap sizes of got sections. */
11632 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11633 htab->elf.irelplt->size -= htab->got_reli_size;
11634 htab->got_reli_size = 0;
11636 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11638 asection *got, *relgot;
11640 if (!is_ppc64_elf (ibfd))
11643 got = ppc64_elf_tdata (ibfd)->got;
11646 got->rawsize = got->size;
11648 relgot = ppc64_elf_tdata (ibfd)->relgot;
11649 relgot->rawsize = relgot->size;
11654 /* Now reallocate the got, local syms first. We don't need to
11655 allocate section contents again since we never increase size. */
11656 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11658 struct got_entry **lgot_ents;
11659 struct got_entry **end_lgot_ents;
11660 struct plt_entry **local_plt;
11661 struct plt_entry **end_local_plt;
11662 unsigned char *lgot_masks;
11663 bfd_size_type locsymcount;
11664 Elf_Internal_Shdr *symtab_hdr;
11667 if (!is_ppc64_elf (ibfd))
11670 lgot_ents = elf_local_got_ents (ibfd);
11674 symtab_hdr = &elf_symtab_hdr (ibfd);
11675 locsymcount = symtab_hdr->sh_info;
11676 end_lgot_ents = lgot_ents + locsymcount;
11677 local_plt = (struct plt_entry **) end_lgot_ents;
11678 end_local_plt = local_plt + locsymcount;
11679 lgot_masks = (unsigned char *) end_local_plt;
11680 s = ppc64_elf_tdata (ibfd)->got;
11681 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11683 struct got_entry *ent;
11685 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11687 unsigned int ent_size = 8;
11688 unsigned int rel_size = sizeof (Elf64_External_Rela);
11690 ent->got.offset = s->size;
11691 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11696 s->size += ent_size;
11697 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
11699 htab->elf.irelplt->size += rel_size;
11700 htab->got_reli_size += rel_size;
11702 else if (bfd_link_pic (info)
11703 && !((ent->tls_type & TLS_TPREL) != 0
11704 && bfd_link_executable (info)))
11706 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11707 srel->size += rel_size;
11713 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11715 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11717 struct got_entry *ent;
11719 if (!is_ppc64_elf (ibfd))
11722 ent = ppc64_tlsld_got (ibfd);
11723 if (!ent->is_indirect
11724 && ent->got.offset != (bfd_vma) -1)
11726 asection *s = ppc64_elf_tdata (ibfd)->got;
11727 ent->got.offset = s->size;
11729 if (bfd_link_pic (info))
11731 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11732 srel->size += sizeof (Elf64_External_Rela);
11737 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11738 if (!done_something)
11739 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11743 if (!is_ppc64_elf (ibfd))
11746 got = ppc64_elf_tdata (ibfd)->got;
11749 done_something = got->rawsize != got->size;
11750 if (done_something)
11755 if (done_something)
11756 (*htab->params->layout_sections_again) ();
11758 /* Set up for second pass over toc sections to recalculate elf_gp
11759 on input sections. */
11760 htab->toc_bfd = NULL;
11761 htab->toc_first_sec = NULL;
11762 htab->second_toc_pass = TRUE;
11763 return done_something;
11766 /* Called after second pass of multitoc partitioning. */
11769 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11771 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11773 /* After the second pass, toc_curr tracks the TOC offset used
11774 for code sections below in ppc64_elf_next_input_section. */
11775 htab->toc_curr = TOC_BASE_OFF;
11778 /* No toc references were found in ISEC. If the code in ISEC makes no
11779 calls, then there's no need to use toc adjusting stubs when branching
11780 into ISEC. Actually, indirect calls from ISEC are OK as they will
11781 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11782 needed, and 2 if a cyclical call-graph was found but no other reason
11783 for a stub was detected. If called from the top level, a return of
11784 2 means the same as a return of 0. */
11787 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11791 /* Mark this section as checked. */
11792 isec->call_check_done = 1;
11794 /* We know none of our code bearing sections will need toc stubs. */
11795 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11798 if (isec->size == 0)
11801 if (isec->output_section == NULL)
11805 if (isec->reloc_count != 0)
11807 Elf_Internal_Rela *relstart, *rel;
11808 Elf_Internal_Sym *local_syms;
11809 struct ppc_link_hash_table *htab;
11811 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11812 info->keep_memory);
11813 if (relstart == NULL)
11816 /* Look for branches to outside of this section. */
11818 htab = ppc_hash_table (info);
11822 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11824 enum elf_ppc64_reloc_type r_type;
11825 unsigned long r_symndx;
11826 struct elf_link_hash_entry *h;
11827 struct ppc_link_hash_entry *eh;
11828 Elf_Internal_Sym *sym;
11830 struct _opd_sec_data *opd;
11834 r_type = ELF64_R_TYPE (rel->r_info);
11835 if (r_type != R_PPC64_REL24
11836 && r_type != R_PPC64_REL24_NOTOC
11837 && r_type != R_PPC64_REL14
11838 && r_type != R_PPC64_REL14_BRTAKEN
11839 && r_type != R_PPC64_REL14_BRNTAKEN
11840 && r_type != R_PPC64_PLTCALL
11841 && r_type != R_PPC64_PLTCALL_NOTOC)
11844 r_symndx = ELF64_R_SYM (rel->r_info);
11845 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11852 /* Calls to dynamic lib functions go through a plt call stub
11854 eh = (struct ppc_link_hash_entry *) h;
11856 && (eh->elf.plt.plist != NULL
11858 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11864 if (sym_sec == NULL)
11865 /* Ignore other undefined symbols. */
11868 /* Assume branches to other sections not included in the
11869 link need stubs too, to cover -R and absolute syms. */
11870 if (sym_sec->output_section == NULL)
11877 sym_value = sym->st_value;
11880 if (h->root.type != bfd_link_hash_defined
11881 && h->root.type != bfd_link_hash_defweak)
11883 sym_value = h->root.u.def.value;
11885 sym_value += rel->r_addend;
11887 /* If this branch reloc uses an opd sym, find the code section. */
11888 opd = get_opd_info (sym_sec);
11891 if (h == NULL && opd->adjust != NULL)
11895 adjust = opd->adjust[OPD_NDX (sym_value)];
11897 /* Assume deleted functions won't ever be called. */
11899 sym_value += adjust;
11902 dest = opd_entry_value (sym_sec, sym_value,
11903 &sym_sec, NULL, FALSE);
11904 if (dest == (bfd_vma) -1)
11909 + sym_sec->output_offset
11910 + sym_sec->output_section->vma);
11912 /* Ignore branch to self. */
11913 if (sym_sec == isec)
11916 /* If the called function uses the toc, we need a stub. */
11917 if (sym_sec->has_toc_reloc
11918 || sym_sec->makes_toc_func_call)
11924 /* Assume any branch that needs a long branch stub might in fact
11925 need a plt_branch stub. A plt_branch stub uses r2. */
11926 else if (dest - (isec->output_offset
11927 + isec->output_section->vma
11928 + rel->r_offset) + (1 << 25)
11929 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11937 /* If calling back to a section in the process of being
11938 tested, we can't say for sure that no toc adjusting stubs
11939 are needed, so don't return zero. */
11940 else if (sym_sec->call_check_in_progress)
11943 /* Branches to another section that itself doesn't have any TOC
11944 references are OK. Recursively call ourselves to check. */
11945 else if (!sym_sec->call_check_done)
11949 /* Mark current section as indeterminate, so that other
11950 sections that call back to current won't be marked as
11952 isec->call_check_in_progress = 1;
11953 recur = toc_adjusting_stub_needed (info, sym_sec);
11954 isec->call_check_in_progress = 0;
11965 if (local_syms != NULL
11966 && (elf_symtab_hdr (isec->owner).contents
11967 != (unsigned char *) local_syms))
11969 if (elf_section_data (isec)->relocs != relstart)
11974 && isec->map_head.s != NULL
11975 && (strcmp (isec->output_section->name, ".init") == 0
11976 || strcmp (isec->output_section->name, ".fini") == 0))
11978 if (isec->map_head.s->has_toc_reloc
11979 || isec->map_head.s->makes_toc_func_call)
11981 else if (!isec->map_head.s->call_check_done)
11984 isec->call_check_in_progress = 1;
11985 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11986 isec->call_check_in_progress = 0;
11993 isec->makes_toc_func_call = 1;
11998 /* The linker repeatedly calls this function for each input section,
11999 in the order that input sections are linked into output sections.
12000 Build lists of input sections to determine groupings between which
12001 we may insert linker stubs. */
12004 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12006 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12011 if ((isec->output_section->flags & SEC_CODE) != 0
12012 && isec->output_section->id < htab->sec_info_arr_size)
12014 /* This happens to make the list in reverse order,
12015 which is what we want. */
12016 htab->sec_info[isec->id].u.list
12017 = htab->sec_info[isec->output_section->id].u.list;
12018 htab->sec_info[isec->output_section->id].u.list = isec;
12021 if (htab->multi_toc_needed)
12023 /* Analyse sections that aren't already flagged as needing a
12024 valid toc pointer. Exclude .fixup for the linux kernel.
12025 .fixup contains branches, but only back to the function that
12026 hit an exception. */
12027 if (!(isec->has_toc_reloc
12028 || (isec->flags & SEC_CODE) == 0
12029 || strcmp (isec->name, ".fixup") == 0
12030 || isec->call_check_done))
12032 if (toc_adjusting_stub_needed (info, isec) < 0)
12035 /* Make all sections use the TOC assigned for this object file.
12036 This will be wrong for pasted sections; We fix that in
12037 check_pasted_section(). */
12038 if (elf_gp (isec->owner) != 0)
12039 htab->toc_curr = elf_gp (isec->owner);
12042 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12046 /* Check that all .init and .fini sections use the same toc, if they
12047 have toc relocs. */
12050 check_pasted_section (struct bfd_link_info *info, const char *name)
12052 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12056 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12057 bfd_vma toc_off = 0;
12060 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12061 if (i->has_toc_reloc)
12064 toc_off = htab->sec_info[i->id].toc_off;
12065 else if (toc_off != htab->sec_info[i->id].toc_off)
12070 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12071 if (i->makes_toc_func_call)
12073 toc_off = htab->sec_info[i->id].toc_off;
12077 /* Make sure the whole pasted function uses the same toc offset. */
12079 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12080 htab->sec_info[i->id].toc_off = toc_off;
12086 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12088 return (check_pasted_section (info, ".init")
12089 & check_pasted_section (info, ".fini"));
12092 /* See whether we can group stub sections together. Grouping stub
12093 sections may result in fewer stubs. More importantly, we need to
12094 put all .init* and .fini* stubs at the beginning of the .init or
12095 .fini output sections respectively, because glibc splits the
12096 _init and _fini functions into multiple parts. Putting a stub in
12097 the middle of a function is not a good idea. */
12100 group_sections (struct bfd_link_info *info,
12101 bfd_size_type stub_group_size,
12102 bfd_boolean stubs_always_before_branch)
12104 struct ppc_link_hash_table *htab;
12106 bfd_boolean suppress_size_errors;
12108 htab = ppc_hash_table (info);
12112 suppress_size_errors = FALSE;
12113 if (stub_group_size == 1)
12115 /* Default values. */
12116 if (stubs_always_before_branch)
12117 stub_group_size = 0x1e00000;
12119 stub_group_size = 0x1c00000;
12120 suppress_size_errors = TRUE;
12123 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12127 if (osec->id >= htab->sec_info_arr_size)
12130 tail = htab->sec_info[osec->id].u.list;
12131 while (tail != NULL)
12135 bfd_size_type total;
12136 bfd_boolean big_sec;
12138 struct map_stub *group;
12139 bfd_size_type group_size;
12142 total = tail->size;
12143 group_size = (ppc64_elf_section_data (tail) != NULL
12144 && ppc64_elf_section_data (tail)->has_14bit_branch
12145 ? stub_group_size >> 10 : stub_group_size);
12147 big_sec = total > group_size;
12148 if (big_sec && !suppress_size_errors)
12149 /* xgettext:c-format */
12150 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12151 tail->owner, tail);
12152 curr_toc = htab->sec_info[tail->id].toc_off;
12154 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12155 && ((total += curr->output_offset - prev->output_offset)
12156 < (ppc64_elf_section_data (prev) != NULL
12157 && ppc64_elf_section_data (prev)->has_14bit_branch
12158 ? (group_size = stub_group_size >> 10) : group_size))
12159 && htab->sec_info[prev->id].toc_off == curr_toc)
12162 /* OK, the size from the start of CURR to the end is less
12163 than group_size and thus can be handled by one stub
12164 section. (or the tail section is itself larger than
12165 group_size, in which case we may be toast.) We should
12166 really be keeping track of the total size of stubs added
12167 here, as stubs contribute to the final output section
12168 size. That's a little tricky, and this way will only
12169 break if stubs added make the total size more than 2^25,
12170 ie. for the default stub_group_size, if stubs total more
12171 than 2097152 bytes, or nearly 75000 plt call stubs. */
12172 group = bfd_alloc (curr->owner, sizeof (*group));
12175 group->link_sec = curr;
12176 group->stub_sec = NULL;
12177 group->needs_save_res = 0;
12178 group->lr_restore = 0;
12179 group->eh_size = 0;
12180 group->eh_base = 0;
12181 group->next = htab->group;
12182 htab->group = group;
12185 prev = htab->sec_info[tail->id].u.list;
12186 /* Set up this stub group. */
12187 htab->sec_info[tail->id].u.group = group;
12189 while (tail != curr && (tail = prev) != NULL);
12191 /* But wait, there's more! Input sections up to group_size
12192 bytes before the stub section can be handled by it too.
12193 Don't do this if we have a really large section after the
12194 stubs, as adding more stubs increases the chance that
12195 branches may not reach into the stub section. */
12196 if (!stubs_always_before_branch && !big_sec)
12199 while (prev != NULL
12200 && ((total += tail->output_offset - prev->output_offset)
12201 < (ppc64_elf_section_data (prev) != NULL
12202 && ppc64_elf_section_data (prev)->has_14bit_branch
12203 ? (group_size = stub_group_size >> 10)
12205 && htab->sec_info[prev->id].toc_off == curr_toc)
12208 prev = htab->sec_info[tail->id].u.list;
12209 htab->sec_info[tail->id].u.group = group;
12218 static const unsigned char glink_eh_frame_cie[] =
12220 0, 0, 0, 16, /* length. */
12221 0, 0, 0, 0, /* id. */
12222 1, /* CIE version. */
12223 'z', 'R', 0, /* Augmentation string. */
12224 4, /* Code alignment. */
12225 0x78, /* Data alignment. */
12227 1, /* Augmentation size. */
12228 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12229 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
12232 /* Stripping output sections is normally done before dynamic section
12233 symbols have been allocated. This function is called later, and
12234 handles cases like htab->brlt which is mapped to its own output
12238 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12240 if (isec->size == 0
12241 && isec->output_section->size == 0
12242 && !(isec->output_section->flags & SEC_KEEP)
12243 && !bfd_section_removed_from_list (info->output_bfd,
12244 isec->output_section)
12245 && elf_section_data (isec->output_section)->dynindx == 0)
12247 isec->output_section->flags |= SEC_EXCLUDE;
12248 bfd_section_list_remove (info->output_bfd, isec->output_section);
12249 info->output_bfd->section_count--;
12253 /* Determine and set the size of the stub section for a final link.
12255 The basic idea here is to examine all the relocations looking for
12256 PC-relative calls to a target that is unreachable with a "bl"
12260 ppc64_elf_size_stubs (struct bfd_link_info *info)
12262 bfd_size_type stub_group_size;
12263 bfd_boolean stubs_always_before_branch;
12264 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12269 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12270 htab->params->plt_thread_safe = 1;
12271 if (!htab->opd_abi)
12272 htab->params->plt_thread_safe = 0;
12273 else if (htab->params->plt_thread_safe == -1)
12275 static const char *const thread_starter[] =
12279 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12281 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12282 "mq_notify", "create_timer",
12287 "GOMP_parallel_start",
12288 "GOMP_parallel_loop_static",
12289 "GOMP_parallel_loop_static_start",
12290 "GOMP_parallel_loop_dynamic",
12291 "GOMP_parallel_loop_dynamic_start",
12292 "GOMP_parallel_loop_guided",
12293 "GOMP_parallel_loop_guided_start",
12294 "GOMP_parallel_loop_runtime",
12295 "GOMP_parallel_loop_runtime_start",
12296 "GOMP_parallel_sections",
12297 "GOMP_parallel_sections_start",
12303 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12305 struct elf_link_hash_entry *h;
12306 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12307 FALSE, FALSE, TRUE);
12308 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12309 if (htab->params->plt_thread_safe)
12313 stubs_always_before_branch = htab->params->group_size < 0;
12314 if (htab->params->group_size < 0)
12315 stub_group_size = -htab->params->group_size;
12317 stub_group_size = htab->params->group_size;
12319 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12322 #define STUB_SHRINK_ITER 20
12323 /* Loop until no stubs added. After iteration 20 of this loop we may
12324 exit on a stub section shrinking. This is to break out of a
12325 pathological case where adding stubs on one iteration decreases
12326 section gaps (perhaps due to alignment), which then requires
12327 fewer or smaller stubs on the next iteration. */
12332 unsigned int bfd_indx;
12333 struct map_stub *group;
12335 htab->stub_iteration += 1;
12337 for (input_bfd = info->input_bfds, bfd_indx = 0;
12339 input_bfd = input_bfd->link.next, bfd_indx++)
12341 Elf_Internal_Shdr *symtab_hdr;
12343 Elf_Internal_Sym *local_syms = NULL;
12345 if (!is_ppc64_elf (input_bfd))
12348 /* We'll need the symbol table in a second. */
12349 symtab_hdr = &elf_symtab_hdr (input_bfd);
12350 if (symtab_hdr->sh_info == 0)
12353 /* Walk over each section attached to the input bfd. */
12354 for (section = input_bfd->sections;
12356 section = section->next)
12358 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12360 /* If there aren't any relocs, then there's nothing more
12362 if ((section->flags & SEC_RELOC) == 0
12363 || (section->flags & SEC_ALLOC) == 0
12364 || (section->flags & SEC_LOAD) == 0
12365 || (section->flags & SEC_CODE) == 0
12366 || section->reloc_count == 0)
12369 /* If this section is a link-once section that will be
12370 discarded, then don't create any stubs. */
12371 if (section->output_section == NULL
12372 || section->output_section->owner != info->output_bfd)
12375 /* Get the relocs. */
12377 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12378 info->keep_memory);
12379 if (internal_relocs == NULL)
12380 goto error_ret_free_local;
12382 /* Now examine each relocation. */
12383 irela = internal_relocs;
12384 irelaend = irela + section->reloc_count;
12385 for (; irela < irelaend; irela++)
12387 enum elf_ppc64_reloc_type r_type;
12388 unsigned int r_indx;
12389 enum ppc_stub_type stub_type;
12390 struct ppc_stub_hash_entry *stub_entry;
12391 asection *sym_sec, *code_sec;
12392 bfd_vma sym_value, code_value;
12393 bfd_vma destination;
12394 unsigned long local_off;
12395 bfd_boolean ok_dest;
12396 struct ppc_link_hash_entry *hash;
12397 struct ppc_link_hash_entry *fdh;
12398 struct elf_link_hash_entry *h;
12399 Elf_Internal_Sym *sym;
12401 const asection *id_sec;
12402 struct _opd_sec_data *opd;
12403 struct plt_entry *plt_ent;
12405 r_type = ELF64_R_TYPE (irela->r_info);
12406 r_indx = ELF64_R_SYM (irela->r_info);
12408 if (r_type >= R_PPC64_max)
12410 bfd_set_error (bfd_error_bad_value);
12411 goto error_ret_free_internal;
12414 /* Only look for stubs on branch instructions. */
12415 if (r_type != R_PPC64_REL24
12416 && r_type != R_PPC64_REL24_NOTOC
12417 && r_type != R_PPC64_REL14
12418 && r_type != R_PPC64_REL14_BRTAKEN
12419 && r_type != R_PPC64_REL14_BRNTAKEN)
12422 /* Now determine the call target, its name, value,
12424 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12425 r_indx, input_bfd))
12426 goto error_ret_free_internal;
12427 hash = (struct ppc_link_hash_entry *) h;
12434 sym_value = sym->st_value;
12435 if (sym_sec != NULL
12436 && sym_sec->output_section != NULL)
12439 else if (hash->elf.root.type == bfd_link_hash_defined
12440 || hash->elf.root.type == bfd_link_hash_defweak)
12442 sym_value = hash->elf.root.u.def.value;
12443 if (sym_sec->output_section != NULL)
12446 else if (hash->elf.root.type == bfd_link_hash_undefweak
12447 || hash->elf.root.type == bfd_link_hash_undefined)
12449 /* Recognise an old ABI func code entry sym, and
12450 use the func descriptor sym instead if it is
12452 if (hash->elf.root.root.string[0] == '.'
12453 && hash->oh != NULL)
12455 fdh = ppc_follow_link (hash->oh);
12456 if (fdh->elf.root.type == bfd_link_hash_defined
12457 || fdh->elf.root.type == bfd_link_hash_defweak)
12459 sym_sec = fdh->elf.root.u.def.section;
12460 sym_value = fdh->elf.root.u.def.value;
12461 if (sym_sec->output_section != NULL)
12470 bfd_set_error (bfd_error_bad_value);
12471 goto error_ret_free_internal;
12478 sym_value += irela->r_addend;
12479 destination = (sym_value
12480 + sym_sec->output_offset
12481 + sym_sec->output_section->vma);
12482 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12487 code_sec = sym_sec;
12488 code_value = sym_value;
12489 opd = get_opd_info (sym_sec);
12494 if (hash == NULL && opd->adjust != NULL)
12496 long adjust = opd->adjust[OPD_NDX (sym_value)];
12499 code_value += adjust;
12500 sym_value += adjust;
12502 dest = opd_entry_value (sym_sec, sym_value,
12503 &code_sec, &code_value, FALSE);
12504 if (dest != (bfd_vma) -1)
12506 destination = dest;
12509 /* Fixup old ABI sym to point at code
12511 hash->elf.root.type = bfd_link_hash_defweak;
12512 hash->elf.root.u.def.section = code_sec;
12513 hash->elf.root.u.def.value = code_value;
12518 /* Determine what (if any) linker stub is needed. */
12520 stub_type = ppc_type_of_stub (section, irela, &hash,
12521 &plt_ent, destination,
12524 if (r_type == R_PPC64_REL24_NOTOC)
12526 if (stub_type == ppc_stub_plt_call)
12527 stub_type = ppc_stub_plt_call_notoc;
12528 else if (stub_type == ppc_stub_long_branch
12529 || (code_sec != NULL
12530 && code_sec->output_section != NULL
12531 && (((hash ? hash->elf.other : sym->st_other)
12532 & STO_PPC64_LOCAL_MASK)
12533 != 1 << STO_PPC64_LOCAL_BIT)))
12534 stub_type = ppc_stub_long_branch_notoc;
12536 else if (stub_type != ppc_stub_plt_call)
12538 /* Check whether we need a TOC adjusting stub.
12539 Since the linker pastes together pieces from
12540 different object files when creating the
12541 _init and _fini functions, it may be that a
12542 call to what looks like a local sym is in
12543 fact a call needing a TOC adjustment. */
12544 if ((code_sec != NULL
12545 && code_sec->output_section != NULL
12546 && (htab->sec_info[code_sec->id].toc_off
12547 != htab->sec_info[section->id].toc_off)
12548 && (code_sec->has_toc_reloc
12549 || code_sec->makes_toc_func_call))
12550 || (((hash ? hash->elf.other : sym->st_other)
12551 & STO_PPC64_LOCAL_MASK)
12552 == 1 << STO_PPC64_LOCAL_BIT))
12553 stub_type = ppc_stub_long_branch_r2off;
12556 if (stub_type == ppc_stub_none)
12559 /* __tls_get_addr calls might be eliminated. */
12560 if (stub_type != ppc_stub_plt_call
12561 && stub_type != ppc_stub_plt_call_notoc
12563 && (hash == htab->tls_get_addr
12564 || hash == htab->tls_get_addr_fd)
12565 && section->has_tls_reloc
12566 && irela != internal_relocs)
12568 /* Get tls info. */
12569 unsigned char *tls_mask;
12571 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12572 irela - 1, input_bfd))
12573 goto error_ret_free_internal;
12574 if ((*tls_mask & TLS_TLS) != 0)
12578 if (stub_type == ppc_stub_plt_call)
12581 && htab->params->plt_localentry0 != 0
12582 && is_elfv2_localentry0 (&hash->elf))
12583 htab->has_plt_localentry0 = 1;
12584 else if (irela + 1 < irelaend
12585 && irela[1].r_offset == irela->r_offset + 4
12586 && (ELF64_R_TYPE (irela[1].r_info)
12587 == R_PPC64_TOCSAVE))
12589 if (!tocsave_find (htab, INSERT,
12590 &local_syms, irela + 1, input_bfd))
12591 goto error_ret_free_internal;
12594 stub_type = ppc_stub_plt_call_r2save;
12597 /* Support for grouping stub sections. */
12598 id_sec = htab->sec_info[section->id].u.group->link_sec;
12600 /* Get the name of this stub. */
12601 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12603 goto error_ret_free_internal;
12605 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12606 stub_name, FALSE, FALSE);
12607 if (stub_entry != NULL)
12609 enum ppc_stub_type old_type;
12610 /* A stub has already been created, but it may
12611 not be the required type. We shouldn't be
12612 transitioning from plt_call to long_branch
12613 stubs or vice versa, but we might be
12614 upgrading from plt_call to plt_call_r2save or
12615 from long_branch to long_branch_r2off. */
12617 old_type = stub_entry->stub_type;
12623 case ppc_stub_save_res:
12626 case ppc_stub_plt_call:
12627 case ppc_stub_plt_call_r2save:
12628 case ppc_stub_plt_call_notoc:
12629 case ppc_stub_plt_call_both:
12630 if (stub_type == ppc_stub_plt_call)
12632 else if (stub_type == ppc_stub_plt_call_r2save)
12634 if (old_type == ppc_stub_plt_call_notoc)
12635 stub_type = ppc_stub_plt_call_both;
12637 else if (stub_type == ppc_stub_plt_call_notoc)
12639 if (old_type == ppc_stub_plt_call_r2save)
12640 stub_type = ppc_stub_plt_call_both;
12646 case ppc_stub_plt_branch:
12647 case ppc_stub_plt_branch_r2off:
12648 case ppc_stub_plt_branch_notoc:
12649 case ppc_stub_plt_branch_both:
12650 old_type += (ppc_stub_long_branch
12651 - ppc_stub_plt_branch);
12652 /* Fall through. */
12653 case ppc_stub_long_branch:
12654 case ppc_stub_long_branch_r2off:
12655 case ppc_stub_long_branch_notoc:
12656 case ppc_stub_long_branch_both:
12657 if (stub_type == ppc_stub_long_branch)
12659 else if (stub_type == ppc_stub_long_branch_r2off)
12661 if (old_type == ppc_stub_long_branch_notoc)
12662 stub_type = ppc_stub_long_branch_both;
12664 else if (stub_type == ppc_stub_long_branch_notoc)
12666 if (old_type == ppc_stub_long_branch_r2off)
12667 stub_type = ppc_stub_long_branch_both;
12673 if (old_type < stub_type)
12674 stub_entry->stub_type = stub_type;
12678 stub_entry = ppc_add_stub (stub_name, section, info);
12679 if (stub_entry == NULL)
12682 error_ret_free_internal:
12683 if (elf_section_data (section)->relocs == NULL)
12684 free (internal_relocs);
12685 error_ret_free_local:
12686 if (local_syms != NULL
12687 && (symtab_hdr->contents
12688 != (unsigned char *) local_syms))
12693 stub_entry->stub_type = stub_type;
12694 if (stub_type >= ppc_stub_plt_call
12695 && stub_type <= ppc_stub_plt_call_both)
12697 stub_entry->target_value = sym_value;
12698 stub_entry->target_section = sym_sec;
12702 stub_entry->target_value = code_value;
12703 stub_entry->target_section = code_sec;
12705 stub_entry->h = hash;
12706 stub_entry->plt_ent = plt_ent;
12707 stub_entry->symtype
12708 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
12709 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12712 && (hash->elf.root.type == bfd_link_hash_defined
12713 || hash->elf.root.type == bfd_link_hash_defweak))
12714 htab->stub_globals += 1;
12717 /* We're done with the internal relocs, free them. */
12718 if (elf_section_data (section)->relocs != internal_relocs)
12719 free (internal_relocs);
12722 if (local_syms != NULL
12723 && symtab_hdr->contents != (unsigned char *) local_syms)
12725 if (!info->keep_memory)
12728 symtab_hdr->contents = (unsigned char *) local_syms;
12732 /* We may have added some stubs. Find out the new size of the
12734 for (group = htab->group; group != NULL; group = group->next)
12736 group->lr_restore = 0;
12737 group->eh_size = 0;
12738 if (group->stub_sec != NULL)
12740 asection *stub_sec = group->stub_sec;
12742 if (htab->stub_iteration <= STUB_SHRINK_ITER
12743 || stub_sec->rawsize < stub_sec->size)
12744 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12745 stub_sec->rawsize = stub_sec->size;
12746 stub_sec->size = 0;
12747 stub_sec->reloc_count = 0;
12748 stub_sec->flags &= ~SEC_RELOC;
12752 if (htab->stub_iteration <= STUB_SHRINK_ITER
12753 || htab->brlt->rawsize < htab->brlt->size)
12754 htab->brlt->rawsize = htab->brlt->size;
12755 htab->brlt->size = 0;
12756 htab->brlt->reloc_count = 0;
12757 htab->brlt->flags &= ~SEC_RELOC;
12758 if (htab->relbrlt != NULL)
12759 htab->relbrlt->size = 0;
12761 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12763 for (group = htab->group; group != NULL; group = group->next)
12764 if (group->needs_save_res)
12765 group->stub_sec->size += htab->sfpr->size;
12767 if (info->emitrelocations
12768 && htab->glink != NULL && htab->glink->size != 0)
12770 htab->glink->reloc_count = 1;
12771 htab->glink->flags |= SEC_RELOC;
12774 if (htab->glink_eh_frame != NULL
12775 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12776 && htab->glink_eh_frame->output_section->size > 8)
12778 size_t size = 0, align = 4;
12780 for (group = htab->group; group != NULL; group = group->next)
12781 if (group->eh_size != 0)
12782 size += (group->eh_size + 17 + align - 1) & -align;
12783 if (htab->glink != NULL && htab->glink->size != 0)
12784 size += (24 + align - 1) & -align;
12786 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12787 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12788 size = (size + align - 1) & -align;
12789 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12790 htab->glink_eh_frame->size = size;
12793 if (htab->params->plt_stub_align != 0)
12794 for (group = htab->group; group != NULL; group = group->next)
12795 if (group->stub_sec != NULL)
12797 int align = abs (htab->params->plt_stub_align);
12798 group->stub_sec->size
12799 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12802 for (group = htab->group; group != NULL; group = group->next)
12803 if (group->stub_sec != NULL
12804 && group->stub_sec->rawsize != group->stub_sec->size
12805 && (htab->stub_iteration <= STUB_SHRINK_ITER
12806 || group->stub_sec->rawsize < group->stub_sec->size))
12810 && (htab->brlt->rawsize == htab->brlt->size
12811 || (htab->stub_iteration > STUB_SHRINK_ITER
12812 && htab->brlt->rawsize > htab->brlt->size))
12813 && (htab->glink_eh_frame == NULL
12814 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12817 /* Ask the linker to do its stuff. */
12818 (*htab->params->layout_sections_again) ();
12821 if (htab->glink_eh_frame != NULL
12822 && htab->glink_eh_frame->size != 0)
12825 bfd_byte *p, *last_fde;
12826 size_t last_fde_len, size, align, pad;
12827 struct map_stub *group;
12829 /* It is necessary to at least have a rough outline of the
12830 linker generated CIEs and FDEs written before
12831 bfd_elf_discard_info is run, in order for these FDEs to be
12832 indexed in .eh_frame_hdr. */
12833 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12836 htab->glink_eh_frame->contents = p;
12840 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12841 /* CIE length (rewrite in case little-endian). */
12842 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
12843 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12844 p += last_fde_len + 4;
12846 for (group = htab->group; group != NULL; group = group->next)
12847 if (group->eh_size != 0)
12849 group->eh_base = p - htab->glink_eh_frame->contents;
12851 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
12853 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12856 val = p - htab->glink_eh_frame->contents;
12857 bfd_put_32 (htab->elf.dynobj, val, p);
12859 /* Offset to stub section, written later. */
12861 /* stub section size. */
12862 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
12864 /* Augmentation. */
12866 /* Make sure we don't have all nops. This is enough for
12867 elf-eh-frame.c to detect the last non-nop opcode. */
12868 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
12869 p = last_fde + last_fde_len + 4;
12871 if (htab->glink != NULL && htab->glink->size != 0)
12874 last_fde_len = ((24 + align - 1) & -align) - 4;
12876 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12879 val = p - htab->glink_eh_frame->contents;
12880 bfd_put_32 (htab->elf.dynobj, val, p);
12882 /* Offset to .glink, written later. */
12885 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12887 /* Augmentation. */
12890 *p++ = DW_CFA_advance_loc + 1;
12891 *p++ = DW_CFA_register;
12893 *p++ = htab->opd_abi ? 12 : 0;
12894 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
12895 *p++ = DW_CFA_restore_extended;
12897 p += ((24 + align - 1) & -align) - 24;
12899 /* Subsume any padding into the last FDE if user .eh_frame
12900 sections are aligned more than glink_eh_frame. Otherwise any
12901 zero padding will be seen as a terminator. */
12902 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12903 size = p - htab->glink_eh_frame->contents;
12904 pad = ((size + align - 1) & -align) - size;
12905 htab->glink_eh_frame->size = size + pad;
12906 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12909 maybe_strip_output (info, htab->brlt);
12910 if (htab->glink_eh_frame != NULL)
12911 maybe_strip_output (info, htab->glink_eh_frame);
12916 /* Called after we have determined section placement. If sections
12917 move, we'll be called again. Provide a value for TOCstart. */
12920 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12923 bfd_vma TOCstart, adjust;
12927 struct elf_link_hash_entry *h;
12928 struct elf_link_hash_table *htab = elf_hash_table (info);
12930 if (is_elf_hash_table (htab)
12931 && htab->hgot != NULL)
12935 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12936 if (is_elf_hash_table (htab))
12940 && h->root.type == bfd_link_hash_defined
12941 && !h->root.linker_def
12942 && (!is_elf_hash_table (htab)
12943 || h->def_regular))
12945 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12946 + h->root.u.def.section->output_offset
12947 + h->root.u.def.section->output_section->vma);
12948 _bfd_set_gp_value (obfd, TOCstart);
12953 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12954 order. The TOC starts where the first of these sections starts. */
12955 s = bfd_get_section_by_name (obfd, ".got");
12956 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12957 s = bfd_get_section_by_name (obfd, ".toc");
12958 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12959 s = bfd_get_section_by_name (obfd, ".tocbss");
12960 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12961 s = bfd_get_section_by_name (obfd, ".plt");
12962 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12964 /* This may happen for
12965 o references to TOC base (SYM@toc / TOC[tc0]) without a
12967 o bad linker script
12968 o --gc-sections and empty TOC sections
12970 FIXME: Warn user? */
12972 /* Look for a likely section. We probably won't even be
12974 for (s = obfd->sections; s != NULL; s = s->next)
12975 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12977 == (SEC_ALLOC | SEC_SMALL_DATA))
12980 for (s = obfd->sections; s != NULL; s = s->next)
12981 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12982 == (SEC_ALLOC | SEC_SMALL_DATA))
12985 for (s = obfd->sections; s != NULL; s = s->next)
12986 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12990 for (s = obfd->sections; s != NULL; s = s->next)
12991 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12997 TOCstart = s->output_section->vma + s->output_offset;
12999 /* Force alignment. */
13000 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13001 TOCstart -= adjust;
13002 _bfd_set_gp_value (obfd, TOCstart);
13004 if (info != NULL && s != NULL)
13006 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13010 if (htab->elf.hgot != NULL)
13012 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13013 htab->elf.hgot->root.u.def.section = s;
13018 struct bfd_link_hash_entry *bh = NULL;
13019 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13020 s, TOC_BASE_OFF - adjust,
13021 NULL, FALSE, FALSE, &bh);
13027 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13028 write out any global entry stubs, and PLT relocations. */
13031 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13033 struct bfd_link_info *info;
13034 struct ppc_link_hash_table *htab;
13035 struct plt_entry *ent;
13038 if (h->root.type == bfd_link_hash_indirect)
13042 htab = ppc_hash_table (info);
13046 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13047 if (ent->plt.offset != (bfd_vma) -1)
13049 /* This symbol has an entry in the procedure linkage
13050 table. Set it up. */
13051 Elf_Internal_Rela rela;
13052 asection *plt, *relplt;
13055 if (!htab->elf.dynamic_sections_created
13056 || h->dynindx == -1)
13058 if (!(h->def_regular
13059 && (h->root.type == bfd_link_hash_defined
13060 || h->root.type == bfd_link_hash_defweak)))
13062 if (h->type == STT_GNU_IFUNC)
13064 plt = htab->elf.iplt;
13065 relplt = htab->elf.irelplt;
13066 htab->local_ifunc_resolver = 1;
13068 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13070 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13074 plt = htab->pltlocal;
13075 if (bfd_link_pic (info))
13077 relplt = htab->relpltlocal;
13079 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13081 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13086 rela.r_addend = (h->root.u.def.value
13087 + h->root.u.def.section->output_offset
13088 + h->root.u.def.section->output_section->vma
13091 if (relplt == NULL)
13093 loc = plt->contents + ent->plt.offset;
13094 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13097 bfd_vma toc = elf_gp (info->output_bfd);
13098 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13099 bfd_put_64 (info->output_bfd, toc, loc + 8);
13104 rela.r_offset = (plt->output_section->vma
13105 + plt->output_offset
13106 + ent->plt.offset);
13107 loc = relplt->contents + (relplt->reloc_count++
13108 * sizeof (Elf64_External_Rela));
13109 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13114 rela.r_offset = (htab->elf.splt->output_section->vma
13115 + htab->elf.splt->output_offset
13116 + ent->plt.offset);
13117 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13118 rela.r_addend = ent->addend;
13119 loc = (htab->elf.srelplt->contents
13120 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13121 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13122 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13123 htab->maybe_local_ifunc_resolver = 1;
13124 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13128 if (!h->pointer_equality_needed)
13131 if (h->def_regular)
13134 s = htab->global_entry;
13135 if (s == NULL || s->size == 0)
13138 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13139 if (ent->plt.offset != (bfd_vma) -1
13140 && ent->addend == 0)
13146 p = s->contents + h->root.u.def.value;
13147 plt = htab->elf.splt;
13148 if (!htab->elf.dynamic_sections_created
13149 || h->dynindx == -1)
13151 if (h->type == STT_GNU_IFUNC)
13152 plt = htab->elf.iplt;
13154 plt = htab->pltlocal;
13156 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13157 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13159 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13161 info->callbacks->einfo
13162 (_("%P: linkage table error against `%pT'\n"),
13163 h->root.root.string);
13164 bfd_set_error (bfd_error_bad_value);
13165 htab->stub_error = TRUE;
13168 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13169 if (htab->params->emit_stub_syms)
13171 size_t len = strlen (h->root.root.string);
13172 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13177 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13178 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13181 if (h->root.type == bfd_link_hash_new)
13183 h->root.type = bfd_link_hash_defined;
13184 h->root.u.def.section = s;
13185 h->root.u.def.value = p - s->contents;
13186 h->ref_regular = 1;
13187 h->def_regular = 1;
13188 h->ref_regular_nonweak = 1;
13189 h->forced_local = 1;
13191 h->root.linker_def = 1;
13195 if (PPC_HA (off) != 0)
13197 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13200 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13202 bfd_put_32 (s->owner, MTCTR_R12, p);
13204 bfd_put_32 (s->owner, BCTR, p);
13210 /* Write PLT relocs for locals. */
13213 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13215 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13218 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13220 struct got_entry **lgot_ents, **end_lgot_ents;
13221 struct plt_entry **local_plt, **lplt, **end_local_plt;
13222 Elf_Internal_Shdr *symtab_hdr;
13223 bfd_size_type locsymcount;
13224 Elf_Internal_Sym *local_syms = NULL;
13225 struct plt_entry *ent;
13227 if (!is_ppc64_elf (ibfd))
13230 lgot_ents = elf_local_got_ents (ibfd);
13234 symtab_hdr = &elf_symtab_hdr (ibfd);
13235 locsymcount = symtab_hdr->sh_info;
13236 end_lgot_ents = lgot_ents + locsymcount;
13237 local_plt = (struct plt_entry **) end_lgot_ents;
13238 end_local_plt = local_plt + locsymcount;
13239 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13240 for (ent = *lplt; ent != NULL; ent = ent->next)
13241 if (ent->plt.offset != (bfd_vma) -1)
13243 Elf_Internal_Sym *sym;
13245 asection *plt, *relplt;
13249 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13250 lplt - local_plt, ibfd))
13252 if (local_syms != NULL
13253 && symtab_hdr->contents != (unsigned char *) local_syms)
13258 val = sym->st_value + ent->addend;
13259 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13260 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13261 if (sym_sec != NULL && sym_sec->output_section != NULL)
13262 val += sym_sec->output_offset + sym_sec->output_section->vma;
13264 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13266 htab->local_ifunc_resolver = 1;
13267 plt = htab->elf.iplt;
13268 relplt = htab->elf.irelplt;
13272 plt = htab->pltlocal;
13273 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13276 if (relplt == NULL)
13278 loc = plt->contents + ent->plt.offset;
13279 bfd_put_64 (info->output_bfd, val, loc);
13282 bfd_vma toc = elf_gp (ibfd);
13283 bfd_put_64 (info->output_bfd, toc, loc + 8);
13288 Elf_Internal_Rela rela;
13289 rela.r_offset = (ent->plt.offset
13290 + plt->output_offset
13291 + plt->output_section->vma);
13292 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13295 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13297 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13302 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13304 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13306 rela.r_addend = val;
13307 loc = relplt->contents + (relplt->reloc_count++
13308 * sizeof (Elf64_External_Rela));
13309 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13313 if (local_syms != NULL
13314 && symtab_hdr->contents != (unsigned char *) local_syms)
13316 if (!info->keep_memory)
13319 symtab_hdr->contents = (unsigned char *) local_syms;
13325 /* Build all the stubs associated with the current output file.
13326 The stubs are kept in a hash table attached to the main linker
13327 hash table. This function is called via gldelf64ppc_finish. */
13330 ppc64_elf_build_stubs (struct bfd_link_info *info,
13333 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13334 struct map_stub *group;
13335 asection *stub_sec;
13337 int stub_sec_count = 0;
13342 /* Allocate memory to hold the linker stubs. */
13343 for (group = htab->group; group != NULL; group = group->next)
13345 group->eh_size = 0;
13346 group->lr_restore = 0;
13347 if ((stub_sec = group->stub_sec) != NULL
13348 && stub_sec->size != 0)
13350 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13352 if (stub_sec->contents == NULL)
13354 stub_sec->size = 0;
13358 if (htab->glink != NULL && htab->glink->size != 0)
13363 /* Build the .glink plt call stub. */
13364 if (htab->params->emit_stub_syms)
13366 struct elf_link_hash_entry *h;
13367 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13368 TRUE, FALSE, FALSE);
13371 if (h->root.type == bfd_link_hash_new)
13373 h->root.type = bfd_link_hash_defined;
13374 h->root.u.def.section = htab->glink;
13375 h->root.u.def.value = 8;
13376 h->ref_regular = 1;
13377 h->def_regular = 1;
13378 h->ref_regular_nonweak = 1;
13379 h->forced_local = 1;
13381 h->root.linker_def = 1;
13384 plt0 = (htab->elf.splt->output_section->vma
13385 + htab->elf.splt->output_offset
13387 if (info->emitrelocations)
13389 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13392 r->r_offset = (htab->glink->output_offset
13393 + htab->glink->output_section->vma);
13394 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13395 r->r_addend = plt0;
13397 p = htab->glink->contents;
13398 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13399 bfd_put_64 (htab->glink->owner, plt0, p);
13403 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13405 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13407 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13409 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13411 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13413 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13415 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13417 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13419 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13421 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13426 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13428 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13430 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13432 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13434 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13436 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13438 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13440 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13442 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13444 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13446 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13448 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13450 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13453 bfd_put_32 (htab->glink->owner, BCTR, p);
13455 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13457 /* Build the .glink lazy link call stubs. */
13459 while (p < htab->glink->contents + htab->glink->size)
13465 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13470 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13472 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13477 bfd_put_32 (htab->glink->owner,
13478 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13484 /* Build .glink global entry stubs, and PLT relocs for globals. */
13485 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13487 if (!write_plt_relocs_for_local_syms (info))
13490 if (htab->brlt != NULL && htab->brlt->size != 0)
13492 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13494 if (htab->brlt->contents == NULL)
13497 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13499 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13500 htab->relbrlt->size);
13501 if (htab->relbrlt->contents == NULL)
13505 /* Build the stubs as directed by the stub hash table. */
13506 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13508 for (group = htab->group; group != NULL; group = group->next)
13509 if (group->needs_save_res)
13510 group->stub_sec->size += htab->sfpr->size;
13512 if (htab->relbrlt != NULL)
13513 htab->relbrlt->reloc_count = 0;
13515 if (htab->params->plt_stub_align != 0)
13516 for (group = htab->group; group != NULL; group = group->next)
13517 if ((stub_sec = group->stub_sec) != NULL)
13519 int align = abs (htab->params->plt_stub_align);
13520 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13523 for (group = htab->group; group != NULL; group = group->next)
13524 if (group->needs_save_res)
13526 stub_sec = group->stub_sec;
13527 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13528 htab->sfpr->contents, htab->sfpr->size);
13529 if (htab->params->emit_stub_syms)
13533 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13534 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13539 if (htab->glink_eh_frame != NULL
13540 && htab->glink_eh_frame->size != 0)
13545 p = htab->glink_eh_frame->contents;
13546 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13548 for (group = htab->group; group != NULL; group = group->next)
13549 if (group->eh_size != 0)
13551 /* Offset to stub section. */
13552 val = (group->stub_sec->output_section->vma
13553 + group->stub_sec->output_offset);
13554 val -= (htab->glink_eh_frame->output_section->vma
13555 + htab->glink_eh_frame->output_offset
13556 + (p + 8 - htab->glink_eh_frame->contents));
13557 if (val + 0x80000000 > 0xffffffff)
13560 (_("%s offset too large for .eh_frame sdata4 encoding"),
13561 group->stub_sec->name);
13564 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13565 p += (group->eh_size + 17 + 3) & -4;
13567 if (htab->glink != NULL && htab->glink->size != 0)
13569 /* Offset to .glink. */
13570 val = (htab->glink->output_section->vma
13571 + htab->glink->output_offset
13573 val -= (htab->glink_eh_frame->output_section->vma
13574 + htab->glink_eh_frame->output_offset
13575 + (p + 8 - htab->glink_eh_frame->contents));
13576 if (val + 0x80000000 > 0xffffffff)
13579 (_("%s offset too large for .eh_frame sdata4 encoding"),
13580 htab->glink->name);
13583 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13584 p += (24 + align - 1) & -align;
13588 for (group = htab->group; group != NULL; group = group->next)
13589 if ((stub_sec = group->stub_sec) != NULL)
13591 stub_sec_count += 1;
13592 if (stub_sec->rawsize != stub_sec->size
13593 && (htab->stub_iteration <= STUB_SHRINK_ITER
13594 || stub_sec->rawsize < stub_sec->size))
13600 htab->stub_error = TRUE;
13601 _bfd_error_handler (_("stubs don't match calculated size"));
13604 if (htab->stub_error)
13610 *stats = bfd_malloc (500);
13611 if (*stats == NULL)
13614 len = sprintf (*stats,
13615 ngettext ("linker stubs in %u group\n",
13616 "linker stubs in %u groups\n",
13619 sprintf (*stats + len, _(" branch %lu\n"
13620 " branch toc adj %lu\n"
13621 " branch notoc %lu\n"
13622 " branch both %lu\n"
13623 " long branch %lu\n"
13624 " long toc adj %lu\n"
13625 " long notoc %lu\n"
13628 " plt call save %lu\n"
13629 " plt call notoc %lu\n"
13630 " plt call both %lu\n"
13631 " global entry %lu"),
13632 htab->stub_count[ppc_stub_long_branch - 1],
13633 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13634 htab->stub_count[ppc_stub_long_branch_notoc - 1],
13635 htab->stub_count[ppc_stub_long_branch_both - 1],
13636 htab->stub_count[ppc_stub_plt_branch - 1],
13637 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13638 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
13639 htab->stub_count[ppc_stub_plt_branch_both - 1],
13640 htab->stub_count[ppc_stub_plt_call - 1],
13641 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13642 htab->stub_count[ppc_stub_plt_call_notoc - 1],
13643 htab->stub_count[ppc_stub_plt_call_both - 1],
13644 htab->stub_count[ppc_stub_global_entry - 1]);
13649 /* What to do when ld finds relocations against symbols defined in
13650 discarded sections. */
13652 static unsigned int
13653 ppc64_elf_action_discarded (asection *sec)
13655 if (strcmp (".opd", sec->name) == 0)
13658 if (strcmp (".toc", sec->name) == 0)
13661 if (strcmp (".toc1", sec->name) == 0)
13664 return _bfd_elf_default_action_discarded (sec);
13667 /* The RELOCATE_SECTION function is called by the ELF backend linker
13668 to handle the relocations for a section.
13670 The relocs are always passed as Rela structures; if the section
13671 actually uses Rel structures, the r_addend field will always be
13674 This function is responsible for adjust the section contents as
13675 necessary, and (if using Rela relocs and generating a
13676 relocatable output file) adjusting the reloc addend as
13679 This function does not have to worry about setting the reloc
13680 address or the reloc symbol index.
13682 LOCAL_SYMS is a pointer to the swapped in local symbols.
13684 LOCAL_SECTIONS is an array giving the section in the input file
13685 corresponding to the st_shndx field of each local symbol.
13687 The global hash table entry for the global symbols can be found
13688 via elf_sym_hashes (input_bfd).
13690 When generating relocatable output, this function must handle
13691 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13692 going to be the section symbol corresponding to the output
13693 section, which means that the addend must be adjusted
13697 ppc64_elf_relocate_section (bfd *output_bfd,
13698 struct bfd_link_info *info,
13700 asection *input_section,
13701 bfd_byte *contents,
13702 Elf_Internal_Rela *relocs,
13703 Elf_Internal_Sym *local_syms,
13704 asection **local_sections)
13706 struct ppc_link_hash_table *htab;
13707 Elf_Internal_Shdr *symtab_hdr;
13708 struct elf_link_hash_entry **sym_hashes;
13709 Elf_Internal_Rela *rel;
13710 Elf_Internal_Rela *wrel;
13711 Elf_Internal_Rela *relend;
13712 Elf_Internal_Rela outrel;
13714 struct got_entry **local_got_ents;
13716 bfd_boolean ret = TRUE;
13717 bfd_boolean is_opd;
13718 /* Assume 'at' branch hints. */
13719 bfd_boolean is_isa_v2 = TRUE;
13720 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13722 /* Initialize howto table if needed. */
13723 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13726 htab = ppc_hash_table (info);
13730 /* Don't relocate stub sections. */
13731 if (input_section->owner == htab->params->stub_bfd)
13734 if (!is_ppc64_elf (input_bfd))
13736 bfd_set_error (bfd_error_wrong_format);
13740 local_got_ents = elf_local_got_ents (input_bfd);
13741 TOCstart = elf_gp (output_bfd);
13742 symtab_hdr = &elf_symtab_hdr (input_bfd);
13743 sym_hashes = elf_sym_hashes (input_bfd);
13744 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13746 rel = wrel = relocs;
13747 relend = relocs + input_section->reloc_count;
13748 for (; rel < relend; wrel++, rel++)
13750 enum elf_ppc64_reloc_type r_type;
13752 bfd_reloc_status_type r;
13753 Elf_Internal_Sym *sym;
13755 struct elf_link_hash_entry *h_elf;
13756 struct ppc_link_hash_entry *h;
13757 struct ppc_link_hash_entry *fdh;
13758 const char *sym_name;
13759 unsigned long r_symndx, toc_symndx;
13760 bfd_vma toc_addend;
13761 unsigned char tls_mask, tls_gd, tls_type;
13762 unsigned char sym_type;
13763 bfd_vma relocation;
13764 bfd_boolean unresolved_reloc, save_unresolved_reloc;
13765 bfd_boolean warned;
13766 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13769 struct ppc_stub_hash_entry *stub_entry;
13770 bfd_vma max_br_offset;
13772 Elf_Internal_Rela orig_rel;
13773 reloc_howto_type *howto;
13774 struct reloc_howto_struct alt_howto;
13779 r_type = ELF64_R_TYPE (rel->r_info);
13780 r_symndx = ELF64_R_SYM (rel->r_info);
13782 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13783 symbol of the previous ADDR64 reloc. The symbol gives us the
13784 proper TOC base to use. */
13785 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13787 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13789 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13795 unresolved_reloc = FALSE;
13798 if (r_symndx < symtab_hdr->sh_info)
13800 /* It's a local symbol. */
13801 struct _opd_sec_data *opd;
13803 sym = local_syms + r_symndx;
13804 sec = local_sections[r_symndx];
13805 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13806 sym_type = ELF64_ST_TYPE (sym->st_info);
13807 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13808 opd = get_opd_info (sec);
13809 if (opd != NULL && opd->adjust != NULL)
13811 long adjust = opd->adjust[OPD_NDX (sym->st_value
13817 /* If this is a relocation against the opd section sym
13818 and we have edited .opd, adjust the reloc addend so
13819 that ld -r and ld --emit-relocs output is correct.
13820 If it is a reloc against some other .opd symbol,
13821 then the symbol value will be adjusted later. */
13822 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13823 rel->r_addend += adjust;
13825 relocation += adjust;
13831 bfd_boolean ignored;
13833 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13834 r_symndx, symtab_hdr, sym_hashes,
13835 h_elf, sec, relocation,
13836 unresolved_reloc, warned, ignored);
13837 sym_name = h_elf->root.root.string;
13838 sym_type = h_elf->type;
13840 && sec->owner == output_bfd
13841 && strcmp (sec->name, ".opd") == 0)
13843 /* This is a symbol defined in a linker script. All
13844 such are defined in output sections, even those
13845 defined by simple assignment from a symbol defined in
13846 an input section. Transfer the symbol to an
13847 appropriate input .opd section, so that a branch to
13848 this symbol will be mapped to the location specified
13849 by the opd entry. */
13850 struct bfd_link_order *lo;
13851 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13852 if (lo->type == bfd_indirect_link_order)
13854 asection *isec = lo->u.indirect.section;
13855 if (h_elf->root.u.def.value >= isec->output_offset
13856 && h_elf->root.u.def.value < (isec->output_offset
13859 h_elf->root.u.def.value -= isec->output_offset;
13860 h_elf->root.u.def.section = isec;
13867 h = (struct ppc_link_hash_entry *) h_elf;
13869 if (sec != NULL && discarded_section (sec))
13871 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13872 input_bfd, input_section,
13873 contents, rel->r_offset);
13874 wrel->r_offset = rel->r_offset;
13876 wrel->r_addend = 0;
13878 /* For ld -r, remove relocations in debug sections against
13879 symbols defined in discarded sections. Not done for
13880 non-debug to preserve relocs in .eh_frame which the
13881 eh_frame editing code expects to be present. */
13882 if (bfd_link_relocatable (info)
13883 && (input_section->flags & SEC_DEBUGGING))
13889 if (bfd_link_relocatable (info))
13892 if (h != NULL && &h->elf == htab->elf.hgot)
13894 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13895 sec = bfd_abs_section_ptr;
13896 unresolved_reloc = FALSE;
13899 /* TLS optimizations. Replace instruction sequences and relocs
13900 based on information we collected in tls_optimize. We edit
13901 RELOCS so that --emit-relocs will output something sensible
13902 for the final instruction stream. */
13907 tls_mask = h->tls_mask;
13908 else if (local_got_ents != NULL)
13910 struct plt_entry **local_plt = (struct plt_entry **)
13911 (local_got_ents + symtab_hdr->sh_info);
13912 unsigned char *lgot_masks = (unsigned char *)
13913 (local_plt + symtab_hdr->sh_info);
13914 tls_mask = lgot_masks[r_symndx];
13916 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
13917 && (r_type == R_PPC64_TLS
13918 || r_type == R_PPC64_TLSGD
13919 || r_type == R_PPC64_TLSLD))
13921 /* Check for toc tls entries. */
13922 unsigned char *toc_tls;
13924 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13925 &local_syms, rel, input_bfd))
13929 tls_mask = *toc_tls;
13932 /* Check that tls relocs are used with tls syms, and non-tls
13933 relocs are used with non-tls syms. */
13934 if (r_symndx != STN_UNDEF
13935 && r_type != R_PPC64_NONE
13937 || h->elf.root.type == bfd_link_hash_defined
13938 || h->elf.root.type == bfd_link_hash_defweak)
13939 && (IS_PPC64_TLS_RELOC (r_type)
13940 != (sym_type == STT_TLS
13941 || (sym_type == STT_SECTION
13942 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13944 if ((tls_mask & TLS_TLS) != 0
13945 && (r_type == R_PPC64_TLS
13946 || r_type == R_PPC64_TLSGD
13947 || r_type == R_PPC64_TLSLD))
13948 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13951 info->callbacks->einfo
13952 (!IS_PPC64_TLS_RELOC (r_type)
13953 /* xgettext:c-format */
13954 ? _("%H: %s used with TLS symbol `%pT'\n")
13955 /* xgettext:c-format */
13956 : _("%H: %s used with non-TLS symbol `%pT'\n"),
13957 input_bfd, input_section, rel->r_offset,
13958 ppc64_elf_howto_table[r_type]->name,
13962 /* Ensure reloc mapping code below stays sane. */
13963 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13964 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13965 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13966 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13967 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13968 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13969 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13970 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13971 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13972 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13980 case R_PPC64_LO_DS_OPT:
13981 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13982 if ((insn & (0x3f << 26)) != 58u << 26)
13984 insn += (14u << 26) - (58u << 26);
13985 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13986 r_type = R_PPC64_TOC16_LO;
13987 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13990 case R_PPC64_TOC16:
13991 case R_PPC64_TOC16_LO:
13992 case R_PPC64_TOC16_DS:
13993 case R_PPC64_TOC16_LO_DS:
13995 /* Check for toc tls entries. */
13996 unsigned char *toc_tls;
13999 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14000 &local_syms, rel, input_bfd);
14006 tls_mask = *toc_tls;
14007 if (r_type == R_PPC64_TOC16_DS
14008 || r_type == R_PPC64_TOC16_LO_DS)
14010 if ((tls_mask & TLS_TLS) != 0
14011 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14016 /* If we found a GD reloc pair, then we might be
14017 doing a GD->IE transition. */
14020 tls_gd = TLS_TPRELGD;
14021 if ((tls_mask & TLS_TLS) != 0
14022 && (tls_mask & TLS_GD) == 0)
14025 else if (retval == 3)
14027 if ((tls_mask & TLS_TLS) != 0
14028 && (tls_mask & TLS_LD) == 0)
14036 case R_PPC64_GOT_TPREL16_HI:
14037 case R_PPC64_GOT_TPREL16_HA:
14038 if ((tls_mask & TLS_TLS) != 0
14039 && (tls_mask & TLS_TPREL) == 0)
14041 rel->r_offset -= d_offset;
14042 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14043 r_type = R_PPC64_NONE;
14044 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14048 case R_PPC64_GOT_TPREL16_DS:
14049 case R_PPC64_GOT_TPREL16_LO_DS:
14050 if ((tls_mask & TLS_TLS) != 0
14051 && (tls_mask & TLS_TPREL) == 0)
14054 insn = bfd_get_32 (input_bfd,
14055 contents + rel->r_offset - d_offset);
14057 insn |= 0x3c0d0000; /* addis 0,13,0 */
14058 bfd_put_32 (input_bfd, insn,
14059 contents + rel->r_offset - d_offset);
14060 r_type = R_PPC64_TPREL16_HA;
14061 if (toc_symndx != 0)
14063 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14064 rel->r_addend = toc_addend;
14065 /* We changed the symbol. Start over in order to
14066 get h, sym, sec etc. right. */
14070 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14075 if ((tls_mask & TLS_TLS) != 0
14076 && (tls_mask & TLS_TPREL) == 0)
14078 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14079 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14082 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14083 /* Was PPC64_TLS which sits on insn boundary, now
14084 PPC64_TPREL16_LO which is at low-order half-word. */
14085 rel->r_offset += d_offset;
14086 r_type = R_PPC64_TPREL16_LO;
14087 if (toc_symndx != 0)
14089 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14090 rel->r_addend = toc_addend;
14091 /* We changed the symbol. Start over in order to
14092 get h, sym, sec etc. right. */
14096 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14100 case R_PPC64_GOT_TLSGD16_HI:
14101 case R_PPC64_GOT_TLSGD16_HA:
14102 tls_gd = TLS_TPRELGD;
14103 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14107 case R_PPC64_GOT_TLSLD16_HI:
14108 case R_PPC64_GOT_TLSLD16_HA:
14109 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14112 if ((tls_mask & tls_gd) != 0)
14113 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14114 + R_PPC64_GOT_TPREL16_DS);
14117 rel->r_offset -= d_offset;
14118 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14119 r_type = R_PPC64_NONE;
14121 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14125 case R_PPC64_GOT_TLSGD16:
14126 case R_PPC64_GOT_TLSGD16_LO:
14127 tls_gd = TLS_TPRELGD;
14128 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14132 case R_PPC64_GOT_TLSLD16:
14133 case R_PPC64_GOT_TLSLD16_LO:
14134 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14136 unsigned int insn1, insn2;
14140 offset = (bfd_vma) -1;
14141 /* If not using the newer R_PPC64_TLSGD/LD to mark
14142 __tls_get_addr calls, we must trust that the call
14143 stays with its arg setup insns, ie. that the next
14144 reloc is the __tls_get_addr call associated with
14145 the current reloc. Edit both insns. */
14146 if (input_section->has_tls_get_addr_call
14147 && rel + 1 < relend
14148 && branch_reloc_hash_match (input_bfd, rel + 1,
14149 htab->tls_get_addr,
14150 htab->tls_get_addr_fd))
14151 offset = rel[1].r_offset;
14152 /* We read the low GOT_TLS (or TOC16) insn because we
14153 need to keep the destination reg. It may be
14154 something other than the usual r3, and moved to r3
14155 before the call by intervening code. */
14156 insn1 = bfd_get_32 (input_bfd,
14157 contents + rel->r_offset - d_offset);
14158 if ((tls_mask & tls_gd) != 0)
14161 insn1 &= (0x1f << 21) | (0x1f << 16);
14162 insn1 |= 58 << 26; /* ld */
14163 insn2 = 0x7c636a14; /* add 3,3,13 */
14164 if (offset != (bfd_vma) -1)
14165 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14166 if ((tls_mask & TLS_EXPLICIT) == 0)
14167 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14168 + R_PPC64_GOT_TPREL16_DS);
14170 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14171 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14176 insn1 &= 0x1f << 21;
14177 insn1 |= 0x3c0d0000; /* addis r,13,0 */
14178 insn2 = 0x38630000; /* addi 3,3,0 */
14181 /* Was an LD reloc. */
14183 sec = local_sections[toc_symndx];
14185 r_symndx < symtab_hdr->sh_info;
14187 if (local_sections[r_symndx] == sec)
14189 if (r_symndx >= symtab_hdr->sh_info)
14190 r_symndx = STN_UNDEF;
14191 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14192 if (r_symndx != STN_UNDEF)
14193 rel->r_addend -= (local_syms[r_symndx].st_value
14194 + sec->output_offset
14195 + sec->output_section->vma);
14197 else if (toc_symndx != 0)
14199 r_symndx = toc_symndx;
14200 rel->r_addend = toc_addend;
14202 r_type = R_PPC64_TPREL16_HA;
14203 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14204 if (offset != (bfd_vma) -1)
14206 rel[1].r_info = ELF64_R_INFO (r_symndx,
14207 R_PPC64_TPREL16_LO);
14208 rel[1].r_offset = offset + d_offset;
14209 rel[1].r_addend = rel->r_addend;
14212 bfd_put_32 (input_bfd, insn1,
14213 contents + rel->r_offset - d_offset);
14214 if (offset != (bfd_vma) -1)
14216 bfd_put_32 (input_bfd, insn2, contents + offset);
14217 if (offset + 8 <= input_section->size)
14219 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14220 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14221 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14224 if ((tls_mask & tls_gd) == 0
14225 && (tls_gd == 0 || toc_symndx != 0))
14227 /* We changed the symbol. Start over in order
14228 to get h, sym, sec etc. right. */
14234 case R_PPC64_TLSGD:
14235 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
14236 && rel + 1 < relend)
14238 unsigned int insn2;
14239 bfd_vma offset = rel->r_offset;
14240 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14242 if (is_plt_seq_reloc (r_type1))
14244 bfd_put_32 (output_bfd, NOP, contents + offset);
14245 if (r_type1 == R_PPC64_PLT_PCREL34
14246 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14247 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14248 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14252 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14253 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14255 if ((tls_mask & TLS_TPRELGD) != 0)
14258 r_type = R_PPC64_NONE;
14259 insn2 = 0x7c636a14; /* add 3,3,13 */
14264 if (toc_symndx != 0)
14266 r_symndx = toc_symndx;
14267 rel->r_addend = toc_addend;
14269 r_type = R_PPC64_TPREL16_LO;
14270 rel->r_offset = offset + d_offset;
14271 insn2 = 0x38630000; /* addi 3,3,0 */
14273 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14274 /* Zap the reloc on the _tls_get_addr call too. */
14275 BFD_ASSERT (offset == rel[1].r_offset);
14276 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14277 bfd_put_32 (input_bfd, insn2, contents + offset);
14278 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
14283 case R_PPC64_TLSLD:
14284 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
14285 && rel + 1 < relend)
14287 unsigned int insn2;
14288 bfd_vma offset = rel->r_offset;
14289 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14291 if (is_plt_seq_reloc (r_type1))
14293 bfd_put_32 (output_bfd, NOP, contents + offset);
14294 if (r_type1 == R_PPC64_PLT_PCREL34
14295 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14296 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14297 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14301 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14302 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14305 sec = local_sections[toc_symndx];
14307 r_symndx < symtab_hdr->sh_info;
14309 if (local_sections[r_symndx] == sec)
14311 if (r_symndx >= symtab_hdr->sh_info)
14312 r_symndx = STN_UNDEF;
14313 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14314 if (r_symndx != STN_UNDEF)
14315 rel->r_addend -= (local_syms[r_symndx].st_value
14316 + sec->output_offset
14317 + sec->output_section->vma);
14319 r_type = R_PPC64_TPREL16_LO;
14320 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14321 rel->r_offset = offset + d_offset;
14322 /* Zap the reloc on the _tls_get_addr call too. */
14323 BFD_ASSERT (offset == rel[1].r_offset);
14324 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14325 insn2 = 0x38630000; /* addi 3,3,0 */
14326 bfd_put_32 (input_bfd, insn2, contents + offset);
14331 case R_PPC64_DTPMOD64:
14332 if (rel + 1 < relend
14333 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14334 && rel[1].r_offset == rel->r_offset + 8)
14336 if ((tls_mask & TLS_GD) == 0)
14338 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14339 if ((tls_mask & TLS_TPRELGD) != 0)
14340 r_type = R_PPC64_TPREL64;
14343 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14344 r_type = R_PPC64_NONE;
14346 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14351 if ((tls_mask & TLS_LD) == 0)
14353 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14354 r_type = R_PPC64_NONE;
14355 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14360 case R_PPC64_TPREL64:
14361 if ((tls_mask & TLS_TPREL) == 0)
14363 r_type = R_PPC64_NONE;
14364 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14368 case R_PPC64_ENTRY:
14369 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14370 if (!bfd_link_pic (info)
14371 && !info->traditional_format
14372 && relocation + 0x80008000 <= 0xffffffff)
14374 unsigned int insn1, insn2;
14376 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14377 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14378 if ((insn1 & ~0xfffc) == LD_R2_0R12
14379 && insn2 == ADD_R2_R2_R12)
14381 bfd_put_32 (input_bfd,
14382 LIS_R2 + PPC_HA (relocation),
14383 contents + rel->r_offset);
14384 bfd_put_32 (input_bfd,
14385 ADDI_R2_R2 + PPC_LO (relocation),
14386 contents + rel->r_offset + 4);
14391 relocation -= (rel->r_offset
14392 + input_section->output_offset
14393 + input_section->output_section->vma);
14394 if (relocation + 0x80008000 <= 0xffffffff)
14396 unsigned int insn1, insn2;
14398 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14399 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14400 if ((insn1 & ~0xfffc) == LD_R2_0R12
14401 && insn2 == ADD_R2_R2_R12)
14403 bfd_put_32 (input_bfd,
14404 ADDIS_R2_R12 + PPC_HA (relocation),
14405 contents + rel->r_offset);
14406 bfd_put_32 (input_bfd,
14407 ADDI_R2_R2 + PPC_LO (relocation),
14408 contents + rel->r_offset + 4);
14414 case R_PPC64_REL16_HA:
14415 /* If we are generating a non-PIC executable, edit
14416 . 0: addis 2,12,.TOC.-0b@ha
14417 . addi 2,2,.TOC.-0b@l
14418 used by ELFv2 global entry points to set up r2, to
14421 if .TOC. is in range. */
14422 if (!bfd_link_pic (info)
14423 && !info->traditional_format
14425 && rel->r_addend == d_offset
14426 && h != NULL && &h->elf == htab->elf.hgot
14427 && rel + 1 < relend
14428 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14429 && rel[1].r_offset == rel->r_offset + 4
14430 && rel[1].r_addend == rel->r_addend + 4
14431 && relocation + 0x80008000 <= 0xffffffff)
14433 unsigned int insn1, insn2;
14434 bfd_vma offset = rel->r_offset - d_offset;
14435 insn1 = bfd_get_32 (input_bfd, contents + offset);
14436 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14437 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14438 && (insn2 & 0xffff0000) == ADDI_R2_R2)
14440 r_type = R_PPC64_ADDR16_HA;
14441 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14442 rel->r_addend -= d_offset;
14443 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14444 rel[1].r_addend -= d_offset + 4;
14445 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14451 /* Handle other relocations that tweak non-addend part of insn. */
14453 max_br_offset = 1 << 25;
14454 addend = rel->r_addend;
14455 reloc_dest = DEST_NORMAL;
14461 case R_PPC64_TOCSAVE:
14462 if (relocation + addend == (rel->r_offset
14463 + input_section->output_offset
14464 + input_section->output_section->vma)
14465 && tocsave_find (htab, NO_INSERT,
14466 &local_syms, rel, input_bfd))
14468 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14470 || insn == CROR_151515 || insn == CROR_313131)
14471 bfd_put_32 (input_bfd,
14472 STD_R2_0R1 + STK_TOC (htab),
14473 contents + rel->r_offset);
14477 /* Branch taken prediction relocations. */
14478 case R_PPC64_ADDR14_BRTAKEN:
14479 case R_PPC64_REL14_BRTAKEN:
14480 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
14481 /* Fall through. */
14483 /* Branch not taken prediction relocations. */
14484 case R_PPC64_ADDR14_BRNTAKEN:
14485 case R_PPC64_REL14_BRNTAKEN:
14486 insn |= bfd_get_32 (input_bfd,
14487 contents + rel->r_offset) & ~(0x01 << 21);
14488 /* Fall through. */
14490 case R_PPC64_REL14:
14491 max_br_offset = 1 << 15;
14492 /* Fall through. */
14494 case R_PPC64_REL24:
14495 case R_PPC64_REL24_NOTOC:
14496 case R_PPC64_PLTCALL:
14497 case R_PPC64_PLTCALL_NOTOC:
14498 /* Calls to functions with a different TOC, such as calls to
14499 shared objects, need to alter the TOC pointer. This is
14500 done using a linkage stub. A REL24 branching to these
14501 linkage stubs needs to be followed by a nop, as the nop
14502 will be replaced with an instruction to restore the TOC
14507 && h->oh->is_func_descriptor)
14508 fdh = ppc_follow_link (h->oh);
14509 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14511 if ((r_type == R_PPC64_PLTCALL
14512 || r_type == R_PPC64_PLTCALL_NOTOC)
14513 && stub_entry != NULL
14514 && stub_entry->stub_type >= ppc_stub_plt_call
14515 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14518 if (stub_entry != NULL
14519 && ((stub_entry->stub_type >= ppc_stub_plt_call
14520 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14521 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14522 || stub_entry->stub_type == ppc_stub_plt_branch_both
14523 || stub_entry->stub_type == ppc_stub_long_branch_r2off
14524 || stub_entry->stub_type == ppc_stub_long_branch_both))
14526 bfd_boolean can_plt_call = FALSE;
14528 if (stub_entry->stub_type == ppc_stub_plt_call
14530 && htab->params->plt_localentry0 != 0
14531 && is_elfv2_localentry0 (&h->elf))
14533 /* The function doesn't use or change r2. */
14534 can_plt_call = TRUE;
14536 else if (r_type == R_PPC64_REL24_NOTOC)
14538 /* NOTOC calls don't need to restore r2. */
14539 can_plt_call = TRUE;
14542 /* All of these stubs may modify r2, so there must be a
14543 branch and link followed by a nop. The nop is
14544 replaced by an insn to restore r2. */
14545 else if (rel->r_offset + 8 <= input_section->size)
14549 br = bfd_get_32 (input_bfd,
14550 contents + rel->r_offset);
14555 nop = bfd_get_32 (input_bfd,
14556 contents + rel->r_offset + 4);
14557 if (nop == LD_R2_0R1 + STK_TOC (htab))
14558 can_plt_call = TRUE;
14559 else if (nop == NOP
14560 || nop == CROR_151515
14561 || nop == CROR_313131)
14564 && (h == htab->tls_get_addr_fd
14565 || h == htab->tls_get_addr)
14566 && htab->params->tls_get_addr_opt)
14568 /* Special stub used, leave nop alone. */
14571 bfd_put_32 (input_bfd,
14572 LD_R2_0R1 + STK_TOC (htab),
14573 contents + rel->r_offset + 4);
14574 can_plt_call = TRUE;
14579 if (!can_plt_call && h != NULL)
14581 const char *name = h->elf.root.root.string;
14586 if (strncmp (name, "__libc_start_main", 17) == 0
14587 && (name[17] == 0 || name[17] == '@'))
14589 /* Allow crt1 branch to go via a toc adjusting
14590 stub. Other calls that never return could do
14591 the same, if we could detect such. */
14592 can_plt_call = TRUE;
14598 /* g++ as of 20130507 emits self-calls without a
14599 following nop. This is arguably wrong since we
14600 have conflicting information. On the one hand a
14601 global symbol and on the other a local call
14602 sequence, but don't error for this special case.
14603 It isn't possible to cheaply verify we have
14604 exactly such a call. Allow all calls to the same
14606 asection *code_sec = sec;
14608 if (get_opd_info (sec) != NULL)
14610 bfd_vma off = (relocation + addend
14611 - sec->output_section->vma
14612 - sec->output_offset);
14614 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14616 if (code_sec == input_section)
14617 can_plt_call = TRUE;
14622 if (stub_entry->stub_type >= ppc_stub_plt_call
14623 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14624 info->callbacks->einfo
14625 /* xgettext:c-format */
14626 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14627 "(plt call stub)\n"),
14628 input_bfd, input_section, rel->r_offset, sym_name);
14630 info->callbacks->einfo
14631 /* xgettext:c-format */
14632 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14633 "(toc save/adjust stub)\n"),
14634 input_bfd, input_section, rel->r_offset, sym_name);
14636 bfd_set_error (bfd_error_bad_value);
14641 && stub_entry->stub_type >= ppc_stub_plt_call
14642 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14643 unresolved_reloc = FALSE;
14646 if ((stub_entry == NULL
14647 || stub_entry->stub_type == ppc_stub_long_branch
14648 || stub_entry->stub_type == ppc_stub_plt_branch)
14649 && get_opd_info (sec) != NULL)
14651 /* The branch destination is the value of the opd entry. */
14652 bfd_vma off = (relocation + addend
14653 - sec->output_section->vma
14654 - sec->output_offset);
14655 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14656 if (dest != (bfd_vma) -1)
14660 reloc_dest = DEST_OPD;
14664 /* If the branch is out of reach we ought to have a long
14666 from = (rel->r_offset
14667 + input_section->output_offset
14668 + input_section->output_section->vma);
14670 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14674 if (stub_entry != NULL
14675 && (stub_entry->stub_type == ppc_stub_long_branch
14676 || stub_entry->stub_type == ppc_stub_plt_branch)
14677 && (r_type == R_PPC64_ADDR14_BRTAKEN
14678 || r_type == R_PPC64_ADDR14_BRNTAKEN
14679 || (relocation + addend - from + max_br_offset
14680 < 2 * max_br_offset)))
14681 /* Don't use the stub if this branch is in range. */
14684 if (stub_entry != NULL
14685 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
14686 || stub_entry->stub_type == ppc_stub_long_branch_both
14687 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
14688 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14689 && (r_type != R_PPC64_REL24_NOTOC
14690 || ((fdh ? fdh->elf.other : sym->st_other)
14691 & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
14692 && (relocation + addend - from + max_br_offset
14693 < 2 * max_br_offset))
14696 if (stub_entry != NULL
14697 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
14698 || stub_entry->stub_type == ppc_stub_long_branch_both
14699 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14700 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14701 && r_type == R_PPC64_REL24_NOTOC
14702 && (relocation + addend - from + max_br_offset
14703 < 2 * max_br_offset))
14706 if (stub_entry != NULL)
14708 /* Munge up the value and addend so that we call the stub
14709 rather than the procedure directly. */
14710 asection *stub_sec = stub_entry->group->stub_sec;
14712 if (stub_entry->stub_type == ppc_stub_save_res)
14713 relocation += (stub_sec->output_offset
14714 + stub_sec->output_section->vma
14715 + stub_sec->size - htab->sfpr->size
14716 - htab->sfpr->output_offset
14717 - htab->sfpr->output_section->vma);
14719 relocation = (stub_entry->stub_offset
14720 + stub_sec->output_offset
14721 + stub_sec->output_section->vma);
14723 reloc_dest = DEST_STUB;
14725 if (((stub_entry->stub_type == ppc_stub_plt_call
14726 && ALWAYS_EMIT_R2SAVE)
14727 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14728 || stub_entry->stub_type == ppc_stub_plt_call_both)
14730 && (h == htab->tls_get_addr_fd
14731 || h == htab->tls_get_addr)
14732 && htab->params->tls_get_addr_opt)
14733 && rel + 1 < relend
14734 && rel[1].r_offset == rel->r_offset + 4
14735 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14737 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
14738 || stub_entry->stub_type == ppc_stub_plt_branch_both
14739 || stub_entry->stub_type == ppc_stub_plt_call_both)
14740 && r_type == R_PPC64_REL24_NOTOC)
14743 if (r_type == R_PPC64_REL24_NOTOC
14744 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
14745 || stub_entry->stub_type == ppc_stub_plt_call_both))
14746 htab->notoc_plt = 1;
14753 /* Set 'a' bit. This is 0b00010 in BO field for branch
14754 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14755 for branch on CTR insns (BO == 1a00t or 1a01t). */
14756 if ((insn & (0x14 << 21)) == (0x04 << 21))
14757 insn |= 0x02 << 21;
14758 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14759 insn |= 0x08 << 21;
14765 /* Invert 'y' bit if not the default. */
14766 if ((bfd_signed_vma) (relocation + addend - from) < 0)
14767 insn ^= 0x01 << 21;
14770 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14773 /* NOP out calls to undefined weak functions.
14774 We can thus call a weak function without first
14775 checking whether the function is defined. */
14777 && h->elf.root.type == bfd_link_hash_undefweak
14778 && h->elf.dynindx == -1
14779 && (r_type == R_PPC64_REL24
14780 || r_type == R_PPC64_REL24_NOTOC)
14784 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14789 case R_PPC64_GOT16_DS:
14790 from = TOCstart + htab->sec_info[input_section->id].toc_off;
14791 if (relocation + addend - from + 0x8000 < 0x10000
14792 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14794 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14795 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
14797 insn += (14u << 26) - (58u << 26);
14798 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
14799 r_type = R_PPC64_TOC16;
14800 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14805 case R_PPC64_GOT16_LO_DS:
14806 case R_PPC64_GOT16_HA:
14807 from = TOCstart + htab->sec_info[input_section->id].toc_off;
14808 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
14809 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14811 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14812 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
14814 insn += (14u << 26) - (58u << 26);
14815 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
14816 r_type = R_PPC64_TOC16_LO;
14817 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14819 else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
14821 r_type = R_PPC64_TOC16_HA;
14822 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14828 /* Set `addend'. */
14830 save_unresolved_reloc = unresolved_reloc;
14834 /* xgettext:c-format */
14835 _bfd_error_handler (_("%pB: %s unsupported"),
14836 input_bfd, ppc64_elf_howto_table[r_type]->name);
14838 bfd_set_error (bfd_error_bad_value);
14844 case R_PPC64_TLSGD:
14845 case R_PPC64_TLSLD:
14846 case R_PPC64_TOCSAVE:
14847 case R_PPC64_GNU_VTINHERIT:
14848 case R_PPC64_GNU_VTENTRY:
14849 case R_PPC64_ENTRY:
14852 /* GOT16 relocations. Like an ADDR16 using the symbol's
14853 address in the GOT as relocation value instead of the
14854 symbol's value itself. Also, create a GOT entry for the
14855 symbol and put the symbol value there. */
14856 case R_PPC64_GOT_TLSGD16:
14857 case R_PPC64_GOT_TLSGD16_LO:
14858 case R_PPC64_GOT_TLSGD16_HI:
14859 case R_PPC64_GOT_TLSGD16_HA:
14860 tls_type = TLS_TLS | TLS_GD;
14863 case R_PPC64_GOT_TLSLD16:
14864 case R_PPC64_GOT_TLSLD16_LO:
14865 case R_PPC64_GOT_TLSLD16_HI:
14866 case R_PPC64_GOT_TLSLD16_HA:
14867 tls_type = TLS_TLS | TLS_LD;
14870 case R_PPC64_GOT_TPREL16_DS:
14871 case R_PPC64_GOT_TPREL16_LO_DS:
14872 case R_PPC64_GOT_TPREL16_HI:
14873 case R_PPC64_GOT_TPREL16_HA:
14874 tls_type = TLS_TLS | TLS_TPREL;
14877 case R_PPC64_GOT_DTPREL16_DS:
14878 case R_PPC64_GOT_DTPREL16_LO_DS:
14879 case R_PPC64_GOT_DTPREL16_HI:
14880 case R_PPC64_GOT_DTPREL16_HA:
14881 tls_type = TLS_TLS | TLS_DTPREL;
14884 case R_PPC64_GOT16:
14885 case R_PPC64_GOT16_LO:
14886 case R_PPC64_GOT16_HI:
14887 case R_PPC64_GOT16_HA:
14888 case R_PPC64_GOT16_DS:
14889 case R_PPC64_GOT16_LO_DS:
14890 case R_PPC64_GOT_PCREL34:
14893 /* Relocation is to the entry for this symbol in the global
14898 unsigned long indx = 0;
14899 struct got_entry *ent;
14900 bfd_vma sym_addend = orig_rel.r_addend;
14902 if (r_type == R_PPC64_GOT_PCREL34)
14905 if (tls_type == (TLS_TLS | TLS_LD)
14907 || !h->elf.def_dynamic))
14908 ent = ppc64_tlsld_got (input_bfd);
14913 if (!htab->elf.dynamic_sections_created
14914 || h->elf.dynindx == -1
14915 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14916 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
14917 /* This is actually a static link, or it is a
14918 -Bsymbolic link and the symbol is defined
14919 locally, or the symbol was forced to be local
14920 because of a version file. */
14924 indx = h->elf.dynindx;
14925 unresolved_reloc = FALSE;
14927 ent = h->elf.got.glist;
14931 if (local_got_ents == NULL)
14933 ent = local_got_ents[r_symndx];
14936 for (; ent != NULL; ent = ent->next)
14937 if (ent->addend == sym_addend
14938 && ent->owner == input_bfd
14939 && ent->tls_type == tls_type)
14945 if (ent->is_indirect)
14946 ent = ent->got.ent;
14947 offp = &ent->got.offset;
14948 got = ppc64_elf_tdata (ent->owner)->got;
14952 /* The offset must always be a multiple of 8. We use the
14953 least significant bit to record whether we have already
14954 processed this entry. */
14956 if ((off & 1) != 0)
14960 /* Generate relocs for the dynamic linker, except in
14961 the case of TLSLD where we'll use one entry per
14969 ? h->elf.type == STT_GNU_IFUNC
14970 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14973 relgot = htab->elf.irelplt;
14975 htab->local_ifunc_resolver = 1;
14976 else if (is_static_defined (&h->elf))
14977 htab->maybe_local_ifunc_resolver = 1;
14980 || (bfd_link_pic (info)
14982 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
14983 || (tls_type == (TLS_TLS | TLS_LD)
14984 && !h->elf.def_dynamic))
14985 && !(tls_type == (TLS_TLS | TLS_TPREL)
14986 && bfd_link_executable (info)
14987 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
14988 relgot = ppc64_elf_tdata (ent->owner)->relgot;
14989 if (relgot != NULL)
14991 outrel.r_offset = (got->output_section->vma
14992 + got->output_offset
14994 outrel.r_addend = sym_addend;
14995 if (tls_type & (TLS_LD | TLS_GD))
14997 outrel.r_addend = 0;
14998 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14999 if (tls_type == (TLS_TLS | TLS_GD))
15001 loc = relgot->contents;
15002 loc += (relgot->reloc_count++
15003 * sizeof (Elf64_External_Rela));
15004 bfd_elf64_swap_reloca_out (output_bfd,
15006 outrel.r_offset += 8;
15007 outrel.r_addend = sym_addend;
15009 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15012 else if (tls_type == (TLS_TLS | TLS_DTPREL))
15013 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15014 else if (tls_type == (TLS_TLS | TLS_TPREL))
15015 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
15016 else if (indx != 0)
15017 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15021 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15023 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15025 /* Write the .got section contents for the sake
15027 loc = got->contents + off;
15028 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15032 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
15034 outrel.r_addend += relocation;
15035 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
15037 if (htab->elf.tls_sec == NULL)
15038 outrel.r_addend = 0;
15040 outrel.r_addend -= htab->elf.tls_sec->vma;
15043 loc = relgot->contents;
15044 loc += (relgot->reloc_count++
15045 * sizeof (Elf64_External_Rela));
15046 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15049 /* Init the .got section contents here if we're not
15050 emitting a reloc. */
15053 relocation += sym_addend;
15056 if (htab->elf.tls_sec == NULL)
15060 if (tls_type & TLS_LD)
15063 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
15064 if (tls_type & TLS_TPREL)
15065 relocation += DTP_OFFSET - TP_OFFSET;
15068 if (tls_type & (TLS_GD | TLS_LD))
15070 bfd_put_64 (output_bfd, relocation,
15071 got->contents + off + 8);
15075 bfd_put_64 (output_bfd, relocation,
15076 got->contents + off);
15080 if (off >= (bfd_vma) -2)
15083 relocation = got->output_section->vma + got->output_offset + off;
15084 if (r_type != R_PPC64_GOT_PCREL34)
15085 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
15089 case R_PPC64_PLT16_HA:
15090 case R_PPC64_PLT16_HI:
15091 case R_PPC64_PLT16_LO:
15092 case R_PPC64_PLT16_LO_DS:
15093 case R_PPC64_PLT_PCREL34:
15094 case R_PPC64_PLT_PCREL34_NOTOC:
15095 case R_PPC64_PLT32:
15096 case R_PPC64_PLT64:
15097 case R_PPC64_PLTSEQ:
15098 case R_PPC64_PLTSEQ_NOTOC:
15099 case R_PPC64_PLTCALL:
15100 case R_PPC64_PLTCALL_NOTOC:
15101 /* Relocation is to the entry for this symbol in the
15102 procedure linkage table. */
15103 unresolved_reloc = TRUE;
15105 struct plt_entry **plt_list = NULL;
15107 plt_list = &h->elf.plt.plist;
15108 else if (local_got_ents != NULL)
15110 struct plt_entry **local_plt = (struct plt_entry **)
15111 (local_got_ents + symtab_hdr->sh_info);
15112 plt_list = local_plt + r_symndx;
15116 struct plt_entry *ent;
15117 bfd_vma sym_addend = orig_rel.r_addend;
15119 if (r_type == R_PPC64_PLT_PCREL34
15120 || r_type == R_PPC64_PLT_PCREL34_NOTOC)
15123 for (ent = *plt_list; ent != NULL; ent = ent->next)
15124 if (ent->plt.offset != (bfd_vma) -1
15125 && ent->addend == sym_addend)
15130 plt = htab->elf.splt;
15131 if (!htab->elf.dynamic_sections_created
15133 || h->elf.dynindx == -1)
15136 ? h->elf.type == STT_GNU_IFUNC
15137 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15138 plt = htab->elf.iplt;
15140 plt = htab->pltlocal;
15142 relocation = (plt->output_section->vma
15143 + plt->output_offset
15144 + ent->plt.offset);
15145 if (r_type == R_PPC64_PLT16_HA
15146 || r_type == R_PPC64_PLT16_HI
15147 || r_type == R_PPC64_PLT16_LO
15148 || r_type == R_PPC64_PLT16_LO_DS)
15150 got = (elf_gp (output_bfd)
15151 + htab->sec_info[input_section->id].toc_off);
15154 if (r_type != R_PPC64_PLT_PCREL34
15155 && r_type != R_PPC64_PLT_PCREL34_NOTOC)
15157 unresolved_reloc = FALSE;
15165 /* Relocation value is TOC base. */
15166 relocation = TOCstart;
15167 if (r_symndx == STN_UNDEF)
15168 relocation += htab->sec_info[input_section->id].toc_off;
15169 else if (unresolved_reloc)
15171 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15172 relocation += htab->sec_info[sec->id].toc_off;
15174 unresolved_reloc = TRUE;
15177 /* TOC16 relocs. We want the offset relative to the TOC base,
15178 which is the address of the start of the TOC plus 0x8000.
15179 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15181 case R_PPC64_TOC16:
15182 case R_PPC64_TOC16_LO:
15183 case R_PPC64_TOC16_HI:
15184 case R_PPC64_TOC16_DS:
15185 case R_PPC64_TOC16_LO_DS:
15186 case R_PPC64_TOC16_HA:
15187 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
15190 /* Relocate against the beginning of the section. */
15191 case R_PPC64_SECTOFF:
15192 case R_PPC64_SECTOFF_LO:
15193 case R_PPC64_SECTOFF_HI:
15194 case R_PPC64_SECTOFF_DS:
15195 case R_PPC64_SECTOFF_LO_DS:
15196 case R_PPC64_SECTOFF_HA:
15198 addend -= sec->output_section->vma;
15201 case R_PPC64_REL16:
15202 case R_PPC64_REL16_LO:
15203 case R_PPC64_REL16_HI:
15204 case R_PPC64_REL16_HA:
15205 case R_PPC64_REL16_HIGH:
15206 case R_PPC64_REL16_HIGHA:
15207 case R_PPC64_REL16_HIGHER:
15208 case R_PPC64_REL16_HIGHERA:
15209 case R_PPC64_REL16_HIGHEST:
15210 case R_PPC64_REL16_HIGHESTA:
15211 case R_PPC64_REL16_HIGHER34:
15212 case R_PPC64_REL16_HIGHERA34:
15213 case R_PPC64_REL16_HIGHEST34:
15214 case R_PPC64_REL16_HIGHESTA34:
15215 case R_PPC64_REL16DX_HA:
15216 case R_PPC64_REL14:
15217 case R_PPC64_REL14_BRNTAKEN:
15218 case R_PPC64_REL14_BRTAKEN:
15219 case R_PPC64_REL24:
15220 case R_PPC64_REL24_NOTOC:
15221 case R_PPC64_PCREL34:
15222 case R_PPC64_PCREL28:
15225 case R_PPC64_TPREL16:
15226 case R_PPC64_TPREL16_LO:
15227 case R_PPC64_TPREL16_HI:
15228 case R_PPC64_TPREL16_HA:
15229 case R_PPC64_TPREL16_DS:
15230 case R_PPC64_TPREL16_LO_DS:
15231 case R_PPC64_TPREL16_HIGH:
15232 case R_PPC64_TPREL16_HIGHA:
15233 case R_PPC64_TPREL16_HIGHER:
15234 case R_PPC64_TPREL16_HIGHERA:
15235 case R_PPC64_TPREL16_HIGHEST:
15236 case R_PPC64_TPREL16_HIGHESTA:
15238 && h->elf.root.type == bfd_link_hash_undefweak
15239 && h->elf.dynindx == -1)
15241 /* Make this relocation against an undefined weak symbol
15242 resolve to zero. This is really just a tweak, since
15243 code using weak externs ought to check that they are
15244 defined before using them. */
15245 bfd_byte *p = contents + rel->r_offset - d_offset;
15247 insn = bfd_get_32 (input_bfd, p);
15248 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15250 bfd_put_32 (input_bfd, insn, p);
15253 if (htab->elf.tls_sec != NULL)
15254 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15255 /* The TPREL16 relocs shouldn't really be used in shared
15256 libs or with non-local symbols as that will result in
15257 DT_TEXTREL being set, but support them anyway. */
15260 case R_PPC64_DTPREL16:
15261 case R_PPC64_DTPREL16_LO:
15262 case R_PPC64_DTPREL16_HI:
15263 case R_PPC64_DTPREL16_HA:
15264 case R_PPC64_DTPREL16_DS:
15265 case R_PPC64_DTPREL16_LO_DS:
15266 case R_PPC64_DTPREL16_HIGH:
15267 case R_PPC64_DTPREL16_HIGHA:
15268 case R_PPC64_DTPREL16_HIGHER:
15269 case R_PPC64_DTPREL16_HIGHERA:
15270 case R_PPC64_DTPREL16_HIGHEST:
15271 case R_PPC64_DTPREL16_HIGHESTA:
15272 if (htab->elf.tls_sec != NULL)
15273 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15276 case R_PPC64_ADDR64_LOCAL:
15277 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15282 case R_PPC64_DTPMOD64:
15287 case R_PPC64_TPREL64:
15288 if (htab->elf.tls_sec != NULL)
15289 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15292 case R_PPC64_DTPREL64:
15293 if (htab->elf.tls_sec != NULL)
15294 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15295 /* Fall through. */
15297 /* Relocations that may need to be propagated if this is a
15299 case R_PPC64_REL30:
15300 case R_PPC64_REL32:
15301 case R_PPC64_REL64:
15302 case R_PPC64_ADDR14:
15303 case R_PPC64_ADDR14_BRNTAKEN:
15304 case R_PPC64_ADDR14_BRTAKEN:
15305 case R_PPC64_ADDR16:
15306 case R_PPC64_ADDR16_DS:
15307 case R_PPC64_ADDR16_HA:
15308 case R_PPC64_ADDR16_HI:
15309 case R_PPC64_ADDR16_HIGH:
15310 case R_PPC64_ADDR16_HIGHA:
15311 case R_PPC64_ADDR16_HIGHER:
15312 case R_PPC64_ADDR16_HIGHERA:
15313 case R_PPC64_ADDR16_HIGHEST:
15314 case R_PPC64_ADDR16_HIGHESTA:
15315 case R_PPC64_ADDR16_LO:
15316 case R_PPC64_ADDR16_LO_DS:
15317 case R_PPC64_ADDR16_HIGHER34:
15318 case R_PPC64_ADDR16_HIGHERA34:
15319 case R_PPC64_ADDR16_HIGHEST34:
15320 case R_PPC64_ADDR16_HIGHESTA34:
15321 case R_PPC64_ADDR24:
15322 case R_PPC64_ADDR32:
15323 case R_PPC64_ADDR64:
15324 case R_PPC64_UADDR16:
15325 case R_PPC64_UADDR32:
15326 case R_PPC64_UADDR64:
15328 case R_PPC64_D34_LO:
15329 case R_PPC64_D34_HI30:
15330 case R_PPC64_D34_HA30:
15333 if ((input_section->flags & SEC_ALLOC) == 0)
15336 if (NO_OPD_RELOCS && is_opd)
15339 if (bfd_link_pic (info)
15341 || h->dyn_relocs != NULL)
15342 && ((h != NULL && pc_dynrelocs (h))
15343 || must_be_dyn_reloc (info, r_type)))
15345 ? h->dyn_relocs != NULL
15346 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15348 bfd_boolean skip, relocate;
15353 /* When generating a dynamic object, these relocations
15354 are copied into the output file to be resolved at run
15360 out_off = _bfd_elf_section_offset (output_bfd, info,
15361 input_section, rel->r_offset);
15362 if (out_off == (bfd_vma) -1)
15364 else if (out_off == (bfd_vma) -2)
15365 skip = TRUE, relocate = TRUE;
15366 out_off += (input_section->output_section->vma
15367 + input_section->output_offset);
15368 outrel.r_offset = out_off;
15369 outrel.r_addend = rel->r_addend;
15371 /* Optimize unaligned reloc use. */
15372 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15373 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15374 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15375 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15376 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15377 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15378 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15379 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15380 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15383 memset (&outrel, 0, sizeof outrel);
15384 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15386 && r_type != R_PPC64_TOC)
15388 indx = h->elf.dynindx;
15389 BFD_ASSERT (indx != -1);
15390 outrel.r_info = ELF64_R_INFO (indx, r_type);
15394 /* This symbol is local, or marked to become local,
15395 or this is an opd section reloc which must point
15396 at a local function. */
15397 outrel.r_addend += relocation;
15398 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
15400 if (is_opd && h != NULL)
15402 /* Lie about opd entries. This case occurs
15403 when building shared libraries and we
15404 reference a function in another shared
15405 lib. The same thing happens for a weak
15406 definition in an application that's
15407 overridden by a strong definition in a
15408 shared lib. (I believe this is a generic
15409 bug in binutils handling of weak syms.)
15410 In these cases we won't use the opd
15411 entry in this lib. */
15412 unresolved_reloc = FALSE;
15415 && r_type == R_PPC64_ADDR64
15417 ? h->elf.type == STT_GNU_IFUNC
15418 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15419 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15422 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15424 /* We need to relocate .opd contents for ld.so.
15425 Prelink also wants simple and consistent rules
15426 for relocs. This make all RELATIVE relocs have
15427 *r_offset equal to r_addend. */
15434 ? h->elf.type == STT_GNU_IFUNC
15435 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15437 info->callbacks->einfo
15438 /* xgettext:c-format */
15439 (_("%H: %s for indirect "
15440 "function `%pT' unsupported\n"),
15441 input_bfd, input_section, rel->r_offset,
15442 ppc64_elf_howto_table[r_type]->name,
15446 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
15448 else if (sec == NULL || sec->owner == NULL)
15450 bfd_set_error (bfd_error_bad_value);
15457 osec = sec->output_section;
15458 indx = elf_section_data (osec)->dynindx;
15462 if ((osec->flags & SEC_READONLY) == 0
15463 && htab->elf.data_index_section != NULL)
15464 osec = htab->elf.data_index_section;
15466 osec = htab->elf.text_index_section;
15467 indx = elf_section_data (osec)->dynindx;
15469 BFD_ASSERT (indx != 0);
15471 /* We are turning this relocation into one
15472 against a section symbol, so subtract out
15473 the output section's address but not the
15474 offset of the input section in the output
15476 outrel.r_addend -= osec->vma;
15479 outrel.r_info = ELF64_R_INFO (indx, r_type);
15483 sreloc = elf_section_data (input_section)->sreloc;
15485 ? h->elf.type == STT_GNU_IFUNC
15486 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15488 sreloc = htab->elf.irelplt;
15490 htab->local_ifunc_resolver = 1;
15491 else if (is_static_defined (&h->elf))
15492 htab->maybe_local_ifunc_resolver = 1;
15494 if (sreloc == NULL)
15497 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15500 loc = sreloc->contents;
15501 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
15502 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15504 /* If this reloc is against an external symbol, it will
15505 be computed at runtime, so there's no need to do
15506 anything now. However, for the sake of prelink ensure
15507 that the section contents are a known value. */
15510 unresolved_reloc = FALSE;
15511 /* The value chosen here is quite arbitrary as ld.so
15512 ignores section contents except for the special
15513 case of .opd where the contents might be accessed
15514 before relocation. Choose zero, as that won't
15515 cause reloc overflow. */
15518 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15519 to improve backward compatibility with older
15521 if (r_type == R_PPC64_ADDR64)
15522 addend = outrel.r_addend;
15523 /* Adjust pc_relative relocs to have zero in *r_offset. */
15524 else if (ppc64_elf_howto_table[r_type]->pc_relative)
15525 addend = outrel.r_offset;
15531 case R_PPC64_GLOB_DAT:
15532 case R_PPC64_JMP_SLOT:
15533 case R_PPC64_JMP_IREL:
15534 case R_PPC64_RELATIVE:
15535 /* We shouldn't ever see these dynamic relocs in relocatable
15537 /* Fall through. */
15539 case R_PPC64_PLTGOT16:
15540 case R_PPC64_PLTGOT16_DS:
15541 case R_PPC64_PLTGOT16_HA:
15542 case R_PPC64_PLTGOT16_HI:
15543 case R_PPC64_PLTGOT16_LO:
15544 case R_PPC64_PLTGOT16_LO_DS:
15545 case R_PPC64_PLTREL32:
15546 case R_PPC64_PLTREL64:
15547 /* These ones haven't been implemented yet. */
15549 info->callbacks->einfo
15550 /* xgettext:c-format */
15551 (_("%P: %pB: %s is not supported for `%pT'\n"),
15553 ppc64_elf_howto_table[r_type]->name, sym_name);
15555 bfd_set_error (bfd_error_invalid_operation);
15560 /* Multi-instruction sequences that access the TOC can be
15561 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15562 to nop; addi rb,r2,x; */
15568 case R_PPC64_GOT_TLSLD16_HI:
15569 case R_PPC64_GOT_TLSGD16_HI:
15570 case R_PPC64_GOT_TPREL16_HI:
15571 case R_PPC64_GOT_DTPREL16_HI:
15572 case R_PPC64_GOT16_HI:
15573 case R_PPC64_TOC16_HI:
15574 /* These relocs would only be useful if building up an
15575 offset to later add to r2, perhaps in an indexed
15576 addressing mode instruction. Don't try to optimize.
15577 Unfortunately, the possibility of someone building up an
15578 offset like this or even with the HA relocs, means that
15579 we need to check the high insn when optimizing the low
15583 case R_PPC64_PLTCALL_NOTOC:
15584 if (!unresolved_reloc)
15585 htab->notoc_plt = 1;
15586 /* Fall through. */
15587 case R_PPC64_PLTCALL:
15588 if (unresolved_reloc)
15590 /* No plt entry. Make this into a direct call. */
15591 bfd_byte *p = contents + rel->r_offset;
15592 insn = bfd_get_32 (input_bfd, p);
15594 bfd_put_32 (input_bfd, B_DOT | insn, p);
15595 if (r_type == R_PPC64_PLTCALL)
15596 bfd_put_32 (input_bfd, NOP, p + 4);
15597 unresolved_reloc = save_unresolved_reloc;
15598 r_type = R_PPC64_REL24;
15602 case R_PPC64_PLTSEQ_NOTOC:
15603 case R_PPC64_PLTSEQ:
15604 if (unresolved_reloc)
15606 unresolved_reloc = FALSE;
15611 case R_PPC64_PLT_PCREL34_NOTOC:
15612 if (!unresolved_reloc)
15613 htab->notoc_plt = 1;
15614 /* Fall through. */
15615 case R_PPC64_PLT_PCREL34:
15616 if (unresolved_reloc)
15618 bfd_byte *p = contents + rel->r_offset;
15619 bfd_put_32 (input_bfd, PNOP >> 32, p);
15620 bfd_put_32 (input_bfd, PNOP, p + 4);
15621 unresolved_reloc = FALSE;
15626 case R_PPC64_PLT16_HA:
15627 if (unresolved_reloc)
15629 unresolved_reloc = FALSE;
15632 /* Fall through. */
15633 case R_PPC64_GOT_TLSLD16_HA:
15634 case R_PPC64_GOT_TLSGD16_HA:
15635 case R_PPC64_GOT_TPREL16_HA:
15636 case R_PPC64_GOT_DTPREL16_HA:
15637 case R_PPC64_GOT16_HA:
15638 case R_PPC64_TOC16_HA:
15639 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15640 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15644 p = contents + (rel->r_offset & ~3);
15645 bfd_put_32 (input_bfd, NOP, p);
15650 case R_PPC64_PLT16_LO:
15651 case R_PPC64_PLT16_LO_DS:
15652 if (unresolved_reloc)
15654 unresolved_reloc = FALSE;
15657 /* Fall through. */
15658 case R_PPC64_GOT_TLSLD16_LO:
15659 case R_PPC64_GOT_TLSGD16_LO:
15660 case R_PPC64_GOT_TPREL16_LO_DS:
15661 case R_PPC64_GOT_DTPREL16_LO_DS:
15662 case R_PPC64_GOT16_LO:
15663 case R_PPC64_GOT16_LO_DS:
15664 case R_PPC64_TOC16_LO:
15665 case R_PPC64_TOC16_LO_DS:
15666 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15667 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15669 bfd_byte *p = contents + (rel->r_offset & ~3);
15670 insn = bfd_get_32 (input_bfd, p);
15671 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15673 /* Transform addic to addi when we change reg. */
15674 insn &= ~((0x3f << 26) | (0x1f << 16));
15675 insn |= (14u << 26) | (2 << 16);
15679 insn &= ~(0x1f << 16);
15682 bfd_put_32 (input_bfd, insn, p);
15686 case R_PPC64_TPREL16_HA:
15687 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15689 bfd_byte *p = contents + (rel->r_offset & ~3);
15690 insn = bfd_get_32 (input_bfd, p);
15691 if ((insn & ((0x3f << 26) | 0x1f << 16))
15692 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15693 /* xgettext:c-format */
15694 info->callbacks->minfo
15695 (_("%H: warning: %s unexpected insn %#x.\n"),
15696 input_bfd, input_section, rel->r_offset,
15697 ppc64_elf_howto_table[r_type]->name, insn);
15700 bfd_put_32 (input_bfd, NOP, p);
15706 case R_PPC64_TPREL16_LO:
15707 case R_PPC64_TPREL16_LO_DS:
15708 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15710 bfd_byte *p = contents + (rel->r_offset & ~3);
15711 insn = bfd_get_32 (input_bfd, p);
15712 insn &= ~(0x1f << 16);
15714 bfd_put_32 (input_bfd, insn, p);
15719 /* Do any further special processing. */
15725 case R_PPC64_REL16_HA:
15726 case R_PPC64_REL16_HIGHA:
15727 case R_PPC64_REL16_HIGHERA:
15728 case R_PPC64_REL16_HIGHESTA:
15729 case R_PPC64_REL16DX_HA:
15730 case R_PPC64_ADDR16_HA:
15731 case R_PPC64_ADDR16_HIGHA:
15732 case R_PPC64_ADDR16_HIGHERA:
15733 case R_PPC64_ADDR16_HIGHESTA:
15734 case R_PPC64_TOC16_HA:
15735 case R_PPC64_SECTOFF_HA:
15736 case R_PPC64_TPREL16_HA:
15737 case R_PPC64_TPREL16_HIGHA:
15738 case R_PPC64_TPREL16_HIGHERA:
15739 case R_PPC64_TPREL16_HIGHESTA:
15740 case R_PPC64_DTPREL16_HA:
15741 case R_PPC64_DTPREL16_HIGHA:
15742 case R_PPC64_DTPREL16_HIGHERA:
15743 case R_PPC64_DTPREL16_HIGHESTA:
15744 /* It's just possible that this symbol is a weak symbol
15745 that's not actually defined anywhere. In that case,
15746 'sec' would be NULL, and we should leave the symbol
15747 alone (it will be set to zero elsewhere in the link). */
15750 /* Fall through. */
15752 case R_PPC64_GOT16_HA:
15753 case R_PPC64_PLTGOT16_HA:
15754 case R_PPC64_PLT16_HA:
15755 case R_PPC64_GOT_TLSGD16_HA:
15756 case R_PPC64_GOT_TLSLD16_HA:
15757 case R_PPC64_GOT_TPREL16_HA:
15758 case R_PPC64_GOT_DTPREL16_HA:
15759 /* Add 0x10000 if sign bit in 0:15 is set.
15760 Bits 0:15 are not used. */
15764 case R_PPC64_D34_HA30:
15765 case R_PPC64_ADDR16_HIGHERA34:
15766 case R_PPC64_ADDR16_HIGHESTA34:
15767 case R_PPC64_REL16_HIGHERA34:
15768 case R_PPC64_REL16_HIGHESTA34:
15770 addend += 1ULL << 33;
15773 case R_PPC64_ADDR16_DS:
15774 case R_PPC64_ADDR16_LO_DS:
15775 case R_PPC64_GOT16_DS:
15776 case R_PPC64_GOT16_LO_DS:
15777 case R_PPC64_PLT16_LO_DS:
15778 case R_PPC64_SECTOFF_DS:
15779 case R_PPC64_SECTOFF_LO_DS:
15780 case R_PPC64_TOC16_DS:
15781 case R_PPC64_TOC16_LO_DS:
15782 case R_PPC64_PLTGOT16_DS:
15783 case R_PPC64_PLTGOT16_LO_DS:
15784 case R_PPC64_GOT_TPREL16_DS:
15785 case R_PPC64_GOT_TPREL16_LO_DS:
15786 case R_PPC64_GOT_DTPREL16_DS:
15787 case R_PPC64_GOT_DTPREL16_LO_DS:
15788 case R_PPC64_TPREL16_DS:
15789 case R_PPC64_TPREL16_LO_DS:
15790 case R_PPC64_DTPREL16_DS:
15791 case R_PPC64_DTPREL16_LO_DS:
15792 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15794 /* If this reloc is against an lq, lxv, or stxv insn, then
15795 the value must be a multiple of 16. This is somewhat of
15796 a hack, but the "correct" way to do this by defining _DQ
15797 forms of all the _DS relocs bloats all reloc switches in
15798 this file. It doesn't make much sense to use these
15799 relocs in data, so testing the insn should be safe. */
15800 if ((insn & (0x3f << 26)) == (56u << 26)
15801 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15803 relocation += addend;
15804 addend = insn & (mask ^ 3);
15805 if ((relocation & mask) != 0)
15807 relocation ^= relocation & mask;
15808 info->callbacks->einfo
15809 /* xgettext:c-format */
15810 (_("%H: error: %s not a multiple of %u\n"),
15811 input_bfd, input_section, rel->r_offset,
15812 ppc64_elf_howto_table[r_type]->name,
15814 bfd_set_error (bfd_error_bad_value);
15821 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15822 because such sections are not SEC_ALLOC and thus ld.so will
15823 not process them. */
15824 howto = ppc64_elf_howto_table[(int) r_type];
15825 if (unresolved_reloc
15826 && !((input_section->flags & SEC_DEBUGGING) != 0
15827 && h->elf.def_dynamic)
15828 && _bfd_elf_section_offset (output_bfd, info, input_section,
15829 rel->r_offset) != (bfd_vma) -1)
15831 info->callbacks->einfo
15832 /* xgettext:c-format */
15833 (_("%H: unresolvable %s against `%pT'\n"),
15834 input_bfd, input_section, rel->r_offset,
15836 h->elf.root.root.string);
15840 /* 16-bit fields in insns mostly have signed values, but a
15841 few insns have 16-bit unsigned values. Really, we should
15842 have different reloc types. */
15843 if (howto->complain_on_overflow != complain_overflow_dont
15844 && howto->dst_mask == 0xffff
15845 && (input_section->flags & SEC_CODE) != 0)
15847 enum complain_overflow complain = complain_overflow_signed;
15849 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15850 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15851 complain = complain_overflow_bitfield;
15852 else if (howto->rightshift == 0
15853 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15854 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15855 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15856 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15857 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15858 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15859 complain = complain_overflow_unsigned;
15860 if (howto->complain_on_overflow != complain)
15862 alt_howto = *howto;
15863 alt_howto.complain_on_overflow = complain;
15864 howto = &alt_howto;
15870 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
15872 case R_PPC64_D34_LO:
15873 case R_PPC64_D34_HI30:
15874 case R_PPC64_D34_HA30:
15875 case R_PPC64_PCREL34:
15876 case R_PPC64_GOT_PCREL34:
15877 case R_PPC64_PLT_PCREL34:
15878 case R_PPC64_PLT_PCREL34_NOTOC:
15880 case R_PPC64_PCREL28:
15881 if (rel->r_offset + 8 > input_section->size)
15882 r = bfd_reloc_outofrange;
15887 relocation += addend;
15888 if (howto->pc_relative)
15889 relocation -= (rel->r_offset
15890 + input_section->output_offset
15891 + input_section->output_section->vma);
15892 relocation >>= howto->rightshift;
15894 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15896 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15898 pinsn &= ~howto->dst_mask;
15899 pinsn |= (((relocation << 16) | (relocation & 0xffff))
15900 & howto->dst_mask);
15901 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
15902 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
15904 if (howto->complain_on_overflow == complain_overflow_signed
15905 && (relocation + (1ULL << (howto->bitsize - 1))
15906 >= 1ULL << howto->bitsize))
15907 r = bfd_reloc_overflow;
15911 case R_PPC64_REL16DX_HA:
15912 if (rel->r_offset + 4 > input_section->size)
15913 r = bfd_reloc_outofrange;
15916 relocation += addend;
15917 relocation -= (rel->r_offset
15918 + input_section->output_offset
15919 + input_section->output_section->vma);
15920 relocation = (bfd_signed_vma) relocation >> 16;
15921 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15923 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15924 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15926 if (relocation + 0x8000 > 0xffff)
15927 r = bfd_reloc_overflow;
15932 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
15933 contents, rel->r_offset,
15934 relocation, addend);
15937 if (r != bfd_reloc_ok)
15939 char *more_info = NULL;
15940 const char *reloc_name = howto->name;
15942 if (reloc_dest != DEST_NORMAL)
15944 more_info = bfd_malloc (strlen (reloc_name) + 8);
15945 if (more_info != NULL)
15947 strcpy (more_info, reloc_name);
15948 strcat (more_info, (reloc_dest == DEST_OPD
15949 ? " (OPD)" : " (stub)"));
15950 reloc_name = more_info;
15954 if (r == bfd_reloc_overflow)
15956 /* On code like "if (foo) foo();" don't report overflow
15957 on a branch to zero when foo is undefined. */
15959 && (reloc_dest == DEST_STUB
15961 && (h->elf.root.type == bfd_link_hash_undefweak
15962 || h->elf.root.type == bfd_link_hash_undefined)
15963 && is_branch_reloc (r_type))))
15964 info->callbacks->reloc_overflow (info, &h->elf.root,
15965 sym_name, reloc_name,
15967 input_bfd, input_section,
15972 info->callbacks->einfo
15973 /* xgettext:c-format */
15974 (_("%H: %s against `%pT': error %d\n"),
15975 input_bfd, input_section, rel->r_offset,
15976 reloc_name, sym_name, (int) r);
15979 if (more_info != NULL)
15989 Elf_Internal_Shdr *rel_hdr;
15990 size_t deleted = rel - wrel;
15992 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15993 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15994 if (rel_hdr->sh_size == 0)
15996 /* It is too late to remove an empty reloc section. Leave
15998 ??? What is wrong with an empty section??? */
15999 rel_hdr->sh_size = rel_hdr->sh_entsize;
16002 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16003 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16004 input_section->reloc_count -= deleted;
16007 /* If we're emitting relocations, then shortly after this function
16008 returns, reloc offsets and addends for this section will be
16009 adjusted. Worse, reloc symbol indices will be for the output
16010 file rather than the input. Save a copy of the relocs for
16011 opd_entry_value. */
16012 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
16015 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16016 rel = bfd_alloc (input_bfd, amt);
16017 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16018 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
16021 memcpy (rel, relocs, amt);
16026 /* Adjust the value of any local symbols in opd sections. */
16029 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16030 const char *name ATTRIBUTE_UNUSED,
16031 Elf_Internal_Sym *elfsym,
16032 asection *input_sec,
16033 struct elf_link_hash_entry *h)
16035 struct _opd_sec_data *opd;
16042 opd = get_opd_info (input_sec);
16043 if (opd == NULL || opd->adjust == NULL)
16046 value = elfsym->st_value - input_sec->output_offset;
16047 if (!bfd_link_relocatable (info))
16048 value -= input_sec->output_section->vma;
16050 adjust = opd->adjust[OPD_NDX (value)];
16054 elfsym->st_value += adjust;
16058 /* Finish up dynamic symbol handling. We set the contents of various
16059 dynamic sections here. */
16062 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16063 struct bfd_link_info *info,
16064 struct elf_link_hash_entry *h,
16065 Elf_Internal_Sym *sym)
16067 struct ppc_link_hash_table *htab;
16068 struct plt_entry *ent;
16070 htab = ppc_hash_table (info);
16074 if (!htab->opd_abi && !h->def_regular)
16075 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16076 if (ent->plt.offset != (bfd_vma) -1)
16078 /* Mark the symbol as undefined, rather than as
16079 defined in glink. Leave the value if there were
16080 any relocations where pointer equality matters
16081 (this is a clue for the dynamic linker, to make
16082 function pointer comparisons work between an
16083 application and shared library), otherwise set it
16085 sym->st_shndx = SHN_UNDEF;
16086 if (!h->pointer_equality_needed)
16088 else if (!h->ref_regular_nonweak)
16090 /* This breaks function pointer comparisons, but
16091 that is better than breaking tests for a NULL
16092 function pointer. */
16100 /* This symbol needs a copy reloc. Set it up. */
16101 Elf_Internal_Rela rela;
16105 if (h->dynindx == -1
16106 || (h->root.type != bfd_link_hash_defined
16107 && h->root.type != bfd_link_hash_defweak)
16108 || htab->elf.srelbss == NULL
16109 || htab->elf.sreldynrelro == NULL)
16112 rela.r_offset = (h->root.u.def.value
16113 + h->root.u.def.section->output_section->vma
16114 + h->root.u.def.section->output_offset);
16115 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16117 if (h->root.u.def.section == htab->elf.sdynrelro)
16118 srel = htab->elf.sreldynrelro;
16120 srel = htab->elf.srelbss;
16121 loc = srel->contents;
16122 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
16123 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
16129 /* Used to decide how to sort relocs in an optimal manner for the
16130 dynamic linker, before writing them out. */
16132 static enum elf_reloc_type_class
16133 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16134 const asection *rel_sec,
16135 const Elf_Internal_Rela *rela)
16137 enum elf_ppc64_reloc_type r_type;
16138 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16140 if (rel_sec == htab->elf.irelplt)
16141 return reloc_class_ifunc;
16143 r_type = ELF64_R_TYPE (rela->r_info);
16146 case R_PPC64_RELATIVE:
16147 return reloc_class_relative;
16148 case R_PPC64_JMP_SLOT:
16149 return reloc_class_plt;
16151 return reloc_class_copy;
16153 return reloc_class_normal;
16157 /* Finish up the dynamic sections. */
16160 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16161 struct bfd_link_info *info)
16163 struct ppc_link_hash_table *htab;
16167 htab = ppc_hash_table (info);
16171 dynobj = htab->elf.dynobj;
16172 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
16174 if (htab->elf.dynamic_sections_created)
16176 Elf64_External_Dyn *dyncon, *dynconend;
16178 if (sdyn == NULL || htab->elf.sgot == NULL)
16181 dyncon = (Elf64_External_Dyn *) sdyn->contents;
16182 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
16183 for (; dyncon < dynconend; dyncon++)
16185 Elf_Internal_Dyn dyn;
16188 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16195 case DT_PPC64_GLINK:
16197 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16198 /* We stupidly defined DT_PPC64_GLINK to be the start
16199 of glink rather than the first entry point, which is
16200 what ld.so needs, and now have a bigger stub to
16201 support automatic multiple TOCs. */
16202 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
16206 s = bfd_get_section_by_name (output_bfd, ".opd");
16209 dyn.d_un.d_ptr = s->vma;
16213 if ((htab->do_multi_toc && htab->multi_toc_needed)
16214 || htab->notoc_plt)
16215 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
16216 if (htab->has_plt_localentry0)
16217 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
16220 case DT_PPC64_OPDSZ:
16221 s = bfd_get_section_by_name (output_bfd, ".opd");
16224 dyn.d_un.d_val = s->size;
16228 s = htab->elf.splt;
16229 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16233 s = htab->elf.srelplt;
16234 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16238 dyn.d_un.d_val = htab->elf.srelplt->size;
16242 if (htab->local_ifunc_resolver)
16243 info->callbacks->einfo
16244 (_("%X%P: text relocations and GNU indirect "
16245 "functions will result in a segfault at runtime\n"));
16246 else if (htab->maybe_local_ifunc_resolver)
16247 info->callbacks->einfo
16248 (_("%P: warning: text relocations and GNU indirect "
16249 "functions may result in a segfault at runtime\n"));
16253 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
16257 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16258 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
16260 /* Fill in the first entry in the global offset table.
16261 We use it to hold the link-time TOCbase. */
16262 bfd_put_64 (output_bfd,
16263 elf_gp (output_bfd) + TOC_BASE_OFF,
16264 htab->elf.sgot->contents);
16266 /* Set .got entry size. */
16267 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
16271 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16272 && htab->elf.splt->output_section != bfd_abs_section_ptr)
16274 /* Set .plt entry size. */
16275 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
16276 = PLT_ENTRY_SIZE (htab);
16279 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16280 brlt ourselves if emitrelocations. */
16281 if (htab->brlt != NULL
16282 && htab->brlt->reloc_count != 0
16283 && !_bfd_elf_link_output_relocs (output_bfd,
16285 elf_section_data (htab->brlt)->rela.hdr,
16286 elf_section_data (htab->brlt)->relocs,
16290 if (htab->glink != NULL
16291 && htab->glink->reloc_count != 0
16292 && !_bfd_elf_link_output_relocs (output_bfd,
16294 elf_section_data (htab->glink)->rela.hdr,
16295 elf_section_data (htab->glink)->relocs,
16300 if (htab->glink_eh_frame != NULL
16301 && htab->glink_eh_frame->size != 0
16302 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16303 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16304 htab->glink_eh_frame,
16305 htab->glink_eh_frame->contents))
16308 /* We need to handle writing out multiple GOT sections ourselves,
16309 since we didn't add them to DYNOBJ. We know dynobj is the first
16311 while ((dynobj = dynobj->link.next) != NULL)
16315 if (!is_ppc64_elf (dynobj))
16318 s = ppc64_elf_tdata (dynobj)->got;
16321 && s->output_section != bfd_abs_section_ptr
16322 && !bfd_set_section_contents (output_bfd, s->output_section,
16323 s->contents, s->output_offset,
16326 s = ppc64_elf_tdata (dynobj)->relgot;
16329 && s->output_section != bfd_abs_section_ptr
16330 && !bfd_set_section_contents (output_bfd, s->output_section,
16331 s->contents, s->output_offset,
16339 #include "elf64-target.h"
16341 /* FreeBSD support */
16343 #undef TARGET_LITTLE_SYM
16344 #undef TARGET_LITTLE_NAME
16346 #undef TARGET_BIG_SYM
16347 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
16348 #undef TARGET_BIG_NAME
16349 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16352 #define ELF_OSABI ELFOSABI_FREEBSD
16355 #define elf64_bed elf64_powerpc_fbsd_bed
16357 #include "elf64-target.h"