Add translations for various sub-directories
[binutils-gdb.git] / bfd / elf64-ppc.c
blob65415bd39c7efb3087eab185da6279a55915f48e
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2025 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 */
28 /* The assembler should generate a full set of section symbols even
29 when they appear unused. The linux kernel build tool recordmcount
30 needs them. */
31 #define TARGET_KEEP_UNUSED_SECTION_SYMBOLS true
33 #include "sysdep.h"
34 #include <stdarg.h>
35 #include "bfd.h"
36 #include "bfdlink.h"
37 #include "libbfd.h"
38 #include "elf-bfd.h"
39 #include "elf/ppc64.h"
40 #include "elf64-ppc.h"
41 #include "dwarf2.h"
43 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
44 #define OCTETS_PER_BYTE(ABFD, SEC) 1
46 static bfd_reloc_status_type ppc64_elf_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_branch_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_reloc_status_type ppc64_elf_toc_reloc
57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
59 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
60 static bfd_reloc_status_type ppc64_elf_toc64_reloc
61 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
62 static bfd_reloc_status_type ppc64_elf_prefix_reloc
63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
64 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
65 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
66 static bfd_vma opd_entry_value
67 (asection *, bfd_vma, asection **, bfd_vma *, bool);
69 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
70 #define TARGET_LITTLE_NAME "elf64-powerpcle"
71 #define TARGET_BIG_SYM powerpc_elf64_vec
72 #define TARGET_BIG_NAME "elf64-powerpc"
73 #define ELF_ARCH bfd_arch_powerpc
74 #define ELF_TARGET_ID PPC64_ELF_DATA
75 #define ELF_MACHINE_CODE EM_PPC64
76 #define ELF_MAXPAGESIZE 0x10000
77 #define ELF_COMMONPAGESIZE 0x1000
78 #define elf_info_to_howto ppc64_elf_info_to_howto
80 #define elf_backend_want_got_sym 0
81 #define elf_backend_want_plt_sym 0
82 #define elf_backend_plt_alignment 3
83 #define elf_backend_plt_not_loaded 1
84 #define elf_backend_got_header_size 8
85 #define elf_backend_want_dynrelro 1
86 #define elf_backend_can_gc_sections 1
87 #define elf_backend_can_refcount 1
88 #define elf_backend_rela_normal 1
89 #define elf_backend_dtrel_excludes_plt 1
90 #define elf_backend_default_execstack 0
92 #define bfd_elf64_mkobject ppc64_elf_mkobject
93 #define bfd_elf64_bfd_free_cached_info ppc64_elf_free_cached_info
94 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
95 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
96 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
97 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
98 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
99 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
100 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
101 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
102 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
104 #define elf_backend_object_p ppc64_elf_object_p
105 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
106 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
107 #define elf_backend_write_core_note ppc64_elf_write_core_note
108 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
109 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
110 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
111 #define elf_backend_check_directives ppc64_elf_before_check_relocs
112 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
113 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
114 #define elf_backend_check_relocs ppc64_elf_check_relocs
115 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
116 #define elf_backend_gc_keep ppc64_elf_gc_keep
117 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
118 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
119 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
120 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
121 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
122 #define elf_backend_early_size_sections ppc64_elf_edit
123 #define elf_backend_late_size_sections ppc64_elf_late_size_sections
124 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
125 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
126 #define elf_backend_action_discarded ppc64_elf_action_discarded
127 #define elf_backend_relocate_section ppc64_elf_relocate_section
128 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
129 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
130 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
131 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
132 #define elf_backend_special_sections ppc64_elf_special_sections
133 #define elf_backend_section_flags ppc64_elf_section_flags
134 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
135 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
136 #define elf_backend_get_reloc_section bfd_get_section_by_name
138 /* The name of the dynamic interpreter. This is put in the .interp
139 section. */
140 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
142 /* The size in bytes of an entry in the procedure linkage table. */
143 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
144 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
146 /* The initial size of the plt reserved for the dynamic linker. */
147 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
149 /* Offsets to some stack save slots. */
150 #define STK_LR 16
151 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
152 /* This one is dodgy. ELFv2 does not have a linker word, so use the
153 CR save slot. Used only by optimised __tls_get_addr call stub,
154 relying on __tls_get_addr_opt not saving CR.. */
155 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
157 /* TOC base pointers offset from start of TOC. */
158 #define TOC_BASE_OFF 0x8000
159 /* TOC base alignment. */
160 #define TOC_BASE_ALIGN 256
162 /* Offset of tp and dtp pointers from start of TLS block. */
163 #define TP_OFFSET 0x7000
164 #define DTP_OFFSET 0x8000
166 /* .plt call stub instructions. The normal stub is like this, but
167 sometimes the .plt entry crosses a 64k boundary and we need to
168 insert an addi to adjust r11. */
169 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
170 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
171 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
172 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
173 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
174 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
175 #define BCTR 0x4e800420 /* bctr */
177 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
178 #define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
179 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
180 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
181 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
183 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
184 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
185 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
186 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
187 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
188 #define BNECTR 0x4ca20420 /* bnectr+ */
189 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
191 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
192 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
193 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
195 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
196 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
197 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
199 #define LI_R11_0 0x39600000 /* li %r11,0 */
200 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
201 #define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
202 #define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
203 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
204 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
205 #define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
206 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
207 #define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
208 #define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
209 #define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
210 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
211 #define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
212 #define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
213 #define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
214 #define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
215 #define PADDI_R12_PC 0x0610000039800000ULL
216 #define PLD_R12_PC 0x04100000e5800000ULL
217 #define PNOP 0x0700000000000000ULL
219 /* __glink_PLTresolve stub instructions. We enter with the index in
220 R0 for ELFv1, and the address of a glink branch in R12 for ELFv2. */
221 #define GLINK_PLTRESOLVE_SIZE(htab) \
222 (8u + (htab->opd_abi ? 11 * 4 : htab->has_plt_localentry0 ? 14 * 4 : 13 * 4))
223 /* 0: */
224 /* .quad plt0-1f */
225 /* __glink: */
226 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
227 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
228 /* 1: */
229 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
230 /* ld %2,(0b-1b)(%11) */
231 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
232 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
233 /* ld %12,0(%11) */
234 /* ld %2,8(%11) */
235 /* mtctr %12 */
236 /* ld %11,16(%11) */
237 /* bctr */
239 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
240 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
241 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
242 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
243 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
244 #define LD_R0_0R11 0xe80b0000 /* ld %r0,0(%r11) */
245 #define ADD_R11_R0_R11 0x7d605a14 /* add %r11,%r0,%r11 */
247 /* Pad with this. */
248 #define NOP 0x60000000
250 /* Some other nops. */
251 #define CROR_151515 0x4def7b82
252 #define CROR_313131 0x4ffffb82
254 /* .glink entries for the first 32k functions are two instructions. */
255 #define LI_R0_0 0x38000000 /* li %r0,0 */
256 #define B_DOT 0x48000000 /* b . */
258 /* After that, we need two instructions to load the index, followed by
259 a branch. */
260 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
261 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
263 /* Instructions used by the save and restore reg functions. */
264 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
265 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
266 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
267 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
268 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
269 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
270 #define LI_R12_0 0x39800000 /* li %r12,0 */
271 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
272 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
273 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
274 #define BLR 0x4e800020 /* blr */
276 /* Since .opd is an array of descriptors and each entry will end up
277 with identical R_PPC64_RELATIVE relocs, there is really no need to
278 propagate .opd relocs; The dynamic linker should be taught to
279 relocate .opd without reloc entries. */
280 #ifndef NO_OPD_RELOCS
281 #define NO_OPD_RELOCS 0
282 #endif
284 #ifndef ARRAY_SIZE
285 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
286 #endif
288 static inline int
289 abiversion (bfd *abfd)
291 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
294 static inline void
295 set_abiversion (bfd *abfd, int ver)
297 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
298 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
301 #define is_ppc64_elf(bfd) \
302 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
303 && elf_object_id (bfd) == PPC64_ELF_DATA)
305 /* Relocation HOWTO's. */
306 /* Like other ELF RELA targets that don't apply multiple
307 field-altering relocations to the same localation, src_mask is
308 always zero and pcrel_offset is the same as pc_relative.
309 PowerPC can always use a zero bitpos, even when the field is not at
310 the LSB. For example, a REL24 could use rightshift=2, bisize=24
311 and bitpos=2 which matches the ABI description, or as we do here,
312 rightshift=0, bitsize=26 and bitpos=0. */
313 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
314 complain, special_func) \
315 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
316 complain_overflow_ ## complain, special_func, \
317 #type, false, 0, mask, pc_relative)
319 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
321 static reloc_howto_type ppc64_elf_howto_raw[] =
323 /* This reloc does nothing. */
324 HOW (R_PPC64_NONE, 0, 0, 0, 0, false, dont,
325 bfd_elf_generic_reloc),
327 /* A standard 32 bit relocation. */
328 HOW (R_PPC64_ADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
329 bfd_elf_generic_reloc),
331 /* An absolute 26 bit branch; the lower two bits must be zero.
332 FIXME: we don't check that, we just clear them. */
333 HOW (R_PPC64_ADDR24, 4, 26, 0x03fffffc, 0, false, bitfield,
334 bfd_elf_generic_reloc),
336 /* A standard 16 bit relocation. */
337 HOW (R_PPC64_ADDR16, 2, 16, 0xffff, 0, false, bitfield,
338 bfd_elf_generic_reloc),
340 /* A 16 bit relocation without overflow. */
341 HOW (R_PPC64_ADDR16_LO, 2, 16, 0xffff, 0, false, dont,
342 bfd_elf_generic_reloc),
344 /* Bits 16-31 of an address. */
345 HOW (R_PPC64_ADDR16_HI, 2, 16, 0xffff, 16, false, signed,
346 bfd_elf_generic_reloc),
348 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
349 bits, treated as a signed number, is negative. */
350 HOW (R_PPC64_ADDR16_HA, 2, 16, 0xffff, 16, false, signed,
351 ppc64_elf_ha_reloc),
353 /* An absolute 16 bit branch; the lower two bits must be zero.
354 FIXME: we don't check that, we just clear them. */
355 HOW (R_PPC64_ADDR14, 4, 16, 0x0000fffc, 0, false, signed,
356 ppc64_elf_branch_reloc),
358 /* An absolute 16 bit branch, for which bit 10 should be set to
359 indicate that the branch is expected to be taken. The lower two
360 bits must be zero. */
361 HOW (R_PPC64_ADDR14_BRTAKEN, 4, 16, 0x0000fffc, 0, false, signed,
362 ppc64_elf_brtaken_reloc),
364 /* An absolute 16 bit branch, for which bit 10 should be set to
365 indicate that the branch is not expected to be taken. The lower
366 two bits must be zero. */
367 HOW (R_PPC64_ADDR14_BRNTAKEN, 4, 16, 0x0000fffc, 0, false, signed,
368 ppc64_elf_brtaken_reloc),
370 /* A relative 26 bit branch; the lower two bits must be zero. */
371 HOW (R_PPC64_REL24, 4, 26, 0x03fffffc, 0, true, signed,
372 ppc64_elf_branch_reloc),
374 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
375 HOW (R_PPC64_REL24_NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
376 ppc64_elf_branch_reloc),
378 /* Another variant, when p10 insns can't be used on stubs. */
379 HOW (R_PPC64_REL24_P9NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
380 ppc64_elf_branch_reloc),
382 /* A relative 16 bit branch; the lower two bits must be zero. */
383 HOW (R_PPC64_REL14, 4, 16, 0x0000fffc, 0, true, signed,
384 ppc64_elf_branch_reloc),
386 /* A relative 16 bit branch. Bit 10 should be set to indicate that
387 the branch is expected to be taken. The lower two bits must be
388 zero. */
389 HOW (R_PPC64_REL14_BRTAKEN, 4, 16, 0x0000fffc, 0, true, signed,
390 ppc64_elf_brtaken_reloc),
392 /* A relative 16 bit branch. Bit 10 should be set to indicate that
393 the branch is not expected to be taken. The lower two bits must
394 be zero. */
395 HOW (R_PPC64_REL14_BRNTAKEN, 4, 16, 0x0000fffc, 0, true, signed,
396 ppc64_elf_brtaken_reloc),
398 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
399 symbol. */
400 HOW (R_PPC64_GOT16, 2, 16, 0xffff, 0, false, signed,
401 ppc64_elf_unhandled_reloc),
403 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
404 the symbol. */
405 HOW (R_PPC64_GOT16_LO, 2, 16, 0xffff, 0, false, dont,
406 ppc64_elf_unhandled_reloc),
408 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
409 the symbol. */
410 HOW (R_PPC64_GOT16_HI, 2, 16, 0xffff, 16, false, signed,
411 ppc64_elf_unhandled_reloc),
413 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
414 the symbol. */
415 HOW (R_PPC64_GOT16_HA, 2, 16, 0xffff, 16, false, signed,
416 ppc64_elf_unhandled_reloc),
418 /* This is used only by the dynamic linker. The symbol should exist
419 both in the object being run and in some shared library. The
420 dynamic linker copies the data addressed by the symbol from the
421 shared library into the object, because the object being
422 run has to have the data at some particular address. */
423 HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
424 ppc64_elf_unhandled_reloc),
426 /* Like R_PPC64_ADDR64, but used when setting global offset table
427 entries. */
428 HOW (R_PPC64_GLOB_DAT, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
429 ppc64_elf_unhandled_reloc),
431 /* Created by the link editor. Marks a procedure linkage table
432 entry for a symbol. */
433 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
434 ppc64_elf_unhandled_reloc),
436 /* Used only by the dynamic linker. When the object is run, this
437 doubleword64 is set to the load address of the object, plus the
438 addend. */
439 HOW (R_PPC64_RELATIVE, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
440 bfd_elf_generic_reloc),
442 /* Like R_PPC64_ADDR32, but may be unaligned. */
443 HOW (R_PPC64_UADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
444 bfd_elf_generic_reloc),
446 /* Like R_PPC64_ADDR16, but may be unaligned. */
447 HOW (R_PPC64_UADDR16, 2, 16, 0xffff, 0, false, bitfield,
448 bfd_elf_generic_reloc),
450 /* 32-bit PC relative. */
451 HOW (R_PPC64_REL32, 4, 32, 0xffffffff, 0, true, signed,
452 bfd_elf_generic_reloc),
454 /* 32-bit relocation to the symbol's procedure linkage table. */
455 HOW (R_PPC64_PLT32, 4, 32, 0xffffffff, 0, false, bitfield,
456 ppc64_elf_unhandled_reloc),
458 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
459 FIXME: R_PPC64_PLTREL32 not supported. */
460 HOW (R_PPC64_PLTREL32, 4, 32, 0xffffffff, 0, true, signed,
461 ppc64_elf_unhandled_reloc),
463 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
464 the symbol. */
465 HOW (R_PPC64_PLT16_LO, 2, 16, 0xffff, 0, false, dont,
466 ppc64_elf_unhandled_reloc),
468 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
469 the symbol. */
470 HOW (R_PPC64_PLT16_HI, 2, 16, 0xffff, 16, false, signed,
471 ppc64_elf_unhandled_reloc),
473 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
474 the symbol. */
475 HOW (R_PPC64_PLT16_HA, 2, 16, 0xffff, 16, false, signed,
476 ppc64_elf_unhandled_reloc),
478 /* 16-bit section relative relocation. */
479 HOW (R_PPC64_SECTOFF, 2, 16, 0xffff, 0, false, signed,
480 ppc64_elf_sectoff_reloc),
482 /* Like R_PPC64_SECTOFF, but no overflow warning. */
483 HOW (R_PPC64_SECTOFF_LO, 2, 16, 0xffff, 0, false, dont,
484 ppc64_elf_sectoff_reloc),
486 /* 16-bit upper half section relative relocation. */
487 HOW (R_PPC64_SECTOFF_HI, 2, 16, 0xffff, 16, false, signed,
488 ppc64_elf_sectoff_reloc),
490 /* 16-bit upper half adjusted section relative relocation. */
491 HOW (R_PPC64_SECTOFF_HA, 2, 16, 0xffff, 16, false, signed,
492 ppc64_elf_sectoff_ha_reloc),
494 /* Like R_PPC64_REL24 without touching the two least significant bits. */
495 HOW (R_PPC64_REL30, 4, 30, 0xfffffffc, 2, true, dont,
496 bfd_elf_generic_reloc),
498 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
500 /* A standard 64-bit relocation. */
501 HOW (R_PPC64_ADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
502 bfd_elf_generic_reloc),
504 /* The bits 32-47 of an address. */
505 HOW (R_PPC64_ADDR16_HIGHER, 2, 16, 0xffff, 32, false, dont,
506 bfd_elf_generic_reloc),
508 /* The bits 32-47 of an address, plus 1 if the contents of the low
509 16 bits, treated as a signed number, is negative. */
510 HOW (R_PPC64_ADDR16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
511 ppc64_elf_ha_reloc),
513 /* The bits 48-63 of an address. */
514 HOW (R_PPC64_ADDR16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
515 bfd_elf_generic_reloc),
517 /* The bits 48-63 of an address, plus 1 if the contents of the low
518 16 bits, treated as a signed number, is negative. */
519 HOW (R_PPC64_ADDR16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
520 ppc64_elf_ha_reloc),
522 /* Like ADDR64, but may be unaligned. */
523 HOW (R_PPC64_UADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
524 bfd_elf_generic_reloc),
526 /* 64-bit relative relocation. */
527 HOW (R_PPC64_REL64, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
528 bfd_elf_generic_reloc),
530 /* 64-bit relocation to the symbol's procedure linkage table. */
531 HOW (R_PPC64_PLT64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
532 ppc64_elf_unhandled_reloc),
534 /* 64-bit PC relative relocation to the symbol's procedure linkage
535 table. */
536 /* FIXME: R_PPC64_PLTREL64 not supported. */
537 HOW (R_PPC64_PLTREL64, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
538 ppc64_elf_unhandled_reloc),
540 /* 16 bit TOC-relative relocation. */
541 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
542 HOW (R_PPC64_TOC16, 2, 16, 0xffff, 0, false, signed,
543 ppc64_elf_toc_reloc),
545 /* 16 bit TOC-relative relocation without overflow. */
546 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
547 HOW (R_PPC64_TOC16_LO, 2, 16, 0xffff, 0, false, dont,
548 ppc64_elf_toc_reloc),
550 /* 16 bit TOC-relative relocation, high 16 bits. */
551 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
552 HOW (R_PPC64_TOC16_HI, 2, 16, 0xffff, 16, false, signed,
553 ppc64_elf_toc_reloc),
555 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
556 contents of the low 16 bits, treated as a signed number, is
557 negative. */
558 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
559 HOW (R_PPC64_TOC16_HA, 2, 16, 0xffff, 16, false, signed,
560 ppc64_elf_toc_ha_reloc),
562 /* 64-bit relocation; insert value of TOC base (.TOC.). */
563 /* R_PPC64_TOC 51 doubleword64 .TOC. */
564 HOW (R_PPC64_TOC, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
565 ppc64_elf_toc64_reloc),
567 /* Like R_PPC64_GOT16, but also informs the link editor that the
568 value to relocate may (!) refer to a PLT entry which the link
569 editor (a) may replace with the symbol value. If the link editor
570 is unable to fully resolve the symbol, it may (b) create a PLT
571 entry and store the address to the new PLT entry in the GOT.
572 This permits lazy resolution of function symbols at run time.
573 The link editor may also skip all of this and just (c) emit a
574 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
575 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
576 HOW (R_PPC64_PLTGOT16, 2, 16, 0xffff, 0, false,signed,
577 ppc64_elf_unhandled_reloc),
579 /* Like R_PPC64_PLTGOT16, but without overflow. */
580 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
581 HOW (R_PPC64_PLTGOT16_LO, 2, 16, 0xffff, 0, false, dont,
582 ppc64_elf_unhandled_reloc),
584 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
585 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
586 HOW (R_PPC64_PLTGOT16_HI, 2, 16, 0xffff, 16, false, signed,
587 ppc64_elf_unhandled_reloc),
589 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
590 1 if the contents of the low 16 bits, treated as a signed number,
591 is negative. */
592 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
593 HOW (R_PPC64_PLTGOT16_HA, 2, 16, 0xffff, 16, false, signed,
594 ppc64_elf_unhandled_reloc),
596 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
597 HOW (R_PPC64_ADDR16_DS, 2, 16, 0xfffc, 0, false, signed,
598 bfd_elf_generic_reloc),
600 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
601 HOW (R_PPC64_ADDR16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
602 bfd_elf_generic_reloc),
604 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
605 HOW (R_PPC64_GOT16_DS, 2, 16, 0xfffc, 0, false, signed,
606 ppc64_elf_unhandled_reloc),
608 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
609 HOW (R_PPC64_GOT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
610 ppc64_elf_unhandled_reloc),
612 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
613 HOW (R_PPC64_PLT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
614 ppc64_elf_unhandled_reloc),
616 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
617 HOW (R_PPC64_SECTOFF_DS, 2, 16, 0xfffc, 0, false, signed,
618 ppc64_elf_sectoff_reloc),
620 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
621 HOW (R_PPC64_SECTOFF_LO_DS, 2, 16, 0xfffc, 0, false, dont,
622 ppc64_elf_sectoff_reloc),
624 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
625 HOW (R_PPC64_TOC16_DS, 2, 16, 0xfffc, 0, false, signed,
626 ppc64_elf_toc_reloc),
628 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
629 HOW (R_PPC64_TOC16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
630 ppc64_elf_toc_reloc),
632 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
633 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
634 HOW (R_PPC64_PLTGOT16_DS, 2, 16, 0xfffc, 0, false, signed,
635 ppc64_elf_unhandled_reloc),
637 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
638 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
639 HOW (R_PPC64_PLTGOT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
640 ppc64_elf_unhandled_reloc),
642 /* Marker relocs for TLS. */
643 HOW (R_PPC64_TLS, 4, 32, 0, 0, false, dont,
644 bfd_elf_generic_reloc),
646 HOW (R_PPC64_TLSGD, 4, 32, 0, 0, false, dont,
647 bfd_elf_generic_reloc),
649 HOW (R_PPC64_TLSLD, 4, 32, 0, 0, false, dont,
650 bfd_elf_generic_reloc),
652 /* Marker reloc for optimizing r2 save in prologue rather than on
653 each plt call stub. */
654 HOW (R_PPC64_TOCSAVE, 4, 32, 0, 0, false, dont,
655 bfd_elf_generic_reloc),
657 /* Marker relocs on inline plt call instructions. */
658 HOW (R_PPC64_PLTSEQ, 4, 32, 0, 0, false, dont,
659 bfd_elf_generic_reloc),
661 HOW (R_PPC64_PLTCALL, 4, 32, 0, 0, false, dont,
662 bfd_elf_generic_reloc),
664 /* Computes the load module index of the load module that contains the
665 definition of its TLS sym. */
666 HOW (R_PPC64_DTPMOD64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
667 ppc64_elf_unhandled_reloc),
669 /* Computes a dtv-relative displacement, the difference between the value
670 of sym+add and the base address of the thread-local storage block that
671 contains the definition of sym, minus 0x8000. */
672 HOW (R_PPC64_DTPREL64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
673 ppc64_elf_unhandled_reloc),
675 /* A 16 bit dtprel reloc. */
676 HOW (R_PPC64_DTPREL16, 2, 16, 0xffff, 0, false, signed,
677 ppc64_elf_unhandled_reloc),
679 /* Like DTPREL16, but no overflow. */
680 HOW (R_PPC64_DTPREL16_LO, 2, 16, 0xffff, 0, false, dont,
681 ppc64_elf_unhandled_reloc),
683 /* Like DTPREL16_LO, but next higher group of 16 bits. */
684 HOW (R_PPC64_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
685 ppc64_elf_unhandled_reloc),
687 /* Like DTPREL16_HI, but adjust for low 16 bits. */
688 HOW (R_PPC64_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
689 ppc64_elf_unhandled_reloc),
691 /* Like DTPREL16_HI, but next higher group of 16 bits. */
692 HOW (R_PPC64_DTPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
693 ppc64_elf_unhandled_reloc),
695 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
696 HOW (R_PPC64_DTPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
697 ppc64_elf_unhandled_reloc),
699 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
700 HOW (R_PPC64_DTPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
701 ppc64_elf_unhandled_reloc),
703 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
704 HOW (R_PPC64_DTPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
705 ppc64_elf_unhandled_reloc),
707 /* Like DTPREL16, but for insns with a DS field. */
708 HOW (R_PPC64_DTPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
709 ppc64_elf_unhandled_reloc),
711 /* Like DTPREL16_DS, but no overflow. */
712 HOW (R_PPC64_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
713 ppc64_elf_unhandled_reloc),
715 /* Computes a tp-relative displacement, the difference between the value of
716 sym+add and the value of the thread pointer (r13). */
717 HOW (R_PPC64_TPREL64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
718 ppc64_elf_unhandled_reloc),
720 /* A 16 bit tprel reloc. */
721 HOW (R_PPC64_TPREL16, 2, 16, 0xffff, 0, false, signed,
722 ppc64_elf_unhandled_reloc),
724 /* Like TPREL16, but no overflow. */
725 HOW (R_PPC64_TPREL16_LO, 2, 16, 0xffff, 0, false, dont,
726 ppc64_elf_unhandled_reloc),
728 /* Like TPREL16_LO, but next higher group of 16 bits. */
729 HOW (R_PPC64_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
730 ppc64_elf_unhandled_reloc),
732 /* Like TPREL16_HI, but adjust for low 16 bits. */
733 HOW (R_PPC64_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
734 ppc64_elf_unhandled_reloc),
736 /* Like TPREL16_HI, but next higher group of 16 bits. */
737 HOW (R_PPC64_TPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
738 ppc64_elf_unhandled_reloc),
740 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
741 HOW (R_PPC64_TPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
742 ppc64_elf_unhandled_reloc),
744 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
745 HOW (R_PPC64_TPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
746 ppc64_elf_unhandled_reloc),
748 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
749 HOW (R_PPC64_TPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
750 ppc64_elf_unhandled_reloc),
752 /* Like TPREL16, but for insns with a DS field. */
753 HOW (R_PPC64_TPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
754 ppc64_elf_unhandled_reloc),
756 /* Like TPREL16_DS, but no overflow. */
757 HOW (R_PPC64_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
758 ppc64_elf_unhandled_reloc),
760 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
761 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
762 to the first entry relative to the TOC base (r2). */
763 HOW (R_PPC64_GOT_TLSGD16, 2, 16, 0xffff, 0, false, signed,
764 ppc64_elf_unhandled_reloc),
766 /* Like GOT_TLSGD16, but no overflow. */
767 HOW (R_PPC64_GOT_TLSGD16_LO, 2, 16, 0xffff, 0, false, dont,
768 ppc64_elf_unhandled_reloc),
770 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
771 HOW (R_PPC64_GOT_TLSGD16_HI, 2, 16, 0xffff, 16, false, signed,
772 ppc64_elf_unhandled_reloc),
774 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
775 HOW (R_PPC64_GOT_TLSGD16_HA, 2, 16, 0xffff, 16, false, signed,
776 ppc64_elf_unhandled_reloc),
778 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
779 with values (sym+add)@dtpmod and zero, and computes the offset to the
780 first entry relative to the TOC base (r2). */
781 HOW (R_PPC64_GOT_TLSLD16, 2, 16, 0xffff, 0, false, signed,
782 ppc64_elf_unhandled_reloc),
784 /* Like GOT_TLSLD16, but no overflow. */
785 HOW (R_PPC64_GOT_TLSLD16_LO, 2, 16, 0xffff, 0, false, dont,
786 ppc64_elf_unhandled_reloc),
788 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
789 HOW (R_PPC64_GOT_TLSLD16_HI, 2, 16, 0xffff, 16, false, signed,
790 ppc64_elf_unhandled_reloc),
792 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
793 HOW (R_PPC64_GOT_TLSLD16_HA, 2, 16, 0xffff, 16, false, signed,
794 ppc64_elf_unhandled_reloc),
796 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
797 the offset to the entry relative to the TOC base (r2). */
798 HOW (R_PPC64_GOT_DTPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
799 ppc64_elf_unhandled_reloc),
801 /* Like GOT_DTPREL16_DS, but no overflow. */
802 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
803 ppc64_elf_unhandled_reloc),
805 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
806 HOW (R_PPC64_GOT_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
807 ppc64_elf_unhandled_reloc),
809 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
810 HOW (R_PPC64_GOT_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
811 ppc64_elf_unhandled_reloc),
813 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
814 offset to the entry relative to the TOC base (r2). */
815 HOW (R_PPC64_GOT_TPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
816 ppc64_elf_unhandled_reloc),
818 /* Like GOT_TPREL16_DS, but no overflow. */
819 HOW (R_PPC64_GOT_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
820 ppc64_elf_unhandled_reloc),
822 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
823 HOW (R_PPC64_GOT_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
824 ppc64_elf_unhandled_reloc),
826 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
827 HOW (R_PPC64_GOT_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
828 ppc64_elf_unhandled_reloc),
830 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
831 ppc64_elf_unhandled_reloc),
833 HOW (R_PPC64_IRELATIVE, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
834 bfd_elf_generic_reloc),
836 /* A 16 bit relative relocation. */
837 HOW (R_PPC64_REL16, 2, 16, 0xffff, 0, true, signed,
838 bfd_elf_generic_reloc),
840 /* A 16 bit relative relocation without overflow. */
841 HOW (R_PPC64_REL16_LO, 2, 16, 0xffff, 0, true, dont,
842 bfd_elf_generic_reloc),
844 /* The high order 16 bits of a relative address. */
845 HOW (R_PPC64_REL16_HI, 2, 16, 0xffff, 16, true, signed,
846 bfd_elf_generic_reloc),
848 /* The high order 16 bits of a relative address, plus 1 if the contents of
849 the low 16 bits, treated as a signed number, is negative. */
850 HOW (R_PPC64_REL16_HA, 2, 16, 0xffff, 16, true, signed,
851 ppc64_elf_ha_reloc),
853 HOW (R_PPC64_REL16_HIGH, 2, 16, 0xffff, 16, true, dont,
854 bfd_elf_generic_reloc),
856 HOW (R_PPC64_REL16_HIGHA, 2, 16, 0xffff, 16, true, dont,
857 ppc64_elf_ha_reloc),
859 HOW (R_PPC64_REL16_HIGHER, 2, 16, 0xffff, 32, true, dont,
860 bfd_elf_generic_reloc),
862 HOW (R_PPC64_REL16_HIGHERA, 2, 16, 0xffff, 32, true, dont,
863 ppc64_elf_ha_reloc),
865 HOW (R_PPC64_REL16_HIGHEST, 2, 16, 0xffff, 48, true, dont,
866 bfd_elf_generic_reloc),
868 HOW (R_PPC64_REL16_HIGHESTA, 2, 16, 0xffff, 48, true, dont,
869 ppc64_elf_ha_reloc),
871 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
872 HOW (R_PPC64_REL16DX_HA, 4, 16, 0x1fffc1, 16, true, signed,
873 ppc64_elf_ha_reloc),
875 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
876 HOW (R_PPC64_16DX_HA, 4, 16, 0x1fffc1, 16, false, signed,
877 ppc64_elf_ha_reloc),
879 /* Like R_PPC64_ADDR16_HI, but no overflow. */
880 HOW (R_PPC64_ADDR16_HIGH, 2, 16, 0xffff, 16, false, dont,
881 bfd_elf_generic_reloc),
883 /* Like R_PPC64_ADDR16_HA, but no overflow. */
884 HOW (R_PPC64_ADDR16_HIGHA, 2, 16, 0xffff, 16, false, dont,
885 ppc64_elf_ha_reloc),
887 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
888 HOW (R_PPC64_DTPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
889 ppc64_elf_unhandled_reloc),
891 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
892 HOW (R_PPC64_DTPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
893 ppc64_elf_unhandled_reloc),
895 /* Like R_PPC64_TPREL16_HI, but no overflow. */
896 HOW (R_PPC64_TPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
897 ppc64_elf_unhandled_reloc),
899 /* Like R_PPC64_TPREL16_HA, but no overflow. */
900 HOW (R_PPC64_TPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
901 ppc64_elf_unhandled_reloc),
903 /* Marker reloc on ELFv2 large-model function entry. */
904 HOW (R_PPC64_ENTRY, 4, 32, 0, 0, false, dont,
905 bfd_elf_generic_reloc),
907 /* Like ADDR64, but use local entry point of function. */
908 HOW (R_PPC64_ADDR64_LOCAL, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
909 bfd_elf_generic_reloc),
911 HOW (R_PPC64_PLTSEQ_NOTOC, 4, 32, 0, 0, false, dont,
912 bfd_elf_generic_reloc),
914 HOW (R_PPC64_PLTCALL_NOTOC, 4, 32, 0, 0, false, dont,
915 bfd_elf_generic_reloc),
917 HOW (R_PPC64_PCREL_OPT, 4, 32, 0, 0, false, dont,
918 bfd_elf_generic_reloc),
920 HOW (R_PPC64_D34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
921 ppc64_elf_prefix_reloc),
923 HOW (R_PPC64_D34_LO, 8, 34, 0x3ffff0000ffffULL, 0, false, dont,
924 ppc64_elf_prefix_reloc),
926 HOW (R_PPC64_D34_HI30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
927 ppc64_elf_prefix_reloc),
929 HOW (R_PPC64_D34_HA30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
930 ppc64_elf_prefix_reloc),
932 HOW (R_PPC64_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
933 ppc64_elf_prefix_reloc),
935 HOW (R_PPC64_GOT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
936 ppc64_elf_unhandled_reloc),
938 HOW (R_PPC64_PLT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
939 ppc64_elf_unhandled_reloc),
941 HOW (R_PPC64_PLT_PCREL34_NOTOC, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
942 ppc64_elf_unhandled_reloc),
944 HOW (R_PPC64_TPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
945 ppc64_elf_unhandled_reloc),
947 HOW (R_PPC64_DTPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
948 ppc64_elf_unhandled_reloc),
950 HOW (R_PPC64_GOT_TLSGD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
951 ppc64_elf_unhandled_reloc),
953 HOW (R_PPC64_GOT_TLSLD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
954 ppc64_elf_unhandled_reloc),
956 HOW (R_PPC64_GOT_TPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
957 ppc64_elf_unhandled_reloc),
959 HOW (R_PPC64_GOT_DTPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
960 ppc64_elf_unhandled_reloc),
962 HOW (R_PPC64_ADDR16_HIGHER34, 2, 16, 0xffff, 34, false, dont,
963 bfd_elf_generic_reloc),
965 HOW (R_PPC64_ADDR16_HIGHERA34, 2, 16, 0xffff, 34, false, dont,
966 ppc64_elf_ha_reloc),
968 HOW (R_PPC64_ADDR16_HIGHEST34, 2, 16, 0xffff, 50, false, dont,
969 bfd_elf_generic_reloc),
971 HOW (R_PPC64_ADDR16_HIGHESTA34, 2, 16, 0xffff, 50, false, dont,
972 ppc64_elf_ha_reloc),
974 HOW (R_PPC64_REL16_HIGHER34, 2, 16, 0xffff, 34, true, dont,
975 bfd_elf_generic_reloc),
977 HOW (R_PPC64_REL16_HIGHERA34, 2, 16, 0xffff, 34, true, dont,
978 ppc64_elf_ha_reloc),
980 HOW (R_PPC64_REL16_HIGHEST34, 2, 16, 0xffff, 50, true, dont,
981 bfd_elf_generic_reloc),
983 HOW (R_PPC64_REL16_HIGHESTA34, 2, 16, 0xffff, 50, true, dont,
984 ppc64_elf_ha_reloc),
986 HOW (R_PPC64_D28, 8, 28, 0xfff0000ffffULL, 0, false, signed,
987 ppc64_elf_prefix_reloc),
989 HOW (R_PPC64_PCREL28, 8, 28, 0xfff0000ffffULL, 0, true, signed,
990 ppc64_elf_prefix_reloc),
992 /* GNU extension to record C++ vtable hierarchy. */
993 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
994 NULL),
996 /* GNU extension to record C++ vtable member usage. */
997 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
998 NULL),
1002 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
1003 be done. */
1005 static void
1006 ppc_howto_init (void)
1008 unsigned int i, type;
1010 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1012 type = ppc64_elf_howto_raw[i].type;
1013 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
1014 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1018 static reloc_howto_type *
1019 ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
1021 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1023 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1024 /* Initialize howto table if needed. */
1025 ppc_howto_init ();
1027 switch (code)
1029 default:
1030 /* xgettext:c-format */
1031 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1032 (int) code);
1033 bfd_set_error (bfd_error_bad_value);
1034 return NULL;
1036 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1037 break;
1038 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1039 break;
1040 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1041 break;
1042 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1043 break;
1044 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1045 break;
1046 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1047 break;
1048 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1049 break;
1050 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1051 break;
1052 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1053 break;
1054 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1055 break;
1056 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1057 break;
1058 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1059 break;
1060 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1061 break;
1062 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1063 break;
1064 case BFD_RELOC_PPC64_REL24_P9NOTOC: r = R_PPC64_REL24_P9NOTOC;
1065 break;
1066 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1067 break;
1068 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1069 break;
1070 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1071 break;
1072 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1073 break;
1074 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1075 break;
1076 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1077 break;
1078 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1079 break;
1080 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1081 break;
1082 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1083 break;
1084 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1085 break;
1086 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1087 break;
1088 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1089 break;
1090 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1091 break;
1092 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1093 break;
1094 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1095 break;
1096 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1097 break;
1098 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1099 break;
1100 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1101 break;
1102 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1103 break;
1104 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1105 break;
1106 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1107 break;
1108 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1109 break;
1110 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1111 break;
1112 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1113 break;
1114 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1115 break;
1116 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1117 break;
1118 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1119 break;
1120 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1121 break;
1122 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1123 break;
1124 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1125 break;
1126 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1127 break;
1128 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1129 break;
1130 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1131 break;
1132 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1133 break;
1134 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1135 break;
1136 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1137 break;
1138 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1139 break;
1140 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1141 break;
1142 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1143 break;
1144 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1145 break;
1146 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1147 break;
1148 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1149 break;
1150 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1151 break;
1152 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1153 break;
1154 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1155 break;
1156 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1157 break;
1158 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1159 break;
1160 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1161 break;
1162 case BFD_RELOC_PPC64_TLS_PCREL:
1163 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1164 break;
1165 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1166 break;
1167 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1168 break;
1169 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1170 break;
1171 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1172 break;
1173 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1174 break;
1175 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1176 break;
1177 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1178 break;
1179 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1180 break;
1181 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1182 break;
1183 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1184 break;
1185 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1186 break;
1187 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1188 break;
1189 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1190 break;
1191 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1192 break;
1193 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1194 break;
1195 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1196 break;
1197 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1198 break;
1199 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1200 break;
1201 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1202 break;
1203 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1204 break;
1205 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1206 break;
1207 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1208 break;
1209 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1210 break;
1211 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1212 break;
1213 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1214 break;
1215 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1216 break;
1217 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1218 break;
1219 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1220 break;
1221 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1222 break;
1223 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1224 break;
1225 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1226 break;
1227 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1228 break;
1229 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1230 break;
1231 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1232 break;
1233 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1234 break;
1235 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1236 break;
1237 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1238 break;
1239 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1240 break;
1241 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1242 break;
1243 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1244 break;
1245 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1246 break;
1247 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1248 break;
1249 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1250 break;
1251 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1252 break;
1253 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1254 break;
1255 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1256 break;
1257 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1258 break;
1259 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1260 break;
1261 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1262 break;
1263 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1264 break;
1265 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1266 break;
1267 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1268 break;
1269 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1270 break;
1271 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1272 break;
1273 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1274 break;
1275 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1276 break;
1277 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1278 break;
1279 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1280 break;
1281 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1282 break;
1283 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1284 break;
1285 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1286 break;
1287 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1288 break;
1289 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1290 break;
1291 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1292 break;
1293 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1294 break;
1295 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1296 break;
1297 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1298 break;
1299 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1300 break;
1301 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
1302 break;
1303 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
1304 break;
1305 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
1306 break;
1307 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34: r = R_PPC64_GOT_DTPREL_PCREL34;
1308 break;
1309 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1310 break;
1311 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1312 break;
1313 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1314 break;
1315 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1316 break;
1317 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1318 break;
1319 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1320 break;
1321 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1322 break;
1323 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1324 break;
1325 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1326 break;
1327 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1328 break;
1329 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1330 break;
1331 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1332 break;
1335 return ppc64_elf_howto_table[r];
1338 static reloc_howto_type *
1339 ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
1341 unsigned int i;
1342 static char *compat_map[][2] = {
1343 { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
1344 { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
1345 { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
1346 { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
1349 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1350 if (ppc64_elf_howto_raw[i].name != NULL
1351 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1352 return &ppc64_elf_howto_raw[i];
1354 /* Handle old names of relocations in case they were used by
1355 .reloc directives.
1356 FIXME: Remove this soon. Mapping the reloc names is very likely
1357 completely unnecessary. */
1358 for (i = 0; i < ARRAY_SIZE (compat_map); i++)
1359 if (strcasecmp (compat_map[i][0], r_name) == 0)
1361 _bfd_error_handler (_("warning: %s should be used rather than %s"),
1362 compat_map[i][1], compat_map[i][0]);
1363 return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
1366 return NULL;
1369 /* Set the howto pointer for a PowerPC ELF reloc. */
1371 static bool
1372 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1373 Elf_Internal_Rela *dst)
1375 unsigned int type;
1377 /* Initialize howto table if needed. */
1378 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1379 ppc_howto_init ();
1381 type = ELF64_R_TYPE (dst->r_info);
1382 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1384 /* xgettext:c-format */
1385 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1386 abfd, type);
1387 bfd_set_error (bfd_error_bad_value);
1388 return false;
1390 cache_ptr->howto = ppc64_elf_howto_table[type];
1391 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1393 /* xgettext:c-format */
1394 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1395 abfd, type);
1396 bfd_set_error (bfd_error_bad_value);
1397 return false;
1400 return true;
1403 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1405 static bfd_reloc_status_type
1406 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1407 void *data, asection *input_section,
1408 bfd *output_bfd, char **error_message)
1410 enum elf_ppc64_reloc_type r_type;
1411 long insn;
1412 bfd_size_type octets;
1413 bfd_vma value;
1415 /* If this is a relocatable link (output_bfd test tells us), just
1416 call the generic function. Any adjustment will be done at final
1417 link time. */
1418 if (output_bfd != NULL)
1419 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1420 input_section, output_bfd, error_message);
1422 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1423 We won't actually be using the low bits, so trashing them
1424 doesn't matter. */
1425 r_type = reloc_entry->howto->type;
1426 if (r_type == R_PPC64_ADDR16_HIGHERA34
1427 || r_type == R_PPC64_ADDR16_HIGHESTA34
1428 || r_type == R_PPC64_REL16_HIGHERA34
1429 || r_type == R_PPC64_REL16_HIGHESTA34)
1430 reloc_entry->addend += 1ULL << 33;
1431 else
1432 reloc_entry->addend += 1U << 15;
1433 if (r_type != R_PPC64_REL16DX_HA)
1434 return bfd_reloc_continue;
1436 value = 0;
1437 if (!bfd_is_com_section (symbol->section))
1438 value = symbol->value;
1439 value += (reloc_entry->addend
1440 + symbol->section->output_offset
1441 + symbol->section->output_section->vma);
1442 value -= (reloc_entry->address
1443 + input_section->output_offset
1444 + input_section->output_section->vma);
1445 value = (bfd_signed_vma) value >> 16;
1447 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1448 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1449 input_section, octets))
1450 return bfd_reloc_outofrange;
1452 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1453 insn &= ~0x1fffc1;
1454 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1455 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1456 if (value + 0x8000 > 0xffff)
1457 return bfd_reloc_overflow;
1458 return bfd_reloc_ok;
1461 static bfd_reloc_status_type
1462 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1463 void *data, asection *input_section,
1464 bfd *output_bfd, char **error_message)
1466 if (output_bfd != NULL)
1467 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1468 input_section, output_bfd, error_message);
1470 if (symbol->section->owner == NULL
1471 || !is_ppc64_elf (symbol->section->owner))
1472 return bfd_reloc_continue;
1474 if (strcmp (symbol->section->name, ".opd") == 0
1475 && (symbol->section->owner->flags & DYNAMIC) == 0)
1477 bfd_vma dest = opd_entry_value (symbol->section,
1478 symbol->value + reloc_entry->addend,
1479 NULL, NULL, false);
1480 if (dest != (bfd_vma) -1)
1481 reloc_entry->addend = dest - (symbol->value
1482 + symbol->section->output_section->vma
1483 + symbol->section->output_offset);
1485 else
1487 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1489 if (symbol->section->owner != abfd
1490 && abiversion (symbol->section->owner) >= 2)
1492 unsigned int i;
1494 for (i = 0; i < symbol->section->owner->symcount; ++i)
1496 asymbol *symdef = symbol->section->owner->outsymbols[i];
1498 if (strcmp (symdef->name, symbol->name) == 0)
1500 elfsym = (elf_symbol_type *) symdef;
1501 break;
1505 reloc_entry->addend
1506 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1508 return bfd_reloc_continue;
1511 static bfd_reloc_status_type
1512 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1513 void *data, asection *input_section,
1514 bfd *output_bfd, char **error_message)
1516 long insn;
1517 enum elf_ppc64_reloc_type r_type;
1518 bfd_size_type octets;
1519 /* Assume 'at' branch hints. */
1520 bool is_isa_v2 = true;
1522 /* If this is a relocatable link (output_bfd test tells us), just
1523 call the generic function. Any adjustment will be done at final
1524 link time. */
1525 if (output_bfd != NULL)
1526 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1527 input_section, output_bfd, error_message);
1529 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1530 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1531 input_section, octets))
1532 return bfd_reloc_outofrange;
1534 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1535 insn &= ~(0x01 << 21);
1536 r_type = reloc_entry->howto->type;
1537 if (r_type == R_PPC64_ADDR14_BRTAKEN
1538 || r_type == R_PPC64_REL14_BRTAKEN)
1539 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1541 if (is_isa_v2)
1543 /* Set 'a' bit. This is 0b00010 in BO field for branch
1544 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1545 for branch on CTR insns (BO == 1a00t or 1a01t). */
1546 if ((insn & (0x14 << 21)) == (0x04 << 21))
1547 insn |= 0x02 << 21;
1548 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1549 insn |= 0x08 << 21;
1550 else
1551 goto out;
1553 else
1555 bfd_vma target = 0;
1556 bfd_vma from;
1558 if (!bfd_is_com_section (symbol->section))
1559 target = symbol->value;
1560 target += symbol->section->output_section->vma;
1561 target += symbol->section->output_offset;
1562 target += reloc_entry->addend;
1564 from = (reloc_entry->address
1565 + input_section->output_offset
1566 + input_section->output_section->vma);
1568 /* Invert 'y' bit if not the default. */
1569 if ((bfd_signed_vma) (target - from) < 0)
1570 insn ^= 0x01 << 21;
1572 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1573 out:
1574 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1575 input_section, output_bfd, error_message);
1578 static bfd_reloc_status_type
1579 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1580 void *data, asection *input_section,
1581 bfd *output_bfd, char **error_message)
1583 /* If this is a relocatable link (output_bfd test tells us), just
1584 call the generic function. Any adjustment will be done at final
1585 link time. */
1586 if (output_bfd != NULL)
1587 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1588 input_section, output_bfd, error_message);
1590 /* Subtract the symbol section base address. */
1591 reloc_entry->addend -= symbol->section->output_section->vma;
1592 return bfd_reloc_continue;
1595 static bfd_reloc_status_type
1596 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1597 void *data, asection *input_section,
1598 bfd *output_bfd, char **error_message)
1600 /* If this is a relocatable link (output_bfd test tells us), just
1601 call the generic function. Any adjustment will be done at final
1602 link time. */
1603 if (output_bfd != NULL)
1604 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1605 input_section, output_bfd, error_message);
1607 /* Subtract the symbol section base address. */
1608 reloc_entry->addend -= symbol->section->output_section->vma;
1610 /* Adjust the addend for sign extension of the low 16 bits. */
1611 reloc_entry->addend += 0x8000;
1612 return bfd_reloc_continue;
1615 static bfd_reloc_status_type
1616 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1617 void *data, asection *input_section,
1618 bfd *output_bfd, char **error_message)
1620 bfd_vma TOCstart;
1622 /* If this is a relocatable link (output_bfd test tells us), just
1623 call the generic function. Any adjustment will be done at final
1624 link time. */
1625 if (output_bfd != NULL)
1626 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1627 input_section, output_bfd, error_message);
1629 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1630 if (TOCstart == 0)
1631 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1633 /* Subtract the TOC base address. */
1634 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1635 return bfd_reloc_continue;
1638 static bfd_reloc_status_type
1639 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1640 void *data, asection *input_section,
1641 bfd *output_bfd, char **error_message)
1643 bfd_vma TOCstart;
1645 /* If this is a relocatable link (output_bfd test tells us), just
1646 call the generic function. Any adjustment will be done at final
1647 link time. */
1648 if (output_bfd != NULL)
1649 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1650 input_section, output_bfd, error_message);
1652 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1653 if (TOCstart == 0)
1654 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1656 /* Subtract the TOC base address. */
1657 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1659 /* Adjust the addend for sign extension of the low 16 bits. */
1660 reloc_entry->addend += 0x8000;
1661 return bfd_reloc_continue;
1664 static bfd_reloc_status_type
1665 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1666 void *data, asection *input_section,
1667 bfd *output_bfd, char **error_message)
1669 bfd_vma TOCstart;
1670 bfd_size_type octets;
1672 /* If this is a relocatable link (output_bfd test tells us), just
1673 call the generic function. Any adjustment will be done at final
1674 link time. */
1675 if (output_bfd != NULL)
1676 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1677 input_section, output_bfd, error_message);
1679 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1680 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1681 input_section, octets))
1682 return bfd_reloc_outofrange;
1684 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1685 if (TOCstart == 0)
1686 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1688 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1689 return bfd_reloc_ok;
1692 static bfd_reloc_status_type
1693 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1694 void *data, asection *input_section,
1695 bfd *output_bfd, char **error_message)
1697 uint64_t insn;
1698 bfd_vma targ;
1699 bfd_size_type octets;
1701 if (output_bfd != NULL)
1702 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1703 input_section, output_bfd, error_message);
1705 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1706 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1707 input_section, octets))
1708 return bfd_reloc_outofrange;
1710 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1711 insn <<= 32;
1712 insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
1714 targ = (symbol->section->output_section->vma
1715 + symbol->section->output_offset
1716 + reloc_entry->addend);
1717 if (!bfd_is_com_section (symbol->section))
1718 targ += symbol->value;
1719 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1720 targ += 1ULL << 33;
1721 if (reloc_entry->howto->pc_relative)
1723 bfd_vma from = (reloc_entry->address
1724 + input_section->output_offset
1725 + input_section->output_section->vma);
1726 targ -=from;
1728 targ >>= reloc_entry->howto->rightshift;
1729 insn &= ~reloc_entry->howto->dst_mask;
1730 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1731 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + octets);
1732 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets + 4);
1733 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1734 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1735 >= 1ULL << reloc_entry->howto->bitsize))
1736 return bfd_reloc_overflow;
1737 return bfd_reloc_ok;
1740 static bfd_reloc_status_type
1741 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1742 void *data, asection *input_section,
1743 bfd *output_bfd, char **error_message)
1745 /* If this is a relocatable link (output_bfd test tells us), just
1746 call the generic function. Any adjustment will be done at final
1747 link time. */
1748 if (output_bfd != NULL)
1749 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1750 input_section, output_bfd, error_message);
1752 if (error_message != NULL)
1753 *error_message = bfd_asprintf (_("generic linker can't handle %s"),
1754 reloc_entry->howto->name);
1755 return bfd_reloc_dangerous;
1758 /* Track GOT entries needed for a given symbol. We might need more
1759 than one got entry per symbol. */
1760 struct got_entry
1762 struct got_entry *next;
1764 /* The symbol addend that we'll be placing in the GOT. */
1765 bfd_vma addend;
1767 /* Unlike other ELF targets, we use separate GOT entries for the same
1768 symbol referenced from different input files. This is to support
1769 automatic multiple TOC/GOT sections, where the TOC base can vary
1770 from one input file to another. After partitioning into TOC groups
1771 we merge entries within the group.
1773 Point to the BFD owning this GOT entry. */
1774 bfd *owner;
1776 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1777 TLS_TPREL or TLS_DTPREL for tls entries. */
1778 unsigned char tls_type;
1780 /* Non-zero if got.ent points to real entry. */
1781 unsigned char is_indirect;
1783 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1784 union
1786 bfd_signed_vma refcount;
1787 bfd_vma offset;
1788 struct got_entry *ent;
1789 } got;
1792 /* The same for PLT. */
1793 struct plt_entry
1795 struct plt_entry *next;
1797 bfd_vma addend;
1799 union
1801 bfd_signed_vma refcount;
1802 bfd_vma offset;
1803 } plt;
1806 struct ppc64_elf_obj_tdata
1808 struct elf_obj_tdata elf;
1810 /* Shortcuts to dynamic linker sections. */
1811 asection *got;
1812 asection *relgot;
1814 /* Used during garbage collection. We attach global symbols defined
1815 on removed .opd entries to this section so that the sym is removed. */
1816 asection *deleted_section;
1818 /* TLS local dynamic got entry handling. Support for multiple GOT
1819 sections means we potentially need one of these for each input bfd. */
1820 struct got_entry tlsld_got;
1822 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1823 the reloc to be in the range -32768 to 32767. */
1824 unsigned int has_small_toc_reloc : 1;
1826 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1827 instruction not one we handle. */
1828 unsigned int unexpected_toc_insn : 1;
1830 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1831 this file. */
1832 unsigned int has_optrel : 1;
1835 #define ppc64_elf_tdata(bfd) \
1836 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1838 #define ppc64_tlsld_got(bfd) \
1839 (&ppc64_elf_tdata (bfd)->tlsld_got)
1841 /* Override the generic function because we store some extras. */
1843 static bool
1844 ppc64_elf_mkobject (bfd *abfd)
1846 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata));
1849 /* Fix bad default arch selected for a 64 bit input bfd when the
1850 default is 32 bit. Also select arch based on apuinfo. */
1852 static bool
1853 ppc64_elf_object_p (bfd *abfd)
1855 if (!abfd->arch_info->the_default)
1856 return true;
1858 if (abfd->arch_info->bits_per_word == 32)
1860 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1862 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1864 /* Relies on arch after 32 bit default being 64 bit default. */
1865 abfd->arch_info = abfd->arch_info->next;
1866 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1869 return _bfd_elf_ppc_set_arch (abfd);
1872 /* Support for core dump NOTE sections. */
1874 static bool
1875 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1877 size_t offset, size;
1879 if (note->descsz != 504)
1880 return false;
1882 /* pr_cursig */
1883 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1885 /* pr_pid */
1886 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1888 /* pr_reg */
1889 offset = 112;
1890 size = 384;
1892 /* Make a ".reg/999" section. */
1893 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1894 size, note->descpos + offset);
1897 static bool
1898 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1900 if (note->descsz != 136)
1901 return false;
1903 elf_tdata (abfd)->core->pid
1904 = bfd_get_32 (abfd, note->descdata + 24);
1905 elf_tdata (abfd)->core->program
1906 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1907 elf_tdata (abfd)->core->command
1908 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1910 return true;
1913 static char *
1914 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1915 ...)
1917 switch (note_type)
1919 default:
1920 return NULL;
1922 case NT_PRPSINFO:
1924 char data[136] ATTRIBUTE_NONSTRING;
1925 va_list ap;
1927 va_start (ap, note_type);
1928 memset (data, 0, sizeof (data));
1929 strncpy (data + 40, va_arg (ap, const char *), 16);
1930 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1931 DIAGNOSTIC_PUSH;
1932 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1933 -Wstringop-truncation:
1934 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1936 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1937 #endif
1938 strncpy (data + 56, va_arg (ap, const char *), 80);
1939 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1940 DIAGNOSTIC_POP;
1941 #endif
1942 va_end (ap);
1943 return elfcore_write_note (abfd, buf, bufsiz,
1944 "CORE", note_type, data, sizeof (data));
1947 case NT_PRSTATUS:
1949 char data[504];
1950 va_list ap;
1951 long pid;
1952 int cursig;
1953 const void *greg;
1955 va_start (ap, note_type);
1956 memset (data, 0, 112);
1957 pid = va_arg (ap, long);
1958 bfd_put_32 (abfd, pid, data + 32);
1959 cursig = va_arg (ap, int);
1960 bfd_put_16 (abfd, cursig, data + 12);
1961 greg = va_arg (ap, const void *);
1962 memcpy (data + 112, greg, 384);
1963 memset (data + 496, 0, 8);
1964 va_end (ap);
1965 return elfcore_write_note (abfd, buf, bufsiz,
1966 "CORE", note_type, data, sizeof (data));
1971 /* Add extra PPC sections. */
1973 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1975 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1976 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1977 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1978 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1979 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1980 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1981 { NULL, 0, 0, 0, 0 }
1984 enum _ppc64_sec_type {
1985 sec_normal = 0,
1986 sec_opd = 1,
1987 sec_toc = 2,
1988 sec_stub = 3
1991 struct _ppc64_elf_section_data
1993 struct bfd_elf_section_data elf;
1995 union
1997 /* An array with one entry for each opd function descriptor,
1998 and some spares since opd entries may be either 16 or 24 bytes. */
1999 #define OPD_NDX(OFF) ((OFF) >> 4)
2000 struct _opd_sec_data
2002 /* Points to the function code section for local opd entries. */
2003 asection **func_sec;
2005 /* After editing .opd, adjust references to opd local syms. */
2006 long *adjust;
2008 union
2010 /* A copy of relocs before they are modified for --emit-relocs. */
2011 Elf_Internal_Rela *relocs;
2013 /* Section contents. */
2014 bfd_byte *contents;
2015 } u;
2016 } opd;
2018 /* An array for toc sections, indexed by offset/8. */
2019 struct _toc_sec_data
2021 /* Specifies the relocation symbol index used at a given toc offset. */
2022 unsigned *symndx;
2024 /* And the relocation addend. */
2025 bfd_vma *add;
2026 } toc;
2028 /* Stub debugging. */
2029 struct ppc_stub_hash_entry *last_ent;
2030 } u;
2032 enum _ppc64_sec_type sec_type:2;
2034 /* Flag set when small branches are detected. Used to
2035 select suitable defaults for the stub group size. */
2036 unsigned int has_14bit_branch:1;
2038 /* Flag set when PLTCALL relocs are detected. */
2039 unsigned int has_pltcall:1;
2041 /* Flag set when section has PLT/GOT/TOC relocations that can be
2042 optimised. */
2043 unsigned int has_optrel:1;
2046 #define ppc64_elf_section_data(sec) \
2047 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2049 static bool
2050 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2052 struct _ppc64_elf_section_data *sdata;
2054 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2055 if (sdata == NULL)
2056 return false;
2057 sec->used_by_bfd = sdata;
2059 return _bfd_elf_new_section_hook (abfd, sec);
2062 static bool
2063 ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2065 const char *name = hdr->bfd_section->name;
2067 if (startswith (name, ".sbss")
2068 || startswith (name, ".sdata"))
2069 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2071 return true;
2074 static struct _opd_sec_data *
2075 get_opd_info (asection * sec)
2077 if (sec != NULL
2078 && ppc64_elf_section_data (sec) != NULL
2079 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2080 return &ppc64_elf_section_data (sec)->u.opd;
2081 return NULL;
2084 /* Parameters for the qsort hook. */
2085 static bool synthetic_relocatable;
2086 static const asection *synthetic_opd;
2088 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2090 static int
2091 compare_symbols (const void *ap, const void *bp)
2093 const asymbol *a = *(const asymbol **) ap;
2094 const asymbol *b = *(const asymbol **) bp;
2096 /* Section symbols first. */
2097 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2098 return -1;
2099 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2100 return 1;
2102 /* then .opd symbols. */
2103 if (synthetic_opd != NULL)
2105 if (strcmp (a->section->name, ".opd") == 0
2106 && strcmp (b->section->name, ".opd") != 0)
2107 return -1;
2108 if (strcmp (a->section->name, ".opd") != 0
2109 && strcmp (b->section->name, ".opd") == 0)
2110 return 1;
2113 /* then other code symbols. */
2114 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2115 == (SEC_CODE | SEC_ALLOC))
2116 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2117 != (SEC_CODE | SEC_ALLOC)))
2118 return -1;
2120 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2121 != (SEC_CODE | SEC_ALLOC))
2122 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2123 == (SEC_CODE | SEC_ALLOC)))
2124 return 1;
2126 if (synthetic_relocatable)
2128 if (a->section->id < b->section->id)
2129 return -1;
2131 if (a->section->id > b->section->id)
2132 return 1;
2135 if (a->value + a->section->vma < b->value + b->section->vma)
2136 return -1;
2138 if (a->value + a->section->vma > b->value + b->section->vma)
2139 return 1;
2141 /* For syms with the same value, prefer strong dynamic global function
2142 syms over other syms. */
2143 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2144 return -1;
2146 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2147 return 1;
2149 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2150 return -1;
2152 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2153 return 1;
2155 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2156 return -1;
2158 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2159 return 1;
2161 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2162 return -1;
2164 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2165 return 1;
2167 /* Finally, sort on where the symbol is in memory. The symbols will
2168 be in at most two malloc'd blocks, one for static syms, one for
2169 dynamic syms, and we distinguish the two blocks above by testing
2170 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2171 originally in the same order as the symbols (and we're not
2172 sorting the symbols themselves), this ensures a stable sort. */
2173 if (a < b)
2174 return -1;
2175 if (a > b)
2176 return 1;
2177 return 0;
2180 /* Search SYMS for a symbol of the given VALUE. */
2182 static asymbol *
2183 sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2184 bfd_vma value)
2186 size_t mid;
2188 if (id == (unsigned) -1)
2190 while (lo < hi)
2192 mid = (lo + hi) >> 1;
2193 if (syms[mid]->value + syms[mid]->section->vma < value)
2194 lo = mid + 1;
2195 else if (syms[mid]->value + syms[mid]->section->vma > value)
2196 hi = mid;
2197 else
2198 return syms[mid];
2201 else
2203 while (lo < hi)
2205 mid = (lo + hi) >> 1;
2206 if (syms[mid]->section->id < id)
2207 lo = mid + 1;
2208 else if (syms[mid]->section->id > id)
2209 hi = mid;
2210 else if (syms[mid]->value < value)
2211 lo = mid + 1;
2212 else if (syms[mid]->value > value)
2213 hi = mid;
2214 else
2215 return syms[mid];
2218 return NULL;
2221 static bool
2222 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2224 bfd_vma vma = *(bfd_vma *) ptr;
2225 return ((section->flags & SEC_ALLOC) != 0
2226 && section->vma <= vma
2227 && vma < section->vma + section->size);
2230 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2231 entry syms. Also generate @plt symbols for the glink branch table.
2232 Returns count of synthetic symbols in RET or -1 on error. */
2234 static long
2235 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2236 long static_count, asymbol **static_syms,
2237 long dyn_count, asymbol **dyn_syms,
2238 asymbol **ret)
2240 asymbol *s;
2241 size_t i, j, count;
2242 char *names;
2243 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2244 asection *opd = NULL;
2245 bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2246 asymbol **syms;
2247 int abi = abiversion (abfd);
2249 *ret = NULL;
2251 if (abi < 2)
2253 opd = bfd_get_section_by_name (abfd, ".opd");
2254 if (opd == NULL && abi == 1)
2255 return 0;
2258 syms = NULL;
2259 codesecsym = 0;
2260 codesecsymend = 0;
2261 secsymend = 0;
2262 opdsymend = 0;
2263 symcount = 0;
2264 if (opd != NULL)
2266 symcount = static_count;
2267 if (!relocatable)
2268 symcount += dyn_count;
2269 if (symcount == 0)
2270 return 0;
2272 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2273 if (syms == NULL)
2274 return -1;
2276 if (!relocatable && static_count != 0 && dyn_count != 0)
2278 /* Use both symbol tables. */
2279 memcpy (syms, static_syms, static_count * sizeof (*syms));
2280 memcpy (syms + static_count, dyn_syms,
2281 (dyn_count + 1) * sizeof (*syms));
2283 else if (!relocatable && static_count == 0)
2284 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2285 else
2286 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2288 /* Trim uninteresting symbols. Interesting symbols are section,
2289 function, and notype symbols. */
2290 for (i = 0, j = 0; i < symcount; ++i)
2291 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2292 | BSF_RELC | BSF_SRELC)) == 0)
2293 syms[j++] = syms[i];
2294 symcount = j;
2296 synthetic_relocatable = relocatable;
2297 synthetic_opd = opd;
2298 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2300 if (!relocatable && symcount > 1)
2302 /* Trim duplicate syms, since we may have merged the normal
2303 and dynamic symbols. Actually, we only care about syms
2304 that have different values, so trim any with the same
2305 value. Don't consider ifunc and ifunc resolver symbols
2306 duplicates however, because GDB wants to know whether a
2307 text symbol is an ifunc resolver. */
2308 for (i = 1, j = 1; i < symcount; ++i)
2310 const asymbol *s0 = syms[i - 1];
2311 const asymbol *s1 = syms[i];
2313 if ((s0->value + s0->section->vma
2314 != s1->value + s1->section->vma)
2315 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2316 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2317 syms[j++] = syms[i];
2319 symcount = j;
2322 i = 0;
2323 /* Note that here and in compare_symbols we can't compare opd and
2324 sym->section directly. With separate debug info files, the
2325 symbols will be extracted from the debug file while abfd passed
2326 to this function is the real binary. */
2327 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2328 && strcmp (syms[i]->section->name, ".opd") == 0)
2329 ++i;
2330 codesecsym = i;
2332 for (; i < symcount; ++i)
2333 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2334 | SEC_THREAD_LOCAL))
2335 != (SEC_CODE | SEC_ALLOC))
2336 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2337 break;
2338 codesecsymend = i;
2340 for (; i < symcount; ++i)
2341 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2342 break;
2343 secsymend = i;
2345 for (; i < symcount; ++i)
2346 if (strcmp (syms[i]->section->name, ".opd") != 0)
2347 break;
2348 opdsymend = i;
2350 for (; i < symcount; ++i)
2351 if (((syms[i]->section->flags
2352 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2353 != (SEC_CODE | SEC_ALLOC))
2354 break;
2355 symcount = i;
2357 count = 0;
2359 if (relocatable)
2361 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2362 arelent *r;
2363 size_t size;
2364 size_t relcount;
2366 if (opdsymend == secsymend)
2367 goto done;
2369 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2370 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2371 if (relcount == 0)
2372 goto done;
2374 if (!(*slurp_relocs) (abfd, opd, static_syms, false))
2376 count = -1;
2377 goto done;
2380 size = 0;
2381 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2383 asymbol *sym;
2385 while (r < opd->relocation + relcount
2386 && r->address < syms[i]->value + opd->vma)
2387 ++r;
2389 if (r == opd->relocation + relcount)
2390 break;
2392 if (r->address != syms[i]->value + opd->vma)
2393 continue;
2395 if (r->howto->type != R_PPC64_ADDR64)
2396 continue;
2398 sym = *r->sym_ptr_ptr;
2399 if (!sym_exists_at (syms, opdsymend, symcount,
2400 sym->section->id, sym->value + r->addend))
2402 ++count;
2403 size += sizeof (asymbol);
2404 size += strlen (syms[i]->name) + 2;
2408 if (size == 0)
2409 goto done;
2410 s = *ret = bfd_malloc (size);
2411 if (s == NULL)
2413 count = -1;
2414 goto done;
2417 names = (char *) (s + count);
2419 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2421 asymbol *sym;
2423 while (r < opd->relocation + relcount
2424 && r->address < syms[i]->value + opd->vma)
2425 ++r;
2427 if (r == opd->relocation + relcount)
2428 break;
2430 if (r->address != syms[i]->value + opd->vma)
2431 continue;
2433 if (r->howto->type != R_PPC64_ADDR64)
2434 continue;
2436 sym = *r->sym_ptr_ptr;
2437 if (!sym_exists_at (syms, opdsymend, symcount,
2438 sym->section->id, sym->value + r->addend))
2440 size_t len;
2442 *s = *syms[i];
2443 s->flags |= BSF_SYNTHETIC;
2444 s->section = sym->section;
2445 s->value = sym->value + r->addend;
2446 s->name = names;
2447 *names++ = '.';
2448 len = strlen (syms[i]->name);
2449 memcpy (names, syms[i]->name, len + 1);
2450 names += len + 1;
2451 /* Have udata.p point back to the original symbol this
2452 synthetic symbol was derived from. */
2453 s->udata.p = syms[i];
2454 s++;
2458 else
2460 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2461 bfd_byte *contents = NULL;
2462 size_t size;
2463 size_t plt_count = 0;
2464 bfd_vma glink_vma = 0, resolv_vma = 0;
2465 asection *dynamic, *glink = NULL, *relplt = NULL;
2466 arelent *p;
2468 if (opd != NULL
2469 && ((opd->flags & SEC_HAS_CONTENTS) == 0
2470 || !bfd_malloc_and_get_section (abfd, opd, &contents)))
2472 free_contents_and_exit_err:
2473 count = -1;
2474 free_contents_and_exit:
2475 free (contents);
2476 goto done;
2479 size = 0;
2480 for (i = secsymend; i < opdsymend; ++i)
2482 bfd_vma ent;
2484 /* Ignore bogus symbols. */
2485 if (syms[i]->value > opd->size - 8)
2486 continue;
2488 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2489 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2491 ++count;
2492 size += sizeof (asymbol);
2493 size += strlen (syms[i]->name) + 2;
2497 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2498 if (dyn_count != 0
2499 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2501 bfd_byte *dynbuf, *extdyn, *extdynend;
2502 size_t extdynsize;
2503 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2505 if ((dynamic->flags & SEC_HAS_CONTENTS) == 0
2506 || !bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2507 goto free_contents_and_exit_err;
2509 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2510 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2512 for (extdyn = dynbuf, extdynend = dynbuf + dynamic->size;
2513 (size_t) (extdynend - extdyn) >= extdynsize;
2514 extdyn += extdynsize)
2516 Elf_Internal_Dyn dyn;
2517 (*swap_dyn_in) (abfd, extdyn, &dyn);
2519 if (dyn.d_tag == DT_NULL)
2520 break;
2522 if (dyn.d_tag == DT_PPC64_GLINK)
2524 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2525 See comment in ppc64_elf_finish_dynamic_sections. */
2526 glink_vma = dyn.d_un.d_val + 8 * 4;
2527 /* The .glink section usually does not survive the final
2528 link; search for the section (usually .text) where the
2529 glink stubs now reside. */
2530 glink = bfd_sections_find_if (abfd, section_covers_vma,
2531 &glink_vma);
2532 break;
2536 free (dynbuf);
2539 if (glink != NULL)
2541 /* Determine __glink trampoline by reading the relative branch
2542 from the first glink stub. */
2543 bfd_byte buf[4];
2544 unsigned int off = 0;
2546 while (bfd_get_section_contents (abfd, glink, buf,
2547 glink_vma + off - glink->vma, 4))
2549 unsigned int insn = bfd_get_32 (abfd, buf);
2550 insn ^= B_DOT;
2551 if ((insn & ~0x3fffffc) == 0)
2553 resolv_vma
2554 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2555 break;
2557 off += 4;
2558 if (off > 4)
2559 break;
2562 if (resolv_vma)
2563 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2565 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2566 if (relplt != NULL)
2568 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2569 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
2570 goto free_contents_and_exit_err;
2572 plt_count = NUM_SHDR_ENTRIES (&elf_section_data (relplt)->this_hdr);
2573 size += plt_count * sizeof (asymbol);
2575 p = relplt->relocation;
2576 for (i = 0; i < plt_count; i++, p++)
2578 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2579 if (p->addend != 0)
2580 size += sizeof ("+0x") - 1 + 16;
2585 if (size == 0)
2586 goto free_contents_and_exit;
2587 s = *ret = bfd_malloc (size);
2588 if (s == NULL)
2589 goto free_contents_and_exit_err;
2591 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2593 for (i = secsymend; i < opdsymend; ++i)
2595 bfd_vma ent;
2597 if (syms[i]->value > opd->size - 8)
2598 continue;
2600 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2601 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2603 size_t lo, hi;
2604 size_t len;
2605 asection *sec = abfd->sections;
2607 *s = *syms[i];
2608 lo = codesecsym;
2609 hi = codesecsymend;
2610 while (lo < hi)
2612 size_t mid = (lo + hi) >> 1;
2613 if (syms[mid]->section->vma < ent)
2614 lo = mid + 1;
2615 else if (syms[mid]->section->vma > ent)
2616 hi = mid;
2617 else
2619 sec = syms[mid]->section;
2620 break;
2624 if (lo >= hi && lo > codesecsym)
2625 sec = syms[lo - 1]->section;
2627 for (; sec != NULL; sec = sec->next)
2629 if (sec->vma > ent)
2630 break;
2631 /* SEC_LOAD may not be set if SEC is from a separate debug
2632 info file. */
2633 if ((sec->flags & SEC_ALLOC) == 0)
2634 break;
2635 if ((sec->flags & SEC_CODE) != 0)
2636 s->section = sec;
2638 s->flags |= BSF_SYNTHETIC;
2639 s->value = ent - s->section->vma;
2640 s->name = names;
2641 *names++ = '.';
2642 len = strlen (syms[i]->name);
2643 memcpy (names, syms[i]->name, len + 1);
2644 names += len + 1;
2645 /* Have udata.p point back to the original symbol this
2646 synthetic symbol was derived from. */
2647 s->udata.p = syms[i];
2648 s++;
2651 free (contents);
2653 if (glink != NULL && relplt != NULL)
2655 if (resolv_vma)
2657 /* Add a symbol for the main glink trampoline. */
2658 memset (s, 0, sizeof *s);
2659 s->the_bfd = abfd;
2660 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2661 s->section = glink;
2662 s->value = resolv_vma - glink->vma;
2663 s->name = names;
2664 memcpy (names, "__glink_PLTresolve",
2665 sizeof ("__glink_PLTresolve"));
2666 names += sizeof ("__glink_PLTresolve");
2667 s++;
2668 count++;
2671 /* FIXME: It would be very much nicer to put sym@plt on the
2672 stub rather than on the glink branch table entry. The
2673 objdump disassembler would then use a sensible symbol
2674 name on plt calls. The difficulty in doing so is
2675 a) finding the stubs, and,
2676 b) matching stubs against plt entries, and,
2677 c) there can be multiple stubs for a given plt entry.
2679 Solving (a) could be done by code scanning, but older
2680 ppc64 binaries used different stubs to current code.
2681 (b) is the tricky one since you need to known the toc
2682 pointer for at least one function that uses a pic stub to
2683 be able to calculate the plt address referenced.
2684 (c) means gdb would need to set multiple breakpoints (or
2685 find the glink branch itself) when setting breakpoints
2686 for pending shared library loads. */
2687 p = relplt->relocation;
2688 for (i = 0; i < plt_count; i++, p++)
2690 size_t len;
2692 *s = **p->sym_ptr_ptr;
2693 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2694 we are defining a symbol, ensure one of them is set. */
2695 if ((s->flags & BSF_LOCAL) == 0)
2696 s->flags |= BSF_GLOBAL;
2697 s->flags |= BSF_SYNTHETIC;
2698 s->section = glink;
2699 s->value = glink_vma - glink->vma;
2700 s->name = names;
2701 s->udata.p = NULL;
2702 len = strlen ((*p->sym_ptr_ptr)->name);
2703 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2704 names += len;
2705 if (p->addend != 0)
2707 memcpy (names, "+0x", sizeof ("+0x") - 1);
2708 names += sizeof ("+0x") - 1;
2709 bfd_sprintf_vma (abfd, names, p->addend);
2710 names += strlen (names);
2712 memcpy (names, "@plt", sizeof ("@plt"));
2713 names += sizeof ("@plt");
2714 s++;
2715 if (abi < 2)
2717 glink_vma += 8;
2718 if (i >= 0x8000)
2719 glink_vma += 4;
2721 else
2722 glink_vma += 4;
2724 count += plt_count;
2728 done:
2729 free (syms);
2730 return count;
2733 /* The following functions are specific to the ELF linker, while
2734 functions above are used generally. Those named ppc64_elf_* are
2735 called by the main ELF linker code. They appear in this file more
2736 or less in the order in which they are called. eg.
2737 ppc64_elf_check_relocs is called early in the link process,
2738 ppc64_elf_finish_dynamic_sections is one of the last functions
2739 called.
2741 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2742 functions have both a function code symbol and a function descriptor
2743 symbol. A call to foo in a relocatable object file looks like:
2745 . .text
2746 . x:
2747 . bl .foo
2748 . nop
2750 The function definition in another object file might be:
2752 . .section .opd
2753 . foo: .quad .foo
2754 . .quad .TOC.@tocbase
2755 . .quad 0
2757 . .text
2758 . .foo: blr
2760 When the linker resolves the call during a static link, the branch
2761 unsurprisingly just goes to .foo and the .opd information is unused.
2762 If the function definition is in a shared library, things are a little
2763 different: The call goes via a plt call stub, the opd information gets
2764 copied to the plt, and the linker patches the nop.
2766 . x:
2767 . bl .foo_stub
2768 . ld 2,40(1)
2771 . .foo_stub:
2772 . std 2,40(1) # in practice, the call stub
2773 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2774 . addi 11,11,Lfoo@toc@l # this is the general idea
2775 . ld 12,0(11)
2776 . ld 2,8(11)
2777 . mtctr 12
2778 . ld 11,16(11)
2779 . bctr
2781 . .section .plt
2782 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2784 The "reloc ()" notation is supposed to indicate that the linker emits
2785 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2786 copying.
2788 What are the difficulties here? Well, firstly, the relocations
2789 examined by the linker in check_relocs are against the function code
2790 sym .foo, while the dynamic relocation in the plt is emitted against
2791 the function descriptor symbol, foo. Somewhere along the line, we need
2792 to carefully copy dynamic link information from one symbol to the other.
2793 Secondly, the generic part of the elf linker will make .foo a dynamic
2794 symbol as is normal for most other backends. We need foo dynamic
2795 instead, at least for an application final link. However, when
2796 creating a shared library containing foo, we need to have both symbols
2797 dynamic so that references to .foo are satisfied during the early
2798 stages of linking. Otherwise the linker might decide to pull in a
2799 definition from some other object, eg. a static library.
2801 Update: As of August 2004, we support a new convention. Function
2802 calls may use the function descriptor symbol, ie. "bl foo". This
2803 behaves exactly as "bl .foo". */
2805 /* Of those relocs that might be copied as dynamic relocs, this
2806 function selects those that must be copied when linking a shared
2807 library or PIE, even when the symbol is local. */
2809 static int
2810 must_be_dyn_reloc (struct bfd_link_info *info,
2811 enum elf_ppc64_reloc_type r_type)
2813 switch (r_type)
2815 default:
2816 /* Only relative relocs can be resolved when the object load
2817 address isn't fixed. DTPREL64 is excluded because the
2818 dynamic linker needs to differentiate global dynamic from
2819 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2820 return 1;
2822 case R_PPC64_REL32:
2823 case R_PPC64_REL64:
2824 case R_PPC64_REL30:
2825 case R_PPC64_TOC16:
2826 case R_PPC64_TOC16_DS:
2827 case R_PPC64_TOC16_LO:
2828 case R_PPC64_TOC16_HI:
2829 case R_PPC64_TOC16_HA:
2830 case R_PPC64_TOC16_LO_DS:
2831 return 0;
2833 case R_PPC64_TPREL16:
2834 case R_PPC64_TPREL16_LO:
2835 case R_PPC64_TPREL16_HI:
2836 case R_PPC64_TPREL16_HA:
2837 case R_PPC64_TPREL16_DS:
2838 case R_PPC64_TPREL16_LO_DS:
2839 case R_PPC64_TPREL16_HIGH:
2840 case R_PPC64_TPREL16_HIGHA:
2841 case R_PPC64_TPREL16_HIGHER:
2842 case R_PPC64_TPREL16_HIGHERA:
2843 case R_PPC64_TPREL16_HIGHEST:
2844 case R_PPC64_TPREL16_HIGHESTA:
2845 case R_PPC64_TPREL64:
2846 case R_PPC64_TPREL34:
2847 /* These relocations are relative but in a shared library the
2848 linker doesn't know the thread pointer base. */
2849 return bfd_link_dll (info);
2853 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2854 copying dynamic variables from a shared lib into an app's .dynbss
2855 section, and instead use a dynamic relocation to point into the
2856 shared lib. With code that gcc generates it is vital that this be
2857 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2858 actually the address of a function descriptor which resides in the
2859 .opd section. gcc uses the descriptor directly rather than going
2860 via the GOT as some other ABIs do, which means that initialized
2861 function pointers reference the descriptor. Thus, a function
2862 pointer initialized to the address of a function in a shared
2863 library will either require a .dynbss copy and a copy reloc, or a
2864 dynamic reloc. Using a .dynbss copy redefines the function
2865 descriptor symbol to point to the copy. This presents a problem as
2866 a PLT entry for that function is also initialized from the function
2867 descriptor symbol and the copy may not be initialized first. */
2868 #define ELIMINATE_COPY_RELOCS 1
2870 /* Section name for stubs is the associated section name plus this
2871 string. */
2872 #define STUB_SUFFIX ".stub"
2874 /* Linker stubs.
2875 ppc_stub_long_branch:
2876 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2877 destination, but a 24 bit branch in a stub section will reach.
2878 . b dest
2880 ppc_stub_plt_branch:
2881 Similar to the above, but a 24 bit branch in the stub section won't
2882 reach its destination.
2883 . addis %r12,%r2,xxx@toc@ha
2884 . ld %r12,xxx@toc@l(%r12)
2885 . mtctr %r12
2886 . bctr
2888 ppc_stub_plt_call:
2889 Used to call a function in a shared library. If it so happens that
2890 the plt entry referenced crosses a 64k boundary, then an extra
2891 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2892 An r2save variant starts with "std %r2,40(%r1)".
2893 . addis %r11,%r2,xxx@toc@ha
2894 . ld %r12,xxx+0@toc@l(%r11)
2895 . mtctr %r12
2896 . ld %r2,xxx+8@toc@l(%r11)
2897 . ld %r11,xxx+16@toc@l(%r11)
2898 . bctr
2900 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2901 code to adjust the value and save r2 to support multiple toc sections.
2902 A ppc_stub_long_branch with an r2 offset looks like:
2903 . std %r2,40(%r1)
2904 . addis %r2,%r2,off@ha
2905 . addi %r2,%r2,off@l
2906 . b dest
2908 A ppc_stub_plt_branch with an r2 offset looks like:
2909 . std %r2,40(%r1)
2910 . addis %r12,%r2,xxx@toc@ha
2911 . ld %r12,xxx@toc@l(%r12)
2912 . addis %r2,%r2,off@ha
2913 . addi %r2,%r2,off@l
2914 . mtctr %r12
2915 . bctr
2917 All of the above stubs are shown as their ELFv1 variants. ELFv2
2918 variants exist too, simpler for plt calls since a new toc pointer
2919 and static chain are not loaded by the stub. In addition, ELFv2
2920 has some more complex stubs to handle calls marked with NOTOC
2921 relocs from functions where r2 is not a valid toc pointer.
2922 ppc_stub_long_branch_p9notoc:
2923 . mflr %r12
2924 . bcl 20,31,1f
2925 . 1:
2926 . mflr %r11
2927 . mtlr %r12
2928 . addis %r12,%r11,dest-1b@ha
2929 . addi %r12,%r12,dest-1b@l
2930 . b dest
2932 ppc_stub_plt_branch_p9notoc:
2933 . mflr %r12
2934 . bcl 20,31,1f
2935 . 1:
2936 . mflr %r11
2937 . mtlr %r12
2938 . lis %r12,xxx-1b@highest
2939 . ori %r12,%r12,xxx-1b@higher
2940 . sldi %r12,%r12,32
2941 . oris %r12,%r12,xxx-1b@high
2942 . ori %r12,%r12,xxx-1b@l
2943 . add %r12,%r11,%r12
2944 . mtctr %r12
2945 . bctr
2947 ppc_stub_plt_call_p9notoc:
2948 . mflr %r12
2949 . bcl 20,31,1f
2950 . 1:
2951 . mflr %r11
2952 . mtlr %r12
2953 . lis %r12,xxx-1b@highest
2954 . ori %r12,%r12,xxx-1b@higher
2955 . sldi %r12,%r12,32
2956 . oris %r12,%r12,xxx-1b@high
2957 . ori %r12,%r12,xxx-1b@l
2958 . ldx %r12,%r11,%r12
2959 . mtctr %r12
2960 . bctr
2962 There are also ELFv1 power10 variants of these stubs.
2963 ppc_stub_long_branch_notoc:
2964 . pla %r12,dest@pcrel
2965 . b dest
2966 ppc_stub_plt_branch_notoc:
2967 . lis %r11,(dest-1f)@highesta34
2968 . ori %r11,%r11,(dest-1f)@highera34
2969 . sldi %r11,%r11,34
2970 . 1: pla %r12,dest@pcrel
2971 . add %r12,%r11,%r12
2972 . mtctr %r12
2973 . bctr
2974 ppc_stub_plt_call_notoc:
2975 . lis %r11,(xxx-1f)@highesta34
2976 . ori %r11,%r11,(xxx-1f)@highera34
2977 . sldi %r11,%r11,34
2978 . 1: pla %r12,xxx@pcrel
2979 . ldx %r12,%r11,%r12
2980 . mtctr %r12
2981 . bctr
2983 In cases where the high instructions would add zero, they are
2984 omitted and following instructions modified in some cases.
2985 For example, a power10 ppc_stub_plt_call_notoc might simplify down
2987 . pld %r12,xxx@pcrel
2988 . mtctr %r12
2989 . bctr
2991 Stub variants may be merged. For example, if printf is called from
2992 code with the tocsave optimization (ie. r2 saved in function
2993 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2994 and from other code without the tocsave optimization requiring a
2995 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2996 type will be created. Calls with the tocsave optimization will
2997 enter this stub after the instruction saving r2. A similar
2998 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2999 relocations. These require a ppc_stub_plt_call_notoc linkage stub
3000 to call an external function like printf. If other calls to printf
3001 require a ppc_stub_plt_call linkage stub then a single
3002 ppc_stub_plt_call_notoc linkage stub may be used for both types of
3003 call. */
3005 enum ppc_stub_main_type
3007 ppc_stub_none,
3008 ppc_stub_long_branch,
3009 ppc_stub_plt_branch,
3010 ppc_stub_plt_call,
3011 ppc_stub_global_entry,
3012 ppc_stub_save_res
3015 /* ppc_stub_long_branch, ppc_stub_plt_branch and ppc_stub_plt_call have
3016 these variations. */
3018 enum ppc_stub_sub_type
3020 ppc_stub_toc,
3021 ppc_stub_notoc,
3022 ppc_stub_p9notoc
3025 struct ppc_stub_type
3027 ENUM_BITFIELD (ppc_stub_main_type) main : 3;
3028 ENUM_BITFIELD (ppc_stub_sub_type) sub : 2;
3029 unsigned int r2save : 1;
3032 /* Information on stub grouping. */
3033 struct map_stub
3035 /* The stub section. */
3036 asection *stub_sec;
3037 /* This is the section to which stubs in the group will be attached. */
3038 asection *link_sec;
3039 /* Next group. */
3040 struct map_stub *next;
3041 /* Whether to emit a copy of register save/restore functions in this
3042 group. */
3043 int needs_save_res;
3044 /* Current offset within stubs after the insn restoring lr in a
3045 _notoc or _both stub using bcl for pc-relative addressing, or
3046 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3047 unsigned int lr_restore;
3048 /* Accumulated size of EH info emitted to describe return address
3049 if stubs modify lr. Does not include 17 byte FDE header. */
3050 unsigned int eh_size;
3051 /* Offset in glink_eh_frame to the start of EH info for this group. */
3052 unsigned int eh_base;
3055 struct ppc_stub_hash_entry
3057 /* Base hash table entry structure. */
3058 struct bfd_hash_entry root;
3060 struct ppc_stub_type type;
3062 /* Group information. */
3063 struct map_stub *group;
3065 /* Offset within stub_sec of the beginning of this stub. */
3066 bfd_vma stub_offset;
3068 /* Given the symbol's value and its section we can determine its final
3069 value when building the stubs (so the stub knows where to jump. */
3070 bfd_vma target_value;
3071 asection *target_section;
3073 /* The symbol table entry, if any, that this was derived from. */
3074 struct ppc_link_hash_entry *h;
3075 struct plt_entry *plt_ent;
3077 /* Symbol type. */
3078 unsigned char symtype;
3080 /* Symbol st_other. */
3081 unsigned char other;
3083 /* Debug: Track hash table traversal. */
3084 unsigned int id;
3087 struct ppc_branch_hash_entry
3089 /* Base hash table entry structure. */
3090 struct bfd_hash_entry root;
3092 /* Offset within branch lookup table. */
3093 unsigned int offset;
3095 /* Generation marker. */
3096 unsigned int iter;
3099 /* Used to track dynamic relocations. */
3100 struct ppc_dyn_relocs
3102 struct ppc_dyn_relocs *next;
3104 /* The input section of the reloc. */
3105 asection *sec;
3107 /* Total number of relocs copied for the input section. */
3108 unsigned int count;
3110 /* Number of pc-relative relocs copied for the input section. */
3111 unsigned int pc_count;
3113 /* Number of relocs that might become R_PPC64_RELATIVE. */
3114 unsigned int rel_count;
3117 struct ppc_local_dyn_relocs
3119 struct ppc_local_dyn_relocs *next;
3121 /* The input section of the reloc. */
3122 asection *sec;
3124 /* Total number of relocs copied for the input section. */
3125 unsigned int count;
3127 /* Number of relocs that might become R_PPC64_RELATIVE. */
3128 unsigned int rel_count : 31;
3130 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3131 unsigned int ifunc : 1;
3134 struct ppc_link_hash_entry
3136 struct elf_link_hash_entry elf;
3138 union
3140 /* A pointer to the most recently used stub hash entry against this
3141 symbol. */
3142 struct ppc_stub_hash_entry *stub_cache;
3144 /* A pointer to the next symbol starting with a '.' */
3145 struct ppc_link_hash_entry *next_dot_sym;
3146 } u;
3148 /* Link between function code and descriptor symbols. */
3149 struct ppc_link_hash_entry *oh;
3151 /* Flag function code and descriptor symbols. */
3152 unsigned int is_func:1;
3153 unsigned int is_func_descriptor:1;
3154 unsigned int fake:1;
3156 /* Whether global opd/toc sym has been adjusted or not.
3157 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3158 should be set for all globals defined in any opd/toc section. */
3159 unsigned int adjust_done:1;
3161 /* Set if this is an out-of-line register save/restore function,
3162 with non-standard calling convention. */
3163 unsigned int save_res:1;
3165 /* Set if a duplicate symbol with non-zero localentry is detected,
3166 even when the duplicate symbol does not provide a definition. */
3167 unsigned int non_zero_localentry:1;
3169 /* Contexts in which symbol is used in the GOT (or TOC).
3170 Bits are or'd into the mask as the corresponding relocs are
3171 encountered during check_relocs, with TLS_TLS being set when any
3172 of the other TLS bits are set. tls_optimize clears bits when
3173 optimizing to indicate the corresponding GOT entry type is not
3174 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3175 set TLS_GDIE when a GD reloc turns into an IE one.
3176 These flags are also kept for local symbols. */
3177 #define TLS_TLS 1 /* Any TLS reloc. */
3178 #define TLS_GD 2 /* GD reloc. */
3179 #define TLS_LD 4 /* LD reloc. */
3180 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
3181 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3182 #define TLS_MARK 32 /* __tls_get_addr call marked. */
3183 #define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
3184 #define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
3185 unsigned char tls_mask;
3187 /* The above field is also used to mark function symbols. In which
3188 case TLS_TLS will be 0. */
3189 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
3190 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
3191 #define NON_GOT 256 /* local symbol plt, not stored. */
3194 static inline struct ppc_link_hash_entry *
3195 ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3197 return (struct ppc_link_hash_entry *) ent;
3200 static inline struct elf_link_hash_entry *
3201 elf_hash_entry (struct ppc_link_hash_entry *ent)
3203 return (struct elf_link_hash_entry *) ent;
3206 /* ppc64 ELF linker hash table. */
3208 struct ppc_link_hash_table
3210 struct elf_link_hash_table elf;
3212 /* The stub hash table. */
3213 struct bfd_hash_table stub_hash_table;
3215 /* Another hash table for plt_branch stubs. */
3216 struct bfd_hash_table branch_hash_table;
3218 /* Hash table for function prologue tocsave. */
3219 htab_t tocsave_htab;
3221 /* Various options and other info passed from the linker. */
3222 struct ppc64_elf_params *params;
3224 /* The size of sec_info below. */
3225 unsigned int sec_info_arr_size;
3227 /* Per-section array of extra section info. Done this way rather
3228 than as part of ppc64_elf_section_data so we have the info for
3229 non-ppc64 sections. */
3230 struct
3232 /* Along with elf_gp, specifies the TOC pointer used by this section. */
3233 bfd_vma toc_off;
3235 union
3237 /* The section group that this section belongs to. */
3238 struct map_stub *group;
3239 /* A temp section list pointer. */
3240 asection *list;
3241 } u;
3242 } *sec_info;
3244 /* Linked list of groups. */
3245 struct map_stub *group;
3247 /* Temp used when calculating TOC pointers. */
3248 bfd_vma toc_curr;
3249 bfd *toc_bfd;
3250 asection *toc_first_sec;
3252 /* Used when adding symbols. */
3253 struct ppc_link_hash_entry *dot_syms;
3255 /* Shortcuts to get to dynamic linker sections. */
3256 asection *glink;
3257 asection *global_entry;
3258 asection *sfpr;
3259 asection *pltlocal;
3260 asection *relpltlocal;
3261 asection *brlt;
3262 asection *relbrlt;
3263 asection *glink_eh_frame;
3265 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3266 struct ppc_link_hash_entry *tls_get_addr;
3267 struct ppc_link_hash_entry *tls_get_addr_fd;
3268 struct ppc_link_hash_entry *tga_desc;
3269 struct ppc_link_hash_entry *tga_desc_fd;
3270 struct map_stub *tga_group;
3272 /* The size of reliplt used by got entry relocs. */
3273 bfd_size_type got_reli_size;
3275 /* DT_RELR array of section/r_offset. */
3276 size_t relr_alloc;
3277 size_t relr_count;
3278 struct
3280 asection *sec;
3281 bfd_vma off;
3282 } *relr;
3284 /* Statistics. */
3285 unsigned long stub_count[ppc_stub_save_res];
3287 /* Number of stubs against global syms. */
3288 unsigned long stub_globals;
3290 /* Set if we're linking code with function descriptors. */
3291 unsigned int opd_abi:1;
3293 /* Support for multiple toc sections. */
3294 unsigned int do_multi_toc:1;
3295 unsigned int multi_toc_needed:1;
3296 unsigned int second_toc_pass:1;
3297 unsigned int do_toc_opt:1;
3299 /* Set if tls optimization is enabled. */
3300 unsigned int do_tls_opt:1;
3302 /* Set if inline plt calls should be converted to direct calls. */
3303 unsigned int can_convert_all_inline_plt:1;
3305 /* Set if a stub_offset changed. */
3306 unsigned int stub_changed:1;
3308 /* Set on error. */
3309 unsigned int stub_error:1;
3311 /* Whether func_desc_adjust needs to be run over symbols. */
3312 unsigned int need_func_desc_adj:1;
3314 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3315 unsigned int has_plt_localentry0:1;
3317 /* Whether calls are made via the PLT from NOTOC functions. */
3318 unsigned int notoc_plt:1;
3320 /* Whether any code linked seems to be Power10. */
3321 unsigned int has_power10_relocs:1;
3323 /* Incremented once for each stub sized. */
3324 unsigned int stub_id;
3326 /* Incremented every time we size stubs. */
3327 unsigned int stub_iteration;
3329 /* After 20 iterations of stub sizing we no longer allow stubs to
3330 shrink. This is to break out of a pathological case where adding
3331 stubs or increasing their size on one iteration decreases section
3332 gaps (perhaps due to alignment), which then results in smaller
3333 stubs on the next iteration. */
3334 #define STUB_SHRINK_ITER 20
3337 /* Rename some of the generic section flags to better document how they
3338 are used here. */
3340 /* Nonzero if this section has TLS related relocations. */
3341 #define has_tls_reloc sec_flg0
3343 /* Nonzero if this section has a call to __tls_get_addr lacking marker
3344 relocations. */
3345 #define nomark_tls_get_addr sec_flg1
3347 /* Nonzero if this section has any toc or got relocs. */
3348 #define has_toc_reloc sec_flg2
3350 /* Nonzero if this section has a call to another section that uses
3351 the toc or got. */
3352 #define makes_toc_func_call sec_flg3
3354 /* Recursion protection when determining above flag. */
3355 #define call_check_in_progress sec_flg4
3356 #define call_check_done sec_flg5
3358 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3360 #define ppc_hash_table(p) \
3361 ((is_elf_hash_table ((p)->hash) \
3362 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3363 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
3365 #define ppc_stub_hash_lookup(table, string, create, copy) \
3366 ((struct ppc_stub_hash_entry *) \
3367 bfd_hash_lookup ((table), (string), (create), (copy)))
3369 #define ppc_branch_hash_lookup(table, string, create, copy) \
3370 ((struct ppc_branch_hash_entry *) \
3371 bfd_hash_lookup ((table), (string), (create), (copy)))
3373 /* Create an entry in the stub hash table. */
3375 static struct bfd_hash_entry *
3376 stub_hash_newfunc (struct bfd_hash_entry *entry,
3377 struct bfd_hash_table *table,
3378 const char *string)
3380 /* Allocate the structure if it has not already been allocated by a
3381 subclass. */
3382 if (entry == NULL)
3384 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3385 if (entry == NULL)
3386 return entry;
3389 /* Call the allocation method of the superclass. */
3390 entry = bfd_hash_newfunc (entry, table, string);
3391 if (entry != NULL)
3393 struct ppc_stub_hash_entry *eh;
3395 /* Initialize the local fields. */
3396 eh = (struct ppc_stub_hash_entry *) entry;
3397 eh->type.main = ppc_stub_none;
3398 eh->type.sub = ppc_stub_toc;
3399 eh->type.r2save = 0;
3400 eh->group = NULL;
3401 eh->stub_offset = 0;
3402 eh->target_value = 0;
3403 eh->target_section = NULL;
3404 eh->h = NULL;
3405 eh->plt_ent = NULL;
3406 eh->symtype = 0;
3407 eh->other = 0;
3408 eh->id = 0;
3411 return entry;
3414 /* Create an entry in the branch hash table. */
3416 static struct bfd_hash_entry *
3417 branch_hash_newfunc (struct bfd_hash_entry *entry,
3418 struct bfd_hash_table *table,
3419 const char *string)
3421 /* Allocate the structure if it has not already been allocated by a
3422 subclass. */
3423 if (entry == NULL)
3425 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3426 if (entry == NULL)
3427 return entry;
3430 /* Call the allocation method of the superclass. */
3431 entry = bfd_hash_newfunc (entry, table, string);
3432 if (entry != NULL)
3434 struct ppc_branch_hash_entry *eh;
3436 /* Initialize the local fields. */
3437 eh = (struct ppc_branch_hash_entry *) entry;
3438 eh->offset = 0;
3439 eh->iter = 0;
3442 return entry;
3445 /* Create an entry in a ppc64 ELF linker hash table. */
3447 static struct bfd_hash_entry *
3448 link_hash_newfunc (struct bfd_hash_entry *entry,
3449 struct bfd_hash_table *table,
3450 const char *string)
3452 /* Allocate the structure if it has not already been allocated by a
3453 subclass. */
3454 if (entry == NULL)
3456 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3457 if (entry == NULL)
3458 return entry;
3461 /* Call the allocation method of the superclass. */
3462 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3463 if (entry != NULL)
3465 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3467 memset (&eh->u.stub_cache, 0,
3468 (sizeof (struct ppc_link_hash_entry)
3469 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3471 /* When making function calls, old ABI code references function entry
3472 points (dot symbols), while new ABI code references the function
3473 descriptor symbol. We need to make any combination of reference and
3474 definition work together, without breaking archive linking.
3476 For a defined function "foo" and an undefined call to "bar":
3477 An old object defines "foo" and ".foo", references ".bar" (possibly
3478 "bar" too).
3479 A new object defines "foo" and references "bar".
3481 A new object thus has no problem with its undefined symbols being
3482 satisfied by definitions in an old object. On the other hand, the
3483 old object won't have ".bar" satisfied by a new object.
3485 Keep a list of newly added dot-symbols. */
3487 if (string[0] == '.')
3489 struct ppc_link_hash_table *htab;
3491 htab = (struct ppc_link_hash_table *) table;
3492 eh->u.next_dot_sym = htab->dot_syms;
3493 htab->dot_syms = eh;
3497 return entry;
3500 struct tocsave_entry
3502 asection *sec;
3503 bfd_vma offset;
3506 static hashval_t
3507 tocsave_htab_hash (const void *p)
3509 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3510 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3513 static int
3514 tocsave_htab_eq (const void *p1, const void *p2)
3516 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3517 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3518 return e1->sec == e2->sec && e1->offset == e2->offset;
3521 /* Destroy a ppc64 ELF linker hash table. */
3523 static void
3524 ppc64_elf_link_hash_table_free (bfd *obfd)
3526 struct ppc_link_hash_table *htab;
3528 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3529 free (htab->relr);
3530 if (htab->tocsave_htab)
3531 htab_delete (htab->tocsave_htab);
3532 bfd_hash_table_free (&htab->branch_hash_table);
3533 bfd_hash_table_free (&htab->stub_hash_table);
3534 _bfd_elf_link_hash_table_free (obfd);
3537 /* Create a ppc64 ELF linker hash table. */
3539 static struct bfd_link_hash_table *
3540 ppc64_elf_link_hash_table_create (bfd *abfd)
3542 struct ppc_link_hash_table *htab;
3543 size_t amt = sizeof (struct ppc_link_hash_table);
3545 htab = bfd_zmalloc (amt);
3546 if (htab == NULL)
3547 return NULL;
3549 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3550 sizeof (struct ppc_link_hash_entry)))
3552 free (htab);
3553 return NULL;
3556 /* Init the stub hash table too. */
3557 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3558 sizeof (struct ppc_stub_hash_entry)))
3560 _bfd_elf_link_hash_table_free (abfd);
3561 return NULL;
3564 /* And the branch hash table. */
3565 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3566 sizeof (struct ppc_branch_hash_entry)))
3568 bfd_hash_table_free (&htab->stub_hash_table);
3569 _bfd_elf_link_hash_table_free (abfd);
3570 return NULL;
3573 htab->tocsave_htab = htab_try_create (1024,
3574 tocsave_htab_hash,
3575 tocsave_htab_eq,
3576 NULL);
3577 if (htab->tocsave_htab == NULL)
3579 ppc64_elf_link_hash_table_free (abfd);
3580 return NULL;
3582 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3584 /* Initializing two fields of the union is just cosmetic. We really
3585 only care about glist, but when compiled on a 32-bit host the
3586 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3587 debugger inspection of these fields look nicer. */
3588 htab->elf.init_got_refcount.refcount = 0;
3589 htab->elf.init_got_refcount.glist = NULL;
3590 htab->elf.init_plt_refcount.refcount = 0;
3591 htab->elf.init_plt_refcount.glist = NULL;
3592 htab->elf.init_got_offset.offset = 0;
3593 htab->elf.init_got_offset.glist = NULL;
3594 htab->elf.init_plt_offset.offset = 0;
3595 htab->elf.init_plt_offset.glist = NULL;
3597 return &htab->elf.root;
3600 /* Create sections for linker generated code. */
3602 static bool
3603 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3605 struct ppc_link_hash_table *htab;
3606 flagword flags;
3608 htab = ppc_hash_table (info);
3610 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3611 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3612 if (htab->params->save_restore_funcs)
3614 /* Create .sfpr for code to save and restore fp regs. */
3615 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3616 flags);
3617 if (htab->sfpr == NULL
3618 || !bfd_set_section_alignment (htab->sfpr, 2))
3619 return false;
3622 if (bfd_link_relocatable (info))
3623 return true;
3625 /* Create .glink for lazy dynamic linking support. */
3626 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3627 flags);
3628 if (htab->glink == NULL
3629 || !bfd_set_section_alignment (htab->glink, 3))
3630 return false;
3632 /* The part of .glink used by global entry stubs, separate so that
3633 it can be aligned appropriately without affecting htab->glink. */
3634 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3635 flags);
3636 if (htab->global_entry == NULL
3637 || !bfd_set_section_alignment (htab->global_entry, 2))
3638 return false;
3640 if (!info->no_ld_generated_unwind_info)
3642 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3643 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3644 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3645 ".eh_frame",
3646 flags);
3647 if (htab->glink_eh_frame == NULL
3648 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
3649 return false;
3652 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3653 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3654 if (htab->elf.iplt == NULL
3655 || !bfd_set_section_alignment (htab->elf.iplt, 3))
3656 return false;
3658 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3659 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3660 htab->elf.irelplt
3661 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3662 if (htab->elf.irelplt == NULL
3663 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
3664 return false;
3666 /* Create branch lookup table for plt_branch stubs. */
3667 flags = (SEC_ALLOC | SEC_LOAD
3668 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3669 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3670 flags);
3671 if (htab->brlt == NULL
3672 || !bfd_set_section_alignment (htab->brlt, 3))
3673 return false;
3675 /* Local plt entries, put in .branch_lt but a separate section for
3676 convenience. */
3677 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3678 flags);
3679 if (htab->pltlocal == NULL
3680 || !bfd_set_section_alignment (htab->pltlocal, 3))
3681 return false;
3683 if (!bfd_link_pic (info))
3684 return true;
3686 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3687 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3688 htab->relbrlt
3689 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3690 if (htab->relbrlt == NULL
3691 || !bfd_set_section_alignment (htab->relbrlt, 3))
3692 return false;
3694 htab->relpltlocal
3695 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3696 if (htab->relpltlocal == NULL
3697 || !bfd_set_section_alignment (htab->relpltlocal, 3))
3698 return false;
3700 return true;
3703 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3705 bool
3706 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3707 struct ppc64_elf_params *params)
3709 struct ppc_link_hash_table *htab;
3711 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3713 /* Always hook our dynamic sections into the first bfd, which is the
3714 linker created stub bfd. This ensures that the GOT header is at
3715 the start of the output TOC section. */
3716 htab = ppc_hash_table (info);
3717 htab->elf.dynobj = params->stub_bfd;
3718 htab->params = params;
3720 return create_linkage_sections (htab->elf.dynobj, info);
3723 /* Build a name for an entry in the stub hash table. */
3725 static char *
3726 ppc_stub_name (const asection *input_section,
3727 const asection *sym_sec,
3728 const struct ppc_link_hash_entry *h,
3729 const Elf_Internal_Rela *rel)
3731 char *stub_name;
3732 ssize_t len;
3734 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3735 offsets from a sym as a branch target? In fact, we could
3736 probably assume the addend is always zero. */
3737 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3739 if (h)
3741 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3742 stub_name = bfd_malloc (len);
3743 if (stub_name == NULL)
3744 return stub_name;
3746 len = sprintf (stub_name, "%08x.%s+%x",
3747 input_section->id & 0xffffffff,
3748 h->elf.root.root.string,
3749 (int) rel->r_addend & 0xffffffff);
3751 else
3753 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3754 stub_name = bfd_malloc (len);
3755 if (stub_name == NULL)
3756 return stub_name;
3758 len = sprintf (stub_name, "%08x.%x:%x+%x",
3759 input_section->id & 0xffffffff,
3760 sym_sec->id & 0xffffffff,
3761 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3762 (int) rel->r_addend & 0xffffffff);
3764 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3765 stub_name[len - 2] = 0;
3766 return stub_name;
3769 /* If mixing power10 with non-power10 code and --power10-stubs is not
3770 specified (or is auto) then there may be multiple stub types for any
3771 given symbol. Up to three classes of stubs are stored in separate
3772 stub_hash_table entries having the same key string. The entries
3773 will always be adjacent on entry->root.next chain, even if hash
3774 table resizing occurs. This function selects the correct entry to
3775 use. */
3777 static struct ppc_stub_hash_entry *
3778 select_alt_stub (struct ppc_stub_hash_entry *entry,
3779 enum elf_ppc64_reloc_type r_type)
3781 enum ppc_stub_sub_type subt;
3783 switch (r_type)
3785 case R_PPC64_REL24_NOTOC:
3786 subt = ppc_stub_notoc;
3787 break;
3788 case R_PPC64_REL24_P9NOTOC:
3789 subt = ppc_stub_p9notoc;
3790 break;
3791 default:
3792 subt = ppc_stub_toc;
3793 break;
3796 while (entry != NULL && entry->type.sub != subt)
3798 const char *stub_name = entry->root.string;
3800 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3801 if (entry != NULL
3802 && entry->root.string != stub_name)
3803 entry = NULL;
3806 return entry;
3809 /* Look up an entry in the stub hash. Stub entries are cached because
3810 creating the stub name takes a bit of time. */
3812 static struct ppc_stub_hash_entry *
3813 ppc_get_stub_entry (const asection *input_section,
3814 const asection *sym_sec,
3815 struct ppc_link_hash_entry *h,
3816 const Elf_Internal_Rela *rel,
3817 struct ppc_link_hash_table *htab)
3819 struct ppc_stub_hash_entry *stub_entry;
3820 struct map_stub *group;
3822 /* If this input section is part of a group of sections sharing one
3823 stub section, then use the id of the first section in the group.
3824 Stub names need to include a section id, as there may well be
3825 more than one stub used to reach say, printf, and we need to
3826 distinguish between them. */
3827 group = htab->sec_info[input_section->id].u.group;
3828 if (group == NULL)
3829 return NULL;
3831 if (h != NULL && h->u.stub_cache != NULL
3832 && h->u.stub_cache->h == h
3833 && h->u.stub_cache->group == group)
3835 stub_entry = h->u.stub_cache;
3837 else
3839 char *stub_name;
3841 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3842 if (stub_name == NULL)
3843 return NULL;
3845 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3846 stub_name, false, false);
3847 if (h != NULL)
3848 h->u.stub_cache = stub_entry;
3850 free (stub_name);
3853 if (stub_entry != NULL && htab->params->power10_stubs == -1)
3854 stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
3856 return stub_entry;
3859 /* Add a new stub entry to the stub hash. Not all fields of the new
3860 stub entry are initialised. */
3862 static struct ppc_stub_hash_entry *
3863 ppc_add_stub (const char *stub_name,
3864 asection *section,
3865 struct bfd_link_info *info)
3867 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3868 struct map_stub *group;
3869 asection *link_sec;
3870 asection *stub_sec;
3871 struct ppc_stub_hash_entry *stub_entry;
3873 group = htab->sec_info[section->id].u.group;
3874 link_sec = group->link_sec;
3875 stub_sec = group->stub_sec;
3876 if (stub_sec == NULL)
3878 size_t namelen;
3879 bfd_size_type len;
3880 char *s_name;
3882 namelen = strlen (link_sec->name);
3883 len = namelen + sizeof (STUB_SUFFIX);
3884 s_name = bfd_alloc (htab->params->stub_bfd, len);
3885 if (s_name == NULL)
3886 return NULL;
3888 memcpy (s_name, link_sec->name, namelen);
3889 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3890 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3891 if (stub_sec == NULL)
3892 return NULL;
3893 group->stub_sec = stub_sec;
3896 /* Enter this entry into the linker stub hash table. */
3897 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3898 true, true);
3899 if (stub_entry == NULL)
3901 /* xgettext:c-format */
3902 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3903 section->owner, stub_name);
3904 return NULL;
3907 stub_entry->group = group;
3908 stub_entry->stub_offset = 0;
3909 return stub_entry;
3912 /* A stub has already been created, but it may not be the required
3913 type. We shouldn't be transitioning from plt_call to long_branch
3914 stubs or vice versa, but we might be upgrading from plt_call to
3915 plt_call with r2save for example. */
3917 static bool
3918 ppc_merge_stub (struct ppc_link_hash_table *htab,
3919 struct ppc_stub_hash_entry *stub_entry,
3920 struct ppc_stub_type stub_type,
3921 enum elf_ppc64_reloc_type r_type)
3923 struct ppc_stub_type old_type = stub_entry->type;
3925 if (old_type.main == ppc_stub_save_res)
3926 return true;
3928 if (htab->params->power10_stubs == -1)
3930 /* For --power10-stubs=auto, don't merge _notoc and other
3931 varieties of stubs. */
3932 struct ppc_stub_hash_entry *alt_stub;
3934 alt_stub = select_alt_stub (stub_entry, r_type);
3935 if (alt_stub == NULL)
3937 alt_stub = ((struct ppc_stub_hash_entry *)
3938 stub_hash_newfunc (NULL,
3939 &htab->stub_hash_table,
3940 stub_entry->root.string));
3941 if (alt_stub == NULL)
3942 return false;
3944 *alt_stub = *stub_entry;
3945 stub_entry->root.next = &alt_stub->root;
3947 /* Sort notoc stubs first, then toc stubs, then p9notoc.
3948 Not that it matters, this just puts smaller stubs first. */
3949 if (stub_type.sub == ppc_stub_notoc)
3950 alt_stub = stub_entry;
3951 else if (stub_type.sub == ppc_stub_p9notoc
3952 && alt_stub->root.next
3953 && alt_stub->root.next->string == alt_stub->root.string)
3955 struct ppc_stub_hash_entry *next
3956 = (struct ppc_stub_hash_entry *) alt_stub->root.next;
3957 alt_stub->type = next->type;
3958 alt_stub = next;
3960 alt_stub->type = stub_type;
3961 return true;
3963 stub_entry = alt_stub;
3966 old_type = stub_entry->type;
3967 if (old_type.main == ppc_stub_plt_branch)
3968 old_type.main = ppc_stub_long_branch;
3970 if (old_type.main != stub_type.main
3971 || (old_type.sub != stub_type.sub
3972 && old_type.sub != ppc_stub_toc
3973 && stub_type.sub != ppc_stub_toc))
3974 abort ();
3976 stub_entry->type.sub |= stub_type.sub;
3977 stub_entry->type.r2save |= stub_type.r2save;
3978 return true;
3981 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3982 not already done. */
3984 static bool
3985 create_got_section (bfd *abfd, struct bfd_link_info *info)
3987 asection *got, *relgot;
3988 flagword flags;
3989 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3991 if (!is_ppc64_elf (abfd))
3992 return false;
3993 if (htab == NULL)
3994 return false;
3996 if (!htab->elf.sgot
3997 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3998 return false;
4000 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4001 | SEC_LINKER_CREATED);
4003 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4004 if (!got
4005 || !bfd_set_section_alignment (got, 3))
4006 return false;
4008 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4009 flags | SEC_READONLY);
4010 if (!relgot
4011 || !bfd_set_section_alignment (relgot, 3))
4012 return false;
4014 ppc64_elf_tdata (abfd)->got = got;
4015 ppc64_elf_tdata (abfd)->relgot = relgot;
4016 return true;
4019 /* Follow indirect and warning symbol links. */
4021 static inline struct bfd_link_hash_entry *
4022 follow_link (struct bfd_link_hash_entry *h)
4024 while (h->type == bfd_link_hash_indirect
4025 || h->type == bfd_link_hash_warning)
4026 h = h->u.i.link;
4027 return h;
4030 static inline struct elf_link_hash_entry *
4031 elf_follow_link (struct elf_link_hash_entry *h)
4033 return (struct elf_link_hash_entry *) follow_link (&h->root);
4036 static inline struct ppc_link_hash_entry *
4037 ppc_follow_link (struct ppc_link_hash_entry *h)
4039 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
4042 /* Merge PLT info on FROM with that on TO. */
4044 static void
4045 move_plt_plist (struct ppc_link_hash_entry *from,
4046 struct ppc_link_hash_entry *to)
4048 if (from->elf.plt.plist != NULL)
4050 if (to->elf.plt.plist != NULL)
4052 struct plt_entry **entp;
4053 struct plt_entry *ent;
4055 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4057 struct plt_entry *dent;
4059 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4060 if (dent->addend == ent->addend)
4062 dent->plt.refcount += ent->plt.refcount;
4063 *entp = ent->next;
4064 break;
4066 if (dent == NULL)
4067 entp = &ent->next;
4069 *entp = to->elf.plt.plist;
4072 to->elf.plt.plist = from->elf.plt.plist;
4073 from->elf.plt.plist = NULL;
4077 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4079 static void
4080 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4081 struct elf_link_hash_entry *dir,
4082 struct elf_link_hash_entry *ind)
4084 struct ppc_link_hash_entry *edir, *eind;
4086 edir = ppc_elf_hash_entry (dir);
4087 eind = ppc_elf_hash_entry (ind);
4089 edir->is_func |= eind->is_func;
4090 edir->is_func_descriptor |= eind->is_func_descriptor;
4091 edir->tls_mask |= eind->tls_mask;
4092 if (eind->oh != NULL)
4093 edir->oh = ppc_follow_link (eind->oh);
4095 if (edir->elf.versioned != versioned_hidden)
4096 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4097 edir->elf.ref_regular |= eind->elf.ref_regular;
4098 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4099 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4100 edir->elf.needs_plt |= eind->elf.needs_plt;
4101 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4103 /* If we were called to copy over info for a weak sym, don't copy
4104 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4105 in order to simplify readonly_dynrelocs and save a field in the
4106 symbol hash entry, but that means dyn_relocs can't be used in any
4107 tests about a specific symbol, or affect other symbol flags which
4108 are then tested. */
4109 if (eind->elf.root.type != bfd_link_hash_indirect)
4110 return;
4112 /* Copy over any dynamic relocs we may have on the indirect sym. */
4113 if (ind->dyn_relocs != NULL)
4115 if (dir->dyn_relocs != NULL)
4117 struct ppc_dyn_relocs **pp;
4118 struct ppc_dyn_relocs *p;
4120 /* Add reloc counts against the indirect sym to the direct sym
4121 list. Merge any entries against the same section. */
4122 for (pp = (struct ppc_dyn_relocs **) &ind->dyn_relocs;
4123 (p = *pp) != NULL;
4126 struct ppc_dyn_relocs *q;
4128 for (q = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4129 q != NULL;
4130 q = q->next)
4131 if (q->sec == p->sec)
4133 q->count += p->count;
4134 q->pc_count += p->pc_count;
4135 q->rel_count += p->rel_count;
4136 *pp = p->next;
4137 break;
4139 if (q == NULL)
4140 pp = &p->next;
4142 *pp = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4145 dir->dyn_relocs = ind->dyn_relocs;
4146 ind->dyn_relocs = NULL;
4149 /* Copy over got entries that we may have already seen to the
4150 symbol which just became indirect. */
4151 if (eind->elf.got.glist != NULL)
4153 if (edir->elf.got.glist != NULL)
4155 struct got_entry **entp;
4156 struct got_entry *ent;
4158 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4160 struct got_entry *dent;
4162 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4163 if (dent->addend == ent->addend
4164 && dent->owner == ent->owner
4165 && dent->tls_type == ent->tls_type)
4167 dent->got.refcount += ent->got.refcount;
4168 *entp = ent->next;
4169 break;
4171 if (dent == NULL)
4172 entp = &ent->next;
4174 *entp = edir->elf.got.glist;
4177 edir->elf.got.glist = eind->elf.got.glist;
4178 eind->elf.got.glist = NULL;
4181 /* And plt entries. */
4182 move_plt_plist (eind, edir);
4184 if (eind->elf.dynindx != -1)
4186 if (edir->elf.dynindx != -1)
4187 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4188 edir->elf.dynstr_index);
4189 edir->elf.dynindx = eind->elf.dynindx;
4190 edir->elf.dynstr_index = eind->elf.dynstr_index;
4191 eind->elf.dynindx = -1;
4192 eind->elf.dynstr_index = 0;
4196 /* Find the function descriptor hash entry from the given function code
4197 hash entry FH. Link the entries via their OH fields. */
4199 static struct ppc_link_hash_entry *
4200 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4202 struct ppc_link_hash_entry *fdh = fh->oh;
4204 if (fdh == NULL)
4206 const char *fd_name = fh->elf.root.root.string + 1;
4208 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
4209 false, false, false));
4210 if (fdh == NULL)
4211 return fdh;
4213 fdh->is_func_descriptor = 1;
4214 fdh->oh = fh;
4215 fh->is_func = 1;
4216 fh->oh = fdh;
4219 fdh = ppc_follow_link (fdh);
4220 fdh->is_func_descriptor = 1;
4221 fdh->oh = fh;
4222 return fdh;
4225 /* Make a fake function descriptor sym for the undefined code sym FH. */
4227 static struct ppc_link_hash_entry *
4228 make_fdh (struct bfd_link_info *info,
4229 struct ppc_link_hash_entry *fh)
4231 bfd *abfd = fh->elf.root.u.undef.abfd;
4232 struct bfd_link_hash_entry *bh = NULL;
4233 struct ppc_link_hash_entry *fdh;
4234 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4235 ? BSF_WEAK
4236 : BSF_GLOBAL);
4238 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4239 fh->elf.root.root.string + 1,
4240 flags, bfd_und_section_ptr, 0,
4241 NULL, false, false, &bh))
4242 return NULL;
4244 fdh = (struct ppc_link_hash_entry *) bh;
4245 fdh->elf.non_elf = 0;
4246 fdh->fake = 1;
4247 fdh->is_func_descriptor = 1;
4248 fdh->oh = fh;
4249 fh->is_func = 1;
4250 fh->oh = fdh;
4251 return fdh;
4254 /* Fix function descriptor symbols defined in .opd sections to be
4255 function type. */
4257 static bool
4258 ppc64_elf_add_symbol_hook (bfd *ibfd,
4259 struct bfd_link_info *info,
4260 Elf_Internal_Sym *isym,
4261 const char **name,
4262 flagword *flags ATTRIBUTE_UNUSED,
4263 asection **sec,
4264 bfd_vma *value)
4266 if (*sec != NULL
4267 && strcmp ((*sec)->name, ".opd") == 0)
4269 asection *code_sec;
4271 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4272 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4273 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4275 /* If the symbol is a function defined in .opd, and the function
4276 code is in a discarded group, let it appear to be undefined. */
4277 if (!bfd_link_relocatable (info)
4278 && (*sec)->reloc_count != 0
4279 && opd_entry_value (*sec, *value, &code_sec, NULL,
4280 false) != (bfd_vma) -1
4281 && discarded_section (code_sec))
4283 *sec = bfd_und_section_ptr;
4284 isym->st_shndx = SHN_UNDEF;
4287 else if (*sec != NULL
4288 && strcmp ((*sec)->name, ".toc") == 0
4289 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4291 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4292 if (htab != NULL)
4293 htab->params->object_in_toc = 1;
4296 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4298 if (abiversion (ibfd) == 0)
4299 set_abiversion (ibfd, 2);
4300 else if (abiversion (ibfd) == 1)
4302 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4303 " for ABI version 1"), *name);
4304 bfd_set_error (bfd_error_bad_value);
4305 return false;
4309 return true;
4312 /* Merge non-visibility st_other attributes: local entry point. */
4314 static void
4315 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4316 unsigned int st_other,
4317 bool definition,
4318 bool dynamic)
4320 if (definition && (!dynamic || !h->def_regular))
4321 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
4322 | ELF_ST_VISIBILITY (h->other));
4325 /* Hook called on merging a symbol. We use this to clear "fake" since
4326 we now have a real symbol. */
4328 static bool
4329 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4330 const Elf_Internal_Sym *isym,
4331 asection **psec ATTRIBUTE_UNUSED,
4332 bool newdef ATTRIBUTE_UNUSED,
4333 bool olddef ATTRIBUTE_UNUSED,
4334 bfd *oldbfd ATTRIBUTE_UNUSED,
4335 const asection *oldsec ATTRIBUTE_UNUSED)
4337 ppc_elf_hash_entry (h)->fake = 0;
4338 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4339 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
4340 return true;
4343 /* This function makes an old ABI object reference to ".bar" cause the
4344 inclusion of a new ABI object archive that defines "bar".
4345 NAME is a symbol defined in an archive. Return a symbol in the hash
4346 table that might be satisfied by the archive symbols. */
4348 static struct bfd_link_hash_entry *
4349 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4350 struct bfd_link_info *info,
4351 const char *name)
4353 struct bfd_link_hash_entry *h;
4354 char *dot_name;
4355 size_t len;
4357 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4358 if (h != NULL
4359 && ppc_hash_table (info) != NULL
4360 /* Don't return this sym if it is a fake function descriptor
4361 created by add_symbol_adjust. */
4362 && !((struct ppc_link_hash_entry *) h)->fake)
4363 return h;
4365 if (name[0] == '.')
4366 return h;
4368 len = strlen (name);
4369 dot_name = bfd_alloc (abfd, len + 2);
4370 if (dot_name == NULL)
4371 return (struct bfd_link_hash_entry *) -1;
4372 dot_name[0] = '.';
4373 memcpy (dot_name + 1, name, len + 1);
4374 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4375 bfd_release (abfd, dot_name);
4376 if (h != NULL)
4377 return h;
4379 if (strcmp (name, "__tls_get_addr_opt") == 0)
4380 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
4381 return h;
4384 /* This function satisfies all old ABI object references to ".bar" if a
4385 new ABI object defines "bar". Well, at least, undefined dot symbols
4386 are made weak. This stops later archive searches from including an
4387 object if we already have a function descriptor definition. It also
4388 prevents the linker complaining about undefined symbols.
4389 We also check and correct mismatched symbol visibility here. The
4390 most restrictive visibility of the function descriptor and the
4391 function entry symbol is used. */
4393 static bool
4394 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4396 struct ppc_link_hash_table *htab;
4397 struct ppc_link_hash_entry *fdh;
4399 if (eh->elf.root.type == bfd_link_hash_warning)
4400 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4402 if (eh->elf.root.type == bfd_link_hash_indirect)
4403 return true;
4405 if (eh->elf.root.root.string[0] != '.')
4406 abort ();
4408 htab = ppc_hash_table (info);
4409 if (htab == NULL)
4410 return false;
4412 fdh = lookup_fdh (eh, htab);
4413 if (fdh == NULL
4414 && !bfd_link_relocatable (info)
4415 && (eh->elf.root.type == bfd_link_hash_undefined
4416 || eh->elf.root.type == bfd_link_hash_undefweak)
4417 && eh->elf.ref_regular)
4419 /* Make an undefined function descriptor sym, in order to
4420 pull in an --as-needed shared lib. Archives are handled
4421 elsewhere. */
4422 fdh = make_fdh (info, eh);
4423 if (fdh == NULL)
4424 return false;
4427 if (fdh != NULL)
4429 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4430 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4432 /* Make both descriptor and entry symbol have the most
4433 constraining visibility of either symbol. */
4434 if (entry_vis < descr_vis)
4435 fdh->elf.other += entry_vis - descr_vis;
4436 else if (entry_vis > descr_vis)
4437 eh->elf.other += descr_vis - entry_vis;
4439 /* Propagate reference flags from entry symbol to function
4440 descriptor symbol. */
4441 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4442 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4443 fdh->elf.ref_regular |= eh->elf.ref_regular;
4444 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4446 if (!fdh->elf.forced_local
4447 && fdh->elf.dynindx == -1
4448 && fdh->elf.versioned != versioned_hidden
4449 && (bfd_link_dll (info)
4450 || fdh->elf.def_dynamic
4451 || fdh->elf.ref_dynamic)
4452 && (eh->elf.ref_regular
4453 || eh->elf.def_regular))
4455 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4456 return false;
4460 return true;
4463 /* Set up opd section info and abiversion for IBFD, and process list
4464 of dot-symbols we made in link_hash_newfunc. */
4466 static bool
4467 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4469 struct ppc_link_hash_table *htab;
4470 struct ppc_link_hash_entry **p, *eh;
4471 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4473 if (opd != NULL && opd->size != 0)
4475 if (ppc64_elf_section_data (opd)->sec_type == sec_normal)
4476 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4477 else if (ppc64_elf_section_data (opd)->sec_type != sec_opd)
4478 BFD_FAIL ();
4480 if (abiversion (ibfd) == 0)
4481 set_abiversion (ibfd, 1);
4482 else if (abiversion (ibfd) >= 2)
4484 /* xgettext:c-format */
4485 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4486 ibfd, abiversion (ibfd));
4487 bfd_set_error (bfd_error_bad_value);
4488 return false;
4492 if (is_ppc64_elf (info->output_bfd))
4494 /* For input files without an explicit abiversion in e_flags
4495 we should have flagged any with symbol st_other bits set
4496 as ELFv2 and above flagged those with .opd as ELFv1.
4497 Set the output abiversion if not yet set, and for any input
4498 still ambiguous, take its abiversion from the output.
4499 Differences in ABI are reported later. */
4500 if (abiversion (info->output_bfd) == 0)
4501 set_abiversion (info->output_bfd, abiversion (ibfd));
4502 else if (abiversion (ibfd) == 0)
4503 set_abiversion (ibfd, abiversion (info->output_bfd));
4506 htab = ppc_hash_table (info);
4507 if (htab == NULL)
4508 return true;
4510 if (opd != NULL && opd->size != 0
4511 && (ibfd->flags & DYNAMIC) == 0
4512 && (opd->flags & SEC_RELOC) != 0
4513 && opd->reloc_count != 0
4514 && !bfd_is_abs_section (opd->output_section)
4515 && info->gc_sections)
4517 /* Garbage collection needs some extra help with .opd sections.
4518 We don't want to necessarily keep everything referenced by
4519 relocs in .opd, as that would keep all functions. Instead,
4520 if we reference an .opd symbol (a function descriptor), we
4521 want to keep the function code symbol's section. This is
4522 easy for global symbols, but for local syms we need to keep
4523 information about the associated function section. */
4524 bfd_size_type amt;
4525 asection **opd_sym_map;
4526 Elf_Internal_Shdr *symtab_hdr;
4527 Elf_Internal_Rela *relocs, *rel_end, *rel;
4529 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4530 opd_sym_map = bfd_zalloc (ibfd, amt);
4531 if (opd_sym_map == NULL)
4532 return false;
4533 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4534 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4535 info->keep_memory);
4536 if (relocs == NULL)
4537 return false;
4538 symtab_hdr = &elf_symtab_hdr (ibfd);
4539 rel_end = relocs + opd->reloc_count - 1;
4540 for (rel = relocs; rel < rel_end; rel++)
4542 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4543 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4545 if (r_type == R_PPC64_ADDR64
4546 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4547 && r_symndx < symtab_hdr->sh_info)
4549 Elf_Internal_Sym *isym;
4550 asection *s;
4552 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4553 r_symndx);
4554 if (isym == NULL)
4556 if (elf_section_data (opd)->relocs != relocs)
4557 free (relocs);
4558 return false;
4561 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4562 if (s != NULL && s != opd)
4563 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4566 if (elf_section_data (opd)->relocs != relocs)
4567 free (relocs);
4570 p = &htab->dot_syms;
4571 while ((eh = *p) != NULL)
4573 *p = NULL;
4574 if (&eh->elf == htab->elf.hgot)
4576 else if (htab->elf.hgot == NULL
4577 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4578 htab->elf.hgot = &eh->elf;
4579 else if (abiversion (ibfd) <= 1)
4581 htab->need_func_desc_adj = 1;
4582 if (!add_symbol_adjust (eh, info))
4583 return false;
4585 p = &eh->u.next_dot_sym;
4587 return true;
4590 /* Undo hash table changes when an --as-needed input file is determined
4591 not to be needed. */
4593 static bool
4594 ppc64_elf_notice_as_needed (bfd *ibfd,
4595 struct bfd_link_info *info,
4596 enum notice_asneeded_action act)
4598 if (act == notice_not_needed)
4600 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4602 if (htab == NULL)
4603 return false;
4605 htab->dot_syms = NULL;
4607 return _bfd_elf_notice_as_needed (ibfd, info, act);
4610 /* If --just-symbols against a final linked binary, then assume we need
4611 toc adjusting stubs when calling functions defined there. */
4613 static void
4614 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4616 if ((sec->flags & SEC_CODE) != 0
4617 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4618 && is_ppc64_elf (sec->owner))
4620 if (abiversion (sec->owner) >= 2
4621 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4622 sec->has_toc_reloc = 1;
4624 _bfd_elf_link_just_syms (sec, info);
4627 static struct plt_entry **
4628 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4629 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4631 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4632 struct plt_entry **local_plt;
4633 unsigned char *local_got_tls_masks;
4635 if (local_got_ents == NULL)
4637 bfd_size_type size = symtab_hdr->sh_info;
4639 size *= (sizeof (*local_got_ents)
4640 + sizeof (*local_plt)
4641 + sizeof (*local_got_tls_masks));
4642 local_got_ents = bfd_zalloc (abfd, size);
4643 if (local_got_ents == NULL)
4644 return NULL;
4645 elf_local_got_ents (abfd) = local_got_ents;
4648 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4650 struct got_entry *ent;
4652 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4653 if (ent->addend == r_addend
4654 && ent->owner == abfd
4655 && ent->tls_type == tls_type)
4656 break;
4657 if (ent == NULL)
4659 size_t amt = sizeof (*ent);
4660 ent = bfd_alloc (abfd, amt);
4661 if (ent == NULL)
4662 return NULL;
4663 ent->next = local_got_ents[r_symndx];
4664 ent->addend = r_addend;
4665 ent->owner = abfd;
4666 ent->tls_type = tls_type;
4667 ent->is_indirect = false;
4668 ent->got.refcount = 0;
4669 local_got_ents[r_symndx] = ent;
4671 ent->got.refcount += 1;
4674 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4675 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4676 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4678 return local_plt + r_symndx;
4681 static bool
4682 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4684 struct plt_entry *ent;
4686 for (ent = *plist; ent != NULL; ent = ent->next)
4687 if (ent->addend == addend)
4688 break;
4689 if (ent == NULL)
4691 size_t amt = sizeof (*ent);
4692 ent = bfd_alloc (abfd, amt);
4693 if (ent == NULL)
4694 return false;
4695 ent->next = *plist;
4696 ent->addend = addend;
4697 ent->plt.refcount = 0;
4698 *plist = ent;
4700 ent->plt.refcount += 1;
4701 return true;
4704 static bool
4705 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4707 return (r_type == R_PPC64_REL24
4708 || r_type == R_PPC64_REL24_NOTOC
4709 || r_type == R_PPC64_REL24_P9NOTOC
4710 || r_type == R_PPC64_REL14
4711 || r_type == R_PPC64_REL14_BRTAKEN
4712 || r_type == R_PPC64_REL14_BRNTAKEN
4713 || r_type == R_PPC64_ADDR24
4714 || r_type == R_PPC64_ADDR14
4715 || r_type == R_PPC64_ADDR14_BRTAKEN
4716 || r_type == R_PPC64_ADDR14_BRNTAKEN
4717 || r_type == R_PPC64_PLTCALL
4718 || r_type == R_PPC64_PLTCALL_NOTOC);
4721 /* Relocs on inline plt call sequence insns prior to the call. */
4723 static bool
4724 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4726 return (r_type == R_PPC64_PLT16_HA
4727 || r_type == R_PPC64_PLT16_HI
4728 || r_type == R_PPC64_PLT16_LO
4729 || r_type == R_PPC64_PLT16_LO_DS
4730 || r_type == R_PPC64_PLT_PCREL34
4731 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4732 || r_type == R_PPC64_PLTSEQ
4733 || r_type == R_PPC64_PLTSEQ_NOTOC);
4736 /* Of relocs which might appear paired with TLSGD and TLSLD marker
4737 relocs, return true for those that operate on a dword. */
4739 static bool
4740 is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
4742 return (r_type == R_PPC64_PLT_PCREL34
4743 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4744 || r_type == R_PPC64_PLTCALL);
4747 /* The RELR encoding doesn't allow odd addresses, so RELR_ALIGN must
4748 be at least 1. R_PPC64_RELATIVE relocs require alignment of 2**3.
4749 We use 3 here to avoid complexity in relocate_section, where for a
4750 value of 1 we'd need to test for not just an output RELATIVE reloc
4751 near the call to maybe_relr but also UADDR64 and some conditions on
4752 the symbol. See PR30824. */
4753 #define RELR_ALIGN 3
4755 static bool
4756 maybe_relr (enum elf_ppc64_reloc_type r_type,
4757 const Elf_Internal_Rela *rel,
4758 const asection *sec)
4760 return ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
4761 && (rel->r_offset & ((1 << RELR_ALIGN) - 1)) == 0
4762 && sec->alignment_power >= RELR_ALIGN);
4765 /* Like bfd_reloc_offset_in_range but without a howto. Return true
4766 iff a field of SIZE bytes at OFFSET is within SEC limits. */
4768 static bool
4769 offset_in_range (asection *sec, bfd_vma offset, size_t size)
4771 return offset <= sec->size && size <= sec->size - offset;
4774 /* Look through the relocs for a section during the first phase, and
4775 calculate needed space in the global offset table, procedure
4776 linkage table, and dynamic reloc sections. */
4778 static bool
4779 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4780 asection *sec, const Elf_Internal_Rela *relocs)
4782 struct ppc_link_hash_table *htab;
4783 Elf_Internal_Shdr *symtab_hdr;
4784 struct elf_link_hash_entry **sym_hashes;
4785 const Elf_Internal_Rela *rel;
4786 const Elf_Internal_Rela *rel_end;
4787 asection *sreloc;
4788 struct elf_link_hash_entry *tga, *dottga;
4789 bool is_opd;
4791 if (bfd_link_relocatable (info))
4792 return true;
4794 BFD_ASSERT (is_ppc64_elf (abfd));
4796 htab = ppc_hash_table (info);
4797 if (htab == NULL)
4798 return false;
4800 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4801 false, false, true);
4802 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4803 false, false, true);
4804 symtab_hdr = &elf_symtab_hdr (abfd);
4805 sym_hashes = elf_sym_hashes (abfd);
4806 sreloc = NULL;
4807 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4808 rel_end = relocs + sec->reloc_count;
4809 for (rel = relocs; rel < rel_end; rel++)
4811 unsigned long r_symndx;
4812 struct elf_link_hash_entry *h;
4813 Elf_Internal_Sym *isym;
4814 enum elf_ppc64_reloc_type r_type;
4815 int tls_type;
4816 struct _ppc64_elf_section_data *ppc64_sec;
4817 struct plt_entry **ifunc, **plt_list;
4819 r_symndx = ELF64_R_SYM (rel->r_info);
4820 if (r_symndx < symtab_hdr->sh_info)
4822 h = NULL;
4823 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
4824 if (isym == NULL)
4825 return false;
4827 else
4829 isym = NULL;
4830 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4831 h = elf_follow_link (h);
4833 if (h == htab->elf.hgot)
4834 sec->has_toc_reloc = 1;
4837 r_type = ELF64_R_TYPE (rel->r_info);
4838 switch (r_type)
4840 case R_PPC64_D34:
4841 case R_PPC64_D34_LO:
4842 case R_PPC64_D34_HI30:
4843 case R_PPC64_D34_HA30:
4844 case R_PPC64_D28:
4845 case R_PPC64_TPREL34:
4846 case R_PPC64_DTPREL34:
4847 case R_PPC64_PCREL34:
4848 case R_PPC64_GOT_PCREL34:
4849 case R_PPC64_GOT_TLSGD_PCREL34:
4850 case R_PPC64_GOT_TLSLD_PCREL34:
4851 case R_PPC64_GOT_TPREL_PCREL34:
4852 case R_PPC64_GOT_DTPREL_PCREL34:
4853 case R_PPC64_PLT_PCREL34:
4854 case R_PPC64_PLT_PCREL34_NOTOC:
4855 case R_PPC64_PCREL28:
4856 htab->has_power10_relocs = 1;
4857 break;
4858 default:
4859 break;
4862 switch (r_type)
4864 case R_PPC64_PLT16_HA:
4865 case R_PPC64_GOT_TLSLD16_HA:
4866 case R_PPC64_GOT_TLSGD16_HA:
4867 case R_PPC64_GOT_TPREL16_HA:
4868 case R_PPC64_GOT_DTPREL16_HA:
4869 case R_PPC64_GOT16_HA:
4870 case R_PPC64_TOC16_HA:
4871 case R_PPC64_PLT16_LO:
4872 case R_PPC64_PLT16_LO_DS:
4873 case R_PPC64_GOT_TLSLD16_LO:
4874 case R_PPC64_GOT_TLSGD16_LO:
4875 case R_PPC64_GOT_TPREL16_LO_DS:
4876 case R_PPC64_GOT_DTPREL16_LO_DS:
4877 case R_PPC64_GOT16_LO:
4878 case R_PPC64_GOT16_LO_DS:
4879 case R_PPC64_TOC16_LO:
4880 case R_PPC64_TOC16_LO_DS:
4881 case R_PPC64_GOT_PCREL34:
4882 ppc64_elf_tdata (abfd)->has_optrel = 1;
4883 ppc64_elf_section_data (sec)->has_optrel = 1;
4884 break;
4885 default:
4886 break;
4889 ifunc = NULL;
4890 if (h != NULL)
4892 if (h->type == STT_GNU_IFUNC)
4894 h->needs_plt = 1;
4895 ifunc = &h->plt.plist;
4898 else
4900 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4902 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4903 rel->r_addend,
4904 NON_GOT | PLT_IFUNC);
4905 if (ifunc == NULL)
4906 return false;
4910 tls_type = 0;
4911 switch (r_type)
4913 case R_PPC64_PLTSEQ:
4914 case R_PPC64_PLTSEQ_NOTOC:
4915 /* Inline plt call code emitted by gcc doesn't support
4916 modifying the tls_index words to short-circuit
4917 __tls_get_addr calls. See PR32387. */
4918 if (h != NULL && (h == tga || h == dottga))
4919 htab->params->tls_get_addr_opt = 0;
4920 break;
4922 case R_PPC64_TLSGD:
4923 case R_PPC64_TLSLD:
4924 /* These special tls relocs tie a call to __tls_get_addr with
4925 its parameter symbol. */
4926 if (h != NULL)
4927 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
4928 else
4929 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4930 rel->r_addend,
4931 NON_GOT | TLS_TLS | TLS_MARK))
4932 return false;
4933 sec->has_tls_reloc = 1;
4934 break;
4936 case R_PPC64_GOT_TLSLD16:
4937 case R_PPC64_GOT_TLSLD16_LO:
4938 case R_PPC64_GOT_TLSLD16_HI:
4939 case R_PPC64_GOT_TLSLD16_HA:
4940 case R_PPC64_GOT_TLSLD_PCREL34:
4941 tls_type = TLS_TLS | TLS_LD;
4942 goto dogottls;
4944 case R_PPC64_GOT_TLSGD16:
4945 case R_PPC64_GOT_TLSGD16_LO:
4946 case R_PPC64_GOT_TLSGD16_HI:
4947 case R_PPC64_GOT_TLSGD16_HA:
4948 case R_PPC64_GOT_TLSGD_PCREL34:
4949 tls_type = TLS_TLS | TLS_GD;
4950 goto dogottls;
4952 case R_PPC64_GOT_TPREL16_DS:
4953 case R_PPC64_GOT_TPREL16_LO_DS:
4954 case R_PPC64_GOT_TPREL16_HI:
4955 case R_PPC64_GOT_TPREL16_HA:
4956 case R_PPC64_GOT_TPREL_PCREL34:
4957 if (bfd_link_dll (info))
4958 info->flags |= DF_STATIC_TLS;
4959 tls_type = TLS_TLS | TLS_TPREL;
4960 goto dogottls;
4962 case R_PPC64_GOT_DTPREL16_DS:
4963 case R_PPC64_GOT_DTPREL16_LO_DS:
4964 case R_PPC64_GOT_DTPREL16_HI:
4965 case R_PPC64_GOT_DTPREL16_HA:
4966 case R_PPC64_GOT_DTPREL_PCREL34:
4967 tls_type = TLS_TLS | TLS_DTPREL;
4968 dogottls:
4969 sec->has_tls_reloc = 1;
4970 goto dogot;
4972 case R_PPC64_GOT16:
4973 case R_PPC64_GOT16_LO:
4974 case R_PPC64_GOT16_HI:
4975 case R_PPC64_GOT16_HA:
4976 case R_PPC64_GOT16_DS:
4977 case R_PPC64_GOT16_LO_DS:
4978 case R_PPC64_GOT_PCREL34:
4979 dogot:
4980 /* This symbol requires a global offset table entry. */
4981 sec->has_toc_reloc = 1;
4982 if (r_type == R_PPC64_GOT_TLSLD16
4983 || r_type == R_PPC64_GOT_TLSGD16
4984 || r_type == R_PPC64_GOT_TPREL16_DS
4985 || r_type == R_PPC64_GOT_DTPREL16_DS
4986 || r_type == R_PPC64_GOT16
4987 || r_type == R_PPC64_GOT16_DS)
4989 htab->do_multi_toc = 1;
4990 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4993 if (ppc64_elf_tdata (abfd)->got == NULL
4994 && !create_got_section (abfd, info))
4995 return false;
4997 if (h != NULL)
4999 struct ppc_link_hash_entry *eh;
5000 struct got_entry *ent;
5002 eh = ppc_elf_hash_entry (h);
5003 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5004 if (ent->addend == rel->r_addend
5005 && ent->owner == abfd
5006 && ent->tls_type == tls_type)
5007 break;
5008 if (ent == NULL)
5010 size_t amt = sizeof (*ent);
5011 ent = bfd_alloc (abfd, amt);
5012 if (ent == NULL)
5013 return false;
5014 ent->next = eh->elf.got.glist;
5015 ent->addend = rel->r_addend;
5016 ent->owner = abfd;
5017 ent->tls_type = tls_type;
5018 ent->is_indirect = false;
5019 ent->got.refcount = 0;
5020 eh->elf.got.glist = ent;
5022 ent->got.refcount += 1;
5023 eh->tls_mask |= tls_type;
5025 else
5026 /* This is a global offset table entry for a local symbol. */
5027 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5028 rel->r_addend, tls_type))
5029 return false;
5030 break;
5032 case R_PPC64_PLT16_HA:
5033 case R_PPC64_PLT16_HI:
5034 case R_PPC64_PLT16_LO:
5035 case R_PPC64_PLT16_LO_DS:
5036 case R_PPC64_PLT_PCREL34:
5037 case R_PPC64_PLT_PCREL34_NOTOC:
5038 case R_PPC64_PLT32:
5039 case R_PPC64_PLT64:
5040 /* This symbol requires a procedure linkage table entry. */
5041 plt_list = ifunc;
5042 if (h != NULL)
5044 h->needs_plt = 1;
5045 if (h->root.root.string[0] == '.'
5046 && h->root.root.string[1] != '\0')
5047 ppc_elf_hash_entry (h)->is_func = 1;
5048 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
5049 plt_list = &h->plt.plist;
5051 if (plt_list == NULL)
5052 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5053 rel->r_addend,
5054 NON_GOT | PLT_KEEP);
5055 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5056 return false;
5057 break;
5059 /* The following relocations don't need to propagate the
5060 relocation if linking a shared object since they are
5061 section relative. */
5062 case R_PPC64_SECTOFF:
5063 case R_PPC64_SECTOFF_LO:
5064 case R_PPC64_SECTOFF_HI:
5065 case R_PPC64_SECTOFF_HA:
5066 case R_PPC64_SECTOFF_DS:
5067 case R_PPC64_SECTOFF_LO_DS:
5068 case R_PPC64_DTPREL16:
5069 case R_PPC64_DTPREL16_LO:
5070 case R_PPC64_DTPREL16_HI:
5071 case R_PPC64_DTPREL16_HA:
5072 case R_PPC64_DTPREL16_DS:
5073 case R_PPC64_DTPREL16_LO_DS:
5074 case R_PPC64_DTPREL16_HIGH:
5075 case R_PPC64_DTPREL16_HIGHA:
5076 case R_PPC64_DTPREL16_HIGHER:
5077 case R_PPC64_DTPREL16_HIGHERA:
5078 case R_PPC64_DTPREL16_HIGHEST:
5079 case R_PPC64_DTPREL16_HIGHESTA:
5080 break;
5082 /* Nor do these. */
5083 case R_PPC64_REL16:
5084 case R_PPC64_REL16_LO:
5085 case R_PPC64_REL16_HI:
5086 case R_PPC64_REL16_HA:
5087 case R_PPC64_REL16_HIGH:
5088 case R_PPC64_REL16_HIGHA:
5089 case R_PPC64_REL16_HIGHER:
5090 case R_PPC64_REL16_HIGHERA:
5091 case R_PPC64_REL16_HIGHEST:
5092 case R_PPC64_REL16_HIGHESTA:
5093 case R_PPC64_REL16_HIGHER34:
5094 case R_PPC64_REL16_HIGHERA34:
5095 case R_PPC64_REL16_HIGHEST34:
5096 case R_PPC64_REL16_HIGHESTA34:
5097 case R_PPC64_REL16DX_HA:
5098 break;
5100 /* Not supported as a dynamic relocation. */
5101 case R_PPC64_ADDR64_LOCAL:
5102 if (bfd_link_pic (info))
5104 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5105 ppc_howto_init ();
5106 /* xgettext:c-format */
5107 info->callbacks->einfo (_("%H: %s reloc unsupported "
5108 "in shared libraries and PIEs\n"),
5109 abfd, sec, rel->r_offset,
5110 ppc64_elf_howto_table[r_type]->name);
5111 bfd_set_error (bfd_error_bad_value);
5112 return false;
5114 break;
5116 case R_PPC64_TOC16:
5117 case R_PPC64_TOC16_DS:
5118 htab->do_multi_toc = 1;
5119 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5120 /* Fall through. */
5121 case R_PPC64_TOC16_LO:
5122 case R_PPC64_TOC16_HI:
5123 case R_PPC64_TOC16_HA:
5124 case R_PPC64_TOC16_LO_DS:
5125 sec->has_toc_reloc = 1;
5126 if (h != NULL && bfd_link_executable (info))
5128 /* We may need a copy reloc. */
5129 h->non_got_ref = 1;
5130 /* Strongly prefer a copy reloc over a dynamic reloc.
5131 glibc ld.so as of 2019-08 will error out if one of
5132 these relocations is emitted. */
5133 h->needs_copy = 1;
5134 goto dodyn;
5136 break;
5138 /* Marker reloc. */
5139 case R_PPC64_ENTRY:
5140 break;
5142 /* This relocation describes the C++ object vtable hierarchy.
5143 Reconstruct it for later use during GC. */
5144 case R_PPC64_GNU_VTINHERIT:
5145 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5146 return false;
5147 break;
5149 /* This relocation describes which C++ vtable entries are actually
5150 used. Record for later use during GC. */
5151 case R_PPC64_GNU_VTENTRY:
5152 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5153 return false;
5154 break;
5156 case R_PPC64_REL14:
5157 case R_PPC64_REL14_BRTAKEN:
5158 case R_PPC64_REL14_BRNTAKEN:
5160 asection *dest = NULL;
5162 /* Heuristic: If jumping outside our section, chances are
5163 we are going to need a stub. */
5164 if (h != NULL)
5166 /* If the sym is weak it may be overridden later, so
5167 don't assume we know where a weak sym lives. */
5168 if (h->root.type == bfd_link_hash_defined)
5169 dest = h->root.u.def.section;
5171 else
5172 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5174 if (dest != sec)
5175 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5177 goto rel24;
5179 case R_PPC64_PLTCALL:
5180 case R_PPC64_PLTCALL_NOTOC:
5181 ppc64_elf_section_data (sec)->has_pltcall = 1;
5182 /* Fall through. */
5184 case R_PPC64_REL24:
5185 case R_PPC64_REL24_NOTOC:
5186 case R_PPC64_REL24_P9NOTOC:
5187 rel24:
5188 plt_list = ifunc;
5189 if (h != NULL)
5191 h->needs_plt = 1;
5192 if (h->root.root.string[0] == '.'
5193 && h->root.root.string[1] != '\0')
5194 ppc_elf_hash_entry (h)->is_func = 1;
5196 if (h == tga || h == dottga)
5198 sec->has_tls_reloc = 1;
5199 if (rel != relocs
5200 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5201 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5202 /* We have a new-style __tls_get_addr call with
5203 a marker reloc. */
5205 else
5206 /* Mark this section as having an old-style call. */
5207 sec->nomark_tls_get_addr = 1;
5209 plt_list = &h->plt.plist;
5212 /* We may need a .plt entry if the function this reloc
5213 refers to is in a shared lib. */
5214 if (plt_list
5215 && !update_plt_info (abfd, plt_list, rel->r_addend))
5216 return false;
5217 break;
5219 case R_PPC64_ADDR14:
5220 case R_PPC64_ADDR14_BRNTAKEN:
5221 case R_PPC64_ADDR14_BRTAKEN:
5222 case R_PPC64_ADDR24:
5223 goto dodyn;
5225 case R_PPC64_TPREL64:
5226 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5227 if (bfd_link_dll (info))
5228 info->flags |= DF_STATIC_TLS;
5229 goto dotlstoc;
5231 case R_PPC64_DTPMOD64:
5232 if (rel + 1 < rel_end
5233 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5234 && rel[1].r_offset == rel->r_offset + 8)
5235 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5236 else
5237 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5238 goto dotlstoc;
5240 case R_PPC64_DTPREL64:
5241 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5242 if (rel != relocs
5243 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5244 && rel[-1].r_offset == rel->r_offset - 8)
5245 /* This is the second reloc of a dtpmod, dtprel pair.
5246 Don't mark with TLS_DTPREL. */
5247 goto dodyn;
5249 dotlstoc:
5250 sec->has_tls_reloc = 1;
5251 if (h != NULL)
5252 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
5253 else
5254 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5255 rel->r_addend, tls_type))
5256 return false;
5258 ppc64_sec = ppc64_elf_section_data (sec);
5259 if (ppc64_sec->sec_type == sec_normal)
5261 bfd_size_type amt;
5263 /* One extra to simplify get_tls_mask. */
5264 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5265 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5266 if (ppc64_sec->u.toc.symndx == NULL)
5267 return false;
5268 amt = sec->size * sizeof (bfd_vma) / 8;
5269 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5270 if (ppc64_sec->u.toc.add == NULL)
5271 return false;
5272 ppc64_sec->sec_type = sec_toc;
5274 if (ppc64_sec->sec_type != sec_toc
5275 || rel->r_offset % 8 != 0)
5277 info->callbacks->einfo (_("%H: %s reloc unsupported here\n"),
5278 abfd, sec, rel->r_offset,
5279 ppc64_elf_howto_table[r_type]->name);
5280 bfd_set_error (bfd_error_bad_value);
5281 return false;
5283 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5284 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5286 /* Mark the second slot of a GD or LD entry.
5287 -1 to indicate GD and -2 to indicate LD. */
5288 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5289 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5290 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5291 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5292 goto dodyn;
5294 case R_PPC64_TPREL16_HI:
5295 case R_PPC64_TPREL16_HA:
5296 case R_PPC64_TPREL16_HIGH:
5297 case R_PPC64_TPREL16_HIGHA:
5298 case R_PPC64_TPREL16_HIGHER:
5299 case R_PPC64_TPREL16_HIGHERA:
5300 case R_PPC64_TPREL16_HIGHEST:
5301 case R_PPC64_TPREL16_HIGHESTA:
5302 sec->has_tls_reloc = 1;
5303 /* Fall through. */
5304 case R_PPC64_TPREL34:
5305 case R_PPC64_TPREL16:
5306 case R_PPC64_TPREL16_DS:
5307 case R_PPC64_TPREL16_LO:
5308 case R_PPC64_TPREL16_LO_DS:
5309 if (bfd_link_dll (info))
5310 info->flags |= DF_STATIC_TLS;
5311 goto dodyn;
5313 case R_PPC64_ADDR64:
5314 if (is_opd
5315 && rel + 1 < rel_end
5316 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5318 if (h != NULL)
5319 ppc_elf_hash_entry (h)->is_func = 1;
5321 /* Fall through. */
5323 case R_PPC64_ADDR16:
5324 case R_PPC64_ADDR16_DS:
5325 case R_PPC64_ADDR16_HA:
5326 case R_PPC64_ADDR16_HI:
5327 case R_PPC64_ADDR16_HIGH:
5328 case R_PPC64_ADDR16_HIGHA:
5329 case R_PPC64_ADDR16_HIGHER:
5330 case R_PPC64_ADDR16_HIGHERA:
5331 case R_PPC64_ADDR16_HIGHEST:
5332 case R_PPC64_ADDR16_HIGHESTA:
5333 case R_PPC64_ADDR16_LO:
5334 case R_PPC64_ADDR16_LO_DS:
5335 case R_PPC64_D34:
5336 case R_PPC64_D34_LO:
5337 case R_PPC64_D34_HI30:
5338 case R_PPC64_D34_HA30:
5339 case R_PPC64_ADDR16_HIGHER34:
5340 case R_PPC64_ADDR16_HIGHERA34:
5341 case R_PPC64_ADDR16_HIGHEST34:
5342 case R_PPC64_ADDR16_HIGHESTA34:
5343 case R_PPC64_D28:
5344 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5345 && rel->r_addend == 0)
5347 /* We may need a .plt entry if this reloc refers to a
5348 function in a shared lib. */
5349 if (!update_plt_info (abfd, &h->plt.plist, 0))
5350 return false;
5351 h->pointer_equality_needed = 1;
5353 /* Fall through. */
5355 case R_PPC64_REL30:
5356 case R_PPC64_REL32:
5357 case R_PPC64_REL64:
5358 case R_PPC64_ADDR32:
5359 case R_PPC64_UADDR16:
5360 case R_PPC64_UADDR32:
5361 case R_PPC64_UADDR64:
5362 case R_PPC64_TOC:
5363 if (h != NULL && bfd_link_executable (info))
5364 /* We may need a copy reloc. */
5365 h->non_got_ref = 1;
5367 /* Don't propagate .opd relocs. */
5368 if (NO_OPD_RELOCS && is_opd)
5369 break;
5371 /* Set up information for symbols that might need dynamic
5372 relocations. At this point in linking we have read all
5373 the input files and resolved most symbols, but have not
5374 yet decided whether symbols are dynamic or finalized
5375 symbol flags. In some cases we might be setting dynamic
5376 reloc info for symbols that do not end up needing such.
5377 That's OK, adjust_dynamic_symbol and allocate_dynrelocs
5378 work together with this code. */
5379 dodyn:
5380 if ((h != NULL
5381 && !SYMBOL_REFERENCES_LOCAL (info, h))
5382 || (bfd_link_pic (info)
5383 && (h != NULL
5384 ? !bfd_is_abs_symbol (&h->root)
5385 : isym->st_shndx != SHN_ABS)
5386 && must_be_dyn_reloc (info, r_type))
5387 || (!bfd_link_pic (info)
5388 && ifunc != NULL))
5390 /* We must copy these reloc types into the output file.
5391 Create a reloc section in dynobj and make room for
5392 this reloc. */
5393 if (sreloc == NULL)
5395 sreloc = _bfd_elf_make_dynamic_reloc_section
5396 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
5398 if (sreloc == NULL)
5399 return false;
5402 /* If this is a global symbol, we count the number of
5403 relocations we need for this symbol. */
5404 if (h != NULL)
5406 struct ppc_dyn_relocs *p;
5407 struct ppc_dyn_relocs **head;
5409 head = (struct ppc_dyn_relocs **) &h->dyn_relocs;
5410 p = *head;
5411 if (p == NULL || p->sec != sec)
5413 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5414 if (p == NULL)
5415 return false;
5416 p->next = *head;
5417 *head = p;
5418 p->sec = sec;
5419 p->count = 0;
5420 p->pc_count = 0;
5421 p->rel_count = 0;
5423 p->count += 1;
5424 if (!must_be_dyn_reloc (info, r_type))
5425 p->pc_count += 1;
5426 if (maybe_relr (r_type, rel, sec))
5427 p->rel_count += 1;
5429 else
5431 /* Track dynamic relocs needed for local syms too. */
5432 struct ppc_local_dyn_relocs *p;
5433 struct ppc_local_dyn_relocs **head;
5434 bool is_ifunc;
5435 asection *s;
5436 void *vpp;
5438 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5439 if (s == NULL)
5440 s = sec;
5442 vpp = &elf_section_data (s)->local_dynrel;
5443 head = (struct ppc_local_dyn_relocs **) vpp;
5444 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5445 p = *head;
5446 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5447 p = p->next;
5448 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5450 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5451 if (p == NULL)
5452 return false;
5453 p->next = *head;
5454 *head = p;
5455 p->sec = sec;
5456 p->count = 0;
5457 p->rel_count = 0;
5458 p->ifunc = is_ifunc;
5460 p->count += 1;
5461 if (maybe_relr (r_type, rel, sec))
5462 p->rel_count += 1;
5465 break;
5467 default:
5468 break;
5472 return true;
5475 /* Merge backend specific data from an object file to the output
5476 object file when linking. */
5478 static bool
5479 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5481 bfd *obfd = info->output_bfd;
5482 unsigned long iflags, oflags;
5484 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5485 return true;
5487 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5488 return true;
5490 if (!_bfd_generic_verify_endian_match (ibfd, info))
5491 return false;
5493 iflags = elf_elfheader (ibfd)->e_flags;
5494 oflags = elf_elfheader (obfd)->e_flags;
5496 if (iflags & ~EF_PPC64_ABI)
5498 _bfd_error_handler
5499 /* xgettext:c-format */
5500 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5501 bfd_set_error (bfd_error_bad_value);
5502 return false;
5504 else if (iflags != oflags && iflags != 0)
5506 _bfd_error_handler
5507 /* xgettext:c-format */
5508 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5509 ibfd, iflags, oflags);
5510 bfd_set_error (bfd_error_bad_value);
5511 return false;
5514 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5515 return false;
5517 /* Merge Tag_compatibility attributes and any common GNU ones. */
5518 return _bfd_elf_merge_object_attributes (ibfd, info);
5521 static bool
5522 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5524 /* Print normal ELF private data. */
5525 _bfd_elf_print_private_bfd_data (abfd, ptr);
5527 if (elf_elfheader (abfd)->e_flags != 0)
5529 FILE *file = ptr;
5531 fprintf (file, _("private flags = 0x%lx:"),
5532 elf_elfheader (abfd)->e_flags);
5534 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5535 fprintf (file, _(" [abiv%ld]"),
5536 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5537 fputc ('\n', file);
5540 return true;
5543 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5544 of the code entry point, and its section, which must be in the same
5545 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
5547 static bfd_vma
5548 opd_entry_value (asection *opd_sec,
5549 bfd_vma offset,
5550 asection **code_sec,
5551 bfd_vma *code_off,
5552 bool in_code_sec)
5554 bfd *opd_bfd = opd_sec->owner;
5555 Elf_Internal_Rela *relocs;
5556 Elf_Internal_Rela *lo, *hi, *look;
5557 bfd_vma val;
5559 if (!is_ppc64_elf (opd_bfd))
5560 return (bfd_vma) -1;
5562 if (ppc64_elf_section_data (opd_sec)->sec_type == sec_normal)
5563 ppc64_elf_section_data (opd_sec)->sec_type = sec_opd;
5564 else if (ppc64_elf_section_data (opd_sec)->sec_type != sec_opd)
5565 return (bfd_vma) -1;
5567 /* No relocs implies we are linking a --just-symbols object, or looking
5568 at a final linked executable with addr2line or somesuch. */
5569 if (opd_sec->reloc_count == 0)
5571 bfd_byte *contents = ppc64_elf_section_data (opd_sec)->u.opd.u.contents;
5573 if (contents == NULL)
5575 if ((opd_sec->flags & SEC_HAS_CONTENTS) == 0
5576 || !bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5577 return (bfd_vma) -1;
5578 ppc64_elf_section_data (opd_sec)->u.opd.u.contents = contents;
5581 /* PR 17512: file: 64b9dfbb. */
5582 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5583 return (bfd_vma) -1;
5585 val = bfd_get_64 (opd_bfd, contents + offset);
5586 if (code_sec != NULL)
5588 asection *sec, *likely = NULL;
5590 if (in_code_sec)
5592 sec = *code_sec;
5593 if (sec->vma <= val
5594 && val < sec->vma + sec->size)
5595 likely = sec;
5596 else
5597 val = -1;
5599 else
5600 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5601 if (sec->vma <= val
5602 && (sec->flags & SEC_LOAD) != 0
5603 && (sec->flags & SEC_ALLOC) != 0)
5604 likely = sec;
5605 if (likely != NULL)
5607 *code_sec = likely;
5608 if (code_off != NULL)
5609 *code_off = val - likely->vma;
5612 return val;
5615 relocs = ppc64_elf_section_data (opd_sec)->u.opd.u.relocs;
5616 if (relocs == NULL)
5617 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
5618 /* PR 17512: file: df8e1fd6. */
5619 if (relocs == NULL)
5620 return (bfd_vma) -1;
5622 /* Go find the opd reloc at the sym address. */
5623 lo = relocs;
5624 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5625 val = (bfd_vma) -1;
5626 while (lo < hi)
5628 look = lo + (hi - lo) / 2;
5629 if (look->r_offset < offset)
5630 lo = look + 1;
5631 else if (look->r_offset > offset)
5632 hi = look;
5633 else
5635 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5637 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5638 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5640 unsigned long symndx = ELF64_R_SYM (look->r_info);
5641 asection *sec = NULL;
5643 if (symndx >= symtab_hdr->sh_info
5644 && elf_sym_hashes (opd_bfd) != NULL)
5646 struct elf_link_hash_entry **sym_hashes;
5647 struct elf_link_hash_entry *rh;
5649 sym_hashes = elf_sym_hashes (opd_bfd);
5650 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5651 if (rh != NULL)
5653 rh = elf_follow_link (rh);
5654 if (rh->root.type != bfd_link_hash_defined
5655 && rh->root.type != bfd_link_hash_defweak)
5656 break;
5657 if (rh->root.u.def.section->owner == opd_bfd)
5659 val = rh->root.u.def.value;
5660 sec = rh->root.u.def.section;
5665 if (sec == NULL)
5667 Elf_Internal_Sym *sym;
5669 if (symndx < symtab_hdr->sh_info)
5671 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5672 if (sym == NULL)
5674 size_t symcnt = symtab_hdr->sh_info;
5675 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5676 symcnt, 0,
5677 NULL, NULL, NULL);
5678 if (sym == NULL)
5679 break;
5680 symtab_hdr->contents = (bfd_byte *) sym;
5682 sym += symndx;
5684 else
5686 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5687 1, symndx,
5688 NULL, NULL, NULL);
5689 if (sym == NULL)
5690 break;
5692 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5693 if (sec != NULL)
5695 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5696 val = sym->st_value;
5698 if (symndx >= symtab_hdr->sh_info)
5699 free (sym);
5700 if (sec == NULL)
5701 break;
5704 val += look->r_addend;
5705 if (code_off != NULL)
5706 *code_off = val;
5707 if (code_sec != NULL)
5709 if (in_code_sec && *code_sec != sec)
5710 return -1;
5711 else
5712 *code_sec = sec;
5714 if (sec->output_section != NULL)
5715 val += sec->output_section->vma + sec->output_offset;
5717 break;
5721 return val;
5724 /* If the ELF symbol SYM might be a function in SEC, return the
5725 function size and set *CODE_OFF to the function's entry point,
5726 otherwise return zero. */
5728 static bfd_size_type
5729 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5730 bfd_vma *code_off)
5732 bfd_size_type size;
5733 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
5735 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5736 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5737 return 0;
5739 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5741 /* In theory we should check that the symbol's type satisfies
5742 _bfd_elf_is_function_type(), but there are some function-like
5743 symbols which would fail this test. (eg _start). Instead
5744 we check for hidden, local, notype symbols with zero size.
5745 This type of symbol is generated by the annobin plugin for gcc
5746 and clang, and should not be considered to be a function symbol. */
5747 if (size == 0
5748 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5749 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5750 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5751 return 0;
5753 if (strcmp (sym->section->name, ".opd") == 0)
5755 struct _opd_sec_data *opd = get_opd_info (sym->section);
5756 bfd_vma symval = sym->value;
5758 if (opd != NULL
5759 && opd->adjust != NULL
5760 && elf_section_data (sym->section)->relocs != NULL)
5762 /* opd_entry_value will use cached relocs that have been
5763 adjusted, but with raw symbols. That means both local
5764 and global symbols need adjusting. */
5765 long adjust = opd->adjust[OPD_NDX (symval)];
5766 if (adjust == -1)
5767 return 0;
5768 symval += adjust;
5771 if (opd_entry_value (sym->section, symval,
5772 &sec, code_off, true) == (bfd_vma) -1)
5773 return 0;
5774 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5775 symbol. This size has nothing to do with the code size of the
5776 function, which is what we're supposed to return, but the
5777 code size isn't available without looking up the dot-sym.
5778 However, doing that would be a waste of time particularly
5779 since elf_find_function will look at the dot-sym anyway.
5780 Now, elf_find_function will keep the largest size of any
5781 function sym found at the code address of interest, so return
5782 1 here to avoid it incorrectly caching a larger function size
5783 for a small function. This does mean we return the wrong
5784 size for a new-ABI function of size 24, but all that does is
5785 disable caching for such functions. */
5786 if (size == 24)
5787 size = 1;
5789 else
5791 if (sym->section != sec)
5792 return 0;
5793 *code_off = sym->value;
5796 /* Do not return 0 for the function's size. */
5797 return size ? size : 1;
5800 /* Return true if symbol is a strong function defined in an ELFv2
5801 object with st_other localentry bits of zero, ie. its local entry
5802 point coincides with its global entry point. */
5804 static bool
5805 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5807 return (h != NULL
5808 && h->type == STT_FUNC
5809 && h->root.type == bfd_link_hash_defined
5810 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5811 && !ppc_elf_hash_entry (h)->non_zero_localentry
5812 && is_ppc64_elf (h->root.u.def.section->owner)
5813 && abiversion (h->root.u.def.section->owner) >= 2);
5816 /* Return true if symbol is defined in a regular object file. */
5818 static bool
5819 is_static_defined (struct elf_link_hash_entry *h)
5821 return ((h->root.type == bfd_link_hash_defined
5822 || h->root.type == bfd_link_hash_defweak)
5823 && h->root.u.def.section != NULL
5824 && h->root.u.def.section->output_section != NULL);
5827 /* If FDH is a function descriptor symbol, return the associated code
5828 entry symbol if it is defined. Return NULL otherwise. */
5830 static struct ppc_link_hash_entry *
5831 defined_code_entry (struct ppc_link_hash_entry *fdh)
5833 if (fdh->is_func_descriptor)
5835 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5836 if (fh->elf.root.type == bfd_link_hash_defined
5837 || fh->elf.root.type == bfd_link_hash_defweak)
5838 return fh;
5840 return NULL;
5843 /* If FH is a function code entry symbol, return the associated
5844 function descriptor symbol if it is defined. Return NULL otherwise. */
5846 static struct ppc_link_hash_entry *
5847 defined_func_desc (struct ppc_link_hash_entry *fh)
5849 if (fh->oh != NULL
5850 && fh->oh->is_func_descriptor)
5852 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5853 if (fdh->elf.root.type == bfd_link_hash_defined
5854 || fdh->elf.root.type == bfd_link_hash_defweak)
5855 return fdh;
5857 return NULL;
5860 /* Given H is a symbol that satisfies is_static_defined, return the
5861 value in the output file. */
5863 static bfd_vma
5864 defined_sym_val (struct elf_link_hash_entry *h)
5866 return (h->root.u.def.section->output_section->vma
5867 + h->root.u.def.section->output_offset
5868 + h->root.u.def.value);
5871 /* Return true if H matches __tls_get_addr or one of its variants. */
5873 static bool
5874 is_tls_get_addr (struct elf_link_hash_entry *h,
5875 struct ppc_link_hash_table *htab)
5877 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5878 || h == elf_hash_entry (htab->tga_desc_fd)
5879 || h == elf_hash_entry (htab->tls_get_addr)
5880 || h == elf_hash_entry (htab->tga_desc));
5883 static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
5885 /* Garbage collect sections, after first dealing with dot-symbols. */
5887 static bool
5888 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5890 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5892 if (htab != NULL && htab->need_func_desc_adj)
5894 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5895 htab->need_func_desc_adj = 0;
5897 return bfd_elf_gc_sections (abfd, info);
5900 /* Mark all our entry sym sections, both opd and code section. */
5902 static void
5903 ppc64_elf_gc_keep (struct bfd_link_info *info)
5905 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5906 struct bfd_sym_chain *sym;
5908 if (htab == NULL)
5909 return;
5911 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5913 struct ppc_link_hash_entry *eh, *fh;
5914 asection *sec;
5916 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5917 false, false, true));
5918 if (eh == NULL)
5919 continue;
5920 if (eh->elf.root.type != bfd_link_hash_defined
5921 && eh->elf.root.type != bfd_link_hash_defweak)
5922 continue;
5924 fh = defined_code_entry (eh);
5925 if (fh != NULL)
5927 sec = fh->elf.root.u.def.section;
5928 sec->flags |= SEC_KEEP;
5930 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5931 && opd_entry_value (eh->elf.root.u.def.section,
5932 eh->elf.root.u.def.value,
5933 &sec, NULL, false) != (bfd_vma) -1)
5934 sec->flags |= SEC_KEEP;
5936 sec = eh->elf.root.u.def.section;
5937 sec->flags |= SEC_KEEP;
5941 /* Mark sections containing dynamically referenced symbols. When
5942 building shared libraries, we must assume that any visible symbol is
5943 referenced. */
5945 static bool
5946 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5948 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5949 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
5950 struct ppc_link_hash_entry *fdh;
5951 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5953 /* Dynamic linking info is on the func descriptor sym. */
5954 fdh = defined_func_desc (eh);
5955 if (fdh != NULL)
5956 eh = fdh;
5958 if ((eh->elf.root.type == bfd_link_hash_defined
5959 || eh->elf.root.type == bfd_link_hash_defweak)
5960 && (!eh->elf.start_stop
5961 || eh->elf.root.ldscript_def
5962 || !info->start_stop_gc)
5963 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5964 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5965 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5966 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5967 && (!bfd_link_executable (info)
5968 || info->gc_keep_exported
5969 || info->export_dynamic
5970 || (eh->elf.dynamic
5971 && d != NULL
5972 && (*d->match) (&d->head, NULL,
5973 eh->elf.root.root.string)))
5974 && (eh->elf.versioned >= versioned
5975 || !bfd_hide_sym_by_version (info->version_info,
5976 eh->elf.root.root.string)))))
5978 asection *code_sec;
5979 struct ppc_link_hash_entry *fh;
5981 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5983 /* Function descriptor syms cause the associated
5984 function code sym section to be marked. */
5985 fh = defined_code_entry (eh);
5986 if (fh != NULL)
5988 code_sec = fh->elf.root.u.def.section;
5989 code_sec->flags |= SEC_KEEP;
5991 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5992 && opd_entry_value (eh->elf.root.u.def.section,
5993 eh->elf.root.u.def.value,
5994 &code_sec, NULL, false) != (bfd_vma) -1)
5995 code_sec->flags |= SEC_KEEP;
5998 return true;
6001 /* Return the section that should be marked against GC for a given
6002 relocation. */
6004 static asection *
6005 ppc64_elf_gc_mark_hook (asection *sec,
6006 struct bfd_link_info *info,
6007 Elf_Internal_Rela *rel,
6008 struct elf_link_hash_entry *h,
6009 Elf_Internal_Sym *sym)
6011 asection *rsec;
6013 /* Syms return NULL if we're marking .opd, so we avoid marking all
6014 function sections, as all functions are referenced in .opd. */
6015 rsec = NULL;
6016 if (get_opd_info (sec) != NULL)
6017 return rsec;
6019 if (h != NULL)
6021 enum elf_ppc64_reloc_type r_type;
6022 struct ppc_link_hash_entry *eh, *fh, *fdh;
6024 r_type = ELF64_R_TYPE (rel->r_info);
6025 switch (r_type)
6027 case R_PPC64_GNU_VTINHERIT:
6028 case R_PPC64_GNU_VTENTRY:
6029 break;
6031 default:
6032 switch (h->root.type)
6034 case bfd_link_hash_defined:
6035 case bfd_link_hash_defweak:
6036 eh = ppc_elf_hash_entry (h);
6037 fdh = defined_func_desc (eh);
6038 if (fdh != NULL)
6040 /* -mcall-aixdesc code references the dot-symbol on
6041 a call reloc. Mark the function descriptor too
6042 against garbage collection. */
6043 fdh->elf.mark = 1;
6044 if (fdh->elf.is_weakalias)
6045 weakdef (&fdh->elf)->mark = 1;
6046 eh = fdh;
6049 /* Function descriptor syms cause the associated
6050 function code sym section to be marked. */
6051 fh = defined_code_entry (eh);
6052 if (fh != NULL)
6054 /* They also mark their opd section. */
6055 eh->elf.root.u.def.section->gc_mark = 1;
6057 rsec = fh->elf.root.u.def.section;
6059 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6060 && opd_entry_value (eh->elf.root.u.def.section,
6061 eh->elf.root.u.def.value,
6062 &rsec, NULL, false) != (bfd_vma) -1)
6063 eh->elf.root.u.def.section->gc_mark = 1;
6064 else
6065 rsec = h->root.u.def.section;
6066 break;
6068 case bfd_link_hash_common:
6069 rsec = h->root.u.c.p->section;
6070 break;
6072 default:
6073 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6077 else
6079 struct _opd_sec_data *opd;
6081 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6082 opd = get_opd_info (rsec);
6083 if (opd != NULL && opd->func_sec != NULL)
6085 rsec->gc_mark = 1;
6087 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6091 return rsec;
6094 /* The maximum size of .sfpr. */
6095 #define SFPR_MAX (218*4)
6097 struct sfpr_def_parms
6099 const char name[12];
6100 unsigned char lo, hi;
6101 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6102 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
6105 /* Auto-generate _save*, _rest* functions in .sfpr.
6106 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6107 instead. */
6109 static bool
6110 sfpr_define (struct bfd_link_info *info,
6111 const struct sfpr_def_parms *parm,
6112 asection *stub_sec)
6114 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6115 unsigned int i;
6116 size_t len = strlen (parm->name);
6117 bool writing = false;
6118 char sym[16];
6120 if (htab == NULL)
6121 return false;
6123 memcpy (sym, parm->name, len);
6124 sym[len + 2] = 0;
6126 for (i = parm->lo; i <= parm->hi; i++)
6128 struct ppc_link_hash_entry *h;
6130 sym[len + 0] = i / 10 + '0';
6131 sym[len + 1] = i % 10 + '0';
6132 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
6133 writing, true, true));
6134 if (stub_sec != NULL)
6136 if (h != NULL
6137 && h->elf.root.type == bfd_link_hash_defined
6138 && h->elf.root.u.def.section == htab->sfpr)
6140 struct elf_link_hash_entry *s;
6141 char buf[32];
6142 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6143 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
6144 if (s == NULL)
6145 return false;
6146 if (s->root.type == bfd_link_hash_new)
6148 s->root.type = bfd_link_hash_defined;
6149 s->root.u.def.section = stub_sec;
6150 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6151 + h->elf.root.u.def.value);
6152 s->ref_regular = 1;
6153 s->def_regular = 1;
6154 s->ref_regular_nonweak = 1;
6155 s->forced_local = 1;
6156 s->non_elf = 0;
6157 s->root.linker_def = 1;
6160 continue;
6162 if (h != NULL)
6164 h->save_res = 1;
6165 if (!h->elf.def_regular)
6167 h->elf.root.type = bfd_link_hash_defined;
6168 h->elf.root.u.def.section = htab->sfpr;
6169 h->elf.root.u.def.value = htab->sfpr->size;
6170 h->elf.type = STT_FUNC;
6171 h->elf.def_regular = 1;
6172 h->elf.non_elf = 0;
6173 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6174 writing = true;
6175 if (htab->sfpr->contents == NULL)
6177 htab->sfpr->contents
6178 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6179 if (htab->sfpr->contents == NULL)
6180 return false;
6181 htab->sfpr->alloced = 1;
6185 if (writing)
6187 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6188 if (i != parm->hi)
6189 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6190 else
6191 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6192 htab->sfpr->size = p - htab->sfpr->contents;
6196 return true;
6199 static bfd_byte *
6200 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6202 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6203 return p + 4;
6206 static bfd_byte *
6207 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6209 p = savegpr0 (abfd, p, r);
6210 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6211 p = p + 4;
6212 bfd_put_32 (abfd, BLR, p);
6213 return p + 4;
6216 static bfd_byte *
6217 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6219 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6220 return p + 4;
6223 static bfd_byte *
6224 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6226 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6227 p = p + 4;
6228 p = restgpr0 (abfd, p, r);
6229 bfd_put_32 (abfd, MTLR_R0, p);
6230 p = p + 4;
6231 if (r == 29)
6233 p = restgpr0 (abfd, p, 30);
6234 p = restgpr0 (abfd, p, 31);
6236 bfd_put_32 (abfd, BLR, p);
6237 return p + 4;
6240 static bfd_byte *
6241 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6243 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6244 return p + 4;
6247 static bfd_byte *
6248 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6250 p = savegpr1 (abfd, p, r);
6251 bfd_put_32 (abfd, BLR, p);
6252 return p + 4;
6255 static bfd_byte *
6256 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6258 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6259 return p + 4;
6262 static bfd_byte *
6263 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6265 p = restgpr1 (abfd, p, r);
6266 bfd_put_32 (abfd, BLR, p);
6267 return p + 4;
6270 static bfd_byte *
6271 savefpr (bfd *abfd, bfd_byte *p, int r)
6273 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6274 return p + 4;
6277 static bfd_byte *
6278 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6280 p = savefpr (abfd, p, r);
6281 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6282 p = p + 4;
6283 bfd_put_32 (abfd, BLR, p);
6284 return p + 4;
6287 static bfd_byte *
6288 restfpr (bfd *abfd, bfd_byte *p, int r)
6290 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6291 return p + 4;
6294 static bfd_byte *
6295 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6297 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6298 p = p + 4;
6299 p = restfpr (abfd, p, r);
6300 bfd_put_32 (abfd, MTLR_R0, p);
6301 p = p + 4;
6302 if (r == 29)
6304 p = restfpr (abfd, p, 30);
6305 p = restfpr (abfd, p, 31);
6307 bfd_put_32 (abfd, BLR, p);
6308 return p + 4;
6311 static bfd_byte *
6312 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6314 p = savefpr (abfd, p, r);
6315 bfd_put_32 (abfd, BLR, p);
6316 return p + 4;
6319 static bfd_byte *
6320 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6322 p = restfpr (abfd, p, r);
6323 bfd_put_32 (abfd, BLR, p);
6324 return p + 4;
6327 static bfd_byte *
6328 savevr (bfd *abfd, bfd_byte *p, int r)
6330 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6331 p = p + 4;
6332 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6333 return p + 4;
6336 static bfd_byte *
6337 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6339 p = savevr (abfd, p, r);
6340 bfd_put_32 (abfd, BLR, p);
6341 return p + 4;
6344 static bfd_byte *
6345 restvr (bfd *abfd, bfd_byte *p, int r)
6347 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6348 p = p + 4;
6349 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6350 return p + 4;
6353 static bfd_byte *
6354 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6356 p = restvr (abfd, p, r);
6357 bfd_put_32 (abfd, BLR, p);
6358 return p + 4;
6361 #define STDU_R1_0R1 0xf8210001
6362 #define ADDI_R1_R1 0x38210000
6364 /* Emit prologue of wrapper preserving regs around a call to
6365 __tls_get_addr_opt. */
6367 static bfd_byte *
6368 tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6370 unsigned int i;
6372 bfd_put_32 (obfd, MFLR_R0, p);
6373 p += 4;
6374 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6375 p += 4;
6377 if (htab->opd_abi)
6379 for (i = 4; i < 12; i++)
6381 bfd_put_32 (obfd,
6382 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6383 p += 4;
6385 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6386 p += 4;
6388 else
6390 for (i = 4; i < 12; i++)
6392 bfd_put_32 (obfd,
6393 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6394 p += 4;
6396 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6397 p += 4;
6399 return p;
6402 /* Emit epilogue of wrapper preserving regs around a call to
6403 __tls_get_addr_opt. */
6405 static bfd_byte *
6406 tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6408 unsigned int i;
6410 if (htab->opd_abi)
6412 for (i = 4; i < 12; i++)
6414 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6415 p += 4;
6417 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6418 p += 4;
6420 else
6422 for (i = 4; i < 12; i++)
6424 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6425 p += 4;
6427 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6428 p += 4;
6430 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6431 p += 4;
6432 bfd_put_32 (obfd, MTLR_R0, p);
6433 p += 4;
6434 bfd_put_32 (obfd, BLR, p);
6435 p += 4;
6436 return p;
6439 /* Called via elf_link_hash_traverse to transfer dynamic linking
6440 information on function code symbol entries to their corresponding
6441 function descriptor symbol entries. Must not be called twice for
6442 any given code symbol. */
6444 static bool
6445 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6447 struct bfd_link_info *info;
6448 struct ppc_link_hash_table *htab;
6449 struct ppc_link_hash_entry *fh;
6450 struct ppc_link_hash_entry *fdh;
6451 bool force_local;
6453 fh = ppc_elf_hash_entry (h);
6454 if (fh->elf.root.type == bfd_link_hash_indirect)
6455 return true;
6457 if (!fh->is_func)
6458 return true;
6460 if (fh->elf.root.root.string[0] != '.'
6461 || fh->elf.root.root.string[1] == '\0')
6462 return true;
6464 info = inf;
6465 htab = ppc_hash_table (info);
6466 if (htab == NULL)
6467 return false;
6469 /* Find the corresponding function descriptor symbol. */
6470 fdh = lookup_fdh (fh, htab);
6472 /* Resolve undefined references to dot-symbols as the value
6473 in the function descriptor, if we have one in a regular object.
6474 This is to satisfy cases like ".quad .foo". Calls to functions
6475 in dynamic objects are handled elsewhere. */
6476 if ((fh->elf.root.type == bfd_link_hash_undefined
6477 || fh->elf.root.type == bfd_link_hash_undefweak)
6478 && (fdh->elf.root.type == bfd_link_hash_defined
6479 || fdh->elf.root.type == bfd_link_hash_defweak)
6480 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6481 && opd_entry_value (fdh->elf.root.u.def.section,
6482 fdh->elf.root.u.def.value,
6483 &fh->elf.root.u.def.section,
6484 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
6486 fh->elf.root.type = fdh->elf.root.type;
6487 fh->elf.forced_local = 1;
6488 fh->elf.def_regular = fdh->elf.def_regular;
6489 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6492 if (!fh->elf.dynamic)
6494 struct plt_entry *ent;
6496 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6497 if (ent->plt.refcount > 0)
6498 break;
6499 if (ent == NULL)
6501 if (fdh != NULL && fdh->fake)
6502 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6503 return true;
6507 /* Create a descriptor as undefined if necessary. */
6508 if (fdh == NULL
6509 && !bfd_link_executable (info)
6510 && (fh->elf.root.type == bfd_link_hash_undefined
6511 || fh->elf.root.type == bfd_link_hash_undefweak))
6513 fdh = make_fdh (info, fh);
6514 if (fdh == NULL)
6515 return false;
6518 /* We can't support overriding of symbols on a fake descriptor. */
6519 if (fdh != NULL
6520 && fdh->fake
6521 && (fh->elf.root.type == bfd_link_hash_defined
6522 || fh->elf.root.type == bfd_link_hash_defweak))
6523 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6525 /* Transfer dynamic linking information to the function descriptor. */
6526 if (fdh != NULL)
6528 fdh->elf.ref_regular |= fh->elf.ref_regular;
6529 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6530 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6531 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6532 fdh->elf.dynamic |= fh->elf.dynamic;
6533 fdh->elf.needs_plt |= (fh->elf.needs_plt
6534 || fh->elf.type == STT_FUNC
6535 || fh->elf.type == STT_GNU_IFUNC);
6536 move_plt_plist (fh, fdh);
6538 if (!fdh->elf.forced_local
6539 && fh->elf.dynindx != -1)
6540 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6541 return false;
6544 /* Now that the info is on the function descriptor, clear the
6545 function code sym info. Any function code syms for which we
6546 don't have a definition in a regular file, we force local.
6547 This prevents a shared library from exporting syms that have
6548 been imported from another library. Function code syms that
6549 are really in the library we must leave global to prevent the
6550 linker dragging in a definition from a static library. */
6551 force_local = (!fh->elf.def_regular
6552 || fdh == NULL
6553 || !fdh->elf.def_regular
6554 || fdh->elf.forced_local);
6555 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6557 return true;
6560 static const struct sfpr_def_parms save_res_funcs[] =
6562 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6563 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6564 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6565 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6566 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6567 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6568 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6569 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6570 { "._savef", 14, 31, savefpr, savefpr1_tail },
6571 { "._restf", 14, 31, restfpr, restfpr1_tail },
6572 { "_savevr_", 20, 31, savevr, savevr_tail },
6573 { "_restvr_", 20, 31, restvr, restvr_tail }
6576 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6577 this hook to a) run the edit functions in this file, b) provide
6578 some gcc support functions, and c) transfer dynamic linking
6579 information gathered so far on function code symbol entries, to
6580 their corresponding function descriptor symbol entries. */
6582 static bool
6583 ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6585 struct ppc_link_hash_table *htab;
6587 htab = ppc_hash_table (info);
6588 if (htab == NULL)
6589 return false;
6591 /* Call back into the linker, which then runs the edit functions. */
6592 htab->params->edit ();
6594 /* Provide any missing _save* and _rest* functions. */
6595 if (htab->sfpr != NULL)
6597 unsigned int i;
6599 htab->sfpr->size = 0;
6600 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6601 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6602 return false;
6603 if (htab->sfpr->size == 0)
6604 htab->sfpr->flags |= SEC_EXCLUDE;
6607 if (bfd_link_relocatable (info))
6608 return true;
6610 if (htab->elf.hgot != NULL)
6612 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
6613 /* Make .TOC. defined so as to prevent it being made dynamic.
6614 The wrong value here is fixed later in ppc64_elf_set_toc. */
6615 if (!htab->elf.hgot->def_regular
6616 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6618 htab->elf.hgot->root.type = bfd_link_hash_defined;
6619 htab->elf.hgot->root.u.def.value = 0;
6620 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6621 htab->elf.hgot->def_regular = 1;
6622 htab->elf.hgot->root.linker_def = 1;
6624 htab->elf.hgot->type = STT_OBJECT;
6625 htab->elf.hgot->other
6626 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6629 return true;
6632 /* Return true if we have dynamic relocs against H or any of its weak
6633 aliases, that apply to read-only sections. Cannot be used after
6634 size_dynamic_sections. */
6636 static bool
6637 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6639 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
6642 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
6643 return true;
6644 eh = ppc_elf_hash_entry (eh->elf.u.alias);
6646 while (eh != NULL && &eh->elf != h);
6648 return false;
6651 /* Return whether EH has pc-relative dynamic relocs. */
6653 static bool
6654 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6656 struct ppc_dyn_relocs *p;
6658 for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
6659 if (p->pc_count != 0)
6660 return true;
6661 return false;
6664 /* Return true if a global entry stub will be created for H. Valid
6665 for ELFv2 before plt entries have been allocated. */
6667 static bool
6668 global_entry_stub (struct elf_link_hash_entry *h)
6670 struct plt_entry *pent;
6672 if (!h->pointer_equality_needed
6673 || h->def_regular)
6674 return false;
6676 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6677 if (pent->plt.refcount > 0
6678 && pent->addend == 0)
6679 return true;
6681 return false;
6684 /* Adjust a symbol defined by a dynamic object and referenced by a
6685 regular object. The current definition is in some section of the
6686 dynamic object, but we're not including those sections. We have to
6687 change the definition to something the rest of the link can
6688 understand. */
6690 static bool
6691 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6692 struct elf_link_hash_entry *h)
6694 struct ppc_link_hash_table *htab;
6695 asection *s, *srel;
6697 htab = ppc_hash_table (info);
6698 if (htab == NULL)
6699 return false;
6701 /* Deal with function syms. */
6702 if (h->type == STT_FUNC
6703 || h->type == STT_GNU_IFUNC
6704 || h->needs_plt)
6706 bool local = (ppc_elf_hash_entry (h)->save_res
6707 || SYMBOL_CALLS_LOCAL (info, h)
6708 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6709 /* Discard dyn_relocs when non-pic if we've decided that a
6710 function symbol is local and not an ifunc. We keep dynamic
6711 relocs for ifuncs when local rather than always emitting a
6712 plt call stub for them and defining the symbol on the call
6713 stub. We can't do that for ELFv1 anyway (a function symbol
6714 is defined on a descriptor, not code) and it can be faster at
6715 run-time due to not needing to bounce through a stub. The
6716 dyn_relocs for ifuncs will be applied even in a static
6717 executable. */
6718 if (!bfd_link_pic (info)
6719 && h->type != STT_GNU_IFUNC
6720 && local)
6721 h->dyn_relocs = NULL;
6723 /* Clear procedure linkage table information for any symbol that
6724 won't need a .plt entry. */
6725 struct plt_entry *ent;
6726 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6727 if (ent->plt.refcount > 0)
6728 break;
6729 if (ent == NULL
6730 || (h->type != STT_GNU_IFUNC
6731 && local
6732 && (htab->can_convert_all_inline_plt
6733 || (ppc_elf_hash_entry (h)->tls_mask
6734 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6736 h->plt.plist = NULL;
6737 h->needs_plt = 0;
6738 h->pointer_equality_needed = 0;
6740 else if (abiversion (info->output_bfd) >= 2)
6742 /* Taking a function's address in a read/write section
6743 doesn't require us to define the function symbol in the
6744 executable on a global entry stub. A dynamic reloc can
6745 be used instead. The reason we prefer a few more dynamic
6746 relocs is that calling via a global entry stub costs a
6747 few more instructions, and pointer_equality_needed causes
6748 extra work in ld.so when resolving these symbols. */
6749 if (global_entry_stub (h))
6751 if (!_bfd_elf_readonly_dynrelocs (h))
6753 h->pointer_equality_needed = 0;
6754 /* If we haven't seen a branch reloc and the symbol
6755 isn't an ifunc then we don't need a plt entry. */
6756 if (!h->needs_plt)
6757 h->plt.plist = NULL;
6759 else if (!bfd_link_pic (info))
6760 /* We are going to be defining the function symbol on the
6761 plt stub, so no dyn_relocs needed when non-pic. */
6762 h->dyn_relocs = NULL;
6765 /* ELFv2 function symbols can't have copy relocs. */
6766 return true;
6768 else if (!h->needs_plt
6769 && !_bfd_elf_readonly_dynrelocs (h))
6771 /* If we haven't seen a branch reloc and the symbol isn't an
6772 ifunc then we don't need a plt entry. */
6773 h->plt.plist = NULL;
6774 h->pointer_equality_needed = 0;
6775 return true;
6778 else
6779 h->plt.plist = NULL;
6781 /* If this is a weak symbol, and there is a real definition, the
6782 processor independent code will have arranged for us to see the
6783 real definition first, and we can just use the same value. */
6784 if (h->is_weakalias)
6786 struct elf_link_hash_entry *def = weakdef (h);
6787 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6788 h->root.u.def.section = def->root.u.def.section;
6789 h->root.u.def.value = def->root.u.def.value;
6790 if (def->root.u.def.section == htab->elf.sdynbss
6791 || def->root.u.def.section == htab->elf.sdynrelro)
6792 h->dyn_relocs = NULL;
6793 return true;
6796 /* If we are creating a shared library, we must presume that the
6797 only references to the symbol are via the global offset table.
6798 For such cases we need not do anything here; the relocations will
6799 be handled correctly by relocate_section. */
6800 if (!bfd_link_executable (info))
6801 return true;
6803 /* If there are no references to this symbol that do not use the
6804 GOT, we don't need to generate a copy reloc. */
6805 if (!h->non_got_ref)
6806 return true;
6808 /* Don't generate a copy reloc for symbols defined in the executable. */
6809 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6811 /* If -z nocopyreloc was given, don't generate them either. */
6812 || info->nocopyreloc
6814 /* If we don't find any dynamic relocs in read-only sections, then
6815 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6816 || (ELIMINATE_COPY_RELOCS
6817 && !h->needs_copy
6818 && !alias_readonly_dynrelocs (h))
6820 /* Protected variables do not work with .dynbss. The copy in
6821 .dynbss won't be used by the shared library with the protected
6822 definition for the variable. Text relocations are preferable
6823 to an incorrect program. */
6824 || h->protected_def)
6825 return true;
6827 if (h->type == STT_FUNC
6828 || h->type == STT_GNU_IFUNC)
6830 /* .dynbss copies of function symbols only work if we have
6831 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6832 use dot-symbols and set the function symbol size to the text
6833 size of the function rather than the size of the descriptor.
6834 That's wrong for copying a descriptor. */
6835 if (ppc_elf_hash_entry (h)->oh == NULL
6836 || !(h->size == 24 || h->size == 16))
6837 return true;
6839 /* We should never get here, but unfortunately there are old
6840 versions of gcc (circa gcc-3.2) that improperly for the
6841 ELFv1 ABI put initialized function pointers, vtable refs and
6842 suchlike in read-only sections. Allow them to proceed, but
6843 warn that this might break at runtime. */
6844 info->callbacks->einfo
6845 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6846 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6847 h->root.root.string);
6850 /* This is a reference to a symbol defined by a dynamic object which
6851 is not a function. */
6853 /* We must allocate the symbol in our .dynbss section, which will
6854 become part of the .bss section of the executable. There will be
6855 an entry for this symbol in the .dynsym section. The dynamic
6856 object will contain position independent code, so all references
6857 from the dynamic object to this symbol will go through the global
6858 offset table. The dynamic linker will use the .dynsym entry to
6859 determine the address it must put in the global offset table, so
6860 both the dynamic object and the regular object will refer to the
6861 same memory location for the variable. */
6862 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6864 s = htab->elf.sdynrelro;
6865 srel = htab->elf.sreldynrelro;
6867 else
6869 s = htab->elf.sdynbss;
6870 srel = htab->elf.srelbss;
6872 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6874 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6875 linker to copy the initial value out of the dynamic object
6876 and into the runtime process image. */
6877 srel->size += sizeof (Elf64_External_Rela);
6878 h->needs_copy = 1;
6881 /* We no longer want dyn_relocs. */
6882 h->dyn_relocs = NULL;
6883 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6886 /* If given a function descriptor symbol, hide both the function code
6887 sym and the descriptor. */
6888 static void
6889 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6890 struct elf_link_hash_entry *h,
6891 bool force_local)
6893 struct ppc_link_hash_entry *eh;
6894 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6896 if (ppc_hash_table (info) == NULL)
6897 return;
6899 eh = ppc_elf_hash_entry (h);
6900 if (eh->is_func_descriptor)
6902 struct ppc_link_hash_entry *fh = eh->oh;
6904 if (fh == NULL)
6906 const char *p, *q;
6907 struct elf_link_hash_table *htab = elf_hash_table (info);
6908 char save;
6910 /* We aren't supposed to use alloca in BFD because on
6911 systems which do not have alloca the version in libiberty
6912 calls xmalloc, which might cause the program to crash
6913 when it runs out of memory. This function doesn't have a
6914 return status, so there's no way to gracefully return an
6915 error. So cheat. We know that string[-1] can be safely
6916 accessed; It's either a string in an ELF string table,
6917 or allocated in an objalloc structure. */
6919 p = eh->elf.root.root.string - 1;
6920 save = *p;
6921 *(char *) p = '.';
6922 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6923 false, false));
6924 *(char *) p = save;
6926 /* Unfortunately, if it so happens that the string we were
6927 looking for was allocated immediately before this string,
6928 then we overwrote the string terminator. That's the only
6929 reason the lookup should fail. */
6930 if (fh == NULL)
6932 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6933 while (q >= eh->elf.root.root.string && *q == *p)
6934 --q, --p;
6935 if (q < eh->elf.root.root.string && *p == '.')
6936 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6937 false, false));
6939 if (fh != NULL)
6941 eh->oh = fh;
6942 fh->oh = eh;
6945 if (fh != NULL)
6946 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6950 static bool
6951 get_sym_h (struct elf_link_hash_entry **hp,
6952 Elf_Internal_Sym **symp,
6953 asection **symsecp,
6954 unsigned char **tls_maskp,
6955 Elf_Internal_Sym **locsymsp,
6956 unsigned long r_symndx,
6957 bfd *ibfd)
6959 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6961 if (r_symndx >= symtab_hdr->sh_info)
6963 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6964 struct elf_link_hash_entry *h;
6966 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6967 h = elf_follow_link (h);
6969 if (hp != NULL)
6970 *hp = h;
6972 if (symp != NULL)
6973 *symp = NULL;
6975 if (symsecp != NULL)
6977 asection *symsec = NULL;
6978 if (h->root.type == bfd_link_hash_defined
6979 || h->root.type == bfd_link_hash_defweak)
6980 symsec = h->root.u.def.section;
6981 *symsecp = symsec;
6984 if (tls_maskp != NULL)
6985 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
6987 else
6989 Elf_Internal_Sym *sym;
6990 Elf_Internal_Sym *locsyms = *locsymsp;
6992 if (locsyms == NULL)
6994 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6995 if (locsyms == NULL)
6996 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6997 symtab_hdr->sh_info,
6998 0, NULL, NULL, NULL);
6999 if (locsyms == NULL)
7000 return false;
7001 *locsymsp = locsyms;
7003 sym = locsyms + r_symndx;
7005 if (hp != NULL)
7006 *hp = NULL;
7008 if (symp != NULL)
7009 *symp = sym;
7011 if (symsecp != NULL)
7012 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7014 if (tls_maskp != NULL)
7016 struct got_entry **lgot_ents;
7017 unsigned char *tls_mask;
7019 tls_mask = NULL;
7020 lgot_ents = elf_local_got_ents (ibfd);
7021 if (lgot_ents != NULL)
7023 struct plt_entry **local_plt = (struct plt_entry **)
7024 (lgot_ents + symtab_hdr->sh_info);
7025 unsigned char *lgot_masks = (unsigned char *)
7026 (local_plt + symtab_hdr->sh_info);
7027 tls_mask = &lgot_masks[r_symndx];
7029 *tls_maskp = tls_mask;
7032 return true;
7035 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7036 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7037 type suitable for optimization, and 1 otherwise. */
7039 static int
7040 get_tls_mask (unsigned char **tls_maskp,
7041 unsigned long *toc_symndx,
7042 bfd_vma *toc_addend,
7043 Elf_Internal_Sym **locsymsp,
7044 const Elf_Internal_Rela *rel,
7045 bfd *ibfd)
7047 unsigned long r_symndx;
7048 int next_r;
7049 struct elf_link_hash_entry *h;
7050 Elf_Internal_Sym *sym;
7051 asection *sec;
7052 bfd_vma off;
7054 r_symndx = ELF64_R_SYM (rel->r_info);
7055 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7056 return 0;
7058 if ((*tls_maskp != NULL
7059 && (**tls_maskp & TLS_TLS) != 0
7060 && **tls_maskp != (TLS_TLS | TLS_MARK))
7061 || sec == NULL
7062 || ppc64_elf_section_data (sec) == NULL
7063 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7064 return 1;
7066 /* Look inside a TOC section too. */
7067 if (h != NULL)
7069 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7070 off = h->root.u.def.value;
7072 else
7073 off = sym->st_value;
7074 off += rel->r_addend;
7075 BFD_ASSERT (off % 8 == 0);
7076 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7077 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7078 if (toc_symndx != NULL)
7079 *toc_symndx = r_symndx;
7080 if (toc_addend != NULL)
7081 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7082 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7083 return 0;
7084 if ((h == NULL || is_static_defined (h))
7085 && (next_r == -1 || next_r == -2))
7086 return 1 - next_r;
7087 return 1;
7090 /* Find (or create) an entry in the tocsave hash table. */
7092 static struct tocsave_entry *
7093 tocsave_find (struct ppc_link_hash_table *htab,
7094 enum insert_option insert,
7095 Elf_Internal_Sym **local_syms,
7096 const Elf_Internal_Rela *irela,
7097 bfd *ibfd)
7099 unsigned long r_indx;
7100 struct elf_link_hash_entry *h;
7101 Elf_Internal_Sym *sym;
7102 struct tocsave_entry ent, *p;
7103 hashval_t hash;
7104 struct tocsave_entry **slot;
7106 r_indx = ELF64_R_SYM (irela->r_info);
7107 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7108 return NULL;
7109 if (ent.sec == NULL || ent.sec->output_section == NULL)
7111 _bfd_error_handler
7112 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7113 return NULL;
7116 if (h != NULL)
7117 ent.offset = h->root.u.def.value;
7118 else
7119 ent.offset = sym->st_value;
7120 ent.offset += irela->r_addend;
7122 hash = tocsave_htab_hash (&ent);
7123 slot = ((struct tocsave_entry **)
7124 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7125 if (slot == NULL)
7126 return NULL;
7128 if (*slot == NULL)
7130 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7131 if (p == NULL)
7132 return NULL;
7133 *p = ent;
7134 *slot = p;
7136 return *slot;
7139 /* Adjust all global syms defined in opd sections. In gcc generated
7140 code for the old ABI, these will already have been done. */
7142 static bool
7143 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7145 struct ppc_link_hash_entry *eh;
7146 asection *sym_sec;
7147 struct _opd_sec_data *opd;
7149 if (h->root.type == bfd_link_hash_indirect)
7150 return true;
7152 if (h->root.type != bfd_link_hash_defined
7153 && h->root.type != bfd_link_hash_defweak)
7154 return true;
7156 eh = ppc_elf_hash_entry (h);
7157 if (eh->adjust_done)
7158 return true;
7160 sym_sec = eh->elf.root.u.def.section;
7161 opd = get_opd_info (sym_sec);
7162 if (opd != NULL && opd->adjust != NULL)
7164 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7165 if (adjust == -1)
7167 /* This entry has been deleted. */
7168 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7169 if (dsec == NULL)
7171 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7172 if (discarded_section (dsec))
7174 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7175 break;
7178 eh->elf.root.u.def.value = 0;
7179 eh->elf.root.u.def.section = dsec;
7181 else
7182 eh->elf.root.u.def.value += adjust;
7183 eh->adjust_done = 1;
7185 return true;
7188 /* Handles decrementing dynamic reloc counts for the reloc specified by
7189 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7190 have already been determined. */
7192 static bool
7193 dec_dynrel_count (const Elf_Internal_Rela *rel,
7194 asection *sec,
7195 struct bfd_link_info *info,
7196 Elf_Internal_Sym **local_syms,
7197 struct elf_link_hash_entry *h,
7198 Elf_Internal_Sym *sym)
7200 enum elf_ppc64_reloc_type r_type;
7201 asection *sym_sec = NULL;
7203 /* Can this reloc be dynamic? This switch, and later tests here
7204 should be kept in sync with the code in check_relocs. */
7205 r_type = ELF64_R_TYPE (rel->r_info);
7206 switch (r_type)
7208 default:
7209 return true;
7211 case R_PPC64_TOC16:
7212 case R_PPC64_TOC16_DS:
7213 case R_PPC64_TOC16_LO:
7214 case R_PPC64_TOC16_HI:
7215 case R_PPC64_TOC16_HA:
7216 case R_PPC64_TOC16_LO_DS:
7217 if (h == NULL)
7218 return true;
7219 break;
7221 case R_PPC64_TPREL16:
7222 case R_PPC64_TPREL16_LO:
7223 case R_PPC64_TPREL16_HI:
7224 case R_PPC64_TPREL16_HA:
7225 case R_PPC64_TPREL16_DS:
7226 case R_PPC64_TPREL16_LO_DS:
7227 case R_PPC64_TPREL16_HIGH:
7228 case R_PPC64_TPREL16_HIGHA:
7229 case R_PPC64_TPREL16_HIGHER:
7230 case R_PPC64_TPREL16_HIGHERA:
7231 case R_PPC64_TPREL16_HIGHEST:
7232 case R_PPC64_TPREL16_HIGHESTA:
7233 case R_PPC64_TPREL64:
7234 case R_PPC64_TPREL34:
7235 case R_PPC64_DTPMOD64:
7236 case R_PPC64_DTPREL64:
7237 case R_PPC64_ADDR64:
7238 case R_PPC64_REL30:
7239 case R_PPC64_REL32:
7240 case R_PPC64_REL64:
7241 case R_PPC64_ADDR14:
7242 case R_PPC64_ADDR14_BRNTAKEN:
7243 case R_PPC64_ADDR14_BRTAKEN:
7244 case R_PPC64_ADDR16:
7245 case R_PPC64_ADDR16_DS:
7246 case R_PPC64_ADDR16_HA:
7247 case R_PPC64_ADDR16_HI:
7248 case R_PPC64_ADDR16_HIGH:
7249 case R_PPC64_ADDR16_HIGHA:
7250 case R_PPC64_ADDR16_HIGHER:
7251 case R_PPC64_ADDR16_HIGHERA:
7252 case R_PPC64_ADDR16_HIGHEST:
7253 case R_PPC64_ADDR16_HIGHESTA:
7254 case R_PPC64_ADDR16_LO:
7255 case R_PPC64_ADDR16_LO_DS:
7256 case R_PPC64_ADDR24:
7257 case R_PPC64_ADDR32:
7258 case R_PPC64_UADDR16:
7259 case R_PPC64_UADDR32:
7260 case R_PPC64_UADDR64:
7261 case R_PPC64_TOC:
7262 case R_PPC64_D34:
7263 case R_PPC64_D34_LO:
7264 case R_PPC64_D34_HI30:
7265 case R_PPC64_D34_HA30:
7266 case R_PPC64_ADDR16_HIGHER34:
7267 case R_PPC64_ADDR16_HIGHERA34:
7268 case R_PPC64_ADDR16_HIGHEST34:
7269 case R_PPC64_ADDR16_HIGHESTA34:
7270 case R_PPC64_D28:
7271 break;
7274 if (local_syms != NULL)
7276 unsigned long r_symndx;
7277 bfd *ibfd = sec->owner;
7279 r_symndx = ELF64_R_SYM (rel->r_info);
7280 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7281 return false;
7284 if ((h != NULL
7285 && !SYMBOL_REFERENCES_LOCAL (info, h))
7286 || (bfd_link_pic (info)
7287 && (h != NULL
7288 ? !bfd_is_abs_symbol (&h->root)
7289 : sym_sec != bfd_abs_section_ptr)
7290 && must_be_dyn_reloc (info, r_type))
7291 || (!bfd_link_pic (info)
7292 && (h != NULL
7293 ? h->type == STT_GNU_IFUNC
7294 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
7296 else
7297 return true;
7299 if (h != NULL)
7301 struct ppc_dyn_relocs *p;
7302 struct ppc_dyn_relocs **pp;
7303 pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
7305 /* elf_gc_sweep may have already removed all dyn relocs associated
7306 with local syms for a given section. Also, symbol flags are
7307 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7308 report a dynreloc miscount. */
7309 if (*pp == NULL && info->gc_sections)
7310 return true;
7312 while ((p = *pp) != NULL)
7314 if (p->sec == sec)
7316 if (!must_be_dyn_reloc (info, r_type))
7317 p->pc_count -= 1;
7318 if (maybe_relr (r_type, rel, sec))
7319 p->rel_count -= 1;
7320 p->count -= 1;
7321 if (p->count == 0)
7322 *pp = p->next;
7323 return true;
7325 pp = &p->next;
7328 else
7330 struct ppc_local_dyn_relocs *p;
7331 struct ppc_local_dyn_relocs **pp;
7332 void *vpp;
7333 bool is_ifunc;
7335 if (local_syms == NULL)
7336 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7337 if (sym_sec == NULL)
7338 sym_sec = sec;
7340 vpp = &elf_section_data (sym_sec)->local_dynrel;
7341 pp = (struct ppc_local_dyn_relocs **) vpp;
7343 if (*pp == NULL && info->gc_sections)
7344 return true;
7346 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7347 while ((p = *pp) != NULL)
7349 if (p->sec == sec && p->ifunc == is_ifunc)
7351 if (maybe_relr (r_type, rel, sec))
7352 p->rel_count -= 1;
7353 p->count -= 1;
7354 if (p->count == 0)
7355 *pp = p->next;
7356 return true;
7358 pp = &p->next;
7362 /* xgettext:c-format */
7363 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7364 sec->owner, sec);
7365 bfd_set_error (bfd_error_bad_value);
7366 return false;
7369 /* Remove unused Official Procedure Descriptor entries. Currently we
7370 only remove those associated with functions in discarded link-once
7371 sections, or weakly defined functions that have been overridden. It
7372 would be possible to remove many more entries for statically linked
7373 applications. */
7375 bool
7376 ppc64_elf_edit_opd (struct bfd_link_info *info)
7378 bfd *ibfd;
7379 bool some_edited = false;
7380 asection *need_pad = NULL;
7381 struct ppc_link_hash_table *htab;
7383 htab = ppc_hash_table (info);
7384 if (htab == NULL)
7385 return false;
7387 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7389 asection *sec;
7390 Elf_Internal_Rela *relstart, *rel, *relend;
7391 Elf_Internal_Shdr *symtab_hdr;
7392 Elf_Internal_Sym *local_syms;
7393 struct _opd_sec_data *opd;
7394 bool need_edit, add_aux_fields, broken;
7395 bfd_size_type cnt_16b = 0;
7397 if (!is_ppc64_elf (ibfd))
7398 continue;
7400 sec = bfd_get_section_by_name (ibfd, ".opd");
7401 if (sec == NULL
7402 || sec->size == 0
7403 || (sec->flags & SEC_HAS_CONTENTS) == 0)
7404 continue;
7406 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7407 continue;
7409 if (sec->output_section == bfd_abs_section_ptr)
7410 continue;
7412 /* Look through the section relocs. */
7413 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7414 continue;
7416 local_syms = NULL;
7417 symtab_hdr = &elf_symtab_hdr (ibfd);
7419 /* Read the relocations. */
7420 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7421 info->keep_memory);
7422 if (relstart == NULL)
7423 return false;
7425 /* First run through the relocs to check they are sane, and to
7426 determine whether we need to edit this opd section. */
7427 need_edit = false;
7428 broken = false;
7429 need_pad = sec;
7430 relend = relstart + sec->reloc_count;
7431 for (rel = relstart; rel < relend; )
7433 enum elf_ppc64_reloc_type r_type;
7434 unsigned long r_symndx;
7435 asection *sym_sec;
7436 struct elf_link_hash_entry *h;
7437 Elf_Internal_Sym *sym;
7438 bfd_vma offset;
7440 /* .opd contains an array of 16 or 24 byte entries. We're
7441 only interested in the reloc pointing to a function entry
7442 point. */
7443 offset = rel->r_offset;
7444 if (rel + 1 == relend
7445 || rel[1].r_offset != offset + 8)
7447 /* If someone messes with .opd alignment then after a
7448 "ld -r" we might have padding in the middle of .opd.
7449 Also, there's nothing to prevent someone putting
7450 something silly in .opd with the assembler. No .opd
7451 optimization for them! */
7452 broken_opd:
7453 _bfd_error_handler
7454 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7455 broken = true;
7456 break;
7459 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7460 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7462 _bfd_error_handler
7463 /* xgettext:c-format */
7464 (_("%pB: unexpected reloc type %u in .opd section"),
7465 ibfd, r_type);
7466 broken = true;
7467 break;
7470 r_symndx = ELF64_R_SYM (rel->r_info);
7471 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7472 r_symndx, ibfd))
7473 goto error_ret;
7475 if (sym_sec == NULL || sym_sec->owner == NULL)
7477 const char *sym_name;
7478 if (h != NULL)
7479 sym_name = h->root.root.string;
7480 else
7481 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7482 sym_sec);
7484 _bfd_error_handler
7485 /* xgettext:c-format */
7486 (_("%pB: undefined sym `%s' in .opd section"),
7487 ibfd, sym_name);
7488 broken = true;
7489 break;
7492 /* opd entries are always for functions defined in the
7493 current input bfd. If the symbol isn't defined in the
7494 input bfd, then we won't be using the function in this
7495 bfd; It must be defined in a linkonce section in another
7496 bfd, or is weak. It's also possible that we are
7497 discarding the function due to a linker script /DISCARD/,
7498 which we test for via the output_section. */
7499 if (sym_sec->owner != ibfd
7500 || sym_sec->output_section == bfd_abs_section_ptr)
7501 need_edit = true;
7503 rel += 2;
7504 if (rel + 1 == relend
7505 || (rel + 2 < relend
7506 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7507 ++rel;
7509 if (rel == relend)
7511 if (sec->size == offset + 24)
7513 need_pad = NULL;
7514 break;
7516 if (sec->size == offset + 16)
7518 cnt_16b++;
7519 break;
7521 goto broken_opd;
7523 else if (rel + 1 < relend
7524 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7525 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7527 if (rel[0].r_offset == offset + 16)
7528 cnt_16b++;
7529 else if (rel[0].r_offset != offset + 24)
7530 goto broken_opd;
7532 else
7533 goto broken_opd;
7536 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7538 if (!broken && (need_edit || add_aux_fields))
7540 Elf_Internal_Rela *write_rel;
7541 Elf_Internal_Shdr *rel_hdr;
7542 bfd_byte *rptr, *wptr;
7543 bfd_byte *new_contents;
7544 bfd_size_type amt;
7546 new_contents = NULL;
7547 amt = OPD_NDX (sec->size) * sizeof (long);
7548 opd = &ppc64_elf_section_data (sec)->u.opd;
7549 opd->adjust = bfd_zalloc (sec->owner, amt);
7550 if (opd->adjust == NULL)
7551 return false;
7553 /* This seems a waste of time as input .opd sections are all
7554 zeros as generated by gcc, but I suppose there's no reason
7555 this will always be so. We might start putting something in
7556 the third word of .opd entries. */
7557 if ((sec->flags & SEC_IN_MEMORY) == 0)
7559 bfd_byte *loc;
7560 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7562 free (loc);
7563 error_ret:
7564 if (symtab_hdr->contents != (unsigned char *) local_syms)
7565 free (local_syms);
7566 if (elf_section_data (sec)->relocs != relstart)
7567 free (relstart);
7568 return false;
7570 sec->contents = loc;
7571 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7574 elf_section_data (sec)->relocs = relstart;
7576 new_contents = sec->contents;
7577 if (add_aux_fields)
7579 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7580 if (new_contents == NULL)
7581 return false;
7582 need_pad = NULL;
7584 wptr = new_contents;
7585 rptr = sec->contents;
7586 write_rel = relstart;
7587 for (rel = relstart; rel < relend; )
7589 unsigned long r_symndx;
7590 asection *sym_sec;
7591 struct elf_link_hash_entry *h;
7592 struct ppc_link_hash_entry *fdh = NULL;
7593 Elf_Internal_Sym *sym;
7594 long opd_ent_size;
7595 Elf_Internal_Rela *next_rel;
7596 bool skip;
7598 r_symndx = ELF64_R_SYM (rel->r_info);
7599 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7600 r_symndx, ibfd))
7601 goto error_ret;
7603 next_rel = rel + 2;
7604 if (next_rel + 1 == relend
7605 || (next_rel + 2 < relend
7606 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7607 ++next_rel;
7609 /* See if the .opd entry is full 24 byte or
7610 16 byte (with fd_aux entry overlapped with next
7611 fd_func). */
7612 opd_ent_size = 24;
7613 if (next_rel == relend)
7615 if (sec->size == rel->r_offset + 16)
7616 opd_ent_size = 16;
7618 else if (next_rel->r_offset == rel->r_offset + 16)
7619 opd_ent_size = 16;
7621 if (h != NULL
7622 && h->root.root.string[0] == '.')
7624 fdh = ppc_elf_hash_entry (h)->oh;
7625 if (fdh != NULL)
7627 fdh = ppc_follow_link (fdh);
7628 if (fdh->elf.root.type != bfd_link_hash_defined
7629 && fdh->elf.root.type != bfd_link_hash_defweak)
7630 fdh = NULL;
7634 skip = (sym_sec->owner != ibfd
7635 || sym_sec->output_section == bfd_abs_section_ptr);
7636 if (skip)
7638 if (fdh != NULL && sym_sec->owner == ibfd)
7640 /* Arrange for the function descriptor sym
7641 to be dropped. */
7642 fdh->elf.root.u.def.value = 0;
7643 fdh->elf.root.u.def.section = sym_sec;
7645 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7647 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7648 rel = next_rel;
7649 else
7650 while (1)
7652 if (!dec_dynrel_count (rel, sec, info,
7653 NULL, h, sym))
7654 goto error_ret;
7656 if (++rel == next_rel)
7657 break;
7659 r_symndx = ELF64_R_SYM (rel->r_info);
7660 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7661 r_symndx, ibfd))
7662 goto error_ret;
7665 else
7667 /* We'll be keeping this opd entry. */
7668 long adjust;
7670 if (fdh != NULL)
7672 /* Redefine the function descriptor symbol to
7673 this location in the opd section. It is
7674 necessary to update the value here rather
7675 than using an array of adjustments as we do
7676 for local symbols, because various places
7677 in the generic ELF code use the value
7678 stored in u.def.value. */
7679 fdh->elf.root.u.def.value = wptr - new_contents;
7680 fdh->adjust_done = 1;
7683 /* Local syms are a bit tricky. We could
7684 tweak them as they can be cached, but
7685 we'd need to look through the local syms
7686 for the function descriptor sym which we
7687 don't have at the moment. So keep an
7688 array of adjustments. */
7689 adjust = (wptr - new_contents) - (rptr - sec->contents);
7690 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7692 if (wptr != rptr)
7693 memcpy (wptr, rptr, opd_ent_size);
7694 wptr += opd_ent_size;
7695 if (add_aux_fields && opd_ent_size == 16)
7697 memset (wptr, '\0', 8);
7698 wptr += 8;
7701 /* We need to adjust any reloc offsets to point to the
7702 new opd entries. */
7703 for ( ; rel != next_rel; ++rel)
7705 rel->r_offset += adjust;
7706 if (write_rel != rel)
7707 memcpy (write_rel, rel, sizeof (*rel));
7708 ++write_rel;
7712 rptr += opd_ent_size;
7715 sec->size = wptr - new_contents;
7716 sec->reloc_count = write_rel - relstart;
7717 if (add_aux_fields)
7719 free (sec->contents);
7720 sec->contents = new_contents;
7723 /* Fudge the header size too, as this is used later in
7724 elf_bfd_final_link if we are emitting relocs. */
7725 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7726 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7727 some_edited = true;
7729 else if (elf_section_data (sec)->relocs != relstart)
7730 free (relstart);
7732 if (local_syms != NULL
7733 && symtab_hdr->contents != (unsigned char *) local_syms)
7735 if (!info->keep_memory)
7736 free (local_syms);
7737 else
7738 symtab_hdr->contents = (unsigned char *) local_syms;
7742 if (some_edited)
7743 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7745 /* If we are doing a final link and the last .opd entry is just 16 byte
7746 long, add a 8 byte padding after it. */
7747 if (need_pad != NULL && !bfd_link_relocatable (info))
7749 bfd_byte *p;
7751 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7753 BFD_ASSERT (need_pad->size > 0);
7755 p = bfd_malloc (need_pad->size + 8);
7756 if (p == NULL)
7757 return false;
7759 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7760 p, 0, need_pad->size))
7761 return false;
7763 need_pad->contents = p;
7764 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7766 else
7768 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7769 if (p == NULL)
7770 return false;
7772 need_pad->contents = p;
7775 memset (need_pad->contents + need_pad->size, 0, 8);
7776 need_pad->size += 8;
7779 return true;
7782 /* Analyze inline PLT call relocations to see whether calls to locally
7783 defined functions can be converted to direct calls. */
7785 bool
7786 ppc64_elf_inline_plt (struct bfd_link_info *info)
7788 struct ppc_link_hash_table *htab;
7789 bfd *ibfd;
7790 asection *sec;
7791 bfd_vma low_vma, high_vma, limit;
7793 htab = ppc_hash_table (info);
7794 if (htab == NULL)
7795 return false;
7797 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7798 reduced somewhat to cater for possible stubs that might be added
7799 between the call and its destination. */
7800 if (htab->params->group_size < 0)
7802 limit = -htab->params->group_size;
7803 if (limit == 1)
7804 limit = 0x1e00000;
7806 else
7808 limit = htab->params->group_size;
7809 if (limit == 1)
7810 limit = 0x1c00000;
7813 low_vma = -1;
7814 high_vma = 0;
7815 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7816 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7818 if (low_vma > sec->vma)
7819 low_vma = sec->vma;
7820 if (high_vma < sec->vma + sec->size)
7821 high_vma = sec->vma + sec->size;
7824 /* If a "bl" can reach anywhere in local code sections, then we can
7825 convert all inline PLT sequences to direct calls when the symbol
7826 is local. */
7827 if (high_vma - low_vma < limit)
7829 htab->can_convert_all_inline_plt = 1;
7830 return true;
7833 /* Otherwise, go looking through relocs for cases where a direct
7834 call won't reach. Mark the symbol on any such reloc to disable
7835 the optimization and keep the PLT entry as it seems likely that
7836 this will be better than creating trampolines. Note that this
7837 will disable the optimization for all inline PLT calls to a
7838 particular symbol, not just those that won't reach. The
7839 difficulty in doing a more precise optimization is that the
7840 linker needs to make a decision depending on whether a
7841 particular R_PPC64_PLTCALL insn can be turned into a direct
7842 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7843 the sequence, and there is nothing that ties those relocs
7844 together except their symbol. */
7846 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7848 Elf_Internal_Shdr *symtab_hdr;
7849 Elf_Internal_Sym *local_syms;
7851 if (!is_ppc64_elf (ibfd))
7852 continue;
7854 local_syms = NULL;
7855 symtab_hdr = &elf_symtab_hdr (ibfd);
7857 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7858 if (ppc64_elf_section_data (sec)->has_pltcall
7859 && !bfd_is_abs_section (sec->output_section))
7861 Elf_Internal_Rela *relstart, *rel, *relend;
7863 /* Read the relocations. */
7864 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7865 info->keep_memory);
7866 if (relstart == NULL)
7867 return false;
7869 relend = relstart + sec->reloc_count;
7870 for (rel = relstart; rel < relend; rel++)
7872 enum elf_ppc64_reloc_type r_type;
7873 unsigned long r_symndx;
7874 asection *sym_sec;
7875 struct elf_link_hash_entry *h;
7876 Elf_Internal_Sym *sym;
7877 unsigned char *tls_maskp;
7879 r_type = ELF64_R_TYPE (rel->r_info);
7880 if (r_type != R_PPC64_PLTCALL
7881 && r_type != R_PPC64_PLTCALL_NOTOC)
7882 continue;
7884 r_symndx = ELF64_R_SYM (rel->r_info);
7885 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7886 r_symndx, ibfd))
7888 if (elf_section_data (sec)->relocs != relstart)
7889 free (relstart);
7890 if (symtab_hdr->contents != (bfd_byte *) local_syms)
7891 free (local_syms);
7892 return false;
7895 if (sym_sec != NULL && sym_sec->output_section != NULL)
7897 bfd_vma from, to;
7898 if (h != NULL)
7899 to = h->root.u.def.value;
7900 else
7901 to = sym->st_value;
7902 to += (rel->r_addend
7903 + sym_sec->output_offset
7904 + sym_sec->output_section->vma);
7905 from = (rel->r_offset
7906 + sec->output_offset
7907 + sec->output_section->vma);
7908 if (to - from + limit < 2 * limit
7909 && !(r_type == R_PPC64_PLTCALL_NOTOC
7910 && (((h ? h->other : sym->st_other)
7911 & STO_PPC64_LOCAL_MASK)
7912 > 1 << STO_PPC64_LOCAL_BIT)))
7913 *tls_maskp &= ~PLT_KEEP;
7916 if (elf_section_data (sec)->relocs != relstart)
7917 free (relstart);
7920 if (local_syms != NULL
7921 && symtab_hdr->contents != (unsigned char *) local_syms)
7923 if (!info->keep_memory)
7924 free (local_syms);
7925 else
7926 symtab_hdr->contents = (unsigned char *) local_syms;
7930 return true;
7933 /* Set htab->tls_get_addr and various other info specific to TLS.
7934 This needs to run before dynamic symbols are processed in
7935 bfd_elf_size_dynamic_sections. */
7937 bool
7938 ppc64_elf_tls_setup (struct bfd_link_info *info)
7940 struct ppc_link_hash_table *htab;
7941 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
7943 htab = ppc_hash_table (info);
7944 if (htab == NULL)
7945 return false;
7947 /* Move dynamic linking info to the function descriptor sym. */
7948 if (htab->need_func_desc_adj)
7950 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7951 htab->need_func_desc_adj = 0;
7954 if (abiversion (info->output_bfd) == 1)
7955 htab->opd_abi = 1;
7957 if (htab->params->no_multi_toc)
7958 htab->do_multi_toc = 0;
7959 else if (!htab->do_multi_toc)
7960 htab->params->no_multi_toc = 1;
7962 /* Default to --no-plt-localentry, as this option can cause problems
7963 with symbol interposition. For example, glibc libpthread.so and
7964 libc.so duplicate many pthread symbols, with a fallback
7965 implementation in libc.so. In some cases the fallback does more
7966 work than the pthread implementation. __pthread_condattr_destroy
7967 is one such symbol: the libpthread.so implementation is
7968 localentry:0 while the libc.so implementation is localentry:8.
7969 An app that "cleverly" uses dlopen to only load necessary
7970 libraries at runtime may omit loading libpthread.so when not
7971 running multi-threaded, which then results in the libc.so
7972 fallback symbols being used and ld.so complaining. Now there
7973 are workarounds in ld (see non_zero_localentry) to detect the
7974 pthread situation, but that may not be the only case where
7975 --plt-localentry can cause trouble. */
7976 if (htab->params->plt_localentry0 < 0)
7977 htab->params->plt_localentry0 = 0;
7978 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7980 /* The issue is that __glink_PLTresolve saves r2, which is done
7981 because glibc ld.so _dl_runtime_resolve restores r2 to support
7982 a glibc plt call optimisation where global entry code is
7983 skipped on calls that resolve to the same binary. The
7984 __glink_PLTresolve save of r2 is incompatible with code
7985 making tail calls, because the tail call might go via the
7986 resolver and thus overwrite the proper saved r2. */
7987 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7988 "power10 pc-relative code"));
7989 htab->params->plt_localentry0 = 0;
7991 if (htab->params->plt_localentry0
7992 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7993 false, false, false) == NULL)
7994 _bfd_error_handler
7995 (_("warning: --plt-localentry is especially dangerous without "
7996 "ld.so support to detect ABI violations"));
7998 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7999 false, false, true);
8000 htab->tls_get_addr = ppc_elf_hash_entry (tga);
8001 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8002 false, false, true);
8003 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
8005 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
8006 false, false, true);
8007 htab->tga_desc = ppc_elf_hash_entry (desc);
8008 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
8009 false, false, true);
8010 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
8012 if (htab->params->tls_get_addr_opt)
8014 struct elf_link_hash_entry *opt, *opt_fd;
8016 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8017 false, false, true);
8018 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8019 false, false, true);
8020 if (opt_fd != NULL
8021 && (opt_fd->root.type == bfd_link_hash_defined
8022 || opt_fd->root.type == bfd_link_hash_defweak))
8024 /* If glibc supports an optimized __tls_get_addr call stub,
8025 signalled by the presence of __tls_get_addr_opt, and we'll
8026 be calling __tls_get_addr via a plt call stub, then
8027 make __tls_get_addr point to __tls_get_addr_opt. */
8028 if (!(htab->elf.dynamic_sections_created
8029 && tga_fd != NULL
8030 && (tga_fd->type == STT_FUNC
8031 || tga_fd->needs_plt)
8032 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8033 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
8034 tga_fd = NULL;
8035 if (!(htab->elf.dynamic_sections_created
8036 && desc_fd != NULL
8037 && (desc_fd->type == STT_FUNC
8038 || desc_fd->needs_plt)
8039 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
8040 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
8041 desc_fd = NULL;
8043 if (tga_fd != NULL || desc_fd != NULL)
8045 struct plt_entry *ent = NULL;
8047 if (tga_fd != NULL)
8048 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8049 if (ent->plt.refcount > 0)
8050 break;
8051 if (ent == NULL && desc_fd != NULL)
8052 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
8053 if (ent->plt.refcount > 0)
8054 break;
8055 if (ent != NULL)
8057 if (tga_fd != NULL)
8059 tga_fd->root.type = bfd_link_hash_indirect;
8060 tga_fd->root.u.i.link = &opt_fd->root;
8061 tga_fd->root.u.i.warning = NULL;
8062 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8064 if (desc_fd != NULL)
8066 desc_fd->root.type = bfd_link_hash_indirect;
8067 desc_fd->root.u.i.link = &opt_fd->root;
8068 desc_fd->root.u.i.warning = NULL;
8069 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
8071 opt_fd->mark = 1;
8072 if (opt_fd->dynindx != -1)
8074 /* Use __tls_get_addr_opt in dynamic relocations. */
8075 opt_fd->dynindx = -1;
8076 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8077 opt_fd->dynstr_index);
8078 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8079 return false;
8081 if (tga_fd != NULL)
8083 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
8084 tga = elf_hash_entry (htab->tls_get_addr);
8085 if (opt != NULL && tga != NULL)
8087 tga->root.type = bfd_link_hash_indirect;
8088 tga->root.u.i.link = &opt->root;
8089 tga->root.u.i.warning = NULL;
8090 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8091 opt->mark = 1;
8092 _bfd_elf_link_hash_hide_symbol (info, opt,
8093 tga->forced_local);
8094 htab->tls_get_addr = ppc_elf_hash_entry (opt);
8096 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8097 htab->tls_get_addr_fd->is_func_descriptor = 1;
8098 if (htab->tls_get_addr != NULL)
8100 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8101 htab->tls_get_addr->is_func = 1;
8104 if (desc_fd != NULL)
8106 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
8107 if (opt != NULL && desc != NULL)
8109 desc->root.type = bfd_link_hash_indirect;
8110 desc->root.u.i.link = &opt->root;
8111 desc->root.u.i.warning = NULL;
8112 ppc64_elf_copy_indirect_symbol (info, opt, desc);
8113 opt->mark = 1;
8114 _bfd_elf_link_hash_hide_symbol (info, opt,
8115 desc->forced_local);
8116 htab->tga_desc = ppc_elf_hash_entry (opt);
8118 htab->tga_desc_fd->oh = htab->tga_desc;
8119 htab->tga_desc_fd->is_func_descriptor = 1;
8120 if (htab->tga_desc != NULL)
8122 htab->tga_desc->oh = htab->tga_desc_fd;
8123 htab->tga_desc->is_func = 1;
8129 else if (htab->params->tls_get_addr_opt < 0)
8130 htab->params->tls_get_addr_opt = 0;
8133 if (htab->tga_desc_fd != NULL
8134 && htab->params->tls_get_addr_opt
8135 && htab->params->no_tls_get_addr_regsave == -1)
8136 htab->params->no_tls_get_addr_regsave = 0;
8138 return true;
8141 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8142 any of HASH1, HASH2, HASH3, or HASH4. */
8144 static bool
8145 branch_reloc_hash_match (bfd *ibfd,
8146 Elf_Internal_Rela *rel,
8147 struct ppc_link_hash_entry *hash1,
8148 struct ppc_link_hash_entry *hash2,
8149 struct ppc_link_hash_entry *hash3,
8150 struct ppc_link_hash_entry *hash4)
8152 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8153 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8154 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8156 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8158 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8159 struct elf_link_hash_entry *h;
8161 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8162 h = elf_follow_link (h);
8163 if (h == elf_hash_entry (hash1)
8164 || h == elf_hash_entry (hash2)
8165 || h == elf_hash_entry (hash3)
8166 || h == elf_hash_entry (hash4))
8167 return true;
8169 return false;
8172 /* Run through all the TLS relocs looking for optimization
8173 opportunities. The linker has been hacked (see ppc64elf.em) to do
8174 a preliminary section layout so that we know the TLS segment
8175 offsets. We can't optimize earlier because some optimizations need
8176 to know the tp offset, and we need to optimize before allocating
8177 dynamic relocations. */
8179 bool
8180 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8182 bfd *ibfd;
8183 asection *sec;
8184 struct ppc_link_hash_table *htab;
8185 unsigned char *toc_ref;
8186 int pass;
8188 if (!bfd_link_executable (info))
8189 return true;
8191 htab = ppc_hash_table (info);
8192 if (htab == NULL)
8193 return false;
8195 htab->do_tls_opt = 1;
8197 /* Make two passes over the relocs. On the first pass, mark toc
8198 entries involved with tls relocs, and check that tls relocs
8199 involved in setting up a tls_get_addr call are indeed followed by
8200 such a call. If they are not, we can't do any tls optimization.
8201 On the second pass twiddle tls_mask flags to notify
8202 relocate_section that optimization can be done, and adjust got
8203 and plt refcounts. */
8204 toc_ref = NULL;
8205 for (pass = 0; pass < 2; ++pass)
8206 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8208 Elf_Internal_Sym *locsyms = NULL;
8209 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8211 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8212 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8214 Elf_Internal_Rela *relstart, *rel, *relend;
8215 bool found_tls_get_addr_arg = 0;
8217 /* Read the relocations. */
8218 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8219 info->keep_memory);
8220 if (relstart == NULL)
8222 free (toc_ref);
8223 return false;
8226 relend = relstart + sec->reloc_count;
8227 for (rel = relstart; rel < relend; rel++)
8229 enum elf_ppc64_reloc_type r_type;
8230 unsigned long r_symndx;
8231 struct elf_link_hash_entry *h;
8232 Elf_Internal_Sym *sym;
8233 asection *sym_sec;
8234 unsigned char *tls_mask;
8235 unsigned int tls_set, tls_clear, tls_type = 0;
8236 bfd_vma value;
8237 bool ok_tprel, is_local;
8238 long toc_ref_index = 0;
8239 int expecting_tls_get_addr = 0;
8240 bool ret = false;
8242 r_symndx = ELF64_R_SYM (rel->r_info);
8243 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8244 r_symndx, ibfd))
8246 err_free_rel:
8247 if (elf_section_data (sec)->relocs != relstart)
8248 free (relstart);
8249 free (toc_ref);
8250 if (elf_symtab_hdr (ibfd).contents
8251 != (unsigned char *) locsyms)
8252 free (locsyms);
8253 return ret;
8256 if (h != NULL)
8258 if (h->root.type == bfd_link_hash_defined
8259 || h->root.type == bfd_link_hash_defweak)
8260 value = h->root.u.def.value;
8261 else if (h->root.type == bfd_link_hash_undefweak)
8262 value = 0;
8263 else
8265 found_tls_get_addr_arg = 0;
8266 continue;
8269 else
8270 /* Symbols referenced by TLS relocs must be of type
8271 STT_TLS. So no need for .opd local sym adjust. */
8272 value = sym->st_value;
8274 ok_tprel = false;
8275 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8276 if (is_local)
8278 if (h != NULL
8279 && h->root.type == bfd_link_hash_undefweak)
8280 ok_tprel = true;
8281 else if (sym_sec != NULL
8282 && sym_sec->output_section != NULL)
8284 value += sym_sec->output_offset;
8285 value += sym_sec->output_section->vma;
8286 value -= htab->elf.tls_sec->vma + TP_OFFSET;
8287 /* Note that even though the prefix insns
8288 allow a 1<<33 offset we use the same test
8289 as for addis;addi. There may be a mix of
8290 pcrel and non-pcrel code and the decision
8291 to optimise is per symbol, not per TLS
8292 sequence. */
8293 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
8297 r_type = ELF64_R_TYPE (rel->r_info);
8298 /* If this section has old-style __tls_get_addr calls
8299 without marker relocs, then check that each
8300 __tls_get_addr call reloc is preceded by a reloc
8301 that conceivably belongs to the __tls_get_addr arg
8302 setup insn. If we don't find matching arg setup
8303 relocs, don't do any tls optimization. */
8304 if (pass == 0
8305 && sec->nomark_tls_get_addr
8306 && h != NULL
8307 && is_tls_get_addr (h, htab)
8308 && !found_tls_get_addr_arg
8309 && is_branch_reloc (r_type))
8311 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8312 "TLS optimization disabled\n"),
8313 ibfd, sec, rel->r_offset);
8314 ret = true;
8315 goto err_free_rel;
8318 found_tls_get_addr_arg = 0;
8319 switch (r_type)
8321 case R_PPC64_GOT_TLSLD16:
8322 case R_PPC64_GOT_TLSLD16_LO:
8323 case R_PPC64_GOT_TLSLD_PCREL34:
8324 expecting_tls_get_addr = 1;
8325 found_tls_get_addr_arg = 1;
8326 /* Fall through. */
8328 case R_PPC64_GOT_TLSLD16_HI:
8329 case R_PPC64_GOT_TLSLD16_HA:
8330 /* These relocs should never be against a symbol
8331 defined in a shared lib. Leave them alone if
8332 that turns out to be the case. */
8333 if (!is_local)
8334 continue;
8336 /* LD -> LE */
8337 tls_set = 0;
8338 tls_clear = TLS_LD;
8339 tls_type = TLS_TLS | TLS_LD;
8340 break;
8342 case R_PPC64_GOT_TLSGD16:
8343 case R_PPC64_GOT_TLSGD16_LO:
8344 case R_PPC64_GOT_TLSGD_PCREL34:
8345 expecting_tls_get_addr = 1;
8346 found_tls_get_addr_arg = 1;
8347 /* Fall through. */
8349 case R_PPC64_GOT_TLSGD16_HI:
8350 case R_PPC64_GOT_TLSGD16_HA:
8351 if (ok_tprel)
8352 /* GD -> LE */
8353 tls_set = 0;
8354 else
8355 /* GD -> IE */
8356 tls_set = TLS_TLS | TLS_GDIE;
8357 tls_clear = TLS_GD;
8358 tls_type = TLS_TLS | TLS_GD;
8359 break;
8361 case R_PPC64_GOT_TPREL_PCREL34:
8362 case R_PPC64_GOT_TPREL16_DS:
8363 case R_PPC64_GOT_TPREL16_LO_DS:
8364 case R_PPC64_GOT_TPREL16_HI:
8365 case R_PPC64_GOT_TPREL16_HA:
8366 if (ok_tprel)
8368 /* IE -> LE */
8369 tls_set = 0;
8370 tls_clear = TLS_TPREL;
8371 tls_type = TLS_TLS | TLS_TPREL;
8372 break;
8374 continue;
8376 case R_PPC64_TLSLD:
8377 if (!is_local)
8378 continue;
8379 /* Fall through. */
8380 case R_PPC64_TLSGD:
8381 if (rel + 1 < relend
8382 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8384 if (pass != 0
8385 && (ELF64_R_TYPE (rel[1].r_info)
8386 != R_PPC64_PLTSEQ)
8387 && (ELF64_R_TYPE (rel[1].r_info)
8388 != R_PPC64_PLTSEQ_NOTOC))
8390 r_symndx = ELF64_R_SYM (rel[1].r_info);
8391 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8392 r_symndx, ibfd))
8393 goto err_free_rel;
8394 if (h != NULL)
8396 struct plt_entry *ent = NULL;
8398 for (ent = h->plt.plist;
8399 ent != NULL;
8400 ent = ent->next)
8401 if (ent->addend == rel[1].r_addend)
8402 break;
8404 if (ent != NULL
8405 && ent->plt.refcount > 0)
8406 ent->plt.refcount -= 1;
8409 continue;
8411 found_tls_get_addr_arg = 1;
8412 /* Fall through. */
8414 case R_PPC64_TLS:
8415 case R_PPC64_TOC16:
8416 case R_PPC64_TOC16_LO:
8417 if (sym_sec == NULL || sym_sec != toc)
8418 continue;
8420 /* Mark this toc entry as referenced by a TLS
8421 code sequence. We can do that now in the
8422 case of R_PPC64_TLS, and after checking for
8423 tls_get_addr for the TOC16 relocs. */
8424 if (toc_ref == NULL)
8425 toc_ref
8426 = bfd_zmalloc (toc->output_section->rawsize / 8);
8427 if (toc_ref == NULL)
8428 goto err_free_rel;
8430 if (h != NULL)
8431 value = h->root.u.def.value;
8432 else
8433 value = sym->st_value;
8434 value += rel->r_addend;
8435 if (value % 8 != 0)
8436 continue;
8437 BFD_ASSERT (value < toc->size
8438 && toc->output_offset % 8 == 0);
8439 toc_ref_index = (value + toc->output_offset) / 8;
8440 if (r_type == R_PPC64_TLS
8441 || r_type == R_PPC64_TLSGD
8442 || r_type == R_PPC64_TLSLD)
8444 toc_ref[toc_ref_index] = 1;
8445 continue;
8448 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8449 continue;
8451 tls_set = 0;
8452 tls_clear = 0;
8453 expecting_tls_get_addr = 2;
8454 break;
8456 case R_PPC64_TPREL64:
8457 if (pass == 0
8458 || sec != toc
8459 || toc_ref == NULL
8460 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8461 continue;
8462 if (ok_tprel)
8464 /* IE -> LE */
8465 tls_set = TLS_EXPLICIT;
8466 tls_clear = TLS_TPREL;
8467 break;
8469 continue;
8471 case R_PPC64_DTPMOD64:
8472 if (pass == 0
8473 || sec != toc
8474 || toc_ref == NULL
8475 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8476 continue;
8477 if (rel + 1 < relend
8478 && (rel[1].r_info
8479 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8480 && rel[1].r_offset == rel->r_offset + 8)
8482 if (ok_tprel)
8483 /* GD -> LE */
8484 tls_set = TLS_EXPLICIT | TLS_GD;
8485 else
8486 /* GD -> IE */
8487 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8488 tls_clear = TLS_GD;
8490 else
8492 if (!is_local)
8493 continue;
8495 /* LD -> LE */
8496 tls_set = TLS_EXPLICIT;
8497 tls_clear = TLS_LD;
8499 break;
8501 case R_PPC64_TPREL16_HA:
8502 if (pass == 0)
8504 unsigned char buf[4];
8505 unsigned int insn;
8506 bfd_vma off = rel->r_offset & ~3;
8507 if (!bfd_get_section_contents (ibfd, sec, buf,
8508 off, 4))
8509 goto err_free_rel;
8510 insn = bfd_get_32 (ibfd, buf);
8511 /* addis rt,13,imm */
8512 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8513 != ((15u << 26) | (13 << 16)))
8515 /* xgettext:c-format */
8516 info->callbacks->minfo
8517 (_("%H: warning: %s unexpected insn %#x.\n"),
8518 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8519 htab->do_tls_opt = 0;
8522 continue;
8524 case R_PPC64_TPREL16_HI:
8525 case R_PPC64_TPREL16_HIGH:
8526 case R_PPC64_TPREL16_HIGHA:
8527 case R_PPC64_TPREL16_HIGHER:
8528 case R_PPC64_TPREL16_HIGHERA:
8529 case R_PPC64_TPREL16_HIGHEST:
8530 case R_PPC64_TPREL16_HIGHESTA:
8531 /* These can all be used in sequences along with
8532 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8533 able to verify easily. */
8534 htab->do_tls_opt = 0;
8535 continue;
8537 default:
8538 continue;
8541 if (pass == 0)
8543 if (!expecting_tls_get_addr
8544 || !sec->nomark_tls_get_addr)
8545 continue;
8547 if (rel + 1 < relend
8548 && branch_reloc_hash_match (ibfd, rel + 1,
8549 htab->tls_get_addr_fd,
8550 htab->tga_desc_fd,
8551 htab->tls_get_addr,
8552 htab->tga_desc))
8554 if (expecting_tls_get_addr == 2)
8556 /* Check for toc tls entries. */
8557 unsigned char *toc_tls;
8558 int retval;
8560 retval = get_tls_mask (&toc_tls, NULL, NULL,
8561 &locsyms,
8562 rel, ibfd);
8563 if (retval == 0)
8564 goto err_free_rel;
8565 if (toc_tls != NULL)
8567 if ((*toc_tls & TLS_TLS) != 0
8568 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8569 found_tls_get_addr_arg = 1;
8570 if (retval > 1)
8571 toc_ref[toc_ref_index] = 1;
8574 continue;
8577 /* Uh oh, we didn't find the expected call. We
8578 could just mark this symbol to exclude it
8579 from tls optimization but it's safer to skip
8580 the entire optimization. */
8581 /* xgettext:c-format */
8582 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8583 "TLS optimization disabled\n"),
8584 ibfd, sec, rel->r_offset);
8585 ret = true;
8586 goto err_free_rel;
8589 /* If we don't have old-style __tls_get_addr calls
8590 without TLSGD/TLSLD marker relocs, and we haven't
8591 found a new-style __tls_get_addr call with a
8592 marker for this symbol, then we either have a
8593 broken object file or an -mlongcall style
8594 indirect call to __tls_get_addr without a marker.
8595 Disable optimization in this case. */
8596 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8597 && (tls_set & TLS_EXPLICIT) == 0
8598 && !sec->nomark_tls_get_addr
8599 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8600 != (TLS_TLS | TLS_MARK)))
8601 continue;
8603 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
8605 struct plt_entry *ent = NULL;
8607 if (htab->tls_get_addr_fd != NULL)
8608 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8609 ent != NULL;
8610 ent = ent->next)
8611 if (ent->addend == 0)
8612 break;
8614 if (ent == NULL && htab->tga_desc_fd != NULL)
8615 for (ent = htab->tga_desc_fd->elf.plt.plist;
8616 ent != NULL;
8617 ent = ent->next)
8618 if (ent->addend == 0)
8619 break;
8621 if (ent == NULL && htab->tls_get_addr != NULL)
8622 for (ent = htab->tls_get_addr->elf.plt.plist;
8623 ent != NULL;
8624 ent = ent->next)
8625 if (ent->addend == 0)
8626 break;
8628 if (ent == NULL && htab->tga_desc != NULL)
8629 for (ent = htab->tga_desc->elf.plt.plist;
8630 ent != NULL;
8631 ent = ent->next)
8632 if (ent->addend == 0)
8633 break;
8635 if (ent != NULL
8636 && ent->plt.refcount > 0)
8637 ent->plt.refcount -= 1;
8640 if (tls_clear == 0)
8641 continue;
8643 if ((tls_set & TLS_EXPLICIT) == 0)
8645 struct got_entry *ent;
8647 /* Adjust got entry for this reloc. */
8648 if (h != NULL)
8649 ent = h->got.glist;
8650 else
8651 ent = elf_local_got_ents (ibfd)[r_symndx];
8653 for (; ent != NULL; ent = ent->next)
8654 if (ent->addend == rel->r_addend
8655 && ent->owner == ibfd
8656 && ent->tls_type == tls_type)
8657 break;
8658 if (ent == NULL)
8659 abort ();
8661 if (tls_set == 0)
8663 /* We managed to get rid of a got entry. */
8664 if (ent->got.refcount > 0)
8665 ent->got.refcount -= 1;
8668 else
8670 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8671 we'll lose one or two dyn relocs. */
8672 if (!dec_dynrel_count (rel, sec, info,
8673 NULL, h, sym))
8674 return false;
8676 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8678 if (!dec_dynrel_count (rel + 1, sec, info,
8679 NULL, h, sym))
8680 return false;
8684 *tls_mask |= tls_set & 0xff;
8685 *tls_mask &= ~tls_clear;
8688 if (elf_section_data (sec)->relocs != relstart)
8689 free (relstart);
8692 if (locsyms != NULL
8693 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8695 if (!info->keep_memory)
8696 free (locsyms);
8697 else
8698 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8702 free (toc_ref);
8703 return true;
8706 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8707 the values of any global symbols in a toc section that has been
8708 edited. Globals in toc sections should be a rarity, so this function
8709 sets a flag if any are found in toc sections other than the one just
8710 edited, so that further hash table traversals can be avoided. */
8712 struct adjust_toc_info
8714 asection *toc;
8715 unsigned long *skip;
8716 bool global_toc_syms;
8719 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8721 static bool
8722 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8724 struct ppc_link_hash_entry *eh;
8725 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8726 unsigned long i;
8728 if (h->root.type != bfd_link_hash_defined
8729 && h->root.type != bfd_link_hash_defweak)
8730 return true;
8732 eh = ppc_elf_hash_entry (h);
8733 if (eh->adjust_done)
8734 return true;
8736 if (eh->elf.root.u.def.section == toc_inf->toc)
8738 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8739 i = toc_inf->toc->rawsize >> 3;
8740 else
8741 i = eh->elf.root.u.def.value >> 3;
8743 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8745 _bfd_error_handler
8746 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8748 ++i;
8749 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8750 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8753 eh->elf.root.u.def.value -= toc_inf->skip[i];
8754 eh->adjust_done = 1;
8756 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8757 toc_inf->global_toc_syms = true;
8759 return true;
8762 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8763 on a _LO variety toc/got reloc. */
8765 static bool
8766 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8768 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8769 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8770 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8771 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8772 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8773 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8774 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8775 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8776 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8777 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8778 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8779 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8780 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8781 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8782 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8783 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8784 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8785 /* Exclude lfqu by testing reloc. If relocs are ever
8786 defined for the reduced D field in psq_lu then those
8787 will need testing too. */
8788 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8789 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
8790 && (insn & 1) == 0)
8791 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8792 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8793 /* Exclude stfqu. psq_stu as above for psq_lu. */
8794 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8795 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
8796 && (insn & 1) == 0));
8799 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8800 pld ra,symbol@got@pcrel
8801 load/store rt,off(ra)
8803 pla ra,symbol@pcrel
8804 load/store rt,off(ra)
8805 may be translated to
8806 pload/pstore rt,symbol+off@pcrel
8807 nop.
8808 This function returns true if the optimization is possible, placing
8809 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8811 On entry to this function, the linker has already determined that
8812 the pld can be replaced with pla: *PINSN1 is that pla insn,
8813 while *PINSN2 is the second instruction. */
8815 static bool
8816 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8818 uint64_t insn1 = *pinsn1;
8819 uint64_t insn2 = *pinsn2;
8820 bfd_signed_vma off;
8822 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8824 /* Check that regs match. */
8825 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8826 return false;
8828 /* P8LS or PMLS form, non-pcrel. */
8829 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8830 return false;
8832 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8833 *pinsn2 = PNOP;
8834 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8835 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8836 return true;
8839 insn2 >>= 32;
8841 /* Check that regs match. */
8842 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8843 return false;
8845 switch ((insn2 >> 26) & 63)
8847 default:
8848 return false;
8850 case 32: /* lwz */
8851 case 34: /* lbz */
8852 case 36: /* stw */
8853 case 38: /* stb */
8854 case 40: /* lhz */
8855 case 42: /* lha */
8856 case 44: /* sth */
8857 case 48: /* lfs */
8858 case 50: /* lfd */
8859 case 52: /* stfs */
8860 case 54: /* stfd */
8861 /* These are the PMLS cases, where we just need to tack a prefix
8862 on the insn. */
8863 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8864 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8865 off = insn2 & 0xffff;
8866 break;
8868 case 58: /* lwa, ld */
8869 if ((insn2 & 1) != 0)
8870 return false;
8871 insn1 = ((1ULL << 58) | (1ULL << 52)
8872 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8873 | (insn2 & (31ULL << 21)));
8874 off = insn2 & 0xfffc;
8875 break;
8877 case 57: /* lxsd, lxssp */
8878 if ((insn2 & 3) < 2)
8879 return false;
8880 insn1 = ((1ULL << 58) | (1ULL << 52)
8881 | ((40ULL | (insn2 & 3)) << 26)
8882 | (insn2 & (31ULL << 21)));
8883 off = insn2 & 0xfffc;
8884 break;
8886 case 61: /* stxsd, stxssp, lxv, stxv */
8887 if ((insn2 & 3) == 0)
8888 return false;
8889 else if ((insn2 & 3) >= 2)
8891 insn1 = ((1ULL << 58) | (1ULL << 52)
8892 | ((44ULL | (insn2 & 3)) << 26)
8893 | (insn2 & (31ULL << 21)));
8894 off = insn2 & 0xfffc;
8896 else
8898 insn1 = ((1ULL << 58) | (1ULL << 52)
8899 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8900 | (insn2 & (31ULL << 21)));
8901 off = insn2 & 0xfff0;
8903 break;
8905 case 56: /* lq */
8906 insn1 = ((1ULL << 58) | (1ULL << 52)
8907 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8908 off = insn2 & 0xffff;
8909 break;
8911 case 6: /* lxvp, stxvp */
8912 if ((insn2 & 0xe) != 0)
8913 return false;
8914 insn1 = ((1ULL << 58) | (1ULL << 52)
8915 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8916 | (insn2 & (31ULL << 21)));
8917 off = insn2 & 0xfff0;
8918 break;
8920 case 62: /* std, stq */
8921 if ((insn2 & 1) != 0)
8922 return false;
8923 insn1 = ((1ULL << 58) | (1ULL << 52)
8924 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8925 | (insn2 & (31ULL << 21)));
8926 off = insn2 & 0xfffc;
8927 break;
8930 *pinsn1 = insn1;
8931 *pinsn2 = (uint64_t) NOP << 32;
8932 *poff = (off ^ 0x8000) - 0x8000;
8933 return true;
8936 /* Examine all relocs referencing .toc sections in order to remove
8937 unused .toc entries. */
8939 bool
8940 ppc64_elf_edit_toc (struct bfd_link_info *info)
8942 bfd *ibfd;
8943 struct adjust_toc_info toc_inf;
8944 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8946 htab->do_toc_opt = 1;
8947 toc_inf.global_toc_syms = true;
8948 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8950 asection *toc, *sec;
8951 Elf_Internal_Shdr *symtab_hdr;
8952 Elf_Internal_Sym *local_syms;
8953 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8954 unsigned long *skip, *drop;
8955 unsigned char *used;
8956 unsigned char *keep, last, some_unused;
8958 if (!is_ppc64_elf (ibfd))
8959 continue;
8961 toc = bfd_get_section_by_name (ibfd, ".toc");
8962 if (toc == NULL
8963 || toc->size == 0
8964 || (toc->flags & SEC_HAS_CONTENTS) == 0
8965 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8966 || discarded_section (toc))
8967 continue;
8969 toc_relocs = NULL;
8970 local_syms = NULL;
8971 symtab_hdr = &elf_symtab_hdr (ibfd);
8973 /* Look at sections dropped from the final link. */
8974 skip = NULL;
8975 relstart = NULL;
8976 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8978 if (sec->reloc_count == 0
8979 || !discarded_section (sec)
8980 || get_opd_info (sec)
8981 || (sec->flags & SEC_ALLOC) == 0
8982 || (sec->flags & SEC_DEBUGGING) != 0)
8983 continue;
8985 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
8986 if (relstart == NULL)
8987 goto error_ret;
8989 /* Run through the relocs to see which toc entries might be
8990 unused. */
8991 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8993 enum elf_ppc64_reloc_type r_type;
8994 unsigned long r_symndx;
8995 asection *sym_sec;
8996 struct elf_link_hash_entry *h;
8997 Elf_Internal_Sym *sym;
8998 bfd_vma val;
9000 r_type = ELF64_R_TYPE (rel->r_info);
9001 switch (r_type)
9003 default:
9004 continue;
9006 case R_PPC64_TOC16:
9007 case R_PPC64_TOC16_LO:
9008 case R_PPC64_TOC16_HI:
9009 case R_PPC64_TOC16_HA:
9010 case R_PPC64_TOC16_DS:
9011 case R_PPC64_TOC16_LO_DS:
9012 break;
9015 r_symndx = ELF64_R_SYM (rel->r_info);
9016 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9017 r_symndx, ibfd))
9018 goto error_ret;
9020 if (sym_sec != toc)
9021 continue;
9023 if (h != NULL)
9024 val = h->root.u.def.value;
9025 else
9026 val = sym->st_value;
9027 val += rel->r_addend;
9029 if (val >= toc->size)
9030 continue;
9032 /* Anything in the toc ought to be aligned to 8 bytes.
9033 If not, don't mark as unused. */
9034 if (val & 7)
9035 continue;
9037 if (skip == NULL)
9039 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9040 if (skip == NULL)
9041 goto error_ret;
9044 skip[val >> 3] = ref_from_discarded;
9047 if (elf_section_data (sec)->relocs != relstart)
9048 free (relstart);
9051 /* For largetoc loads of address constants, we can convert
9052 . addis rx,2,addr@got@ha
9053 . ld ry,addr@got@l(rx)
9055 . addis rx,2,addr@toc@ha
9056 . addi ry,rx,addr@toc@l
9057 when addr is within 2G of the toc pointer. This then means
9058 that the word storing "addr" in the toc is no longer needed. */
9060 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9061 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9062 && toc->reloc_count != 0)
9064 /* Read toc relocs. */
9065 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9066 info->keep_memory);
9067 if (toc_relocs == NULL)
9068 goto error_ret;
9070 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9072 enum elf_ppc64_reloc_type r_type;
9073 unsigned long r_symndx;
9074 asection *sym_sec;
9075 struct elf_link_hash_entry *h;
9076 Elf_Internal_Sym *sym;
9077 bfd_vma val, addr;
9079 r_type = ELF64_R_TYPE (rel->r_info);
9080 if (r_type != R_PPC64_ADDR64)
9081 continue;
9083 r_symndx = ELF64_R_SYM (rel->r_info);
9084 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9085 r_symndx, ibfd))
9086 goto error_ret;
9088 if (sym_sec == NULL
9089 || sym_sec->output_section == NULL
9090 || discarded_section (sym_sec))
9091 continue;
9093 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9094 || (bfd_link_pic (info)
9095 && sym_sec == bfd_abs_section_ptr))
9096 continue;
9098 if (h != NULL)
9100 if (h->type == STT_GNU_IFUNC)
9101 continue;
9102 val = h->root.u.def.value;
9104 else
9106 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9107 continue;
9108 val = sym->st_value;
9110 val += rel->r_addend;
9111 val += sym_sec->output_section->vma + sym_sec->output_offset;
9113 /* We don't yet know the exact toc pointer value, but we
9114 know it will be somewhere in the toc section. Don't
9115 optimize if the difference from any possible toc
9116 pointer is outside [ff..f80008000, 7fff7fff]. */
9117 addr = toc->output_section->vma + TOC_BASE_OFF;
9118 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9119 continue;
9121 addr = toc->output_section->vma + toc->output_section->rawsize;
9122 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9123 continue;
9125 if (skip == NULL)
9127 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9128 if (skip == NULL)
9129 goto error_ret;
9132 skip[rel->r_offset >> 3]
9133 |= can_optimize | ((rel - toc_relocs) << 2);
9137 if (skip == NULL)
9138 continue;
9140 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9141 if (used == NULL)
9143 error_ret:
9144 if (symtab_hdr->contents != (unsigned char *) local_syms)
9145 free (local_syms);
9146 if (sec != NULL
9147 && elf_section_data (sec)->relocs != relstart)
9148 free (relstart);
9149 if (elf_section_data (toc)->relocs != toc_relocs)
9150 free (toc_relocs);
9151 free (skip);
9152 return false;
9155 /* Now check all kept sections that might reference the toc.
9156 Check the toc itself last. */
9157 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9158 : ibfd->sections);
9159 sec != NULL;
9160 sec = (sec == toc ? NULL
9161 : sec->next == NULL ? toc
9162 : sec->next == toc && toc->next ? toc->next
9163 : sec->next))
9165 int repeat;
9167 if (sec->reloc_count == 0
9168 || discarded_section (sec)
9169 || get_opd_info (sec)
9170 || (sec->flags & SEC_ALLOC) == 0
9171 || (sec->flags & SEC_DEBUGGING) != 0)
9172 continue;
9174 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9175 info->keep_memory);
9176 if (relstart == NULL)
9178 free (used);
9179 goto error_ret;
9182 /* Mark toc entries referenced as used. */
9185 repeat = 0;
9186 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9188 enum elf_ppc64_reloc_type r_type;
9189 unsigned long r_symndx;
9190 asection *sym_sec;
9191 struct elf_link_hash_entry *h;
9192 Elf_Internal_Sym *sym;
9193 bfd_vma val;
9195 r_type = ELF64_R_TYPE (rel->r_info);
9196 switch (r_type)
9198 case R_PPC64_TOC16:
9199 case R_PPC64_TOC16_LO:
9200 case R_PPC64_TOC16_HI:
9201 case R_PPC64_TOC16_HA:
9202 case R_PPC64_TOC16_DS:
9203 case R_PPC64_TOC16_LO_DS:
9204 /* In case we're taking addresses of toc entries. */
9205 case R_PPC64_ADDR64:
9206 break;
9208 default:
9209 continue;
9212 r_symndx = ELF64_R_SYM (rel->r_info);
9213 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9214 r_symndx, ibfd))
9216 free (used);
9217 goto error_ret;
9220 if (sym_sec != toc)
9221 continue;
9223 if (h != NULL)
9224 val = h->root.u.def.value;
9225 else
9226 val = sym->st_value;
9227 val += rel->r_addend;
9229 if (val >= toc->size)
9230 continue;
9232 if ((skip[val >> 3] & can_optimize) != 0)
9234 bfd_vma off;
9235 unsigned char opc;
9237 switch (r_type)
9239 case R_PPC64_TOC16_HA:
9240 break;
9242 case R_PPC64_TOC16_LO_DS:
9243 off = rel->r_offset;
9244 off += (bfd_big_endian (ibfd) ? -2 : 3);
9245 if (!bfd_get_section_contents (ibfd, sec, &opc,
9246 off, 1))
9248 free (used);
9249 goto error_ret;
9251 if ((opc & (0x3f << 2)) == (58u << 2))
9252 break;
9253 /* Fall through. */
9255 default:
9256 /* Wrong sort of reloc, or not a ld. We may
9257 as well clear ref_from_discarded too. */
9258 skip[val >> 3] = 0;
9262 if (sec != toc)
9263 used[val >> 3] = 1;
9264 /* For the toc section, we only mark as used if this
9265 entry itself isn't unused. */
9266 else if ((used[rel->r_offset >> 3]
9267 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9268 && !used[val >> 3])
9270 /* Do all the relocs again, to catch reference
9271 chains. */
9272 repeat = 1;
9273 used[val >> 3] = 1;
9277 while (repeat);
9279 if (elf_section_data (sec)->relocs != relstart)
9280 free (relstart);
9283 /* Merge the used and skip arrays. Assume that TOC
9284 doublewords not appearing as either used or unused belong
9285 to an entry more than one doubleword in size. */
9286 for (drop = skip, keep = used, last = 0, some_unused = 0;
9287 drop < skip + (toc->size + 7) / 8;
9288 ++drop, ++keep)
9290 if (*keep)
9292 *drop &= ~ref_from_discarded;
9293 if ((*drop & can_optimize) != 0)
9294 some_unused = 1;
9295 last = 0;
9297 else if ((*drop & ref_from_discarded) != 0)
9299 some_unused = 1;
9300 last = ref_from_discarded;
9302 else
9303 *drop = last;
9306 free (used);
9308 if (some_unused)
9310 bfd_byte *contents, *src;
9311 unsigned long off;
9312 Elf_Internal_Sym *sym;
9313 bool local_toc_syms = false;
9315 /* Shuffle the toc contents, and at the same time convert the
9316 skip array from booleans into offsets. */
9317 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9318 goto error_ret;
9320 elf_section_data (toc)->this_hdr.contents = contents;
9322 for (src = contents, off = 0, drop = skip;
9323 src < contents + toc->size;
9324 src += 8, ++drop)
9326 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9327 off += 8;
9328 else if (off != 0)
9330 *drop = off;
9331 memcpy (src - off, src, 8);
9334 *drop = off;
9335 toc->rawsize = toc->size;
9336 toc->size = src - contents - off;
9338 /* Adjust addends for relocs against the toc section sym,
9339 and optimize any accesses we can. */
9340 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9342 if (sec->reloc_count == 0
9343 || discarded_section (sec))
9344 continue;
9346 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9347 info->keep_memory);
9348 if (relstart == NULL)
9349 goto error_ret;
9351 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9353 enum elf_ppc64_reloc_type r_type;
9354 unsigned long r_symndx;
9355 asection *sym_sec;
9356 struct elf_link_hash_entry *h;
9357 bfd_vma val;
9359 r_type = ELF64_R_TYPE (rel->r_info);
9360 switch (r_type)
9362 default:
9363 continue;
9365 case R_PPC64_TOC16:
9366 case R_PPC64_TOC16_LO:
9367 case R_PPC64_TOC16_HI:
9368 case R_PPC64_TOC16_HA:
9369 case R_PPC64_TOC16_DS:
9370 case R_PPC64_TOC16_LO_DS:
9371 case R_PPC64_ADDR64:
9372 break;
9375 r_symndx = ELF64_R_SYM (rel->r_info);
9376 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9377 r_symndx, ibfd))
9378 goto error_ret;
9380 if (sym_sec != toc)
9381 continue;
9383 if (h != NULL)
9384 val = h->root.u.def.value;
9385 else
9387 val = sym->st_value;
9388 if (val != 0)
9389 local_toc_syms = true;
9392 val += rel->r_addend;
9394 if (val > toc->rawsize)
9395 val = toc->rawsize;
9396 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9397 continue;
9398 else if ((skip[val >> 3] & can_optimize) != 0)
9400 Elf_Internal_Rela *tocrel
9401 = toc_relocs + (skip[val >> 3] >> 2);
9402 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9404 switch (r_type)
9406 case R_PPC64_TOC16_HA:
9407 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9408 break;
9410 case R_PPC64_TOC16_LO_DS:
9411 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9412 break;
9414 default:
9415 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9416 ppc_howto_init ();
9417 info->callbacks->einfo
9418 /* xgettext:c-format */
9419 (_("%H: %s references "
9420 "optimized away TOC entry\n"),
9421 ibfd, sec, rel->r_offset,
9422 ppc64_elf_howto_table[r_type]->name);
9423 bfd_set_error (bfd_error_bad_value);
9424 goto error_ret;
9426 rel->r_addend = tocrel->r_addend;
9427 elf_section_data (sec)->relocs = relstart;
9428 continue;
9431 if (h != NULL || sym->st_value != 0)
9432 continue;
9434 rel->r_addend -= skip[val >> 3];
9435 elf_section_data (sec)->relocs = relstart;
9438 if (elf_section_data (sec)->relocs != relstart)
9439 free (relstart);
9442 /* We shouldn't have local or global symbols defined in the TOC,
9443 but handle them anyway. */
9444 if (local_syms != NULL)
9445 for (sym = local_syms;
9446 sym < local_syms + symtab_hdr->sh_info;
9447 ++sym)
9448 if (sym->st_value != 0
9449 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9451 unsigned long i;
9453 if (sym->st_value > toc->rawsize)
9454 i = toc->rawsize >> 3;
9455 else
9456 i = sym->st_value >> 3;
9458 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9460 if (local_toc_syms)
9461 _bfd_error_handler
9462 (_("%s defined on removed toc entry"),
9463 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9465 ++i;
9466 while ((skip[i] & (ref_from_discarded | can_optimize)));
9467 sym->st_value = (bfd_vma) i << 3;
9470 sym->st_value -= skip[i];
9471 symtab_hdr->contents = (unsigned char *) local_syms;
9474 /* Adjust any global syms defined in this toc input section. */
9475 if (toc_inf.global_toc_syms)
9477 toc_inf.toc = toc;
9478 toc_inf.skip = skip;
9479 toc_inf.global_toc_syms = false;
9480 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9481 &toc_inf);
9484 if (toc->reloc_count != 0)
9486 Elf_Internal_Shdr *rel_hdr;
9487 Elf_Internal_Rela *wrel;
9488 bfd_size_type sz;
9490 /* Remove unused toc relocs, and adjust those we keep. */
9491 if (toc_relocs == NULL)
9492 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9493 info->keep_memory);
9494 if (toc_relocs == NULL)
9495 goto error_ret;
9497 wrel = toc_relocs;
9498 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9499 if ((skip[rel->r_offset >> 3]
9500 & (ref_from_discarded | can_optimize)) == 0)
9502 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9503 wrel->r_info = rel->r_info;
9504 wrel->r_addend = rel->r_addend;
9505 ++wrel;
9507 else if (!dec_dynrel_count (rel, toc, info,
9508 &local_syms, NULL, NULL))
9509 goto error_ret;
9511 elf_section_data (toc)->relocs = toc_relocs;
9512 toc->reloc_count = wrel - toc_relocs;
9513 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9514 sz = rel_hdr->sh_entsize;
9515 rel_hdr->sh_size = toc->reloc_count * sz;
9518 else if (elf_section_data (toc)->relocs != toc_relocs)
9519 free (toc_relocs);
9521 if (local_syms != NULL
9522 && symtab_hdr->contents != (unsigned char *) local_syms)
9524 if (!info->keep_memory)
9525 free (local_syms);
9526 else
9527 symtab_hdr->contents = (unsigned char *) local_syms;
9529 free (skip);
9532 /* Look for cases where we can change an indirect GOT access to
9533 a GOT relative or PC relative access, possibly reducing the
9534 number of GOT entries. */
9535 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9537 asection *sec;
9538 Elf_Internal_Shdr *symtab_hdr;
9539 Elf_Internal_Sym *local_syms;
9540 Elf_Internal_Rela *relstart, *rel;
9541 bfd_vma got;
9543 if (!is_ppc64_elf (ibfd))
9544 continue;
9546 if (!ppc64_elf_tdata (ibfd)->has_optrel)
9547 continue;
9549 sec = ppc64_elf_tdata (ibfd)->got;
9550 got = 0;
9551 if (sec != NULL)
9552 got = sec->output_section->vma + sec->output_offset + 0x8000;
9554 local_syms = NULL;
9555 symtab_hdr = &elf_symtab_hdr (ibfd);
9557 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9559 if (sec->reloc_count == 0
9560 || !ppc64_elf_section_data (sec)->has_optrel
9561 || discarded_section (sec))
9562 continue;
9564 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9565 info->keep_memory);
9566 if (relstart == NULL)
9568 got_error_ret:
9569 if (symtab_hdr->contents != (unsigned char *) local_syms)
9570 free (local_syms);
9571 if (sec != NULL
9572 && elf_section_data (sec)->relocs != relstart)
9573 free (relstart);
9574 return false;
9577 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9579 enum elf_ppc64_reloc_type r_type;
9580 unsigned long r_symndx;
9581 Elf_Internal_Sym *sym;
9582 asection *sym_sec;
9583 struct elf_link_hash_entry *h;
9584 struct got_entry *ent;
9585 bfd_vma val, pc;
9586 unsigned char buf[8];
9587 unsigned int insn;
9588 enum {no_check, check_lo, check_ha} insn_check;
9590 r_type = ELF64_R_TYPE (rel->r_info);
9591 switch (r_type)
9593 default:
9594 insn_check = no_check;
9595 break;
9597 case R_PPC64_PLT16_HA:
9598 case R_PPC64_GOT_TLSLD16_HA:
9599 case R_PPC64_GOT_TLSGD16_HA:
9600 case R_PPC64_GOT_TPREL16_HA:
9601 case R_PPC64_GOT_DTPREL16_HA:
9602 case R_PPC64_GOT16_HA:
9603 case R_PPC64_TOC16_HA:
9604 insn_check = check_ha;
9605 break;
9607 case R_PPC64_PLT16_LO:
9608 case R_PPC64_PLT16_LO_DS:
9609 case R_PPC64_GOT_TLSLD16_LO:
9610 case R_PPC64_GOT_TLSGD16_LO:
9611 case R_PPC64_GOT_TPREL16_LO_DS:
9612 case R_PPC64_GOT_DTPREL16_LO_DS:
9613 case R_PPC64_GOT16_LO:
9614 case R_PPC64_GOT16_LO_DS:
9615 case R_PPC64_TOC16_LO:
9616 case R_PPC64_TOC16_LO_DS:
9617 insn_check = check_lo;
9618 break;
9621 if (insn_check != no_check)
9623 bfd_vma off = rel->r_offset & ~3;
9625 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9626 goto got_error_ret;
9628 insn = bfd_get_32 (ibfd, buf);
9629 if (insn_check == check_lo
9630 ? !ok_lo_toc_insn (insn, r_type)
9631 : ((insn & ((0x3fu << 26) | 0x1f << 16))
9632 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9634 char str[12];
9636 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9637 sprintf (str, "%#08x", insn);
9638 info->callbacks->einfo
9639 /* xgettext:c-format */
9640 (_("%H: got/toc optimization is not supported for"
9641 " %s instruction\n"),
9642 ibfd, sec, rel->r_offset & ~3, str);
9643 continue;
9647 switch (r_type)
9649 /* Note that we don't delete GOT entries for
9650 R_PPC64_GOT16_DS since we'd need a lot more
9651 analysis. For starters, the preliminary layout is
9652 before the GOT, PLT, dynamic sections and stubs are
9653 laid out. Then we'd need to allow for changes in
9654 distance between sections caused by alignment. */
9655 default:
9656 continue;
9658 case R_PPC64_GOT16_HA:
9659 case R_PPC64_GOT16_LO_DS:
9660 case R_PPC64_GOT_PCREL34:
9661 break;
9664 r_symndx = ELF64_R_SYM (rel->r_info);
9665 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9666 r_symndx, ibfd))
9667 goto got_error_ret;
9669 if (sym_sec == NULL
9670 || sym_sec->output_section == NULL
9671 || discarded_section (sym_sec))
9672 continue;
9674 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9675 continue;
9677 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9678 || (bfd_link_pic (info)
9679 && sym_sec == bfd_abs_section_ptr))
9680 continue;
9682 if (h != NULL)
9683 val = h->root.u.def.value;
9684 else
9685 val = sym->st_value;
9686 val += rel->r_addend;
9687 val += sym_sec->output_section->vma + sym_sec->output_offset;
9689 /* Fudge factor to allow for the fact that the preliminary layout
9690 isn't exact. Reduce limits by this factor. */
9691 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9693 switch (r_type)
9695 default:
9696 continue;
9698 case R_PPC64_GOT16_HA:
9699 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9700 >= LIMIT_ADJUST (0x100000000ULL))
9701 continue;
9703 if (!bfd_get_section_contents (ibfd, sec, buf,
9704 rel->r_offset & ~3, 4))
9705 goto got_error_ret;
9706 insn = bfd_get_32 (ibfd, buf);
9707 if (((insn & ((0x3fu << 26) | 0x1f << 16))
9708 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9709 continue;
9710 break;
9712 case R_PPC64_GOT16_LO_DS:
9713 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9714 >= LIMIT_ADJUST (0x100000000ULL))
9715 continue;
9716 if (!bfd_get_section_contents (ibfd, sec, buf,
9717 rel->r_offset & ~3, 4))
9718 goto got_error_ret;
9719 insn = bfd_get_32 (ibfd, buf);
9720 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
9721 continue;
9722 break;
9724 case R_PPC64_GOT_PCREL34:
9725 pc = rel->r_offset;
9726 pc += sec->output_section->vma + sec->output_offset;
9727 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9728 >= LIMIT_ADJUST (1ULL << 34))
9729 continue;
9730 if (!bfd_get_section_contents (ibfd, sec, buf,
9731 rel->r_offset & ~3, 8))
9732 goto got_error_ret;
9733 insn = bfd_get_32 (ibfd, buf);
9734 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9735 continue;
9736 insn = bfd_get_32 (ibfd, buf + 4);
9737 if ((insn & (0x3fu << 26)) != 57u << 26)
9738 continue;
9739 break;
9741 #undef LIMIT_ADJUST
9743 if (h != NULL)
9744 ent = h->got.glist;
9745 else
9747 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9748 ent = local_got_ents[r_symndx];
9750 for (; ent != NULL; ent = ent->next)
9751 if (ent->addend == rel->r_addend
9752 && ent->owner == ibfd
9753 && ent->tls_type == 0)
9754 break;
9755 BFD_ASSERT (ent && ent->got.refcount > 0);
9756 ent->got.refcount -= 1;
9759 if (elf_section_data (sec)->relocs != relstart)
9760 free (relstart);
9763 if (local_syms != NULL
9764 && symtab_hdr->contents != (unsigned char *) local_syms)
9766 if (!info->keep_memory)
9767 free (local_syms);
9768 else
9769 symtab_hdr->contents = (unsigned char *) local_syms;
9773 return true;
9776 /* Return true iff input section I references the TOC using
9777 instructions limited to +/-32k offsets. */
9779 bool
9780 ppc64_elf_has_small_toc_reloc (asection *i)
9782 return (is_ppc64_elf (i->owner)
9783 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9786 /* Allocate space for one GOT entry. */
9788 static void
9789 allocate_got (struct elf_link_hash_entry *h,
9790 struct bfd_link_info *info,
9791 struct got_entry *gent)
9793 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9794 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
9795 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9796 ? 16 : 8);
9797 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9798 ? 2 : 1) * sizeof (Elf64_External_Rela);
9799 asection *got = ppc64_elf_tdata (gent->owner)->got;
9801 gent->got.offset = got->size;
9802 got->size += entsize;
9804 if (h->type == STT_GNU_IFUNC)
9806 htab->elf.irelplt->size += rentsize;
9807 htab->got_reli_size += rentsize;
9809 else if (((bfd_link_pic (info)
9810 && (gent->tls_type == 0
9811 ? !info->enable_dt_relr
9812 : !(bfd_link_executable (info)
9813 && SYMBOL_REFERENCES_LOCAL (info, h)))
9814 && !bfd_is_abs_symbol (&h->root))
9815 || (htab->elf.dynamic_sections_created
9816 && h->dynindx != -1
9817 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9818 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9820 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9821 relgot->size += rentsize;
9825 /* This function merges got entries in the same toc group. */
9827 static void
9828 merge_got_entries (struct got_entry **pent)
9830 struct got_entry *ent, *ent2;
9832 for (ent = *pent; ent != NULL; ent = ent->next)
9833 if (!ent->is_indirect)
9834 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9835 if (!ent2->is_indirect
9836 && ent2->addend == ent->addend
9837 && ent2->tls_type == ent->tls_type
9838 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9840 ent2->is_indirect = true;
9841 ent2->got.ent = ent;
9845 /* If H is undefined, make it dynamic if that makes sense. */
9847 static bool
9848 ensure_undef_dynamic (struct bfd_link_info *info,
9849 struct elf_link_hash_entry *h)
9851 struct elf_link_hash_table *htab = elf_hash_table (info);
9853 if (htab->dynamic_sections_created
9854 && ((info->dynamic_undefined_weak != 0
9855 && h->root.type == bfd_link_hash_undefweak)
9856 || h->root.type == bfd_link_hash_undefined)
9857 && h->dynindx == -1
9858 && !h->forced_local
9859 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9860 return bfd_elf_link_record_dynamic_symbol (info, h);
9861 return true;
9864 /* Choose whether to use htab->iplt or htab->pltlocal rather than the
9865 usual htab->elf.splt section for a PLT entry. */
9867 static inline
9868 bool use_local_plt (struct bfd_link_info *info,
9869 struct elf_link_hash_entry *h)
9871 return (h == NULL
9872 || h->dynindx == -1
9873 || !elf_hash_table (info)->dynamic_sections_created);
9876 /* Allocate space in .plt, .got and associated reloc sections for
9877 dynamic relocs. */
9879 static bool
9880 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9882 struct bfd_link_info *info;
9883 struct ppc_link_hash_table *htab;
9884 asection *s;
9885 struct ppc_link_hash_entry *eh;
9886 struct got_entry **pgent, *gent;
9888 if (h->root.type == bfd_link_hash_indirect)
9889 return true;
9891 info = (struct bfd_link_info *) inf;
9892 htab = ppc_hash_table (info);
9893 if (htab == NULL)
9894 return false;
9896 eh = ppc_elf_hash_entry (h);
9897 /* Run through the TLS GD got entries first if we're changing them
9898 to TPREL. */
9899 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9900 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9901 if (gent->got.refcount > 0
9902 && (gent->tls_type & TLS_GD) != 0)
9904 /* This was a GD entry that has been converted to TPREL. If
9905 there happens to be a TPREL entry we can use that one. */
9906 struct got_entry *ent;
9907 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9908 if (ent->got.refcount > 0
9909 && (ent->tls_type & TLS_TPREL) != 0
9910 && ent->addend == gent->addend
9911 && ent->owner == gent->owner)
9913 gent->got.refcount = 0;
9914 break;
9917 /* If not, then we'll be using our own TPREL entry. */
9918 if (gent->got.refcount != 0)
9919 gent->tls_type = TLS_TLS | TLS_TPREL;
9922 /* Remove any list entry that won't generate a word in the GOT before
9923 we call merge_got_entries. Otherwise we risk merging to empty
9924 entries. */
9925 pgent = &h->got.glist;
9926 while ((gent = *pgent) != NULL)
9927 if (gent->got.refcount > 0)
9929 if ((gent->tls_type & TLS_LD) != 0
9930 && SYMBOL_REFERENCES_LOCAL (info, h))
9932 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9933 *pgent = gent->next;
9935 else
9936 pgent = &gent->next;
9938 else
9939 *pgent = gent->next;
9941 if (!htab->do_multi_toc)
9942 merge_got_entries (&h->got.glist);
9944 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9945 if (!gent->is_indirect)
9947 /* Ensure we catch all the cases where this symbol should
9948 be made dynamic. */
9949 if (!ensure_undef_dynamic (info, h))
9950 return false;
9952 if (!is_ppc64_elf (gent->owner))
9953 abort ();
9955 allocate_got (h, info, gent);
9958 /* If no dynamic sections we can't have dynamic relocs, except for
9959 IFUNCs which are handled even in static executables. */
9960 if (!htab->elf.dynamic_sections_created
9961 && h->type != STT_GNU_IFUNC)
9962 h->dyn_relocs = NULL;
9964 /* Discard relocs on undefined symbols that must be local. */
9965 else if (h->root.type == bfd_link_hash_undefined
9966 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9967 h->dyn_relocs = NULL;
9969 /* Also discard relocs on undefined weak syms with non-default
9970 visibility, or when dynamic_undefined_weak says so. */
9971 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9972 h->dyn_relocs = NULL;
9974 if (h->dyn_relocs != NULL)
9976 struct ppc_dyn_relocs *p, **pp;
9978 /* In the shared -Bsymbolic case, discard space allocated for
9979 dynamic pc-relative relocs against symbols which turn out to
9980 be defined in regular objects. For the normal shared case,
9981 discard space for relocs that have become local due to symbol
9982 visibility changes. */
9983 if (bfd_link_pic (info))
9985 /* Relocs that use pc_count are those that appear on a call
9986 insn, or certain REL relocs (see must_be_dyn_reloc) that
9987 can be generated via assembly. We want calls to
9988 protected symbols to resolve directly to the function
9989 rather than going via the plt. If people want function
9990 pointer comparisons to work as expected then they should
9991 avoid writing weird assembly. */
9992 if (SYMBOL_CALLS_LOCAL (info, h))
9994 for (pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
9995 (p = *pp) != NULL;
9998 p->count -= p->pc_count;
9999 p->pc_count = 0;
10000 if (p->count == 0)
10001 *pp = p->next;
10002 else
10003 pp = &p->next;
10007 if (h->dyn_relocs != NULL)
10009 /* Ensure we catch all the cases where this symbol
10010 should be made dynamic. */
10011 if (!ensure_undef_dynamic (info, h))
10012 return false;
10016 /* For a fixed position executable, discard space for
10017 relocs against symbols which are not dynamic. */
10018 else if (h->type != STT_GNU_IFUNC)
10020 if ((h->dynamic_adjusted
10021 || (h->ref_regular
10022 && h->root.type == bfd_link_hash_undefweak
10023 && (info->dynamic_undefined_weak > 0
10024 || !_bfd_elf_readonly_dynrelocs (h))))
10025 && !h->def_regular
10026 && !ELF_COMMON_DEF_P (h))
10028 /* Ensure we catch all the cases where this symbol
10029 should be made dynamic. */
10030 if (!ensure_undef_dynamic (info, h))
10031 return false;
10033 /* But if that didn't work out, discard dynamic relocs. */
10034 if (h->dynindx == -1)
10035 h->dyn_relocs = NULL;
10037 else
10038 h->dyn_relocs = NULL;
10041 /* Finally, allocate space. */
10042 for (p = (struct ppc_dyn_relocs *) h->dyn_relocs; p != NULL; p = p->next)
10043 if (!discarded_section (p->sec))
10045 unsigned int count;
10046 asection *sreloc = elf_section_data (p->sec)->sreloc;
10047 if (eh->elf.type == STT_GNU_IFUNC)
10048 sreloc = htab->elf.irelplt;
10049 count = p->count;
10050 if (info->enable_dt_relr
10051 && ((!NO_OPD_RELOCS
10052 && ppc64_elf_section_data (p->sec)->sec_type == sec_opd)
10053 || (eh->elf.type != STT_GNU_IFUNC
10054 && SYMBOL_REFERENCES_LOCAL (info, h))))
10055 count -= p->rel_count;
10056 sreloc->size += count * sizeof (Elf64_External_Rela);
10060 /* We might need a PLT entry when the symbol
10061 a) is dynamic, or
10062 b) is an ifunc, or
10063 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10064 d) has plt16 relocs and we are linking statically. */
10065 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10066 || h->type == STT_GNU_IFUNC
10067 || (h->needs_plt && h->dynamic_adjusted)
10068 || (h->needs_plt
10069 && h->def_regular
10070 && !htab->elf.dynamic_sections_created
10071 && !htab->can_convert_all_inline_plt
10072 && (ppc_elf_hash_entry (h)->tls_mask
10073 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10075 struct plt_entry *pent;
10076 bool doneone = false;
10077 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10078 if (pent->plt.refcount > 0)
10080 if (!ensure_undef_dynamic (info, h))
10081 return false;
10083 if (use_local_plt (info, h))
10085 if (h->type == STT_GNU_IFUNC)
10087 s = htab->elf.iplt;
10088 pent->plt.offset = s->size;
10089 s->size += PLT_ENTRY_SIZE (htab);
10090 s = htab->elf.irelplt;
10092 else
10094 s = htab->pltlocal;
10095 pent->plt.offset = s->size;
10096 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10097 s = NULL;
10098 if (bfd_link_pic (info)
10099 && !(info->enable_dt_relr && !htab->opd_abi))
10100 s = htab->relpltlocal;
10103 else
10105 /* If this is the first .plt entry, make room for the special
10106 first entry. */
10107 s = htab->elf.splt;
10108 if (s->size == 0)
10109 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10111 pent->plt.offset = s->size;
10113 /* Make room for this entry. */
10114 s->size += PLT_ENTRY_SIZE (htab);
10116 /* Make room for the .glink code. */
10117 s = htab->glink;
10118 if (s->size == 0)
10119 s->size += GLINK_PLTRESOLVE_SIZE (htab);
10120 if (htab->opd_abi)
10122 /* We need bigger stubs past index 32767. */
10123 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10124 s->size += 4;
10125 s->size += 2*4;
10127 else
10128 s->size += 4;
10130 /* We also need to make an entry in the .rela.plt section. */
10131 s = htab->elf.srelplt;
10133 if (s != NULL)
10134 s->size += sizeof (Elf64_External_Rela);
10135 doneone = true;
10137 else
10138 pent->plt.offset = (bfd_vma) -1;
10139 if (!doneone)
10141 h->plt.plist = NULL;
10142 h->needs_plt = 0;
10145 else
10147 h->plt.plist = NULL;
10148 h->needs_plt = 0;
10151 return true;
10154 #define PPC_LO(v) ((v) & 0xffff)
10155 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10156 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10157 #define D34(v) \
10158 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
10159 #define HA34(v) ((v + (1ULL << 33)) >> 34)
10161 /* Called via elf_link_hash_traverse from ppc64_elf_late_size_sections
10162 to set up space for global entry stubs. These are put in glink,
10163 after the branch table. */
10165 static bool
10166 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10168 struct bfd_link_info *info;
10169 struct ppc_link_hash_table *htab;
10170 struct plt_entry *pent;
10171 asection *s, *plt;
10173 if (h->root.type == bfd_link_hash_indirect)
10174 return true;
10176 if (!h->pointer_equality_needed)
10177 return true;
10179 if (h->def_regular)
10180 return true;
10182 info = inf;
10183 htab = ppc_hash_table (info);
10184 if (htab == NULL)
10185 return false;
10187 s = htab->global_entry;
10188 plt = htab->elf.splt;
10189 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10190 if (pent->plt.offset != (bfd_vma) -1
10191 && pent->addend == 0)
10193 /* For ELFv2, if this symbol is not defined in a regular file
10194 and we are not generating a shared library or pie, then we
10195 need to define the symbol in the executable on a call stub.
10196 This is to avoid text relocations. */
10197 bfd_vma off, stub_align, stub_off, stub_size;
10198 unsigned int align_power;
10200 stub_size = 16;
10201 stub_off = s->size;
10202 if (htab->params->plt_stub_align >= 0)
10203 align_power = htab->params->plt_stub_align;
10204 else
10205 align_power = -htab->params->plt_stub_align;
10206 /* Setting section alignment is delayed until we know it is
10207 non-empty. Otherwise the .text output section will be
10208 aligned at least to plt_stub_align even when no global
10209 entry stubs are needed. */
10210 if (s->alignment_power < align_power)
10211 s->alignment_power = align_power;
10212 stub_align = (bfd_vma) 1 << align_power;
10213 if (htab->params->plt_stub_align >= 0
10214 || ((((stub_off + stub_size - 1) & -stub_align)
10215 - (stub_off & -stub_align))
10216 > ((stub_size - 1) & -stub_align)))
10217 stub_off = (stub_off + stub_align - 1) & -stub_align;
10218 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10219 off -= stub_off + s->output_offset + s->output_section->vma;
10220 /* Note that for --plt-stub-align negative we have a possible
10221 dependency between stub offset and size. Break that
10222 dependency by assuming the max stub size when calculating
10223 the stub offset. */
10224 if (PPC_HA (off) == 0)
10225 stub_size -= 4;
10226 h->root.type = bfd_link_hash_defined;
10227 h->root.u.def.section = s;
10228 h->root.u.def.value = stub_off;
10229 s->size = stub_off + stub_size;
10230 break;
10232 return true;
10235 /* Set the sizes of the dynamic sections. */
10237 static bool
10238 ppc64_elf_late_size_sections (bfd *output_bfd,
10239 struct bfd_link_info *info)
10241 struct ppc_link_hash_table *htab;
10242 bfd *dynobj;
10243 asection *s;
10244 bool relocs;
10245 bfd *ibfd;
10246 struct got_entry *first_tlsld;
10248 htab = ppc_hash_table (info);
10249 if (htab == NULL)
10250 return false;
10252 dynobj = htab->elf.dynobj;
10253 if (dynobj == NULL)
10254 return true;
10256 if (htab->elf.dynamic_sections_created)
10258 /* Set the contents of the .interp section to the interpreter. */
10259 if (bfd_link_executable (info) && !info->nointerp)
10261 s = bfd_get_linker_section (dynobj, ".interp");
10262 if (s == NULL)
10263 abort ();
10264 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10265 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10266 s->alloced = 1;
10270 /* Set up .got offsets for local syms, and space for local dynamic
10271 relocs. */
10272 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10274 struct got_entry **lgot_ents;
10275 struct got_entry **end_lgot_ents;
10276 struct plt_entry **local_plt;
10277 struct plt_entry **end_local_plt;
10278 unsigned char *lgot_masks;
10279 bfd_size_type locsymcount;
10280 Elf_Internal_Shdr *symtab_hdr;
10281 Elf_Internal_Sym *local_syms;
10282 Elf_Internal_Sym *isym;
10284 if (!is_ppc64_elf (ibfd))
10285 continue;
10287 for (s = ibfd->sections; s != NULL; s = s->next)
10289 struct ppc_local_dyn_relocs *p;
10291 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10293 if (discarded_section (p->sec))
10295 /* Input section has been discarded, either because
10296 it is a copy of a linkonce section or due to
10297 linker script /DISCARD/, so we'll be discarding
10298 the relocs too. */
10300 else if (p->count != 0)
10302 unsigned int count;
10303 asection *srel;
10305 count = p->count;
10306 if (info->enable_dt_relr
10307 && ((!NO_OPD_RELOCS
10308 && (ppc64_elf_section_data (p->sec)->sec_type
10309 == sec_opd))
10310 || !p->ifunc))
10311 count -= p->rel_count;
10312 srel = elf_section_data (p->sec)->sreloc;
10313 if (p->ifunc)
10314 srel = htab->elf.irelplt;
10315 srel->size += count * sizeof (Elf64_External_Rela);
10316 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10317 info->flags |= DF_TEXTREL;
10322 lgot_ents = elf_local_got_ents (ibfd);
10323 if (!lgot_ents)
10324 continue;
10326 symtab_hdr = &elf_symtab_hdr (ibfd);
10327 locsymcount = symtab_hdr->sh_info;
10328 end_lgot_ents = lgot_ents + locsymcount;
10329 local_plt = (struct plt_entry **) end_lgot_ents;
10330 end_local_plt = local_plt + locsymcount;
10331 lgot_masks = (unsigned char *) end_local_plt;
10332 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
10333 if (local_syms == NULL && locsymcount != 0)
10335 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
10336 0, NULL, NULL, NULL);
10337 if (local_syms == NULL)
10338 return false;
10340 s = ppc64_elf_tdata (ibfd)->got;
10341 for (isym = local_syms;
10342 lgot_ents < end_lgot_ents;
10343 ++lgot_ents, ++lgot_masks, isym++)
10345 struct got_entry **pent, *ent;
10347 pent = lgot_ents;
10348 while ((ent = *pent) != NULL)
10349 if (ent->got.refcount > 0)
10351 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10353 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10354 *pent = ent->next;
10356 else
10358 unsigned int ent_size = 8;
10359 unsigned int rel_size = sizeof (Elf64_External_Rela);
10361 ent->got.offset = s->size;
10362 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10364 ent_size *= 2;
10365 rel_size *= 2;
10367 s->size += ent_size;
10368 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10370 htab->elf.irelplt->size += rel_size;
10371 htab->got_reli_size += rel_size;
10373 else if (bfd_link_pic (info)
10374 && (ent->tls_type == 0
10375 ? !info->enable_dt_relr
10376 : !bfd_link_executable (info))
10377 && isym->st_shndx != SHN_ABS)
10379 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10380 srel->size += rel_size;
10382 pent = &ent->next;
10385 else
10386 *pent = ent->next;
10388 if (local_syms != NULL
10389 && symtab_hdr->contents != (unsigned char *) local_syms)
10391 if (!info->keep_memory)
10392 free (local_syms);
10393 else
10394 symtab_hdr->contents = (unsigned char *) local_syms;
10397 /* Allocate space for plt calls to local syms. */
10398 lgot_masks = (unsigned char *) end_local_plt;
10399 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10401 struct plt_entry *ent;
10403 for (ent = *local_plt; ent != NULL; ent = ent->next)
10404 if (ent->plt.refcount > 0)
10406 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10408 s = htab->elf.iplt;
10409 ent->plt.offset = s->size;
10410 s->size += PLT_ENTRY_SIZE (htab);
10411 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10413 else if (htab->can_convert_all_inline_plt
10414 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10415 ent->plt.offset = (bfd_vma) -1;
10416 else
10418 s = htab->pltlocal;
10419 ent->plt.offset = s->size;
10420 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10421 if (bfd_link_pic (info)
10422 && !(info->enable_dt_relr && !htab->opd_abi))
10423 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10426 else
10427 ent->plt.offset = (bfd_vma) -1;
10431 /* Allocate global sym .plt and .got entries, and space for global
10432 sym dynamic relocs. */
10433 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10435 if (!htab->opd_abi && !bfd_link_pic (info))
10436 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10438 first_tlsld = NULL;
10439 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10441 struct got_entry *ent;
10443 if (!is_ppc64_elf (ibfd))
10444 continue;
10446 ent = ppc64_tlsld_got (ibfd);
10447 if (ent->got.refcount > 0)
10449 if (!htab->do_multi_toc && first_tlsld != NULL)
10451 ent->is_indirect = true;
10452 ent->got.ent = first_tlsld;
10454 else
10456 if (first_tlsld == NULL)
10457 first_tlsld = ent;
10458 s = ppc64_elf_tdata (ibfd)->got;
10459 ent->got.offset = s->size;
10460 ent->owner = ibfd;
10461 s->size += 16;
10462 if (bfd_link_dll (info))
10464 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10465 srel->size += sizeof (Elf64_External_Rela);
10469 else
10470 ent->got.offset = (bfd_vma) -1;
10473 /* We now have determined the sizes of the various dynamic sections.
10474 Allocate memory for them. */
10475 relocs = false;
10476 for (s = dynobj->sections; s != NULL; s = s->next)
10478 if ((s->flags & SEC_LINKER_CREATED) == 0)
10479 continue;
10481 if (s == htab->brlt || s == htab->relbrlt || s == htab->elf.srelrdyn)
10482 /* These haven't been allocated yet; don't strip. */
10483 continue;
10484 else if (s == htab->elf.sgot
10485 || s == htab->elf.splt
10486 || s == htab->elf.iplt
10487 || s == htab->pltlocal
10488 || s == htab->glink
10489 || s == htab->global_entry
10490 || s == htab->elf.sdynbss
10491 || s == htab->elf.sdynrelro)
10493 /* Strip this section if we don't need it; see the
10494 comment below. */
10496 else if (s == htab->glink_eh_frame)
10498 if (!bfd_is_abs_section (s->output_section))
10499 /* Not sized yet. */
10500 continue;
10502 else if (startswith (s->name, ".rela"))
10504 if (s->size != 0)
10506 if (s != htab->elf.srelplt)
10507 relocs = true;
10509 /* We use the reloc_count field as a counter if we need
10510 to copy relocs into the output file. */
10511 s->reloc_count = 0;
10514 else
10516 /* It's not one of our sections, so don't allocate space. */
10517 continue;
10520 if (s->size == 0)
10522 /* If we don't need this section, strip it from the
10523 output file. This is mostly to handle .rela.bss and
10524 .rela.plt. We must create both sections in
10525 create_dynamic_sections, because they must be created
10526 before the linker maps input sections to output
10527 sections. The linker does that before
10528 adjust_dynamic_symbol is called, and it is that
10529 function which decides whether anything needs to go
10530 into these sections. */
10531 s->flags |= SEC_EXCLUDE;
10532 continue;
10535 if (bfd_is_abs_section (s->output_section))
10536 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10537 s->name);
10539 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10540 continue;
10542 /* Allocate memory for the section contents. We use bfd_zalloc
10543 here in case unused entries are not reclaimed before the
10544 section's contents are written out. This should not happen,
10545 but this way if it does we get a R_PPC64_NONE reloc in .rela
10546 sections instead of garbage.
10547 We also rely on the section contents being zero when writing
10548 the GOT and .dynrelro. */
10549 s->contents = bfd_zalloc (dynobj, s->size);
10550 if (s->contents == NULL)
10551 return false;
10552 s->alloced = 1;
10555 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10557 if (!is_ppc64_elf (ibfd))
10558 continue;
10560 s = ppc64_elf_tdata (ibfd)->got;
10561 if (s != NULL && s != htab->elf.sgot)
10563 if (s->size == 0)
10564 s->flags |= SEC_EXCLUDE;
10565 else
10567 s->contents = bfd_zalloc (ibfd, s->size);
10568 if (s->contents == NULL)
10569 return false;
10570 s->alloced = 1;
10573 s = ppc64_elf_tdata (ibfd)->relgot;
10574 if (s != NULL)
10576 if (s->size == 0)
10577 s->flags |= SEC_EXCLUDE;
10578 else
10580 s->contents = bfd_zalloc (ibfd, s->size);
10581 if (s->contents == NULL)
10582 return false;
10583 s->alloced = 1;
10584 relocs = true;
10585 s->reloc_count = 0;
10590 if (htab->elf.dynamic_sections_created)
10592 bool tls_opt;
10594 /* Add some entries to the .dynamic section. We fill in the
10595 values later, in ppc64_elf_finish_dynamic_sections, but we
10596 must add the entries now so that we get the correct size for
10597 the .dynamic section. The DT_DEBUG entry is filled in by the
10598 dynamic linker and used by the debugger. */
10599 #define add_dynamic_entry(TAG, VAL) \
10600 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10602 if (bfd_link_executable (info))
10604 if (!add_dynamic_entry (DT_DEBUG, 0))
10605 return false;
10608 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10610 if (!add_dynamic_entry (DT_PLTGOT, 0)
10611 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10612 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10613 || !add_dynamic_entry (DT_JMPREL, 0)
10614 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10615 return false;
10618 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10620 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10621 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10622 return false;
10625 tls_opt = (htab->params->tls_get_addr_opt
10626 && ((htab->tls_get_addr_fd != NULL
10627 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10628 || (htab->tga_desc_fd != NULL
10629 && htab->tga_desc_fd->elf.plt.plist != NULL)));
10630 if (tls_opt || !htab->opd_abi)
10632 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10633 return false;
10636 if (relocs)
10638 if (!add_dynamic_entry (DT_RELA, 0)
10639 || !add_dynamic_entry (DT_RELASZ, 0)
10640 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10641 return false;
10643 /* If any dynamic relocs apply to a read-only section,
10644 then we need a DT_TEXTREL entry. */
10645 if ((info->flags & DF_TEXTREL) == 0)
10646 elf_link_hash_traverse (&htab->elf,
10647 _bfd_elf_maybe_set_textrel, info);
10649 if ((info->flags & DF_TEXTREL) != 0)
10651 if (!add_dynamic_entry (DT_TEXTREL, 0))
10652 return false;
10656 #undef add_dynamic_entry
10658 return true;
10661 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10663 static bool
10664 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10666 if (h->plt.plist != NULL
10667 && !h->def_regular
10668 && !h->pointer_equality_needed)
10669 return false;
10671 return _bfd_elf_hash_symbol (h);
10674 /* Determine the type of stub needed, if any, for a call. */
10676 static inline enum ppc_stub_main_type
10677 ppc_type_of_stub (asection *input_sec,
10678 const Elf_Internal_Rela *rel,
10679 struct ppc_link_hash_entry **hash,
10680 struct plt_entry **plt_ent,
10681 bfd_vma destination,
10682 unsigned long local_off)
10684 struct ppc_link_hash_entry *h = *hash;
10685 bfd_vma location;
10686 bfd_vma branch_offset;
10687 bfd_vma max_branch_offset;
10688 enum elf_ppc64_reloc_type r_type;
10690 if (h != NULL)
10692 struct plt_entry *ent;
10693 struct ppc_link_hash_entry *fdh = h;
10694 if (h->oh != NULL
10695 && h->oh->is_func_descriptor)
10697 fdh = ppc_follow_link (h->oh);
10698 *hash = fdh;
10701 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10702 if (ent->addend == rel->r_addend
10703 && ent->plt.offset != (bfd_vma) -1)
10705 *plt_ent = ent;
10706 return ppc_stub_plt_call;
10709 /* Here, we know we don't have a plt entry. If we don't have a
10710 either a defined function descriptor or a defined entry symbol
10711 in a regular object file, then it is pointless trying to make
10712 any other type of stub. */
10713 if (!is_static_defined (&fdh->elf)
10714 && !is_static_defined (&h->elf))
10715 return ppc_stub_none;
10717 else if (elf_local_got_ents (input_sec->owner) != NULL)
10719 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10720 struct plt_entry **local_plt = (struct plt_entry **)
10721 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10722 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10724 if (local_plt[r_symndx] != NULL)
10726 struct plt_entry *ent;
10728 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10729 if (ent->addend == rel->r_addend
10730 && ent->plt.offset != (bfd_vma) -1)
10732 *plt_ent = ent;
10733 return ppc_stub_plt_call;
10738 /* Determine where the call point is. */
10739 location = (input_sec->output_offset
10740 + input_sec->output_section->vma
10741 + rel->r_offset);
10743 branch_offset = destination - location;
10744 r_type = ELF64_R_TYPE (rel->r_info);
10746 /* Determine if a long branch stub is needed. */
10747 max_branch_offset = 1 << 25;
10748 if (r_type == R_PPC64_REL14
10749 || r_type == R_PPC64_REL14_BRTAKEN
10750 || r_type == R_PPC64_REL14_BRNTAKEN)
10751 max_branch_offset = 1 << 15;
10753 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10754 /* We need a stub. Figure out whether a long_branch or plt_branch
10755 is needed later. */
10756 return ppc_stub_long_branch;
10758 return ppc_stub_none;
10761 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10762 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10763 . mflr %r12
10764 . bcl 20,31,1f
10765 .1: mflr %r11
10766 . mtlr %r12
10767 . lis %r12,xxx-1b@highest
10768 . ori %r12,%r12,xxx-1b@higher
10769 . sldi %r12,%r12,32
10770 . oris %r12,%r12,xxx-1b@high
10771 . ori %r12,%r12,xxx-1b@l
10772 . add/ldx %r12,%r11,%r12 */
10774 static bfd_byte *
10775 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
10777 bfd_put_32 (abfd, MFLR_R12, p);
10778 p += 4;
10779 bfd_put_32 (abfd, BCL_20_31, p);
10780 p += 4;
10781 bfd_put_32 (abfd, MFLR_R11, p);
10782 p += 4;
10783 bfd_put_32 (abfd, MTLR_R12, p);
10784 p += 4;
10785 if (off + 0x8000 < 0x10000)
10787 if (load)
10788 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10789 else
10790 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10791 p += 4;
10793 else if (off + 0x80008000ULL < 0x100000000ULL)
10795 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10796 p += 4;
10797 if (load)
10798 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10799 else
10800 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10801 p += 4;
10803 else
10805 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10807 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10808 p += 4;
10810 else
10812 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10813 p += 4;
10814 if (((off >> 32) & 0xffff) != 0)
10816 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10817 p += 4;
10820 if (((off >> 32) & 0xffffffffULL) != 0)
10822 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10823 p += 4;
10825 if (PPC_HI (off) != 0)
10827 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10828 p += 4;
10830 if (PPC_LO (off) != 0)
10832 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10833 p += 4;
10835 if (load)
10836 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10837 else
10838 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10839 p += 4;
10841 return p;
10844 static unsigned int
10845 size_offset (bfd_vma off)
10847 unsigned int size;
10848 if (off + 0x8000 < 0x10000)
10849 size = 4;
10850 else if (off + 0x80008000ULL < 0x100000000ULL)
10851 size = 8;
10852 else
10854 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10855 size = 4;
10856 else
10858 size = 4;
10859 if (((off >> 32) & 0xffff) != 0)
10860 size += 4;
10862 if (((off >> 32) & 0xffffffffULL) != 0)
10863 size += 4;
10864 if (PPC_HI (off) != 0)
10865 size += 4;
10866 if (PPC_LO (off) != 0)
10867 size += 4;
10868 size += 4;
10870 return size + 16;
10873 static unsigned int
10874 num_relocs_for_offset (bfd_vma off)
10876 unsigned int num_rel;
10877 if (off + 0x8000 < 0x10000)
10878 num_rel = 1;
10879 else if (off + 0x80008000ULL < 0x100000000ULL)
10880 num_rel = 2;
10881 else
10883 num_rel = 1;
10884 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10885 && ((off >> 32) & 0xffff) != 0)
10886 num_rel += 1;
10887 if (PPC_HI (off) != 0)
10888 num_rel += 1;
10889 if (PPC_LO (off) != 0)
10890 num_rel += 1;
10892 return num_rel;
10895 static Elf_Internal_Rela *
10896 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10897 bfd_vma roff, bfd_vma targ, bfd_vma off)
10899 bfd_vma relative_targ = targ - (roff - 8);
10900 if (bfd_big_endian (info->output_bfd))
10901 roff += 2;
10902 r->r_offset = roff;
10903 r->r_addend = relative_targ + roff;
10904 if (off + 0x8000 < 0x10000)
10905 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10906 else if (off + 0x80008000ULL < 0x100000000ULL)
10908 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10909 ++r;
10910 roff += 4;
10911 r->r_offset = roff;
10912 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10913 r->r_addend = relative_targ + roff;
10915 else
10917 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10918 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10919 else
10921 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10922 if (((off >> 32) & 0xffff) != 0)
10924 ++r;
10925 roff += 4;
10926 r->r_offset = roff;
10927 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10928 r->r_addend = relative_targ + roff;
10931 if (((off >> 32) & 0xffffffffULL) != 0)
10932 roff += 4;
10933 if (PPC_HI (off) != 0)
10935 ++r;
10936 roff += 4;
10937 r->r_offset = roff;
10938 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10939 r->r_addend = relative_targ + roff;
10941 if (PPC_LO (off) != 0)
10943 ++r;
10944 roff += 4;
10945 r->r_offset = roff;
10946 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10947 r->r_addend = relative_targ + roff;
10950 return r;
10953 static bfd_byte *
10954 build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10955 bool load)
10957 uint64_t insn;
10958 if (off - odd + (1ULL << 33) < 1ULL << 34)
10960 off -= odd;
10961 if (odd)
10963 bfd_put_32 (abfd, NOP, p);
10964 p += 4;
10966 if (load)
10967 insn = PLD_R12_PC;
10968 else
10969 insn = PADDI_R12_PC;
10970 insn |= D34 (off);
10971 bfd_put_32 (abfd, insn >> 32, p);
10972 p += 4;
10973 bfd_put_32 (abfd, insn, p);
10975 /* The minimum value for paddi is -0x200000000. The minimum value
10976 for li is -0x8000, which when shifted by 34 and added gives a
10977 minimum value of -0x2000200000000. The maximum value is
10978 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10979 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10981 off -= 8 - odd;
10982 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10983 p += 4;
10984 if (!odd)
10986 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10987 p += 4;
10989 insn = PADDI_R12_PC | D34 (off);
10990 bfd_put_32 (abfd, insn >> 32, p);
10991 p += 4;
10992 bfd_put_32 (abfd, insn, p);
10993 p += 4;
10994 if (odd)
10996 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10997 p += 4;
10999 if (load)
11000 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
11001 else
11002 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
11004 else
11006 off -= odd + 8;
11007 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
11008 p += 4;
11009 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
11010 p += 4;
11011 if (odd)
11013 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
11014 p += 4;
11016 insn = PADDI_R12_PC | D34 (off);
11017 bfd_put_32 (abfd, insn >> 32, p);
11018 p += 4;
11019 bfd_put_32 (abfd, insn, p);
11020 p += 4;
11021 if (!odd)
11023 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
11024 p += 4;
11026 if (load)
11027 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
11028 else
11029 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
11031 p += 4;
11032 return p;
11035 static unsigned int
11036 size_power10_offset (bfd_vma off, int odd)
11038 if (off - odd + (1ULL << 33) < 1ULL << 34)
11039 return odd + 8;
11040 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11041 return 20;
11042 else
11043 return 24;
11046 static unsigned int
11047 num_relocs_for_power10_offset (bfd_vma off, int odd)
11049 if (off - odd + (1ULL << 33) < 1ULL << 34)
11050 return 1;
11051 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11052 return 2;
11053 else
11054 return 3;
11057 static Elf_Internal_Rela *
11058 emit_relocs_for_power10_offset (struct bfd_link_info *info,
11059 Elf_Internal_Rela *r, bfd_vma roff,
11060 bfd_vma targ, bfd_vma off, int odd)
11062 if (off - odd + (1ULL << 33) < 1ULL << 34)
11063 roff += odd;
11064 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11066 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11067 r->r_offset = roff + d_offset;
11068 r->r_addend = targ + 8 - odd - d_offset;
11069 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11070 ++r;
11071 roff += 8 - odd;
11073 else
11075 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11076 r->r_offset = roff + d_offset;
11077 r->r_addend = targ + 8 + odd - d_offset;
11078 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
11079 ++r;
11080 roff += 4;
11081 r->r_offset = roff + d_offset;
11082 r->r_addend = targ + 4 + odd - d_offset;
11083 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11084 ++r;
11085 roff += 4 + odd;
11087 r->r_offset = roff;
11088 r->r_addend = targ;
11089 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
11090 return r;
11093 /* Emit .eh_frame opcode to advance pc by DELTA. */
11095 static bfd_byte *
11096 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
11098 delta /= 4;
11099 if (delta < 64)
11100 *eh++ = DW_CFA_advance_loc + delta;
11101 else if (delta < 256)
11103 *eh++ = DW_CFA_advance_loc1;
11104 *eh++ = delta;
11106 else if (delta < 65536)
11108 *eh++ = DW_CFA_advance_loc2;
11109 bfd_put_16 (abfd, delta, eh);
11110 eh += 2;
11112 else
11114 *eh++ = DW_CFA_advance_loc4;
11115 bfd_put_32 (abfd, delta, eh);
11116 eh += 4;
11118 return eh;
11121 /* Size of required .eh_frame opcode to advance pc by DELTA. */
11123 static unsigned int
11124 eh_advance_size (unsigned int delta)
11126 if (delta < 64 * 4)
11127 /* DW_CFA_advance_loc+[1..63]. */
11128 return 1;
11129 if (delta < 256 * 4)
11130 /* DW_CFA_advance_loc1, byte. */
11131 return 2;
11132 if (delta < 65536 * 4)
11133 /* DW_CFA_advance_loc2, 2 bytes. */
11134 return 3;
11135 /* DW_CFA_advance_loc4, 4 bytes. */
11136 return 5;
11139 /* With power7 weakly ordered memory model, it is possible for ld.so
11140 to update a plt entry in one thread and have another thread see a
11141 stale zero toc entry. To avoid this we need some sort of acquire
11142 barrier in the call stub. One solution is to make the load of the
11143 toc word seem to appear to depend on the load of the function entry
11144 word. Another solution is to test for r2 being zero, and branch to
11145 the appropriate glink entry if so.
11147 . fake dep barrier compare
11148 . ld 12,xxx(2) ld 12,xxx(2)
11149 . mtctr 12 mtctr 12
11150 . xor 11,12,12 ld 2,xxx+8(2)
11151 . add 2,2,11 cmpldi 2,0
11152 . ld 2,xxx+8(2) bnectr+
11153 . bctr b <glink_entry>
11155 The solution involving the compare turns out to be faster, so
11156 that's what we use unless the branch won't reach. */
11158 #define ALWAYS_USE_FAKE_DEP 0
11159 #define ALWAYS_EMIT_R2SAVE 0
11161 static inline unsigned int
11162 plt_stub_size (struct ppc_link_hash_table *htab,
11163 struct ppc_stub_hash_entry *stub_entry,
11164 bfd_vma off,
11165 unsigned int odd)
11167 unsigned size;
11169 if (stub_entry->type.sub == ppc_stub_notoc)
11171 size = 8 + size_power10_offset (off, odd);
11172 if (stub_entry->type.r2save)
11173 size += 4;
11175 else if (stub_entry->type.sub == ppc_stub_p9notoc)
11177 size = 8 + size_offset (off - 8);
11178 if (stub_entry->type.r2save)
11179 size += 4;
11181 else
11183 size = 12;
11184 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11185 size += 4;
11186 if (PPC_HA (off) != 0)
11187 size += 4;
11188 if (htab->opd_abi)
11190 size += 4;
11191 if (htab->params->plt_static_chain)
11192 size += 4;
11193 if (htab->params->plt_thread_safe
11194 && htab->elf.dynamic_sections_created
11195 && stub_entry->h != NULL
11196 && stub_entry->h->elf.dynindx != -1)
11197 size += 8;
11198 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
11199 != PPC_HA (off))
11200 size += 4;
11203 if (stub_entry->h != NULL
11204 && is_tls_get_addr (&stub_entry->h->elf, htab)
11205 && htab->params->tls_get_addr_opt)
11207 if (!htab->params->no_tls_get_addr_regsave)
11209 size += 30 * 4;
11210 if (stub_entry->type.r2save)
11211 size += 4;
11213 else
11215 size += 7 * 4;
11216 if (stub_entry->type.r2save)
11217 size += 6 * 4;
11220 return size;
11223 /* Depending on the sign of plt_stub_align:
11224 If positive, return the padding to align to a 2**plt_stub_align
11225 boundary.
11226 If negative, if this stub would cross fewer 2**plt_stub_align
11227 boundaries if we align, then return the padding needed to do so. */
11229 static inline unsigned int
11230 plt_stub_pad (int plt_stub_align,
11231 bfd_vma stub_off,
11232 unsigned int stub_size)
11234 unsigned int stub_align;
11236 if (plt_stub_align >= 0)
11237 stub_align = 1u << plt_stub_align;
11238 else
11240 stub_align = 1u << -plt_stub_align;
11241 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
11242 <= ((stub_size - 1) & -stub_align))
11243 return 0;
11245 return stub_align - 1 - ((stub_off - 1) & (stub_align - 1));
11248 /* Build a toc using .plt call stub. */
11250 static inline bfd_byte *
11251 build_plt_stub (struct ppc_link_hash_table *htab,
11252 struct ppc_stub_hash_entry *stub_entry,
11253 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11255 bfd *obfd = htab->params->stub_bfd;
11256 bool plt_load_toc = htab->opd_abi;
11257 bool plt_static_chain = htab->params->plt_static_chain;
11258 bool plt_thread_safe = (htab->params->plt_thread_safe
11259 && htab->elf.dynamic_sections_created
11260 && stub_entry->h != NULL
11261 && stub_entry->h->elf.dynindx != -1);
11262 bool use_fake_dep = plt_thread_safe;
11263 bfd_vma cmp_branch_off = 0;
11265 if (!ALWAYS_USE_FAKE_DEP
11266 && plt_load_toc
11267 && plt_thread_safe
11268 && !(stub_entry->h != NULL
11269 && is_tls_get_addr (&stub_entry->h->elf, htab)
11270 && htab->params->tls_get_addr_opt))
11272 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
11273 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11274 / PLT_ENTRY_SIZE (htab));
11275 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
11276 bfd_vma to, from;
11278 if (pltindex > 32768)
11279 glinkoff += (pltindex - 32768) * 4;
11280 to = (glinkoff
11281 + htab->glink->output_offset
11282 + htab->glink->output_section->vma);
11283 from = (p - stub_entry->group->stub_sec->contents
11284 + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11285 + 4 * (PPC_HA (offset) != 0)
11286 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11287 != PPC_HA (offset))
11288 + 4 * (plt_static_chain != 0)
11289 + 20
11290 + stub_entry->group->stub_sec->output_offset
11291 + stub_entry->group->stub_sec->output_section->vma);
11292 cmp_branch_off = to - from;
11293 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11296 if (PPC_HA (offset) != 0)
11298 if (r != NULL)
11300 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11301 r[0].r_offset += 4;
11302 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11303 r[1].r_offset = r[0].r_offset + 4;
11304 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11305 r[1].r_addend = r[0].r_addend;
11306 if (plt_load_toc)
11308 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11310 r[2].r_offset = r[1].r_offset + 4;
11311 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11312 r[2].r_addend = r[0].r_addend;
11314 else
11316 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11317 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11318 r[2].r_addend = r[0].r_addend + 8;
11319 if (plt_static_chain)
11321 r[3].r_offset = r[2].r_offset + 4;
11322 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11323 r[3].r_addend = r[0].r_addend + 16;
11328 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11329 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11330 if (plt_load_toc)
11332 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11333 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11335 else
11337 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11338 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11340 if (plt_load_toc
11341 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11343 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
11344 offset = 0;
11346 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11347 if (plt_load_toc)
11349 if (use_fake_dep)
11351 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11352 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11354 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11355 if (plt_static_chain)
11356 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
11359 else
11361 if (r != NULL)
11363 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11364 r[0].r_offset += 4;
11365 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11366 if (plt_load_toc)
11368 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11370 r[1].r_offset = r[0].r_offset + 4;
11371 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11372 r[1].r_addend = r[0].r_addend;
11374 else
11376 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11377 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11378 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11379 if (plt_static_chain)
11381 r[2].r_offset = r[1].r_offset + 4;
11382 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11383 r[2].r_addend = r[0].r_addend + 8;
11388 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11389 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11390 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
11391 if (plt_load_toc
11392 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11394 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11395 offset = 0;
11397 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11398 if (plt_load_toc)
11400 if (use_fake_dep)
11402 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11403 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11405 if (plt_static_chain)
11406 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11407 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11410 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11412 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11413 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
11414 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11416 else
11417 bfd_put_32 (obfd, BCTR, p), p += 4;
11418 return p;
11421 /* Build a special .plt call stub for __tls_get_addr. */
11423 #define LD_R0_0R3 0xe8030000
11424 #define LD_R12_0R3 0xe9830000
11425 #define MR_R0_R3 0x7c601b78
11426 #define CMPDI_R0_0 0x2c200000
11427 #define ADD_R3_R12_R13 0x7c6c6a14
11428 #define BEQLR 0x4d820020
11429 #define MR_R3_R0 0x7c030378
11430 #define BCTRL 0x4e800421
11432 static bfd_byte *
11433 build_tls_get_addr_head (struct ppc_link_hash_table *htab,
11434 struct ppc_stub_hash_entry *stub_entry,
11435 bfd_byte *p)
11437 bfd *obfd = htab->params->stub_bfd;
11439 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
11440 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11441 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
11442 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11443 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11444 bfd_put_32 (obfd, BEQLR, p), p += 4;
11445 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
11447 if (!htab->params->no_tls_get_addr_regsave)
11448 p = tls_get_addr_prologue (obfd, p, htab);
11449 else if (stub_entry->type.r2save)
11451 bfd_put_32 (obfd, MFLR_R0, p);
11452 p += 4;
11453 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11454 p += 4;
11456 return p;
11459 static bfd_byte *
11460 build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11461 struct ppc_stub_hash_entry *stub_entry,
11462 bfd_byte *p,
11463 bfd_byte *loc)
11465 bfd *obfd = htab->params->stub_bfd;
11467 if (!htab->params->no_tls_get_addr_regsave)
11469 bfd_put_32 (obfd, BCTRL, p - 4);
11471 if (stub_entry->type.r2save)
11473 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11474 p += 4;
11476 p = tls_get_addr_epilogue (obfd, p, htab);
11478 else if (stub_entry->type.r2save)
11480 bfd_put_32 (obfd, BCTRL, p - 4);
11482 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11483 p += 4;
11484 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11485 p += 4;
11486 bfd_put_32 (obfd, MTLR_R0, p);
11487 p += 4;
11488 bfd_put_32 (obfd, BLR, p);
11489 p += 4;
11492 if (htab->glink_eh_frame != NULL
11493 && htab->glink_eh_frame->size != 0)
11495 bfd_byte *base, *eh;
11497 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11498 eh = base + stub_entry->group->eh_size;
11500 if (!htab->params->no_tls_get_addr_regsave)
11502 unsigned int cfa_updt, delta, i;
11504 /* After the bctrl, lr has been modified so we need to emit
11505 .eh_frame info saying the return address is on the stack. In
11506 fact we must put the EH info at or before the call rather
11507 than after it, because the EH info for a call needs to be
11508 specified by that point.
11509 See libgcc/unwind-dw2.c execute_cfa_program.
11510 Any stack pointer update must be described immediately after
11511 the instruction making the change, and since the stdu occurs
11512 after saving regs we put all the reg saves and the cfa
11513 change there. */
11514 cfa_updt = stub_entry->stub_offset + 18 * 4;
11515 delta = cfa_updt - stub_entry->group->lr_restore;
11516 stub_entry->group->lr_restore
11517 = stub_entry->stub_offset + (p - loc) - 4;
11518 eh = eh_advance (htab->elf.dynobj, eh, delta);
11519 *eh++ = DW_CFA_def_cfa_offset;
11520 if (htab->opd_abi)
11522 *eh++ = 128;
11523 *eh++ = 1;
11525 else
11526 *eh++ = 96;
11527 *eh++ = DW_CFA_offset_extended_sf;
11528 *eh++ = 65;
11529 *eh++ = (-16 / 8) & 0x7f;
11530 for (i = 4; i < 12; i++)
11532 *eh++ = DW_CFA_offset + i;
11533 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11535 *eh++ = (DW_CFA_advance_loc
11536 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11537 *eh++ = DW_CFA_def_cfa_offset;
11538 *eh++ = 0;
11539 for (i = 4; i < 12; i++)
11540 *eh++ = DW_CFA_restore + i;
11541 *eh++ = DW_CFA_advance_loc + 2;
11542 *eh++ = DW_CFA_restore_extended;
11543 *eh++ = 65;
11544 stub_entry->group->eh_size = eh - base;
11546 else if (stub_entry->type.r2save)
11548 unsigned int lr_used, delta;
11550 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11551 delta = lr_used - stub_entry->group->lr_restore;
11552 stub_entry->group->lr_restore = lr_used + 16;
11553 eh = eh_advance (htab->elf.dynobj, eh, delta);
11554 *eh++ = DW_CFA_offset_extended_sf;
11555 *eh++ = 65;
11556 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11557 *eh++ = DW_CFA_advance_loc + 4;
11558 *eh++ = DW_CFA_restore_extended;
11559 *eh++ = 65;
11560 stub_entry->group->eh_size = eh - base;
11563 return p;
11566 static Elf_Internal_Rela *
11567 get_relocs (asection *sec, int count)
11569 Elf_Internal_Rela *relocs;
11570 struct bfd_elf_section_data *elfsec_data;
11572 elfsec_data = elf_section_data (sec);
11573 relocs = elfsec_data->relocs;
11574 if (relocs == NULL)
11576 bfd_size_type relsize;
11577 relsize = sec->reloc_count * sizeof (*relocs);
11578 relocs = bfd_malloc (relsize);
11579 if (relocs == NULL)
11580 return NULL;
11581 elfsec_data->relocs = relocs;
11582 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11583 sizeof (Elf_Internal_Shdr));
11584 if (elfsec_data->rela.hdr == NULL)
11585 return NULL;
11586 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11587 * sizeof (Elf64_External_Rela));
11588 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11589 sec->reloc_count = 0;
11591 relocs += sec->reloc_count;
11592 sec->reloc_count += count;
11593 return relocs;
11596 static bool
11597 swap_reloc_out (bfd *obfd, Elf_Internal_Rela *rel, bfd_byte *loc, asection *s)
11599 if ((size_t) (loc - s->contents) >= s->size)
11600 return false;
11601 bfd_elf64_swap_reloca_out (obfd, rel, loc);
11602 return true;
11605 static bool
11606 count_and_swap_reloc_out (bfd *obfd, Elf_Internal_Rela *rel, asection *s)
11608 bfd_byte *loc = s->contents;
11609 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
11610 return swap_reloc_out (obfd, rel, loc, s);
11614 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11615 forms, to the equivalent relocs against the global symbol given by
11616 STUB_ENTRY->H. */
11618 static bool
11619 use_global_in_relocs (struct ppc_link_hash_table *htab,
11620 struct ppc_stub_hash_entry *stub_entry,
11621 Elf_Internal_Rela *r, unsigned int num_rel)
11623 struct elf_link_hash_entry **hashes;
11624 unsigned long symndx;
11625 struct ppc_link_hash_entry *h;
11626 bfd_vma symval;
11628 /* Relocs are always against symbols in their own object file. Fake
11629 up global sym hashes for the stub bfd (which has no symbols). */
11630 hashes = elf_sym_hashes (htab->params->stub_bfd);
11631 if (hashes == NULL)
11633 bfd_size_type hsize;
11635 /* When called the first time, stub_globals will contain the
11636 total number of symbols seen during stub sizing. After
11637 allocating, stub_globals is used as an index to fill the
11638 hashes array. */
11639 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11640 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11641 if (hashes == NULL)
11642 return false;
11643 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11644 htab->stub_globals = 1;
11646 symndx = htab->stub_globals++;
11647 h = stub_entry->h;
11648 hashes[symndx] = &h->elf;
11649 if (h->oh != NULL && h->oh->is_func)
11650 h = ppc_follow_link (h->oh);
11651 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11652 || h->elf.root.type == bfd_link_hash_defweak);
11653 symval = defined_sym_val (&h->elf);
11654 while (num_rel-- != 0)
11656 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11657 if (h->elf.root.u.def.section != stub_entry->target_section)
11659 /* H is an opd symbol. The addend must be zero, and the
11660 branch reloc is the only one we can convert. */
11661 r->r_addend = 0;
11662 break;
11664 else
11665 r->r_addend -= symval;
11666 --r;
11668 return true;
11671 static bfd_vma
11672 get_r2off (struct bfd_link_info *info,
11673 struct ppc_stub_hash_entry *stub_entry)
11675 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11676 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11678 if (r2off == 0)
11680 /* Support linking -R objects. Get the toc pointer from the
11681 opd entry. */
11682 char buf[8];
11683 if (!htab->opd_abi)
11684 return r2off;
11685 asection *opd = stub_entry->h->elf.root.u.def.section;
11686 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11688 if (strcmp (opd->name, ".opd") != 0
11689 || opd->reloc_count != 0)
11691 info->callbacks->einfo
11692 (_("%P: cannot find opd entry toc for `%pT'\n"),
11693 stub_entry->h->elf.root.root.string);
11694 bfd_set_error (bfd_error_bad_value);
11695 return (bfd_vma) -1;
11697 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11698 return (bfd_vma) -1;
11699 r2off = bfd_get_64 (opd->owner, buf);
11700 r2off -= elf_gp (info->output_bfd);
11702 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11703 return r2off;
11706 /* Debug dump. */
11708 static void
11709 dump_stub (const char *header,
11710 struct ppc_stub_hash_entry *stub_entry,
11711 size_t end_offset)
11713 const char *t1, *t2, *t3;
11714 switch (stub_entry->type.main)
11716 case ppc_stub_none: t1 = "none"; break;
11717 case ppc_stub_long_branch: t1 = "long_branch"; break;
11718 case ppc_stub_plt_branch: t1 = "plt_branch"; break;
11719 case ppc_stub_plt_call: t1 = "plt_call"; break;
11720 case ppc_stub_global_entry: t1 = "global_entry"; break;
11721 case ppc_stub_save_res: t1 = "save_res"; break;
11722 default: t1 = "???"; break;
11724 switch (stub_entry->type.sub)
11726 case ppc_stub_toc: t2 = "toc"; break;
11727 case ppc_stub_notoc: t2 = "notoc"; break;
11728 case ppc_stub_p9notoc: t2 = "p9notoc"; break;
11729 default: t2 = "???"; break;
11731 t3 = stub_entry->type.r2save ? "r2save" : "";
11732 fprintf (stderr, "%s id = %u type = %s:%s:%s\n",
11733 header, stub_entry->id, t1, t2, t3);
11734 fprintf (stderr, "name = %s\n", stub_entry->root.string);
11735 fprintf (stderr, "offset = 0x%" PRIx64 ":", stub_entry->stub_offset);
11736 for (size_t i = stub_entry->stub_offset; i < end_offset; i += 4)
11738 asection *stub_sec = stub_entry->group->stub_sec;
11739 uint32_t *p = (uint32_t *) (stub_sec->contents + i);
11740 fprintf (stderr, " %08x", (uint32_t) bfd_get_32 (stub_sec->owner, p));
11742 fprintf (stderr, "\n");
11745 static bool
11746 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11748 struct ppc_stub_hash_entry *stub_entry;
11749 struct ppc_branch_hash_entry *br_entry;
11750 struct bfd_link_info *info;
11751 struct ppc_link_hash_table *htab;
11752 bfd *obfd;
11753 bfd_byte *loc;
11754 bfd_byte *p, *relp;
11755 bfd_vma targ, off;
11756 Elf_Internal_Rela *r;
11757 asection *plt;
11758 int num_rel;
11759 int odd;
11760 bool is_tga;
11762 /* Massage our args to the form they really have. */
11763 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11764 info = in_arg;
11766 htab = ppc_hash_table (info);
11767 if (htab == NULL)
11768 return false;
11770 struct _ppc64_elf_section_data *esd
11771 = ppc64_elf_section_data (stub_entry->group->stub_sec);
11772 ++htab->stub_id;
11773 if (stub_entry->id != htab->stub_id
11774 || (stub_entry->type.main != ppc_stub_save_res
11775 && stub_entry->stub_offset < stub_entry->group->stub_sec->size))
11777 BFD_ASSERT (0);
11778 if (stub_entry->id != htab->stub_id)
11779 fprintf (stderr, "Expected id %u, got %u\n",
11780 htab->stub_id, stub_entry->id);
11781 if (stub_entry->stub_offset < stub_entry->group->stub_sec->size)
11782 fprintf (stderr, "Expected offset >= %" PRIx64 ", got %"
11783 PRIx64 "\n", stub_entry->group->stub_sec->size,
11784 stub_entry->stub_offset);
11785 if (esd->sec_type == sec_stub)
11786 dump_stub ("Previous:", esd->u.last_ent, stub_entry->stub_offset);
11787 dump_stub ("Current:", stub_entry, 0);
11789 if (esd->sec_type == sec_normal)
11790 esd->sec_type = sec_stub;
11791 if (esd->sec_type == sec_stub)
11792 esd->u.last_ent = stub_entry;
11793 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11795 htab->stub_count[stub_entry->type.main - 1] += 1;
11796 if (stub_entry->type.main == ppc_stub_long_branch
11797 && stub_entry->type.sub == ppc_stub_toc)
11799 /* Branches are relative. This is where we are going to. */
11800 targ = (stub_entry->target_value
11801 + stub_entry->target_section->output_offset
11802 + stub_entry->target_section->output_section->vma);
11803 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11805 /* And this is where we are coming from. */
11806 off = (stub_entry->stub_offset
11807 + stub_entry->group->stub_sec->output_offset
11808 + stub_entry->group->stub_sec->output_section->vma);
11809 off = targ - off;
11811 p = loc;
11812 obfd = htab->params->stub_bfd;
11813 if (stub_entry->type.r2save)
11815 bfd_vma r2off = get_r2off (info, stub_entry);
11817 if (r2off == (bfd_vma) -1)
11819 htab->stub_error = true;
11820 return false;
11822 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11823 p += 4;
11824 if (PPC_HA (r2off) != 0)
11826 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11827 p += 4;
11829 if (PPC_LO (r2off) != 0)
11831 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11832 p += 4;
11834 off -= p - loc;
11836 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
11837 p += 4;
11839 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11841 _bfd_error_handler
11842 (_("long branch stub `%s' offset overflow"),
11843 stub_entry->root.string);
11844 htab->stub_error = true;
11845 return false;
11848 if (info->emitrelocations)
11850 r = get_relocs (stub_entry->group->stub_sec, 1);
11851 if (r == NULL)
11852 return false;
11853 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11854 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11855 r->r_addend = targ;
11856 if (stub_entry->h != NULL
11857 && !use_global_in_relocs (htab, stub_entry, r, 1))
11858 return false;
11861 else if (stub_entry->type.main == ppc_stub_plt_branch
11862 && stub_entry->type.sub == ppc_stub_toc)
11864 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11865 stub_entry->root.string + 9,
11866 false, false);
11867 if (br_entry == NULL)
11869 _bfd_error_handler (_("can't find branch stub `%s'"),
11870 stub_entry->root.string);
11871 htab->stub_error = true;
11872 return false;
11875 targ = (stub_entry->target_value
11876 + stub_entry->target_section->output_offset
11877 + stub_entry->target_section->output_section->vma);
11878 if (!stub_entry->type.r2save)
11879 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11881 bfd_put_64 (htab->brlt->owner, targ,
11882 htab->brlt->contents + br_entry->offset);
11884 if (br_entry->iter == htab->stub_iteration)
11886 br_entry->iter = 0;
11888 if (htab->relbrlt != NULL && !info->enable_dt_relr)
11890 /* Create a reloc for the branch lookup table entry. */
11891 Elf_Internal_Rela rela;
11893 rela.r_offset = (br_entry->offset
11894 + htab->brlt->output_offset
11895 + htab->brlt->output_section->vma);
11896 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11897 rela.r_addend = targ;
11899 BFD_ASSERT (count_and_swap_reloc_out (htab->relbrlt->owner, &rela,
11900 htab->relbrlt));
11902 else if (info->emitrelocations)
11904 r = get_relocs (htab->brlt, 1);
11905 if (r == NULL)
11906 return false;
11907 /* brlt, being SEC_LINKER_CREATED does not go through the
11908 normal reloc processing. Symbols and offsets are not
11909 translated from input file to output file form, so
11910 set up the offset per the output file. */
11911 r->r_offset = (br_entry->offset
11912 + htab->brlt->output_offset
11913 + htab->brlt->output_section->vma);
11914 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11915 r->r_addend = targ;
11919 targ = (br_entry->offset
11920 + htab->brlt->output_offset
11921 + htab->brlt->output_section->vma);
11923 off = (elf_gp (info->output_bfd)
11924 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11925 off = targ - off;
11927 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11929 info->callbacks->einfo
11930 (_("%P: linkage table error against `%pT'\n"),
11931 stub_entry->root.string);
11932 bfd_set_error (bfd_error_bad_value);
11933 htab->stub_error = true;
11934 return false;
11937 if (info->emitrelocations)
11939 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11940 if (r == NULL)
11941 return false;
11942 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11943 if (bfd_big_endian (info->output_bfd))
11944 r[0].r_offset += 2;
11945 if (stub_entry->type.r2save)
11946 r[0].r_offset += 4;
11947 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11948 r[0].r_addend = targ;
11949 if (PPC_HA (off) != 0)
11951 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11952 r[1].r_offset = r[0].r_offset + 4;
11953 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11954 r[1].r_addend = r[0].r_addend;
11958 p = loc;
11959 obfd = htab->params->stub_bfd;
11960 if (!stub_entry->type.r2save)
11962 if (PPC_HA (off) != 0)
11964 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11965 p += 4;
11966 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11968 else
11969 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11971 else
11973 bfd_vma r2off = get_r2off (info, stub_entry);
11975 if (r2off == (bfd_vma) -1)
11977 htab->stub_error = true;
11978 return false;
11981 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11982 p += 4;
11983 if (PPC_HA (off) != 0)
11985 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11986 p += 4;
11987 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11989 else
11990 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11992 if (PPC_HA (r2off) != 0)
11994 p += 4;
11995 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11997 if (PPC_LO (r2off) != 0)
11999 p += 4;
12000 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
12003 p += 4;
12004 bfd_put_32 (obfd, MTCTR_R12, p);
12005 p += 4;
12006 bfd_put_32 (obfd, BCTR, p);
12007 p += 4;
12009 else if (stub_entry->type.sub >= ppc_stub_notoc)
12011 bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
12012 p = loc;
12013 off = (stub_entry->stub_offset
12014 + stub_entry->group->stub_sec->output_offset
12015 + stub_entry->group->stub_sec->output_section->vma);
12016 obfd = htab->params->stub_bfd;
12017 is_tga = (is_plt
12018 && stub_entry->h != NULL
12019 && is_tls_get_addr (&stub_entry->h->elf, htab)
12020 && htab->params->tls_get_addr_opt);
12021 if (is_tga)
12023 p = build_tls_get_addr_head (htab, stub_entry, p);
12024 off += p - loc;
12026 if (stub_entry->type.r2save)
12028 off += 4;
12029 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
12030 p += 4;
12032 if (is_plt)
12034 targ = stub_entry->plt_ent->plt.offset & ~1;
12035 if (targ >= (bfd_vma) -2)
12036 abort ();
12038 plt = htab->elf.splt;
12039 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12041 if (stub_entry->symtype == STT_GNU_IFUNC)
12042 plt = htab->elf.iplt;
12043 else
12044 plt = htab->pltlocal;
12046 targ += plt->output_offset + plt->output_section->vma;
12048 else
12049 targ = (stub_entry->target_value
12050 + stub_entry->target_section->output_offset
12051 + stub_entry->target_section->output_section->vma);
12052 odd = off & 4;
12053 off = targ - off;
12055 relp = p;
12056 num_rel = 0;
12057 if (stub_entry->type.sub == ppc_stub_notoc)
12058 p = build_power10_offset (obfd, p, off, odd, is_plt);
12059 else
12061 if (htab->glink_eh_frame != NULL
12062 && htab->glink_eh_frame->size != 0)
12064 bfd_byte *base, *eh;
12065 unsigned int lr_used, delta;
12067 base = (htab->glink_eh_frame->contents
12068 + stub_entry->group->eh_base + 17);
12069 eh = base + stub_entry->group->eh_size;
12070 lr_used = stub_entry->stub_offset + (p - loc) + 8;
12071 delta = lr_used - stub_entry->group->lr_restore;
12072 stub_entry->group->lr_restore = lr_used + 8;
12073 eh = eh_advance (htab->elf.dynobj, eh, delta);
12074 *eh++ = DW_CFA_register;
12075 *eh++ = 65;
12076 *eh++ = 12;
12077 *eh++ = DW_CFA_advance_loc + 2;
12078 *eh++ = DW_CFA_restore_extended;
12079 *eh++ = 65;
12080 stub_entry->group->eh_size = eh - base;
12083 /* The notoc stubs calculate their target (either a PLT entry or
12084 the global entry point of a function) relative to the PC
12085 returned by the "bcl" two instructions past the start of the
12086 sequence emitted by build_offset. The offset is therefore 8
12087 less than calculated from the start of the sequence. */
12088 off -= 8;
12089 p = build_offset (obfd, p, off, is_plt);
12092 if (stub_entry->type.main == ppc_stub_long_branch)
12094 bfd_vma from;
12095 num_rel = 1;
12096 from = (stub_entry->stub_offset
12097 + stub_entry->group->stub_sec->output_offset
12098 + stub_entry->group->stub_sec->output_section->vma
12099 + (p - loc));
12100 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
12102 else
12104 bfd_put_32 (obfd, MTCTR_R12, p);
12105 p += 4;
12106 bfd_put_32 (obfd, BCTR, p);
12108 p += 4;
12110 if (is_tga)
12111 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12113 if (info->emitrelocations)
12115 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
12116 if (stub_entry->type.sub == ppc_stub_notoc)
12117 num_rel += num_relocs_for_power10_offset (off, odd);
12118 else
12120 num_rel += num_relocs_for_offset (off);
12121 roff += 16;
12123 r = get_relocs (stub_entry->group->stub_sec, num_rel);
12124 if (r == NULL)
12125 return false;
12126 if (stub_entry->type.sub == ppc_stub_notoc)
12127 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
12128 else
12129 r = emit_relocs_for_offset (info, r, roff, targ, off);
12130 if (stub_entry->type.main == ppc_stub_long_branch)
12132 ++r;
12133 roff = p - 4 - stub_entry->group->stub_sec->contents;
12134 r->r_offset = roff;
12135 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
12136 r->r_addend = targ;
12137 if (stub_entry->h != NULL
12138 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
12139 return false;
12143 else if (stub_entry->type.main == ppc_stub_plt_call)
12145 if (stub_entry->h != NULL
12146 && stub_entry->h->is_func_descriptor
12147 && stub_entry->h->oh != NULL)
12149 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
12151 /* If the old-ABI "dot-symbol" is undefined make it weak so
12152 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
12153 if (fh->elf.root.type == bfd_link_hash_undefined
12154 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
12155 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
12156 fh->elf.root.type = bfd_link_hash_undefweak;
12159 /* Now build the stub. */
12160 targ = stub_entry->plt_ent->plt.offset & ~1;
12161 if (targ >= (bfd_vma) -2)
12162 abort ();
12164 plt = htab->elf.splt;
12165 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12167 if (stub_entry->symtype == STT_GNU_IFUNC)
12168 plt = htab->elf.iplt;
12169 else
12170 plt = htab->pltlocal;
12172 targ += plt->output_offset + plt->output_section->vma;
12174 off = (elf_gp (info->output_bfd)
12175 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12176 off = targ - off;
12178 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
12180 info->callbacks->einfo
12181 /* xgettext:c-format */
12182 (_("%P: linkage table error against `%pT'\n"),
12183 stub_entry->h != NULL
12184 ? stub_entry->h->elf.root.root.string
12185 : "<local sym>");
12186 bfd_set_error (bfd_error_bad_value);
12187 htab->stub_error = true;
12188 return false;
12191 r = NULL;
12192 if (info->emitrelocations)
12194 r = get_relocs (stub_entry->group->stub_sec,
12195 ((PPC_HA (off) != 0)
12196 + (htab->opd_abi
12197 ? 2 + (htab->params->plt_static_chain
12198 && PPC_HA (off + 16) == PPC_HA (off))
12199 : 1)));
12200 if (r == NULL)
12201 return false;
12202 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
12203 if (bfd_big_endian (info->output_bfd))
12204 r[0].r_offset += 2;
12205 r[0].r_addend = targ;
12207 p = loc;
12208 obfd = htab->params->stub_bfd;
12209 is_tga = (stub_entry->h != NULL
12210 && is_tls_get_addr (&stub_entry->h->elf, htab)
12211 && htab->params->tls_get_addr_opt);
12212 if (is_tga)
12214 p = build_tls_get_addr_head (htab, stub_entry, p);
12215 if (r != NULL)
12216 r[0].r_offset += p - loc;
12218 p = build_plt_stub (htab, stub_entry, p, off, r);
12219 if (is_tga)
12220 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12222 else if (stub_entry->type.main == ppc_stub_save_res)
12223 return true;
12224 else
12226 BFD_FAIL ();
12227 return false;
12230 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
12232 if (htab->params->emit_stub_syms)
12234 struct elf_link_hash_entry *h;
12235 size_t len1, len2;
12236 char *name;
12237 const char *const stub_str[] = { "long_branch",
12238 "plt_branch",
12239 "plt_call" };
12241 len1 = strlen (stub_str[stub_entry->type.main - 1]);
12242 len2 = strlen (stub_entry->root.string);
12243 name = bfd_alloc (info->output_bfd, len1 + len2 + 2);
12244 if (name == NULL)
12245 return false;
12246 memcpy (name, stub_entry->root.string, 9);
12247 memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
12248 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
12249 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
12250 if (h == NULL)
12251 return false;
12252 if (h->root.type == bfd_link_hash_new)
12254 h->root.type = bfd_link_hash_defined;
12255 h->root.u.def.section = stub_entry->group->stub_sec;
12256 h->root.u.def.value = stub_entry->stub_offset;
12257 h->ref_regular = 1;
12258 h->def_regular = 1;
12259 h->ref_regular_nonweak = 1;
12260 h->forced_local = 1;
12261 h->non_elf = 0;
12262 h->root.linker_def = 1;
12266 return true;
12269 /* As above, but don't actually build the stub. Just bump offset so
12270 we know stub section sizes, and select plt_branch stubs where
12271 long_branch stubs won't do. */
12273 static bool
12274 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
12276 struct ppc_stub_hash_entry *stub_entry;
12277 struct bfd_link_info *info;
12278 struct ppc_link_hash_table *htab;
12279 asection *plt;
12280 bfd_vma targ, off, r2off;
12281 unsigned int size, pad, extra, lr_used, delta, odd;
12282 bfd_vma stub_offset;
12284 /* Massage our args to the form they really have. */
12285 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
12286 info = in_arg;
12288 htab = ppc_hash_table (info);
12289 if (htab == NULL)
12290 return false;
12292 /* Fail if the target section could not be assigned to an output
12293 section. The user should fix his linker script. */
12294 if (stub_entry->target_section != NULL
12295 && stub_entry->target_section->output_section == NULL
12296 && info->non_contiguous_regions)
12297 info->callbacks->einfo (_("%F%P: Could not assign `%pA' to an output section. "
12298 "Retry without --enable-non-contiguous-regions.\n"),
12299 stub_entry->target_section);
12301 /* Same for the group. */
12302 if (stub_entry->group->stub_sec != NULL
12303 && stub_entry->group->stub_sec->output_section == NULL
12304 && info->non_contiguous_regions)
12305 info->callbacks->einfo (_("%F%P: Could not assign `%pA' to an output section. "
12306 "Retry without --enable-non-contiguous-regions.\n"),
12307 stub_entry->group->stub_sec);
12309 /* Make a note of the offset within the stubs for this entry. */
12310 stub_offset = stub_entry->group->stub_sec->size;
12311 if (htab->stub_iteration > STUB_SHRINK_ITER
12312 && stub_entry->stub_offset > stub_offset)
12313 stub_offset = stub_entry->stub_offset;
12314 stub_entry->id = ++htab->stub_id;
12316 if (stub_entry->h != NULL
12317 && stub_entry->h->save_res
12318 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12319 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12321 /* Don't make stubs to out-of-line register save/restore
12322 functions. Instead, emit copies of the functions. */
12323 stub_entry->group->needs_save_res = 1;
12324 stub_entry->type.main = ppc_stub_save_res;
12325 stub_entry->type.sub = ppc_stub_toc;
12326 stub_entry->type.r2save = 0;
12327 return true;
12330 if (stub_entry->type.main == ppc_stub_plt_branch)
12332 /* Reset the stub type from the plt branch variant in case we now
12333 can reach with a shorter stub. */
12334 stub_entry->type.main = ppc_stub_long_branch;
12337 if (stub_entry->type.main == ppc_stub_long_branch
12338 && stub_entry->type.sub == ppc_stub_toc)
12340 targ = (stub_entry->target_value
12341 + stub_entry->target_section->output_offset
12342 + stub_entry->target_section->output_section->vma);
12343 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
12344 off = (stub_offset
12345 + stub_entry->group->stub_sec->output_offset
12346 + stub_entry->group->stub_sec->output_section->vma);
12348 size = 4;
12349 r2off = 0;
12350 if (stub_entry->type.r2save)
12352 r2off = get_r2off (info, stub_entry);
12353 if (r2off == (bfd_vma) -1)
12355 htab->stub_error = true;
12356 return false;
12358 size = 8;
12359 if (PPC_HA (r2off) != 0)
12360 size += 4;
12361 if (PPC_LO (r2off) != 0)
12362 size += 4;
12363 off += size - 4;
12365 off = targ - off;
12367 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12368 Do the same for -R objects without function descriptors. */
12369 if ((stub_entry->type.r2save
12370 && r2off == 0
12371 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12372 || off + (1 << 25) >= (bfd_vma) (1 << 26))
12374 struct ppc_branch_hash_entry *br_entry;
12376 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12377 stub_entry->root.string + 9,
12378 true, false);
12379 if (br_entry == NULL)
12381 _bfd_error_handler (_("can't build branch stub `%s'"),
12382 stub_entry->root.string);
12383 htab->stub_error = true;
12384 return false;
12387 if (br_entry->iter != htab->stub_iteration)
12389 br_entry->iter = htab->stub_iteration;
12390 br_entry->offset = htab->brlt->size;
12391 htab->brlt->size += 8;
12393 if (htab->relbrlt != NULL && !info->enable_dt_relr)
12394 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12395 else if (info->emitrelocations)
12397 htab->brlt->reloc_count += 1;
12398 htab->brlt->flags |= SEC_RELOC;
12402 targ = (br_entry->offset
12403 + htab->brlt->output_offset
12404 + htab->brlt->output_section->vma);
12405 off = (elf_gp (info->output_bfd)
12406 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12407 off = targ - off;
12409 if (info->emitrelocations)
12411 stub_entry->group->stub_sec->reloc_count
12412 += 1 + (PPC_HA (off) != 0);
12413 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12416 stub_entry->type.main = ppc_stub_plt_branch;
12417 if (!stub_entry->type.r2save)
12419 size = 12;
12420 if (PPC_HA (off) != 0)
12421 size = 16;
12423 else
12425 size = 16;
12426 if (PPC_HA (off) != 0)
12427 size += 4;
12429 if (PPC_HA (r2off) != 0)
12430 size += 4;
12431 if (PPC_LO (r2off) != 0)
12432 size += 4;
12434 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12435 stub_offset += pad;
12437 else if (info->emitrelocations)
12439 stub_entry->group->stub_sec->reloc_count += 1;
12440 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12443 else if (stub_entry->type.main == ppc_stub_long_branch)
12445 off = (stub_offset
12446 + stub_entry->group->stub_sec->output_offset
12447 + stub_entry->group->stub_sec->output_section->vma);
12448 size = 0;
12449 if (stub_entry->type.r2save)
12450 size = 4;
12451 off += size;
12452 targ = (stub_entry->target_value
12453 + stub_entry->target_section->output_offset
12454 + stub_entry->target_section->output_section->vma);
12455 odd = off & 4;
12456 off = targ - off;
12458 if (stub_entry->type.sub == ppc_stub_notoc)
12459 extra = size_power10_offset (off, odd);
12460 else
12461 extra = size_offset (off - 8);
12462 /* Include branch insn plus those in the offset sequence. */
12463 size += 4 + extra;
12465 /* If the branch can't reach, use a plt_branch.
12466 The branch insn is at the end, or "extra" bytes along. So
12467 its offset will be "extra" bytes less that that already
12468 calculated. */
12469 if (off - extra + (1 << 25) >= (bfd_vma) (1 << 26))
12471 stub_entry->type.main = ppc_stub_plt_branch;
12472 size += 4;
12473 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12474 if (pad != 0)
12476 stub_offset += pad;
12477 off -= pad;
12478 odd ^= pad & 4;
12479 size -= extra;
12480 if (stub_entry->type.sub == ppc_stub_notoc)
12481 extra = size_power10_offset (off, odd);
12482 else
12483 extra = size_offset (off - 8);
12484 size += extra;
12487 else if (info->emitrelocations)
12488 stub_entry->group->stub_sec->reloc_count +=1;
12490 if (info->emitrelocations)
12492 unsigned int num_rel;
12493 if (stub_entry->type.sub == ppc_stub_notoc)
12494 num_rel = num_relocs_for_power10_offset (off, odd);
12495 else
12496 num_rel = num_relocs_for_offset (off - 8);
12497 stub_entry->group->stub_sec->reloc_count += num_rel;
12498 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12501 if (stub_entry->type.sub != ppc_stub_notoc)
12503 /* After the bcl, lr has been modified so we need to emit
12504 .eh_frame info saying the return address is in r12. */
12505 lr_used = stub_offset + 8;
12506 if (stub_entry->type.r2save)
12507 lr_used += 4;
12508 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12509 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12510 DW_CFA_restore_extended 65. */
12511 delta = lr_used - stub_entry->group->lr_restore;
12512 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12513 stub_entry->group->lr_restore = lr_used + 8;
12516 else if (stub_entry->type.sub >= ppc_stub_notoc)
12518 BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
12519 lr_used = 0;
12520 if (stub_entry->h != NULL
12521 && is_tls_get_addr (&stub_entry->h->elf, htab)
12522 && htab->params->tls_get_addr_opt)
12524 lr_used += 7 * 4;
12525 if (!htab->params->no_tls_get_addr_regsave)
12526 lr_used += 11 * 4;
12527 else if (stub_entry->type.r2save)
12528 lr_used += 2 * 4;
12530 if (stub_entry->type.r2save)
12531 lr_used += 4;
12532 targ = stub_entry->plt_ent->plt.offset & ~1;
12533 if (targ >= (bfd_vma) -2)
12534 abort ();
12536 plt = htab->elf.splt;
12537 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12539 if (stub_entry->symtype == STT_GNU_IFUNC)
12540 plt = htab->elf.iplt;
12541 else
12542 plt = htab->pltlocal;
12544 targ += plt->output_offset + plt->output_section->vma;
12545 off = (stub_offset
12546 + stub_entry->group->stub_sec->output_offset
12547 + stub_entry->group->stub_sec->output_section->vma
12548 + lr_used);
12549 odd = off & 4;
12550 off = targ - off;
12552 size = plt_stub_size (htab, stub_entry, off, odd);
12553 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12554 if (pad != 0)
12556 stub_offset += pad;
12557 off -= pad;
12558 odd ^= pad & 4;
12559 size = plt_stub_size (htab, stub_entry, off, odd);
12562 if (info->emitrelocations)
12564 unsigned int num_rel;
12565 if (stub_entry->type.sub == ppc_stub_notoc)
12566 num_rel = num_relocs_for_power10_offset (off, odd);
12567 else
12568 num_rel = num_relocs_for_offset (off - 8);
12569 stub_entry->group->stub_sec->reloc_count += num_rel;
12570 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12573 if (stub_entry->type.sub != ppc_stub_notoc)
12575 /* After the bcl, lr has been modified so we need to emit
12576 .eh_frame info saying the return address is in r12. */
12577 lr_used += stub_offset + 8;
12578 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12579 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12580 DW_CFA_restore_extended 65. */
12581 delta = lr_used - stub_entry->group->lr_restore;
12582 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12583 stub_entry->group->lr_restore = lr_used + 8;
12585 if (stub_entry->h != NULL
12586 && is_tls_get_addr (&stub_entry->h->elf, htab)
12587 && htab->params->tls_get_addr_opt)
12589 if (!htab->params->no_tls_get_addr_regsave)
12591 unsigned int cfa_updt = stub_offset + 18 * 4;
12592 delta = cfa_updt - stub_entry->group->lr_restore;
12593 stub_entry->group->eh_size += eh_advance_size (delta);
12594 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12595 stub_entry->group->lr_restore = stub_offset + size - 4;
12597 else if (stub_entry->type.r2save)
12599 lr_used = stub_offset + size - 20;
12600 delta = lr_used - stub_entry->group->lr_restore;
12601 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12602 stub_entry->group->lr_restore = stub_offset + size - 4;
12606 else if (stub_entry->type.main == ppc_stub_plt_call)
12608 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12609 if (targ >= (bfd_vma) -2)
12610 abort ();
12611 plt = htab->elf.splt;
12612 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12614 if (stub_entry->symtype == STT_GNU_IFUNC)
12615 plt = htab->elf.iplt;
12616 else
12617 plt = htab->pltlocal;
12619 targ += plt->output_offset + plt->output_section->vma;
12621 off = (elf_gp (info->output_bfd)
12622 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12623 off = targ - off;
12625 size = plt_stub_size (htab, stub_entry, off, 0);
12626 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12627 stub_offset += pad;
12629 if (info->emitrelocations)
12631 stub_entry->group->stub_sec->reloc_count
12632 += ((PPC_HA (off) != 0)
12633 + (htab->opd_abi
12634 ? 2 + (htab->params->plt_static_chain
12635 && PPC_HA (off + 16) == PPC_HA (off))
12636 : 1));
12637 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12640 if (stub_entry->h != NULL
12641 && is_tls_get_addr (&stub_entry->h->elf, htab)
12642 && htab->params->tls_get_addr_opt
12643 && stub_entry->type.r2save)
12645 if (!htab->params->no_tls_get_addr_regsave)
12647 /* Adjustments to r1 need to be described. */
12648 unsigned int cfa_updt = stub_offset + 18 * 4;
12649 delta = cfa_updt - stub_entry->group->lr_restore;
12650 stub_entry->group->eh_size += eh_advance_size (delta);
12651 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12653 else
12655 lr_used = stub_offset + size - 20;
12656 /* The eh_frame info will consist of a DW_CFA_advance_loc
12657 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12658 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12659 delta = lr_used - stub_entry->group->lr_restore;
12660 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12662 stub_entry->group->lr_restore = stub_offset + size - 4;
12665 else
12667 BFD_FAIL ();
12668 return false;
12671 if (stub_entry->stub_offset != stub_offset)
12672 htab->stub_changed = true;
12673 stub_entry->stub_offset = stub_offset;
12674 stub_entry->group->stub_sec->size = stub_offset + size;
12675 return true;
12678 /* Set up various things so that we can make a list of input sections
12679 for each output section included in the link. Returns -1 on error,
12680 0 when no stubs will be needed, and 1 on success. */
12683 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
12685 unsigned int id;
12686 size_t amt;
12687 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12689 if (htab == NULL)
12690 return -1;
12692 /* The access to _bfd_section_id here is unlocked, so for the time
12693 being this function cannot be called in multi-threaded mode. */
12694 BFD_ASSERT (!_bfd_threading_enabled ());
12696 htab->sec_info_arr_size = _bfd_section_id;
12697 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12698 htab->sec_info = bfd_zalloc (info->output_bfd, amt);
12699 if (htab->sec_info == NULL)
12700 return -1;
12702 /* Set toc_off for com, und, abs and ind sections. */
12703 for (id = 0; id < 3; id++)
12704 htab->sec_info[id].toc_off = TOC_BASE_OFF;
12706 return 1;
12709 /* Set up for first pass at multitoc partitioning. */
12711 void
12712 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12714 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12716 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
12717 htab->toc_bfd = NULL;
12718 htab->toc_first_sec = NULL;
12721 /* The linker repeatedly calls this function for each TOC input section
12722 and linker generated GOT section. Group input bfds such that the toc
12723 within a group is less than 64k in size. */
12725 bool
12726 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
12728 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12729 bfd_vma addr, off, limit;
12731 if (htab == NULL)
12732 return false;
12734 if (!htab->second_toc_pass)
12736 /* Keep track of the first .toc or .got section for this input bfd. */
12737 bool new_bfd = htab->toc_bfd != isec->owner;
12739 if (new_bfd)
12741 htab->toc_bfd = isec->owner;
12742 htab->toc_first_sec = isec;
12745 addr = isec->output_offset + isec->output_section->vma;
12746 off = addr - htab->toc_curr;
12747 limit = 0x80008000;
12748 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12749 limit = 0x10000;
12750 if (off + isec->size > limit)
12752 addr = (htab->toc_first_sec->output_offset
12753 + htab->toc_first_sec->output_section->vma);
12754 htab->toc_curr = addr;
12755 htab->toc_curr &= -TOC_BASE_ALIGN;
12758 /* toc_curr is the base address of this toc group. Set elf_gp
12759 for the input section to be the offset relative to the
12760 output toc base plus 0x8000. Making the input elf_gp an
12761 offset allows us to move the toc as a whole without
12762 recalculating input elf_gp. */
12763 off = htab->toc_curr - elf_gp (info->output_bfd);
12764 off += TOC_BASE_OFF;
12766 /* Die if someone uses a linker script that doesn't keep input
12767 file .toc and .got together. */
12768 if (new_bfd
12769 && elf_gp (isec->owner) != 0
12770 && elf_gp (isec->owner) != off)
12771 return false;
12773 elf_gp (isec->owner) = off;
12774 return true;
12777 /* During the second pass toc_first_sec points to the start of
12778 a toc group, and toc_curr is used to track the old elf_gp.
12779 We use toc_bfd to ensure we only look at each bfd once. */
12780 if (htab->toc_bfd == isec->owner)
12781 return true;
12782 htab->toc_bfd = isec->owner;
12784 if (htab->toc_first_sec == NULL
12785 || htab->toc_curr != elf_gp (isec->owner))
12787 htab->toc_curr = elf_gp (isec->owner);
12788 htab->toc_first_sec = isec;
12790 addr = (htab->toc_first_sec->output_offset
12791 + htab->toc_first_sec->output_section->vma);
12792 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12793 elf_gp (isec->owner) = off;
12795 return true;
12798 /* Called via elf_link_hash_traverse to merge GOT entries for global
12799 symbol H. */
12801 static bool
12802 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12804 if (h->root.type == bfd_link_hash_indirect)
12805 return true;
12807 merge_got_entries (&h->got.glist);
12809 return true;
12812 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12813 symbol H. */
12815 static bool
12816 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12818 struct got_entry *gent;
12820 if (h->root.type == bfd_link_hash_indirect)
12821 return true;
12823 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12824 if (!gent->is_indirect)
12825 allocate_got (h, (struct bfd_link_info *) inf, gent);
12826 return true;
12829 /* Called on the first multitoc pass after the last call to
12830 ppc64_elf_next_toc_section. This function removes duplicate GOT
12831 entries. */
12833 bool
12834 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12836 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12837 struct bfd *ibfd, *ibfd2;
12838 bool done_something;
12840 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12842 if (!htab->do_multi_toc)
12843 return false;
12845 /* Merge global sym got entries within a toc group. */
12846 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12848 /* And tlsld_got. */
12849 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12851 struct got_entry *ent, *ent2;
12853 if (!is_ppc64_elf (ibfd))
12854 continue;
12856 ent = ppc64_tlsld_got (ibfd);
12857 if (!ent->is_indirect
12858 && ent->got.offset != (bfd_vma) -1)
12860 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12862 if (!is_ppc64_elf (ibfd2))
12863 continue;
12865 ent2 = ppc64_tlsld_got (ibfd2);
12866 if (!ent2->is_indirect
12867 && ent2->got.offset != (bfd_vma) -1
12868 && elf_gp (ibfd2) == elf_gp (ibfd))
12870 ent2->is_indirect = true;
12871 ent2->got.ent = ent;
12877 /* Zap sizes of got sections. */
12878 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12879 htab->elf.irelplt->size -= htab->got_reli_size;
12880 htab->got_reli_size = 0;
12882 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12884 asection *got, *relgot;
12886 if (!is_ppc64_elf (ibfd))
12887 continue;
12889 got = ppc64_elf_tdata (ibfd)->got;
12890 if (got != NULL)
12892 got->rawsize = got->size;
12893 got->size = 0;
12894 relgot = ppc64_elf_tdata (ibfd)->relgot;
12895 relgot->rawsize = relgot->size;
12896 relgot->size = 0;
12900 /* Now reallocate the got, local syms first. We don't need to
12901 allocate section contents again since we never increase size. */
12902 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12904 struct got_entry **lgot_ents;
12905 struct got_entry **end_lgot_ents;
12906 struct plt_entry **local_plt;
12907 struct plt_entry **end_local_plt;
12908 unsigned char *lgot_masks;
12909 bfd_size_type locsymcount;
12910 Elf_Internal_Shdr *symtab_hdr;
12911 asection *s;
12912 Elf_Internal_Sym *local_syms;
12913 Elf_Internal_Sym *isym;
12915 if (!is_ppc64_elf (ibfd))
12916 continue;
12918 lgot_ents = elf_local_got_ents (ibfd);
12919 if (!lgot_ents)
12920 continue;
12922 symtab_hdr = &elf_symtab_hdr (ibfd);
12923 locsymcount = symtab_hdr->sh_info;
12924 end_lgot_ents = lgot_ents + locsymcount;
12925 local_plt = (struct plt_entry **) end_lgot_ents;
12926 end_local_plt = local_plt + locsymcount;
12927 lgot_masks = (unsigned char *) end_local_plt;
12928 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
12929 if (local_syms == NULL && locsymcount != 0)
12931 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
12932 0, NULL, NULL, NULL);
12933 if (local_syms == NULL)
12934 return false;
12936 s = ppc64_elf_tdata (ibfd)->got;
12937 for (isym = local_syms;
12938 lgot_ents < end_lgot_ents;
12939 ++lgot_ents, ++lgot_masks, isym++)
12941 struct got_entry *ent;
12943 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12945 unsigned int ent_size = 8;
12946 unsigned int rel_size = sizeof (Elf64_External_Rela);
12948 ent->got.offset = s->size;
12949 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12951 ent_size *= 2;
12952 rel_size *= 2;
12954 s->size += ent_size;
12955 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12957 htab->elf.irelplt->size += rel_size;
12958 htab->got_reli_size += rel_size;
12960 else if (bfd_link_pic (info)
12961 && (ent->tls_type == 0
12962 ? !info->enable_dt_relr
12963 : !bfd_link_executable (info))
12964 && isym->st_shndx != SHN_ABS)
12966 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12967 srel->size += rel_size;
12973 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12975 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12977 struct got_entry *ent;
12979 if (!is_ppc64_elf (ibfd))
12980 continue;
12982 ent = ppc64_tlsld_got (ibfd);
12983 if (!ent->is_indirect
12984 && ent->got.offset != (bfd_vma) -1)
12986 asection *s = ppc64_elf_tdata (ibfd)->got;
12987 ent->got.offset = s->size;
12988 s->size += 16;
12989 if (bfd_link_dll (info))
12991 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12992 srel->size += sizeof (Elf64_External_Rela);
12997 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12998 if (!done_something)
12999 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13001 asection *got;
13003 if (!is_ppc64_elf (ibfd))
13004 continue;
13006 got = ppc64_elf_tdata (ibfd)->got;
13007 if (got != NULL)
13009 done_something = got->rawsize != got->size;
13010 if (done_something)
13011 break;
13015 if (done_something)
13016 (*htab->params->layout_sections_again) ();
13018 /* Set up for second pass over toc sections to recalculate elf_gp
13019 on input sections. */
13020 htab->toc_bfd = NULL;
13021 htab->toc_first_sec = NULL;
13022 htab->second_toc_pass = true;
13023 return done_something;
13026 /* Called after second pass of multitoc partitioning. */
13028 void
13029 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
13031 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13033 /* After the second pass, toc_curr tracks the TOC offset used
13034 for code sections below in ppc64_elf_next_input_section. */
13035 htab->toc_curr = TOC_BASE_OFF;
13038 /* No toc references were found in ISEC. If the code in ISEC makes no
13039 calls, then there's no need to use toc adjusting stubs when branching
13040 into ISEC. Actually, indirect calls from ISEC are OK as they will
13041 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
13042 needed, and 2 if a cyclical call-graph was found but no other reason
13043 for a stub was detected. If called from the top level, a return of
13044 2 means the same as a return of 0. */
13046 static int
13047 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
13049 int ret;
13051 /* Mark this section as checked. */
13052 isec->call_check_done = 1;
13054 /* We know none of our code bearing sections will need toc stubs. */
13055 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13056 return 0;
13058 if (isec->size == 0)
13059 return 0;
13061 if (isec->output_section == NULL)
13062 return 0;
13064 ret = 0;
13065 if (isec->reloc_count != 0)
13067 Elf_Internal_Rela *relstart, *rel;
13068 Elf_Internal_Sym *local_syms;
13069 struct ppc_link_hash_table *htab;
13071 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
13072 info->keep_memory);
13073 if (relstart == NULL)
13074 return -1;
13076 /* Look for branches to outside of this section. */
13077 local_syms = NULL;
13078 htab = ppc_hash_table (info);
13079 if (htab == NULL)
13080 return -1;
13082 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
13084 enum elf_ppc64_reloc_type r_type;
13085 unsigned long r_symndx;
13086 struct elf_link_hash_entry *h;
13087 struct ppc_link_hash_entry *eh;
13088 Elf_Internal_Sym *sym;
13089 asection *sym_sec;
13090 struct _opd_sec_data *opd;
13091 bfd_vma sym_value;
13092 bfd_vma dest;
13094 r_type = ELF64_R_TYPE (rel->r_info);
13095 if (r_type != R_PPC64_REL24
13096 && r_type != R_PPC64_REL24_NOTOC
13097 && r_type != R_PPC64_REL24_P9NOTOC
13098 && r_type != R_PPC64_REL14
13099 && r_type != R_PPC64_REL14_BRTAKEN
13100 && r_type != R_PPC64_REL14_BRNTAKEN
13101 && r_type != R_PPC64_PLTCALL
13102 && r_type != R_PPC64_PLTCALL_NOTOC)
13103 continue;
13105 r_symndx = ELF64_R_SYM (rel->r_info);
13106 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
13107 isec->owner))
13109 ret = -1;
13110 break;
13113 /* Calls to dynamic lib functions go through a plt call stub
13114 that uses r2. */
13115 eh = ppc_elf_hash_entry (h);
13116 if (eh != NULL
13117 && (eh->elf.plt.plist != NULL
13118 || (eh->oh != NULL
13119 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
13121 ret = 1;
13122 break;
13125 if (sym_sec == NULL)
13126 /* Ignore other undefined symbols. */
13127 continue;
13129 /* Assume branches to other sections not included in the
13130 link need stubs too, to cover -R and absolute syms. */
13131 if (sym_sec->output_section == NULL)
13133 ret = 1;
13134 break;
13137 if (h == NULL)
13138 sym_value = sym->st_value;
13139 else
13141 if (h->root.type != bfd_link_hash_defined
13142 && h->root.type != bfd_link_hash_defweak)
13143 abort ();
13144 sym_value = h->root.u.def.value;
13146 sym_value += rel->r_addend;
13148 /* If this branch reloc uses an opd sym, find the code section. */
13149 opd = get_opd_info (sym_sec);
13150 if (opd != NULL)
13152 if (h == NULL && opd->adjust != NULL)
13154 long adjust;
13156 adjust = opd->adjust[OPD_NDX (sym_value)];
13157 if (adjust == -1)
13158 /* Assume deleted functions won't ever be called. */
13159 continue;
13160 sym_value += adjust;
13163 dest = opd_entry_value (sym_sec, sym_value,
13164 &sym_sec, NULL, false);
13165 if (dest == (bfd_vma) -1)
13166 continue;
13168 else
13169 dest = (sym_value
13170 + sym_sec->output_offset
13171 + sym_sec->output_section->vma);
13173 /* Ignore branch to self. */
13174 if (sym_sec == isec)
13175 continue;
13177 /* If the called function uses the toc, we need a stub. */
13178 if (sym_sec->has_toc_reloc
13179 || sym_sec->makes_toc_func_call)
13181 ret = 1;
13182 break;
13185 /* Assume any branch that needs a long branch stub might in fact
13186 need a plt_branch stub. A plt_branch stub uses r2. */
13187 else if (dest - (isec->output_offset
13188 + isec->output_section->vma
13189 + rel->r_offset) + (1 << 25)
13190 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
13191 ? h->other
13192 : sym->st_other))
13194 ret = 1;
13195 break;
13198 /* If calling back to a section in the process of being
13199 tested, we can't say for sure that no toc adjusting stubs
13200 are needed, so don't return zero. */
13201 else if (sym_sec->call_check_in_progress)
13202 ret = 2;
13204 /* Branches to another section that itself doesn't have any TOC
13205 references are OK. Recursively call ourselves to check. */
13206 else if (!sym_sec->call_check_done)
13208 int recur;
13210 /* Mark current section as indeterminate, so that other
13211 sections that call back to current won't be marked as
13212 known. */
13213 isec->call_check_in_progress = 1;
13214 recur = toc_adjusting_stub_needed (info, sym_sec);
13215 isec->call_check_in_progress = 0;
13217 if (recur != 0)
13219 ret = recur;
13220 if (recur != 2)
13221 break;
13226 if (elf_symtab_hdr (isec->owner).contents
13227 != (unsigned char *) local_syms)
13228 free (local_syms);
13229 if (elf_section_data (isec)->relocs != relstart)
13230 free (relstart);
13233 if ((ret & 1) == 0
13234 && isec->map_head.s != NULL
13235 && (strcmp (isec->output_section->name, ".init") == 0
13236 || strcmp (isec->output_section->name, ".fini") == 0))
13238 if (isec->map_head.s->has_toc_reloc
13239 || isec->map_head.s->makes_toc_func_call)
13240 ret = 1;
13241 else if (!isec->map_head.s->call_check_done)
13243 int recur;
13244 isec->call_check_in_progress = 1;
13245 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
13246 isec->call_check_in_progress = 0;
13247 if (recur != 0)
13248 ret = recur;
13252 if (ret == 1)
13253 isec->makes_toc_func_call = 1;
13255 return ret;
13258 /* The linker repeatedly calls this function for each input section,
13259 in the order that input sections are linked into output sections.
13260 Build lists of input sections to determine groupings between which
13261 we may insert linker stubs. */
13263 bool
13264 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
13266 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13268 if (htab == NULL)
13269 return false;
13271 if ((isec->output_section->flags & SEC_CODE) != 0
13272 && isec->output_section->id < htab->sec_info_arr_size)
13274 /* This happens to make the list in reverse order,
13275 which is what we want. */
13276 htab->sec_info[isec->id].u.list
13277 = htab->sec_info[isec->output_section->id].u.list;
13278 htab->sec_info[isec->output_section->id].u.list = isec;
13281 if (htab->multi_toc_needed)
13283 /* Analyse sections that aren't already flagged as needing a
13284 valid toc pointer. Exclude .fixup for the linux kernel.
13285 .fixup contains branches, but only back to the function that
13286 hit an exception. */
13287 if (!(isec->has_toc_reloc
13288 || (isec->flags & SEC_CODE) == 0
13289 || strcmp (isec->name, ".fixup") == 0
13290 || isec->call_check_done))
13292 if (toc_adjusting_stub_needed (info, isec) < 0)
13293 return false;
13295 /* Make all sections use the TOC assigned for this object file.
13296 This will be wrong for pasted sections; We fix that in
13297 check_pasted_section(). */
13298 if (elf_gp (isec->owner) != 0)
13299 htab->toc_curr = elf_gp (isec->owner);
13302 htab->sec_info[isec->id].toc_off = htab->toc_curr;
13303 return true;
13306 /* Check that all .init and .fini sections use the same toc, if they
13307 have toc relocs. */
13309 static bool
13310 check_pasted_section (struct bfd_link_info *info, const char *name)
13312 asection *o = bfd_get_section_by_name (info->output_bfd, name);
13314 if (o != NULL)
13316 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13317 bfd_vma toc_off = 0;
13318 asection *i;
13320 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13321 if (i->has_toc_reloc)
13323 if (toc_off == 0)
13324 toc_off = htab->sec_info[i->id].toc_off;
13325 else if (toc_off != htab->sec_info[i->id].toc_off)
13326 return false;
13329 if (toc_off == 0)
13330 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13331 if (i->makes_toc_func_call)
13333 toc_off = htab->sec_info[i->id].toc_off;
13334 break;
13337 /* Make sure the whole pasted function uses the same toc offset. */
13338 if (toc_off != 0)
13339 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13340 htab->sec_info[i->id].toc_off = toc_off;
13342 return true;
13345 bool
13346 ppc64_elf_check_init_fini (struct bfd_link_info *info)
13348 bool ret1 = check_pasted_section (info, ".init");
13349 bool ret2 = check_pasted_section (info, ".fini");
13351 return ret1 && ret2;
13354 /* See whether we can group stub sections together. Grouping stub
13355 sections may result in fewer stubs. More importantly, we need to
13356 put all .init* and .fini* stubs at the beginning of the .init or
13357 .fini output sections respectively, because glibc splits the
13358 _init and _fini functions into multiple parts. Putting a stub in
13359 the middle of a function is not a good idea. */
13361 static bool
13362 group_sections (struct bfd_link_info *info,
13363 bfd_size_type stub_group_size,
13364 bool stubs_always_before_branch)
13366 struct ppc_link_hash_table *htab;
13367 asection *osec;
13368 bool suppress_size_errors;
13370 htab = ppc_hash_table (info);
13371 if (htab == NULL)
13372 return false;
13374 suppress_size_errors = false;
13375 if (stub_group_size == 1)
13377 /* Default values. */
13378 if (stubs_always_before_branch)
13379 stub_group_size = 0x1e00000;
13380 else
13381 stub_group_size = 0x1c00000;
13382 suppress_size_errors = true;
13385 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
13387 asection *tail;
13389 if (osec->id >= htab->sec_info_arr_size)
13390 continue;
13392 tail = htab->sec_info[osec->id].u.list;
13393 while (tail != NULL)
13395 asection *curr;
13396 asection *prev;
13397 bfd_size_type total;
13398 bool big_sec;
13399 bfd_vma curr_toc;
13400 struct map_stub *group;
13401 bfd_size_type group_size;
13403 curr = tail;
13404 total = tail->size;
13405 group_size = (ppc64_elf_section_data (tail) != NULL
13406 && ppc64_elf_section_data (tail)->has_14bit_branch
13407 ? stub_group_size >> 10 : stub_group_size);
13409 big_sec = total > group_size;
13410 if (big_sec && !suppress_size_errors)
13411 /* xgettext:c-format */
13412 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
13413 tail->owner, tail);
13414 curr_toc = htab->sec_info[tail->id].toc_off;
13416 while ((prev = htab->sec_info[curr->id].u.list) != NULL
13417 && ((total += curr->output_offset - prev->output_offset)
13418 < (ppc64_elf_section_data (prev) != NULL
13419 && ppc64_elf_section_data (prev)->has_14bit_branch
13420 ? (group_size = stub_group_size >> 10) : group_size))
13421 && htab->sec_info[prev->id].toc_off == curr_toc)
13422 curr = prev;
13424 /* OK, the size from the start of CURR to the end is less
13425 than group_size and thus can be handled by one stub
13426 section. (or the tail section is itself larger than
13427 group_size, in which case we may be toast.) We should
13428 really be keeping track of the total size of stubs added
13429 here, as stubs contribute to the final output section
13430 size. That's a little tricky, and this way will only
13431 break if stubs added make the total size more than 2^25,
13432 ie. for the default stub_group_size, if stubs total more
13433 than 2097152 bytes, or nearly 75000 plt call stubs. */
13434 group = bfd_alloc (curr->owner, sizeof (*group));
13435 if (group == NULL)
13436 return false;
13437 group->link_sec = curr;
13438 group->stub_sec = NULL;
13439 group->needs_save_res = 0;
13440 group->lr_restore = 0;
13441 group->eh_size = 0;
13442 group->eh_base = 0;
13443 group->next = htab->group;
13444 htab->group = group;
13447 prev = htab->sec_info[tail->id].u.list;
13448 /* Set up this stub group. */
13449 htab->sec_info[tail->id].u.group = group;
13451 while (tail != curr && (tail = prev) != NULL);
13453 /* But wait, there's more! Input sections up to group_size
13454 bytes before the stub section can be handled by it too.
13455 Don't do this if we have a really large section after the
13456 stubs, as adding more stubs increases the chance that
13457 branches may not reach into the stub section. */
13458 if (!stubs_always_before_branch && !big_sec)
13460 total = 0;
13461 while (prev != NULL
13462 && ((total += tail->output_offset - prev->output_offset)
13463 < (ppc64_elf_section_data (prev) != NULL
13464 && ppc64_elf_section_data (prev)->has_14bit_branch
13465 ? (group_size = stub_group_size >> 10)
13466 : group_size))
13467 && htab->sec_info[prev->id].toc_off == curr_toc)
13469 tail = prev;
13470 prev = htab->sec_info[tail->id].u.list;
13471 htab->sec_info[tail->id].u.group = group;
13474 tail = prev;
13477 return true;
13480 static const unsigned char glink_eh_frame_cie[] =
13482 0, 0, 0, 16, /* length. */
13483 0, 0, 0, 0, /* id. */
13484 1, /* CIE version. */
13485 'z', 'R', 0, /* Augmentation string. */
13486 4, /* Code alignment. */
13487 0x78, /* Data alignment. */
13488 65, /* RA reg. */
13489 1, /* Augmentation size. */
13490 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
13491 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
13494 /* Stripping output sections is normally done before dynamic section
13495 symbols have been allocated. This function is called later, and
13496 handles cases like htab->brlt which is mapped to its own output
13497 section. */
13499 static void
13500 maybe_strip_output (struct bfd_link_info *info, asection *isec)
13502 if (isec->size == 0
13503 && isec->output_section->size == 0
13504 && !(isec->output_section->flags & SEC_KEEP)
13505 && !bfd_section_removed_from_list (info->output_bfd,
13506 isec->output_section)
13507 && elf_section_data (isec->output_section)->dynindx == 0)
13509 isec->output_section->flags |= SEC_EXCLUDE;
13510 bfd_section_list_remove (info->output_bfd, isec->output_section);
13511 info->output_bfd->section_count--;
13515 /* Stash R_PPC64_RELATIVE reloc at input section SEC, r_offset OFF to
13516 the array of such relocs. */
13518 static bool
13519 append_relr_off (struct ppc_link_hash_table *htab, asection *sec, bfd_vma off)
13521 if (htab->relr_count >= htab->relr_alloc)
13523 if (htab->relr_alloc == 0)
13524 htab->relr_alloc = 4096;
13525 else
13526 htab->relr_alloc *= 2;
13527 htab->relr = bfd_realloc (htab->relr,
13528 htab->relr_alloc * sizeof (*htab->relr));
13529 if (htab->relr == NULL)
13530 return false;
13532 htab->relr[htab->relr_count].sec = sec;
13533 htab->relr[htab->relr_count].off = off;
13534 htab->relr_count++;
13535 return true;
13538 /* qsort comparator for bfd_vma args. */
13540 static int
13541 compare_relr_address (const void *arg1, const void *arg2)
13543 bfd_vma a = *(bfd_vma *) arg1;
13544 bfd_vma b = *(bfd_vma *) arg2;
13545 return a < b ? -1 : a > b ? 1 : 0;
13548 /* Produce a malloc'd sorted array of reloc addresses from the info
13549 stored by append_relr_off. */
13551 static bfd_vma *
13552 sort_relr (struct ppc_link_hash_table *htab)
13554 bfd_vma *addr = bfd_malloc (htab->relr_count * sizeof (*addr));
13555 if (addr == NULL)
13556 return NULL;
13558 for (size_t i = 0; i < htab->relr_count; i++)
13559 addr[i] = (htab->relr[i].sec->output_section->vma
13560 + htab->relr[i].sec->output_offset
13561 + htab->relr[i].off);
13563 if (htab->relr_count > 1)
13564 qsort (addr, htab->relr_count, sizeof (*addr), compare_relr_address);
13566 return addr;
13569 /* Look over GOT and PLT entries saved on elf_local_got_ents for all
13570 input files, stashing info about needed relative relocs. */
13572 static bool
13573 got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
13575 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13576 bfd *ibfd;
13578 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13580 struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
13581 struct plt_entry **local_plt, **lplt, **end_local_plt;
13582 Elf_Internal_Shdr *symtab_hdr;
13583 bfd_size_type locsymcount;
13584 Elf_Internal_Sym *local_syms;
13585 Elf_Internal_Sym *isym;
13586 struct plt_entry *pent;
13587 struct got_entry *gent;
13589 if (!is_ppc64_elf (ibfd))
13590 continue;
13592 lgot_ents = elf_local_got_ents (ibfd);
13593 if (!lgot_ents)
13594 continue;
13596 symtab_hdr = &elf_symtab_hdr (ibfd);
13597 locsymcount = symtab_hdr->sh_info;
13598 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
13599 if (local_syms == NULL && locsymcount != 0)
13601 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
13602 0, NULL, NULL, NULL);
13603 if (local_syms == NULL)
13604 return false;
13606 end_lgot_ents = lgot_ents + locsymcount;
13607 local_plt = (struct plt_entry **) end_lgot_ents;
13608 end_local_plt = local_plt + locsymcount;
13609 for (lgot = lgot_ents, isym = local_syms;
13610 lgot < end_lgot_ents;
13611 ++lgot, ++isym)
13612 for (gent = *lgot; gent != NULL; gent = gent->next)
13613 if (!gent->is_indirect
13614 && gent->tls_type == 0
13615 && gent->got.offset != (bfd_vma) -1
13616 && isym->st_shndx != SHN_ABS)
13618 asection *got = ppc64_elf_tdata (gent->owner)->got;
13619 if (!append_relr_off (htab, got, gent->got.offset))
13621 htab->stub_error = true;
13622 return false;
13626 if (!htab->opd_abi)
13627 for (lplt = local_plt, isym = local_syms;
13628 lplt < end_local_plt;
13629 ++lplt, ++isym)
13630 for (pent = *lplt; pent != NULL; pent = pent->next)
13631 if (pent->plt.offset != (bfd_vma) -1
13632 && ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC)
13634 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13636 if (symtab_hdr->contents != (unsigned char *) local_syms)
13637 free (local_syms);
13638 return false;
13642 if (local_syms != NULL
13643 && symtab_hdr->contents != (unsigned char *) local_syms)
13645 if (!info->keep_memory)
13646 free (local_syms);
13647 else
13648 symtab_hdr->contents = (unsigned char *) local_syms;
13651 return true;
13654 /* Stash info about needed GOT and PLT entry relative relocs for
13655 global symbol H. */
13657 static bool
13658 got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
13660 struct bfd_link_info *info;
13661 struct ppc_link_hash_table *htab;
13662 struct plt_entry *pent;
13663 struct got_entry *gent;
13665 if (h->root.type == bfd_link_hash_indirect)
13666 return true;
13668 info = (struct bfd_link_info *) inf;
13669 htab = ppc_hash_table (info);
13670 if (htab == NULL)
13671 return false;
13673 if (h->type != STT_GNU_IFUNC
13674 && h->def_regular
13675 && (h->root.type == bfd_link_hash_defined
13676 || h->root.type == bfd_link_hash_defweak))
13678 if ((!htab->elf.dynamic_sections_created
13679 || h->dynindx == -1
13680 || SYMBOL_REFERENCES_LOCAL (info, h))
13681 && !bfd_is_abs_symbol (&h->root))
13682 for (gent = h->got.glist; gent != NULL; gent = gent->next)
13683 if (!gent->is_indirect
13684 && gent->tls_type == 0
13685 && gent->got.offset != (bfd_vma) -1)
13687 asection *got = ppc64_elf_tdata (gent->owner)->got;
13688 if (!append_relr_off (htab, got, gent->got.offset))
13690 htab->stub_error = true;
13691 return false;
13695 if (!htab->opd_abi
13696 && use_local_plt (info, h))
13697 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13698 if (pent->plt.offset != (bfd_vma) -1)
13700 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13702 htab->stub_error = true;
13703 return false;
13707 return true;
13710 /* Determine and set the size of the stub section for a final link.
13712 The basic idea here is to examine all the relocations looking for
13713 PC-relative calls to a target that is unreachable with a "bl"
13714 instruction. */
13716 bool
13717 ppc64_elf_size_stubs (struct bfd_link_info *info)
13719 bfd_size_type stub_group_size;
13720 bool stubs_always_before_branch;
13721 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13723 if (htab == NULL)
13724 return false;
13726 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13727 htab->params->power10_stubs = 0;
13729 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
13730 htab->params->plt_thread_safe = 1;
13731 if (!htab->opd_abi)
13732 htab->params->plt_thread_safe = 0;
13733 else if (htab->params->plt_thread_safe == -1)
13735 static const char *const thread_starter[] =
13737 "pthread_create",
13738 /* libstdc++ */
13739 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13740 /* librt */
13741 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13742 "mq_notify", "create_timer",
13743 /* libanl */
13744 "getaddrinfo_a",
13745 /* libgomp */
13746 "GOMP_parallel",
13747 "GOMP_parallel_start",
13748 "GOMP_parallel_loop_static",
13749 "GOMP_parallel_loop_static_start",
13750 "GOMP_parallel_loop_dynamic",
13751 "GOMP_parallel_loop_dynamic_start",
13752 "GOMP_parallel_loop_guided",
13753 "GOMP_parallel_loop_guided_start",
13754 "GOMP_parallel_loop_runtime",
13755 "GOMP_parallel_loop_runtime_start",
13756 "GOMP_parallel_sections",
13757 "GOMP_parallel_sections_start",
13758 /* libgo */
13759 "__go_go",
13761 unsigned i;
13763 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
13765 struct elf_link_hash_entry *h;
13766 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13767 false, false, true);
13768 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13769 if (htab->params->plt_thread_safe)
13770 break;
13773 stubs_always_before_branch = htab->params->group_size < 0;
13774 if (htab->params->group_size < 0)
13775 stub_group_size = -htab->params->group_size;
13776 else
13777 stub_group_size = htab->params->group_size;
13779 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13780 return false;
13782 htab->tga_group = NULL;
13783 if (!htab->params->no_tls_get_addr_regsave
13784 && htab->tga_desc_fd != NULL
13785 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13786 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13787 && htab->tls_get_addr_fd != NULL
13788 && is_static_defined (&htab->tls_get_addr_fd->elf))
13790 asection *sym_sec, *code_sec, *stub_sec;
13791 bfd_vma sym_value;
13792 struct _opd_sec_data *opd;
13794 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13795 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13796 code_sec = sym_sec;
13797 opd = get_opd_info (sym_sec);
13798 if (opd != NULL)
13799 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
13800 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13801 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13802 htab->tga_group->link_sec);
13803 if (stub_sec == NULL)
13804 return false;
13805 htab->tga_group->stub_sec = stub_sec;
13807 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13808 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13809 htab->tga_desc_fd->elf.root.u.def.value = 0;
13810 htab->tga_desc_fd->elf.type = STT_FUNC;
13811 htab->tga_desc_fd->elf.def_regular = 1;
13812 htab->tga_desc_fd->elf.non_elf = 0;
13813 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
13816 /* Loop until no stubs added. After iteration 20 of this loop we may
13817 exit on a stub section shrinking. */
13819 while (1)
13821 bfd *input_bfd;
13822 unsigned int bfd_indx;
13823 struct map_stub *group;
13825 htab->stub_iteration += 1;
13826 htab->relr_count = 0;
13828 for (input_bfd = info->input_bfds, bfd_indx = 0;
13829 input_bfd != NULL;
13830 input_bfd = input_bfd->link.next, bfd_indx++)
13832 Elf_Internal_Shdr *symtab_hdr;
13833 asection *section;
13834 Elf_Internal_Sym *local_syms = NULL;
13836 if (!is_ppc64_elf (input_bfd))
13837 continue;
13839 /* We'll need the symbol table in a second. */
13840 symtab_hdr = &elf_symtab_hdr (input_bfd);
13841 if (symtab_hdr->sh_info == 0)
13842 continue;
13844 /* Walk over each section attached to the input bfd. */
13845 for (section = input_bfd->sections;
13846 section != NULL;
13847 section = section->next)
13849 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13850 bool is_opd;
13852 /* If there aren't any relocs, then there's nothing more
13853 to do. */
13854 if ((section->flags & SEC_RELOC) == 0
13855 || (section->flags & SEC_ALLOC) == 0
13856 || (section->flags & SEC_LOAD) == 0
13857 || section->reloc_count == 0)
13858 continue;
13860 if (!info->enable_dt_relr
13861 && (section->flags & SEC_CODE) == 0)
13862 continue;
13864 /* If this section is a link-once section that will be
13865 discarded, then don't create any stubs. */
13866 if (section->output_section == NULL
13867 || section->output_section->owner != info->output_bfd)
13868 continue;
13870 /* Get the relocs. */
13871 internal_relocs
13872 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13873 info->keep_memory);
13874 if (internal_relocs == NULL)
13875 goto error_ret_free_local;
13877 is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
13879 /* Now examine each relocation. */
13880 irela = internal_relocs;
13881 irelaend = irela + section->reloc_count;
13882 for (; irela < irelaend; irela++)
13884 enum elf_ppc64_reloc_type r_type;
13885 unsigned int r_indx;
13886 struct ppc_stub_type stub_type;
13887 struct ppc_stub_hash_entry *stub_entry;
13888 asection *sym_sec, *code_sec;
13889 bfd_vma sym_value, code_value;
13890 bfd_vma destination;
13891 unsigned long local_off;
13892 bool ok_dest;
13893 struct ppc_link_hash_entry *hash;
13894 struct ppc_link_hash_entry *fdh;
13895 struct elf_link_hash_entry *h;
13896 Elf_Internal_Sym *sym;
13897 char *stub_name;
13898 const asection *id_sec;
13899 struct _opd_sec_data *opd;
13900 struct plt_entry *plt_ent;
13902 r_type = ELF64_R_TYPE (irela->r_info);
13903 r_indx = ELF64_R_SYM (irela->r_info);
13905 if (r_type >= R_PPC64_max)
13907 bfd_set_error (bfd_error_bad_value);
13908 goto error_ret_free_internal;
13911 /* Only look for stubs on branch instructions. */
13912 switch (r_type)
13914 default:
13915 if (info->enable_dt_relr
13916 && maybe_relr (r_type, irela, section))
13917 break;
13918 continue;
13920 case R_PPC64_REL24:
13921 case R_PPC64_REL24_NOTOC:
13922 case R_PPC64_REL24_P9NOTOC:
13923 case R_PPC64_REL14:
13924 case R_PPC64_REL14_BRTAKEN:
13925 case R_PPC64_REL14_BRNTAKEN:
13926 if ((section->flags & SEC_CODE) != 0)
13927 break;
13928 continue;
13931 /* Now determine the call target, its name, value,
13932 section. */
13933 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13934 r_indx, input_bfd))
13935 goto error_ret_free_internal;
13937 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13939 /* Only locally defined symbols can possibly use
13940 relative relocations. */
13941 bfd_vma r_offset;
13942 if ((sym_sec == NULL
13943 || sym_sec->output_section == NULL)
13944 /* No symbol is OK too. */
13945 && !(sym != NULL && sym->st_shndx == 0)
13946 /* Hack for __ehdr_start, which is undefined
13947 at this point. */
13948 && !(h != NULL && h->root.linker_def))
13949 continue;
13950 if (NO_OPD_RELOCS && is_opd)
13951 continue;
13952 if (!is_opd
13953 && r_type == R_PPC64_ADDR64)
13955 if (h != NULL
13956 ? h->type == STT_GNU_IFUNC
13957 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13958 continue;
13959 if (h != NULL
13960 ? bfd_is_abs_symbol (&h->root)
13961 : sym->st_shndx == SHN_ABS)
13962 continue;
13963 if (h != NULL
13964 && !SYMBOL_REFERENCES_LOCAL (info, h))
13965 continue;
13967 r_offset = _bfd_elf_section_offset (info->output_bfd,
13968 info,
13969 section,
13970 irela->r_offset);
13971 if (r_offset >= (bfd_vma) -2)
13972 continue;
13973 if (!append_relr_off (htab, section, r_offset))
13974 goto error_ret_free_internal;
13975 continue;
13978 hash = ppc_elf_hash_entry (h);
13979 ok_dest = false;
13980 fdh = NULL;
13981 sym_value = 0;
13982 if (hash == NULL)
13984 sym_value = sym->st_value;
13985 if (sym_sec != NULL
13986 && sym_sec->output_section != NULL)
13987 ok_dest = true;
13989 else if (hash->elf.root.type == bfd_link_hash_defined
13990 || hash->elf.root.type == bfd_link_hash_defweak)
13992 sym_value = hash->elf.root.u.def.value;
13993 if (sym_sec->output_section != NULL)
13994 ok_dest = true;
13996 else if (hash->elf.root.type == bfd_link_hash_undefweak
13997 || hash->elf.root.type == bfd_link_hash_undefined)
13999 /* Recognise an old ABI func code entry sym, and
14000 use the func descriptor sym instead if it is
14001 defined. */
14002 if (hash->elf.root.root.string[0] == '.'
14003 && hash->oh != NULL)
14005 fdh = ppc_follow_link (hash->oh);
14006 if (fdh->elf.root.type == bfd_link_hash_defined
14007 || fdh->elf.root.type == bfd_link_hash_defweak)
14009 sym_sec = fdh->elf.root.u.def.section;
14010 sym_value = fdh->elf.root.u.def.value;
14011 if (sym_sec->output_section != NULL)
14012 ok_dest = true;
14014 else
14015 fdh = NULL;
14018 else
14020 bfd_set_error (bfd_error_bad_value);
14021 goto error_ret_free_internal;
14024 destination = 0;
14025 local_off = 0;
14026 if (ok_dest)
14028 sym_value += irela->r_addend;
14029 destination = (sym_value
14030 + sym_sec->output_offset
14031 + sym_sec->output_section->vma);
14032 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
14033 ? hash->elf.other
14034 : sym->st_other);
14037 code_sec = sym_sec;
14038 code_value = sym_value;
14039 opd = get_opd_info (sym_sec);
14040 if (opd != NULL)
14042 bfd_vma dest;
14044 if (hash == NULL && opd->adjust != NULL)
14046 long adjust = opd->adjust[OPD_NDX (sym_value)];
14047 if (adjust == -1)
14048 continue;
14049 code_value += adjust;
14050 sym_value += adjust;
14052 dest = opd_entry_value (sym_sec, sym_value,
14053 &code_sec, &code_value, false);
14054 if (dest != (bfd_vma) -1)
14056 destination = dest;
14057 if (fdh != NULL)
14059 /* Fixup old ABI sym to point at code
14060 entry. */
14061 hash->elf.root.type = bfd_link_hash_defweak;
14062 hash->elf.root.u.def.section = code_sec;
14063 hash->elf.root.u.def.value = code_value;
14068 /* Determine what (if any) linker stub is needed. */
14069 plt_ent = NULL;
14070 stub_type.main = ppc_type_of_stub (section, irela, &hash,
14071 &plt_ent, destination,
14072 local_off);
14073 stub_type.sub = ppc_stub_toc;
14074 stub_type.r2save = 0;
14076 if (r_type == R_PPC64_REL24_NOTOC
14077 || r_type == R_PPC64_REL24_P9NOTOC)
14079 enum ppc_stub_sub_type notoc = ppc_stub_notoc;
14080 if (htab->params->power10_stubs == 0
14081 || (r_type == R_PPC64_REL24_P9NOTOC
14082 && htab->params->power10_stubs != 1))
14083 notoc = ppc_stub_p9notoc;
14084 if (stub_type.main == ppc_stub_plt_call)
14085 stub_type.sub = notoc;
14086 else if (stub_type.main == ppc_stub_long_branch
14087 || (code_sec != NULL
14088 && code_sec->output_section != NULL
14089 && (((hash ? hash->elf.other : sym->st_other)
14090 & STO_PPC64_LOCAL_MASK)
14091 > 1 << STO_PPC64_LOCAL_BIT)))
14093 stub_type.main = ppc_stub_long_branch;
14094 stub_type.sub = notoc;
14095 stub_type.r2save = 0;
14098 else if (stub_type.main != ppc_stub_plt_call)
14100 /* Check whether we need a TOC adjusting stub.
14101 Since the linker pastes together pieces from
14102 different object files when creating the
14103 _init and _fini functions, it may be that a
14104 call to what looks like a local sym is in
14105 fact a call needing a TOC adjustment. */
14106 if ((code_sec != NULL
14107 && code_sec->output_section != NULL
14108 && (code_sec->has_toc_reloc
14109 || code_sec->makes_toc_func_call)
14110 && (htab->sec_info[code_sec->id].toc_off
14111 != htab->sec_info[section->id].toc_off))
14112 || (((hash ? hash->elf.other : sym->st_other)
14113 & STO_PPC64_LOCAL_MASK)
14114 == 1 << STO_PPC64_LOCAL_BIT))
14116 stub_type.main = ppc_stub_long_branch;
14117 stub_type.sub = ppc_stub_toc;
14118 stub_type.r2save = 1;
14122 if (stub_type.main == ppc_stub_none)
14123 continue;
14125 /* __tls_get_addr calls might be eliminated. */
14126 if (stub_type.main != ppc_stub_plt_call
14127 && hash != NULL
14128 && is_tls_get_addr (&hash->elf, htab)
14129 && section->has_tls_reloc
14130 && irela != internal_relocs)
14132 /* Get tls info. */
14133 unsigned char *tls_mask;
14135 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
14136 irela - 1, input_bfd))
14137 goto error_ret_free_internal;
14138 if ((*tls_mask & TLS_TLS) != 0
14139 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
14140 continue;
14143 if (stub_type.main == ppc_stub_plt_call
14144 && stub_type.sub == ppc_stub_toc)
14146 if (!htab->opd_abi
14147 && htab->params->plt_localentry0 != 0
14148 && is_elfv2_localentry0 (&hash->elf))
14149 htab->has_plt_localentry0 = 1;
14150 else if (irela + 1 < irelaend
14151 && irela[1].r_offset == irela->r_offset + 4
14152 && (ELF64_R_TYPE (irela[1].r_info)
14153 == R_PPC64_TOCSAVE))
14155 if (!tocsave_find (htab, INSERT,
14156 &local_syms, irela + 1, input_bfd))
14157 goto error_ret_free_internal;
14159 else
14160 stub_type.r2save = 1;
14163 /* Support for grouping stub sections. */
14164 id_sec = htab->sec_info[section->id].u.group->link_sec;
14166 /* Get the name of this stub. */
14167 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
14168 if (!stub_name)
14169 goto error_ret_free_internal;
14171 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
14172 stub_name, false, false);
14173 if (stub_entry != NULL)
14175 free (stub_name);
14176 if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
14178 /* xgettext:c-format */
14179 _bfd_error_handler
14180 (_("%pB: cannot create stub entry %s"),
14181 section->owner, stub_entry->root.string);
14182 goto error_ret_free_internal;
14184 continue;
14187 stub_entry = ppc_add_stub (stub_name, section, info);
14188 free (stub_name);
14189 if (stub_entry == NULL)
14191 error_ret_free_internal:
14192 if (elf_section_data (section)->relocs == NULL)
14193 free (internal_relocs);
14194 error_ret_free_local:
14195 if (symtab_hdr->contents
14196 != (unsigned char *) local_syms)
14197 free (local_syms);
14198 return false;
14201 stub_entry->type = stub_type;
14202 if (stub_type.main == ppc_stub_plt_call)
14204 stub_entry->target_value = sym_value;
14205 stub_entry->target_section = sym_sec;
14207 else
14209 stub_entry->target_value = code_value;
14210 stub_entry->target_section = code_sec;
14212 stub_entry->h = hash;
14213 stub_entry->plt_ent = plt_ent;
14214 stub_entry->symtype
14215 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
14216 stub_entry->other = hash ? hash->elf.other : sym->st_other;
14218 if (hash != NULL
14219 && (hash->elf.root.type == bfd_link_hash_defined
14220 || hash->elf.root.type == bfd_link_hash_defweak))
14221 htab->stub_globals += 1;
14224 /* We're done with the internal relocs, free them. */
14225 if (elf_section_data (section)->relocs != internal_relocs)
14226 free (internal_relocs);
14229 if (local_syms != NULL
14230 && symtab_hdr->contents != (unsigned char *) local_syms)
14232 if (!info->keep_memory)
14233 free (local_syms);
14234 else
14235 symtab_hdr->contents = (unsigned char *) local_syms;
14239 /* We may have added some stubs. Find out the new size of the
14240 stub sections. */
14241 for (group = htab->group; group != NULL; group = group->next)
14243 group->lr_restore = 0;
14244 group->eh_size = 0;
14245 if (group->stub_sec != NULL)
14247 asection *stub_sec = group->stub_sec;
14249 stub_sec->rawsize = stub_sec->size;
14250 stub_sec->size = 0;
14251 stub_sec->reloc_count = 0;
14252 stub_sec->flags &= ~SEC_RELOC;
14255 if (htab->tga_group != NULL)
14257 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
14258 htab->tga_group->eh_size
14259 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
14260 htab->tga_group->lr_restore = 23 * 4;
14261 htab->tga_group->stub_sec->size = 24 * 4;
14264 htab->brlt->rawsize = htab->brlt->size;
14265 htab->brlt->size = 0;
14266 htab->brlt->reloc_count = 0;
14267 htab->brlt->flags &= ~SEC_RELOC;
14268 if (htab->relbrlt != NULL)
14269 htab->relbrlt->size = 0;
14271 if (htab->elf.srelrdyn != NULL)
14273 htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
14274 htab->elf.srelrdyn->size = 0;
14277 htab->stub_changed = false;
14278 htab->stub_id = 0;
14279 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
14281 for (group = htab->group; group != NULL; group = group->next)
14282 if (group->needs_save_res)
14283 group->stub_sec->size += htab->sfpr->size;
14285 if (info->emitrelocations
14286 && htab->glink != NULL && htab->glink->size != 0)
14288 htab->glink->reloc_count = 1;
14289 htab->glink->flags |= SEC_RELOC;
14292 if (htab->glink_eh_frame != NULL
14293 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
14294 && htab->glink_eh_frame->output_section->size > 8)
14296 size_t size = 0, align = 4;
14298 for (group = htab->group; group != NULL; group = group->next)
14299 if (group->eh_size != 0)
14300 size += (group->eh_size + 17 + align - 1) & -align;
14301 if (htab->glink != NULL && htab->glink->size != 0)
14302 size += (24 + align - 1) & -align;
14303 if (size != 0)
14304 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14305 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14306 size = (size + align - 1) & -align;
14307 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
14308 htab->glink_eh_frame->size = size;
14311 if (htab->params->plt_stub_align != 0)
14312 for (group = htab->group; group != NULL; group = group->next)
14313 if (group->stub_sec != NULL)
14315 int align = abs (htab->params->plt_stub_align);
14316 group->stub_sec->size
14317 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
14320 if (htab->elf.srelrdyn != NULL)
14322 bfd_vma r_offset;
14324 for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
14325 if (!append_relr_off (htab, htab->brlt, r_offset))
14326 return false;
14328 if (!got_and_plt_relr_for_local_syms (info))
14329 return false;
14330 elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
14331 if (htab->stub_error)
14332 return false;
14334 bfd_vma *relr_addr = sort_relr (htab);
14335 if (htab->relr_count != 0 && relr_addr == NULL)
14336 return false;
14338 size_t i = 0;
14339 while (i < htab->relr_count)
14341 bfd_vma base = relr_addr[i];
14342 htab->elf.srelrdyn->size += 8;
14343 i++;
14344 /* Handle possible duplicate address. This can happen
14345 as sections increase in size when adding stubs. */
14346 while (i < htab->relr_count
14347 && relr_addr[i] == base)
14348 i++;
14349 base += 8;
14350 while (1)
14352 size_t start_i = i;
14353 while (i < htab->relr_count
14354 && relr_addr[i] - base < 63 * 8
14355 && (relr_addr[i] - base) % 8 == 0)
14356 i++;
14357 if (i == start_i)
14358 break;
14359 htab->elf.srelrdyn->size += 8;
14360 base += 63 * 8;
14363 free (relr_addr);
14366 for (group = htab->group; group != NULL; group = group->next)
14367 if (group->stub_sec != NULL
14368 && group->stub_sec->rawsize != group->stub_sec->size
14369 && (htab->stub_iteration <= STUB_SHRINK_ITER
14370 || group->stub_sec->rawsize < group->stub_sec->size))
14371 break;
14373 if (group == NULL
14374 && (!htab->stub_changed
14375 || htab->stub_iteration > STUB_SHRINK_ITER)
14376 && (htab->brlt->rawsize == htab->brlt->size
14377 || (htab->stub_iteration > STUB_SHRINK_ITER
14378 && htab->brlt->rawsize > htab->brlt->size))
14379 && (htab->elf.srelrdyn == NULL
14380 || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
14381 || (htab->stub_iteration > STUB_SHRINK_ITER
14382 && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
14383 && (htab->glink_eh_frame == NULL
14384 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
14385 && (htab->tga_group == NULL
14386 || htab->stub_iteration > 1))
14387 break;
14389 if (htab->stub_iteration > STUB_SHRINK_ITER)
14391 for (group = htab->group; group != NULL; group = group->next)
14392 if (group->stub_sec != NULL
14393 && group->stub_sec->size < group->stub_sec->rawsize)
14394 group->stub_sec->size = group->stub_sec->rawsize;
14396 if (htab->brlt->size < htab->brlt->rawsize)
14397 htab->brlt->size = htab->brlt->rawsize;
14399 if (htab->elf.srelrdyn != NULL
14400 && htab->elf.srelrdyn->size < htab->elf.srelrdyn->rawsize)
14401 htab->elf.srelrdyn->size = htab->elf.srelrdyn->rawsize;
14404 /* Ask the linker to do its stuff. */
14405 (*htab->params->layout_sections_again) ();
14408 if (htab->glink_eh_frame != NULL
14409 && htab->glink_eh_frame->size != 0)
14411 bfd_vma val;
14412 bfd_byte *p, *last_fde;
14413 size_t last_fde_len, size, align, pad;
14414 struct map_stub *group;
14416 /* It is necessary to at least have a rough outline of the
14417 linker generated CIEs and FDEs written before
14418 bfd_elf_discard_info is run, in order for these FDEs to be
14419 indexed in .eh_frame_hdr. */
14420 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
14421 if (p == NULL)
14422 return false;
14423 htab->glink_eh_frame->contents = p;
14424 htab->glink_eh_frame->alloced = 1;
14425 last_fde = p;
14426 align = 4;
14428 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
14429 /* CIE length (rewrite in case little-endian). */
14430 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
14431 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14432 p += last_fde_len + 4;
14434 for (group = htab->group; group != NULL; group = group->next)
14435 if (group->eh_size != 0)
14437 group->eh_base = p - htab->glink_eh_frame->contents;
14438 last_fde = p;
14439 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
14440 /* FDE length. */
14441 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14442 p += 4;
14443 /* CIE pointer. */
14444 val = p - htab->glink_eh_frame->contents;
14445 bfd_put_32 (htab->elf.dynobj, val, p);
14446 p += 4;
14447 /* Offset to stub section, written later. */
14448 p += 4;
14449 /* stub section size. */
14450 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
14451 p += 4;
14452 /* Augmentation. */
14453 p += 1;
14454 /* Make sure we don't have all nops. This is enough for
14455 elf-eh-frame.c to detect the last non-nop opcode. */
14456 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
14457 p = last_fde + last_fde_len + 4;
14459 if (htab->glink != NULL && htab->glink->size != 0)
14461 last_fde = p;
14462 last_fde_len = ((24 + align - 1) & -align) - 4;
14463 /* FDE length. */
14464 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14465 p += 4;
14466 /* CIE pointer. */
14467 val = p - htab->glink_eh_frame->contents;
14468 bfd_put_32 (htab->elf.dynobj, val, p);
14469 p += 4;
14470 /* Offset to .glink, written later. */
14471 p += 4;
14472 /* .glink size. */
14473 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
14474 p += 4;
14475 /* Augmentation. */
14476 p += 1;
14478 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
14479 *p++ = DW_CFA_register;
14480 *p++ = 65;
14481 *p++ = htab->opd_abi ? 12 : 0;
14482 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
14483 *p++ = DW_CFA_restore_extended;
14484 *p++ = 65;
14485 p += ((24 + align - 1) & -align) - 24;
14487 /* Subsume any padding into the last FDE if user .eh_frame
14488 sections are aligned more than glink_eh_frame. Otherwise any
14489 zero padding will be seen as a terminator. */
14490 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14491 size = p - htab->glink_eh_frame->contents;
14492 pad = ((size + align - 1) & -align) - size;
14493 htab->glink_eh_frame->size = size + pad;
14494 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
14497 maybe_strip_output (info, htab->brlt);
14498 if (htab->relbrlt != NULL)
14499 maybe_strip_output (info, htab->relbrlt);
14500 if (htab->glink_eh_frame != NULL)
14501 maybe_strip_output (info, htab->glink_eh_frame);
14502 if (htab->elf.srelrdyn != NULL)
14503 maybe_strip_output (info, htab->elf.srelrdyn);
14505 return true;
14508 /* Called after we have determined section placement. If sections
14509 move, we'll be called again. Provide a value for TOCstart. */
14511 bfd_vma
14512 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
14514 asection *s;
14515 bfd_vma TOCstart, adjust;
14517 if (info != NULL)
14519 struct elf_link_hash_entry *h;
14520 struct elf_link_hash_table *htab = elf_hash_table (info);
14522 if (is_elf_hash_table (&htab->root)
14523 && htab->hgot != NULL)
14524 h = htab->hgot;
14525 else
14527 h = (struct elf_link_hash_entry *)
14528 bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
14529 if (is_elf_hash_table (&htab->root))
14530 htab->hgot = h;
14532 if (h != NULL
14533 && h->root.type == bfd_link_hash_defined
14534 && !h->root.linker_def
14535 && (!is_elf_hash_table (&htab->root)
14536 || h->def_regular))
14538 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
14539 _bfd_set_gp_value (obfd, TOCstart);
14540 return TOCstart;
14544 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14545 order. The TOC starts where the first of these sections starts. */
14546 s = bfd_get_section_by_name (obfd, ".got");
14547 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14548 s = bfd_get_section_by_name (obfd, ".toc");
14549 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14550 s = bfd_get_section_by_name (obfd, ".tocbss");
14551 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14552 s = bfd_get_section_by_name (obfd, ".plt");
14553 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14555 /* This may happen for
14556 o references to TOC base (SYM@toc / TOC[tc0]) without a
14557 .toc directive
14558 o bad linker script
14559 o --gc-sections and empty TOC sections
14561 FIXME: Warn user? */
14563 /* Look for a likely section. We probably won't even be
14564 using TOCstart. */
14565 for (s = obfd->sections; s != NULL; s = s->next)
14566 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14567 | SEC_EXCLUDE))
14568 == (SEC_ALLOC | SEC_SMALL_DATA))
14569 break;
14570 if (s == NULL)
14571 for (s = obfd->sections; s != NULL; s = s->next)
14572 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
14573 == (SEC_ALLOC | SEC_SMALL_DATA))
14574 break;
14575 if (s == NULL)
14576 for (s = obfd->sections; s != NULL; s = s->next)
14577 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14578 == SEC_ALLOC)
14579 break;
14580 if (s == NULL)
14581 for (s = obfd->sections; s != NULL; s = s->next)
14582 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
14583 break;
14586 TOCstart = 0;
14587 if (s != NULL)
14588 TOCstart = s->output_section->vma + s->output_offset;
14590 /* Force alignment. */
14591 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14592 TOCstart -= adjust;
14593 _bfd_set_gp_value (obfd, TOCstart);
14595 if (info != NULL && s != NULL)
14597 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14599 if (htab != NULL)
14601 if (htab->elf.hgot != NULL)
14603 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
14604 htab->elf.hgot->root.u.def.section = s;
14607 else
14609 struct bfd_link_hash_entry *bh = NULL;
14610 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
14611 s, TOC_BASE_OFF - adjust,
14612 NULL, false, false, &bh);
14615 return TOCstart;
14618 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
14619 write out any global entry stubs, and PLT relocations. */
14621 static bool
14622 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
14624 struct bfd_link_info *info;
14625 struct ppc_link_hash_table *htab;
14626 struct plt_entry *ent;
14627 asection *s;
14629 if (h->root.type == bfd_link_hash_indirect)
14630 return true;
14632 info = inf;
14633 htab = ppc_hash_table (info);
14634 if (htab == NULL)
14635 return false;
14637 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14638 if (ent->plt.offset != (bfd_vma) -1)
14640 /* This symbol has an entry in the procedure linkage
14641 table. Set it up. */
14642 Elf_Internal_Rela rela;
14643 asection *plt, *relplt;
14644 bfd_byte *loc;
14646 if (use_local_plt (info, h))
14648 if (!(h->def_regular
14649 && (h->root.type == bfd_link_hash_defined
14650 || h->root.type == bfd_link_hash_defweak)))
14651 continue;
14652 if (h->type == STT_GNU_IFUNC)
14654 plt = htab->elf.iplt;
14655 relplt = htab->elf.irelplt;
14656 htab->elf.ifunc_resolvers = true;
14657 if (htab->opd_abi)
14658 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14659 else
14660 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14662 else
14664 plt = htab->pltlocal;
14665 relplt = NULL;
14666 if (bfd_link_pic (info)
14667 && !(info->enable_dt_relr && !htab->opd_abi))
14669 relplt = htab->relpltlocal;
14670 if (htab->opd_abi)
14671 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14672 else
14673 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14676 rela.r_addend = defined_sym_val (h) + ent->addend;
14678 if (relplt == NULL)
14680 loc = plt->contents + ent->plt.offset;
14681 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14682 if (htab->opd_abi)
14684 bfd_vma toc = elf_gp (info->output_bfd);
14685 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14686 bfd_put_64 (info->output_bfd, toc, loc + 8);
14689 else
14691 rela.r_offset = (plt->output_section->vma
14692 + plt->output_offset
14693 + ent->plt.offset);
14694 BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd, &rela,
14695 relplt));
14698 else
14700 rela.r_offset = (htab->elf.splt->output_section->vma
14701 + htab->elf.splt->output_offset
14702 + ent->plt.offset);
14703 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14704 rela.r_addend = ent->addend;
14705 loc = (htab->elf.srelplt->contents
14706 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14707 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14708 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
14709 htab->elf.ifunc_resolvers = true;
14710 BFD_ASSERT (swap_reloc_out (info->output_bfd, &rela,
14711 loc, htab->elf.srelplt));
14715 if (!h->pointer_equality_needed)
14716 return true;
14718 if (h->def_regular)
14719 return true;
14721 s = htab->global_entry;
14722 if (s == NULL || s->size == 0)
14723 return true;
14725 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14726 if (ent->plt.offset != (bfd_vma) -1
14727 && ent->addend == 0)
14729 bfd_byte *p;
14730 asection *plt;
14731 bfd_vma off;
14733 p = s->contents + h->root.u.def.value;
14734 plt = htab->elf.splt;
14735 if (use_local_plt (info, h))
14737 if (h->type == STT_GNU_IFUNC)
14738 plt = htab->elf.iplt;
14739 else
14740 plt = htab->pltlocal;
14742 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
14743 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14745 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14747 info->callbacks->einfo
14748 (_("%P: linkage table error against `%pT'\n"),
14749 h->root.root.string);
14750 bfd_set_error (bfd_error_bad_value);
14751 htab->stub_error = true;
14754 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14755 if (htab->params->emit_stub_syms)
14757 size_t len = strlen (h->root.root.string);
14758 char *name = bfd_alloc (info->output_bfd,
14759 sizeof "12345678.global_entry." + len);
14761 if (name == NULL)
14762 return false;
14764 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14765 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
14766 if (h == NULL)
14767 return false;
14768 if (h->root.type == bfd_link_hash_new)
14770 h->root.type = bfd_link_hash_defined;
14771 h->root.u.def.section = s;
14772 h->root.u.def.value = p - s->contents;
14773 h->ref_regular = 1;
14774 h->def_regular = 1;
14775 h->ref_regular_nonweak = 1;
14776 h->forced_local = 1;
14777 h->non_elf = 0;
14778 h->root.linker_def = 1;
14782 if (PPC_HA (off) != 0)
14784 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14785 p += 4;
14787 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14788 p += 4;
14789 bfd_put_32 (s->owner, MTCTR_R12, p);
14790 p += 4;
14791 bfd_put_32 (s->owner, BCTR, p);
14792 break;
14794 return true;
14797 /* Write PLT relocs for locals. */
14799 static bool
14800 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14802 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14803 bfd *ibfd;
14805 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14807 struct got_entry **lgot_ents, **end_lgot_ents;
14808 struct plt_entry **local_plt, **lplt, **end_local_plt;
14809 Elf_Internal_Shdr *symtab_hdr;
14810 bfd_size_type locsymcount;
14811 Elf_Internal_Sym *local_syms = NULL;
14812 struct plt_entry *ent;
14814 if (!is_ppc64_elf (ibfd))
14815 continue;
14817 lgot_ents = elf_local_got_ents (ibfd);
14818 if (!lgot_ents)
14819 continue;
14821 symtab_hdr = &elf_symtab_hdr (ibfd);
14822 locsymcount = symtab_hdr->sh_info;
14823 end_lgot_ents = lgot_ents + locsymcount;
14824 local_plt = (struct plt_entry **) end_lgot_ents;
14825 end_local_plt = local_plt + locsymcount;
14826 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14827 for (ent = *lplt; ent != NULL; ent = ent->next)
14828 if (ent->plt.offset != (bfd_vma) -1)
14830 Elf_Internal_Sym *sym;
14831 asection *sym_sec;
14832 asection *plt, *relplt;
14833 bfd_vma val;
14835 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14836 lplt - local_plt, ibfd))
14838 if (symtab_hdr->contents != (unsigned char *) local_syms)
14839 free (local_syms);
14840 return false;
14843 val = sym->st_value + ent->addend;
14844 if (sym_sec != NULL && sym_sec->output_section != NULL)
14845 val += sym_sec->output_offset + sym_sec->output_section->vma;
14847 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14849 htab->elf.ifunc_resolvers = true;
14850 plt = htab->elf.iplt;
14851 relplt = htab->elf.irelplt;
14853 else
14855 plt = htab->pltlocal;
14856 relplt = NULL;
14857 if (bfd_link_pic (info)
14858 && !(info->enable_dt_relr && !htab->opd_abi))
14859 relplt = htab->relpltlocal;
14862 if (relplt == NULL)
14864 bfd_byte *loc = plt->contents + ent->plt.offset;
14865 bfd_put_64 (info->output_bfd, val, loc);
14866 if (htab->opd_abi)
14868 bfd_vma toc = elf_gp (ibfd);
14869 bfd_put_64 (info->output_bfd, toc, loc + 8);
14872 else
14874 Elf_Internal_Rela rela;
14875 rela.r_offset = (ent->plt.offset
14876 + plt->output_offset
14877 + plt->output_section->vma);
14878 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14880 if (htab->opd_abi)
14881 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14882 else
14883 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14885 else
14887 if (htab->opd_abi)
14888 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14889 else
14890 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14892 rela.r_addend = val;
14893 BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd,
14894 &rela, relplt));
14898 if (local_syms != NULL
14899 && symtab_hdr->contents != (unsigned char *) local_syms)
14901 if (!info->keep_memory)
14902 free (local_syms);
14903 else
14904 symtab_hdr->contents = (unsigned char *) local_syms;
14907 return true;
14910 /* Emit the static wrapper function preserving registers around a
14911 __tls_get_addr_opt call. */
14913 static bool
14914 emit_tga_desc (struct ppc_link_hash_table *htab)
14916 asection *stub_sec = htab->tga_group->stub_sec;
14917 unsigned int cfa_updt = 11 * 4;
14918 bfd_byte *p;
14919 bfd_vma to, from, delta;
14921 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14922 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14923 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14924 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14925 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14926 delta = to - from;
14927 if (delta + (1 << 25) >= 1 << 26)
14929 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14930 htab->stub_error = true;
14931 return false;
14934 p = stub_sec->contents;
14935 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14936 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14937 p += 4;
14938 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14939 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14942 /* Emit eh_frame describing the static wrapper function. */
14944 static bfd_byte *
14945 emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14947 unsigned int cfa_updt = 11 * 4;
14948 unsigned int i;
14950 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14951 *p++ = DW_CFA_def_cfa_offset;
14952 if (htab->opd_abi)
14954 *p++ = 128;
14955 *p++ = 1;
14957 else
14958 *p++ = 96;
14959 *p++ = DW_CFA_offset_extended_sf;
14960 *p++ = 65;
14961 *p++ = (-16 / 8) & 0x7f;
14962 for (i = 4; i < 12; i++)
14964 *p++ = DW_CFA_offset + i;
14965 *p++ = (htab->opd_abi ? 13 : 12) - i;
14967 *p++ = DW_CFA_advance_loc + 10;
14968 *p++ = DW_CFA_def_cfa_offset;
14969 *p++ = 0;
14970 for (i = 4; i < 12; i++)
14971 *p++ = DW_CFA_restore + i;
14972 *p++ = DW_CFA_advance_loc + 2;
14973 *p++ = DW_CFA_restore_extended;
14974 *p++ = 65;
14975 return p;
14978 /* Build all the stubs associated with the current output file.
14979 The stubs are kept in a hash table attached to the main linker
14980 hash table. This function is called via gldelf64ppc_finish. */
14982 bool
14983 ppc64_elf_build_stubs (struct bfd_link_info *info,
14984 char **stats)
14986 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14987 struct map_stub *group;
14988 asection *stub_sec;
14989 bfd_byte *p;
14990 int stub_sec_count = 0;
14992 if (htab == NULL)
14993 return false;
14995 /* Allocate memory to hold the linker stubs. */
14996 for (group = htab->group; group != NULL; group = group->next)
14998 group->eh_size = 0;
14999 group->lr_restore = 0;
15000 if ((stub_sec = group->stub_sec) != NULL
15001 && stub_sec->size != 0)
15003 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
15004 stub_sec->size);
15005 if (stub_sec->contents == NULL)
15006 return false;
15007 stub_sec->alloced = 1;
15008 stub_sec->size = 0;
15012 if (htab->glink != NULL && htab->glink->size != 0)
15014 unsigned int indx;
15015 bfd_vma plt0;
15017 /* Build the .glink plt call stub. */
15018 if (htab->params->emit_stub_syms)
15020 struct elf_link_hash_entry *h;
15021 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
15022 true, false, false);
15023 if (h == NULL)
15024 return false;
15025 if (h->root.type == bfd_link_hash_new)
15027 h->root.type = bfd_link_hash_defined;
15028 h->root.u.def.section = htab->glink;
15029 h->root.u.def.value = 8;
15030 h->ref_regular = 1;
15031 h->def_regular = 1;
15032 h->ref_regular_nonweak = 1;
15033 h->forced_local = 1;
15034 h->non_elf = 0;
15035 h->root.linker_def = 1;
15038 plt0 = (htab->elf.splt->output_section->vma
15039 + htab->elf.splt->output_offset
15040 - 16);
15041 if (info->emitrelocations)
15043 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
15044 if (r == NULL)
15045 return false;
15046 r->r_offset = (htab->glink->output_offset
15047 + htab->glink->output_section->vma);
15048 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
15049 r->r_addend = plt0;
15051 p = htab->glink->contents;
15052 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
15053 bfd_put_64 (htab->glink->owner, plt0, p);
15054 p += 8;
15055 if (htab->opd_abi)
15057 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
15058 p += 4;
15059 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15060 p += 4;
15061 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15062 p += 4;
15063 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
15064 p += 4;
15065 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
15066 p += 4;
15067 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
15068 p += 4;
15069 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15070 p += 4;
15071 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
15072 p += 4;
15073 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15074 p += 4;
15075 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
15076 p += 4;
15078 else
15080 unsigned int insn;
15082 /* 0:
15083 . .quad plt0-1f # plt0 entry relative to 1:
15085 # We get here with r12 initially @ a glink branch
15086 # Load the address of _dl_runtime_resolve from plt0 and
15087 # jump to it, with r0 set to the index of the PLT entry
15088 # to be resolved and r11 the link map.
15089 __glink_PLTresolve:
15090 . std %r2,24(%r1) # optional
15091 . mflr %r0
15092 . bcl 20,31,1f
15094 . mflr %r11
15095 . mtlr %r0
15096 . ld %r0,(0b-1b)(%r11)
15097 . sub %r12,%r12,%r11
15098 . add %r11,%r0,%r11
15099 . addi %r0,%r12,1b-2f
15100 . ld %r12,0(%r11)
15101 . srdi %r0,%r0,2
15102 . mtctr %r12
15103 . ld %r11,8(%r11)
15104 . bctr
15106 . b __glink_PLTresolve
15107 . ...
15108 . b __glink_PLTresolve */
15110 if (htab->has_plt_localentry0)
15112 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
15113 p += 4;
15115 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
15116 p += 4;
15117 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15118 p += 4;
15119 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15120 p += 4;
15121 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
15122 p += 4;
15123 if (htab->has_plt_localentry0)
15124 insn = LD_R0_0R11 | (-20 & 0xfffc);
15125 else
15126 insn = LD_R0_0R11 | (-16 & 0xfffc);
15127 bfd_put_32 (htab->glink->owner, insn, p);
15128 p += 4;
15129 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
15130 p += 4;
15131 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
15132 p += 4;
15133 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
15134 p += 4;
15135 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15136 p += 4;
15137 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
15138 p += 4;
15139 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15140 p += 4;
15141 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
15142 p += 4;
15144 bfd_put_32 (htab->glink->owner, BCTR, p);
15145 p += 4;
15146 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
15148 /* Build the .glink lazy link call stubs. */
15149 indx = 0;
15150 while (p < htab->glink->contents + htab->glink->size)
15152 if (htab->opd_abi)
15154 if (indx < 0x8000)
15156 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
15157 p += 4;
15159 else
15161 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
15162 p += 4;
15163 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
15165 p += 4;
15168 bfd_put_32 (htab->glink->owner,
15169 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
15170 indx++;
15171 p += 4;
15175 if (htab->tga_group != NULL)
15177 htab->tga_group->lr_restore = 23 * 4;
15178 htab->tga_group->stub_sec->size = 24 * 4;
15179 if (!emit_tga_desc (htab))
15180 return false;
15181 if (htab->glink_eh_frame != NULL
15182 && htab->glink_eh_frame->size != 0)
15184 size_t align = 4;
15186 p = htab->glink_eh_frame->contents;
15187 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15188 p += 17;
15189 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
15193 /* Build .glink global entry stubs, and PLT relocs for globals. */
15194 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
15196 if (!write_plt_relocs_for_local_syms (info))
15197 return false;
15199 if (htab->brlt != NULL && htab->brlt->size != 0)
15201 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
15202 htab->brlt->size);
15203 if (htab->brlt->contents == NULL)
15204 return false;
15205 htab->brlt->alloced = 1;
15207 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
15209 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
15210 htab->relbrlt->size);
15211 if (htab->relbrlt->contents == NULL)
15212 return false;
15213 htab->relbrlt->alloced = 1;
15216 /* Build the stubs as directed by the stub hash table. */
15217 htab->stub_id = 0;
15218 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
15220 for (group = htab->group; group != NULL; group = group->next)
15221 if (group->needs_save_res)
15222 group->stub_sec->size += htab->sfpr->size;
15224 if (htab->relbrlt != NULL)
15225 htab->relbrlt->reloc_count = 0;
15227 if (htab->params->plt_stub_align != 0)
15228 for (group = htab->group; group != NULL; group = group->next)
15229 if ((stub_sec = group->stub_sec) != NULL)
15231 int align = abs (htab->params->plt_stub_align);
15232 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
15235 for (group = htab->group; group != NULL; group = group->next)
15236 if (group->needs_save_res)
15238 stub_sec = group->stub_sec;
15239 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
15240 htab->sfpr->contents, htab->sfpr->size);
15241 if (htab->params->emit_stub_syms)
15243 unsigned int i;
15245 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
15246 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
15247 return false;
15251 if (htab->glink_eh_frame != NULL
15252 && htab->glink_eh_frame->size != 0)
15254 bfd_vma val;
15255 size_t align = 4;
15257 p = htab->glink_eh_frame->contents;
15258 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15260 for (group = htab->group; group != NULL; group = group->next)
15261 if (group->eh_size != 0)
15263 /* Offset to stub section. */
15264 val = (group->stub_sec->output_section->vma
15265 + group->stub_sec->output_offset);
15266 val -= (htab->glink_eh_frame->output_section->vma
15267 + htab->glink_eh_frame->output_offset
15268 + (p + 8 - htab->glink_eh_frame->contents));
15269 if (val + 0x80000000 > 0xffffffff)
15271 _bfd_error_handler
15272 (_("%s offset too large for .eh_frame sdata4 encoding"),
15273 group->stub_sec->name);
15274 return false;
15276 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15277 p += (group->eh_size + 17 + 3) & -4;
15279 if (htab->glink != NULL && htab->glink->size != 0)
15281 /* Offset to .glink. */
15282 val = (htab->glink->output_section->vma
15283 + htab->glink->output_offset
15284 + 8);
15285 val -= (htab->glink_eh_frame->output_section->vma
15286 + htab->glink_eh_frame->output_offset
15287 + (p + 8 - htab->glink_eh_frame->contents));
15288 if (val + 0x80000000 > 0xffffffff)
15290 _bfd_error_handler
15291 (_("%s offset too large for .eh_frame sdata4 encoding"),
15292 htab->glink->name);
15293 return false;
15295 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15296 p += (24 + align - 1) & -align;
15300 if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
15302 htab->elf.srelrdyn->contents
15303 = bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
15304 if (htab->elf.srelrdyn->contents == NULL)
15305 return false;
15306 htab->elf.srelrdyn->alloced = 1;
15308 bfd_vma *relr_addr = sort_relr (htab);
15309 if (htab->relr_count != 0 && relr_addr == NULL)
15310 return false;
15312 size_t i = 0;
15313 bfd_byte *loc = htab->elf.srelrdyn->contents;
15314 while (i < htab->relr_count)
15316 bfd_vma base = relr_addr[i];
15317 BFD_ASSERT ((base & ((1 << RELR_ALIGN) - 1)) == 0);
15318 bfd_put_64 (htab->elf.dynobj, base, loc);
15319 loc += 8;
15320 i++;
15321 while (i < htab->relr_count
15322 && relr_addr[i] == base)
15324 htab->stub_error = true;
15325 i++;
15327 base += 8;
15328 while (1)
15330 bfd_vma bits = 0;
15331 while (i < htab->relr_count
15332 && relr_addr[i] - base < 63 * 8
15333 && (relr_addr[i] - base) % 8 == 0)
15335 bits |= (bfd_vma) 1 << ((relr_addr[i] - base) / 8);
15336 i++;
15338 if (bits == 0)
15339 break;
15340 bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
15341 loc += 8;
15342 base += 63 * 8;
15345 free (relr_addr);
15346 /* Pad any excess with 1's, a do-nothing encoding. */
15347 while ((size_t) (loc - htab->elf.srelrdyn->contents)
15348 < htab->elf.srelrdyn->size)
15350 bfd_put_64 (htab->elf.dynobj, 1, loc);
15351 loc += 8;
15354 free (htab->relr);
15355 htab->relr = NULL;
15357 for (group = htab->group; group != NULL; group = group->next)
15358 if ((stub_sec = group->stub_sec) != NULL)
15360 stub_sec_count += 1;
15361 if (stub_sec->rawsize != stub_sec->size
15362 && (htab->stub_iteration <= STUB_SHRINK_ITER
15363 || stub_sec->rawsize < stub_sec->size))
15364 break;
15367 if (group != NULL)
15368 htab->stub_error = true;
15370 if (htab->stub_error)
15372 _bfd_error_handler (_("stubs don't match calculated size"));
15373 return false;
15376 if (stats != NULL)
15378 char *groupmsg;
15379 if (asprintf (&groupmsg,
15380 ngettext ("linker stubs in %u group",
15381 "linker stubs in %u groups",
15382 stub_sec_count),
15383 stub_sec_count) < 0)
15384 *stats = NULL;
15385 else
15387 if (asprintf (stats, _("%s, iter %u\n"
15388 " branch %lu\n"
15389 " long branch %lu\n"
15390 " plt call %lu\n"
15391 " global entry %lu"),
15392 groupmsg, htab->stub_iteration,
15393 htab->stub_count[ppc_stub_long_branch - 1],
15394 htab->stub_count[ppc_stub_plt_branch - 1],
15395 htab->stub_count[ppc_stub_plt_call - 1],
15396 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
15397 *stats = NULL;
15398 free (groupmsg);
15401 return true;
15404 /* What to do when ld finds relocations against symbols defined in
15405 discarded sections. */
15407 static unsigned int
15408 ppc64_elf_action_discarded (asection *sec)
15410 if (strcmp (".opd", sec->name) == 0)
15411 return 0;
15413 if (strcmp (".toc", sec->name) == 0)
15414 return 0;
15416 if (strcmp (".toc1", sec->name) == 0)
15417 return 0;
15419 return _bfd_elf_default_action_discarded (sec);
15422 /* These are the dynamic relocations supported by glibc. */
15424 static bool
15425 ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
15427 switch (r_type)
15429 case R_PPC64_RELATIVE:
15430 case R_PPC64_NONE:
15431 case R_PPC64_ADDR64:
15432 case R_PPC64_GLOB_DAT:
15433 case R_PPC64_IRELATIVE:
15434 case R_PPC64_JMP_IREL:
15435 case R_PPC64_JMP_SLOT:
15436 case R_PPC64_DTPMOD64:
15437 case R_PPC64_DTPREL64:
15438 case R_PPC64_TPREL64:
15439 case R_PPC64_TPREL16_LO_DS:
15440 case R_PPC64_TPREL16_DS:
15441 case R_PPC64_TPREL16:
15442 case R_PPC64_TPREL16_LO:
15443 case R_PPC64_TPREL16_HI:
15444 case R_PPC64_TPREL16_HIGH:
15445 case R_PPC64_TPREL16_HA:
15446 case R_PPC64_TPREL16_HIGHA:
15447 case R_PPC64_TPREL16_HIGHER:
15448 case R_PPC64_TPREL16_HIGHEST:
15449 case R_PPC64_TPREL16_HIGHERA:
15450 case R_PPC64_TPREL16_HIGHESTA:
15451 case R_PPC64_ADDR16_LO_DS:
15452 case R_PPC64_ADDR16_LO:
15453 case R_PPC64_ADDR16_HI:
15454 case R_PPC64_ADDR16_HIGH:
15455 case R_PPC64_ADDR16_HA:
15456 case R_PPC64_ADDR16_HIGHA:
15457 case R_PPC64_REL30:
15458 case R_PPC64_COPY:
15459 case R_PPC64_UADDR64:
15460 case R_PPC64_UADDR32:
15461 case R_PPC64_ADDR32:
15462 case R_PPC64_ADDR24:
15463 case R_PPC64_ADDR16:
15464 case R_PPC64_UADDR16:
15465 case R_PPC64_ADDR16_DS:
15466 case R_PPC64_ADDR16_HIGHER:
15467 case R_PPC64_ADDR16_HIGHEST:
15468 case R_PPC64_ADDR16_HIGHERA:
15469 case R_PPC64_ADDR16_HIGHESTA:
15470 case R_PPC64_ADDR14:
15471 case R_PPC64_ADDR14_BRTAKEN:
15472 case R_PPC64_ADDR14_BRNTAKEN:
15473 case R_PPC64_REL32:
15474 case R_PPC64_REL64:
15475 return true;
15477 default:
15478 return false;
15482 /* The RELOCATE_SECTION function is called by the ELF backend linker
15483 to handle the relocations for a section.
15485 The relocs are always passed as Rela structures; if the section
15486 actually uses Rel structures, the r_addend field will always be
15487 zero.
15489 This function is responsible for adjust the section contents as
15490 necessary, and (if using Rela relocs and generating a
15491 relocatable output file) adjusting the reloc addend as
15492 necessary.
15494 This function does not have to worry about setting the reloc
15495 address or the reloc symbol index.
15497 LOCAL_SYMS is a pointer to the swapped in local symbols.
15499 LOCAL_SECTIONS is an array giving the section in the input file
15500 corresponding to the st_shndx field of each local symbol.
15502 The global hash table entry for the global symbols can be found
15503 via elf_sym_hashes (input_bfd).
15505 When generating relocatable output, this function must handle
15506 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
15507 going to be the section symbol corresponding to the output
15508 section, which means that the addend must be adjusted
15509 accordingly. */
15511 static int
15512 ppc64_elf_relocate_section (bfd *output_bfd,
15513 struct bfd_link_info *info,
15514 bfd *input_bfd,
15515 asection *input_section,
15516 bfd_byte *contents,
15517 Elf_Internal_Rela *relocs,
15518 Elf_Internal_Sym *local_syms,
15519 asection **local_sections)
15521 struct ppc_link_hash_table *htab;
15522 Elf_Internal_Shdr *symtab_hdr;
15523 struct elf_link_hash_entry **sym_hashes;
15524 Elf_Internal_Rela *rel;
15525 Elf_Internal_Rela *wrel;
15526 Elf_Internal_Rela *relend;
15527 Elf_Internal_Rela outrel;
15528 bfd_byte *loc;
15529 struct got_entry **local_got_ents;
15530 bfd_vma TOCstart;
15531 bool ret = true;
15532 bool is_opd;
15533 /* Assume 'at' branch hints. */
15534 bool is_isa_v2 = true;
15535 bool warned_dynamic = false;
15536 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
15538 /* Initialize howto table if needed. */
15539 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
15540 ppc_howto_init ();
15542 htab = ppc_hash_table (info);
15543 if (htab == NULL)
15544 return false;
15546 /* Don't relocate stub sections. */
15547 if (input_section->owner == htab->params->stub_bfd)
15548 return true;
15550 if (!is_ppc64_elf (input_bfd))
15552 bfd_set_error (bfd_error_wrong_format);
15553 return false;
15556 local_got_ents = elf_local_got_ents (input_bfd);
15557 TOCstart = elf_gp (output_bfd);
15558 symtab_hdr = &elf_symtab_hdr (input_bfd);
15559 sym_hashes = elf_sym_hashes (input_bfd);
15560 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
15562 rel = wrel = relocs;
15563 relend = relocs + input_section->reloc_count;
15564 for (; rel < relend; wrel++, rel++)
15566 enum elf_ppc64_reloc_type r_type;
15567 bfd_vma addend;
15568 bfd_reloc_status_type r;
15569 Elf_Internal_Sym *sym;
15570 asection *sec;
15571 struct elf_link_hash_entry *h_elf;
15572 struct ppc_link_hash_entry *h;
15573 struct ppc_link_hash_entry *fdh;
15574 const char *sym_name;
15575 unsigned long r_symndx, toc_symndx;
15576 bfd_vma toc_addend;
15577 unsigned char tls_mask, tls_gd, tls_type;
15578 unsigned char sym_type;
15579 bfd_vma relocation;
15580 bool unresolved_reloc, save_unresolved_reloc;
15581 bool warned;
15582 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
15583 unsigned int insn;
15584 unsigned int mask;
15585 struct ppc_stub_hash_entry *stub_entry;
15586 bfd_vma max_br_offset;
15587 bfd_vma from;
15588 Elf_Internal_Rela orig_rel;
15589 reloc_howto_type *howto;
15590 struct reloc_howto_struct alt_howto;
15591 uint64_t pinsn;
15592 bfd_vma offset;
15594 again:
15595 orig_rel = *rel;
15597 r_type = ELF64_R_TYPE (rel->r_info);
15598 r_symndx = ELF64_R_SYM (rel->r_info);
15600 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15601 symbol of the previous ADDR64 reloc. The symbol gives us the
15602 proper TOC base to use. */
15603 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
15604 && wrel != relocs
15605 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
15606 && is_opd)
15607 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
15609 sym = NULL;
15610 sec = NULL;
15611 h_elf = NULL;
15612 sym_name = NULL;
15613 unresolved_reloc = false;
15614 warned = false;
15616 if (r_symndx < symtab_hdr->sh_info)
15618 /* It's a local symbol. */
15619 struct _opd_sec_data *opd;
15621 sym = local_syms + r_symndx;
15622 sec = local_sections[r_symndx];
15623 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
15624 sym_type = ELF64_ST_TYPE (sym->st_info);
15625 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
15626 opd = get_opd_info (sec);
15627 if (opd != NULL && opd->adjust != NULL)
15629 long adjust = opd->adjust[OPD_NDX (sym->st_value
15630 + rel->r_addend)];
15631 if (adjust == -1)
15632 relocation = 0;
15633 else
15635 /* If this is a relocation against the opd section sym
15636 and we have edited .opd, adjust the reloc addend so
15637 that ld -r and ld --emit-relocs output is correct.
15638 If it is a reloc against some other .opd symbol,
15639 then the symbol value will be adjusted later. */
15640 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15641 rel->r_addend += adjust;
15642 else
15643 relocation += adjust;
15647 else
15649 bool ignored;
15651 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15652 r_symndx, symtab_hdr, sym_hashes,
15653 h_elf, sec, relocation,
15654 unresolved_reloc, warned, ignored);
15655 sym_name = h_elf->root.root.string;
15656 sym_type = h_elf->type;
15657 if (sec != NULL
15658 && sec->owner == output_bfd
15659 && strcmp (sec->name, ".opd") == 0)
15661 /* This is a symbol defined in a linker script. All
15662 such are defined in output sections, even those
15663 defined by simple assignment from a symbol defined in
15664 an input section. Transfer the symbol to an
15665 appropriate input .opd section, so that a branch to
15666 this symbol will be mapped to the location specified
15667 by the opd entry. */
15668 struct bfd_link_order *lo;
15669 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15670 if (lo->type == bfd_indirect_link_order)
15672 asection *isec = lo->u.indirect.section;
15673 if (h_elf->root.u.def.value >= isec->output_offset
15674 && h_elf->root.u.def.value < (isec->output_offset
15675 + isec->size))
15677 h_elf->root.u.def.value -= isec->output_offset;
15678 h_elf->root.u.def.section = isec;
15679 sec = isec;
15680 break;
15685 h = ppc_elf_hash_entry (h_elf);
15687 if (sec != NULL && discarded_section (sec))
15689 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15690 input_bfd, input_section,
15691 contents, rel->r_offset);
15692 wrel->r_offset = rel->r_offset;
15693 wrel->r_info = 0;
15694 wrel->r_addend = 0;
15696 /* For ld -r, remove relocations in debug sections against
15697 symbols defined in discarded sections. Not done for
15698 non-debug to preserve relocs in .eh_frame which the
15699 eh_frame editing code expects to be present. */
15700 if (bfd_link_relocatable (info)
15701 && (input_section->flags & SEC_DEBUGGING))
15702 wrel--;
15704 continue;
15707 if (bfd_link_relocatable (info))
15708 goto copy_reloc;
15710 if (h != NULL && &h->elf == htab->elf.hgot)
15712 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15713 sec = bfd_abs_section_ptr;
15714 unresolved_reloc = false;
15717 /* TLS optimizations. Replace instruction sequences and relocs
15718 based on information we collected in tls_optimize. We edit
15719 RELOCS so that --emit-relocs will output something sensible
15720 for the final instruction stream. */
15721 tls_mask = 0;
15722 tls_gd = 0;
15723 toc_symndx = 0;
15724 if (h != NULL)
15725 tls_mask = h->tls_mask;
15726 else if (local_got_ents != NULL)
15728 struct plt_entry **local_plt = (struct plt_entry **)
15729 (local_got_ents + symtab_hdr->sh_info);
15730 unsigned char *lgot_masks = (unsigned char *)
15731 (local_plt + symtab_hdr->sh_info);
15732 tls_mask = lgot_masks[r_symndx];
15734 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
15735 && (r_type == R_PPC64_TLS
15736 || r_type == R_PPC64_TLSGD
15737 || r_type == R_PPC64_TLSLD))
15739 /* Check for toc tls entries. */
15740 unsigned char *toc_tls;
15742 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15743 &local_syms, rel, input_bfd))
15744 return false;
15746 if (toc_tls)
15747 tls_mask = *toc_tls;
15750 /* Check that tls relocs are used with tls syms, and non-tls
15751 relocs are used with non-tls syms. */
15752 if (r_symndx != STN_UNDEF
15753 && r_type != R_PPC64_NONE
15754 && (h == NULL
15755 || h->elf.root.type == bfd_link_hash_defined
15756 || h->elf.root.type == bfd_link_hash_defweak)
15757 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
15759 if ((tls_mask & TLS_TLS) != 0
15760 && (r_type == R_PPC64_TLS
15761 || r_type == R_PPC64_TLSGD
15762 || r_type == R_PPC64_TLSLD))
15763 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15765 else
15766 info->callbacks->einfo
15767 (!IS_PPC64_TLS_RELOC (r_type)
15768 /* xgettext:c-format */
15769 ? _("%H: %s used with TLS symbol `%pT'\n")
15770 /* xgettext:c-format */
15771 : _("%H: %s used with non-TLS symbol `%pT'\n"),
15772 input_bfd, input_section, rel->r_offset,
15773 ppc64_elf_howto_table[r_type]->name,
15774 sym_name);
15777 /* Ensure reloc mapping code below stays sane. */
15778 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15779 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15780 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15781 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15782 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15783 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15784 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15785 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15786 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15787 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15788 abort ();
15790 switch (r_type)
15792 default:
15793 break;
15795 case R_PPC64_LO_DS_OPT:
15796 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15798 insn = bfd_get_32 (input_bfd,
15799 contents + rel->r_offset - d_offset);
15800 if ((insn & (0x3fu << 26)) != 58u << 26)
15801 abort ();
15802 insn += (14u << 26) - (58u << 26);
15803 bfd_put_32 (input_bfd, insn,
15804 contents + rel->r_offset - d_offset);
15805 r_type = R_PPC64_TOC16_LO;
15806 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15808 break;
15810 case R_PPC64_TOC16:
15811 case R_PPC64_TOC16_LO:
15812 case R_PPC64_TOC16_DS:
15813 case R_PPC64_TOC16_LO_DS:
15815 /* Check for toc tls entries. */
15816 unsigned char *toc_tls;
15817 int retval;
15819 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15820 &local_syms, rel, input_bfd);
15821 if (retval == 0)
15822 return false;
15824 if (toc_tls)
15826 tls_mask = *toc_tls;
15827 if (r_type == R_PPC64_TOC16_DS
15828 || r_type == R_PPC64_TOC16_LO_DS)
15830 if ((tls_mask & TLS_TLS) != 0
15831 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15832 goto toctprel;
15834 else
15836 /* If we found a GD reloc pair, then we might be
15837 doing a GD->IE transition. */
15838 if (retval == 2)
15840 tls_gd = TLS_GDIE;
15841 if ((tls_mask & TLS_TLS) != 0
15842 && (tls_mask & TLS_GD) == 0)
15843 goto tls_ldgd_opt;
15845 else if (retval == 3)
15847 if ((tls_mask & TLS_TLS) != 0
15848 && (tls_mask & TLS_LD) == 0)
15849 goto tls_ldgd_opt;
15854 break;
15856 case R_PPC64_GOT_TPREL16_HI:
15857 case R_PPC64_GOT_TPREL16_HA:
15858 if ((tls_mask & TLS_TLS) != 0
15859 && (tls_mask & TLS_TPREL) == 0
15860 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15862 rel->r_offset -= d_offset;
15863 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15864 r_type = R_PPC64_NONE;
15865 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15867 break;
15869 case R_PPC64_GOT_TPREL16_DS:
15870 case R_PPC64_GOT_TPREL16_LO_DS:
15871 if ((tls_mask & TLS_TLS) != 0
15872 && (tls_mask & TLS_TPREL) == 0
15873 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15875 toctprel:
15876 insn = bfd_get_32 (input_bfd,
15877 contents + rel->r_offset - d_offset);
15878 insn &= 31 << 21;
15879 insn |= 0x3c0d0000; /* addis 0,13,0 */
15880 bfd_put_32 (input_bfd, insn,
15881 contents + rel->r_offset - d_offset);
15882 r_type = R_PPC64_TPREL16_HA;
15883 if (toc_symndx != 0)
15885 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15886 rel->r_addend = toc_addend;
15887 /* We changed the symbol. Start over in order to
15888 get h, sym, sec etc. right. */
15889 goto again;
15891 else
15892 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15894 break;
15896 case R_PPC64_GOT_TPREL_PCREL34:
15897 if ((tls_mask & TLS_TLS) != 0
15898 && (tls_mask & TLS_TPREL) == 0
15899 && offset_in_range (input_section, rel->r_offset, 8))
15901 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15902 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15903 pinsn <<= 32;
15904 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15905 pinsn += ((2ULL << 56) + (-1ULL << 52)
15906 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15907 bfd_put_32 (input_bfd, pinsn >> 32,
15908 contents + rel->r_offset);
15909 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15910 contents + rel->r_offset + 4);
15911 r_type = R_PPC64_TPREL34;
15912 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15914 break;
15916 case R_PPC64_TLS:
15917 if ((tls_mask & TLS_TLS) != 0
15918 && (tls_mask & TLS_TPREL) == 0
15919 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15921 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15922 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15923 if (insn == 0)
15924 break;
15925 if ((rel->r_offset & 3) == 0)
15927 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15928 /* Was PPC64_TLS which sits on insn boundary, now
15929 PPC64_TPREL16_LO which is at low-order half-word. */
15930 rel->r_offset += d_offset;
15931 r_type = R_PPC64_TPREL16_LO;
15932 if (toc_symndx != 0)
15934 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15935 rel->r_addend = toc_addend;
15936 /* We changed the symbol. Start over in order to
15937 get h, sym, sec etc. right. */
15938 goto again;
15940 else
15941 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15943 else if ((rel->r_offset & 3) == 1)
15945 /* For pcrel IE to LE we already have the full
15946 offset and thus don't need an addi here. A nop
15947 or mr will do. */
15948 if ((insn & (0x3fu << 26)) == 14 << 26)
15950 /* Extract regs from addi rt,ra,si. */
15951 unsigned int rt = (insn >> 21) & 0x1f;
15952 unsigned int ra = (insn >> 16) & 0x1f;
15953 if (rt == ra)
15954 insn = NOP;
15955 else
15957 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15958 insn = (rt << 16) | (ra << 21) | (ra << 11);
15959 insn |= (31u << 26) | (444u << 1);
15962 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
15965 break;
15967 case R_PPC64_GOT_TLSGD16_HI:
15968 case R_PPC64_GOT_TLSGD16_HA:
15969 tls_gd = TLS_GDIE;
15970 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15971 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15972 goto tls_gdld_hi;
15973 break;
15975 case R_PPC64_GOT_TLSLD16_HI:
15976 case R_PPC64_GOT_TLSLD16_HA:
15977 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15978 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15980 tls_gdld_hi:
15981 if ((tls_mask & tls_gd) != 0)
15982 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15983 + R_PPC64_GOT_TPREL16_DS);
15984 else
15986 rel->r_offset -= d_offset;
15987 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15988 r_type = R_PPC64_NONE;
15990 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15992 break;
15994 case R_PPC64_GOT_TLSGD16:
15995 case R_PPC64_GOT_TLSGD16_LO:
15996 tls_gd = TLS_GDIE;
15997 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15998 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15999 goto tls_ldgd_opt;
16000 break;
16002 case R_PPC64_GOT_TLSLD16:
16003 case R_PPC64_GOT_TLSLD16_LO:
16004 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16005 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16007 unsigned int insn1, insn2;
16009 tls_ldgd_opt:
16010 offset = (bfd_vma) -1;
16011 /* If not using the newer R_PPC64_TLSGD/LD to mark
16012 __tls_get_addr calls, we must trust that the call
16013 stays with its arg setup insns, ie. that the next
16014 reloc is the __tls_get_addr call associated with
16015 the current reloc. Edit both insns. */
16016 if (input_section->nomark_tls_get_addr
16017 && rel + 1 < relend
16018 && branch_reloc_hash_match (input_bfd, rel + 1,
16019 htab->tls_get_addr_fd,
16020 htab->tga_desc_fd,
16021 htab->tls_get_addr,
16022 htab->tga_desc))
16023 offset = rel[1].r_offset;
16024 /* We read the low GOT_TLS (or TOC16) insn because we
16025 need to keep the destination reg. It may be
16026 something other than the usual r3, and moved to r3
16027 before the call by intervening code. */
16028 insn1 = bfd_get_32 (input_bfd,
16029 contents + rel->r_offset - d_offset);
16030 if ((tls_mask & tls_gd) != 0)
16032 /* IE */
16033 insn1 &= (0x1f << 21) | (0x1f << 16);
16034 insn1 |= 58u << 26; /* ld */
16035 insn2 = 0x7c636a14; /* add 3,3,13 */
16036 if (offset != (bfd_vma) -1)
16037 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16038 if (r_type == R_PPC64_TOC16
16039 || r_type == R_PPC64_TOC16_LO)
16040 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
16041 else
16042 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
16043 + R_PPC64_GOT_TPREL16_DS);
16044 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16046 else
16048 /* LE */
16049 insn1 &= 0x1f << 21;
16050 insn1 |= 0x3c0d0000; /* addis r,13,0 */
16051 insn2 = 0x38630000; /* addi 3,3,0 */
16052 if (tls_gd == 0)
16054 /* Was an LD reloc. */
16055 r_symndx = STN_UNDEF;
16056 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16058 else if (toc_symndx != 0)
16060 r_symndx = toc_symndx;
16061 rel->r_addend = toc_addend;
16063 r_type = R_PPC64_TPREL16_HA;
16064 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16065 if (offset != (bfd_vma) -1)
16067 rel[1].r_info = ELF64_R_INFO (r_symndx,
16068 R_PPC64_TPREL16_LO);
16069 rel[1].r_offset = offset + d_offset;
16070 rel[1].r_addend = rel->r_addend;
16073 bfd_put_32 (input_bfd, insn1,
16074 contents + rel->r_offset - d_offset);
16075 if (offset != (bfd_vma) -1
16076 && offset_in_range (input_section, offset, 4))
16078 bfd_put_32 (input_bfd, insn2, contents + offset);
16079 if (offset_in_range (input_section, offset + 4, 4))
16081 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16082 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
16083 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
16086 if ((tls_mask & tls_gd) == 0
16087 && (tls_gd == 0 || toc_symndx != 0))
16089 /* We changed the symbol. Start over in order
16090 to get h, sym, sec etc. right. */
16091 goto again;
16094 break;
16096 case R_PPC64_GOT_TLSGD_PCREL34:
16097 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16098 && offset_in_range (input_section, rel->r_offset, 8))
16100 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16101 pinsn <<= 32;
16102 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16103 if ((tls_mask & TLS_GDIE) != 0)
16105 /* IE, pla -> pld */
16106 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
16107 r_type = R_PPC64_GOT_TPREL_PCREL34;
16109 else
16111 /* LE, pla pcrel -> paddi r13 */
16112 pinsn += (-1ULL << 52) + (13ULL << 16);
16113 r_type = R_PPC64_TPREL34;
16115 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16116 bfd_put_32 (input_bfd, pinsn >> 32,
16117 contents + rel->r_offset);
16118 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16119 contents + rel->r_offset + 4);
16121 break;
16123 case R_PPC64_GOT_TLSLD_PCREL34:
16124 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16125 && offset_in_range (input_section, rel->r_offset, 8))
16127 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16128 pinsn <<= 32;
16129 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16130 pinsn += (-1ULL << 52) + (13ULL << 16);
16131 bfd_put_32 (input_bfd, pinsn >> 32,
16132 contents + rel->r_offset);
16133 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16134 contents + rel->r_offset + 4);
16135 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16136 r_symndx = STN_UNDEF;
16137 r_type = R_PPC64_TPREL34;
16138 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16139 goto again;
16141 break;
16143 case R_PPC64_TLSGD:
16144 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16145 && rel + 1 < relend
16146 && offset_in_range (input_section, rel->r_offset,
16147 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16148 ? 8 : 4))
16150 unsigned int insn2;
16151 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16153 offset = rel->r_offset;
16154 if (is_plt_seq_reloc (r_type1))
16156 bfd_put_32 (output_bfd, NOP, contents + offset);
16157 if (r_type1 == R_PPC64_PLT_PCREL34
16158 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16159 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16160 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16161 break;
16164 if (r_type1 == R_PPC64_PLTCALL)
16165 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16167 if ((tls_mask & TLS_GDIE) != 0)
16169 /* IE */
16170 r_type = R_PPC64_NONE;
16171 insn2 = 0x7c636a14; /* add 3,3,13 */
16173 else
16175 /* LE */
16176 if (toc_symndx != 0)
16178 r_symndx = toc_symndx;
16179 rel->r_addend = toc_addend;
16181 if (r_type1 == R_PPC64_REL24_NOTOC
16182 || r_type1 == R_PPC64_REL24_P9NOTOC
16183 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16185 r_type = R_PPC64_NONE;
16186 insn2 = NOP;
16188 else
16190 rel->r_offset = offset + d_offset;
16191 r_type = R_PPC64_TPREL16_LO;
16192 insn2 = 0x38630000; /* addi 3,3,0 */
16195 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16196 /* Zap the reloc on the _tls_get_addr call too. */
16197 BFD_ASSERT (offset == rel[1].r_offset);
16198 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16199 bfd_put_32 (input_bfd, insn2, contents + offset);
16200 if ((tls_mask & TLS_GDIE) == 0
16201 && toc_symndx != 0
16202 && r_type != R_PPC64_NONE)
16203 goto again;
16205 break;
16207 case R_PPC64_TLSLD:
16208 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16209 && rel + 1 < relend
16210 && offset_in_range (input_section, rel->r_offset,
16211 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16212 ? 8 : 4))
16214 unsigned int insn2;
16215 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16217 offset = rel->r_offset;
16218 if (is_plt_seq_reloc (r_type1))
16220 bfd_put_32 (output_bfd, NOP, contents + offset);
16221 if (r_type1 == R_PPC64_PLT_PCREL34
16222 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16223 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16224 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16225 break;
16228 if (r_type1 == R_PPC64_PLTCALL)
16229 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16231 if (r_type1 == R_PPC64_REL24_NOTOC
16232 || r_type1 == R_PPC64_REL24_P9NOTOC
16233 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16235 r_type = R_PPC64_NONE;
16236 insn2 = NOP;
16238 else
16240 rel->r_offset = offset + d_offset;
16241 r_symndx = STN_UNDEF;
16242 r_type = R_PPC64_TPREL16_LO;
16243 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16244 insn2 = 0x38630000; /* addi 3,3,0 */
16246 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16247 /* Zap the reloc on the _tls_get_addr call too. */
16248 BFD_ASSERT (offset == rel[1].r_offset);
16249 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16250 bfd_put_32 (input_bfd, insn2, contents + offset);
16251 if (r_type != R_PPC64_NONE)
16252 goto again;
16254 break;
16256 case R_PPC64_DTPMOD64:
16257 if (rel + 1 < relend
16258 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
16259 && rel[1].r_offset == rel->r_offset + 8)
16261 if ((tls_mask & TLS_GD) == 0
16262 && offset_in_range (input_section, rel->r_offset, 8))
16264 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
16265 if ((tls_mask & TLS_GDIE) != 0)
16266 r_type = R_PPC64_TPREL64;
16267 else
16269 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16270 r_type = R_PPC64_NONE;
16272 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16275 else
16277 if ((tls_mask & TLS_LD) == 0
16278 && offset_in_range (input_section, rel->r_offset, 8))
16280 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16281 r_type = R_PPC64_NONE;
16282 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16285 break;
16287 case R_PPC64_TPREL64:
16288 if ((tls_mask & TLS_TPREL) == 0)
16290 r_type = R_PPC64_NONE;
16291 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16293 break;
16295 case R_PPC64_ENTRY:
16296 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
16297 if (!bfd_link_pic (info)
16298 && !info->traditional_format
16299 && relocation + 0x80008000 <= 0xffffffff
16300 && offset_in_range (input_section, rel->r_offset, 8))
16302 unsigned int insn1, insn2;
16304 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16305 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16306 if ((insn1 & ~0xfffc) == LD_R2_0R12
16307 && insn2 == ADD_R2_R2_R12)
16309 bfd_put_32 (input_bfd,
16310 LIS_R2 + PPC_HA (relocation),
16311 contents + rel->r_offset);
16312 bfd_put_32 (input_bfd,
16313 ADDI_R2_R2 + PPC_LO (relocation),
16314 contents + rel->r_offset + 4);
16317 else
16319 relocation -= (rel->r_offset
16320 + input_section->output_offset
16321 + input_section->output_section->vma);
16322 if (relocation + 0x80008000 <= 0xffffffff
16323 && offset_in_range (input_section, rel->r_offset, 8))
16325 unsigned int insn1, insn2;
16327 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16328 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16329 if ((insn1 & ~0xfffc) == LD_R2_0R12
16330 && insn2 == ADD_R2_R2_R12)
16332 bfd_put_32 (input_bfd,
16333 ADDIS_R2_R12 + PPC_HA (relocation),
16334 contents + rel->r_offset);
16335 bfd_put_32 (input_bfd,
16336 ADDI_R2_R2 + PPC_LO (relocation),
16337 contents + rel->r_offset + 4);
16341 break;
16343 case R_PPC64_REL16_HA:
16344 /* If we are generating a non-PIC executable, edit
16345 . 0: addis 2,12,.TOC.-0b@ha
16346 . addi 2,2,.TOC.-0b@l
16347 used by ELFv2 global entry points to set up r2, to
16348 . lis 2,.TOC.@ha
16349 . addi 2,2,.TOC.@l
16350 if .TOC. is in range. */
16351 if (!bfd_link_pic (info)
16352 && !info->traditional_format
16353 && !htab->opd_abi
16354 && rel->r_addend == d_offset
16355 && h != NULL && &h->elf == htab->elf.hgot
16356 && rel + 1 < relend
16357 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
16358 && rel[1].r_offset == rel->r_offset + 4
16359 && rel[1].r_addend == rel->r_addend + 4
16360 && relocation + 0x80008000 <= 0xffffffff
16361 && offset_in_range (input_section, rel->r_offset - d_offset, 8))
16363 unsigned int insn1, insn2;
16364 offset = rel->r_offset - d_offset;
16365 insn1 = bfd_get_32 (input_bfd, contents + offset);
16366 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16367 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
16368 && (insn2 & 0xffff0000) == ADDI_R2_R2)
16370 r_type = R_PPC64_ADDR16_HA;
16371 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16372 rel->r_addend -= d_offset;
16373 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
16374 rel[1].r_addend -= d_offset + 4;
16375 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
16378 break;
16381 /* Handle other relocations that tweak non-addend part of insn. */
16382 insn = 0;
16383 max_br_offset = 1 << 25;
16384 addend = rel->r_addend;
16385 reloc_dest = DEST_NORMAL;
16386 switch (r_type)
16388 default:
16389 break;
16391 case R_PPC64_TOCSAVE:
16392 if (relocation + addend == (rel->r_offset
16393 + input_section->output_offset
16394 + input_section->output_section->vma)
16395 && tocsave_find (htab, NO_INSERT,
16396 &local_syms, rel, input_bfd)
16397 && offset_in_range (input_section, rel->r_offset, 4))
16399 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16400 if (insn == NOP
16401 || insn == CROR_151515 || insn == CROR_313131)
16402 bfd_put_32 (input_bfd,
16403 STD_R2_0R1 + STK_TOC (htab),
16404 contents + rel->r_offset);
16406 break;
16408 /* Branch taken prediction relocations. */
16409 case R_PPC64_ADDR14_BRTAKEN:
16410 case R_PPC64_REL14_BRTAKEN:
16411 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
16412 /* Fall through. */
16414 /* Branch not taken prediction relocations. */
16415 case R_PPC64_ADDR14_BRNTAKEN:
16416 case R_PPC64_REL14_BRNTAKEN:
16417 if (!offset_in_range (input_section, rel->r_offset, 4))
16418 break;
16419 insn |= bfd_get_32 (input_bfd,
16420 contents + rel->r_offset) & ~(0x01 << 21);
16421 /* Fall through. */
16423 case R_PPC64_REL14:
16424 max_br_offset = 1 << 15;
16425 /* Fall through. */
16427 case R_PPC64_REL24:
16428 case R_PPC64_REL24_NOTOC:
16429 case R_PPC64_REL24_P9NOTOC:
16430 case R_PPC64_PLTCALL:
16431 case R_PPC64_PLTCALL_NOTOC:
16432 /* Calls to functions with a different TOC, such as calls to
16433 shared objects, need to alter the TOC pointer. This is
16434 done using a linkage stub. A REL24 branching to these
16435 linkage stubs needs to be followed by a nop, as the nop
16436 will be replaced with an instruction to restore the TOC
16437 base pointer. */
16438 fdh = h;
16439 if (h != NULL
16440 && h->oh != NULL
16441 && h->oh->is_func_descriptor)
16442 fdh = ppc_follow_link (h->oh);
16443 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
16444 htab);
16445 if ((r_type == R_PPC64_PLTCALL
16446 || r_type == R_PPC64_PLTCALL_NOTOC)
16447 && stub_entry != NULL
16448 && stub_entry->type.main == ppc_stub_plt_call)
16449 stub_entry = NULL;
16451 if (stub_entry != NULL
16452 && (stub_entry->type.main == ppc_stub_plt_call
16453 || stub_entry->type.r2save))
16455 bool can_plt_call = false;
16457 if (r_type == R_PPC64_REL24_NOTOC
16458 || r_type == R_PPC64_REL24_P9NOTOC)
16460 /* NOTOC calls don't need to restore r2. */
16461 can_plt_call = true;
16463 else if (stub_entry->type.main == ppc_stub_plt_call
16464 && !htab->opd_abi
16465 && htab->params->plt_localentry0 != 0
16466 && h != NULL
16467 && is_elfv2_localentry0 (&h->elf))
16469 /* The function doesn't use or change r2. */
16470 can_plt_call = true;
16473 /* All of these stubs may modify r2, so there must be a
16474 branch and link followed by a nop. The nop is
16475 replaced by an insn to restore r2. */
16476 else if (offset_in_range (input_section, rel->r_offset, 8))
16478 unsigned long br;
16480 br = bfd_get_32 (input_bfd,
16481 contents + rel->r_offset);
16482 if ((br & 1) != 0)
16484 unsigned long nop;
16486 nop = bfd_get_32 (input_bfd,
16487 contents + rel->r_offset + 4);
16488 if (nop == LD_R2_0R1 + STK_TOC (htab))
16489 can_plt_call = true;
16490 else if (nop == NOP
16491 || nop == CROR_151515
16492 || nop == CROR_313131)
16494 if (h != NULL
16495 && is_tls_get_addr (&h->elf, htab)
16496 && htab->params->tls_get_addr_opt)
16498 /* Special stub used, leave nop alone. */
16500 else
16501 bfd_put_32 (input_bfd,
16502 LD_R2_0R1 + STK_TOC (htab),
16503 contents + rel->r_offset + 4);
16504 can_plt_call = true;
16509 if (!can_plt_call && h != NULL)
16511 const char *name = h->elf.root.root.string;
16513 if (*name == '.')
16514 ++name;
16516 if (startswith (name, "__libc_start_main")
16517 && (name[17] == 0 || name[17] == '@'))
16519 /* Allow crt1 branch to go via a toc adjusting
16520 stub. Other calls that never return could do
16521 the same, if we could detect such. */
16522 can_plt_call = true;
16526 if (!can_plt_call)
16528 /* g++ as of 20130507 emits self-calls without a
16529 following nop. This is arguably wrong since we
16530 have conflicting information. On the one hand a
16531 global symbol and on the other a local call
16532 sequence, but don't error for this special case.
16533 It isn't possible to cheaply verify we have
16534 exactly such a call. Allow all calls to the same
16535 section. */
16536 asection *code_sec = sec;
16538 if (get_opd_info (sec) != NULL)
16540 bfd_vma off = (relocation + addend
16541 - sec->output_section->vma
16542 - sec->output_offset);
16544 opd_entry_value (sec, off, &code_sec, NULL, false);
16546 if (code_sec == input_section)
16547 can_plt_call = true;
16550 if (!can_plt_call)
16552 if (stub_entry->type.main == ppc_stub_plt_call)
16553 info->callbacks->einfo
16554 /* xgettext:c-format */
16555 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16556 "(plt call stub)\n"),
16557 input_bfd, input_section, rel->r_offset, sym_name);
16558 else
16559 info->callbacks->einfo
16560 /* xgettext:c-format */
16561 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16562 "(toc save/adjust stub)\n"),
16563 input_bfd, input_section, rel->r_offset, sym_name);
16565 bfd_set_error (bfd_error_bad_value);
16566 ret = false;
16569 if (can_plt_call
16570 && stub_entry->type.main == ppc_stub_plt_call)
16571 unresolved_reloc = false;
16574 if ((stub_entry == NULL
16575 || stub_entry->type.main == ppc_stub_long_branch
16576 || stub_entry->type.main == ppc_stub_plt_branch)
16577 && get_opd_info (sec) != NULL)
16579 /* The branch destination is the value of the opd entry. */
16580 bfd_vma off = (relocation + addend
16581 - sec->output_section->vma
16582 - sec->output_offset);
16583 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
16584 if (dest != (bfd_vma) -1)
16586 relocation = dest;
16587 addend = 0;
16588 reloc_dest = DEST_OPD;
16592 /* If the branch is out of reach we ought to have a long
16593 branch stub. */
16594 from = (rel->r_offset
16595 + input_section->output_offset
16596 + input_section->output_section->vma);
16598 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
16599 ? fdh->elf.other
16600 : sym->st_other);
16602 if (stub_entry != NULL
16603 && (stub_entry->type.main == ppc_stub_long_branch
16604 || stub_entry->type.main == ppc_stub_plt_branch))
16606 if (stub_entry->type.sub == ppc_stub_toc
16607 && !stub_entry->type.r2save
16608 && (r_type == R_PPC64_ADDR14_BRTAKEN
16609 || r_type == R_PPC64_ADDR14_BRNTAKEN
16610 || (relocation + addend - from + max_br_offset
16611 < 2 * max_br_offset)))
16612 /* Don't use the stub if this branch is in range. */
16613 stub_entry = NULL;
16615 if (stub_entry != NULL
16616 && stub_entry->type.sub >= ppc_stub_notoc
16617 && ((r_type != R_PPC64_REL24_NOTOC
16618 && r_type != R_PPC64_REL24_P9NOTOC)
16619 || ((fdh ? fdh->elf.other : sym->st_other)
16620 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16621 && (relocation + addend - from + max_br_offset
16622 < 2 * max_br_offset))
16623 stub_entry = NULL;
16625 if (stub_entry != NULL
16626 && stub_entry->type.r2save
16627 && (r_type == R_PPC64_REL24_NOTOC
16628 || r_type == R_PPC64_REL24_P9NOTOC)
16629 && (relocation + addend - from + max_br_offset
16630 < 2 * max_br_offset))
16631 stub_entry = NULL;
16634 if (stub_entry != NULL)
16636 /* Munge up the value and addend so that we call the stub
16637 rather than the procedure directly. */
16638 asection *stub_sec = stub_entry->group->stub_sec;
16640 if (stub_entry->type.main == ppc_stub_save_res)
16641 relocation += (stub_sec->output_offset
16642 + stub_sec->output_section->vma
16643 + stub_sec->size - htab->sfpr->size
16644 - htab->sfpr->output_offset
16645 - htab->sfpr->output_section->vma);
16646 else
16647 relocation = (stub_entry->stub_offset
16648 + stub_sec->output_offset
16649 + stub_sec->output_section->vma);
16650 addend = 0;
16651 reloc_dest = DEST_STUB;
16653 if (((stub_entry->type.r2save
16654 && (r_type == R_PPC64_REL24_NOTOC
16655 || r_type == R_PPC64_REL24_P9NOTOC))
16656 || ((stub_entry->type.main == ppc_stub_plt_call
16657 && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
16658 && rel + 1 < relend
16659 && rel[1].r_offset == rel->r_offset + 4
16660 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
16661 && !(stub_entry->type.main == ppc_stub_plt_call
16662 && htab->params->tls_get_addr_opt
16663 && h != NULL
16664 && is_tls_get_addr (&h->elf, htab)))
16666 /* Skip over the r2 store at the start of the stub. */
16667 relocation += 4;
16670 if ((r_type == R_PPC64_REL24_NOTOC
16671 || r_type == R_PPC64_REL24_P9NOTOC)
16672 && stub_entry->type.main == ppc_stub_plt_call
16673 && stub_entry->type.sub >= ppc_stub_notoc)
16674 htab->notoc_plt = 1;
16677 if (insn != 0)
16679 if (is_isa_v2)
16681 /* Set 'a' bit. This is 0b00010 in BO field for branch
16682 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16683 for branch on CTR insns (BO == 1a00t or 1a01t). */
16684 if ((insn & (0x14 << 21)) == (0x04 << 21))
16685 insn |= 0x02 << 21;
16686 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16687 insn |= 0x08 << 21;
16688 else
16689 break;
16691 else
16693 /* Invert 'y' bit if not the default. */
16694 if ((bfd_signed_vma) (relocation + addend - from) < 0)
16695 insn ^= 0x01 << 21;
16698 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16701 /* NOP out calls to undefined weak functions.
16702 We can thus call a weak function without first
16703 checking whether the function is defined. */
16704 else if (h != NULL
16705 && h->elf.root.type == bfd_link_hash_undefweak
16706 && h->elf.dynindx == -1
16707 && (r_type == R_PPC64_REL24
16708 || r_type == R_PPC64_REL24_NOTOC
16709 || r_type == R_PPC64_REL24_P9NOTOC)
16710 && relocation == 0
16711 && addend == 0
16712 && offset_in_range (input_section, rel->r_offset, 4))
16714 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
16715 goto copy_reloc;
16717 break;
16719 case R_PPC64_GOT16_DS:
16720 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16721 || (bfd_link_pic (info)
16722 && sec == bfd_abs_section_ptr)
16723 || !htab->do_toc_opt)
16724 break;
16725 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16726 if (relocation + addend - from + 0x8000 < 0x10000
16727 && sec != NULL
16728 && sec->output_section != NULL
16729 && !discarded_section (sec)
16730 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16731 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16733 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16734 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16736 insn += (14u << 26) - (58u << 26);
16737 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16738 r_type = R_PPC64_TOC16;
16739 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16742 break;
16744 case R_PPC64_GOT16_LO_DS:
16745 case R_PPC64_GOT16_HA:
16746 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16747 || (bfd_link_pic (info)
16748 && sec == bfd_abs_section_ptr)
16749 || !htab->do_toc_opt)
16750 break;
16751 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16752 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
16753 && sec != NULL
16754 && sec->output_section != NULL
16755 && !discarded_section (sec)
16756 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16757 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16759 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16760 if (r_type == R_PPC64_GOT16_LO_DS
16761 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16763 insn += (14u << 26) - (58u << 26);
16764 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16765 r_type = R_PPC64_TOC16_LO;
16766 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16768 else if (r_type == R_PPC64_GOT16_HA
16769 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
16771 r_type = R_PPC64_TOC16_HA;
16772 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16775 break;
16777 case R_PPC64_GOT_PCREL34:
16778 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16779 || (bfd_link_pic (info)
16780 && sec == bfd_abs_section_ptr)
16781 || !htab->do_toc_opt)
16782 break;
16783 from = (rel->r_offset
16784 + input_section->output_section->vma
16785 + input_section->output_offset);
16786 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
16787 && sec != NULL
16788 && sec->output_section != NULL
16789 && !discarded_section (sec)
16790 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16791 && offset_in_range (input_section, rel->r_offset, 8)))
16792 break;
16794 offset = rel->r_offset;
16795 pinsn = bfd_get_32 (input_bfd, contents + offset);
16796 pinsn <<= 32;
16797 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16798 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16799 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16800 break;
16802 /* Replace with paddi. */
16803 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16804 r_type = R_PPC64_PCREL34;
16805 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16806 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16807 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16808 /* Fall through. */
16810 case R_PPC64_PCREL34:
16811 if (!htab->params->no_pcrel_opt
16812 && rel + 1 < relend
16813 && rel[1].r_offset == rel->r_offset
16814 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
16815 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16816 && offset_in_range (input_section, rel->r_offset, 8))
16818 offset = rel->r_offset;
16819 pinsn = bfd_get_32 (input_bfd, contents + offset);
16820 pinsn <<= 32;
16821 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16822 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16823 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16824 | (14ULL << 26) /* paddi */))
16826 bfd_vma off2 = rel[1].r_addend;
16827 if (off2 == 0)
16828 /* zero means next insn. */
16829 off2 = 8;
16830 off2 += offset;
16831 if (offset_in_range (input_section, off2, 4))
16833 uint64_t pinsn2;
16834 bfd_signed_vma addend_off;
16835 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16836 pinsn2 <<= 32;
16837 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16839 if (!offset_in_range (input_section, off2, 8))
16840 break;
16841 pinsn2 |= bfd_get_32 (input_bfd,
16842 contents + off2 + 4);
16844 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16846 addend += addend_off;
16847 rel->r_addend = addend;
16848 bfd_put_32 (input_bfd, pinsn >> 32,
16849 contents + offset);
16850 bfd_put_32 (input_bfd, pinsn,
16851 contents + offset + 4);
16852 bfd_put_32 (input_bfd, pinsn2 >> 32,
16853 contents + off2);
16854 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16855 bfd_put_32 (input_bfd, pinsn2,
16856 contents + off2 + 4);
16861 break;
16864 tls_type = 0;
16865 save_unresolved_reloc = unresolved_reloc;
16866 switch (r_type)
16868 default:
16869 /* xgettext:c-format */
16870 _bfd_error_handler (_("%pB: %s unsupported"),
16871 input_bfd, ppc64_elf_howto_table[r_type]->name);
16873 bfd_set_error (bfd_error_bad_value);
16874 ret = false;
16875 goto copy_reloc;
16877 case R_PPC64_NONE:
16878 case R_PPC64_TLS:
16879 case R_PPC64_TLSGD:
16880 case R_PPC64_TLSLD:
16881 case R_PPC64_TOCSAVE:
16882 case R_PPC64_GNU_VTINHERIT:
16883 case R_PPC64_GNU_VTENTRY:
16884 case R_PPC64_ENTRY:
16885 case R_PPC64_PCREL_OPT:
16886 goto copy_reloc;
16888 /* GOT16 relocations. Like an ADDR16 using the symbol's
16889 address in the GOT as relocation value instead of the
16890 symbol's value itself. Also, create a GOT entry for the
16891 symbol and put the symbol value there. */
16892 case R_PPC64_GOT_TLSGD16:
16893 case R_PPC64_GOT_TLSGD16_LO:
16894 case R_PPC64_GOT_TLSGD16_HI:
16895 case R_PPC64_GOT_TLSGD16_HA:
16896 case R_PPC64_GOT_TLSGD_PCREL34:
16897 tls_type = TLS_TLS | TLS_GD;
16898 goto dogot;
16900 case R_PPC64_GOT_TLSLD16:
16901 case R_PPC64_GOT_TLSLD16_LO:
16902 case R_PPC64_GOT_TLSLD16_HI:
16903 case R_PPC64_GOT_TLSLD16_HA:
16904 case R_PPC64_GOT_TLSLD_PCREL34:
16905 tls_type = TLS_TLS | TLS_LD;
16906 goto dogot;
16908 case R_PPC64_GOT_TPREL16_DS:
16909 case R_PPC64_GOT_TPREL16_LO_DS:
16910 case R_PPC64_GOT_TPREL16_HI:
16911 case R_PPC64_GOT_TPREL16_HA:
16912 case R_PPC64_GOT_TPREL_PCREL34:
16913 tls_type = TLS_TLS | TLS_TPREL;
16914 goto dogot;
16916 case R_PPC64_GOT_DTPREL16_DS:
16917 case R_PPC64_GOT_DTPREL16_LO_DS:
16918 case R_PPC64_GOT_DTPREL16_HI:
16919 case R_PPC64_GOT_DTPREL16_HA:
16920 case R_PPC64_GOT_DTPREL_PCREL34:
16921 tls_type = TLS_TLS | TLS_DTPREL;
16922 goto dogot;
16924 case R_PPC64_GOT16:
16925 case R_PPC64_GOT16_LO:
16926 case R_PPC64_GOT16_HI:
16927 case R_PPC64_GOT16_HA:
16928 case R_PPC64_GOT16_DS:
16929 case R_PPC64_GOT16_LO_DS:
16930 case R_PPC64_GOT_PCREL34:
16931 dogot:
16933 /* Relocation is to the entry for this symbol in the global
16934 offset table. */
16935 asection *got;
16936 bfd_vma *offp;
16937 bfd_vma off;
16938 unsigned long indx = 0;
16939 struct got_entry *ent;
16941 if (tls_type == (TLS_TLS | TLS_LD)
16942 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
16943 ent = ppc64_tlsld_got (input_bfd);
16944 else
16946 if (h != NULL)
16948 if (!htab->elf.dynamic_sections_created
16949 || h->elf.dynindx == -1
16950 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16951 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16952 /* This is actually a static link, or it is a
16953 -Bsymbolic link and the symbol is defined
16954 locally, or the symbol was forced to be local
16955 because of a version file. */
16957 else
16959 indx = h->elf.dynindx;
16960 unresolved_reloc = false;
16962 ent = h->elf.got.glist;
16964 else
16966 if (local_got_ents == NULL)
16967 abort ();
16968 ent = local_got_ents[r_symndx];
16971 for (; ent != NULL; ent = ent->next)
16972 if (ent->addend == orig_rel.r_addend
16973 && ent->owner == input_bfd
16974 && ent->tls_type == tls_type)
16975 break;
16978 if (ent == NULL)
16979 abort ();
16980 if (ent->is_indirect)
16981 ent = ent->got.ent;
16982 offp = &ent->got.offset;
16983 got = ppc64_elf_tdata (ent->owner)->got;
16984 if (got == NULL)
16985 abort ();
16987 /* The offset must always be a multiple of 8. We use the
16988 least significant bit to record whether we have already
16989 processed this entry. */
16990 off = *offp;
16991 if ((off & 1) != 0)
16992 off &= ~1;
16993 else
16995 /* Generate relocs for the dynamic linker, except in
16996 the case of TLSLD where we'll use one entry per
16997 module. */
16998 asection *relgot;
16999 bool ifunc;
17001 *offp = off | 1;
17002 relgot = NULL;
17003 ifunc = (h != NULL
17004 ? h->elf.type == STT_GNU_IFUNC
17005 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
17006 if (ifunc)
17008 relgot = htab->elf.irelplt;
17009 if (indx == 0 || is_static_defined (&h->elf))
17010 htab->elf.ifunc_resolvers = true;
17012 else if (indx != 0
17013 || (bfd_link_pic (info)
17014 && (h == NULL
17015 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
17016 && !(tls_type != 0
17017 && bfd_link_executable (info)
17018 && (h == NULL
17019 || SYMBOL_REFERENCES_LOCAL (info,
17020 &h->elf)))
17021 && (h != NULL
17022 ? !bfd_is_abs_symbol (&h->elf.root)
17023 : sym->st_shndx != SHN_ABS)))
17025 relgot = ppc64_elf_tdata (ent->owner)->relgot;
17026 if (relgot != NULL)
17028 outrel.r_offset = (got->output_section->vma
17029 + got->output_offset
17030 + off);
17031 outrel.r_addend = orig_rel.r_addend;
17032 if (tls_type & (TLS_LD | TLS_GD))
17034 outrel.r_addend = 0;
17035 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
17036 if (tls_type == (TLS_TLS | TLS_GD))
17038 BFD_ASSERT (count_and_swap_reloc_out (output_bfd,
17039 &outrel,
17040 relgot));
17041 outrel.r_offset += 8;
17042 outrel.r_addend = orig_rel.r_addend;
17043 outrel.r_info
17044 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
17047 else if (tls_type == (TLS_TLS | TLS_DTPREL))
17048 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
17049 else if (tls_type == (TLS_TLS | TLS_TPREL))
17050 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
17051 else if (indx != 0)
17052 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
17053 else
17055 if (ifunc)
17056 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17057 else
17058 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
17060 /* Write the .got section contents for the sake
17061 of prelink. */
17062 loc = got->contents + off;
17063 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
17064 loc);
17067 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
17069 outrel.r_addend += relocation;
17070 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
17072 if (htab->elf.tls_sec == NULL)
17073 outrel.r_addend = 0;
17074 else
17075 outrel.r_addend -= htab->elf.tls_sec->vma;
17078 if (!(info->enable_dt_relr
17079 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
17080 BFD_ASSERT (count_and_swap_reloc_out (output_bfd,
17081 &outrel, relgot));
17084 /* Init the .got section contents here if we're not
17085 emitting a reloc. */
17086 else
17088 relocation += orig_rel.r_addend;
17089 if (tls_type != 0)
17091 if (htab->elf.tls_sec == NULL)
17092 relocation = 0;
17093 else
17095 if (tls_type & TLS_LD)
17096 relocation = 0;
17097 else
17098 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
17099 if (tls_type & TLS_TPREL)
17100 relocation += DTP_OFFSET - TP_OFFSET;
17103 if (tls_type & (TLS_GD | TLS_LD))
17105 bfd_put_64 (output_bfd, relocation,
17106 got->contents + off + 8);
17107 relocation = 1;
17110 bfd_put_64 (output_bfd, relocation,
17111 got->contents + off);
17115 if (off >= (bfd_vma) -2)
17116 abort ();
17118 relocation = got->output_section->vma + got->output_offset + off;
17119 addend = 0;
17120 if (!(r_type == R_PPC64_GOT_PCREL34
17121 || r_type == R_PPC64_GOT_TLSGD_PCREL34
17122 || r_type == R_PPC64_GOT_TLSLD_PCREL34
17123 || r_type == R_PPC64_GOT_TPREL_PCREL34
17124 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
17125 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
17127 break;
17129 case R_PPC64_PLT16_HA:
17130 case R_PPC64_PLT16_HI:
17131 case R_PPC64_PLT16_LO:
17132 case R_PPC64_PLT16_LO_DS:
17133 case R_PPC64_PLT_PCREL34:
17134 case R_PPC64_PLT_PCREL34_NOTOC:
17135 case R_PPC64_PLT32:
17136 case R_PPC64_PLT64:
17137 case R_PPC64_PLTSEQ:
17138 case R_PPC64_PLTSEQ_NOTOC:
17139 case R_PPC64_PLTCALL:
17140 case R_PPC64_PLTCALL_NOTOC:
17141 /* Relocation is to the entry for this symbol in the
17142 procedure linkage table. */
17143 unresolved_reloc = true;
17145 struct plt_entry **plt_list = NULL;
17146 if (h != NULL)
17147 plt_list = &h->elf.plt.plist;
17148 else if (local_got_ents != NULL)
17150 struct plt_entry **local_plt = (struct plt_entry **)
17151 (local_got_ents + symtab_hdr->sh_info);
17152 plt_list = local_plt + r_symndx;
17154 if (plt_list)
17156 struct plt_entry *ent;
17158 for (ent = *plt_list; ent != NULL; ent = ent->next)
17159 if (ent->plt.offset != (bfd_vma) -1
17160 && ent->addend == orig_rel.r_addend)
17162 asection *plt;
17163 bfd_vma got;
17165 plt = htab->elf.splt;
17166 if (use_local_plt (info, elf_hash_entry (h)))
17168 if (h != NULL
17169 ? h->elf.type == STT_GNU_IFUNC
17170 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17171 plt = htab->elf.iplt;
17172 else
17173 plt = htab->pltlocal;
17175 relocation = (plt->output_section->vma
17176 + plt->output_offset
17177 + ent->plt.offset);
17178 if (r_type == R_PPC64_PLT16_HA
17179 || r_type == R_PPC64_PLT16_HI
17180 || r_type == R_PPC64_PLT16_LO
17181 || r_type == R_PPC64_PLT16_LO_DS)
17183 got = (elf_gp (output_bfd)
17184 + htab->sec_info[input_section->id].toc_off);
17185 relocation -= got;
17187 addend = 0;
17188 unresolved_reloc = false;
17189 break;
17193 break;
17195 case R_PPC64_TOC:
17196 /* Relocation value is TOC base. */
17197 relocation = TOCstart;
17198 if (r_symndx == STN_UNDEF)
17199 relocation += htab->sec_info[input_section->id].toc_off;
17200 else if (unresolved_reloc)
17202 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
17203 relocation += htab->sec_info[sec->id].toc_off;
17204 else
17205 unresolved_reloc = true;
17206 if (unresolved_reloc
17207 || (!is_opd
17208 && h != NULL
17209 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
17210 info->callbacks->einfo
17211 /* xgettext:c-format */
17212 (_("%H: %s against %pT is not supported\n"),
17213 input_bfd, input_section, rel->r_offset,
17214 ppc64_elf_howto_table[r_type]->name, sym_name);
17215 goto dodyn;
17217 /* TOC16 relocs. We want the offset relative to the TOC base,
17218 which is the address of the start of the TOC plus 0x8000.
17219 The TOC consists of sections .got, .toc, .tocbss, and .plt,
17220 in this order. */
17221 case R_PPC64_TOC16:
17222 case R_PPC64_TOC16_LO:
17223 case R_PPC64_TOC16_HI:
17224 case R_PPC64_TOC16_DS:
17225 case R_PPC64_TOC16_LO_DS:
17226 case R_PPC64_TOC16_HA:
17227 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
17228 if (h != NULL)
17229 goto dodyn;
17230 break;
17232 /* Relocate against the beginning of the section. */
17233 case R_PPC64_SECTOFF:
17234 case R_PPC64_SECTOFF_LO:
17235 case R_PPC64_SECTOFF_HI:
17236 case R_PPC64_SECTOFF_DS:
17237 case R_PPC64_SECTOFF_LO_DS:
17238 case R_PPC64_SECTOFF_HA:
17239 if (sec != NULL)
17240 addend -= sec->output_section->vma;
17241 break;
17243 case R_PPC64_REL16:
17244 case R_PPC64_REL16_LO:
17245 case R_PPC64_REL16_HI:
17246 case R_PPC64_REL16_HA:
17247 case R_PPC64_REL16_HIGH:
17248 case R_PPC64_REL16_HIGHA:
17249 case R_PPC64_REL16_HIGHER:
17250 case R_PPC64_REL16_HIGHERA:
17251 case R_PPC64_REL16_HIGHEST:
17252 case R_PPC64_REL16_HIGHESTA:
17253 case R_PPC64_REL16_HIGHER34:
17254 case R_PPC64_REL16_HIGHERA34:
17255 case R_PPC64_REL16_HIGHEST34:
17256 case R_PPC64_REL16_HIGHESTA34:
17257 case R_PPC64_REL16DX_HA:
17258 case R_PPC64_REL14:
17259 case R_PPC64_REL14_BRNTAKEN:
17260 case R_PPC64_REL14_BRTAKEN:
17261 case R_PPC64_REL24:
17262 case R_PPC64_REL24_NOTOC:
17263 case R_PPC64_REL24_P9NOTOC:
17264 case R_PPC64_PCREL34:
17265 case R_PPC64_PCREL28:
17266 break;
17268 case R_PPC64_TPREL16:
17269 case R_PPC64_TPREL16_LO:
17270 case R_PPC64_TPREL16_HI:
17271 case R_PPC64_TPREL16_HA:
17272 case R_PPC64_TPREL16_DS:
17273 case R_PPC64_TPREL16_LO_DS:
17274 case R_PPC64_TPREL16_HIGH:
17275 case R_PPC64_TPREL16_HIGHA:
17276 case R_PPC64_TPREL16_HIGHER:
17277 case R_PPC64_TPREL16_HIGHERA:
17278 case R_PPC64_TPREL16_HIGHEST:
17279 case R_PPC64_TPREL16_HIGHESTA:
17280 if (h != NULL
17281 && h->elf.root.type == bfd_link_hash_undefweak
17282 && h->elf.dynindx == -1
17283 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
17285 /* Make this relocation against an undefined weak symbol
17286 resolve to zero. This is really just a tweak, since
17287 code using weak externs ought to check that they are
17288 defined before using them. */
17289 bfd_byte *p = contents + rel->r_offset - d_offset;
17291 insn = bfd_get_32 (input_bfd, p);
17292 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
17293 if (insn != 0)
17294 bfd_put_32 (input_bfd, insn, p);
17295 break;
17297 /* Fall through. */
17299 case R_PPC64_TPREL34:
17300 if (htab->elf.tls_sec != NULL)
17301 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17302 /* The TPREL16 relocs shouldn't really be used in shared
17303 libs or with non-local symbols as that will result in
17304 DT_TEXTREL being set, but support them anyway. */
17305 goto dodyn;
17307 case R_PPC64_DTPREL16:
17308 case R_PPC64_DTPREL16_LO:
17309 case R_PPC64_DTPREL16_HI:
17310 case R_PPC64_DTPREL16_HA:
17311 case R_PPC64_DTPREL16_DS:
17312 case R_PPC64_DTPREL16_LO_DS:
17313 case R_PPC64_DTPREL16_HIGH:
17314 case R_PPC64_DTPREL16_HIGHA:
17315 case R_PPC64_DTPREL16_HIGHER:
17316 case R_PPC64_DTPREL16_HIGHERA:
17317 case R_PPC64_DTPREL16_HIGHEST:
17318 case R_PPC64_DTPREL16_HIGHESTA:
17319 case R_PPC64_DTPREL34:
17320 if (htab->elf.tls_sec != NULL)
17321 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17322 break;
17324 case R_PPC64_ADDR64_LOCAL:
17325 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
17326 ? h->elf.other
17327 : sym->st_other);
17328 break;
17330 case R_PPC64_DTPMOD64:
17331 relocation = 1;
17332 addend = 0;
17333 goto dodyn;
17335 case R_PPC64_TPREL64:
17336 if (htab->elf.tls_sec != NULL)
17337 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17338 goto dodyn;
17340 case R_PPC64_DTPREL64:
17341 if (htab->elf.tls_sec != NULL)
17342 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17343 /* Fall through. */
17345 /* Relocations that may need to be propagated if this is a
17346 dynamic object. */
17347 case R_PPC64_REL30:
17348 case R_PPC64_REL32:
17349 case R_PPC64_REL64:
17350 case R_PPC64_ADDR14:
17351 case R_PPC64_ADDR14_BRNTAKEN:
17352 case R_PPC64_ADDR14_BRTAKEN:
17353 case R_PPC64_ADDR16:
17354 case R_PPC64_ADDR16_DS:
17355 case R_PPC64_ADDR16_HA:
17356 case R_PPC64_ADDR16_HI:
17357 case R_PPC64_ADDR16_HIGH:
17358 case R_PPC64_ADDR16_HIGHA:
17359 case R_PPC64_ADDR16_HIGHER:
17360 case R_PPC64_ADDR16_HIGHERA:
17361 case R_PPC64_ADDR16_HIGHEST:
17362 case R_PPC64_ADDR16_HIGHESTA:
17363 case R_PPC64_ADDR16_LO:
17364 case R_PPC64_ADDR16_LO_DS:
17365 case R_PPC64_ADDR16_HIGHER34:
17366 case R_PPC64_ADDR16_HIGHERA34:
17367 case R_PPC64_ADDR16_HIGHEST34:
17368 case R_PPC64_ADDR16_HIGHESTA34:
17369 case R_PPC64_ADDR24:
17370 case R_PPC64_ADDR32:
17371 case R_PPC64_ADDR64:
17372 case R_PPC64_UADDR16:
17373 case R_PPC64_UADDR32:
17374 case R_PPC64_UADDR64:
17375 case R_PPC64_D34:
17376 case R_PPC64_D34_LO:
17377 case R_PPC64_D34_HI30:
17378 case R_PPC64_D34_HA30:
17379 case R_PPC64_D28:
17380 dodyn:
17381 if ((input_section->flags & SEC_ALLOC) == 0)
17382 break;
17384 if (NO_OPD_RELOCS && is_opd)
17385 break;
17387 if (bfd_link_pic (info)
17388 ? ((h == NULL
17389 || h->elf.dyn_relocs != NULL)
17390 && ((h != NULL && pc_dynrelocs (h))
17391 || must_be_dyn_reloc (info, r_type)))
17392 : (h != NULL
17393 ? h->elf.dyn_relocs != NULL
17394 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17396 bool skip, relocate;
17397 asection *sreloc;
17398 bfd_vma out_off;
17399 long indx = 0;
17401 /* When generating a dynamic object, these relocations
17402 are copied into the output file to be resolved at run
17403 time. */
17405 skip = false;
17406 relocate = false;
17408 out_off = _bfd_elf_section_offset (output_bfd, info,
17409 input_section, rel->r_offset);
17410 if (out_off == (bfd_vma) -1)
17411 skip = true;
17412 else if (out_off == (bfd_vma) -2)
17413 skip = true, relocate = true;
17414 out_off += (input_section->output_section->vma
17415 + input_section->output_offset);
17416 outrel.r_offset = out_off;
17417 outrel.r_addend = rel->r_addend;
17419 /* Optimize unaligned reloc use. */
17420 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
17421 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
17422 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
17423 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
17424 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
17425 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
17426 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
17427 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
17428 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
17430 if (skip)
17431 memset (&outrel, 0, sizeof outrel);
17432 else if (h != NULL
17433 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
17434 && !is_opd
17435 && r_type != R_PPC64_TOC)
17437 indx = h->elf.dynindx;
17438 BFD_ASSERT (indx != -1);
17439 outrel.r_info = ELF64_R_INFO (indx, r_type);
17441 else
17443 /* This symbol is local, or marked to become local,
17444 or this is an opd section reloc which must point
17445 at a local function. */
17446 outrel.r_addend += relocation;
17447 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
17449 if (is_opd && h != NULL)
17451 /* Lie about opd entries. This case occurs
17452 when building shared libraries and we
17453 reference a function in another shared
17454 lib. The same thing happens for a weak
17455 definition in an application that's
17456 overridden by a strong definition in a
17457 shared lib. (I believe this is a generic
17458 bug in binutils handling of weak syms.)
17459 In these cases we won't use the opd
17460 entry in this lib. */
17461 unresolved_reloc = false;
17463 if (!is_opd
17464 && r_type == R_PPC64_ADDR64
17465 && (h != NULL
17466 ? h->elf.type == STT_GNU_IFUNC
17467 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17468 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17469 else
17471 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
17473 /* We need to relocate .opd contents for ld.so.
17474 Prelink also wants simple and consistent rules
17475 for relocs. This make all RELATIVE relocs have
17476 *r_offset equal to r_addend. */
17477 relocate = true;
17480 else
17482 if (h != NULL
17483 ? h->elf.type == STT_GNU_IFUNC
17484 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17486 info->callbacks->einfo
17487 /* xgettext:c-format */
17488 (_("%H: %s for indirect "
17489 "function `%pT' unsupported\n"),
17490 input_bfd, input_section, rel->r_offset,
17491 ppc64_elf_howto_table[r_type]->name,
17492 sym_name);
17493 ret = false;
17495 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
17497 else if (sec == NULL || sec->owner == NULL)
17499 bfd_set_error (bfd_error_bad_value);
17500 return false;
17502 else
17504 asection *osec = sec->output_section;
17506 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
17508 /* TLS symbol values are relative to the
17509 TLS segment. Dynamic relocations for
17510 local TLS symbols therefore can't be
17511 reduced to a relocation against their
17512 section symbol because it holds the
17513 address of the section, not a value
17514 relative to the TLS segment. We could
17515 change the .tdata dynamic section symbol
17516 to be zero value but STN_UNDEF works
17517 and is used elsewhere, eg. for TPREL64
17518 GOT relocs against local TLS symbols. */
17519 osec = htab->elf.tls_sec;
17520 indx = 0;
17522 else
17524 indx = elf_section_data (osec)->dynindx;
17525 if (indx == 0)
17527 if ((osec->flags & SEC_READONLY) == 0
17528 && htab->elf.data_index_section != NULL)
17529 osec = htab->elf.data_index_section;
17530 else
17531 osec = htab->elf.text_index_section;
17532 indx = elf_section_data (osec)->dynindx;
17534 BFD_ASSERT (indx != 0);
17537 /* We are turning this relocation into one
17538 against a section symbol, so subtract out
17539 the output section's address but not the
17540 offset of the input section in the output
17541 section. */
17542 outrel.r_addend -= osec->vma;
17545 outrel.r_info = ELF64_R_INFO (indx, r_type);
17549 if (!(info->enable_dt_relr
17550 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
17551 && maybe_relr (ELF64_R_TYPE (orig_rel.r_info),
17552 rel, input_section)))
17554 sreloc = elf_section_data (input_section)->sreloc;
17555 if (h != NULL
17556 ? h->elf.type == STT_GNU_IFUNC
17557 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17559 sreloc = htab->elf.irelplt;
17560 if (indx == 0 || is_static_defined (&h->elf))
17561 htab->elf.ifunc_resolvers = true;
17563 if (sreloc == NULL)
17564 abort ();
17566 BFD_ASSERT (count_and_swap_reloc_out (output_bfd, &outrel,
17567 sreloc));
17570 if (!warned_dynamic
17571 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
17573 info->callbacks->einfo
17574 /* xgettext:c-format */
17575 (_("%X%P: %pB: %s against %pT "
17576 "is not supported by glibc as a dynamic relocation\n"),
17577 input_bfd,
17578 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
17579 sym_name);
17580 warned_dynamic = true;
17583 /* If this reloc is against an external symbol, it will
17584 be computed at runtime, so there's no need to do
17585 anything now. However, for the sake of prelink ensure
17586 that the section contents are a known value. */
17587 if (!relocate)
17589 unresolved_reloc = false;
17590 /* The value chosen here is quite arbitrary as ld.so
17591 ignores section contents except for the special
17592 case of .opd where the contents might be accessed
17593 before relocation. Choose zero, as that won't
17594 cause reloc overflow. */
17595 relocation = 0;
17596 addend = 0;
17597 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
17598 to improve backward compatibility with older
17599 versions of ld. */
17600 if (r_type == R_PPC64_ADDR64)
17601 addend = outrel.r_addend;
17602 /* Adjust pc_relative relocs to have zero in *r_offset. */
17603 else if (ppc64_elf_howto_table[r_type]->pc_relative)
17604 addend = outrel.r_offset;
17607 break;
17609 case R_PPC64_COPY:
17610 case R_PPC64_GLOB_DAT:
17611 case R_PPC64_JMP_SLOT:
17612 case R_PPC64_JMP_IREL:
17613 case R_PPC64_RELATIVE:
17614 /* We shouldn't ever see these dynamic relocs in relocatable
17615 files. */
17616 /* Fall through. */
17618 case R_PPC64_PLTGOT16:
17619 case R_PPC64_PLTGOT16_DS:
17620 case R_PPC64_PLTGOT16_HA:
17621 case R_PPC64_PLTGOT16_HI:
17622 case R_PPC64_PLTGOT16_LO:
17623 case R_PPC64_PLTGOT16_LO_DS:
17624 case R_PPC64_PLTREL32:
17625 case R_PPC64_PLTREL64:
17626 /* These ones haven't been implemented yet. */
17628 info->callbacks->einfo
17629 /* xgettext:c-format */
17630 (_("%P: %pB: %s is not supported for `%pT'\n"),
17631 input_bfd,
17632 ppc64_elf_howto_table[r_type]->name, sym_name);
17634 bfd_set_error (bfd_error_invalid_operation);
17635 ret = false;
17636 goto copy_reloc;
17639 /* Multi-instruction sequences that access the TOC can be
17640 optimized, eg. addis ra,r2,0; addi rb,ra,x;
17641 to nop; addi rb,r2,x; */
17642 switch (r_type)
17644 default:
17645 break;
17647 case R_PPC64_GOT_TLSLD16_HI:
17648 case R_PPC64_GOT_TLSGD16_HI:
17649 case R_PPC64_GOT_TPREL16_HI:
17650 case R_PPC64_GOT_DTPREL16_HI:
17651 case R_PPC64_GOT16_HI:
17652 case R_PPC64_TOC16_HI:
17653 /* These relocs would only be useful if building up an
17654 offset to later add to r2, perhaps in an indexed
17655 addressing mode instruction. Don't try to optimize.
17656 Unfortunately, the possibility of someone building up an
17657 offset like this or even with the HA relocs, means that
17658 we need to check the high insn when optimizing the low
17659 insn. */
17660 break;
17662 case R_PPC64_PLTCALL_NOTOC:
17663 if (!unresolved_reloc)
17664 htab->notoc_plt = 1;
17665 /* Fall through. */
17666 case R_PPC64_PLTCALL:
17667 if (unresolved_reloc
17668 && offset_in_range (input_section, rel->r_offset,
17669 r_type == R_PPC64_PLTCALL ? 8 : 4))
17671 /* No plt entry. Make this into a direct call. */
17672 bfd_byte *p = contents + rel->r_offset;
17673 insn = bfd_get_32 (input_bfd, p);
17674 insn &= 1;
17675 bfd_put_32 (input_bfd, B_DOT | insn, p);
17676 if (r_type == R_PPC64_PLTCALL)
17677 bfd_put_32 (input_bfd, NOP, p + 4);
17678 unresolved_reloc = save_unresolved_reloc;
17679 r_type = R_PPC64_REL24;
17681 break;
17683 case R_PPC64_PLTSEQ_NOTOC:
17684 case R_PPC64_PLTSEQ:
17685 if (unresolved_reloc)
17687 unresolved_reloc = false;
17688 goto nop_it;
17690 break;
17692 case R_PPC64_PLT_PCREL34_NOTOC:
17693 if (!unresolved_reloc)
17694 htab->notoc_plt = 1;
17695 /* Fall through. */
17696 case R_PPC64_PLT_PCREL34:
17697 if (unresolved_reloc
17698 && offset_in_range (input_section, rel->r_offset, 8))
17700 bfd_byte *p = contents + rel->r_offset;
17701 bfd_put_32 (input_bfd, PNOP >> 32, p);
17702 bfd_put_32 (input_bfd, PNOP, p + 4);
17703 unresolved_reloc = false;
17704 goto copy_reloc;
17706 break;
17708 case R_PPC64_PLT16_HA:
17709 if (unresolved_reloc)
17711 unresolved_reloc = false;
17712 goto nop_it;
17714 /* Fall through. */
17715 case R_PPC64_GOT_TLSLD16_HA:
17716 case R_PPC64_GOT_TLSGD16_HA:
17717 case R_PPC64_GOT_TPREL16_HA:
17718 case R_PPC64_GOT_DTPREL16_HA:
17719 case R_PPC64_GOT16_HA:
17720 case R_PPC64_TOC16_HA:
17721 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17722 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17723 && !(bfd_link_pic (info)
17724 && (h != NULL
17725 ? bfd_is_abs_symbol (&h->elf.root)
17726 : sec == bfd_abs_section_ptr)))
17728 bfd_byte *p;
17729 nop_it:
17730 if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17732 p = contents + (rel->r_offset & ~3);
17733 bfd_put_32 (input_bfd, NOP, p);
17734 goto copy_reloc;
17737 break;
17739 case R_PPC64_PLT16_LO:
17740 case R_PPC64_PLT16_LO_DS:
17741 if (unresolved_reloc)
17743 unresolved_reloc = false;
17744 goto nop_it;
17746 /* Fall through. */
17747 case R_PPC64_GOT_TLSLD16_LO:
17748 case R_PPC64_GOT_TLSGD16_LO:
17749 case R_PPC64_GOT_TPREL16_LO_DS:
17750 case R_PPC64_GOT_DTPREL16_LO_DS:
17751 case R_PPC64_GOT16_LO:
17752 case R_PPC64_GOT16_LO_DS:
17753 case R_PPC64_TOC16_LO:
17754 case R_PPC64_TOC16_LO_DS:
17755 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17756 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17757 && !(bfd_link_pic (info)
17758 && (h != NULL
17759 ? bfd_is_abs_symbol (&h->elf.root)
17760 : sec == bfd_abs_section_ptr))
17761 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17763 bfd_byte *p = contents + (rel->r_offset & ~3);
17764 insn = bfd_get_32 (input_bfd, p);
17765 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
17767 /* Transform addic to addi when we change reg. */
17768 insn &= ~((0x3fu << 26) | (0x1f << 16));
17769 insn |= (14u << 26) | (2 << 16);
17771 else
17773 insn &= ~(0x1f << 16);
17774 insn |= 2 << 16;
17776 bfd_put_32 (input_bfd, insn, p);
17778 break;
17780 case R_PPC64_TPREL16_HA:
17781 if (htab->do_tls_opt
17782 && relocation + addend + 0x8000 < 0x10000
17783 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17785 bfd_byte *p = contents + (rel->r_offset & ~3);
17786 bfd_put_32 (input_bfd, NOP, p);
17787 goto copy_reloc;
17789 break;
17791 case R_PPC64_TPREL16_LO:
17792 case R_PPC64_TPREL16_LO_DS:
17793 if (htab->do_tls_opt
17794 && relocation + addend + 0x8000 < 0x10000
17795 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17797 bfd_byte *p = contents + (rel->r_offset & ~3);
17798 insn = bfd_get_32 (input_bfd, p);
17799 insn &= ~(0x1f << 16);
17800 insn |= 13 << 16;
17801 bfd_put_32 (input_bfd, insn, p);
17803 break;
17806 /* Do any further special processing. */
17807 switch (r_type)
17809 default:
17810 break;
17812 case R_PPC64_REL16_HA:
17813 case R_PPC64_REL16_HIGHA:
17814 case R_PPC64_REL16_HIGHERA:
17815 case R_PPC64_REL16_HIGHESTA:
17816 case R_PPC64_REL16DX_HA:
17817 case R_PPC64_ADDR16_HA:
17818 case R_PPC64_ADDR16_HIGHA:
17819 case R_PPC64_ADDR16_HIGHERA:
17820 case R_PPC64_ADDR16_HIGHESTA:
17821 case R_PPC64_TOC16_HA:
17822 case R_PPC64_SECTOFF_HA:
17823 case R_PPC64_TPREL16_HA:
17824 case R_PPC64_TPREL16_HIGHA:
17825 case R_PPC64_TPREL16_HIGHERA:
17826 case R_PPC64_TPREL16_HIGHESTA:
17827 case R_PPC64_DTPREL16_HA:
17828 case R_PPC64_DTPREL16_HIGHA:
17829 case R_PPC64_DTPREL16_HIGHERA:
17830 case R_PPC64_DTPREL16_HIGHESTA:
17831 /* It's just possible that this symbol is a weak symbol
17832 that's not actually defined anywhere. In that case,
17833 'sec' would be NULL, and we should leave the symbol
17834 alone (it will be set to zero elsewhere in the link). */
17835 if (sec == NULL)
17836 break;
17837 /* Fall through. */
17839 case R_PPC64_GOT16_HA:
17840 case R_PPC64_PLTGOT16_HA:
17841 case R_PPC64_PLT16_HA:
17842 case R_PPC64_GOT_TLSGD16_HA:
17843 case R_PPC64_GOT_TLSLD16_HA:
17844 case R_PPC64_GOT_TPREL16_HA:
17845 case R_PPC64_GOT_DTPREL16_HA:
17846 /* Add 0x10000 if sign bit in 0:15 is set.
17847 Bits 0:15 are not used. */
17848 addend += 0x8000;
17849 break;
17851 case R_PPC64_D34_HA30:
17852 case R_PPC64_ADDR16_HIGHERA34:
17853 case R_PPC64_ADDR16_HIGHESTA34:
17854 case R_PPC64_REL16_HIGHERA34:
17855 case R_PPC64_REL16_HIGHESTA34:
17856 if (sec != NULL)
17857 addend += 1ULL << 33;
17858 break;
17860 case R_PPC64_ADDR16_DS:
17861 case R_PPC64_ADDR16_LO_DS:
17862 case R_PPC64_GOT16_DS:
17863 case R_PPC64_GOT16_LO_DS:
17864 case R_PPC64_PLT16_LO_DS:
17865 case R_PPC64_SECTOFF_DS:
17866 case R_PPC64_SECTOFF_LO_DS:
17867 case R_PPC64_TOC16_DS:
17868 case R_PPC64_TOC16_LO_DS:
17869 case R_PPC64_PLTGOT16_DS:
17870 case R_PPC64_PLTGOT16_LO_DS:
17871 case R_PPC64_GOT_TPREL16_DS:
17872 case R_PPC64_GOT_TPREL16_LO_DS:
17873 case R_PPC64_GOT_DTPREL16_DS:
17874 case R_PPC64_GOT_DTPREL16_LO_DS:
17875 case R_PPC64_TPREL16_DS:
17876 case R_PPC64_TPREL16_LO_DS:
17877 case R_PPC64_DTPREL16_DS:
17878 case R_PPC64_DTPREL16_LO_DS:
17879 if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17880 break;
17881 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17882 mask = 3;
17883 /* If this reloc is against an lq, lxv, or stxv insn, then
17884 the value must be a multiple of 16. This is somewhat of
17885 a hack, but the "correct" way to do this by defining _DQ
17886 forms of all the _DS relocs bloats all reloc switches in
17887 this file. It doesn't make much sense to use these
17888 relocs in data, so testing the insn should be safe. */
17889 if ((insn & (0x3fu << 26)) == (56u << 26)
17890 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
17891 mask = 15;
17892 relocation += addend;
17893 addend = insn & (mask ^ 3);
17894 if ((relocation & mask) != 0)
17896 relocation ^= relocation & mask;
17897 info->callbacks->einfo
17898 /* xgettext:c-format */
17899 (_("%H: error: %s not a multiple of %u\n"),
17900 input_bfd, input_section, rel->r_offset,
17901 ppc64_elf_howto_table[r_type]->name,
17902 mask + 1);
17903 bfd_set_error (bfd_error_bad_value);
17904 ret = false;
17905 goto copy_reloc;
17907 break;
17910 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17911 because such sections are not SEC_ALLOC and thus ld.so will
17912 not process them. */
17913 howto = ppc64_elf_howto_table[(int) r_type];
17914 if (unresolved_reloc
17915 && !((input_section->flags & SEC_DEBUGGING) != 0
17916 && h->elf.def_dynamic)
17917 && _bfd_elf_section_offset (output_bfd, info, input_section,
17918 rel->r_offset) != (bfd_vma) -1)
17920 info->callbacks->einfo
17921 /* xgettext:c-format */
17922 (_("%H: unresolvable %s against `%pT'\n"),
17923 input_bfd, input_section, rel->r_offset,
17924 howto->name,
17925 h->elf.root.root.string);
17926 ret = false;
17929 /* 16-bit fields in insns mostly have signed values, but a
17930 few insns have 16-bit unsigned values. Really, we should
17931 have different reloc types. */
17932 if (howto->complain_on_overflow != complain_overflow_dont
17933 && howto->dst_mask == 0xffff
17934 && (input_section->flags & SEC_CODE) != 0
17935 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17937 enum complain_overflow complain = complain_overflow_signed;
17939 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17940 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
17941 complain = complain_overflow_bitfield;
17942 else if (howto->rightshift == 0
17943 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17944 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17945 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17946 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17947 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17948 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
17949 complain = complain_overflow_unsigned;
17950 if (howto->complain_on_overflow != complain)
17952 alt_howto = *howto;
17953 alt_howto.complain_on_overflow = complain;
17954 howto = &alt_howto;
17958 switch (r_type)
17960 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17961 case R_PPC64_D34:
17962 case R_PPC64_D34_LO:
17963 case R_PPC64_D34_HI30:
17964 case R_PPC64_D34_HA30:
17965 case R_PPC64_PCREL34:
17966 case R_PPC64_GOT_PCREL34:
17967 case R_PPC64_TPREL34:
17968 case R_PPC64_DTPREL34:
17969 case R_PPC64_GOT_TLSGD_PCREL34:
17970 case R_PPC64_GOT_TLSLD_PCREL34:
17971 case R_PPC64_GOT_TPREL_PCREL34:
17972 case R_PPC64_GOT_DTPREL_PCREL34:
17973 case R_PPC64_PLT_PCREL34:
17974 case R_PPC64_PLT_PCREL34_NOTOC:
17975 case R_PPC64_D28:
17976 case R_PPC64_PCREL28:
17977 if (!offset_in_range (input_section, rel->r_offset, 8))
17978 r = bfd_reloc_outofrange;
17979 else
17981 relocation += addend;
17982 if (howto->pc_relative)
17983 relocation -= (rel->r_offset
17984 + input_section->output_offset
17985 + input_section->output_section->vma);
17986 relocation >>= howto->rightshift;
17988 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17989 pinsn <<= 32;
17990 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17992 pinsn &= ~howto->dst_mask;
17993 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17994 & howto->dst_mask);
17995 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17996 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17997 r = bfd_reloc_ok;
17998 if (howto->complain_on_overflow == complain_overflow_signed
17999 && (relocation + (1ULL << (howto->bitsize - 1))
18000 >= 1ULL << howto->bitsize))
18001 r = bfd_reloc_overflow;
18003 break;
18005 case R_PPC64_REL16DX_HA:
18006 if (!offset_in_range (input_section, rel->r_offset, 4))
18007 r = bfd_reloc_outofrange;
18008 else
18010 relocation += addend;
18011 relocation -= (rel->r_offset
18012 + input_section->output_offset
18013 + input_section->output_section->vma);
18014 relocation = (bfd_signed_vma) relocation >> 16;
18015 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
18016 insn &= ~0x1fffc1;
18017 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
18018 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
18019 r = bfd_reloc_ok;
18020 if (relocation + 0x8000 > 0xffff)
18021 r = bfd_reloc_overflow;
18023 break;
18025 default:
18026 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
18027 contents, rel->r_offset,
18028 relocation, addend);
18031 if (r != bfd_reloc_ok)
18033 char *more_info = NULL;
18034 const char *reloc_name = howto->name;
18036 if (reloc_dest != DEST_NORMAL)
18038 more_info = bfd_malloc (strlen (reloc_name) + 8);
18039 if (more_info != NULL)
18041 strcpy (more_info, reloc_name);
18042 strcat (more_info, (reloc_dest == DEST_OPD
18043 ? " (OPD)" : " (stub)"));
18044 reloc_name = more_info;
18048 if (r == bfd_reloc_overflow)
18050 /* On code like "if (foo) foo();" don't report overflow
18051 on a branch to zero when foo is undefined. */
18052 if (!warned
18053 && (reloc_dest == DEST_STUB
18054 || !(h != NULL
18055 && (h->elf.root.type == bfd_link_hash_undefweak
18056 || h->elf.root.type == bfd_link_hash_undefined)
18057 && is_branch_reloc (r_type))))
18058 info->callbacks->reloc_overflow
18059 (info, (struct bfd_link_hash_entry *) h, sym_name,
18060 reloc_name, orig_rel.r_addend, input_bfd, input_section,
18061 rel->r_offset);
18063 else
18065 info->callbacks->einfo
18066 /* xgettext:c-format */
18067 (_("%H: %s against `%pT': error %d\n"),
18068 input_bfd, input_section, rel->r_offset,
18069 reloc_name, sym_name, (int) r);
18070 ret = false;
18072 free (more_info);
18074 copy_reloc:
18075 if (wrel != rel)
18076 *wrel = *rel;
18079 if (wrel != rel)
18081 Elf_Internal_Shdr *rel_hdr;
18082 size_t deleted = rel - wrel;
18084 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
18085 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18086 if (rel_hdr->sh_size == 0)
18088 /* It is too late to remove an empty reloc section. Leave
18089 one NONE reloc.
18090 ??? What is wrong with an empty section??? */
18091 rel_hdr->sh_size = rel_hdr->sh_entsize;
18092 deleted -= 1;
18094 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
18095 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18096 input_section->reloc_count -= deleted;
18099 /* If we're emitting relocations, then shortly after this function
18100 returns, reloc offsets and addends for this section will be
18101 adjusted. Worse, reloc symbol indices will be for the output
18102 file rather than the input. Save a copy of the relocs for
18103 opd_entry_value. */
18104 if (is_opd
18105 && (info->emitrelocations || bfd_link_relocatable (info))
18106 && input_section->reloc_count != 0)
18108 bfd_size_type amt;
18109 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
18110 rel = bfd_alloc (input_bfd, amt);
18111 ppc64_elf_section_data (input_section)->u.opd.u.relocs = rel;
18112 if (rel == NULL)
18113 return false;
18114 memcpy (rel, relocs, amt);
18116 return ret;
18119 /* Adjust the value of any local symbols in opd sections. */
18121 static int
18122 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
18123 const char *name ATTRIBUTE_UNUSED,
18124 Elf_Internal_Sym *elfsym,
18125 asection *input_sec,
18126 struct elf_link_hash_entry *h)
18128 struct _opd_sec_data *opd;
18129 long adjust;
18130 bfd_vma value;
18132 if (h != NULL)
18133 return 1;
18135 opd = get_opd_info (input_sec);
18136 if (opd == NULL || opd->adjust == NULL)
18137 return 1;
18139 value = elfsym->st_value - input_sec->output_offset;
18140 if (!bfd_link_relocatable (info))
18141 value -= input_sec->output_section->vma;
18143 adjust = opd->adjust[OPD_NDX (value)];
18144 if (adjust == -1)
18145 return 2;
18147 elfsym->st_value += adjust;
18148 return 1;
18151 /* Finish up dynamic symbol handling. We set the contents of various
18152 dynamic sections here. */
18154 static bool
18155 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
18156 struct bfd_link_info *info,
18157 struct elf_link_hash_entry *h,
18158 Elf_Internal_Sym *sym)
18160 struct ppc_link_hash_table *htab;
18161 struct plt_entry *ent;
18163 htab = ppc_hash_table (info);
18165 if (!htab->opd_abi && !h->def_regular)
18166 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
18167 if (ent->plt.offset != (bfd_vma) -1)
18169 /* Mark the symbol as undefined, rather than as
18170 defined in glink. Leave the value if there were
18171 any relocations where pointer equality matters
18172 (this is a clue for the dynamic linker, to make
18173 function pointer comparisons work between an
18174 application and shared library), otherwise set it
18175 to zero. */
18176 sym->st_shndx = SHN_UNDEF;
18177 if (!h->pointer_equality_needed)
18178 sym->st_value = 0;
18179 else if (!h->ref_regular_nonweak)
18181 /* This breaks function pointer comparisons, but
18182 that is better than breaking tests for a NULL
18183 function pointer. */
18184 sym->st_value = 0;
18186 break;
18189 if (h->needs_copy
18190 && (h->root.type == bfd_link_hash_defined
18191 || h->root.type == bfd_link_hash_defweak)
18192 && (h->root.u.def.section == htab->elf.sdynbss
18193 || h->root.u.def.section == htab->elf.sdynrelro))
18195 /* This symbol needs a copy reloc. Set it up. */
18196 Elf_Internal_Rela rela;
18197 asection *srel;
18199 if (h->dynindx == -1)
18200 abort ();
18202 rela.r_offset = defined_sym_val (h);
18203 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
18204 rela.r_addend = 0;
18205 if (h->root.u.def.section == htab->elf.sdynrelro)
18206 srel = htab->elf.sreldynrelro;
18207 else
18208 srel = htab->elf.srelbss;
18209 BFD_ASSERT (count_and_swap_reloc_out (output_bfd, &rela, srel));
18212 return true;
18215 /* Used to decide how to sort relocs in an optimal manner for the
18216 dynamic linker, before writing them out. */
18218 static enum elf_reloc_type_class
18219 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
18220 const asection *rel_sec,
18221 const Elf_Internal_Rela *rela)
18223 enum elf_ppc64_reloc_type r_type;
18224 struct ppc_link_hash_table *htab = ppc_hash_table (info);
18226 if (rel_sec == htab->elf.irelplt)
18227 return reloc_class_ifunc;
18229 r_type = ELF64_R_TYPE (rela->r_info);
18230 switch (r_type)
18232 case R_PPC64_RELATIVE:
18233 return reloc_class_relative;
18234 case R_PPC64_JMP_SLOT:
18235 return reloc_class_plt;
18236 case R_PPC64_COPY:
18237 return reloc_class_copy;
18238 default:
18239 return reloc_class_normal;
18243 /* Finish up the dynamic sections. */
18245 static bool
18246 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
18247 struct bfd_link_info *info)
18249 struct ppc_link_hash_table *htab;
18250 bfd *dynobj;
18251 asection *sdyn;
18253 htab = ppc_hash_table (info);
18254 if (htab == NULL)
18255 return false;
18257 dynobj = htab->elf.dynobj;
18258 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
18260 if (htab->elf.dynamic_sections_created)
18262 Elf64_External_Dyn *dyncon, *dynconend;
18264 if (sdyn == NULL || htab->elf.sgot == NULL)
18265 abort ();
18267 dyncon = (Elf64_External_Dyn *) sdyn->contents;
18268 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
18269 for (; dyncon < dynconend; dyncon++)
18271 Elf_Internal_Dyn dyn;
18272 asection *s;
18274 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
18276 switch (dyn.d_tag)
18278 default:
18279 continue;
18281 case DT_PPC64_GLINK:
18282 s = htab->glink;
18283 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18284 /* We stupidly defined DT_PPC64_GLINK to be the start
18285 of glink rather than the first entry point, which is
18286 what ld.so needs, and now have a bigger stub to
18287 support automatic multiple TOCs. */
18288 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
18289 break;
18291 case DT_PPC64_OPD:
18292 s = bfd_get_section_by_name (output_bfd, ".opd");
18293 if (s == NULL)
18294 continue;
18295 dyn.d_un.d_ptr = s->vma;
18296 break;
18298 case DT_PPC64_OPT:
18299 if ((htab->do_multi_toc && htab->multi_toc_needed)
18300 || htab->notoc_plt)
18301 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
18302 if (htab->has_plt_localentry0)
18303 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
18304 break;
18306 case DT_PPC64_OPDSZ:
18307 s = bfd_get_section_by_name (output_bfd, ".opd");
18308 if (s == NULL)
18309 continue;
18310 dyn.d_un.d_val = s->size;
18311 break;
18313 case DT_PLTGOT:
18314 s = htab->elf.splt;
18315 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18316 break;
18318 case DT_JMPREL:
18319 s = htab->elf.srelplt;
18320 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18321 break;
18323 case DT_PLTRELSZ:
18324 dyn.d_un.d_val = htab->elf.srelplt->size;
18325 break;
18327 case DT_TEXTREL:
18328 if (htab->elf.ifunc_resolvers)
18329 info->callbacks->einfo
18330 (_("%P: warning: text relocations and GNU indirect "
18331 "functions may result in a segfault at runtime\n"));
18332 continue;
18335 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
18339 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
18340 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
18342 /* Fill in the first entry in the global offset table.
18343 We use it to hold the link-time TOCbase. */
18344 bfd_put_64 (output_bfd,
18345 elf_gp (output_bfd) + TOC_BASE_OFF,
18346 htab->elf.sgot->contents);
18348 /* Set .got entry size. */
18349 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
18350 = 8;
18353 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
18354 && htab->elf.splt->output_section != bfd_abs_section_ptr)
18356 /* Set .plt entry size. */
18357 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
18358 = PLT_ENTRY_SIZE (htab);
18361 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
18362 brlt ourselves if emitrelocations. */
18363 if (htab->brlt != NULL
18364 && htab->brlt->reloc_count != 0
18365 && !_bfd_elf_link_output_relocs (output_bfd,
18366 htab->brlt,
18367 elf_section_data (htab->brlt)->rela.hdr,
18368 elf_section_data (htab->brlt)->relocs,
18369 NULL))
18370 return false;
18372 if (htab->glink != NULL
18373 && htab->glink->reloc_count != 0
18374 && !_bfd_elf_link_output_relocs (output_bfd,
18375 htab->glink,
18376 elf_section_data (htab->glink)->rela.hdr,
18377 elf_section_data (htab->glink)->relocs,
18378 NULL))
18379 return false;
18382 if (htab->glink_eh_frame != NULL
18383 && htab->glink_eh_frame->size != 0
18384 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
18385 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
18386 htab->glink_eh_frame,
18387 htab->glink_eh_frame->contents))
18388 return false;
18390 /* We need to handle writing out multiple GOT sections ourselves,
18391 since we didn't add them to DYNOBJ. We know dynobj is the first
18392 bfd. */
18393 while ((dynobj = dynobj->link.next) != NULL)
18395 asection *s;
18397 if (!is_ppc64_elf (dynobj))
18398 continue;
18400 s = ppc64_elf_tdata (dynobj)->got;
18401 if (s != NULL
18402 && s->size != 0
18403 && s->output_section != bfd_abs_section_ptr
18404 && !bfd_set_section_contents (output_bfd, s->output_section,
18405 s->contents, s->output_offset,
18406 s->size))
18407 return false;
18408 s = ppc64_elf_tdata (dynobj)->relgot;
18409 if (s != NULL
18410 && s->size != 0
18411 && s->output_section != bfd_abs_section_ptr
18412 && !bfd_set_section_contents (output_bfd, s->output_section,
18413 s->contents, s->output_offset,
18414 s->size))
18415 return false;
18418 return true;
18421 static bool
18422 ppc64_elf_free_cached_info (bfd *abfd)
18424 if (abfd->sections)
18425 for (asection *opd = bfd_get_section_by_name (abfd, ".opd");
18426 opd != NULL;
18427 opd = bfd_get_next_section_by_name (NULL, opd))
18428 if (opd->reloc_count == 0)
18429 free (ppc64_elf_section_data (opd)->u.opd.u.contents);
18431 return _bfd_elf_free_cached_info (abfd);
18434 #include "elf64-target.h"
18436 /* FreeBSD support */
18438 #undef TARGET_LITTLE_SYM
18439 #define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
18440 #undef TARGET_LITTLE_NAME
18441 #define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
18443 #undef TARGET_BIG_SYM
18444 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
18445 #undef TARGET_BIG_NAME
18446 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
18448 #undef ELF_OSABI
18449 #define ELF_OSABI ELFOSABI_FREEBSD
18451 #undef elf64_bed
18452 #define elf64_bed elf64_powerpc_fbsd_bed
18454 #include "elf64-target.h"